• 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 #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     napi_property_descriptor desc[] = {
39         DECLARE_NAPI_FUNCTION("on", RegisterHostObserver),
40         DECLARE_NAPI_FUNCTION("off", DeregisterHostObserver),
41     };
42     HITRACE_METER_NAME(HITRACE_TAG_OHOS, "host:napi_define_properties");
43     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
44     return exports;
45 }
46 
RegisterHostObserver(napi_env env,napi_callback_info info)47 napi_value RegisterHostObserver(napi_env env, napi_callback_info info)
48 {
49     HILOGD("enter");
50     size_t argc = ARGS_SIZE_TWO;
51     napi_value argv[ARGS_SIZE_TWO] = {0};
52     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
53     std::string type;
54     auto status = NapiParseString(env, argv[PARAM0], type);
55     NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
56     if (type == REGISTER_STATE_CHANGE_TYPE) {
57         return NapiAccess::RegisterAccessObserver(env, info);
58     } else if (type == REGISTER_DEVICE_FIND_TYPE || type == REGISTER_PIN_REQUEST_TYPE ||
59                type == REGISTER_BOND_STATE_TYPE) {
60         return RegisterConnectionObserver(env, info);
61     } else if (type == REGISTER_SPP_READ_TYPE) {
62         return NapiSppServer::RegisterSocketObserver(env, info);
63     } else {
64         NAPI_BT_ASSERT_RETURN_UNDEF(env, false, BT_ERR_INVALID_PARAM);
65     }
66 }
67 
DeregisterHostObserver(napi_env env,napi_callback_info info)68 napi_value DeregisterHostObserver(napi_env env, napi_callback_info info)
69 {
70     HILOGD("enter");
71     size_t argc = ARGS_SIZE_TWO;
72     napi_value argv[ARGS_SIZE_TWO] = {0};
73     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
74     std::string type;
75     auto status = NapiParseString(env, argv[PARAM0], type);
76     NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
77 
78     if (type == REGISTER_STATE_CHANGE_TYPE) {
79         return NapiAccess::DeregisterAccessObserver(env, info);
80     } else if (type == REGISTER_DEVICE_FIND_TYPE || type == REGISTER_PIN_REQUEST_TYPE ||
81                type == REGISTER_BOND_STATE_TYPE) {
82         return DeRegisterConnectionObserver(env, info);
83     } else if (type == REGISTER_SPP_READ_TYPE) {
84         return NapiSppServer::DeRegisterSocketObserver(env, info);
85     } else {
86         NAPI_BT_ASSERT_RETURN_UNDEF(env, false, BT_ERR_INVALID_PARAM);
87     }
88 }
89 }  // namespace Bluetooth
90 }  // namespace OHOS
91