A work-in-progress rework of the Contiki MAC and radio layers. The

main ideas are:

* Separates the Contiki low-layer network stack into four layers:
  network (e.g. sicslowpan / rime), Medium Access Control MAC
  (e.g. CSMA), Radio Duty Cycling RDC (e.g. ContikiMAC, X-MAC), and
  radio (e.g. cc2420).
* Introduces a new way to configure the network stack. Four #defines
  that specify what mechanism/protocol/driver to use at the four
  layers: NETSTACK_CONF_NETWORK, NETSTACK_CONF_MAC, NETSTACK_CONF_RDC,
  NETSTACK_CONF_RADIO.
* Adds a callback mechanism to inform the MAC and network layers about
  the fate of a transmitted packet: if the packet was not possible to
  transmit, the cause of the failure is reported, and if the packets
  was successfully transmitted, the number of tries before it was
  finally transmitted is reported.
* NULL-protocols at both the MAC and RDC layers: nullmac and nullrdc,
  which can be used when MAC and RDC functionality is not needed.
* Extends the radio API with three new functions that enable more
  efficient radio duty cycling protocols: channel check, pending
  packet, and receiving packet.
* New initialization mechanism, which takes advantage of the NETSTACK
  #defines.
This commit is contained in:
adamdunkels 2010-02-18 21:48:39 +00:00
parent 1817acae15
commit e34eb54960
29 changed files with 1176 additions and 768 deletions

View file

