• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_exec.h"
17 
18 #include "net_conn_client.h"
19 #include "net_manager_constants.h"
20 #include "network_constant.h"
21 #ifndef HAS_TELEPHONY
22 #define HAS_TELEPHONY 0
23 #endif
24 
25 #if HAS_TELEPHONY
26 #include "core_service_client.h"
27 #endif
28 #include "napi_utils.h"
29 #include "netmanager_base_log.h"
30 #include "network_observer.h"
31 #include "securec.h"
32 
33 namespace OHOS::NetManagerStandard {
34 static constexpr const int ERROR_PARAM_NUM = 2;
35 static constexpr const char *ERROR_MSG = "failed";
36 static constexpr const char *NETWORK_NONE = "none";
37 static constexpr const char *NETWORK_WIFI = "WiFi";
38 static constexpr const uint32_t DEFAULT_TIMEOUT_MS = 1000;
39 
40 #if HAS_TELEPHONY
CellularTypeToString(Telephony::SignalInformation::NetworkType type)41 static std::string CellularTypeToString(Telephony::SignalInformation::NetworkType type)
42 {
43     switch (type) {
44         case Telephony::SignalInformation::NetworkType::GSM:
45             return "2g";
46         case Telephony::SignalInformation::NetworkType::CDMA:
47         case Telephony::SignalInformation::NetworkType::WCDMA:
48         case Telephony::SignalInformation::NetworkType::TDSCDMA:
49             return "3g";
50         case Telephony::SignalInformation::NetworkType::LTE:
51             return "4g";
52         default:
53             break;
54     }
55     return "5g";
56 }
57 #endif
58 
MakeNetworkResponse(napi_env env,const std::set<NetBearType> & bearerTypes)59 static napi_value MakeNetworkResponse(napi_env env, const std::set<NetBearType> &bearerTypes)
60 {
61     napi_value obj = NapiUtils::CreateObject(env);
62     if (bearerTypes.find(BEARER_WIFI) != bearerTypes.end()) {
63         NapiUtils::SetStringPropertyUtf8(env, obj, KEY_TYPE, NETWORK_WIFI);
64         NapiUtils::SetBooleanProperty(env, obj, KEY_METERED, false);
65         return obj;
66     }
67 
68 #if HAS_TELEPHONY
69     if (bearerTypes.find(BEARER_CELLULAR) != bearerTypes.end()) {
70         std::vector<sptr<Telephony::SignalInformation>> vec;
71         DelayedRefSingleton<Telephony::CoreServiceClient>::GetInstance().GetSignalInfoList(0, vec);
72         if (vec.empty()) {
73             NapiUtils::SetStringPropertyUtf8(env, obj, KEY_TYPE, NETWORK_NONE);
74             NapiUtils::SetBooleanProperty(env, obj, KEY_METERED, false);
75             return obj;
76         }
77 
78         std::sort(vec.begin(), vec.end(),
79                   [](const sptr<Telephony::SignalInformation> &info1, const sptr<Telephony::SignalInformation> &info2)
80                       -> bool { return info1->GetSignalLevel() > info2->GetSignalLevel(); });
81         NapiUtils::SetStringPropertyUtf8(env, obj, KEY_TYPE, CellularTypeToString(vec[0]->GetNetworkType()));
82         NapiUtils::SetBooleanProperty(env, obj, KEY_METERED, true);
83         return obj;
84     }
85 #endif
86 
87     NapiUtils::SetStringPropertyUtf8(env, obj, KEY_TYPE, NETWORK_NONE);
88     NapiUtils::SetBooleanProperty(env, obj, KEY_METERED, false);
89     return obj;
90 }
91 
ExecGetType(GetTypeContext * context)92 bool NetworkExec::ExecGetType(GetTypeContext *context)
93 {
94     NETMANAGER_BASE_LOGI("NetworkExec::ExecGetType");
95     NetHandle handle;
96     auto ret = NetConnClient::GetInstance().GetDefaultNet(handle);
97     if (ret != NETMANAGER_SUCCESS) {
98         context->SetErrorCode(ret);
99         return ret == NETMANAGER_SUCCESS;
100     }
101 
102     if (handle.GetNetId() == 0) {
103         context->SetErrorCode(NETMANAGER_ERR_INTERNAL);
104         return false;
105     }
106 
107     NetAllCapabilities cap;
108     ret = NetConnClient::GetInstance().GetNetCapabilities(handle, cap);
109     if (ret == NETMANAGER_SUCCESS) {
110         context->SetCap(cap);
111     }
112 
113     context->SetErrorCode(ret);
114     return ret == NETMANAGER_SUCCESS;
115 }
116 
GetTypeCallback(GetTypeContext * context)117 napi_value NetworkExec::GetTypeCallback(GetTypeContext *context)
118 {
119     if (!context->IsExecOK()) {
120         napi_value fail = context->GetFailCallback();
121         if (NapiUtils::GetValueType(context->GetEnv(), fail) == napi_function) {
122             napi_value argv[ERROR_PARAM_NUM] = {
123                 NapiUtils::CreateStringUtf8(context->GetEnv(), ERROR_MSG),
124                 NapiUtils::CreateInt32(context->GetEnv(), context->GetErrorCode()),
125             };
126             NapiUtils::CallFunction(context->GetEnv(), NapiUtils::GetUndefined(context->GetEnv()), fail,
127                                     ERROR_PARAM_NUM, argv);
128         }
129 
130         napi_value complete = context->GetCompleteCallback();
131         // if ok complete will be called in observer
132         if (NapiUtils::GetValueType(context->GetEnv(), complete) == napi_function) {
133             NapiUtils::CallFunction(context->GetEnv(), NapiUtils::GetUndefined(context->GetEnv()), complete, 0,
134                                     nullptr);
135         }
136     } else {
137         napi_value success = context->GetSuccessCallback();
138         if (NapiUtils::GetValueType(context->GetEnv(), success) == napi_function) {
139             auto cap = context->GetCap();
140             auto obj = MakeNetworkResponse(context->GetEnv(), cap.bearerTypes_);
141             NapiUtils::CallFunction(context->GetEnv(), NapiUtils::GetUndefined(context->GetEnv()), success, 1, &obj);
142         }
143     }
144 
145     return NapiUtils::GetUndefined(context->GetEnv());
146 }
147 
ExecSubscribe(SubscribeContext * context)148 bool NetworkExec::ExecSubscribe(SubscribeContext *context)
149 {
150     NETMANAGER_BASE_LOGI("NetworkExec::ExecSubscribe");
151     EventManager *manager = context->GetManager();
152 
153     sptr<INetConnCallback> callback = g_observerMap[manager];
154     if (callback == nullptr) {
155         return false;
156     }
157     sptr<NetSpecifier> specifier = new NetSpecifier;
158     specifier->netCapabilities_.netCaps_.insert(NET_CAPABILITY_INTERNET);
159     NetConnClient::GetInstance().UnregisterNetConnCallback(callback);
160     int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(specifier, callback, DEFAULT_TIMEOUT_MS);
161 
162     context->SetErrorCode(ret);
163     return ret == NETMANAGER_SUCCESS;
164 }
165 
SubscribeCallback(SubscribeContext * context)166 napi_value NetworkExec::SubscribeCallback(SubscribeContext *context)
167 {
168     if (!context->IsExecOK()) {
169         napi_value fail = context->GetFailCallback();
170         if (NapiUtils::GetValueType(context->GetEnv(), fail) == napi_function) {
171             napi_value argv[ERROR_PARAM_NUM] = {
172                 NapiUtils::CreateStringUtf8(context->GetEnv(), ERROR_MSG),
173                 NapiUtils::CreateInt32(context->GetEnv(), context->GetErrorCode()),
174             };
175             NapiUtils::CallFunction(context->GetEnv(), NapiUtils::GetUndefined(context->GetEnv()), fail,
176                                     ERROR_PARAM_NUM, argv);
177         }
178     }
179 
180     return NapiUtils::GetUndefined(context->GetEnv());
181 }
182 
ExecUnsubscribe(UnsubscribeContext * context)183 bool NetworkExec::ExecUnsubscribe(UnsubscribeContext *context)
184 {
185     EventManager *manager = context->GetManager();
186     sptr<INetConnCallback> callback = g_observerMap[manager];
187     if (callback == nullptr) {
188         return false;
189     }
190 
191     int32_t ret = NetConnClient::GetInstance().UnregisterNetConnCallback(callback);
192     context->SetErrorCode(ret);
193     return ret == NETMANAGER_SUCCESS;
194 }
195 
UnsubscribeCallback(UnsubscribeContext * context)196 napi_value NetworkExec::UnsubscribeCallback(UnsubscribeContext *context)
197 {
198     context->GetManager()->DeleteListener(EVENT_SUBSCRIBE);
199     return NapiUtils::GetUndefined(context->GetEnv());
200 }
201 } // namespace OHOS::NetManagerStandard