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 "battery_srv_client.h"
17 #include "net_manager_constants.h"
18 #include "netmgr_ext_log_wrapper.h"
19 #include "wearable_distributed_net_agent.h"
20 #include "wearable_distributed_net_link_info.h"
21
22 namespace OHOS {
23 namespace NetManagerStandard {
24 const std::string WEARABLE_DISTRIBUTED_NET_NAME = "wearabledistributednet";
25
GetInstance()26 WearableDistributedNetAgent &WearableDistributedNetAgent::GetInstance()
27 {
28 static WearableDistributedNetAgent instance;
29 return instance;
30 }
31
EnableWearableDistributedNetForward(const int32_t tcpPortId,const int32_t udpPortId)32 int32_t WearableDistributedNetAgent::EnableWearableDistributedNetForward(const int32_t tcpPortId,
33 const int32_t udpPortId)
34 {
35 int32_t result = staticConfiguration_.EnableWearableDistributedNetForward(tcpPortId, udpPortId);
36 if (result != NETMANAGER_SUCCESS) {
37 NETMGR_EXT_LOG_E("Wearable Distributed Net Agent Forward failed, result:[%{public}d]", result);
38 }
39 return result;
40 }
41
DisableWearableDistributedNetForward()42 int32_t WearableDistributedNetAgent::DisableWearableDistributedNetForward()
43 {
44 int32_t ret = staticConfiguration_.DisableWearableDistributedNetForward();
45 if (ret != NETMANAGER_SUCCESS) {
46 NETMGR_EXT_LOG_E("Wearable Distributed Net Agent Disable Forward failed, ret:[%{public}d]", ret);
47 }
48 return ret;
49 }
50
ObtainNetCaps(const bool isMetered)51 void WearableDistributedNetAgent::ObtainNetCaps(const bool isMetered)
52 {
53 netCaps_ = staticConfiguration_.GetNetCaps(isMetered);
54 }
55
SetNetSupplierInfo(NetSupplierInfo & networkSupplierInfo)56 void WearableDistributedNetAgent::SetNetSupplierInfo(NetSupplierInfo &networkSupplierInfo)
57 {
58 return staticConfiguration_.GetNetSupplierInfo(networkSupplierInfo);
59 }
60
SetNetLinkInfo(NetLinkInfo & networkLinkInfo)61 int32_t WearableDistributedNetAgent::SetNetLinkInfo(NetLinkInfo &networkLinkInfo)
62 {
63 return staticConfiguration_.GetNetLinkInfo(networkLinkInfo);
64 }
65
ClearWearableDistributedNetForwardConfig()66 int32_t WearableDistributedNetAgent::ClearWearableDistributedNetForwardConfig()
67 {
68 int32_t ret = DisableWearableDistributedNetForward();
69 if (ret != NETMANAGER_SUCCESS) {
70 NETMGR_EXT_LOG_E("DisableWearableDistributedNetForward failed, ret:[%{public}d]", ret);
71 }
72 return ret;
73 }
74
UpdateMeteredStatus(const bool isMetered)75 int32_t WearableDistributedNetAgent::UpdateMeteredStatus(const bool isMetered)
76 {
77 return UpdateNetCaps(isMetered);
78 }
79
SetupWearableDistributedNetwork(const int32_t tcpPortId,const int32_t udpPortId,const bool isMetered)80 int32_t WearableDistributedNetAgent::SetupWearableDistributedNetwork(const int32_t tcpPortId, const int32_t udpPortId,
81 const bool isMetered)
82 {
83 NETMGR_EXT_LOG_I("SetupWearableDistributedNetwork isMetered:[%{public}s]", isMetered ? "true" : "false");
84 int32_t result = RegisterNetSupplier(isMetered);
85 if (result != NETMANAGER_SUCCESS) {
86 NETMGR_EXT_LOG_E("WearableDistributedNetAgent RegisterNetSupplier failed, result:[%{public}d]", result);
87 return result;
88 }
89
90 result = EnableWearableDistributedNetForward(tcpPortId, udpPortId);
91 if (result != NETMANAGER_SUCCESS) {
92 NETMGR_EXT_LOG_E("Wearable Distributed NetAgent Enable Forward failed, ret:[%{public}d]", result);
93 return ClearWearableDistributedNetForwardConfig();
94 }
95
96 result = SetInterfaceDummyUp();
97 if (result != NETMANAGER_SUCCESS) {
98 NETMGR_EXT_LOG_E("Wearable Distributed Net Agent SetInterfaceDummyUp failed, result:[%{public}d]", result);
99 return ClearWearableDistributedNetForwardConfig();
100 }
101 isMetered_ = isMetered;
102 return NETMANAGER_SUCCESS;
103 }
104
EnableWearableDistributedNetwork(bool enableFlag)105 int32_t WearableDistributedNetAgent::EnableWearableDistributedNetwork(bool enableFlag)
106 {
107 NETMGR_EXT_LOG_I("EnableWearableDistributedNetwork : %{public}u", enableFlag);
108 int32_t result = UpdateNetSupplierInfo(enableFlag);
109 if (result != NETMANAGER_SUCCESS) {
110 NETMGR_EXT_LOG_E("Wearable Distributed Net Agent UpdateNetSupplierInfo failed, result:[%{public}d]", result);
111 return ClearWearableDistributedNetForwardConfig();
112 }
113
114 if (enableFlag) {
115 result = UpdateNetLinkInfo();
116 if (result != NETMANAGER_SUCCESS) {
117 NETMGR_EXT_LOG_E("Wearable Distributed Net Agent UpdateNetLinkInfo failed, result:[%{public}d]", result);
118 return ClearWearableDistributedNetForwardConfig();
119 }
120 }
121
122 return NETMANAGER_SUCCESS;
123 }
124
TearDownWearableDistributedNetwork()125 int32_t WearableDistributedNetAgent::TearDownWearableDistributedNetwork()
126 {
127 NETMGR_EXT_LOG_I("Wearable Distributed Net Agent TearDown Network");
128 int32_t result = UnregisterNetSupplier();
129 if (result != NETMANAGER_SUCCESS) {
130 NETMGR_EXT_LOG_E("WearableDistributedNetAgent UnregisterNetSupplier failed, result:[%{public}d]", result);
131 return NETMANAGER_EXT_ERR_INTERNAL;
132 }
133 result = DisableWearableDistributedNetForward();
134 if (result != NETMANAGER_SUCCESS) {
135 NETMGR_EXT_LOG_E("Wearable Distributed Net Agent Disable Forward failed, result:[%{public}d]", result);
136 return NETMANAGER_EXT_ERR_INTERNAL;
137 }
138 result = SetInterfaceDummyDown();
139 if (result != NETMANAGER_SUCCESS) {
140 NETMGR_EXT_LOG_E("Wearable Distributed Net Agent Set InterfaceDummyDown failed, result:[%{public}d]", result);
141 return NETMANAGER_EXT_ERR_INTERNAL;
142 }
143 return NETMANAGER_SUCCESS;
144 }
145
RegisterNetSupplier(const bool isMetered)146 int32_t WearableDistributedNetAgent::RegisterNetSupplier(const bool isMetered)
147 {
148 NETMGR_EXT_LOG_I("WearableDistributedNetAgent RegisterNetSupplier");
149 if (netSupplierId_ != 0) {
150 NETMGR_EXT_LOG_D("NetSupplier [%{public}d] has been registered", netSupplierId_);
151 return NETMANAGER_SUCCESS;
152 }
153
154 ObtainNetCaps(isMetered);
155 return NetConnClient::GetInstance().RegisterNetSupplier(BEARER_BLUETOOTH, WEARABLE_DISTRIBUTED_NET_NAME,
156 netCaps_, netSupplierId_);
157 }
158
UnregisterNetSupplier()159 int32_t WearableDistributedNetAgent::UnregisterNetSupplier()
160 {
161 NETMGR_EXT_LOG_I("WearableDistributedNetAgent UnregisterNetSupplier");
162 if (netSupplierId_ == 0) {
163 NETMGR_EXT_LOG_E("NetSupplier [%{public}d] has been unregistered", netSupplierId_);
164 return NETMANAGER_EXT_ERR_INTERNAL;
165 }
166 int32_t result = UpdateNetSupplierInfo(false);
167 if (result != NETMANAGER_SUCCESS) {
168 NETMGR_EXT_LOG_E("WearableDistributedNetAgent UnregisterNetSupplier error, result: [%{public}d]", result);
169 return result;
170 }
171 result = NetConnClient::GetInstance().UnregisterNetSupplier(netSupplierId_);
172 if (result != NETMANAGER_SUCCESS) {
173 NETMGR_EXT_LOG_E("WearableDistributedNetAgent UnregisterNetSupplier error, result: [%{public}d]", result);
174 return NETMANAGER_EXT_ERR_INTERNAL;
175 }
176 netSupplierId_ = 0;
177 return NETMANAGER_SUCCESS;
178 }
179
SetInitNetScore(OHOS::PowerMgr::BatteryChargeState chargeState)180 void WearableDistributedNetAgent::SetInitNetScore(OHOS::PowerMgr::BatteryChargeState chargeState)
181 {
182 switch (chargeState) {
183 case OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_ENABLE:
184 case OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_FULL:
185 score_ = NET_SCORE_WITH_CHARGE_STATE;
186 break;
187 case OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_DISABLE:
188 score_ = NET_SCORE_WITH_UNCHARGE_STATE;
189 break;
190 default:
191 break;
192 }
193 }
194
SetScoreBaseNetStatus(const bool isAvailable)195 void WearableDistributedNetAgent::SetScoreBaseNetStatus(const bool isAvailable)
196 {
197 if (isAvailable) {
198 auto chargeState = OHOS::PowerMgr::BatterySrvClient::GetInstance().GetChargingStatus();
199 SetInitNetScore(chargeState);
200 netSupplierInfo_.score_ = score_;
201 } else {
202 netSupplierInfo_.score_ = 0;
203 }
204 }
205
UpdateNetSupplierInfo(const bool isAvailable)206 int32_t WearableDistributedNetAgent::UpdateNetSupplierInfo(const bool isAvailable)
207 {
208 NETMGR_EXT_LOG_I("WearableDistributedNetAgent UpdateNetSupplierInfo");
209 if (netSupplierId_ == 0) {
210 NETMGR_EXT_LOG_E("WearableDistributedNetAgent UpdateNetSupplierInfo error, netSupplierId is zero");
211 return NETMANAGER_EXT_ERR_INTERNAL;
212 }
213
214 SetNetSupplierInfo(netSupplierInfo_);
215 netSupplierInfo_.isAvailable_ = isAvailable;
216 SetScoreBaseNetStatus(isAvailable);
217 auto networkSupplierInfo = sptr<NetSupplierInfo>::MakeSptr(netSupplierInfo_);
218 if (networkSupplierInfo == nullptr) {
219 NETMGR_EXT_LOG_E("NetSupplierInfo new failed, networkSupplierInfo is nullptr");
220 return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
221 }
222 return NetConnClient::GetInstance().UpdateNetSupplierInfo(netSupplierId_, networkSupplierInfo);
223 }
224
UpdateNetLinkInfo()225 int32_t WearableDistributedNetAgent::UpdateNetLinkInfo()
226 {
227 NETMGR_EXT_LOG_I("Wearable Distributed Net Agent UpdateNetLinkInfo.");
228 if (netSupplierId_ == 0) {
229 NETMGR_EXT_LOG_E("Wearable Distributed Net Agent UpdateNetLinkInfo error, netSupplierId_ is zero");
230 return NETMANAGER_EXT_ERR_INTERNAL;
231 }
232
233 int32_t result = SetNetLinkInfo(netLinkInfo_);
234 if (result != NETMANAGER_SUCCESS) {
235 NETMGR_EXT_LOG_E("Wearable Distributed Net Agent GetNetLinkInfo error, result:[%{public}d]", result);
236 return result;
237 }
238 auto networkLinkInfo = sptr<NetLinkInfo>::MakeSptr(netLinkInfo_);
239 if (networkLinkInfo == nullptr) {
240 NETMGR_EXT_LOG_E("NetLinkInfo new failed, networkLinkInfo is nullptr");
241 return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
242 }
243 return NetConnClient::GetInstance().UpdateNetLinkInfo(netSupplierId_, networkLinkInfo);
244 }
245
UpdateNetCaps(const bool isMetered)246 int32_t WearableDistributedNetAgent::UpdateNetCaps(const bool isMetered)
247 {
248 if (isMetered_ == isMetered) {
249 return NETMANAGER_SUCCESS;
250 }
251
252 ObtainNetCaps(isMetered);
253 int32_t result = NetConnClient::GetInstance().UpdateNetCaps(netCaps_, netSupplierId_);
254 if (result != NETMANAGER_SUCCESS) {
255 return result;
256 }
257 isMetered_ = isMetered;
258 return NETMANAGER_SUCCESS;
259 }
260
UpdateNetScore(const bool isCharging)261 int32_t WearableDistributedNetAgent::UpdateNetScore(const bool isCharging)
262 {
263 SetScoreBaseChargeStatus(isCharging);
264 netSupplierInfo_.score_ = score_;
265 if (netSupplierId_ == 0) {
266 NETMGR_EXT_LOG_E("WearableDistributedNetAgent UpdateNetScore error, netSupplierId is zero");
267 return NETMANAGER_EXT_ERR_INTERNAL;
268 }
269 auto networkSupplierInfo = sptr<NetSupplierInfo>::MakeSptr(netSupplierInfo_);
270 if (networkSupplierInfo == nullptr) {
271 NETMGR_EXT_LOG_E("NetSupplierInfo new failed, networkSupplierInfo is nullptr");
272 return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
273 }
274 return NetConnClient::GetInstance().UpdateNetSupplierInfo(netSupplierId_, networkSupplierInfo);
275 }
276
SetScoreBaseChargeStatus(const bool isCharging)277 void WearableDistributedNetAgent::SetScoreBaseChargeStatus(const bool isCharging)
278 {
279 score_ = isCharging ? NET_SCORE_WITH_CHARGE_STATE : NET_SCORE_WITH_UNCHARGE_STATE;
280 }
281 } // namespace NetManagerStandard
282 } // namespace OHOS
283