1 /*
2 * Copyright (C) 2021-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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_napi_native_module"
17 #endif
18
19 #include <map>
20 #include <string>
21 #include <thread>
22
23 #include "bluetooth_log.h"
24 #include "napi_bluetooth_ble.h"
25 #include "napi_bluetooth_gatt_client.h"
26 #include "napi_bluetooth_gatt_server.h"
27 #include "napi_bluetooth_host.h"
28 #include "napi_bluetooth_hfp_ag.h"
29 #include "napi_bluetooth_hfp_hf.h"
30 #include "napi_bluetooth_profile.h"
31 #include "napi_bluetooth_spp_server.h"
32 #include "napi_bluetooth_a2dp_snk.h"
33 #include "napi_bluetooth_a2dp_src.h"
34 #include "napi_bluetooth_avrcp_ct.h"
35 #include "napi_bluetooth_avrcp_tg.h"
36 #include "napi_bluetooth_hid_host.h"
37 #include "napi_bluetooth_pan.h"
38 #include "napi_bluetooth_opp.h"
39
40 #include "access/napi_bluetooth_access.h"
41 #include "connection/napi_bluetooth_connection.h"
42 #include "constant/napi_bluetooth_constant.h"
43 #include "hitrace_meter.h"
44 namespace OHOS {
45 namespace Bluetooth {
46 EXTERN_C_START
47 /*
48 * Module initialization function
49 */
Init(napi_env env,napi_value exports)50 static napi_value Init(napi_env env, napi_value exports)
51 {
52 #ifdef ENABLE_NAPI_BLUETOOTH_MANAGER
53 HITRACE_METER_NAME(HITRACE_TAG_OHOS, "bluetoothManager init");
54 #else
55 HITRACE_METER_NAME(HITRACE_TAG_OHOS, "bluetooth init");
56 #endif
57 HILOGD("-----Init start------");
58 napi_property_descriptor desc[] = {};
59
60 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
61
62 NapiGattServer::DefineGattServerJSClass(env);
63 NapiGattClient::DefineGattClientJSClass(env);
64 DefineBLEJSObject(env, exports);
65 DefineSppFunctions(env, exports);
66 NapiProfile::DefineProfileFunctions(env, exports);
67 NapiHandsFreeAudioGateway::DefineHandsFreeAudioGatewayJSClass(env, exports);
68 NapiHandsFreeUnit::DefineHandsFreeUnitJSClass(env);
69
70 NapiAccess::DefineAccessJSFunction(env, exports);
71 DefineConnectionFunctions(env, exports);
72 NapiConstant::DefineJSConstant(env, exports);
73
74 BluetoothHostInit(env, exports);
75 NapiA2dpSink::DefineA2dpSinkJSClass(env);
76 NapiA2dpSource::DefineA2dpSourceJSClass(env, exports);
77 NapiAvrcpController::DefineAvrcpControllerJSClass(env);
78 NapiAvrcpTarget::DefineAvrcpTargetJSClass(env);
79 NapiBluetoothHidHost::DefineHidHostJSClass(env, exports);
80 NapiBluetoothPan::DefinePanJSClass(env, exports);
81 DefineSystemBLEInterface(env, exports);
82
83 HILOGD("-----Init end------");
84 return exports;
85 }
86 EXTERN_C_END
87 /*
88 * Module define
89 */
90 static napi_module bluetoothModule = {.nm_version = 1,
91 .nm_flags = 0,
92 .nm_filename = NULL,
93 .nm_register_func = Init,
94 #ifdef ENABLE_NAPI_BLUETOOTH_MANAGER
95 .nm_modname = "bluetoothManager",
96 #else
97 .nm_modname = "bluetooth",
98 #endif
99 .nm_priv = ((void *)0),
100 .reserved = {0}};
101 /*
102 * Module register function
103 */
RegisterModule(void)104 extern "C" __attribute__((constructor)) void RegisterModule(void)
105 {
106 HILOGI("Register bluetoothModule nm_modname:%{public}s", bluetoothModule.nm_modname);
107 napi_module_register(&bluetoothModule);
108 }
109 } // namespace Bluetooth
110 } // namespace OHOS
111