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 "net_conn_client.h"
22 #include "parameter.h"
23 
24 #include "dfs_error.h"
25 #include "network_set_manager.h"
26 #include "net_conn_callback_observer.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     return;
128 }
129 
GetNetConnStatus()130 NetworkStatus::NetConnStatus NetworkStatus::GetNetConnStatus()
131 {
132     return netStatus_;
133 }
134 
CheckMobileNetwork(const std::string & bundleName,const int32_t userId)135 bool NetworkStatus::CheckMobileNetwork(const std::string &bundleName, const int32_t userId)
136 {
137     if (bundleName != "com.ohos.photos") {
138         return true;
139     }
140     if (NetworkSetManager::IsAllowCellularConnect(bundleName, userId)) {
141         LOGI("datashare status open, CheckMobileNetwork success");
142         return true;
143     }
144     if (netStatus_ == WIFI_CONNECT) {
145         LOGI("datashare status close, networkdtatus:wifi");
146         return true;
147     }
148     return false;
149 }
150 
CheckNetwork(const std::string & bundleName,const int32_t userId)151 bool NetworkStatus::CheckNetwork(const std::string &bundleName, const int32_t userId)
152 {
153     if (bundleName != "com.ohos.photos") {
154         return true;
155     }
156     if (NetworkSetManager::IsAllowNetConnect(bundleName, userId)) {
157         LOGI("CheckNetwork on");
158         return true;
159     }
160     LOGI("CheckNetwork off");
161     return false;
162 }
163 } // namespace OHOS::FileManagement::CloudSync