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