1 /* 2 * Copyright (C) 2021 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_CONFIG_CENTER_H 17 #define OHOS_WIFI_CONFIG_CENTER_H 18 19 #include <atomic> 20 #include <chrono> 21 #include <map> 22 #include <memory> 23 #include <string> 24 #include <vector> 25 #include "wifi_internal_msg.h" 26 #include "wifi_logger.h" 27 #include "wifi_settings.h" 28 #include "wifi_msg.h" 29 30 namespace OHOS { 31 namespace Wifi { 32 class WifiConfigCenter { 33 public: 34 WifiConfigCenter(); 35 ~WifiConfigCenter(); 36 static WifiConfigCenter &GetInstance(); 37 38 /** 39 * @Description Init WifiConfigCenter object 40 * 41 * @return int - init result, when 0 means success, other means some fails happened 42 */ 43 int Init(); 44 45 /** 46 * @Description Get current wifi middle state 47 * 48 * @return WifiOprMidState - which can be a CLOSED/CLOSING/OPENING/RUNNING/UNKNOWN 49 */ 50 WifiOprMidState GetWifiMidState(); 51 52 /** 53 * @Description Set current wifi middle state 54 * 55 * @param expState - expect the original state 56 * @param state - want to set state 57 * @return true - set the state success 58 * @return false - set state failed, current mid state is not equal to the expState 59 */ 60 bool SetWifiMidState(WifiOprMidState expState, WifiOprMidState state); 61 62 /** 63 * @Description Force to set current wifi middle state 64 * 65 * @param state - want to set state 66 */ 67 void SetWifiMidState(WifiOprMidState state); 68 69 /** 70 * @Description Set the wifi close time, just current timestamp 71 * 72 */ 73 void SetWifiStaCloseTime(); 74 75 /** 76 * @Description Get the interval since last wifi close time 77 * 78 * @return double - the interval, millisecond 79 */ 80 double GetWifiStaInterval(); 81 82 /** 83 * @Description Get current wifi state 84 * 85 * @return int - the wifi state, DISABLING/DISABLED/ENABLING/ENABLED/UNKNOWN 86 */ 87 int GetWifiState(); 88 89 /** 90 * @Description Get the ScanAlways switch state 91 * 92 * @return true - the ScanAlways switch is on 93 * @return false - the ScanAlways switch is off 94 */ 95 bool IsScanAlwaysActive(); 96 97 /** 98 * @Description Get scan results 99 * 100 * @param results - output scan results 101 * @return int - 0 success 102 */ 103 int GetScanInfoList(std::vector<WifiScanInfo> &results); 104 105 /** 106 * @Description Get the scan control policy info 107 * 108 * @param info - output scan control policy info struct 109 * @return int - 0 success 110 */ 111 int GetScanControlInfo(ScanControlInfo &info); 112 113 /** 114 * @Description Save the scan control policy info 115 * 116 * @param info - input scan control policy info struct 117 * @return int - 0 success 118 */ 119 int SetScanControlInfo(const ScanControlInfo &info); 120 121 /** 122 * @Description Save a wifi device config 123 * 124 * @param config - input wifi device config 125 * @return int - network id 126 */ 127 int AddDeviceConfig(const WifiDeviceConfig &config); 128 129 /** 130 * @Description Remove a wifi device config who's networkId equals input networkId 131 * 132 * @param networkId - want to remove a networkId 133 * @return int - 0 success 134 */ 135 int RemoveDevice(int networkId); 136 137 /** 138 * @Description Get all saved wifi device config 139 * 140 * @param results - output wifi device config results 141 * @return int - 0 success 142 */ 143 int GetDeviceConfig(std::vector<WifiDeviceConfig> &results); 144 145 /** 146 * @Description Get all candidate wifi device config 147 * 148 * @param results - output wifi device config results 149 * @return int - 0 success 150 */ 151 int GetCandidateConfigs(const int uid, std::vector<WifiDeviceConfig> &results); 152 153 /** 154 * @Description Set a wifi device's state who's networkId equals input networkId; 155 * when the param bSetOther is true and the state is ENABLED, that means we need 156 * to set other wifi device DISABLED 157 * @param networkId - the wifi device's id 158 * @param state - WifiDeviceConfigStatus DISABLED/ENABLED/UNKNOWN 159 * @param bSetOther - whether set other device config disabled 160 * @return int - when 0 means success, other means some fails happened, 161 * Input state invalid or not find the wifi device config 162 */ 163 int SetDeviceState(int networkId, int state, bool bSetOther = false); 164 165 /** 166 * @Description Get the dhcp info 167 * 168 * @param info - output IpInfo struct 169 * @return int - 0 success 170 */ 171 int GetIpInfo(IpInfo &info); 172 173 /** 174 * @Description Get the dhcp inV6fo 175 * 176 * @param info - output IpV6Info struct 177 * @return int - 0 success 178 */ 179 int GetIpv6Info(IpV6Info &info); 180 181 /** 182 * @Description Get current link info 183 * 184 * @param info - output WifiLinkedInfo struct 185 * @return int - 0 success 186 */ 187 int GetLinkedInfo(WifiLinkedInfo &info); 188 189 /** 190 * @Description Get the mac address 191 * 192 * @param macAddress - output mac address info 193 * @return int - 0 success 194 */ 195 int GetMacAddress(std::string &macAddress); 196 197 /** 198 * @Description Save the country code 199 * 200 * @param countryCode - input country code 201 * @return int - 0 success 202 */ 203 int SetCountryCode(const std::string &countryCode); 204 205 /** 206 * @Description Get the country code 207 * 208 * @param countryCode - output country code 209 * @return int - 0 success 210 */ 211 int GetCountryCode(std::string &countryCode); 212 213 /** 214 * @Description Obtaining Whether to Load the Configuration of the Standby STA. 215 * 216 * @return bool - Indicates whether to load the configuration of the standby STA. 217 */ 218 bool IsLoadStabak(); 219 220 /** 221 * @Description Get current hotspot middle state 222 * 223 * @return WifiOprMidState - which can be a CLOSED/CLOSING/OPENING/RUNNING/UNKNOWN 224 */ 225 WifiOprMidState GetApMidState(int id = 0); 226 227 /** 228 * @Description Set current hotspot middle state 229 * 230 * @param expState - expect the original state 231 * @param state - want to set state 232 * @return true - set the state success 233 * @return false - set state failed, current mid state is not equal to the expState 234 */ 235 bool SetApMidState(WifiOprMidState expState, WifiOprMidState state, int id = 0); 236 237 /** 238 * @Description Force to set current hotspot middle state 239 * 240 * @param state - want to set state 241 */ 242 void SetApMidState(WifiOprMidState state, int id = 0); 243 244 /** 245 * @Description Get current hotspot state 246 * 247 * @return int - the hotspot state, IDLE/STARTING/STARTED/CLOSING/CLOSED 248 */ 249 int GetHotspotState(int id = 0); 250 251 /** 252 * @Description Set the hotspot config 253 * 254 * @param config - input HotspotConfig struct 255 * @return int - 0 success 256 */ 257 int SetHotspotConfig(const HotspotConfig &config, int id = 0); 258 259 /** 260 * @Description Get the hotspot config 261 * 262 * @param config - output HotspotConfig struct 263 * @return int - 0 success 264 */ 265 int GetHotspotConfig(HotspotConfig &config, int id = 0); 266 267 /** 268 * @Description Set the idel timeout of Hotspot 269 * 270 * @return int - 0 success 271 */ 272 int SetHotspotIdleTimeout(int time); 273 274 /** 275 * @Description Get the idel timeout of Hotspot 276 * 277 * @param time -input time, 278 * @return int - the hotspot idle timeout 279 */ 280 int GetHotspotIdleTimeout(); 281 282 /** 283 * @Description Get current hotspot accept linked stations 284 * 285 * @param results - output StationInfo results 286 * @return int - 0 success 287 */ 288 int GetStationList(std::vector<StationInfo> &results, int id = 0); 289 290 /** 291 * @Description Judge whether the station is in current linked station list 292 * 293 * @param info - input StationInfo struct 294 * @return int - 0 find the station, exist; -1 not find, not exist 295 */ 296 int FindConnStation(const StationInfo &info, int id = 0); 297 298 /** 299 * @Description Get the block list 300 * 301 * @param infos - output StationInfo results 302 * @return int - 0 success 303 */ 304 int GetBlockLists(std::vector<StationInfo> &infos, int id = 0); 305 306 /** 307 * @Description Add the station info into the block list 308 * 309 * @param info - input StationInfo struct 310 * @return int - 0 success 311 */ 312 int AddBlockList(const StationInfo &info, int id = 0); 313 314 /** 315 * @Description Remove the station info from the block list 316 * 317 * @param info - input StationInfo struct 318 * @return int - 0 success 319 */ 320 int DelBlockList(const StationInfo &info, int id = 0); 321 322 /** 323 * @Description Get the valid bands info 324 * 325 * @param bands - output band results 326 * @return int - 0 success 327 */ 328 int GetValidBands(std::vector<BandType> &bands); 329 330 /** 331 * @Description Get current valid channels 332 * 333 * @param channelsInfo - output valid channel info 334 * @return int - 0 success 335 */ 336 int GetValidChannels(ChannelsTable &channelsInfo); 337 338 /** 339 * @Description Get current scan service middle state 340 * 341 * @return WifiOprMidState - which can be a CLOSED/CLOSING/OPENING/RUNNING/UNKNOWN 342 */ 343 WifiOprMidState GetScanMidState(); 344 345 /** 346 * @Description Set current scan service middle state 347 * 348 * @param expState - expect the original state 349 * @param state - want to set state 350 * @return true - set the state success 351 * @return false - set state failed, current mid state is not equal to the expState 352 */ 353 bool SetScanMidState(WifiOprMidState expState, WifiOprMidState state); 354 355 /** 356 * @Description Force to set the scan service middle state 357 * 358 * @param state - want to set state 359 */ 360 void SetScanMidState(WifiOprMidState state); 361 362 /** 363 * @Description Get signal level about given rssi and band 364 * 365 * @param rssi - rssi info 366 * @param band - band info 367 * @return int - 0 or the level 368 */ 369 int GetSignalLevel(const int &rssi, const int &band); 370 371 /** 372 * @Description Get current p2p middle state 373 * 374 * @return WifiOprMidState 375 */ 376 WifiOprMidState GetP2pMidState(); 377 378 /** 379 * @Description Set current hotspot middle state. 380 * 381 * @param expState - expect the original state 382 * @param state - want to set state 383 * @return true - set the state success 384 * @return false - set state failed, current mid state is not equal to the expState 385 */ 386 bool SetP2pMidState(WifiOprMidState expState, WifiOprMidState state); 387 388 /** 389 * @Description Force to set current hotspot middle state 390 * 391 * @param state - want to set state 392 */ 393 void SetP2pMidState(WifiOprMidState state); 394 395 /** 396 * @Description Get current p2p state 397 * 398 * @return int - the p2p state,NONE/IDLE/STARTING/STARTED/CLOSING/CLOSED 399 */ 400 int GetP2pState(); 401 402 /** 403 * @Description Get operator wifi state 404 * 405 * @return type - enum OperatorWifiType 406 */ 407 int GetOperatorWifiType(); 408 409 /** 410 * @Description Set operator wifi state 411 * 412 * @param type - enum OperatorWifiType 413 * @return int - 0 success 414 */ 415 int SetOperatorWifiType(int type); 416 417 /** 418 * @Description Get the config whether can open sta when airplane mode opened 419 * 420 * @return true - can open 421 * @return false - can't open 422 */ 423 bool GetCanOpenStaWhenAirplaneMode(); 424 425 /** 426 * @Description when last airplane mode, get sta state, open or close 427 * 428 * @return true - when in airplane mode, we open sta 429 * @return false - when in airplane mode, we close sta 430 */ 431 bool GetWifiStateWhenAirplaneMode(); 432 433 /** 434 * @Description when in airplane mode, set sta state 435 * 436 * @param bState - open or close 437 */ 438 void SetWifiStateWhenAirplaneMode(bool bState); 439 440 /** 441 * @Description Get the STA service last running state 442 * 443 * @return true - sta is running 444 * @return false - sta not running 445 */ 446 bool GetStaLastRunState(); 447 448 /** 449 * @Description Set the STA service running state 450 * 451 * @param bRun - true / false 452 * @return int - 0 success 453 */ 454 int SetStaLastRunState(bool bRun); 455 456 /** 457 * @Description Set current phone screen state 458 * 459 * @param state - 1 open; 2 close 460 */ 461 void SetScreenState(const int &state); 462 463 /** 464 * @Description Get current phone screen state 465 * 466 * @return int - 1 open; 2 close 467 */ 468 int GetScreenState() const; 469 470 /** 471 * @Description Set current airplane mode state 472 * 473 * @param state - 1 open; 2 close 474 */ 475 void SetAirplaneModeState(const int &state); 476 477 /** 478 * @Description Get current airplane mode state 479 * 480 * @return int - 1 open; 2 close 481 */ 482 int GetAirplaneModeState() const; 483 484 /** 485 * @Description Set current app running mode 486 * 487 * @param appRunMode - app run mode 488 */ 489 void SetAppRunningState(ScanMode appRunMode); 490 491 /** 492 * @Description Get current app running mode 493 * 494 * @param ScanMode 495 */ 496 ScanMode GetAppRunningState() const; 497 498 /** 499 * @Description Set current power saving mode 500 * 501 * @param state - 1 saving mode; 2 not saving mode 502 */ 503 void SetPowerSavingModeState(const int &state); 504 505 /** 506 * @Description Get current power saving mode 507 * 508 * @return int - 1 saving mode; 2 not saving mode 509 */ 510 int GetPowerSavingModeState() const; 511 512 /** 513 * @Description Set app package name. 514 * 515 * @param appPackageName - app package name 516 */ 517 void SetAppPackageName(const std::string &appPackageName); 518 519 /** 520 * @Description Get app package name. 521 * 522 * @return const std::string& - app package name. 523 */ 524 const std::string GetAppPackageName() const; 525 526 /** 527 * @Description Set freeze mode state. 528 * 529 * @param state - 1 freeze mode; 2 moving mode 530 */ 531 void SetFreezeModeState(int state); 532 533 /** 534 * @Description Get freeze mode state. 535 * 536 * @return freeze mode. 537 */ 538 int GetFreezeModeState() const; 539 540 /** 541 * @Description Set no charger plugged in mode. 542 * 543 * @param state - 1 no charger plugged in mode; 2 charger plugged in mode 544 */ 545 void SetNoChargerPlugModeState(int state); 546 547 /** 548 * @Description Get no charger plugged in mode. 549 * 550 * @return no charger plugged in mode. 551 */ 552 int GetNoChargerPlugModeState() const; 553 554 /** 555 * @Description set the device name 556 * 557 * @param deviceName - device name 558 * @return int - 0 success 559 */ 560 int SetP2pDeviceName(const std::string &deviceName); 561 562 /** 563 * @Description get the last disconnected reason 564 * 565 * @param discReason - discReason 566 * @return int - 0 success 567 */ 568 int GetDisconnectedReason(DisconnectedReason &discReason); 569 570 /** 571 * @Description Get current scan only state 572 * 573 * @return WifiOprMidState - which can be a CLOSED/CLOSING/OPENING/RUNNING/UNKNOWN 574 */ 575 WifiOprMidState GetWifiScanOnlyMidState(); 576 577 /** 578 * @Description Force to set current scan only state 579 * 580 * @param state - want to set state 581 */ 582 bool SetWifiScanOnlyMidState(WifiOprMidState expState, WifiOprMidState state); 583 584 /** 585 * @Description Set current scan only state 586 * 587 * @param state - want to set state 588 */ 589 void SetWifiScanOnlyMidState(WifiOprMidState state); 590 591 /** 592 * @Description Get sta ap exclusion type 593 * 594 * @return type - enum StaApExclusionType 595 */ 596 int GetStaApExclusionType(); 597 598 /** 599 * @Description Set sta ap exclusion type 600 * 601 * @param type - enum StaApExclusionType 602 * @return int - 0 success 603 */ 604 int SetStaApExclusionType(int type); 605 606 private: 607 std::mutex mApMutex; 608 std::atomic<WifiOprMidState> mStaMidState; 609 std::map<int, std::atomic<WifiOprMidState>> mApMidState; 610 std::atomic<WifiOprMidState> mP2pMidState; 611 std::atomic<WifiOprMidState> mScanMidState; 612 std::atomic<WifiOprMidState> mStaScanOnlyMidState; 613 /* Time interval for disabling and re-enabling the STA */ 614 std::chrono::steady_clock::time_point mWifiCloseTime; 615 std::atomic<bool> mWifiOpenedWhenAirplane; 616 }; 617 } // namespace Wifi 618 } // namespace OHOS 619 #endif 620