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
16 #include "network_adapter.h"
17 #include <functional>
18 #include <memory>
19 #include <new>
20 #include <set>
21 #include <singleton.h>
22 #include <type_traits>
23 #include "i_net_conn_callback.h"
24 #include "net_specifier.h"
25 #include "net_conn_client.h"
26 #include "network_state.h"
27 #include "net_conn_constants.h"
28 #include "telephony_errors.h"
29 #include "core_service_client.h"
30 #include "log.h"
31
32 using namespace OHOS::NetManagerStandard;
33 using namespace OHOS::Telephony;
34 namespace OHOS::Request::Download {
35 constexpr int32_t INVALID_SLOT_ID = -1;
36
GetInstance()37 NetworkAdapter &NetworkAdapter::GetInstance()
38 {
39 static NetworkAdapter adapter;
40 return adapter;
41 }
42
RegOnNetworkChange(RegCallBack && callback)43 bool NetworkAdapter::RegOnNetworkChange(RegCallBack&& callback)
44 {
45 NetSpecifier netSpecifier;
46 NetAllCapabilities netAllCapabilities;
47 netAllCapabilities.netCaps_.insert(NetCap::NET_CAPABILITY_INTERNET);
48 netSpecifier.netCapabilities_ = netAllCapabilities;
49 sptr<NetSpecifier> specifier = new(std::nothrow) NetSpecifier(netSpecifier);
50 if (specifier == nullptr) {
51 DOWNLOAD_HILOGE("new operator error.specifier is nullptr");
52 return false;
53 }
54 sptr<NetConnCallbackObserver> observer = new(std::nothrow) NetConnCallbackObserver(*this);
55 if (observer == nullptr) {
56 DOWNLOAD_HILOGE("new operator error.observer is nullptr");
57 return false;
58 }
59 int nRet = DelayedSingleton<NetConnClient>::GetInstance()->RegisterNetConnCallback(specifier, observer, 0);
60 if (nRet == NETMANAGER_SUCCESS) {
61 callback_ = callback;
62 return true;
63 }
64 DOWNLOAD_HILOGE("Failed to register the callback retcode= %{public}d", nRet);
65 return false;
66 }
67
IsOnline()68 bool NetworkAdapter::IsOnline()
69 {
70 return isOnline_;
71 }
72
NetAvailable(sptr<NetHandle> & netHandle)73 int32_t NetworkAdapter::NetConnCallbackObserver::NetAvailable(sptr<NetHandle> &netHandle)
74 {
75 return 0;
76 }
77
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)78 int32_t NetworkAdapter::NetConnCallbackObserver::NetCapabilitiesChange(sptr <NetHandle> &netHandle,
79 const sptr <NetAllCapabilities> &netAllCap)
80 {
81 DOWNLOAD_HILOGD("Observe net capabilities change. start");
82 if (netAllCap->netCaps_.count(NetCap::NET_CAPABILITY_INTERNET)) {
83 netAdapter_.isOnline_ = true;
84 UpdateRoaming();
85 if (netAllCap->bearerTypes_.count(NetBearType::BEARER_CELLULAR)) {
86 DOWNLOAD_HILOGI("Bearer Cellular");
87 netAdapter_.networkInfo_.networkType_ = NETWORK_MOBILE;
88 netAdapter_.networkInfo_.isMetered_ = true;
89 } else if (netAllCap->bearerTypes_.count(NetBearType::BEARER_WIFI)) {
90 DOWNLOAD_HILOGI("Bearer Wifi");
91 netAdapter_.networkInfo_.networkType_ = NETWORK_WIFI;
92 netAdapter_.networkInfo_.isMetered_ = false;
93 }
94 if (netAdapter_.callback_ != nullptr) {
95 netAdapter_.callback_();
96 DOWNLOAD_HILOGD("NetCapabilitiesChange callback");
97 }
98 } else {
99 netAdapter_.isOnline_ = false;
100 }
101 DOWNLOAD_HILOGD("Observe net capabilities change. end");
102 return 0;
103 }
104
NetConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & info)105 int32_t NetworkAdapter::NetConnCallbackObserver::NetConnectionPropertiesChange(sptr<NetHandle> &netHandle,
106 const sptr<NetLinkInfo> &info)
107 {
108 return 0;
109 }
110
NetLost(sptr<NetHandle> & netHandle)111 int32_t NetworkAdapter::NetConnCallbackObserver::NetLost(sptr<NetHandle> &netHandle)
112 {
113 DOWNLOAD_HILOGD("Observe bearer cellular lost");
114 netAdapter_.networkInfo_.networkType_ = NETWORK_INVALID;
115 netAdapter_.networkInfo_.isMetered_ = false;
116 if (netAdapter_.callback_ != nullptr) {
117 netAdapter_.callback_();
118 DOWNLOAD_HILOGD("NetCapabilitiesChange callback");
119 }
120 return 0;
121 }
122
NetUnavailable()123 int32_t NetworkAdapter::NetConnCallbackObserver::NetUnavailable()
124 {
125 return 0;
126 }
127
NetBlockStatusChange(sptr<NetHandle> & netHandle,bool blocked)128 int32_t NetworkAdapter::NetConnCallbackObserver::NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked)
129 {
130 return 0;
131 }
132
UpdateRoaming()133 void NetworkAdapter::NetConnCallbackObserver::UpdateRoaming()
134 {
135 int32_t slotId = INVALID_SLOT_ID;
136 DelayedRefSingleton<CoreServiceClient>::GetInstance().GetPrimarySlotId(slotId);
137 if (slotId <= INVALID_SLOT_ID) {
138 DOWNLOAD_HILOGE("GetDefaultCellularDataSlotId InValidData");
139 return;
140 }
141 sptr<NetworkState> networkClient = nullptr;
142 DelayedRefSingleton<CoreServiceClient>::GetInstance().GetNetworkState(slotId, networkClient);
143 if (networkClient == nullptr) {
144 DOWNLOAD_HILOGE("networkState is nullptr");
145 return;
146 }
147 DOWNLOAD_HILOGI("Roaming = %{public}d", networkClient->IsRoaming());
148 netAdapter_.networkInfo_.isRoaming_ = networkClient->IsRoaming();
149 }
150
GetNetworkInfo()151 NetworkInfo NetworkAdapter::GetNetworkInfo()
152 {
153 return networkInfo_;
154 }
155 } // namespace OHOS::Request::Download
156