2007-04-07 05:43:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2007, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
2009-02-07 18:45:50 +00:00
|
|
|
* $Id: xmac.c,v 1.22 2009/02/07 18:45:50 adamdunkels Exp $
|
2007-04-07 05:43:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* A simple power saving MAC protocol based on X-MAC [SenSys 2006]
|
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sys/pt.h"
|
|
|
|
#include "net/mac/xmac.h"
|
|
|
|
#include "sys/rtimer.h"
|
|
|
|
#include "dev/leds.h"
|
|
|
|
#include "net/rime.h"
|
2008-01-23 15:00:54 +00:00
|
|
|
#include "net/rime/timesynch.h"
|
2007-04-07 05:43:31 +00:00
|
|
|
#include "dev/radio.h"
|
2008-02-24 21:07:28 +00:00
|
|
|
#include "dev/watchdog.h"
|
2007-11-12 22:29:37 +00:00
|
|
|
#include "lib/random.h"
|
|
|
|
/*#include "lib/bb.h"*/
|
2007-04-07 05:43:31 +00:00
|
|
|
|
2008-01-23 15:00:54 +00:00
|
|
|
#include "sys/timetable.h"
|
|
|
|
|
2007-05-25 08:06:44 +00:00
|
|
|
#include "contiki-conf.h"
|
|
|
|
|
2007-11-12 22:29:37 +00:00
|
|
|
#if CHAMELEON
|
2007-05-15 08:07:07 +00:00
|
|
|
#include "net/chameleon/packattr.h"
|
|
|
|
#endif
|
|
|
|
|
2008-01-24 06:17:51 +00:00
|
|
|
#define WITH_TIMETABLE 0
|
2008-02-25 02:14:34 +00:00
|
|
|
#define WITH_CHANNEL_CHECK 0 /* Seems to work badly when enabled */
|
2008-02-24 21:07:28 +00:00
|
|
|
#define WITH_TIMESYNCH 0
|
2007-05-25 08:06:44 +00:00
|
|
|
#define WITH_QUEUE 0
|
|
|
|
|
2007-04-07 05:43:31 +00:00
|
|
|
struct xmac_hdr {
|
2007-05-15 08:07:07 +00:00
|
|
|
rimeaddr_t sender;
|
2007-04-07 05:43:31 +00:00
|
|
|
rimeaddr_t receiver;
|
|
|
|
};
|
|
|
|
|
2007-05-22 20:55:24 +00:00
|
|
|
#ifdef XMAC_CONF_ON_TIME
|
2007-11-12 22:29:37 +00:00
|
|
|
#define DEFAULT_ON_TIME (XMAC_CONF_ON_TIME)
|
2007-05-15 08:07:07 +00:00
|
|
|
#else
|
2008-02-24 21:07:28 +00:00
|
|
|
#define DEFAULT_ON_TIME (RTIMER_ARCH_SECOND / 200)
|
2007-05-15 08:07:07 +00:00
|
|
|
#endif
|
2007-04-07 05:43:31 +00:00
|
|
|
|
2007-05-22 20:55:24 +00:00
|
|
|
#ifdef XMAC_CONF_OFF_TIME
|
2007-11-12 22:29:37 +00:00
|
|
|
#define DEFAULT_OFF_TIME (XMAC_CONF_OFF_TIME)
|
2007-05-15 08:07:07 +00:00
|
|
|
#else
|
2008-06-30 08:10:02 +00:00
|
|
|
#define DEFAULT_OFF_TIME (RTIMER_ARCH_SECOND / 2 - DEFAULT_ON_TIME)
|
2007-05-15 08:07:07 +00:00
|
|
|
#endif
|
|
|
|
|
2008-02-24 21:07:28 +00:00
|
|
|
#define DEFAULT_STROBE_WAIT_TIME (7 * DEFAULT_ON_TIME / 8)
|
2008-01-14 09:26:42 +00:00
|
|
|
|
2007-11-12 22:29:37 +00:00
|
|
|
struct xmac_config xmac_config = {
|
2008-01-14 09:26:42 +00:00
|
|
|
DEFAULT_ON_TIME,
|
|
|
|
DEFAULT_OFF_TIME,
|
2008-02-24 21:07:28 +00:00
|
|
|
20 * DEFAULT_ON_TIME + DEFAULT_OFF_TIME,
|
2007-11-12 22:29:37 +00:00
|
|
|
DEFAULT_STROBE_WAIT_TIME
|
|
|
|
};
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
static struct rtimer rt;
|
|
|
|
static struct pt pt;
|
2007-04-07 05:43:31 +00:00
|
|
|
|
2007-12-05 13:23:17 +00:00
|
|
|
static int xmac_is_on = 0;
|
|
|
|
|
2008-01-23 15:00:54 +00:00
|
|
|
static volatile unsigned char waiting_for_packet = 0;
|
2007-05-15 08:07:07 +00:00
|
|
|
static volatile unsigned char someone_is_sending = 0;
|
|
|
|
static volatile unsigned char we_are_sending = 0;
|
2007-04-07 05:43:31 +00:00
|
|
|
static volatile unsigned char radio_is_on = 0;
|
|
|
|
|
2007-05-15 08:07:07 +00:00
|
|
|
static const struct radio_driver *radio;
|
2007-04-07 05:43:31 +00:00
|
|
|
|
|
|
|
#undef LEDS_ON
|
|
|
|
#undef LEDS_OFF
|
|
|
|
#undef LEDS_TOGGLE
|
|
|
|
|
2008-01-14 09:26:42 +00:00
|
|
|
|
2008-01-23 15:00:54 +00:00
|
|
|
#define CPRINTF(...)
|
2008-01-14 09:26:42 +00:00
|
|
|
|
2007-11-12 22:29:37 +00:00
|
|
|
#define BB_SET(x,y)
|
|
|
|
#define BB_INC(x,y)
|
|
|
|
/* #define BB_SET(x,y) bb_set(x,y) */
|
|
|
|
/* #define BB_INC(x,y) bb_inc(x,y) */
|
2007-04-07 05:43:31 +00:00
|
|
|
#define LEDS_ON(x) leds_on(x)
|
|
|
|
#define LEDS_OFF(x) leds_off(x)
|
|
|
|
#define LEDS_TOGGLE(x) leds_toggle(x)
|
2007-11-12 22:29:37 +00:00
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
2007-05-15 08:07:07 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
2007-04-07 05:43:31 +00:00
|
|
|
#else
|
2007-11-17 10:12:19 +00:00
|
|
|
#undef LEDS_ON
|
|
|
|
#undef LEDS_OFF
|
|
|
|
#undef LEDS_TOGGLE
|
2007-04-07 05:43:31 +00:00
|
|
|
#define LEDS_ON(x)
|
|
|
|
#define LEDS_OFF(x)
|
|
|
|
#define LEDS_TOGGLE(x)
|
2007-05-15 08:07:07 +00:00
|
|
|
#define PRINTF(...)
|
2007-04-07 05:43:31 +00:00
|
|
|
#endif
|
|
|
|
|
2007-05-25 08:06:44 +00:00
|
|
|
static void (* receiver_callback)(const struct mac_driver *);
|
2008-01-23 15:00:54 +00:00
|
|
|
|
2008-01-24 06:17:51 +00:00
|
|
|
#if WITH_TIMETABLE
|
2008-02-24 21:07:28 +00:00
|
|
|
#define xmac_timetable_size 1024
|
2008-01-23 15:00:54 +00:00
|
|
|
TIMETABLE_NONSTATIC(xmac_timetable);
|
2008-01-24 06:17:51 +00:00
|
|
|
#endif /* WITH_TIMETABLE */
|
2007-05-25 08:06:44 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
set_receive_function(void (* recv)(const struct mac_driver *))
|
|
|
|
{
|
|
|
|
receiver_callback = recv;
|
|
|
|
}
|
2007-04-07 05:43:31 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-01-23 15:00:54 +00:00
|
|
|
static void
|
|
|
|
on(void)
|
|
|
|
{
|
2008-02-24 21:07:28 +00:00
|
|
|
if(xmac_is_on && radio_is_on == 0) {
|
2008-01-23 15:00:54 +00:00
|
|
|
radio_is_on = 1;
|
|
|
|
radio->on();
|
|
|
|
LEDS_ON(LEDS_RED);
|
|
|
|
CPRINTF("/");
|
2008-01-24 06:17:51 +00:00
|
|
|
#if WITH_TIMETABLE
|
2008-01-23 15:00:54 +00:00
|
|
|
TIMETABLE_TIMESTAMP(xmac_timetable, "on");
|
2008-01-24 06:17:51 +00:00
|
|
|
#endif
|
2008-01-23 15:00:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
off(void)
|
|
|
|
{
|
2008-02-24 21:07:28 +00:00
|
|
|
if(xmac_is_on && radio_is_on != 0) {
|
2008-01-23 15:00:54 +00:00
|
|
|
radio_is_on = 0;
|
|
|
|
radio->off();
|
2008-01-24 06:17:51 +00:00
|
|
|
#if WITH_TIMETABLE
|
2008-01-23 15:00:54 +00:00
|
|
|
TIMETABLE_TIMESTAMP(xmac_timetable, "off");
|
2008-01-24 06:17:51 +00:00
|
|
|
#endif
|
2008-01-23 15:00:54 +00:00
|
|
|
LEDS_OFF(LEDS_RED);
|
|
|
|
CPRINTF("\\");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-05-19 13:12:00 +00:00
|
|
|
static char
|
2007-04-07 05:43:31 +00:00
|
|
|
powercycle(struct rtimer *t, void *ptr)
|
|
|
|
{
|
2007-05-25 08:06:44 +00:00
|
|
|
int r;
|
2008-01-23 15:00:54 +00:00
|
|
|
#if WITH_TIMESYNCH
|
|
|
|
rtimer_clock_t should_be, adjust;
|
|
|
|
#endif /* WITH_TIMESYNCH */
|
2007-11-12 22:29:37 +00:00
|
|
|
|
2008-01-23 15:00:54 +00:00
|
|
|
CPRINTF("*");
|
2008-01-14 09:26:42 +00:00
|
|
|
|
2007-04-07 05:43:31 +00:00
|
|
|
PT_BEGIN(&pt);
|
|
|
|
|
|
|
|
while(1) {
|
2007-11-12 22:29:37 +00:00
|
|
|
/* Only wait for some cycles to pass for someone to start sending */
|
|
|
|
if(someone_is_sending > 0) {
|
|
|
|
someone_is_sending--;
|
2007-04-07 05:43:31 +00:00
|
|
|
}
|
2007-11-12 22:29:37 +00:00
|
|
|
|
|
|
|
if(xmac_config.off_time > 0) {
|
2008-01-23 15:00:54 +00:00
|
|
|
if(waiting_for_packet == 0) {
|
2008-02-24 21:07:28 +00:00
|
|
|
if(we_are_sending == 0) {
|
2008-01-23 15:00:54 +00:00
|
|
|
off();
|
2007-12-05 13:23:17 +00:00
|
|
|
}
|
2007-11-12 22:29:37 +00:00
|
|
|
} else {
|
2008-01-23 15:00:54 +00:00
|
|
|
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;
|
2008-01-24 06:17:51 +00:00
|
|
|
#if WITH_TIMETABLE
|
2008-01-23 15:00:54 +00:00
|
|
|
TIMETABLE_TIMESTAMP(xmac_timetable, "off waiting");
|
2008-01-24 06:17:51 +00:00
|
|
|
#endif
|
2008-01-23 15:00:54 +00:00
|
|
|
off();
|
2007-11-12 22:29:37 +00:00
|
|
|
}
|
|
|
|
}
|
2008-01-23 15:00:54 +00:00
|
|
|
|
|
|
|
#if WITH_TIMESYNCH
|
|
|
|
#define NUM_SLOTS 8
|
|
|
|
should_be = ((timesynch_rtimer_to_time(RTIMER_TIME(t)) +
|
|
|
|
xmac_config.off_time) &
|
|
|
|
~(xmac_config.off_time + xmac_config.on_time - 1)) +
|
|
|
|
(rimeaddr_node_addr.u8[0] % NUM_SLOTS *
|
|
|
|
((xmac_config.off_time + xmac_config.on_time) / NUM_SLOTS));
|
|
|
|
|
|
|
|
should_be = timesynch_time_to_rtimer(should_be);
|
|
|
|
|
|
|
|
if(should_be - RTIMER_TIME(t) > xmac_config.off_time) {
|
|
|
|
adjust = xmac_config.off_time / 2;
|
|
|
|
} else {
|
|
|
|
adjust = should_be - RTIMER_TIME(t);
|
|
|
|
}
|
|
|
|
r = rtimer_set(t, RTIMER_TIME(t) + adjust, 1,
|
|
|
|
(void (*)(struct rtimer *, void *))powercycle, ptr);
|
|
|
|
#else /* WITH_TIMESYNCH */
|
2007-11-12 22:29:37 +00:00
|
|
|
r = rtimer_set(t, RTIMER_TIME(t) + xmac_config.off_time, 1,
|
|
|
|
(void (*)(struct rtimer *, void *))powercycle, ptr);
|
2008-01-23 15:00:54 +00:00
|
|
|
#endif /* WITH_TIMESYNCH */
|
2007-11-12 22:29:37 +00:00
|
|
|
if(r) {
|
|
|
|
PRINTF("xmac: 1 could not set rtimer %d\n", r);
|
|
|
|
}
|
|
|
|
PT_YIELD(&pt);
|
|
|
|
}
|
2007-05-15 08:07:07 +00:00
|
|
|
|
2008-02-24 21:07:28 +00:00
|
|
|
if(we_are_sending == 0 &&
|
2008-01-23 15:00:54 +00:00
|
|
|
waiting_for_packet == 0) {
|
|
|
|
on();
|
2007-04-07 05:43:31 +00:00
|
|
|
}
|
2007-11-12 22:29:37 +00:00
|
|
|
r = rtimer_set(t, RTIMER_TIME(t) + xmac_config.on_time, 1,
|
2007-05-25 08:06:44 +00:00
|
|
|
(void (*)(struct rtimer *, void *))powercycle, ptr);
|
|
|
|
if(r) {
|
2008-01-23 15:00:54 +00:00
|
|
|
PRINTF("xmac: 3 could not set rtimer %d\n", r);
|
2007-05-15 08:07:07 +00:00
|
|
|
}
|
|
|
|
|
2007-04-07 05:43:31 +00:00
|
|
|
PT_YIELD(&pt);
|
|
|
|
}
|
|
|
|
|
|
|
|
PT_END(&pt);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-05-25 08:06:44 +00:00
|
|
|
static int
|
2007-12-23 14:56:54 +00:00
|
|
|
send_packet(void)
|
2007-04-07 05:43:31 +00:00
|
|
|
{
|
2007-11-12 22:29:37 +00:00
|
|
|
rtimer_clock_t t0;
|
|
|
|
rtimer_clock_t t;
|
2007-04-07 05:43:31 +00:00
|
|
|
int strobes;
|
2007-05-15 08:07:07 +00:00
|
|
|
struct xmac_hdr *hdr;
|
|
|
|
int got_ack = 0;
|
Support for z80(sdcc) port.
In order to support, some core modules are modified as follows:
core/sys/dsc.h
- If CTK_CONF_ICONS is diabled, the whole icon-related code is disabled.
- DSC_HEADER is changed to remove extra semicolon.
core/sys/process.h
- process_data_t is expressed by void* in signatures (known bug on sdcc).
core/sys/autostart.h
- autostart_processes is changed to remove extra semicolon.
core/sys/cc.h
- CC_CONF_ASSIGN_AGGREGATE is introduced.
- CC_CONF_INC_CAST_POINTER is introduced, a workaround of a kind
of sdcc bug for an increment.
core/net/hc.c
core/net/uip_arp.c
core/net/uaodv.c
- Aggregation assignments are changed to uip_ipaddr_copy.
core/net/psock.c
core/net/uipbuf.c
core/net/dhcpc.c
apps/shell/shell.c
core/ctk/vnc-server.c
core/ctk/vnc-out.c
- "register" keyword in a signature cannot be used in sdcc,
CC_REGISTER_ARG is used instead.
core/net/uip-over-mesh.c
- An extra semicolon is removed.
apps/dhcp/dhcp-dsc.c
apps/shell/shell-dsc.
apps/ftp/ftp-dsc.c
apps/process-list/process-list-dsc.c
apps/email/email-dsc.c
apps/webserver/webserver-dsc.c
apps/vnc/vnc-dsc.c
apps/vnc/vnc-viewer.h
apps/webbrowser/www-dsc.c
apps/about/about-dsc.c
apps/irc/irc-dsc.c
apps/telnet/telnet-dsc.c
apps/telnetd/telnetd-dsc.c
apps/netconf/netconf-dsc.c
apps/directory/directory-dsc.c
pps/calc/calc-dsc.c
- Modify an extern type to a real declaration, which is static
to prevent a compile error.
core/net/mac/xmac.c
- Variables cannot be defined in a head of block on sdcc.
core/ctk/ctk.h
core/ctk/ctk.c
apps/program-handler/program-handler.c
- If CTK_CONF_ICONS is diabled, the whole icon-related code is disabled.
Makefile.include
- Add a set of configuration for an assembler.
- $(CLEAN) variable is introduced for customized cleanup.
apps/process-list/process-list.c
- PROCESSLIST_CONF_HEIGHT is introduced to address smaller screen size.
core/lib/ctk-filedialog.c
- FILES_CONF_HEIGHT is introduced to address smaller screen size.
- "register" keyword in a signature cannot be used in sdcc,
CC_REGISTER_ARG is used instead.
apps/vnc/vnc-viewer.c
- A cast is added to prevent a compile error.
- "register" keyword in a signature cannot be used in sdcc,
CC_REGISTER_ARG is used instead.
apps/webbrowser/webclient.c
- CC_CONF_INC_CAST_POINTER is introduced, a workaround of a kind
of sdcc bug for an increment.
core/loader/elfloader.c
- A cast is added to prevent a compile error.
core/net/rime/rimeaddr.c
- An initialization is added to prevent a compile error.
core/net/rime/rudolph0.c
- NULL is changed to 0, because NULL causes a compile error.
core/net/rime/route-discovery.c
- Add an argument to match the definition of nf_callbacks.
cpu/z80/strcasecmp.h
cpu/z80/strcasecmp.c
cpu/z80/contiki-sdcc-conf.h
cpu/z80/mtarch.c
cpu/z80/mtarch.h
cpu/z80/Makefile.z80
- New files to make compilation availble on sdcc.
- Added support for multithreading.
2007-08-30 14:39:16 +00:00
|
|
|
struct xmac_hdr msg;
|
|
|
|
int len;
|
2008-06-30 08:10:02 +00:00
|
|
|
int is_broadcast = 0;
|
2007-04-07 05:43:31 +00:00
|
|
|
|
2008-01-24 06:17:51 +00:00
|
|
|
#if WITH_TIMETABLE
|
2008-01-23 15:00:54 +00:00
|
|
|
TIMETABLE_TIMESTAMP(xmac_timetable, "send");
|
2008-01-24 06:17:51 +00:00
|
|
|
#endif
|
|
|
|
|
2008-01-23 15:00:54 +00:00
|
|
|
#if WITH_CHANNEL_CHECK
|
|
|
|
/* Check if there are other strobes in the air. */
|
|
|
|
waiting_for_packet = 1;
|
|
|
|
on();
|
|
|
|
t0 = RTIMER_NOW();
|
|
|
|
while(RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + xmac_config.strobe_wait_time * 2)) {
|
|
|
|
len = radio->read(&msg, sizeof(msg));
|
|
|
|
if(len > 0) {
|
|
|
|
someone_is_sending = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
waiting_for_packet = 0;
|
|
|
|
|
|
|
|
while(someone_is_sending); /* {printf("z");}*/
|
|
|
|
|
2008-01-24 06:17:51 +00:00
|
|
|
#if WITH_TIMETABLE
|
2008-01-23 15:00:54 +00:00
|
|
|
TIMETABLE_TIMESTAMP(xmac_timetable, "send 2");
|
2008-01-24 06:17:51 +00:00
|
|
|
#endif /* WITH_TIMETABLE */
|
|
|
|
|
2008-01-23 15:00:54 +00:00
|
|
|
#endif /* WITH_CHANNEL_CHECK */
|
|
|
|
|
|
|
|
/* By setting we_are_sending to one, we ensure that the rtimer
|
|
|
|
powercycle interrupt do not interfere with us sending the packet. */
|
2007-05-15 08:07:07 +00:00
|
|
|
we_are_sending = 1;
|
|
|
|
|
2008-01-23 15:00:54 +00:00
|
|
|
off();
|
2007-11-12 22:29:37 +00:00
|
|
|
|
2008-01-23 15:00:54 +00:00
|
|
|
|
2007-04-07 05:43:31 +00:00
|
|
|
rimebuf_hdralloc(sizeof(struct xmac_hdr));
|
2007-05-15 08:07:07 +00:00
|
|
|
hdr = rimebuf_hdrptr();
|
|
|
|
rimeaddr_copy(&hdr->sender, &rimeaddr_node_addr);
|
2008-02-25 02:14:34 +00:00
|
|
|
rimeaddr_copy(&hdr->receiver, rimebuf_addr(RIMEBUF_ADDR_RECEIVER));
|
2008-06-30 08:10:02 +00:00
|
|
|
if(rimeaddr_cmp(&hdr->receiver, &rimeaddr_null)) {
|
|
|
|
is_broadcast = 1;
|
|
|
|
}
|
2007-04-07 05:43:31 +00:00
|
|
|
rimebuf_compact();
|
|
|
|
|
|
|
|
t0 = RTIMER_NOW();
|
|
|
|
strobes = 0;
|
|
|
|
|
2007-11-12 22:29:37 +00:00
|
|
|
BB_SET(XMAC_RECEIVER, hdr->receiver.u16[0]);
|
2008-01-23 15:00:54 +00:00
|
|
|
|
|
|
|
LEDS_ON(LEDS_BLUE);
|
2007-11-12 22:29:37 +00:00
|
|
|
|
2008-01-23 15:00:54 +00:00
|
|
|
/* Send a train of strobes until the receiver answers with an ACK. */
|
2007-04-07 05:43:31 +00:00
|
|
|
|
2008-06-30 08:10:02 +00:00
|
|
|
/* Turn on the radio to listen for the strobe ACK. */
|
|
|
|
if(!is_broadcast) {
|
|
|
|
on();
|
|
|
|
}
|
2008-01-23 15:00:54 +00:00
|
|
|
|
2008-02-24 21:07:28 +00:00
|
|
|
watchdog_stop();
|
2008-01-23 15:00:54 +00:00
|
|
|
got_ack = 0;
|
|
|
|
for(strobes = 0;
|
|
|
|
got_ack == 0 &&
|
|
|
|
RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + xmac_config.strobe_time);
|
|
|
|
strobes++) {
|
2007-11-12 22:29:37 +00:00
|
|
|
|
2007-04-07 05:43:31 +00:00
|
|
|
t = RTIMER_NOW();
|
2007-11-12 22:29:37 +00:00
|
|
|
|
2007-05-15 08:07:07 +00:00
|
|
|
rimeaddr_copy(&msg.sender, &rimeaddr_node_addr);
|
2008-02-25 02:14:34 +00:00
|
|
|
rimeaddr_copy(&msg.receiver, rimebuf_addr(RIMEBUF_ADDR_RECEIVER));
|
2007-04-07 05:43:31 +00:00
|
|
|
|
2008-01-24 06:17:51 +00:00
|
|
|
#if WITH_TIMETABLE
|
2008-02-24 21:07:28 +00:00
|
|
|
if(rimeaddr_cmp(&msg.receiver, &rimeaddr_null)) {
|
|
|
|
TIMETABLE_TIMESTAMP(xmac_timetable, "send broadcast strobe");
|
|
|
|
} else {
|
|
|
|
TIMETABLE_TIMESTAMP(xmac_timetable, "send strobe");
|
|
|
|
}
|
2008-01-24 06:17:51 +00:00
|
|
|
#endif
|
2009-02-07 18:45:50 +00:00
|
|
|
if(0/*is_broadcast*/) {
|
2008-07-07 23:25:12 +00:00
|
|
|
/* If we are sending a broadcast, we don't send strobes, we
|
|
|
|
simply send the data packet repetedly */
|
|
|
|
radio->send(rimebuf_hdrptr(), rimebuf_totlen());
|
|
|
|
} else {
|
|
|
|
/* Send the strobe packet. */
|
|
|
|
radio->send((const uint8_t *)&msg, sizeof(struct xmac_hdr));
|
|
|
|
}
|
2008-01-23 15:00:54 +00:00
|
|
|
CPRINTF("+");
|
2007-05-15 08:07:07 +00:00
|
|
|
|
2008-01-23 15:00:54 +00:00
|
|
|
while(got_ack == 0 &&
|
|
|
|
RTIMER_CLOCK_LT(RTIMER_NOW(), t + xmac_config.strobe_wait_time)) {
|
2007-05-15 08:07:07 +00:00
|
|
|
/* See if we got an ACK */
|
2008-02-24 22:10:30 +00:00
|
|
|
len = radio->read((uint8_t *)&msg, sizeof(struct xmac_hdr));
|
2007-05-15 08:07:07 +00:00
|
|
|
if(len > 0) {
|
2008-01-23 15:00:54 +00:00
|
|
|
CPRINTF("_");
|
2007-11-12 22:29:37 +00:00
|
|
|
if(rimeaddr_cmp(&msg.sender, &rimeaddr_node_addr) &&
|
|
|
|
rimeaddr_cmp(&msg.receiver, &rimeaddr_node_addr)) {
|
2008-01-24 06:17:51 +00:00
|
|
|
#if WITH_TIMETABLE
|
2008-01-23 15:00:54 +00:00
|
|
|
TIMETABLE_TIMESTAMP(xmac_timetable, "send ack received");
|
2008-01-24 06:17:51 +00:00
|
|
|
#endif
|
2008-01-23 15:00:54 +00:00
|
|
|
CPRINTF("@");
|
2007-05-15 08:07:07 +00:00
|
|
|
/* We got an ACK from the receiver, so we can immediately send
|
|
|
|
the packet. */
|
|
|
|
got_ack = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-11-12 22:29:37 +00:00
|
|
|
|
2008-01-23 15:00:54 +00:00
|
|
|
/* XXX: turn off radio if we haven't heard an ACK within a
|
|
|
|
specified time interval. */
|
2007-11-12 22:29:37 +00:00
|
|
|
|
2008-01-23 15:00:54 +00:00
|
|
|
/* if(got_ack == 0) {
|
|
|
|
off();
|
|
|
|
while(RTIMER_CLOCK_LT(RTIMER_NOW(), t + xmac_config.strobe_wait_time));
|
|
|
|
on();
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
|
|
|
|
if(got_ack /* XXX && needs_ack */) {
|
2008-01-24 06:17:51 +00:00
|
|
|
#if WITH_TIMETABLE
|
2008-01-23 15:00:54 +00:00
|
|
|
TIMETABLE_TIMESTAMP(xmac_timetable, "send got ack");
|
2008-01-24 06:17:51 +00:00
|
|
|
#endif
|
2008-01-23 15:00:54 +00:00
|
|
|
on(); /* Wait for possible ACK packet */
|
2008-09-04 17:44:01 +00:00
|
|
|
} else if(!is_broadcast) {
|
2008-02-24 21:07:28 +00:00
|
|
|
#if WITH_TIMETABLE
|
|
|
|
TIMETABLE_TIMESTAMP(xmac_timetable, "send no ack received");
|
|
|
|
#endif
|
2008-01-23 15:00:54 +00:00
|
|
|
on(); /* shell ping don't seem to work with off() here, so we'll
|
|
|
|
keep it on() for a while. */
|
|
|
|
}
|
|
|
|
|
2008-06-30 08:10:02 +00:00
|
|
|
/* Send the data packet. */
|
2008-07-07 23:25:12 +00:00
|
|
|
if(is_broadcast || got_ack) {
|
2008-01-24 06:17:51 +00:00
|
|
|
#if WITH_TIMETABLE
|
2008-01-23 15:00:54 +00:00
|
|
|
TIMETABLE_TIMESTAMP(xmac_timetable, "send packet");
|
2008-01-24 06:17:51 +00:00
|
|
|
#endif
|
2008-01-23 15:00:54 +00:00
|
|
|
radio->send(rimebuf_hdrptr(), rimebuf_totlen());
|
|
|
|
CPRINTF("#");
|
|
|
|
}
|
2008-02-24 21:07:28 +00:00
|
|
|
watchdog_start();
|
2007-05-15 08:07:07 +00:00
|
|
|
|
2007-11-12 22:29:37 +00:00
|
|
|
PRINTF("xmac: send (strobes=%u,len=%u,%s), done\n", strobes,
|
|
|
|
rimebuf_totlen(), got_ack ? "ack" : "no ack");
|
|
|
|
|
|
|
|
BB_SET(XMAC_STROBES, strobes);
|
|
|
|
if(got_ack) {
|
|
|
|
BB_INC(XMAC_SEND_WITH_ACK, 1);
|
|
|
|
} else {
|
|
|
|
BB_INC(XMAC_SEND_WITH_NOACK, 1);
|
|
|
|
}
|
2007-05-15 08:07:07 +00:00
|
|
|
|
|
|
|
/* printf("Strobe %d got_ack %d\n", strobes, got_ack);*/
|
|
|
|
|
2007-11-12 22:29:37 +00:00
|
|
|
we_are_sending = 0;
|
2008-01-24 06:17:51 +00:00
|
|
|
#if WITH_TIMETABLE
|
2008-01-23 15:00:54 +00:00
|
|
|
TIMETABLE_TIMESTAMP(xmac_timetable, "send we_are_sending = 0");
|
2008-01-24 06:17:51 +00:00
|
|
|
#endif
|
2008-01-23 15:00:54 +00:00
|
|
|
LEDS_OFF(LEDS_BLUE);
|
2007-05-25 08:06:44 +00:00
|
|
|
return 1;
|
2007-05-15 08:07:07 +00:00
|
|
|
|
2007-04-07 05:43:31 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-05-22 20:55:24 +00:00
|
|
|
static struct queuebuf *queued_packet;
|
2007-05-25 08:06:44 +00:00
|
|
|
static int
|
2007-12-23 14:56:54 +00:00
|
|
|
qsend_packet(void)
|
2007-04-07 05:43:31 +00:00
|
|
|
{
|
2007-05-15 08:07:07 +00:00
|
|
|
if(someone_is_sending) {
|
|
|
|
PRINTF("xmac: should queue packet, now just dropping %d %d %d %d.\n",
|
2008-01-23 15:00:54 +00:00
|
|
|
waiting_for_packet, someone_is_sending, we_are_sending, radio_is_on);
|
2007-05-22 20:55:24 +00:00
|
|
|
if(queued_packet != NULL) {
|
2007-11-12 22:29:37 +00:00
|
|
|
RIMESTATS_ADD(sendingdrop);
|
2007-05-25 08:06:44 +00:00
|
|
|
return 0;
|
2007-05-22 20:55:24 +00:00
|
|
|
} else {
|
|
|
|
#if WITH_QUEUE
|
|
|
|
queued_packet = queuebuf_new_from_rimebuf();
|
2007-05-25 08:06:44 +00:00
|
|
|
return 1;
|
2007-05-22 20:55:24 +00:00
|
|
|
#else
|
2007-11-12 22:29:37 +00:00
|
|
|
RIMESTATS_ADD(sendingdrop);
|
2007-05-25 08:06:44 +00:00
|
|
|
return 0;
|
2007-05-22 20:55:24 +00:00
|
|
|
#endif
|
|
|
|
}
|
2007-05-15 08:07:07 +00:00
|
|
|
} else {
|
|
|
|
PRINTF("xmac: send immediately.\n");
|
2007-12-23 14:56:54 +00:00
|
|
|
return send_packet();
|
2007-05-15 08:07:07 +00:00
|
|
|
}
|
2007-11-12 22:29:37 +00:00
|
|
|
|
2007-04-07 05:43:31 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-05-15 08:07:07 +00:00
|
|
|
static void
|
2007-12-23 14:56:54 +00:00
|
|
|
input_packet(const struct radio_driver *d)
|
2007-05-25 08:06:44 +00:00
|
|
|
{
|
|
|
|
if(receiver_callback) {
|
|
|
|
receiver_callback(&xmac_driver);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2007-12-23 14:56:54 +00:00
|
|
|
read_packet(void)
|
2007-04-07 05:43:31 +00:00
|
|
|
{
|
2007-05-15 08:07:07 +00:00
|
|
|
struct xmac_hdr *hdr;
|
2008-02-24 22:10:30 +00:00
|
|
|
uint8_t len;
|
2007-11-12 22:29:37 +00:00
|
|
|
|
2007-05-15 08:07:07 +00:00
|
|
|
rimebuf_clear();
|
2007-04-07 05:43:31 +00:00
|
|
|
|
2007-05-25 08:06:44 +00:00
|
|
|
len = radio->read(rimebuf_dataptr(), RIMEBUF_SIZE);
|
2007-11-12 22:29:37 +00:00
|
|
|
|
2007-05-15 08:07:07 +00:00
|
|
|
if(len > 0) {
|
|
|
|
rimebuf_set_datalen(len);
|
|
|
|
hdr = rimebuf_dataptr();
|
2007-11-12 22:29:37 +00:00
|
|
|
|
2007-05-15 08:07:07 +00:00
|
|
|
rimebuf_hdrreduce(sizeof(struct xmac_hdr));
|
2008-01-23 15:00:54 +00:00
|
|
|
|
|
|
|
if(rimebuf_totlen() == 0) {
|
|
|
|
CPRINTF(".");
|
|
|
|
/* There is no data in the packet so it has to be a strobe. */
|
|
|
|
someone_is_sending = 2;
|
|
|
|
|
|
|
|
if(rimeaddr_cmp(&hdr->receiver, &rimeaddr_node_addr)) {
|
|
|
|
/* This is a strobe packet for us. */
|
|
|
|
|
|
|
|
if(rimeaddr_cmp(&hdr->sender, &rimeaddr_node_addr)) {
|
|
|
|
/* If the sender address is our node address, the strobe is
|
|
|
|
a stray strobe ACK to us, which we ignore unless we are
|
|
|
|
currently sending a packet. */
|
|
|
|
CPRINTF("&");
|
|
|
|
someone_is_sending = 0;
|
|
|
|
} else {
|
2007-05-15 08:07:07 +00:00
|
|
|
struct xmac_hdr msg;
|
2008-01-23 15:00:54 +00:00
|
|
|
/* If the sender address is someone else, we should
|
|
|
|
acknowledge the strobe and wait for the packet. By using
|
|
|
|
the same address as both sender and receiver, we flag the
|
|
|
|
message is a strobe ack. */
|
2008-01-24 06:17:51 +00:00
|
|
|
#if WITH_TIMETABLE
|
2008-01-23 15:00:54 +00:00
|
|
|
TIMETABLE_TIMESTAMP(xmac_timetable, "read send ack");
|
2008-01-24 06:17:51 +00:00
|
|
|
#endif
|
2007-05-15 08:07:07 +00:00
|
|
|
rimeaddr_copy(&msg.receiver, &hdr->sender);
|
|
|
|
rimeaddr_copy(&msg.sender, &hdr->sender);
|
2008-01-23 15:00:54 +00:00
|
|
|
CPRINTF("!");
|
|
|
|
/* We turn on the radio in anticipation of the incoming
|
|
|
|
packet. */
|
|
|
|
someone_is_sending = 1;
|
|
|
|
waiting_for_packet = 1;
|
|
|
|
on();
|
2008-02-24 22:10:30 +00:00
|
|
|
radio->send((const uint8_t *)&msg, sizeof(struct xmac_hdr));
|
2007-05-15 08:07:07 +00:00
|
|
|
}
|
2008-01-23 15:00:54 +00:00
|
|
|
} else if(rimeaddr_cmp(&hdr->receiver, &rimeaddr_null)) {
|
|
|
|
/* If the receiver address is null, the strobe is sent to
|
|
|
|
prepare for an incoming broadcast packet. If this is the
|
|
|
|
case, we turn on the radio and wait for the incoming
|
|
|
|
broadcast packet. */
|
|
|
|
waiting_for_packet = 1;
|
|
|
|
on();
|
2007-05-15 08:07:07 +00:00
|
|
|
}
|
2008-01-23 15:00:54 +00:00
|
|
|
/* We are done processing the strobe and we therefore return
|
|
|
|
to the caller. */
|
|
|
|
return RIME_OK;
|
2007-05-15 08:07:07 +00:00
|
|
|
} else {
|
2008-01-23 15:00:54 +00:00
|
|
|
CPRINTF("-");
|
2007-05-15 08:07:07 +00:00
|
|
|
someone_is_sending = 0;
|
2008-01-23 15:00:54 +00:00
|
|
|
if(rimeaddr_cmp(&hdr->receiver, &rimeaddr_node_addr) ||
|
|
|
|
rimeaddr_cmp(&hdr->receiver, &rimeaddr_null)) {
|
2008-01-24 06:17:51 +00:00
|
|
|
#if WITH_TIMETABLE
|
2008-01-23 15:00:54 +00:00
|
|
|
TIMETABLE_TIMESTAMP(xmac_timetable, "read got packet");
|
2008-01-24 06:17:51 +00:00
|
|
|
#endif
|
2008-01-23 15:00:54 +00:00
|
|
|
/* This is a regular packet that is destined to us or to the
|
|
|
|
broadcast address. */
|
|
|
|
|
|
|
|
/* We have received the final packet, so we can go back to being
|
|
|
|
asleep. */
|
|
|
|
off();
|
|
|
|
waiting_for_packet = 0;
|
|
|
|
|
|
|
|
/* XXX should set timer to send queued packet later. */
|
|
|
|
if(queued_packet != NULL) {
|
|
|
|
queuebuf_free(queued_packet);
|
|
|
|
queued_packet = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rimebuf_totlen();
|
2007-05-22 20:55:24 +00:00
|
|
|
}
|
2007-05-15 08:07:07 +00:00
|
|
|
}
|
2007-04-07 05:43:31 +00:00
|
|
|
}
|
2007-05-25 08:06:44 +00:00
|
|
|
return 0;
|
2007-04-07 05:43:31 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-11-12 22:29:37 +00:00
|
|
|
const struct mac_driver *
|
|
|
|
xmac_init(const struct radio_driver *d)
|
|
|
|
{
|
2008-01-24 06:17:51 +00:00
|
|
|
#if WITH_TIMETABLE
|
2008-01-23 15:00:54 +00:00
|
|
|
timetable_clear(&xmac_timetable);
|
2008-01-24 06:17:51 +00:00
|
|
|
#endif
|
2007-11-12 22:29:37 +00:00
|
|
|
radio_is_on = 0;
|
2008-01-23 15:00:54 +00:00
|
|
|
waiting_for_packet = 0;
|
2007-11-12 22:29:37 +00:00
|
|
|
PT_INIT(&pt);
|
|
|
|
rtimer_set(&rt, RTIMER_NOW() + xmac_config.off_time, 1,
|
|
|
|
(void (*)(struct rtimer *, void *))powercycle, NULL);
|
|
|
|
|
2007-12-05 13:23:17 +00:00
|
|
|
xmac_is_on = 1;
|
2007-11-12 22:29:37 +00:00
|
|
|
radio = d;
|
2007-12-23 14:56:54 +00:00
|
|
|
radio->set_receive_function(input_packet);
|
2007-11-12 22:29:37 +00:00
|
|
|
|
2008-01-23 15:00:54 +00:00
|
|
|
BB_SET("xmac.state_addr", (int) &waiting_for_packet);
|
2007-11-12 22:29:37 +00:00
|
|
|
BB_SET(XMAC_RECEIVER, 0);
|
|
|
|
BB_SET(XMAC_STROBES, 0);
|
|
|
|
BB_SET(XMAC_SEND_WITH_ACK, 0);
|
|
|
|
BB_SET(XMAC_SEND_WITH_NOACK, 0);
|
|
|
|
return &xmac_driver;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-05-25 08:06:44 +00:00
|
|
|
static int
|
2008-01-23 15:00:54 +00:00
|
|
|
turn_on(void)
|
2007-05-25 08:06:44 +00:00
|
|
|
{
|
2007-12-05 13:23:17 +00:00
|
|
|
xmac_is_on = 1;
|
|
|
|
return 1;
|
2007-05-25 08:06:44 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2008-02-24 21:07:28 +00:00
|
|
|
turn_off(int keep_radio_on)
|
2007-05-25 08:06:44 +00:00
|
|
|
{
|
2007-12-05 13:23:17 +00:00
|
|
|
xmac_is_on = 0;
|
2008-02-24 21:07:28 +00:00
|
|
|
if(keep_radio_on) {
|
|
|
|
return radio->on();
|
|
|
|
} else {
|
|
|
|
return radio->off();
|
|
|
|
}
|
2007-05-25 08:06:44 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
const struct mac_driver xmac_driver =
|
|
|
|
{
|
2008-06-30 08:10:02 +00:00
|
|
|
"X-MAC",
|
2007-12-23 14:56:54 +00:00
|
|
|
qsend_packet,
|
|
|
|
read_packet,
|
2007-05-25 08:06:44 +00:00
|
|
|
set_receive_function,
|
2008-01-23 15:00:54 +00:00
|
|
|
turn_on,
|
|
|
|
turn_off
|
2007-05-25 08:06:44 +00:00
|
|
|
};
|