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 16 #ifndef OHOS_WIFI_P2P_SERVICE_IMPL_H 17 #define OHOS_WIFI_P2P_SERVICE_IMPL_H 18 19 #include "wifi_p2p_msg.h" 20 #include "wifi_errcode.h" 21 #include "system_ability.h" 22 #include "wifi_p2p_stub.h" 23 #include "iremote_object.h" 24 25 namespace OHOS { 26 namespace Wifi { 27 enum ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 28 class WifiP2pServiceImpl : public SystemAbility, public WifiP2pStub { 29 DECLARE_SYSTEM_ABILITY(WifiP2pServiceImpl); 30 31 public: 32 WifiP2pServiceImpl(); 33 virtual ~WifiP2pServiceImpl(); 34 35 static sptr<WifiP2pServiceImpl> GetInstance(); 36 37 void OnStart() override; 38 void OnStop() override; 39 40 /** 41 * @Description Enabling the P2P Mode 42 * 43 * @return ErrCode - operation result 44 */ 45 ErrCode EnableP2p(void) override; 46 47 /** 48 * @Description Disable the P2P mode 49 * 50 * @return ErrCode - operation result 51 */ 52 ErrCode DisableP2p(void) override; 53 54 /** 55 * @Description Start Wi-Fi P2P device search 56 * 57 * @return ErrCode - operation result 58 */ 59 ErrCode DiscoverDevices(void) override; 60 61 /** 62 * @Description Stop Wi-Fi P2P device search 63 * 64 * @return ErrCode - operation result 65 */ 66 ErrCode StopDiscoverDevices(void) override; 67 68 /** 69 * @Description Start the search for the Wi-Fi P2P service 70 * 71 * @return ErrCode - operation result 72 */ 73 ErrCode DiscoverServices(void) override; 74 75 /** 76 * @Description Stop the search for the Wi-Fi P2P service 77 * 78 * @return ErrCode - operation result 79 */ 80 ErrCode StopDiscoverServices(void) override; 81 82 /** 83 * @Description request the P2P service 84 * 85 * @param device - WifiP2pDevice object 86 * @param request - WifiP2pServiceRequest object 87 * @return ErrCode - operation result 88 */ 89 ErrCode RequestService(const WifiP2pDevice &device, const WifiP2pServiceRequest &request) override; 90 91 /** 92 * @Description Register the local P2P service 93 * 94 * @param srvInfo - WifiP2pServiceInfo object 95 * @return ErrCode - operation result 96 */ 97 ErrCode PutLocalP2pService(const WifiP2pServiceInfo &srvInfo) override; 98 99 /** 100 * @Description Delete the local P2P service 101 * 102 * @param srvInfo - WifiP2pServiceInfo object 103 * @return ErrCode - operation result 104 */ 105 ErrCode DeleteLocalP2pService(const WifiP2pServiceInfo &srvInfo) override; 106 107 /** 108 * @Description Enable Wi-Fi P2P listening 109 * 110 * @param period - period 111 * @param interval - interval 112 * @return ErrCode - operation result 113 */ 114 ErrCode StartP2pListen(int period, int interval) override; 115 116 /** 117 * @Description Disable Wi-Fi P2P listening 118 * 119 * @return ErrCode - operation result 120 */ 121 ErrCode StopP2pListen(void) override; 122 123 /** 124 * @Description Creating a P2P Group 125 * 126 * @param config - WifiP2pGroupInfo object 127 * @return ErrCode - operation result 128 */ 129 ErrCode CreateGroup(const WifiP2pConfig &config) override; 130 131 /** 132 * @Description Remove a P2P Group 133 * 134 * 135 * @return ErrCode - operation result 136 */ 137 ErrCode RemoveGroup(void) override; 138 139 /** 140 * @Description Remove a P2P client of current group. 141 * 142 * @param deviceMac - client deviceMac address 143 * @return ErrCode - operation result 144 */ 145 ErrCode RemoveGroupClient(const GcInfo &info) override; 146 147 /** 148 * @Description Delete a p2p Group 149 * 150 * @param group - WifiP2pGroupInfo object 151 * @return ErrCode - operation result 152 */ 153 ErrCode DeleteGroup(const WifiP2pGroupInfo &group) override; 154 155 /** 156 * @Description P2P connection 157 * 158 * @param config - WifiP2pConfig object 159 * @return ErrCode - operation result 160 */ 161 ErrCode P2pConnect(const WifiP2pConfig &config) override; 162 163 /** 164 * @Description Canceling a P2P connection 165 * 166 * @return ErrCode - operation result 167 */ 168 ErrCode P2pCancelConnect(void) override; 169 170 /** 171 * @Description Querying Wi-Fi P2P Connection Information 172 * 173 * @param linkedInfo - Get the WifiP2pLinkedInfo msg 174 * @return ErrCode - operation result 175 */ 176 ErrCode QueryP2pLinkedInfo(WifiP2pLinkedInfo &linkedInfo) override; 177 178 /** 179 * @Description Get the Current Group object 180 * 181 * @param group - the WifiP2pGroupInfo object 182 * @return ErrCode - operation result 183 */ 184 ErrCode GetCurrentGroup(WifiP2pGroupInfo &group) override; 185 186 /** 187 * @Description Obtains the P2P switch status 188 * 189 * @param status - the P2P switch status 190 * @return ErrCode - operation result 191 */ 192 ErrCode GetP2pEnableStatus(int &status) override; 193 194 /** 195 * @Description Obtains the P2P discovery status 196 * 197 * @param status - the P2P discovery status 198 * @return ErrCode 199 */ 200 ErrCode GetP2pDiscoverStatus(int &status) override; 201 202 /** 203 * @Description Obtains the P2P connection status 204 * 205 * @param status - the P2P connection status 206 * @return ErrCode - operation result 207 */ 208 ErrCode GetP2pConnectedStatus(int &status) override; 209 210 /** 211 * @Description Query the information about the found devices 212 * 213 * @param devices - Get result vector of WifiP2pDevice 214 * @return ErrCode - operation result 215 */ 216 ErrCode QueryP2pDevices(std::vector<WifiP2pDevice> &devices) override; 217 218 /** 219 * @Description Query the information about the local device 220 * 221 * @param devives - Get result of WifiP2pDevice 222 * @return ErrCode - operation result 223 */ 224 ErrCode QueryP2pLocalDevice(WifiP2pDevice &device) override; 225 226 /** 227 * @Description Query the information about the found groups 228 * 229 * @param groups - Get result vector of WifiP2pGroupInfo 230 * @return ErrCode - operation result 231 */ 232 ErrCode QueryP2pGroups(std::vector<WifiP2pGroupInfo> &groups) override; 233 234 /** 235 * @Description Query the service information 236 * 237 * @param services - Get result vector of Device 238 * @return ErrCode - operation result 239 */ 240 ErrCode QueryP2pServices(std::vector<WifiP2pServiceInfo> &services) override; 241 242 /** 243 * @Description Register callback function 244 * 245 * @param callback - IWifiP2pCallback object 246 * @return ErrCode - operation result 247 */ 248 ErrCode RegisterCallBack(const sptr<IWifiP2pCallback> &callback, const std::vector<std::string> &event) override; 249 250 /** 251 * @Description Get supported feature 252 * 253 * @param features - return supported feature 254 * @return ErrCode - operation result 255 */ 256 ErrCode GetSupportedFeatures(long &features) override; 257 258 /** 259 * @Description set the device name 260 * 261 * @param deviceName - device name 262 * @return ErrCode - operation result 263 */ 264 ErrCode SetP2pDeviceName(const std::string &deviceName) override; 265 266 /** 267 * @Description set p2p wifi display info 268 * 269 * @param wfdInfo - wifi display info 270 * @return ErrCode - operation result 271 */ 272 ErrCode SetP2pWfdInfo(const WifiP2pWfdInfo &wfdInfo) override; 273 274 /** 275 * @Description Request an IP address to the Gc from the IP address pool, used on the GO side. 276 * 277 * @param gcMac - gc mac address 278 * @param ipAddr - applied ip address 279 * @return ErrCode - operation result 280 */ 281 ErrCode Hid2dRequestGcIp(const std::string& gcMac, std::string& ipAddr) override; 282 283 /** 284 * @Description Increase(+1) hid2d shared link reference counting 285 * 286 * @return ErrCode - operation result 287 */ 288 ErrCode Hid2dSharedlinkIncrease() override; 289 290 /** 291 * @Description Decrease(-1) hid2d shared link reference counting 292 * 293 * @return ErrCode - operation result 294 */ 295 ErrCode Hid2dSharedlinkDecrease() override; 296 297 /** 298 * @Description Create hid2d group, used on the GO side. 299 * 300 * @param frequency - frequency 301 * @param type - frequency type 302 * @return ErrCode - operation result 303 */ 304 ErrCode Hid2dCreateGroup(const int frequency, FreqType type) override; 305 306 /** 307 * @Description The GC side actively disconnects from the GO, used on the GC side. 308 * 309 * @param gcIfName - network interface name 310 * @return ErrCode - operation result 311 */ 312 ErrCode Hid2dRemoveGcGroup(const std::string& gcIfName) override; 313 314 /** 315 * @Description Connect to a specified group using hid2d, used on the GC side. 316 * 317 * @param config - connection parameters 318 * @return ErrCode - operation result 319 */ 320 ErrCode Hid2dConnect(const Hid2dConnectConfig& config) override; 321 322 /** 323 * @Description Configuring IP addresses for P2P network interfaces, used on the GC side. 324 * 325 * @param ifName - network interface name 326 * @param ipInfo - IP infos 327 * @return ErrCode - operation result 328 */ 329 ErrCode Hid2dConfigIPAddr(const std::string& ifName, const IpAddrInfo& ipInfo) override; 330 331 /** 332 * @Description Clear IP address when the P2P connection is disconnected, used on the GC side. 333 * 334 * @param ifName - network interface name 335 * @return ErrCode - operation result 336 */ 337 ErrCode Hid2dReleaseIPAddr(const std::string& ifName) override; 338 339 /** 340 * @Description Obtain the recommended channel and bandwidth for link setup 341 * 342 * @param request - request data 343 * @param response - response result 344 * @return ErrCode - operation result 345 */ 346 ErrCode Hid2dGetRecommendChannel(const RecommendChannelRequest& request, 347 RecommendChannelResponse& response) override; 348 349 /** 350 * @Description get 5G channel list 351 * 352 * @param vecChannelList - result for channel list 353 * @return ErrCode - operation result 354 */ 355 ErrCode Hid2dGetChannelListFor5G(std::vector<int>& vecChannelList) override; 356 357 /** 358 * @Description get the self wifi configuration information 359 * 360 * @param cfgType - configuration type 361 * @param cfgData - the queried data of wifi configuration 362 * @param getDatValidLen - the valid data length in the array `cfgData` 363 * @return ErrCode - operation result 364 */ 365 ErrCode Hid2dGetSelfWifiCfgInfo(SelfCfgType cfgType, 366 char cfgData[CFG_DATA_MAX_BYTES], int* getDatValidLen) override; 367 368 /** 369 * @Description set the peer wifi configuration information 370 * 371 * @param cfgType - configuration type 372 * @param cfgData - the wifi configuration data to be set 373 * @param setDataValidLen - the valid data length in the array `cfgData` 374 * @return ErrCode - operation result 375 */ 376 ErrCode Hid2dSetPeerWifiCfgInfo(PeerCfgType cfgType, 377 char cfgData[CFG_DATA_MAX_BYTES], int setDataValidLen) override; 378 379 /** 380 * @Description Set the scene of upper layer 381 * 382 * @param ifName - interface name 383 * @param scene - scene 384 * @return ErrCode - operate result 385 */ 386 ErrCode Hid2dSetUpperScene(const std::string& ifName, const Hid2dUpperScene& scene) override; 387 388 /** 389 * @Description Monitor the wifi configuration change 390 * 391 * @return ErrCode - operate result 392 */ 393 ErrCode MonitorCfgChange(void) override; 394 395 /** 396 * @Description dump p2p information 397 * 398 * @param fd - file descriptor 399 * @param args - dump arguments 400 * @return ErrCode - operation result 401 */ 402 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 403 404 /** 405 * @Description Check whether service is died. 406 * 407 * @return bool - true: service is died, false: service is not died. 408 */ 409 bool IsRemoteDied(void) override; 410 411 /** 412 * @Description Discover p2p device 413 * 414 * @return ErrCode - operate result 415 */ 416 ErrCode DiscoverPeers(int32_t channelid) override; 417 418 /** 419 * @Description Disable random mac 420 * 421 * @return ErrCode - operate result 422 */ 423 ErrCode DisableRandomMac(int setmode) override; 424 425 /** 426 * @Description Check can use P2P 427 * 428 * @return ErrCode - operation result 429 */ 430 ErrCode CheckCanUseP2p() override; 431 432 /** 433 * @Description Hid2d Is Wide Bandwidth Supported 434 * 435 * @param isSupport - is support 160M 436 * @return ErrCode - operation result 437 */ 438 ErrCode Hid2dIsWideBandwidthSupported(bool &isSupport) override; 439 440 /** 441 * @Description Set miracast sink config 442 * 443 * @param config - miracast config 444 * @return ErrCode - operation result 445 */ 446 ErrCode SetMiracastSinkConfig(const std::string& config) override; 447 448 /** 449 * @Description Get support channels for band 450 * 451 * @param channels - support channels 452 * @param band - channel band 453 * @return ErrCode - operation result 454 */ 455 ErrCode GetSupportedChanForBand(std::vector<int> &channels, int band) override; 456 457 /** 458 * @Description Set p2p high perf mode 459 * 460 * @param isEnable - enable high perf mode or not 461 * @return ErrCode - operation result 462 */ 463 ErrCode SetP2pHighPerf(bool isEnable) override; 464 private: 465 bool Init(); 466 ErrCode CheckCanEnableP2p(void); 467 // forceEnable: true - force enable P2pService if p2pservice is not running, false - check the service status 468 bool IsP2pServiceRunning(bool forceEnable = true); 469 bool IsCallingAllowed(); 470 static void SaBasicDump(std::string& result); 471 472 private: 473 static sptr<WifiP2pServiceImpl> instance; 474 static std::mutex instanceLock; 475 static std::mutex g_p2pMutex; 476 bool mPublishFlag; 477 ServiceRunningState mState; 478 static constexpr int WIFI_P2P_PASSPHRASE_MIN_LEN = 8; 479 static constexpr int WIFI_P2P_PASSPHRASE_MAX_LEN = 63; 480 }; 481 } // namespace Wifi 482 } // namespace OHOS 483 #endif