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:
parent
1817acae15
commit
e34eb54960
29 changed files with 1176 additions and 768 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2007, Swedish Institute of Computer Science.
|
||||
* Copyright (c) 2010, Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -28,7 +28,7 @@
|
|||
*
|
||||
* This file is part of the Contiki operating system.
|
||||
*
|
||||
* $Id: csma.c,v 1.5 2010/02/03 01:17:54 adamdunkels Exp $
|
||||
* $Id: csma.c,v 1.6 2010/02/18 21:48:39 adamdunkels Exp $
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -47,170 +47,235 @@
|
|||
|
||||
#include "lib/random.h"
|
||||
|
||||
#include "net/netstack.h"
|
||||
|
||||
#include "lib/list.h"
|
||||
#include "lib/memb.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define DEBUG 0
|
||||
#if DEBUG
|
||||
#include <stdio.h>
|
||||
#define PRINTF(...) printf(__VA_ARGS__)
|
||||
#else /* DEBUG */
|
||||
#define PRINTF(...)
|
||||
#endif /* DEBUG */
|
||||
|
||||
struct queued_packet {
|
||||
struct queued_packet *next;
|
||||
struct queuebuf *buf;
|
||||
struct ctimer retransmit_timer;
|
||||
uint8_t retransmits;
|
||||
mac_callback_t sent;
|
||||
void *cptr;
|
||||
uint8_t transmissions;
|
||||
};
|
||||
|
||||
#define MAX_QUEUED_PACKETS 4
|
||||
LIST(packet_list);
|
||||
#define MAX_RETRANSMITS 2
|
||||
|
||||
#define MAX_QUEUED_PACKETS 8
|
||||
MEMB(packet_memb, struct queued_packet, MAX_QUEUED_PACKETS);
|
||||
|
||||
static const struct mac_driver *mac;
|
||||
static void (* receiver_callback)(const struct mac_driver *);
|
||||
|
||||
const struct mac_driver *csma_init(const struct mac_driver *psc);
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#if CSMA_CONF_REXMIT
|
||||
static void
|
||||
free_packet(struct queued_packet *q)
|
||||
{
|
||||
queuebuf_free(q->buf);
|
||||
memb_free(&packet_memb, q);
|
||||
}
|
||||
|
||||
static void retransmit_packet(void *ptr);
|
||||
|
||||
static void
|
||||
packet_sent(void *ptr, int status, int num_transmissions)
|
||||
{
|
||||
struct queued_packet *q = ptr;
|
||||
clock_time_t time = 0;
|
||||
mac_callback_t sent;
|
||||
void *cptr;
|
||||
int num_tx;
|
||||
|
||||
sent = q->sent;
|
||||
cptr = q->cptr;
|
||||
num_tx = q->transmissions;
|
||||
|
||||
if(status != MAC_TX_OK) {
|
||||
switch(status) {
|
||||
case MAC_TX_COLLISION:
|
||||
PRINTF("csma: rexmit collision %d\n", q->transmissions);
|
||||
break;
|
||||
case MAC_TX_NOACK:
|
||||
PRINTF("csma: rexmit noack %d\n", q->transmissions);
|
||||
break;
|
||||
default:
|
||||
PRINTF("csma: rexmit err %d, %d\n", ret, q->transmissions);
|
||||
}
|
||||
|
||||
time = NETSTACK_RDC.channel_check_interval();
|
||||
if(time == 0) {
|
||||
time = CLOCK_SECOND;
|
||||
}
|
||||
time = time + (random_rand() % (q->transmissions * 3 * time));
|
||||
|
||||
if(q->transmissions - 1 < MAX_RETRANSMITS) {
|
||||
ctimer_set(&q->retransmit_timer, time,
|
||||
retransmit_packet, q);
|
||||
} else {
|
||||
PRINTF("csma: drop after %d\n", q->transmissions);
|
||||
free_packet(q);
|
||||
mac_call_sent_callback(sent, ptr, status, num_tx);
|
||||
}
|
||||
} else {
|
||||
PRINTF("csma: rexmit ok %d\n", q->transmissions);
|
||||
free_packet(q);
|
||||
mac_call_sent_callback(sent, ptr, status, num_tx);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
retransmit_packet(void *ptr)
|
||||
{
|
||||
int ret;
|
||||
struct queued_packet *q = ptr;
|
||||
|
||||
queuebuf_to_packetbuf(q->buf);
|
||||
ret = mac->send();
|
||||
|
||||
queuebuf_free(q->buf);
|
||||
list_remove(packet_list, q);
|
||||
memb_free(&packet_memb, q);
|
||||
q->transmissions++;
|
||||
NETSTACK_RDC.send(packet_sent, q);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
send_packet(void)
|
||||
static void
|
||||
sent_packet_1(void *ptr, int status, int num_transmissions)
|
||||
{
|
||||
struct queuebuf *buf;
|
||||
int ret;
|
||||
struct queued_packet *q = ptr;
|
||||
clock_time_t time;
|
||||
rimeaddr_t receiver;
|
||||
|
||||
/* Remember packet for later. */
|
||||
|
||||
rimeaddr_copy(&receiver, packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
|
||||
buf = queuebuf_new_from_packetbuf();
|
||||
ret = mac->send();
|
||||
/* if(ret != MAC_TX_OK) {
|
||||
printf("CSMA: err %d\n", ret);
|
||||
}*/
|
||||
|
||||
if(rimeaddr_cmp(&receiver, &rimeaddr_null)) {
|
||||
PRINTF("broadcast/");
|
||||
}
|
||||
if(status != MAC_TX_OK) {
|
||||
switch(status) {
|
||||
case MAC_TX_COLLISION:
|
||||
PRINTF("csma: collision\n");
|
||||
break;
|
||||
case MAC_TX_NOACK:
|
||||
PRINTF("csma: noack\n");
|
||||
break;
|
||||
default:
|
||||
PRINTF("csma: err %d\n", ret);
|
||||
}
|
||||
} else {
|
||||
PRINTF("csma: ok\n");
|
||||
}
|
||||
|
||||
/* Check if we saw a collission, and if we have a queuebuf with the
|
||||
packet available. Only retransmit unicast packets. Retransmit
|
||||
only once, for now. */
|
||||
if((ret == MAC_TX_COLLISION || ret == MAC_TX_NOACK) &&
|
||||
buf != NULL && !rimeaddr_cmp(&receiver, &rimeaddr_null)) {
|
||||
struct queued_packet *q;
|
||||
|
||||
q = memb_alloc(&packet_memb);
|
||||
if(q == NULL) {
|
||||
queuebuf_free(buf);
|
||||
return ret;
|
||||
if((status == MAC_TX_COLLISION || status == MAC_TX_NOACK) &&
|
||||
!rimeaddr_cmp(&receiver, &rimeaddr_null)) {
|
||||
/* If the packet couldn't be sent because of a collision or the
|
||||
lack of an ACK, we let the other transmissions get through
|
||||
before we try again. */
|
||||
time = NETSTACK_RDC.channel_check_interval();
|
||||
if(time == 0) {
|
||||
time = CLOCK_SECOND;
|
||||
}
|
||||
q->buf = buf;
|
||||
q->retransmits = 0;
|
||||
time = time + (random_rand() % (3 * time));
|
||||
|
||||
if(ret == MAC_TX_COLLISION) {
|
||||
/* If the packet wasn't sent because of a collission, we let the
|
||||
other packet get through before we try again. */
|
||||
time = mac->channel_check_interval();
|
||||
if(time == 0) {
|
||||
time = CLOCK_SECOND;
|
||||
}
|
||||
time = time + (random_rand() % (3 * time));
|
||||
} else {
|
||||
/* If the packet didn't get an ACK, we retransmit immediately. */
|
||||
time = 0;
|
||||
}
|
||||
|
||||
ctimer_set(&q->retransmit_timer, time,
|
||||
retransmit_packet, q);
|
||||
list_add(packet_list, q);
|
||||
} else {
|
||||
queuebuf_free(buf);
|
||||
mac_callback_t sent;
|
||||
void *cptr;
|
||||
int num_tx;
|
||||
|
||||
sent = q->sent;
|
||||
cptr = q->cptr;
|
||||
num_tx = q->transmissions;
|
||||
free_packet(q);
|
||||
mac_call_sent_callback(sent, ptr, status, num_tx);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
send_packet(mac_callback_t sent, void *ptr)
|
||||
{
|
||||
struct queued_packet *q;
|
||||
|
||||
/* Remember packet for later. */
|
||||
q = memb_alloc(&packet_memb);
|
||||
if(q != NULL) {
|
||||
q->buf = queuebuf_new_from_packetbuf();
|
||||
if(q != NULL) {
|
||||
q->transmissions = 1;
|
||||
q->sent = sent;
|
||||
q->cptr = ptr;
|
||||
NETSTACK_RDC.send(sent_packet_1, q);
|
||||
return;
|
||||
}
|
||||
memb_free(&packet_memb, q);
|
||||
}
|
||||
PRINTF("csma: could not allocate queuebuf, will drop if collision or noack\n");
|
||||
NETSTACK_RDC.send(sent, ptr);
|
||||
}
|
||||
#else /* CSMA_CONF_REXMIT */
|
||||
static int
|
||||
send_packet(void)
|
||||
static void
|
||||
send_packet(mac_callback_t sent, void *ptr)
|
||||
{
|
||||
return mac->send();
|
||||
NETSTACK_RDC.send(NULL, NULL);
|
||||
}
|
||||
#endif /* CSMA_CONF_REXMIT */
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
input_packet(const struct mac_driver *d)
|
||||
input_packet(void)
|
||||
{
|
||||
if(receiver_callback) {
|
||||
receiver_callback(&csma_driver);
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
read_packet(void)
|
||||
{
|
||||
int len;
|
||||
len = mac->read();
|
||||
return len;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
set_receive_function(void (* recv)(const struct mac_driver *))
|
||||
{
|
||||
receiver_callback = recv;
|
||||
NETSTACK_NETWORK.input();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
on(void)
|
||||
{
|
||||
return mac->on();
|
||||
return NETSTACK_RDC.on();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
off(int keep_radio_on)
|
||||
{
|
||||
return mac->off(keep_radio_on);
|
||||
return NETSTACK_RDC.off(keep_radio_on);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static unsigned short
|
||||
channel_check_interval(void)
|
||||
{
|
||||
if(mac->channel_check_interval) {
|
||||
return mac->channel_check_interval();
|
||||
if(NETSTACK_RDC.channel_check_interval) {
|
||||
return NETSTACK_RDC.channel_check_interval();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define NAMEBUF_LEN 16
|
||||
static char namebuf[NAMEBUF_LEN];
|
||||
static void
|
||||
init(void)
|
||||
{
|
||||
memb_init(&packet_memb);
|
||||
|
||||
/* PRINTF("CSMA with MAC %s, channel check rate %d Hz\n", mac->name,
|
||||
CLOCK_SECOND / channel_check_interval());*/
|
||||
memcpy(namebuf, "CSMA ", 5);
|
||||
memcpy(namebuf + 5, NETSTACK_RDC.name, NAMEBUF_LEN - 6);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
const struct mac_driver csma_driver = {
|
||||
namebuf,
|
||||
NULL,
|
||||
init,
|
||||
send_packet,
|
||||
read_packet,
|
||||
set_receive_function,
|
||||
input_packet,
|
||||
on,
|
||||
off,
|
||||
channel_check_interval,
|
||||
};
|
||||
/*---------------------------------------------------------------------------*/
|
||||
const struct mac_driver *
|
||||
csma_init(const struct mac_driver *psc)
|
||||
{
|
||||
memb_init(&packet_memb);
|
||||
list_init(packet_list);
|
||||
mac = psc;
|
||||
mac->set_receive_function(input_packet);
|
||||
/* printf("CSMA with MAC %s, channel check rate %d Hz\n", mac->name,
|
||||
CLOCK_SECOND / channel_check_interval());*/
|
||||
memcpy(namebuf, "CSMA ", 5);
|
||||
memcpy(namebuf + 5, psc->name, NAMEBUF_LEN - 6);
|
||||
return &csma_driver;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue