1 /*
2 * Copyright (C) 2024 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 #include "napi/native_api.h"
16 #include "telephony/cellular_data/telephony_data.h"
17 #include "telephony/core_service/telephony_radio.h"
18 #include "telephony/core_service/telephony_radio_type.h"
19 #include "hilog/log.h"
20 #include <cstdint>
21 #include <cstring>
22
23 #define TELEPHONY_LOG_TAG "LogTagTelephonyNDKXts"
24 #define TELEPHONY_LOG_DOMAIN 0x0000
25
26 #define CASE_INDEX_1 1
27 #define CASE_INDEX_2 2
28 #define CASE_INDEX_3 3
29 #define CASE_INDEX_4 4
30 #define CASE_INDEX_5 5
31 #define CASE_INDEX_6 6
32 #define CASE_INDEX_7 7
33 #define CASE_INDEX_8 8
34 #define CASE_INDEX_9 9
35
36 /* 用例一 */
OHTelephonyGetDefaultCellularDataSlotId(napi_env env,napi_callback_info info)37 static napi_value OHTelephonyGetDefaultCellularDataSlotId(napi_env env, napi_callback_info info)
38 {
39 napi_status status;
40 int32_t id = OH_Telephony_GetDefaultCellularDataSlotId();
41 napi_value result = nullptr;
42 status = napi_create_int32(env, id, &result);
43 if (status != napi_ok) {
44 OH_LOG_Print(LOG_APP, LOG_ERROR, TELEPHONY_LOG_DOMAIN, TELEPHONY_LOG_TAG,
45 "Failed to get UTF-8 string");
46 return nullptr;
47 }
48 return result;
49 }
50
TestCaseOHTelephonyGetNetworkStateForSlotParam(uint32_t index)51 static int32_t TestCaseOHTelephonyGetNetworkStateForSlotParam(uint32_t index)
52 {
53 Telephony_NetworkState telState;
54 Telephony_RadioResult ret = TEL_RADIO_SUCCESS;
55
56 if (index == CASE_INDEX_1) {
57 ret = OH_Telephony_GetNetworkStateForSlot(0, nullptr); //查看卡槽0
58 } else if (index == CASE_INDEX_2) {
59 ret = OH_Telephony_GetNetworkStateForSlot(-1, &telState); //错误参数卡槽-1
60 } else if (index == CASE_INDEX_3) {
61 ret = OH_Telephony_GetNetworkStateForSlot(-1, nullptr); //错误参数卡槽-1
62 } else if (index == CASE_INDEX_4) {
63 ret = OH_Telephony_GetNetworkStateForSlot(0, &telState); //查看卡槽0
64 } else if (index == CASE_INDEX_5) {
65 ret = OH_Telephony_GetNetworkStateForSlot(100, &telState); //错误参数卡槽100
66 }
67
68 return static_cast<int32_t>(ret);
69 }
70
71 /* 用例二 */
OHTelephonyGetNetworkStateForSlotParam(napi_env env,napi_callback_info info)72 static napi_value OHTelephonyGetNetworkStateForSlotParam(napi_env env, napi_callback_info info)
73 {
74 size_t argc = 1;
75 napi_value args[1] = {};
76 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
77 int32_t index;
78 napi_get_value_int32(env, args[0], &index);
79
80 int32_t ret = -1;
81 ret = TestCaseOHTelephonyGetNetworkStateForSlotParam(index);
82 napi_value result = nullptr;
83 napi_create_int32(env, ret, &result);
84 return result;
85 }
86
TestCaseOHTelephonyGetNetworkStateParam(uint32_t index)87 static int32_t TestCaseOHTelephonyGetNetworkStateParam(uint32_t index)
88 {
89 Telephony_NetworkState telState;
90 Telephony_RadioResult ret;
91
92 if (index == CASE_INDEX_1) {
93 ret = OH_Telephony_GetNetworkState(nullptr);
94 } else if (index == CASE_INDEX_2) {
95 ret = OH_Telephony_GetNetworkState(&telState);
96 }
97
98 return static_cast<int32_t>(ret);
99 }
100
101 /* 用例三 */
OHTelephonyGetNetworkStateParam(napi_env env,napi_callback_info info)102 static napi_value OHTelephonyGetNetworkStateParam(napi_env env, napi_callback_info info)
103 {
104 size_t argc = 1;
105 napi_value args[1] = {};
106 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
107 int32_t index;
108 napi_get_value_int32(env, args[0], &index);
109
110 int32_t ret = -1;
111 ret = TestCaseOHTelephonyGetNetworkStateParam(index);
112 napi_value result = nullptr;
113 napi_create_int32(env, ret, &result);
114 return result;
115 }
116
Conv2JsObject(napi_env env,Telephony_NetworkState * state)117 static napi_value Conv2JsObject(napi_env env, Telephony_NetworkState *state)
118 {
119 napi_value jsValue = nullptr;
120 napi_value propertyValue = nullptr;
121 napi_create_object(env, &jsValue);
122
123 napi_create_string_utf8(env, state->longOperatorName_, strlen(state->longOperatorName_), &propertyValue);
124 napi_set_named_property(env, jsValue, "longOperatorName", propertyValue);
125
126 napi_create_string_utf8(env, state->shortOperatorName_, strlen(state->shortOperatorName_), &propertyValue);
127 napi_set_named_property(env, jsValue, "shortOperatorName", propertyValue);
128
129 napi_create_string_utf8(env, state->plmnNumeric_, strlen(state->plmnNumeric_), &propertyValue);
130 napi_set_named_property(env, jsValue, "plmnNumeric", propertyValue);
131
132 napi_get_boolean(env, state->isRoaming_, &propertyValue);
133 napi_set_named_property(env, jsValue, "isRoaming", propertyValue);
134
135 napi_create_int32(env, state->regState_, &propertyValue);
136 napi_set_named_property(env, jsValue, "regState", propertyValue);
137
138 napi_create_int32(env, state->nsaState_, &propertyValue);
139 napi_set_named_property(env, jsValue, "nsaState", propertyValue);
140
141 napi_create_int32(env, state->cfgTech_, &propertyValue);
142 napi_set_named_property(env, jsValue, "cfgTech", propertyValue);
143
144 napi_get_boolean(env, state->isCaActive_, &propertyValue);
145 napi_set_named_property(env, jsValue, "isCaActive", propertyValue);
146
147 napi_get_boolean(env, state->isEmergency_, &propertyValue);
148 napi_set_named_property(env, jsValue, "isEmergency", propertyValue);
149
150 return jsValue;
151 }
152
153 /* 用例四 */
OHTelephonyGetNetworkStateForSlot(napi_env env,napi_callback_info info)154 static napi_value OHTelephonyGetNetworkStateForSlot(napi_env env, napi_callback_info info)
155 {
156 size_t argc = 1;
157 napi_value args[1] = {};
158 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
159 int32_t soltId;
160 napi_get_value_int32(env, args[0], &soltId);
161
162 Telephony_NetworkState telState = {0};
163 Telephony_RadioResult res = TEL_RADIO_SUCCESS;
164 res = OH_Telephony_GetNetworkStateForSlot(soltId, &telState);
165 if (res != Telephony_RadioResult::TEL_RADIO_SUCCESS) {
166 OH_LOG_Print(LOG_APP, LOG_ERROR, TELEPHONY_LOG_DOMAIN, TELEPHONY_LOG_TAG,
167 "OH_Telephony_GetNetworkStateForSlot failed %{public}d", res);
168 return nullptr;
169 }
170
171 return Conv2JsObject(env, &telState);
172 }
173
174 /* 用例五 */
OHTelephonyGetNetworkState(napi_env env,napi_callback_info info)175 static napi_value OHTelephonyGetNetworkState(napi_env env, napi_callback_info info)
176 {
177 Telephony_NetworkState telState = {0};
178 Telephony_RadioResult res = TEL_RADIO_SUCCESS;
179 res = OH_Telephony_GetNetworkState(&telState);
180 if (res != Telephony_RadioResult::TEL_RADIO_SUCCESS) {
181 OH_LOG_Print(LOG_APP, LOG_ERROR, TELEPHONY_LOG_DOMAIN, TELEPHONY_LOG_TAG,
182 "OH_Telephony_GetNetworkState failed %{public}d", res);
183 return nullptr;
184 }
185
186 return Conv2JsObject(env, &telState);
187 }
188
189 EXTERN_C_START
Init(napi_env env,napi_value exports)190 static napi_value Init(napi_env env, napi_value exports)
191 {
192 napi_property_descriptor desc[] = {
193 {"OHTelephonyGetDefaultCellularDataSlotId", nullptr, OHTelephonyGetDefaultCellularDataSlotId, nullptr, nullptr,
194 nullptr, napi_default, nullptr},
195 {"OHTelephonyGetNetworkStateForSlotParam", nullptr, OHTelephonyGetNetworkStateForSlotParam, nullptr, nullptr,
196 nullptr, napi_default, nullptr},
197 {"OHTelephonyGetNetworkStateParam", nullptr, OHTelephonyGetNetworkStateParam, nullptr, nullptr,
198 nullptr, napi_default, nullptr},
199 {"OHTelephonyGetNetworkStateForSlot", nullptr, OHTelephonyGetNetworkStateForSlot, nullptr, nullptr,
200 nullptr, napi_default, nullptr},
201 {"OHTelephonyGetNetworkState", nullptr, OHTelephonyGetNetworkState, nullptr, nullptr,
202 nullptr, napi_default, nullptr},
203 };
204 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
205 return exports;
206 }
207 EXTERN_C_END
208
209 static napi_module demoModule = {
210 .nm_version =1,
211 .nm_flags = 0,
212 .nm_filename = nullptr,
213 // 模块对外接口注册函数
214 .nm_register_func = Init,
215 // 自定义模块
216 .nm_modname = "entry",
217 .nm_priv = ((void*)0),
218 .reserved = { 0 },
219 };
220
RegisterEntryModule(void)221 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
222 {
223 napi_module_register(&demoModule);
224 }
225