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 #include "wifi_chr_utils.h" 35 #include "network_selection_manager.h" 36 37 #ifndef OHOS_ARCH_LITE 38 #include "want.h" 39 #include "wifi_net_agent.h" 40 #include "wifi_net_observer.h" 41 #ifdef EXTENSIBLE_AUTHENTICATION 42 #include "net_eap_observer.h" 43 #endif 44 #include "ienhance_service.h" 45 #include "iself_cure_service.h" 46 #include "appmgr/app_mgr_interface.h" 47 #include "wifi_common_event_helper.h" 48 #include "sta_sm_ext.h" 49 #ifdef WIFI_DATA_REPORT_ENABLE 50 #include "select_network_data_report.h" 51 #endif 52 #endif 53 54 namespace OHOS { 55 namespace Wifi { 56 #ifndef OHOS_ARCH_LITE 57 #endif 58 constexpr int STA_RENEWAL_MIN_TIME = 120; 59 constexpr int STREAM_TXPACKET_THRESHOLD = 0; 60 constexpr int STREAM_RXPACKET_THRESHOLD = 0; 61 constexpr int STA_AP_ROAMING_TIMEOUT = 15000; // 15s->15000 ms 62 constexpr int STA_NO_INTERNET_TIMEOUT = 60000; // 60s 63 constexpr int CMD_NETWORK_CONNECT_TIMEOUT = 0X01; 64 constexpr int CMD_SIGNAL_POLL = 0X02; 65 constexpr int CMD_START_NETCHECK = 0X03; 66 constexpr int CMD_START_GET_DHCP_IP_TIMEOUT = 0X04; 67 constexpr int CMD_AP_ROAMING_TIMEOUT_CHECK = 0X06; 68 constexpr int CMD_LINK_SWITCH_DETECT_TIMEOUT = 0x07; 69 constexpr int CMD_NO_INTERNET_TIMEOUT = 0x08; 70 71 constexpr int STA_NETWORK_CONNECTTING_DELAY = 20 * 1000; 72 constexpr int STA_SIGNAL_POLL_DELAY = 3 * 1000; 73 constexpr int STA_SIGNAL_POLL_DELAY_WITH_TASK = 1 * 1000; 74 constexpr int STA_SIGNAL_START_GET_DHCP_IP_DELAY = 30 * 1000; 75 constexpr int STA_LINK_SWITCH_DETECT_DURATION = 2000; // ms 76 77 /* pincode length */ 78 constexpr int PIN_CODE_LEN = 8; 79 80 /* DHCP timeout interval */ 81 constexpr int DHCP_TIME = 15; 82 /* rssi thresholds */ 83 constexpr int INVALID_RSSI_VALUE = -127; 84 constexpr int MAX_RSSI_VALUE = 200; 85 constexpr int SIGNAL_INFO = 256; 86 constexpr int RSSI_LEVEL_2 = 2; 87 constexpr int RSSI_LEVEL_3 = 3; 88 89 /* 2.4g and 5g frequency thresholds */ 90 constexpr int FREQ_2G_MIN = 2412; 91 constexpr int FREQ_2G_MAX = 2472; 92 constexpr int FREQ_5G_MIN = 5170; 93 constexpr int FREQ_5G_MAX = 5825; 94 constexpr int CHANNEL_14_FREQ = 2484; 95 constexpr int CHANNEL_14 = 14; 96 constexpr int CENTER_FREQ_DIFF = 5; 97 constexpr int CHANNEL_2G_MIN = 1; 98 constexpr int CHANNEL_5G_MIN = 34; 99 100 constexpr int MULTI_AP = 0; 101 102 /* Wpa3 selfcure failreason num*/ 103 constexpr int WLAN_STATUS_AUTH_TIMEOUT = 16; 104 constexpr int MAC_AUTH_RSP2_TIMEOUT = 5201; 105 constexpr int MAC_AUTH_RSP4_TIMEOUT = 5202; 106 constexpr int MAC_ASSOC_RSP_TIMEOUT = 5203; 107 constexpr int DHCP_RENEW_FAILED = 4; 108 constexpr int DHCP_RENEW_TIMEOUT = 5; 109 constexpr int DHCP_LEASE_EXPIRED = 6; 110 /* FoldState Status*/ 111 constexpr int RSSI_OFFSET_MIN = 0; 112 constexpr int RSSI_OFFSET_DEFAULT = 5; 113 constexpr int RSSI_OFFSET_MAX = 10; 114 115 constexpr unsigned int BIT_MLO_CONNECT = 0x80; 116 117 #define DNS_IP_ADDR_LEN 15 118 #define WIFI_FIRST_DNS_NAME "const.wifi.wifi_first_dns" 119 #define WIFI_SECOND_DNS_NAME "const.wifi.wifi_second_dns" 120 121 enum Wpa3ConnectFailReason { 122 WPA3_AUTH_TIMEOUT, 123 WPA3_ASSOC_TIMEOUT, 124 WPA3_FAIL_REASON_MAX 125 }; 126 127 enum CoFeatureType:uint8_t { 128 COFEATURE_TYPE_MLO = 0, 129 COFEATURE_TYPE_WUR = 1, 130 }; 131 132 typedef enum EnumDhcpReturnCode { 133 DHCP_RESULT, 134 DHCP_JUMP, 135 DHCP_RENEW_FAIL, 136 DHCP_IP_EXPIRED, 137 DHCP_FAIL, 138 DHCP_OFFER_REPORT, 139 } DhcpReturnCode; 140 141 enum FoldStatus { 142 UNKONWN = 0, 143 EXPAND, 144 FOLDED, 145 HALF_FOLD, 146 }; 147 148 inline const int DETECT_TYPE_DEFAULT = 0; 149 inline const int DETECT_TYPE_PERIODIC = 1; 150 inline const int DETECT_TYPE_CHECK_PORTAL_EXPERIED = 2; 151 inline const int PORTAL_EXPERIED_DETECT_MAX_COUNT = 2; 152 enum PortalState { 153 UNCHECKED = 0, 154 NOT_PORTAL, 155 UNAUTHED, 156 AUTHED, 157 EXPERIED 158 }; 159 160 const std::string WPA_BSSID_ANY = "any"; 161 162 class StaStateMachine : public StateMachine { 163 #ifndef OHOS_ARCH_LITE 164 friend class StaSMExt; 165 #ifdef WIFI_DATA_REPORT_ENABLE 166 friend class WifiDataReportService; 167 #endif 168 #endif 169 FRIEND_GTEST(StaStateMachine); 170 public: 171 explicit StaStateMachine(int instId = 0); 172 ~StaStateMachine(); 173 using staSmHandleFunc = std::function<void(InternalMessagePtr)>; 174 using StaSmHandleFuncMap = std::map<int, staSmHandleFunc>; 175 int foldStatus_ = 0; 176 /** 177 * @Description Definition of member function of State base class in StaStateMachine. 178 * 179 */ 180 class ClosedState : public State { 181 public: 182 explicit ClosedState(StaStateMachine *staStateMachine); 183 ~ClosedState() override; 184 void GoInState() override; 185 void GoOutState() override; 186 bool ExecuteStateMsg(InternalMessagePtr msg) override; 187 private: 188 void StartWifiProcess(); 189 void StopWifiProcess(); 190 StaStateMachine *pStaStateMachine; 191 }; 192 /** 193 * @Description : Definition of member function of InitState class in StaStateMachine. 194 * 195 */ 196 class InitState : public State { 197 public: 198 explicit InitState(StaStateMachine *staStateMachine); 199 ~InitState() override; 200 void GoInState() override; 201 void GoOutState() override; 202 bool ExecuteStateMsg(InternalMessagePtr msg) override; 203 private: 204 void StartConnectEvent(InternalMessagePtr msg); 205 void UpdateCountryCode(InternalMessagePtr msg); 206 bool AllowAutoConnect(); 207 #ifdef FEATURE_WIFI_MDM_RESTRICTED_SUPPORT 208 bool RestrictedByMdm(WifiDeviceConfig &config); 209 #endif 210 void HandleNetworkConnectionEvent(InternalMessagePtr msg); 211 void SaveFoldStatus(InternalMessagePtr msg); 212 bool NotAllowConnectToNetwork(int networkId, const std::string& bssid, int connTriggerMode); 213 bool NotExistInScanList(WifiDeviceConfig &config); 214 void DealScreenStateChangedEvent(InternalMessagePtr msg); 215 void DealHiddenSsidConnectMiss(int networkId); 216 StaStateMachine *pStaStateMachine; 217 }; 218 219 /** 220 * @Description Definition of member function of LinkState class in StaStateMachine. 221 * 222 */ 223 class LinkState : public State { 224 public: 225 explicit LinkState(StaStateMachine *staStateMachine); 226 ~LinkState() override; 227 void GoInState() override; 228 void GoOutState() override; 229 bool ExecuteStateMsg(InternalMessagePtr msg) override; 230 231 private: 232 StaSmHandleFuncMap staSmHandleFuncMap; 233 StaStateMachine *pStaStateMachine; 234 int InitStaSMHandleMap(); 235 void StopWifiProcessInLinkState(InternalMessagePtr msg); 236 void DealDisconnectEventInLinkState(InternalMessagePtr msg); 237 void DealConnectTimeOutCmd(InternalMessagePtr msg); 238 void DealNetworkRemoved(InternalMessagePtr msg); 239 void DealWpaStateChange(InternalMessagePtr msg); 240 void DealMloStateChange(InternalMessagePtr msg); 241 void DealWpaCustomEapAuthEvent(InternalMessagePtr msg); 242 }; 243 /** 244 * @Description Definition of member function of SeparatedState class in StaStateMachine. 245 * 246 */ 247 class SeparatedState : public State { 248 public: 249 explicit SeparatedState(StaStateMachine *staStateMachine); 250 ~SeparatedState() override; 251 void GoInState() override; 252 void GoOutState() override; 253 bool ExecuteStateMsg(InternalMessagePtr msg) override; 254 255 private: 256 StaStateMachine *pStaStateMachine; 257 void DealReConnectCmdInSeparatedState(InternalMessagePtr msg); 258 }; 259 /** 260 * @Description Definition of member function of ApLinkingState class in StaStateMachine. 261 * 262 */ 263 class ApLinkingState : public State { 264 public: 265 explicit ApLinkingState(StaStateMachine *staStateMachine); 266 ~ApLinkingState() override; 267 void GoInState() override; 268 void GoOutState() override; 269 bool ExecuteStateMsg(InternalMessagePtr msg) override; 270 271 private: 272 void HandleStaBssidChangedEvent(InternalMessagePtr msg); 273 void DealWpaLinkPasswdWrongFailEvent(InternalMessagePtr msg); 274 void DealWpaLinkFullConnectFailEvent(InternalMessagePtr msg); 275 void DealWpaLinkAssocRejectFailEvent(InternalMessagePtr msg); 276 void DealWpaLinkFailEvent(InternalMessagePtr msg); 277 private: 278 StaStateMachine *pStaStateMachine; 279 }; 280 /** 281 * @Description Definition of member function of ApLinkedState class in StaStateMachine. 282 * 283 */ 284 class ApLinkedState : public State { 285 public: 286 explicit ApLinkedState(StaStateMachine *staStateMachine); 287 ~ApLinkedState() override; 288 void GoInState() override; 289 void GoOutState() override; 290 bool ExecuteStateMsg(InternalMessagePtr msg) override; 291 292 private: 293 void HandleNetWorkConnectionEvent(InternalMessagePtr msg); 294 void HandleStaBssidChangedEvent(InternalMessagePtr msg); 295 void HandleLinkSwitchEvent(InternalMessagePtr msg); 296 void DealStartRoamCmdInApLinkedState(InternalMessagePtr msg); 297 void DealCsaChannelChanged(InternalMessagePtr msg); 298 void DealNoInternetTimeout(); 299 private: 300 StaStateMachine *pStaStateMachine; 301 }; 302 /** 303 * @Description Definition of member function of GetIpState class in StaStateMachine. 304 * 305 */ 306 class GetIpState : public State { 307 public: 308 explicit GetIpState(StaStateMachine *staStateMachine); 309 ~GetIpState() override; 310 void GoInState() override; 311 void GoOutState() override; 312 bool ExecuteStateMsg(InternalMessagePtr msg) override; 313 314 private: 315 bool IsPublicESS(); 316 bool IsProhibitUseCacheIp(); 317 void DealGetDhcpIpTimeout(InternalMessagePtr msg); 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 void UpdateExpandOffset(); 332 int halfFoldUpdateRssi_ = 0; 333 int halfFoldRssi_ = 0; 334 int expandRssi_ = 0; 335 int rssiOffset_ = RSSI_OFFSET_DEFAULT; 336 bool isExpandUpdateRssi_ = true; 337 private: 338 #ifndef OHOS_ARCH_LITE 339 void CheckIfRestoreWifi(); 340 #endif 341 void DhcpResultNotify(InternalMessagePtr msg); 342 void NetDetectionNotify(InternalMessagePtr msg); 343 void DealNetworkCheck(InternalMessagePtr msg); 344 void FoldStatusNotify(InternalMessagePtr msg); 345 StaStateMachine *pStaStateMachine; 346 }; 347 /** 348 * @Description Definition of member function of ApRoamingState class in StaStateMachine. 349 * 350 */ 351 class ApRoamingState : public State { 352 public: 353 explicit ApRoamingState(StaStateMachine *staStateMachine); 354 ~ApRoamingState() override; 355 void GoInState() override; 356 void GoOutState() override; 357 bool ExecuteStateMsg(InternalMessagePtr msg) override; 358 359 private: 360 bool HandleNetworkConnectionEvent(InternalMessagePtr msg); 361 void DealApRoamingStateTimeout(InternalMessagePtr msg); 362 StaStateMachine *pStaStateMachine; 363 }; 364 365 class DhcpResultNotify { 366 public: 367 /** 368 * @Description : Construct a new dhcp result notify object 369 * 370 */ 371 explicit DhcpResultNotify(StaStateMachine *staStateMachine); 372 373 /** 374 * @Description : Destroy the dhcp result notify object 375 * 376 */ 377 ~DhcpResultNotify(); 378 379 /** 380 * @Description : Get dhcp result of specified interface success notify asynchronously 381 * 382 * @param status - int 383 * @param ifname - interface name,eg:wlan0 384 * @param result - dhcp result 385 */ 386 static void OnSuccess(int status, const char *ifname, DhcpResult *result); 387 388 /** 389 * @Description : Get dhcp offer result of specified interface success notify asynchronously 390 * 391 * @param status - int 392 * @param ifname - interface name,eg:wlan0 393 * @param result - dhcp offer 394 */ 395 static void OnDhcpOffer(int status, const char *ifname, DhcpResult *result); 396 397 /** 398 * @Description : Get dhcp result of specified interface failed notify asynchronously 399 * 400 * @param status - int 401 * @param ifname - interface name,eg:wlan0 402 * @param reason - failed reason 403 */ 404 static void OnFailed(int status, const char *ifname, const char *reason); 405 void OnSuccessDhcpResult(int status, const char *ifname, DhcpResult *result); 406 void OnFailedDhcpResult(int status, const char *ifname, const char *reason); 407 void OnDhcpOfferResult(int status, const char *ifname, DhcpResult *result); 408 void DealDhcpResult(int ipType); 409 void DealDhcpResultFailed(); 410 void DealDhcpOfferResult(); 411 void Clear(); 412 private: 413 void TryToSaveIpV4Result(IpInfo &ipInfo, IpV6Info &ipv6Info, DhcpResult *result); 414 void TryToSaveIpV4ResultExt(IpInfo &ipInfo, IpV6Info &ipv6Info, DhcpResult *result); 415 void TryToSaveIpV6Result(IpInfo &ipInfo, IpV6Info &ipv6Info, DhcpResult *result); 416 void TryToCloseDhcpClient(int iptype); 417 void SaveDhcpResult(DhcpResult *dest, DhcpResult *source); 418 void SaveDhcpResultExt(DhcpResult *dest, DhcpResult *source); 419 void DhcpResultNotifyEvent(DhcpReturnCode result, int ipType = -1); 420 void ClearDhcpResult(DhcpResult *result); 421 static StaStateMachine *pStaStateMachineList[STA_INSTANCE_MAX_NUM]; 422 StaStateMachine *pStaStateMachine; 423 std::mutex dhcpResultMutex; 424 DhcpResult DhcpIpv4Result; 425 DhcpResult DhcpIpv6Result; 426 DhcpResult DhcpOfferInfo; 427 }; 428 429 public: 430 /** 431 * @Description Initialize StaStateMachine 432 * 433 * @Return: WIFI_OPT_SUCCESS - success WIFI_OPT_FAILED - failed 434 */ 435 ErrCode InitStaStateMachine(); 436 /** 437 * @Description Start roaming connection. 438 * 439 * @param networkId - the networkId 440 * @param bssid - the mac address of network(in) 441 */ 442 void StartConnectToBssid(const int32_t networkId, std::string bssid); 443 /** 444 * @Description Register sta callback function 445 * 446 * @param callback - Callback function pointer storage structure 447 */ 448 void RegisterStaServiceCallback(const StaServiceCallback &callback); 449 450 /** 451 * @Description unRegister sta callback function 452 * 453 * @param callback - Callback function pointer storage structure 454 */ 455 void UnRegisterStaServiceCallback(const StaServiceCallback &callback); 456 457 /** 458 * @Description Reupdate net link info 459 */ 460 void ReUpdateNetLinkInfo(const WifiDeviceConfig &config); 461 462 /** 463 * @Description On netmanager restart. 464 */ 465 void OnNetManagerRestart(void); 466 467 /** 468 * @Description : start detect timer. 469 * @param detectType - type of detect 470 */ 471 void StartDetectTimer(int detectType); 472 473 /** 474 * @Description start browser to login portal 475 * 476 */ 477 void HandlePortalNetworkPorcess(); 478 479 void SetPortalBrowserFlag(bool flag); 480 void DealApRoamingStateTimeout(InternalMessagePtr msg); 481 void DealHiLinkDataToWpa(InternalMessagePtr msg); 482 void HilinkSetMacAddress(std::string &cmd); 483 void DealWpaStateChange(InternalMessagePtr msg); 484 void GetDetectNetState(OperateResState &state); 485 /** 486 * @Description Save the disconnected reason. 487 * 488 * @param discReason - disconnected reason(in) 489 */ 490 void SaveDiscReason(DisconnectedReason discReason); 491 #ifdef FEATURE_WIFI_MDM_RESTRICTED_SUPPORT 492 void DealMdmRestrictedConnect(WifiDeviceConfig &config); 493 bool WhetherRestrictedByMdm(const std::string &ssid, const std::string &bssid, bool checkBssid); 494 #endif 495 #ifndef OHOS_ARCH_LITE 496 void SetEnhanceService(IEnhanceService* enhanceService); 497 void SetSelfCureService(ISelfCureService *selfCureService); 498 void UpdateAcceptUnvalidatedState(); 499 500 /** 501 * @Description: Handle Foreground App Changed Action 502 * 503 * @param msg - Message body received by the state machine[in] 504 */ 505 void HandleForegroundAppChangedAction(InternalMessagePtr msg); 506 #endif 507 /* ------------------ state machine private function ----------------- */ 508 private: 509 /** 510 * @Description Destruct state. 511 * 512 */ 513 template<typename T> ParsePointer(T * & pointer)514 inline void ParsePointer(T *&pointer) 515 { 516 if (pointer != nullptr) { 517 delete pointer; 518 pointer = nullptr; 519 } 520 } 521 /** 522 * @Description Build state tree 523 * 524 */ 525 void BuildStateTree(); 526 /** 527 * @Description Determine whether it is empty during initialization 528 * 529 */ 530 template<typename T> JudgmentEmpty(T * & pointer)531 inline ErrCode JudgmentEmpty(T *&pointer) 532 { 533 if (pointer == nullptr) { 534 return WIFI_OPT_FAILED; 535 } 536 return WIFI_OPT_SUCCESS; 537 } 538 539 bool SetMacToHal(const std::string ¤tMac, const std::string &realMac, int instId); 540 /** 541 * @Description Register dhcp client CallBack 542 * 543 * @Return: DHCP_OPT_SUCCESS - success DHCP_OPT_FAILED - failed 544 */ 545 int RegisterDhcpCallBack(); 546 547 /** 548 * @Description Initializing state of Sta. 549 * 550 */ 551 ErrCode InitStaStates(); 552 /** 553 * @Description The process of initializing connected wifi information. 554 * 555 */ 556 void InitWifiLinkedInfo(); 557 558 /** 559 * @Description Convert the deviceConfig structure and set it to idl structure 560 * 561 * @param config -The Network info(in) 562 * @param halDeviceConfig -The Network info(in) 563 * @Return success: WIFI_OPT_SUCCESS fail: WIFI_OPT_FAILED 564 */ 565 ErrCode FillEapCfg(const WifiDeviceConfig &config, WifiHalDeviceConfig &halDeviceConfig) const; 566 567 /** 568 * @Description Convert the deviceConfig structure and set it to wpa_supplicant 569 * 570 --=* @param config -The Network info(in) 571 * @Return success: WIFI_OPT_SUCCESS fail: WIFI_OPT_FAILED 572 */ 573 ErrCode ConvertDeviceCfg(WifiDeviceConfig &config, std::string bssid) const; 574 575 /** 576 * @Description Save the current connected state into WifiLinkedInfo. 577 * 578 * @param state - current connecting state(in) 579 * @param detailState - the current detail state of StaStateMachine.(in) 580 */ 581 void SaveLinkstate(ConnState state, DetailedState detailState); 582 583 /** 584 * @Description Update wifi status and save connection information. 585 * 586 * @param bssid - the mac address of wifi(in) 587 */ 588 void AfterApLinkedprocess(std::string bssid); 589 590 /** 591 * @Description Start to connect to network. 592 * 593 * @param networkId - the networkId of network which is going to be connected.(in) 594 * @param bssid - the bssid of network which is going to be connected. 595 * @Return success: WIFI_OPT_SUCCESS fail: WIFI_OPT_FAILED 596 */ 597 ErrCode StartConnectToNetwork(int networkId, const std::string &bssid, int connTriggerMode); 598 599 /** 600 * @Description User select connect to network. 601 * 602 * @param deviceConfig - Ap device config information 603 */ 604 void UserSelectConnectToNetwork(WifiDeviceConfig& deviceConfig, std::string& ifaceName); 605 606 /** 607 * @Description Auto select connect to network. 608 * 609 * @param bssid - the bssid of network which is going to be connected. 610 */ 611 void AutoSelectConnectToNetwork(const std::string& bssid, std::string& ifaceName); 612 613 /** 614 * @Description Disconnect network 615 * 616 */ 617 void StartDisConnectToNetwork(); 618 619 /** 620 * @Description Configure static ipaddress. 621 * 622 * @param staticIpAddress- static ip address(in) 623 */ 624 bool ConfigStaticIpAddress(StaticIpAddress &staticIpAddress); 625 626 /** 627 * @Description the process of handling network check results. 628 * 629 * @param netState the state of connecting network(in) 630 * @param portalUrl portal network redirection address 631 */ 632 void HandleNetCheckResult(SystemNetWorkState netState, const std::string &portalUrl); 633 634 /** 635 * @Description update portalState 636 * 637 * @param netState the state of connecting network(in) 638 * @param updatePortalAuthTime need update portalAuthTime or not [out] 639 */ 640 void UpdatePortalState(SystemNetWorkState netState, bool &updatePortalAuthTime); 641 642 /** 643 * @Description start detection if portalState is expired 644 */ 645 void PortalExpiredDetect(); 646 647 /** 648 * @Description implementation of the network detection callback function 649 * 650 * @param netState the state of connecting network 651 * @param url portal network redirection address 652 */ 653 void NetStateObserverCallback(SystemNetWorkState netState, std::string url); 654 655 /** 656 * @Description implementation of Register Eap Custom Handler 657 * 658 * @param eapCode Indicates eap code need to customize 659 * @param eapType Indicates eap type need to customize 660 */ 661 void RegisterCustomEapCallback(const std::string ®Cmd); 662 663 /** 664 * @Description implementation of Reply CustomEap Data 665 * 666 * @param eapBuf Indicates eap packet that customized 667 */ 668 void ReplyCustomEapDataCallback(int result, const std::string &strEapData); 669 670 /** 671 * @Description notification portal network. 672 * 673 */ 674 void PublishPortalNetworkNotification(); 675 676 /** 677 * @Description : Update RSSI to LinkedInfo. 678 * 679 * @param inRssi - Rssi get from SignalPoll Result 680 */ 681 int UpdateLinkInfoRssi(int inRssi); 682 683 /** 684 * @Description : Deal SignalPoll Result. 685 */ 686 void DealSignalPollResult(); 687 688 void DealMloLinkSignalPollResult(); 689 690 /** 691 * @Description : Update RSSI to LinkedInfo and public rssi changed broadcast. 692 * 693 * @param signalInfo - SignalPoll Result 694 */ 695 void UpdateLinkRssi(const WifiSignalPollInfo &signalInfo, int foldStateRssi = INVALID_RSSI_VALUE); 696 697 /** 698 * @Description : JudgeEnableSignalPoll. 699 * 700 * @param signalInfo -JudgeEnableSignalPoll 701 */ 702 void JudgeEnableSignalPoll(WifiSignalPollInfo &signalInfo); 703 /** 704 * @Description : Converting frequencies to channels. 705 * 706 */ 707 void ConvertFreqToChannel(); 708 709 /** 710 * @Description : send packet direction to hisysevent 711 * 712 */ 713 void DealSignalPacketChanged(int txPackets, int rxPackets); 714 715 /** 716 * @Description Connect to selected network. 717 * 718 * @param msg - Message body received by the state machine[in] 719 */ 720 void DealConnectToSelectedNetCmd(InternalMessagePtr msg); 721 722 /** 723 * @Description Operations after the Connection Event is reported. 724 * 725 * @param msg - Message body received by the state machine[in] 726 */ 727 void DealConnectionEventInApLinkingState(InternalMessagePtr msg); 728 729 /** 730 * @Description Operations after the Reassociate lead is issued 731 * 732 * @param msg - Message body received by the state machine[in] 733 */ 734 void DealReassociateCmd(InternalMessagePtr msg); 735 736 /** 737 * @Description set sta connect failed count 738 *@Return void 739 */ 740 void DealSetStaConnectFailedCount(int count, bool set); 741 742 /** 743 * @Description Reassociate network. 744 * 745 */ 746 void ReassociateProcess(); 747 748 /** 749 * @Description Set value of randomMacInfo. 750 * 751 * @param deviceConfig - deviceConfig[in] 752 * @param bssid - bssid[in] 753 * @param deviceConfig - randomMacInfo[out] 754 */ 755 void InitRandomMacInfo(const WifiDeviceConfig &deviceConfig, const std::string &bssid, 756 WifiStoreRandomMac &randomMacInfo); 757 758 /** 759 * @Description Set a random MAC address. 760 * 761 * @param deviceConfig - deviceConfig[in] 762 * @param bssid - bssid[in] 763 */ 764 bool SetRandomMac(WifiDeviceConfig &deviceConfig, const std::string &bssid); 765 766 /** 767 * @Description Generate a random MAC address. 768 * 769 * @param strMac - Randomly generated MAC address[out] 770 */ 771 void MacAddressGenerate(WifiStoreRandomMac &randomMacInfo); 772 773 /** 774 * @Description Compare the encryption mode of the current network with that of the network in the scanning result. 775 * 776 * @param scanInfoKeymgmt - Network encryption mode in the scanning result[in] 777 * @param deviceKeymgmt - Encryption mode of the current network[in] 778 */ 779 bool ComparedKeymgmt(const std::string scanInfoKeymgmt, const std::string deviceKeymgmt); 780 781 /** 782 * @Description : is wpa3 transition mode. 783 * 784 * @param ssid - ssid 785 */ 786 bool IsWpa3Transition(std::string ssid, std::string bssid) const; 787 788 /** 789 * @Description : get wpa3 failreason connect fail count 790 * 791 * @param failreason - auth or assoc fail 792 * @param ssid - ssid 793 */ 794 int GetWpa3FailCount(int failreason, std::string ssid) const; 795 796 /** 797 * @Description : add wpa3 failreason connect fail count 798 * 799 * @param failreason - auth or assoc fail 800 * @param ssid - ssid 801 */ 802 void AddWpa3FailCount(int failreason, std::string ssid); 803 804 /** 805 * @Description : add wpa3 black map 806 * 807 * @param ssid - ssid 808 */ 809 void AddWpa3BlackMap(std::string ssid); 810 811 /** 812 * @Description : is in wpa3 black map 813 * 814 * @param ssid - ssid 815 */ 816 bool IsInWpa3BlackMap(std::string ssid) const; 817 818 /** 819 * @Description : wpa3 transition selfcure 820 * 821 * @param failreason - auth or assoc fail 822 * @param networkId - networkId 823 */ 824 void OnWifiWpa3SelfCure(int failreason, int networkId); 825 826 /** 827 * @Description : Deal screen state change event. 828 * 829 * @param msg - Message body received by the state machine[in] 830 */ 831 void DealScreenStateChangedEvent(InternalMessagePtr msg); 832 833 /** 834 * @Description : Deal audio state change event. 835 * 836 * @param msg - Message body received by the state machine[in] 837 */ 838 void DealAudioStateChangedEvent(InternalMessagePtr msg); 839 840 /** 841 * @Description set external sim 842 * 843 * @param ifName - port name(in) 844 * @param eap - eap method(in) 845 * @Return success: 0 fail: others 846 */ 847 ErrCode SetExternalSim(const std::string ifName, const std::string &eap, int value) const; 848 849 /** 850 * @Description : should sta connect use factory mac address. 851 * 852 * @param networkId - networkId. 853 */ 854 bool ShouldUseFactoryMac(const WifiDeviceConfig &deviceConfig); 855 856 /** 857 * @Description : Check Current Connect is used randomized mac or not. 858 * 859 * @param networkId - networkId. 860 * @Return true: used randomized mac address. 861 */ 862 bool CurrentIsRandomizedMac(); 863 864 /** 865 * @Description : Check wpa report DisConnect reason is should stoptimer. 866 * 867 * @param reason - reason. 868 * @Return true: need stop timer. 869 */ 870 bool IsDisConnectReasonShouldStopTimer(int reason); 871 872 /** 873 * @Description : Hilink Save Data To Device Config. 874 * 875 */ 876 void HilinkSaveConfig(void); 877 878 /** 879 * @Description operation before dhcp 880 */ 881 void HandlePreDhcpSetup(); 882 883 /** 884 * @Description judge if specific network 885 */ 886 bool IsSpecificNetwork(); 887 888 /** 889 * @Description operation after dhcp 890 */ 891 void HandlePostDhcpSetup(); 892 893 /** 894 * @Description Get Wifi7 MLO link info. 895 */ 896 void DealMloConnectionLinkInfo(void); 897 898 #ifndef OHOS_ARCH_LITE 899 /** 900 * @Description verify simId. 901 * @param simId - sim id 902 * @Return int32_t - true: success, false: failed 903 */ 904 bool IsValidSimId(int32_t simId); 905 906 /** 907 * @Description Get SIM card authentication information. 908 * @param param - authentication information 909 * @Return int32_t - 0:success, other value:failed 910 */ 911 std::string GetGsmAuthResponseWithLength(EapSimGsmAuthParam param); 912 913 /** 914 * @Description Get SIM card authentication information. 915 * @param param - authentication information 916 * @Return int32_t - 0:success, other value:failed 917 */ 918 std::string GetGsmAuthResponseWithoutLength(EapSimGsmAuthParam param); 919 920 /** 921 * @Description sim authentication notify events 922 * 923 * @param msg: authentication data 924 */ 925 void DealWpaEapSimAuthEvent(InternalMessagePtr msg); 926 927 /** 928 * @Description deal register custom eap event 929 * 930 * @param msg: register param 931 */ 932 void DealRegCustomEapEvent(InternalMessagePtr msg); 933 934 /** 935 * @Description aka/aka' authentication Pre-process 936 * 937 */ 938 bool PreWpaEapUmtsAuthEvent(); 939 940 /** 941 * @Description fill aka/aka' authentication request message 942 * 943 * @param param: authentication data 944 */ 945 std::vector<uint8_t> FillUmtsAuthReq(EapSimUmtsAuthParam ¶m); 946 947 /** 948 * @Description fill aka/aka' authentication request message 949 * 950 * @param nonce: authentication data 951 */ 952 std::string ParseAndFillUmtsAuthParam(std::vector<uint8_t> &nonce); 953 954 /** 955 * @Description Get aka/aka' card authentication information 956 * 957 * @param param: authentication data 958 */ 959 std::string GetUmtsAuthResponse(EapSimUmtsAuthParam ¶m); 960 961 /** 962 * @Description aka/aka' authentication notify events 963 * 964 * @param msg: authentication data 965 */ 966 void DealWpaEapUmtsAuthEvent(InternalMessagePtr msg); 967 968 /** 969 * @Description Subscribe system ability changed. 970 */ 971 void SubscribeSystemAbilityChanged(void); 972 973 /** 974 * @Description save wificonfig for update mode. 975 * 976 * @param networkId - current connected networkId; 977 */ 978 void SaveWifiConfigForUpdate(int networkId); 979 void CloseNoInternetDialog(); 980 void SyncDeviceEverConnectedState(bool hasNet); 981 #endif // OHOS_ARCH_LITE 982 bool IsNewConnectionInProgress(); 983 void StopDhcp(bool isStopIpv4, bool isStopIpv6 = false); 984 /** 985 * @Description Replace empty dns 986 */ 987 void ReplaceEmptyDns(DhcpResult *result); 988 void InvokeOnStaConnChanged(OperateResState state, const WifiLinkedInfo &info); 989 void InvokeOnStaStreamChanged(StreamDirection direction); 990 void InvokeOnStaRssiLevelChanged(int level); 991 void InvokeOnDhcpOfferReport(IpInfo ipInfo); 992 WifiDeviceConfig getCurrentWifiDeviceConfig(); 993 void InsertOrUpdateNetworkStatusHistory(const NetworkStatus &networkStatus, bool updatePortalAuthTime); 994 bool CanArpReachable(); 995 void AddRandomMacCure(); 996 ErrCode ConfigRandMacSelfCure(const int networkId); 997 void UpdateLinkedBssid(std::string &bssid); 998 /** 999 * @Description broadcast network state for system UI and setting 1000 */ 1001 void InvokeOnInternetAccessChanged(SystemNetWorkState internetAccessStatus); 1002 void HandleInternetAccessChanged(SystemNetWorkState internetAccessStatus); 1003 #ifndef OHOS_ARCH_LITE 1004 void ShowPortalNitification(); 1005 void ResetWifi7WurInfo(); 1006 void UpdateLinkedInfoFromScanInfo(); 1007 void SetSupportedWifiCategory(); 1008 #endif 1009 void SetConnectMethod(int connectMethod); 1010 void FillSuiteB192Cfg(WifiHalDeviceConfig &halDeviceConfig) const; 1011 void FillWapiCfg(const WifiDeviceConfig &config, WifiHalDeviceConfig &halDeviceConfig) const; 1012 void TransHalDeviceConfig(WifiHalDeviceConfig &halDeviceConfig, const WifiDeviceConfig &config) const; 1013 void SetRandomMacConfig(WifiStoreRandomMac &randomMacInfo, const WifiDeviceConfig &deviceConfig, 1014 std::string ¤tMac); 1015 bool IsGoodSignalQuality(); 1016 void AppendFastTransitionKeyMgmt(const WifiScanInfo &scanInfo, WifiHalDeviceConfig &halDeviceConfig) const; 1017 void ConvertSsidToOriginalSsid(const WifiDeviceConfig &config, WifiHalDeviceConfig &halDeviceConfig) const; 1018 std::string GetSuitableKeyMgmtForWpaMixed(const WifiDeviceConfig &config, const std::string &bssid) const; 1019 void TryModifyPortalAttribute(SystemNetWorkState netState); 1020 void ChangePortalAttribute(bool isNeedChange, WifiDeviceConfig &config); 1021 void UpdateHiLinkAttribute(); 1022 void LogSignalInfo(WifiSignalPollInfo &signalInfo); 1023 void HandleNetCheckResultIsPortal(SystemNetWorkState netState, bool updatePortalAuthTime); 1024 void EnableScreenOffSignalPoll(); 1025 void PublishPortalNitificationAndLogin(); 1026 private: 1027 std::shared_mutex m_staCallbackMutex; 1028 std::map<std::string, StaServiceCallback> m_staCallback; 1029 bool m_hilinkFlag = false; 1030 WifiDeviceConfig m_hilinkDeviceConfig; 1031 #ifndef OHOS_ARCH_LITE 1032 bool hasNoInternetDialog_ = false; 1033 sptr<NetManagerStandard::NetSupplierInfo> NetSupplierInfo; 1034 sptr<NetStateObserver> m_NetWorkState; 1035 IEnhanceService *enhanceService_ = nullptr; /* EnhanceService handle */ 1036 ISelfCureService *selfCureService_ = nullptr; 1037 #endif 1038 1039 int targetNetworkId_; 1040 int lastSignalLevel_; 1041 std::string targetRoamBssid; 1042 int currentTpType; 1043 int getIpSucNum; 1044 int getIpFailNum; 1045 bool enableSignalPoll; 1046 bool isRoam; 1047 bool isCurrentRoaming_ = false; 1048 int64_t lastTimestamp; 1049 bool autoPullBrowserFlag; 1050 PortalState portalState; 1051 int detectNum; 1052 int portalExpiredDetectCount; 1053 bool mIsWifiInternetCHRFlag; 1054 bool networkStatusHistoryInserted; 1055 WifiLinkedInfo linkedInfo; 1056 DhcpResultNotify *pDhcpResultNotify; 1057 ClientCallBack dhcpclientCallBack_; 1058 DhcpClientReport dhcpClientReport_; 1059 ClosedState *pClosedState; 1060 InitState *pInitState; 1061 LinkState *pLinkState; 1062 SeparatedState *pSeparatedState; 1063 ApLinkingState *pApLinkingState; 1064 ApLinkedState *pApLinkedState; 1065 GetIpState *pGetIpState; 1066 LinkedState *pLinkedState; 1067 ApRoamingState *pApRoamingState; 1068 int m_instId; 1069 std::map<std::string, time_t> wpa3BlackMap; 1070 std::map<std::string, int> wpa3ConnectFailCountMapArray[WPA3_FAIL_REASON_MAX]; 1071 std::string mPortalUrl; 1072 int mLastConnectNetId; /* last request connect netword id */ 1073 int mConnectFailedCnt; /* mLastConnectNetId connect failed count */ 1074 std::string curForegroundAppBundleName_ = ""; 1075 int staSignalPollDelayTime_ = STA_SIGNAL_POLL_DELAY; 1076 OperateResState lastCheckNetState_ = OperateResState::CONNECT_NETWORK_NORELATED; 1077 int isAudioOn_ = 0; 1078 SystemNetWorkState lastInternetIconStatus_ = SystemNetWorkState::NETWORK_DEFAULT_STATE; 1079 int32_t noInternetAccessCnt_ = 0; 1080 /* 1081 linkswitch detect flag to avoid freq linkswitch cause signal level jump, 1082 set to true when linkswitch start, to false when linkswitch duration 2s later 1083 */ 1084 bool linkSwitchDetectingFlag_{false}; 1085 #ifndef OHOS_ARCH_LITE 1086 #ifdef WIFI_DATA_REPORT_ENABLE 1087 WifiDataReportService *wifiDataReportService_ = nullptr; 1088 #endif 1089 #endif 1090 }; 1091 } // namespace Wifi 1092 } // namespace OHOS 1093 #endif 1094