• 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 NAPI_BLUETOOTH_GATT_CLIENT_H_
16 #define NAPI_BLUETOOTH_GATT_CLIENT_H_
17 
18 #include "bluetooth_gatt_client.h"
19 #include "bluetooth_log.h"
20 #include "bluetooth_remote_device.h"
21 #include "napi_bluetooth_gatt_client_callback.h"
22 
23 namespace OHOS {
24 namespace Bluetooth {
25 const int SLEEP_TIME = 50000;
26 const int VALUE_1 = 1;
27 const int VALUE_2 = 2;
28 const int VALUE_3 = 3;
29 
30 class NapiGattClient {
31 public:
32 
33     static napi_value CreateGattClientDevice(napi_env env, napi_callback_info info);
34     static void DefineGattClientJSClass(napi_env env);
35     static napi_value GattClientConstructor(napi_env env, napi_callback_info info);
36 
37     static napi_value On(napi_env env, napi_callback_info info);
38     static napi_value Off(napi_env env, napi_callback_info info);
39 
40     static napi_value Connect(napi_env env, napi_callback_info info);
41     static napi_value Disconnect(napi_env env, napi_callback_info info);
42     static napi_value Close(napi_env env, napi_callback_info info);
43     static napi_value GetServices(napi_env env, napi_callback_info info);
44     static napi_value ReadCharacteristicValue(napi_env env, napi_callback_info info);
45     static napi_value ReadDescriptorValue(napi_env env, napi_callback_info info);
46 
47 #ifdef BLUETOOTH_API_SINCE_10
48     static napi_value WriteCharacteristicValueEx(napi_env env, napi_callback_info info);
49     static napi_value WriteDescriptorValueEx(napi_env env, napi_callback_info info);
50     static napi_value setCharacteristicChangeNotification(napi_env env, napi_callback_info info);
51     static napi_value setCharacteristicChangeIndication(napi_env env, napi_callback_info info);
52 #else
53     static napi_value WriteCharacteristicValue(napi_env env, napi_callback_info info);
54     static napi_value WriteDescriptorValue(napi_env env, napi_callback_info info);
55     static napi_value SetNotifyCharacteristicChanged(napi_env env, napi_callback_info info);
56 #endif
57 
58     static napi_value SetBLEMtuSize(napi_env env, napi_callback_info info);
59     static napi_value GetRssiValue(napi_env env, napi_callback_info info);
60     static napi_value GetDeviceName(napi_env env, napi_callback_info info);
61 
GetClient()62     std::shared_ptr<GattClient> &GetClient()
63     {
64         return client_;
65     }
66 
GetCallback()67     NapiGattClientCallback &GetCallback()
68     {
69         return callback_;
70     }
71 
GetDevice()72     std::shared_ptr<BluetoothRemoteDevice> GetDevice()
73     {
74         return device_;
75     }
76 
NapiGattClient(std::string & deviceId)77     explicit NapiGattClient(std::string &deviceId)
78     {
79         HILOGI("enter");
80         device_ = std::make_shared<BluetoothRemoteDevice>(deviceId, 1);
81         client_ = std::make_shared<GattClient>(*device_);
82         client_->Init();
83         callback_.SetClient(this);
84     }
85     ~NapiGattClient() = default;
86 
87     static thread_local napi_ref consRef_;
88 
89 private:
90     std::shared_ptr<GattClient> client_ = nullptr;
91     NapiGattClientCallback callback_;
92     std::shared_ptr<BluetoothRemoteDevice> device_ = nullptr;
93 };
94 } // namespace Bluetooth
95 } // namespace OHOS
96 #endif /* NAPI_BLUETOOTH_GATT_CLIENT_H_ */