/*---------------------------------------------------------------------------*/ PROCESS_THREAD(example_collect_process, ev, data) { static struct etimer periodic; static struct etimer et; PROCESS_BEGIN(); collect_open(&tc, 130, COLLECT_ROUTER, &callbacks); if(rimeaddr_node_addr.u8[0] == 1 && rimeaddr_node_addr.u8[1] == 0) { printf("I am sink\n"); collect_set_sink(&tc, 1); } /* Allow some time for the network to settle. */ etimer_set(&et, 120 * CLOCK_SECOND); PROCESS_WAIT_UNTIL(etimer_expired(&et)); while(1) { /* Send a packet every 30 seconds. */ if(etimer_expired(&periodic)) { etimer_set(&periodic, CLOCK_SECOND * 30); etimer_set(&et, random_rand() % (CLOCK_SECOND * 30)); } PROCESS_WAIT_EVENT(); if(etimer_expired(&et)) { static rimeaddr_t oldparent; const rimeaddr_t *parent; printf("Sending\n"); packetbuf_clear(); packetbuf_set_datalen(sprintf(packetbuf_dataptr(), "%s", "Hello") + 1); collect_send(&tc, 15); parent = collect_parent(&tc); if(!rimeaddr_cmp(parent, &oldparent)) { //alten parent ausgeben (falls der nicht null war) if(!rimeaddr_cmp(&oldparent, &rimeaddr_null)) { printf("#L %d 0\n", oldparent.u8[0]); } //neuen parent ausgeben (falls der nicht null ist) if(!rimeaddr_cmp(parent, &rimeaddr_null)) { printf("#L %d 1\n", parent->u8[0]); } //speichern rimeaddr_copy(&oldparent, parent); } } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(example_collect_process, ev, data) { PROCESS_BEGIN(); SENSORS_ACTIVATE(button_sensor); collect_open(&tc, 130, COLLECT_ROUTER, &callbacks); if(rimeaddr_node_addr.u8[0] == 1 && rimeaddr_node_addr.u8[1] == 1) { printf("I am sink\n"); collect_set_sink(&tc, 1); } while(1) { static struct etimer et; /* Send a packet every 16 seconds; first wait 8 seconds, than a random time between 0 and 8 seconds. */ etimer_set(&et, CLOCK_SECOND * 16 + random_rand() % (CLOCK_SECOND * 16)); PROCESS_WAIT_EVENT(); if(etimer_expired(&et)) { while(tc.sending) { PROCESS_PAUSE(); } printf("Sending\n"); packetbuf_clear(); packetbuf_set_datalen(sprintf(packetbuf_dataptr(), "%s", "Hello") + 1); collect_send(&tc, 4); } if(ev == sensors_event) { if(data == &button_sensor) { printf("I am sink\n"); collect_set_sink(&tc, 1); } } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(shell_collect_process, ev, data) { PROCESS_BEGIN(); #if TIMESYNCH_CONF_ENABLED timesynch_set_authority_level(0); #endif collect_set_sink(&collect, 1); is_sink = 1; waiting_for_collect = 1; PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input); waiting_for_collect = 0; PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(shell_nodes_process, ev, data) { static struct etimer etimer; struct netflood_msg *msg; char buf[10]; PROCESS_BEGIN(); if(!is_sink) { shell_output_str(&nodes_command, "Setting up a collection network...", ""); #if TIMESYNCH_CONF_ENABLED timesynch_set_authority_level(0); #endif collect_set_sink(&collect, 1); etimer_set(&etimer, CLOCK_SECOND * 2); PROCESS_WAIT_UNTIL(etimer_expired(&etimer)); is_sink = 1; } packetbuf_clear(); msg = packetbuf_dataptr(); packetbuf_set_datalen(sizeof(struct netflood_msg)); msg->type = NETFLOOD_TYPE_NODES; netflood_send(&netflood, nodes_seqno++); etimer_set(&etimer, CLOCK_SECOND * 10); waiting_for_nodes = 1; shell_output_str(&nodes_command, "Request sent, waiting for replies...", ""); messages_received = 0; PROCESS_WAIT_UNTIL(etimer_expired(&etimer)); snprintf(buf, sizeof(buf), "%d", messages_received); shell_output_str(&nodes_command, buf, " nodes heard"); waiting_for_nodes = 0; PROCESS_END(); }