1 /*
2 * Copyright (c) 2022-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 "networkshare_service.h"
17
18 #include "net_event_report.h"
19 #include "net_manager_center.h"
20 #include "net_manager_constants.h"
21 #include "netmanager_base_permission.h"
22 #include "netmgr_ext_log_wrapper.h"
23 #include "networkshare_notification.h"
24 #include "networkshare_constants.h"
25 #include "networkshare_upstreammonitor.h"
26 #include "xcollie/xcollie.h"
27 #include "xcollie/xcollie_define.h"
28 #include "system_ability_definition.h"
29 #include "netsys_controller.h"
30 #include "edm_parameter_utils.h"
31 #include "ipc_skeleton.h"
32 #include "bundle_mgr_interface.h"
33 #include "iservice_registry.h"
34 #include "ffrt.h"
35 #ifdef USB_MODOULE
36 #include "usb_srv_support.h"
37 #endif
38
39 namespace OHOS {
40 namespace NetManagerStandard {
41 const std::string NETWORK_TIMER = "NetworkShare::RegisterSharingEvent";
42 const bool REGISTER_LOCAL_RESULT_NETSHARE =
43 SystemAbility::MakeAndRegisterAbility(DelayedSingleton<NetworkShareService>::GetInstance().get());
44 constexpr int32_t XCOLLIE_TIMEOUT_DURATION = 30;
45 constexpr const char *NETWORK_SHARE_POLICY_PARAM = "persist.edm.tethering_disallowed";
46 constexpr const char *IDLE_AP_USER_RESTART_NOTIFICATION = "ohos.event.notification.wifi.TAP_ENABLE_HOTSPOT";
47 constexpr const char *EVENT_THERMAL_STOP_AP = "usual.event.thermal.WIFI_POLICY";
48
NetworkShareService()49 NetworkShareService::NetworkShareService() : SystemAbility(COMM_NET_TETHERING_MANAGER_SYS_ABILITY_ID, true) {}
50
~NetworkShareService()51 NetworkShareService::~NetworkShareService(){};
52
OnStart()53 void NetworkShareService::OnStart()
54 {
55 if (state_ == STATE_RUNNING) {
56 NETMGR_EXT_LOG_D("OnStart Service state is already running");
57 return;
58 }
59 if (!Init()) {
60 NETMGR_EXT_LOG_E("OnStart init failed");
61 EventInfo eventInfo;
62 eventInfo.operatorType = static_cast<int32_t>(NetworkShareEventOperator::OPERATION_START_SA);
63 eventInfo.errorType = static_cast<int32_t>(NetworkShareEventErrorType::ERROR_START_SA);
64 eventInfo.errorMsg = "Start Network Share Service failed";
65 NetEventReport::SendSetupFaultEvent(eventInfo);
66 return;
67 }
68 state_ = STATE_RUNNING;
69 NETMGR_EXT_LOG_I("OnStart successful");
70 }
71
OnStop()72 void NetworkShareService::OnStop()
73 {
74 NetworkShareTracker::GetInstance().Uninit();
75 state_ = STATE_STOPPED;
76 registerToService_ = false;
77 EdmParameterUtils::GetInstance().UnRegisterEdmParameterChangeEvent(NETWORK_SHARE_POLICY_PARAM);
78 NETMGR_EXT_LOG_I("OnStop successful");
79 }
80
Dump(int32_t fd,const std::vector<std::u16string> & args)81 int32_t NetworkShareService::Dump(int32_t fd, const std::vector<std::u16string> &args)
82 {
83 NETMGR_EXT_LOG_I("Start Dump, fd: %{public}d", fd);
84 std::string result;
85 GetDumpMessage(result);
86 NETMGR_EXT_LOG_I("Dump content: %{public}s", result.c_str());
87 int32_t ret = dprintf(fd, "%s\n", result.c_str());
88 return ret < 0 ? NETWORKSHARE_ERROR_INTERNAL_ERROR : NETMANAGER_EXT_SUCCESS;
89 }
90
Init()91 bool NetworkShareService::Init()
92 {
93 if (!REGISTER_LOCAL_RESULT_NETSHARE) {
94 NETMGR_EXT_LOG_E("Register to local sa manager failed");
95 return false;
96 }
97 if (!registerToService_) {
98 if (!Publish(DelayedSingleton<NetworkShareService>::GetInstance().get())) {
99 NETMGR_EXT_LOG_E("Register to sa manager failed");
100 return false;
101 }
102 registerToService_ = true;
103 }
104
105 EdmParameterUtils::GetInstance().RegisterEdmParameterChangeEvent(NETWORK_SHARE_POLICY_PARAM,
106 DisAllowNetworkShareEventCallback, this);
107
108 AddSystemAbilityListener(COMM_NETSYS_NATIVE_SYS_ABILITY_ID);
109 AddSystemAbilityListener(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID);
110 AddSystemAbilityListener(WIFI_HOTSPOT_SYS_ABILITY_ID);
111 SubscribeCommonEvent();
112 #ifdef SHARE_NOTIFICATION_ENABLE
113 SubscribeWifiShareNtfEvent();
114 #endif
115 return true;
116 }
117
GetDumpMessage(std::string & message)118 void NetworkShareService::GetDumpMessage(std::string &message)
119 {
120 message.append("Net Sharing Info:\n");
121 int32_t supported = NETWORKSHARE_IS_UNSUPPORTED;
122 NetworkShareTracker::GetInstance().IsNetworkSharingSupported(supported);
123 std::string surpportContent = supported == NETWORKSHARE_IS_SUPPORTED ? "surpported" : "not surpported";
124 message.append("\tIs Sharing Supported: " + surpportContent + "\n");
125 int32_t sharingStatus = NETWORKSHARE_IS_UNSHARING;
126 NetworkShareTracker::GetInstance().IsSharing(sharingStatus);
127 std::string sharingState = sharingStatus ? "is sharing" : "not sharing";
128 message.append("\tSharing State: " + sharingState + "\n");
129 if (sharingStatus) {
130 std::string sharingType;
131 GetSharingType(SharingIfaceType::SHARING_WIFI, "wifi;", sharingType);
132 GetSharingType(SharingIfaceType::SHARING_USB, "usb;", sharingType);
133 GetSharingType(SharingIfaceType::SHARING_BLUETOOTH, "bluetooth;", sharingType);
134 message.append("\tSharing Types: " + sharingType + "\n");
135 }
136
137 std::string wifiShareRegexs;
138 GetShareRegexsContent(SharingIfaceType::SHARING_WIFI, wifiShareRegexs);
139 message.append("\tUsb Regexs: " + wifiShareRegexs + "\n");
140 std::string usbShareRegexs;
141 GetShareRegexsContent(SharingIfaceType::SHARING_USB, usbShareRegexs);
142 message.append("\tWifi Regexs: " + usbShareRegexs + "\n");
143 std::string btpanShareRegexs;
144 GetShareRegexsContent(SharingIfaceType::SHARING_BLUETOOTH, btpanShareRegexs);
145 message.append("\tBluetooth Regexs: " + btpanShareRegexs + "\n");
146 }
147
GetSharingType(const SharingIfaceType & type,const std::string & typeContent,std::string & sharingType)148 void NetworkShareService::GetSharingType(const SharingIfaceType &type, const std::string &typeContent,
149 std::string &sharingType)
150 {
151 SharingIfaceState state;
152 NetworkShareTracker::GetInstance().GetSharingState(type, state);
153 if (state == SharingIfaceState::SHARING_NIC_SERVING) {
154 sharingType += typeContent;
155 }
156 }
157
GetShareRegexsContent(const SharingIfaceType & type,std::string & shareRegexsContent)158 void NetworkShareService::GetShareRegexsContent(const SharingIfaceType &type, std::string &shareRegexsContent)
159 {
160 std::vector<std::string> regexs;
161 NetworkShareTracker::GetInstance().GetSharableRegexs(type, regexs);
162 for_each(regexs.begin(), regexs.end(),
163 [&shareRegexsContent](const std::string ®ex) { shareRegexsContent += regex + ";"; });
164 }
165
IsNetworkSharingSupported(int32_t & supported)166 int32_t NetworkShareService::IsNetworkSharingSupported(int32_t &supported)
167 {
168 NETMGR_EXT_LOG_I("NetworkSharing IsNetworkSharingSupported");
169 if (!NetManagerPermission::IsSystemCaller()) {
170 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
171 }
172 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
173 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
174 }
175 return NetworkShareTracker::GetInstance().IsNetworkSharingSupported(supported);
176 }
177
IsSharing(int32_t & sharingStatus)178 int32_t NetworkShareService::IsSharing(int32_t &sharingStatus)
179 {
180 NETMGR_EXT_LOG_I("NetworkSharing IsSharing");
181 if (!NetManagerPermission::IsSystemCaller()) {
182 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
183 }
184 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
185 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
186 }
187 return NetworkShareTracker::GetInstance().IsSharing(sharingStatus);
188 }
189
StartNetworkSharing(int32_t typeInt)190 int32_t NetworkShareService::StartNetworkSharing(int32_t typeInt)
191 {
192 SharingIfaceType type = SharingIfaceType(typeInt);
193 if (EdmParameterUtils::GetInstance().CheckBoolEdmParameter(NETWORK_SHARE_POLICY_PARAM, "false")) {
194 NETMGR_EXT_LOG_E("NetworkSharing start sharing, check EDM param true");
195 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
196 }
197 NETMGR_EXT_LOG_I("NetworkSharing start sharing,type is %{public}d", type);
198 if (!NetManagerPermission::IsSystemCaller()) {
199 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
200 }
201 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
202 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
203 }
204 int32_t uid = IPCSkeleton::GetCallingUid();
205 std::string packageName;
206 int Neils = GetBundleNameByUid(uid, packageName);
207 if (Neils != NETMANAGER_EXT_SUCCESS) {
208 NETMGR_EXT_LOG_E("Error GetBundleNameForUid fail");
209 }
210 NETMGR_EXT_LOG_I("StartNetworkSharing uid = %{public}d, packagename = %{public}s", uid, packageName.c_str());
211 int32_t ret = NetworkShareTracker::GetInstance().StartNetworkSharing(type);
212 if (ret == NETMANAGER_EXT_SUCCESS) {
213 ret = NetsysController::GetInstance().UpdateNetworkSharingType(static_cast<uint32_t>(type), true);
214 if (typeInt == 0) {
215 bool operateType = true;
216 NetworkShareHisysEvent::GetInstance().WriteSoftApOpenAndCloseFailedEvent(operateType, uid, packageName);
217 }
218 }
219 SetConfigureForShare(true);
220 return ret;
221 }
222
StopNetworkSharing(int32_t typeInt)223 int32_t NetworkShareService::StopNetworkSharing(int32_t typeInt)
224 {
225 SharingIfaceType type = SharingIfaceType(typeInt);
226 SetConfigureForShare(false);
227 NETMGR_EXT_LOG_I("NetworkSharing stop sharing,type is %{public}d", type);
228 if (!NetManagerPermission::IsSystemCaller()) {
229 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
230 }
231 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
232 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
233 }
234 int32_t uid = IPCSkeleton::GetCallingUid();
235 std::string packageName;
236 int Neils = GetBundleNameByUid(uid, packageName);
237 if (Neils != NETMANAGER_EXT_SUCCESS) {
238 NETMGR_EXT_LOG_E("Error GetBundleNameForUid fail");
239 }
240 NETMGR_EXT_LOG_I("StopNetworkSharing uid = %{public}d, packagname = %{public}s", uid, packageName.c_str());
241 int32_t ret = NetworkShareTracker::GetInstance().StopNetworkSharing(type);
242 if (ret == NETMANAGER_EXT_SUCCESS) {
243 ret = NetsysController::GetInstance().UpdateNetworkSharingType(static_cast<uint32_t>(type), false);
244 if (typeInt == 0) {
245 bool operateType = false;
246 NetworkShareHisysEvent::GetInstance().WriteSoftApOpenAndCloseFailedEvent(operateType, uid, packageName);
247 }
248 }
249 return ret;
250 }
251
GetBundleManager()252 sptr<AppExecFwk::IBundleMgr> GetBundleManager()
253 {
254 sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
255 if (systemManager == nullptr) {
256 NETMGR_EXT_LOG_E("Get system ability manager failed!");
257 return nullptr;
258 }
259 return iface_cast<AppExecFwk::IBundleMgr>(systemManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID));
260 }
261
GetBundleNameByUid(const int uid,std::string & bundleName)262 int32_t NetworkShareService::GetBundleNameByUid(const int uid, std::string &bundleName)
263 {
264 sptr<AppExecFwk::IBundleMgr> bundleInstance = GetBundleManager();
265 if (bundleInstance == nullptr) {
266 NETMGR_EXT_LOG_E("%{public}s bundle instance is null", __FUNCTION__);
267 return NETMANAGER_ERROR;
268 }
269 if(!bundleInstance->GetBundleNameForUid(uid, bundleName)) {
270 NETMGR_EXT_LOG_D("%{public}s get bundlename failed", __FUNCTION__);
271 return NETMANAGER_ERROR;
272 }
273 return NETMANAGER_EXT_SUCCESS;
274 }
275
RegisterSharingEvent(const sptr<ISharingEventCallback> & callback)276 int32_t NetworkShareService::RegisterSharingEvent(const sptr<ISharingEventCallback>& callback)
277 {
278 NETMGR_EXT_LOG_I("NetworkSharing Register Sharing Event.");
279 int id = HiviewDFX::XCollie::GetInstance().SetTimer(NETWORK_TIMER, XCOLLIE_TIMEOUT_DURATION, nullptr, nullptr,
280 HiviewDFX::XCOLLIE_FLAG_LOG);
281 if (!NetManagerPermission::IsSystemCaller()) {
282 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
283 }
284 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
285 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
286 }
287 auto ret = NetworkShareTracker::GetInstance().RegisterSharingEvent(callback);
288 HiviewDFX::XCollie::GetInstance().CancelTimer(id);
289 return ret;
290 }
291
UnregisterSharingEvent(const sptr<ISharingEventCallback> & callback)292 int32_t NetworkShareService::UnregisterSharingEvent(const sptr<ISharingEventCallback>& callback)
293 {
294 NETMGR_EXT_LOG_I("NetworkSharing UnRegister Sharing Event.");
295 if (!NetManagerPermission::IsSystemCaller()) {
296 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
297 }
298 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
299 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
300 }
301 return NetworkShareTracker::GetInstance().UnregisterSharingEvent(callback);
302 }
303
GetSharableRegexs(int32_t typeInt,std::vector<std::string> & ifaceRegexs)304 int32_t NetworkShareService::GetSharableRegexs(int32_t typeInt, std::vector<std::string> &ifaceRegexs)
305 {
306 SharingIfaceType type = SharingIfaceType(typeInt);
307 if (!NetManagerPermission::IsSystemCaller()) {
308 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
309 }
310 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
311 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
312 }
313 return NetworkShareTracker::GetInstance().GetSharableRegexs(type, ifaceRegexs);
314 }
315
GetSharingState(int32_t typeInt,int32_t & stateInt)316 int32_t NetworkShareService::GetSharingState(int32_t typeInt, int32_t &stateInt)
317 {
318 SharingIfaceType type = SharingIfaceType(typeInt);
319 SharingIfaceState state = SharingIfaceState(stateInt);
320 if (!NetManagerPermission::IsSystemCaller()) {
321 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
322 }
323 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
324 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
325 }
326 int32_t ret = NetworkShareTracker::GetInstance().GetSharingState(type, state);
327 stateInt = static_cast<int32_t>(state);
328 return ret;
329 }
330
GetNetSharingIfaces(int32_t stateInt,std::vector<std::string> & ifaces)331 int32_t NetworkShareService::GetNetSharingIfaces(int32_t stateInt, std::vector<std::string> &ifaces)
332 {
333 SharingIfaceState state = SharingIfaceState(stateInt);
334 if (!NetManagerPermission::IsSystemCaller()) {
335 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
336 }
337 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
338 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
339 }
340 return NetworkShareTracker::GetInstance().GetNetSharingIfaces(state, ifaces);
341 }
342
GetStatsRxBytes(int32_t & bytes)343 int32_t NetworkShareService::GetStatsRxBytes(int32_t &bytes)
344 {
345 if (!NetManagerPermission::IsSystemCaller()) {
346 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
347 }
348 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
349 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
350 }
351 return NetworkShareTracker::GetInstance().GetSharedSubSMTraffic(TrafficType::TRAFFIC_RX, bytes);
352 }
353
GetStatsTxBytes(int32_t & bytes)354 int32_t NetworkShareService::GetStatsTxBytes(int32_t &bytes)
355 {
356 if (!NetManagerPermission::IsSystemCaller()) {
357 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
358 }
359 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
360 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
361 }
362 return NetworkShareTracker::GetInstance().GetSharedSubSMTraffic(TrafficType::TRAFFIC_TX, bytes);
363 }
364
GetStatsTotalBytes(int32_t & bytes)365 int32_t NetworkShareService::GetStatsTotalBytes(int32_t &bytes)
366 {
367 if (!NetManagerPermission::IsSystemCaller()) {
368 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
369 }
370 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
371 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
372 }
373 return NetworkShareTracker::GetInstance().GetSharedSubSMTraffic(TrafficType::TRAFFIC_ALL, bytes);
374 }
375
SetConfigureForShare(bool enabled)376 int32_t NetworkShareService::SetConfigureForShare(bool enabled)
377 {
378 if (!NetManagerPermission::IsSystemCaller()) {
379 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
380 }
381 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
382 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
383 }
384 NETMGR_EXT_LOG_I("SetConfigureForShare begin, %{public}d", enabled);
385 std::lock_guard<ffrt::mutex> lock(setConfigureMutex_);
386 if (enabled) {
387 setConfigTimes_++;
388
389 if (setConfigTimes_ > 1) {
390 return NETMANAGER_EXT_SUCCESS;
391 }
392 } else {
393 setConfigTimes_ = setConfigTimes_ > 0 ? --setConfigTimes_ : setConfigTimes_;
394 if (setConfigTimes_ > 0) {
395 return NETMANAGER_EXT_SUCCESS;
396 }
397 }
398 std::shared_ptr<ffrt::queue> networkShareFfrtQueue = std::make_shared<ffrt::queue>("NetworkShare");
399 if (!networkShareFfrtQueue) {
400 NETMGR_EXT_LOG_E("SetConfigureForShare error, FFRT Init Fail");
401 return NETMANAGER_EXT_ERR_OPERATION_FAILED;
402 }
403 auto ffrtSetConfig = [setConfigSharedPtr = std::make_shared<NetworkShareConfiguration>(), enabled,
404 shareServiceWeakPtr = std::weak_ptr<NetworkShareService>(shared_from_this())]() {
405 auto shareServiceSharedPtr = shareServiceWeakPtr.lock();
406 if (!shareServiceSharedPtr) {
407 NETMGR_EXT_LOG_E("SetConfigureForShare error, NetworkShareService instance is invalid");
408 return;
409 }
410 std::lock_guard<ffrt::mutex> lock(shareServiceSharedPtr->openFileMutex_);
411 setConfigSharedPtr->SetConfigureForShare(enabled);
412 };
413 networkShareFfrtQueue->submit(ffrtSetConfig);
414 return NETMANAGER_EXT_SUCCESS;
415 }
416
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)417 void NetworkShareService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
418 {
419 NETMGR_EXT_LOG_D("OnAddSystemAbility systemAbilityId[%{public}d]", systemAbilityId);
420 if (systemAbilityId == COMM_NETSYS_NATIVE_SYS_ABILITY_ID) {
421 if (hasSARemoved_) {
422 OnNetSysRestart();
423 hasSARemoved_ = false;
424 }
425 }
426 if (systemAbilityId == COMM_NET_CONN_MANAGER_SYS_ABILITY_ID) {
427 NetworkShareTracker::GetInstance().Init();
428 }
429 #ifdef WIFI_MODOULE
430 if (systemAbilityId == WIFI_HOTSPOT_SYS_ABILITY_ID) {
431 NetworkShareTracker::GetInstance().RegisterWifiApCallback();
432 }
433 #endif
434 }
435
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)436 void NetworkShareService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
437 {
438 NETMGR_EXT_LOG_D("OnRemoveSystemAbility systemAbilityId[%{public}d]", systemAbilityId);
439 if (systemAbilityId == COMM_NETSYS_NATIVE_SYS_ABILITY_ID) {
440 hasSARemoved_ = true;
441 }
442 }
443
OnNetSysRestart()444 void NetworkShareService::OnNetSysRestart()
445 {
446 NETMGR_EXT_LOG_I("OnNetSysRestart");
447 NetworkShareTracker::GetInstance().RestartResume();
448 }
449
DisAllowNetworkShareEventCallback(const char * key,const char * value,void * context)450 void NetworkShareService::DisAllowNetworkShareEventCallback(const char *key, const char *value, void *context)
451 {
452 if (strcmp(value, "true") == 0) {
453 NETMGR_EXT_LOG_I("DisAllowNetworkShareEventCallback calledstop all network sharing with %{public}s", value);
454
455 if (!context) {
456 NETMGR_EXT_LOG_I("DisAllowNetworkShareEventCallback context is NULL");
457 return;
458 }
459
460 NetworkShareService* servicePtr = static_cast<NetworkShareService*>(context);
461 std::string sharingType;
462 servicePtr->GetSharingType(SharingIfaceType::SHARING_WIFI, "wifi;", sharingType);
463 servicePtr->GetSharingType(SharingIfaceType::SHARING_USB, "usb;", sharingType);
464 servicePtr->GetSharingType(SharingIfaceType::SHARING_BLUETOOTH, "bluetooth;", sharingType);
465 if (sharingType.find("wifi") != std::string::npos) {
466 std::function<void()> StopNetworkSharingWifi =
467 [servicePtr]() {
468 servicePtr->StopNetworkSharing(
469 static_cast<int32_t>(SharingIfaceType::SHARING_WIFI));
470 };
471 ffrt::task_handle wifiHandle = ffrt::submit_h(StopNetworkSharingWifi,
472 ffrt::task_attr().name("StopNetworkSharingWifi_task"));
473 ffrt::wait({wifiHandle});
474 NETMGR_EXT_LOG_D("DisAllowNetworkShareEventCallback stop wifi end");
475 }
476 if (sharingType.find("usb") != std::string::npos) {
477 std::function<void()> StopNetworkSharingUsb =
478 [servicePtr]() {
479 servicePtr->StopNetworkSharing(
480 static_cast<int32_t>(SharingIfaceType::SHARING_USB));
481 };
482 ffrt::task_handle usbHandle = ffrt::submit_h(StopNetworkSharingUsb,
483 ffrt::task_attr().name("StopNetworkSharingUsb_task"));
484 ffrt::wait({usbHandle});
485 NETMGR_EXT_LOG_D("DisAllowNetworkShareEventCallback stop usb end");
486 }
487 if (sharingType.find("bluetooth") != std::string::npos) {
488 std::function<void()> StopNetworkSharingBluetooth =
489 [servicePtr]() {
490 servicePtr->StopNetworkSharing(
491 static_cast<int32_t>(SharingIfaceType::SHARING_BLUETOOTH));
492 };
493 ffrt::task_handle bluetoothHandle = ffrt::submit_h(StopNetworkSharingBluetooth,
494 ffrt::task_attr().name("StopNetworkSharingBluetooth_task"));
495 ffrt::wait({bluetoothHandle});
496 NETMGR_EXT_LOG_D("DisAllowNetworkShareEventCallback stop bluetooth end");
497 }
498 NETMGR_EXT_LOG_D("DisAllowNetworkShareEventCallback all end");
499 return;
500 }
501 }
502
SubscribeCommonEvent()503 void NetworkShareService::SubscribeCommonEvent()
504 {
505 EventFwk::MatchingSkills matchingSkills;
506 matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED);
507 matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED);
508 matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
509 #ifdef USB_MODOULE
510 matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USB_STATE);
511 #endif
512 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
513
514 // 1 means CORE_EVENT_PRIORITY
515 subscribeInfo.SetPriority(1);
516 commonEventSubscriber_ = std::make_shared<CommonEventSubscriber>(subscribeInfo);
517 if (commonEventSubscriber_ == nullptr) {
518 NETMGR_EXT_LOG_E("Subscribe common event subscriber_ is NULL");
519 return;
520 }
521 bool ret = EventFwk::CommonEventManager::SubscribeCommonEvent(commonEventSubscriber_);
522 if (!ret) {
523 NETMGR_EXT_LOG_E("Subscribe common event fail:%{public}d", ret);
524 }
525 }
526
OnReceiveEvent(const EventFwk::CommonEventData & eventData)527 void NetworkShareService::CommonEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
528 {
529 const auto &action = eventData.GetWant().GetAction();
530 NETMGR_EXT_LOG_I("NetworkShareService::OnReceiveEvent: %{public}s.", action.c_str());
531 if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED) {
532 NetworkShareTracker::GetInstance().OnPowerConnected();
533 } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED) {
534 NetworkShareTracker::GetInstance().OnPowerDisConnected();
535 #ifdef USB_MODOULE
536 } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USB_STATE) {
537 auto want = eventData.GetWant();
538 auto connected = want.GetBoolParam(std::string {USB::UsbSrvSupport::CONNECTED}, false);
539 NETMGR_EXT_LOG_I("UsbSrvSupport::CONNECTED: %{public}d.", connected);
540 if (!connected) {
541 NetworkShareTracker::GetInstance().StopNetworkSharing(SharingIfaceType::SHARING_USB);
542 }
543 #endif
544 } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE) {
545 int32_t state = eventData.GetCode();
546 int32_t bearerType = eventData.GetWant().GetIntParam("NetType", BEARER_DEFAULT);
547 NETMGR_EXT_LOG_I("ConnectivityChange state=%{public}d, type=%{public}d", state, bearerType);
548 DelayedSingleton<NetworkShareUpstreamMonitor>::GetInstance()->OnNetworkConnectChange(state, bearerType);
549 }
550 }
551
552 #ifdef SHARE_NOTIFICATION_ENABLE
SubscribeWifiShareNtfEvent()553 void NetworkShareService::SubscribeWifiShareNtfEvent()
554 {
555 EventFwk::MatchingSkills matchingSkills;
556 matchingSkills.AddEvent(IDLE_AP_USER_RESTART_NOTIFICATION);
557 matchingSkills.AddEvent(EVENT_THERMAL_STOP_AP);
558 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
559
560 // 1 means CORE_EVENT_PRIORITY
561 subscribeInfo.SetPriority(1);
562 subscribeInfo.SetPermission("ohos.permission.SET_WIFI_CONFIG");
563 wifiShareNtfSubscriber_ = std::make_shared<WifiShareNtfSubscriber>(subscribeInfo);
564 if (wifiShareNtfSubscriber_ == nullptr) {
565 NETMGR_EXT_LOG_E("Subscribe common event subscriber_ is NULL");
566 return;
567 }
568 bool ret = EventFwk::CommonEventManager::SubscribeCommonEvent(wifiShareNtfSubscriber_);
569 if (!ret) {
570 NETMGR_EXT_LOG_E("Subscribe common event fail:%{public}d", ret);
571 }
572 }
573
OnReceiveEvent(const EventFwk::CommonEventData & eventData)574 void NetworkShareService::WifiShareNtfSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
575 {
576 const auto &action = eventData.GetWant().GetAction();
577 NETMGR_EXT_LOG_I("NetworkShareService::OnReceiveEvent: %{public}s.", action.c_str());
578 if (action == IDLE_AP_USER_RESTART_NOTIFICATION) {
579 NetworkShareTracker::GetInstance().StartNetworkSharing(SharingIfaceType::SHARING_WIFI);
580 } else if (action == EVENT_THERMAL_STOP_AP) {
581 NetworkShareNotification::GetInstance().PublishNetworkShareNotification(
582 NotificationId::THERMAL_STOP_AP_NOTIFICATION_ID);
583 }
584 }
585 #endif
586 } // namespace NetManagerStandard
587 } // namespace OHOS
588