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