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 "netmanager_base_permission.h"
17 #include "wearable_distributed_net_service.h"
18
19 namespace OHOS {
20 namespace NetManagerStandard {
21 REGISTER_SYSTEM_ABILITY_BY_ID(WearableDistributedNetService, COMM_WEARABLE_DISTRIBUTED_NET_ABILITY_ID, true);
22
WearableDistributedNetService(int32_t saId,bool runOnCreate)23 WearableDistributedNetService::WearableDistributedNetService(int32_t saId, bool runOnCreate)
24 : SystemAbility(saId, runOnCreate) {}
25
26 WearableDistributedNetService::~WearableDistributedNetService() = default;
27
OnStart()28 void WearableDistributedNetService::OnStart()
29 {
30 NETMGR_EXT_LOG_I("WearableDistributedNetService::OnStart begin");
31 if (state_ == STATE_RUNNING) {
32 NETMGR_EXT_LOG_D("WearableDistributedNetService the state is already running");
33 return;
34 }
35 if (!Init()) {
36 NETMGR_EXT_LOG_E("WearableDistributedNetService init failed");
37 return;
38 }
39 state_ = STATE_RUNNING;
40 NETMGR_EXT_LOG_I("WearableDistributedNetService::OnStart end");
41 }
42
OnStop()43 void WearableDistributedNetService::OnStop()
44 {
45 state_ = STATE_STOPPED;
46 registerToService_ = false;
47 if (subscriber_ != nullptr) {
48 OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
49 subscriber_ = nullptr;
50 }
51 }
52
SetupWearableDistributedNet(int32_t tcpPortId,int32_t udpPortId,bool isMetered)53 int32_t WearableDistributedNetService::SetupWearableDistributedNet(int32_t tcpPortId, int32_t udpPortId, bool isMetered)
54 {
55 NETMGR_EXT_LOG_I("Wearable Distributed Net Service Setup Net");
56 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
57 NETMGR_EXT_LOG_E("Wearable Distributed Net Service Setup Net no permission");
58 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
59 }
60 return WearableDistributedNetManagement::GetInstance()
61 .StartWearableDistributedNetwork(tcpPortId, udpPortId, isMetered);
62 }
63
TearDownWearableDistributedNet()64 int32_t WearableDistributedNetService::TearDownWearableDistributedNet()
65 {
66 NETMGR_EXT_LOG_I("Wearable Distributed Net Service TearDown Net");
67 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
68 NETMGR_EXT_LOG_E("Wearable Distributed Net Service TearDown Net no permission");
69 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
70 }
71 return WearableDistributedNetManagement::GetInstance().StopWearableDistributedNetwork();
72 }
73
UpdateMeteredStatus(bool isMetered)74 int32_t WearableDistributedNetService::UpdateMeteredStatus(bool isMetered)
75 {
76 NETMGR_EXT_LOG_I("Update wearable distributed net metered status");
77 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
78 NETMGR_EXT_LOG_E("Update wearable distributed net metered status no permission");
79 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
80 }
81 return WearableDistributedNetManagement::GetInstance().UpdateMeteredStatus(isMetered);
82 }
83
Init()84 bool WearableDistributedNetService::Init()
85 {
86 NETMGR_EXT_LOG_I("Wearable Distributed Net Service Init");
87 AddSystemAbilityListener(NET_MANAGER_SYS_ABILITY_ID);
88 if (!registerToService_) {
89 if (!Publish(this)) {
90 NETMGR_EXT_LOG_E("Wearable Distributed Net Service Register to sa manager failed");
91 return false;
92 }
93 registerToService_ = true;
94 }
95 AddSystemAbilityListener(POWER_MANAGER_BATT_SERVICE_ID);
96 AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
97 if (!SubscribeCommonEvent()) {
98 return false;
99 }
100 return true;
101 }
102
UpdateNetScore(const bool isCharging)103 void WearableDistributedNetService::UpdateNetScore(const bool isCharging)
104 {
105 WearableDistributedNetManagement::GetInstance().UpdateNetScore(isCharging);
106 }
107
SubscribeCommonEvent()108 bool WearableDistributedNetService::SubscribeCommonEvent()
109 {
110 NETMGR_EXT_LOG_I("Wearable Distributed Net subscribe power event.");
111 EventFwk::MatchingSkills matchingSkills;
112 matchingSkills.AddEvent(Event::COMMON_EVENT_POWER_CONNECTED);
113 matchingSkills.AddEvent(Event::COMMON_EVENT_POWER_DISCONNECTED);
114 matchingSkills.AddEvent(Event::COMMON_EVENT_BATTERY_CHANGED);
115 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
116
117 // 1 means CORE_EVENT_PRIORITY
118 subscribeInfo.SetPriority(1);
119 subscriber_ = std::make_shared<ReceiveMessage>(subscribeInfo, *this);
120 if (subscriber_ == nullptr) {
121 return false;
122 }
123 return EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
124 }
125
OnReceiveEvent(const EventFwk::CommonEventData & eventData)126 void WearableDistributedNetService::ReceiveMessage::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
127 {
128 const auto &want = eventData.GetWant();
129 const auto &chargingState = want.GetIntParam(PowerMgr::BatteryInfo::COMMON_EVENT_KEY_CHARGE_STATE,
130 static_cast<int32_t>(ChargeState::CHARGE_STATE_BUTT));
131
132 NETMGR_EXT_LOG_I("Wearable Distributed Net receive power message: chargingState = %{public}d", chargingState);
133 if (chargingState == static_cast<int32_t>(ChargeState::CHARGE_STATE_DISABLE) ||
134 chargingState == static_cast<int32_t>(ChargeState::CHARGE_STATE_NONE)) {
135 NETMGR_EXT_LOG_I("Wearable Distributed Net receive power disconnected message");
136 WearableDistributedNetService_.UpdateNetScore(false);
137 } else if (chargingState == static_cast<int32_t>(ChargeState::CHARGE_STATE_ENABLE) ||
138 chargingState == static_cast<int32_t>(ChargeState::CHARGE_STATE_FULL)) {
139 NETMGR_EXT_LOG_I("Wearable Distributed Net receive power connected message");
140 WearableDistributedNetService_.UpdateNetScore(true);
141 }
142 }
143 } // namespace NetManagerStandard
144 } // namespace OHOS
145