1 /* 2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef OHOS_WIFI_DEVICE_PROXY_H 16 #define OHOS_WIFI_DEVICE_PROXY_H 17 18 #ifdef OHOS_ARCH_LITE 19 #include "iproxy_client.h" 20 #include "serializer.h" 21 #else 22 #include "iremote_proxy.h" 23 #endif 24 #include "i_wifi_device.h" 25 #include "wifi_errcode.h" 26 #include "wifi_msg.h" 27 28 namespace OHOS { 29 namespace Wifi { 30 #ifdef OHOS_ARCH_LITE 31 class WifiDeviceProxy : public IWifiDevice { 32 public: 33 static WifiDeviceProxy *GetInstance(void); 34 static void ReleaseInstance(void); 35 explicit WifiDeviceProxy(); 36 ErrCode Init(void); 37 #else 38 class WifiDeviceProxy : public IRemoteProxy<IWifiDevice> { 39 public: 40 explicit WifiDeviceProxy(const sptr<IRemoteObject> &impl); 41 #endif 42 ~WifiDeviceProxy(); 43 44 /** 45 * @Description Turn on Wi-Fi 46 * 47 * @return ErrCode - operation result 48 */ 49 ErrCode EnableWifi() override; 50 51 /** 52 * @Description Turn off Wi-Fi 53 * 54 * @return ErrCode - operation result 55 */ 56 ErrCode DisableWifi() override; 57 58 /** 59 * @Description create the Wi-Fi protect. 60 * 61 * @param protectType - WifiProtectMode object 62 * @param protectName - the protect name 63 * @return ErrCode - operation result 64 */ 65 ErrCode InitWifiProtect(const WifiProtectType &protectType, const std::string &protectName) override; 66 67 /** 68 * @Description Acquire the Wi-Fi protect mode. 69 * 70 * @param protectMode - WifiProtectMode object 71 * @param protectName - the protect name 72 * @return ErrCode - operation result 73 */ 74 ErrCode GetWifiProtectRef(const WifiProtectMode &protectMode, const std::string &protectName) override; 75 76 /** 77 * @Description Release the Wi-Fi protect mode. 78 * 79 * @param protectName - the protect name 80 * @return ErrCode - operation result 81 */ 82 ErrCode PutWifiProtectRef(const std::string &protectName) override; 83 84 /** 85 * @Description Remove a specified untrusted hotspot configuration. 86 * 87 * @param config - WifiDeviceConfig object 88 * @return ErrCode - operation result 89 */ 90 ErrCode RemoveCandidateConfig(const WifiDeviceConfig &config) override; 91 92 /** 93 * @Description Remove the wifi Untrusted device config equals to input network id 94 * 95 * @param networkId - the untrusted device network id 96 * @return ErrCode - operation result 97 */ 98 ErrCode RemoveCandidateConfig(int networkId) override; 99 100 /** 101 * @Description Add a wifi device configuration. 102 * 103 * @param config - WifiDeviceConfig object 104 * @param result - the device configuration's network id 105 * @param isCandidate - Whether is candidate 106 * @return ErrCode - operation result 107 */ 108 ErrCode AddDeviceConfig(const WifiDeviceConfig &config, int &result, bool isCandidate) override; 109 110 /** 111 * @Description Update a wifi device configuration. 112 * 113 * @param config - WifiDeviceConfig object 114 * @param result - the device configuration's network id after updated 115 * @return ErrCode - operation result 116 */ 117 ErrCode UpdateDeviceConfig(const WifiDeviceConfig &config, int &result) override; 118 119 /** 120 * @Description Remove the wifi device config equals to input network id 121 * 122 * @param networkId - want to remove device config's network id 123 * @return ErrCode - operation result 124 */ 125 ErrCode RemoveDevice(int networkId) override; 126 127 /** 128 * @Description Delete all device configs. 129 * 130 * @return ErrCode - operation result 131 */ 132 ErrCode RemoveAllDevice() override; 133 134 /** 135 * @Description Get all the device configs 136 * 137 * @param result - Get result vector of WifiDeviceConfig 138 * @param isCandidate - Whether is candidate 139 * @return ErrCode - operation result 140 */ 141 ErrCode GetDeviceConfigs(std::vector<WifiDeviceConfig> &result, bool isCandidate) override; 142 143 /** 144 * @Description Enable device config, when set attemptEnable, disable other device config 145 * 146 * @param networkId - need enable device config's network id 147 * @param attemptEnable - if set true, disable other device config 148 * @return ErrCode - operation result 149 */ 150 ErrCode EnableDeviceConfig(int networkId, bool attemptEnable) override; 151 152 /** 153 * @Description Disable Wi-Fi device configuration. 154 * 155 * @param networkId - device config's network id 156 * @return ErrCode - operation result 157 */ 158 ErrCode DisableDeviceConfig(int networkId) override; 159 160 /** 161 * @Description Connecting to a Specified Network 162 * 163 * @param networkId - network id 164 * @param isCandidate - Whether is candidate 165 * @return ErrCode - operation result 166 */ 167 ErrCode ConnectToNetwork(int networkId, bool isCandidate) override; 168 169 /** 170 * @Description Connect To a network base WifiDeviceConfig object 171 * 172 * @param config - WifiDeviceConfig object 173 * @return ErrCode - operation result 174 */ 175 ErrCode ConnectToDevice(const WifiDeviceConfig &config) override; 176 177 /** 178 * @Description Check whether Wi-Fi is connected. 179 * 180 * @param bool - true: connected, false: not connected 181 * @return ErrCode - operation result 182 */ 183 ErrCode IsConnected(bool &isConnected) override; 184 185 /** 186 * @Description Reconnect to the currently active network 187 * 188 * @return ErrCode - operation result 189 */ 190 ErrCode ReConnect() override; 191 192 /** 193 * @Description ReAssociate network 194 * 195 * @return ErrCode - operation result 196 */ 197 ErrCode ReAssociate(void) override; 198 199 /** 200 * @Description Disconnect 201 * 202 * @return ErrCode - operation result 203 */ 204 ErrCode Disconnect(void) override; 205 206 /** 207 * @Description Enable WPS connection 208 * 209 * @param config - WpsConfig object 210 * @return ErrCode - operation result 211 */ 212 ErrCode StartWps(const WpsConfig &config) override; 213 214 /** 215 * @Description Close the WPS connection 216 * 217 * @return ErrCode - operation result 218 */ 219 ErrCode CancelWps(void) override; 220 221 /** 222 * @Description Check whether Wi-Fi is active 223 * 224 * @param bActive - active / inactive 225 * @return ErrCode - operation result 226 */ 227 ErrCode IsWifiActive(bool &bActive) override; 228 229 /** 230 * @Description Get the Wifi State 231 * 232 * @param state - return current wifi state 233 * @return ErrCode - operation result 234 */ 235 ErrCode GetWifiState(int &state) override; 236 237 /** 238 * @Description Obtains the current Wi-Fi connection information 239 * 240 * @param info - WifiLinkedInfo object 241 * @return ErrCode - operation result 242 */ 243 ErrCode GetLinkedInfo(WifiLinkedInfo &info) override; 244 245 /** 246 * @Description Obtaining DHCP Request Information 247 * 248 * @param info - IpInfo object 249 * @return ErrCode - operation result 250 */ 251 ErrCode GetIpInfo(IpInfo &info) override; 252 253 /** 254 * @Description Set the Country Code 255 * 256 * @param countryCode - country code 257 * @return ErrCode - operation result 258 */ 259 ErrCode SetCountryCode(const std::string &countryCode) override; 260 261 /** 262 * @Description Obtains the country code 263 * 264 * @param countryCode - output the country code 265 * @return ErrCode - operation result 266 */ 267 ErrCode GetCountryCode(std::string &countryCode) override; 268 269 /** 270 * @Description Register callback function. 271 * 272 * @param callback - IWifiDeviceCallBack object 273 * @return ErrCode - operation result 274 */ 275 #ifdef OHOS_ARCH_LITE 276 ErrCode RegisterCallBack(const std::shared_ptr<IWifiDeviceCallBack> &callback) override; 277 #else 278 ErrCode RegisterCallBack(const sptr<IWifiDeviceCallBack> &callback) override; 279 #endif 280 281 /** 282 * @Description Get the Signal Level object 283 * 284 * @param rssi - rssi 285 * @param band - band 286 * @param level - return the level 287 * @return ErrCode - operation result 288 */ 289 ErrCode GetSignalLevel(const int &rssi, const int &band, int &level) override; 290 291 /** 292 * @Description Get supported features 293 * 294 * @param features - return supported features 295 * @return ErrCode - operation result 296 */ 297 ErrCode GetSupportedFeatures(long &features) override; 298 299 /** 300 * @Description Get the device MAC address 301 * 302 * @param result - Get device mac String 303 * @return ErrCode - operation result 304 */ 305 ErrCode GetDeviceMacAddress(std::string &result) override; 306 307 /** 308 * @Description set low latency mode 309 * 310 * @param enabled - true: enable low latency, false: disable low latency 311 * @return bool - operation result 312 */ 313 bool SetLowLatencyMode(bool enabled) override; 314 315 /** 316 * @Description Check whether service is died. 317 * 318 * @return bool - true: service is died, false: service is not died. 319 */ 320 bool IsRemoteDied(void) override; 321 322 #ifdef OHOS_ARCH_LITE 323 /** 324 * @Description Handle remote object died event. 325 */ 326 void OnRemoteDied(void); 327 private: 328 static WifiDeviceProxy *g_instance; 329 IClientProxy *remote_ = nullptr; 330 SvcIdentity svcIdentity_ = { 0 }; 331 bool remoteDied_; 332 void WriteIpAddress(IpcIo &req, const WifiIpAddress &address); 333 void WriteDeviceConfig(const WifiDeviceConfig &config, IpcIo &req); 334 #else 335 private: 336 class WifiDeathRecipient : public IRemoteObject::DeathRecipient { 337 public: WifiDeathRecipient(WifiDeviceProxy & client)338 explicit WifiDeathRecipient(WifiDeviceProxy &client) : client_(client) {} 339 ~WifiDeathRecipient() override = default; OnRemoteDied(const wptr<IRemoteObject> & remote)340 void OnRemoteDied(const wptr<IRemoteObject> &remote) override 341 { 342 client_.OnRemoteDied(remote); 343 } 344 345 private: 346 WifiDeviceProxy &client_; 347 }; 348 349 /** 350 * @Description Handle remote object died event. 351 * @param remoteObject remote object. 352 */ 353 void OnRemoteDied(const wptr<IRemoteObject> &remoteObject); 354 void WriteIpAddress(MessageParcel &data, const WifiIpAddress &address); 355 void ReadIpAddress(MessageParcel &reply, WifiIpAddress &address); 356 void ReadLinkedInfo(MessageParcel &reply, WifiLinkedInfo &info); 357 void WriteDeviceConfig(const WifiDeviceConfig &config, MessageParcel &data); 358 void ParseDeviceConfigs(MessageParcel &reply, std::vector<WifiDeviceConfig> &result); 359 void RemoveDeathRecipient(void); 360 static BrokerDelegator<WifiDeviceProxy> g_delegator; 361 sptr<IRemoteObject> remote_ = nullptr; 362 bool mRemoteDied; 363 std::mutex mutex_; 364 sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr; 365 #endif 366 }; 367 } // namespace Wifi 368 } // namespace OHOS 369 #endif 370