1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/*
* Copyright (c) 2016, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. Neither the name of the copyright holder 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 HOLDER 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.
*/
/**
* @file
* This file includes definitions for the IEEE 802.15.4 MAC.
*/
#ifndef MAC_HPP_
#define MAC_HPP_
#include "openthread-core-config.h"
#include <openthread/platform/radio.h>
#include <openthread/platform/time.h>
#include "common/locator.hpp"
#include "common/tasklet.hpp"
#include "common/timer.hpp"
#include "mac/channel_mask.hpp"
#include "mac/mac_filter.hpp"
#include "mac/mac_frame.hpp"
#include "mac/mac_types.hpp"
#include "mac/sub_mac.hpp"
#include "thread/key_manager.hpp"
#include "thread/link_quality.hpp"
namespace ot {
class Neighbor;
/**
* @addtogroup core-mac
*
* @brief
* This module includes definitions for the IEEE 802.15.4 MAC
*
* @{
*
*/
namespace Mac {
/**
* Protocol parameters and constants.
*
*/
enum
{
kDataPollTimeout = 100, ///< Timeout for receiving Data Frame (milliseconds).
kSleepDelay = 300, ///< Max sleep delay when frame is pending (milliseconds).
kScanDurationDefault = 300, ///< Default interval between channels (milliseconds).
kMaxCsmaBackoffsDirect =
OPENTHREAD_CONFIG_MAC_MAX_CSMA_BACKOFFS_DIRECT, ///< macMaxCsmaBackoffs for direct transmissions
kMaxCsmaBackoffsIndirect =
OPENTHREAD_CONFIG_MAC_MAX_CSMA_BACKOFFS_INDIRECT, ///< macMaxCsmaBackoffs for indirect transmissions
kDefaultMaxFrameRetriesDirect =
OPENTHREAD_CONFIG_MAC_DEFAULT_MAX_FRAME_RETRIES_DIRECT, ///< macDefaultMaxFrameRetries for direct transmissions
kDefaultMaxFrameRetriesIndirect =
OPENTHREAD_CONFIG_MAC_DEFAULT_MAX_FRAME_RETRIES_INDIRECT, ///< macDefaultMaxFrameRetries for indirect
///< transmissions
kTxNumBcast = OPENTHREAD_CONFIG_MAC_TX_NUM_BCAST ///< Number of times each broadcast frame is transmitted
};
/**
* This type defines the function pointer called on receiving an IEEE 802.15.4 Beacon during an Active Scan.
*
*/
typedef otHandleActiveScanResult ActiveScanHandler;
/**
* This type defines an Active Scan result.
*
*/
typedef otActiveScanResult ActiveScanResult;
/**
* This type defines the function pointer which is called during an Energy Scan when the scan result for a channel is
* ready or when the scan completes.
*
*/
typedef otHandleEnergyScanResult EnergyScanHandler;
/**
* This type defines an Energy Scan result.
*
*/
typedef otEnergyScanResult EnergyScanResult;
/**
* This class implements the IEEE 802.15.4 MAC.
*
*/
class Mac : public InstanceLocator
{
friend class ot::Instance;
public:
/**
* This constructor initializes the MAC object.
*
* @param[in] aInstance A reference to the OpenThread instance.
*
*/
explicit Mac(Instance &aInstance);
/**
* This method starts an IEEE 802.15.4 Active Scan.
*
* @param[in] aScanChannels A bit vector indicating which channels to scan. Zero is mapped to all channels.
* @param[in] aScanDuration The time in milliseconds to spend scanning each channel. Zero duration maps to
* default value `kScanDurationDefault` = 300 ms.
* @param[in] aHandler A pointer to a function that is called on receiving an IEEE 802.15.4 Beacon.
* @param[in] aContext A pointer to an arbitrary context (used when invoking `aHandler` callback).
*
* @retval OT_ERROR_NONE Successfully scheduled the Active Scan request.
* @retval OT_ERROR_BUSY Could not schedule the scan (a scan is ongoing or scheduled).
*
*/
otError ActiveScan(uint32_t aScanChannels, uint16_t aScanDuration, ActiveScanHandler aHandler, void *aContext);
/**
* This method starts an IEEE 802.15.4 Energy Scan.
*
* @param[in] aScanChannels A bit vector indicating on which channels to scan. Zero is mapped to all channels.
* @param[in] aScanDuration The time in milliseconds to spend scanning each channel. If the duration is set to
* zero, a single RSSI sample will be taken per channel.
* @param[in] aHandler A pointer to a function called to pass on scan result or indicate scan completion.
* @param[in] aContext A pointer to an arbitrary context (used when invoking @p aHandler callback).
*
* @retval OT_ERROR_NONE Accepted the Energy Scan request.
* @retval OT_ERROR_BUSY Could not start the energy scan.
*
*/
otError EnergyScan(uint32_t aScanChannels, uint16_t aScanDuration, EnergyScanHandler aHandler, void *aContext);
/**
* This method indicates the energy scan for the current channel is complete.
*
* @param[in] aEnergyScanMaxRssi The maximum RSSI encountered on the scanned channel.
*
*/
void EnergyScanDone(int8_t aEnergyScanMaxRssi);
/**
* This method indicates whether or not IEEE 802.15.4 Beacon transmissions are enabled.
*
* @retval TRUE if IEEE 802.15.4 Beacon transmissions are enabled, FALSE otherwise.
*
*/
bool IsBeaconEnabled(void) const { return mBeaconsEnabled; }
/**
* This method enables/disables IEEE 802.15.4 Beacon transmissions.
*
* @param[in] aEnabled TRUE to enable IEEE 802.15.4 Beacon transmissions, FALSE otherwise.
*
*/
void SetBeaconEnabled(bool aEnabled) { mBeaconsEnabled = aEnabled; }
/**
* This method indicates whether or not rx-on-when-idle is enabled.
*
* @retval TRUE If rx-on-when-idle is enabled.
* @retval FALSE If rx-on-when-idle is not enabled.
*/
bool GetRxOnWhenIdle(void) const { return mRxOnWhenIdle; }
/**
* This method sets the rx-on-when-idle mode.
*
* @param[in] aRxOnWhenIdle The rx-on-when-idle mode.
*
*/
void SetRxOnWhenIdle(bool aRxOnWhenIdle);
/**
* This method requests a direct data frame transmission.
*
*/
void RequestDirectFrameTransmission(void);
#if OPENTHREAD_FTD
/**
* This method requests an indirect data frame transmission.
*
*/
void RequestIndirectFrameTransmission(void);
#endif
/**
* This method requests an Out of Band frame for MAC Transmission.
*
* An Out of Band frame is one that was generated outside of OpenThread.
*
* @param[in] aOobFrame A pointer to the frame.
*
* @retval OT_ERROR_NONE Successfully scheduled the frame transmission.
* @retval OT_ERROR_ALREADY MAC layer is busy sending a previously requested frame.
* @retval OT_ERROR_INVALID_STATE The MAC layer is not enabled.
* @retval OT_ERROR_INVALID_ARGS The argument @p aOobFrame is NULL.
*
*/
otError RequestOutOfBandFrameTransmission(otRadioFrame *aOobFrame);
/**
* This method requests transmission of a data poll (MAC Data Request) frame.
*
* @retval OT_ERROR_NONE Data poll transmission request is scheduled successfully.
* @retval OT_ERROR_ALREADY MAC is busy sending earlier poll transmission request.
* @retval OT_ERROR_INVALID_STATE The MAC layer is not enabled.
*
*/
otError RequestDataPollTransmission(void);
/**
* This method returns a reference to the IEEE 802.15.4 Extended Address.
*
* @returns A pointer to the IEEE 802.15.4 Extended Address.
*
*/
const ExtAddress &GetExtAddress(void) const { return mSubMac.GetExtAddress(); }
/**
* This method sets the IEEE 802.15.4 Extended Address.
*
* @param[in] aExtAddress A reference to the IEEE 802.15.4 Extended Address.
*
*/
void SetExtAddress(const ExtAddress &aExtAddress) { mSubMac.SetExtAddress(aExtAddress); }
/**
* This method returns the IEEE 802.15.4 Short Address.
*
* @returns The IEEE 802.15.4 Short Address.
*
*/
ShortAddress GetShortAddress(void) const { return mSubMac.GetShortAddress(); }
/**
* This method sets the IEEE 802.15.4 Short Address.
*
* @param[in] aShortAddress The IEEE 802.15.4 Short Address.
*
*/
void SetShortAddress(ShortAddress aShortAddress) { mSubMac.SetShortAddress(aShortAddress); }
/**
* This method returns the IEEE 802.15.4 PAN Channel.
*
* @returns The IEEE 802.15.4 PAN Channel.
*
*/
uint8_t GetPanChannel(void) const { return mPanChannel; }
/**
* This method sets the IEEE 802.15.4 PAN Channel.
*
* @param[in] aChannel The IEEE 802.15.4 PAN Channel.
*
* @retval OT_ERROR_NONE Successfully set the IEEE 802.15.4 PAN Channel.
* @retval OT_ERROR_INVALID_ARGS The @p aChannel is not in the supported channel mask.
*
*/
otError SetPanChannel(uint8_t aChannel);
/**
* This method sets the temporary IEEE 802.15.4 radio channel.
*
* This method allows user to temporarily change the radio channel and use a different channel (during receive)
* instead of the PAN channel (from `SetPanChannel()`). A call to `ClearTemporaryChannel()` would clear the
* temporary channel and adopt the PAN channel again. The `SetTemporaryChannel()` can be used multiple times in row
* (before a call to `ClearTemporaryChannel()`) to change the temporary channel.
*
* @param[in] aChannel A IEEE 802.15.4 channel.
*
* @retval OT_ERROR_NONE Successfully set the temporary channel
* @retval OT_ERROR_INVALID_ARGS The @p aChannel is not in the supported channel mask.
*
*/
otError SetTemporaryChannel(uint8_t aChannel);
/**
* This method clears the use of a previously set temporary channel and adopts the PAN channel.
*
*/
void ClearTemporaryChannel(void);
/**
* This method returns the supported channel mask.
*
* @returns The supported channel mask.
*
*/
const ChannelMask &GetSupportedChannelMask(void) const { return mSupportedChannelMask; }
/**
* This method sets the supported channel mask
*
* @param[in] aMask The supported channel mask.
*
*/
void SetSupportedChannelMask(const ChannelMask &aMask);
/**
* This method returns the IEEE 802.15.4 Network Name.
*
* @returns The IEEE 802.15.4 Network Name.
*
*/
const NetworkName &GetNetworkName(void) const { return mNetworkName; }
/**
* This method sets the IEEE 802.15.4 Network Name.
*
* @param[in] aNameString A pointer to a string character array. Must be null terminated.
*
* @retval OT_ERROR_NONE Successfully set the IEEE 802.15.4 Network Name.
* @retval OT_ERROR_INVALID_ARGS Given name is too long.
*
*/
otError SetNetworkName(const char *aNameString);
/**
* This method sets the IEEE 802.15.4 Network Name.
*
* @param[in] aNameData A name data (pointer to char buffer and length).
*
* @retval OT_ERROR_NONE Successfully set the IEEE 802.15.4 Network Name.
* @retval OT_ERROR_INVALID_ARGS Given name is too long.
*
*/
otError SetNetworkName(const NameData &aNameData);
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
/**
* This method returns the Thread Domain Name.
*
* @returns The Thread Domain Name.
*
*/
const DomainName &GetDomainName(void) const { return mDomainName; }
/**
* This method sets the Thread Domain Name.
*
* @param[in] aNameString A pointer to a string character array. Must be null terminated.
*
* @retval OT_ERROR_NONE Successfully set the Thread Domain Name.
* @retval OT_ERROR_INVALID_ARGS Given name is too long.
*
*/
otError SetDomainName(const char *aNameString);
/**
* This method sets the Thread Domain Name.
*
* @param[in] aNameData A name data (pointer to char buffer and length).
*
* @retval OT_ERROR_NONE Successfully set the Thread Domain Name.
* @retval OT_ERROR_INVALID_ARGS Given name is too long.
*
*/
otError SetDomainName(const NameData &aNameData);
#endif // (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
/**
* This method returns the IEEE 802.15.4 PAN ID.
*
* @returns The IEEE 802.15.4 PAN ID.
*
*/
PanId GetPanId(void) const { return mPanId; }
/**
* This method sets the IEEE 802.15.4 PAN ID.
*
* @param[in] aPanId The IEEE 802.15.4 PAN ID.
*
*/
void SetPanId(PanId aPanId);
/**
* This method returns the IEEE 802.15.4 Extended PAN Identifier.
*
* @returns The IEEE 802.15.4 Extended PAN Identifier.
*
*/
const ExtendedPanId &GetExtendedPanId(void) const { return mExtendedPanId; }
/**
* This method sets the IEEE 802.15.4 Extended PAN Identifier.
*
* @param[in] aExtendedPanId The IEEE 802.15.4 Extended PAN Identifier.
*
*/
void SetExtendedPanId(const ExtendedPanId &aExtendedPanId);
/**
* This method returns the maximum number of frame retries during direct transmission.
*
* @returns The maximum number of retries during direct transmission.
*
*/
uint8_t GetMaxFrameRetriesDirect(void) const { return mMaxFrameRetriesDirect; }
/**
* This method sets the maximum number of frame retries during direct transmission.
*
* @param[in] aMaxFrameRetriesDirect The maximum number of retries during direct transmission.
*
*/
void SetMaxFrameRetriesDirect(uint8_t aMaxFrameRetriesDirect) { mMaxFrameRetriesDirect = aMaxFrameRetriesDirect; }
#if OPENTHREAD_FTD
/**
* This method returns the maximum number of frame retries during indirect transmission.
*
* @returns The maximum number of retries during indirect transmission.
*
*/
uint8_t GetMaxFrameRetriesIndirect(void) const { return mMaxFrameRetriesIndirect; }
/**
* This method sets the maximum number of frame retries during indirect transmission.
*
* @param[in] aMaxFrameRetriesIndirect The maximum number of retries during indirect transmission.
*
*/
void SetMaxFrameRetriesIndirect(uint8_t aMaxFrameRetriesIndirect)
{
mMaxFrameRetriesIndirect = aMaxFrameRetriesIndirect;
}
#endif
/**
* This method is called to handle a received frame.
*
* @param[in] aFrame A pointer to the received frame, or NULL if the receive operation was aborted.
* @param[in] aError OT_ERROR_NONE when successfully received a frame,
* OT_ERROR_ABORT when reception was aborted and a frame was not received.
*
*/
void HandleReceivedFrame(RxFrame *aFrame, otError aError);
/**
* This method records CCA status (success/failure) for a frame transmission attempt.
*
* @param[in] aCcaSuccess TRUE if the CCA succeeded, FALSE otherwise.
* @param[in] aChannel The channel on which CCA was performed.
*
*/
void RecordCcaStatus(bool aCcaSuccess, uint8_t aChannel);
/**
* This method records the status of a frame transmission attempt, updating MAC counters.
*
* Unlike `HandleTransmitDone` which is called after all transmission attempts of frame to indicate final status
* of a frame transmission request, this method is invoked on all frame transmission attempts.
*
* @param[in] aFrame The transmitted frame.
* @param[in] aAckFrame A pointer to the ACK frame, or NULL if no ACK was received.
* @param[in] aError OT_ERROR_NONE when the frame was transmitted successfully,
* OT_ERROR_NO_ACK when the frame was transmitted but no ACK was received,
* OT_ERROR_CHANNEL_ACCESS_FAILURE tx failed due to activity on the channel,
* OT_ERROR_ABORT when transmission was aborted for other reasons.
* @param[in] aRetryCount Indicates number of transmission retries for this frame.
* @param[in] aWillRetx Indicates whether frame will be retransmitted or not. This is applicable only
* when there was an error in transmission (i.e., `aError` is not NONE).
*
*/
void RecordFrameTransmitStatus(const TxFrame &aFrame,
const RxFrame *aAckFrame,
otError aError,
uint8_t aRetryCount,
bool aWillRetx);
/**
* This method is called to handle transmit events.
*
* @param[in] aFrame The frame that was transmitted.
* @param[in] aAckFrame A pointer to the ACK frame, NULL if no ACK was received.
* @param[in] aError OT_ERROR_NONE when the frame was transmitted successfully,
* OT_ERROR_NO_ACK when the frame was transmitted but no ACK was received,
* OT_ERROR_CHANNEL_ACCESS_FAILURE when the tx failed due to activity on the channel,
* OT_ERROR_ABORT when transmission was aborted for other reasons.
*
*/
void HandleTransmitDone(TxFrame &aFrame, RxFrame *aAckFrame, otError aError);
/**
* This method returns if an active scan is in progress.
*
*/
bool IsActiveScanInProgress(void) const { return (mOperation == kOperationActiveScan) || (mPendingActiveScan); }
/**
* This method returns if an energy scan is in progress.
*
*/
bool IsEnergyScanInProgress(void) const { return (mOperation == kOperationEnergyScan) || (mPendingEnergyScan); }
#if OPENTHREAD_FTD
/**
* This method indicates whether the MAC layer is performing an indirect transmission (in middle of a tx).
*
* @returns TRUE if in middle of an indirect transmission, FALSE otherwise.
*
*/
bool IsPerformingIndirectTransmit(void) const { return (mOperation == kOperationTransmitDataIndirect); }
#endif
/**
* This method returns if the MAC layer is in transmit state.
*
* The MAC layer is in transmit state during CSMA/CA, CCA, transmission of Data, Beacon or Data Request frames and
* receiving of ACK frames. The MAC layer is not in transmit state during transmission of ACK frames or Beacon
* Requests.
*
*/
bool IsInTransmitState(void) const;
/**
* This method registers a callback to provide received raw IEEE 802.15.4 frames.
*
* @param[in] aPcapCallback A pointer to a function that is called when receiving an IEEE 802.15.4 link frame
* or NULL to disable the callback.
* @param[in] aCallbackContext A pointer to application-specific context.
*
*/
void SetPcapCallback(otLinkPcapCallback aPcapCallback, void *aCallbackContext)
{
mSubMac.SetPcapCallback(aPcapCallback, aCallbackContext);
}
/**
* This method indicates whether or not promiscuous mode is enabled at the link layer.
*
* @retval true Promiscuous mode is enabled.
* @retval false Promiscuous mode is not enabled.
*
*/
bool IsPromiscuous(void) const { return mPromiscuous; }
/**
* This method enables or disables the link layer promiscuous mode.
*
* Promiscuous mode keeps the receiver enabled, overriding the value of mRxOnWhenIdle.
*
* @param[in] aPromiscuous true to enable promiscuous mode, or false otherwise.
*
*/
void SetPromiscuous(bool aPromiscuous);
/**
* This method resets mac counters
*
*/
void ResetCounters(void) { memset(&mCounters, 0, sizeof(mCounters)); }
/**
* This method returns the MAC counter.
*
* @returns A reference to the MAC counter.
*
*/
otMacCounters &GetCounters(void) { return mCounters; }
#if OPENTHREAD_CONFIG_MAC_RETRY_SUCCESS_HISTOGRAM_ENABLE
/**
* This method returns the MAC retry histogram for direct transmission.
*
* @param[out] aNumberOfEntries A reference to where the size of returned histogram array is placed.
*
* @returns A pointer to the histogram of retries (in a form of an array).
* The n-th element indicates that the packet has been sent with n-th retry.
*
*/
const uint32_t *GetDirectRetrySuccessHistogram(uint8_t &aNumberOfEntries);
#if OPENTHREAD_FTD
/**
* This method returns the MAC retry histogram for indirect transmission.
*
* @param[out] aNumberOfEntries A reference to where the size of returned histogram array is placed.
*
* @returns A pointer to the histogram of retries (in a form of an array).
* The n-th element indicates that the packet has been sent with n-th retry.
*
*/
const uint32_t *GetIndirectRetrySuccessHistogram(uint8_t &aNumberOfEntries);
#endif
/**
* This method resets MAC retry histogram.
*
*/
void ResetRetrySuccessHistogram(void);
#endif // OPENTHREAD_CONFIG_MAC_RETRY_SUCCESS_HISTOGRAM_ENABLE
/**
* This method returns the noise floor value (currently use the radio receive sensitivity value).
*
* @returns The noise floor value in dBm.
*
*/
int8_t GetNoiseFloor(void) { return mSubMac.GetNoiseFloor(); }
/**
* This method returns the current CCA (Clear Channel Assessment) failure rate.
*
* The rate is maintained over a window of (roughly) last `OPENTHREAD_CONFIG_CCA_FAILURE_RATE_AVERAGING_WINDOW`
* frame transmissions.
*
* @returns The CCA failure rate with maximum value `0xffff` corresponding to 100% failure rate.
*
*/
uint16_t GetCcaFailureRate(void) const { return mCcaSuccessRateTracker.GetFailureRate(); }
/**
* This method Starts/Stops the Link layer. It may only be used when the Netif Interface is down.
*
* @param[in] aEnable The requested State for the MAC layer. true - Start, false - Stop.
*
*/
void SetEnabled(bool aEnable) { mEnabled = aEnable; }
/**
* This method indicates whether or not the link layer is enabled.
*
* @retval true Link layer is enabled.
* @retval false Link layer is not enabled.
*
*/
bool IsEnabled(void) const { return mEnabled; }
private:
enum
{
kInvalidRssiValue = SubMac::kInvalidRssiValue,
kMaxCcaSampleCount = OPENTHREAD_CONFIG_CCA_FAILURE_RATE_AVERAGING_WINDOW,
kMaxAcquisitionId = 0xffff,
};
enum Operation
{
kOperationIdle = 0,
kOperationActiveScan,
kOperationEnergyScan,
kOperationTransmitBeacon,
kOperationTransmitDataDirect,
#if OPENTHREAD_FTD
kOperationTransmitDataIndirect,
#endif
kOperationTransmitPoll,
kOperationWaitingForData,
kOperationTransmitOutOfBandFrame,
};
#if OPENTHREAD_CONFIG_MAC_RETRY_SUCCESS_HISTOGRAM_ENABLE
struct RetryHistogram
{
/**
* Histogram of number of retries for a single direct packet until success
* [0 retry: packet count, 1 retry: packet count, 2 retry : packet count ...
* until max retry limit: packet count]
*
* The size of the array is OPENTHREAD_CONFIG_MAC_RETRY_SUCCESS_HISTOGRAM_MAX_SIZE_COUNT_DIRECT.
*/
uint32_t mTxDirectRetrySuccess[OPENTHREAD_CONFIG_MAC_RETRY_SUCCESS_HISTOGRAM_MAX_SIZE_COUNT_DIRECT];
/**
* Histogram of number of retries for a single indirect packet until success
* [0 retry: packet count, 1 retry: packet count, 2 retry : packet count ...
* until max retry limit: packet count]
*
* The size of the array is OPENTHREAD_CONFIG_MAC_RETRY_SUCCESS_HISTOGRAM_MAX_SIZE_COUNT_INDIRECT.
*/
uint32_t mTxIndirectRetrySuccess[OPENTHREAD_CONFIG_MAC_RETRY_SUCCESS_HISTOGRAM_MAX_SIZE_COUNT_INDIRECT];
};
#endif // OPENTHREAD_CONFIG_MAC_RETRY_SUCCESS_HISTOGRAM_ENABLE
void ProcessTransmitSecurity(TxFrame &aFrame);
otError ProcessReceiveSecurity(RxFrame &aFrame, const Address &aSrcAddr, Neighbor *aNeighbor);
void UpdateIdleMode(void);
void StartOperation(Operation aOperation);
void FinishOperation(void);
void PerformNextOperation(void);
otError PrepareDataRequest(TxFrame &aFrame);
void PrepareBeaconRequest(TxFrame &aFrame);
void PrepareBeacon(TxFrame &aFrame);
bool ShouldSendBeacon(void) const;
bool IsJoinable(void) const;
void BeginTransmit(void);
bool HandleMacCommand(RxFrame &aFrame);
static void HandleTimer(Timer &aTimer);
void HandleTimer(void);
static void HandleOperationTask(Tasklet &aTasklet);
void Scan(Operation aScanOperation, uint32_t aScanChannels, uint16_t aScanDuration);
otError UpdateScanChannel(void);
void PerformActiveScan(void);
void ReportActiveScanResult(const RxFrame *aBeaconFrame);
otError ConvertBeaconToActiveScanResult(const RxFrame *aBeaconFrame, ActiveScanResult &aResult);
void PerformEnergyScan(void);
void ReportEnergyScanResult(int8_t aRssi);
void LogFrameRxFailure(const RxFrame *aFrame, otError aError) const;
void LogFrameTxFailure(const TxFrame &aFrame, otError aError, uint8_t aRetryCount, bool aWillRetx) const;
void LogBeacon(const char *aActionText, const BeaconPayload &aBeaconPayload) const;
#if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
uint8_t GetTimeIeOffset(const Frame &aFrame);
#endif
static const char *OperationToString(Operation aOperation);
static const otMacKey sMode2Key;
static const otExtAddress sMode2ExtAddress;
static const otExtendedPanId sExtendedPanidInit;
static const char sNetworkNameInit[];
static const char sDomainNameInit[];
bool mEnabled : 1;
bool mPendingActiveScan : 1;
bool mPendingEnergyScan : 1;
bool mPendingTransmitBeacon : 1;
bool mPendingTransmitDataDirect : 1;
#if OPENTHREAD_FTD
bool mPendingTransmitDataIndirect : 1;
#endif
bool mPendingTransmitPoll : 1;
bool mPendingTransmitOobFrame : 1;
bool mPendingWaitingForData : 1;
bool mShouldTxPollBeforeData : 1;
bool mRxOnWhenIdle : 1;
bool mPromiscuous : 1;
bool mBeaconsEnabled : 1;
bool mUsingTemporaryChannel : 1;
#if OPENTHREAD_CONFIG_MAC_STAY_AWAKE_BETWEEN_FRAGMENTS
bool mShouldDelaySleep : 1;
bool mDelayingSleep : 1;
#endif
Operation mOperation;
uint8_t mBeaconSequence;
uint8_t mDataSequence;
uint8_t mBroadcastTransmitCount;
PanId mPanId;
uint8_t mPanChannel;
uint8_t mRadioChannel;
ChannelMask mSupportedChannelMask;
ExtendedPanId mExtendedPanId;
NetworkName mNetworkName;
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
DomainName mDomainName;
#endif
uint8_t mScanChannel;
uint16_t mScanDuration;
ChannelMask mScanChannelMask;
uint8_t mMaxFrameRetriesDirect;
#if OPENTHREAD_FTD
uint8_t mMaxFrameRetriesIndirect;
#endif
union
{
ActiveScanHandler mActiveScanHandler;
EnergyScanHandler mEnergyScanHandler;
};
void *mScanHandlerContext;
SubMac mSubMac;
Tasklet mOperationTask;
TimerMilli mTimer;
TxFrame * mOobFrame;
otMacCounters mCounters;
uint32_t mKeyIdMode2FrameCounter;
SuccessRateTracker mCcaSuccessRateTracker;
uint16_t mCcaSampleCount;
#if OPENTHREAD_CONFIG_MAC_RETRY_SUCCESS_HISTOGRAM_ENABLE
RetryHistogram mRetryHistogram;
#endif
#if OPENTHREAD_CONFIG_MAC_FILTER_ENABLE
Filter mFilter;
#endif // OPENTHREAD_CONFIG_MAC_FILTER_ENABLE
};
/**
* @}
*
*/
} // namespace Mac
} // namespace ot
#endif // MAC_HPP_