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

📄 beacontransmitp.nc

📁 tinyos-2.x.rar
💻 NC
📖 第 1 页 / 共 3 页
字号:
/*
 * Copyright (c) 2008, Technische Universitaet Berlin
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met:
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - 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.
 * - Neither the name of the Technische Universitaet Berlin 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 COPYRIGHT HOLDERS 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 COPYRIGHT 
 * OWNER 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.
 *
 * - Revision -------------------------------------------------------------
 * $Revision: 1.10 $
 * $Date: 2009/05/28 09:52:54 $
 * @author Jan Hauer <hauer@tkn.tu-berlin.de>
 * ========================================================================
 */

/** 
 * This module is responsible for periodic beacon transmission in a 
 * beacon-enabled PAN.
 */

#include "TKN154_MAC.h"
#include "TKN154_PHY.h"
module BeaconTransmitP
{
  provides
  {
    interface Init as Reset;
    interface MLME_START;
    interface IEEE154TxBeaconPayload;
    interface SuperframeStructure as OutgoingSF;
    interface GetNow<bool> as IsSendingBeacons;
  } uses {
    interface Notify<bool> as GtsSpecUpdated;
    interface Notify<bool> as PendingAddrSpecUpdated;
    interface Notify<const void*> as PIBUpdate[uint8_t attributeID];
    interface Alarm<TSymbolIEEE802154,uint32_t> as BeaconSendAlarm;
    interface Timer<TSymbolIEEE802154> as BeaconPayloadUpdateTimer;
    interface RadioOff;
    interface RadioTx as BeaconTx;
    interface MLME_GET;
    interface MLME_SET;
    interface TransferableResource as RadioToken;
    interface FrameTx as RealignmentBeaconEnabledTx;
    interface FrameTx as RealignmentNonBeaconEnabledTx;
    interface FrameRx as BeaconRequestRx;
    interface WriteBeaconField as GtsInfoWrite;
    interface WriteBeaconField as PendingAddrWrite;
    interface FrameUtility;
    interface GetNow<bool> as IsTrackingBeacons;
    interface SuperframeStructure as IncomingSF;
    interface Set<ieee154_macSuperframeOrder_t> as SetMacSuperframeOrder;
    interface Set<ieee154_macBeaconTxTime_t> as SetMacBeaconTxTime;
    interface Set<ieee154_macPanCoordinator_t> as SetMacPanCoordinator;
    interface GetSet<ieee154_txframe_t*> as GetSetRealignmentFrame;
    interface GetNow<bool> as IsBroadcastReady; 
    interface TimeCalc;
    interface Leds;
  }
}
implementation
{
  /* state variables */
  norace uint8_t m_requestBitmap;
  norace uint8_t m_txState;
  uint8_t m_payloadState;
  norace bool m_txOneBeaconImmediately;

  /* variables that describe the current superframe configuration */
  norace uint32_t m_startTime;
  norace uint8_t m_beaconOrder;
  norace uint8_t m_superframeOrder;
  norace uint32_t m_beaconInterval;
  norace uint32_t m_previousBeaconInterval;
  norace uint32_t m_dt;
  norace uint8_t m_bsn;
  norace uint32_t m_lastBeaconTxTime;
  norace ieee154_timestamp_t m_lastBeaconTxRefTime;
  norace ieee154_macBattLifeExtPeriods_t m_battLifeExtPeriods;

  /* variables that describe the latest superframe */
  norace uint32_t m_sfSlotDuration;
  norace bool m_framePendingBit;
  norace uint8_t m_numCapSlots;
  norace uint8_t m_numGtsSlots;
  norace uint16_t m_battLifeExtDuration;
  uint8_t m_gtsField[1+1+3*7];

  /* variables that describe the beacon (payload) */
  norace ieee154_txframe_t m_beaconFrame;
  ieee154_header_t m_header; 
  ieee154_metadata_t m_metadata; 
  void *m_updateBeaconPayload;
  uint8_t m_updateBeaconOffset;
  uint8_t m_updateBeaconLength;
  uint8_t m_beaconPayloadLen;
  uint8_t m_pendingAddrLen;
  uint8_t m_pendingGtsLen;

  /* buffers for the parameters of the MLME-START request */
  uint16_t m_updatePANId;
  uint8_t m_updateLogicalChannel;
  uint32_t m_updateStartTime;
  norace uint8_t m_updateBeaconOrder;
  uint8_t m_updateSuperframeOrder;
  bool m_updatePANCoordinator;
  bool m_updateBatteryLifeExtension;

  enum {
    MAX_BEACON_PAYLOAD_SIZE =  IEEE154_aMaxBeaconOverhead + IEEE154_aMaxBeaconPayloadLength,

    REQUEST_UPDATE_SF = 0x01,
    REQUEST_REALIGNMENT = 0x02,
    REQUEST_CONFIRM_PENDING = 0x04,
    REQUEST_REALIGNMENT_DONE_PENDING = 0x08,

    MODIFIED_SF_SPEC = 0x01,
    MODIFIED_GTS_FIELD = 0x02,
    MODIFIED_PENDING_ADDR_FIELD = 0x04,
    MODIFIED_SPECS_MASK = 0x0F,

    MODIFIED_BEACON_PAYLOAD = 0x10,
    MODIFIED_BEACON_PAYLOAD_NEW = 0x20,
    MODIFIED_BEACON_PAYLOAD_MASK = 0xF0,

    S_TX_IDLE = 0,
    S_TX_LOCKED = 1,
    S_TX_WAITING = 2,
  };
  uint8_t m_payload[MAX_BEACON_PAYLOAD_SIZE]; 

  /* function/task prototypes */
  task void txDoneTask();
  task void signalStartConfirmSuccessTask();
  void nextRound();
  void prepareBeaconTransmission();
  void continueStartRequest();
  void finishRealignment(ieee154_txframe_t *frame, ieee154_status_t status);

  command error_t Reset.init()
  {
    // reset this component, will only be called while we're not owning the token
    // TODO: check to signal MLME_START.confirm ?
    m_beaconFrame.header = &m_header;
    m_beaconFrame.headerLen = 0;
    m_beaconFrame.payload = m_payload;
    m_beaconFrame.payloadLen = 0;
    m_beaconFrame.metadata = &m_metadata;
    m_updateBeaconPayload = NULL;
    m_updateBeaconLength = 0;
    m_requestBitmap = m_payloadState = m_txState = 0;
    m_beaconPayloadLen = m_pendingAddrLen = m_pendingGtsLen = 0;
    m_gtsField[0] = 0;
    m_numCapSlots = 0;
    m_numGtsSlots = 0;
    m_beaconOrder = 15;
    call BeaconPayloadUpdateTimer.stop();
    call BeaconSendAlarm.stop();
    return SUCCESS;
  }

  /* ----------------------- MLME-START ----------------------- */
  /* "The MLME-START.request primitive allows the PAN coordinator to initiate a
   * new PAN or to begin using a new superframe configuration. This primitive may
   * also be used by a device already associated with an existing PAN to begin
   * using a new superframe configuration." (IEEE 802.15.4-2006 Sect. 7.1.14.1)
   **/

  command ieee154_status_t MLME_START.request  (
                          uint16_t panID,
                          uint8_t logicalChannel,
                          uint8_t channelPage,
                          uint32_t startTime,
                          uint8_t beaconOrder,
                          uint8_t superframeOrder,
                          bool panCoordinator,
                          bool batteryLifeExtension,
                          bool coordRealignment,
                          ieee154_security_t *coordRealignSecurity,
                          ieee154_security_t *beaconSecurity)
  {
    ieee154_status_t status = IEEE154_SUCCESS;
    ieee154_macShortAddress_t shortAddress = call MLME_GET.macShortAddress();

    // check parameters
    if ((coordRealignSecurity && coordRealignSecurity->SecurityLevel) ||
        (beaconSecurity && beaconSecurity->SecurityLevel))
      status = IEEE154_UNSUPPORTED_SECURITY;
    else if (shortAddress == 0xFFFF) 
      status = IEEE154_NO_SHORT_ADDRESS;
    else if (logicalChannel > 26 || beaconOrder > 15 || 
        (channelPage != IEEE154_SUPPORTED_CHANNELPAGE) ||
        !(IEEE154_SUPPORTED_CHANNELS & ((uint32_t) 1 << logicalChannel)) ||
        (superframeOrder > beaconOrder))
      status =  IEEE154_INVALID_PARAMETER;
    else if (startTime && !call IsTrackingBeacons.getNow())
      status = IEEE154_TRACKING_OFF;
    else if (startTime & 0xFF000000)
      status = IEEE154_INVALID_PARAMETER;
    else if (m_requestBitmap & (REQUEST_CONFIRM_PENDING | REQUEST_UPDATE_SF))
      status = IEEE154_TRANSACTION_OVERFLOW;
    else {

      // New configuration *will* be put in operation, we'll buffer
      // the parameters now, and continue once we get the token.
      if (panCoordinator)
        startTime = 0;        // start immediately
      if (beaconOrder == 15)
        superframeOrder = 15; // beaconless PAN 
      m_updatePANId = panID;
      m_updateLogicalChannel = logicalChannel;
      m_updateStartTime = startTime;
      m_updateBeaconOrder = beaconOrder; 
      m_updateSuperframeOrder = superframeOrder;
      m_updatePANCoordinator = panCoordinator;
      m_updateBatteryLifeExtension = batteryLifeExtension;   
      m_requestBitmap = (REQUEST_CONFIRM_PENDING | REQUEST_UPDATE_SF); // lock

      if (coordRealignment)
        m_requestBitmap |= REQUEST_REALIGNMENT;
      if (m_beaconOrder == 15) {
        // We're not already transmitting beacons, i.e. we have to request the token
        // (otherwise we'd get the token "automatically" for the next scheduled beacon).
        call RadioToken.request();
      }
      // We'll continue the MLME_START operation in continueStartRequest() once we have the token
    }
    dbg_serial("BeaconTransmitP", "MLME_START.request -> result: %lu\n", (uint32_t) status);
    return status;
  }

  void continueStartRequest()
  {
    uint8_t offset;
    ieee154_macShortAddress_t shortAddress;
    bool isShortAddr;

    // (1) coord realignment?
    if (m_requestBitmap & REQUEST_REALIGNMENT) {
      ieee154_txframe_t *realignmentFrame = call GetSetRealignmentFrame.get();
      m_requestBitmap &= ~REQUEST_REALIGNMENT;
      if (realignmentFrame == NULL) {
        // allocation failed!
        m_requestBitmap = 0;
        signal MLME_START.confirm(IEEE154_TRANSACTION_OVERFLOW);
        return;
      }
      // set the payload portion of the realignmentFrame
      // (the header fields are already set correctly)
      realignmentFrame->payload[0] = CMD_FRAME_COORDINATOR_REALIGNMENT;
      *((nxle_uint16_t*) &realignmentFrame->payload[1]) = m_updatePANId;
      *((nxle_uint16_t*) &realignmentFrame->payload[3]) = call MLME_GET.macShortAddress();
      realignmentFrame->payload[5] = m_updateLogicalChannel;
      *((nxle_uint16_t*) &realignmentFrame->payload[6]) = 0xFFFF;
      realignmentFrame->payloadLen = 8;
      
      if (m_beaconOrder < 15) {
        // we're already transmitting beacons; the realignment frame
        // must be sent (broadcast) after the next beacon
        if (call RealignmentBeaconEnabledTx.transmit(realignmentFrame) != IEEE154_SUCCESS) {
          m_requestBitmap = 0;
          call GetSetRealignmentFrame.set(realignmentFrame);
          signal MLME_START.confirm(IEEE154_TRANSACTION_OVERFLOW);
        } else {

⌨️ 快捷键说明

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