0.01.00
mac.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, The OpenThread Authors.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of the copyright holder nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
34 #ifndef MAC_HPP_
35 #define MAC_HPP_
36 
37 #include "openthread-core-config.h"
38 
40 
41 #include "common/context.hpp"
42 #include "common/locator.hpp"
43 #include "common/tasklet.hpp"
44 #include "common/timer.hpp"
45 #include "mac/mac_frame.hpp"
46 #include "mac/mac_filter.hpp"
47 #include "thread/key_manager.hpp"
49 #include "thread/topology.hpp"
50 
51 namespace ot {
52 
53 namespace Mle { class MleRouter; }
54 
65 namespace Mac {
66 
71 enum
72 {
73  kMinBE = 3,
74  kMaxBE = 5,
77 
79 
80  kAckTimeout = 16,
82  kSleepDelay = 300,
83  kNonceSize = 13,
84 
87 
93 
100 };
101 
106 class Receiver: public Context
107 {
108  friend class Mac;
109 
110 public:
118  typedef void (*ReceiveFrameHandler)(Receiver &aReceiver, Frame &aFrame);
119 
127  typedef void (*DataPollTimeoutHandler)(Receiver &aReceiver);
128 
137  Receiver(ReceiveFrameHandler aReceiveFrameHandler, DataPollTimeoutHandler aPollTimeoutHandler, void *aContext):
138  Context(aContext),
139  mReceiveFrameHandler(aReceiveFrameHandler),
140  mPollTimeoutHandler(aPollTimeoutHandler),
141  mNext(NULL) {
142  }
143 
144 private:
145  void HandleReceivedFrame(Frame &aFrame) { mReceiveFrameHandler(*this, aFrame); }
146 
147  void HandleDataPollTimeout(void) {
148  if (mPollTimeoutHandler != NULL) {
149  mPollTimeoutHandler(*this);
150  }
151  }
152 
153  ReceiveFrameHandler mReceiveFrameHandler;
154  DataPollTimeoutHandler mPollTimeoutHandler;
155  Receiver *mNext;
156 };
157 
162 class Sender: public Context
163 {
164  friend class Mac;
165 
166 public:
174  typedef otError(*FrameRequestHandler)(Sender &aSender, Frame &aFrame);
175 
184  typedef void (*SentFrameHandler)(Sender &aSender, Frame &aFrame, otError aError);
185 
194  Sender(FrameRequestHandler aFrameRequestHandler, SentFrameHandler aSentFrameHandler, void *aContext):
195  Context(aContext),
196  mFrameRequestHandler(aFrameRequestHandler),
197  mSentFrameHandler(aSentFrameHandler),
198  mNext(NULL) {
199  }
200 
201 private:
202  otError HandleFrameRequest(Frame &aFrame) { return mFrameRequestHandler(*this, aFrame); }
203  void HandleSentFrame(Frame &aFrame, otError aError) { mSentFrameHandler(*this, aFrame, aError); }
204 
205  FrameRequestHandler mFrameRequestHandler;
206  SentFrameHandler mSentFrameHandler;
207  Sender *mNext;
208 };
209 
214 class Mac: public InstanceLocator
215 {
216 public:
223  explicit Mac(otInstance &aInstance);
224 
232  typedef void (*ActiveScanHandler)(void *aContext, Frame *aBeaconFrame);
233 
243  otError ActiveScan(uint32_t aScanChannels, uint16_t aScanDuration, ActiveScanHandler aHandler, void *aContext);
244 
256  otError ConvertBeaconToActiveScanResult(Frame *aBeaconFrame, otActiveScanResult &aResult);
257 
267  typedef void (*EnergyScanHandler)(void *aContext, otEnergyScanResult *aResult);
268 
281  otError EnergyScan(uint32_t aScanChannels, uint16_t aScanDuration, EnergyScanHandler aHandler, void *aContext);
282 
289  void EnergyScanDone(int8_t aEnergyScanMaxRssi);
290 
297  bool IsBeaconEnabled(void) const { return mBeaconsEnabled; }
298 
305  void SetBeaconEnabled(bool aEnabled) { mBeaconsEnabled = aEnabled; }
306 
313  bool GetRxOnWhenIdle(void) const { return mRxOnWhenIdle; }
314 
321  void SetRxOnWhenIdle(bool aRxOnWhenIdle);
322 
332  otError RegisterReceiver(Receiver &aReceiver);
333 
343  otError SendFrameRequest(Sender &aSender);
344 
351  void GenerateExtAddress(ExtAddress *aExtAddress);
352 
359  const ExtAddress *GetExtAddress(void) const { return &mExtAddress; }
360 
367  void SetExtAddress(const ExtAddress &aExtAddress);
368 
375  ShortAddress GetShortAddress(void) const { return mShortAddress; }
376 
385  otError SetShortAddress(ShortAddress aShortAddress);
386 
393  uint8_t GetChannel(void) const { return mChannel; }
394 
403  otError SetChannel(uint8_t aChannel);
404 
411  int8_t GetMaxTransmitPower(void) const { return mMaxTransmitPower; }
412 
419  void SetMaxTransmitPower(int8_t aPower) { mMaxTransmitPower = aPower; }
420 
427  const char *GetNetworkName(void) const { return mNetworkName.m8; }
428 
437  otError SetNetworkName(const char *aNetworkName);
438 
445  uint16_t GetPanId(void) const { return mPanId; }
446 
455  otError SetPanId(uint16_t aPanId);
456 
463  const uint8_t *GetExtendedPanId(void) const { return mExtendedPanId.m8; }
464 
473  otError SetExtendedPanId(const uint8_t *aExtPanId);
474 
475 #if OPENTHREAD_ENABLE_MAC_FILTER
476 
482  Filter &GetFilter(void) { return mFilter; }
483 #endif // OPENTHREAD_ENABLE_MAC_FILTER
484 
493  void ReceiveDoneTask(Frame *aFrame, otError aError);
494 
500  void TransmitStartedTask(otRadioFrame *aFrame);
501 
513  void TransmitDoneTask(otRadioFrame *aFrame, otRadioFrame *aAckFrame, otError aError);
514 
519  bool IsActiveScanInProgress(void);
520 
525  bool IsEnergyScanInProgress(void);
526 
535  bool IsInTransmitState(void);
536 
545  void SetPcapCallback(otLinkPcapCallback aPcapCallback, void *aCallbackContext);
546 
554  bool IsPromiscuous(void);
555 
564  void SetPromiscuous(bool aPromiscuous);
565 
572  void FillMacCountersTlv(NetworkDiagnostic::MacCountersTlv &aMacCounters) const;
573 
578  void ResetCounters(void);
579 
580 #if OPENTHREAD_CONFIG_ENABLE_BEACON_RSP_WHEN_JOINABLE
581 
588  bool IsBeaconJoinable(void);
589 #endif // OPENTHREAD_CONFIG_ENABLE_BEACON_RSP_WHEN_JOINABLE
590 
597  otMacCounters &GetCounters(void) { return mCounters; }
598 
605  int8_t GetNoiseFloor(void) { return otPlatRadioGetReceiveSensitivity(&GetInstance()); }
606 
614  bool RadioSupportsCsmaBackoff(void);
615 
623  bool RadioSupportsRetries(void);
624 
625 private:
626  enum
627  {
628  kInvalidRssiValue = 127
629  };
630 
631  enum Operation
632  {
633  kOperationIdle = 0,
634  kOperationActiveScan,
635  kOperationEnergyScan,
636  kOperationTransmitBeacon,
637  kOperationTransmitData,
638  kOperationWaitingForData,
639  };
640 
641  void GenerateNonce(const ExtAddress &aAddress, uint32_t aFrameCounter, uint8_t aSecurityLevel, uint8_t *aNonce);
642  void ProcessTransmitSecurity(Frame &aFrame);
643  otError ProcessReceiveSecurity(Frame &aFrame, const Address &aSrcAddr, Neighbor *aNeighbor);
644  void UpdateIdleMode(void);
645  void StartOperation(Operation aOperation);
646  void FinishOperation(void);
647  void SentFrame(otError aError);
648  void SendBeaconRequest(Frame &aFrame);
649  void SendBeacon(Frame &aFrame);
650  void StartBackoff(void);
651  void StartEnergyScan(void);
652  otError HandleMacCommand(Frame &aFrame);
653 
654  static void HandleMacTimer(Timer &aTimer);
655  void HandleMacTimer(void);
656  static void HandleBeginTransmit(Timer &aTimer);
657  void HandleBeginTransmit(void);
658  static void HandleReceiveTimer(Timer &aTimer);
659  void HandleReceiveTimer(void);
660  static void HandleEnergyScanSampleRssi(Tasklet &aTasklet);
661  void HandleEnergyScanSampleRssi(void);
662 
663  void StartCsmaBackoff(void);
664  otError Scan(Operation aScanOperation, uint32_t aScanChannels, uint16_t aScanDuration, void *aContext);
665 
666  otError RadioTransmit(Frame *aSendFrame);
667  otError RadioReceive(uint8_t aChannel);
668  otError RadioSleep(void);
669 
670  static Mac &GetOwner(const Context &aContext);
671 
672  static const char *OperationToString(Operation aOperation);
673 
674  Operation mOperation;
675 
676  bool mPendingActiveScan : 1;
677  bool mPendingEnergyScan : 1;
678  bool mPendingTransmitBeacon : 1;
679  bool mPendingTransmitData : 1;
680  bool mPendingWaitingForData : 1;
681  bool mRxOnWhenIdle : 1;
682  bool mBeaconsEnabled : 1;
683 #if OPENTHREAD_CONFIG_STAY_AWAKE_BETWEEN_FRAGMENTS
684  bool mDelaySleep : 1;
685 #endif
686 
687  TimerMilli mMacTimer;
688 #if OPENTHREAD_CONFIG_ENABLE_PLATFORM_USEC_TIMER
689  TimerMicro mBackoffTimer;
690 #else
691  TimerMilli mBackoffTimer;
692 #endif
693  TimerMilli mReceiveTimer;
694 
695  ExtAddress mExtAddress;
696  ShortAddress mShortAddress;
697  PanId mPanId;
698  uint8_t mChannel;
699  int8_t mMaxTransmitPower;
700 
701  otNetworkName mNetworkName;
702  otExtendedPanId mExtendedPanId;
703 
704  Sender *mSendHead, *mSendTail;
705  Receiver *mReceiveHead, *mReceiveTail;
706 
707  uint8_t mBeaconSequence;
708  uint8_t mDataSequence;
709  uint8_t mCsmaAttempts;
710  uint8_t mTransmitAttempts;
711 
712  uint32_t mScanChannels;
713  uint16_t mScanDuration;
714  uint8_t mScanChannel;
715  int8_t mEnergyScanCurrentMaxRssi;
716  void *mScanContext;
717  union
718  {
719  ActiveScanHandler mActiveScanHandler;
720  EnergyScanHandler mEnergyScanHandler;
721  };
722  Tasklet mEnergyScanSampleRssiTask;
723 
724  otLinkPcapCallback mPcapCallback;
725  void *mPcapCallbackContext;
726 
727 #if OPENTHREAD_ENABLE_MAC_FILTER
728  Filter mFilter;
729 #endif // OPENTHREAD_ENABLE_MAC_FILTER
730 
731  Frame *mTxFrame;
732 
733  otMacCounters mCounters;
734  uint32_t mKeyIdMode2FrameCounter;
735 };
736 
742 } // namespace Mac
743 } // namespace ot
744 
745 #endif // MAC_HPP_
This struct represents an energy scan result.
Definition: types.h:542
#define OPENTHREAD_CONFIG_MAX_TX_ATTEMPTS_INDIRECT_PER_POLL
Maximum number of MAC layer transmit attempts for an outbound indirect frame (to a sleepy child) afte...
Definition: openthread-core-default-config.h:147
Minimum backoff (milliseconds).
Definition: mac.hpp:78
void SetBeaconEnabled(bool aEnabled)
This method enables/disables IEEE 802.15.4 Beacon transmissions.
Definition: mac.hpp:305
This type represents all the static / global variables used by OpenThread allocated in one place...
Definition: openthread-instance.h:59
Timeout for waiting on an ACK (milliseconds).
Definition: mac.hpp:80
Maximum number of MAC layer tx attempts for an outbound indirect frame (for a sleepy child) after rec...
Definition: mac.hpp:99
Definition: cli.cpp:90
This class implements IEEE 802.15.4 MAC frame generation and parsing.
Definition: mac_frame.hpp:177
This structure represents an IEEE 802.15.4 Short or Extended Address.
Definition: mac_frame.hpp:147
const ExtAddress * GetExtAddress(void) const
This method returns a pointer to the IEEE 802.15.4 Extended Address.
Definition: mac.hpp:359
int8_t otPlatRadioGetReceiveSensitivity(otInstance *aInstance)
Get the radio receive sensitivity value.
This class implements a MAC receiver client.
Definition: mac.hpp:106
This class implements MLE functionality required by the Thread Router and Leader roles.
Definition: mle_router_ftd.hpp:76
All channels.
Definition: mac.hpp:85
This file includes definitions for locator class for OpenThread objects.
uint16_t GetPanId(void) const
This method returns the IEEE 802.15.4 PAN ID.
Definition: mac.hpp:445
This class implements MLE functionality required by the Thread EndDevices, Router, and Leader roles.
Definition: mle.hpp:462
This file defines the radio interface for OpenThread.
ShortAddress GetShortAddress(void) const
This method returns the IEEE 802.15.4 Short Address.
Definition: mac.hpp:375
This class implements a MAC sender client.
Definition: mac.hpp:162
This class implements Mac Counters TLV generation and parsing.
Definition: network_diagnostic_tlvs.hpp:894
Default interval between channels (milliseconds).
Definition: mac.hpp:86
This class is used to represent a tasklet.
Definition: tasklet.hpp:64
This file includes definitions for maintaining Thread network topologies.
This structure represents an IEEE 802.15.4 radio frame.
Definition: radio.h:99
This file includes definitions for maintaining a pointer to arbitrary context information.
This class represents a Thread neighbor.
Definition: topology.hpp:53
This class implements a timer.
Definition: timer.hpp:69
This class implements the millisecond timer.
Definition: timer.hpp:145
Sender(FrameRequestHandler aFrameRequestHandler, SentFrameHandler aSentFrameHandler, void *aContext)
This constructor creates a MAC sender client.
Definition: mac.hpp:194
This file includes definitions for IEEE 802.15.4 frame filtering based on MAC address.
void SetMaxTransmitPower(int8_t aPower)
This method sets the maximum transmit power in dBm.
Definition: mac.hpp:419
int8_t GetNoiseFloor(void)
This method returns the noise floor value (currently use the radio receive sensitivity value)...
Definition: mac.hpp:605
Timeout for receiving Data Frame (milliseconds).
Definition: mac.hpp:81
const uint8_t * GetExtendedPanId(void) const
This method returns the IEEE 802.15.4 Extended PAN ID.
Definition: mac.hpp:463
This structure represents an IEEE 802.15.4 Extended Address.
Definition: mac_frame.hpp:84
This file includes definitions for generating and processing IEEE 802.15.4 MAC frames.
bool IsBeaconEnabled(void) const
This method indicates whether or not IEEE 802.15.4 Beacon transmissions are enabled.
Definition: mac.hpp:297
uint8_t GetChannel(void) const
This method returns the IEEE 802.15.4 Channel.
Definition: mac.hpp:393
This class implements definitions for maintaining a pointer to arbitrary context information.
Definition: context.hpp:61
This file includes definitions for tasklets and the tasklet scheduler.
This file includes definitions for generating and processing MLE TLVs.
otMacCounters & GetCounters(void)
This method returns the MAC counter.
Definition: mac.hpp:597
int8_t GetMaxTransmitPower(void) const
This method returns the maximum transmit power in dBm.
Definition: mac.hpp:411
This struct represents a received IEEE 802.15.4 Beacon.
Definition: types.h:522
Receiver(ReceiveFrameHandler aReceiveFrameHandler, DataPollTimeoutHandler aPollTimeoutHandler, void *aContext)
This constructor creates a MAC receiver client.
Definition: mac.hpp:137
This class implements the IEEE 802.15.4 MAC.
Definition: mac.hpp:214
This class implements locator for otInstance object.
Definition: locator.hpp:63
Maximum number of MAC layer tx attempts for an outbound direct frame.
Definition: mac.hpp:92
const char * GetNetworkName(void) const
This method returns the IEEE 802.15.4 Network Name.
Definition: mac.hpp:427
This file includes definitions for the multiplexed timer service.
#define OPENTHREAD_CONFIG_MAX_TX_ATTEMPTS_DIRECT
Maximum number of MAC layer transmit attempts for an outbound direct frame.
Definition: openthread-core-default-config.h:136
macMaxCSMABackoffs (IEEE 802.15.4-2006)
Definition: mac.hpp:75
macMinBE (IEEE 802.15.4-2006)
Definition: mac.hpp:73
This structure represents a Network Name.
Definition: types.h:314
This structure represents the MAC layer counters.
Definition: types.h:1011
Max sleep delay when frame is pending (milliseconds).
Definition: mac.hpp:82
#define OT_CHANNEL_ALL
All channels.
Definition: types.h:504
otPanId PanId
This type represents the IEEE 802.15.4 PAN ID.
Definition: mac_frame.hpp:72
bool GetRxOnWhenIdle(void) const
This method indicates whether or not rx-on-when-idle is enabled.
Definition: mac.hpp:313
Size of IEEE 802.15.4 Nonce (bytes).
Definition: mac.hpp:83
Number of symbols (IEEE 802.15.4-2006)
Definition: mac.hpp:76
This structure represents an Extended PAN ID.
Definition: types.h:325
otError
This enumeration represents error codes used throughout OpenThread.
Definition: types.h:107
otShortAddress ShortAddress
This type represents the IEEE 802.15.4 Short Address.
Definition: mac_frame.hpp:78
This file includes compile-time configuration constants for OpenThread.
macMaxBE (IEEE 802.15.4-2006)
Definition: mac.hpp:74
This file includes definitions for Thread security material generation.