⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmac.c

📁 Contiki是一个开源
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * 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. * * $Id: xmac.c,v 1.21 2008/09/04 17:44:01 adamdunkels Exp $ *//** * \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"#include "net/rime/timesynch.h"#include "dev/radio.h"#include "dev/watchdog.h"#include "lib/random.h"/*#include "lib/bb.h"*/#include "sys/timetable.h"#include "contiki-conf.h"#if CHAMELEON#include "net/chameleon/packattr.h"#endif#define WITH_TIMETABLE 0#define WITH_CHANNEL_CHECK 0   /* Seems to work badly when enabled */#define WITH_TIMESYNCH 0#define WITH_QUEUE 0struct xmac_hdr {  rimeaddr_t sender;  rimeaddr_t receiver;};#ifdef XMAC_CONF_ON_TIME#define DEFAULT_ON_TIME (XMAC_CONF_ON_TIME)#else#define DEFAULT_ON_TIME (RTIMER_ARCH_SECOND / 200)#endif#ifdef XMAC_CONF_OFF_TIME#define DEFAULT_OFF_TIME (XMAC_CONF_OFF_TIME)#else#define DEFAULT_OFF_TIME (RTIMER_ARCH_SECOND / 2 - DEFAULT_ON_TIME)#endif#define DEFAULT_STROBE_WAIT_TIME (7 * DEFAULT_ON_TIME / 8)struct xmac_config xmac_config = {  DEFAULT_ON_TIME,  DEFAULT_OFF_TIME,  20 * DEFAULT_ON_TIME + DEFAULT_OFF_TIME,  DEFAULT_STROBE_WAIT_TIME};#include <stdio.h>static struct rtimer rt;static struct pt pt;static int xmac_is_on = 0;static volatile unsigned char waiting_for_packet = 0;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#define CPRINTF(...)#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) */#define LEDS_ON(x) leds_on(x)#define LEDS_OFF(x) leds_off(x)#define LEDS_TOGGLE(x) leds_toggle(x)#define DEBUG 0#if DEBUG#include <stdio.h>#define PRINTF(...) printf(__VA_ARGS__)#else#undef LEDS_ON#undef LEDS_OFF#undef LEDS_TOGGLE#define LEDS_ON(x)#define LEDS_OFF(x)#define LEDS_TOGGLE(x)#define PRINTF(...)#endifstatic void (* receiver_callback)(const struct mac_driver *);#if WITH_TIMETABLE#define xmac_timetable_size 1024TIMETABLE_NONSTATIC(xmac_timetable);#endif /* WITH_TIMETABLE *//*---------------------------------------------------------------------------*/static voidset_receive_function(void (* recv)(const struct mac_driver *)){  receiver_callback = recv;}/*---------------------------------------------------------------------------*/static voidon(void){  if(xmac_is_on && radio_is_on == 0) {    radio_is_on = 1;    radio->on();    LEDS_ON(LEDS_RED);    CPRINTF("/");#if WITH_TIMETABLE    TIMETABLE_TIMESTAMP(xmac_timetable, "on");#endif  }}/*---------------------------------------------------------------------------*/static voidoff(void){  if(xmac_is_on && radio_is_on != 0) {    radio_is_on = 0;    radio->off();#if WITH_TIMETABLE    TIMETABLE_TIMESTAMP(xmac_timetable, "off");#endif    LEDS_OFF(LEDS_RED);    CPRINTF("\\");  }}/*---------------------------------------------------------------------------*/static charpowercycle(struct rtimer *t, void *ptr){  int r;#if WITH_TIMESYNCH  rtimer_clock_t should_be, adjust;#endif /* WITH_TIMESYNCH */  CPRINTF("*");    PT_BEGIN(&pt);  while(1) {    /* Only wait for some cycles to pass for someone to start sending */    if(someone_is_sending > 0) {      someone_is_sending--;    }    if(xmac_config.off_time > 0) {      if(waiting_for_packet == 0) {	if(we_are_sending == 0) {	  off();	}      } else {	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;#if WITH_TIMETABLE	  TIMETABLE_TIMESTAMP(xmac_timetable, "off waiting");#endif	  off();	}      }#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 */      r = rtimer_set(t, RTIMER_TIME(t) + xmac_config.off_time, 1,		     (void (*)(struct rtimer *, void *))powercycle, ptr);#endif /* WITH_TIMESYNCH */      if(r) {	PRINTF("xmac: 1 could not set rtimer %d\n", r);      }      PT_YIELD(&pt);    }    if(we_are_sending == 0 &&       waiting_for_packet == 0) {      on();    }    r = rtimer_set(t, RTIMER_TIME(t) + xmac_config.on_time, 1,		   (void (*)(struct rtimer *, void *))powercycle, ptr);    if(r) {      PRINTF("xmac: 3 could not set rtimer %d\n", r);    }    PT_YIELD(&pt);  }  PT_END(&pt);}/*---------------------------------------------------------------------------*/static intsend_packet(void){  rtimer_clock_t t0;  rtimer_clock_t t;  int strobes;  struct xmac_hdr *hdr;  int got_ack = 0;  struct xmac_hdr msg;  int len;  int is_broadcast = 0;#if WITH_TIMETABLE  TIMETABLE_TIMESTAMP(xmac_timetable, "send");#endif  #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");}*/#if WITH_TIMETABLE  TIMETABLE_TIMESTAMP(xmac_timetable, "send 2");#endif /* WITH_TIMETABLE */  #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. */  we_are_sending = 1;  off();    rimebuf_hdralloc(sizeof(struct xmac_hdr));  hdr = rimebuf_hdrptr();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -