1 /*
2  * Copyright (c) 2024 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.h"
17 
18 #include <cstdint>
19 
20 #include "cxx.h"
21 #include "log.h"
22 #include "manage/network.rs.h"
23 #include "net_all_capabilities.h"
24 #include "net_conn_callback_stub.h"
25 #include "net_conn_client.h"
26 #include "net_specifier.h"
27 #include "refbase.h"
28 
29 #ifdef REQUEST_TELEPHONY_CORE_SERVICE
30 #include "cellular_data_client.h"
31 #include "core_service_client.h"
32 #endif
33 
34 #ifdef REQUEST_TELEPHONY_CORE_SERVICE
35 #include "iservice_registry.h"
36 #include "network_state.h"
37 #include "system_ability_definition.h"
38 #include "telephony_errors.h"
39 #endif
40 
41 namespace OHOS::Request {
42 using namespace OHOS::NetManagerStandard;
43 
RequestNetCallbackStub( rust::box<NetworkInner> network, rust::box<NetworkTaskManagerTx> task_manager, rust::fn<void(const NetworkTaskManagerTx &task_manager)> notifyTaskManagerOnline, rust::fn<void(const NetworkTaskManagerTx &task_manager)> notifyTaskManagerOffline )44 RequestNetCallbackStub::RequestNetCallbackStub(
45     rust::box<NetworkInner> network, rust::box<NetworkTaskManagerTx> task_manager,
46     rust::fn<void(const NetworkTaskManagerTx &task_manager)> notifyTaskManagerOnline,
47     rust::fn<void(const NetworkTaskManagerTx &task_manager)> notifyTaskManagerOffline
48 
49 )
50 {
51     networkNotifier_ = network.into_raw();
52     task_manager_ = task_manager.into_raw();
53     notifyTaskManagerOnline_ = notifyTaskManagerOnline;
54     notifyTaskManagerOffline_ = notifyTaskManagerOffline;
55 }
56 
~RequestNetCallbackStub()57 RequestNetCallbackStub::~RequestNetCallbackStub()
58 {
59     rust::Box<NetworkInner>::from_raw(networkNotifier_);
60     rust::Box<NetworkTaskManagerTx>::from_raw(task_manager_);
61 }
62 
HandleNetCap(const sptr<NetAllCapabilities> &netAllCap)63 void RequestNetCallbackStub::HandleNetCap(const sptr<NetAllCapabilities> &netAllCap)
64 {
65     for (auto bearerType : netAllCap->bearerTypes_) {
66         auto networkInfo = NetworkInfo();
67         if (bearerType == NetManagerStandard::NetBearType::BEARER_WIFI) {
68             networkInfo.network_type = NetworkType::Wifi;
69             networkInfo.is_metered = false;
70             networkInfo.is_roaming = false;
71 
72             if (networkNotifier_->notify_online(networkInfo)) {
73                 notifyTaskManagerOnline_(*task_manager_);
74             }
75             return;
76         } else if (bearerType == NetManagerStandard::NetBearType::BEARER_CELLULAR) {
77             networkInfo.network_type = NetworkType::Cellular;
78             networkInfo.is_metered = true;
79             networkInfo.is_roaming = this->IsRoaming();
80 
81             if (networkNotifier_->notify_online(networkInfo)) {
82                 notifyTaskManagerOnline_(*task_manager_);
83             }
84             return;
85         };
86     }
87     if (networkNotifier_->notify_online(NetworkInfo{
88             .network_type = NetworkType::Other,
89             .is_metered = false,
90             .is_roaming = false,
91         })) {
92         notifyTaskManagerOnline_(*task_manager_);
93     }
94     return;
95 }
96 
NetAvailable(sptr<NetHandle> &netHandle)97 int32_t RequestNetCallbackStub::NetAvailable(sptr<NetHandle> &netHandle)
98 {
99     sptr<NetAllCapabilities> netAllCap = sptr<NetAllCapabilities>::MakeSptr();
100     int32_t ret = NetConnClient::GetInstance().GetNetCapabilities(*netHandle, *netAllCap);
101     if (ret != 0) {
102         REQUEST_HILOGE("GetNetCapabilities failed, ret = %{public}d", ret);
103         return ret;
104     }
105     this->HandleNetCap(netAllCap);
106     return 0;
107 }
108 
NetLost(sptr<NetHandle> &netHandle)109 int32_t RequestNetCallbackStub::NetLost(sptr<NetHandle> &netHandle)
110 {
111     networkNotifier_->notify_offline();
112     notifyTaskManagerOffline_(*task_manager_);
113     return 0;
114 }
115 
NetUnavailable()116 int32_t RequestNetCallbackStub::NetUnavailable()
117 {
118     networkNotifier_->notify_offline();
119     notifyTaskManagerOffline_(*task_manager_);
120     return 0;
121 }
122 
NetCapabilitiesChange( sptr<NetHandle> &netHandle, const sptr<NetAllCapabilities> &netAllCap)123 int32_t RequestNetCallbackStub::NetCapabilitiesChange(
124     sptr<NetHandle> &netHandle, const sptr<NetAllCapabilities> &netAllCap)
125 {
126     REQUEST_HILOGI("NetCapabilitiesChange");
127     this->HandleNetCap(netAllCap);
128     return 0;
129 }
130 
IsRoaming()131 bool RequestNetCallbackStub::IsRoaming()
132 {
133 #ifdef REQUEST_TELEPHONY_CORE_SERVICE
134     REQUEST_HILOGD("upload roaming");
135     // Check telephony SA.
136     {
137         std::lock_guard<std::mutex> lock(roamingMutex_);
138 
139         auto sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
140         if (sm == nullptr) {
141             REQUEST_HILOGE("GetSystemAbilityManager return null");
142             return false;
143         }
144         auto systemAbility = sm->CheckSystemAbility(TELEPHONY_CORE_SERVICE_SYS_ABILITY_ID);
145         if (systemAbility == nullptr) {
146             REQUEST_HILOGE("Telephony SA not found");
147             return false;
148         }
149     }
150 
151     constexpr int32_t INVALID_SLOT_ID = -1;
152     int32_t maxSlotNum = DelayedRefSingleton<OHOS::Telephony::CoreServiceClient>::GetInstance().GetMaxSimCount();
153     bool isSim = false;
154     for (int32_t i = 0; i < maxSlotNum; ++i) {
155         if (DelayedRefSingleton<OHOS::Telephony::CoreServiceClient>::GetInstance().IsSimActive(i)) {
156             isSim = true;
157             break;
158         }
159     }
160     if (!isSim) {
161         REQUEST_HILOGD("no sim");
162         return false;
163     }
164 
165     int32_t slotId =
166         DelayedRefSingleton<OHOS::Telephony::CellularDataClient>::GetInstance().GetDefaultCellularDataSlotId();
167     if (slotId <= INVALID_SLOT_ID) {
168         REQUEST_HILOGE("GetDefaultCellularDataSlotId InValidData");
169         return false;
170     }
171     sptr<OHOS::Telephony::NetworkState> networkClient = nullptr;
172     DelayedRefSingleton<OHOS::Telephony::CoreServiceClient>::GetInstance().GetNetworkState(slotId, networkClient);
173     if (networkClient == nullptr) {
174         REQUEST_HILOGE("networkState is nullptr");
175         return false;
176     }
177     REQUEST_HILOGI("Roaming = %{public}d", networkClient->IsRoaming());
178     return networkClient->IsRoaming();
179 #else
180     REQUEST_HILOGE("Telephony SA not found");
181     return false;
182 #endif
183 }
184 
RegisterNetworkChange(rust::box<NetworkInner> notifier, rust::box<NetworkTaskManagerTx> task_manager, rust::fn<void(const NetworkTaskManagerTx &task_manager)> notifyTaskManagerOnline, rust::fn<void(const NetworkTaskManagerTx &task_manager)> notifyTaskManagerOffline)185 std::unique_ptr<NetworkRegistry> RegisterNetworkChange(rust::box<NetworkInner> notifier,
186     rust::box<NetworkTaskManagerTx> task_manager,
187     rust::fn<void(const NetworkTaskManagerTx &task_manager)> notifyTaskManagerOnline,
188     rust::fn<void(const NetworkTaskManagerTx &task_manager)> notifyTaskManagerOffline)
189 {
190     REQUEST_HILOGI("RegisterNetworkChange");
191     sptr<RequestNetCallbackStub> callbackStub = sptr<RequestNetCallbackStub>::MakeSptr(
192         std::move(notifier), std::move(task_manager), notifyTaskManagerOnline, notifyTaskManagerOffline);
193     if (callbackStub == nullptr) {
194         REQUEST_HILOGE("callbackStub is nullptr");
195         return nullptr;
196     }
197     int ret = NetConnClient::GetInstance().RegisterNetConnCallback(callbackStub);
198     if (ret != 0) {
199         REQUEST_HILOGE("RegisterNetConnCallback failed, ret = %{public}d", ret);
200         return nullptr;
201     }
202     return std::make_unique<NetworkRegistry>(callbackStub);
203 }
204 
NetworkRegistry(sptr<RequestNetCallbackStub> callback)205 NetworkRegistry::NetworkRegistry(sptr<RequestNetCallbackStub> callback)
206 {
207     callback_ = callback;
208 }
209 
~NetworkRegistry()210 NetworkRegistry::~NetworkRegistry()
211 {
212     REQUEST_HILOGI("UnregisterNetworkChange");
213     int32_t ret = NetConnClient::GetInstance().UnregisterNetConnCallback(callback_);
214     if (ret != 0) {
215         REQUEST_HILOGE("UnregisterNetConnCallback failed, ret = %{public}d", ret);
216     }
217 }
218 
219 } // namespace OHOS::Request