• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_constants.h"
24 #include "xcollie/xcollie.h"
25 #include "xcollie/xcollie_define.h"
26 #include "system_ability_definition.h"
27 #include "netsys_controller.h"
28 #include "edm_parameter_utils.h"
29 #include "ffrt.h"
30 
31 namespace OHOS {
32 namespace NetManagerStandard {
33 const std::string NETWORK_TIMER = "NetworkShare::RegisterSharingEvent";
34 const bool REGISTER_LOCAL_RESULT_NETSHARE =
35     SystemAbility::MakeAndRegisterAbility(DelayedSingleton<NetworkShareService>::GetInstance().get());
36 constexpr int32_t XCOLLIE_TIMEOUT_DURATION = 30;
37 constexpr const char *NETWORK_SHARE_POLICY_PARAM = "persist.edm.tethering_disallowed";
38 inline const std::string IDLE_AP_USER_RESTART_NOTIFICATION = "ohos.event.notification.wifi.TAP_ENABLE_HOTSPOT";
39 
NetworkShareService()40 NetworkShareService::NetworkShareService() : SystemAbility(COMM_NET_TETHERING_MANAGER_SYS_ABILITY_ID, true) {}
41 
~NetworkShareService()42 NetworkShareService::~NetworkShareService(){};
43 
OnStart()44 void NetworkShareService::OnStart()
45 {
46     if (state_ == STATE_RUNNING) {
47         NETMGR_EXT_LOG_D("OnStart Service state is already running");
48         return;
49     }
50     if (!Init()) {
51         NETMGR_EXT_LOG_E("OnStart init failed");
52         EventInfo eventInfo;
53         eventInfo.operatorType = static_cast<int32_t>(NetworkShareEventOperator::OPERATION_START_SA);
54         eventInfo.errorType = static_cast<int32_t>(NetworkShareEventErrorType::ERROR_START_SA);
55         eventInfo.errorMsg = "Start Network Share Service failed";
56         NetEventReport::SendSetupFaultEvent(eventInfo);
57         return;
58     }
59     state_ = STATE_RUNNING;
60     NETMGR_EXT_LOG_I("OnStart successful");
61 }
62 
OnStop()63 void NetworkShareService::OnStop()
64 {
65     NetworkShareTracker::GetInstance().Uninit();
66     state_ = STATE_STOPPED;
67     registerToService_ = false;
68     EdmParameterUtils::GetInstance().UnRegisterEdmParameterChangeEvent(NETWORK_SHARE_POLICY_PARAM);
69     NETMGR_EXT_LOG_I("OnStop successful");
70 }
71 
Dump(int32_t fd,const std::vector<std::u16string> & args)72 int32_t NetworkShareService::Dump(int32_t fd, const std::vector<std::u16string> &args)
73 {
74     NETMGR_EXT_LOG_I("Start Dump, fd: %{public}d", fd);
75     std::string result;
76     GetDumpMessage(result);
77     NETMGR_EXT_LOG_I("Dump content: %{public}s", result.c_str());
78     int32_t ret = dprintf(fd, "%s\n", result.c_str());
79     return ret < 0 ? NETWORKSHARE_ERROR_INTERNAL_ERROR : NETMANAGER_EXT_SUCCESS;
80 }
81 
Init()82 bool NetworkShareService::Init()
83 {
84     if (!REGISTER_LOCAL_RESULT_NETSHARE) {
85         NETMGR_EXT_LOG_E("Register to local sa manager failed");
86         return false;
87     }
88     if (!registerToService_) {
89         if (!Publish(DelayedSingleton<NetworkShareService>::GetInstance().get())) {
90             NETMGR_EXT_LOG_E("Register to sa manager failed");
91             return false;
92         }
93         registerToService_ = true;
94     }
95 
96     EdmParameterUtils::GetInstance().RegisterEdmParameterChangeEvent(NETWORK_SHARE_POLICY_PARAM,
97         DisAllowNetworkShareEventCallback, this);
98 
99     AddSystemAbilityListener(COMM_NETSYS_NATIVE_SYS_ABILITY_ID);
100     AddSystemAbilityListener(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID);
101     SubscribeCommonEvent();
102 #ifdef SHARE_NOTIFICATION_ENABLE
103     SubscribeWifiShareNtfEvent();
104 #endif
105     return true;
106 }
107 
GetDumpMessage(std::string & message)108 void NetworkShareService::GetDumpMessage(std::string &message)
109 {
110     message.append("Net Sharing Info:\n");
111     int32_t supported = NETWORKSHARE_IS_UNSUPPORTED;
112     NetworkShareTracker::GetInstance().IsNetworkSharingSupported(supported);
113     std::string surpportContent = supported == NETWORKSHARE_IS_SUPPORTED ? "surpported" : "not surpported";
114     message.append("\tIs Sharing Supported: " + surpportContent + "\n");
115     int32_t sharingStatus = NETWORKSHARE_IS_UNSHARING;
116     NetworkShareTracker::GetInstance().IsSharing(sharingStatus);
117     std::string sharingState = sharingStatus ? "is sharing" : "not sharing";
118     message.append("\tSharing State: " + sharingState + "\n");
119     if (sharingStatus) {
120         std::string sharingType;
121         GetSharingType(SharingIfaceType::SHARING_WIFI, "wifi;", sharingType);
122         GetSharingType(SharingIfaceType::SHARING_USB, "usb;", sharingType);
123         GetSharingType(SharingIfaceType::SHARING_BLUETOOTH, "bluetooth;", sharingType);
124         message.append("\tSharing Types: " + sharingType + "\n");
125     }
126 
127     std::string wifiShareRegexs;
128     GetShareRegexsContent(SharingIfaceType::SHARING_WIFI, wifiShareRegexs);
129     message.append("\tUsb Regexs: " + wifiShareRegexs + "\n");
130     std::string usbShareRegexs;
131     GetShareRegexsContent(SharingIfaceType::SHARING_USB, usbShareRegexs);
132     message.append("\tWifi Regexs: " + usbShareRegexs + "\n");
133     std::string btpanShareRegexs;
134     GetShareRegexsContent(SharingIfaceType::SHARING_BLUETOOTH, btpanShareRegexs);
135     message.append("\tBluetooth Regexs: " + btpanShareRegexs + "\n");
136 }
137 
GetSharingType(const SharingIfaceType & type,const std::string & typeContent,std::string & sharingType)138 void NetworkShareService::GetSharingType(const SharingIfaceType &type, const std::string &typeContent,
139                                          std::string &sharingType)
140 {
141     SharingIfaceState state;
142     NetworkShareTracker::GetInstance().GetSharingState(type, state);
143     if (state == SharingIfaceState::SHARING_NIC_SERVING) {
144         sharingType += typeContent;
145     }
146 }
147 
GetShareRegexsContent(const SharingIfaceType & type,std::string & shareRegexsContent)148 void NetworkShareService::GetShareRegexsContent(const SharingIfaceType &type, std::string &shareRegexsContent)
149 {
150     std::vector<std::string> regexs;
151     NetworkShareTracker::GetInstance().GetSharableRegexs(type, regexs);
152     for_each(regexs.begin(), regexs.end(),
153              [&shareRegexsContent](const std::string &regex) { shareRegexsContent += regex + ";"; });
154 }
155 
IsNetworkSharingSupported(int32_t & supported)156 int32_t NetworkShareService::IsNetworkSharingSupported(int32_t &supported)
157 {
158     NETMGR_EXT_LOG_I("NetworkSharing IsNetworkSharingSupported");
159     if (!NetManagerPermission::IsSystemCaller()) {
160         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
161     }
162     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
163         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
164     }
165     return NetworkShareTracker::GetInstance().IsNetworkSharingSupported(supported);
166 }
167 
IsSharing(int32_t & sharingStatus)168 int32_t NetworkShareService::IsSharing(int32_t &sharingStatus)
169 {
170     NETMGR_EXT_LOG_I("NetworkSharing IsSharing");
171     if (!NetManagerPermission::IsSystemCaller()) {
172         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
173     }
174     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
175         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
176     }
177     return NetworkShareTracker::GetInstance().IsSharing(sharingStatus);
178 }
179 
StartNetworkSharing(const SharingIfaceType & type)180 int32_t NetworkShareService::StartNetworkSharing(const SharingIfaceType &type)
181 {
182     if (EdmParameterUtils::GetInstance().CheckBoolEdmParameter(NETWORK_SHARE_POLICY_PARAM, "false")) {
183         NETMGR_EXT_LOG_E("NetworkSharing start sharing, check EDM param true");
184         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
185     }
186     NETMGR_EXT_LOG_I("NetworkSharing start sharing,type is %{public}d", type);
187     if (!NetManagerPermission::IsSystemCaller()) {
188         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
189     }
190     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
191         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
192     }
193     int32_t ret = NetworkShareTracker::GetInstance().StartNetworkSharing(type);
194     if (ret == NETMANAGER_EXT_SUCCESS) {
195         ret = NetsysController::GetInstance().UpdateNetworkSharingType(static_cast<uint32_t>(type), true);
196     }
197     SetConfigureForShare(true);
198     return ret;
199 }
200 
StopNetworkSharing(const SharingIfaceType & type)201 int32_t NetworkShareService::StopNetworkSharing(const SharingIfaceType &type)
202 {
203     SetConfigureForShare(false);
204     NETMGR_EXT_LOG_I("NetworkSharing stop sharing,type is %{public}d", type);
205     if (!NetManagerPermission::IsSystemCaller()) {
206         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
207     }
208     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
209         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
210     }
211     int32_t ret = NetworkShareTracker::GetInstance().StopNetworkSharing(type);
212     if (ret == NETMANAGER_EXT_SUCCESS) {
213         ret = NetsysController::GetInstance().UpdateNetworkSharingType(static_cast<uint32_t>(type), false);
214     }
215 
216     return ret;
217 }
218 
RegisterSharingEvent(sptr<ISharingEventCallback> callback)219 int32_t NetworkShareService::RegisterSharingEvent(sptr<ISharingEventCallback> callback)
220 {
221     NETMGR_EXT_LOG_I("NetworkSharing Register Sharing Event.");
222     int id = HiviewDFX::XCollie::GetInstance().SetTimer(NETWORK_TIMER, XCOLLIE_TIMEOUT_DURATION, nullptr, nullptr,
223                                                         HiviewDFX::XCOLLIE_FLAG_LOG);
224     if (!NetManagerPermission::IsSystemCaller()) {
225         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
226     }
227     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
228         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
229     }
230     auto ret = NetworkShareTracker::GetInstance().RegisterSharingEvent(callback);
231     HiviewDFX::XCollie::GetInstance().CancelTimer(id);
232     return ret;
233 }
234 
UnregisterSharingEvent(sptr<ISharingEventCallback> callback)235 int32_t NetworkShareService::UnregisterSharingEvent(sptr<ISharingEventCallback> callback)
236 {
237     NETMGR_EXT_LOG_I("NetworkSharing UnRegister Sharing Event.");
238     if (!NetManagerPermission::IsSystemCaller()) {
239         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
240     }
241     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
242         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
243     }
244     return NetworkShareTracker::GetInstance().UnregisterSharingEvent(callback);
245 }
246 
GetSharableRegexs(SharingIfaceType type,std::vector<std::string> & ifaceRegexs)247 int32_t NetworkShareService::GetSharableRegexs(SharingIfaceType type, std::vector<std::string> &ifaceRegexs)
248 {
249     if (!NetManagerPermission::IsSystemCaller()) {
250         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
251     }
252     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
253         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
254     }
255     return NetworkShareTracker::GetInstance().GetSharableRegexs(type, ifaceRegexs);
256 }
257 
GetSharingState(SharingIfaceType type,SharingIfaceState & state)258 int32_t NetworkShareService::GetSharingState(SharingIfaceType type, SharingIfaceState &state)
259 {
260     if (!NetManagerPermission::IsSystemCaller()) {
261         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
262     }
263     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
264         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
265     }
266     return NetworkShareTracker::GetInstance().GetSharingState(type, state);
267 }
268 
GetNetSharingIfaces(const SharingIfaceState & state,std::vector<std::string> & ifaces)269 int32_t NetworkShareService::GetNetSharingIfaces(const SharingIfaceState &state, std::vector<std::string> &ifaces)
270 {
271     if (!NetManagerPermission::IsSystemCaller()) {
272         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
273     }
274     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
275         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
276     }
277     return NetworkShareTracker::GetInstance().GetNetSharingIfaces(state, ifaces);
278 }
279 
GetStatsRxBytes(int32_t & bytes)280 int32_t NetworkShareService::GetStatsRxBytes(int32_t &bytes)
281 {
282     if (!NetManagerPermission::IsSystemCaller()) {
283         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
284     }
285     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
286         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
287     }
288     return NetworkShareTracker::GetInstance().GetSharedSubSMTraffic(TrafficType::TRAFFIC_RX, bytes);
289 }
290 
GetStatsTxBytes(int32_t & bytes)291 int32_t NetworkShareService::GetStatsTxBytes(int32_t &bytes)
292 {
293     if (!NetManagerPermission::IsSystemCaller()) {
294         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
295     }
296     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
297         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
298     }
299     return NetworkShareTracker::GetInstance().GetSharedSubSMTraffic(TrafficType::TRAFFIC_TX, bytes);
300 }
301 
GetStatsTotalBytes(int32_t & bytes)302 int32_t NetworkShareService::GetStatsTotalBytes(int32_t &bytes)
303 {
304     if (!NetManagerPermission::IsSystemCaller()) {
305         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
306     }
307     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
308         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
309     }
310     return NetworkShareTracker::GetInstance().GetSharedSubSMTraffic(TrafficType::TRAFFIC_ALL, bytes);
311 }
312 
SetConfigureForShare(bool enabled)313 int32_t NetworkShareService::SetConfigureForShare(bool enabled)
314 {
315     NETMGR_EXT_LOG_I("SetConfigureForShare begin, %{public}d", enabled);
316     std::lock_guard<ffrt::mutex> lock(setConfigureMutex_);
317     if (enabled) {
318         setConfigTimes_++;
319 
320         if (setConfigTimes_ > 1) {
321             return NETMANAGER_EXT_SUCCESS;
322         }
323     } else {
324         setConfigTimes_ = setConfigTimes_ > 0 ? --setConfigTimes_ : setConfigTimes_;
325         if (setConfigTimes_ > 0) {
326             return NETMANAGER_EXT_SUCCESS;
327         }
328     }
329     std::shared_ptr<ffrt::queue> networkShareFfrtQueue = std::make_shared<ffrt::queue>("NetworkShare");
330     if (!networkShareFfrtQueue) {
331         NETMGR_EXT_LOG_E("SetConfigureForShare error, FFRT Init Fail");
332         return NETMANAGER_EXT_ERR_OPERATION_FAILED;
333     }
334     auto ffrtSetConfig = [setConfigSharedPtr = std::make_shared<NetworkShareConfiguration>(), enabled,
335             shareServiceWeakPtr = std::weak_ptr<NetworkShareService>(shared_from_this())]() {
336         auto shareServiceSharedPtr = shareServiceWeakPtr.lock();
337         if (!shareServiceSharedPtr) {
338             NETMGR_EXT_LOG_E("SetConfigureForShare error, NetworkShareService instance is invalid");
339             return;
340         }
341         std::lock_guard<ffrt::mutex> lock(shareServiceSharedPtr->openFileMutex_);
342         setConfigSharedPtr->SetConfigureForShare(enabled);
343     };
344     networkShareFfrtQueue->submit(ffrtSetConfig);
345     return NETMANAGER_EXT_SUCCESS;
346 }
347 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)348 void NetworkShareService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
349 {
350     NETMGR_EXT_LOG_D("OnAddSystemAbility systemAbilityId[%{public}d]", systemAbilityId);
351     if (systemAbilityId == COMM_NETSYS_NATIVE_SYS_ABILITY_ID) {
352         if (hasSARemoved_) {
353             OnNetSysRestart();
354             hasSARemoved_ = false;
355         }
356     }
357     if (systemAbilityId == COMM_NET_CONN_MANAGER_SYS_ABILITY_ID) {
358         NetworkShareTracker::GetInstance().Init();
359     }
360 }
361 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)362 void NetworkShareService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
363 {
364     NETMGR_EXT_LOG_D("OnRemoveSystemAbility systemAbilityId[%{public}d]", systemAbilityId);
365     if (systemAbilityId == COMM_NETSYS_NATIVE_SYS_ABILITY_ID) {
366         hasSARemoved_ = true;
367     }
368 }
369 
OnNetSysRestart()370 void NetworkShareService::OnNetSysRestart()
371 {
372     NETMGR_EXT_LOG_I("OnNetSysRestart");
373     NetworkShareTracker::GetInstance().RestartResume();
374 }
375 
DisAllowNetworkShareEventCallback(const char * key,const char * value,void * context)376 void NetworkShareService::DisAllowNetworkShareEventCallback(const char *key, const char *value, void *context)
377 {
378     if (strcmp(value, "true") == 0) {
379         NETMGR_EXT_LOG_I("DisAllowNetworkShareEventCallback calledstop all network sharing with %{public}s", value);
380 
381         if (!context) {
382             NETMGR_EXT_LOG_I("DisAllowNetworkShareEventCallback context is NULL");
383             return;
384         }
385 
386         NetworkShareService* servicePtr = static_cast<NetworkShareService*>(context);
387         std::string sharingType;
388         servicePtr->GetSharingType(SharingIfaceType::SHARING_WIFI, "wifi;", sharingType);
389         servicePtr->GetSharingType(SharingIfaceType::SHARING_USB, "usb;", sharingType);
390         servicePtr->GetSharingType(SharingIfaceType::SHARING_BLUETOOTH, "bluetooth;", sharingType);
391         if (sharingType.find("wifi") != std::string::npos) {
392             std::function<void()> StopNetworkSharingWifi =
393                 [servicePtr]() { servicePtr->StopNetworkSharing(SharingIfaceType::SHARING_WIFI); };
394             ffrt::task_handle wifiHandle = ffrt::submit_h(StopNetworkSharingWifi,
395                 ffrt::task_attr().name("StopNetworkSharingWifi_task"));
396             ffrt::wait({wifiHandle});
397             NETMGR_EXT_LOG_D("DisAllowNetworkShareEventCallback stop wifi end");
398         }
399         if (sharingType.find("usb") != std::string::npos) {
400             std::function<void()> StopNetworkSharingUsb =
401                 [servicePtr]() { servicePtr->StopNetworkSharing(SharingIfaceType::SHARING_USB); };
402             ffrt::task_handle usbHandle = ffrt::submit_h(StopNetworkSharingUsb,
403                 ffrt::task_attr().name("StopNetworkSharingUsb_task"));
404             ffrt::wait({usbHandle});
405             NETMGR_EXT_LOG_D("DisAllowNetworkShareEventCallback stop usb end");
406         }
407         if (sharingType.find("bluetooth") != std::string::npos) {
408             std::function<void()> StopNetworkSharingBluetooth =
409                 [servicePtr]() { servicePtr->StopNetworkSharing(SharingIfaceType::SHARING_BLUETOOTH); };
410             ffrt::task_handle bluetoothHandle = ffrt::submit_h(StopNetworkSharingBluetooth,
411                 ffrt::task_attr().name("StopNetworkSharingBluetooth_task"));
412             ffrt::wait({bluetoothHandle});
413             NETMGR_EXT_LOG_D("DisAllowNetworkShareEventCallback stop bluetooth end");
414         }
415         NETMGR_EXT_LOG_D("DisAllowNetworkShareEventCallback all end");
416         return;
417     }
418 }
419 
SubscribeCommonEvent()420 void NetworkShareService::SubscribeCommonEvent()
421 {
422     EventFwk::MatchingSkills matchingSkills;
423     matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED);
424     matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED);
425     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
426 
427     // 1 means CORE_EVENT_PRIORITY
428     subscribeInfo.SetPriority(1);
429     commonEventSubscriber_ = std::make_shared<CommonEventSubscriber>(subscribeInfo);
430     if (commonEventSubscriber_ == nullptr) {
431         NETMGR_EXT_LOG_E("Subscribe common event subscriber_ is NULL");
432         return;
433     }
434     bool ret = EventFwk::CommonEventManager::SubscribeCommonEvent(commonEventSubscriber_);
435     if (!ret) {
436         NETMGR_EXT_LOG_E("Subscribe common event fail:%{public}d", ret);
437     }
438 }
439 
OnReceiveEvent(const EventFwk::CommonEventData & eventData)440 void NetworkShareService::CommonEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
441 {
442     const auto &action = eventData.GetWant().GetAction();
443     NETMGR_EXT_LOG_I("NetworkShareService::OnReceiveEvent: %{public}s.", action.c_str());
444     if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED) {
445         NetworkShareTracker::GetInstance().OnPowerConnected();
446     } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED) {
447         NetworkShareTracker::GetInstance().OnPowerDisConnected();
448     }
449 }
450 
451 #ifdef SHARE_NOTIFICATION_ENABLE
SubscribeWifiShareNtfEvent()452 void NetworkShareService::SubscribeWifiShareNtfEvent()
453 {
454     EventFwk::MatchingSkills matchingSkills;
455     matchingSkills.AddEvent(IDLE_AP_USER_RESTART_NOTIFICATION);
456     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
457 
458     // 1 means CORE_EVENT_PRIORITY
459     subscribeInfo.SetPriority(1);
460     subscribeInfo.SetPermission("ohos.permission.SET_WIFI_CONFIG");
461     wifiShareNtfSubscriber_ = std::make_shared<WifiShareNtfSubscriber>(subscribeInfo);
462     if (wifiShareNtfSubscriber_ == nullptr) {
463         NETMGR_EXT_LOG_E("Subscribe common event subscriber_ is NULL");
464         return;
465     }
466     bool ret = EventFwk::CommonEventManager::SubscribeCommonEvent(wifiShareNtfSubscriber_);
467     if (!ret) {
468         NETMGR_EXT_LOG_E("Subscribe common event fail:%{public}d", ret);
469     }
470 }
471 
OnReceiveEvent(const EventFwk::CommonEventData & eventData)472 void NetworkShareService::WifiShareNtfSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
473 {
474     const auto &action = eventData.GetWant().GetAction();
475     NETMGR_EXT_LOG_I("NetworkShareService::OnReceiveEvent: %{public}s.", action.c_str());
476     if (action == IDLE_AP_USER_RESTART_NOTIFICATION) {
477         NetworkShareTracker::GetInstance().StartNetworkSharing(SharingIfaceType::SHARING_WIFI);
478     }
479 }
480 #endif
481 } // namespace NetManagerStandard
482 } // namespace OHOS
483