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, int cause = 0) deviceAddr_(deviceAddr)143 : deviceAddr_(deviceAddr), connectState_(connectState), stateChangeCause_(cause) {} 144 virtual ~NapiNativeStateChangeParam() override = default; 145 146 napi_value ToNapiValue(napi_env env) const override; 147 private: 148 std::string deviceAddr_ = ""; 149 int connectState_ = -1; 150 int stateChangeCause_ = -1; 151 }; 152 153 class NapiNativeBleConnectionStateChangeParam : public NapiNativeStateChangeParam { 154 public: NapiNativeBleConnectionStateChangeParam(std::string deviceAddr,int connectState)155 NapiNativeBleConnectionStateChangeParam(std::string deviceAddr, int connectState) 156 : NapiNativeStateChangeParam(deviceAddr, connectState) {} 157 ~NapiNativeBleConnectionStateChangeParam() override = default; 158 }; 159 160 class NapiNativeGattsCharacterReadRequest : public NapiNativeObject { 161 public: NapiNativeGattsCharacterReadRequest(int transId,std::string deviceAddr,GattCharacteristic & character)162 NapiNativeGattsCharacterReadRequest(int transId, std::string deviceAddr, GattCharacteristic &character) 163 : transId_(transId), deviceAddr_(deviceAddr), character_(character) {} 164 ~NapiNativeGattsCharacterReadRequest() override = default; 165 166 napi_value ToNapiValue(napi_env env) const override; 167 private: 168 int transId_ = 0; 169 std::string deviceAddr_ = ""; 170 GattCharacteristic character_; 171 }; 172 173 class NapiNativeGattsCharacterWriteRequest : public NapiNativeObject { 174 public: NapiNativeGattsCharacterWriteRequest(int transId,std::string deviceAddr,GattCharacteristic & character)175 NapiNativeGattsCharacterWriteRequest(int transId, std::string deviceAddr, GattCharacteristic &character) 176 : transId_(transId), deviceAddr_(deviceAddr), character_(character) {} 177 ~NapiNativeGattsCharacterWriteRequest() override = default; 178 179 napi_value ToNapiValue(napi_env env) const override; 180 private: 181 int transId_ = 0; 182 std::string deviceAddr_ = ""; 183 GattCharacteristic character_; 184 }; 185 186 class NapiNativeGattsDescriptorWriteRequest : public NapiNativeObject { 187 public: NapiNativeGattsDescriptorWriteRequest(int transId,std::string deviceAddr,GattDescriptor & descriptor)188 NapiNativeGattsDescriptorWriteRequest(int transId, std::string deviceAddr, GattDescriptor &descriptor) 189 : transId_(transId), deviceAddr_(deviceAddr), descriptor_(descriptor) {} 190 ~NapiNativeGattsDescriptorWriteRequest() override = default; 191 192 napi_value ToNapiValue(napi_env env) const override; 193 private: 194 int transId_ = 0; 195 std::string deviceAddr_ = ""; 196 GattDescriptor descriptor_; 197 }; 198 199 class NapiNativeGattsDescriptorReadRequest : public NapiNativeObject { 200 public: NapiNativeGattsDescriptorReadRequest(int transId,std::string deviceAddr,GattDescriptor & descriptor)201 NapiNativeGattsDescriptorReadRequest(int transId, std::string deviceAddr, GattDescriptor &descriptor) 202 : transId_(transId), deviceAddr_(deviceAddr), descriptor_(descriptor) {} 203 ~NapiNativeGattsDescriptorReadRequest() override = default; 204 205 napi_value ToNapiValue(napi_env env) const override; 206 private: 207 int transId_ = 0; 208 std::string deviceAddr_ = ""; 209 GattDescriptor descriptor_; 210 }; 211 212 class NapiNativeOppTransferInformation : public NapiNativeObject { 213 public: NapiNativeOppTransferInformation(const BluetoothOppTransferInformation & information)214 explicit NapiNativeOppTransferInformation(const BluetoothOppTransferInformation &information) 215 : information_(information) {} 216 ~NapiNativeOppTransferInformation() override = default; 217 218 napi_value ToNapiValue(napi_env env) const override; 219 private: 220 BluetoothOppTransferInformation information_; 221 }; 222 223 class NapiNativeBatteryInfo : public NapiNativeObject { 224 public: NapiNativeBatteryInfo(DeviceBatteryInfo batteryInfo)225 explicit NapiNativeBatteryInfo(DeviceBatteryInfo batteryInfo) : batteryInfo_(batteryInfo) {} 226 ~NapiNativeBatteryInfo() override = default; 227 228 napi_value ToNapiValue(napi_env env) const override; 229 private: 230 DeviceBatteryInfo batteryInfo_; 231 }; 232 233 class NapiNativeInt64 : public NapiNativeObject { 234 public: NapiNativeInt64(int64_t value)235 explicit NapiNativeInt64(int64_t value) : value_(value) {} 236 ~NapiNativeInt64() override = default; 237 238 napi_value ToNapiValue(napi_env env) const override; 239 private: 240 int64_t value_; 241 }; 242 243 class NapiNativeArrayBuffer : public NapiNativeObject { 244 public: NapiNativeArrayBuffer(SppCallbackBuffer sppBuffer)245 explicit NapiNativeArrayBuffer(SppCallbackBuffer sppBuffer) 246 : sppBuffer_(sppBuffer) {} 247 ~NapiNativeArrayBuffer() override = default; 248 249 napi_value ToNapiValue(napi_env env) const override; 250 private: 251 SppCallbackBuffer sppBuffer_; 252 }; 253 } // namespace Bluetooth 254 } // namespace OHOS 255 #endif // NAPI_NATIVE_OBJECT_H