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_host"
17 #endif
18
19 #include "napi_bluetooth_host.h"
20 #include "bluetooth_host.h"
21 #include "bluetooth_log.h"
22 #include "bluetooth_errorcode.h"
23 #include "napi_async_work.h"
24 #include "napi_bluetooth_error.h"
25 #include "napi_bluetooth_utils.h"
26 #include "parser/napi_parser_utils.h"
27 #include "access/napi_bluetooth_access.h"
28 #include "connection/napi_bluetooth_connection.h"
29 #include "napi_bluetooth_spp_server.h"
30 #include "hitrace_meter.h"
31
32 namespace OHOS {
33 namespace Bluetooth {
34
BluetoothHostInit(napi_env env,napi_value exports)35 napi_value BluetoothHostInit(napi_env env, napi_value exports)
36 {
37 HILOGD("start");
38 PropertyValueInit(env, exports);
39 napi_property_descriptor desc[] = {
40 DECLARE_NAPI_FUNCTION("on", RegisterHostObserver),
41 DECLARE_NAPI_FUNCTION("off", DeregisterHostObserver),
42 };
43 HITRACE_METER_NAME(HITRACE_TAG_OHOS, "host:napi_define_properties");
44 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
45 return exports;
46 }
47
RegisterHostObserver(napi_env env,napi_callback_info info)48 napi_value RegisterHostObserver(napi_env env, napi_callback_info info)
49 {
50 HILOGD("enter");
51 size_t argc = ARGS_SIZE_TWO;
52 napi_value argv[ARGS_SIZE_TWO] = {0};
53 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
54 std::string type;
55 auto status = NapiParseString(env, argv[PARAM0], type);
56 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
57 if (type == REGISTER_STATE_CHANGE_TYPE) {
58 return NapiAccess::RegisterAccessObserver(env, info);
59 } else if (type == REGISTER_DEVICE_FIND_TYPE || type == REGISTER_PIN_REQUEST_TYPE ||
60 type == REGISTER_BOND_STATE_TYPE) {
61 return RegisterConnectionObserver(env, info);
62 } else if (type == REGISTER_SPP_READ_TYPE) {
63 return NapiSppServer::RegisterSocketObserver(env, info);
64 } else {
65 NAPI_BT_ASSERT_RETURN_UNDEF(env, false, BT_ERR_INVALID_PARAM);
66 }
67 }
68
DeregisterHostObserver(napi_env env,napi_callback_info info)69 napi_value DeregisterHostObserver(napi_env env, napi_callback_info info)
70 {
71 HILOGD("enter");
72 size_t argc = ARGS_SIZE_TWO;
73 napi_value argv[ARGS_SIZE_TWO] = {0};
74 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
75 std::string type;
76 auto status = NapiParseString(env, argv[PARAM0], type);
77 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
78
79 if (type == REGISTER_STATE_CHANGE_TYPE) {
80 return NapiAccess::DeregisterAccessObserver(env, info);
81 } else if (type == REGISTER_DEVICE_FIND_TYPE || type == REGISTER_PIN_REQUEST_TYPE ||
82 type == REGISTER_BOND_STATE_TYPE) {
83 return DeRegisterConnectionObserver(env, info);
84 } else if (type == REGISTER_SPP_READ_TYPE) {
85 return NapiSppServer::DeRegisterSocketObserver(env, info);
86 } else {
87 NAPI_BT_ASSERT_RETURN_UNDEF(env, false, BT_ERR_INVALID_PARAM);
88 }
89 }
90
PropertyValueInit(napi_env env,napi_value exports)91 napi_value PropertyValueInit(napi_env env, napi_value exports)
92 {
93 HILOGD("start");
94 napi_value scanDutyObject = ScanDutyInit(env);
95 napi_value matchModeObject = MatchModeInit(env);
96
97 napi_property_descriptor exportFuncs[] = {
98 DECLARE_NAPI_PROPERTY("ScanDuty", scanDutyObject),
99 DECLARE_NAPI_PROPERTY("MatchMode", matchModeObject),
100
101 };
102 HITRACE_METER_NAME(HITRACE_TAG_OHOS, "host:napi_define_properties");
103 napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs);
104 return exports;
105 }
106
ScanDutyInit(napi_env env)107 napi_value ScanDutyInit(napi_env env)
108 {
109 napi_value scanDuty = nullptr;
110 napi_create_object(env, &scanDuty);
111 SetNamedPropertyByInteger(env, scanDuty, static_cast<int>(ScanDuty::SCAN_MODE_LOW_POWER), "SCAN_MODE_LOW_POWER");
112 SetNamedPropertyByInteger(env, scanDuty, static_cast<int>(ScanDuty::SCAN_MODE_BALANCED), "SCAN_MODE_BALANCED");
113 SetNamedPropertyByInteger(
114 env, scanDuty, static_cast<int>(ScanDuty::SCAN_MODE_LOW_LATENCY), "SCAN_MODE_LOW_LATENCY");
115 return scanDuty;
116 }
117
MatchModeInit(napi_env env)118 napi_value MatchModeInit(napi_env env)
119 {
120 napi_value matchMode = nullptr;
121 napi_create_object(env, &matchMode);
122 SetNamedPropertyByInteger(
123 env, matchMode, static_cast<int>(MatchMode::MATCH_MODE_AGGRESSIVE), "MATCH_MODE_AGGRESSIVE");
124 SetNamedPropertyByInteger(env, matchMode, static_cast<int>(MatchMode::MATCH_MODE_STICKY), "MATCH_MODE_STICKY");
125 return matchMode;
126 }
127 } // namespace Bluetooth
128 } // namespace OHOS
129