1 /*
2 * Copyright (C) 2023 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_base_profile"
17 #endif
18
19 #include "napi_bluetooth_base_profile.h"
20
21 #include "bluetooth_log.h"
22 #include "napi_bluetooth_utils.h"
23 #include "hitrace_meter.h"
24
25 namespace OHOS {
26 namespace Bluetooth {
DefineBaseProfileJSFunction(napi_env env,napi_value exports)27 napi_value NapiBaseProfile::DefineBaseProfileJSFunction(napi_env env, napi_value exports)
28 {
29 HILOGD("start");
30 BaseProfilePropertyValueInit(env, exports);
31 napi_property_descriptor desc[] = {};
32 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
33 HILOGI("end");
34 return exports;
35 }
36
BaseProfilePropertyValueInit(napi_env env,napi_value exports)37 napi_value NapiBaseProfile::BaseProfilePropertyValueInit(napi_env env, napi_value exports)
38 {
39 HILOGD("start");
40 napi_value strategyObj = ConnectionStrategyInit(env);
41 napi_value disconnectCauseObj = DisconnectCauseInit(env);
42 napi_property_descriptor exportFuncs[] = {
43 DECLARE_NAPI_PROPERTY("ConnectionStrategy", strategyObj),
44 DECLARE_NAPI_PROPERTY("DisconnectCause", disconnectCauseObj),
45 };
46 HITRACE_METER_NAME(HITRACE_TAG_OHOS, "baseprofile:napi_define_properties");
47 napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs);
48 HILOGI("end");
49 return exports;
50 }
51
ConnectionStrategyInit(napi_env env)52 napi_value NapiBaseProfile::ConnectionStrategyInit(napi_env env)
53 {
54 HILOGI("enter");
55 napi_value strategyObj = nullptr;
56 napi_create_object(env, &strategyObj);
57 SetNamedPropertyByInteger(env, strategyObj, ConnectionStrategy::CONNECTION_UNKNOWN,
58 "CONNECTION_STRATEGY_UNSUPPORTED");
59 SetNamedPropertyByInteger(env, strategyObj, ConnectionStrategy::CONNECTION_ALLOWED,
60 "CONNECTION_STRATEGY_ALLOWED");
61 SetNamedPropertyByInteger(env, strategyObj, ConnectionStrategy::CONNECTION_FORBIDDEN,
62 "CONNECTION_STRATEGY_FORBIDDEN");
63 return strategyObj;
64 }
65
DisconnectCauseInit(napi_env env)66 napi_value NapiBaseProfile::DisconnectCauseInit(napi_env env)
67 {
68 HILOGI("enter");
69 napi_value disconnectCauseObj = nullptr;
70 napi_create_object(env, &disconnectCauseObj);
71 SetNamedPropertyByInteger(env, disconnectCauseObj,
72 static_cast<int>(ConnChangeCause::DISCONNECT_CAUSE_USER_DISCONNECT), "USER_DISCONNECT");
73 SetNamedPropertyByInteger(env, disconnectCauseObj,
74 static_cast<int>(ConnChangeCause::DISCONNECT_CAUSE_CONNECT_FROM_KEYBOARD), "CONNECT_FROM_KEYBOARD");
75 SetNamedPropertyByInteger(env, disconnectCauseObj,
76 static_cast<int>(ConnChangeCause::DISCONNECT_CAUSE_CONNECT_FROM_MOUSE), "CONNECT_FROM_MOUSE");
77 SetNamedPropertyByInteger(env, disconnectCauseObj,
78 static_cast<int>(ConnChangeCause::DISCONNECT_CAUSE_CONNECT_FROM_CAR), "CONNECT_FROM_CAR");
79 SetNamedPropertyByInteger(env, disconnectCauseObj,
80 static_cast<int>(ConnChangeCause::DISCONNECT_TOO_MANY_CONNECTED_DEVICES), "TOO_MANY_CONNECTED_DEVICES");
81 SetNamedPropertyByInteger(env, disconnectCauseObj,
82 static_cast<int>(ConnChangeCause::DISCONNECT_CAUSE_CONNECT_FAIL_INTERNAL), "CONNECT_FAIL_INTERNAL");
83 return disconnectCauseObj;
84 }
85 } // namespace Bluetooth
86 } // namespace OHOS