static char powercycle(struct rtimer *t, void *ptr) { if(is_streaming) { if(!RTIMER_CLOCK_LT(RTIMER_NOW(), stream_until)) { is_streaming = 0; rimeaddr_copy(&is_streaming_to, &rimeaddr_null); rimeaddr_copy(&is_streaming_to_too, &rimeaddr_null); } } PT_BEGIN(&pt); while(1) { /* Only wait for some cycles to pass for someone to start sending */ if(someone_is_sending > 0) { someone_is_sending--; } /* If there were a strobe in the air, turn radio on */ powercycle_turn_radio_on(); schedule_powercycle(t, xmac_config.on_time); PT_YIELD(&pt); if(xmac_config.off_time > 0 && !NETSTACK_RADIO.receiving_packet()) { powercycle_turn_radio_off(); if(waiting_for_packet != 0) { waiting_for_packet++; if(waiting_for_packet > 2) { /* We should not be awake for more than two consecutive power cycles without having heard a packet, so we turn off the radio. */ waiting_for_packet = 0; powercycle_turn_radio_off(); } } schedule_powercycle(t, xmac_config.off_time); PT_YIELD(&pt); } } PT_END(&pt); }
/*---------------------------------------------------------------------------*/ static char cpowercycle(void *ptr) { if(is_streaming) { if(!RTIMER_CLOCK_LT(RTIMER_NOW(), stream_until)) { is_streaming = 0; rimeaddr_copy(&is_streaming_to, &rimeaddr_null); rimeaddr_copy(&is_streaming_to_too, &rimeaddr_null); } } PT_BEGIN(&pt); while(1) { /* Only wait for some cycles to pass for someone to start sending */ if(someone_is_sending > 0) { someone_is_sending--; } /* If there were a strobe in the air, turn radio on */ powercycle_turn_radio_on(); CSCHEDULE_POWERCYCLE(DEFAULT_ON_TIME); PT_YIELD(&pt); if(cxmac_config.off_time > 0) { powercycle_turn_radio_off(); if(waiting_for_packet != 0) { waiting_for_packet++; if(waiting_for_packet > 2) { /* We should not be awake for more than two consecutive power cycles without having heard a packet, so we turn off the radio. */ waiting_for_packet = 0; powercycle_turn_radio_off(); } } CSCHEDULE_POWERCYCLE(DEFAULT_OFF_TIME); PT_YIELD(&pt); } } PT_END(&pt); }
/*---------------------------------------------------------------------------*/ static char powercycle(struct rtimer *t, void *ptr) { #if SYNC_CYCLE_STARTS static volatile rtimer_clock_t sync_cycle_start; static volatile uint8_t sync_cycle_phase; #endif PT_BEGIN(&pt); #if SYNC_CYCLE_STARTS sync_cycle_start = RTIMER_NOW(); #else cycle_start = RTIMER_NOW(); #endif while(1) { static uint8_t packet_seen; static rtimer_clock_t t0; static uint8_t count; #if SYNC_CYCLE_STARTS /* Compute cycle start when RTIMER_ARCH_SECOND is not a multiple of CHANNEL_CHECK_RATE */ if(sync_cycle_phase++ == NETSTACK_RDC_CHANNEL_CHECK_RATE) { sync_cycle_phase = 0; sync_cycle_start += RTIMER_ARCH_SECOND; cycle_start = sync_cycle_start; } else { #if (RTIMER_ARCH_SECOND * NETSTACK_RDC_CHANNEL_CHECK_RATE) > 65535 cycle_start = sync_cycle_start + ((unsigned long)(sync_cycle_phase*RTIMER_ARCH_SECOND))/NETSTACK_RDC_CHANNEL_CHECK_RATE; #else cycle_start = sync_cycle_start + (sync_cycle_phase*RTIMER_ARCH_SECOND)/NETSTACK_RDC_CHANNEL_CHECK_RATE; #endif } #else cycle_start += CYCLE_TIME; #endif packet_seen = 0; for(count = 0; count < CCA_COUNT_MAX; ++count) { t0 = RTIMER_NOW(); if(we_are_sending == 0 && we_are_receiving_burst == 0) { powercycle_turn_radio_on(); /* Check if a packet is seen in the air. If so, we keep the radio on for a while (LISTEN_TIME_AFTER_PACKET_DETECTED) to be able to receive the packet. We also continuously check the radio medium to make sure that we wasn't woken up by a false positive: a spurious radio interference that was not caused by an incoming packet. */ if(NETSTACK_RADIO.channel_clear() == 0) { packet_seen = 1; break; } powercycle_turn_radio_off(); } schedule_powercycle_fixed(t, RTIMER_NOW() + CCA_SLEEP_TIME); PT_YIELD(&pt); } if(packet_seen) { static rtimer_clock_t start; static uint8_t silence_periods, periods; start = RTIMER_NOW(); periods = silence_periods = 0; while(we_are_sending == 0 && radio_is_on && RTIMER_CLOCK_LT(RTIMER_NOW(), (start + LISTEN_TIME_AFTER_PACKET_DETECTED))) { /* Check for a number of consecutive periods of non-activity. If we see two such periods, we turn the radio off. Also, if a packet has been successfully received (as indicated by the NETSTACK_RADIO.pending_packet() function), we stop snooping. */ #if !RDC_CONF_HARDWARE_CSMA /* A cca cycle will disrupt rx on some radios, e.g. mc1322x, rf230 */ /*TODO: Modify those drivers to just return the internal RSSI when already in rx mode */ if(NETSTACK_RADIO.channel_clear()) { ++silence_periods; } else { silence_periods = 0; } #endif ++periods; if(NETSTACK_RADIO.receiving_packet()) { silence_periods = 0; } if(silence_periods > MAX_SILENCE_PERIODS) { powercycle_turn_radio_off(); break; } if(WITH_FAST_SLEEP && periods > MAX_NONACTIVITY_PERIODS && !(NETSTACK_RADIO.receiving_packet() || NETSTACK_RADIO.pending_packet())) { powercycle_turn_radio_off(); break; } if(NETSTACK_RADIO.pending_packet()) { break; } schedule_powercycle(t, CCA_CHECK_TIME + CCA_SLEEP_TIME); PT_YIELD(&pt); } if(radio_is_on) { if(!(NETSTACK_RADIO.receiving_packet() || NETSTACK_RADIO.pending_packet()) || !RTIMER_CLOCK_LT(RTIMER_NOW(), (start + LISTEN_TIME_AFTER_PACKET_DETECTED))) { powercycle_turn_radio_off(); } } } if(RTIMER_CLOCK_LT(RTIMER_NOW() - cycle_start, CYCLE_TIME - CHECK_TIME * 4)) { /* Schedule the next powercycle interrupt, or sleep the mcu until then. Sleeping will not exit from this interrupt, so ensure an occasional wake cycle or foreground processing will be blocked until a packet is detected */ #if RDC_CONF_MCU_SLEEP static uint8_t sleepcycle; if((sleepcycle++ < 16) && !we_are_sending && !radio_is_on) { rtimer_arch_sleep(CYCLE_TIME - (RTIMER_NOW() - cycle_start)); } else { sleepcycle = 0; schedule_powercycle_fixed(t, CYCLE_TIME + cycle_start); PT_YIELD(&pt); } #else schedule_powercycle_fixed(t, CYCLE_TIME + cycle_start); PT_YIELD(&pt); #endif } } PT_END(&pt); }
/*---------------------------------------------------------------------------*/ static void powercycle_turn_radio_off(void) { #if CONTIKIMAC_CONF_COMPOWER uint8_t was_on = radio_is_on; #endif /* CONTIKIMAC_CONF_COMPOWER */ #if RDC_CONF_HARDWARE_SEND_ACK if(we_are_sending == 0 && we_are_receiving_burst == 0) { #else if(we_are_sending == 0 && we_are_receiving_burst == 0 && we_are_acking == 0) { #endif off(); #if CONTIKIMAC_CONF_COMPOWER if(was_on && !radio_is_on) { compower_accumulate(&compower_idle_activity); } #endif /* CONTIKIMAC_CONF_COMPOWER */ } } /*---------------------------------------------------------------------------*/ static void powercycle_turn_radio_on(void) { #if RDC_CONF_HARDWARE_SEND_ACK if(we_are_sending == 0 && we_are_receiving_burst == 0) { #else if(we_are_sending == 0 && we_are_receiving_burst == 0 && we_are_acking == 0) { #endif on(); } } /*---------------------------------------------------------------------------*/ static char powercycle(struct rtimer *t, void *ptr) { #if SYNC_CYCLE_STARTS static volatile rtimer_clock_t sync_cycle_start; static volatile uint8_t sync_cycle_phase; #endif PT_BEGIN(&pt); #if SYNC_CYCLE_STARTS sync_cycle_start = RTIMER_NOW(); #else cycle_start = RTIMER_NOW(); #endif while(1) { static uint8_t packet_seen; static rtimer_clock_t t0; static uint8_t count; #if SYNC_CYCLE_STARTS /* Compute cycle start when RTIMER_ARCH_SECOND is not a multiple of CHANNEL_CHECK_RATE */ if(sync_cycle_phase++ == NETSTACK_RDC_CHANNEL_CHECK_RATE) { sync_cycle_phase = 0; sync_cycle_start += RTIMER_ARCH_SECOND; cycle_start = sync_cycle_start; } else { #if (RTIMER_ARCH_SECOND * NETSTACK_RDC_CHANNEL_CHECK_RATE) > 65535 cycle_start = sync_cycle_start + ((unsigned long)(sync_cycle_phase*RTIMER_ARCH_SECOND))/NETSTACK_RDC_CHANNEL_CHECK_RATE; #else cycle_start = sync_cycle_start + (sync_cycle_phase*RTIMER_ARCH_SECOND)/NETSTACK_RDC_CHANNEL_CHECK_RATE; #endif } #else cycle_start += CYCLE_TIME; #endif packet_seen = 0; for(count = 0; count < CCA_COUNT_MAX; ++count) { t0 = RTIMER_NOW(); if(we_are_sending == 0 && we_are_receiving_burst == 0) { powercycle_turn_radio_on(); /* Check if a packet is seen in the air. If so, we keep the radio on for a while (LISTEN_TIME_AFTER_PACKET_DETECTED) to be able to receive the packet. We also continuously check the radio medium to make sure that we wasn't woken up by a false positive: a spurious radio interference that was not caused by an incoming packet. */ if(NETSTACK_RADIO.channel_clear() == 0) { packet_seen = 1; break; } powercycle_turn_radio_off(); } schedule_powercycle_fixed(t, RTIMER_NOW() + CCA_SLEEP_TIME); PT_YIELD(&pt); } if(packet_seen) { static rtimer_clock_t start; static uint8_t silence_periods, periods; start = RTIMER_NOW(); periods = silence_periods = 0; while(we_are_sending == 0 && radio_is_on && RTIMER_CLOCK_LT(RTIMER_NOW(), (start + LISTEN_TIME_AFTER_PACKET_DETECTED))) { /* Check for a number of consecutive periods of non-activity. If we see two such periods, we turn the radio off. Also, if a packet has been successfully received (as indicated by the NETSTACK_RADIO.pending_packet() function), we stop snooping. */ #if !RDC_CONF_HARDWARE_CSMA /* A cca cycle will disrupt rx on some radios, e.g. mc1322x, rf230 */ /*TODO: Modify those drivers to just return the internal RSSI when already in rx mode */ if(NETSTACK_RADIO.channel_clear()) { ++silence_periods; } else { silence_periods = 0; } #endif ++periods; if(NETSTACK_RADIO.receiving_packet()) { silence_periods = 0; } if(silence_periods > MAX_SILENCE_PERIODS) { powercycle_turn_radio_off(); break; } if(WITH_FAST_SLEEP && periods > MAX_NONACTIVITY_PERIODS && !(NETSTACK_RADIO.receiving_packet() || NETSTACK_RADIO.pending_packet())) { powercycle_turn_radio_off(); break; } if(NETSTACK_RADIO.pending_packet()) { break; } schedule_powercycle(t, CCA_CHECK_TIME + CCA_SLEEP_TIME); PT_YIELD(&pt); } if(radio_is_on) { if(!(NETSTACK_RADIO.receiving_packet() || NETSTACK_RADIO.pending_packet()) || !RTIMER_CLOCK_LT(RTIMER_NOW(), (start + LISTEN_TIME_AFTER_PACKET_DETECTED))) { powercycle_turn_radio_off(); } } } if(RTIMER_CLOCK_LT(RTIMER_NOW() - cycle_start, CYCLE_TIME - CHECK_TIME * 4)) { /* Schedule the next powercycle interrupt, or sleep the mcu until then. Sleeping will not exit from this interrupt, so ensure an occasional wake cycle or foreground processing will be blocked until a packet is detected */ #if RDC_CONF_MCU_SLEEP static uint8_t sleepcycle; if((sleepcycle++ < 16) && !we_are_sending && !radio_is_on) { rtimer_arch_sleep(CYCLE_TIME - (RTIMER_NOW() - cycle_start)); } else { sleepcycle = 0; schedule_powercycle_fixed(t, CYCLE_TIME + cycle_start); PT_YIELD(&pt); } #else schedule_powercycle_fixed(t, CYCLE_TIME + cycle_start); PT_YIELD(&pt); #endif } } PT_END(&pt); } /*---------------------------------------------------------------------------*/ static int broadcast_rate_drop(void) { #if CONTIKIMAC_CONF_BROADCAST_RATE_LIMIT if(!timer_expired(&broadcast_rate_timer)) { broadcast_rate_counter++; if(broadcast_rate_counter < CONTIKIMAC_CONF_BROADCAST_RATE_LIMIT) { return 0; } else { return 1; } } else { timer_set(&broadcast_rate_timer, CLOCK_SECOND); broadcast_rate_counter = 0; return 0; } #else /* CONTIKIMAC_CONF_BROADCAST_RATE_LIMIT */ return 0; #endif /* CONTIKIMAC_CONF_BROADCAST_RATE_LIMIT */ } /*---------------------------------------------------------------------------*/ static int send_packet(mac_callback_t mac_callback, void *mac_callback_ptr, struct rdc_buf_list *buf_list, int is_receiver_awake) { rtimer_clock_t t0; rtimer_clock_t encounter_time = 0; int strobes; uint8_t got_strobe_ack = 0; int hdrlen, len; uint8_t is_broadcast = 0; uint8_t is_reliable = 0; uint8_t is_known_receiver = 0; uint8_t collisions; int transmit_len; int ret; uint8_t contikimac_was_on; uint8_t seqno; #if WITH_CONTIKIMAC_HEADER struct hdr *chdr; #endif /* WITH_CONTIKIMAC_HEADER */ /* Exit if RDC and radio were explicitly turned off */ if(!contikimac_is_on && !contikimac_keep_radio_on) { PRINTF("contikimac: radio is turned off\n"); return MAC_TX_ERR_FATAL; } if(packetbuf_totlen() == 0) { PRINTF("contikimac: send_packet data len 0\n"); return MAC_TX_ERR_FATAL; } #if !NETSTACK_CONF_BRIDGE_MODE /* If NETSTACK_CONF_BRIDGE_MODE is set, assume PACKETBUF_ADDR_SENDER is already set. */ packetbuf_set_addr(PACKETBUF_ADDR_SENDER, &rimeaddr_node_addr); #endif if(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), &rimeaddr_null)) { is_broadcast = 1; PRINTDEBUG("contikimac: send broadcast\n"); if(broadcast_rate_drop()) { return MAC_TX_COLLISION; } } else { #if UIP_CONF_IPV6 PRINTDEBUG("contikimac: send unicast to %02x%02x:%02x%02x:%02x%02x:%02x%02x\n", packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[0], packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[1], packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[2], packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[3], packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[4], packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[5], packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[6], packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[7]); #else /* UIP_CONF_IPV6 */ PRINTDEBUG("contikimac: send unicast to %u.%u\n", packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[0], packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[1]); #endif /* UIP_CONF_IPV6 */ } is_reliable = packetbuf_attr(PACKETBUF_ATTR_RELIABLE) || packetbuf_attr(PACKETBUF_ATTR_ERELIABLE); packetbuf_set_attr(PACKETBUF_ATTR_MAC_ACK, 1); #if WITH_CONTIKIMAC_HEADER hdrlen = packetbuf_totlen(); if(packetbuf_hdralloc(sizeof(struct hdr)) == 0) { /* Failed to allocate space for contikimac header */ PRINTF("contikimac: send failed, too large header\n"); return MAC_TX_ERR_FATAL; } chdr = packetbuf_hdrptr(); chdr->id = CONTIKIMAC_ID; chdr->len = hdrlen; /* Create the MAC header for the data packet. */ hdrlen = NETSTACK_FRAMER.create(); if(hdrlen < 0) { /* Failed to send */ PRINTF("contikimac: send failed, too large header\n"); packetbuf_hdr_remove(sizeof(struct hdr)); return MAC_TX_ERR_FATAL; } hdrlen += sizeof(struct hdr); #else /* Create the MAC header for the data packet. */ hdrlen = NETSTACK_FRAMER.create(); if(hdrlen < 0) { /* Failed to send */ PRINTF("contikimac: send failed, too large header\n"); return MAC_TX_ERR_FATAL; } #endif /* Make sure that the packet is longer or equal to the shortest packet length. */ transmit_len = packetbuf_totlen(); if(transmit_len < SHORTEST_PACKET_SIZE) { /* Pad with zeroes */ uint8_t *ptr; ptr = packetbuf_dataptr(); memset(ptr + packetbuf_datalen(), 0, SHORTEST_PACKET_SIZE - packetbuf_totlen()); PRINTF("contikimac: shorter than shortest (%d)\n", packetbuf_totlen()); transmit_len = SHORTEST_PACKET_SIZE; } packetbuf_compact(); #ifdef NETSTACK_ENCRYPT NETSTACK_ENCRYPT(); #endif /* NETSTACK_ENCRYPT */ transmit_len = packetbuf_totlen(); NETSTACK_RADIO.prepare(packetbuf_hdrptr(), transmit_len); /* Remove the MAC-layer header since it will be recreated next time around. */ packetbuf_hdr_remove(hdrlen); if(!is_broadcast && !is_receiver_awake) { #if WITH_PHASE_OPTIMIZATION ret = phase_wait(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), CYCLE_TIME, GUARD_TIME, mac_callback, mac_callback_ptr, buf_list); if(ret == PHASE_DEFERRED) { return MAC_TX_DEFERRED; } if(ret != PHASE_UNKNOWN) { is_known_receiver = 1; } #endif /* WITH_PHASE_OPTIMIZATION */ } /* By setting we_are_sending to one, we ensure that the rtimer powercycle interrupt do not interfere with us sending the packet. */ we_are_sending = 1; /* If we have a pending packet in the radio, we should not send now, because we will trash the received packet. Instead, we signal that we have a collision, which lets the packet be received. This packet will be retransmitted later by the MAC protocol instread. */ if(NETSTACK_RADIO.receiving_packet() || NETSTACK_RADIO.pending_packet()) { we_are_sending = 0; PRINTF("contikimac: collision receiving %d, pending %d\n", NETSTACK_RADIO.receiving_packet(), NETSTACK_RADIO.pending_packet()); return MAC_TX_COLLISION; } /* Switch off the radio to ensure that we didn't start sending while the radio was doing a channel check. */ off(); strobes = 0; /* Send a train of strobes until the receiver answers with an ACK. */ collisions = 0; got_strobe_ack = 0; /* Set contikimac_is_on to one to allow the on() and off() functions to control the radio. We restore the old value of contikimac_is_on when we are done. */ contikimac_was_on = contikimac_is_on; contikimac_is_on = 1; #if !RDC_CONF_HARDWARE_CSMA /* Check if there are any transmissions by others. */ /* TODO: why does this give collisions before sending with the mc1322x? */ if(is_receiver_awake == 0) { int i; for(i = 0; i < CCA_COUNT_MAX_TX; ++i) { t0 = RTIMER_NOW(); on(); #if CCA_CHECK_TIME > 0 while(RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + CCA_CHECK_TIME)) { } #endif if(NETSTACK_RADIO.channel_clear() == 0) { collisions++; off(); break; } off(); t0 = RTIMER_NOW(); while(RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + CCA_SLEEP_TIME)) { } } } if(collisions > 0) { we_are_sending = 0; off(); PRINTF("contikimac: collisions before sending\n"); contikimac_is_on = contikimac_was_on; return MAC_TX_COLLISION; } #endif /* RDC_CONF_HARDWARE_CSMA */ #if !RDC_CONF_HARDWARE_ACK if(!is_broadcast) { /* Turn radio on to receive expected unicast ack. Not necessary with hardware ack detection, and may trigger an unnecessary cca or rx cycle */ on(); } #endif watchdog_periodic(); t0 = RTIMER_NOW(); seqno = packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO); for(strobes = 0, collisions = 0; got_strobe_ack == 0 && collisions == 0 && RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + STROBE_TIME); strobes++) { watchdog_periodic(); if(!is_broadcast && (is_receiver_awake || is_known_receiver) && !RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + MAX_PHASE_STROBE_TIME)) { PRINTF("miss to %d\n", packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[0]); break; } len = 0; { rtimer_clock_t wt; rtimer_clock_t txtime; int ret; txtime = RTIMER_NOW(); ret = NETSTACK_RADIO.transmit(transmit_len); #if RDC_CONF_HARDWARE_ACK /* For radios that block in the transmit routine and detect the ACK in hardware */ if(ret == RADIO_TX_OK) { if(!is_broadcast) { got_strobe_ack = 1; encounter_time = txtime; break; } } else if (ret == RADIO_TX_NOACK) { } else if (ret == RADIO_TX_COLLISION) { PRINTF("contikimac: collisions while sending\n"); collisions++; } wt = RTIMER_NOW(); while(RTIMER_CLOCK_LT(RTIMER_NOW(), wt + INTER_PACKET_INTERVAL)) { } #else /* RDC_CONF_HARDWARE_ACK */ /* Wait for the ACK packet */ wt = RTIMER_NOW(); while(RTIMER_CLOCK_LT(RTIMER_NOW(), wt + INTER_PACKET_INTERVAL)) { } if(!is_broadcast && (NETSTACK_RADIO.receiving_packet() || NETSTACK_RADIO.pending_packet() || NETSTACK_RADIO.channel_clear() == 0)) { uint8_t ackbuf[ACK_LEN]; wt = RTIMER_NOW(); while(RTIMER_CLOCK_LT(RTIMER_NOW(), wt + AFTER_ACK_DETECTECT_WAIT_TIME)) { } len = NETSTACK_RADIO.read(ackbuf, ACK_LEN); //PRINTF("%u %u vs %u", len, ackbuf[ACK_LEN - 1], seqno); if(len == ACK_LEN && seqno == ackbuf[ACK_LEN - 1]) { got_strobe_ack = 1; encounter_time = txtime; break; } else { PRINTF("contikimac: collisions while sending\n"); collisions++; } } #endif /* RDC_CONF_HARDWARE_ACK */ } } off(); PRINTF("contikimac: send (strobes=%u, len=%u, %s, %s), done\n", strobes, packetbuf_totlen(), got_strobe_ack ? "ack" : "no ack", collisions ? "collision" : "no collision"); #if CONTIKIMAC_CONF_COMPOWER /* Accumulate the power consumption for the packet transmission. */ compower_accumulate(¤t_packet); /* Convert the accumulated power consumption for the transmitted packet to packet attributes so that the higher levels can keep track of the amount of energy spent on transmitting the packet. */ compower_attrconv(¤t_packet); /* Clear the accumulated power consumption so that it is ready for the next packet. */ compower_clear(¤t_packet); #endif /* CONTIKIMAC_CONF_COMPOWER */ contikimac_is_on = contikimac_was_on; we_are_sending = 0; /* Determine the return value that we will return from the function. We must pass this value to the phase module before we return from the function. */ if(collisions > 0) { ret = MAC_TX_COLLISION; } else if(!is_broadcast && !got_strobe_ack) { ret = MAC_TX_NOACK; } else { ret = MAC_TX_OK; } #if WITH_PHASE_OPTIMIZATION if(is_known_receiver && got_strobe_ack) { PRINTF("no miss %d wake-ups %d\n", packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[0], strobes); } if(!is_broadcast) { if(collisions == 0 && is_receiver_awake == 0) { phase_update(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), encounter_time, ret); } } #endif /* WITH_PHASE_OPTIMIZATION */ return ret; } /*---------------------------------------------------------------------------*/ static void qsend_packet(mac_callback_t sent, void *ptr) { int ret = send_packet(sent, ptr, NULL, 0); if(ret != MAC_TX_DEFERRED) { mac_call_sent_callback(sent, ptr, ret, 1); } }
/*---------------------------------------------------------------------------*/ static char powercycle(struct rtimer *t, void *ptr) { PT_BEGIN(&pt); cycle_start = RTIMER_NOW(); while(1) { static uint8_t packet_seen; static rtimer_clock_t t0; static uint8_t count; cycle_start += CYCLE_TIME; if(WITH_STREAMING && is_streaming) { if(!RTIMER_CLOCK_LT(RTIMER_NOW(), stream_until)) { is_streaming = 0; rimeaddr_copy(&is_streaming_to, &rimeaddr_null); rimeaddr_copy(&is_streaming_to_too, &rimeaddr_null); } } packet_seen = 0; do { for(count = 0; count < CCA_COUNT_MAX; ++count) { t0 = RTIMER_NOW(); if(we_are_sending == 0) { powercycle_turn_radio_on(); /* Check if a packet is seen in the air. If so, we keep the radio on for a while (LISTEN_TIME_AFTER_PACKET_DETECTED) to be able to receive the packet. We also continuously check the radio medium to make sure that we wasn't woken up by a false positive: a spurious radio interference that was not caused by an incoming packet. */ if(NETSTACK_RADIO.channel_clear() == 0) { packet_seen = 1; break; } powercycle_turn_radio_off(); } schedule_powercycle_fixed(t, RTIMER_NOW() + CCA_SLEEP_TIME); PT_YIELD(&pt); } if(packet_seen) { static rtimer_clock_t start; static uint8_t silence_periods, periods; start = RTIMER_NOW(); periods = silence_periods = 0; while(we_are_sending == 0 && radio_is_on && RTIMER_CLOCK_LT(RTIMER_NOW(), (start + LISTEN_TIME_AFTER_PACKET_DETECTED))) { /* Check for a number of consecutive periods of non-activity. If we see two such periods, we turn the radio off. Also, if a packet has been successfully received (as indicated by the NETSTACK_RADIO.pending_packet() function), we stop snooping. */ if(NETSTACK_RADIO.channel_clear()) { ++silence_periods; } else { silence_periods = 0; } ++periods; if(NETSTACK_RADIO.receiving_packet()) { silence_periods = 0; } if(silence_periods > MAX_SILENCE_PERIODS) { powercycle_turn_radio_off(); break; } if(WITH_FAST_SLEEP && periods > MAX_NONACTIVITY_PERIODS && !(NETSTACK_RADIO.receiving_packet() || NETSTACK_RADIO.pending_packet())) { powercycle_turn_radio_off(); break; } if(NETSTACK_RADIO.pending_packet()) { break; } schedule_powercycle(t, CCA_CHECK_TIME + CCA_SLEEP_TIME); PT_YIELD(&pt); } if(radio_is_on) { if(!(NETSTACK_RADIO.receiving_packet() || NETSTACK_RADIO.pending_packet()) || !RTIMER_CLOCK_LT(RTIMER_NOW(), (start + LISTEN_TIME_AFTER_PACKET_DETECTED))) { powercycle_turn_radio_off(); } } } } while((is_snooping || is_streaming) && RTIMER_CLOCK_LT(RTIMER_NOW() - cycle_start, CYCLE_TIME - CHECK_TIME * 8)); if(RTIMER_CLOCK_LT(RTIMER_NOW() - cycle_start, CYCLE_TIME - CHECK_TIME * 4)) { schedule_powercycle_fixed(t, CYCLE_TIME + cycle_start); PT_YIELD(&pt); } } PT_END(&pt); }
static char powercycle(struct rtimer *t, void *ptr) { PT_BEGIN(&pt); while(1) { static uint8_t packet_seen; static rtimer_clock_t t0; static uint8_t count; cycle_start = RTIMER_NOW(); if(WITH_STREAMING && is_streaming) { #if NURTIMER if(!RTIMER_CLOCK_LT(cycle_start, RTIMER_NOW(), stream_until)) #else if(!RTIMER_CLOCK_LT(RTIMER_NOW(), stream_until)) #endif { is_streaming = 0; rimeaddr_copy(&is_streaming_to, &rimeaddr_null); rimeaddr_copy(&is_streaming_to_too, &rimeaddr_null); } } packet_seen = 0; do { for(count = 0; count < CCA_COUNT_MAX; ++count) { t0 = RTIMER_NOW(); if(we_are_sending == 0) { powercycle_turn_radio_on(); #if 0 #if NURTIMER while(RTIMER_CLOCK_LT(t0, RTIMER_NOW(), t0 + CCA_CHECK_TIME)); #else while(RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + CCA_CHECK_TIME)); #endif #endif /* 0 */ /* Check if a packet is seen in the air. If so, we keep the radio on for a while (LISTEN_TIME_AFTER_PACKET_DETECTED) to be able to receive the packet. We also continuously check the radio medium to make sure that we wasn't woken up by a false positive: a spurious radio interference that was not caused by an incoming packet. */ if(NETSTACK_RADIO.channel_clear() == 0) { packet_seen = 1; break; } powercycle_turn_radio_off(); } schedule_powercycle_fixed(t, RTIMER_NOW() + CCA_SLEEP_TIME); /* COOJA_DEBUG_STR("yield\n");*/ PT_YIELD(&pt); } if(packet_seen) { static rtimer_clock_t start; static uint8_t silence_periods, periods; start = RTIMER_NOW(); periods = silence_periods = 0; while(we_are_sending == 0 && radio_is_on && RTIMER_CLOCK_LT(RTIMER_NOW(), (start + LISTEN_TIME_AFTER_PACKET_DETECTED))) { /* Check for a number of consecutive periods of non-activity. If we see two such periods, we turn the radio off. Also, if a packet has been successfully received (as indicated by the NETSTACK_RADIO.pending_packet() function), we stop snooping. */ if(NETSTACK_RADIO.channel_clear()) { ++silence_periods; } else { silence_periods = 0; } ++periods; if(NETSTACK_RADIO.receiving_packet()) { silence_periods = 0; } if(silence_periods > MAX_SILENCE_PERIODS) { LEDS_ON(LEDS_RED); powercycle_turn_radio_off(); #if CONTIKIMAC_CONF_COMPOWER compower_accumulate(&compower_idle_activity); #endif /* CONTIKIMAC_CONF_COMPOWER */ LEDS_OFF(LEDS_RED); break; } #if 1 if(periods > MAX_NONACTIVITY_PERIODIC && !(NETSTACK_RADIO.receiving_packet() || NETSTACK_RADIO.pending_packet())) { LEDS_ON(LEDS_GREEN); powercycle_turn_radio_off(); #if CONTIKIMAC_CONF_COMPOWER compower_accumulate(&compower_idle_activity); #endif /* CONTIKIMAC_CONF_COMPOWER */ LEDS_OFF(LEDS_GREEN); break; } #endif /* 0 */ if(NETSTACK_RADIO.pending_packet()) { break; } schedule_powercycle(t, CCA_CHECK_TIME + CCA_SLEEP_TIME); LEDS_ON(LEDS_BLUE); PT_YIELD(&pt); LEDS_OFF(LEDS_BLUE); } if(radio_is_on && !(NETSTACK_RADIO.receiving_packet() || NETSTACK_RADIO.pending_packet())) { LEDS_ON(LEDS_RED + LEDS_GREEN); powercycle_turn_radio_off(); #if CONTIKIMAC_CONF_COMPOWER compower_accumulate(&compower_idle_activity); #endif /* CONTIKIMAC_CONF_COMPOWER */ LEDS_OFF(LEDS_RED + LEDS_GREEN); } } else { #if CONTIKIMAC_CONF_COMPOWER compower_accumulate(&compower_idle_activity); #endif /* CONTIKIMAC_CONF_COMPOWER */ } } while(is_snooping && RTIMER_CLOCK_LT(RTIMER_NOW() - cycle_start, CYCLE_TIME - CHECK_TIME)); if(is_snooping) { LEDS_ON(LEDS_RED); } if(RTIMER_CLOCK_LT(RTIMER_NOW() - cycle_start, CYCLE_TIME)) { /* schedule_powercycle(t, CYCLE_TIME - (RTIMER_NOW() - cycle_start));*/ schedule_powercycle_fixed(t, CYCLE_TIME + cycle_start); /* printf("cycle_start 0x%02x now 0x%02x wait 0x%02x\n", cycle_start, RTIMER_NOW(), CYCLE_TIME - (RTIMER_NOW() - cycle_start));*/ PT_YIELD(&pt); } LEDS_OFF(LEDS_RED); } PT_END(&pt); }