• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     result = UpdateNetSupplierInfo(true);
96     if (result != NETMANAGER_SUCCESS) {
97         NETMGR_EXT_LOG_E("Wearable Distributed Net Agent UpdateNetSupplierInfo failed, result:[%{public}d]", result);
98         return ClearWearableDistributedNetForwardConfig();
99     }
100     result = UpdateNetLinkInfo();
101     if (result != NETMANAGER_SUCCESS) {
102         NETMGR_EXT_LOG_E("Wearable Distributed Net Agent UpdateNetLinkInfo failed, result:[%{public}d]", result);
103         return ClearWearableDistributedNetForwardConfig();
104     }
105     result = SetInterfaceDummyUp();
106     if (result != NETMANAGER_SUCCESS) {
107         NETMGR_EXT_LOG_E("Wearable Distributed Net Agent SetInterfaceDummyUp failed, result:[%{public}d]", result);
108         return ClearWearableDistributedNetForwardConfig();
109     }
110     isMetered_ = isMetered;
111     return NETMANAGER_SUCCESS;
112 }
113 
TearDownWearableDistributedNetwork()114 int32_t WearableDistributedNetAgent::TearDownWearableDistributedNetwork()
115 {
116     NETMGR_EXT_LOG_I("Wearable Distributed Net Agent TearDown Network");
117     int32_t result = UnregisterNetSupplier();
118     if (result != NETMANAGER_SUCCESS) {
119         NETMGR_EXT_LOG_E("WearableDistributedNetAgent UnregisterNetSupplier failed, result:[%{public}d]", result);
120         return NETMANAGER_EXT_ERR_INTERNAL;
121     }
122     result = DisableWearableDistributedNetForward();
123     if (result != NETMANAGER_SUCCESS) {
124         NETMGR_EXT_LOG_E("Wearable Distributed Net Agent Disable Forward failed, result:[%{public}d]", result);
125         return NETMANAGER_EXT_ERR_INTERNAL;
126     }
127     result = SetInterfaceDummyDown();
128     if (result != NETMANAGER_SUCCESS) {
129         NETMGR_EXT_LOG_E("Wearable Distributed Net Agent Set InterfaceDummyDown failed, result:[%{public}d]", result);
130         return NETMANAGER_EXT_ERR_INTERNAL;
131     }
132     return NETMANAGER_SUCCESS;
133 }
134 
RegisterNetSupplier(const bool isMetered)135 int32_t WearableDistributedNetAgent::RegisterNetSupplier(const bool isMetered)
136 {
137     NETMGR_EXT_LOG_I("WearableDistributedNetAgent RegisterNetSupplier");
138     if (netSupplierId_ != 0) {
139         NETMGR_EXT_LOG_D("NetSupplier [%{public}d] has been registered", netSupplierId_);
140         return NETMANAGER_SUCCESS;
141     }
142 
143     ObtainNetCaps(isMetered);
144     return NetConnClient::GetInstance().RegisterNetSupplier(BEARER_BLUETOOTH, WEARABLE_DISTRIBUTED_NET_NAME,
145         netCaps_, netSupplierId_);
146 }
147 
UnregisterNetSupplier()148 int32_t WearableDistributedNetAgent::UnregisterNetSupplier()
149 {
150     NETMGR_EXT_LOG_I("WearableDistributedNetAgent UnregisterNetSupplier");
151     if (netSupplierId_ == 0) {
152         NETMGR_EXT_LOG_E("NetSupplier [%{public}d] has been unregistered", netSupplierId_);
153         return NETMANAGER_EXT_ERR_INTERNAL;
154     }
155     int32_t result = UpdateNetSupplierInfo(false);
156     if (result != NETMANAGER_SUCCESS) {
157         NETMGR_EXT_LOG_E("WearableDistributedNetAgent UnregisterNetSupplier error, result: [%{public}d]", result);
158         return result;
159     }
160     result = NetConnClient::GetInstance().UnregisterNetSupplier(netSupplierId_);
161     if (result != NETMANAGER_SUCCESS) {
162         NETMGR_EXT_LOG_E("WearableDistributedNetAgent UnregisterNetSupplier error, result: [%{public}d]", result);
163         return NETMANAGER_EXT_ERR_INTERNAL;
164     }
165     netSupplierId_  = 0;
166     return NETMANAGER_SUCCESS;
167 }
168 
SetInitNetScore(OHOS::PowerMgr::BatteryChargeState chargeState)169 void WearableDistributedNetAgent::SetInitNetScore(OHOS::PowerMgr::BatteryChargeState chargeState)
170 {
171     switch (chargeState) {
172         case OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_ENABLE:
173         case OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_FULL:
174             score_ = NET_SCORE_WITH_CHARGE_STATE;
175             break;
176         case OHOS::PowerMgr::BatteryChargeState::CHARGE_STATE_DISABLE:
177             score_ = NET_SCORE_WITH_UNCHARGE_STATE;
178             break;
179         default:
180             break;
181     }
182 }
183 
SetScoreBaseNetStatus(const bool isAvailable)184 void WearableDistributedNetAgent::SetScoreBaseNetStatus(const bool isAvailable)
185 {
186     if (isAvailable) {
187         auto chargeState = OHOS::PowerMgr::BatterySrvClient::GetInstance().GetChargingStatus();
188         SetInitNetScore(chargeState);
189         netSupplierInfo_.score_ = score_;
190     } else {
191         netSupplierInfo_.score_ = 0;
192     }
193 }
194 
UpdateNetSupplierInfo(const bool isAvailable)195 int32_t WearableDistributedNetAgent::UpdateNetSupplierInfo(const bool isAvailable)
196 {
197     NETMGR_EXT_LOG_I("WearableDistributedNetAgent UpdateNetSupplierInfo");
198     if (netSupplierId_ == 0) {
199         NETMGR_EXT_LOG_E("WearableDistributedNetAgent UpdateNetSupplierInfo error, netSupplierId is zero");
200         return NETMANAGER_EXT_ERR_INTERNAL;
201     }
202 
203     SetNetSupplierInfo(netSupplierInfo_);
204     netSupplierInfo_.isAvailable_ = isAvailable;
205     SetScoreBaseNetStatus(isAvailable);
206     auto networkSupplierInfo = sptr<NetSupplierInfo>::MakeSptr(netSupplierInfo_);
207     if (networkSupplierInfo == nullptr) {
208         NETMGR_EXT_LOG_E("NetSupplierInfo new failed, networkSupplierInfo is nullptr");
209         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
210     }
211     return NetConnClient::GetInstance().UpdateNetSupplierInfo(netSupplierId_, networkSupplierInfo);
212 }
213 
UpdateNetLinkInfo()214 int32_t WearableDistributedNetAgent::UpdateNetLinkInfo()
215 {
216     NETMGR_EXT_LOG_I("Wearable Distributed Net Agent UpdateNetLinkInfo.");
217     if (netSupplierId_ == 0) {
218         NETMGR_EXT_LOG_E("Wearable Distributed Net Agent UpdateNetLinkInfo error, netSupplierId_ is zero");
219         return NETMANAGER_EXT_ERR_INTERNAL;
220     }
221 
222     int32_t result = SetNetLinkInfo(netLinkInfo_);
223     if (result != NETMANAGER_SUCCESS) {
224         NETMGR_EXT_LOG_E("Wearable Distributed Net Agent GetNetLinkInfo error, result:[%{public}d]", result);
225         return result;
226     }
227     auto networkLinkInfo = sptr<NetLinkInfo>::MakeSptr(netLinkInfo_);
228     if (networkLinkInfo == nullptr) {
229         NETMGR_EXT_LOG_E("NetLinkInfo new failed, networkLinkInfo is nullptr");
230         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
231     }
232     return NetConnClient::GetInstance().UpdateNetLinkInfo(netSupplierId_, networkLinkInfo);
233 }
234 
UpdateNetCaps(const bool isMetered)235 int32_t WearableDistributedNetAgent::UpdateNetCaps(const bool isMetered)
236 {
237     if (isMetered_ == isMetered) {
238         return NETMANAGER_SUCCESS;
239     }
240 
241     ObtainNetCaps(isMetered);
242     int32_t result = NetConnClient::GetInstance().UpdateNetCaps(netCaps_, netSupplierId_);
243     if (result != NETMANAGER_SUCCESS) {
244         return result;
245     }
246     isMetered_ = isMetered;
247     return NETMANAGER_SUCCESS;
248 }
249 
UpdateNetScore(const bool isCharging)250 int32_t WearableDistributedNetAgent::UpdateNetScore(const bool isCharging)
251 {
252     SetScoreBaseChargeStatus(isCharging);
253     netSupplierInfo_.score_ = score_;
254     if (netSupplierId_ == 0) {
255         NETMGR_EXT_LOG_E("WearableDistributedNetAgent UpdateNetScore error, netSupplierId is zero");
256         return NETMANAGER_EXT_ERR_INTERNAL;
257     }
258     auto networkSupplierInfo = sptr<NetSupplierInfo>::MakeSptr(netSupplierInfo_);
259     if (networkSupplierInfo == nullptr) {
260         NETMGR_EXT_LOG_E("NetSupplierInfo new failed, networkSupplierInfo is nullptr");
261         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
262     }
263     return NetConnClient::GetInstance().UpdateNetSupplierInfo(netSupplierId_, networkSupplierInfo);
264 }
265 
SetScoreBaseChargeStatus(const bool isCharging)266 void WearableDistributedNetAgent::SetScoreBaseChargeStatus(const bool isCharging)
267 {
268     score_ = isCharging ? NET_SCORE_WITH_CHARGE_STATE : NET_SCORE_WITH_UNCHARGE_STATE;
269 }
270 } // namespace NetManagerStandard
271 } // namespace OHOS
272