Rewrote the timesynch code to use the Rime sniffer interface instead of the kludge of running timesynch as a MAC layer

This commit is contained in:
adamdunkels 2008-01-14 14:50:01 +00:00
parent 02ee418313
commit 34bd11905c
3 changed files with 28 additions and 85 deletions

View file

@ -34,7 +34,7 @@
*
* This file is part of the Contiki operating system.
*
* $Id: timesynch.c,v 1.1 2008/01/14 14:22:16 adamdunkels Exp $
* $Id: timesynch.c,v 1.2 2008/01/14 14:50:01 adamdunkels Exp $
*/
/**
@ -49,9 +49,6 @@
#include "net/rime.h"
#include "dev/simple-cc2420.h"
static const struct mac_driver *mac;
static void (* receiver_callback)(const struct mac_driver *);
#include <stdio.h>
static int authority_level;
@ -88,44 +85,28 @@ timesynch_offset(void)
return offset;
}
/*---------------------------------------------------------------------------*/
static int
send_packet(void)
{
return mac->send();
}
/*---------------------------------------------------------------------------*/
static void
adjust_offset(rtimer_clock_t authoritative_time, rtimer_clock_t local_time)
{
offset = offset + authoritative_time - local_time;
}
/*---------------------------------------------------------------------------*/
static int
read_packet(void)
static void
incoming_packet(void)
{
int len;
len = mac->read();
if(len == 0) {
return 0;
}
/* We check the authority level of the sender of the incoming
packet. If the sending node has a lower authority level than we
have, we synchronize to the time of the sending node and set our
own authority level to be one more than the sending node. */
if(simple_cc2420_authority_level_of_sender < authority_level) {
adjust_offset(simple_cc2420_time_of_departure,
simple_cc2420_time_of_arrival);
if(simple_cc2420_authority_level_of_sender + 1 != authority_level) {
authority_level = simple_cc2420_authority_level_of_sender + 1;
if(rimebuf_totlen() != 0) {
/* We check the authority level of the sender of the incoming
packet. If the sending node has a lower authority level than we
have, we synchronize to the time of the sending node and set our
own authority level to be one more than the sending node. */
if(simple_cc2420_authority_level_of_sender < authority_level) {
adjust_offset(simple_cc2420_time_of_departure,
simple_cc2420_time_of_arrival);
if(simple_cc2420_authority_level_of_sender + 1 != authority_level) {
authority_level = simple_cc2420_authority_level_of_sender + 1;
}
}
}
return len;
}
/*---------------------------------------------------------------------------*/
#if 0
@ -137,47 +118,12 @@ periodic_authority_increase(void *ptr)
}
#endif
/*---------------------------------------------------------------------------*/
static void
set_receive_function(void (* recv)(const struct mac_driver *))
{
receiver_callback = recv;
}
RIME_SNIFFER(sniffer, incoming_packet, NULL);
/*---------------------------------------------------------------------------*/
static int
on(void)
void
timesynch_init(void)
{
return mac->on();
}
/*---------------------------------------------------------------------------*/
static int
off(void)
{
return mac->off();
}
/*---------------------------------------------------------------------------*/
static const struct mac_driver timesynch_driver = {
send_packet,
read_packet,
set_receive_function,
on,
off,
};
/*---------------------------------------------------------------------------*/
static void
input_packet(const struct mac_driver *d)
{
if(receiver_callback) {
receiver_callback(&timesynch_driver);
}
}
/*---------------------------------------------------------------------------*/
const struct mac_driver *
timesynch_init(const struct mac_driver *d)
{
mac = d;
mac->set_receive_function(input_packet);
mac->on();
return &timesynch_driver;
rime_sniffer_add(&sniffer);
}
/*---------------------------------------------------------------------------*/
/** @} */

View file

@ -55,7 +55,7 @@
*
* This file is part of the Contiki operating system.
*
* $Id: timesynch.h,v 1.1 2008/01/14 14:22:16 adamdunkels Exp $
* $Id: timesynch.h,v 1.2 2008/01/14 14:50:02 adamdunkels Exp $
*/
/**
@ -72,16 +72,13 @@
#include "sys/rtimer.h"
/**
* \brief Initialize the timesynch module; should be called during the MAC initialization procedure
* \param r The underlying MAC driver
* \return A pointer to the timesynch module's meta MAC driver
* \brief Initialize the timesynch module
*
* This function initializes the timesynch module. The
* function should be called as part of the MAC
* initialization procedure.
* This function initializes the timesynch module. This
* function must not be called before rime_init().
*
*/
const struct mac_driver *timesynch_init(const struct mac_driver *r);
void timesynch_init(void);
/**
* \brief Get the current time-synchronized time

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)$Id: contiki-sky-main.c,v 1.22 2008/01/14 14:22:17 adamdunkels Exp $
* @(#)$Id: contiki-sky-main.c,v 1.23 2008/01/14 14:50:02 adamdunkels Exp $
*/
#include <signal.h>
@ -151,7 +151,7 @@ main(int argc, char **argv)
#endif /* WITH_UIP */
/* printf("Starting %s " */
/* "($Id: contiki-sky-main.c,v 1.22 2008/01/14 14:22:17 adamdunkels Exp $)\n", __FILE__); */
/* "($Id: contiki-sky-main.c,v 1.23 2008/01/14 14:50:02 adamdunkels Exp $)\n", __FILE__); */
leds_on(LEDS_GREEN);
ds2411_init();
sensors_light_init();
@ -211,9 +211,9 @@ main(int argc, char **argv)
simple_cc2420_init();
simple_cc2420_set_chan_pan_addr(RF_CHANNEL, panId, 0 /*XXX*/, ds2411_id);
/* rime_init(nullmac_init(&simple_cc2420_driver));*/
/* rime_init(xmac_init(&simple_cc2420_driver));*/
/* rime_init(timesynch_init(nullmac_init(&simple_cc2420_driver)));*/
rime_init(timesynch_init(xmac_init(&simple_cc2420_driver)));
rime_init(xmac_init(&simple_cc2420_driver));
timesynch_init();
timesynch_set_authority_level(rimeaddr_node_addr.u8[0]);