1 /*
2 * Copyright (c) 2023 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 #include "dupdate_net_observer.h"
17
18 #include <pthread.h>
19 #include <thread>
20 #include <unistd.h>
21
22 #include "net_conn_client.h"
23 #include "net_conn_constants.h"
24
25 #include "string_utils.h"
26 #include "update_log.h"
27
28 using namespace OHOS::NetManagerStandard;
29
30 namespace OHOS {
31 namespace UpdateEngine {
SetCallback(const std::weak_ptr<INetObserverCallback> & callback)32 void NetObserver::SetCallback(const std::weak_ptr<INetObserverCallback> &callback)
33 {
34 callback_ = callback;
35 }
36
StartObserver()37 void NetObserver::StartObserver()
38 {
39 ENGINE_LOGI("StartObserver");
40 std::thread th = std::thread([this]() {
41 NetSpecifier netSpecifier;
42 NetAllCapabilities netAllCapabilities;
43 netAllCapabilities.netCaps_.insert(NetManagerStandard::NetCap::NET_CAPABILITY_INTERNET);
44 netSpecifier.ident_ = "";
45 netSpecifier.netCapabilities_ = netAllCapabilities;
46 sptr<NetSpecifier> specifier = new NetSpecifier(netSpecifier);
47 constexpr int32_t RETRY_MAX_TIMES = 10;
48 int32_t retryCount = 0;
49 int32_t ret = NetConnResultCode::NET_CONN_SUCCESS;
50 do {
51 ret = NetConnClient::GetInstance().RegisterNetConnCallback(specifier, this, 0);
52 if (ret == NetConnResultCode::NET_CONN_SUCCESS) {
53 ENGINE_LOGI("StartObserver register success");
54 return;
55 }
56 retryCount++;
57 ENGINE_LOGE("StartObserver retry, ret = %{public}d", ret);
58 sleep(1);
59 } while (retryCount < RETRY_MAX_TIMES);
60 ENGINE_LOGE("StartObserver failed");
61 });
62 th.detach();
63 }
64
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)65 int32_t NetObserver::NetCapabilitiesChange(sptr<NetHandle> &netHandle, const sptr<NetAllCapabilities> &netAllCap)
66 {
67 if (netAllCap == nullptr) {
68 return 0;
69 }
70 if (netAllCap->netCaps_.count(NET_CAPABILITY_INTERNET) <= 0) {
71 OnNetChange(NetType::NO_NET);
72 return 0;
73 }
74 ENGINE_LOGI("NetCapabilitiesChange NetAvailable");
75 if (netAllCap->bearerTypes_.count(BEARER_ETHERNET) > 0 || netAllCap->bearerTypes_.count(BEARER_WIFI) > 0) {
76 // wifi和以太网都映射为wifi
77 OnNetChange(NetType::NOT_METERED_WIFI);
78 return 0;
79 }
80 if (netAllCap->bearerTypes_.count(BEARER_WIFI_AWARE) > 0) {
81 OnNetChange(NetType::METERED_WIFI);
82 return 0;
83 }
84 if (netAllCap->bearerTypes_.count(BEARER_CELLULAR) > 0) {
85 OnNetChange(NetType::CELLULAR);
86 return 0;
87 }
88 return 0;
89 }
90
NetLost(sptr<NetHandle> & netHandle)91 int32_t NetObserver::NetLost(sptr<NetHandle> &netHandle)
92 {
93 ENGINE_LOGI("NetLost");
94 OnNetChange(NetType::NO_NET);
95 return 0;
96 }
97
OnNetChange(NetType netType)98 void NetObserver::OnNetChange(NetType netType)
99 {
100 ENGINE_LOGI("OnNetChange %{public}d", netType);
101 if (callback_.expired()) {
102 ENGINE_LOGI("callback is recycled");
103 return;
104 }
105 bool result = callback_.lock()->OnNetChange(netType);
106 ENGINE_LOGD("OnNetChange callback result %{public}s", StringUtils::GetBoolStr(result).c_str());
107 }
108 } // namespace UpdateEngine
109 } // namespace OHOS