/* A periodic process to send TSCH Enhanced Beacons (EB) */ PROCESS_THREAD(tsch_send_eb_process, ev, data) { static struct etimer eb_timer; PROCESS_BEGIN(); /* Wait until association */ etimer_set(&eb_timer, CLOCK_SECOND / 10); while(!tsch_is_associated) { PROCESS_WAIT_UNTIL(etimer_expired(&eb_timer)); etimer_reset(&eb_timer); } /* Set an initial delay except for coordinator, which should send an EB asap */ if(!tsch_is_coordinator) { etimer_set(&eb_timer, random_rand() % TSCH_EB_PERIOD); PROCESS_WAIT_UNTIL(etimer_expired(&eb_timer)); } while(1) { unsigned long delay; if(tsch_is_associated && tsch_current_eb_period > 0) { /* Enqueue EB only if there isn't already one in queue */ if(tsch_queue_packet_count(&tsch_eb_address) == 0) { int eb_len; uint8_t hdr_len = 0; uint8_t tsch_sync_ie_offset; /* Prepare the EB packet and schedule it to be sent */ eb_len = tsch_packet_create_eb(&hdr_len, &tsch_sync_ie_offset); if(eb_len > 0) { struct tsch_packet *p; /* Enqueue EB packet */ if(!(p = tsch_queue_add_packet(&tsch_eb_address, NULL, NULL))) { PRINTF("TSCH:! could not enqueue EB packet\n"); } else { PRINTF("TSCH: enqueue EB packet %u %u\n", eb_len, hdr_len); p->tsch_sync_ie_offset = tsch_sync_ie_offset; p->header_len = hdr_len; } } } } if(tsch_current_eb_period > 0) { /* Next EB transmission with a random delay * within [tsch_current_eb_period*0.75, tsch_current_eb_period[ */ delay = (tsch_current_eb_period - tsch_current_eb_period / 4) + random_rand() % (tsch_current_eb_period / 4); } else { delay = TSCH_EB_PERIOD; } etimer_set(&eb_timer, delay); PROCESS_WAIT_UNTIL(etimer_expired(&eb_timer)); } PROCESS_END(); }
/* A periodic process to send TSCH Enhanced Beacons (EB) */ PROCESS_THREAD(tsch_send_eb_process, ev, data) { static struct etimer eb_timer; PROCESS_BEGIN(); /* Wait until association */ etimer_set(&eb_timer, CLOCK_SECOND / 10); while(!tsch_is_associated) { PROCESS_WAIT_UNTIL(etimer_expired(&eb_timer)); etimer_reset(&eb_timer); } /* Set an initial delay except for coordinator, which should send an EB asap */ if(!tsch_is_coordinator) { etimer_set(&eb_timer, random_rand() % TSCH_EB_PERIOD); PROCESS_WAIT_UNTIL(etimer_expired(&eb_timer)); } while(1) { unsigned long delay; if(tsch_is_associated && tsch_current_eb_period > 0) { /* Enqueue EB only if there isn't already one in queue */ if(tsch_queue_packet_count(&tsch_eb_address) == 0) { int eb_len; uint8_t hdr_len = 0; uint8_t tsch_sync_ie_offset; /* Prepare the EB packet and schedule it to be sent */ packetbuf_clear(); /* We don't use seqno 0 */ if(++tsch_packet_seqno == 0) { tsch_packet_seqno++; } packetbuf_set_attr(PACKETBUF_ATTR_FRAME_TYPE, FRAME802154_BEACONFRAME); packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, tsch_packet_seqno); #if TSCH_SECURITY_ENABLED if(tsch_is_pan_secured) { /* Set security level, key id and index */ packetbuf_set_attr(PACKETBUF_ATTR_SECURITY_LEVEL, TSCH_SECURITY_KEY_SEC_LEVEL_EB); packetbuf_set_attr(PACKETBUF_ATTR_KEY_ID_MODE, FRAME802154_1_BYTE_KEY_ID_MODE); /* Use 1-byte key index */ packetbuf_set_attr(PACKETBUF_ATTR_KEY_INDEX, TSCH_SECURITY_KEY_INDEX_EB); } #endif /* TSCH_SECURITY_ENABLED */ eb_len = tsch_packet_create_eb(packetbuf_dataptr(), PACKETBUF_SIZE, tsch_packet_seqno, &hdr_len, &tsch_sync_ie_offset); if(eb_len != 0) { struct tsch_packet *p; packetbuf_set_datalen(eb_len); /* Enqueue EB packet */ if(!(p = tsch_queue_add_packet(&tsch_eb_address, NULL, NULL))) { PRINTF("TSCH:! could not enqueue EB packet\n"); } else { PRINTF("TSCH: enqueue EB packet %u %u\n", eb_len, hdr_len); p->tsch_sync_ie_offset = tsch_sync_ie_offset; p->header_len = hdr_len; } } } } if(tsch_current_eb_period > 0) { /* Next EB transmission with a random delay * within [tsch_current_eb_period*0.75, tsch_current_eb_period[ */ delay = (tsch_current_eb_period - tsch_current_eb_period / 4) + random_rand() % (tsch_current_eb_period / 4); } else { delay = TSCH_EB_PERIOD; } etimer_set(&eb_timer, delay); PROCESS_WAIT_UNTIL(etimer_expired(&eb_timer)); } PROCESS_END(); }