1 /*
2 * Copyright (c) 2020, 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 Thread URIs.
32 */
33
34 #include "uri_paths.hpp"
35
36 #include "common/binary_search.hpp"
37 #include "common/debug.hpp"
38 #include "common/string.hpp"
39
40 #include <string.h>
41
42 namespace ot {
43
44 namespace UriList {
45
46 struct Entry
47 {
48 const char *mPath;
49
AreInOrderot::UriList::Entry50 constexpr static bool AreInOrder(const Entry &aFirst, const Entry &aSecond)
51 {
52 return AreStringsInOrder(aFirst.mPath, aSecond.mPath);
53 }
54
Compareot::UriList::Entry55 int Compare(const char *aPath) const { return strcmp(aPath, mPath); }
56 };
57
58 // The list of URI paths (MUST be sorted alphabetically)
59 static constexpr Entry kEntries[] = {
60 {"a/ae"}, // (0) kUriAddressError
61 {"a/an"}, // (1) kUriAddressNotify
62 {"a/aq"}, // (2) kUriAddressQuery
63 {"a/ar"}, // (3) kUriAddressRelease
64 {"a/as"}, // (4) kUriAddressSolicit
65 {"a/sd"}, // (5) kUriServerData
66 {"a/yl"}, // (6) kUriAnycastLocate
67 {"b/ba"}, // (7) kUriBackboneAnswer
68 {"b/bmr"}, // (8) kUriBackboneMlr
69 {"b/bq"}, // (9) kUriBackboneQuery
70 {"c/ab"}, // (10) kUriAnnounceBegin
71 {"c/ag"}, // (11) kUriActiveGet
72 {"c/ar"}, // (12) kUriActiveReplace
73 {"c/as"}, // (13) kUriActiveSet
74 {"c/ca"}, // (14) kUriCommissionerKeepAlive
75 {"c/cg"}, // (15) kUriCommissionerGet
76 {"c/cp"}, // (16) kUriCommissionerPetition
77 {"c/cs"}, // (17) kUriCommissionerSet
78 {"c/dc"}, // (18) kUriDatasetChanged
79 {"c/er"}, // (19) kUriEnergyReport
80 {"c/es"}, // (20) kUriEnergyScan
81 {"c/je"}, // (21) kUriJoinerEntrust
82 {"c/jf"}, // (22) kUriJoinerFinalize
83 {"c/la"}, // (23) kUriLeaderKeepAlive
84 {"c/lp"}, // (24) kUriLeaderPetition
85 {"c/pc"}, // (25) kUriPanIdConflict
86 {"c/pg"}, // (26) kUriPendingGet
87 {"c/pq"}, // (27) kUriPanIdQuery
88 {"c/ps"}, // (28) kUriPendingSet
89 {"c/rx"}, // (29) kUriRelayRx
90 {"c/tx"}, // (30) kUriRelayTx
91 {"c/ur"}, // (31) kUriProxyRx
92 {"c/ut"}, // (32) kUriProxyTx
93 {"d/da"}, // (33) kUriDiagnosticGetAnswer
94 {"d/dg"}, // (34) kUriDiagnosticGetRequest
95 {"d/dq"}, // (35) kUriDiagnosticGetQuery
96 {"d/dr"}, // (36) kUriDiagnosticReset
97 {"n/dn"}, // (37) kUriDuaRegistrationNotify
98 {"n/dr"}, // (38) kUriDuaRegistrationRequest
99 {"n/mr"}, // (39) kUriMlr
100 };
101
102 static_assert(BinarySearch::IsSorted(kEntries), "kEntries is not sorted");
103
104 static_assert(0 == kUriAddressError, "kUriAddressError (`a/ae`) is invalid");
105 static_assert(1 == kUriAddressNotify, "kUriAddressNotify (`a/an`) is invalid");
106 static_assert(2 == kUriAddressQuery, "kUriAddressQuery (`a/aq`) is invalid");
107 static_assert(3 == kUriAddressRelease, "kUriAddressRelease (`a/ar`) is invalid");
108 static_assert(4 == kUriAddressSolicit, "kUriAddressSolicit (`a/as`) is invalid");
109 static_assert(5 == kUriServerData, "kUriServerData (`a/sd`) is invalid");
110 static_assert(6 == kUriAnycastLocate, "kUriAnycastLocate (`a/yl`) is invalid");
111 static_assert(7 == kUriBackboneAnswer, "kUriBackboneAnswer (`b/ba`) is invalid");
112 static_assert(8 == kUriBackboneMlr, "kUriBackboneMlr (`b/bmr`) is invalid");
113 static_assert(9 == kUriBackboneQuery, "kUriBackboneQuery (`b/bq`) is invalid");
114 static_assert(10 == kUriAnnounceBegin, "kUriAnnounceBegin (`c/ab`) is invalid");
115 static_assert(11 == kUriActiveGet, "kUriActiveGet (`c/ag`) is invalid");
116 static_assert(12 == kUriActiveReplace, "kUriActiveReplace (`c/ar`) is invalid");
117 static_assert(13 == kUriActiveSet, "kUriActiveSet (`c/as`) is invalid");
118 static_assert(14 == kUriCommissionerKeepAlive, "kUriCommissionerKeepAlive (`c/ca`) is invalid");
119 static_assert(15 == kUriCommissionerGet, "kUriCommissionerGet (`c/cg`) is invalid");
120 static_assert(16 == kUriCommissionerPetition, "kUriCommissionerPetition (`c/cp`) is invalid");
121 static_assert(17 == kUriCommissionerSet, "kUriCommissionerSet (`c/cs`) is invalid");
122 static_assert(18 == kUriDatasetChanged, "kUriDatasetChanged (`c/dc`) is invalid");
123 static_assert(19 == kUriEnergyReport, "kUriEnergyReport (`c/er`) is invalid");
124 static_assert(20 == kUriEnergyScan, "kUriEnergyScan (`c/es`) is invalid");
125 static_assert(21 == kUriJoinerEntrust, "kUriJoinerEntrust (`c/je`) is invalid");
126 static_assert(22 == kUriJoinerFinalize, "kUriJoinerFinalize (`c/jf`) is invalid");
127 static_assert(23 == kUriLeaderKeepAlive, "kUriLeaderKeepAlive (`c/la`) is invalid");
128 static_assert(24 == kUriLeaderPetition, "kUriLeaderPetition (`c/lp`) is invalid");
129 static_assert(25 == kUriPanIdConflict, "kUriPanIdConflict (`c/pc`) is invalid");
130 static_assert(26 == kUriPendingGet, "kUriPendingGet (`c/pg`) is invalid");
131 static_assert(27 == kUriPanIdQuery, "kUriPanIdQuery (`c/pq`) is invalid");
132 static_assert(28 == kUriPendingSet, "kUriPendingSet (`c/ps`) is invalid");
133 static_assert(29 == kUriRelayRx, "kUriRelayRx (`c/rx`) is invalid");
134 static_assert(30 == kUriRelayTx, "kUriRelayTx (`c/tx`) is invalid");
135 static_assert(31 == kUriProxyRx, "kUriProxyRx (`c/ur`) is invalid");
136 static_assert(32 == kUriProxyTx, "kUriProxyTx (`c/ut`) is invalid");
137 static_assert(33 == kUriDiagnosticGetAnswer, "kUriDiagnosticGetAnswer (`d/da`) is invalid");
138 static_assert(34 == kUriDiagnosticGetRequest, "kUriDiagnosticGetRequest (`d/dg`) is invalid");
139 static_assert(35 == kUriDiagnosticGetQuery, "kUriDiagnosticGetQuery (`d/dq`) is invalid");
140 static_assert(36 == kUriDiagnosticReset, "kUriDiagnosticReset (`d/dr`) is invalid");
141 static_assert(37 == kUriDuaRegistrationNotify, "kUriDuaRegistrationNotify (`n/dn`) is invalid");
142 static_assert(38 == kUriDuaRegistrationRequest, "kUriDuaRegistrationRequest (`n/dr`) is invalid");
143 static_assert(39 == kUriMlr, "kUriMlr (`n/mr`) is invalid");
144
145 } // namespace UriList
146
PathForUri(Uri aUri)147 const char *PathForUri(Uri aUri)
148 {
149 OT_ASSERT(aUri != kUriUnknown);
150
151 return UriList::kEntries[aUri].mPath;
152 }
153
UriFromPath(const char * aPath)154 Uri UriFromPath(const char *aPath)
155 {
156 Uri uri = kUriUnknown;
157 const UriList::Entry *entry = BinarySearch::Find(aPath, UriList::kEntries);
158
159 VerifyOrExit(entry != nullptr);
160 uri = static_cast<Uri>(entry - UriList::kEntries);
161
162 exit:
163 return uri;
164 }
165
UriToString(void)166 template <> const char *UriToString<kUriAddressError>(void) { return "AddressError"; }
UriToString(void)167 template <> const char *UriToString<kUriAddressNotify>(void) { return "AddressNotify"; }
UriToString(void)168 template <> const char *UriToString<kUriAddressQuery>(void) { return "AddressQuery"; }
UriToString(void)169 template <> const char *UriToString<kUriAddressRelease>(void) { return "AddressRelease"; }
UriToString(void)170 template <> const char *UriToString<kUriAddressSolicit>(void) { return "AddressSolicit"; }
UriToString(void)171 template <> const char *UriToString<kUriServerData>(void) { return "ServerData"; }
UriToString(void)172 template <> const char *UriToString<kUriAnycastLocate>(void) { return "AnycastLocate"; }
UriToString(void)173 template <> const char *UriToString<kUriBackboneAnswer>(void) { return "BackboneAnswer"; }
UriToString(void)174 template <> const char *UriToString<kUriBackboneMlr>(void) { return "BackboneMlr"; }
UriToString(void)175 template <> const char *UriToString<kUriBackboneQuery>(void) { return "BackboneQuery"; }
UriToString(void)176 template <> const char *UriToString<kUriAnnounceBegin>(void) { return "AnnounceBegin"; }
UriToString(void)177 template <> const char *UriToString<kUriActiveGet>(void) { return "ActiveGet"; }
UriToString(void)178 template <> const char *UriToString<kUriActiveReplace>(void) { return "ActiveReplace"; }
UriToString(void)179 template <> const char *UriToString<kUriActiveSet>(void) { return "ActiveSet"; }
UriToString(void)180 template <> const char *UriToString<kUriCommissionerKeepAlive>(void) { return "CommissionerKeepAlive"; }
UriToString(void)181 template <> const char *UriToString<kUriCommissionerGet>(void) { return "CommissionerGet"; }
UriToString(void)182 template <> const char *UriToString<kUriCommissionerPetition>(void) { return "CommissionerPetition"; }
UriToString(void)183 template <> const char *UriToString<kUriCommissionerSet>(void) { return "CommissionerSet"; }
UriToString(void)184 template <> const char *UriToString<kUriDatasetChanged>(void) { return "DatasetChanged"; }
UriToString(void)185 template <> const char *UriToString<kUriEnergyReport>(void) { return "EnergyReport"; }
UriToString(void)186 template <> const char *UriToString<kUriEnergyScan>(void) { return "EnergyScan"; }
UriToString(void)187 template <> const char *UriToString<kUriJoinerEntrust>(void) { return "JoinerEntrust"; }
UriToString(void)188 template <> const char *UriToString<kUriJoinerFinalize>(void) { return "JoinerFinalize"; }
UriToString(void)189 template <> const char *UriToString<kUriLeaderKeepAlive>(void) { return "LeaderKeepAlive"; }
UriToString(void)190 template <> const char *UriToString<kUriLeaderPetition>(void) { return "LeaderPetition"; }
UriToString(void)191 template <> const char *UriToString<kUriPanIdConflict>(void) { return "PanIdConflict"; }
UriToString(void)192 template <> const char *UriToString<kUriPendingGet>(void) { return "PendingGet"; }
UriToString(void)193 template <> const char *UriToString<kUriPanIdQuery>(void) { return "PanIdQuery"; }
UriToString(void)194 template <> const char *UriToString<kUriPendingSet>(void) { return "PendingSet"; }
UriToString(void)195 template <> const char *UriToString<kUriRelayRx>(void) { return "RelayRx"; }
UriToString(void)196 template <> const char *UriToString<kUriRelayTx>(void) { return "RelayTx"; }
UriToString(void)197 template <> const char *UriToString<kUriProxyRx>(void) { return "ProxyRx"; }
UriToString(void)198 template <> const char *UriToString<kUriProxyTx>(void) { return "ProxyTx"; }
UriToString(void)199 template <> const char *UriToString<kUriDiagnosticGetAnswer>(void) { return "DiagGetAnswer"; }
UriToString(void)200 template <> const char *UriToString<kUriDiagnosticGetRequest>(void) { return "DiagGetRequest"; }
UriToString(void)201 template <> const char *UriToString<kUriDiagnosticGetQuery>(void) { return "DiagGetQuery"; }
UriToString(void)202 template <> const char *UriToString<kUriDiagnosticReset>(void) { return "DiagReset"; }
UriToString(void)203 template <> const char *UriToString<kUriDuaRegistrationNotify>(void) { return "DuaRegNotify"; }
UriToString(void)204 template <> const char *UriToString<kUriDuaRegistrationRequest>(void) { return "DuaRegRequest"; }
UriToString(void)205 template <> const char *UriToString<kUriMlr>(void) { return "Mlr"; }
206
207 } // namespace ot
208