Skip to content

Commit

Permalink
Merge pull request contiki-os#176 from adamdunkels/feature-net-bugfixes
Browse files Browse the repository at this point in the history
Network system bugfixes
  • Loading branch information
Nicolas Tsiftes committed Mar 18, 2013
2 parents adf401b + 7742b05 commit 6a07172
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 173 deletions.
2 changes: 2 additions & 0 deletions core/lib/print-stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
void
print_stats(void)
{
#if RIMESTATS_CONF_ENABLED
PRINTA("S %d.%d clock %lu tx %lu rx %lu rtx %lu rrx %lu rexmit %lu acktx %lu noacktx %lu ackrx %lu timedout %lu badackrx %lu toolong %lu tooshort %lu badsynch %lu badcrc %lu contentiondrop %lu sendingdrop %lu lltx %lu llrx %lu\n",
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
clock_seconds(),
Expand All @@ -66,6 +67,7 @@ print_stats(void)
rimestats.badsynch, rimestats.badcrc,
rimestats.contentiondrop, rimestats.sendingdrop,
rimestats.lltx, rimestats.llrx);
#endif /* RIMESTATS_CONF_ENABLED */
#if ENERGEST_CONF_ON
PRINTA("E %d.%d clock %lu cpu %lu lpm %lu irq %lu gled %lu yled %lu rled %lu tx %lu listen %lu sensors %lu serial %lu\n",
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
Expand Down
78 changes: 42 additions & 36 deletions core/net/mac/contikimac.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ struct hdr {

/* Are we currently receiving a burst? */
static int we_are_receiving_burst = 0;
/* Has the receiver been awoken by a burst we're sending? */
static int is_receiver_awake = 0;

/* BURST_RECV_TIME is the maximum time a receiver waits for the
next packet of a burst when FRAME_PENDING is set. */
Expand Down Expand Up @@ -377,16 +375,17 @@ powercycle(struct rtimer *t, void *ptr)
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;
/* 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;
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;
cycle_start = sync_cycle_start + (sync_cycle_phase*RTIMER_ARCH_SECOND)/NETSTACK_RDC_CHANNEL_CHECK_RATE;
#endif
}
#else
Expand Down Expand Up @@ -475,12 +474,13 @@ powercycle(struct rtimer *t, void *ptr)
}

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 */
/* 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) {
if((sleepcycle++ < 16) && !we_are_sending && !radio_is_on) {
rtimer_arch_sleep(CYCLE_TIME - (RTIMER_NOW() - cycle_start));
} else {
sleepcycle = 0;
Expand Down Expand Up @@ -519,7 +519,9 @@ broadcast_rate_drop(void)
}
/*---------------------------------------------------------------------------*/
static int
send_packet(mac_callback_t mac_callback, void *mac_callback_ptr, struct rdc_buf_list *buf_list)
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;
Expand All @@ -538,8 +540,8 @@ send_packet(mac_callback_t mac_callback, void *mac_callback_ptr, struct rdc_buf_
struct hdr *chdr;
#endif /* WITH_CONTIKIMAC_HEADER */

/* Exit if RDC and radio were explicitly turned off */
if (!contikimac_is_on && !contikimac_keep_radio_on) {
/* 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;
}
Expand Down Expand Up @@ -612,7 +614,6 @@ send_packet(mac_callback_t mac_callback, void *mac_callback_ptr, struct rdc_buf_
}
#endif


/* Make sure that the packet is longer or equal to the shortest
packet length. */
transmit_len = packetbuf_totlen();
Expand Down Expand Up @@ -694,7 +695,7 @@ send_packet(mac_callback_t mac_callback, void *mac_callback_ptr, struct rdc_buf_
/* 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;
int i;
for(i = 0; i < CCA_COUNT_MAX_TX; ++i) {
t0 = RTIMER_NOW();
on();
Expand Down Expand Up @@ -723,8 +724,9 @@ send_packet(mac_callback_t mac_callback, void *mac_callback_ptr, struct rdc_buf_

#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 */
/* 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
Expand All @@ -738,14 +740,14 @@ send_packet(mac_callback_t mac_callback, void *mac_callback_ptr, struct rdc_buf_

watchdog_periodic();

if((is_receiver_awake || is_known_receiver) && !RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + MAX_PHASE_STROBE_TIME)) {
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;
Expand All @@ -755,7 +757,8 @@ send_packet(mac_callback_t mac_callback, void *mac_callback_ptr, struct rdc_buf_
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 */
/* 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;
Expand All @@ -767,9 +770,9 @@ send_packet(mac_callback_t mac_callback, void *mac_callback_ptr, struct rdc_buf_
PRINTF("contikimac: collisions while sending\n");
collisions++;
}
wt = RTIMER_NOW();
wt = RTIMER_NOW();
while(RTIMER_CLOCK_LT(RTIMER_NOW(), wt + INTER_PACKET_INTERVAL)) { }
#else
#else /* RDC_CONF_HARDWARE_ACK */
/* Wait for the ACK packet */
wt = RTIMER_NOW();
while(RTIMER_CLOCK_LT(RTIMER_NOW(), wt + INTER_PACKET_INTERVAL)) { }
Expand All @@ -782,7 +785,7 @@ send_packet(mac_callback_t mac_callback, void *mac_callback_ptr, struct rdc_buf_
while(RTIMER_CLOCK_LT(RTIMER_NOW(), wt + AFTER_ACK_DETECTECT_WAIT_TIME)) { }

len = NETSTACK_RADIO.read(ackbuf, ACK_LEN);
if(len == ACK_LEN && seqno == ackbuf[ACK_LEN-1]) {
if(len == ACK_LEN && seqno == ackbuf[ACK_LEN - 1]) {
got_strobe_ack = 1;
encounter_time = txtime;
break;
Expand Down Expand Up @@ -832,16 +835,16 @@ send_packet(mac_callback_t mac_callback, void *mac_callback_ptr, struct rdc_buf_
}

#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],
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(&phase_list, packetbuf_addr(PACKETBUF_ADDR_RECEIVER), encounter_time,
ret);
phase_update(&phase_list, packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
encounter_time, ret);
}
}
#endif /* WITH_PHASE_OPTIMIZATION */
Expand All @@ -852,7 +855,7 @@ send_packet(mac_callback_t mac_callback, void *mac_callback_ptr, struct rdc_buf_
static void
qsend_packet(mac_callback_t sent, void *ptr)
{
int ret = send_packet(sent, ptr, NULL);
int ret = send_packet(sent, ptr, NULL, 0);
if(ret != MAC_TX_DEFERRED) {
mac_call_sent_callback(sent, ptr, ret, 1);
}
Expand All @@ -864,6 +867,8 @@ qsend_list(mac_callback_t sent, void *ptr, struct rdc_buf_list *buf_list)
struct rdc_buf_list *curr = buf_list;
struct rdc_buf_list *next;
int ret;
int is_receiver_awake;

if(curr == NULL) {
return;
}
Expand All @@ -887,7 +892,7 @@ qsend_list(mac_callback_t sent, void *ptr, struct rdc_buf_list *buf_list)
}

/* Send the current packet */
ret = send_packet(sent, ptr, curr);
ret = send_packet(sent, ptr, curr, is_receiver_awake);
if(ret != MAC_TX_DEFERRED) {
mac_call_sent_callback(sent, ptr, ret, 1);
}
Expand All @@ -903,11 +908,11 @@ qsend_list(mac_callback_t sent, void *ptr, struct rdc_buf_list *buf_list)
next = NULL;
}
} while(next != NULL);
is_receiver_awake = 0;
}
/*---------------------------------------------------------------------------*/
/* Timer callback triggered when receiving a burst, after having waited for a next
packet for a too long time. Turns the radio off and leaves burst reception mode */
/* Timer callback triggered when receiving a burst, after having
waited for a next packet for a too long time. Turns the radio off
and leaves burst reception mode */
static void
recv_burst_off(void *ptr)
{
Expand Down Expand Up @@ -955,7 +960,8 @@ input_packet(void)
we_are_receiving_burst = packetbuf_attr(PACKETBUF_ATTR_PENDING);
if(we_are_receiving_burst) {
on();
/* Set a timer to turn the radio off in case we do not receive a next packet */
/* Set a timer to turn the radio off in case we do not receive
a next packet */
ctimer_set(&ct, INTER_PACKET_DEADLINE, recv_burst_off, NULL);
} else {
off();
Expand Down
Loading

0 comments on commit 6a07172

Please sign in to comment.