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 such as Address, Extended PAN Identifier, Network Name, etc.
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/clearable.hpp"
46 #include "common/equatable.hpp"
47 #include "common/string.hpp"
48 
49 namespace ot {
50 namespace Mac {
51 
52 /**
53  * @addtogroup core-mac
54  *
55  * @{
56  *
57  */
58 
59 /**
60  * This type represents the IEEE 802.15.4 PAN ID.
61  *
62  */
63 typedef otPanId PanId;
64 
65 constexpr PanId kPanIdBroadcast = 0xffff; ///< Broadcast PAN ID.
66 
67 /**
68  * This type represents the IEEE 802.15.4 Short Address.
69  *
70  */
71 typedef otShortAddress ShortAddress;
72 
73 constexpr ShortAddress kShortAddrBroadcast = 0xffff; ///< Broadcast Short Address.
74 constexpr ShortAddress kShortAddrInvalid   = 0xfffe; ///< Invalid Short Address.
75 
76 /**
77  * This function generates a random IEEE 802.15.4 PAN ID.
78  *
79  * @returns A randomly generated IEEE 802.15.4 PAN ID (excluding `kPanIdBroadcast`).
80  *
81  */
82 PanId GenerateRandomPanId(void);
83 
84 /**
85  * This structure represents an IEEE 802.15.4 Extended Address.
86  *
87  */
88 OT_TOOL_PACKED_BEGIN
89 class ExtAddress : public otExtAddress, public Equatable<ExtAddress>, public Clearable<ExtAddress>
90 {
91 public:
92     static constexpr uint16_t kInfoStringSize = 17; ///< Max chars for the info string (`ToString()`).
93 
94     /**
95      * This type defines the fixed-length `String` object returned from `ToString()`.
96      *
97      */
98     typedef String<kInfoStringSize> InfoString;
99 
100     /**
101      * This enumeration type specifies the copy byte order when Extended Address is being copied to/from a buffer.
102      *
103      */
104     enum CopyByteOrder : uint8_t
105     {
106         kNormalByteOrder,  ///< Copy address bytes in normal order (as provided in array buffer).
107         kReverseByteOrder, ///< Copy address bytes in reverse byte order.
108     };
109 
110     /**
111      * This method fills all bytes of address with a given byte value.
112      *
113      * @param[in] aByte A byte value to fill address with.
114      *
115      */
Fill(uint8_t aByte)116     void Fill(uint8_t aByte) { memset(this, aByte, sizeof(*this)); }
117 
118     /**
119      * This method generates a random IEEE 802.15.4 Extended Address.
120      *
121      */
122     void GenerateRandom(void);
123 
124     /**
125      * This method sets the Extended Address from a given byte array.
126      *
127      * @param[in] aBuffer    Pointer to an array containing the Extended Address. `OT_EXT_ADDRESS_SIZE` bytes from
128      *                       buffer are copied to form the Extended Address.
129      * @param[in] aByteOrder The byte order to use when copying the address.
130      *
131      */
Set(const uint8_t * aBuffer,CopyByteOrder aByteOrder=kNormalByteOrder)132     void Set(const uint8_t *aBuffer, CopyByteOrder aByteOrder = kNormalByteOrder)
133     {
134         CopyAddress(m8, aBuffer, aByteOrder);
135     }
136 
137     /**
138      * This method indicates whether or not the Group bit is set.
139      *
140      * @retval TRUE   If the group bit is set.
141      * @retval FALSE  If the group bit is not set.
142      *
143      */
IsGroup(void) const144     bool IsGroup(void) const { return (m8[0] & kGroupFlag) != 0; }
145 
146     /**
147      * This method sets the Group bit.
148      *
149      * @param[in]  aGroup  TRUE if group address, FALSE otherwise.
150      *
151      */
SetGroup(bool aGroup)152     void SetGroup(bool aGroup)
153     {
154         if (aGroup)
155         {
156             m8[0] |= kGroupFlag;
157         }
158         else
159         {
160             m8[0] &= ~kGroupFlag;
161         }
162     }
163 
164     /**
165      * This method toggles the Group bit.
166      *
167      */
ToggleGroup(void)168     void ToggleGroup(void) { m8[0] ^= kGroupFlag; }
169 
170     /**
171      * This method indicates whether or not the Local bit is set.
172      *
173      * @retval TRUE   If the local bit is set.
174      * @retval FALSE  If the local bit is not set.
175      *
176      */
IsLocal(void) const177     bool IsLocal(void) const { return (m8[0] & kLocalFlag) != 0; }
178 
179     /**
180      * This method sets the Local bit.
181      *
182      * @param[in]  aLocal  TRUE if locally administered, FALSE otherwise.
183      *
184      */
SetLocal(bool aLocal)185     void SetLocal(bool aLocal)
186     {
187         if (aLocal)
188         {
189             m8[0] |= kLocalFlag;
190         }
191         else
192         {
193             m8[0] &= ~kLocalFlag;
194         }
195     }
196 
197     /**
198      * This method toggles the Local bit.
199      *
200      */
ToggleLocal(void)201     void ToggleLocal(void) { m8[0] ^= kLocalFlag; }
202 
203     /**
204      * This method copies the Extended Address into a given buffer.
205      *
206      * @param[out] aBuffer     A pointer to a buffer to copy the Extended Address into.
207      * @param[in]  aByteOrder  The byte order to copy the address.
208      *
209      */
CopyTo(uint8_t * aBuffer,CopyByteOrder aByteOrder=kNormalByteOrder) const210     void CopyTo(uint8_t *aBuffer, CopyByteOrder aByteOrder = kNormalByteOrder) const
211     {
212         CopyAddress(aBuffer, m8, aByteOrder);
213     }
214 
215     /**
216      * This method converts an address to a string.
217      *
218      * @returns An `InfoString` containing the string representation of the Extended Address.
219      *
220      */
221     InfoString ToString(void) const;
222 
223 private:
224     static constexpr uint8_t kGroupFlag = (1 << 0);
225     static constexpr uint8_t kLocalFlag = (1 << 1);
226 
227     static void CopyAddress(uint8_t *aDst, const uint8_t *aSrc, CopyByteOrder aByteOrder);
228 } OT_TOOL_PACKED_END;
229 
230 /**
231  * This class represents an IEEE 802.15.4 Short or Extended Address.
232  *
233  */
234 class Address
235 {
236 public:
237     /**
238      * This type defines the fixed-length `String` object returned from `ToString()`.
239      *
240      */
241     typedef ExtAddress::InfoString InfoString;
242 
243     /**
244      * This enumeration specifies the IEEE 802.15.4 Address type.
245      *
246      */
247     enum Type : uint8_t
248     {
249         kTypeNone,     ///< No address.
250         kTypeShort,    ///< IEEE 802.15.4 Short Address.
251         kTypeExtended, ///< IEEE 802.15.4 Extended Address.
252     };
253 
254     /**
255      * This constructor initializes an Address.
256      *
257      */
Address(void)258     Address(void)
259         : mType(kTypeNone)
260     {
261     }
262 
263     /**
264      * This method gets the address type (Short Address, Extended Address, or none).
265      *
266      * @returns The address type.
267      *
268      */
GetType(void) const269     Type GetType(void) const { return mType; }
270 
271     /**
272      * This method indicates whether or not there is an address.
273      *
274      * @returns TRUE if there is no address (i.e. address type is `kTypeNone`), FALSE otherwise.
275      *
276      */
IsNone(void) const277     bool IsNone(void) const { return (mType == kTypeNone); }
278 
279     /**
280      * This method indicates whether or not the Address is a Short Address.
281      *
282      * @returns TRUE if it is a Short Address, FALSE otherwise.
283      *
284      */
IsShort(void) const285     bool IsShort(void) const { return (mType == kTypeShort); }
286 
287     /**
288      * This method indicates whether or not the Address is an Extended Address.
289      *
290      * @returns TRUE if it is an Extended Address, FALSE otherwise.
291      *
292      */
IsExtended(void) const293     bool IsExtended(void) const { return (mType == kTypeExtended); }
294 
295     /**
296      * This method gets the address as a Short Address.
297      *
298      * This method MUST be used only if the address type is Short Address.
299      *
300      * @returns The Short Address.
301      *
302      */
GetShort(void) const303     ShortAddress GetShort(void) const { return mShared.mShortAddress; }
304 
305     /**
306      * This method gets the address as an Extended Address.
307      *
308      * This method MUST be used only if the address type is Extended Address.
309      *
310      * @returns A constant reference to the Extended Address.
311      *
312      */
GetExtended(void) const313     const ExtAddress &GetExtended(void) const { return mShared.mExtAddress; }
314 
315     /**
316      * This method gets the address as an Extended Address.
317      *
318      * This method MUST be used only if the address type is Extended Address.
319      *
320      * @returns A reference to the Extended Address.
321      *
322      */
GetExtended(void)323     ExtAddress &GetExtended(void) { return mShared.mExtAddress; }
324 
325     /**
326      * This method sets the address to none (i.e., clears the address).
327      *
328      * Address type will be updated to `kTypeNone`.
329      *
330      */
SetNone(void)331     void SetNone(void) { mType = kTypeNone; }
332 
333     /**
334      * This method sets the address with a Short Address.
335      *
336      * The type is also updated to indicate that address is Short.
337      *
338      * @param[in]  aShortAddress  A Short Address
339      *
340      */
SetShort(ShortAddress aShortAddress)341     void SetShort(ShortAddress aShortAddress)
342     {
343         mShared.mShortAddress = aShortAddress;
344         mType                 = kTypeShort;
345     }
346 
347     /**
348      * This method sets the address with an Extended Address.
349      *
350      * The type is also updated to indicate that the address is Extended.
351      *
352      * @param[in]  aExtAddress  An Extended Address
353      *
354      */
SetExtended(const ExtAddress & aExtAddress)355     void SetExtended(const ExtAddress &aExtAddress)
356     {
357         mShared.mExtAddress = aExtAddress;
358         mType               = kTypeExtended;
359     }
360 
361     /**
362      * This method sets the address with an Extended Address given as a byte array.
363      *
364      * The type is also updated to indicate that the address is Extended.
365      *
366      * @param[in] aBuffer    Pointer to an array containing the Extended Address. `OT_EXT_ADDRESS_SIZE` bytes from
367      *                       buffer are copied to form the Extended Address.
368      * @param[in] aByteOrder The byte order to copy the address from @p aBuffer.
369      *
370      */
SetExtended(const uint8_t * aBuffer,ExtAddress::CopyByteOrder aByteOrder=ExtAddress::kNormalByteOrder)371     void SetExtended(const uint8_t *aBuffer, ExtAddress::CopyByteOrder aByteOrder = ExtAddress::kNormalByteOrder)
372     {
373         mShared.mExtAddress.Set(aBuffer, aByteOrder);
374         mType = kTypeExtended;
375     }
376 
377     /**
378      * This method indicates whether or not the address is a Short Broadcast Address.
379      *
380      * @returns TRUE if address is Short Broadcast Address, FALSE otherwise.
381      *
382      */
IsBroadcast(void) const383     bool IsBroadcast(void) const { return ((mType == kTypeShort) && (GetShort() == kShortAddrBroadcast)); }
384 
385     /**
386      * This method indicates whether or not the address is a Short Invalid Address.
387      *
388      * @returns TRUE if address is Short Invalid Address, FALSE otherwise.
389      *
390      */
IsShortAddrInvalid(void) const391     bool IsShortAddrInvalid(void) const { return ((mType == kTypeShort) && (GetShort() == kShortAddrInvalid)); }
392 
393     /**
394      * This method converts an address to a null-terminated string
395      *
396      * @returns A `String` representing the address.
397      *
398      */
399     InfoString ToString(void) const;
400 
401 private:
402     union
403     {
404         ShortAddress mShortAddress; ///< The IEEE 802.15.4 Short Address.
405         ExtAddress   mExtAddress;   ///< The IEEE 802.15.4 Extended Address.
406     } mShared;
407 
408     Type mType; ///< The address type (Short, Extended, or none).
409 };
410 
411 /**
412  * This class represents a MAC key.
413  *
414  */
415 OT_TOOL_PACKED_BEGIN
416 class Key : public otMacKey, public Equatable<Key>, public Clearable<Key>
417 {
418 public:
419     static constexpr uint16_t kSize = OT_MAC_KEY_SIZE; ///< Key size in bytes.
420 
421     /**
422      * This method gets a pointer to the buffer containing the key.
423      *
424      * @returns A pointer to the buffer containing the key.
425      *
426      */
GetKey(void) const427     const uint8_t *GetKey(void) const { return m8; }
428 
429 } OT_TOOL_PACKED_END;
430 
431 /**
432  * This structure represents an IEEE 802.15.4 Extended PAN Identifier.
433  *
434  */
435 OT_TOOL_PACKED_BEGIN
436 class ExtendedPanId : public otExtendedPanId, public Equatable<ExtendedPanId>, public Clearable<ExtendedPanId>
437 {
438 public:
439     static constexpr uint16_t kInfoStringSize = 17; ///< Max chars for the info string (`ToString()`).
440 
441     /**
442      * This type defines the fixed-length `String` object returned from `ToString()`.
443      *
444      */
445     typedef String<kInfoStringSize> InfoString;
446 
447     /**
448      * This method converts an address to a string.
449      *
450      * @returns An `InfoString` containing the string representation of the Extended PAN Identifier.
451      *
452      */
453     InfoString ToString(void) const;
454 
455 } OT_TOOL_PACKED_END;
456 
457 /**
458  * This class represents a name string as data (pointer to a char buffer along with a length).
459  *
460  * @note The char array does NOT need to be null terminated.
461  *
462  */
463 class NameData
464 {
465 public:
466     /**
467      * This constructor initializes the NameData object.
468      *
469      * @param[in] aBuffer   A pointer to a `char` buffer (does not need to be null terminated).
470      * @param[in] aLength   The length (number of chars) in the buffer.
471      *
472      */
NameData(const char * aBuffer,uint8_t aLength)473     NameData(const char *aBuffer, uint8_t aLength)
474         : mBuffer(aBuffer)
475         , mLength(aLength)
476     {
477     }
478 
479     /**
480      * This method returns the pointer to char buffer (not necessarily null terminated).
481      *
482      * @returns The pointer to the char buffer.
483      *
484      */
GetBuffer(void) const485     const char *GetBuffer(void) const { return mBuffer; }
486 
487     /**
488      * This method returns the length (number of chars in buffer).
489      *
490      * @returns The name length.
491      *
492      */
GetLength(void) const493     uint8_t GetLength(void) const { return mLength; }
494 
495     /**
496      * This method copies the name data into a given char buffer with a given size.
497      *
498      * The given buffer is cleared (`memset` to zero) before copying the name into it. The copied string
499      * in @p aBuffer is NOT necessarily null terminated.
500      *
501      * @param[out] aBuffer   A pointer to a buffer where to copy the name into.
502      * @param[in]  aMaxSize  Size of @p aBuffer (maximum number of chars to write into @p aBuffer).
503      *
504      * @returns The actual number of chars copied into @p aBuffer.
505      *
506      */
507     uint8_t CopyTo(char *aBuffer, uint8_t aMaxSize) const;
508 
509 private:
510     const char *mBuffer;
511     uint8_t     mLength;
512 };
513 
514 /**
515  * This structure represents an IEEE802.15.4 Network Name.
516  *
517  */
518 class NetworkName : public otNetworkName
519 {
520 public:
521     /**
522      * This constant specified the maximum number of chars in Network Name (excludes null char).
523      *
524      */
525     static constexpr uint8_t kMaxSize = OT_NETWORK_NAME_MAX_SIZE;
526 
527     /**
528      * This constructor initializes the IEEE802.15.4 Network Name as an empty string.
529      *
530      */
NetworkName(void)531     NetworkName(void) { m8[0] = '\0'; }
532 
533     /**
534      * This method gets the IEEE802.15.4 Network Name as a null terminated C string.
535      *
536      * @returns The Network Name as a null terminated C string array.
537      *
538      */
GetAsCString(void) const539     const char *GetAsCString(void) const { return m8; }
540 
541     /**
542      * This method gets the IEEE802.15.4 Network Name as NameData.
543      *
544      * @returns The Network Name as NameData.
545      *
546      */
547     NameData GetAsData(void) const;
548 
549     /**
550      * This method sets the IEEE 802.15.4 Network Name from a given null terminated C string.
551      *
552      * This method also validates that the given @p aNameString follows UTF-8 encoding and can fit in `kMaxSize`
553      * chars.
554      *
555      * @param[in] aNameString      A name C string.
556      *
557      * @retval kErrorNone          Successfully set the IEEE 802.15.4 Network Name.
558      * @retval kErrorAlready       The name is already set to the same string.
559      * @retval kErrorInvalidArgs   Given name is invalid (too long or does not follow UTF-8 encoding).
560      *
561      */
562     Error Set(const char *aNameString);
563 
564     /**
565      * This method sets the IEEE 802.15.4 Network Name.
566      *
567      * @param[in]  aNameData           A reference to name data.
568      *
569      * @retval kErrorNone          Successfully set the IEEE 802.15.4 Network Name.
570      * @retval kErrorAlready       The name is already set to the same string.
571      * @retval kErrorInvalidArgs   Given name is too long.
572      *
573      */
574     Error Set(const NameData &aNameData);
575 
576     /**
577      * This method overloads operator `==` to evaluate whether or not two given `NetworkName` objects are equal.
578      *
579      * @param[in]  aOther  The other `NetworkName` to compare with.
580      *
581      * @retval TRUE   If the two are equal.
582      * @retval FALSE  If the two are not equal.
583      *
584      */
585     bool operator==(const NetworkName &aOther) const;
586 };
587 
588 #if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
589 /**
590  * This type represents a Thread Domain Name.
591  *
592  */
593 typedef NetworkName DomainName;
594 #endif
595 
596 #if OPENTHREAD_CONFIG_MULTI_RADIO
597 
598 /**
599  * This enumeration defines the radio link types.
600  *
601  */
602 enum RadioType : uint8_t
603 {
604 #if OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE
605     kRadioTypeIeee802154, ///< IEEE 802.15.4 (2.4GHz) link type.
606 #endif
607 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
608     kRadioTypeTrel, ///< Thread Radio Encapsulation link type.
609 #endif
610 };
611 
612 /**
613  * This constant specifies the number of supported radio link types.
614  *
615  */
616 constexpr uint8_t kNumRadioTypes = (((OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE) ? 1 : 0) +
617                                     ((OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE) ? 1 : 0));
618 
619 /**
620  * This class represents a set of radio links.
621  *
622  */
623 class RadioTypes
624 {
625 public:
626     static constexpr uint16_t kInfoStringSize = 32; ///< Max chars for the info string (`ToString()`).
627 
628     /**
629      * This type defines the fixed-length `String` object returned from `ToString()`.
630      *
631      */
632     typedef String<kInfoStringSize> InfoString;
633 
634     /**
635      * This static class variable defines an array containing all supported radio link types.
636      *
637      */
638     static const RadioType kAllRadioTypes[kNumRadioTypes];
639 
640     /**
641      * This constructor initializes a `RadioTypes` object as empty set
642      *
643      */
RadioTypes(void)644     RadioTypes(void)
645         : mBitMask(0)
646     {
647     }
648 
649     /**
650      * This constructor initializes a `RadioTypes` object with a given bit-mask.
651      *
652      * @param[in] aMask   A bit-mask representing the radio types (the first bit corresponds to radio type 0, and so on)
653      *
654      */
RadioTypes(uint8_t aMask)655     explicit RadioTypes(uint8_t aMask)
656         : mBitMask(aMask)
657     {
658     }
659 
660     /**
661      * This method clears the set.
662      *
663      */
Clear(void)664     void Clear(void) { mBitMask = 0; }
665 
666     /**
667      * This method indicates whether the set is empty or not
668      *
669      * @returns TRUE if the set is empty, FALSE otherwise.
670      *
671      */
IsEmpty(void) const672     bool IsEmpty(void) const { return (mBitMask == 0); }
673 
674     /**
675      *  This method indicates whether the set contains only a single radio type.
676      *
677      * @returns TRUE if the set contains a single radio type, FALSE otherwise.
678      *
679      */
ContainsSingleRadio(void) const680     bool ContainsSingleRadio(void) const { return !IsEmpty() && ((mBitMask & (mBitMask - 1)) == 0); }
681 
682     /**
683      * This method indicates whether or not the set contains a given radio type.
684      *
685      * @param[in] aType  A radio link type.
686      *
687      * @returns TRUE if the set contains @p aType, FALSE otherwise.
688      *
689      */
Contains(RadioType aType) const690     bool Contains(RadioType aType) const { return ((mBitMask & BitFlag(aType)) != 0); }
691 
692     /**
693      * This method adds a radio type to the set.
694      *
695      * @param[in] aType  A radio link type.
696      *
697      */
Add(RadioType aType)698     void Add(RadioType aType) { mBitMask |= BitFlag(aType); }
699 
700     /**
701      * This method adds another radio types set to the current one.
702      *
703      * @param[in] aTypes   A radio link type set to add.
704      *
705      */
Add(RadioTypes aTypes)706     void Add(RadioTypes aTypes) { mBitMask |= aTypes.mBitMask; }
707 
708     /**
709      * This method adds all radio types supported by device to the set.
710      *
711      */
712     void AddAll(void);
713 
714     /**
715      * This method removes a given radio type from the set.
716      *
717      * @param[in] aType  A radio link type.
718      *
719      */
Remove(RadioType aType)720     void Remove(RadioType aType) { mBitMask &= ~BitFlag(aType); }
721 
722     /**
723      * This method gets the radio type set as a bitmask.
724      *
725      * The first bit in the mask corresponds to first radio type (radio type with value zero), and so on.
726      *
727      * @returns A bitmask representing the set of radio types.
728      *
729      */
GetAsBitMask(void) const730     uint8_t GetAsBitMask(void) const { return mBitMask; }
731 
732     /**
733      * This method overloads operator `-` to return a new set which is the set difference between current set and
734      * a given set.
735      *
736      * @param[in] aOther  Another radio type set.
737      *
738      * @returns A new set which is set difference between current one and @p aOther.
739      *
740      */
operator -(const RadioTypes & aOther) const741     RadioTypes operator-(const RadioTypes &aOther) const { return RadioTypes(mBitMask & ~aOther.mBitMask); }
742 
743     /**
744      * This method converts the radio set to human-readable string.
745      *
746      * @return A string representation of the set of radio types.
747      *
748      */
749     InfoString ToString(void) const;
750 
751 private:
BitFlag(RadioType aType)752     static uint8_t BitFlag(RadioType aType) { return static_cast<uint8_t>(1U << static_cast<uint8_t>(aType)); }
753 
754     uint8_t mBitMask;
755 };
756 
757 /**
758  * This function converts a link type to a string
759  *
760  * @param[in] aRadioType  A link type value.
761  *
762  * @returns A string representation of the link type.
763  *
764  */
765 const char *RadioTypeToString(RadioType aRadioType);
766 
767 #endif // OPENTHREAD_CONFIG_MULTI_RADIO
768 
769 /**
770  * This class represents Link Frame Counters for all supported radio links.
771  *
772  */
773 class LinkFrameCounters
774 {
775 public:
776     /**
777      * This method resets all counters (set them all to zero).
778      *
779      */
Reset(void)780     void Reset(void) { SetAll(0); }
781 
782 #if OPENTHREAD_CONFIG_MULTI_RADIO
783 
784     /**
785      * This method gets the link Frame Counter for a given radio link.
786      *
787      * @param[in] aRadioType  A radio link type.
788      *
789      * @returns The Link Frame Counter for radio link @p aRadioType.
790      *
791      */
792     uint32_t Get(RadioType aRadioType) const;
793 
794     /**
795      * This method sets the Link Frame Counter for a given radio link.
796      *
797      * @param[in] aRadioType  A radio link type.
798      * @param[in] aCounter    The new counter value.
799      *
800      */
801     void Set(RadioType aRadioType, uint32_t aCounter);
802 
803 #else
804 
805     /**
806      * This method gets the Link Frame Counter value.
807      *
808      * @return The Link Frame Counter value.
809      *
810      */
Get(void) const811     uint32_t Get(void) const
812 #if OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE
813     {
814         return m154Counter;
815     }
816 #elif OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
817     {
818         return mTrelCounter;
819     }
820 #endif
821 
822     /**
823      * This method sets the Link Frame Counter for a given radio link.
824      *
825      * @param[in] aCounter    The new counter value.
826      *
827      */
Set(uint32_t aCounter)828     void Set(uint32_t aCounter)
829 #if OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE
830     {
831         m154Counter = aCounter;
832     }
833 #elif OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
834     {
835         mTrelCounter = aCounter;
836     }
837 #endif
838 
839 #endif // OPENTHREAD_CONFIG_MULTI_RADIO
840 
841 #if OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE
842     /**
843      * This method gets the Link Frame Counter for 802.15.4 radio link.
844      *
845      * @returns The Link Frame Counter for 802.15.4 radio link.
846      *
847      */
Get154(void) const848     uint32_t Get154(void) const { return m154Counter; }
849 
850     /**
851      * This method sets the Link Frame Counter for 802.15.4 radio link.
852      *
853      * @param[in] aCounter   The new counter value.
854      *
855      */
Set154(uint32_t aCounter)856     void Set154(uint32_t aCounter) { m154Counter = aCounter; }
857 #endif
858 
859 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
860     /**
861      * This method gets the Link Frame Counter for TREL radio link.
862      *
863      * @returns The Link Frame Counter for TREL radio link.
864      *
865      */
GetTrel(void) const866     uint32_t GetTrel(void) const { return mTrelCounter; }
867 
868     /**
869      * This method increments the Link Frame Counter for TREL radio link.
870      *
871      */
IncrementTrel(void)872     void IncrementTrel(void) { mTrelCounter++; }
873 #endif
874 
875     /**
876      * This method gets the maximum Link Frame Counter among all supported radio links.
877      *
878      * @return The maximum Link frame Counter among all supported radio links.
879      *
880      */
881     uint32_t GetMaximum(void) const;
882 
883     /**
884      * This method sets the Link Frame Counter value for all radio links.
885      *
886      * @param[in]  aCounter  The Link Frame Counter value.
887      *
888      */
889     void SetAll(uint32_t aCounter);
890 
891 private:
892 #if OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE
893     uint32_t m154Counter;
894 #endif
895 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
896     uint32_t mTrelCounter;
897 #endif
898 };
899 
900 /**
901  * @}
902  *
903  */
904 
905 } // namespace Mac
906 } // namespace ot
907 
908 #endif // MAC_TYPES_HPP_
909