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

📄 amstandardtinysec.nc

📁 传感器网络中的嵌入式操作系统源代码
💻 NC
字号:
// $Id: AMStandardTinySec.nc,v 1.1.2.3 2003/08/26 09:08:14 cssharp 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. *//* * * Authors:		Jason Hill, David Gay, Philip Levis, Chris Karlof * Date last modified:  8/1/03 * *///This is an AM messaging layer implementation that understands multiple// output devices.  All packets addressed to TOS_UART_ADDR are sent to the UART// instead of the radio./** * @author Jason Hill * @author David Gay * @author Philip Levis * @author Chris Karlof */module AMStandardTinySec{  provides {    interface StdControl as Control;        // The interface are as parameterised by the active message id    interface SendMsg[uint8_t id];    interface SendMsg as SendMsgEncryptAndAuth[uint8_t id];    interface SendMsg as SendMsgCRC[uint8_t id];    interface ReceiveMsg[uint8_t id];    interface ReceiveMsg as ReceiveMsgNoAuth[uint8_t id];    // How many packets were received in the past second    command uint16_t activity();  }  uses {    // signaled after every send completion for components which wish to    // retry failed sends    event result_t sendDone();    interface StdControl as UARTControl;    interface BareSendMsg as UARTSend;    interface ReceiveMsg as UARTReceive;    interface StdControl as RadioControl;    interface BareSendMsg as RadioSend;    interface ReceiveMsg as RadioReceive;    interface StdControl as TimerControl;    interface Timer as ActivityTimer;    interface PowerManagement;  }}implementation{  bool state;  TOS_MsgPtr buffer;  uint16_t lastCount;  uint16_t counter;    // Initialization of this component  command bool Control.init() {    result_t ok1, ok2;    atomic {      call TimerControl.init();      ok1 = call UARTControl.init();      ok2 = call RadioControl.init();      state = FALSE;      lastCount = 0;      counter = 0;      dbg(DBG_BOOT, "AM Module initialized\n");    }    return rcombine(ok1, ok2);  }  // Command to be used for power managment  command bool Control.start() {    result_t ok0 = call TimerControl.start();    result_t ok1 = call UARTControl.start();    result_t ok2 = call RadioControl.start();    result_t ok3 = call ActivityTimer.start(TIMER_REPEAT, 1000);    //HACK -- unset start here to work around possible lost calls to     // sendDone which seem to occur when using power management.  SRM 4.4.03    atomic {      state = FALSE;    }    call PowerManagement.adjustPower();    return rcombine4(ok0, ok1, ok2, ok3);  }    command bool Control.stop() {    result_t ok1 = call UARTControl.stop();    result_t ok2 = call RadioControl.stop();    result_t ok3 = call ActivityTimer.stop();    // call TimerControl.stop();    call PowerManagement.adjustPower();    return rcombine3(ok1, ok2, ok3);  }  command uint16_t activity() {    return lastCount;  }    void dbgPacket(TOS_MsgPtr data) {    uint8_t i;    for(i = 0; i < sizeof(TOS_Msg); i++)      {	dbg_clear(DBG_AM, "%02hhx ", ((uint8_t *)data)[i]);      }    dbg_clear(DBG_AM, "\n");  }  // Handle the event of the completion of a message transmission  result_t reportSendDone(TOS_MsgPtr msg, result_t success) {    atomic {      state = FALSE;    }    if(msg->sendSecurityMode == TINYSEC_ENCRYPT_AND_AUTH) {      signal SendMsgEncryptAndAuth.sendDone[msg->type](msg, success);    } else if(msg->sendSecurityMode == TINYSEC_AUTH_ONLY) {      signal SendMsg.sendDone[msg->type](msg, success);    } else if(msg->sendSecurityMode == TINYSEC_DISABLED) {      signal SendMsgCRC.sendDone[msg->type](msg, success);    } else      return FAIL;    signal sendDone();    return SUCCESS;  }  event result_t ActivityTimer.fired() {    atomic {      lastCount = counter;      counter = 0;    }    return SUCCESS;  }    default event result_t SendMsg.sendDone[uint8_t id](TOS_MsgPtr msg, result_t success) {    return SUCCESS;  }  default event result_t SendMsgEncryptAndAuth.sendDone[uint8_t id](TOS_MsgPtr msg, result_t success) {    return SUCCESS;  }  default event result_t SendMsgCRC.sendDone[uint8_t id](TOS_MsgPtr msg, result_t success) {    return SUCCESS;  }  default event result_t sendDone() {    return SUCCESS;  }  // This task schedules the transmission of the Active Message  task void sendTask() {    result_t ok;    TOS_MsgPtr buf;    atomic {      buf = buffer;    }    if (buf->addr == TOS_UART_ADDR)      ok = call UARTSend.send(buf);    else      ok = call RadioSend.send(buf);    if (ok == FAIL) // failed, signal completion immediately      reportSendDone(buffer, FAIL);  }  // Command to accept transmission of an Active Message  command result_t SendMsg.send[uint8_t id](uint16_t addr, uint8_t length, TOS_MsgPtr data) {    bool oldState;    atomic {      oldState = state;      state = TRUE;    }        if (!oldState) {      if (length > DATA_LENGTH) {	dbg(DBG_AM, "AM: Send length too long: %i. Fail.\n", (int)length);	atomic {	  state = FALSE;	}	return FAIL;      }      if (!(post sendTask())) {	dbg(DBG_AM, "AM: post sendTask failed.\n");	atomic {	  state = FALSE;	}	return FAIL;      }      else {	atomic {	  buffer = data;	  data->length = length;	  data->addr = addr;	  data->type = id;	  buffer->group = TOS_AM_GROUP;	  buffer->sendSecurityMode = TINYSEC_AUTH_ONLY;	  dbg(DBG_AM, "Sending message: %hx, %hhx\n\t", addr, id);	  dbgPacket(data);	}      }      return SUCCESS;    }        return FAIL;  }  // Command to accept transmission of an Active Message  command result_t SendMsgEncryptAndAuth.send[uint8_t id](uint16_t addr, uint8_t length, TOS_MsgPtr data) {    bool oldState;    atomic {      oldState = state;      state = TRUE;    }        if (!oldState) {      if (length > DATA_LENGTH) {	dbg(DBG_AM, "AM: Send length too long: %i. Fail.\n", (int)length);	atomic {	  state = FALSE;	}	return FAIL;      }      if (!(post sendTask())) {	dbg(DBG_AM, "AM: post sendTask failed.\n");	atomic {	  state = FALSE;	}	return FAIL;      }      else {	atomic {	  buffer = data;	  data->length = length;	  data->addr = addr;	  data->type = id;	  buffer->group = TOS_AM_GROUP;	  buffer->sendSecurityMode = TINYSEC_ENCRYPT_AND_AUTH;	  dbg(DBG_AM, "Sending message: %hx, %hhx\n\t", addr, id);	  dbgPacket(data);	}      }      return SUCCESS;    }        return FAIL;  }  // Command to accept transmission of an Active Message  command result_t SendMsgCRC.send[uint8_t id](uint16_t addr, uint8_t length, TOS_MsgPtr data) {    bool oldState;    atomic {      oldState = state;      state = TRUE;    }        if (!oldState) {      if (length > DATA_LENGTH) {	dbg(DBG_AM, "AM: Send length too long: %i. Fail.\n", (int)length);	atomic {	  state = FALSE;	}	return FAIL;      }      if (!(post sendTask())) {	dbg(DBG_AM, "AM: post sendTask failed.\n");	atomic {	  state = FALSE;	}	return FAIL;      }      else {	atomic {	  buffer = data;	  data->length = length;	  data->addr = addr;	  data->type = id;	  buffer->group = TOS_AM_GROUP;	  buffer->sendSecurityMode = TINYSEC_DISABLED;	  dbg(DBG_AM, "Sending message: %hx, %hhx\n\t", addr, id);	  dbgPacket(data);	}      }      return SUCCESS;    }        return FAIL;  }    event result_t UARTSend.sendDone(TOS_MsgPtr msg, result_t success) {    return reportSendDone(msg, success);  }  event result_t RadioSend.sendDone(TOS_MsgPtr msg, result_t success) {    return reportSendDone(msg, success);  }  // Handle the event of the reception of an incoming message  TOS_MsgPtr received(TOS_MsgPtr packet)  __attribute__ ((C, spontaneous)) {    uint16_t addr = TOS_LOCAL_ADDRESS;    atomic {      counter++;    }    dbg(DBG_AM, "AM_address = %hx, %hhx; counter:%i\n", packet->addr, packet->type, (int)counter);    if (packet->crc == 1 && // Uncomment this line to check crcs	packet->group == TOS_AM_GROUP &&	(packet->addr == TOS_BCAST_ADDR ||	 packet->addr == addr))      {	uint8_t type = packet->type;	TOS_MsgPtr tmp;	// Debugging output	dbg(DBG_AM, "Received message:\n\t");	dbgPacket(packet);	dbg(DBG_AM, "AM_type = %d\n", type);	// dispatch message	if(packet->receiveSecurityMode == TINYSEC_AUTH_ONLY ||	   packet->receiveSecurityMode == TINYSEC_ENCRYPT_AND_AUTH)	  tmp = signal ReceiveMsg.receive[type](packet);	else	  tmp = signal ReceiveMsgNoAuth.receive[type](packet);	if (tmp) 	  packet = tmp;      }     return packet;  }  // default do-nothing message receive handler  default event TOS_MsgPtr ReceiveMsg.receive[uint8_t id](TOS_MsgPtr msg) {    return msg;  }  // default do-nothing message receive handler  default event TOS_MsgPtr ReceiveMsgNoAuth.receive[uint8_t id](TOS_MsgPtr msg) {    return msg;  }  event TOS_MsgPtr UARTReceive.receive(TOS_MsgPtr packet) {    return received(packet);  }  event TOS_MsgPtr RadioReceive.receive(TOS_MsgPtr packet) {    return received(packet);  }}

⌨️ 快捷键说明

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