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