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 <ConnectivityKit/wifi/oh_wifi.h>
17 #include "hilog/log.h"
18 #include <cstdint>
19 
20 #undef LOG_DOMAIN
21 #undef LOG_TAG
22 #define LOG_DOMAIN 0x2300 // 全局domain宏,标识业务领域
23 #define LOG_TAG "MY_TAG"   // 全局tag宏,标识模块日志tag
24 
GetWifiSwitchState(napi_env env, napi_callback_info info)25 napi_value GetWifiSwitchState(napi_env env, napi_callback_info info)
26 {
27     bool state = false;
28     Wifi_ResultCode code = OH_Wifi_IsWifiEnabled(&state);
29     OH_LOG_INFO(LOG_APP, "testTag ret success: %{public}d, wifi state: %{public}d",
30         static_cast<int>(code),
31         static_cast<int>(state)
32     );
33 
34     int32_t stateValue = static_cast<int32_t>(state);
35     napi_value res;
36     napi_create_uint32(env, stateValue, &res);
37     return res;
38 }
39 
40 EXTERN_C_START
Init(napi_env env, napi_value exports)41 static napi_value Init(napi_env env, napi_value exports)
42 {
43     napi_property_descriptor desc[] = {
44         { "GetWifiSwitchState", nullptr, GetWifiSwitchState,
45           nullptr, nullptr, nullptr, napi_default, nullptr },
46     };
47     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
48     return exports;
49 }
50 EXTERN_C_END
51 
52 static napi_module demoModule = {
53     .nm_version = 1,
54     .nm_flags = 0,
55     .nm_filename = nullptr,
56     .nm_register_func = Init,
57     .nm_modname = "entry",
58     .nm_priv = ((void*)0),
59     .reserved = { 0 },
60 };
61 
RegisterEntryModule(void)62 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
63 {
64     napi_module_register(&demoModule);
65 }
66