/*---------------------------------------------------------------------------*/ static void handle_incoming_rerr(void) { struct uaodv_msg_rerr *rm = (struct uaodv_msg_rerr *)uip_appdata; struct uaodv_rt_entry *rt; print_debug("RERR %d.%d.%d.%d -> %d.%d.%d.%d" " unreach=%d.%d.%d.%d seq=%lu\n", uip_ipaddr_to_quad(&BUF->srcipaddr), uip_ipaddr_to_quad(&BUF->destipaddr), uip_ipaddr_to_quad((uip_ipaddr_t *)&rm->unreach[0]), uip_ntohl(rm->unreach[0].seqno)); if(uip_ipaddr_cmp(&rm->unreach[0].addr, &uip_hostaddr)) return; rt = uaodv_rt_lookup_any(&rm->unreach[0].addr); if(rt != NULL && uip_ipaddr_cmp(&rt->nexthop, uip_udp_sender())) { if((rm->flags & UAODV_RERR_UNKNOWN) || rm->unreach[0].seqno == 0 || SCMP32(rt->hseqno, uip_ntohl(rm->unreach[0].seqno)) <= 0) { rt->is_bad = 1; if(rm->flags & UAODV_RERR_UNKNOWN) { rm->flags &= ~UAODV_RERR_UNKNOWN; rm->unreach[0].seqno = uip_htonl(rt->hseqno); } print_debug("RERR rebroadcast\n"); uip_udp_packet_send(bcastconn, rm, sizeof(struct uaodv_msg_rerr)); } } }
static void send_rreq(uip_ipaddr_t *addr) { struct uaodv_msg_rreq *rm = (struct uaodv_msg_rreq *)uip_appdata; int len; print_debug("send RREQ for %d.%d.%d.%d\n", uip_ipaddr_to_quad(addr)); rm->type = UAODV_RREQ_TYPE; rm->dest_seqno = last_known_seqno(addr); if(rm->dest_seqno == 0) rm->flags = UAODV_RREQ_UNKSEQNO; else rm->flags = 0; rm->reserved = 0; rm->hop_count = 0; rm->rreq_id = uip_htonl(rreq_id++); uip_ipaddr_copy(&rm->dest_addr, addr); uip_gethostaddr(&rm->orig_addr); my_hseqno++; /* Always */ rm->orig_seqno = uip_htonl(my_hseqno); bcastconn->ttl = MY_NET_DIAMETER; len = sizeof(struct uaodv_msg_rreq); len += add_rreq_extensions(rm + 1); uip_udp_packet_send(bcastconn, rm, len); }
void setup_usb(void) { uip_ipaddr_t hostaddr, netmask, defrouter; uip_ipaddr(&hostaddr, 10,0,0,2); uip_ipaddr(&netmask, 255,0,0,0); uip_ipaddr(&defrouter, 10,0,0,1); uip_sethostaddr(&hostaddr); uip_setnetmask(&netmask); uip_setdraddr(&defrouter); /* uip_over_mesh_set_net(&hostaddr, &netmask); uip_fw_register(&slipif); uip_over_mesh_set_gateway_netif(&slipif); uip_fw_default(&meshif); uip_over_mesh_init(UIP_OVER_MESH_CHANNEL); */ printf("uIP started with IP address %d.%d.%d.%d\n", uip_ipaddr_to_quad(&hostaddr)); #ifdef WITH_USB printf("Setting up USB\n"); usb_setup(); printf("usb setup done\n"); usb_cdc_eth_set_ifaddr(&hostaddr); usb_cdc_eth_setup(); #endif printf("cdc_eth setup done\n"); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(server_process, ev, data) { PROCESS_BEGIN(); u16_t hostport = 7777; printf("TCP listen on %d.%d.%d.%d:%u\n", uip_ipaddr_to_quad(&uip_hostaddr), hostport); tcp_listen(uip_htons(hostport)); while(1) { PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event); if (uip_connected()) { printf("server: client arrived!\n"); connected = 1; while (!(uip_aborted() || uip_closed() || uip_timedout())) { PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event); } if (uip_aborted() || uip_closed()) { connected = 0; } } else { printf("server: client not accepted!\n"); KLEENET_ASSERT(0 && "server: client not accepted!\n"); } } PROCESS_END(); }
static unsigned add_rreq_extensions(void *_p) { struct uaodv_bad_hop_ext *p = _p; uip_ipaddr_t *a = p->addrs; unsigned i, n; #define SCALE_RETRANS_THRESHOLD (3*4) cc2420_check_remote(0xffff); /* Age table. */ n = 0; for (i = 0; i < NNEIGBOURS; i++) { if (neigbours[i].nretrans >= SCALE_RETRANS_THRESHOLD && neigbours[i].mac != 0xffff) { a->u16[0] = uip_hostaddr.u16[0]; a->u16[1] = neigbours[i].mac; n++; if(n == 15) break; /* Avoid buffer overrun */ print_debug("BAD HOP %d.%d.%d.%d\t%d\n", uip_ipaddr_to_quad(a), neigbours[i].nretrans); } } if(n == 0) return 0; p->type = RREQ_BAD_HOP_EXT; p->length = 2 + 4*n; /* Two unused bytes + addresses */ return 2 + p->length; /* Type + len + extension data */ }
/*---------------------------------------------------------------------*/ PROCESS_THREAD(test_uaodv_process, ev, data) { static uip_ipaddr_t addr; PROCESS_BEGIN(); printf("uIP uAODV test process started\n"); uip_ipaddr(&addr, 0,0,0,0); in_conn = udp_new(&addr, UIP_HTONS(0), NULL); uip_udp_bind(in_conn, UIP_HTONS(COOJA_PORT)); uip_ipaddr(&addr, 10,10,10,4); out_conn = udp_new(&addr, UIP_HTONS(COOJA_PORT), NULL); button_sensor.configure(SENSORS_ACTIVE, 1); while(1) { PROCESS_WAIT_EVENT(); if(ev == sensors_event && data == &button_sensor) { struct uaodv_rt_entry *route; uip_ipaddr(&addr, 10,10,10,4); route = uaodv_rt_lookup_any(&addr); if (route == NULL || route->is_bad) { printf("%d.%d.%d.%d: lookup %d.%d.%d.%d\n", uip_ipaddr_to_quad(&uip_hostaddr), uip_ipaddr_to_quad(&addr)); uaodv_request_route_to(&addr); } else { printf("%d.%d.%d.%d: send to %d.%d.%d.%d\n", uip_ipaddr_to_quad(&uip_hostaddr), uip_ipaddr_to_quad(&addr)); tcpip_poll_udp(out_conn); PROCESS_WAIT_UNTIL(ev == tcpip_event && uip_poll()); uip_send("cooyah COOJA", 12); } } if(ev == tcpip_event && uip_newdata()) { ((char*) uip_appdata)[uip_datalen()] = 0; printf("data received from %d.%d.%d.%d: %s\n", uip_ipaddr_to_quad(&((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])->srcipaddr), (char *)uip_appdata); leds_toggle(LEDS_ALL); } } PROCESS_END(); }
void network_handler(ev, data){ char buf[UIP_BUFSIZE]; // packet data buffer unsigned short cmd; // DataPayload *dp; ChannelState *state = NULL; uint16_t len = uip_datalen(); PRINTF("ipaddr=%d.%d.%d.%d\n", uip_ipaddr_to_quad(&(UDP_HDR->srcipaddr))); PRINTF("Packet is %d bytes long\n",len); memcpy(buf, uip_appdata, len); buf[len] = '\0'; dp = (DataPayload *)buf; PRINTF("Data is %d bytes long\n",uip_ntohs(dp->dhdr.tlen)); cmd = dp->hdr.cmd; // only a byte so no reordering :) PRINTF("Received a %s command.\n", cmdnames[cmd]); PRINTF("Message for channel %d\n",dp->hdr.dst_chan_num); if (dp->hdr.dst_chan_num == HOMECHANNEL || cmd == DISCONNECT){ if (cmd == QACK){ state = &home_channel_state; copy_link_address(state); } else if (cmd == DISCONNECT){ state = get_channel_state(dp->hdr.dst_chan_num); if (state){ remove_channel(state->chan_num); } state = &home_channel_state; copy_link_address(state); } } else{ state = get_channel_state(dp->hdr.dst_chan_num); if (state == NULL){ PRINTF("Channel %d doesn't exist\n", dp->hdr.dst_chan_num); return; } if (check_seqno(state, dp) == 0) { printf("OH NOES\n"); return; }else { //CHECK IF RIGHT CONNECTION //copy_link_address(state); } } // Received a message so reset pingOUT state->pingOUT = 0; if (cmd == QUERY) PRINTF("I'm a controller, Ignoring QUERY\n"); else if (cmd == CONNECT) PRINTF("I'm a controller, Ignoring CONNECT\n"); else if (cmd == QACK) qack_handler(state, dp); else if (cmd == CACK) cack_handler(state, dp); else if (cmd == RESPONSE) response_handler(state, dp); else if (cmd == CMDACK) command_ack_handler(state,dp); else if (cmd == PING) ping_handler(state, dp); else if (cmd == PACK) pack_handler(state, dp); else if (cmd == DISCONNECT) close_handler(state,dp); }
/* Compare sequence numbers as per RFC 3561. */ #define SCMP32(a, b) ((int32_t)((a) - (b))) static CC_INLINE uint32_t last_known_seqno(uip_ipaddr_t *host) { struct uaodv_rt_entry *route = uaodv_rt_lookup_any(host); if(route != NULL) return uip_htonl(route->hseqno); return 0; } static uint32_t rreq_id, my_hseqno; /* In host byte order! */ #define NFWCACHE 16 static struct { uip_ipaddr_t orig; uint32_t id; } fwcache[NFWCACHE]; static CC_INLINE int fwc_lookup(const uip_ipaddr_t *orig, const uint32_t *id) { unsigned n = (orig->u8[2] + orig->u8[3]) % NFWCACHE; return fwcache[n].id == *id && uip_ipaddr_cmp(&fwcache[n].orig, orig); } static CC_INLINE void fwc_add(const uip_ipaddr_t *orig, const uint32_t *id) { unsigned n = (orig->u8[2] + orig->u8[3]) % NFWCACHE; fwcache[n].id = *id; uip_ipaddr_copy(&fwcache[n].orig, orig); } #ifdef NDEBUG #define PRINTF(...) do {} while (0) #define print_debug(...) do{}while(0) #else #define PRINTF(...) printf(__VA_ARGS__) #ifdef __GNUC__ static void print_debug(const char *fmt, ...) __attribute__((format(printf, 1, 2))); #endif /* __GNUC__ */ static void print_debug(const char *fmt, ...) { va_list ap; va_start(ap, fmt); printf("%d.%d.%d.%d: ", uip_ipaddr_to_quad(&uip_hostaddr)); vprintf(fmt, ap); va_end(ap); return; }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(shell_id_process, ev, data) { char buf[40]; PROCESS_BEGIN(); snprintf(buf, sizeof(buf), "%d.%d.%d.%d", uip_ipaddr_to_quad(&uip_hostaddr)); shell_output_str(&id_command, "IP address: ", buf); snprintf(buf, sizeof(buf), "%d.%d", rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]); shell_output_str(&id_command, "Rime address: ", buf); PROCESS_END(); }
static void set_gateway(void) { if(!is_gateway) { printf("%d.%d: making myself the IP network gateway.\n\n", linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]); printf("IPv4 address of the gateway: %d.%d.%d.%d\n\n", uip_ipaddr_to_quad(&uip_hostaddr)); uip_over_mesh_set_gateway(&linkaddr_node_addr); uip_over_mesh_make_announced_gateway(); is_gateway = 1; } }
static void set_gateway(void) { if(!is_gateway) { leds_on(LEDS_RED); PRINTF("%d.%d: making myself the IP network gateway.\n\n", rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]); PRINTF("IPv4 address of the gateway: %d.%d.%d.%d\n\n", uip_ipaddr_to_quad(&uip_hostaddr)); uip_over_mesh_set_gateway(&rimeaddr_node_addr); uip_over_mesh_make_announced_gateway(); is_gateway = 1; } }
/*---------------------------------------------------------------------------*/ static void send_rerr(uip_ipaddr_t *addr, uint32_t *seqno) { struct uaodv_msg_rerr *rm = (struct uaodv_msg_rerr *)uip_appdata; print_debug("send RERR for %d.%d.%d.%d\n", uip_ipaddr_to_quad(addr)); rm->type = UAODV_RERR_TYPE; rm->reserved = 0; rm->dest_count = 1; uip_ipaddr_copy(&rm->unreach[0].addr, addr); rm->unreach[0].seqno = *seqno; if(*seqno == 0) rm->flags = UAODV_RERR_UNKNOWN; else rm->flags = 0; uip_udp_packet_send(bcastconn, rm, sizeof(struct uaodv_msg_rerr)); }
/*---------------------------------------------------------------------------*/ static void send_rrep(uip_ipaddr_t *dest, uip_ipaddr_t *nexthop, uip_ipaddr_t *orig, uint32_t *seqno, unsigned hop_count) { struct uaodv_msg_rrep *rm = (struct uaodv_msg_rrep *)uip_appdata; print_debug("send RREP orig=%d.%d.%d.%d hops=%d\n", uip_ipaddr_to_quad(orig), hop_count); rm->type = UAODV_RREP_TYPE; rm->flags = 0; rm->prefix_sz = 0; /* I.e a /32 route. */ rm->hop_count = hop_count; uip_ipaddr_copy(&rm->orig_addr, orig); rm->dest_seqno = *seqno; uip_ipaddr_copy(&rm->dest_addr, dest); rm->lifetime = UIP_HTONL(MY_ROUTE_TIMEOUT); sendto(nexthop, rm, sizeof(struct uaodv_msg_rrep)); }
/*---------------------------------------------------------------------------*/ void init_net(void) { int i; uip_ipaddr_t hostaddr, netmask; rimeaddr_t rimeaddr; /* Init Rime */ ctimer_init(); rimeaddr.u8[0] = node_id & 0xff; rimeaddr.u8[1] = node_id >> 8; rimeaddr_set_node_addr(&rimeaddr); printf("Rime started with address: "); for(i = 0; i < sizeof(rimeaddr_node_addr.u8) - 1; i++) { printf("%d.", rimeaddr_node_addr.u8[i]); } printf("%d\n", rimeaddr_node_addr.u8[i]); /* Init uIPv4 */ process_start(&tcpip_process, NULL); process_start(&uip_fw_process, NULL); process_start(&slip_process, NULL); uip_init(); uip_fw_init(); uip_ipaddr(&hostaddr, 172, 16, rimeaddr_node_addr.u8[1], rimeaddr_node_addr.u8[0]); uip_ipaddr(&netmask, 255,255,0,0); uip_sethostaddr(&hostaddr); uip_setnetmask(&netmask); uip_fw_register(&wsn_if); uip_fw_default(&slip_if); rs232_set_input(slip_input_byte); printf("uIP started with IP address: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&hostaddr)); /* uIPv4 <-> COOJA's packet radio */ /*tcpip_set_outputfunc(sender);*/ cooja_radio.set_receive_function(receiver); }
/*---------------------------------------------------------------------------*/ uint8_t httpd_sprint_ip4(const uip_ip4addr_t *addr, char *result) { return sprintf(result, "%d.%d.%d.%d", uip_ipaddr_to_quad(addr)); }
static void dhcpTask(void* arg) { int wait; init: xid++; s.state = STATE_SENDING; wait = 1; while (1) { send_discover(); if (readResponse(DHCPOFFER, wait) != -1) { s.state = STATE_OFFER_RECEIVED; goto selecting; } if(wait < 60) wait = wait * 2; } selecting: xid++; wait = 1; do { send_request(); if (readResponse(DHCPACK, wait) != -1) { s.state = STATE_CONFIG_RECEIVED; goto bound; } if(wait < 10) { wait++; } else { goto init; } } while(s.state != STATE_CONFIG_RECEIVED); bound: #if 0 nosPrintf("Got IP address %d.%d.%d.%d\n", uip_ipaddr_to_quad(&s.ipaddr)); nosPrintf("Got netmask %d.%d.%d.%d\n", uip_ipaddr_to_quad(&s.netmask)); nosPrintf("Got DNS server %d.%d.%d.%d\n", uip_ipaddr_to_quad(&s.dnsaddr)); nosPrintf("Got default router %d.%d.%d.%d\n", uip_ipaddr_to_quad(&s.default_router)); nosPrintf("Lease expires in %ld seconds\n", uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1])); #endif dhcpc_configured(&s); uint32_t leaseLeft = uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1]); uint32_t sleepLeft = leaseLeft / 2; #define MAX_SECS ((INFINITE - 1) / HZ) while (sleepLeft > 0) { if (sleepLeft > MAX_SECS) { wait = MAX_SECS * HZ; sleepLeft -= MAX_SECS; } else { wait = sleepLeft * HZ; sleepLeft = 0; } posTaskSleep(wait); } leaseLeft = leaseLeft / 2; /* renewing: */ xid++; do { send_request(); if (leaseLeft / 2 > MAX_SECS) wait = MAX_SECS; else wait = (leaseLeft / 2); if (readResponse(DHCPACK, wait) != -1) { s.state = STATE_CONFIG_RECEIVED; goto bound; } leaseLeft -= wait; } while (leaseLeft > 3); /* rebinding: */ /* lease_expired: */ dhcpc_unconfigured(&s); goto init; }
/*---------------------------------------------------------------------------*/ int main(void) { process_init(); procinit_init(); ctimer_init(); autostart_start(autostart_processes); #if !UIP_CONF_IPV6 uip_ipaddr_t addr; uip_ipaddr(&addr, 192,168,1,2); printf("IP Address: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr)); uip_sethostaddr(&addr); uip_ipaddr(&addr, 255,255,255,0); printf("Subnet Mask: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr)); uip_setnetmask(&addr); uip_ipaddr(&addr, 192,168,1,1); printf("Def. Router: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr)); uip_setdraddr(&addr); #else { uip_ipaddr_t ipaddr; uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0); uip_netif_addr_autoconf_set(&ipaddr, &uip_lladdr); uip_netif_addr_add(&ipaddr, 16, 0, TENTATIVE); } #endif /* Make standard output unbuffered. */ setvbuf(stdout, (char *)NULL, _IONBF, 0); while(1) { fd_set fds; int n; struct timeval tv; n = process_run(); /* if(n > 0) { printf("%d processes in queue\n"); }*/ tv.tv_sec = 0; tv.tv_usec = 1; FD_ZERO(&fds); FD_SET(STDIN_FILENO, &fds); select(1, &fds, NULL, NULL, &tv); if(FD_ISSET(STDIN_FILENO, &fds)) { char c; if(read(STDIN_FILENO, &c, 1) > 0) { serial_line_input_byte(c); } } etimer_request_poll(); } return 0; }
int main() { disableIRQ(); disableFIQ(); *AT91C_AIC_IDCR = 0xffffffff; *AT91C_PMC_PCDR = 0xffffffff; *AT91C_PMC_PCER = (1 << AT91C_ID_PIOA); dbg_setup_uart(); printf("Initialising\n"); leds_arch_init(); clock_init(); process_init(); process_start(&etimer_process, NULL); ctimer_init(); robot_stepper_init(); enableIRQ(); cc2420_init(); cc2420_set_pan_addr(0x2024, 0, &uip_hostaddr.u16[1]); cc2420_set_channel(RF_CHANNEL); rime_init(nullmac_init(&cc2420_driver)); printf("CC2420 setup done\n"); rimeaddr_set_node_addr(&node_addr); #if WITH_UIP { uip_ipaddr_t hostaddr, netmask; uip_init(); uip_ipaddr(&hostaddr, 172,16, rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1]); uip_ipaddr(&netmask, 255,255,0,0); uip_ipaddr_copy(&meshif.ipaddr, &hostaddr); printf("Host addr\n"); uip_sethostaddr(&hostaddr); uip_setnetmask(&netmask); uip_over_mesh_set_net(&hostaddr, &netmask); /* uip_fw_register(&slipif);*/ /*uip_over_mesh_set_gateway_netif(&slipif);*/ uip_fw_default(&meshif); printf("Mesh init\n"); uip_over_mesh_init(UIP_OVER_MESH_CHANNEL); printf("uIP started with IP address %d.%d.%d.%d\n", uip_ipaddr_to_quad(&hostaddr)); } #endif /* WITH_UIP */ #if WITH_UIP process_start(&tcpip_process, NULL); process_start(&uip_fw_process, NULL); /* Start IP output */ #endif /* WITH_UIP */ printf("Heap size: %ld bytes\n", &__heap_end__ - (char*)sbrk(0)); printf("Started\n"); autostart_start(autostart_processes); printf("Processes running\n"); while(1) { do { /* Reset watchdog. */ wdt_reset(); } while(process_run() > 0); /* Idle! */ /* Stop processor clock */ *AT91C_PMC_SCDR |= AT91C_PMC_PCK; } return 0; }
// Requires a buffer of at least 16 bytes to format into void NanodeUIP::format_ipaddr(char *buf, uip_ipaddr_t *addr) { sprintf_P(buf, PSTR("%d.%d.%d.%d"), uip_ipaddr_to_quad(addr)); }
int main(int argc, char **argv) { /* * Initalize hardware. */ msp430_cpu_init(); clock_init(); leds_init(); leds_toggle(LEDS_ALL); slip_arch_init(BAUD2UBR(115200)); /* Must come before first printf */ printf("Starting %s " "($Id: gateway.c,v 1.2 2010/10/19 18:29:04 adamdunkels Exp $)\n", __FILE__); ds2411_init(); sensors_light_init(); cc2420_init(); xmem_init(); leds_toggle(LEDS_ALL); /* * Hardware initialization done! */ printf("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x CHANNEL %d\n", ds2411_id[0], ds2411_id[1], ds2411_id[2], ds2411_id[3], ds2411_id[4], ds2411_id[5], ds2411_id[6], ds2411_id[7], RF_CHANNEL); uip_ipaddr_copy(&uip_hostaddr, &cc2420if.ipaddr); uip_ipaddr_copy(&uip_netmask, &cc2420if.netmask); printf("IP %d.%d.%d.%d netmask %d.%d.%d.%d\n", uip_ipaddr_to_quad(&uip_hostaddr), uip_ipaddr_to_quad(&uip_netmask)); cc2420_set_chan_pan_addr(RF_CHANNEL, panId, uip_hostaddr.u16[1], ds2411_id); srand(rand() + (ds2411_id[3]<<8) + (ds2411_id[4]<<6) + (ds2411_id[5]<<4) + (ds2411_id[6]<<2) + ds2411_id[7]); /* * Initialize Contiki and our processes. */ process_init(); process_start(&etimer_process, NULL); /* Configure IP stack. */ uip_init(); uip_fw_default(&slipif); /* Point2point, no default router. */ uip_fw_register(&cc2420if); tcpip_set_forwarding(1); /* Start IP stack. */ process_start(&tcpip_process, NULL); process_start(&uip_fw_process, NULL); /* Start IP output */ process_start(&slip_process, NULL); process_start(&cc2420_process, NULL); cc2420_on(); process_start(&uaodv_process, NULL); process_start(&tcp_loader_process, NULL); /* * This is the scheduler loop. */ printf("process_run()...\n"); while (1) { do { /* Reset watchdog. */ } while(process_run() > 0); /* Idle! */ } return 0; }
/*---------------------------------------------------------------------------*/ void contiki_init() { /* Initialize random generator (moved to moteid.c) */ /* Start process handler */ process_init(); /* Start Contiki processes */ process_start(&etimer_process, NULL); process_start(&sensors_process, NULL); ctimer_init(); /* Print startup information */ printf(CONTIKI_VERSION_STRING " started. "); if(node_id > 0) { printf("Node id is set to %u.\n", node_id); } else { printf("Node id is not set.\n"); } set_rime_addr(); { uint8_t longaddr[8]; memset(longaddr, 0, sizeof(longaddr)); linkaddr_copy((linkaddr_t *)&longaddr, &linkaddr_node_addr); printf("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ", longaddr[0], longaddr[1], longaddr[2], longaddr[3], longaddr[4], longaddr[5], longaddr[6], longaddr[7]); } queuebuf_init(); /* Initialize communication stack */ netstack_init(); printf("%s/%s/%s, channel check rate %lu Hz\n", NETSTACK_NETWORK.name, NETSTACK_MAC.name, NETSTACK_RDC.name, CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1: NETSTACK_RDC.channel_check_interval())); #if WITH_UIP /* IPv4 CONFIGURATION */ { uip_ipaddr_t hostaddr, netmask; process_start(&tcpip_process, NULL); process_start(&uip_fw_process, NULL); process_start(&slip_process, NULL); slip_set_input_callback(set_gateway); uip_init(); uip_fw_init(); uip_ipaddr(&hostaddr, 172,16,linkaddr_node_addr.u8[0],linkaddr_node_addr.u8[1]); uip_ipaddr(&netmask, 255,255,0,0); uip_ipaddr_copy(&meshif.ipaddr, &hostaddr); uip_sethostaddr(&hostaddr); uip_setnetmask(&netmask); uip_over_mesh_set_net(&hostaddr, &netmask); uip_over_mesh_set_gateway_netif(&slipif); uip_fw_default(&meshif); uip_over_mesh_init(UIP_OVER_MESH_CHANNEL); rs232_set_input(slip_input_byte); printf("IPv4 address: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&hostaddr)); } #endif /* WITH_UIP */ #if WITH_UIP6 /* IPv6 CONFIGURATION */ { int i; uint8_t addr[sizeof(uip_lladdr.addr)]; for(i = 0; i < sizeof(uip_lladdr.addr); i += 2) { addr[i + 1] = node_id & 0xff; addr[i + 0] = node_id >> 8; } linkaddr_copy((linkaddr_t *)addr, &linkaddr_node_addr); memcpy(&uip_lladdr.addr, addr, sizeof(uip_lladdr.addr)); process_start(&tcpip_process, NULL); printf("Tentative link-local IPv6 address "); { uip_ds6_addr_t *lladdr; int i; lladdr = uip_ds6_get_link_local(-1); for(i = 0; i < 7; ++i) { printf("%02x%02x:", lladdr->ipaddr.u8[i * 2], lladdr->ipaddr.u8[i * 2 + 1]); } printf("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]); } if(1) { uip_ipaddr_t ipaddr; int i; uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0); uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE); printf("Tentative global IPv6 address "); for(i = 0; i < 7; ++i) { printf("%02x%02x:", ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]); } printf("%02x%02x\n", ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]); } } #endif /* WITH_UIP6 */ /* Initialize eeprom */ eeprom_init(); /* Start serial process */ serial_line_init(); /* Start autostart processes (defined in Contiki application) */ print_processes(autostart_processes); autostart_start(autostart_processes); }
/*---------------------------------------------------------------------------*/ static void handle_incoming_rrep(void) { struct uaodv_msg_rrep *rm = (struct uaodv_msg_rrep *)uip_appdata; struct uaodv_rt_entry *rt; /* Useless HELLO message? */ if(uip_ipaddr_cmp(&BUF->destipaddr, &uip_broadcast_addr)) { #ifdef AODV_RESPOND_TO_HELLOS uint32_t net_seqno; #ifdef CC2420_RADIO int ret = cc2420_check_remote(uip_udp_sender()->u16[1]); if(ret == REMOTE_YES) { print_debug("HELLO drop is remote\n"); return; } else if (ret == REMOTE_NO) { /* Is neigbour, accept it. */ } else if(cc2420_last_rssi < RSSI_THRESHOLD) { print_debug("HELLO drop %d %d\n", cc2420_last_rssi, cc2420_last_correlation); return; } #endif /* Sometimes it helps to send a non-requested RREP in response! */ net_seqno = uip_htonl(my_hseqno); send_rrep(&uip_hostaddr, &BUF->srcipaddr, &BUF->srcipaddr, &net_seqno, 0); #endif return; } print_debug("RREP %d.%d.%d.%d -> %d.%d.%d.%d" " dest=%d.%d.%d.%d seq=%lu hops=%u orig=%d.%d.%d.%d\n", uip_ipaddr_to_quad(&BUF->srcipaddr), uip_ipaddr_to_quad(&BUF->destipaddr), uip_ipaddr_to_quad(&rm->dest_addr), uip_ntohl(rm->dest_seqno), rm->hop_count, uip_ipaddr_to_quad(&rm->orig_addr)); rt = uaodv_rt_lookup(&rm->dest_addr); /* New forward route? */ if(rt == NULL || (SCMP32(uip_ntohl(rm->dest_seqno), rt->hseqno) > 0)) { print_debug("Inserting3\n"); rt = uaodv_rt_add(&rm->dest_addr, uip_udp_sender(), rm->hop_count, &rm->dest_seqno); #ifdef CC2420_RADIO /* This link is ok since he is unicasting back to us! */ cc2420_recv_ok(uip_udp_sender()); print_debug("RREP recv ok %d %d\n", cc2420_last_rssi, cc2420_last_correlation); #endif } else { print_debug("Not inserting\n"); } /* Forward RREP towards originator? */ if(uip_ipaddr_cmp(&rm->orig_addr, &uip_hostaddr)) { print_debug("ROUTE FOUND\n"); if(rm->flags & UAODV_RREP_ACK) { struct uaodv_msg_rrep_ack *ack = (void *)uip_appdata; ack->type = UAODV_RREP_ACK_TYPE; ack->reserved = 0; sendto(uip_udp_sender(), ack, sizeof(*ack)); } } else { rt = uaodv_rt_lookup(&rm->orig_addr); if(rt == NULL) { print_debug("RREP received, but no route back to originator... :-( \n"); return; } if(rm->flags & UAODV_RREP_ACK) { print_debug("RREP with ACK request (ignored)!\n"); /* Don't want any RREP-ACKs in return! */ rm->flags &= ~UAODV_RREP_ACK; } rm->hop_count++; print_debug("Fwd RREP to %d.%d.%d.%d\n", uip_ipaddr_to_quad(&rt->nexthop)); sendto(&rt->nexthop, rm, sizeof(struct uaodv_msg_rrep)); } }
/*---------------------------------------------------------------------------*/ static void handle_incoming_rreq(void) { struct uaodv_msg_rreq *rm = (struct uaodv_msg_rreq *)uip_appdata; uip_ipaddr_t dest_addr, orig_addr; struct uaodv_rt_entry *rt, *fw = NULL; print_debug("RREQ %d.%d.%d.%d -> %d.%d.%d.%d ttl=%u" " orig=%d.%d.%d.%d seq=%lu hops=%u dest=%d.%d.%d.%d seq=%lu\n", uip_ipaddr_to_quad(&BUF->srcipaddr), uip_ipaddr_to_quad(&BUF->destipaddr), BUF->ttl, uip_ipaddr_to_quad(&rm->orig_addr), uip_ntohl(rm->orig_seqno), rm->hop_count, uip_ipaddr_to_quad(&rm->dest_addr), uip_ntohl(rm->dest_seqno)); if(uip_ipaddr_cmp(&rm->orig_addr, &uip_hostaddr)) { return; /* RREQ looped back! */ } #ifdef CC2420_RADIO { int ret = cc2420_check_remote(uip_udp_sender()->u16[1]); if(ret == REMOTE_YES) { print_debug("RREQ drop is remote\n"); return; } else if (ret == REMOTE_NO) { /* Is neigbour, accept it. */ } else if(cc2420_last_rssi < RSSI_THRESHOLD) { print_debug("RREQ drop %d %d\n", cc2420_last_rssi, cc2420_last_correlation); return; } } #endif #ifdef AODV_BAD_HOP_EXTENSION if(uip_len > (sizeof(*rm) + 2)) { struct uaodv_bad_hop_ext *ext = (void *)(uip_appdata + sizeof(*rm)); uint8_t *end = uip_appdata + uip_len; for(; (uint8_t *)ext < end; ext = (void *)((uint8_t *)ext + ext->length + 2)) { uint8_t *eend = (uint8_t *)ext + ext->length; if(eend > end) eend = end; if(ext->type == RREQ_BAD_HOP_EXT) { uip_ipaddr_t *a; for(a = ext->addrs; (uint8_t *)a < eend; a++) { if(uip_ipaddr_cmp(a, &uip_hostaddr)) { print_debug("BAD_HOP drop\n"); return; } } } } } #endif /* AODV_BAD_HOP_EXTENSION */ /* New reverse route? */ rt = uaodv_rt_lookup(&rm->orig_addr); if(rt == NULL || (SCMP32(uip_ntohl(rm->orig_seqno), rt->hseqno) > 0) /* New route. */ || (SCMP32(uip_ntohl(rm->orig_seqno), rt->hseqno) == 0 && rm->hop_count < rt->hop_count)) { /* Better route. */ print_debug("Inserting1\n"); rt = uaodv_rt_add(&rm->orig_addr, uip_udp_sender(), rm->hop_count, &rm->orig_seqno); } /* Check if it is for our address or a fresh route. */ if(uip_ipaddr_cmp(&rm->dest_addr, &uip_hostaddr) || rm->flags & UAODV_RREQ_DESTONLY) { fw = NULL; } else { fw = uaodv_rt_lookup(&rm->dest_addr); if(!(rm->flags & UAODV_RREQ_UNKSEQNO) && fw != NULL && SCMP32(fw->hseqno, uip_ntohl(rm->dest_seqno)) <= 0) { fw = NULL; } } if (fw != NULL) { uint32_t net_seqno; print_debug("RREQ for known route\n"); uip_ipaddr_copy(&dest_addr, &rm->dest_addr); uip_ipaddr_copy(&orig_addr, &rm->orig_addr); net_seqno = uip_htonl(fw->hseqno); send_rrep(&dest_addr, &rt->nexthop, &orig_addr, &net_seqno, fw->hop_count + 1); } else if(uip_ipaddr_cmp(&rm->dest_addr, &uip_hostaddr)) { uint32_t net_seqno; print_debug("RREQ for our address\n"); uip_ipaddr_copy(&dest_addr, &rm->dest_addr); uip_ipaddr_copy(&orig_addr, &rm->orig_addr); my_hseqno++; if(!(rm->flags & UAODV_RREQ_UNKSEQNO) && SCMP32(my_hseqno, uip_ntohl(rm->dest_seqno)) < 0) { print_debug("New my_hseqno %lu\n", my_hseqno); /* We have rebooted. */ my_hseqno = uip_ntohl(rm->dest_seqno) + 1; } net_seqno = uip_htonl(my_hseqno); send_rrep(&dest_addr, &rt->nexthop, &orig_addr, &net_seqno, 0); } else if(BUF->ttl > 1) { int len; /* Have we seen this RREQ before? */ if(fwc_lookup(&rm->orig_addr, &rm->rreq_id)) { print_debug("RREQ cached, not fwd\n"); return; } fwc_add(&rm->orig_addr, &rm->rreq_id); print_debug("RREQ fwd\n"); rm->hop_count++; bcastconn->ttl = BUF->ttl - 1; len = sizeof(struct uaodv_msg_rreq); len += add_rreq_extensions(rm + 1); uip_udp_packet_send(bcastconn, rm, len); } }
void init_net(void) { /* Start radio and radio receive process */ NETSTACK_RADIO.init(); /* Set addresses BEFORE starting tcpip process */ set_rime_addr(); /* Setup nullmac-like MAC for 802.15.4 */ /* sicslowpan_init(sicslowmac_init(&cc2420_driver)); */ /* printf(" %s channel %u\n", sicslowmac_driver.name, RF_CHANNEL); */ /* Setup X-MAC for 802.15.4 */ queuebuf_init(); NETSTACK_RDC.init(); NETSTACK_MAC.init(); NETSTACK_NETWORK.init(); PRINTA("%s %s, channel %u , check rate %u Hz tx power %u\n", NETSTACK_MAC.name, NETSTACK_RDC.name, rf2xx_get_channel(), CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1 : NETSTACK_RDC.channel_check_interval()), rf2xx_get_txpower()); #if UIP_CONF_IPV6_RPL PRINTA("RPL Enabled\n"); #endif #if UIP_CONF_ROUTER PRINTA("Routing Enabled\n"); #endif process_start(&tcpip_process, NULL); #if ANNOUNCE_BOOT && UIP_CONF_IPV6 PRINTA("Tentative link-local IPv6 address "); { uip_ds6_addr_t *lladdr; int i; lladdr = uip_ds6_get_link_local(-1); for(i = 0; i < 7; ++i) { PRINTA("%02x%02x:", lladdr->ipaddr.u8[i * 2], lladdr->ipaddr.u8[i * 2 + 1]); } PRINTA("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]); } if(!UIP_CONF_IPV6_RPL) { uip_ipaddr_t ipaddr; int i; uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0); uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE); PRINTA("Tentative global IPv6 address "); for(i = 0; i < 7; ++i) { PRINTA("%02x%02x:", ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]); } PRINTA("%02x%02x\n", ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]); } #endif /* ANNOUNCE_BOOT */ #if WITH_UIP uip_ipaddr_t hostaddr, netmask; uip_init(); uip_fw_init(); process_start(&tcpip_process, NULL); process_start(&slip_process, NULL); process_start(&uip_fw_process, NULL); slip_set_input_callback(set_gateway); /* Construct ip address from four bytes. */ uip_ipaddr(&hostaddr, 172, 16, rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]); /* Construct netmask from four bytes. */ uip_ipaddr(&netmask, 255,255,0,0); uip_ipaddr_copy(&meshif.ipaddr, &hostaddr); /* Set the IP address for this host. */ uip_sethostaddr(&hostaddr); /* Set the netmask for this host. */ uip_setnetmask(&netmask); uip_over_mesh_set_net(&hostaddr, &netmask); /* Register slip interface with forwarding module. */ //uip_fw_register(&slipif); uip_over_mesh_set_gateway_netif(&slipif); /* Set slip interface to be a default forwarding interface . */ uip_fw_default(&meshif); uip_over_mesh_init(UIP_OVER_MESH_CHANNEL); PRINTA(PSTR("uIP started with IP address %d.%d.%d.%d\n"), uip_ipaddr_to_quad(&hostaddr)); #endif /* WITH_UIP */ }
/*---------------------------------------------------------------------------*/ int main(void) { #if UIP_CONF_IPV6 /* A hard coded address overrides the stack default MAC address to allow multiple instances. * uip6.c defines it as {0x00,0x06,0x98,0x00,0x02,0x32} giving an ipv6 address of [fe80::206:98ff:fe00:232] * We make it simpler, {0x02,0x00,0x00 + the last three bytes of the hard coded address (if any are nonzero). * HARD_CODED_ADDRESS can be defined in the contiki-conf.h file, or here to allow quick builds using different addresses. * If HARD_CODED_ADDRESS has a prefix it also applied, unless built as a RPL end node. * E.g. bbbb::12:3456 becomes fe80::ff:fe12:3456 and prefix bbbb::/64 if non-RPL * ::10 becomes fe80::ff:fe00:10 and prefix awaits RA or RPL formation * bbbb:: gives an address of bbbb::206:98ff:fe00:232 if non-RPL */ //#define HARD_CODED_ADDRESS "bbbb::20" #ifdef HARD_CODED_ADDRESS { uip_ipaddr_t ipaddr; uiplib_ipaddrconv(HARD_CODED_ADDRESS, &ipaddr); if ((ipaddr.u8[13]!=0) || (ipaddr.u8[14]!=0) || (ipaddr.u8[15]!=0)) { if (sizeof(uip_lladdr)==6) { //Minimal-net uses ethernet MAC uip_lladdr.addr[0]=0x02;uip_lladdr.addr[1]=0;uip_lladdr.addr[2]=0; uip_lladdr.addr[3]=ipaddr.u8[13];; uip_lladdr.addr[4]=ipaddr.u8[14]; uip_lladdr.addr[5]=ipaddr.u8[15]; } } } #endif #endif process_init(); /* procinit_init initializes RPL which sets a ctimer for the first DIS */ /* We must start etimers and ctimers,before calling it */ process_start(&etimer_process, NULL); ctimer_init(); procinit_init(); autostart_start(autostart_processes); #if RPL_BORDER_ROUTER process_start(&border_router_process, NULL); printf("Border Router Process started\n"); #elif UIP_CONF_IPV6_RPL printf("RPL enabled\n"); #endif /* Set default IP addresses if not specified */ #if !UIP_CONF_IPV6 uip_ipaddr_t addr; uip_gethostaddr(&addr); if (addr.u8[0]==0) { uip_ipaddr(&addr, 10,1,1,1); } printf("IP Address: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr)); uip_sethostaddr(&addr); uip_getnetmask(&addr); if (addr.u8[0]==0) { uip_ipaddr(&addr, 255,0,0,0); uip_setnetmask(&addr); } printf("Subnet Mask: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr)); uip_getdraddr(&addr); if (addr.u8[0]==0) { uip_ipaddr(&addr, 10,1,1,100); uip_setdraddr(&addr); } printf("Def. Router: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr)); #else /* UIP_CONF_IPV6 */ #if !UIP_CONF_IPV6_RPL #ifdef HARD_CODED_ADDRESS uip_ipaddr_t ipaddr; uiplib_ipaddrconv(HARD_CODED_ADDRESS, &ipaddr); if ((ipaddr.u16[0]!=0) || (ipaddr.u16[1]!=0) || (ipaddr.u16[2]!=0) || (ipaddr.u16[3]!=0)) { #if UIP_CONF_ROUTER uip_ds6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0, 0, 0, 0); #else /* UIP_CONF_ROUTER */ uip_ds6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0); #endif /* UIP_CONF_ROUTER */ #if !UIP_CONF_IPV6_RPL uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); #endif } #endif /* HARD_CODED_ADDRESS */ #endif #if !RPL_BORDER_ROUTER //Border router process prints addresses later { uint8_t i; for (i=0;i<UIP_DS6_ADDR_NB;i++) { if (uip_ds6_if.addr_list[i].isused) { printf("IPV6 Addresss: ");sprint_ip6(uip_ds6_if.addr_list[i].ipaddr);printf("\n"); } } } #endif #endif /* !UIP_CONF_IPV6 */ /* Make standard output unbuffered. */ setvbuf(stdout, (char *)NULL, _IONBF, 0); while(1) { fd_set fds; int n; struct timeval tv; n = process_run(); /* if(n > 0) { printf("%d processes in queue\n"); }*/ tv.tv_sec = 0; tv.tv_usec = 1; FD_ZERO(&fds); FD_SET(STDIN_FILENO, &fds); select(1, &fds, NULL, NULL, &tv); if(FD_ISSET(STDIN_FILENO, &fds)) { char c; if(read(STDIN_FILENO, &c, 1) > 0) { serial_line_input_byte(c); } } etimer_request_poll(); } return 0; }
/*---------------------------------------------------------------------------*/ uint8_t uip_over_mesh_send(void) { linkaddr_t receiver; struct route_entry *rt; /* This function is called by the uip-fw module to send out an IP packet. We try to send the IP packet to the next hop route, or we queue the packet and send out a route request for the final receiver of the packet. */ /* Packets destined to this network is sent using mesh, whereas packets destined to a network outside this network is sent towards the gateway node. */ if(uip_ipaddr_maskcmp(&BUF->destipaddr, &netaddr, &netmask)) { receiver.u8[0] = BUF->destipaddr.u8[2]; receiver.u8[1] = BUF->destipaddr.u8[3]; } else { if(linkaddr_cmp(&gateway, &linkaddr_node_addr)) { PRINTF("uip_over_mesh_send: I am gateway, packet to %d.%d.%d.%d to local interface\n", uip_ipaddr_to_quad(&BUF->destipaddr)); if(gw_netif != NULL) { return gw_netif->output(); } return UIP_FW_DROPPED; } else if(linkaddr_cmp(&gateway, &linkaddr_null)) { PRINTF("uip_over_mesh_send: No gateway setup, dropping packet\n"); return UIP_FW_OK; } else { PRINTF("uip_over_mesh_send: forwarding packet to %d.%d.%d.%d towards gateway %d.%d\n", uip_ipaddr_to_quad(&BUF->destipaddr), gateway.u8[0], gateway.u8[1]); linkaddr_copy(&receiver, &gateway); } } PRINTF("uIP over mesh send to %d.%d with len %d\n", receiver.u8[0], receiver.u8[1], uip_len); packetbuf_copyfrom(&uip_buf[UIP_LLH_LEN], uip_len); /* Send TCP data with the PACKETBUF_ATTR_ERELIABLE set so that an underlying power-saving MAC layer knows that it should be waiting for an ACK. */ if(BUF->proto == UIP_PROTO_TCP) { packetbuf_set_attr(PACKETBUF_ATTR_ERELIABLE, 1); packetbuf_set_attr(PACKETBUF_ATTR_RELIABLE, 1); /* packetbuf_set_attr(PACKETBUF_ATTR_PACKET_TYPE, PACKETBUF_ATTR_PACKET_TYPE_STREAM);*/ } rt = route_lookup(&receiver); if(rt == NULL) { PRINTF("uIP over mesh no route to %d.%d\n", receiver.u8[0], receiver.u8[1]); if(queued_packet == NULL) { queued_packet = queuebuf_new_from_packetbuf(); linkaddr_copy(&queued_receiver, &receiver); route_discovery_discover(&route_discovery, &receiver, ROUTE_TIMEOUT); } else if(!linkaddr_cmp(&queued_receiver, &receiver)) { route_discovery_discover(&route_discovery, &receiver, ROUTE_TIMEOUT); } } else { route_decay(rt); send_data(&rt->nexthop); } return UIP_FW_OK; }
/*---------------------------------------------------------------------------*/ void init_net(void) { set_rime_addr(); cc2420_init(); { uint8_t longaddr[8]; uint16_t shortaddr; shortaddr = (linkaddr_node_addr.u8[0] << 8) + linkaddr_node_addr.u8[1]; memset(longaddr, 0, sizeof(longaddr)); linkaddr_copy((linkaddr_t *)&longaddr, &linkaddr_node_addr); printf_P(PSTR("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n"), longaddr[0], longaddr[1], longaddr[2], longaddr[3], longaddr[4], longaddr[5], longaddr[6], longaddr[7]); cc2420_set_pan_addr(IEEE802154_PANID, shortaddr, longaddr); } #if NETSTACK_CONF_WITH_IPV6 memcpy(&uip_lladdr.addr, ds2401_id, sizeof(uip_lladdr.addr)); /* Setup nullmac-like MAC for 802.15.4 */ /* sicslowpan_init(sicslowmac_init(&cc2420_driver)); */ /* printf(" %s channel %u\n", sicslowmac_driver.name, CC2420_CONF_CHANNEL); */ /* Setup X-MAC for 802.15.4 */ queuebuf_init(); NETSTACK_RDC.init(); NETSTACK_MAC.init(); NETSTACK_NETWORK.init(); printf_P(PSTR("%s %s, channel check rate %d Hz, radio channel %d\n"), NETSTACK_MAC.name, NETSTACK_RDC.name, CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1: NETSTACK_RDC.channel_check_interval()), CC2420_CONF_CHANNEL); process_start(&tcpip_process, NULL); printf_P(PSTR("Tentative link-local IPv6 address ")); { uip_ds6_addr_t *lladdr; int i; lladdr = uip_ds6_get_link_local(-1); for(i = 0; i < 7; ++i) { printf_P(PSTR("%02x%02x:"), lladdr->ipaddr.u8[i * 2], lladdr->ipaddr.u8[i * 2 + 1]); } printf_P(PSTR("%02x%02x\n"), lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]); } if(!UIP_CONF_IPV6_RPL) { uip_ipaddr_t ipaddr; int i; uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0); uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE); printf_P(PSTR("Tentative global IPv6 address ")); for(i = 0; i < 7; ++i) { printf_P(PSTR("%02x%02x:"), ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]); } printf_P(PSTR("%02x%02x\n"), ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]); } #else /* NETSTACK_CONF_WITH_IPV6 */ NETSTACK_RDC.init(); NETSTACK_MAC.init(); NETSTACK_NETWORK.init(); printf_P(PSTR("%s %s, channel check rate %d Hz, radio channel %d\n"), NETSTACK_MAC.name, NETSTACK_RDC.name, CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0? 1: NETSTACK_RDC.channel_check_interval()), CC2420_CONF_CHANNEL); #endif /* NETSTACK_CONF_WITH_IPV6 */ #if NETSTACK_CONF_WITH_IPV4 uip_ipaddr_t hostaddr, netmask; uip_init(); uip_fw_init(); process_start(&tcpip_process, NULL); process_start(&slip_process, NULL); process_start(&uip_fw_process, NULL); slip_set_input_callback(set_gateway); /* Construct ip address from four bytes. */ uip_ipaddr(&hostaddr, 172, 16, linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]); /* Construct netmask from four bytes. */ uip_ipaddr(&netmask, 255,255,0,0); uip_ipaddr_copy(&meshif.ipaddr, &hostaddr); /* Set the IP address for this host. */ uip_sethostaddr(&hostaddr); /* Set the netmask for this host. */ uip_setnetmask(&netmask); uip_over_mesh_set_net(&hostaddr, &netmask); /* Register slip interface with forwarding module. */ //uip_fw_register(&slipif); uip_over_mesh_set_gateway_netif(&slipif); /* Set slip interface to be a default forwarding interface . */ uip_fw_default(&meshif); uip_over_mesh_init(UIP_OVER_MESH_CHANNEL); printf_P(PSTR("uIP started with IP address %d.%d.%d.%d\n"), uip_ipaddr_to_quad(&hostaddr)); #endif /* NETSTACK_CONF_WITH_IPV4 */ }
/*---------------------------------------------------------------------------*/ int main(int argc, char **argv) { /* * Initalize hardware. */ msp430_cpu_init(); clock_init(); leds_init(); leds_on(LEDS_RED); clock_wait(100); uart0_init(BAUD2UBR(UART0_BAUD_RATE)); /* Must come before first printf */ #if NETSTACK_CONF_WITH_IPV4 slip_arch_init(BAUD2UBR(UART0_BAUD_RATE)); #endif /* NETSTACK_CONF_WITH_IPV4 */ xmem_init(); rtimer_init(); /* * Hardware initialization done! */ /* Restore node id if such has been stored in external mem */ node_id_restore(); /* If no MAC address was burned, we use the node id or the Z1 product ID */ if(!(node_mac[0] | node_mac[1] | node_mac[2] | node_mac[3] | node_mac[4] | node_mac[5] | node_mac[6] | node_mac[7])) { #ifdef SERIALNUM if(!node_id) { PRINTF("Node id is not set, using Z1 product ID\n"); node_id = SERIALNUM; } #endif node_mac[0] = 0xc1; /* Hardcoded for Z1 */ node_mac[1] = 0x0c; /* Hardcoded for Revision C */ node_mac[2] = 0x00; /* Hardcoded to arbitrary even number so that the 802.15.4 MAC address is compatible with an Ethernet MAC address - byte 0 (byte 2 in the DS ID) */ node_mac[3] = 0x00; /* Hardcoded */ node_mac[4] = 0x00; /* Hardcoded */ node_mac[5] = 0x00; /* Hardcoded */ node_mac[6] = node_id >> 8; node_mac[7] = node_id & 0xff; } /* Overwrite node MAC if desired at compile time */ #ifdef MACID #warning "***** CHANGING DEFAULT MAC *****" node_mac[0] = 0xc1; /* Hardcoded for Z1 */ node_mac[1] = 0x0c; /* Hardcoded for Revision C */ node_mac[2] = 0x00; /* Hardcoded to arbitrary even number so that the 802.15.4 MAC address is compatible with an Ethernet MAC address - byte 0 (byte 2 in the DS ID) */ node_mac[3] = 0x00; /* Hardcoded */ node_mac[4] = 0x00; /* Hardcoded */ node_mac[5] = 0x00; /* Hardcoded */ node_mac[6] = MACID >> 8; node_mac[7] = MACID & 0xff; #endif #ifdef IEEE_802154_MAC_ADDRESS /* for setting "hardcoded" IEEE 802.15.4 MAC addresses */ { uint8_t ieee[] = IEEE_802154_MAC_ADDRESS; memcpy(node_mac, ieee, sizeof(uip_lladdr.addr)); node_mac[7] = node_id & 0xff; } #endif /* IEEE_802154_MAC_ADDRESS */ /* * Initialize Contiki and our processes. */ random_init(node_mac[6] + node_mac[7]); process_init(); process_start(&etimer_process, NULL); ctimer_init(); init_platform(); set_rime_addr(); cc2420_init(); SENSORS_ACTIVATE(adxl345); { uint8_t longaddr[8]; uint16_t shortaddr; shortaddr = (linkaddr_node_addr.u8[0] << 8) + linkaddr_node_addr.u8[1]; memset(longaddr, 0, sizeof(longaddr)); linkaddr_copy((linkaddr_t *)&longaddr, &linkaddr_node_addr); printf("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ", longaddr[0], longaddr[1], longaddr[2], longaddr[3], longaddr[4], longaddr[5], longaddr[6], longaddr[7]); cc2420_set_pan_addr(IEEE802154_PANID, shortaddr, longaddr); } leds_off(LEDS_ALL); #ifdef SERIALNUM PRINTF("Ref ID: %u\n", SERIALNUM); #endif PRINTF(CONTIKI_VERSION_STRING " started. "); if(node_id) { PRINTF("Node id is set to %u.\n", node_id); } else { PRINTF("Node id not set\n"); } #if NETSTACK_CONF_WITH_IPV6 memcpy(&uip_lladdr.addr, node_mac, sizeof(uip_lladdr.addr)); /* Setup nullmac-like MAC for 802.15.4 */ /* sicslowpan_init(sicslowmac_init(&cc2420_driver)); */ /* printf(" %s channel %u\n", sicslowmac_driver.name, CC2420_CONF_CHANNEL); */ /* Setup X-MAC for 802.15.4 */ queuebuf_init(); netstack_init(); // NETSTACK_RDC.init(); // NETSTACK_MAC.init(); // NETSTACK_LLSEC.init(); // NETSTACK_NETWORK.init(); printf("%s %s %s, channel check rate %lu Hz, radio channel %u\n", NETSTACK_LLSEC.name, NETSTACK_MAC.name, NETSTACK_RDC.name, CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1 : NETSTACK_RDC.channel_check_interval()), CC2420_CONF_CHANNEL); process_start(&tcpip_process, NULL); printf("Tentative link-local IPv6 address "); { uip_ds6_addr_t *lladdr; int i; lladdr = uip_ds6_get_link_local(-1); for(i = 0; i < 7; ++i) { printf("%02x%02x:", lladdr->ipaddr.u8[i * 2], lladdr->ipaddr.u8[i * 2 + 1]); } printf("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]); } if(!UIP_CONF_IPV6_RPL) { uip_ipaddr_t ipaddr; int i; uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE); printf("Tentative global IPv6 address "); for(i = 0; i < 7; ++i) { printf("%02x%02x:", ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]); } printf("%02x%02x\n", ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]); } #else /* NETSTACK_CONF_WITH_IPV6 */ netstack_init(); //NETSTACK_RDC.init(); //NETSTACK_MAC.init(); //NETSTACK_LLSEC.init(); //NETSTACK_NETWORK.init(); printf("%s %s %s, channel check rate %lu Hz, radio channel %u\n", NETSTACK_LLSEC.name, NETSTACK_MAC.name, NETSTACK_RDC.name, CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1 : NETSTACK_RDC.channel_check_interval()), CC2420_CONF_CHANNEL); #endif /* NETSTACK_CONF_WITH_IPV6 */ #if !NETSTACK_CONF_WITH_IPV4 && !NETSTACK_CONF_WITH_IPV6 uart0_set_input(serial_line_input_byte); serial_line_init(); #endif leds_off(LEDS_GREEN); #if TIMESYNCH_CONF_ENABLED timesynch_init(); timesynch_set_authority_level(linkaddr_node_addr.u8[0]); #endif /* TIMESYNCH_CONF_ENABLED */ #if NETSTACK_CONF_WITH_IPV4 process_start(&tcpip_process, NULL); process_start(&uip_fw_process, NULL); /* Start IP output */ process_start(&slip_process, NULL); slip_set_input_callback(set_gateway); { uip_ipaddr_t hostaddr, netmask; uip_init(); uip_ipaddr(&hostaddr, 172, 16, linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]); uip_ipaddr(&netmask, 255, 255, 0, 0); uip_ipaddr_copy(&meshif.ipaddr, &hostaddr); uip_sethostaddr(&hostaddr); uip_setnetmask(&netmask); uip_over_mesh_set_net(&hostaddr, &netmask); /* uip_fw_register(&slipif);*/ uip_over_mesh_set_gateway_netif(&slipif); uip_fw_default(&meshif); uip_over_mesh_init(UIP_OVER_MESH_CHANNEL); printf("uIP started with IP address %d.%d.%d.%d\n", uip_ipaddr_to_quad(&hostaddr)); } #endif /* NETSTACK_CONF_WITH_IPV4 */ energest_init(); ENERGEST_ON(ENERGEST_TYPE_CPU); print_processes(autostart_processes); autostart_start(autostart_processes); /* * This is the scheduler loop. */ #if DCOSYNCH_CONF_ENABLED timer_set(&mgt_timer, DCOSYNCH_PERIOD * CLOCK_SECOND); #endif watchdog_start(); /* watchdog_stop();*/ while(1) { int r; do { /* Reset watchdog. */ watchdog_periodic(); r = process_run(); } while(r > 0); /* * Idle processing. */ int s = splhigh(); /* Disable interrupts. */ /* uart0_active is for avoiding LPM3 when still sending or receiving */ if(process_nevents() != 0 || uart0_active()) { splx(s); /* Re-enable interrupts. */ } else { static unsigned long irq_energest = 0; #if DCOSYNCH_CONF_ENABLED /* before going down to sleep possibly do some management */ if(timer_expired(&mgt_timer)) { timer_reset(&mgt_timer); msp430_sync_dco(); } #endif /* Re-enable interrupts and go to sleep atomically. */ ENERGEST_SWITCH(ENERGEST_TYPE_CPU, ENERGEST_TYPE_LPM); /* We only want to measure the processing done in IRQs when we are asleep, so we discard the processing time done when we were awake. */ energest_type_set(ENERGEST_TYPE_IRQ, irq_energest); watchdog_stop(); _BIS_SR(GIE | SCG0 | SCG1 | CPUOFF); /* LPM3 sleep. This statement will block until the CPU is woken up by an interrupt that sets the wake up flag. */ /* We get the current processing time for interrupts that was done during the LPM and store it for next time around. */ dint(); irq_energest = energest_type_time(ENERGEST_TYPE_IRQ); eint(); watchdog_start(); ENERGEST_SWITCH(ENERGEST_TYPE_LPM, ENERGEST_TYPE_CPU); } } return 0; }
/*---------------------------------------------------------------------------*/ int main(int argc, char **argv) { /* * Initalize hardware. */ msp430_cpu_init(); clock_init(); leds_init(); leds_on(LEDS_RED); clock_wait(2); uart1_init(115200); /* Must come before first printf */ #if WITH_UIP slip_arch_init(115200); #endif /* WITH_UIP */ clock_wait(1); leds_on(LEDS_GREEN); //ds2411_init(); /* XXX hack: Fix it so that the 802.15.4 MAC address is compatible with an Ethernet MAC address - byte 0 (byte 2 in the DS ID) cannot be odd. */ //ds2411_id[2] &= 0xfe; leds_on(LEDS_BLUE); //xmem_init(); leds_off(LEDS_RED); rtimer_init(); /* * Hardware initialization done! */ node_id = NODE_ID; /* Restore node id if such has been stored in external mem */ //node_id_restore(); /* for setting "hardcoded" IEEE 802.15.4 MAC addresses */ #ifdef IEEE_802154_MAC_ADDRESS { uint8_t ieee[] = IEEE_802154_MAC_ADDRESS; //memcpy(ds2411_id, ieee, sizeof(uip_lladdr.addr)); //ds2411_id[7] = node_id & 0xff; } #endif //random_init(ds2411_id[0] + node_id); leds_off(LEDS_BLUE); /* * Initialize Contiki and our processes. */ process_init(); process_start(&etimer_process, NULL); ctimer_init(); init_platform(); set_rime_addr(); cc2520_init(); { uint8_t longaddr[8]; uint16_t shortaddr; shortaddr = (rimeaddr_node_addr.u8[0] << 8) + rimeaddr_node_addr.u8[1]; memset(longaddr, 0, sizeof(longaddr)); rimeaddr_copy((rimeaddr_t *)&longaddr, &rimeaddr_node_addr); printf("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ", longaddr[0], longaddr[1], longaddr[2], longaddr[3], longaddr[4], longaddr[5], longaddr[6], longaddr[7]); cc2520_set_pan_addr(IEEE802154_PANID, shortaddr, longaddr); } cc2520_set_channel(RF_CHANNEL); printf(CONTIKI_VERSION_STRING " started. "); if(node_id > 0) { printf("Node id is set to %u.\n", node_id); } else { printf("Node id is not set.\n"); } #if WITH_UIP6 /* memcpy(&uip_lladdr.addr, ds2411_id, sizeof(uip_lladdr.addr)); */ memcpy(&uip_lladdr.addr, rimeaddr_node_addr.u8, UIP_LLADDR_LEN > RIMEADDR_SIZE ? RIMEADDR_SIZE : UIP_LLADDR_LEN); /* Setup nullmac-like MAC for 802.15.4 */ /* sicslowpan_init(sicslowmac_init(&cc2520_driver)); */ /* printf(" %s channel %u\n", sicslowmac_driver.name, RF_CHANNEL); */ /* Setup X-MAC for 802.15.4 */ queuebuf_init(); NETSTACK_RDC.init(); NETSTACK_MAC.init(); NETSTACK_NETWORK.init(); printf("%s %s, channel check rate %lu Hz, radio channel %u\n", NETSTACK_MAC.name, NETSTACK_RDC.name, CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1: NETSTACK_RDC.channel_check_interval()), RF_CHANNEL); process_start(&tcpip_process, NULL); printf("Tentative link-local IPv6 address "); { uip_ds6_addr_t *lladdr; int i; lladdr = uip_ds6_get_link_local(-1); for(i = 0; i < 7; ++i) { printf("%02x%02x:", lladdr->ipaddr.u8[i * 2], lladdr->ipaddr.u8[i * 2 + 1]); } printf("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]); } if(!UIP_CONF_IPV6_RPL) { uip_ipaddr_t ipaddr; int i; uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0); uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE); printf("Tentative global IPv6 address "); for(i = 0; i < 7; ++i) { printf("%02x%02x:", ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]); } printf("%02x%02x\n", ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]); } #else /* WITH_UIP6 */ NETSTACK_RDC.init(); NETSTACK_MAC.init(); NETSTACK_NETWORK.init(); printf("%s %s, channel check rate %lu Hz, radio channel %u\n", NETSTACK_MAC.name, NETSTACK_RDC.name, CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0? 1: NETSTACK_RDC.channel_check_interval()), RF_CHANNEL); #endif /* WITH_UIP6 */ #if !WITH_UIP && !WITH_UIP6 uart1_set_input(serial_line_input_byte); serial_line_init(); #endif leds_off(LEDS_GREEN); #if TIMESYNCH_CONF_ENABLED timesynch_init(); timesynch_set_authority_level((rimeaddr_node_addr.u8[0] << 4) + 16); #endif /* TIMESYNCH_CONF_ENABLED */ #if WITH_UIP process_start(&tcpip_process, NULL); process_start(&uip_fw_process, NULL); /* Start IP output */ process_start(&slip_process, NULL); slip_set_input_callback(set_gateway); { uip_ipaddr_t hostaddr, netmask; uip_init(); uip_ipaddr(&hostaddr, 172,16, rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1]); uip_ipaddr(&netmask, 255,255,0,0); uip_ipaddr_copy(&meshif.ipaddr, &hostaddr); uip_sethostaddr(&hostaddr); uip_setnetmask(&netmask); uip_over_mesh_set_net(&hostaddr, &netmask); /* uip_fw_register(&slipif);*/ uip_over_mesh_set_gateway_netif(&slipif); uip_fw_default(&meshif); uip_over_mesh_init(UIP_OVER_MESH_CHANNEL); printf("uIP started with IP address %d.%d.%d.%d\n", uip_ipaddr_to_quad(&hostaddr)); } #endif /* WITH_UIP */ energest_init(); ENERGEST_ON(ENERGEST_TYPE_CPU); watchdog_start(); /* Stop the watchdog */ watchdog_stop(); #if !PROCESS_CONF_NO_PROCESS_NAMES print_processes(autostart_processes); #else /* !PROCESS_CONF_NO_PROCESS_NAMES */ putchar('\n'); /* include putchar() */ #endif /* !PROCESS_CONF_NO_PROCESS_NAMES */ autostart_start(autostart_processes); /* * This is the scheduler loop. */ while(1) { int r; do { /* Reset watchdog. */ watchdog_periodic(); r = process_run(); } while(r > 0); /* * Idle processing. */ int s = splhigh(); /* Disable interrupts. */ /* uart1_active is for avoiding LPM3 when still sending or receiving */ if(process_nevents() != 0 || uart1_active()) { splx(s); /* Re-enable interrupts. */ } else { static unsigned long irq_energest = 0; /* Re-enable interrupts and go to sleep atomically. */ ENERGEST_OFF(ENERGEST_TYPE_CPU); ENERGEST_ON(ENERGEST_TYPE_LPM); /* We only want to measure the processing done in IRQs when we are asleep, so we discard the processing time done when we were awake. */ energest_type_set(ENERGEST_TYPE_IRQ, irq_energest); watchdog_stop(); _BIS_SR(GIE | SCG0 | SCG1 | CPUOFF); /* LPM3 sleep. This statement will block until the CPU is woken up by an interrupt that sets the wake up flag. */ /* We get the current processing time for interrupts that was done during the LPM and store it for next time around. */ dint(); irq_energest = energest_type_time(ENERGEST_TYPE_IRQ); eint(); watchdog_start(); ENERGEST_OFF(ENERGEST_TYPE_LPM); ENERGEST_ON(ENERGEST_TYPE_CPU); } } }
/*---------------------------------------------------------------------------*/ static PT_THREAD(handle_dhcp(process_event_t ev, void *data)) { clock_time_t ticks; PT_BEGIN(&s.pt); // printf("handle_dhcp\n"); init: // printf("init\n"); xid++; s.state = STATE_SENDING; s.ticks = CLOCK_SECOND * 4; while(1) { while(ev != tcpip_event) { tcpip_poll_udp(s.conn); PT_YIELD(&s.pt); } send_discover(); etimer_set(&s.etimer, s.ticks); do { PT_YIELD(&s.pt); // printf("tcpip_event:%d ev:%d uip_newdata():%d msg_for_me():%d\n",tcpip_event, ev,uip_newdata(),msg_for_me()); if(ev == tcpip_event && uip_newdata() && msg_for_me() == DHCPOFFER) { // printf("here2\n"); parse_msg(); s.state = STATE_OFFER_RECEIVED; goto selecting; } } while(!etimer_expired(&s.etimer)); if(s.ticks < CLOCK_SECOND * 60) { s.ticks *= 2; } } selecting: // printf("selecting\n"); xid++; s.ticks = CLOCK_SECOND; do { while(ev != tcpip_event) { tcpip_poll_udp(s.conn); PT_YIELD(&s.pt); } send_request(); etimer_set(&s.etimer, s.ticks); do { PT_YIELD(&s.pt); if(ev == tcpip_event && uip_newdata() && msg_for_me() == DHCPACK) { parse_msg(); s.state = STATE_CONFIG_RECEIVED; goto bound; } } while (!etimer_expired(&s.etimer)); if(s.ticks <= CLOCK_SECOND * 10) { s.ticks += CLOCK_SECOND; } else { goto init; } } while(s.state != STATE_CONFIG_RECEIVED); bound: #if 0 printf("Got IP address %d.%d.%d.%d\n", uip_ipaddr_to_quad(&s.ipaddr)); printf("Got netmask %d.%d.%d.%d\n", uip_ipaddr_to_quad(&s.netmask)); printf("Got DNS server %d.%d.%d.%d\n", uip_ipaddr_to_quad(&s.dnsaddr)); printf("Got default router %d.%d.%d.%d\n", uip_ipaddr_to_quad(&s.default_router)); printf("Lease expires in %ld seconds\n", uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1])); #endif // printf("bound\n"); ip64_dhcpc_configured(&s); #define MAX_TICKS (~((clock_time_t)0) / 2) #define MAX_TICKS32 (~((uint32_t)0)) #define IMIN(a, b) ((a) < (b) ? (a) : (b)) if((uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1]))*CLOCK_SECOND/2 <= MAX_TICKS32) { s.ticks = (uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1]) )*CLOCK_SECOND/2; } else { s.ticks = MAX_TICKS32; } while(s.ticks > 0) { ticks = IMIN(s.ticks, MAX_TICKS); s.ticks -= ticks; etimer_set(&s.etimer, ticks); PT_YIELD_UNTIL(&s.pt, etimer_expired(&s.etimer)); } if((uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1]))*CLOCK_SECOND/2 <= MAX_TICKS32) { s.ticks = (uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1]) )*CLOCK_SECOND/2; } else { s.ticks = MAX_TICKS32; } /* renewing: */ xid++; do { while(ev != tcpip_event) { tcpip_poll_udp(s.conn); PT_YIELD(&s.pt); } send_request(); ticks = IMIN(s.ticks / 2, MAX_TICKS); s.ticks -= ticks; etimer_set(&s.etimer, ticks); do { PT_YIELD(&s.pt); if(ev == tcpip_event && uip_newdata() && msg_for_me() == DHCPACK) { parse_msg(); goto bound; } } while(!etimer_expired(&s.etimer)); } while(s.ticks >= CLOCK_SECOND*3); /* rebinding: */ /* lease_expired: */ ip64_dhcpc_unconfigured(&s); goto init; PT_END(&s.pt); }