1 /*
2 * Copyright (c) 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 "sync_rule/network_status.h"
17
18 #include <cstdint>
19 #include <unistd.h>
20
21 #include "net_conn_client.h"
22 #include "parameter.h"
23
24 #include "dfs_error.h"
25 #include "sync_rule/net_conn_callback_observer.h"
26 #include "utils_log.h"
27
28 using namespace OHOS::NetManagerStandard;
29
30 namespace OHOS::FileManagement::CloudSync {
31 static constexpr const int32_t MIN_VALID_NETID = 100;
32 static constexpr const int32_t WAIT_NET_SERVICE_TIME = 4;
33 static const char *g_netManagerOnStatus = "2";
34
RegisterNetConnCallback(std::shared_ptr<DataSyncManager> dataSyncManager)35 int32_t NetworkStatus::RegisterNetConnCallback(std::shared_ptr<DataSyncManager> dataSyncManager)
36 {
37 sptr<NetConnCallbackObserver> observer(new (std::nothrow) NetConnCallbackObserver(dataSyncManager));
38 if (observer == nullptr) {
39 LOGE("new operator error.observer is nullptr");
40 return E_GET_NETWORK_MANAGER_FAILED;
41 }
42 int nRet = NetConnClient::GetInstance().RegisterNetConnCallback(observer);
43 if (nRet != NETMANAGER_SUCCESS) {
44 LOGE("RegisterNetConnCallback failed, ret = %{public}d", nRet);
45 return E_GET_NETWORK_MANAGER_FAILED;
46 }
47 return E_OK;
48 }
49
GetDefaultNet()50 int32_t NetworkStatus::GetDefaultNet()
51 {
52 NetHandle netHandle;
53 int ret = NetConnClient::GetInstance().GetDefaultNet(netHandle);
54 if (ret != NETMANAGER_SUCCESS) {
55 LOGE("GetDefaultNet failed, ret = %{public}d", ret);
56 return E_GET_NETWORK_MANAGER_FAILED;
57 }
58 if (netHandle.GetNetId() < MIN_VALID_NETID) {
59 SetNetConnStatus(NetConnStatus::NO_NETWORK);
60 return E_OK;
61 }
62 NetAllCapabilities netAllCap;
63 ret = NetConnClient::GetInstance().GetNetCapabilities(netHandle, netAllCap);
64 if (ret != NETMANAGER_SUCCESS) {
65 LOGE("GetNetCapbilities failed, ret = %{public}d", ret);
66 return E_GET_NETWORK_MANAGER_FAILED;
67 }
68 SetNetConnStatus(netAllCap);
69 return E_OK;
70 }
71
SetNetConnStatus(NetManagerStandard::NetAllCapabilities & netAllCap)72 void NetworkStatus::SetNetConnStatus(NetManagerStandard::NetAllCapabilities &netAllCap)
73 {
74 if (netAllCap.netCaps_.count(NetCap::NET_CAPABILITY_INTERNET)) {
75 if (netAllCap.bearerTypes_.count(BEARER_ETHERNET)) {
76 SetNetConnStatus(NetConnStatus::ETHERNET_CONNECT);
77 } else if (netAllCap.bearerTypes_.count(BEARER_WIFI)) {
78 SetNetConnStatus(NetConnStatus::WIFI_CONNECT);
79 } else if (netAllCap.bearerTypes_.count(BEARER_CELLULAR)) {
80 SetNetConnStatus(NetConnStatus::CELLULAR_CONNECT);
81 }
82 } else {
83 SetNetConnStatus(NetConnStatus::NO_NETWORK);
84 }
85 }
86
GetAndRegisterNetwork(std::shared_ptr<DataSyncManager> dataSyncManager)87 int32_t NetworkStatus::GetAndRegisterNetwork(std::shared_ptr<DataSyncManager> dataSyncManager)
88 {
89 int32_t res = GetDefaultNet();
90 if (res != E_OK) {
91 return res;
92 }
93
94 return RegisterNetConnCallback(dataSyncManager);
95 }
96
InitNetwork(std::shared_ptr<DataSyncManager> dataSyncManager)97 void NetworkStatus::InitNetwork(std::shared_ptr<DataSyncManager> dataSyncManager)
98 {
99 int status = WaitParameter("startup.service.ctl.netmanager", g_netManagerOnStatus, WAIT_NET_SERVICE_TIME);
100 if (status != 0) {
101 LOGE(" wait SAMGR error, return value %{public}d.", status);
102 return;
103 }
104 constexpr int RETRY_MAX_TIMES = 2;
105 int retryCount = 0;
106 constexpr int RETRY_TIME_INTERVAL_MILLISECOND = 1 * 1000 * 1000;
107 do {
108 if (GetAndRegisterNetwork(dataSyncManager) == E_OK) {
109 break;
110 }
111 LOGE("wait and retry registering network callback");
112 retryCount++;
113 usleep(RETRY_TIME_INTERVAL_MILLISECOND);
114 } while (retryCount < RETRY_MAX_TIMES);
115 }
116
SetNetConnStatus(NetworkStatus::NetConnStatus netStatus)117 void NetworkStatus::SetNetConnStatus(NetworkStatus::NetConnStatus netStatus)
118 {
119 netStatus_ = netStatus;
120 return;
121 }
122
GetNetConnStatus()123 NetworkStatus::NetConnStatus NetworkStatus::GetNetConnStatus()
124 {
125 return netStatus_;
126 }
127
OnNetworkAvail()128 void NetworkStatus::OnNetworkAvail()
129 {
130 if (netStatus_ == NetConnStatus::NO_NETWORK) {
131 netStatus_ = NetConnStatus::NETWORK_AVAIL;
132 }
133 }
134 } // namespace OHOS::FileManagement::CloudSync