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 // TlvIterator
96 
Iterate(NetworkDataTlv::Type aType)97 const NetworkDataTlv *TlvIterator::Iterate(NetworkDataTlv::Type aType)
98 {
99     const NetworkDataTlv *tlv = NetworkDataTlv::Find(mStart, mEnd, aType);
100 
101     VerifyOrExit(tlv != nullptr);
102     mStart = tlv->GetNext();
103 
104 exit:
105     return tlv;
106 }
107 
Iterate(NetworkDataTlv::Type aType,bool aStable)108 const NetworkDataTlv *TlvIterator::Iterate(NetworkDataTlv::Type aType, bool aStable)
109 {
110     const NetworkDataTlv *tlv = NetworkDataTlv::Find(mStart, mEnd, aType, aStable);
111 
112     VerifyOrExit(tlv != nullptr);
113     mStart = tlv->GetNext();
114 
115 exit:
116     return tlv;
117 }
118 
119 } // namespace NetworkData
120 } // namespace ot
121