• 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 "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     return RegisterNetConnCallback(dataSyncManager);
96 }
97 
InitNetwork(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)98 void NetworkStatus::InitNetwork(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)
99 {
100     int status = WaitParameter("startup.service.ctl.netmanager", NET_MANAGER_ON_STATUS, WAIT_NET_SERVICE_TIME);
101     if (status != 0) {
102         LOGE(" wait SAMGR error, return value %{public}d.", status);
103         return;
104     }
105     constexpr int RETRY_MAX_TIMES = 2;
106     int retryCount = 0;
107     constexpr int RETRY_TIME_INTERVAL_MILLISECOND = 1 * 1000 * 1000;
108     do {
109         if (GetAndRegisterNetwork(dataSyncManager) == E_OK) {
110             break;
111         }
112         LOGE("wait and retry registering network callback");
113         retryCount++;
114         usleep(RETRY_TIME_INTERVAL_MILLISECOND);
115     } while (retryCount < RETRY_MAX_TIMES);
116 }
117 
SetNetConnStatus(NetworkStatus::NetConnStatus netStatus)118 void NetworkStatus::SetNetConnStatus(NetworkStatus::NetConnStatus netStatus)
119 {
120     netStatus_ = netStatus;
121     return;
122 }
123 
GetNetConnStatus()124 NetworkStatus::NetConnStatus NetworkStatus::GetNetConnStatus()
125 {
126     return netStatus_;
127 }
128 
CheckMobileNetwork(const std::string & bundleName,const int32_t userId)129 bool NetworkStatus::CheckMobileNetwork(const std::string &bundleName, const int32_t userId)
130 {
131     if (bundleName != "com.ohos.photos") {
132         return true;
133     }
134     if (NetworkSetManager::IsAllowCellularConnect(bundleName, userId)) {
135         LOGI("datashare status open, CheckMobileNetwork success");
136         return true;
137     }
138     if (netStatus_ == WIFI_CONNECT) {
139         LOGI("datashare status close, networkdtatus:wifi");
140         return true;
141     }
142     return false;
143 }
144 
CheckNetwork(const std::string & bundleName,const int32_t userId)145 bool NetworkStatus::CheckNetwork(const std::string &bundleName, const int32_t userId)
146 {
147     if (bundleName != "com.ohos.photos") {
148         return true;
149     }
150     if (NetworkSetManager::IsAllowNetConnect(bundleName, userId)) {
151         LOGI("CheckNetwork on");
152         return true;
153     }
154     LOGI("CheckNetwork off");
155     return false;
156 }
157 } // namespace OHOS::FileManagement::CloudSync