• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2016-21, 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 implements methods for generating and processing Thread Network Data TLVs.
32  */
33 
34 #include "network_data_tlvs.hpp"
35 
36 namespace ot {
37 namespace NetworkData {
38 
39 //---------------------------------------------------------------------------------------------------------------------
40 // NetworkDataTlv
41 
Find(const NetworkDataTlv * aStart,const NetworkDataTlv * aEnd,Type aType)42 const NetworkDataTlv *NetworkDataTlv::Find(const NetworkDataTlv *aStart, const NetworkDataTlv *aEnd, Type aType)
43 {
44     const NetworkDataTlv *tlv;
45 
46     for (tlv = aStart; (tlv + 1 <= aEnd) && (tlv->GetNext() <= aEnd); tlv = tlv->GetNext())
47     {
48         if (tlv->GetType() == aType)
49         {
50             ExitNow();
51         }
52     }
53 
54     tlv = nullptr;
55 
56 exit:
57     return tlv;
58 }
59 
Find(const NetworkDataTlv * aStart,const NetworkDataTlv * aEnd,Type aType,bool aStable)60 const NetworkDataTlv *NetworkDataTlv::Find(const NetworkDataTlv *aStart,
61                                            const NetworkDataTlv *aEnd,
62                                            Type                  aType,
63                                            bool                  aStable)
64 {
65     const NetworkDataTlv *tlv;
66 
67     for (tlv = aStart; (tlv + 1 <= aEnd) && (tlv->GetNext() <= aEnd); tlv = tlv->GetNext())
68     {
69         if ((tlv->GetType() == aType) && (tlv->IsStable() == aStable))
70         {
71             ExitNow();
72         }
73     }
74 
75     tlv = nullptr;
76 
77 exit:
78     return tlv;
79 }
80 
81 //---------------------------------------------------------------------------------------------------------------------
82 // PrefixTlv
83 
FindSubTlv(Type aType) const84 const NetworkDataTlv *PrefixTlv::FindSubTlv(Type aType) const
85 {
86     return Find(GetSubTlvs(), GetNext(), aType);
87 }
88 
FindSubTlv(Type aType,bool aStable) const89 const NetworkDataTlv *PrefixTlv::FindSubTlv(Type aType, bool aStable) const
90 {
91     return Find(GetSubTlvs(), GetNext(), aType, aStable);
92 }
93 
94 //---------------------------------------------------------------------------------------------------------------------
95 // ServiceTlv
96 
Init(uint8_t aServiceId,uint32_t aEnterpriseNumber,const ServiceData & aServiceData)97 void ServiceTlv::Init(uint8_t aServiceId, uint32_t aEnterpriseNumber, const ServiceData &aServiceData)
98 {
99     NetworkDataTlv::Init();
100     SetType(kTypeService);
101 
102     mFlagsServiceId = (aEnterpriseNumber == kThreadEnterpriseNumber) ? kThreadEnterpriseFlag : 0;
103     mFlagsServiceId |= (aServiceId & kServiceIdMask);
104 
105     if (aEnterpriseNumber != kThreadEnterpriseNumber)
106     {
107         mShared.mEnterpriseNumber = HostSwap32(aEnterpriseNumber);
108         mServiceDataLength        = aServiceData.GetLength();
109         aServiceData.CopyBytesTo(&mServiceDataLength + sizeof(uint8_t));
110     }
111     else
112     {
113         mShared.mServiceDataLengthThreadEnterprise = aServiceData.GetLength();
114         aServiceData.CopyBytesTo(&mShared.mServiceDataLengthThreadEnterprise + sizeof(uint8_t));
115     }
116 
117     SetLength(GetFieldsLength());
118 }
119 
120 //---------------------------------------------------------------------------------------------------------------------
121 // TlvIterator
122 
Iterate(NetworkDataTlv::Type aType)123 const NetworkDataTlv *TlvIterator::Iterate(NetworkDataTlv::Type aType)
124 {
125     const NetworkDataTlv *tlv = NetworkDataTlv::Find(mStart, mEnd, aType);
126 
127     VerifyOrExit(tlv != nullptr);
128     mStart = tlv->GetNext();
129 
130 exit:
131     return tlv;
132 }
133 
Iterate(NetworkDataTlv::Type aType,bool aStable)134 const NetworkDataTlv *TlvIterator::Iterate(NetworkDataTlv::Type aType, bool aStable)
135 {
136     const NetworkDataTlv *tlv = NetworkDataTlv::Find(mStart, mEnd, aType, aStable);
137 
138     VerifyOrExit(tlv != nullptr);
139     mStart = tlv->GetNext();
140 
141 exit:
142     return tlv;
143 }
144 
145 } // namespace NetworkData
146 } // namespace ot
147