1 /* 2 * Copyright (c) 2024 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 BLUETOOTH_BLE_FFI_H 17 #define BLUETOOTH_BLE_FFI_H 18 19 #include "cj_common_ffi.h" 20 #include "native/ffi_remote_data.h" 21 22 #include <cstdint> 23 24 extern "C" { 25 typedef struct { 26 char *deviceId; 27 char *name; 28 char *serviceUuid; 29 char *serviceUuidMask; 30 char *serviceSolicitationUuid; 31 char *serviceSolicitationUuidMask; 32 CArrUI8 serviceData; 33 CArrUI8 serviceDataMask; 34 uint16_t manufactureId; 35 CArrUI8 manufactureData; 36 CArrUI8 manufactureDataMask; 37 } NativeScanFilter; 38 39 typedef struct { 40 NativeScanFilter *head; 41 int64_t size; 42 } CArrNativeScanFilter; 43 44 typedef struct { 45 int32_t interval; 46 int32_t dutyMode; 47 int32_t matchMode; 48 int32_t phyType; 49 } NativeScanOptions; 50 51 typedef struct { 52 uint16_t interval; 53 int8_t txPower; 54 bool connectable; 55 } NativeAdvertiseSetting; 56 57 typedef struct { 58 uint16_t manufactureId; 59 CArrUI8 manufactureValue; 60 } NativeManufactureData; 61 62 typedef struct { 63 NativeManufactureData *head; 64 int64_t size; 65 } CArrNativeManufactureData; 66 67 typedef struct { 68 char *serviceUuid; 69 CArrUI8 serviceValue; 70 } NativeServiceData; 71 72 typedef struct { 73 NativeServiceData *head; 74 int64_t size; 75 } CArrNativeServiceData; 76 77 typedef struct { 78 CArrString serviceUuids; 79 CArrNativeManufactureData manufactureData; 80 CArrNativeServiceData serviceData; 81 bool includeDeviceName; 82 } NativeAdvertiseData; 83 84 typedef struct { 85 NativeAdvertiseSetting advertisingSettings; 86 NativeAdvertiseData advertisingData; 87 NativeAdvertiseData advertisingResponse; 88 uint16_t duration; 89 } NativeAdvertisingParams; 90 91 typedef struct { 92 uint32_t advertisingId; 93 uint16_t duration; 94 } NativeAdvertisingEnableParams; 95 96 typedef struct { 97 uint32_t advertisingId; 98 } NativeAdvertisingDisableParams; 99 100 typedef struct { 101 int32_t advertisingId; 102 int32_t state; 103 } CAdvertisingStateChangeInfo; 104 105 typedef struct { 106 char *deviceId; 107 int32_t rssi; 108 CArrUI8 data; 109 char *deviceName; 110 bool connectable; 111 } NativeScanResult; 112 113 typedef struct { 114 NativeScanResult *head; 115 int64_t size; 116 } CArrScanResult; 117 118 typedef struct { 119 char *serviceUuid; 120 char *characteristicUuid; 121 char *descriptorUuid; 122 CArrUI8 descriptorValue; 123 } NativeBLEDescriptor; 124 125 typedef struct { 126 int32_t code; 127 NativeBLEDescriptor data; 128 } RetNativeBLEDescriptor; 129 130 typedef struct { 131 NativeBLEDescriptor *head; 132 int64_t size; 133 } CArrBLEDescriptor; 134 135 typedef struct { 136 bool write; 137 bool writeNoResponse; 138 bool read; 139 bool notify; 140 bool indicate; 141 } NativeGattProperties; 142 143 typedef struct { 144 char *serviceUuid; 145 char *characteristicUuid; 146 CArrUI8 characteristicValue; 147 CArrBLEDescriptor descriptors; 148 NativeGattProperties properties; 149 } NativeBLECharacteristic; 150 151 typedef struct { 152 int32_t code; 153 NativeBLECharacteristic data; 154 } RetNativeBLECharacteristic; 155 156 typedef struct { 157 NativeBLECharacteristic *head; 158 int64_t size; 159 } CArrBLECharacteristic; 160 161 struct NativeGattService; 162 163 struct CArrGattService { 164 NativeGattService *head; 165 int64_t size; 166 }; 167 168 struct NativeGattService { 169 char *serviceUuid; 170 bool isPrimary; 171 CArrBLECharacteristic characteristics; 172 CArrGattService includeServices; 173 }; 174 175 typedef struct { 176 char *serviceUuid; 177 char *characteristicUuid; 178 CArrUI8 characteristicValue; 179 bool confirm; 180 } NativeNotifyCharacteristic; 181 182 typedef struct { 183 char *deviceId; 184 int32_t transId; 185 int32_t status; 186 int32_t offset; 187 CArrUI8 value; 188 bool confirm; 189 } NativeServerResponse; 190 191 typedef struct { 192 char *deviceId; 193 int32_t transId; 194 int32_t offset; 195 char *characteristicUuid; 196 char *serviceUuid; 197 } NativeCharacteristicReadRequest; 198 199 typedef struct { 200 char *deviceId; 201 int32_t transId; 202 int32_t offset; 203 bool isPrepared; 204 bool needRsp; 205 CArrUI8 value; 206 char *characteristicUuid; 207 char *serviceUuid; 208 } NativeCharacteristicWriteRequest; 209 210 typedef struct { 211 char *deviceId; 212 int32_t transId; 213 int32_t offset; 214 char *descriptorUuid; 215 char *characteristicUuid; 216 char *serviceUuid; 217 } NativeDescriptorReadRequest; 218 219 typedef struct { 220 char *deviceId; 221 int32_t transId; 222 int32_t offset; 223 bool isPrepared; 224 bool needRsp; 225 CArrUI8 value; 226 char *descriptorUuid; 227 char *characteristicUuid; 228 char *serviceUuid; 229 } NativeDescriptorWriteRequest; 230 231 typedef struct { 232 char *deviceId; 233 int32_t state; 234 } NativeBLEConnectionChangeState; 235 236 const int32_t REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE = 0; 237 const int32_t REGISTER_BLE_FIND_DEVICE_TYPE = 1; 238 239 const int32_t CHARACTERISTIC_READ = 0; 240 const int32_t CHARACTERISTIC_WRITE = 1; 241 const int32_t DESCRIPTOR_READ = 2; 242 const int32_t DESCRIPTOR_WRITE = 3; 243 const int32_t CONNECTION_STATE_CHANGE = 4; 244 const int32_t SERVER_BLE_MTU_CHANGE = 5; 245 246 const int32_t BLE_CHARACTERISTIC_CHANGE = 0; 247 const int32_t BLE_CONNECTION_STATE_CHANGE = 1; 248 const int32_t CLIENT_BLE_MTU_CHANGE = 2; 249 250 struct NativeGattPermission { 251 bool readable = false; 252 bool writeable = false; 253 bool readEncrypted = false; 254 bool writeEncrypted = false; 255 }; 256 257 FFI_EXPORT int64_t FfiBluetoothBleCreateGattServer(int32_t *errCode); 258 FFI_EXPORT int64_t FfiBluetoothBleCreateGattClientDevice(const char *deviceId, int32_t *errCode); 259 FFI_EXPORT CArrString FfiBluetoothBleGetConnectedBleDevices(int32_t *errCode); 260 FFI_EXPORT void FfiBluetoothBleStartBleScan(CArrNativeScanFilter filters, NativeScanOptions *options, int32_t *errCode); 261 FFI_EXPORT void FfiBluetoothBleStopBleScan(int32_t *errCode); 262 FFI_EXPORT void FfiBluetoothBleStartAdvertising(NativeAdvertiseSetting setting, NativeAdvertiseData advData, 263 NativeAdvertiseData *advResponse, int32_t *errCode); 264 FFI_EXPORT void FfiBluetoothBleStopAdvertising(int32_t *errCode); 265 FFI_EXPORT int32_t FfiBluetoothBleStartAdvertisingWithId(NativeAdvertisingParams advertisingParams, int32_t *errCode); 266 FFI_EXPORT void FfiBluetoothBleEnableAdvertising(NativeAdvertisingEnableParams advertisingEnableParams, 267 int32_t *errCode); 268 FFI_EXPORT void FfiBluetoothBleDisableAdvertising(NativeAdvertisingDisableParams advertisingDisableParams, 269 int32_t *errCode); 270 FFI_EXPORT void FfiBluetoothBleStopAdvertisingWithId(uint32_t advertisingId, int32_t *errCode); 271 FFI_EXPORT void FfiBluetoothBleOn(int32_t callbackType, void (*callback)(), int32_t *errCode); 272 273 FFI_EXPORT void FfiBluetoothBleGattClientDeviceConnect(int64_t id, int32_t *errCode); 274 FFI_EXPORT void FfiBluetoothBleGattClientDeviceDisconnect(int64_t id, int32_t *errCode); 275 FFI_EXPORT void FfiBluetoothBleGattClientDeviceClose(int64_t id, int32_t *errCode); 276 FFI_EXPORT char *FfiBluetoothBleGattClientDeviceGetDeviceName(int64_t id, int32_t *errCode); 277 FFI_EXPORT CArrGattService FfiBluetoothBleGattClientDeviceGetServices(int64_t id, int32_t *errCode); 278 FFI_EXPORT void FfiBluetoothBleGattClientDeviceReadCharacteristicValue(int64_t id, 279 NativeBLECharacteristic characteristic, void (*callback)(), int32_t *errCode); 280 FFI_EXPORT void FfiBluetoothBleGattClientDeviceReadDescriptorValue(int64_t id, NativeBLEDescriptor descriptor, 281 void (*callback)(), int32_t *errCode); 282 FFI_EXPORT void FfiBluetoothBleGattClientDeviceWriteCharacteristicValue(int64_t id, 283 NativeBLECharacteristic characteristic, int32_t writeType, void (*callback)(), int32_t *errCode); 284 FFI_EXPORT void FfiBluetoothBleGattClientDeviceWriteDescriptorValue(int64_t id, NativeBLEDescriptor descriptor, 285 void (*callback)(), int32_t *errCode); 286 FFI_EXPORT void FfiBluetoothBleGattClientDeviceGetRssiValue(int64_t id, void (*callback)(), int32_t *errCode); 287 FFI_EXPORT void FfiBluetoothBleGattClientDeviceSetBLEMtuSize(int64_t id, int32_t mut, int32_t *errCode); 288 FFI_EXPORT void FfiBluetoothBleGattClientDeviceSetCharacteristicChangeNotification(int64_t id, 289 NativeBLECharacteristic characteristic, bool enable, int32_t *errCode); 290 FFI_EXPORT void FfiBluetoothBleGattClientDeviceSetCharacteristicChangeIndication(int64_t id, 291 NativeBLECharacteristic characteristic, bool enable, int32_t *errCode); 292 FFI_EXPORT void FfiBluetoothBleGattClientDeviceOn(int64_t id, int32_t callbackType, void (*callback)(), 293 int32_t *errCode); 294 295 FFI_EXPORT void FfiBluetoothBleGattServerAddService(int64_t id, NativeGattService service, int32_t *errCode); 296 FFI_EXPORT void FfiBluetoothBleGattServerRemoveService(int64_t id, char *serviceUuid, int32_t *errCode); 297 FFI_EXPORT void FfiBluetoothBleGattServerClose(int64_t id, int32_t *errCode); 298 FFI_EXPORT void FfiBluetoothBleGattServerNotifyCharacteristicChanged(int64_t id, char *deviceId, 299 NativeNotifyCharacteristic characteristic, int32_t *errCode); 300 FFI_EXPORT void FfiBluetoothBleGattServerSendResponse(int64_t id, NativeServerResponse serverResponse, 301 int32_t *errCode); 302 FFI_EXPORT void FfiBluetoothBleGattServerOn(int64_t id, int32_t callbackType, void (*callback)(), int32_t *errCode); 303 } 304 305 #endif // BLUETOOTH_BLE_FFI_H