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 #include <shared_mutex> 23 #include "wifi_internal_msg.h" 24 #include "wifi_log.h" 25 #include "wifi_errcode.h" 26 #include "wifi_msg.h" 27 #include "state_machine.h" 28 #include "arp_checker.h" 29 #include "sta_service_callback.h" 30 #include "dhcp_c_api.h" 31 #include "sta_define.h" 32 #include "network_status_history_manager.h" 33 #include "wifi_native_struct.h" 34 35 #ifndef OHOS_ARCH_LITE 36 #include "want.h" 37 #include "wifi_net_agent.h" 38 #include "wifi_net_observer.h" 39 #include "sim_state_type.h" 40 #include "core_service_client.h" 41 #include "cellular_data_client.h" 42 #include "core_manager_inner.h" 43 #include "telephony_errors.h" 44 #include "ienhance_service.h" 45 #endif 46 47 namespace OHOS { 48 namespace Wifi { 49 #ifndef OHOS_ARCH_LITE 50 using namespace OHOS::Telephony; 51 #endif 52 constexpr int STA_CONNECT_MODE = 1; 53 constexpr int STA_SCAN_ONLY_MODE = 2; 54 constexpr int STA_CAN_ONLY_WITH_WIFI_OFF_MODE = 3; 55 constexpr int STA_DISABLED_MODE = 4; 56 constexpr int STA_RENEWAL_MIN_TIME = 120; 57 constexpr int STREAM_TXPACKET_THRESHOLD = 0; 58 constexpr int STREAM_RXPACKET_THRESHOLD = 0; 59 constexpr int STA_AP_ROAMING_TIMEOUT = 15000; // 15s->15000 ms 60 61 constexpr int CMD_NETWORK_CONNECT_TIMEOUT = 0X01; 62 constexpr int CMD_SIGNAL_POLL = 0X02; 63 constexpr int CMD_START_NETCHECK = 0X03; 64 constexpr int CMD_START_GET_DHCP_IP_TIMEOUT = 0X04; 65 constexpr int CMD_AP_ROAMING_TIMEOUT_CHECK = 0X06; 66 67 constexpr int STA_NETWORK_CONNECTTING_DELAY = 20 * 1000; 68 constexpr int STA_SIGNAL_POLL_DELAY = 3 * 1000; 69 constexpr int STA_SIGNAL_START_GET_DHCP_IP_DELAY = 30 * 1000; 70 71 /* pincode length */ 72 constexpr int PIN_CODE_LEN = 8; 73 74 /* DHCP timeout interval */ 75 constexpr int DHCP_TIME = 15; 76 /* rssi thresholds */ 77 constexpr int INVALID_RSSI_VALUE = -127; 78 constexpr int MAX_RSSI_VALUE = 200; 79 constexpr int SIGNAL_INFO = 256; 80 constexpr int RSSI_LEVEL_3 = 3; 81 82 /* 2.4g and 5g frequency thresholds */ 83 constexpr int FREQ_2G_MIN = 2412; 84 constexpr int FREQ_2G_MAX = 2472; 85 constexpr int FREQ_5G_MIN = 5170; 86 constexpr int FREQ_5G_MAX = 5825; 87 constexpr int CHANNEL_14_FREQ = 2484; 88 constexpr int CHANNEL_14 = 14; 89 constexpr int CENTER_FREQ_DIFF = 5; 90 constexpr int CHANNEL_2G_MIN = 1; 91 constexpr int CHANNEL_5G_MIN = 34; 92 93 constexpr int MULTI_AP = 0; 94 95 /* 96 * During the WPS PIN connection, the WPA_SUPPLICANT blocklist is cleared every 10 seconds 97 * until the network connection is successful. 98 */ 99 constexpr int BLOCK_LIST_CLEAR_TIMER = 20 * 1000; 100 101 /* Wpa3 selfcure failreason num*/ 102 constexpr int WLAN_STATUS_AUTH_TIMEOUT = 16; 103 constexpr int MAC_AUTH_RSP2_TIMEOUT = 5201; 104 constexpr int MAC_AUTH_RSP4_TIMEOUT = 5202; 105 constexpr int MAC_ASSOC_RSP_TIMEOUT = 5203; 106 constexpr int DHCP_RENEW_FAILED = 4; 107 constexpr int DHCP_RENEW_TIMEOUT = 5; 108 constexpr int DHCP_LEASE_EXPIRED = 6; 109 110 constexpr unsigned int BIT_MLO_CONNECT = 0x80; 111 112 #define DNS_IP_ADDR_LEN 15 113 #define WIFI_FIRST_DNS_NAME "const.wifi.wifi_first_dns" 114 #define WIFI_SECOND_DNS_NAME "const.wifi.wifi_second_dns" 115 116 enum Wpa3ConnectFailReason { 117 WPA3_AUTH_TIMEOUT, 118 WPA3_ASSOC_TIMEOUT, 119 WPA3_FAIL_REASON_MAX 120 }; 121 122 typedef enum EnumDhcpReturnCode { 123 DHCP_RESULT, 124 DHCP_JUMP, 125 DHCP_RENEW_FAIL, 126 DHCP_IP_EXPIRED, 127 DHCP_FAIL, 128 DHCP_OFFER_REPORT, 129 } DhcpReturnCode; 130 131 const int DETECT_TYPE_DEFAULT = 0; 132 const int DETECT_TYPE_PERIODIC = 1; 133 const int DETECT_TYPE_CHECK_PORTAL_EXPERIED = 2; 134 const int PORTAL_EXPERIED_DETECT_MAX_COUNT = 2; 135 enum PortalState { 136 UNCHECKED = 0, 137 NOT_PORTAL, 138 UNAUTHED, 139 AUTHED, 140 EXPERIED 141 }; 142 143 /* Signal levels are classified into: 0 1 2 3 4 ,the max is 4. */ 144 constexpr int MAX_LEVEL = 4; 145 const std::string WPA_BSSID_ANY = "any"; 146 147 class StaStateMachine : public StateMachine { 148 FRIEND_GTEST(StaStateMachine); 149 public: 150 explicit StaStateMachine(int instId = 0); 151 ~StaStateMachine(); 152 using staSmHandleFunc = std::function<void(InternalMessagePtr)>; 153 using StaSmHandleFuncMap = std::map<int, staSmHandleFunc>; 154 /** 155 * @Description Definition of member function of State base class in StaStateMachine. 156 * 157 */ 158 class RootState : public State { 159 public: 160 explicit RootState(); 161 ~RootState() override; 162 void GoInState() override; 163 void GoOutState() override; 164 bool ExecuteStateMsg(InternalMessagePtr msg) override; 165 }; 166 /** 167 * @Description : Definition of member function of InitState class in StaStateMachine. 168 * 169 */ 170 class InitState : public State { 171 public: 172 explicit InitState(StaStateMachine *staStateMachine); 173 ~InitState() override; 174 void GoInState() override; 175 void GoOutState() override; 176 bool ExecuteStateMsg(InternalMessagePtr msg) override; 177 178 private: 179 StaStateMachine *pStaStateMachine; 180 }; 181 /** 182 * @Description : Definition of member function of WpaStartingState class in StaStateMachine. 183 * 184 */ 185 class WpaStartingState : public State { 186 public: 187 explicit WpaStartingState(StaStateMachine *staStateMachine); 188 ~WpaStartingState() override; 189 void InitWpsSettings(); 190 void GoInState() override; 191 void GoOutState() override; 192 bool ExecuteStateMsg(InternalMessagePtr msg) override; 193 194 private: 195 StaStateMachine *pStaStateMachine; 196 }; 197 /** 198 * @Description Definition of member function of WpaStartedState class in StaStateMachine. 199 * 200 */ 201 class WpaStartedState : public State { 202 public: 203 explicit WpaStartedState(StaStateMachine *staStateMachine); 204 ~WpaStartedState() override; 205 void GoInState() override; 206 void GoOutState() override; 207 bool ExecuteStateMsg(InternalMessagePtr msg) override; 208 209 private: 210 StaStateMachine *pStaStateMachine; 211 }; 212 /** 213 * @Description Definition of member function of WpaStoppingState class in StaStateMachine. 214 * 215 */ 216 class WpaStoppingState : public State { 217 public: 218 explicit WpaStoppingState(StaStateMachine *staStateMachine); 219 ~WpaStoppingState() override; 220 void GoInState() override; 221 void GoOutState() override; 222 bool ExecuteStateMsg(InternalMessagePtr msg) override; 223 224 private: 225 StaStateMachine *pStaStateMachine; 226 }; 227 /** 228 * @Description Definition of member function of LinkState class in StaStateMachine. 229 * 230 */ 231 class LinkState : public State { 232 public: 233 explicit LinkState(StaStateMachine *staStateMachine); 234 ~LinkState() override; 235 void GoInState() override; 236 void GoOutState() override; 237 bool ExecuteStateMsg(InternalMessagePtr msg) override; 238 239 private: 240 StaStateMachine *pStaStateMachine; 241 }; 242 /** 243 * @Description Definition of member function of SeparatingState class in StaStateMachine. 244 * 245 */ 246 class SeparatingState : public State { 247 public: 248 explicit SeparatingState(); 249 ~SeparatingState() override; 250 void GoInState() override; 251 void GoOutState() override; 252 bool ExecuteStateMsg(InternalMessagePtr msg) override; 253 }; 254 /** 255 * @Description Definition of member function of SeparatedState class in StaStateMachine. 256 * 257 */ 258 class SeparatedState : public State { 259 public: 260 explicit SeparatedState(StaStateMachine *staStateMachine); 261 ~SeparatedState() override; 262 void GoInState() override; 263 void GoOutState() override; 264 bool ExecuteStateMsg(InternalMessagePtr msg) override; 265 266 private: 267 StaStateMachine *pStaStateMachine; 268 }; 269 /** 270 * @Description Definition of member function of ApLinkedState class in StaStateMachine. 271 * 272 */ 273 class ApLinkedState : public State { 274 public: 275 explicit ApLinkedState(StaStateMachine *staStateMachine); 276 ~ApLinkedState() override; 277 void GoInState() override; 278 void GoOutState() override; 279 bool ExecuteStateMsg(InternalMessagePtr msg) override; 280 281 private: 282 void HandleNetWorkConnectionEvent(InternalMessagePtr msg); 283 void HandleStaBssidChangedEvent(InternalMessagePtr msg); 284 void HandleLinkSwitchEvent(InternalMessagePtr msg); 285 private: 286 StaStateMachine *pStaStateMachine; 287 }; 288 /** 289 * @Description Definition of member function of WpsState class in StaStateMachine. 290 * 291 */ 292 class StaWpsState : public State { 293 public: 294 explicit StaWpsState(StaStateMachine *staStateMachine); 295 ~StaWpsState() override; 296 void GoInState() override; 297 void GoOutState() override; 298 bool ExecuteStateMsg(InternalMessagePtr msg) override; 299 300 private: 301 StaStateMachine *pStaStateMachine; 302 }; 303 /** 304 * @Description Definition of member function of GetIpState class in StaStateMachine. 305 * 306 */ 307 class GetIpState : public State { 308 public: 309 explicit GetIpState(StaStateMachine *staStateMachine); 310 ~GetIpState() override; 311 void GoInState() override; 312 void GoOutState() override; 313 bool ExecuteStateMsg(InternalMessagePtr msg) override; 314 315 private: 316 bool IsPublicESS(); 317 bool IsProhibitUseCacheIp(); 318 StaStateMachine *pStaStateMachine; 319 }; 320 /** 321 * @Description Definition of member function of LinkedState class in StaStateMachine. 322 * 323 */ 324 class LinkedState : public State { 325 public: 326 explicit LinkedState(StaStateMachine *staStateMachine); 327 ~LinkedState() override; 328 void GoInState() override; 329 void GoOutState() override; 330 bool ExecuteStateMsg(InternalMessagePtr msg) override; 331 332 private: 333 #ifndef OHOS_ARCH_LITE 334 void CheckIfRestoreWifi(); 335 #endif 336 StaStateMachine *pStaStateMachine; 337 }; 338 /** 339 * @Description Definition of member function of ApRoamingState class in StaStateMachine. 340 * 341 */ 342 class ApRoamingState : public State { 343 public: 344 explicit ApRoamingState(StaStateMachine *staStateMachine); 345 ~ApRoamingState() override; 346 void GoInState() override; 347 void GoOutState() override; 348 bool ExecuteStateMsg(InternalMessagePtr msg) override; 349 350 private: 351 bool HandleNetworkConnectionEvent(InternalMessagePtr msg); 352 StaStateMachine *pStaStateMachine; 353 }; 354 355 class DhcpResultNotify { 356 public: 357 /** 358 * @Description : Construct a new dhcp result notify object 359 * 360 */ 361 explicit DhcpResultNotify(); 362 363 /** 364 * @Description : Destroy the dhcp result notify object 365 * 366 */ 367 ~DhcpResultNotify(); 368 369 /** 370 * @Description : Get dhcp result of specified interface success notify asynchronously 371 * 372 * @param status - int 373 * @param ifname - interface name,eg:wlan0 374 * @param result - dhcp result 375 */ 376 static void OnSuccess(int status, const char *ifname, DhcpResult *result); 377 378 /** 379 * @Description : Get dhcp offer result of specified interface success notify asynchronously 380 * 381 * @param status - int 382 * @param ifname - interface name,eg:wlan0 383 * @param result - dhcp offer 384 */ 385 static void OnDhcpOfferResult(int status, const char *ifname, DhcpResult *result); 386 387 /** 388 * @Description : deal dhcp result 389 * 390 */ 391 void DealDhcpResult(int ipType); 392 393 /** 394 * @Description : Get dhcp result of specified interface failed notify asynchronously 395 * 396 * @param status - int 397 * @param ifname - interface name,eg:wlan0 398 * @param reason - failed reason 399 */ 400 static void OnFailed(int status, const char *ifname, const char *reason); 401 /** 402 * @Description : deal dhcp result failed 403 * 404 */ 405 void DealDhcpResultFailed(); 406 void DealDhcpOfferResult(); 407 static void SetStaStateMachine(StaStateMachine *staStateMachine); 408 static void TryToSaveIpV4Result(IpInfo &ipInfo, IpV6Info &ipv6Info, DhcpResult *result); 409 static void TryToSaveIpV4ResultExt(IpInfo &ipInfo, IpV6Info &ipv6Info, DhcpResult *result); 410 static void TryToSaveIpV6Result(IpInfo &ipInfo, IpV6Info &ipv6Info, DhcpResult *result); 411 static void TryToCloseDhcpClient(int iptype); 412 static void SaveDhcpResult(DhcpResult *dest, DhcpResult *source); 413 static void SaveDhcpResultExt(DhcpResult *dest, DhcpResult *source); 414 private: 415 static StaStateMachine *pStaStateMachine; 416 static DhcpResult DhcpIpv4Result; 417 static DhcpResult DhcpIpv6Result; 418 static DhcpResult DhcpOfferInfo; 419 }; 420 421 public: 422 /** 423 * @Description Register dhcp client CallBack 424 * 425 * @Return: DHCP_OPT_SUCCESS - success DHCP_OPT_FAILED - failed 426 */ 427 int RegisterCallBack(); 428 429 /** 430 * @Description Initialize StaStateMachine 431 * 432 * @Return: WIFI_OPT_SUCCESS - success WIFI_OPT_FAILED - failed 433 */ 434 ErrCode InitStaStateMachine(); 435 /** 436 * @Description Start roaming connection. 437 * 438 * @param bssid - the mac address of network(in) 439 */ 440 void StartRoamToNetwork(std::string bssid); 441 /** 442 * @Description if it is roaming now. 443 */ 444 bool IsRoaming(void); 445 /** 446 * @Description Connecting events 447 * 448 * @param networkId - the networkId of network which is going to be connected(in) 449 * @param bssid - bssid - the mac address of wifi(in) 450 */ 451 void OnNetworkConnectionEvent(int networkId, std::string bssid); 452 /** 453 * @Description Disconnect events 454 * 455 * @param reason - the reason of wifi disconnection 456 */ 457 void OnNetworkDisconnectEvent(int reason); 458 /** 459 * @Description sta chr events 460 * 461 * @param state - the state of wifi sta 462 */ 463 void OnNetworkHiviewEvent(int state); 464 /** 465 * @Description Assoc events 466 * 467 * @param reason - the state of wifi assoc 468 */ 469 void OnNetworkAssocEvent(int assocState, std::string bssid, StaStateMachine *pStaStateMachine); 470 /** 471 * @Description Bssid change events 472 * 473 * @param reason: the reason of bssid changed(in) 474 * @param bssid: the mac address of wifi(in) 475 */ 476 void OnBssidChangedEvent(std::string reason, std::string bssid); 477 /** 478 * @Description dhcp result notify events 479 * 480 * @param result: true-success, false-fail(in) 481 */ 482 void OnDhcpResultNotifyEvent(DhcpReturnCode result, int ipType = -1); 483 /** 484 * @Description Register sta callback function 485 * 486 * @param callback - Callback function pointer storage structure 487 */ 488 void RegisterStaServiceCallback(const StaServiceCallback &callback); 489 490 /** 491 * @Description unRegister sta callback function 492 * 493 * @param callback - Callback function pointer storage structure 494 */ 495 void UnRegisterStaServiceCallback(const StaServiceCallback &callback); 496 497 /** 498 * @Description Convert the deviceConfig structure and set it to idl structure 499 * 500 * @param config -The Network info(in) 501 * @param halDeviceConfig -The Network info(in) 502 * @Return success: WIFI_OPT_SUCCESS fail: WIFI_OPT_FAILED 503 */ 504 ErrCode FillEapCfg(const WifiDeviceConfig &config, WifiHalDeviceConfig &halDeviceConfig) const; 505 506 /** 507 * @Description Convert the deviceConfig structure and set it to wpa_supplicant 508 * 509 * @param config -The Network info(in) 510 * @Return success: WIFI_OPT_SUCCESS fail: WIFI_OPT_FAILED 511 */ 512 ErrCode ConvertDeviceCfg(const WifiDeviceConfig &config) const; 513 514 /** 515 * @Description Get linked info. 516 * 517 * @param linkedInfo - linked info 518 * @return int - operation result 519 */ 520 int GetLinkedInfo(WifiLinkedInfo& linkedInfo); 521 522 /** 523 * @Description Reupdate net link info 524 */ 525 void ReUpdateNetLinkInfo(const WifiDeviceConfig &config); 526 527 /** 528 * @Description On netmanager restart. 529 */ 530 void OnNetManagerRestart(void); 531 532 /** 533 * @Description : start detect timer. 534 * @param detectType - type of detect 535 */ 536 void StartDetectTimer(int detectType); 537 538 /** 539 * @Description start browser to login portal 540 * 541 */ 542 void HandlePortalNetworkPorcess(); 543 544 void SetPortalBrowserFlag(bool flag); 545 int GetInstanceId(); 546 void DealApRoamingStateTimeout(InternalMessagePtr msg); 547 void DealHiLinkDataToWpa(InternalMessagePtr msg); 548 void HilinkSetMacAddress(std::string &cmd); 549 void DealWpaStateChange(InternalMessagePtr msg); 550 void DealNetworkRemoved(InternalMessagePtr msg); 551 #ifndef OHOS_ARCH_LITE 552 void SetEnhanceService(IEnhanceService* enhanceService); 553 void SyncDeviceEverConnectedState(bool hasNet); 554 #endif 555 556 bool SetMacToHal(const std::string ¤tMac, const std::string &realMac, int instId); 557 private: 558 /** 559 * @Description Destruct state. 560 * 561 */ 562 template<typename T> ParsePointer(T * & pointer)563 inline void ParsePointer(T *&pointer) 564 { 565 if (pointer != nullptr) { 566 delete pointer; 567 pointer = nullptr; 568 } 569 } 570 /** 571 * @Description Build state tree 572 * 573 */ 574 void BuildStateTree(); 575 /** 576 * @Description Determine whether it is empty during initialization 577 * 578 */ 579 template<typename T> JudgmentEmpty(T * & pointer)580 inline ErrCode JudgmentEmpty(T *&pointer) 581 { 582 if (pointer == nullptr) { 583 return WIFI_OPT_FAILED; 584 } 585 return WIFI_OPT_SUCCESS; 586 } 587 /** 588 * @Description Initializing state of Sta. 589 * 590 */ 591 ErrCode InitStaStates(); 592 /** 593 * @Description The process of initializing connected wifi information. 594 * 595 */ 596 void InitWifiLinkedInfo(); 597 /** 598 * @Description The process of initializing the last connected wifi information. 599 * 600 */ 601 void InitLastWifiLinkedInfo(); 602 /** 603 * @Description Get device config information. 604 * 605 * @param bassid - the mac address of wifi(in). 606 * @param deviceConfig - the device config(out). 607 */ 608 void GetDeviceCfgInfo(const std::string& bssid, WifiDeviceConfig &deviceConfig); 609 /** 610 * @Description Setting linkedInfo in case of when wpa connects 611 automatically there isn't any connection information. 612 * 613 * @param networkId - the nerworkId of network which is saved in the WifiLinkedInfo.(in) 614 */ 615 void SetWifiLinkedInfo(int networkId); 616 617 /** 618 * @Description Save the current connected state into WifiLinkedInfo. 619 * 620 * @param state - current connecting state(in) 621 * @param detailState - the current detail state of StaStateMachine.(in) 622 */ 623 void SaveLinkstate(ConnState state, DetailedState detailState); 624 625 /** 626 * @Description Save the disconnected reason. 627 * 628 * @param discReason - disconnected reason(in) 629 */ 630 void SaveDiscReason(DisconnectedReason discReason); 631 632 /** 633 * @Description Translate frequency to band(2.4G or 5G). 634 * 635 * @param freQuency -the frequency needed to be translted into band.(in) 636 */ 637 void GetBandFromFreQuencies(const int &freQuency); 638 639 /** 640 * @Description Processing after a success response is returned after Wi-Fi 641 is enabled successfully, such as setting the MAC address and 642 saving the connection information. 643 * 644 */ 645 void StartWifiProcess(); 646 647 /** 648 * @Description Update wifi status and save connection information. 649 * 650 * @param bssid - the mac address of wifi(in) 651 */ 652 void ConnectToNetworkProcess(std::string bssid); 653 654 /** 655 * @Description Update wifi device config after wifi connected. 656 * 657 * @param deviceConfig - deviceConfig 658 * @param bssid - the mac address of wifi(in) 659 */ 660 void UpdateDeviceConfigAfterWifiConnected(WifiDeviceConfig &deviceConfig, const std::string &bssid); 661 662 /** 663 * @Description On connect fail. 664 * 665 * @param networkId - the networkId of network which is going to be connected.(in) 666 */ 667 void OnConnectFailed(int networkId); 668 669 /** 670 * @Description Start to connect to network. 671 * 672 * @param networkId - the networkId of network which is going to be connected.(in) 673 * @param bssid - the bssid of network which is going to be connected. 674 * @Return success: WIFI_OPT_SUCCESS fail: WIFI_OPT_FAILED 675 */ 676 ErrCode StartConnectToNetwork(int networkId, const std::string &bssid); 677 678 /** 679 * @Description Disconnect network 680 * 681 */ 682 void DisConnectProcess(); 683 684 /** 685 * @Description Disable wifi process. 686 * 687 */ 688 void StopWifiProcess(); 689 /** 690 * @Description Setting statemachine status during the process of enable or disable wifi. 691 * 692 * @param mode - operating mode(in) 693 */ 694 void SetOperationalMode(int mode); 695 void SetSuspendMode(bool enabled); 696 void SetPowerMode(bool mode); 697 void SetPowerSave(bool enabled); 698 699 /** 700 * @Description Configure static ipaddress. 701 * 702 * @param staticIpAddress- static ip address(in) 703 */ 704 bool ConfigStaticIpAddress(StaticIpAddress &staticIpAddress); 705 706 /** 707 * @Description the process of handling network check results. 708 * 709 * @param netState the state of connecting network(in) 710 * @param portalUrl portal network redirection address 711 */ 712 void HandleNetCheckResult(SystemNetWorkState netState, const std::string &portalUrl); 713 714 /** 715 * @Description update portalState 716 * 717 * @param netState the state of connecting network(in) 718 * @param updatePortalAuthTime need update portalAuthTime or not [out] 719 */ 720 void UpdatePortalState(SystemNetWorkState netState, bool &updatePortalAuthTime); 721 722 /** 723 * @Description start detection if portalState is expired 724 */ 725 void PortalExpiredDetect(); 726 727 /** 728 * @Description implementation of the network detection callback function 729 * 730 * @param netState the state of connecting network 731 * @param url portal network redirection address 732 */ 733 void NetStateObserverCallback(SystemNetWorkState netState, std::string url); 734 735 /** 736 * @Description the process of handling arp check results. 737 * 738 * @param arpState - the state of arp proto(in) 739 */ 740 void HandleArpCheckResult(StaArpState arpState); 741 742 /** 743 * @Description the process of handling network check results. 744 * 745 * @param dnsState - the state of dns protol(in) 746 */ 747 void HandleDnsCheckResult(StaDnsState dnsState); 748 749 /** 750 * @Description notification portal network. 751 * 752 */ 753 void PublishPortalNetworkNotification(); 754 755 /** 756 * @Description Remove all device configurations before enabling WPS. 757 * 758 */ 759 void RemoveAllDeviceConfigs(); 760 761 /** 762 * @Description Initialize the connection state processing message map 763 * 764 */ 765 int InitStaSMHandleMap(); 766 767 /** 768 * @Description : Update RSSI to LinkedInfo. 769 * 770 * @param inRssi - Rssi get from SignalPoll Result 771 */ 772 int UpdateLinkInfoRssi(int inRssi); 773 774 /** 775 * @Description : Deal SignalPoll Result. 776 */ 777 void DealSignalPollResult(); 778 779 /** 780 * @Description : Update RSSI to LinkedInfo and public rssi changed broadcast. 781 * 782 * @param signalInfo - SignalPoll Result 783 */ 784 void UpdateLinkRssi(const WifiHalWpaSignalInfo &signalInfo); 785 786 /** 787 * @Description : Converting frequencies to channels. 788 * 789 */ 790 void ConvertFreqToChannel(); 791 792 /** 793 * @Description : send packet direction to hisysevent 794 * 795 */ 796 void DealSignalPacketChanged(int txPackets, int rxPackets); 797 798 /** 799 * @Description Connect to selected network. 800 * 801 * @param msg - Message body received by the state machine[in] 802 */ 803 void DealConnectToSelectedNetCmd(InternalMessagePtr msg); 804 805 /** 806 * @Description : Ready to connect to the network selected by user. 807 * 808 * @param msg - Message body received by the state machine[in] 809 */ 810 void DealConnectToUserSelectedNetwork(InternalMessagePtr msg); 811 812 /** 813 * @Description Operations after the disconnection Event is reported. 814 * 815 * @param msg - Message body received by the state machine[in] 816 */ 817 void DealDisconnectEvent(InternalMessagePtr msg); 818 819 /** 820 * @Description Operations after the Connection Event is reported. 821 * 822 * @param msg - Message body received by the state machine[in] 823 */ 824 void DealConnectionEvent(InternalMessagePtr msg); 825 826 /** 827 * @Description Operations after Disable specified network commands. 828 * 829 * @param msg - Message body received by the state machine[in] 830 */ 831 void DealConnectTimeOutCmd(InternalMessagePtr msg); 832 833 /** 834 * @Description Operations after Clear blocklist is reported. 835 * 836 * @param msg - Message body received by the state machine[in] 837 */ 838 void DealWpaBlockListClearEvent(InternalMessagePtr msg); 839 840 /** 841 * @Description Operations after StartWps commands. 842 * 843 * @param msg - Message body received by the state machine[in] 844 */ 845 void DealStartWpsCmd(InternalMessagePtr msg); 846 847 /** 848 * @Description Operations after the Wps Connect TimeOut Event is reported. 849 * 850 * @param msg - Message body received by the state machine[in] 851 */ 852 void DealWpsConnectTimeOutEvent(InternalMessagePtr msg); 853 854 /** 855 * @Description Cancel wps connection 856 * 857 * @param msg - Message body received by the state machine[in] 858 */ 859 void DealCancelWpsCmd(InternalMessagePtr msg); 860 861 /** 862 * @Description Reconnect network 863 * 864 * @param msg - Message body received by the state machine[in] 865 */ 866 void DealReConnectCmd(InternalMessagePtr msg); 867 868 /** 869 * @Description Operations after the Reassociate lead is issued 870 * 871 * @param msg - Message body received by the state machine[in] 872 */ 873 void DealReassociateCmd(InternalMessagePtr msg); 874 875 /** 876 * @Description Roaming connection. 877 * 878 * @param msg - Message body received by the state machine[in] 879 */ 880 void DealStartRoamCmd(InternalMessagePtr msg); 881 882 /** 883 * @Description Operation after the password error is reported 884 * 885 * @param msg - Message body received by the state machine[in] 886 */ 887 void DealWpaLinkFailEvent(InternalMessagePtr msg); 888 889 /** 890 * @Description set sta connect failed count 891 *@Return void 892 */ 893 void DealSetStaConnectFailedCount(int count, bool set); 894 895 /** 896 * @Description Wps mode is ON 897 * 898 * @param msg - Message body received by the state machine[in] 899 */ 900 void StartWpsMode(InternalMessagePtr msg); 901 902 /** 903 * @Description Reassociate network. 904 * 905 */ 906 void ReassociateProcess(); 907 908 /** 909 * @Description Set value of randomMacInfo. 910 * 911 * @param deviceConfig - deviceConfig[in] 912 * @param bssid - bssid[in] 913 * @param deviceConfig - randomMacInfo[out] 914 */ 915 void InitRandomMacInfo(const WifiDeviceConfig &deviceConfig, const std::string &bssid, 916 WifiStoreRandomMac &randomMacInfo); 917 918 /** 919 * @Description Set a random MAC address. 920 * 921 * @param networkId - network id[in] 922 */ 923 bool SetRandomMac(int networkId, const std::string &bssid); 924 925 /** 926 * @Description check whether the current bssid are consistent. 927 * 928 * @param bssid - bssid 929 */ 930 bool CheckRoamingBssidIsSame(std::string bssid); 931 932 /** 933 * @Description Generate a random MAC address. 934 * 935 * @param strMac - Randomly generated MAC address[out] 936 */ 937 void MacAddressGenerate(WifiStoreRandomMac &randomMacInfo); 938 939 /** 940 * @Description Compare the encryption mode of the current network with that of the network in the scanning result. 941 * 942 * @param scanInfoKeymgmt - Network encryption mode in the scanning result[in] 943 * @param deviceKeymgmt - Encryption mode of the current network[in] 944 */ 945 bool ComparedKeymgmt(const std::string scanInfoKeymgmt, const std::string deviceKeymgmt); 946 947 /** 948 * @Description : Deal network check cmd. 949 * 950 * @param msg - Message body received by the state machine[in] 951 */ 952 void DealNetworkCheck(InternalMessagePtr msg); 953 954 /** 955 * @Description : Deal get dhcp ip timeout. 956 * 957 * @param msg - Message body received by the state machine[in] 958 */ 959 void DealGetDhcpIpTimeout(InternalMessagePtr msg); 960 961 /** 962 * @Description : is wpa3 transition mode. 963 * 964 * @param ssid - ssid 965 */ 966 bool IsWpa3Transition(std::string ssid) const; 967 968 /** 969 * @Description : get wpa3 failreason connect fail count 970 * 971 * @param failreason - auth or assoc fail 972 * @param ssid - ssid 973 */ 974 int GetWpa3FailCount(int failreason, std::string ssid) const; 975 976 /** 977 * @Description : add wpa3 failreason connect fail count 978 * 979 * @param failreason - auth or assoc fail 980 * @param ssid - ssid 981 */ 982 void AddWpa3FailCount(int failreason, std::string ssid); 983 984 /** 985 * @Description : add wpa3 black map 986 * 987 * @param ssid - ssid 988 */ 989 void AddWpa3BlackMap(std::string ssid); 990 991 /** 992 * @Description : is in wpa3 black map 993 * 994 * @param ssid - ssid 995 */ 996 bool IsInWpa3BlackMap(std::string ssid) const; 997 998 /** 999 * @Description : wpa3 transition selfcure 1000 * 1001 * @param failreason - auth or assoc fail 1002 * @param networkId - networkId 1003 */ 1004 void OnWifiWpa3SelfCure(int failreason, int networkId); 1005 1006 /** 1007 * @Description : Deal screen state change event. 1008 * 1009 * @param msg - Message body received by the state machine[in] 1010 */ 1011 void DealScreenStateChangedEvent(InternalMessagePtr msg); 1012 1013 /** 1014 * @Description set external sim 1015 * 1016 * @param ifName - port name(in) 1017 * @param eap - eap method(in) 1018 * @Return success: 0 fail: others 1019 */ 1020 ErrCode SetExternalSim(const std::string ifName, const std::string &eap, int value) const; 1021 1022 /** 1023 * @Description : should sta connect use factory mac address. 1024 * 1025 * @param networkId - networkId. 1026 */ 1027 bool ShouldUseFactoryMac(const WifiDeviceConfig &deviceConfig); 1028 1029 /** 1030 * @Description : Check Current Connect is used randomized mac or not. 1031 * 1032 * @param networkId - networkId. 1033 * @Return true: used randomized mac address. 1034 */ 1035 bool CurrentIsRandomizedMac(); 1036 1037 /** 1038 * @Description : Check wpa report DisConnect reason is should stoptimer. 1039 * 1040 * @param reason - reason. 1041 * @Return true: need stop timer. 1042 */ 1043 bool IsDisConnectReasonShouldStopTimer(int reason); 1044 1045 /** 1046 * @Description : Hilink Save Data To Device Config. 1047 * 1048 */ 1049 void HilinkSaveConfig(void); 1050 1051 /** 1052 * @Description operation before dhcp 1053 */ 1054 void HandlePreDhcpSetup(); 1055 1056 /** 1057 * @Description operation after dhcp 1058 */ 1059 void HandlePostDhcpSetup(); 1060 1061 #ifndef OHOS_ARCH_LITE 1062 /** 1063 * @Description Get slot id. 1064 * @Return int32_t - 0:success, other value:failed 1065 */ 1066 int32_t GetDataSlotId(int32_t slotId); 1067 1068 /** 1069 * @Description Get card type. 1070 * @param cardType - card type 1071 * @Return int32_t - 0:success, other value:failed 1072 */ 1073 int32_t GetCardType(CardType &cardType); 1074 1075 /** 1076 * @Description Get default slot id. 1077 * @param slotId - slot id 1078 * @Return int32_t - 0 success, other value:failed 1079 */ 1080 int32_t GetDefaultId(int32_t slotId); 1081 1082 /** 1083 * @Description Get card state. 1084 * @param slotId - slot id 1085 * @Return int32_t - card state 1086 */ 1087 int32_t GetSimCardState(int32_t slotId); 1088 1089 /** 1090 * @Description verify simId. 1091 * @param simId - sim id 1092 * @Return int32_t - true: success, false: failed 1093 */ 1094 bool IsValidSimId(int32_t simId); 1095 1096 /** 1097 * @Description Check whether the SIM card is a multi-SIM card. 1098 * @Return int32_t - true: success, false: failed 1099 */ 1100 bool IsMultiSimEnabled(); 1101 1102 /** 1103 * @Description sim authenticate 1104 * @param nonce - sim id 1105 * @Return int32_t - 0:success, other value:failed 1106 */ 1107 std::string SimAkaAuth(const std::string &nonce, AuthType authType); 1108 1109 /** 1110 * @Description Get SIM card authentication information. 1111 * @param param - authentication information 1112 * @Return int32_t - 0:success, other value:failed 1113 */ 1114 std::string GetGsmAuthResponseWithLength(EapSimGsmAuthParam param); 1115 1116 /** 1117 * @Description Get SIM card authentication information. 1118 * @param param - authentication information 1119 * @Return int32_t - 0:success, other value:failed 1120 */ 1121 std::string GetGsmAuthResponseWithoutLength(EapSimGsmAuthParam param); 1122 1123 /** 1124 * @Description sim authentication notify events 1125 * 1126 * @param msg: authentication data 1127 */ 1128 void DealWpaEapSimAuthEvent(InternalMessagePtr msg); 1129 1130 /** 1131 * @Description aka/aka' authentication Pre-process 1132 * 1133 */ 1134 bool PreWpaEapUmtsAuthEvent(); 1135 1136 /** 1137 * @Description fill aka/aka' authentication request message 1138 * 1139 * @param param: authentication data 1140 */ 1141 std::vector<uint8_t> FillUmtsAuthReq(EapSimUmtsAuthParam ¶m); 1142 1143 /** 1144 * @Description fill aka/aka' authentication request message 1145 * 1146 * @param nonce: authentication data 1147 */ 1148 std::string ParseAndFillUmtsAuthParam(std::vector<uint8_t> &nonce); 1149 1150 /** 1151 * @Description Get aka/aka' card authentication information 1152 * 1153 * @param param: authentication data 1154 */ 1155 std::string GetUmtsAuthResponse(EapSimUmtsAuthParam ¶m); 1156 1157 /** 1158 * @Description aka/aka' authentication notify events 1159 * 1160 * @param msg: authentication data 1161 */ 1162 void DealWpaEapUmtsAuthEvent(InternalMessagePtr msg); 1163 1164 /** 1165 * @Description Get the SIM card ID. 1166 * 1167 */ 1168 int32_t GetSimId(); 1169 1170 /** 1171 * @Description Set the SIM card ID. 1172 * 1173 * @param id - Sim card id 1174 */ 1175 void SetSimId(int32_t simId); 1176 1177 /** 1178 * @Description Subscribe system ability changed. 1179 */ 1180 void SubscribeSystemAbilityChanged(void); 1181 1182 /** 1183 * @Description save wificonfig for update mode. 1184 * 1185 * @param networkId - current connected networkId; 1186 */ 1187 void SaveWifiConfigForUpdate(int networkId); 1188 #endif // OHOS_ARCH_LITE 1189 1190 private: 1191 StaSmHandleFuncMap staSmHandleFuncMap; 1192 std::shared_mutex m_staCallbackMutex; 1193 std::map<std::string, StaServiceCallback> m_staCallback; 1194 bool m_hilinkFlag = false; 1195 WifiDeviceConfig m_hilinkDeviceConfig; 1196 #ifndef OHOS_ARCH_LITE 1197 sptr<NetManagerStandard::NetSupplierInfo> NetSupplierInfo; 1198 sptr<NetStateObserver> m_NetWorkState; 1199 IEnhanceService *enhanceService_ = nullptr; /* EnhanceService handle */ 1200 #endif 1201 1202 int lastNetworkId; 1203 int operationalMode; 1204 int targetNetworkId; 1205 int pinCode; 1206 SetupMethod wpsState; 1207 int lastSignalLevel_; 1208 std::string targetRoamBssid; 1209 int currentTpType; 1210 IsWpsConnected isWpsConnect; 1211 int getIpSucNum; 1212 int getIpFailNum; 1213 bool enableSignalPoll; 1214 bool isRoam; 1215 int64_t lastTimestamp; 1216 bool portalFlag; 1217 PortalState portalState; 1218 int detectNum; 1219 int portalExpiredDetectCount; 1220 bool mIsWifiInternetCHRFlag; 1221 bool networkStatusHistoryInserted; 1222 WifiLinkedInfo linkedInfo; 1223 WifiLinkedInfo lastLinkedInfo; 1224 DhcpResultNotify *pDhcpResultNotify; 1225 ClientCallBack clientCallBack; 1226 DhcpClientReport dhcpClientReport_; 1227 RootState *pRootState; 1228 InitState *pInitState; 1229 WpaStartingState *pWpaStartingState; /* Starting wpa_supplicant state. */ 1230 WpaStartedState *pWpaStartedState; /* Started wpa_supplicant state. */ 1231 WpaStoppingState *pWpaStoppingState; /* Stopping wpa_supplicant state. */ 1232 LinkState *pLinkState; 1233 SeparatingState *pSeparatingState; 1234 SeparatedState *pSeparatedState; 1235 ApLinkedState *pApLinkedState; 1236 StaWpsState *pWpsState; 1237 GetIpState *pGetIpState; 1238 LinkedState *pLinkedState; 1239 ApRoamingState *pApRoamingState; 1240 int m_instId; 1241 std::map<std::string, time_t> wpa3BlackMap; 1242 std::map<std::string, int> wpa3ConnectFailCountMapArray[WPA3_FAIL_REASON_MAX]; 1243 std::string mPortalUrl; 1244 int mLastConnectNetId; /* last request connect netword id */ 1245 int mConnectFailedCnt; /* mLastConnectNetId connect failed count */ 1246 /** 1247 * @Description Replace empty dns 1248 */ 1249 void ReplaceEmptyDns(DhcpResult *result); 1250 void InvokeOnStaConnChanged(OperateResState state, const WifiLinkedInfo &info); 1251 void InvokeOnWpsChanged(WpsStartState state, const int code); 1252 void InvokeOnStaStreamChanged(StreamDirection direction); 1253 void InvokeOnStaRssiLevelChanged(int level); 1254 void InvokeOnDhcpOfferReport(IpInfo ipInfo); 1255 WifiDeviceConfig getCurrentWifiDeviceConfig(); 1256 void InsertOrUpdateNetworkStatusHistory(const NetworkStatus &networkStatus, bool updatePortalAuthTime); 1257 bool CanArpReachable(); 1258 void AddRandomMacCure(); 1259 ErrCode ConfigRandMacSelfCure(const int networkId); 1260 #ifndef OHOS_ARCH_LITE 1261 void ShowPortalNitification(); 1262 void UpdateWifiCategory(); 1263 void SetSupportedWifiCategory(); 1264 #endif 1265 void SetConnectMethod(int connectMethod); 1266 void FillSuiteB192Cfg(WifiHalDeviceConfig &halDeviceConfig) const; 1267 void FillWapiCfg(const WifiDeviceConfig &config, WifiHalDeviceConfig &halDeviceConfig) const; 1268 void TransHalDeviceConfig(WifiHalDeviceConfig &halDeviceConfig, const WifiDeviceConfig &config) const; 1269 void SetRandomMacConfig(WifiStoreRandomMac &randomMacInfo, const WifiDeviceConfig &deviceConfig, 1270 std::string ¤tMac); 1271 bool IsGoodSignalQuality(); 1272 void AppendFastTransitionKeyMgmt(const WifiScanInfo &scanInfo, WifiHalDeviceConfig &halDeviceConfig) const; 1273 void ConvertSsidToOriginalSsid(const WifiDeviceConfig &config, WifiHalDeviceConfig &halDeviceConfig) const; 1274 }; 1275 } // namespace Wifi 1276 } // namespace OHOS 1277 #endif 1278