/[mdap]/mdap-server.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /mdap-server.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 8 by dpavlin, Sun Apr 22 16:06:03 2007 UTC revision 15 by dpavlin, Mon Apr 23 00:04:26 2007 UTC
# Line 9  use constant PORT  => '3235'; Line 9  use constant PORT  => '3235';
9    
10  my $debug = shift @ARGV;  my $debug = shift @ARGV;
11    
 my $local_port = 1000;  
   
12  my $resend_search_delay = 3;  my $resend_search_delay = 3;
13  my $tftp_path = '/srv/tftp/';  my $tftp_path = '/srv/tftp/';
14    
15    my $user_id = 'Administrator';
16    my $user_pwd = '';
17    
18    warn "search for ants every ${resend_search_delay}s\ntftp server path: $tftp_path\n";
19    
20  sub fw {  sub fw {
21          my ($board, $offset,$len) = @_;          my ($board, $offset,$len) = @_;
22          open(my $fh, "$tftp_path/$board") || die "Can't open image $tftp_path/$board: $!";          open(my $fh, "$tftp_path/$board") || die "Can't open image $tftp_path/$board: $!";
# Line 54  sub ant2hash { Line 57  sub ant2hash {
57    
58  sub mdap_send {  sub mdap_send {
59          my $data = shift;          my $data = shift;
60          warn ">> $data\n>>----------\n" if ($debug);  
61          $sock->mcast_send( "${data}", GROUP . ':' . PORT );          my $xor = 0;
62            map { $xor ^= ord($_) } split(//,$data);
63            $data .= sprintf('%02X', $xor);
64    
65            $sock->mcast_send( $data, GROUP . ':' . PORT );
66            if ($debug) {
67                    warn ">> ", dump( $data ), $/;
68            } else {
69                    $data =~ s/\s+/ /gi;
70                    warn ">> ", substr($data,0,70), $/;
71            }
72  }  }
73    
74  local $SIG{ALRM} = sub {  local $SIG{ALRM} = sub {
75          mdap_send("ANT-SEARCH MDAP/1.1\r\n46");          mdap_send("ANT-SEARCH MDAP/1.1\r\n");
76          alarm( $resend_search_delay );          alarm( $resend_search_delay );
77  };  };
78    
79  alarm( $resend_search_delay );  alarm( $resend_search_delay );
80    
81  mdap_send("ANT-SEARCH MDAP/1.1\r\n46");  mdap_send("ANT-SEARCH MDAP/1.1\r\n");
82    
83  while (1) {  while (1) {
84          my $data;          my $data;
85          next unless $sock->recv($data,1024);          next unless $sock->recv($data,1024);
86    
87          if ( $data =~ m#^(REPLY-\S+)\s(MDAP/\d+\.\d+)# ) {          if ( $data =~ m#^(INFO|ANT-SEARCH|EXEC-CLI|REPLY-\S+)\s(MDAP)/(\d+\.\d+)# ) {
88    
89                  my ($type,$proto) = ($1,$2);                  my ($type,$proto,$mdap_ver) = ($1,$2,$3);
90    
91                  my $h = ant2hash($data);                  my $h = ant2hash($data);
92    
93                  my $ant = $h->{'ANT-ID'} || die "no ANT-ID in ", dump( $h );                  my $client_version = $h->{'MDAP-VERSION'};
94                    $mdap_ver = $client_version if ($client_version);
95    
96                  print "<< $type $proto ", length($data), " bytes\n";                  print "<< $type $proto/$mdap_ver << ", length($data), " bytes\n";
97    
98                  warn dump($h),$/ if ($debug);                  warn dump($h),$/ if ($debug);
99    
100                    # we are getting our own INFO messages
101                    next if ( $type =~ m#^(INFO|ANT-SEARCH|EXEC-CLI)# );
102    
103                    my $ant = $h->{'ANT-ID'} || die "no ANT-ID in ", dump( $h );
104    
105                  if ($type eq 'REPLY-ANT-SEARCH') {                  if ($type eq 'REPLY-ANT-SEARCH') {
106                          mdap_send("INFO MDAP/1.2\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:Administrator\r\nUSER-PWD:\r\n22");                          mdap_send("INFO MDAP/$mdap_ver\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n");
107                  } elsif ($type eq 'REPLY-INFO') {                  } elsif ($type eq 'REPLY-INFO') {
108    
109                            if ($h->{'SEQ-NR'} < 0) {
110                                    warn "!! password protected ant $ant, skipping\n";
111                                    next;
112                            }
113    
114                          my $board = $h->{'_BOARD_NAME'} || die "no _BOARD_NAME?";                          my $board = $h->{'_BOARD_NAME'} || die "no _BOARD_NAME?";
115                          if ( fw_exists( $board ) ) {                          if ( fw_exists( $board ) ) {
116                                  if ( $h->{'_BUILD'} ne fw_build( $board ) ) {                                  my $build = $h->{'_BUILD'} || die "no _BUILD?";
117                                          print "UPDATE STEP 1 on ant $ant\n";                                  my $new_build = fw_build( $board );
118                                          mdap_send("EXEC-CLI MDAP/1.2\r\nCLI-CMD:software upgrade\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:Administrator\r\nUSER-PWD:\r\n1F");                                  if ( $build ne $new_build ) {
119                                            print "UPDATE STEP 1 on ant $ant version $build -> $new_build\n";
120                                            mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nCLI-CMD:software upgrade\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n");
121                                  } else {                                  } else {
122                                          print "OK ant $ant allready updated...\n";                                          print "OK ant $ant allready updated...\n";
123                                  }                                  }
# Line 99  while (1) { Line 126  while (1) {
126                          }                          }
127                  } elsif ( $type eq 'REPLY-EXEC-CLI' && $h->{'SEQ-NR'} == 1 ) {                  } elsif ( $type eq 'REPLY-EXEC-CLI' && $h->{'SEQ-NR'} == 1 ) {
128                                  print "UPDATE STEP 2 on ant $ant\n";                                  print "UPDATE STEP 2 on ant $ant\n";
129                                  mdap_send("EXEC-CLI MDAP/1.2\r\nSEQ-NR:2\r\nTO-ANT:$ant\r\nUSER-ID:Administrator\r\nUSER-PWD:\r\n5F");                                  mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nSEQ-NR:2\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n");
130                  } else {                  } else {
131                          print "!! reply ignored ", dump( $h ), $/;                          print "!! reply ignored ", dump( $h ), $/;
132                  }                  }

Legend:
Removed from v.8  
changed lines
  Added in v.15

  ViewVC Help
Powered by ViewVC 1.1.26