1 /*
2 * Copyright (C) 2021 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 "napi_cellular_data.h"
17
18 #include <memory>
19
20 #include "cellular_data_client.h"
21 #include "cellular_data_types.h"
22 #include "cstddef"
23 #include "iosfwd"
24 #include "js_native_api.h"
25 #include "js_native_api_types.h"
26 #include "napi/native_common.h"
27 #include "napi_util.h"
28 #include "node_api.h"
29 #include "string"
30 #include "telephony_log_wrapper.h"
31 #include "telephony_napi_common_error.h"
32
33 namespace OHOS {
34 namespace Telephony {
35 static constexpr int32_t DEFAULT_REF_COUNT = 1;
36 static constexpr const char *SET_TELEPHONY_STATE = "ohos.permission.SET_TELEPHONY_STATE";
37 static constexpr const char *GET_NETWORK_INFO = "ohos.permission.GET_NETWORK_INFO";
38
IsCellularDataManagerInited()39 static bool IsCellularDataManagerInited()
40 {
41 return CellularDataClient::GetInstance().IsConnect();
42 }
43
IsValidSlotId(int32_t slotId)44 static inline bool IsValidSlotId(int32_t slotId)
45 {
46 return ((slotId >= DEFAULT_SIM_SLOT_ID) && (slotId < SIM_SLOT_COUNT));
47 }
48
IsValidSlotIdEx(int32_t slotId)49 static inline bool IsValidSlotIdEx(int32_t slotId)
50 {
51 // One more slot for VSim.
52 return ((slotId >= DEFAULT_SIM_SLOT_ID) && (slotId < SIM_SLOT_COUNT + 1));
53 }
54
MatchCellularDataParameters(napi_env env,const napi_value parameters[],const size_t parameterCount)55 static bool MatchCellularDataParameters(napi_env env, const napi_value parameters[], const size_t parameterCount)
56 {
57 switch (parameterCount) {
58 case 0:
59 return true;
60 case 1:
61 return NapiUtil::MatchParameters(env, parameters, {napi_function});
62 default:
63 return false;
64 }
65 }
66
MatchEnableCellularDataRoamingParameters(napi_env env,const napi_value parameters[],const size_t parameterCount)67 static bool MatchEnableCellularDataRoamingParameters(
68 napi_env env, const napi_value parameters[], const size_t parameterCount)
69 {
70 const size_t paramLimitOne = 1;
71 const size_t paramLimitTwo = 2;
72 switch (parameterCount) {
73 case paramLimitOne:
74 return NapiUtil::MatchParameters(env, parameters, {napi_number});
75 case paramLimitTwo:
76 return NapiUtil::MatchParameters(env, parameters, {napi_number, napi_function});
77 default:
78 return false;
79 }
80 }
81
WrapCellularDataType(const int32_t cellularDataType)82 static int32_t WrapCellularDataType(const int32_t cellularDataType)
83 {
84 switch (cellularDataType) {
85 case static_cast<int32_t>(DataConnectionStatus::DATA_STATE_DISCONNECTED): {
86 return static_cast<int32_t>(DataConnectState::DATA_STATE_DISCONNECTED);
87 }
88 case static_cast<int32_t>(DataConnectionStatus::DATA_STATE_CONNECTING): {
89 return static_cast<int32_t>(DataConnectState::DATA_STATE_CONNECTING);
90 }
91 case static_cast<int32_t>(DataConnectionStatus::DATA_STATE_CONNECTED): {
92 return static_cast<int32_t>(DataConnectState::DATA_STATE_CONNECTED);
93 }
94 case static_cast<int32_t>(DataConnectionStatus::DATA_STATE_SUSPENDED): {
95 return static_cast<int32_t>(DataConnectState::DATA_STATE_SUSPENDED);
96 }
97 default: {
98 return static_cast<int32_t>(DataConnectState::DATA_STATE_UNKNOWN);
99 }
100 }
101 }
102
WrapGetCellularDataFlowTypeType(const int32_t cellularDataType)103 static int32_t WrapGetCellularDataFlowTypeType(const int32_t cellularDataType)
104 {
105 switch (cellularDataType) {
106 case static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_NONE): {
107 return static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_NONE);
108 }
109 case static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_DOWN): {
110 return static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_DOWN);
111 }
112 case static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_UP): {
113 return static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_UP);
114 }
115 case static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_UP_DOWN): {
116 return static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_UP_DOWN);
117 }
118 case static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_DORMANT): {
119 return static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_DORMANT);
120 }
121 default: {
122 return static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_NONE);
123 }
124 }
125 }
126
NativeGetCellularDataState(napi_env env,void * data)127 static void NativeGetCellularDataState(napi_env env, void *data)
128 {
129 auto asyncContext = static_cast<AsyncContext *>(data);
130 if (asyncContext == nullptr) {
131 return;
132 }
133 if (IsCellularDataManagerInited()) {
134 int32_t dataState = CellularDataClient::GetInstance().GetCellularDataState();
135 asyncContext->resolved = true;
136 asyncContext->result = WrapCellularDataType(dataState);
137 } else {
138 asyncContext->resolved = false;
139 asyncContext->errorCode = ERROR_SERVICE_UNAVAILABLE;
140 }
141 }
142
GetCellularDataStateCallback(napi_env env,napi_status status,void * data)143 static void GetCellularDataStateCallback(napi_env env, napi_status status, void *data)
144 {
145 auto context = static_cast<AsyncContext *>(data);
146 if (context == nullptr) {
147 return;
148 }
149 std::unique_ptr<AsyncContext> asyncContext(context);
150 napi_value callbackValue = nullptr;
151 if (asyncContext->resolved) {
152 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, asyncContext->result, &callbackValue));
153 } else {
154 if (asyncContext->errorCode == ERROR_SERVICE_UNAVAILABLE) {
155 callbackValue =
156 NapiUtil::CreateErrorMessage(env, "cellular data service unavailable", ERROR_SERVICE_UNAVAILABLE);
157 } else {
158 callbackValue = NapiUtil::CreateErrorMessage(env, "unkonwn error");
159 }
160 }
161 NapiUtil::Handle2ValueCallback(env, asyncContext.release(), callbackValue);
162 }
163
GetCellularDataState(napi_env env,napi_callback_info info)164 static napi_value GetCellularDataState(napi_env env, napi_callback_info info)
165 {
166 size_t parameterCount = 1;
167 napi_value parameters[1];
168 napi_value thisVar = nullptr;
169 void *data = nullptr;
170 NAPI_CALL(env, napi_get_cb_info(env, info, ¶meterCount, parameters, &thisVar, &data));
171 NAPI_ASSERT(env, MatchCellularDataParameters(env, parameters, parameterCount), "type mismatch");
172 auto asyncContext = std::make_unique<AsyncContext>();
173 if (asyncContext == nullptr) {
174 std::string errorCode = std::to_string(napi_generic_failure);
175 std::string errorMessage = "error at baseContext is nullptr";
176 NAPI_CALL(env, napi_throw_error(env, errorCode.c_str(), errorMessage.c_str()));
177 return nullptr;
178 }
179 if (parameterCount == 1) {
180 NAPI_CALL(env, napi_create_reference(env, parameters[0], DEFAULT_REF_COUNT, &asyncContext->callbackRef));
181 }
182 return NapiUtil::HandleAsyncWork(env, asyncContext.release(), "GetCellularDataState",
183 NativeGetCellularDataState, GetCellularDataStateCallback);
184 }
185
NativeIsCellularDataEnabled(napi_env env,void * data)186 static void NativeIsCellularDataEnabled(napi_env env, void *data)
187 {
188 auto asyncContext = static_cast<AsyncContext *>(data);
189 if (asyncContext == nullptr) {
190 NapiUtil::ThrowParameterError(env);
191 return;
192 }
193 if (IsCellularDataManagerInited()) {
194 bool enabled = false;
195 asyncContext->errorCode = CellularDataClient::GetInstance().IsCellularDataEnabled(enabled);
196 if (asyncContext->errorCode == TELEPHONY_SUCCESS) {
197 asyncContext->resolved = true;
198 }
199 asyncContext->result = enabled;
200 } else {
201 asyncContext->resolved = false;
202 asyncContext->errorCode = ERROR_SERVICE_UNAVAILABLE;
203 }
204 }
205
IsCellularDataEnabledCallback(napi_env env,napi_status status,void * data)206 static void IsCellularDataEnabledCallback(napi_env env, napi_status status, void *data)
207 {
208 auto context = static_cast<AsyncContext *>(data);
209 if (context == nullptr) {
210 return;
211 }
212 std::unique_ptr<AsyncContext> asyncContext(context);
213 napi_value callbackValue = nullptr;
214 if (asyncContext->resolved) {
215 napi_status status = napi_get_boolean(env,
216 asyncContext->result == static_cast<int32_t>(DataSwitchCode::CELLULAR_DATA_ENABLED), &callbackValue);
217 NAPI_CALL_RETURN_VOID(env, status);
218 } else {
219 JsError error = NapiUtil::ConverErrorMessageWithPermissionForJs(
220 asyncContext->errorCode, "IsCellularDataEnabled", GET_NETWORK_INFO);
221 callbackValue = NapiUtil::CreateErrorMessage(env, error.errorMessage, error.errorCode);
222 }
223 NapiUtil::Handle2ValueCallback(env, asyncContext.release(), callbackValue);
224 }
225
IsCellularDataEnabled(napi_env env,napi_callback_info info)226 static napi_value IsCellularDataEnabled(napi_env env, napi_callback_info info)
227 {
228 size_t parameterCount = 1;
229 napi_value parameters[1];
230 napi_value thisVar = nullptr;
231 void *data = nullptr;
232 NAPI_CALL(env, napi_get_cb_info(env, info, ¶meterCount, parameters, &thisVar, &data));
233 if (!MatchCellularDataParameters(env, parameters, parameterCount)) {
234 TELEPHONY_LOGE("IsCellularDataEnabled MatchCellularDataParameters failed.");
235 NapiUtil::ThrowParameterError(env);
236 return nullptr;
237 }
238 auto asyncContext = std::make_unique<AsyncContext>();
239 if (asyncContext == nullptr) {
240 TELEPHONY_LOGE("IsCellularDataEnabled asyncContext is nullptr.");
241 NapiUtil::ThrowParameterError(env);
242 return nullptr;
243 }
244 if (parameterCount == 1) {
245 NAPI_CALL(env, napi_create_reference(env, parameters[0], DEFAULT_REF_COUNT, &(asyncContext->callbackRef)));
246 }
247 return NapiUtil::HandleAsyncWork(env, asyncContext.release(), "IsCellularDataEnabled",
248 NativeIsCellularDataEnabled, IsCellularDataEnabledCallback);
249 }
250
IsCellularDataEnabledSync(napi_env env,napi_callback_info info)251 static napi_value IsCellularDataEnabledSync(napi_env env, napi_callback_info info)
252 {
253 bool isEnabled = false;
254 napi_value value = nullptr;
255 if (IsCellularDataManagerInited()) {
256 auto errorCode = CellularDataClient::GetInstance().IsCellularDataEnabled(isEnabled);
257 if (errorCode != TELEPHONY_SUCCESS) {
258 return value;
259 }
260 } else {
261 return value;
262 }
263 NAPI_CALL(env, napi_create_int32(env, isEnabled, &value));
264 return value;
265 }
266
NativeEnableCellularData(napi_env env,void * data)267 static void NativeEnableCellularData(napi_env env, void *data)
268 {
269 auto asyncContext = static_cast<AsyncContext *>(data);
270 if (asyncContext == nullptr) {
271 NapiUtil::ThrowParameterError(env);
272 return;
273 }
274 if (IsCellularDataManagerInited()) {
275 asyncContext->result = CellularDataClient::GetInstance().EnableCellularData(true);
276 if (asyncContext->result == TELEPHONY_ERR_SUCCESS) {
277 asyncContext->resolved = true;
278 } else {
279 asyncContext->resolved = false;
280 asyncContext->errorCode = asyncContext->result;
281 }
282 } else {
283 asyncContext->resolved = false;
284 asyncContext->errorCode = ERROR_SERVICE_UNAVAILABLE;
285 }
286 }
287
EnableCellularDataCallback(napi_env env,napi_status status,void * data)288 static void EnableCellularDataCallback(napi_env env, napi_status status, void *data)
289 {
290 auto context = static_cast<AsyncContext *>(data);
291 if (context == nullptr) {
292 NapiUtil::ThrowParameterError(env);
293 return;
294 }
295 std::unique_ptr<AsyncContext> asyncContext(context);
296 napi_value callbackValue = nullptr;
297 if (asyncContext->resolved) {
298 callbackValue = NapiUtil::CreateUndefined(env);
299 } else {
300 JsError error = NapiUtil::ConverErrorMessageWithPermissionForJs(
301 asyncContext->errorCode, "enableCellularData", SET_TELEPHONY_STATE);
302 callbackValue = NapiUtil::CreateErrorMessage(env, error.errorMessage, error.errorCode);
303 }
304 NapiUtil::Handle1ValueCallback(env, asyncContext.release(), callbackValue);
305 TELEPHONY_LOGI("EnableCellularDataCallback end");
306 }
307
DisableCellularDataCallback(napi_env env,napi_status status,void * data)308 static void DisableCellularDataCallback(napi_env env, napi_status status, void *data)
309 {
310 auto context = static_cast<AsyncContext *>(data);
311 if (context == nullptr) {
312 NapiUtil::ThrowParameterError(env);
313 return;
314 }
315 std::unique_ptr<AsyncContext> asyncContext(context);
316 napi_value callbackValue = nullptr;
317 if (asyncContext->resolved) {
318 callbackValue = NapiUtil::CreateUndefined(env);
319 } else {
320 JsError error = NapiUtil::ConverErrorMessageWithPermissionForJs(
321 asyncContext->errorCode, "disableCellularData", SET_TELEPHONY_STATE);
322 callbackValue = NapiUtil::CreateErrorMessage(env, error.errorMessage, error.errorCode);
323 }
324 NapiUtil::Handle1ValueCallback(env, asyncContext.release(), callbackValue);
325 TELEPHONY_LOGI("DisableCellularDataCallback end");
326 }
327
EnableCellularDataRoamingCallback(napi_env env,napi_status status,void * data)328 static void EnableCellularDataRoamingCallback(napi_env env, napi_status status, void *data)
329 {
330 auto context = static_cast<AsyncContext *>(data);
331 if (context == nullptr) {
332 NapiUtil::ThrowParameterError(env);
333 return;
334 }
335 std::unique_ptr<AsyncContext> asyncContext(context);
336 napi_value callbackValue = nullptr;
337 if (asyncContext->resolved) {
338 callbackValue = NapiUtil::CreateUndefined(env);
339 } else {
340 JsError error = NapiUtil::ConverErrorMessageWithPermissionForJs(
341 asyncContext->errorCode, "enableCellularDataRoaming", SET_TELEPHONY_STATE);
342 callbackValue = NapiUtil::CreateErrorMessage(env, error.errorMessage, error.errorCode);
343 }
344 NapiUtil::Handle1ValueCallback(env, asyncContext.release(), callbackValue);
345 TELEPHONY_LOGI("EnableCellularDataRoamingCallback end");
346 }
347
DisableCellularDataRoamingCallback(napi_env env,napi_status status,void * data)348 static void DisableCellularDataRoamingCallback(napi_env env, napi_status status, void *data)
349 {
350 auto context = static_cast<AsyncContext *>(data);
351 if (context == nullptr) {
352 NapiUtil::ThrowParameterError(env);
353 return;
354 }
355 std::unique_ptr<AsyncContext> asyncContext(context);
356 napi_value callbackValue = nullptr;
357 if (asyncContext->resolved) {
358 callbackValue = NapiUtil::CreateUndefined(env);
359 } else {
360 JsError error = NapiUtil::ConverErrorMessageWithPermissionForJs(
361 asyncContext->errorCode, "disableCellularDataRoaming", SET_TELEPHONY_STATE);
362 callbackValue = NapiUtil::CreateErrorMessage(env, error.errorMessage, error.errorCode);
363 }
364 NapiUtil::Handle1ValueCallback(env, asyncContext.release(), callbackValue);
365 TELEPHONY_LOGI("DisableCellularDataRoamingCallback end");
366 }
367
EnableCellularData(napi_env env,napi_callback_info info)368 static napi_value EnableCellularData(napi_env env, napi_callback_info info)
369 {
370 size_t parameterCount = 1;
371 napi_value parameters[1];
372 napi_value thisVar = nullptr;
373 void *data = nullptr;
374 NAPI_CALL(env, napi_get_cb_info(env, info, ¶meterCount, parameters, &thisVar, &data));
375 if (!MatchCellularDataParameters(env, parameters, parameterCount)) {
376 NapiUtil::ThrowParameterError(env);
377 return nullptr;
378 }
379 auto asyncContext = std::make_unique<AsyncContext>();
380 if (asyncContext == nullptr) {
381 NapiUtil::ThrowParameterError(env);
382 return nullptr;
383 }
384 if (parameterCount == 1) {
385 NAPI_CALL(env, napi_create_reference(env, parameters[0], DEFAULT_REF_COUNT, &(asyncContext->callbackRef)));
386 }
387 return NapiUtil::HandleAsyncWork(
388 env, asyncContext.release(), "EnableCellularData", NativeEnableCellularData, EnableCellularDataCallback);
389 }
390
NativeDisableCellularData(napi_env env,void * data)391 static void NativeDisableCellularData(napi_env env, void *data)
392 {
393 auto asyncContext = static_cast<AsyncContext *>(data);
394 if (asyncContext == nullptr) {
395 NapiUtil::ThrowParameterError(env);
396 return;
397 }
398 if (IsCellularDataManagerInited()) {
399 asyncContext->result = CellularDataClient::GetInstance().EnableCellularData(false);
400 if (asyncContext->result == TELEPHONY_ERR_SUCCESS) {
401 asyncContext->resolved = true;
402 } else {
403 asyncContext->resolved = false;
404 asyncContext->errorCode = asyncContext->result;
405 }
406 } else {
407 asyncContext->resolved = false;
408 asyncContext->errorCode = ERROR_SERVICE_UNAVAILABLE;
409 }
410 }
411
DisableCellularData(napi_env env,napi_callback_info info)412 static napi_value DisableCellularData(napi_env env, napi_callback_info info)
413 {
414 size_t parameterCount = 1;
415 napi_value parameters[1];
416 napi_value thisVar = nullptr;
417 void *data = nullptr;
418 NAPI_CALL(env, napi_get_cb_info(env, info, ¶meterCount, parameters, &thisVar, &data));
419 if (!MatchCellularDataParameters(env, parameters, parameterCount)) {
420 NapiUtil::ThrowParameterError(env);
421 return nullptr;
422 }
423 auto asyncContext = std::make_unique<AsyncContext>();
424 if (asyncContext == nullptr) {
425 NapiUtil::ThrowParameterError(env);
426 return nullptr;
427 }
428 if (parameterCount == 1) {
429 NAPI_CALL(env, napi_create_reference(env, parameters[0], DEFAULT_REF_COUNT, &(asyncContext->callbackRef)));
430 }
431 return NapiUtil::HandleAsyncWork(
432 env, asyncContext.release(), "DisableCellularData", NativeDisableCellularData, DisableCellularDataCallback);
433 }
434
NativeEnableCellularDataRoaming(napi_env env,void * data)435 static void NativeEnableCellularDataRoaming(napi_env env, void *data)
436 {
437 auto asyncContext = static_cast<AsyncContext *>(data);
438 if (asyncContext == nullptr) {
439 NapiUtil::ThrowParameterError(env);
440 return;
441 }
442 if (!IsValidSlotId(asyncContext->slotId)) {
443 TELEPHONY_LOGE("NativeEnableCellularDataRoaming slotId is invalid");
444 asyncContext->errorCode = ERROR_SLOT_ID_INVALID;
445 return;
446 }
447 if (IsCellularDataManagerInited()) {
448 asyncContext->result = CellularDataClient::GetInstance().EnableCellularDataRoaming(asyncContext->slotId, true);
449 if (asyncContext->result == TELEPHONY_ERR_SUCCESS) {
450 asyncContext->resolved = true;
451 } else {
452 asyncContext->resolved = false;
453 asyncContext->errorCode = asyncContext->result;
454 }
455 } else {
456 asyncContext->resolved = false;
457 asyncContext->errorCode = ERROR_SERVICE_UNAVAILABLE;
458 }
459 TELEPHONY_LOGI("NativeEnableCellularDataRoaming end");
460 }
461
EnableCellularDataRoaming(napi_env env,napi_callback_info info)462 static napi_value EnableCellularDataRoaming(napi_env env, napi_callback_info info)
463 {
464 const size_t paramLimitTwo = 2;
465 size_t parameterCount = paramLimitTwo;
466 napi_value parameters[paramLimitTwo] = { 0 };
467 napi_value thisVar = nullptr;
468 void *data = nullptr;
469 NAPI_CALL(env, napi_get_cb_info(env, info, ¶meterCount, parameters, &thisVar, &data));
470 if (!MatchEnableCellularDataRoamingParameters(env, parameters, parameterCount)) {
471 NapiUtil::ThrowParameterError(env);
472 return nullptr;
473 }
474 auto asyncContext = std::make_unique<AsyncContext>();
475 if (asyncContext == nullptr) {
476 NapiUtil::ThrowParameterError(env);
477 return nullptr;
478 }
479 NAPI_CALL(env, napi_get_value_int32(env, parameters[0], &asyncContext->slotId));
480 if (parameterCount == paramLimitTwo) {
481 NAPI_CALL(env, napi_create_reference(env, parameters[1], DEFAULT_REF_COUNT, &asyncContext->callbackRef));
482 }
483 return NapiUtil::HandleAsyncWork(env, asyncContext.release(), "EnableCellularDataRoaming",
484 NativeEnableCellularDataRoaming, EnableCellularDataRoamingCallback);
485 }
486
NativeDisableCellularDataRoaming(napi_env env,void * data)487 static void NativeDisableCellularDataRoaming(napi_env env, void *data)
488 {
489 auto asyncContext = static_cast<AsyncContext *>(data);
490 if (asyncContext == nullptr) {
491 NapiUtil::ThrowParameterError(env);
492 return;
493 }
494 if (!IsValidSlotId(asyncContext->slotId)) {
495 TELEPHONY_LOGE("NativeDisableCellularDataRoaming slotId is invalid");
496 asyncContext->errorCode = ERROR_SLOT_ID_INVALID;
497 return;
498 }
499 if (IsCellularDataManagerInited()) {
500 asyncContext->result = CellularDataClient::GetInstance().EnableCellularDataRoaming(asyncContext->slotId, false);
501 if (asyncContext->result == TELEPHONY_ERR_SUCCESS) {
502 asyncContext->resolved = true;
503 } else {
504 asyncContext->resolved = false;
505 asyncContext->errorCode = asyncContext->result;
506 }
507 } else {
508 asyncContext->resolved = false;
509 asyncContext->errorCode = ERROR_SERVICE_UNAVAILABLE;
510 }
511 }
512
DisableCellularDataRoaming(napi_env env,napi_callback_info info)513 static napi_value DisableCellularDataRoaming(napi_env env, napi_callback_info info)
514 {
515 const size_t paramLimitTwo = 2;
516 size_t parameterCount = paramLimitTwo;
517 napi_value parameters[paramLimitTwo] = { 0 };
518 napi_value thisVar = nullptr;
519 void *data = nullptr;
520 NAPI_CALL(env, napi_get_cb_info(env, info, ¶meterCount, parameters, &thisVar, &data));
521 if (!MatchEnableCellularDataRoamingParameters(env, parameters, parameterCount)) {
522 NapiUtil::ThrowParameterError(env);
523 return nullptr;
524 }
525 auto asyncContext = std::make_unique<AsyncContext>();
526 if (asyncContext == nullptr) {
527 NapiUtil::ThrowParameterError(env);
528 return nullptr;
529 }
530 NAPI_CALL(env, napi_get_value_int32(env, parameters[0], &asyncContext->slotId));
531 if (parameterCount == paramLimitTwo) {
532 NAPI_CALL(env, napi_create_reference(env, parameters[1], DEFAULT_REF_COUNT, &asyncContext->callbackRef));
533 }
534 return NapiUtil::HandleAsyncWork(env, asyncContext.release(), "DisableCellularDataRoaming",
535 NativeDisableCellularDataRoaming, DisableCellularDataRoamingCallback);
536 }
537
NativeIsCellularDataRoamingEnabled(napi_env env,void * data)538 static void NativeIsCellularDataRoamingEnabled(napi_env env, void *data)
539 {
540 auto asyncContext = static_cast<AsyncContext *>(data);
541 if (asyncContext == nullptr) {
542 NapiUtil::ThrowParameterError(env);
543 return;
544 }
545 if (!IsValidSlotId(asyncContext->slotId)) {
546 TELEPHONY_LOGE("NativeIsCellularDataRoamingEnabled slotId is invalid");
547 asyncContext->errorCode = ERROR_SLOT_ID_INVALID;
548 return;
549 }
550 if (IsCellularDataManagerInited()) {
551 auto &dataManager = CellularDataClient::GetInstance();
552 bool enabled = false;
553 asyncContext->errorCode = dataManager.IsCellularDataRoamingEnabled(asyncContext->slotId, enabled);
554 if (asyncContext->errorCode == TELEPHONY_SUCCESS) {
555 asyncContext->resolved = true;
556 }
557 asyncContext->result = enabled;
558 } else {
559 asyncContext->resolved = false;
560 asyncContext->errorCode = ERROR_SERVICE_UNAVAILABLE;
561 }
562 }
563
IsCellularDataRoamingEnabledCallback(napi_env env,napi_status status,void * data)564 static void IsCellularDataRoamingEnabledCallback(napi_env env, napi_status status, void *data)
565 {
566 auto context = static_cast<AsyncContext *>(data);
567 if (context == nullptr) {
568 return;
569 }
570 std::unique_ptr<AsyncContext> asyncContext(context);
571 napi_value callbackValue = nullptr;
572 if (asyncContext->resolved) {
573 napi_status status = napi_get_boolean(env,
574 asyncContext->result == static_cast<int32_t>(RoamingSwitchCode::CELLULAR_DATA_ROAMING_ENABLED),
575 &callbackValue);
576 NAPI_CALL_RETURN_VOID(env, status);
577 } else {
578 JsError error = NapiUtil::ConverErrorMessageWithPermissionForJs(
579 asyncContext->errorCode, "isCellularDataRoamingEnabled", GET_NETWORK_INFO);
580 callbackValue = NapiUtil::CreateErrorMessage(env, error.errorMessage, error.errorCode);
581 }
582 NapiUtil::Handle2ValueCallback(env, asyncContext.release(), callbackValue);
583 }
584
IsCellularDataRoamingEnabled(napi_env env,napi_callback_info info)585 static napi_value IsCellularDataRoamingEnabled(napi_env env, napi_callback_info info)
586 {
587 const size_t paramLimitTwo = 2;
588 size_t parameterCount = paramLimitTwo;
589 napi_value parameters[paramLimitTwo] = {0};
590 napi_value thisVar = nullptr;
591 void *data = nullptr;
592 napi_get_cb_info(env, info, ¶meterCount, parameters, &thisVar, &data);
593 if (!MatchEnableCellularDataRoamingParameters(env, parameters, parameterCount)) {
594 TELEPHONY_LOGE("IsCellularDataRoamingEnabled MatchEnableCellularDataRoamingParameters failed.");
595 NapiUtil::ThrowParameterError(env);
596 return nullptr;
597 }
598 auto asyncContext = std::make_unique<AsyncContext>();
599 if (asyncContext == nullptr) {
600 TELEPHONY_LOGE("IsCellularDataRoamingEnabled asyncContext is nullptr.");
601 NapiUtil::ThrowParameterError(env);
602 return nullptr;
603 }
604 napi_get_value_int32(env, parameters[0], &asyncContext->slotId);
605 if (parameterCount == paramLimitTwo) {
606 napi_create_reference(env, parameters[1], DEFAULT_REF_COUNT, &asyncContext->callbackRef);
607 }
608 return NapiUtil::HandleAsyncWork(env, asyncContext.release(), "IsCellularDataRoamingEnabled",
609 NativeIsCellularDataRoamingEnabled, IsCellularDataRoamingEnabledCallback);
610 }
611
IsCellularDataRoamingEnabledSync(napi_env env,napi_callback_info info)612 static napi_value IsCellularDataRoamingEnabledSync(napi_env env, napi_callback_info info)
613 {
614 size_t parameterCount = 0;
615 napi_value parameters[] = { nullptr };
616 napi_value thisVar = nullptr;
617 void *data = nullptr;
618 int32_t slotId;
619 bool dataRoamingEnabled = false;
620 napi_value value = nullptr;
621 napi_get_cb_info(env, info, ¶meterCount, parameters, &thisVar, &data);
622 napi_get_value_int32(env, parameters[0], &slotId);
623 if (!IsValidSlotId(slotId)) {
624 TELEPHONY_LOGE("IsCellularDataRoamingEnabledSync slotId is invalid");
625 return value;
626 }
627 if (IsCellularDataManagerInited()) {
628 auto &dataManager = CellularDataClient::GetInstance();
629 auto errorCode = dataManager.IsCellularDataRoamingEnabled(slotId, dataRoamingEnabled);
630 if (errorCode != TELEPHONY_SUCCESS) {
631 return value;
632 }
633 } else {
634 return value;
635 }
636 NAPI_CALL(env, napi_create_int32(env, dataRoamingEnabled, &value));
637 return value;
638 }
639
MatchGetDefaultCellularDataSlotIdParameters(napi_env env,const napi_value parameters[],const size_t parameterCount)640 static bool MatchGetDefaultCellularDataSlotIdParameters(
641 napi_env env, const napi_value parameters[], const size_t parameterCount)
642 {
643 switch (parameterCount) {
644 case 0:
645 return true;
646 case 1:
647 return NapiUtil::MatchParameters(env, parameters, {napi_function});
648 default:
649 return false;
650 }
651 }
652
NativeGetDefaultCellularDataSlotId(napi_env env,void * data)653 static void NativeGetDefaultCellularDataSlotId(napi_env env, void *data)
654 {
655 auto context = static_cast<AsyncContext *>(data);
656 if (context == nullptr) {
657 return;
658 }
659 int32_t result = CellularDataClient::GetInstance().GetDefaultCellularDataSlotId();
660 if (IsValidSlotIdEx(result) || result == DEFAULT_SIM_SLOT_ID_REMOVE) {
661 context->slotId = result;
662 context->resolved = true;
663 } else {
664 context->resolved = false;
665 context->errorCode = ERROR_NATIVE_API_EXECUTE_FAIL;
666 }
667 }
668
GetDefaultCellularDataSlotIdCallback(napi_env env,napi_status status,void * data)669 static void GetDefaultCellularDataSlotIdCallback(napi_env env, napi_status status, void *data)
670 {
671 auto asyncContext = static_cast<AsyncContext *>(data);
672 if (asyncContext == nullptr) {
673 return;
674 }
675 napi_value callbackValue = nullptr;
676 if (asyncContext->resolved) {
677 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, asyncContext->slotId, &callbackValue));
678 } else {
679 if (asyncContext->errorCode == ERROR_NATIVE_API_EXECUTE_FAIL) {
680 callbackValue = NapiUtil::CreateErrorMessage(
681 env, "GetDefaultCellularDataSlotId api execute failed", ERROR_NATIVE_API_EXECUTE_FAIL);
682 } else {
683 callbackValue = NapiUtil::CreateErrorMessage(env, "get default cellular data slotId failed");
684 }
685 }
686 NapiUtil::Handle2ValueCallback(env, asyncContext, callbackValue);
687 }
688
GetDefaultCellularDataSlotId(napi_env env,napi_callback_info info)689 static napi_value GetDefaultCellularDataSlotId(napi_env env, napi_callback_info info)
690 {
691 size_t parameterCount = 1;
692 napi_value parameters[] = {nullptr};
693 napi_value thisVar = nullptr;
694 void *data = nullptr;
695 napi_get_cb_info(env, info, ¶meterCount, parameters, &thisVar, &data);
696 NAPI_ASSERT(env, MatchGetDefaultCellularDataSlotIdParameters(env, parameters, parameterCount), "type mismatch");
697 auto asyncContext = std::make_unique<AsyncContext>();
698 if (asyncContext == nullptr) {
699 std::string errorCode = std::to_string(napi_generic_failure);
700 std::string errorMessage = "error at baseContext is nullptr";
701 NAPI_CALL(env, napi_throw_error(env, errorCode.c_str(), errorMessage.c_str()));
702 return nullptr;
703 }
704 if (parameterCount == 1) {
705 napi_create_reference(env, parameters[0], DEFAULT_REF_COUNT, &asyncContext->callbackRef);
706 }
707 return NapiUtil::HandleAsyncWork(env, asyncContext.release(), "GetDefaultCellularDataSlotId",
708 NativeGetDefaultCellularDataSlotId, GetDefaultCellularDataSlotIdCallback);
709 }
710
GetDefaultCellularDataSlotIdSync(napi_env env,napi_callback_info info)711 static napi_value GetDefaultCellularDataSlotIdSync(napi_env env, napi_callback_info info)
712 {
713 size_t parameterCount = 1;
714 napi_value parameters[] = { nullptr };
715 napi_value thisVar = nullptr;
716 void *data = nullptr;
717 int32_t slotId = -1;
718 napi_get_cb_info(env, info, ¶meterCount, parameters, &thisVar, &data);
719 if (parameterCount == 0) {
720 slotId = CellularDataClient::GetInstance().GetDefaultCellularDataSlotId();
721 }
722 napi_value value = nullptr;
723 NAPI_CALL(env, napi_create_int32(env, slotId, &value));
724 return value;
725 }
726
GetDefaultCellularDataSimId(napi_env env,napi_callback_info info)727 static napi_value GetDefaultCellularDataSimId(napi_env env, napi_callback_info info)
728 {
729 size_t parameterCount = 1;
730 napi_value parameters[] = { nullptr };
731 napi_value thisVar = nullptr;
732 void *data = nullptr;
733 int32_t simId = 0;
734 napi_get_cb_info(env, info, ¶meterCount, parameters, &thisVar, &data);
735 if (parameterCount == 0) {
736 CellularDataClient::GetInstance().GetDefaultCellularDataSimId(simId);
737 }
738 napi_value value = nullptr;
739 NAPI_CALL(env, napi_create_int32(env, simId, &value));
740 return value;
741 }
742
NativeSetDefaultCellularDataSlotId(napi_env env,void * data)743 static void NativeSetDefaultCellularDataSlotId(napi_env env, void *data)
744 {
745 auto asyncContext = static_cast<AsyncContext *>(data);
746 if (asyncContext == nullptr) {
747 NapiUtil::ThrowParameterError(env);
748 return;
749 }
750 if (!IsValidSlotId(asyncContext->slotId)) {
751 TELEPHONY_LOGE("NativeSetDefaultCellularDataSlotId slotId is invalid");
752 asyncContext->errorCode = ERROR_SLOT_ID_INVALID;
753 return;
754 }
755 if (IsCellularDataManagerInited()) {
756 asyncContext->errorCode = CellularDataClient::GetInstance().SetDefaultCellularDataSlotId(asyncContext->slotId);
757 if (asyncContext->errorCode == TELEPHONY_ERR_SUCCESS) {
758 asyncContext->resolved = true;
759 } else {
760 asyncContext->resolved = false;
761 }
762 } else {
763 asyncContext->resolved = false;
764 asyncContext->errorCode = ERROR_SERVICE_UNAVAILABLE;
765 }
766 }
767
SetDefaultCellularDataSlotIdCallback(napi_env env,napi_status status,void * data)768 static void SetDefaultCellularDataSlotIdCallback(napi_env env, napi_status status, void *data)
769 {
770 auto asyncContext = static_cast<AsyncContext *>(data);
771 if (asyncContext == nullptr) {
772 NapiUtil::ThrowParameterError(env);
773 return;
774 }
775 napi_value callbackValue = nullptr;
776 if (asyncContext->resolved) {
777 callbackValue = NapiUtil::CreateUndefined(env);
778 } else {
779 JsError error = NapiUtil::ConverErrorMessageWithPermissionForJs(
780 asyncContext->errorCode, "setDefaultCellularDataSlotId", SET_TELEPHONY_STATE);
781 callbackValue = NapiUtil::CreateErrorMessage(env, error.errorMessage, error.errorCode);
782 }
783 NapiUtil::Handle1ValueCallback(env, asyncContext, callbackValue);
784 }
785
SetDefaultCellularDataSlotId(napi_env env,napi_callback_info info)786 static napi_value SetDefaultCellularDataSlotId(napi_env env, napi_callback_info info)
787 {
788 const size_t paramLimitTwo = 2;
789 size_t parameterCount = paramLimitTwo;
790 napi_value parameters[] = { nullptr, nullptr };
791 napi_value thisVar = nullptr;
792 void *data = nullptr;
793 napi_get_cb_info(env, info, ¶meterCount, parameters, &thisVar, &data);
794 if (!MatchEnableCellularDataRoamingParameters(env, parameters, parameterCount)) {
795 NapiUtil::ThrowParameterError(env);
796 return nullptr;
797 }
798 auto asyncContext = std::make_unique<AsyncContext>();
799 if (asyncContext == nullptr) {
800 NapiUtil::ThrowParameterError(env);
801 return nullptr;
802 }
803 napi_get_value_int32(env, parameters[0], &asyncContext->slotId);
804 if (parameterCount == paramLimitTwo) {
805 napi_create_reference(env, parameters[1], DEFAULT_REF_COUNT, &asyncContext->callbackRef);
806 }
807 return NapiUtil::HandleAsyncWork(env, asyncContext.release(), "SetDefaultCellularDataSlotId",
808 NativeSetDefaultCellularDataSlotId, SetDefaultCellularDataSlotIdCallback);
809 }
810
NativeGetCellularDataFlowType(napi_env env,void * data)811 void NativeGetCellularDataFlowType(napi_env env, void *data)
812 {
813 auto asyncContext = static_cast<AsyncContext *>(data);
814 if (asyncContext == nullptr) {
815 return;
816 }
817 if (IsCellularDataManagerInited()) {
818 int32_t dataState = CellularDataClient::GetInstance().GetCellularDataFlowType();
819 TELEPHONY_LOGI("dataState = %{public}d", dataState);
820 asyncContext->resolved = true;
821 asyncContext->result = WrapGetCellularDataFlowTypeType(dataState);
822 } else {
823 asyncContext->resolved = false;
824 asyncContext->errorCode = ERROR_SERVICE_UNAVAILABLE;
825 }
826 }
827
GetCellularDataFlowTypeCallback(napi_env env,napi_status status,void * data)828 void GetCellularDataFlowTypeCallback(napi_env env, napi_status status, void *data)
829 {
830 auto context = static_cast<AsyncContext *>(data);
831 if (context == nullptr) {
832 return;
833 }
834 std::unique_ptr<AsyncContext> asyncContext(context);
835 napi_value callbackValue = nullptr;
836 if (asyncContext->resolved) {
837 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, asyncContext->result, &callbackValue));
838 } else {
839 if (asyncContext->errorCode == ERROR_SERVICE_UNAVAILABLE) {
840 callbackValue =
841 NapiUtil::CreateErrorMessage(env, "cellular data service unavailable", ERROR_SERVICE_UNAVAILABLE);
842 } else {
843 callbackValue = NapiUtil::CreateErrorMessage(env, "unkonwn error");
844 }
845 }
846 NapiUtil::Handle2ValueCallback(env, asyncContext.release(), callbackValue);
847 TELEPHONY_LOGI("GetCellularDataFlowTypeCallback end");
848 }
849
GetCellularDataFlowType(napi_env env,napi_callback_info info)850 static napi_value GetCellularDataFlowType(napi_env env, napi_callback_info info)
851 {
852 size_t parameterCount = 1;
853 napi_value parameters[] = {nullptr};
854 napi_value thisVar = nullptr;
855 void *data = nullptr;
856 napi_get_cb_info(env, info, ¶meterCount, parameters, &thisVar, &data);
857 NAPI_ASSERT(env, MatchGetDefaultCellularDataSlotIdParameters(env, parameters, parameterCount), "type mismatch");
858 auto asyncContext = std::make_unique<AsyncContext>();
859 if (asyncContext == nullptr) {
860 std::string errorCode = std::to_string(napi_generic_failure);
861 std::string errorMessage = "error at baseContext is nullptr";
862 NAPI_CALL(env, napi_throw_error(env, errorCode.c_str(), errorMessage.c_str()));
863 return nullptr;
864 }
865 if (parameterCount == 1) {
866 napi_create_reference(env, parameters[0], DEFAULT_REF_COUNT, &asyncContext->callbackRef);
867 }
868 return NapiUtil::HandleAsyncWork(env, asyncContext.release(), "GetCellularDataFlowType",
869 NativeGetCellularDataFlowType, GetCellularDataFlowTypeCallback);
870 }
871
CreateEnumConstructor(napi_env env,napi_callback_info info)872 static napi_value CreateEnumConstructor(napi_env env, napi_callback_info info)
873 {
874 napi_value thisArg = nullptr;
875 void *data = nullptr;
876 napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
877 napi_value global = nullptr;
878 napi_get_global(env, &global);
879 return thisArg;
880 }
881
InitEnumDataConnectState(napi_env env,napi_value exports)882 static napi_value InitEnumDataConnectState(napi_env env, napi_value exports)
883 {
884 napi_property_descriptor desc[] = {
885 DECLARE_NAPI_STATIC_PROPERTY("DATA_STATE_UNKNOWN",
886 NapiUtil::ToInt32Value(env, static_cast<int32_t>(DataConnectState::DATA_STATE_UNKNOWN))),
887 DECLARE_NAPI_STATIC_PROPERTY("DATA_STATE_DISCONNECTED",
888 NapiUtil::ToInt32Value(env, static_cast<int32_t>(DataConnectState::DATA_STATE_DISCONNECTED))),
889 DECLARE_NAPI_STATIC_PROPERTY("DATA_STATE_CONNECTING",
890 NapiUtil::ToInt32Value(env, static_cast<int32_t>(DataConnectState::DATA_STATE_CONNECTING))),
891 DECLARE_NAPI_STATIC_PROPERTY("DATA_STATE_CONNECTED",
892 NapiUtil::ToInt32Value(env, static_cast<int32_t>(DataConnectState::DATA_STATE_CONNECTED))),
893 DECLARE_NAPI_STATIC_PROPERTY("DATA_STATE_SUSPENDED",
894 NapiUtil::ToInt32Value(env, static_cast<int32_t>(DataConnectState::DATA_STATE_SUSPENDED))),
895 };
896 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
897 return exports;
898 }
899
CreateDataConnectState(napi_env env,napi_value exports)900 static napi_value CreateDataConnectState(napi_env env, napi_value exports)
901 {
902 napi_property_descriptor desc[] = {
903 DECLARE_NAPI_STATIC_PROPERTY("DATA_STATE_UNKNOWN",
904 NapiUtil::ToInt32Value(env, static_cast<int32_t>(DataConnectState::DATA_STATE_UNKNOWN))),
905 DECLARE_NAPI_STATIC_PROPERTY("DATA_STATE_DISCONNECTED",
906 NapiUtil::ToInt32Value(env, static_cast<int32_t>(DataConnectState::DATA_STATE_DISCONNECTED))),
907 DECLARE_NAPI_STATIC_PROPERTY("DATA_STATE_CONNECTING",
908 NapiUtil::ToInt32Value(env, static_cast<int32_t>(DataConnectState::DATA_STATE_CONNECTING))),
909 DECLARE_NAPI_STATIC_PROPERTY("DATA_STATE_CONNECTED",
910 NapiUtil::ToInt32Value(env, static_cast<int32_t>(DataConnectState::DATA_STATE_CONNECTED))),
911 DECLARE_NAPI_STATIC_PROPERTY("DATA_STATE_SUSPENDED",
912 NapiUtil::ToInt32Value(env, static_cast<int32_t>(DataConnectState::DATA_STATE_SUSPENDED))),
913
914 };
915 napi_value result = nullptr;
916 napi_define_class(env, "DataConnectState", NAPI_AUTO_LENGTH, CreateEnumConstructor, nullptr,
917 sizeof(desc) / sizeof(*desc), desc, &result);
918 napi_set_named_property(env, exports, "DataConnectState", result);
919 return exports;
920 }
921
InitEnumDataFlowType(napi_env env,napi_value exports)922 static napi_value InitEnumDataFlowType(napi_env env, napi_value exports)
923 {
924 napi_property_descriptor desc[] = {
925 DECLARE_NAPI_STATIC_PROPERTY("DATA_FLOW_TYPE_NONE",
926 NapiUtil::ToInt32Value(env, static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_NONE))),
927 DECLARE_NAPI_STATIC_PROPERTY("DATA_FLOW_TYPE_DOWN",
928 NapiUtil::ToInt32Value(env, static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_DOWN))),
929 DECLARE_NAPI_STATIC_PROPERTY("DATA_FLOW_TYPE_UP",
930 NapiUtil::ToInt32Value(env, static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_UP))),
931 DECLARE_NAPI_STATIC_PROPERTY("DATA_FLOW_TYPE_UP_DOWN",
932 NapiUtil::ToInt32Value(env, static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_UP_DOWN))),
933 DECLARE_NAPI_STATIC_PROPERTY("DATA_FLOW_TYPE_DORMANT",
934 NapiUtil::ToInt32Value(env, static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_DORMANT))),
935 };
936 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
937 return exports;
938 }
939
CreateDataFlowType(napi_env env,napi_value exports)940 static napi_value CreateDataFlowType(napi_env env, napi_value exports)
941 {
942 napi_property_descriptor desc[] = {
943 DECLARE_NAPI_STATIC_PROPERTY("DATA_FLOW_TYPE_NONE",
944 NapiUtil::ToInt32Value(env, static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_NONE))),
945 DECLARE_NAPI_STATIC_PROPERTY("DATA_FLOW_TYPE_DOWN",
946 NapiUtil::ToInt32Value(env, static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_DOWN))),
947 DECLARE_NAPI_STATIC_PROPERTY("DATA_FLOW_TYPE_UP",
948 NapiUtil::ToInt32Value(env, static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_UP))),
949 DECLARE_NAPI_STATIC_PROPERTY("DATA_FLOW_TYPE_UP_DOWN",
950 NapiUtil::ToInt32Value(env, static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_UP_DOWN))),
951 DECLARE_NAPI_STATIC_PROPERTY("DATA_FLOW_TYPE_DORMANT",
952 NapiUtil::ToInt32Value(env, static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_DORMANT))),
953 };
954 napi_value result = nullptr;
955 napi_define_class(env, "DataFlowType", NAPI_AUTO_LENGTH, CreateEnumConstructor, nullptr,
956 sizeof(desc) / sizeof(*desc), desc, &result);
957 napi_set_named_property(env, exports, "DataFlowType", result);
958 return exports;
959 }
960
EnableIntelligenceSwitch(napi_env env,napi_callback_info info)961 static napi_value EnableIntelligenceSwitch(napi_env env, napi_callback_info info)
962 {
963 size_t parameterCount = 1;
964 napi_value parameters[1];
965 napi_value thisVar = nullptr;
966 void *data = nullptr;
967 bool enable = false;
968 napi_get_cb_info(env, info, ¶meterCount, parameters, &thisVar, &data);
969 int32_t result = -1;
970 napi_get_value_bool(env, parameters[0], &enable);
971 if (parameterCount == 1) {
972 result = CellularDataClient::GetInstance().EnableIntelligenceSwitch(enable);
973 }
974 napi_value value = nullptr;
975 NAPI_CALL(env, napi_create_int32(env, result, &value));
976 return value;
977 }
978
GetIntelligenceSwitchState(napi_env env,napi_callback_info info)979 static napi_value GetIntelligenceSwitchState(napi_env env, napi_callback_info info)
980 {
981 size_t parameterCount = 1;
982 napi_value parameters[] = { nullptr };
983 napi_value thisVar = nullptr;
984 void *data = nullptr;
985 bool switchState = false;
986 napi_get_cb_info(env, info, ¶meterCount, parameters, &thisVar, &data);
987 if (parameterCount == 0) {
988 CellularDataClient::GetInstance().GetIntelligenceSwitchState(switchState);
989 }
990 napi_value value = nullptr;
991 NAPI_CALL(env, napi_get_boolean(env, switchState, &value));
992 return value;
993 }
994
995 EXTERN_C_START
RegistCellularData(napi_env env,napi_value exports)996 napi_value RegistCellularData(napi_env env, napi_value exports)
997 {
998 auto cellularDataPoxy = CellularDataClient::GetInstance().GetProxy();
999 napi_property_descriptor desc[] = {
1000 DECLARE_NAPI_FUNCTION("getCellularDataState", GetCellularDataState),
1001 DECLARE_NAPI_FUNCTION("isCellularDataEnabled", IsCellularDataEnabled),
1002 DECLARE_NAPI_FUNCTION("isCellularDataEnabledSync", IsCellularDataEnabledSync),
1003 DECLARE_NAPI_FUNCTION("enableCellularData", EnableCellularData),
1004 DECLARE_NAPI_FUNCTION("disableCellularData", DisableCellularData),
1005 DECLARE_NAPI_FUNCTION("enableCellularDataRoaming", EnableCellularDataRoaming),
1006 DECLARE_NAPI_FUNCTION("disableCellularDataRoaming", DisableCellularDataRoaming),
1007 DECLARE_NAPI_FUNCTION("isCellularDataRoamingEnabled", IsCellularDataRoamingEnabled),
1008 DECLARE_NAPI_FUNCTION("isCellularDataRoamingEnabledSync", IsCellularDataRoamingEnabledSync),
1009 DECLARE_NAPI_FUNCTION("getDefaultCellularDataSlotId", GetDefaultCellularDataSlotId),
1010 DECLARE_NAPI_FUNCTION("getDefaultCellularDataSimId", GetDefaultCellularDataSimId),
1011 DECLARE_NAPI_FUNCTION("getDefaultCellularDataSlotIdSync", GetDefaultCellularDataSlotIdSync),
1012 DECLARE_NAPI_FUNCTION("setDefaultCellularDataSlotId", SetDefaultCellularDataSlotId),
1013 DECLARE_NAPI_FUNCTION("getCellularDataFlowType", GetCellularDataFlowType),
1014 DECLARE_NAPI_FUNCTION("enableIntelligenceSwitch", EnableIntelligenceSwitch),
1015 DECLARE_NAPI_FUNCTION("getIntelligenceSwitchState", GetIntelligenceSwitchState),
1016 };
1017 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
1018 CreateDataConnectState(env, exports);
1019 InitEnumDataConnectState(env, exports);
1020 CreateDataFlowType(env, exports);
1021 InitEnumDataFlowType(env, exports);
1022 return exports;
1023 }
1024 EXTERN_C_END
1025
1026 static napi_module _cellularDataModule = {
1027 .nm_version = 1,
1028 .nm_flags = 0,
1029 .nm_filename = nullptr,
1030 .nm_register_func = RegistCellularData,
1031 .nm_modname = "telephony.data",
1032 .nm_priv = nullptr,
1033 .reserved = {nullptr},
1034 };
1035
RegisterCellularDataModule(void)1036 extern "C" __attribute__((constructor)) void RegisterCellularDataModule(void)
1037 {
1038 napi_module_register(&_cellularDataModule);
1039 }
1040 } // namespace Telephony
1041 } // namespace OHOS
1042