• 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     static napi_value WriteCharacteristicValue(napi_env env, napi_callback_info info);
48     static napi_value WriteDescriptorValue(napi_env env, napi_callback_info info);
49     static napi_value SetBLEMtuSize(napi_env env, napi_callback_info info);
50     static napi_value SetNotifyCharacteristicChanged(napi_env env, napi_callback_info info);
51 
GetClient()52     std::shared_ptr<GattClient> &GetClient()
53     {
54         return client_;
55     }
56 
GetCallback()57     NapiGattClientCallback &GetCallback()
58     {
59         return callback_;
60     }
61 
GetDevice()62     std::shared_ptr<BluetoothRemoteDevice> GetDevice()
63     {
64         return device_;
65     }
66 
NapiGattClient(std::string & deviceId)67     explicit NapiGattClient(std::string &deviceId)
68     {
69         HILOGI("enter");
70         device_ = std::make_shared<BluetoothRemoteDevice>(deviceId, 1);
71         client_ = std::make_shared<GattClient>(*device_);
72         callback_.SetClient(this);
73     }
74     ~NapiGattClient() = default;
75 
76     static thread_local napi_ref consRef_;
77     ReadCharacteristicValueCallbackInfo *readCharacteristicValueCallbackInfo_;
78     ReadDescriptorValueCallbackInfo *readDescriptorValueCallbackInfo_;
79 
80 private:
81     std::shared_ptr<GattClient> client_ = nullptr;
82     NapiGattClientCallback callback_;
83     std::shared_ptr<BluetoothRemoteDevice> device_ = nullptr;
84 };
85 } // namespace Bluetooth
86 } // namespace OHOS
87 #endif /* NAPI_BLUETOOTH_GATT_CLIENT_H_ */