• 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     static int GattStatusFromService(int status);
62 
GetClient()63     std::shared_ptr<GattClient> &GetClient()
64     {
65         return client_;
66     }
67 
GetCallback()68     std::shared_ptr<NapiGattClientCallback> GetCallback()
69     {
70         return callback_;
71     }
72 
GetDevice()73     std::shared_ptr<BluetoothRemoteDevice> GetDevice()
74     {
75         return device_;
76     }
77 
NapiGattClient(std::string & deviceId)78     explicit NapiGattClient(std::string &deviceId)
79     {
80         HILOGI("enter");
81         device_ = std::make_shared<BluetoothRemoteDevice>(deviceId, 1);
82         client_ = std::make_shared<GattClient>(*device_);
83         client_->Init();
84         callback_ = std::make_shared<NapiGattClientCallback>();
85         callback_->deviceAddr_ = deviceId;
86     }
87     ~NapiGattClient() = default;
88 
89     static thread_local napi_ref consRef_;
90 
91     // napi (first) <-> service (second)
92     static const std::vector<std::pair<int, int>> g_gattStatusSrvToNapi;
93 private:
94     std::shared_ptr<GattClient> client_ = nullptr;
95     std::shared_ptr<NapiGattClientCallback> callback_;
96     std::shared_ptr<BluetoothRemoteDevice> device_ = nullptr;
97 };
98 } // namespace Bluetooth
99 } // namespace OHOS
100 #endif /* NAPI_BLUETOOTH_GATT_CLIENT_H_ */