00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 #include <stdio.h>
00072
00073 #include "contiki-net.h"
00074 #include "cfs/cfs.h"
00075 #include "codeprop-otf.h"
00076 #include "loader/elfloader-otf.h"
00077 #include <string.h>
00078
00079 static const char *err_msgs[] =
00080 {"OK\r\n", "Bad ELF header\r\n", "No symtab\r\n", "No strtab\r\n",
00081 "No text\r\n", "Symbol not found\r\n", "Segment not found\r\n",
00082 "No startpoint\r\n", "Unhandled relocation\r\n",
00083 "Relocation out of range\r\n", "Relocations not sorted\r\n",
00084 "Input error\r\n" , "Ouput error\r\n" };
00085
00086 #define CODEPROP_DATA_PORT 6510
00087
00088
00089
00090 #if 0
00091 #define PRINTF(x) printf x
00092 #else
00093 #define PRINTF(x)
00094 #endif
00095
00096 #define START_TIMEOUT 12 * CLOCK_SECOND
00097 #define MISS_NACK_TIMEOUT (CLOCK_SECOND / 8) * (random_rand() % 8)
00098 #define HIT_NACK_TIMEOUT (CLOCK_SECOND / 8) * (8 + random_rand() % 16)
00099 #define NACK_REXMIT_TIMEOUT CLOCK_SECOND * (4 + random_rand() % 4)
00100
00101 #define WAITING_TIME CLOCK_SECOND * 10
00102
00103 #define NUM_SEND_DUPLICATES 2
00104
00105 #define UDPHEADERSIZE 8
00106 #define UDPDATASIZE 32
00107
00108 struct codeprop_udphdr {
00109 u16_t id;
00110 u16_t type;
00111 #define TYPE_DATA 0x0001
00112 #define TYPE_NACK 0x0002
00113 u16_t addr;
00114 u16_t len;
00115 u8_t data[UDPDATASIZE];
00116 };
00117
00118 struct codeprop_tcphdr {
00119 u16_t len;
00120 u16_t pad;
00121 };
00122
00123 static void uipcall(void *state);
00124
00125 PROCESS(codeprop_process, "Code propagator");
00126
00127 struct codeprop_state {
00128 u8_t state;
00129 #define STATE_NONE 0
00130 #define STATE_RECEIVING_TCPDATA 1
00131 #define STATE_RECEIVING_UDPDATA 2
00132 #define STATE_SENDING_UDPDATA 3
00133 u16_t count;
00134 u16_t addr;
00135 u16_t len;
00136 u16_t id;
00137 struct etimer sendtimer;
00138 struct timer nacktimer, timer, starttimer;
00139 u8_t received;
00140 u8_t send_counter;
00141 struct pt tcpthread_pt;
00142 struct pt udpthread_pt;
00143 struct pt recv_udpthread_pt;
00144 };
00145
00146 static int fd;
00147
00148 static struct uip_udp_conn *udp_conn;
00149
00150 static struct codeprop_state s;
00151
00152 void system_log(char *msg);
00153
00154 static clock_time_t send_time;
00155
00156 #define CONNECTION_TIMEOUT (30 * CLOCK_SECOND)
00157
00158
00159 void
00160 codeprop_set_rate(clock_time_t time)
00161 {
00162 send_time = time;
00163 }
00164
00165 PROCESS_THREAD(codeprop_process, ev, data)
00166 {
00167 PROCESS_BEGIN();
00168
00169 elfloader_init();
00170
00171 s.id = 0;
00172
00173 send_time = CLOCK_SECOND/4;
00174
00175 PT_INIT(&s.udpthread_pt);
00176 PT_INIT(&s.recv_udpthread_pt);
00177
00178 tcp_listen(UIP_HTONS(CODEPROP_DATA_PORT));
00179
00180 udp_conn = udp_broadcast_new(UIP_HTONS(CODEPROP_DATA_PORT), NULL);
00181
00182 s.state = STATE_NONE;
00183 s.received = 0;
00184 s.addr = 0;
00185 s.len = 0;
00186
00187 fd = cfs_open("codeprop-image", CFS_READ | CFS_WRITE);
00188
00189 while(1) {
00190
00191 PROCESS_YIELD();
00192
00193 if(ev == tcpip_event) {
00194 uipcall(data);
00195 } else if(ev == PROCESS_EVENT_TIMER) {
00196 tcpip_poll_udp(udp_conn);
00197 }
00198 }
00199
00200 PROCESS_END();
00201 }
00202
00203 static u16_t
00204 send_udpdata(struct codeprop_udphdr *uh)
00205 {
00206 u16_t len;
00207
00208 uh->type = UIP_HTONS(TYPE_DATA);
00209 uh->addr = uip_htons(s.addr);
00210 uh->id = uip_htons(s.id);
00211
00212 if(s.len - s.addr > UDPDATASIZE) {
00213 len = UDPDATASIZE;
00214 } else {
00215 len = s.len - s.addr;
00216 }
00217
00218 cfs_seek(fd, s.addr, CFS_SEEK_SET);
00219 cfs_read(fd, (char*)&uh->data[0], len);
00220
00221
00222
00223 uh->len = uip_htons(s.len);
00224
00225 PRINTF(("codeprop: sending packet from address 0x%04x\n", s.addr));
00226 uip_udp_send(len + UDPHEADERSIZE);
00227
00228 return len;
00229 }
00230
00231 static
00232 PT_THREAD(send_udpthread(struct pt *pt))
00233 {
00234 int len;
00235 struct codeprop_udphdr *uh = (struct codeprop_udphdr *)uip_appdata;
00236
00237
00238 PT_BEGIN(pt);
00239
00240 while(1) {
00241 PT_WAIT_UNTIL(pt, s.state == STATE_SENDING_UDPDATA);
00242
00243 for(s.addr = 0; s.addr < s.len; ) {
00244 len = send_udpdata(uh);
00245 s.addr += len;
00246
00247 etimer_set(&s.sendtimer, CLOCK_SECOND/4);
00248 do {
00249 PT_WAIT_UNTIL(pt, uip_newdata() || etimer_expired(&s.sendtimer));
00250
00251 if(uip_newdata()) {
00252 if(uh->type == UIP_HTONS(TYPE_NACK)) {
00253 PRINTF(("send_udpthread: got NACK for address 0x%x (now 0x%x)\n",
00254 uip_htons(uh->addr), s.addr));
00255
00256 if(uip_htons(uh->addr) <= s.addr) {
00257
00258 s.addr = uip_htons(uh->addr);
00259 }
00260 }
00261 PT_YIELD(pt);
00262 }
00263 } while(!etimer_expired(&s.sendtimer));
00264 }
00265
00266 s.state = STATE_NONE;
00267
00268
00269 }
00270 PT_END(pt);
00271 }
00272
00273 static void
00274 send_nack(struct codeprop_udphdr *uh, unsigned short addr)
00275 {
00276 uh->type = UIP_HTONS(TYPE_NACK);
00277 uh->addr = uip_htons(addr);
00278 uip_udp_send(UDPHEADERSIZE);
00279 }
00280
00281 static
00282 PT_THREAD(recv_udpthread(struct pt *pt))
00283 {
00284 int len;
00285 struct codeprop_udphdr *uh = (struct codeprop_udphdr *)uip_appdata;
00286
00287
00288
00289
00290
00291 PT_BEGIN(pt);
00292
00293 while(1) {
00294
00295 do {
00296 PT_WAIT_UNTIL(pt, uip_newdata() &&
00297 uh->type == UIP_HTONS(TYPE_DATA) &&
00298 uip_htons(uh->id) > s.id);
00299
00300 if(uip_htons(uh->addr) != 0) {
00301 s.addr = 0;
00302 send_nack(uh, 0);
00303 }
00304
00305 } while(uip_htons(uh->addr) != 0);
00306
00307
00308
00309
00310 s.addr = 0;
00311 s.id = uip_htons(uh->id);
00312 s.len = uip_htons(uh->len);
00313
00314 timer_set(&s.timer, CONNECTION_TIMEOUT);
00315
00316
00317 while(s.addr < s.len) {
00318
00319 if(uip_htons(uh->addr) == s.addr) {
00320
00321 len = uip_datalen() - UDPHEADERSIZE;
00322 if(len > 0) {
00323
00324
00325 cfs_seek(fd, s.addr, CFS_SEEK_SET);
00326 cfs_write(fd, (char*)&uh->data[0], len);
00327
00328
00329 PRINTF(("Saved %d bytes at address %d, %d bytes left\n",
00330 uip_datalen() - UDPHEADERSIZE, s.addr,
00331 s.len - s.addr));
00332
00333 s.addr += len;
00334 }
00335
00336 } else if(uip_htons(uh->addr) > s.addr) {
00337 PRINTF(("sending nack since 0x%x != 0x%x\n", uip_htons(uh->addr), s.addr));
00338 send_nack(uh, s.addr);
00339 }
00340
00341 if(s.addr < s.len) {
00342
00343
00344
00345 do {
00346 timer_set(&s.nacktimer, HIT_NACK_TIMEOUT);
00347 PT_YIELD_UNTIL(pt, timer_expired(&s.nacktimer) ||
00348 (uip_newdata() &&
00349 uh->type == UIP_HTONS(TYPE_DATA) &&
00350 uip_htons(uh->id) == s.id));
00351 if(timer_expired(&s.nacktimer)) {
00352 send_nack(uh, s.addr);
00353 }
00354 } while(timer_expired(&s.nacktimer));
00355 }
00356
00357 }
00358
00359
00360
00361
00362 codeprop_start_program();
00363 PT_EXIT(pt);
00364 }
00365
00366 PT_END(pt);
00367 }
00368
00369
00370 #define CODEPROP_TCPHDR_SIZE sizeof(struct codeprop_tcphdr)
00371
00372 static
00373 PT_THREAD(recv_tcpthread(struct pt *pt))
00374 {
00375 struct codeprop_tcphdr *th;
00376 int datalen = uip_datalen();
00377 PT_BEGIN(pt);
00378
00379 while(1) {
00380
00381 PT_WAIT_UNTIL(pt, uip_connected());
00382
00383 codeprop_exit_program();
00384
00385 s.state = STATE_RECEIVING_TCPDATA;
00386
00387 s.addr = 0;
00388 s.count = 0;
00389
00390
00391 PT_WAIT_UNTIL(pt, uip_newdata() && uip_datalen() > 0);
00392
00393 if(uip_datalen() < CODEPROP_TCPHDR_SIZE) {
00394 PRINTF(("codeprop: header not found in first tcp segment\n"));
00395 uip_abort();
00396 }
00397 th = (struct codeprop_tcphdr *)uip_appdata;
00398 s.len = uip_htons(th->len);
00399 s.addr = 0;
00400 uip_appdata += CODEPROP_TCPHDR_SIZE;
00401 datalen -= CODEPROP_TCPHDR_SIZE;
00402
00403
00404 do {
00405 if(datalen > 0) {
00406
00407
00408 if (cfs_seek(fd, s.addr, CFS_SEEK_SET) != s.addr) {
00409 PRINTF(("codeprop: seek in buffer file failed\n"));
00410 uip_abort();
00411 }
00412
00413 if (cfs_write(fd, uip_appdata, datalen) != datalen) {
00414 PRINTF(("codeprop: write to buffer file failed\n"));
00415 uip_abort();
00416 }
00417 s.addr += datalen;
00418 }
00419 if(s.addr < s.len) {
00420 PT_YIELD_UNTIL(pt, uip_newdata());
00421 }
00422 } while(s.addr < s.len);
00423 #if 1
00424
00425 {
00426 static int err;
00427
00428 err = codeprop_start_program();
00429
00430
00431 do {
00432 if (err >= 0 && err < sizeof(err_msgs)/sizeof(char*)) {
00433 uip_send(err_msgs[err], strlen(err_msgs[err]));
00434 } else {
00435 uip_send("Unknown error\r\n", 15);
00436 }
00437 PT_WAIT_UNTIL(pt, uip_acked() || uip_rexmit() || uip_closed());
00438 } while(uip_rexmit());
00439
00440
00441 uip_close();
00442 }
00443 #endif
00444 ++s.id;
00445 s.state = STATE_SENDING_UDPDATA;
00446 tcpip_poll_udp(udp_conn);
00447
00448 PT_WAIT_UNTIL(pt, s.state != STATE_SENDING_UDPDATA);
00449
00450 }
00451
00452 PT_END(pt);
00453 }
00454
00455 void
00456 codeprop_start_broadcast(unsigned int len)
00457 {
00458 s.addr = 0;
00459 s.len = len;
00460 ++s.id;
00461 s.state = STATE_SENDING_UDPDATA;
00462 tcpip_poll_udp(udp_conn);
00463 }
00464
00465 void
00466 codeprop_exit_program(void)
00467 {
00468 if(elfloader_autostart_processes != NULL) {
00469 autostart_exit(elfloader_autostart_processes);
00470 }
00471 }
00472
00473 int
00474 codeprop_start_program(void)
00475 {
00476 int err;
00477
00478 codeprop_exit_program();
00479
00480 err = elfloader_load(fd, codeprop_output);
00481 if(err == ELFLOADER_OK) {
00482 PRINTF(("codeprop: starting %s\n",
00483 elfloader_autostart_processes[0]->name));
00484 autostart_start(elfloader_autostart_processes);
00485 }
00486 return err;
00487 }
00488
00489 static void
00490 uipcall(void *state)
00491 {
00492 if(uip_udpconnection()) {
00493 recv_udpthread(&s.recv_udpthread_pt);
00494 send_udpthread(&s.udpthread_pt);
00495 } else {
00496 if(uip_conn->lport == UIP_HTONS(CODEPROP_DATA_PORT)) {
00497 if(uip_connected()) {
00498
00499 if(state == NULL) {
00500 s.addr = 0;
00501 s.count = 0;
00502 PT_INIT(&s.tcpthread_pt);
00503 process_poll(&codeprop_process);
00504 tcp_markconn(uip_conn, &s);
00505
00506
00507 } else {
00508 PRINTF(("codeprop: uip_connected() and state != NULL\n"));
00509 uip_abort();
00510 }
00511 }
00512 recv_tcpthread(&s.tcpthread_pt);
00513
00514
00515 if(uip_closed() || uip_aborted() || uip_timedout()) {
00516 PRINTF(("codeprop: connection down\n"));
00517 tcp_markconn(uip_conn, NULL);
00518 }
00519 }
00520 }
00521 }
00522
00523