1 /* 2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_WIFI_NET_CHECK_H 17 #define OHOS_WIFI_NET_CHECK_H 18 19 #include <atomic> 20 #include <condition_variable> 21 #include <cstring> 22 #include <fstream> 23 #include <mutex> 24 #include <thread> 25 #include <unistd.h> 26 #include <vector> 27 #include "http_request.h" 28 #include "sta_define.h" 29 #include "wifi_errcode.h" 30 #include "wifi_log.h" 31 32 #define DEFAULT_PORTAL_HTTPS_URL "" 33 34 namespace OHOS { 35 namespace Wifi { 36 class StaNetworkCheck { 37 FRIEND_GTEST(StaNetworkCheck); 38 public: 39 explicit StaNetworkCheck(NetStateHandler handle); 40 virtual ~StaNetworkCheck(); 41 /** 42 * @Description : Start NetCheck thread 43 * 44 * @Return success : WIFI_OPT_SUCCESS failed : WIFI_OPT_FAILED 45 */ 46 virtual ErrCode InitNetCheckThread(); 47 /** 48 * @Description : wake up the DHCP processing thread. 49 * 50 * @param ipType - Type of IP to be obtained [in] 51 */ 52 virtual void SignalNetCheckThread(); 53 /** 54 * @Description : stop the NetCheck processing thread. 55 * 56 */ 57 virtual void StopNetCheckThread(); 58 59 /** 60 * @Description : Exit the NetCheck thread. 61 * 62 */ 63 virtual void ExitNetCheckThread(); 64 65 private: 66 std::thread *pDealNetCheckThread; 67 NetStateHandler netStateHandler; 68 std::atomic<StaNetState> lastNetState; 69 70 /** 71 * @Description : Detect Internet ability 72 * 73 */ 74 bool HttpDetection(); 75 /** 76 * @Description : NetCheck thread function 77 * 78 */ 79 void RunNetCheckThreadFunc(); 80 81 private: 82 std::mutex mMutex; 83 std::condition_variable mCondition; 84 std::condition_variable mCondition_timeout; 85 std::atomic<bool> isStopNetCheck; 86 std::atomic<bool> isExitNetCheckThread; 87 std::atomic<bool> isExited; 88 }; 89 } // namespace Wifi 90 } // namespace OHOS 91 #endif