ule6lo_status_t ule6loGI_getIp6addr(ule6lo_ipType_t ipType, ule6lo_ip6addr_t* ipAddr, ule6lo_ipMode_t mode) { ule6lo_status_t status = STATUS_NO_DATA; uip_ds6_addr_t *uip_addr = NULL; uint8_t index=0; switch(ipType) { case IP_ADDRESS_LINK_LOCAL: uip_addr = uip_ds6_get_link_local((int8_t)mode); break; case IP_ADDRESS_GLOBAL: uip_addr = uip_ds6_get_global((int8_t)mode); break; default: break; } if(uip_addr !=NULL) { for(index=0;index<16;index++) { ipAddr->u8[index]=uip_addr->ipaddr.u8[index]; } status = STATUS_SUCCESS; } return status; }
/*---------------------------------------------------------------------------*/ void print_addresses(void) { uip_ds6_addr_t *lladdr; printf("link-local IPv6 address: "); lladdr = uip_ds6_get_link_local(-1); if(lladdr != NULL){ print_address(lladdr); printf("\r\n"); } else printf("None\r\n"); printf("global IPv6 address: "); lladdr = uip_ds6_get_global(-1); if(lladdr != NULL){ print_address(lladdr); printf("\r\n"); } else printf("None\r\n"); }
int net_set_mac(uint8_t *mac, uint8_t len) { if ((len > UIP_LLADDR_LEN) || (len != 6 && len != 8)) { NET_ERR("Wrong ll addr len, len %d, max %d\n", len, UIP_LLADDR_LEN); return -EINVAL; } linkaddr_set_node_addr((linkaddr_t *)mac); #ifdef CONFIG_NETWORKING_WITH_IPV6 { uip_ds6_addr_t *lladdr; uip_ds6_set_lladdr((uip_lladdr_t *)mac); lladdr = uip_ds6_get_link_local(-1); NET_DBG("Tentative link-local IPv6 address "); PRINT6ADDR(&lladdr->ipaddr); PRINTF("\n"); lladdr->state = ADDR_AUTOCONF; } #endif return 0; }
/** * \brief Start the network stack */ static void start_network(void) { #if NETSTACK_CONF_WITH_IPV6 #ifndef CONTIKI_WAIT_FOR_MAC memcpy(&uip_lladdr.addr, serial_id, sizeof(uip_lladdr.addr)); #endif process_start(&tcpip_process,NULL); #ifdef __CYGWIN__ process_start(&wpcap_process, NULL); #endif 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]); } /* make it hardcoded... */ lladdr->state = ADDR_AUTOCONF; printf("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]); } #elif NETSTACK_CONF_WITH_IPV4 process_start(&tcpip_process, NULL); #endif autostart_start(autostart_processes); }
/*---------------------------------------------------------------------------*/ static void handle_dao_timer(void *ptr) { rpl_instance_t *instance; instance = (rpl_instance_t *)ptr; if(!dio_send_ok && uip_ds6_get_link_local(ADDR_PREFERRED) == NULL) { PRINTF("RPL: Postpone DAO transmission\n"); ctimer_set(&instance->dao_timer, CLOCK_SECOND, handle_dao_timer, instance); return; } /* Send the DAO to the DAO parent set -- the preferred parent in our case. */ if(instance->current_dag->preferred_parent != NULL) { PRINTF("RPL: handle_dao_timer - sending DAO\n"); /* Set the route lifetime to the default value. */ dao_output(instance->current_dag->preferred_parent, instance->default_lifetime); } else { PRINTF("RPL: No suitable DAO parent\n"); } ctimer_stop(&instance->dao_timer); if(etimer_expired(&instance->dao_lifetime_timer.etimer)) { set_dao_lifetime_timer(instance); } }
/*---------------------------------------------------------------------------*/ void uip_ds6_select_src(uip_ipaddr_t *src, uip_ipaddr_t *dst) { uint8_t best = 0; /* number of bit in common with best match */ uint8_t n = 0; uip_ds6_addr_t *matchaddr = NULL; if(!uip_is_addr_link_local(dst) && (!uip_is_addr_mcast(dst) || uip_is_addr_routable_mcast(dst))) { /* find longest match */ for(locaddr = uip_ds6_if.addr_list; locaddr < uip_ds6_if.addr_list + UIP_DS6_ADDR_NB; locaddr++) { /* Only preferred global (not link-local) addresses */ if(locaddr->isused && locaddr->state == ADDR_PREFERRED && !uip_is_addr_link_local(&locaddr->ipaddr)) { n = get_match_length(dst, &locaddr->ipaddr); if(n >= best) { best = n; matchaddr = locaddr; } } } } else { matchaddr = uip_ds6_get_link_local(ADDR_PREFERRED); } /* use the :: (unspecified address) as source if no match found */ if(matchaddr == NULL) { uip_create_unspecified(src); } else { uip_ipaddr_copy(src, &matchaddr->ipaddr); } }
/*---------------------------------------------------------------------------*/ static void handle_dao_timer(struct net_buf *not_used, void *ptr) { rpl_instance_t *instance; #if RPL_CONF_MULTICAST uip_mcast6_route_t *mcast_route; uint8_t i; #endif instance = (rpl_instance_t *)ptr; if(!dio_send_ok && uip_ds6_get_link_local(ADDR_PREFERRED) == NULL) { PRINTF("RPL: Postpone DAO transmission\n"); ctimer_set(NULL, &instance->dao_timer, CLOCK_SECOND, handle_dao_timer, instance); return; } /* Send the DAO to the DAO parent set -- the preferred parent in our case. */ if(instance->current_dag->preferred_parent != NULL) { PRINTF("RPL: handle_dao_timer - sending DAO\n"); /* Set the route lifetime to the default value. */ dao_output(instance->current_dag->preferred_parent, instance->default_lifetime); #if RPL_CONF_MULTICAST /* Send DAOs for multicast prefixes only if the instance is in MOP 3 */ if(instance->mop == RPL_MOP_STORING_MULTICAST) { /* Send a DAO for own multicast addresses */ for(i = 0; i < UIP_DS6_MADDR_NB; i++) { if(uip_ds6_if.maddr_list[i].isused && uip_is_addr_mcast_global(&uip_ds6_if.maddr_list[i].ipaddr)) { dao_output_target(instance->current_dag->preferred_parent, &uip_ds6_if.maddr_list[i].ipaddr, RPL_MCAST_LIFETIME); } } /* Iterate over multicast routes and send DAOs */ mcast_route = uip_mcast6_route_list_head(); while(mcast_route != NULL) { /* Don't send if it's also our own address, done that already */ if(uip_ds6_maddr_lookup(&mcast_route->group) == NULL) { dao_output_target(instance->current_dag->preferred_parent, &mcast_route->group, RPL_MCAST_LIFETIME); } mcast_route = list_item_next(mcast_route); } } #endif } else { PRINTF("RPL: No suitable DAO parent\n"); } ctimer_stop(&instance->dao_timer); if(etimer_expired(&instance->dao_lifetime_timer.etimer)) { set_dao_lifetime_timer(instance); } }
/*---------------------------------------------------------------------------*/ static void handle_dio_timer(struct net_buf *not_used, void *ptr) { rpl_instance_t *instance; instance = (rpl_instance_t *)ptr; PRINTF("RPL: DIO Timer triggered\n"); if(!dio_send_ok) { if(uip_ds6_get_link_local(ADDR_PREFERRED) != NULL) { dio_send_ok = 1; } else { PRINTF("RPL: Postponing DIO transmission since link local address is not ok\n"); ctimer_set(NULL, &instance->dio_timer, CLOCK_SECOND, &handle_dio_timer, instance); return; } } if(instance->dio_send) { /* send DIO if counter is less than desired redundancy */ if(instance->dio_redundancy != 0 && instance->dio_counter < instance->dio_redundancy) { #if RPL_CONF_STATS instance->dio_totsend++; #endif /* RPL_CONF_STATS */ dio_output(instance, NULL); } else { PRINTF("RPL: Supressing DIO transmission (%d >= %d)\n", instance->dio_counter, instance->dio_redundancy); } instance->dio_send = 0; PRINTF("RPL: Scheduling DIO timer %lu ticks in future (sent)\n", instance->dio_next_delay); ctimer_set(NULL, &instance->dio_timer, instance->dio_next_delay, handle_dio_timer, instance); } else { /* check if we need to double interval */ if(instance->dio_intcurrent < instance->dio_intmin + instance->dio_intdoubl) { instance->dio_intcurrent++; PRINTF("RPL: DIO Timer interval doubled %d\n", instance->dio_intcurrent); } new_dio_interval(instance); } #if DEBUG rpl_print_neighbor_list(); #endif }
static bool get_local_address(struct sol_network_link_addr *addr) { uip_ds6_addr_t *dsaddr; dsaddr = uip_ds6_get_global(-1); if (!dsaddr) dsaddr = uip_ds6_get_link_local(-1); SOL_NULL_CHECK(dsaddr, false); addr->family = SOL_NETWORK_FAMILY_INET6; addr->port = 0; memcpy(&addr->addr.in6, &dsaddr->ipaddr, sizeof(addr->addr.in6)); return true; }
static void start_uip6(void) { NETSTACK_NETWORK.init(); #ifndef WITH_SLIP_RADIO process_start(&tcpip_process, NULL); #else #if WITH_SLIP_RADIO == 0 process_start(&tcpip_process, NULL); #endif #endif /* WITH_SLIP_RADIO */ #if DEBUG 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]); /* make it hardcoded... */ } lladdr->state = ADDR_AUTOCONF; PRINTF("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]); } #endif /* DEBUG */ 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]); } }
static void handle_dao_timer(void *ptr) { rpl_instance_t *instance; instance = (rpl_instance_t *)ptr; if(!dio_send_ok && uip_ds6_get_link_local(ADDR_PREFERRED) == NULL) { PRINTF("RPL: Postpone DAO transmission\n"); ctimer_set(&instance->dao_timer, CLOCK_SECOND, handle_dao_timer, instance); return; } /* Send the DAO to the DAO parent set -- the preferred parent in our case. */ if(instance->current_dag->preferred_parent != NULL) { PRINTF("RPL: handle_dao_timer - sending DAO\n"); /* Set the route lifetime to the default value. */ dao_output(instance->current_dag->preferred_parent, instance->default_lifetime, NULL); #if UIP_IPV6_MULTICAST_RPL if(instance->mop == RPL_MOP_STORING_MULTICAST) { /* Send a DAO for own multicast addresses */ for(i = 0; i < UIP_DS6_MADDR_NB; i++) { if(uip_ds6_if.maddr_list[i].isused && uip_is_addr_mcast_global(&uip_ds6_if.maddr_list[i].ipaddr)) { dao_output(instance->current_dag->preferred_parent, RPL_MCAST_LIFETIME, &uip_ds6_if.maddr_list[i].ipaddr); } } /* Iterate multicast routes and send DAOs */ for(i = 0; i < UIP_DS6_MCAST_ROUTES; i++) { /* Don't send if it's also our own address, done that already */ if(uip_ds6_mcast_table[i].isused) { if(uip_ds6_maddr_lookup(&uip_ds6_mcast_table[i].group) == NULL) { dao_output(instance->current_dag->preferred_parent, RPL_MCAST_LIFETIME, &uip_ds6_mcast_table[i].group); } } } } #endif } else { PRINTF("RPL: No suitable DAO parent\n"); } ctimer_stop(&instance->dao_timer); }
static void handle_dio_timer(void *ptr) { rpl_dag_t *dag; dag = (rpl_dag_t *)ptr; PRINTF("RPL: DIO Timer triggered"); if(!dio_send_ok) { if(uip_ds6_get_link_local(ADDR_PREFERRED) != NULL) { dio_send_ok = 1; } else { PRINTF("RPL: Postponing DIO transmission since link local address is not ok"); ctimer_set(&dag->dio_timer, CLOCK_SECOND, &handle_dio_timer, dag); return; } } if(dag->dio_send) { /* send DIO if counter is less than desired redundancy */ if(dag->dio_counter < dag->dio_redundancy) { #if RPL_CONF_STATS dag->dio_totsend++; #endif /* RPL_CONF_STATS */ dio_output(dag, NULL); } else { PRINTF("RPL: Supressing DIO transmission (Xd >= Xd)");//, dag->dio_counter, dag->dio_redundancy); } dag->dio_send = 0; PRINTF("RPL: Scheduling DIO timer Xu ticks in future (sent)");//, dag->dio_next_delay); ctimer_set(&dag->dio_timer, dag->dio_next_delay, handle_dio_timer, dag); } else { /* check if we need to double interval */ if(dag->dio_intcurrent < dag->dio_intmin + dag->dio_intdoubl) { dag->dio_intcurrent++; PRINTF("RPL: DIO Timer interval doubled Xd");//, dag->dio_intcurrent); } new_dio_interval(dag); } }
static void handle_dao_timer(void *ptr) { rpl_dag_t *dag; dag = (rpl_dag_t *)ptr; if (!dio_send_ok && uip_ds6_get_link_local(ADDR_PREFERRED) == NULL) { PRINTF("RPL: Postpone DAO transmission... "); ctimer_set(&dag->dao_timer, CLOCK_SECOND, handle_dao_timer, dag); return; } /* Send the DAO to the DAO parent set -- the preferred parent in our case. */ if(dag->preferred_parent != NULL) { PRINTF("RPL: handle_dao_timer - sending DAO"); /* Set the route lifetime to the default value. */ dao_output(dag->preferred_parent, dag->default_lifetime); } else { PRINTF("RPL: No suitable DAO parent"); } ctimer_stop(&dag->dao_timer); }
/*---------------------------------------------------------------------------*/ static void tcpip_handler(void) { uint8_t *appdata; uint8_t reqID; uint32_t sinklastTX; if (uip_newdata()) { // Collect data from received payload appdata = (uint8_t *) uip_appdata; // printf ("sizeof(appdata): %u\n", uip_datalen()); reqID = *appdata; // Check for next lines appdata += 2; // 01 for alignment memcpy(&sinklastTX, appdata, 4); // Stop when reaching condition if (reqID >= STOPCONDITION) { stopCond = 1; printf("I am stopping \n"); return; } //Get the time for receiving response uint32_t time = get_time_ms(); // Updating statistics information for the node // PRINTF("Updating statistics information\n"); if (nodeInf.nodeID == 0) { // get nodeId for the first time uip_ds6_addr_t *addr; addr = uip_ds6_get_link_local(-1); nodeInf.nodeID = (addr->ipaddr.u8[sizeof(uip_ipaddr_t) - 2] << 8) + addr->ipaddr.u8[sizeof(uip_ipaddr_t) - 1]; } uint8_t oldresID = nodeInf.resID; // Store the resID nodeInf.senderlastRX = time; /*if (nodeInf.resID==0){ // first time received a request } else { // received at least one request }*/ nodeInf.resID = reqID; // Update resID to be identical with its request's ID // nodeInf.senderlastTX = time; nodeInf.numReq++; uint16_t sinkTX = (uint16_t) (time - sinklastTX) & 0xffff; if (nodeInf.sinkMinTX > sinkTX) { nodeInf.sinkMinTX = sinkTX; } if (nodeInf.sinkMaxTX < sinkTX) { nodeInf.sinkMinTX = sinkTX; } /*PRINTF("RX_Time:%lu nodeID=%u resID=%u sinkTX=%u\n", time, nodeInf.nodeID, nodeInf.resID, sinkTX);*/ printf("RX: Time : %lu sinklastTX: %lu reqID: %u \n", time, sinklastTX, reqID); //client_send(); } }
/*--------------------------------------------------------------------------*/ int main(int argc, char **argv) { /* * Initalize hardware. */ msp430_cpu_init(); clock_init(); leds_init(); leds_on(LEDS_RED); uart1_init(BAUD2UBR(115200)); /* Must come before first printf */ #if NETSTACK_CONF_WITH_IPV4 slip_arch_init(BAUD2UBR(115200)); #endif /* NETSTACK_CONF_WITH_IPV4 */ leds_on(LEDS_GREEN); /* xmem_init(); */ rtimer_init(); lcd_init(); PRINTF(CONTIKI_VERSION_STRING "\n"); /* * Hardware initialization done! */ leds_on(LEDS_RED); /* Restore node id if such has been stored in external mem */ // node_id_restore(); #ifdef NODEID node_id = NODEID; #ifdef BURN_NODEID flash_setup(); flash_clear(0x1800); flash_write(0x1800, node_id); flash_done(); #endif /* BURN_NODEID */ #endif /* NODE_ID */ if(node_id == 0) { node_id = *((unsigned short *)0x1800); } memset(node_mac, 0, sizeof(node_mac)); node_mac[6] = node_id >> 8; node_mac[7] = node_id & 0xff; /* for setting "hardcoded" IEEE 802.15.4 MAC addresses */ #ifdef MAC_1 { uint8_t ieee[] = { MAC_1, MAC_2, MAC_3, MAC_4, MAC_5, MAC_6, MAC_7, MAC_8 }; memcpy(node_mac, ieee, sizeof(uip_lladdr.addr)); } #endif /* * Initialize Contiki and our processes. */ process_init(); process_start(&etimer_process, NULL); ctimer_init(); 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("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); } leds_off(LEDS_ALL); if(node_id > 0) { PRINTF("Node id %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 */ queuebuf_init(); NETSTACK_RDC.init(); NETSTACK_MAC.init(); NETSTACK_NETWORK.init(); printf("%s %lu %u\n", 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("IPv6 "); { 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_RDC.init(); NETSTACK_MAC.init(); NETSTACK_NETWORK.init(); printf("%s %lu %u\n", 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_IPV6 uart1_set_input(serial_line_input_byte); serial_line_init(); #endif #if TIMESYNCH_CONF_ENABLED timesynch_init(); timesynch_set_authority_level(linkaddr_node_addr.u8[0]); #endif /* TIMESYNCH_CONF_ENABLED */ /* process_start(&sensors_process, NULL); SENSORS_ACTIVATE(button_sensor);*/ energest_init(); ENERGEST_ON(ENERGEST_TYPE_CPU); print_processes(autostart_processes); autostart_start(autostart_processes); duty_cycle_scroller_start(CLOCK_SECOND * 2); /* * This is the scheduler loop. */ watchdog_start(); watchdog_stop(); /* Stop the wdt... */ 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_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); } } }
struct net_context *net_context_get(enum ip_protocol ip_proto, const struct net_addr *remote_addr, uint16_t remote_port, struct net_addr *local_addr, uint16_t local_port) { #ifdef CONFIG_NETWORKING_WITH_IPV6 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; const uip_ds6_addr_t *uip_addr; uip_ipaddr_t ipaddr; #endif int i; struct net_context *context = NULL; /* User must provide storage for the local address. */ if (!local_addr) { return NULL; } #ifdef CONFIG_NETWORKING_WITH_IPV6 if (memcmp(&local_addr->in6_addr, &in6addr_any, sizeof(in6addr_any)) == 0) { uip_addr = uip_ds6_get_global(-1); if (!uip_addr) { uip_addr = uip_ds6_get_link_local(-1); } if (!uip_addr) { return NULL; } memcpy(&local_addr->in6_addr, &uip_addr->ipaddr, sizeof(struct in6_addr)); } #else if (local_addr->in_addr.s_addr == INADDR_ANY) { uip_gethostaddr((uip_ipaddr_t *)&local_addr->in_addr); } #endif nano_sem_take(&contexts_lock, TICKS_UNLIMITED); if (local_port) { if (context_port_used(ip_proto, local_port, local_addr) < 0) { return NULL; } } else { do { local_port = random_rand() | 0x8000; } while (context_port_used(ip_proto, local_port, local_addr) == -EEXIST); } for (i = 0; i < NET_MAX_CONTEXT; i++) { if (!contexts[i].tuple.ip_proto) { contexts[i].tuple.ip_proto = ip_proto; contexts[i].tuple.remote_addr = (struct net_addr *)remote_addr; contexts[i].tuple.remote_port = remote_port; contexts[i].tuple.local_addr = (struct net_addr *)local_addr; contexts[i].tuple.local_port = local_port; context = &contexts[i]; break; } } context_sem_give(&contexts_lock); /* Set our local address */ #ifdef CONFIG_NETWORKING_WITH_IPV6 memcpy(&ipaddr.u8, local_addr->in6_addr.s6_addr, sizeof(ipaddr.u8)); if (uip_is_addr_mcast(&ipaddr)) { uip_ds6_maddr_add(&ipaddr); } else { uip_ds6_addr_add(&ipaddr, 0, ADDR_MANUAL); } #endif return context; }
/*---------------------------------------------------------------------------*/ 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 */ }
/*---------------------------------------------------------------------------*/ 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); }
void lowlevel_init() { rimeaddr_t rimeaddr; uint16_t *fsize = (uint16_t *)0x1FF8004C; uint16_t *uid96 = (uint16_t *)0x1FF80050; uint32_t *dbgmcu_id = (uint32_t *)0xE0042000; uint16_t uid16; dbg_setup_uart(); printf("\nInitialising\n"); printf("Device ID: 0x%03x, silicon rev: 0x%04x\n", (unsigned int)*dbgmcu_id & 0x0fff, (unsigned int)(*dbgmcu_id >> 16) & 0xffff); printf("Flash size is %d kB\n", *fsize); printf("UID96 is %04x %04x %04x %04x %04x %04x\n", uid96[0], uid96[1], uid96[2], uid96[3], uid96[4], uid96[5]); uid16 = u101_chksum(0, (uint8_t *)uid96, 12); printf("Pseudo-UID16 is %02x\n", uid16); clock_init(); rtimer_init(); process_init(); process_start(&etimer_process, NULL); ctimer_init(); serial_line_init(); leds_init(); #ifdef U101_RF231 printf("Low-level networking init\n"); queuebuf_init(); NETSTACK_RADIO.init(); NETSTACK_RADIO.on(); NETSTACK_MAC.init(); NETSTACK_RDC.on(); #endif #if 0 printf("%s %s, channel check rate %u 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 //memcpy(&uip_lladdr.addr, ds2411_id, sizeof(uip_lladdr.addr)); #if defined WITH_UIP6 printf("\nAddresses [%u max]\n", UIP_DS6_ADDR_NB); for (i=0; i<UIP_DS6_ADDR_NB; i++) { if (uip_ds6_if.addr_list[i].isused) { uip_debug_ipaddr_print(&uip_ds6_if.addr_list[i].ipaddr); printf("\n"); } } #endif /* Temporarily, we use a part of the STM32's UID as address. It seems like uid_0[1] is usable in our batch. Note that this does not guarrantee unique addresses. */ rimeaddr.u8[0] = (uint8_t)(uid16 >> 8) & 0xff; rimeaddr.u8[1] = (uint8_t)(uid16 & 0xff); printf("Rime address is: %02x.%02x\n", rimeaddr.u8[0], rimeaddr.u8[1]); #if NETSTACK_CONF_RADIO == rf230_driver rf230_set_pan_addr(IEEE802154_PANID, 0, (uint8_t *)&rimeaddr.u8); rf230_set_channel(CHANNEL_802_15_4); rimeaddr_set_node_addr(&rimeaddr); #endif process_start(&tcpip_process, NULL); #if defined WITH_UIP6 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]); } printf("\n"); #endif //em_init(); leds_on(LEDS_ALL); print_local_addresses(); #ifdef WITH_UIP printf("Starting tcpip and fw\n"); process_start(&tcpip_process, NULL); process_start(&uip_fw_process, NULL); #endif #ifdef WITH_USB //(void)setup_usb(); process_start(&usbeth_process, NULL); #endif //process_start(&lsm303_process, NULL); //process_start(&eriks_process, NULL); printf("Processes running\n"); }
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 */ }
/*---------------------------------------------------------------------------*/ void init_net(void) { #ifndef WITH_SLIP uint8_t i; id.u32[0] = djb2_hash((const uint8_t *)&(SIM->UIDH), 8); /* Use SIM_UIDH, SIM_UIDMH for first half */ id.u32[1] = djb2_hash((const uint8_t *)&(SIM->UIDML), 8); /* Use SIM_UIDML, SIM_UIDL for second half */ id.u8[0] |= 0x02; /* Set the Local/Universal bit to Local */ #else /* Use fixed address for border router. */ id.u32[0] = 0x00000000; id.u32[1] = 0x00000000; id.u8[0] = 0x02; id.u8[7] = 0x01; #endif #if NETSTACK_CONF_WITH_IPV6 set_rime_addr(); NETSTACK_RADIO.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); rf230_set_pan_addr(IEEE802154_CONF_PANID, shortaddr, longaddr); } rf230_set_channel(RF_CHANNEL); memcpy(&uip_lladdr.addr, id.u8, sizeof(uip_lladdr.addr)); queuebuf_init(); NETSTACK_RDC.init(); NETSTACK_MAC.init(); NETSTACK_NETWORK.init(); PRINTF("%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()), 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("%04x:", lladdr->ipaddr.u8[i * 2] * 256 + lladdr->ipaddr.u8[i * 2 + 1]); } PRINTF("%04x\n", lladdr->ipaddr.u8[14] * 256 + lladdr->ipaddr.u8[15]); } if(!UIP_CONF_IPV6_RPL) { uip_ipaddr_t ipaddr; int i; uip_ip6addr(&ipaddr, 0xfdfd, 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("%04x:", ipaddr.u8[i * 2] * 256 + ipaddr.u8[i * 2 + 1]); } PRINTF("%04x\n", ipaddr.u8[7 * 2] * 256 + ipaddr.u8[7 * 2 + 1]); } #else /* If no radio stack should be used only turn on radio and set it to sleep for minimal power consumption */ rf230_init(); rf230_driver.off(); #endif /* NETSTACK_CONF_WITH_IPV6 */ }
int main(int argc, char *argv[]) { node_id_restore(); /* init system: clocks, board etc */ system_init(); sio2host_init(); leds_init(); leds_on(LEDS_ALL); system_interrupt_enable_global(); flash_init(); delay_init(); /* Initialize Contiki and our processes. */ #ifdef LOW_POWER_MODE configure_tc3(); #else clock_init(); #endif process_init(); ctimer_init(); rtimer_init(); process_start(&etimer_process, NULL); /* Set MAC address and node ID */ #ifdef NODEID node_id = NODEID; #ifdef BURN_NODEID node_id_burn(node_id); #endif /* BURN_NODEID */ #else/* NODE_ID */ #endif /* NODE_ID */ printf("\r\n\n\n\n Starting the SmartConnect-6LoWPAN \r\n Platform : Atmel IoT device \r\n"); print_reset_causes(); netstack_init(); #if BOARD == SAMR21_XPLAINED_PRO eui64 = edbg_eui_read_eui64(); SetIEEEAddr(eui64); #else SetIEEEAddr(node_mac); #endif set_link_addr(); rf_set_channel(RF_CHANNEL); printf("\r\n Configured RF channel: %d\r\n", rf_get_channel()); leds_off(LEDS_ALL); process_start(&sensors_process, NULL); energest_init(); ENERGEST_ON(ENERGEST_TYPE_CPU); if(node_id > 0) { printf(" Node id %u.\r\n", node_id); } else { printf(" Node id not set.\r\n"); } /* Setup nullmac-like MAC for 802.15.4 */ #if SAMD memcpy(&uip_lladdr.addr, node_mac, sizeof(uip_lladdr.addr)); #else memcpy(&uip_lladdr.addr, eui64, sizeof(uip_lladdr.addr)); #endif queuebuf_init(); printf(" %s %lu %d\r\n", NETSTACK_RDC.name, (uint32_t) (CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1: NETSTACK_RDC.channel_check_interval())), RF_CHANNEL); process_start(&tcpip_process, NULL); printf(" 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\r\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]); } { uip_ipaddr_t ipaddr; int i; uip_ip6addr(&ipaddr, 0xfc00, 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\r\n", ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]); } print_processes(autostart_processes); /* set up AES key */ #if ((THSQ_CONF_NETSTACK) & THSQ_CONF_AES) #ifndef NETSTACK_AES_KEY #error Please define NETSTACK_AES_KEY! #endif /* NETSTACK_AES_KEY */ { const uint8_t key[] = NETSTACK_AES_KEY; netstack_aes_set_key(key); } printf("AES encryption is enabled\n"); #else /* ((THSQ_CONF_NETSTACK) & THSQ_CONF_AES) */ printf("\r\n Warning: AES encryption is disabled\n"); #endif /* ((THSQ_CONF_NETSTACK) & THSQ_CONF_AES) */ #ifdef ENABLE_LEDCTRL ledctrl_init(); #endif autostart_start(autostart_processes); while(1){ int r = 0; serial_data_handler(); do { r = process_run(); } while(r > 0); } }
/*---------------------------------------------------------------------------*/ 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) { #if UIP_CONF_IPV6 #if UIP_CONF_IPV6_RPL printf(CONTIKI_VERSION_STRING " started with IPV6, RPL\n"); #else printf(CONTIKI_VERSION_STRING " started with IPV6\n"); #endif #else printf(CONTIKI_VERSION_STRING " started\n"); #endif /* crappy way of remembering and accessing argc/v */ contiki_argc = argc; contiki_argv = argv; /* native under windows is hardcoded to use the first one or two args */ /* for wpcap configuration so this needs to be "removed" from */ /* contiki_args (used by the native-border-router) */ #ifdef __CYGWIN__ contiki_argc--; contiki_argv++; #ifdef UIP_FALLBACK_INTERFACE contiki_argc--; contiki_argv++; #endif #endif process_init(); process_start(&etimer_process, NULL); ctimer_init(); set_rime_addr(); queuebuf_init(); netstack_init(); printf("MAC %s RDC %s NETWORK %s\n", NETSTACK_MAC.name, NETSTACK_RDC.name, NETSTACK_NETWORK.name); #if WITH_UIP6 memcpy(&uip_lladdr.addr, serial_id, sizeof(uip_lladdr.addr)); process_start(&tcpip_process, NULL); #ifdef __CYGWIN__ process_start(&wpcap_process, NULL); #endif 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]); } /* make it hardcoded... */ lladdr->state = ADDR_AUTOCONF; printf("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]); } #else process_start(&tcpip_process, NULL); #endif serial_line_init(); autostart_start(autostart_processes); /* Make standard output unbuffered. */ setvbuf(stdout, (char *)NULL, _IONBF, 0); select_set_callback(STDIN_FILENO, &stdin_fd); simple_rpl_init(); ip64_init(); while(1) { fd_set fdr; fd_set fdw; int maxfd; int i; int retval; struct timeval tv; retval = process_run(); tv.tv_sec = 0; tv.tv_usec = retval ? 1 : 1000; FD_ZERO(&fdr); FD_ZERO(&fdw); maxfd = 0; for(i = 0; i <= select_max; i++) { if(select_callback[i] != NULL && select_callback[i]->set_fd(&fdr, &fdw)) { maxfd = i; } } retval = select(maxfd + 1, &fdr, &fdw, NULL, &tv); if(retval < 0) { perror("select"); } else if(retval > 0) { /* timeout => retval == 0 */ for(i = 0; i <= maxfd; i++) { if(select_callback[i] != NULL) { select_callback[i]->handle_fd(&fdr, &fdw); } } } etimer_request_poll(); } return 0; }
/*---------------------------------------------------------------------------*/ void cetic_6lbr_init(void) { uip_ds6_addr_t *local = uip_ds6_get_link_local(-1); uip_ipaddr_copy(&wsn_ip_local_addr, &local->ipaddr); LOG6LBR_6ADDR(INFO, &wsn_ip_local_addr, "Tentative local IPv6 address "); eth_mac64_addr.addr[0] = eth_mac_addr[0]; eth_mac64_addr.addr[1] = eth_mac_addr[1]; eth_mac64_addr.addr[2] = eth_mac_addr[2]; eth_mac64_addr.addr[3] = CETIC_6LBR_ETH_EXT_A; eth_mac64_addr.addr[4] = CETIC_6LBR_ETH_EXT_B; eth_mac64_addr.addr[5] = eth_mac_addr[3]; eth_mac64_addr.addr[6] = eth_mac_addr[4]; eth_mac64_addr.addr[7] = eth_mac_addr[5]; #if CETIC_6LBR_SMARTBRIDGE if((nvm_data.mode & CETIC_MODE_WAIT_RA_MASK) == 0) //Manual configuration { memcpy(wsn_net_prefix.u8, &nvm_data.wsn_net_prefix, sizeof(nvm_data.wsn_net_prefix)); wsn_net_prefix_len = nvm_data.wsn_net_prefix_len; if((nvm_data.mode & CETIC_MODE_WSN_AUTOCONF) != 0) //Address auto configuration { uip_ipaddr_copy(&wsn_ip_addr, &wsn_net_prefix); uip_ds6_set_addr_iid(&wsn_ip_addr, &uip_lladdr); uip_ds6_addr_add(&wsn_ip_addr, 0, ADDR_AUTOCONF); } else { memcpy(wsn_ip_addr.u8, &nvm_data.wsn_ip_addr, sizeof(nvm_data.wsn_ip_addr)); uip_ds6_addr_add(&wsn_ip_addr, 0, ADDR_MANUAL); } LOG6LBR_6ADDR(INFO, &wsn_ip_addr, "Tentative global IPv6 address "); memcpy(eth_dft_router.u8, &nvm_data.eth_dft_router, sizeof(nvm_data.eth_dft_router)); if ( !uip_is_addr_unspecified(ð_dft_router) ) { uip_ds6_defrt_add(ð_dft_router, 0); } uip_ipaddr_t dns; memcpy(dns.u8, &nvm_data.dns_server, sizeof(nvm_data.dns_server)); uip_nameserver_update(&dns, UIP_NAMESERVER_INFINITE_LIFETIME); } else { //End manual configuration uip_create_unspecified(&wsn_net_prefix); wsn_net_prefix_len = 0; uip_create_unspecified(&wsn_ip_addr); } #endif #if CETIC_6LBR_ROUTER //WSN network configuration memcpy(wsn_net_prefix.u8, &nvm_data.wsn_net_prefix, sizeof(nvm_data.wsn_net_prefix)); wsn_net_prefix_len = nvm_data.wsn_net_prefix_len; if((nvm_data.mode & CETIC_MODE_WSN_AUTOCONF) != 0) //Address auto configuration { uip_ipaddr_copy(&wsn_ip_addr, &wsn_net_prefix); uip_ds6_set_addr_iid(&wsn_ip_addr, &uip_lladdr); uip_ds6_addr_add(&wsn_ip_addr, 0, ADDR_AUTOCONF); } else { memcpy(wsn_ip_addr.u8, &nvm_data.wsn_ip_addr, sizeof(nvm_data.wsn_ip_addr)); uip_ds6_addr_add(&wsn_ip_addr, 0, ADDR_MANUAL); } LOG6LBR_6ADDR(INFO, &wsn_ip_addr, "Tentative global IPv6 address (WSN) "); uip_ipaddr_t dns; memcpy(dns.u8, &nvm_data.dns_server, sizeof(nvm_data.dns_server)); uip_nameserver_update(&dns, UIP_NAMESERVER_INFINITE_LIFETIME); //Ethernet network configuration memcpy(eth_net_prefix.u8, &nvm_data.eth_net_prefix, sizeof(nvm_data.eth_net_prefix)); memcpy(eth_dft_router.u8, &nvm_data.eth_dft_router, sizeof(nvm_data.eth_dft_router)); if ( !uip_is_addr_unspecified(ð_dft_router) ) { uip_ds6_defrt_add(ð_dft_router, 0); } if((nvm_data.mode & CETIC_MODE_ETH_AUTOCONF) != 0) //Address auto configuration { uip_ipaddr_copy(ð_ip_addr, ð_net_prefix); uip_ds6_set_addr_iid(ð_ip_addr, ð_mac64_addr); uip_ds6_addr_add(ð_ip_addr, 0, ADDR_AUTOCONF); } else { memcpy(eth_ip_addr.u8, &nvm_data.eth_ip_addr, sizeof(nvm_data.eth_ip_addr)); uip_ds6_addr_add(ð_ip_addr, 0, ADDR_MANUAL); } LOG6LBR_6ADDR(INFO, ð_ip_addr, "Tentative global IPv6 address (ETH) "); //Ugly hack : in order to set WSN local address as the default address //We must add it afterwards as uip_ds6_addr_add allocates addr from the end of the list uip_ds6_addr_rm(local); uip_create_linklocal_prefix(ð_ip_local_addr); uip_ds6_set_addr_iid(ð_ip_local_addr, ð_mac64_addr); uip_ds6_addr_add(ð_ip_local_addr, 0, ADDR_AUTOCONF); uip_ds6_addr_add(&wsn_ip_local_addr, 0, ADDR_AUTOCONF); //Prefix and RA configuration #if CETIC_6LBR_WITH_RPL uint8_t publish = (nvm_data.ra_prefix_flags & CETIC_6LBR_MODE_SEND_PIO) != 0; uip_ds6_prefix_add(ð_net_prefix, nvm_data.eth_net_prefix_len, publish, nvm_data.ra_prefix_flags, nvm_data.ra_prefix_vtime, nvm_data.ra_prefix_ptime); #else uip_ds6_prefix_add(ð_net_prefix, nvm_data.eth_net_prefix_len, 0, 0, 0, 0); uint8_t publish = (nvm_data.ra_prefix_flags & CETIC_6LBR_MODE_SEND_PIO) != 0; uip_ds6_prefix_add(&wsn_net_prefix, nvm_data.wsn_net_prefix_len, publish, nvm_data.ra_prefix_flags, nvm_data.ra_prefix_vtime, nvm_data.ra_prefix_ptime); #endif #if CETIC_6LBR_WITH_RPL if ((nvm_data.ra_rio_flags & CETIC_6LBR_MODE_SEND_RIO) != 0 ) { uip_ds6_route_info_add(&wsn_net_prefix, nvm_data.wsn_net_prefix_len, nvm_data.ra_rio_flags, nvm_data.ra_rio_lifetime); } #endif if ((nvm_data.mode & CETIC_MODE_ROUTER_RA_DAEMON) != 0 ) { LOG6LBR_INFO("RA Daemon enabled\n"); } else { LOG6LBR_INFO("RA Daemon disabled\n"); } #endif }
/*---------------------------------------------------------------------------*/ void contiki_init(void) { int i; uint8_t addr[sizeof(uip_lladdr.addr)]; uip_ipaddr_t ipaddr; uip_ds6_addr_t *lladdr; uip_ip4addr_t ipv4addr, netmask; /* 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_mac_addr(); 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())); /* IPv6 CONFIGURATION */ for(i = 0; i < sizeof(uip_lladdr.addr); i += 2) { addr[i + 1] = node_id & 0xff; addr[i + 0] = node_id >> 8; } linkaddr_copy(addr, &linkaddr_node_addr); memcpy(&uip_lladdr.addr, addr, sizeof(uip_lladdr.addr)); process_start(&tcpip_process, NULL); printf("Tentative link-local IPv6 address "); 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]); 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]); /* Start serial process */ serial_line_init(); /* Start autostart processes (defined in Contiki application) */ print_processes(autostart_processes); autostart_start(autostart_processes); /* Start the SLIP */ printf("Initiating SLIP with IP address is 172.16.0.2.\n"); uip_ipaddr(&ipv4addr, 172, 16, 0, 2); uip_ipaddr(&netmask, 255, 255, 255, 0); ip64_set_ipv4_address(&ipv4addr, &netmask); rs232_set_input(slip_input_byte); log_set_putchar_with_slip(1); uip_ip4addr_t ip4addr; uip_ip6addr_t ip6addr; uip_ipaddr(&ip4addr, 8,8,8,8); ip64_addr_4to6(&ip4addr, &ip6addr); uip_nameserver_update((uip_ipaddr_t *)&ip6addr, UIP_NAMESERVER_INFINITE_LIFETIME); }
/*---------------------------------------------------------------------------*/ void init_net(uint8_t node_id) { uint16_t shortaddr; uint64_t longaddr; linkaddr_t addr; #if WITH_UIP6 uip_ds6_addr_t *lladdr; uip_ipaddr_t ipaddr; #endif uint8_t i; memset(&shortaddr, 0, sizeof(shortaddr)); memset(&longaddr, 0, sizeof(longaddr)); *((uint8_t *)&shortaddr) = node_id >> 8; *((uint8_t *)&shortaddr + 1) = node_id; *((uint8_t *)&longaddr) = node_id >> 8; *((uint8_t *)&longaddr + 1) = node_id; for(i = 2; i < sizeof(longaddr); ++i) { ((uint8_t *)&longaddr)[i] = random_rand(); } PRINTF("SHORT MAC ADDRESS %02x:%02x\n", *((uint8_t *) & shortaddr), *((uint8_t *) & shortaddr + 1)); PRINTF("EXTENDED MAC ADDRESS %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", *((uint8_t *)&longaddr), *((uint8_t *)&longaddr + 1), *((uint8_t *)&longaddr + 2), *((uint8_t *)&longaddr + 3), *((uint8_t *)&longaddr + 4), *((uint8_t *)&longaddr + 5), *((uint8_t *)&longaddr + 6), *((uint8_t *)&longaddr + 7)); memset(&addr, 0, sizeof(linkaddr_t)); for(i = 0; i < sizeof(addr.u8); ++i) { addr.u8[i] = ((uint8_t *)&longaddr)[i]; } linkaddr_set_node_addr(&addr); PRINTF("Rime started with address: "); for(i = 0; i < sizeof(addr.u8) - 1; ++i) { PRINTF("%d.", addr.u8[i]); } PRINTF("%d\n", addr.u8[i]); queuebuf_init(); NETSTACK_RADIO.init(); mrf24j40_set_channel(RF_CHANNEL); mrf24j40_set_panid(IEEE802154_PANID); mrf24j40_set_short_mac_addr(shortaddr); mrf24j40_set_extended_mac_addr(longaddr); NETSTACK_RDC.init(); NETSTACK_MAC.init(); NETSTACK_NETWORK.init(); PRINTF("%s %s, channel check rate %d 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); #if WITH_UIP6 #if LINKADDR_CONF_SIZE == 2 memset(&uip_lladdr.addr, 0, sizeof(uip_lladdr.addr)); uip_lladdr.addr[3] = 0xff; uip_lladdr.addr[4]= 0xfe; memcpy(&uip_lladdr.addr[6], &shortaddr, sizeof(shortaddr)); #else memcpy(&uip_lladdr.addr, &longaddr, sizeof(uip_lladdr.addr)); #endif process_start(&tcpip_process, NULL); lladdr = uip_ds6_get_link_local(-1); PRINTF("Tentative link-local IPv6 address "); 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_ip6addr(&ipaddr, 0x2001, 0x1418, 0x100, 0x823c, 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 }
/*---------------------------------------------------------------------------*/ int main(void) { /* * Initalize hardware. */ halInit(); clock_init(); uart1_init(115200); /* Led initialization */ leds_init(); INTERRUPTS_ON(); PRINTF("\r\nStarting "); PRINTF(CONTIKI_VERSION_STRING); PRINTF(" on %s\r\n", boardDescription->name); boardPrintStringDescription(); PRINTF("\r\n"); /* * Initialize Contiki and our processes. */ process_init(); #if WITH_SERIAL_LINE_INPUT uart1_set_input(serial_line_input_byte); serial_line_init(); #endif /* rtimer and ctimer should be initialized before radio duty cycling layers */ rtimer_init(); /* etimer_process should be initialized before ctimer */ process_start(&etimer_process, NULL); ctimer_init(); netstack_init(); set_rime_addr(); printf("%s %s, channel check rate %lu Hz\n", NETSTACK_MAC.name, NETSTACK_RDC.name, CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1: NETSTACK_RDC.channel_check_interval())); printf("802.15.4 PAN ID 0x%x, EUI-%d:", IEEE802154_CONF_PANID, UIP_CONF_LL_802154?64:16); uip_debug_lladdr_print(&linkaddr_node_addr); printf(", radio channel %u\n", RF_CHANNEL); procinit_init(); energest_init(); ENERGEST_ON(ENERGEST_TYPE_CPU); /* Set the Clear Channel Assessment (CCA) threshold of the radio. The CCA threshold is used both for sending packets and for waking up ContikiMAC nodes. If the CCA threshold is too high, ContikiMAC will not wake up from neighbor transmissions. If the CCA threshold is too low, transmissions will be too restrictive and no packets will be sent. DEFAULT_RADIO_CCA_THRESHOLD is defined in this file. */ ST_RadioSetEdCcaThreshold(DEFAULT_RADIO_CCA_THRESHOLD); autostart_start(autostart_processes); #if UIP_CONF_IPV6 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]); } #endif /* UIP_CONF_IPV6 */ watchdog_start(); while(1) { int r; do { /* Reset watchdog. */ watchdog_periodic(); r = process_run(); } while(r > 0); ENERGEST_OFF(ENERGEST_TYPE_CPU); /* watchdog_stop(); */ ENERGEST_ON(ENERGEST_TYPE_LPM); /* Go to idle mode. */ halSleepWithOptions(SLEEPMODE_IDLE,0); /* We are awake. */ /* watchdog_start(); */ ENERGEST_OFF(ENERGEST_TYPE_LPM); ENERGEST_ON(ENERGEST_TYPE_CPU); } }
/*---------------------------------------------------------------------------*/ 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); } } }
/*--------------------------------------------------------------------------*/ int main(int argc, char **argv) { /* * Initalize hardware. */ msp430_cpu_init(); clock_init(); leds_init(); leds_on(LEDS_RED); uart1_init(BAUD2UBR(115200)); /* Must come before first printf */ leds_on(LEDS_GREEN); /* xmem_init(); */ rtimer_init(); lcd_init(); watchdog_init(); PRINTF(CONTIKI_VERSION_STRING "\n"); /* PRINTF("Compiled at %s, %s\n", __TIME__, __DATE__);*/ /* * Hardware initialization done! */ leds_on(LEDS_RED); /* Restore node id if such has been stored in external mem */ #ifdef NODEID node_id = NODEID; #ifdef BURN_NODEID node_id_burn(node_id); node_id_restore(); /* also configures node_mac[] */ #endif /* BURN_NODEID */ #else node_id_restore(); /* also configures node_mac[] */ #endif /* NODE_ID */ /* for setting "hardcoded" IEEE 802.15.4 MAC addresses */ #ifdef MAC_1 { uint8_t ieee[] = { MAC_1, MAC_2, MAC_3, MAC_4, MAC_5, MAC_6, MAC_7, MAC_8 }; memcpy(node_mac, ieee, sizeof(uip_lladdr.addr)); } #endif /* * Initialize Contiki and our processes. */ process_init(); process_start(&etimer_process, NULL); ctimer_init(); set_rime_addr(); random_init(node_id); NETSTACK_RADIO.init(); #if CC11xx_CC1101 || CC11xx_CC1120 printf("Starting up cc11xx radio at channel %d\n", RF_CHANNEL); cc11xx_channel_set(RF_CHANNEL); #endif /* CC11xx_CC1101 || CC11xx_CC1120 */ #if CONFIGURE_CC2420 || CONFIGURE_CC2520 { 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\n", longaddr[0], longaddr[1], longaddr[2], longaddr[3], longaddr[4], longaddr[5], longaddr[6], longaddr[7]); #if CONFIGURE_CC2420 cc2420_set_pan_addr(IEEE802154_PANID, shortaddr, longaddr); #endif /* CONFIGURE_CC2420 */ #if CONFIGURE_CC2520 cc2520_set_pan_addr(IEEE802154_PANID, shortaddr, longaddr); #endif /* CONFIGURE_CC2520 */ } #if CONFIGURE_CC2420 cc2420_set_channel(RF_CHANNEL); #endif /* CONFIGURE_CC2420 */ #if CONFIGURE_CC2520 cc2520_set_channel(RF_CHANNEL); #endif /* CONFIGURE_CC2520 */ #endif /* CONFIGURE_CC2420 || CONFIGURE_CC2520 */ NETSTACK_RADIO.on(); leds_off(LEDS_ALL); if(node_id > 0) { PRINTF("Node id %u.\n", node_id); } else { PRINTF("Node id not set.\n"); } #if WITH_UIP6 memcpy(&uip_lladdr.addr, node_mac, sizeof(uip_lladdr.addr)); /* Setup nullmac-like MAC for 802.15.4 */ queuebuf_init(); netstack_init(); printf("%s/%s %lu %u\n", NETSTACK_RDC.name, NETSTACK_MAC.name, CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1: NETSTACK_RDC.channel_check_interval()), RF_CHANNEL); process_start(&tcpip_process, NULL); printf("IPv6 "); { 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, 0xfc00, 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_init(); printf("%s %lu %u\n", NETSTACK_RDC.name, CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0? 1: NETSTACK_RDC.channel_check_interval()), RF_CHANNEL); #endif /* WITH_UIP6 */ #if !WITH_UIP6 uart1_set_input(serial_line_input_byte); serial_line_init(); #endif #ifdef NETSTACK_AES_H #ifndef NETSTACK_AES_KEY #error Please define NETSTACK_AES_KEY! #endif /* NETSTACK_AES_KEY */ { const uint8_t key[] = NETSTACK_AES_KEY; netstack_aes_set_key(key); } /*printf("AES encryption is enabled: '%s'\n", NETSTACK_AES_KEY);*/ printf("AES encryption is enabled\n"); #else /* NETSTACK_AES_H */ printf("Warning: AES encryption is disabled\n"); #endif /* NETSTACK_AES_H */ #if TIMESYNCH_CONF_ENABLED timesynch_init(); timesynch_set_authority_level(rimeaddr_node_addr.u8[0]); #endif /* TIMESYNCH_CONF_ENABLED */ #if CC11xx_CC1101 || CC11xx_CC1120 printf("cc11xx radio at channel %d\n", RF_CHANNEL); cc11xx_channel_set(RF_CHANNEL); #endif /* CC11xx_CC1101 || CC11xx_CC1120 */ #if CONFIGURE_CC2420 { 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\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); } cc2420_set_channel(RF_CHANNEL); #endif /* CONFIGURE_CC2420 */ NETSTACK_RADIO.on(); /* process_start(&sensors_process, NULL); SENSORS_ACTIVATE(button_sensor);*/ energest_init(); ENERGEST_ON(ENERGEST_TYPE_CPU); simple_rpl_init(); watchdog_start(); print_processes(autostart_processes); autostart_start(autostart_processes); duty_cycle_scroller_start(CLOCK_SECOND * 2); #if IP64_CONF_UIP_FALLBACK_INTERFACE_SLIP && WITH_SLIP /* Start the SLIP */ printf("Initiating SLIP: my IP is 172.16.0.2...\n"); slip_arch_init(0); { uip_ip4addr_t ipv4addr, netmask; uip_ipaddr(&ipv4addr, 172, 16, 0, 2); uip_ipaddr(&netmask, 255, 255, 255, 0); ip64_set_ipv4_address(&ipv4addr, &netmask); } uart1_set_input(slip_input_byte); #endif /* IP64_CONF_UIP_FALLBACK_INTERFACE_SLIP */ /* * 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); } } }