/*-----------------------------------------------------------------------*/ PROCESS_THREAD(udp_sender_process, ev, data) { static struct etimer period_timer, wait_timer; PROCESS_BEGIN(); set_global_address(); PRINTF("UDP sender process started\n"); print_local_address(); /* new connection with remote host */ sender_conn = udp_new(NULL, UIP_HTONS(UDP_SINK_PORT), NULL); udp_bind(sender_conn, UIP_HTONS(UDP_SENDER_PORT)); PRINTF("Created a connection with the sink "); PRINT6ADDR(&sender_conn->ripaddr); PRINTF(" local/remote port %u/%u\n", UIP_HTONS(sender_conn->lport), UIP_HTONS(sender_conn->rport)); etimer_set(&period_timer, CLOCK_SECOND * PERIOD); while(1) { PROCESS_WAIT_EVENT(); if(ev == PROCESS_EVENT_TIMER) { if(data == &period_timer) { etimer_reset(&period_timer); etimer_set(&wait_timer, random_rand() % (CLOCK_SECOND * RANDWAIT)); } else if(data ==&wait_timer) { /* Time to send a data. */ collect_common_send(); } } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ static void tcpip_handler(void) { /* handle the incoming packet */ if(uip_newdata()) { if( ((query_pkt_t*)uip_appdata)->type == 1){ /* Query Packet */ query_pkt_t *appdata; appdata = (query_pkt_t*)uip_appdata; if(!appdata->multicast_flag) collect_common_send(); else{ if(appdata->query_id > cur_query_id) { /* Do not reply to the duplicate query */ cur_query_id = appdata->query_id; if(region == SENSOR || region == ANONYMOUS){ collect_common_send(); } else if (appdata->id < region) { //else drop the query, received from the lower region /* send multicast query to the lower regions */ appdata->id = region; uip_ipaddr_t addr; uip_create_linklocal_allnodes_mcast(&addr); uip_udp_packet_sendto(server_conn, appdata, sizeof(query_pkt_t), &addr, UIP_HTONS(UDP_CLIENT_PORT)); } } } } else if( ((identity_pkt_t*)uip_appdata)->type == 2){ /* Identity Packet */ if(region > ((identity_pkt_t*)uip_appdata)->id ) add_new_nbr(((identity_pkt_t*)uip_appdata)->id, UIP_IP_BUF->srcipaddr, cc2420_last_rssi); print_nbr_table(); } } }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(collect_common_process, ev, data) { static struct etimer period_timer, wait_timer; PROCESS_BEGIN(); collect_common_net_init(); /* Send a packet every 60-62 seconds. */ etimer_set(&period_timer, CLOCK_SECOND * PERIOD); while(1) { PROCESS_WAIT_EVENT(); if(ev == serial_line_event_message) { /* char *line; line = (char *)data; if(strncmp(line, "collect", 7) == 0 || strncmp(line, "gw", 2) == 0) { collect_common_set_sink(); } else if(strncmp(line, "net", 3) == 0) { collect_common_net_print(); } else if(strncmp(line, "time ", 5) == 0) { unsigned long tmp; line += 6; while(*line == ' ') { line++; } tmp = strtolong(line); time_offset = clock_seconds() - tmp; printf("Time offset set to %lu\n", time_offset); } else if(strncmp(line, "mac ", 4) == 0) { line +=4; while(*line == ' ') { line++; } if(*line == '0') { NETSTACK_RDC.off(1); printf("mac: turned MAC off (keeping radio on): %s\n", NETSTACK_RDC.name); } else { NETSTACK_RDC.on(); printf("mac: turned MAC on: %s\n", NETSTACK_RDC.name); } } else if(strncmp(line, "~K", 2) == 0 || strncmp(line, "killall", 7) == 0) { /* Ignore stop commands */ /* } else { printf("unhandled command: %s\n", line); }*/ } if(ev == PROCESS_EVENT_TIMER) { if(data == &period_timer) { etimer_reset(&period_timer); etimer_set(&wait_timer, random_rand() % (CLOCK_SECOND * RANDWAIT)); } else if(data == &wait_timer) { if(send_active) { /* Time to send the data */ collect_common_send(); } } } } PROCESS_END(); }