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_STATE_MACHINE_H 16 #define OHOS_WIFI_STATE_MACHINE_H 17 18 #include <regex.h> 19 #include <sys/types.h> 20 #include <fstream> 21 #include <vector> 22 23 #ifndef OHOS_ARCH_LITE 24 #include "system_ability.h" 25 #include "system_ability_status_change_stub.h" 26 #endif // OHOS_ARCH_LITE 27 28 #include "wifi_internal_msg.h" 29 #include "wifi_log.h" 30 #include "wifi_errcode.h" 31 #include "wifi_msg.h" 32 #include "state_machine.h" 33 #include "sta_network_check.h" 34 #include "i_dhcp_result_notify.h" 35 #include "sta_service_callback.h" 36 #include "i_dhcp_service.h" 37 #include "sta_define.h" 38 #ifndef OHOS_ARCH_LITE 39 #include "wifi_net_agent.h" 40 #endif 41 42 namespace OHOS { 43 namespace Wifi { 44 constexpr int STA_CONNECT_MODE = 1; 45 constexpr int STA_SCAN_ONLY_MODE = 2; 46 constexpr int STA_CAN_ONLY_WITH_WIFI_OFF_MODE = 3; 47 constexpr int STA_DISABLED_MODE = 4; 48 49 constexpr int CMD_NETWORK_CONNECT_TIMEOUT = 0X01; 50 constexpr int CMD_SIGNAL_POLL = 0X02; 51 constexpr int CMD_START_NETCHECK = 0X03; 52 53 constexpr int STA_NETWORK_CONNECTTING_DELAY = 20 * 1000; 54 constexpr int STA_SIGNAL_POLL_DELAY = 3 * 1000; 55 56 /* pincode length */ 57 constexpr int PIN_CODE_LEN = 8; 58 59 /* DHCP timeout interval */ 60 constexpr int DHCP_TIME = 15; 61 /* rssi thresholds */ 62 constexpr int INVALID_RSSI_VALUE = -127; 63 constexpr int MAX_RSSI_VALUE = 200; 64 constexpr int SIGNAL_INFO = 256; 65 66 /* 2.4g and 5g frequency thresholds */ 67 constexpr int FREQ_2G_MIN = 2412; 68 constexpr int FREQ_2G_MAX = 2472; 69 constexpr int FREQ_5G_MIN = 5170; 70 constexpr int FREQ_5G_MAX = 5825; 71 constexpr int CHANNEL_14_FREQ = 2484; 72 constexpr int CHANNEL_14 = 14; 73 constexpr int CENTER_FREQ_DIFF = 5; 74 constexpr int CHANNEL_2G_MIN = 1; 75 constexpr int CHANNEL_5G_MIN = 34; 76 77 constexpr int MULTI_AP = 0; 78 79 /* 80 * During the WPS PIN connection, the WPA_SUPPLICANT blocklist is cleared every 10 seconds 81 * until the network connection is successful. 82 */ 83 constexpr int BLOCK_LIST_CLEAR_TIMER = 20 * 1000; 84 85 /* Signal levels are classified into: 0 1 2 3 4 ,the max is 4. */ 86 constexpr int MAX_LEVEL = 4; 87 const std::string WPA_BSSID_ANY = "any"; 88 const std::string IF_NAME = "wlan0"; 89 90 class StaStateMachine : public StateMachine { 91 FRIEND_GTEST(StaStateMachine); 92 public: 93 StaStateMachine(); 94 ~StaStateMachine(); 95 using staSmHandleFunc = void (StaStateMachine::*)(InternalMessage *msg); 96 using StaSmHandleFuncMap = std::map<int, staSmHandleFunc>; 97 /** 98 * @Description Definition of member function of State base class in StaStateMachine. 99 * 100 */ 101 class RootState : public State { 102 public: 103 explicit RootState(); 104 ~RootState() override; 105 void GoInState() override; 106 void GoOutState() override; 107 bool ExecuteStateMsg(InternalMessage *msg) override; 108 }; 109 /** 110 * @Description : Definition of member function of InitState class in StaStateMachine. 111 * 112 */ 113 class InitState : public State { 114 public: 115 explicit InitState(StaStateMachine *staStateMachine); 116 ~InitState() override; 117 void GoInState() override; 118 void GoOutState() override; 119 bool ExecuteStateMsg(InternalMessage *msg) override; 120 121 private: 122 StaStateMachine *pStaStateMachine; 123 }; 124 /** 125 * @Description : Definition of member function of WpaStartingState class in StaStateMachine. 126 * 127 */ 128 class WpaStartingState : public State { 129 public: 130 explicit WpaStartingState(StaStateMachine *staStateMachine); 131 ~WpaStartingState() override; 132 void InitWpsSettings(); 133 void GoInState() override; 134 void GoOutState() override; 135 bool ExecuteStateMsg(InternalMessage *msg) override; 136 137 private: 138 StaStateMachine *pStaStateMachine; 139 }; 140 /** 141 * @Description Definition of member function of WpaStartedState class in StaStateMachine. 142 * 143 */ 144 class WpaStartedState : public State { 145 public: 146 explicit WpaStartedState(StaStateMachine *staStateMachine); 147 ~WpaStartedState() override; 148 void GoInState() override; 149 void GoOutState() override; 150 bool ExecuteStateMsg(InternalMessage *msg) override; 151 152 private: 153 StaStateMachine *pStaStateMachine; 154 }; 155 /** 156 * @Description Definition of member function of WpaStoppingState class in StaStateMachine. 157 * 158 */ 159 class WpaStoppingState : public State { 160 public: 161 explicit WpaStoppingState(StaStateMachine *staStateMachine); 162 ~WpaStoppingState() override; 163 void GoInState() override; 164 void GoOutState() override; 165 bool ExecuteStateMsg(InternalMessage *msg) override; 166 167 private: 168 StaStateMachine *pStaStateMachine; 169 }; 170 /** 171 * @Description Definition of member function of LinkState class in StaStateMachine. 172 * 173 */ 174 class LinkState : public State { 175 public: 176 explicit LinkState(StaStateMachine *staStateMachine); 177 ~LinkState() override; 178 void GoInState() override; 179 void GoOutState() override; 180 bool ExecuteStateMsg(InternalMessage *msg) override; 181 182 private: 183 StaStateMachine *pStaStateMachine; 184 }; 185 /** 186 * @Description Definition of member function of SeparatingState class in StaStateMachine. 187 * 188 */ 189 class SeparatingState : public State { 190 public: 191 explicit SeparatingState(); 192 ~SeparatingState() override; 193 void GoInState() override; 194 void GoOutState() override; 195 bool ExecuteStateMsg(InternalMessage *msg) override; 196 }; 197 /** 198 * @Description Definition of member function of SeparatedState class in StaStateMachine. 199 * 200 */ 201 class SeparatedState : public State { 202 public: 203 explicit SeparatedState(StaStateMachine *staStateMachine); 204 ~SeparatedState() override; 205 void GoInState() override; 206 void GoOutState() override; 207 bool ExecuteStateMsg(InternalMessage *msg) override; 208 209 private: 210 StaStateMachine *pStaStateMachine; 211 }; 212 /** 213 * @Description Definition of member function of ApLinkedState class in StaStateMachine. 214 * 215 */ 216 class ApLinkedState : public State { 217 public: 218 explicit ApLinkedState(StaStateMachine *staStateMachine); 219 ~ApLinkedState() override; 220 void GoInState() override; 221 void GoOutState() override; 222 bool ExecuteStateMsg(InternalMessage *msg) override; 223 224 private: 225 StaStateMachine *pStaStateMachine; 226 }; 227 /** 228 * @Description Definition of member function of WpsState class in StaStateMachine. 229 * 230 */ 231 class StaWpsState : public State { 232 public: 233 explicit StaWpsState(StaStateMachine *staStateMachine); 234 ~StaWpsState() override; 235 void GoInState() override; 236 void GoOutState() override; 237 bool ExecuteStateMsg(InternalMessage *msg) override; 238 239 private: 240 StaStateMachine *pStaStateMachine; 241 }; 242 /** 243 * @Description Definition of member function of GetIpState class in StaStateMachine. 244 * 245 */ 246 class GetIpState : public State { 247 public: 248 explicit GetIpState(StaStateMachine *staStateMachine); 249 ~GetIpState() override; 250 void GoInState() override; 251 void GoOutState() override; 252 bool ExecuteStateMsg(InternalMessage *msg) override; 253 254 private: 255 StaStateMachine *pStaStateMachine; 256 }; 257 /** 258 * @Description Definition of member function of LinkedState class in StaStateMachine. 259 * 260 */ 261 class LinkedState : public State { 262 public: 263 explicit LinkedState(StaStateMachine *staStateMachine); 264 ~LinkedState() override; 265 void GoInState() override; 266 void GoOutState() override; 267 bool ExecuteStateMsg(InternalMessage *msg) override; 268 269 private: 270 StaStateMachine *pStaStateMachine; 271 }; 272 /** 273 * @Description Definition of member function of ApRoamingState class in StaStateMachine. 274 * 275 */ 276 class ApRoamingState : public State { 277 public: 278 explicit ApRoamingState(StaStateMachine *staStateMachine); 279 ~ApRoamingState() override; 280 void GoInState() override; 281 void GoOutState() override; 282 bool ExecuteStateMsg(InternalMessage *msg) override; 283 284 private: 285 StaStateMachine *pStaStateMachine; 286 }; 287 288 class DhcpResultNotify : public IDhcpResultNotify { 289 public: 290 /** 291 * @Description : Construct a new dhcp result notify object 292 * 293 */ 294 explicit DhcpResultNotify(StaStateMachine *staStateMachine); 295 296 /** 297 * @Description : Destroy the dhcp result notify object 298 * 299 */ 300 ~DhcpResultNotify() override; 301 302 /** 303 * @Description : Get dhcp result of specified interface success notify asynchronously 304 * 305 * @param status - int 306 * @param ifname - interface name,eg:wlan0 307 * @param result - dhcp result 308 */ 309 void OnSuccess(int status, const std::string &ifname, DhcpResult &result) override; 310 311 /** 312 * @Description : Get dhcp result of specified interface failed notify asynchronously 313 * 314 * @param status - int 315 * @param ifname - interface name,eg:wlan0 316 * @param reason - failed reason 317 */ 318 void OnFailed(int status, const std::string &ifname, const std::string &reason) override; 319 320 /** 321 * @Description : Get the abnormal exit notify of dhcp server process. 322 * 323 * @param ifname - interface name,eg:wlan0 324 */ 325 void OnSerExitNotify(const std::string& ifname) override; 326 327 private: 328 StaStateMachine *pStaStateMachine; 329 }; 330 331 public: 332 /** 333 * @Description Initialize StaStateMachine 334 * 335 * @Return: WIFI_OPT_SUCCESS - success WIFI_OPT_FAILED - failed 336 */ 337 ErrCode InitStaStateMachine(); 338 /** 339 * @Description Start roaming connection. 340 * 341 * @param bssid - the mac address of network(in) 342 */ 343 void StartRoamToNetwork(std::string bssid); 344 /** 345 * @Description if it is roaming now. 346 */ 347 bool IsRoaming(void); 348 /** 349 * @Description Connecting events 350 * 351 * @param networkId - the networkId of network which is going to be connected(in) 352 * @param bssid - bssid - the mac address of wifi(in) 353 */ 354 void OnNetworkConnectionEvent(int networkId, std::string bssid); 355 /** 356 * @Description Bssid change events 357 * 358 * @param reason: the reason of bssid changed(in) 359 * @param bssid: the mac address of wifi(in) 360 */ 361 void OnBssidChangedEvent(std::string reason, std::string bssid); 362 /** 363 * @Description dhcp result notify events 364 * 365 * @param result: true-success, false-fail(in) 366 */ 367 void OnDhcpResultNotifyEvent(bool result); 368 /** 369 * @Description Register sta callback function 370 * 371 * @param callbacks - Callback function pointer storage structure 372 */ 373 void RegisterStaServiceCallback(const StaServiceCallback &callbacks); 374 375 /** 376 * @Description Convert the deviceConfig structure and set it to wpa_supplicant 377 * 378 * @param config -The Network info(in) 379 * @Return success: WIFI_OPT_SUCCESS fail: WIFI_OPT_FAILED 380 */ 381 ErrCode ConvertDeviceCfg(const WifiDeviceConfig &config) const; 382 383 /** 384 * @Description Get linked info. 385 * 386 * @param linkedInfo - linked info 387 * @return int - operation result 388 */ 389 int GetLinkedInfo(WifiLinkedInfo& linkedInfo); 390 391 #ifndef OHOS_ARCH_LITE 392 private: 393 class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub { 394 public: 395 explicit SystemAbilityStatusChangeListener(StaStateMachine &stateMachine); 396 ~SystemAbilityStatusChangeListener() = default; 397 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 398 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 399 400 private: 401 StaStateMachine &stateMachine_; 402 bool hasSARemoved_ = false; 403 }; 404 #endif // OHOS_ARCH_LITE 405 406 private: 407 /** 408 * @Description Destruct state. 409 * 410 */ 411 template<typename T> ParsePointer(T * & pointer)412 inline void ParsePointer(T *&pointer) 413 { 414 if (pointer != nullptr) { 415 delete pointer; 416 pointer = nullptr; 417 } 418 } 419 /** 420 * @Description Build state tree 421 * 422 */ 423 void BuildStateTree(); 424 /** 425 * @Description Determine whether it is empty during initialization 426 * 427 */ 428 template<typename T> JudgmentEmpty(T * & pointer)429 inline ErrCode JudgmentEmpty(T *&pointer) 430 { 431 if (pointer == nullptr) { 432 return WIFI_OPT_FAILED; 433 } 434 return WIFI_OPT_SUCCESS; 435 } 436 /** 437 * @Description Initializing state of Sta. 438 * 439 */ 440 ErrCode InitStaStates(); 441 /** 442 * @Description The process of initializing connected wifi information. 443 * 444 */ 445 void InitWifiLinkedInfo(); 446 /** 447 * @Description The process of initializing the last connected wifi information. 448 * 449 */ 450 void InitLastWifiLinkedInfo(); 451 /** 452 * @Description Setting linkedInfo in case of when wpa connects 453 automatically there isn't any connection information. 454 * 455 * @param networkId - the nerworkId of network which is saved in the WifiLinkedInfo.(in) 456 */ 457 void SetWifiLinkedInfo(int networkId); 458 /** 459 * @Description Save the current connected state into WifiLinkedInfo. 460 * 461 * @param state - current connecting state(in) 462 * @param detailState - the current detail state of StaStateMachine.(in) 463 */ 464 void SaveLinkstate(ConnState state, DetailedState detailState); 465 /** 466 * @Description Translate frequency to band(2.4G or 5G). 467 * 468 * @param freQuency -the frequency needed to be translted into band.(in) 469 */ 470 void GetBandFromFreQuencies(const int &freQuency); 471 472 /** 473 * @Description Processing after a success response is returned after Wi-Fi 474 is enabled successfully, such as setting the MAC address and 475 saving the connection information. 476 * 477 */ 478 void StartWifiProcess(); 479 /** 480 * @Description Synchronize the deviceConfig structure to wpa_supplicant 481 */ 482 void SyncDeviceConfigToWpa() const; 483 /** 484 * @Description Update wifi status and save connection information. 485 * 486 * @param networkId - the networkId of selected network which is going to be connected(in) 487 * @param bssid - the mac address of wifi(in) 488 */ 489 void ConnectToNetworkProcess(InternalMessage *msg); 490 491 /** 492 * @Description On connect fail. 493 * 494 * @param networkId - the networkId of network which is going to be connected.(in) 495 */ 496 void OnConnectFailed(int networkId); 497 498 /** 499 * @Description Start to connect to network. 500 * 501 * @param networkId - the networkId of network which is going to be connected.(in) 502 * @Return success: WIFI_OPT_SUCCESS fail: WIFI_OPT_FAILED 503 */ 504 ErrCode StartConnectToNetwork(int networkId); 505 /** 506 * @Description Disable network 507 * 508 * @param networkId - the networkId of network which is going to be disabled.(in) 509 */ 510 ErrCode DisableNetwork(int networkId); 511 /** 512 * @Description Disconnect network 513 * 514 */ 515 void DisConnectProcess(); 516 /** 517 * @Description Disable wifi process. 518 * 519 */ 520 void StopWifiProcess(); 521 /** 522 * @Description Setting statemachine status during the process of enable or disable wifi. 523 * 524 * @param mode - operating mode(in) 525 */ 526 void SetOperationalMode(int mode); 527 void SetSuspendMode(bool enabled); 528 void SetPowerSave(bool enabled); 529 /** 530 * @Description Configure static ipaddress. 531 * 532 * @param staticIpAddress- static ip address(in) 533 */ 534 bool ConfigStaticIpAddress(StaticIpAddress &staticIpAddress); 535 int PortalHttpDetection(); 536 /** 537 * @Description the process of handling network check results. 538 * 539 * @param netState - the state of connecting network(in) 540 */ 541 void HandleNetCheckResult(StaNetState netState, const std::string portalUrl); 542 /** 543 * @Description Remove all device configurations before enabling WPS. 544 * 545 */ 546 void RemoveAllDeviceConfigs(); 547 /** 548 * @Description Synchronize all networks saved in the configuration center to the WPA. 549 * 550 */ 551 void SyncAllDeviceConfigs(); 552 /** 553 * @Description Initialize the connection state processing message map 554 * 555 */ 556 int InitStaSMHandleMap(); 557 /** 558 * @Description : Deal SignalPoll Result. 559 * 560 * @param msg - Message body received by the state machine[in] 561 */ 562 void DealSignalPollResult(InternalMessage *msg); 563 /** 564 * @Description : Converting frequencies to channels. 565 * 566 */ 567 void ConvertFreqToChannel(); 568 /** 569 * @Description Connect to selected network. 570 * 571 * @param msg - Message body received by the state machine[in] 572 */ 573 void DealConnectToSelectedNetCmd(InternalMessage *msg); 574 /** 575 * @Description : Ready to connect to the network selected by user. 576 * 577 * @param msg - Message body received by the state machine[in] 578 */ 579 void DealConnectToUserSelectedNetwork(InternalMessage *msg); 580 /** 581 * @Description Operations after the disconnection Event is reported. 582 * 583 * @param msg - Message body received by the state machine[in] 584 */ 585 void DealDisconnectEvent(InternalMessage *msg); 586 /** 587 * @Description Operations after the Connection Event is reported. 588 * 589 * @param msg - Message body received by the state machine[in] 590 */ 591 void DealConnectionEvent(InternalMessage *msg); 592 /** 593 * @Description Operations after Disable specified network commands. 594 * 595 * @param msg - Message body received by the state machine[in] 596 */ 597 void DealConnectTimeOutCmd(InternalMessage *msg); 598 /** 599 * @Description Operations after Clear blocklist is reported. 600 * 601 * @param msg - Message body received by the state machine[in] 602 */ 603 void DealWpaBlockListClearEvent(InternalMessage *msg); 604 /** 605 * @Description Operations after StartWps commands. 606 * 607 * @param msg - Message body received by the state machine[in] 608 */ 609 void DealStartWpsCmd(InternalMessage *msg); 610 /** 611 * @Description Operations after the Wps Connect TimeOut Event is reported. 612 * 613 * @param msg - Message body received by the state machine[in] 614 */ 615 void DealWpsConnectTimeOutEvent(InternalMessage *msg); 616 /** 617 * @Description Cancel wps connection 618 * 619 * @param msg - Message body received by the state machine[in] 620 */ 621 void DealCancelWpsCmd(InternalMessage *msg); 622 /** 623 * @Description Reconnect network 624 * 625 * @param msg - Message body received by the state machine[in] 626 */ 627 void DealReConnectCmd(InternalMessage *msg); 628 /** 629 * @Description Operations after the Reassociate lead is issued 630 * 631 * @param msg - Message body received by the state machine[in] 632 */ 633 void DealReassociateCmd(InternalMessage *msg); 634 /** 635 * @Description Roaming connection. 636 * 637 * @param msg - Message body received by the state machine[in] 638 */ 639 void DealStartRoamCmd(InternalMessage *msg); 640 /** 641 * @Description Operation after the password error is reported 642 * 643 * @param msg - Message body received by the state machine[in] 644 */ 645 void DealWpaLinkFailEvent(InternalMessage *msg); 646 /** 647 * @Description Wps mode is ON 648 * 649 * @param msg - Message body received by the state machine[in] 650 */ 651 void StartWpsMode(InternalMessage *msg); 652 /** 653 * @Description Reassociate network. 654 * 655 */ 656 void ReassociateProcess(); 657 658 /** 659 * @Description Set a random MAC address. 660 * 661 * @param networkId - network id[in] 662 */ 663 bool SetRandomMac(int networkId); 664 /** 665 * @Description Generate a random MAC address. 666 * 667 * @param strMac - Randomly generated MAC address[out] 668 */ 669 void MacAddressGenerate(std::string &strMac); 670 /** 671 * @Description Compare the encryption mode of the current network with that of the network in the scanning result. 672 * 673 * @param scanInfoKeymgmt - Network encryption mode in the scanning result[in] 674 * @param deviceKeymgmt - Encryption mode of the current network[in] 675 */ 676 bool ComparedKeymgmt(const std::string scanInfoKeymgmt, const std::string deviceKeymgmt); 677 /** 678 * @Description : Deal network check cmd. 679 * 680 * @param msg - Message body received by the state machine[in] 681 */ 682 void DealNetworkCheck(InternalMessage *msg); 683 #ifndef OHOS_ARCH_LITE 684 /** 685 * @Description Subscribe system ability changed. 686 */ 687 void SubscribeSystemAbilityChanged(void); 688 /** 689 * @Description On netmanager restart. 690 */ 691 void OnNetManagerRestart(void); 692 /** 693 * @Description Reupdate net supplier info 694 */ 695 void ReUpdateNetSupplierInfo(sptr<NetManagerStandard::NetSupplierInfo> supplierInfo); 696 697 /** 698 * @Description Reupdate net link inf 699 */ 700 void ReUpdateNetLinkInfo(void); 701 #endif // OHOS_ARCH_LITE 702 703 private: 704 StaSmHandleFuncMap staSmHandleFuncMap; 705 StaServiceCallback staCallback; 706 #ifndef OHOS_ARCH_LITE 707 sptr<NetManagerStandard::NetSupplierInfo> NetSupplierInfo; 708 sptr<ISystemAbilityStatusChange> statusChangeListener_; 709 #endif 710 711 int lastNetworkId; 712 int operationalMode; 713 int targetNetworkId; 714 int pinCode; 715 SetupMethod wpsState; 716 int lastSignalLevel; 717 std::string targetRoamBssid; 718 int currentTpType; 719 IsWpsConnected isWpsConnect; 720 int getIpSucNum; 721 int getIpFailNum; 722 bool isRoam; 723 WifiLinkedInfo linkedInfo; 724 WifiLinkedInfo lastLinkedInfo; 725 IDhcpService *pDhcpService; 726 DhcpResultNotify *pDhcpResultNotify; 727 StaNetworkCheck *pNetcheck; 728 729 RootState *pRootState; 730 InitState *pInitState; 731 WpaStartingState *pWpaStartingState; /* Starting wpa_supplicant state. */ 732 WpaStartedState *pWpaStartedState; /* Started wpa_supplicant state. */ 733 WpaStoppingState *pWpaStoppingState; /* Stopping wpa_supplicant state. */ 734 LinkState *pLinkState; 735 SeparatingState *pSeparatingState; 736 SeparatedState *pSeparatedState; 737 ApLinkedState *pApLinkedState; 738 StaWpsState *pWpsState; 739 GetIpState *pGetIpState; 740 LinkedState *pLinkedState; 741 ApRoamingState *pApRoamingState; 742 }; 743 } // namespace Wifi 744 } // namespace OHOS 745 #endif 746