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