• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2022, 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 Network Name management.
32  *
33  */
34 
35 #include "network_name.hpp"
36 
37 #include "common/locator_getters.hpp"
38 #include "common/notifier.hpp"
39 
40 namespace ot {
41 namespace MeshCoP {
42 
43 const char NetworkNameManager::sNetworkNameInit[] = "OpenThread";
44 
45 #if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
46 const char NetworkNameManager::sDomainNameInit[] = "DefaultDomain";
47 #endif
48 
CopyTo(char * aBuffer,uint8_t aMaxSize) const49 uint8_t NameData::CopyTo(char *aBuffer, uint8_t aMaxSize) const
50 {
51     MutableData<kWithUint8Length> destData;
52 
53     destData.Init(aBuffer, aMaxSize);
54     destData.ClearBytes();
55     IgnoreError(destData.CopyBytesFrom(*this));
56 
57     return destData.GetLength();
58 }
59 
GetAsData(void) const60 NameData NetworkName::GetAsData(void) const
61 {
62     return NameData(m8, static_cast<uint8_t>(StringLength(m8, kMaxSize + 1)));
63 }
64 
Set(const char * aNameString)65 Error NetworkName::Set(const char *aNameString)
66 {
67     // When setting `NetworkName` from a string, we treat it as `NameData`
68     // with `kMaxSize + 1` chars. `NetworkName::Set(data)` will look
69     // for null char in the data (within its given size) to calculate
70     // the name's length and ensure that the name fits in `kMaxSize`
71     // chars. The `+ 1` ensures that a `aNameString` with length
72     // longer than `kMaxSize` is correctly rejected (returning error
73     // `kErrorInvalidArgs`).
74 
75     Error    error;
76     NameData data(aNameString, kMaxSize + 1);
77 
78     VerifyOrExit(IsValidUtf8String(aNameString), error = kErrorInvalidArgs);
79 
80     error = Set(data);
81 
82 exit:
83     return error;
84 }
85 
Set(const NameData & aNameData)86 Error NetworkName::Set(const NameData &aNameData)
87 {
88     Error    error  = kErrorNone;
89     NameData data   = aNameData;
90     uint8_t  newLen = static_cast<uint8_t>(StringLength(data.GetBuffer(), data.GetLength()));
91 
92     VerifyOrExit((0 < newLen) && (newLen <= kMaxSize), error = kErrorInvalidArgs);
93 
94     data.SetLength(newLen);
95 
96     // Ensure the new name does not match the current one.
97     if (data.MatchesBytesIn(m8) && m8[newLen] == '\0')
98     {
99         ExitNow(error = kErrorAlready);
100     }
101 
102     data.CopyBytesTo(m8);
103     m8[newLen] = '\0';
104 
105 exit:
106     return error;
107 }
108 
operator ==(const NetworkName & aOther) const109 bool NetworkName::operator==(const NetworkName &aOther) const
110 {
111     return GetAsData() == aOther.GetAsData();
112 }
113 
NetworkNameManager(Instance & aInstance)114 NetworkNameManager::NetworkNameManager(Instance &aInstance)
115     : InstanceLocator(aInstance)
116 {
117     IgnoreError(SetNetworkName(sNetworkNameInit));
118 
119 #if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
120     IgnoreError(SetDomainName(sDomainNameInit));
121 #endif
122 }
123 
SetNetworkName(const char * aNameString)124 Error NetworkNameManager::SetNetworkName(const char *aNameString)
125 {
126     return SignalNetworkNameChange(mNetworkName.Set(aNameString));
127 }
128 
SetNetworkName(const NameData & aNameData)129 Error NetworkNameManager::SetNetworkName(const NameData &aNameData)
130 {
131     return SignalNetworkNameChange(mNetworkName.Set(aNameData));
132 }
133 
SignalNetworkNameChange(Error aError)134 Error NetworkNameManager::SignalNetworkNameChange(Error aError)
135 {
136     switch (aError)
137     {
138     case kErrorNone:
139         Get<Notifier>().Signal(kEventThreadNetworkNameChanged);
140         break;
141 
142     case kErrorAlready:
143         Get<Notifier>().SignalIfFirst(kEventThreadNetworkNameChanged);
144         aError = kErrorNone;
145         break;
146 
147     default:
148         break;
149     }
150 
151     return aError;
152 }
153 
154 #if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
SetDomainName(const char * aNameString)155 Error NetworkNameManager::SetDomainName(const char *aNameString)
156 {
157     Error error = mDomainName.Set(aNameString);
158 
159     return (error == kErrorAlready) ? kErrorNone : error;
160 }
161 
SetDomainName(const NameData & aNameData)162 Error NetworkNameManager::SetDomainName(const NameData &aNameData)
163 {
164     Error error = mDomainName.Set(aNameData);
165 
166     return (error == kErrorAlready) ? kErrorNone : error;
167 }
168 #endif // (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
169 
170 } // namespace MeshCoP
171 } // namespace ot
172