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