1 /* 2 * Copyright (c) 2017, 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 the wpan controller service 32 */ 33 34 #ifndef OTBR_WEB_WEB_SERVICE_WPAN_SERVICE_ 35 #define OTBR_WEB_WEB_SERVICE_WPAN_SERVICE_ 36 37 #include "openthread-br/config.h" 38 39 #include <net/if.h> 40 #include <stdint.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 45 #include <json/json.h> 46 #include <json/writer.h> 47 48 #include "common/logging.hpp" 49 #include "utils/hex.hpp" 50 #include "utils/pskc.hpp" 51 #include "web/web-service/ot_client.hpp" 52 53 /** 54 * WPAN parameter constants 55 * 56 */ 57 58 #define OT_EXTENDED_PANID_LENGTH 8 59 #define OT_HARDWARE_ADDRESS_LENGTH 8 60 #define OT_NETWORK_NAME_LENGTH 16 61 #define OT_PANID_LENGTH 2 62 #define OT_PSKC_MAX_LENGTH 16 63 #define OT_HEX_PREFIX_LENGTH 2 64 #define OT_PUBLISH_SERVICE_INTERVAL 20 65 66 namespace otbr { 67 namespace Web { 68 69 /** 70 * This class provides web service to manage WPAN. 71 * 72 */ 73 class WpanService 74 { 75 public: 76 /** 77 * This method handles http request to get information to generate QR code. 78 * 79 * @returns The string to the http response of getting QR code. 80 * 81 */ 82 std::string HandleGetQRCodeRequest(void); 83 84 /** 85 * This method handles the http request to join network. 86 * 87 * @param[in] aJoinRequest A reference to the http request of joining network. 88 * 89 * @returns The string to the http response of joining network. 90 * 91 */ 92 std::string HandleJoinNetworkRequest(const std::string &aJoinRequest); 93 94 /** 95 * This method handles the http request to form network. 96 * 97 * @param[in] aFormRequest A reference to the http request of forming network. 98 * 99 * @returns The string to the http response of forming network. 100 * 101 */ 102 std::string HandleFormNetworkRequest(const std::string &aFormRequest); 103 104 /** 105 * This method handles the http request to add on-mesh prefix. 106 * 107 * @param[in] aAddPrefixRequest A reference to the http request of adding on-mesh prefix. 108 * 109 * @returns The string to the http response of adding on-mesh prefix. 110 * 111 */ 112 std::string HandleAddPrefixRequest(const std::string &aAddPrefixRequest); 113 114 /** 115 * This method handles the http request to delete on-mesh prefix http request. 116 * 117 * @param[in] aDeleteRequest A reference to the http request of deleting on-mesh prefix. 118 * 119 * @returns The string to the http response of deleting on-mesh prefix. 120 * 121 */ 122 std::string HandleDeletePrefixRequest(const std::string &aDeleteRequest); 123 124 /** 125 * This method handles http request to get netowrk status. 126 * 127 * @returns The string to the http response of getting status. 128 * 129 */ 130 std::string HandleStatusRequest(void); 131 132 /** 133 * This method handles http request to get available networks. 134 * 135 * @returns The string to the http response of getting available networks. 136 * 137 */ 138 std::string HandleAvailableNetworkRequest(void); 139 140 /** 141 * This method handles http request to commission device 142 * 143 * @returns The string to the http response of commissioning 144 * 145 */ 146 std::string HandleCommission(const std::string &aCommissionRequest); 147 148 /** 149 * This method sets the Thread interface name. 150 * 151 * @param[in] aIfName The pointer to the Thread interface name. 152 * 153 */ SetInterfaceName(const char * aIfName)154 void SetInterfaceName(const char *aIfName) 155 { 156 strncpy(mIfName, aIfName, sizeof(mIfName) - 1); 157 mIfName[sizeof(mIfName) - 1] = '\0'; 158 } 159 160 /** 161 * This method gets status of wpan service. 162 * 163 * @param[in,out] aNetworkName The pointer to the network name. 164 * @param[in,out] aIfName The pointer to the extended PAN ID. 165 * 166 * @retval kWpanStatus_OK Successfully started the Thread service. 167 * @retval kWpanStatus_Offline Not started the Thread service. 168 * @retval kWpanStatus_Down The Thread service was down. 169 * 170 */ 171 int GetWpanServiceStatus(std::string &aNetworkName, std::string &aExtPanId) const; 172 173 /** 174 * This method starts commissioner and wait for a device to join 175 * 176 * @param[in] aPskd Joiner pskd 177 * @param[in] aNetworkPassword Network password 178 * 179 * @returns The string to the http response of getting available networks. 180 * 181 */ 182 std::string CommissionDevice(const char *aPskd, const char *aNetworkPassword); 183 184 private: 185 int formActiveDataset(otbr::Web::OpenThreadClient &aClient, 186 const std::string & aNetworkKey, 187 const std::string & aNetworkName, 188 const std::string & aPskc, 189 uint16_t aChannel, 190 uint64_t aExtPanId, 191 uint16_t aPanId); 192 int joinActiveDataset(otbr::Web::OpenThreadClient &aClient, 193 const std::string & aNetworkKey, 194 uint16_t aChannel, 195 uint16_t aPanId); 196 static std::string escapeOtCliEscapable(const std::string &aArg); 197 198 WpanNetworkInfo mNetworks[OT_SCANNED_NET_BUFFER_SIZE]; 199 int mNetworksCount; 200 char mIfName[IFNAMSIZ]; 201 std::string mNetworkName; 202 std::string mExtPanId; 203 204 enum 205 { 206 kWpanStatus_Ok = 0, 207 kWpanStatus_Associating, 208 kWpanStatus_Down, 209 kWpanStatus_FormFailed, 210 kWpanStatus_GetPropertyFailed, 211 kWpanStatus_JoinFailed, 212 kWpanStatus_JoinFailed_NotFound, 213 kWpanStatus_JoinFailed_Security, 214 kWpanStatus_LeaveFailed, 215 kWpanStatus_NetworkNotFound, 216 kWpanStatus_Offline, 217 kWpanStatus_ParseRequestFailed, 218 kWpanStatus_ScanFailed, 219 kWpanStatus_SetFailed, 220 kWpanStatus_SetGatewayFailed, 221 kWpanStatus_Uninitialized, 222 }; 223 224 enum 225 { 226 kPropertyType_String = 0, 227 kPropertyType_Data, 228 }; 229 }; 230 231 } // namespace Web 232 } // namespace otbr 233 234 #endif // OTBR_WEB_WEB_SERVICE_WPAN_SERVICE_ 235