• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2016-2019, 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 
29 /**
30  * @file
31  *   This file includes definitions for MAC types.
32  */
33 
34 #ifndef MAC_TYPES_HPP_
35 #define MAC_TYPES_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #include <stdint.h>
40 #include <string.h>
41 
42 #include <openthread/link.h>
43 #include <openthread/thread.h>
44 
45 #include "common/as_core_type.hpp"
46 #include "common/clearable.hpp"
47 #include "common/data.hpp"
48 #include "common/equatable.hpp"
49 #include "common/string.hpp"
50 #include "crypto/storage.hpp"
51 
52 namespace ot {
53 namespace Mac {
54 
55 /**
56  * @addtogroup core-mac
57  *
58  * @{
59  *
60  */
61 
62 /**
63  * This type represents the IEEE 802.15.4 PAN ID.
64  *
65  */
66 typedef otPanId PanId;
67 
68 constexpr PanId kPanIdBroadcast = 0xffff; ///< Broadcast PAN ID.
69 
70 /**
71  * This type represents the IEEE 802.15.4 Short Address.
72  *
73  */
74 typedef otShortAddress ShortAddress;
75 
76 constexpr ShortAddress kShortAddrBroadcast = 0xffff; ///< Broadcast Short Address.
77 constexpr ShortAddress kShortAddrInvalid   = 0xfffe; ///< Invalid Short Address.
78 
79 /**
80  * This function generates a random IEEE 802.15.4 PAN ID.
81  *
82  * @returns A randomly generated IEEE 802.15.4 PAN ID (excluding `kPanIdBroadcast`).
83  *
84  */
85 PanId GenerateRandomPanId(void);
86 
87 /**
88  * This structure represents an IEEE 802.15.4 Extended Address.
89  *
90  */
91 OT_TOOL_PACKED_BEGIN
92 class ExtAddress : public otExtAddress, public Equatable<ExtAddress>, public Clearable<ExtAddress>
93 {
94 public:
95     static constexpr uint16_t kInfoStringSize = 17; ///< Max chars for the info string (`ToString()`).
96 
97     /**
98      * This type defines the fixed-length `String` object returned from `ToString()`.
99      *
100      */
101     typedef String<kInfoStringSize> InfoString;
102 
103     /**
104      * This enumeration type specifies the copy byte order when Extended Address is being copied to/from a buffer.
105      *
106      */
107     enum CopyByteOrder : uint8_t
108     {
109         kNormalByteOrder,  ///< Copy address bytes in normal order (as provided in array buffer).
110         kReverseByteOrder, ///< Copy address bytes in reverse byte order.
111     };
112 
113     /**
114      * This method fills all bytes of address with a given byte value.
115      *
116      * @param[in] aByte A byte value to fill address with.
117      *
118      */
Fill(uint8_t aByte)119     void Fill(uint8_t aByte) { memset(this, aByte, sizeof(*this)); }
120 
121     /**
122      * This method generates a random IEEE 802.15.4 Extended Address.
123      *
124      */
125     void GenerateRandom(void);
126 
127     /**
128      * This method sets the Extended Address from a given byte array.
129      *
130      * @param[in] aBuffer    Pointer to an array containing the Extended Address. `OT_EXT_ADDRESS_SIZE` bytes from
131      *                       buffer are copied to form the Extended Address.
132      * @param[in] aByteOrder The byte order to use when copying the address.
133      *
134      */
Set(const uint8_t * aBuffer,CopyByteOrder aByteOrder=kNormalByteOrder)135     void Set(const uint8_t *aBuffer, CopyByteOrder aByteOrder = kNormalByteOrder)
136     {
137         CopyAddress(m8, aBuffer, aByteOrder);
138     }
139 
140     /**
141      * This method indicates whether or not the Group bit is set.
142      *
143      * @retval TRUE   If the group bit is set.
144      * @retval FALSE  If the group bit is not set.
145      *
146      */
IsGroup(void) const147     bool IsGroup(void) const { return (m8[0] & kGroupFlag) != 0; }
148 
149     /**
150      * This method sets the Group bit.
151      *
152      * @param[in]  aGroup  TRUE if group address, FALSE otherwise.
153      *
154      */
SetGroup(bool aGroup)155     void SetGroup(bool aGroup)
156     {
157         if (aGroup)
158         {
159             m8[0] |= kGroupFlag;
160         }
161         else
162         {
163             m8[0] &= ~kGroupFlag;
164         }
165     }
166 
167     /**
168      * This method toggles the Group bit.
169      *
170      */
ToggleGroup(void)171     void ToggleGroup(void) { m8[0] ^= kGroupFlag; }
172 
173     /**
174      * This method indicates whether or not the Local bit is set.
175      *
176      * @retval TRUE   If the local bit is set.
177      * @retval FALSE  If the local bit is not set.
178      *
179      */
IsLocal(void) const180     bool IsLocal(void) const { return (m8[0] & kLocalFlag) != 0; }
181 
182     /**
183      * This method sets the Local bit.
184      *
185      * @param[in]  aLocal  TRUE if locally administered, FALSE otherwise.
186      *
187      */
SetLocal(bool aLocal)188     void SetLocal(bool aLocal)
189     {
190         if (aLocal)
191         {
192             m8[0] |= kLocalFlag;
193         }
194         else
195         {
196             m8[0] &= ~kLocalFlag;
197         }
198     }
199 
200     /**
201      * This method toggles the Local bit.
202      *
203      */
ToggleLocal(void)204     void ToggleLocal(void) { m8[0] ^= kLocalFlag; }
205 
206     /**
207      * This method copies the Extended Address into a given buffer.
208      *
209      * @param[out] aBuffer     A pointer to a buffer to copy the Extended Address into.
210      * @param[in]  aByteOrder  The byte order to copy the address.
211      *
212      */
CopyTo(uint8_t * aBuffer,CopyByteOrder aByteOrder=kNormalByteOrder) const213     void CopyTo(uint8_t *aBuffer, CopyByteOrder aByteOrder = kNormalByteOrder) const
214     {
215         CopyAddress(aBuffer, m8, aByteOrder);
216     }
217 
218     /**
219      * This method converts an address to a string.
220      *
221      * @returns An `InfoString` containing the string representation of the Extended Address.
222      *
223      */
224     InfoString ToString(void) const;
225 
226 private:
227     static constexpr uint8_t kGroupFlag = (1 << 0);
228     static constexpr uint8_t kLocalFlag = (1 << 1);
229 
230     static void CopyAddress(uint8_t *aDst, const uint8_t *aSrc, CopyByteOrder aByteOrder);
231 } OT_TOOL_PACKED_END;
232 
233 /**
234  * This class represents an IEEE 802.15.4 Short or Extended Address.
235  *
236  */
237 class Address
238 {
239 public:
240     /**
241      * This type defines the fixed-length `String` object returned from `ToString()`.
242      *
243      */
244     typedef ExtAddress::InfoString InfoString;
245 
246     /**
247      * This enumeration specifies the IEEE 802.15.4 Address type.
248      *
249      */
250     enum Type : uint8_t
251     {
252         kTypeNone,     ///< No address.
253         kTypeShort,    ///< IEEE 802.15.4 Short Address.
254         kTypeExtended, ///< IEEE 802.15.4 Extended Address.
255     };
256 
257     /**
258      * This constructor initializes an Address.
259      *
260      */
Address(void)261     Address(void)
262         : mType(kTypeNone)
263     {
264     }
265 
266     /**
267      * This method gets the address type (Short Address, Extended Address, or none).
268      *
269      * @returns The address type.
270      *
271      */
GetType(void) const272     Type GetType(void) const { return mType; }
273 
274     /**
275      * This method indicates whether or not there is an address.
276      *
277      * @returns TRUE if there is no address (i.e. address type is `kTypeNone`), FALSE otherwise.
278      *
279      */
IsNone(void) const280     bool IsNone(void) const { return (mType == kTypeNone); }
281 
282     /**
283      * This method indicates whether or not the Address is a Short Address.
284      *
285      * @returns TRUE if it is a Short Address, FALSE otherwise.
286      *
287      */
IsShort(void) const288     bool IsShort(void) const { return (mType == kTypeShort); }
289 
290     /**
291      * This method indicates whether or not the Address is an Extended Address.
292      *
293      * @returns TRUE if it is an Extended Address, FALSE otherwise.
294      *
295      */
IsExtended(void) const296     bool IsExtended(void) const { return (mType == kTypeExtended); }
297 
298     /**
299      * This method gets the address as a Short Address.
300      *
301      * This method MUST be used only if the address type is Short Address.
302      *
303      * @returns The Short Address.
304      *
305      */
GetShort(void) const306     ShortAddress GetShort(void) const { return mShared.mShortAddress; }
307 
308     /**
309      * This method gets the address as an Extended Address.
310      *
311      * This method MUST be used only if the address type is Extended Address.
312      *
313      * @returns A constant reference to the Extended Address.
314      *
315      */
GetExtended(void) const316     const ExtAddress &GetExtended(void) const { return mShared.mExtAddress; }
317 
318     /**
319      * This method gets the address as an Extended Address.
320      *
321      * This method MUST be used only if the address type is Extended Address.
322      *
323      * @returns A reference to the Extended Address.
324      *
325      */
GetExtended(void)326     ExtAddress &GetExtended(void) { return mShared.mExtAddress; }
327 
328     /**
329      * This method sets the address to none (i.e., clears the address).
330      *
331      * Address type will be updated to `kTypeNone`.
332      *
333      */
SetNone(void)334     void SetNone(void) { mType = kTypeNone; }
335 
336     /**
337      * This method sets the address with a Short Address.
338      *
339      * The type is also updated to indicate that address is Short.
340      *
341      * @param[in]  aShortAddress  A Short Address
342      *
343      */
SetShort(ShortAddress aShortAddress)344     void SetShort(ShortAddress aShortAddress)
345     {
346         mShared.mShortAddress = aShortAddress;
347         mType                 = kTypeShort;
348     }
349 
350     /**
351      * This method sets the address with an Extended Address.
352      *
353      * The type is also updated to indicate that the address is Extended.
354      *
355      * @param[in]  aExtAddress  An Extended Address
356      *
357      */
SetExtended(const ExtAddress & aExtAddress)358     void SetExtended(const ExtAddress &aExtAddress)
359     {
360         mShared.mExtAddress = aExtAddress;
361         mType               = kTypeExtended;
362     }
363 
364     /**
365      * This method sets the address with an Extended Address given as a byte array.
366      *
367      * The type is also updated to indicate that the address is Extended.
368      *
369      * @param[in] aBuffer    Pointer to an array containing the Extended Address. `OT_EXT_ADDRESS_SIZE` bytes from
370      *                       buffer are copied to form the Extended Address.
371      * @param[in] aByteOrder The byte order to copy the address from @p aBuffer.
372      *
373      */
SetExtended(const uint8_t * aBuffer,ExtAddress::CopyByteOrder aByteOrder=ExtAddress::kNormalByteOrder)374     void SetExtended(const uint8_t *aBuffer, ExtAddress::CopyByteOrder aByteOrder = ExtAddress::kNormalByteOrder)
375     {
376         mShared.mExtAddress.Set(aBuffer, aByteOrder);
377         mType = kTypeExtended;
378     }
379 
380     /**
381      * This method indicates whether or not the address is a Short Broadcast Address.
382      *
383      * @returns TRUE if address is Short Broadcast Address, FALSE otherwise.
384      *
385      */
IsBroadcast(void) const386     bool IsBroadcast(void) const { return ((mType == kTypeShort) && (GetShort() == kShortAddrBroadcast)); }
387 
388     /**
389      * This method indicates whether or not the address is a Short Invalid Address.
390      *
391      * @returns TRUE if address is Short Invalid Address, FALSE otherwise.
392      *
393      */
IsShortAddrInvalid(void) const394     bool IsShortAddrInvalid(void) const { return ((mType == kTypeShort) && (GetShort() == kShortAddrInvalid)); }
395 
396     /**
397      * This method converts an address to a null-terminated string
398      *
399      * @returns A `String` representing the address.
400      *
401      */
402     InfoString ToString(void) const;
403 
404 private:
405     union
406     {
407         ShortAddress mShortAddress; ///< The IEEE 802.15.4 Short Address.
408         ExtAddress   mExtAddress;   ///< The IEEE 802.15.4 Extended Address.
409     } mShared;
410 
411     Type mType; ///< The address type (Short, Extended, or none).
412 };
413 
414 /**
415  * This class represents a MAC key.
416  *
417  */
418 OT_TOOL_PACKED_BEGIN
419 class Key : public otMacKey, public Equatable<Key>, public Clearable<Key>
420 {
421 public:
422     static constexpr uint16_t kSize = OT_MAC_KEY_SIZE; ///< Key size in bytes.
423 
424     /**
425      * This method gets a pointer to the bytes array containing the key
426      *
427      * @returns A pointer to the byte array containing the key.
428      *
429      */
GetBytes(void) const430     const uint8_t *GetBytes(void) const { return m8; }
431 
432 } OT_TOOL_PACKED_END;
433 
434 /**
435  * This type represents a MAC Key Ref used by PSA.
436  *
437  */
438 typedef otMacKeyRef KeyRef;
439 
440 /**
441  * This class represents a MAC Key Material.
442  *
443  */
444 class KeyMaterial : public otMacKeyMaterial, public Unequatable<KeyMaterial>
445 {
446 public:
447     /**
448      * This constructor initializes a `KeyMaterial`.
449      *
450      */
KeyMaterial(void)451     KeyMaterial(void)
452     {
453         GetKey().Clear();
454 #if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
455         SetKeyRef(kInvalidKeyRef);
456 #endif
457     }
458 
459 #if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
460     /**
461      * This method overload `=` operator to assign the `KeyMaterial` from another one.
462      *
463      * If the `KeyMaterial` currently stores a valid and different `KeyRef`, the assignment of new value will ensure to
464      * delete the previous one before using the new `KeyRef` from @p aOther.
465      *
466      * @param[in] aOther  aOther  The other `KeyMaterial` instance to assign from.
467      *
468      * @returns A reference to the current `KeyMaterial`
469      *
470      */
471     KeyMaterial &operator=(const KeyMaterial &aOther);
472 
473     KeyMaterial(const KeyMaterial &) = delete;
474 #endif
475 
476     /**
477      *  This method clears the `KeyMaterial`.
478      *
479      * Under `OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE`, if the `KeyMaterial` currently stores a valid previous
480      * `KeyRef`, the `Clear()` call will ensure to delete the previous `KeyRef` and set it to `kInvalidKeyRef`.
481      *
482      */
483     void Clear(void);
484 
485 #if !OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
486     /**
487      * This method gets the literal `Key`.
488      *
489      * @returns The literal `Key`
490      *
491      */
GetKey(void) const492     const Key &GetKey(void) const { return static_cast<const Key &>(mKeyMaterial.mKey); }
493 
494 #else
495     /**
496      * This method gets the stored `KeyRef`
497      *
498      * @returns The `KeyRef`
499      *
500      */
GetKeyRef(void) const501     KeyRef GetKeyRef(void) const { return mKeyMaterial.mKeyRef; }
502 #endif
503 
504     /**
505      * This method sets the `KeyMaterial` from a given Key.
506      *
507      * If the `KeyMaterial` currently stores a valid `KeyRef`, the `SetFrom()` call will ensure to delete the previous
508      * one before creating and using a new `KeyRef` associated with the new `Key`.
509      *
510      * @param[in] aKey           A reference to the new key.
511      * @param[in] aIsExportable  Boolean indicating if the key is exportable (this is only applicable under
512      *                           `OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE` config).
513      *
514      */
515     void SetFrom(const Key &aKey, bool aIsExportable = false);
516 
517     /**
518      * This method extracts the literal key from `KeyMaterial`
519      *
520      * @param[out] aKey  A reference to the output the key.
521      *
522      */
523     void ExtractKey(Key &aKey);
524 
525     /**
526      * This method converts `KeyMaterial` to a `Crypto::Key`.
527      *
528      * @param[out]  aCryptoKey  A reference to a `Crypto::Key` to populate.
529      *
530      */
531     void ConvertToCryptoKey(Crypto::Key &aCryptoKey) const;
532 
533     /**
534      * This method overloads operator `==` to evaluate whether or not two `KeyMaterial` instances are equal.
535      *
536      * @param[in]  aOther  The other `KeyMaterial` instance to compare with.
537      *
538      * @retval TRUE   If the two `KeyMaterial` instances are equal.
539      * @retval FALSE  If the two `KeyMaterial` instances are not equal.
540      *
541      */
542     bool operator==(const KeyMaterial &aOther) const;
543 
544 private:
545 #if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
546     static constexpr KeyRef kInvalidKeyRef = Crypto::Storage::kInvalidKeyRef;
547 
548     void DestroyKey(void);
SetKeyRef(KeyRef aKeyRef)549     void SetKeyRef(KeyRef aKeyRef) { mKeyMaterial.mKeyRef = aKeyRef; }
550 #endif
GetKey(void)551     Key &GetKey(void) { return static_cast<Key &>(mKeyMaterial.mKey); }
SetKey(const Key & aKey)552     void SetKey(const Key &aKey) { mKeyMaterial.mKey = aKey; }
553 };
554 
555 #if OPENTHREAD_CONFIG_MULTI_RADIO
556 
557 /**
558  * This enumeration defines the radio link types.
559  *
560  */
561 enum RadioType : uint8_t
562 {
563 #if OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE
564     kRadioTypeIeee802154, ///< IEEE 802.15.4 (2.4GHz) link type.
565 #endif
566 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
567     kRadioTypeTrel, ///< Thread Radio Encapsulation link type.
568 #endif
569 };
570 
571 /**
572  * This constant specifies the number of supported radio link types.
573  *
574  */
575 constexpr uint8_t kNumRadioTypes = (((OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE) ? 1 : 0) +
576                                     ((OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE) ? 1 : 0));
577 
578 /**
579  * This class represents a set of radio links.
580  *
581  */
582 class RadioTypes
583 {
584 public:
585     static constexpr uint16_t kInfoStringSize = 32; ///< Max chars for the info string (`ToString()`).
586 
587     /**
588      * This type defines the fixed-length `String` object returned from `ToString()`.
589      *
590      */
591     typedef String<kInfoStringSize> InfoString;
592 
593     /**
594      * This static class variable defines an array containing all supported radio link types.
595      *
596      */
597     static const RadioType kAllRadioTypes[kNumRadioTypes];
598 
599     /**
600      * This constructor initializes a `RadioTypes` object as empty set
601      *
602      */
RadioTypes(void)603     RadioTypes(void)
604         : mBitMask(0)
605     {
606     }
607 
608     /**
609      * This constructor initializes a `RadioTypes` object with a given bit-mask.
610      *
611      * @param[in] aMask   A bit-mask representing the radio types (the first bit corresponds to radio type 0, and so on)
612      *
613      */
RadioTypes(uint8_t aMask)614     explicit RadioTypes(uint8_t aMask)
615         : mBitMask(aMask)
616     {
617     }
618 
619     /**
620      * This method clears the set.
621      *
622      */
Clear(void)623     void Clear(void) { mBitMask = 0; }
624 
625     /**
626      * This method indicates whether the set is empty or not
627      *
628      * @returns TRUE if the set is empty, FALSE otherwise.
629      *
630      */
IsEmpty(void) const631     bool IsEmpty(void) const { return (mBitMask == 0); }
632 
633     /**
634      *  This method indicates whether the set contains only a single radio type.
635      *
636      * @returns TRUE if the set contains a single radio type, FALSE otherwise.
637      *
638      */
ContainsSingleRadio(void) const639     bool ContainsSingleRadio(void) const { return !IsEmpty() && ((mBitMask & (mBitMask - 1)) == 0); }
640 
641     /**
642      * This method indicates whether or not the set contains a given radio type.
643      *
644      * @param[in] aType  A radio link type.
645      *
646      * @returns TRUE if the set contains @p aType, FALSE otherwise.
647      *
648      */
Contains(RadioType aType) const649     bool Contains(RadioType aType) const { return ((mBitMask & BitFlag(aType)) != 0); }
650 
651     /**
652      * This method adds a radio type to the set.
653      *
654      * @param[in] aType  A radio link type.
655      *
656      */
Add(RadioType aType)657     void Add(RadioType aType) { mBitMask |= BitFlag(aType); }
658 
659     /**
660      * This method adds another radio types set to the current one.
661      *
662      * @param[in] aTypes   A radio link type set to add.
663      *
664      */
Add(RadioTypes aTypes)665     void Add(RadioTypes aTypes) { mBitMask |= aTypes.mBitMask; }
666 
667     /**
668      * This method adds all radio types supported by device to the set.
669      *
670      */
671     void AddAll(void);
672 
673     /**
674      * This method removes a given radio type from the set.
675      *
676      * @param[in] aType  A radio link type.
677      *
678      */
Remove(RadioType aType)679     void Remove(RadioType aType) { mBitMask &= ~BitFlag(aType); }
680 
681     /**
682      * This method gets the radio type set as a bitmask.
683      *
684      * The first bit in the mask corresponds to first radio type (radio type with value zero), and so on.
685      *
686      * @returns A bitmask representing the set of radio types.
687      *
688      */
GetAsBitMask(void) const689     uint8_t GetAsBitMask(void) const { return mBitMask; }
690 
691     /**
692      * This method overloads operator `-` to return a new set which is the set difference between current set and
693      * a given set.
694      *
695      * @param[in] aOther  Another radio type set.
696      *
697      * @returns A new set which is set difference between current one and @p aOther.
698      *
699      */
operator -(const RadioTypes & aOther) const700     RadioTypes operator-(const RadioTypes &aOther) const { return RadioTypes(mBitMask & ~aOther.mBitMask); }
701 
702     /**
703      * This method converts the radio set to human-readable string.
704      *
705      * @return A string representation of the set of radio types.
706      *
707      */
708     InfoString ToString(void) const;
709 
710 private:
BitFlag(RadioType aType)711     static uint8_t BitFlag(RadioType aType) { return static_cast<uint8_t>(1U << static_cast<uint8_t>(aType)); }
712 
713     uint8_t mBitMask;
714 };
715 
716 /**
717  * This function converts a link type to a string
718  *
719  * @param[in] aRadioType  A link type value.
720  *
721  * @returns A string representation of the link type.
722  *
723  */
724 const char *RadioTypeToString(RadioType aRadioType);
725 
726 #endif // OPENTHREAD_CONFIG_MULTI_RADIO
727 
728 /**
729  * This class represents Link Frame Counters for all supported radio links.
730  *
731  */
732 class LinkFrameCounters
733 {
734 public:
735     /**
736      * This method resets all counters (set them all to zero).
737      *
738      */
Reset(void)739     void Reset(void) { SetAll(0); }
740 
741 #if OPENTHREAD_CONFIG_MULTI_RADIO
742 
743     /**
744      * This method gets the link Frame Counter for a given radio link.
745      *
746      * @param[in] aRadioType  A radio link type.
747      *
748      * @returns The Link Frame Counter for radio link @p aRadioType.
749      *
750      */
751     uint32_t Get(RadioType aRadioType) const;
752 
753     /**
754      * This method sets the Link Frame Counter for a given radio link.
755      *
756      * @param[in] aRadioType  A radio link type.
757      * @param[in] aCounter    The new counter value.
758      *
759      */
760     void Set(RadioType aRadioType, uint32_t aCounter);
761 
762 #else
763 
764     /**
765      * This method gets the Link Frame Counter value.
766      *
767      * @return The Link Frame Counter value.
768      *
769      */
Get(void) const770     uint32_t Get(void) const
771 #if OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE
772     {
773         return m154Counter;
774     }
775 #elif OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
776     {
777         return mTrelCounter;
778     }
779 #endif
780 
781     /**
782      * This method sets the Link Frame Counter for a given radio link.
783      *
784      * @param[in] aCounter    The new counter value.
785      *
786      */
Set(uint32_t aCounter)787     void Set(uint32_t aCounter)
788 #if OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE
789     {
790         m154Counter = aCounter;
791     }
792 #elif OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
793     {
794         mTrelCounter = aCounter;
795     }
796 #endif
797 
798 #endif // OPENTHREAD_CONFIG_MULTI_RADIO
799 
800 #if OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE
801     /**
802      * This method gets the Link Frame Counter for 802.15.4 radio link.
803      *
804      * @returns The Link Frame Counter for 802.15.4 radio link.
805      *
806      */
Get154(void) const807     uint32_t Get154(void) const { return m154Counter; }
808 
809     /**
810      * This method sets the Link Frame Counter for 802.15.4 radio link.
811      *
812      * @param[in] aCounter   The new counter value.
813      *
814      */
Set154(uint32_t aCounter)815     void Set154(uint32_t aCounter) { m154Counter = aCounter; }
816 #endif
817 
818 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
819     /**
820      * This method gets the Link Frame Counter for TREL radio link.
821      *
822      * @returns The Link Frame Counter for TREL radio link.
823      *
824      */
GetTrel(void) const825     uint32_t GetTrel(void) const { return mTrelCounter; }
826 
827     /**
828      * This method increments the Link Frame Counter for TREL radio link.
829      *
830      */
IncrementTrel(void)831     void IncrementTrel(void) { mTrelCounter++; }
832 #endif
833 
834     /**
835      * This method gets the maximum Link Frame Counter among all supported radio links.
836      *
837      * @return The maximum Link frame Counter among all supported radio links.
838      *
839      */
840     uint32_t GetMaximum(void) const;
841 
842     /**
843      * This method sets the Link Frame Counter value for all radio links.
844      *
845      * @param[in]  aCounter  The Link Frame Counter value.
846      *
847      */
848     void SetAll(uint32_t aCounter);
849 
850 private:
851 #if OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE
852     uint32_t m154Counter;
853 #endif
854 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
855     uint32_t mTrelCounter;
856 #endif
857 };
858 
859 /**
860  * @}
861  *
862  */
863 
864 } // namespace Mac
865 
866 DefineCoreType(otExtAddress, Mac::ExtAddress);
867 DefineCoreType(otMacKey, Mac::Key);
868 
869 } // namespace ot
870 
871 #endif // MAC_TYPES_HPP_
872