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 includes definitions for managing the Network Name. 32 * 33 */ 34 35 #ifndef MESHCOP_NETWORK_NAME_HPP_ 36 #define MESHCOP_NETWORK_NAME_HPP_ 37 38 #include "openthread-core-config.h" 39 40 #include <openthread/dataset.h> 41 42 #include "common/as_core_type.hpp" 43 #include "common/data.hpp" 44 #include "common/equatable.hpp" 45 #include "common/locator.hpp" 46 #include "common/non_copyable.hpp" 47 #include "common/string.hpp" 48 49 namespace ot { 50 namespace MeshCoP { 51 52 /** 53 * This class represents a name string as data (pointer to a char buffer along with a length). 54 * 55 * @note The char array does NOT need to be null terminated. 56 * 57 */ 58 class NameData : private Data<kWithUint8Length> 59 { 60 friend class NetworkName; 61 62 public: 63 /** 64 * This constructor initializes the NameData object. 65 * 66 * @param[in] aBuffer A pointer to a `char` buffer (does not need to be null terminated). 67 * @param[in] aLength The length (number of chars) in the buffer. 68 * 69 */ NameData(const char * aBuffer,uint8_t aLength)70 NameData(const char *aBuffer, uint8_t aLength) { Init(aBuffer, aLength); } 71 72 /** 73 * This method returns the pointer to char buffer (not necessarily null terminated). 74 * 75 * @returns The pointer to the char buffer. 76 * 77 */ GetBuffer(void) const78 const char *GetBuffer(void) const { return reinterpret_cast<const char *>(GetBytes()); } 79 80 /** 81 * This method returns the length (number of chars in buffer). 82 * 83 * @returns The name length. 84 * 85 */ GetLength(void) const86 uint8_t GetLength(void) const { return Data<kWithUint8Length>::GetLength(); } 87 88 /** 89 * This method copies the name data into a given char buffer with a given size. 90 * 91 * The given buffer is cleared (`memset` to zero) before copying the name into it. The copied string 92 * in @p aBuffer is NOT necessarily null terminated. 93 * 94 * @param[out] aBuffer A pointer to a buffer where to copy the name into. 95 * @param[in] aMaxSize Size of @p aBuffer (maximum number of chars to write into @p aBuffer). 96 * 97 * @returns The actual number of chars copied into @p aBuffer. 98 * 99 */ 100 uint8_t CopyTo(char *aBuffer, uint8_t aMaxSize) const; 101 }; 102 103 /** 104 * This structure represents an Network Name. 105 * 106 */ 107 class NetworkName : public otNetworkName, public Unequatable<NetworkName> 108 { 109 public: 110 static constexpr const char *kNetworkNameInit = "OpenThread"; 111 static constexpr const char *kDomainNameInit = "DefaultDomain"; 112 113 /** 114 * This constant specified the maximum number of chars in Network Name (excludes null char). 115 * 116 */ 117 static constexpr uint8_t kMaxSize = OT_NETWORK_NAME_MAX_SIZE; 118 119 /** 120 * This constructor initializes the Network Name as an empty string. 121 * 122 */ NetworkName(void)123 NetworkName(void) { m8[0] = '\0'; } 124 125 /** 126 * This method gets the Network Name as a null terminated C string. 127 * 128 * @returns The Network Name as a null terminated C string array. 129 * 130 */ GetAsCString(void) const131 const char *GetAsCString(void) const { return m8; } 132 133 /** 134 * This method gets the Network Name as NameData. 135 * 136 * @returns The Network Name as NameData. 137 * 138 */ 139 NameData GetAsData(void) const; 140 141 /** 142 * This method sets the Network Name from a given null terminated C string. 143 * 144 * This method also validates that the given @p aNameString follows UTF-8 encoding and can fit in `kMaxSize` 145 * chars. 146 * 147 * @param[in] aNameString A name C string. 148 * 149 * @retval kErrorNone Successfully set the Network Name. 150 * @retval kErrorAlready The name is already set to the same string. 151 * @retval kErrorInvalidArgs Given name is invalid (too long or does not follow UTF-8 encoding). 152 * 153 */ 154 Error Set(const char *aNameString); 155 156 /** 157 * This method sets the Network Name. 158 * 159 * @param[in] aNameData A reference to name data. 160 * 161 * @retval kErrorNone Successfully set the Network Name. 162 * @retval kErrorAlready The name is already set to the same string. 163 * @retval kErrorInvalidArgs Given name is too long. 164 * 165 */ 166 Error Set(const NameData &aNameData); 167 168 /** 169 * This method overloads operator `==` to evaluate whether or not two given `NetworkName` objects are equal. 170 * 171 * @param[in] aOther The other `NetworkName` to compare with. 172 * 173 * @retval TRUE If the two are equal. 174 * @retval FALSE If the two are not equal. 175 * 176 */ 177 bool operator==(const NetworkName &aOther) const; 178 }; 179 180 #if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2) 181 /** 182 * This type represents a Thread Domain Name. 183 * 184 */ 185 typedef NetworkName DomainName; 186 #endif 187 188 /** 189 * This class manages the Network Name value. 190 * 191 */ 192 class NetworkNameManager : public InstanceLocator, private NonCopyable 193 { 194 public: 195 /** 196 * Constructor. 197 * 198 * @param[in] aInstance A reference to the OpenThread instance. 199 * 200 */ 201 explicit NetworkNameManager(Instance &aInstance); 202 203 /** 204 * This method returns the Network Name. 205 * 206 * @returns The Network Name. 207 * 208 */ GetNetworkName(void) const209 const NetworkName &GetNetworkName(void) const { return mNetworkName; } 210 211 /** 212 * This method sets the Network Name. 213 * 214 * @param[in] aNameString A pointer to a string character array. Must be null terminated. 215 * 216 * @retval kErrorNone Successfully set the Network Name. 217 * @retval kErrorInvalidArgs Given name is too long. 218 * 219 */ 220 Error SetNetworkName(const char *aNameString); 221 222 /** 223 * This method sets the Network Name. 224 * 225 * @param[in] aNameData A name data (pointer to char buffer and length). 226 * 227 * @retval kErrorNone Successfully set the Network Name. 228 * @retval kErrorInvalidArgs Given name is too long. 229 * 230 */ 231 Error SetNetworkName(const NameData &aNameData); 232 233 #if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2) 234 /** 235 * This method returns the Thread Domain Name. 236 * 237 * @returns The Thread Domain Name. 238 * 239 */ GetDomainName(void) const240 const DomainName &GetDomainName(void) const { return mDomainName; } 241 242 /** 243 * This method sets the Thread Domain Name. 244 * 245 * @param[in] aNameString A pointer to a string character array. Must be null terminated. 246 * 247 * @retval kErrorNone Successfully set the Thread Domain Name. 248 * @retval kErrorInvalidArgs Given name is too long. 249 * 250 */ 251 Error SetDomainName(const char *aNameString); 252 253 /** 254 * This method sets the Thread Domain Name. 255 * 256 * @param[in] aNameData A name data (pointer to char buffer and length). 257 * 258 * @retval kErrorNone Successfully set the Thread Domain Name. 259 * @retval kErrorInvalidArgs Given name is too long. 260 * 261 */ 262 Error SetDomainName(const NameData &aNameData); 263 #endif // (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2) 264 265 private: 266 Error SignalNetworkNameChange(Error aError); 267 268 NetworkName mNetworkName; 269 270 #if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2) 271 DomainName mDomainName; 272 #endif 273 }; 274 275 } // namespace MeshCoP 276 277 DefineCoreType(otNetworkName, MeshCoP::NetworkName); 278 279 } // namespace ot 280 281 #endif // MESHCOP_EXTENDED_PANID_HPP_ 282