@ -28,7 +28,7 @@
*
* This file is part of the Contiki operating system.
*
* $Id: xmac.c,v 1.52 2010/02/03 01:17:32 adamdunkels Exp $
* $Id: xmac.c,v 1.53 2010/02/18 21:48:39 adamdunkels Exp $
*/
/**
@ -44,6 +44,7 @@
#include "dev/radio.h"
#include "dev/watchdog.h"
#include "lib/random.h"
#include "net/netstack.h"
#include "net/mac/framer.h"
#include "net/mac/xmac.h"
#include "net/rime.h"
@ -157,8 +158,6 @@ static volatile unsigned char someone_is_sending = 0;
static volatile unsigned char we_are_sending = 0;
static volatile unsigned char radio_is_on = 0;
static const struct radio_driver *radio;
#undef LEDS_ON
#undef LEDS_OFF
#undef LEDS_TOGGLE
@ -193,8 +192,6 @@ static int announcement_radio_txpower;
for announcements from neighbors. */
static uint8_t is_listening;
static void (* receiver_callback)(const struct mac_driver *);
#if XMAC_CONF_COMPOWER
static struct compower_activity current_packet;
#endif /* XMAC_CONF_COMPOWER */
@ -224,19 +221,13 @@ static rtimer_clock_t stream_until;
#define MIN(a, b) ((a) < (b)? (a) : (b))
#endif /* MIN */
/*---------------------------------------------------------------------------*/
static void
set_receive_function(void (* recv)(const struct mac_driver *))
{
receiver_callback = recv;
}
/*---------------------------------------------------------------------------*/
static void
on(void)
{
if(xmac_is_on && radio_is_on == 0) {
radio_is_on = 1;
radio->on();
NETSTACK_RADIO.on();
LEDS_ON(LEDS_RED);
}
}
@ -247,7 +238,7 @@ off(void)
if(xmac_is_on && radio_is_on != 0 && is_listening == 0 &&
is_streaming == 0) {
radio_is_on = 0;
radio->off();
NETSTACK_RADIO.off();
LEDS_OFF(LEDS_RED);
}
}
@ -566,7 +557,7 @@ send_packet(void)
rtimer_clock_t now = RTIMER_NOW();
/* See if we got an ACK */
packetbuf_clear();
len = radio->read(packetbuf_dataptr(), PACKETBUF_SIZE);
len = NETSTACK_RADIO.read(packetbuf_dataptr(), PACKETBUF_SIZE);
if(len > 0) {
packetbuf_set_datalen(len);
if(framer_get()->parse()) {
@ -597,16 +588,16 @@ send_packet(void)
if(is_broadcast) {
#if WITH_STROBE_BROADCAST
radio->send(strobe, strobe_len);
NETSTACK_RADIO.send(strobe, strobe_len);
#else
/* restore the packet to send */
queuebuf_to_packetbuf(packet);
radio->send(packetbuf_hdrptr(), packetbuf_totlen());
NETSTACK_RADIO.send(packetbuf_hdrptr(), packetbuf_totlen());
#endif
off();
} else {
rtimer_clock_t wt;
radio->send(strobe, strobe_len);
NETSTACK_RADIO.send(strobe, strobe_len);
#if 1
/* Turn off the radio for a while to let the other side
respond. We don't need to keep our radio on when we know
@ -644,7 +635,7 @@ send_packet(void)
/* Send the data packet. */
if((is_broadcast || got_strobe_ack || is_streaming) && collisions == 0) {
radio->send(packetbuf_hdrptr(), packetbuf_totlen());
NETSTACK_RADIO.send(packetbuf_hdrptr(), packetbuf_totlen());
}
#if WITH_ENCOUNTER_OPTIMIZATION
@ -688,42 +679,28 @@ send_packet(void)
}
/*---------------------------------------------------------------------------*/
static int
qsend_packet(void)
static void
qsend_packet(mac_callback_t sent, void *ptr)
{
int ret;
if(someone_is_sending) {
PRINTF("xmac: should queue packet, now just dropping %d %d %d %d.\n",
waiting_for_packet, someone_is_sending, we_are_sending, radio_is_on);
RIMESTATS_ADD(sendingdrop);
return MAC_TX_COLLISION;
ret = MAC_TX_COLLISION;
} else {
PRINTF("xmac: send immediately.\n");
return send_packet();
ret = send_packet();
}
mac_call_sent_callback(sent, ptr, ret, 1);
}
/*---------------------------------------------------------------------------*/
static void
input_packet(const struct radio_driver *d)
{
if(receiver_callback) {
receiver_callback(&xmac_driver);
}
}
/*---------------------------------------------------------------------------*/
static int
read_packet(void)
input_packet(void)
{
struct xmac_hdr *hdr;
uint8_t len;
packetbuf_clear();
len = radio->read(packetbuf_dataptr(), PACKETBUF_SIZE);
if(len == 0) {
return 0;
}
packetbuf_set_datalen(len);
if(framer_get()->parse()) {
hdr = packetbuf_dataptr();
@ -758,7 +735,8 @@ read_packet(void)
waiting_for_packet = 0;
PRINTDEBUG("xmac: data(%u)\n", packetbuf_datalen());
return packetbuf_datalen();
NETSTACK_MAC.input();
return;
} else {
PRINTDEBUG("xmac: data not for us\n");
}
@ -785,7 +763,7 @@ read_packet(void)
someone_is_sending = 1;
waiting_for_packet = 1;
on();
radio->send(packetbuf_hdrptr(), packetbuf_totlen());
NETSTACK_RADIO.send(packetbuf_hdrptr(), packetbuf_totlen());
PRINTDEBUG("xmac: send strobe ack %u\n", packetbuf_totlen());
} else {
PRINTF("xmac: failed to send strobe ack\n");
@ -804,7 +782,7 @@ read_packet(void)
/* We are done processing the strobe and we therefore return
to the caller. */
return RIME_OK;
return;
#if XMAC_CONF_ANNOUNCEMENTS
} else if(hdr->type == TYPE_ANNOUNCEMENT) {
packetbuf_hdrreduce(sizeof(struct xmac_hdr));
@ -819,7 +797,6 @@ read_packet(void)
} else {
PRINTF("xmac: failed to parse (%u)\n", packetbuf_totlen());
}
return 0;
}
/*---------------------------------------------------------------------------*/
#if XMAC_CONF_ANNOUNCEMENTS
@ -845,7 +822,7 @@ send_announcement(void *ptr)
packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, &rimeaddr_null);
packetbuf_set_attr(PACKETBUF_ATTR_RADIO_TXPOWER, announcement_radio_txpower);
if(framer_get()->create()) {
radio->send(packetbuf_hdrptr(), packetbuf_totlen());
NETSTACK_RADIO.send(packetbuf_hdrptr(), packetbuf_totlen());
}
}
}
@ -878,8 +855,8 @@ xmac_set_announcement_radio_txpower(int txpower)
#endif /* XMAC_CONF_ANNOUNCEMENTS */
}
/*---------------------------------------------------------------------------*/
const struct mac_driver *
xmac_init(const struct radio_driver *d)
static void
init(void)
{
radio_is_on = 0;
waiting_for_packet = 0;
@ -888,8 +865,6 @@ xmac_init(const struct radio_driver *d)
(void (*)(struct rtimer *, void *))powercycle, NULL);
xmac_is_on = 1;
radio = d;
radio->set_receive_function(input_packet);
#if WITH_ENCOUNTER_OPTIMIZATION
list_init(encounter_list);
@ -901,7 +876,6 @@ xmac_init(const struct radio_driver *d)
ctimer_set(&announcement_cycle_ctimer, ANNOUNCEMENT_TIME,
cycle_announcement, NULL);
#endif /* XMAC_CONF_ANNOUNCEMENTS */
return &xmac_driver;
}
/*---------------------------------------------------------------------------*/
static int
@ -918,9 +892,9 @@ turn_off(int keep_radio_on)
{
xmac_is_on = 0;
if(keep_radio_on) {
return radio->on();
return NETSTACK_RADIO.on();
} else {
return radio->off();
return NETSTACK_RADIO.off();
}
}
/*---------------------------------------------------------------------------*/
@ -933,10 +907,9 @@ channel_check_interval(void)
const struct mac_driver xmac_driver =
{
"X-MAC",
xmac_init,
init,
qsend_packet,
read_packet,
set_receive_function,
input_packet,
turn_on,
turn_off,
channel_check_interval,