• 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 
16 #ifndef NAPI_NATIVE_OBJECT_H
17 #define NAPI_NATIVE_OBJECT_H
18 
19 #include "napi_bluetooth_utils.h"
20 #include "napi_bluetooth_spp_client.h"
21 
22 namespace OHOS {
23 namespace Bluetooth {
24 class NapiNativeObject {
25 public:
26     virtual ~NapiNativeObject() = default;
27     virtual napi_value ToNapiValue(napi_env env) const = 0;
28 };
29 
30 class NapiNativeEmpty : public NapiNativeObject {
31 public:
32     ~NapiNativeEmpty() override = default;
33 
ToNapiValue(napi_env env)34     napi_value ToNapiValue(napi_env env) const override
35     {
36         return NapiGetNull(env);
37     }
38 };
39 
40 class NapiNativeInt : public NapiNativeObject {
41 public:
NapiNativeInt(int value)42     explicit NapiNativeInt(int value) : value_(value) {}
43     ~NapiNativeInt() override = default;
44 
45     napi_value ToNapiValue(napi_env env) const override;
46 private:
47     int value_;
48 };
49 
50 class NapiNativeBool : public NapiNativeObject {
51 public:
NapiNativeBool(bool value)52     explicit NapiNativeBool(bool value) : value_(value) {}
53     ~NapiNativeBool() override = default;
54 
55     napi_value ToNapiValue(napi_env env) const override;
56 private:
57     bool value_;
58 };
59 
60 class NapiNativeString : public NapiNativeObject {
61 public:
NapiNativeString(std::string value)62     explicit NapiNativeString(std::string value) : value_(std::move(value)) {}
63     ~NapiNativeString() override = default;
64 
65     napi_value ToNapiValue(napi_env env) const override;
66 private:
67     std::string value_;
68 };
69 
70 class NapiNativeStringArray : public NapiNativeObject {
71 public:
NapiNativeStringArray(std::vector<std::string> value)72     explicit NapiNativeStringArray(std::vector<std::string> value) : value_(std::move(value)) {}
73     ~NapiNativeStringArray() override = default;
74 
75     napi_value ToNapiValue(napi_env env) const override;
76 private:
77     std::vector<std::string> value_;
78 };
79 
80 class NapiNativeUuidsArray : public NapiNativeObject {
81 public:
NapiNativeUuidsArray(const std::vector<std::string> uuids)82     explicit NapiNativeUuidsArray(const std::vector<std::string> uuids) : uuids_(uuids) {}
83     ~NapiNativeUuidsArray() override = default;
84 
85     napi_value ToNapiValue(napi_env env) const override;
86 private:
87     std::vector<std::string> uuids_;
88 };
89 
90 class NapiNativeDiscoveryResultArray : public NapiNativeObject {
91 public:
NapiNativeDiscoveryResultArray(const std::shared_ptr<BluetoothRemoteDevice> & device)92     explicit NapiNativeDiscoveryResultArray(const std::shared_ptr<BluetoothRemoteDevice> &device)
93         : remoteDevice_(device) {}
94     ~NapiNativeDiscoveryResultArray() override = default;
95 
96     napi_value ToNapiValue(napi_env env) const override;
97 private:
98     std::shared_ptr<BluetoothRemoteDevice> remoteDevice_ {nullptr};
99 };
100 
101 class NapiNativeDiscoveryInfoResultArray : public NapiNativeObject {
102 public:
NapiNativeDiscoveryInfoResultArray(const std::shared_ptr<BluetoothRemoteDevice> & device,int rssi,const std::string deviceName,int deviceClass)103     explicit NapiNativeDiscoveryInfoResultArray(
104         const std::shared_ptr<BluetoothRemoteDevice> &device, int rssi, const std::string deviceName, int deviceClass)
105         : remoteDevice_(device), rssi_(rssi), deviceName_(deviceName), deviceClass_(deviceClass) {}
106     ~NapiNativeDiscoveryInfoResultArray() override = default;
107 
108     napi_value ToNapiValue(napi_env env) const override;
109 private:
110     std::shared_ptr<BluetoothRemoteDevice> remoteDevice_ {nullptr};
111     int rssi_ = 0;
112     std::string deviceName_ = "";
113     int deviceClass_ = MajorClass::MAJOR_UNCATEGORIZED;
114 };
115 
116 class NapiNativePinRequiredParam : public NapiNativeObject {
117 public:
NapiNativePinRequiredParam(const std::shared_ptr<PairConfirmedCallBackInfo> & pairConfirmInfo)118     explicit NapiNativePinRequiredParam(const std::shared_ptr<PairConfirmedCallBackInfo> &pairConfirmInfo)
119         : pairConfirmInfo_(pairConfirmInfo) {}
120     ~NapiNativePinRequiredParam() override = default;
121 
122     napi_value ToNapiValue(napi_env env) const override;
123 private:
124     std::shared_ptr<PairConfirmedCallBackInfo> pairConfirmInfo_ {nullptr};
125 };
126 
127 class NapiNativeBondStateParam : public NapiNativeObject {
128 public:
NapiNativeBondStateParam(std::string deviceAddr,int bondStatus,int unbondCause)129     NapiNativeBondStateParam(std::string deviceAddr, int bondStatus, int unbondCause)
130         : deviceAddr_(deviceAddr), bondStatus_(bondStatus), unbondCause_(unbondCause) {}
131     ~NapiNativeBondStateParam() override = default;
132 
133     napi_value ToNapiValue(napi_env env) const override;
134 private:
135     std::string deviceAddr_ = "";
136     int bondStatus_ = -1;
137     int unbondCause_ = -1;
138 };
139 
140 class NapiNativeStateChangeParam : public NapiNativeObject {
141 public:
142     NapiNativeStateChangeParam(std::string deviceAddr, int connectState,
143         int cause = 0, bool isDisconnected = false, int disconnectReason = -1)
deviceAddr_(deviceAddr)144         : deviceAddr_(deviceAddr), connectState_(connectState), stateChangeCause_(cause),
145           isDisconnected_(isDisconnected), disconnectReason_(disconnectReason) {}
146     virtual ~NapiNativeStateChangeParam() override = default;
147 
148     napi_value ToNapiValue(napi_env env) const override;
149 private:
150     std::string deviceAddr_ = "";
151     int connectState_ = -1;
152     int stateChangeCause_ = -1;
153     bool isDisconnected_ = false;
154     int disconnectReason_ = -1;
155 };
156 
157 class NapiNativeBleConnectionStateChangeParam : public NapiNativeStateChangeParam {
158 public:
NapiNativeBleConnectionStateChangeParam(std::string deviceAddr,int connectState)159     NapiNativeBleConnectionStateChangeParam(std::string deviceAddr, int connectState)
160         : NapiNativeStateChangeParam(deviceAddr, connectState) {}
NapiNativeBleConnectionStateChangeParam(std::string deviceAddr,int connectState,int cause,bool isDisconnected,int disconnectReason)161     NapiNativeBleConnectionStateChangeParam(
162         std::string deviceAddr, int connectState, int cause, bool isDisconnected, int disconnectReason)
163         : NapiNativeStateChangeParam(deviceAddr, connectState, cause, isDisconnected, disconnectReason) {}
164     ~NapiNativeBleConnectionStateChangeParam() override = default;
165 };
166 
167 class NapiNativeGattsCharacterReadRequest : public NapiNativeObject {
168 public:
NapiNativeGattsCharacterReadRequest(int transId,std::string deviceAddr,GattCharacteristic & character)169     NapiNativeGattsCharacterReadRequest(int transId, std::string deviceAddr, GattCharacteristic &character)
170         : transId_(transId), deviceAddr_(deviceAddr), character_(character) {}
171     ~NapiNativeGattsCharacterReadRequest() override = default;
172 
173     napi_value ToNapiValue(napi_env env) const override;
174 private:
175     int transId_ = 0;
176     std::string deviceAddr_ = "";
177     GattCharacteristic character_;
178 };
179 
180 class NapiNativeGattsCharacterWriteRequest : public NapiNativeObject {
181 public:
NapiNativeGattsCharacterWriteRequest(int transId,std::string deviceAddr,GattCharacteristic & character)182     NapiNativeGattsCharacterWriteRequest(int transId, std::string deviceAddr, GattCharacteristic &character)
183         : transId_(transId), deviceAddr_(deviceAddr), character_(character) {}
184     ~NapiNativeGattsCharacterWriteRequest() override = default;
185 
186     napi_value ToNapiValue(napi_env env) const override;
187 private:
188     int transId_ = 0;
189     std::string deviceAddr_ = "";
190     GattCharacteristic character_;
191 };
192 
193 class NapiNativeGattsDescriptorWriteRequest : public NapiNativeObject {
194 public:
NapiNativeGattsDescriptorWriteRequest(int transId,std::string deviceAddr,GattDescriptor & descriptor)195     NapiNativeGattsDescriptorWriteRequest(int transId, std::string deviceAddr, GattDescriptor &descriptor)
196         : transId_(transId), deviceAddr_(deviceAddr), descriptor_(descriptor) {}
197     ~NapiNativeGattsDescriptorWriteRequest() override = default;
198 
199     napi_value ToNapiValue(napi_env env) const override;
200 private:
201     int transId_ = 0;
202     std::string deviceAddr_ = "";
203     GattDescriptor descriptor_;
204 };
205 
206 class NapiNativeGattsDescriptorReadRequest : public NapiNativeObject {
207 public:
NapiNativeGattsDescriptorReadRequest(int transId,std::string deviceAddr,GattDescriptor & descriptor)208     NapiNativeGattsDescriptorReadRequest(int transId, std::string deviceAddr, GattDescriptor &descriptor)
209         : transId_(transId), deviceAddr_(deviceAddr), descriptor_(descriptor) {}
210     ~NapiNativeGattsDescriptorReadRequest() override = default;
211 
212     napi_value ToNapiValue(napi_env env) const override;
213 private:
214     int transId_ = 0;
215     std::string deviceAddr_ = "";
216     GattDescriptor descriptor_;
217 };
218 
219 class NapiNativeOppTransferInformation : public NapiNativeObject {
220 public:
NapiNativeOppTransferInformation(const BluetoothOppTransferInformation & information)221     explicit NapiNativeOppTransferInformation(const BluetoothOppTransferInformation &information)
222         : information_(information) {}
223     ~NapiNativeOppTransferInformation() override = default;
224 
225     napi_value ToNapiValue(napi_env env) const override;
226 private:
227     BluetoothOppTransferInformation information_;
228 };
229 
230 class NapiNativeBatteryInfo : public NapiNativeObject {
231 public:
NapiNativeBatteryInfo(DeviceBatteryInfo batteryInfo)232     explicit NapiNativeBatteryInfo(DeviceBatteryInfo batteryInfo) : batteryInfo_(batteryInfo) {}
233     ~NapiNativeBatteryInfo() override = default;
234 
235     napi_value ToNapiValue(napi_env env) const override;
236 private:
237     DeviceBatteryInfo batteryInfo_;
238 };
239 
240 class NapiNativeInt64 : public NapiNativeObject {
241 public:
NapiNativeInt64(int64_t value)242     explicit NapiNativeInt64(int64_t value) : value_(value) {}
243     ~NapiNativeInt64() override = default;
244 
245     napi_value ToNapiValue(napi_env env) const override;
246 private:
247     int64_t value_;
248 };
249 
250 class NapiNativeArrayBuffer : public NapiNativeObject {
251 public:
NapiNativeArrayBuffer(SppCallbackBuffer sppBuffer)252     explicit NapiNativeArrayBuffer(SppCallbackBuffer sppBuffer)
253         : sppBuffer_(sppBuffer) {}
254     ~NapiNativeArrayBuffer() override = default;
255 
256     napi_value ToNapiValue(napi_env env) const override;
257 private:
258     SppCallbackBuffer sppBuffer_;
259 };
260 }  // namespace Bluetooth
261 }  // namespace OHOS
262 #endif  // NAPI_NATIVE_OBJECT_H