• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "network_status.h"
17 
18 #include <cstdint>
19 #include <unistd.h>
20 
21 #include "dfs_error.h"
22 #include "network_set_manager.h"
23 #include "net_conn_callback_observer.h"
24 #include "net_conn_client.h"
25 #include "parameter.h"
26 #include "settings_data_manager.h"
27 #include "utils_log.h"
28 
29 using namespace OHOS::NetManagerStandard;
30 
31 namespace OHOS::FileManagement::CloudSync {
32 static constexpr const int32_t MIN_VALID_NETID = 100;
33 static constexpr const int32_t WAIT_NET_SERVICE_TIME = 4;
34 static const char *NET_MANAGER_ON_STATUS = "2";
35 
RegisterNetConnCallback(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)36 int32_t NetworkStatus::RegisterNetConnCallback(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)
37 {
38     sptr<NetConnCallbackObserver> observer(new (std::nothrow) NetConnCallbackObserver(dataSyncManager));
39     if (observer == nullptr) {
40         LOGE("new operator error.observer is nullptr");
41         return E_GET_NETWORK_MANAGER_FAILED;
42     }
43     int nRet = NetConnClient::GetInstance().RegisterNetConnCallback(observer);
44     if (nRet != NETMANAGER_SUCCESS) {
45         LOGE("RegisterNetConnCallback failed, ret = %{public}d", nRet);
46         return E_GET_NETWORK_MANAGER_FAILED;
47     }
48     return E_OK;
49 }
50 
GetDefaultNet()51 int32_t NetworkStatus::GetDefaultNet()
52 {
53     NetHandle netHandle;
54     int ret = NetConnClient::GetInstance().GetDefaultNet(netHandle);
55     if (ret != NETMANAGER_SUCCESS) {
56         LOGE("GetDefaultNet failed, ret = %{public}d", ret);
57         return E_GET_NETWORK_MANAGER_FAILED;
58     }
59     if (netHandle.GetNetId() < MIN_VALID_NETID) {
60         SetNetConnStatus(NetConnStatus::NO_NETWORK);
61         return E_OK;
62     }
63     NetAllCapabilities netAllCap;
64     ret = NetConnClient::GetInstance().GetNetCapabilities(netHandle, netAllCap);
65     if (ret != NETMANAGER_SUCCESS) {
66         LOGE("GetNetCapbilities failed, ret = %{public}d", ret);
67         return E_GET_NETWORK_MANAGER_FAILED;
68     }
69     SetNetConnStatus(netAllCap);
70     return E_OK;
71 }
72 
SetNetConnStatus(NetManagerStandard::NetAllCapabilities & netAllCap)73 void NetworkStatus::SetNetConnStatus(NetManagerStandard::NetAllCapabilities &netAllCap)
74 {
75     if (netAllCap.netCaps_.count(NetCap::NET_CAPABILITY_INTERNET)) {
76         if (netAllCap.bearerTypes_.count(BEARER_ETHERNET)) {
77             SetNetConnStatus(NetConnStatus::ETHERNET_CONNECT);
78         } else if (netAllCap.bearerTypes_.count(BEARER_WIFI)) {
79             SetNetConnStatus(NetConnStatus::WIFI_CONNECT);
80         } else if (netAllCap.bearerTypes_.count(BEARER_CELLULAR)) {
81             SetNetConnStatus(NetConnStatus::CELLULAR_CONNECT);
82         }
83     } else {
84         SetNetConnStatus(NetConnStatus::NO_NETWORK);
85     }
86 }
87 
GetAndRegisterNetwork(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)88 int32_t NetworkStatus::GetAndRegisterNetwork(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)
89 {
90     int32_t res = GetDefaultNet();
91     if (res != E_OK) {
92         return res;
93     }
94 
95     NetworkSetManager::InitDataSyncManager(dataSyncManager);
96     return RegisterNetConnCallback(dataSyncManager);
97 }
98 
NetWorkChangeStopUploadTask()99 void NetworkStatus::NetWorkChangeStopUploadTask()
100 {
101     NetworkSetManager::NetWorkChangeStopUploadTask();
102 }
103 
InitNetwork(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)104 void NetworkStatus::InitNetwork(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)
105 {
106     int status = WaitParameter("startup.service.ctl.netmanager", NET_MANAGER_ON_STATUS, WAIT_NET_SERVICE_TIME);
107     if (status != 0) {
108         LOGE(" wait SAMGR error, return value %{public}d.", status);
109         return;
110     }
111     constexpr int RETRY_MAX_TIMES = 2;
112     int retryCount = 0;
113     constexpr int RETRY_TIME_INTERVAL_MILLISECOND = 1 * 1000 * 1000;
114     do {
115         if (GetAndRegisterNetwork(dataSyncManager) == E_OK) {
116             break;
117         }
118         LOGE("wait and retry registering network callback");
119         retryCount++;
120         usleep(RETRY_TIME_INTERVAL_MILLISECOND);
121     } while (retryCount < RETRY_MAX_TIMES);
122 }
123 
SetNetConnStatus(NetworkStatus::NetConnStatus netStatus)124 void NetworkStatus::SetNetConnStatus(NetworkStatus::NetConnStatus netStatus)
125 {
126     netStatus_ = netStatus;
127     NetworkSetManager::SetNetConnStatus(static_cast<NetworkSetManager::NetConnStatus>(netStatus));
128     return;
129 }
130 
GetNetConnStatus()131 NetworkStatus::NetConnStatus NetworkStatus::GetNetConnStatus()
132 {
133     return netStatus_;
134 }
135 
CheckMobileNetwork(const std::string & bundleName,const int32_t userId)136 bool NetworkStatus::CheckMobileNetwork(const std::string &bundleName, const int32_t userId)
137 {
138     if (bundleName == GALLERY_BUNDLE_NAME) {
139         if (NetworkSetManager::IsAllowCellularConnect(bundleName, userId)) {
140             LOGI("datashare status open, CheckMobileNetwork success");
141             return true;
142         }
143     } else if (bundleName == HDC_BUNDLE_NAME) {
144         if (SettingsDataManager::GetMobileDataStatus()) {
145             LOGI("ailife is setting mobie data sync");
146             return true;
147         }
148     } else {
149         return true;
150     }
151     return CheckWifiOrEthernet();
152 }
153 
CheckNetwork(const std::string & bundleName,const int32_t userId)154 bool NetworkStatus::CheckNetwork(const std::string &bundleName, const int32_t userId)
155 {
156     if (bundleName == GALLERY_BUNDLE_NAME || bundleName == HDC_BUNDLE_NAME) {
157         if (SettingsDataManager::GetNetworkConnectionStatus()) {
158             LOGI("settingData is set network connection");
159             return true;
160         }
161     } else {
162         return true;
163     }
164     LOGI("CheckNetwork off");
165     return false;
166 }
167 
CheckWifiOrEthernet()168 bool NetworkStatus::CheckWifiOrEthernet()
169 {
170     if (netStatus_ == WIFI_CONNECT) {
171         LOGI("datashare status close, network_status:wifi");
172         return true;
173     }
174     if (netStatus_ == ETHERNET_CONNECT) {
175         LOGI("datashare status close, network_status:ethernet");
176         return true;
177     }
178     LOGI("CheckWifiOrEthernet off");
179     return false;
180 }
181 } // namespace OHOS::FileManagement::CloudSync