📄 tosbasem.nc
字号:
// $Id: TOSBaseM.nc,v 1.9 2005/07/14 17:48:56 gtolle Exp $/* tab:4 * "Copyright (c) 2000-2003 The Regents of the University of California. * All rights reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written agreement is * hereby granted, provided that the above copyright notice, the following * two paragraphs and the author appear in all copies of this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." * * Copyright (c) 2002-2003 Intel Corporation * All rights reserved. * * This file is distributed under the terms in the attached INTEL-LICENSE * file. If you do not find these files, copies can be found by writing to * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, * 94704. Attention: Intel License Inquiry. *//* * @author Phil Buonadonna * @author Gilman Tolle * Revision: $Id: TOSBaseM.nc,v 1.9 2005/07/14 17:48:56 gtolle Exp $ */ /* * TOSBaseM bridges packets between a serial channel and the radio. * Messages moving from serial to radio will be tagged with the group * ID compiled into the TOSBase, and messages moving from radio to * serial will be filtered by that same group id. */#ifndef TOSBASE_BLINK_ON_DROP#define TOSBASE_BLINK_ON_DROP#endifmodule TOSBaseM { provides interface StdControl; uses { interface StdControl as UARTControl; interface BareSendMsg as UARTSend; interface ReceiveMsg as UARTReceive; interface TokenReceiveMsg as UARTTokenReceive; interface StdControl as RadioControl; interface BareSendMsg as RadioSend; interface ReceiveMsg as RadioReceive; interface Leds; }}implementation{ enum { UART_QUEUE_LEN = 12, RADIO_QUEUE_LEN = 12, }; TOS_Msg uartQueueBufs[UART_QUEUE_LEN]; uint8_t uartIn, uartOut; bool uartBusy, uartCount; TOS_Msg radioQueueBufs[RADIO_QUEUE_LEN]; uint8_t radioIn, radioOut; bool radioBusy, radioCount; task void UARTSendTask(); task void RadioSendTask(); void failBlink(); void dropBlink(); void processUartPacket(TOS_MsgPtr Msg, bool wantsAck, uint8_t Token); command result_t StdControl.init() { result_t ok1, ok2, ok3; uartIn = uartOut = uartCount = 0; uartBusy = FALSE; radioIn = radioOut = radioCount = 0; radioBusy = FALSE; ok1 = call UARTControl.init(); ok2 = call RadioControl.init(); ok3 = call Leds.init(); dbg(DBG_BOOT, "TOSBase initialized\n"); return rcombine3(ok1, ok2, ok3); } command result_t StdControl.start() { result_t ok1, ok2; ok1 = call UARTControl.start(); ok2 = call RadioControl.start(); return rcombine(ok1, ok2); } command result_t StdControl.stop() { result_t ok1, ok2; ok1 = call UARTControl.stop(); ok2 = call RadioControl.stop(); return rcombine(ok1, ok2); } event TOS_MsgPtr RadioReceive.receive(TOS_MsgPtr Msg) { dbg(DBG_USR1, "TOSBase received radio packet.\n"); if ((!Msg->crc) || (Msg->group != TOS_AM_GROUP)) return Msg; if (uartCount < UART_QUEUE_LEN) { memcpy(&uartQueueBufs[uartIn], Msg, sizeof(TOS_Msg)); uartCount++; if( ++uartIn >= UART_QUEUE_LEN ) uartIn = 0; if (!uartBusy) { if (post UARTSendTask()) { uartBusy = TRUE; } } } else { dropBlink(); } return Msg; } task void UARTSendTask() { dbg (DBG_USR1, "TOSBase forwarding Radio packet to UART\n"); if (uartCount == 0) { uartBusy = FALSE; } else { if (call UARTSend.send(&uartQueueBufs[uartOut]) == SUCCESS) { call Leds.greenToggle(); } else { failBlink(); post UARTSendTask(); } } } event result_t UARTSend.sendDone(TOS_MsgPtr msg, result_t success) { if (!success) { failBlink(); } else { uartCount--; if( ++uartOut >= UART_QUEUE_LEN ) uartOut = 0; } post UARTSendTask(); return SUCCESS; } event TOS_MsgPtr UARTReceive.receive(TOS_MsgPtr Msg) { processUartPacket(Msg, FALSE, 0); return Msg; } event TOS_MsgPtr UARTTokenReceive.receive(TOS_MsgPtr Msg, uint8_t Token) { processUartPacket(Msg, TRUE, Token); return Msg; } void processUartPacket(TOS_MsgPtr Msg, bool wantsAck, uint8_t Token) { bool reflectToken = FALSE; dbg(DBG_USR1, "TOSBase received UART token packet.\n"); if (radioCount < RADIO_QUEUE_LEN) { reflectToken = TRUE; memcpy(&radioQueueBufs[radioIn], Msg, sizeof(TOS_Msg)); radioCount++; if( ++radioIn >= RADIO_QUEUE_LEN ) radioIn = 0; if (!radioBusy) { if (post RadioSendTask()) { radioBusy = TRUE; } } } else { dropBlink(); } if (wantsAck && reflectToken) { call UARTTokenReceive.ReflectToken(Token); } } task void RadioSendTask() { dbg(DBG_USR1, "TOSBase forwarding UART packet to Radio\n"); if (radioCount == 0) { radioBusy = FALSE; } else { radioQueueBufs[radioOut].group = TOS_AM_GROUP; if (call RadioSend.send(&radioQueueBufs[radioOut]) == SUCCESS) { call Leds.redToggle(); } else { failBlink(); post RadioSendTask(); } } } event result_t RadioSend.sendDone(TOS_MsgPtr msg, result_t success) { if (!success) { failBlink(); } else { radioCount--; if( ++radioOut >= RADIO_QUEUE_LEN ) radioOut = 0; } post RadioSendTask(); return SUCCESS; } void dropBlink() {#ifdef TOSBASE_BLINK_ON_DROP call Leds.yellowToggle();#endif } void failBlink() {#ifdef TOSBASE_BLINK_ON_FAIL call Leds.yellowToggle();#endif }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -