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