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 .SetupWearableDistributedNetwork(tcpPortId, udpPortId, isMetered);
62 }
63
EnableWearableDistributedNet(bool enableFlag)64 int32_t WearableDistributedNetService::EnableWearableDistributedNet(bool enableFlag)
65 {
66 NETMGR_EXT_LOG_I("Wearable Distributed Net Service Enable Net : %{public}u", enableFlag);
67 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
68 NETMGR_EXT_LOG_E("Wearable Distributed Net Service Setup Net no permission");
69 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
70 }
71 return WearableDistributedNetManagement::GetInstance().EnableWearableDistributedNetwork(enableFlag);
72 }
73
TearDownWearableDistributedNet()74 int32_t WearableDistributedNetService::TearDownWearableDistributedNet()
75 {
76 NETMGR_EXT_LOG_I("Wearable Distributed Net Service TearDown Net");
77 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
78 NETMGR_EXT_LOG_E("Wearable Distributed Net Service TearDown Net no permission");
79 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
80 }
81 return WearableDistributedNetManagement::GetInstance().StopWearableDistributedNetwork();
82 }
83
UpdateMeteredStatus(bool isMetered)84 int32_t WearableDistributedNetService::UpdateMeteredStatus(bool isMetered)
85 {
86 NETMGR_EXT_LOG_I("Update wearable distributed net metered status");
87 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
88 NETMGR_EXT_LOG_E("Update wearable distributed net metered status no permission");
89 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
90 }
91 return WearableDistributedNetManagement::GetInstance().UpdateMeteredStatus(isMetered);
92 }
93
Init()94 bool WearableDistributedNetService::Init()
95 {
96 NETMGR_EXT_LOG_I("Wearable Distributed Net Service Init");
97 AddSystemAbilityListener(NET_MANAGER_SYS_ABILITY_ID);
98 if (!registerToService_) {
99 if (!Publish(this)) {
100 NETMGR_EXT_LOG_E("Wearable Distributed Net Service Register to sa manager failed");
101 return false;
102 }
103 registerToService_ = true;
104 }
105 AddSystemAbilityListener(POWER_MANAGER_BATT_SERVICE_ID);
106 AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
107 if (!SubscribeCommonEvent()) {
108 return false;
109 }
110 return true;
111 }
112
UpdateNetScore(const bool isCharging)113 void WearableDistributedNetService::UpdateNetScore(const bool isCharging)
114 {
115 WearableDistributedNetManagement::GetInstance().UpdateNetScore(isCharging);
116 }
117
SubscribeCommonEvent()118 bool WearableDistributedNetService::SubscribeCommonEvent()
119 {
120 NETMGR_EXT_LOG_I("Wearable Distributed Net subscribe power event.");
121 EventFwk::MatchingSkills matchingSkills;
122 matchingSkills.AddEvent(Event::COMMON_EVENT_POWER_CONNECTED);
123 matchingSkills.AddEvent(Event::COMMON_EVENT_POWER_DISCONNECTED);
124 matchingSkills.AddEvent(Event::COMMON_EVENT_BATTERY_CHANGED);
125 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
126
127 // 1 means CORE_EVENT_PRIORITY
128 subscribeInfo.SetPriority(1);
129 subscriber_ = std::make_shared<ReceiveMessage>(subscribeInfo, *this);
130 if (subscriber_ == nullptr) {
131 return false;
132 }
133 return EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
134 }
135
OnReceiveEvent(const EventFwk::CommonEventData & eventData)136 void WearableDistributedNetService::ReceiveMessage::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
137 {
138 const auto &want = eventData.GetWant();
139 const auto &chargingState = want.GetIntParam(PowerMgr::BatteryInfo::COMMON_EVENT_KEY_CHARGE_STATE,
140 static_cast<int32_t>(ChargeState::CHARGE_STATE_BUTT));
141
142 NETMGR_EXT_LOG_I("Wearable Distributed Net receive power message: chargingState = %{public}d", chargingState);
143 if (chargingState == static_cast<int32_t>(ChargeState::CHARGE_STATE_DISABLE) ||
144 chargingState == static_cast<int32_t>(ChargeState::CHARGE_STATE_NONE)) {
145 NETMGR_EXT_LOG_I("Wearable Distributed Net receive power disconnected message");
146 WearableDistributedNetService_.UpdateNetScore(false);
147 } else if (chargingState == static_cast<int32_t>(ChargeState::CHARGE_STATE_ENABLE) ||
148 chargingState == static_cast<int32_t>(ChargeState::CHARGE_STATE_FULL)) {
149 NETMGR_EXT_LOG_I("Wearable Distributed Net receive power connected message");
150 WearableDistributedNetService_.UpdateNetScore(true);
151 }
152 }
153 } // namespace NetManagerStandard
154 } // namespace OHOS
155