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