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

📄 pibp.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 -------------------------------------------------------------
 * $Date: 2009/09/08 09:04:43 $
 * @author Jan Hauer <hauer@tkn.tu-berlin.de>
 * ========================================================================
 */

/** 
 * This component maintains the PIB (PAN Information Base) attributes and
 * provides interfaces for accessing fields in a MAC frame. 
 */

#include "TKN154.h"
#include "TKN154_PIB.h"
#include "TKN154_PHY.h"

module PibP {
  provides 
  {
    interface Init as LocalInit;
    interface MLME_RESET;
    interface MLME_GET;
    interface MLME_SET;
    interface Set<ieee154_macSuperframeOrder_t> as SetMacSuperframeOrder;
    interface Set<ieee154_macBeaconTxTime_t> as SetMacBeaconTxTime;
    interface Set<ieee154_macPanCoordinator_t> as SetMacPanCoordinator;
    interface FrameUtility;
    interface IEEE154Frame as Frame;
    interface IEEE154BeaconFrame as BeaconFrame;
    interface Get<uint64_t> as GetLocalExtendedAddress;
    interface GetNow<token_requested_t> as IsRadioTokenRequested;
    interface Notify<const void*> as PIBUpdate[uint8_t PIBAttributeID];
    interface Packet;
    interface TimeCalc;
  }
  uses
  {
    interface Get<bool> as PromiscuousModeGet;
    interface Init as DispatchReset;
    interface Init as DispatchQueueReset;
    interface Init as MacReset;
    interface SplitControl as RadioControl;
    interface Random;
    interface TransferableResource as RadioToken;
    interface RadioOff;
    interface LocalTime<TSymbolIEEE802154>;
  }
}
implementation
{
  ieee154_PIB_t m_pib;
  uint8_t m_numResetClientPending;
  bool m_setDefaultPIB;
  norace uint8_t m_resetSpin;

#ifdef IEEE154_EXTENDED_ADDRESS
  const uint64_t m_aExtendedAddressLE = IEEE154_EXTENDED_ADDRESS;
#else
  norace uint64_t m_aExtendedAddressLE;
#endif
  task void radioControlStopTask();
  void updateMacMaxFrameTotalWaitTime();
  void resetAttributesToDefault();
  bool isLocalExtendedAddress(uint8_t *addrLE);
  bool isCoordExtendedAddress(uint8_t *addrLE); 
  uint8_t getPendAddrSpecOffset(uint8_t *macPayloadField);
  task void resetSpinTask();
  
  command error_t LocalInit.init()
  {
#ifndef IEEE154_EXTENDED_ADDRESS
    m_aExtendedAddressLE = (((uint64_t) call Random.rand32() ) << 32 ) | call Random.rand32(); 
#endif
    resetAttributesToDefault();
    return SUCCESS;
  }

  void resetAttributesToDefault() {
    m_pib.phyCurrentChannel = IEEE154_DEFAULT_CURRENTCHANNEL;
    m_pib.phyTransmitPower = (IEEE154_TXPOWER_TOLERANCE | (IEEE154_DEFAULT_TRANSMITPOWER_dBm & 0x3F));
    m_pib.phyCCAMode = IEEE154_DEFAULT_CCAMODE;
    m_pib.phyCurrentPage = IEEE154_DEFAULT_CURRENTPAGE;

    m_pib.macAssociatedPANCoord = IEEE154_DEFAULT_ASSOCIATEDPANCOORD;
    m_pib.macAssociationPermit = IEEE154_DEFAULT_ASSOCIATIONPERMIT;
    m_pib.macAutoRequest = IEEE154_DEFAULT_AUTOREQUEST;
    m_pib.macBattLifeExt = IEEE154_DEFAULT_BATTLIFEEXT;
    m_pib.macBattLifeExtPeriods = IEEE154_DEFAULT_BATTLIFEEXTPERIODS;
    m_pib.macBeaconPayloadLength = IEEE154_DEFAULT_BEACONPAYLOADLENGTH;
    m_pib.macBeaconOrder = IEEE154_DEFAULT_BEACONORDER;
    m_pib.macBeaconTxTime = IEEE154_DEFAULT_BEACONTXTIME;
    m_pib.macBSN = 0xFF & (call Random.rand16());
    //  macCoordExtendedAddress: default is undefined
    m_pib.macCoordShortAddress = IEEE154_DEFAULT_COORDSHORTADDRESS;
    m_pib.macDSN = 0xFF & (call Random.rand16());
    m_pib.macGTSPermit = IEEE154_DEFAULT_GTSPERMIT;
    m_pib.macMaxBE = IEEE154_DEFAULT_MAXBE;
    m_pib.macMaxCSMABackoffs = IEEE154_DEFAULT_MAXCSMABACKOFFS;
    m_pib.macMaxFrameRetries = IEEE154_DEFAULT_MAXFRAMERETRIES;
    m_pib.macMinBE = IEEE154_DEFAULT_MINBE;
    m_pib.macPANId = IEEE154_DEFAULT_PANID;
    m_pib.macPromiscuousMode = IEEE154_DEFAULT_PROMISCUOUSMODE;
    m_pib.macResponseWaitTime = IEEE154_DEFAULT_RESPONSEWAITTIME;
    m_pib.macRxOnWhenIdle = IEEE154_DEFAULT_RXONWHENIDLE;
    m_pib.macSecurityEnabled = IEEE154_DEFAULT_SECURITYENABLED;
    m_pib.macShortAddress = IEEE154_DEFAULT_SHORTADDRESS;
    m_pib.macSuperframeOrder = IEEE154_DEFAULT_SUPERFRAMEORDER;
    m_pib.macTransactionPersistenceTime = IEEE154_DEFAULT_TRANSACTIONPERSISTENCETIME;
    updateMacMaxFrameTotalWaitTime();
  }

  void updateMacMaxFrameTotalWaitTime()
  {
    // using equation 14 on page 160
    ieee154_macMinBE_t macMinBE = m_pib.macMinBE; 
    ieee154_macMaxBE_t macMaxBE = m_pib.macMaxBE; 
    ieee154_macMaxCSMABackoffs_t macMaxCSMABackoffs = m_pib.macMaxCSMABackoffs;
    uint8_t m = macMaxBE - macMinBE, k;
    uint32_t waitTime = 0;

    if (macMaxCSMABackoffs < m)
      m = macMaxCSMABackoffs;
    waitTime = (((uint16_t) 1 << macMaxBE) - 1) * (macMaxCSMABackoffs - m);
    if (m) {
      k = 0;
      while (k != m) {
        waitTime += ((uint16_t) 1 << (macMinBE+k));
        k += 1;
      }
    }
    waitTime *= IEEE154_aUnitBackoffPeriod;
    waitTime += call MLME_GET.phyMaxFrameDuration();
    m_pib.macMaxFrameTotalWaitTime = waitTime;
  }

  command ieee154_status_t MLME_RESET.request(bool SetDefaultPIB) 
  {
    // resetting the complete stack is not so easy...
    // first we acquire the Token (get exclusive radio access), then we switch off 
    // the radio and reset all MAC components, starting from the ones that might 
    // still have any frames queued / allocated to get them flushed out. While we 
    // own the Token all other components are "inactive" (and there are no pending
    // Alarms!), but there can still be pending Timers/tasks -> we stop all Timers
    // through MacReset.init() and then spin a few tasks in between to get 
    // everything "flushed out"
    ieee154_status_t status = IEEE154_SUCCESS;
    if (call PromiscuousModeGet.get())
      status = IEEE154_TRANSACTION_OVERFLOW; // must first cancel promiscuous mode!
    else {
      m_setDefaultPIB = SetDefaultPIB;
      m_resetSpin = 5;
      call RadioToken.request();
    }   
    dbg_serial("PibP", "MLME_RESET.request(%lu) -> result: %lu\n", 
        (uint32_t) SetDefaultPIB, (uint32_t) status);
    return status;
  }

  event void RadioToken.granted()
  {
    if (call RadioOff.off() != SUCCESS)
      signal RadioOff.offDone();
  }

  async event void RadioOff.offDone()
  {
    post radioControlStopTask();
  }

  task void radioControlStopTask()
  {
    error_t result = call RadioControl.stop();
    if (result == EALREADY)
      signal RadioControl.stopDone(SUCCESS);
    else
      ASSERT(result == SUCCESS);
  }

  event void RadioControl.stopDone(error_t result)
  {
    ASSERT(result == SUCCESS);
    call DispatchReset.init();       // resets the dispatch component(s), spools out frames
    call DispatchQueueReset.init();  // resets the dispatch queue component(s), spools out frames
    call MacReset.init();            // resets the remaining components
    post resetSpinTask();
  }

  task void resetSpinTask()
  {
    m_resetSpin -= 1;
    if (m_resetSpin != 0) {
      post resetSpinTask();
      return;
    }
    ASSERT(call RadioControl.start() == SUCCESS);
  }

  async command token_requested_t IsRadioTokenRequested.getNow(){ return m_resetSpin != 0; }

  event void RadioControl.startDone(error_t error)
  {
    if (m_setDefaultPIB)
      resetAttributesToDefault();
    else {
      // restore previous PHY attributes
      signal PIBUpdate.notify[IEEE154_phyCurrentChannel](&m_pib.phyCurrentChannel);
      signal PIBUpdate.notify[IEEE154_phyTransmitPower](&m_pib.phyTransmitPower);
      signal PIBUpdate.notify[IEEE154_phyCCAMode](&m_pib.phyCCAMode);
      signal PIBUpdate.notify[IEEE154_phyCurrentPage](&m_pib.phyCurrentPage);
      signal PIBUpdate.notify[IEEE154_macPANId](&m_pib.macPANId);
      signal PIBUpdate.notify[IEEE154_macShortAddress](&m_pib.macShortAddress);
      signal PIBUpdate.notify[IEEE154_macPanCoordinator](&m_pib.macPanCoordinator);
    }
    call RadioToken.release();
    signal MLME_RESET.confirm(IEEE154_SUCCESS);
  }
  
  /* ----------------------- MLME-GET ----------------------- */

  command ieee154_phyCurrentChannel_t MLME_GET.phyCurrentChannel() { return m_pib.phyCurrentChannel;}

  command ieee154_phyChannelsSupported_t MLME_GET.phyChannelsSupported() { return IEEE154_SUPPORTED_CHANNELS;}

  command ieee154_phyTransmitPower_t MLME_GET.phyTransmitPower() { return m_pib.phyTransmitPower;}

  command ieee154_phyCCAMode_t MLME_GET.phyCCAMode() { return m_pib.phyCCAMode;}

  command ieee154_phyCurrentPage_t MLME_GET.phyCurrentPage() { return m_pib.phyCurrentPage;}

  command ieee154_phyMaxFrameDuration_t MLME_GET.phyMaxFrameDuration() { return IEEE154_MAX_FRAME_DURATION;}

  command ieee154_phySHRDuration_t MLME_GET.phySHRDuration() { return IEEE154_SHR_DURATION;}

  command ieee154_phySymbolsPerOctet_t MLME_GET.phySymbolsPerOctet() { return IEEE154_SYMBOLS_PER_OCTET;}

  command ieee154_macAckWaitDuration_t MLME_GET.macAckWaitDuration() { return IEEE154_ACK_WAIT_DURATION;}

  command ieee154_macAssociationPermit_t MLME_GET.macAssociationPermit() { return m_pib.macAssociationPermit;}

  command ieee154_macAutoRequest_t MLME_GET.macAutoRequest() { return m_pib.macAutoRequest;}

  command ieee154_macBattLifeExt_t MLME_GET.macBattLifeExt() { return m_pib.macBattLifeExt;}

  command ieee154_macBattLifeExtPeriods_t MLME_GET.macBattLifeExtPeriods() { return m_pib.macBattLifeExtPeriods;}

  command ieee154_macBeaconOrder_t MLME_GET.macBeaconOrder() { return m_pib.macBeaconOrder;}

  command ieee154_macBeaconTxTime_t MLME_GET.macBeaconTxTime() { return m_pib.macBeaconTxTime;}

  command ieee154_macBSN_t MLME_GET.macBSN() { return m_pib.macBSN;}

  command ieee154_macCoordExtendedAddress_t MLME_GET.macCoordExtendedAddress() { return m_pib.macCoordExtendedAddress;}

  command ieee154_macCoordShortAddress_t MLME_GET.macCoordShortAddress() { return m_pib.macCoordShortAddress;}

  command ieee154_macDSN_t MLME_GET.macDSN() { return m_pib.macDSN;}

  command ieee154_macGTSPermit_t MLME_GET.macGTSPermit() { return m_pib.macGTSPermit;}

  command ieee154_macMaxCSMABackoffs_t MLME_GET.macMaxCSMABackoffs() { return m_pib.macMaxCSMABackoffs;}

  command ieee154_macMinBE_t MLME_GET.macMinBE() { return m_pib.macMinBE;}

  command ieee154_macPANId_t MLME_GET.macPANId() { return m_pib.macPANId;}

  command ieee154_macPromiscuousMode_t MLME_GET.macPromiscuousMode() { return call PromiscuousModeGet.get();}

  command ieee154_macRxOnWhenIdle_t MLME_GET.macRxOnWhenIdle() { return m_pib.macRxOnWhenIdle;}

  command ieee154_macShortAddress_t MLME_GET.macShortAddress() { return m_pib.macShortAddress;}

  command ieee154_macSuperframeOrder_t MLME_GET.macSuperframeOrder() { return m_pib.macSuperframeOrder;}

  command ieee154_macTransactionPersistenceTime_t MLME_GET.macTransactionPersistenceTime() { return m_pib.macTransactionPersistenceTime;}

  command ieee154_macAssociatedPANCoord_t MLME_GET.macAssociatedPANCoord() { return m_pib.macAssociatedPANCoord;}

  command ieee154_macMaxBE_t MLME_GET.macMaxBE() { return m_pib.macMaxBE;}

  command ieee154_macMaxFrameTotalWaitTime_t MLME_GET.macMaxFrameTotalWaitTime() { return m_pib.macMaxFrameTotalWaitTime;}

  command ieee154_macMaxFrameRetries_t MLME_GET.macMaxFrameRetries() { return m_pib.macMaxFrameRetries;}

  command ieee154_macResponseWaitTime_t MLME_GET.macResponseWaitTime() { return m_pib.macResponseWaitTime;}

  command ieee154_macSyncSymbolOffset_t MLME_GET.macSyncSymbolOffset() { return IEEE154_SYNC_SYMBOL_OFFSET;}

  command ieee154_macTimestampSupported_t MLME_GET.macTimestampSupported() { return IEEE154_TIMESTAMP_SUPPORTED;}

  command ieee154_macSecurityEnabled_t MLME_GET.macSecurityEnabled() { return m_pib.macSecurityEnabled;}

  command ieee154_macMinLIFSPeriod_t MLME_GET.macMinLIFSPeriod() { return IEEE154_MIN_LIFS_PERIOD;}

  command ieee154_macMinSIFSPeriod_t MLME_GET.macMinSIFSPeriod() { return IEEE154_MIN_SIFS_PERIOD;}

  command ieee154_macPanCoordinator_t MLME_GET.macPanCoordinator() { return m_pib.macPanCoordinator;}

  /* ----------------------- MLME-SET ----------------------- */

  command ieee154_status_t MLME_SET.phyCurrentChannel(ieee154_phyCurrentChannel_t value) {
    uint32_t i = 1;
    uint8_t k = value;
    while (i && k) {
      i <<= 1;
      k -= 1;
    }
    if (!(IEEE154_SUPPORTED_CHANNELS & i))
      return IEEE154_INVALID_PARAMETER;
    m_pib.phyCurrentChannel = value;
    signal PIBUpdate.notify[IEEE154_phyCurrentChannel](&m_pib.phyCurrentChannel);
    return IEEE154_SUCCESS;
  }

  command ieee154_status_t MLME_SET.phyTransmitPower(ieee154_phyTransmitPower_t value) {
    m_pib.phyTransmitPower = (value & 0x3F);
    signal PIBUpdate.notify[IEEE154_phyTransmitPower](&m_pib.phyTransmitPower);
    return IEEE154_SUCCESS;
  }

  command ieee154_status_t MLME_SET.phyCCAMode(ieee154_phyCCAMode_t value) {
    if (value < 1 || value > 3)
      return IEEE154_INVALID_PARAMETER;
    m_pib.phyCCAMode = value;
    signal PIBUpdate.notify[IEEE154_phyCCAMode](&m_pib.phyCCAMode);

⌨️ 快捷键说明

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