1 /*
2  * Copyright (C) 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 #include "wifi_logger.h"
16 #include <unistd.h>
17 #include <pthread.h>
18 #include <thread>
19 #include "wifi_net_observer.h"
20 #include "net_conn_client.h"
21 #include "net_conn_constants.h"
22 #include "net_all_capabilities.h"
23 #include "wifi_hisysevent.h"
24 
25 DEFINE_WIFILOG_LABEL("WifiNetObserver");
26 
27 namespace OHOS {
28 namespace Wifi {
29 using namespace NetManagerStandard;
30 
NetStateObserver()31 NetStateObserver::NetStateObserver(): m_callback(nullptr)
32 {
33     WIFI_LOGD("construct NetStateObserver");
34 }
35 
~NetStateObserver()36 NetStateObserver::~NetStateObserver()
37 {
38     WIFI_LOGD("~NetStateObserver");
39 }
40 
SetNetStateCallback(const std::function<void (SystemNetWorkState,std::string)> & callback)41 void NetStateObserver::SetNetStateCallback(const std::function<void(SystemNetWorkState, std::string)> &callback)
42 {
43     m_callback = callback;
44 }
45 
StartNetStateObserver(sptr<NetStateObserver> & netStateObserverPtr)46 void NetStateObserver::StartNetStateObserver(sptr<NetStateObserver> &netStateObserverPtr)
47 {
48     int32_t netId = GetWifiNetId();
49     int32_t ret = NetManagerStandard::NetConnClient::GetInstance().RegisterNetDetectionCallback(
50         netId, netStateObserverPtr);
51     if (ret == 0) {
52         WIFI_LOGI("StartNetObserver register success, netId=%{public}d", netId);
53         return;
54     }
55     WIFI_LOGI("StartNetObserver failed, netId=%{public}d, ret=%{public}d", netId, ret);
56 }
57 
StopNetStateObserver(sptr<NetStateObserver> & netStateObserverPtr)58 void NetStateObserver::StopNetStateObserver(sptr<NetStateObserver> &netStateObserverPtr)
59 {
60     int32_t netId = GetWifiNetId();
61     int32_t ret = NetManagerStandard::NetConnClient::GetInstance().UnRegisterNetDetectionCallback(
62         netId, netStateObserverPtr);
63     if (ret == 0) {
64         WIFI_LOGI("StopNetObserver unregister success, netId=%{public}d", netId);
65         return;
66     }
67     WIFI_LOGI("StopNetObserver failed, netId=%{public}d, ret=%{public}d", netId, ret);
68 }
69 
OnNetDetectionResultChanged(NetManagerStandard::NetDetectionResultCode detectionResult,const std::string & urlRedirect)70 int32_t NetStateObserver::OnNetDetectionResultChanged(
71     NetManagerStandard::NetDetectionResultCode detectionResult, const std::string &urlRedirect)
72 {
73     WIFI_LOGD("OnNetDetectionResultChanged nettype:%{public}d, url:%{public}s", detectionResult, urlRedirect.c_str());
74     switch (detectionResult) {
75         case NetManagerStandard::NET_DETECTION_CAPTIVE_PORTAL: {
76             m_callback(SystemNetWorkState::NETWORK_IS_PORTAL, urlRedirect);
77             break;
78         }
79         case NetManagerStandard::NET_DETECTION_FAIL: {
80             m_callback(SystemNetWorkState::NETWORK_NOTWORKING, "");
81             break;
82         }
83         case NetManagerStandard::NET_DETECTION_SUCCESS: {
84             m_callback(SystemNetWorkState::NETWORK_IS_WORKING, "");
85             break;
86         }
87         default:
88             // do nothing
89             break;
90     }
91     return 0;
92 }
93 
GetWifiNetworkHandle()94 sptr<NetHandle> NetStateObserver::GetWifiNetworkHandle()
95 {
96     std::list<sptr<NetHandle>> netList;
97     int32_t ret = NetConnClient::GetInstance().GetAllNets(netList);
98     if (ret != NETMANAGER_SUCCESS) {
99         WIFI_LOGE("GetAllNets failed ret = %{public}d", ret);
100         return nullptr;
101     }
102     for (auto iter : netList) {
103         NetManagerStandard::NetAllCapabilities netAllCap;
104         NetConnClient::GetInstance().GetNetCapabilities(*iter, netAllCap);
105         if (netAllCap.bearerTypes_.count(NetManagerStandard::BEARER_WIFI) > 0) {
106             return iter;
107         }
108     }
109     WIFI_LOGE("GetWifiNetworkHandle not find wifi network");
110     return nullptr;
111 }
112 
StartWifiDetection()113 int32_t NetStateObserver::StartWifiDetection()
114 {
115     WIFI_LOGI("StartWifiDetection start");
116     sptr<NetHandle> netHandle = GetWifiNetworkHandle();
117     if (netHandle == nullptr) {
118         WIFI_LOGE("StartWifiDetection failed!");
119         return 1;
120     }
121     int32_t res = NetConnClient::GetInstance().NetDetection(*netHandle);
122     if (res != 0) {
123         WIFI_LOGE("StartWifiDetection failed %{public}d", res);
124         return 1;
125     }
126     return 0;
127 }
128 
GetWifiNetId()129 int32_t NetStateObserver::GetWifiNetId()
130 {
131     sptr<NetHandle> netHandle = GetWifiNetworkHandle();
132     if (netHandle != nullptr) {
133         return netHandle->GetNetId();
134     }
135     return 0;
136 }
137 }
138 }
139