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 { return Find(GetSubTlvs(), GetNext(), aType); }
85 
FindSubTlv(Type aType,bool aStable) const86 const NetworkDataTlv *PrefixTlv::FindSubTlv(Type aType, bool aStable) const
87 {
88     return Find(GetSubTlvs(), GetNext(), aType, aStable);
89 }
90 
91 //---------------------------------------------------------------------------------------------------------------------
92 // ServiceTlv
93 
Init(uint8_t aServiceId,uint32_t aEnterpriseNumber,const ServiceData & aServiceData)94 void ServiceTlv::Init(uint8_t aServiceId, uint32_t aEnterpriseNumber, const ServiceData &aServiceData)
95 {
96     NetworkDataTlv::Init();
97     SetType(kTypeService);
98 
99     mFlagsServiceId = (aEnterpriseNumber == kThreadEnterpriseNumber) ? kThreadEnterpriseFlag : 0;
100     mFlagsServiceId |= (aServiceId & kServiceIdMask);
101 
102     if (aEnterpriseNumber != kThreadEnterpriseNumber)
103     {
104         mShared.mEnterpriseNumber = BigEndian::HostSwap32(aEnterpriseNumber);
105         mServiceDataLength        = aServiceData.GetLength();
106         aServiceData.CopyBytesTo(&mServiceDataLength + sizeof(uint8_t));
107     }
108     else
109     {
110         mShared.mServiceDataLengthThreadEnterprise = aServiceData.GetLength();
111         aServiceData.CopyBytesTo(&mShared.mServiceDataLengthThreadEnterprise + sizeof(uint8_t));
112     }
113 
114     SetLength(GetFieldsLength());
115 }
116 
117 //---------------------------------------------------------------------------------------------------------------------
118 // TlvIterator
119 
Iterate(NetworkDataTlv::Type aType)120 const NetworkDataTlv *TlvIterator::Iterate(NetworkDataTlv::Type aType)
121 {
122     const NetworkDataTlv *tlv = NetworkDataTlv::Find(mStart, mEnd, aType);
123 
124     VerifyOrExit(tlv != nullptr);
125     mStart = tlv->GetNext();
126 
127 exit:
128     return tlv;
129 }
130 
Iterate(NetworkDataTlv::Type aType,bool aStable)131 const NetworkDataTlv *TlvIterator::Iterate(NetworkDataTlv::Type aType, bool aStable)
132 {
133     const NetworkDataTlv *tlv = NetworkDataTlv::Find(mStart, mEnd, aType, aStable);
134 
135     VerifyOrExit(tlv != nullptr);
136     mStart = tlv->GetNext();
137 
138 exit:
139     return tlv;
140 }
141 
142 } // namespace NetworkData
143 } // namespace ot
144