• 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     HILOGI("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     HILOGI("end");
41     return exports;
42 }
43 
RegisterHostObserver(napi_env env,napi_callback_info info)44 napi_value RegisterHostObserver(napi_env env, napi_callback_info info)
45 {
46     HILOGI("enter");
47     size_t argc = ARGS_SIZE_TWO;
48     napi_value argv[ARGS_SIZE_TWO] = {0};
49     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
50     std::string type;
51     auto status = NapiParseString(env, argv[PARAM0], type);
52     NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
53     if (type == REGISTER_STATE_CHANGE_TYPE) {
54         return NapiAccess::RegisterAccessObserver(env, info);
55     } else if (type == REGISTER_DEVICE_FIND_TYPE || type == REGISTER_PIN_REQUEST_TYPE ||
56                type == REGISTER_BOND_STATE_TYPE) {
57         return RegisterConnectionObserver(env, info);
58     } else if (type == REGISTER_SPP_READ_TYPE) {
59         return NapiSppServer::RegisterSocketObserver(env, info);
60     } else {
61         NAPI_BT_ASSERT_RETURN_UNDEF(env, false, BT_ERR_INVALID_PARAM);
62     }
63 }
64 
DeregisterHostObserver(napi_env env,napi_callback_info info)65 napi_value DeregisterHostObserver(napi_env env, napi_callback_info info)
66 {
67     HILOGI("enter");
68     size_t argc = ARGS_SIZE_TWO;
69     napi_value argv[ARGS_SIZE_TWO] = {0};
70     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
71     std::string type;
72     auto status = NapiParseString(env, argv[PARAM0], type);
73     NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
74 
75     if (type == REGISTER_STATE_CHANGE_TYPE) {
76         return NapiAccess::DeregisterAccessObserver(env, info);
77     } else if (type == REGISTER_DEVICE_FIND_TYPE || type == REGISTER_PIN_REQUEST_TYPE ||
78                type == REGISTER_BOND_STATE_TYPE) {
79         return DeRegisterConnectionObserver(env, info);
80     } else if (type == REGISTER_SPP_READ_TYPE) {
81         return NapiSppServer::DeRegisterSocketObserver(env, info);
82     } else {
83         NAPI_BT_ASSERT_RETURN_UNDEF(env, false, BT_ERR_INVALID_PARAM);
84     }
85 }
86 
PropertyValueInit(napi_env env,napi_value exports)87 napi_value PropertyValueInit(napi_env env, napi_value exports)
88 {
89     HILOGI("start");
90     napi_value scanDutyObject = ScanDutyInit(env);
91     napi_value matchModeObject = MatchModeInit(env);
92 
93     napi_property_descriptor exportFuncs[] = {
94         DECLARE_NAPI_PROPERTY("ScanDuty", scanDutyObject),
95         DECLARE_NAPI_PROPERTY("MatchMode", matchModeObject),
96 
97     };
98     napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs);
99     HILOGI("end");
100     return exports;
101 }
102 
ScanDutyInit(napi_env env)103 napi_value ScanDutyInit(napi_env env)
104 {
105     HILOGI("enter");
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     HILOGI("enter");
118     napi_value matchMode = nullptr;
119     napi_create_object(env, &matchMode);
120     SetNamedPropertyByInteger(
121         env, matchMode, static_cast<int>(MatchMode::MATCH_MODE_AGGRESSIVE), "MATCH_MODE_AGGRESSIVE");
122     SetNamedPropertyByInteger(env, matchMode, static_cast<int>(MatchMode::MATCH_MODE_STICKY), "MATCH_MODE_STICKY");
123     return matchMode;
124 }
125 }  // namespace Bluetooth
126 }  // namespace OHOS
127