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 #include "bluetooth_ble_ffi.h"
17
18 #include "bluetooth_ble_common.h"
19 #include "bluetooth_ble_impl.h"
20 #include "bluetooth_errorcode.h"
21 #include "bluetooth_log.h"
22
23 using namespace OHOS::FFI;
24
25 namespace OHOS {
26 namespace CJSystemapi {
27 namespace CJBluetoothBle {
28 using Bluetooth::BT_ERR_INTERNAL_ERROR;
29 using Bluetooth::BT_NO_ERROR;
30
31 extern "C" {
FfiBluetoothBleCreateGattServer(int32_t * errCode)32 int64_t FfiBluetoothBleCreateGattServer(int32_t *errCode)
33 {
34 FfiGattServer *nativeGattServer = nullptr;
35 *errCode = BleImpl::CreateGattServer(nativeGattServer);
36 if (*errCode != BT_NO_ERROR) {
37 return -1;
38 }
39 return nativeGattServer->GetID();
40 }
41
FfiBluetoothBleCreateGattClientDevice(const char * deviceId,int32_t * errCode)42 int64_t FfiBluetoothBleCreateGattClientDevice(const char *deviceId, int32_t *errCode)
43 {
44 FfiClientDevice *nativeGattClientDevice = nullptr;
45 *errCode = BleImpl::CreateGattClientDevice(deviceId, nativeGattClientDevice);
46 if (*errCode != BT_NO_ERROR) {
47 return -1;
48 }
49 return nativeGattClientDevice->GetID();
50 }
51
FfiBluetoothBleGetConnectedBleDevices(int32_t * errCode)52 CArrString FfiBluetoothBleGetConnectedBleDevices(int32_t *errCode)
53 {
54 CArrString res{};
55 *errCode = BleImpl::GetConnectedBleDevices(res);
56 return res;
57 }
58
FfiBluetoothBleStartBleScan(CArrNativeScanFilter filters,NativeScanOptions * options,int32_t * errCode)59 void FfiBluetoothBleStartBleScan(CArrNativeScanFilter filters, NativeScanOptions *options, int32_t *errCode)
60 {
61 *errCode = BleImpl::StartBleScan(filters, options);
62 return;
63 }
64
FfiBluetoothBleStopBleScan(int32_t * errCode)65 void FfiBluetoothBleStopBleScan(int32_t *errCode)
66 {
67 *errCode = BleImpl::StopBleScan();
68 return;
69 }
70
FfiBluetoothBleStartAdvertising(NativeAdvertiseSetting setting,NativeAdvertiseData advData,NativeAdvertiseData * advResponse,int32_t * errCode)71 void FfiBluetoothBleStartAdvertising(NativeAdvertiseSetting setting, NativeAdvertiseData advData,
72 NativeAdvertiseData *advResponse, int32_t *errCode)
73 {
74 *errCode = BleImpl::StartAdvertising(setting, advData, advResponse);
75 return;
76 }
77
FfiBluetoothBleStopAdvertising(int32_t * errCode)78 void FfiBluetoothBleStopAdvertising(int32_t *errCode)
79 {
80 *errCode = BleImpl::StopAdvertising();
81 return;
82 }
83
FfiBluetoothBleStartAdvertisingWithId(NativeAdvertisingParams advertisingParams,int32_t * errCode)84 int32_t FfiBluetoothBleStartAdvertisingWithId(NativeAdvertisingParams advertisingParams, int32_t *errCode)
85 {
86 int32_t id = -1;
87 *errCode = BleImpl::StartAdvertising(advertisingParams, id);
88 return id;
89 }
90
FfiBluetoothBleEnableAdvertising(NativeAdvertisingEnableParams advertisingEnableParams,int32_t * errCode)91 void FfiBluetoothBleEnableAdvertising(NativeAdvertisingEnableParams advertisingEnableParams, int32_t *errCode)
92 {
93 *errCode = BleImpl::EnableAdvertising(advertisingEnableParams);
94 return;
95 }
96
FfiBluetoothBleDisableAdvertising(NativeAdvertisingDisableParams advertisingDisableParams,int32_t * errCode)97 void FfiBluetoothBleDisableAdvertising(NativeAdvertisingDisableParams advertisingDisableParams, int32_t *errCode)
98 {
99 *errCode = BleImpl::DisableAdvertising(advertisingDisableParams);
100 return;
101 }
102
FfiBluetoothBleStopAdvertisingWithId(uint32_t advertisingId,int32_t * errCode)103 void FfiBluetoothBleStopAdvertisingWithId(uint32_t advertisingId, int32_t *errCode)
104 {
105 *errCode = BleImpl::StopAdvertising(advertisingId);
106 return;
107 }
108
FfiBluetoothBleOn(int32_t callbackType,void (* callback)(),int32_t * errCode)109 void FfiBluetoothBleOn(int32_t callbackType, void (*callback)(), int32_t *errCode)
110 {
111 *errCode = BleImpl::RegisterBleObserver(callbackType, callback);
112 return;
113 }
114
FfiBluetoothBleGattClientDeviceConnect(int64_t id,int32_t * errCode)115 void FfiBluetoothBleGattClientDeviceConnect(int64_t id, int32_t *errCode)
116 {
117 auto clientDevice = FFIData::GetData<FfiClientDevice>(id);
118 if (clientDevice == nullptr) {
119 *errCode = BT_ERR_INTERNAL_ERROR;
120 return;
121 }
122 *errCode = clientDevice->Connect();
123 return;
124 }
125
FfiBluetoothBleGattClientDeviceDisconnect(int64_t id,int32_t * errCode)126 void FfiBluetoothBleGattClientDeviceDisconnect(int64_t id, int32_t *errCode)
127 {
128 auto clientDevice = FFIData::GetData<FfiClientDevice>(id);
129 if (clientDevice == nullptr) {
130 *errCode = BT_ERR_INTERNAL_ERROR;
131 return;
132 }
133 *errCode = clientDevice->Disconnect();
134 return;
135 }
136
FfiBluetoothBleGattClientDeviceClose(int64_t id,int32_t * errCode)137 void FfiBluetoothBleGattClientDeviceClose(int64_t id, int32_t *errCode)
138 {
139 auto clientDevice = FFIData::GetData<FfiClientDevice>(id);
140 if (clientDevice == nullptr) {
141 *errCode = BT_ERR_INTERNAL_ERROR;
142 return;
143 }
144 *errCode = clientDevice->Close();
145 return;
146 }
147
FfiBluetoothBleGattClientDeviceGetDeviceName(int64_t id,int32_t * errCode)148 char *FfiBluetoothBleGattClientDeviceGetDeviceName(int64_t id, int32_t *errCode)
149 {
150 auto clientDevice = FFIData::GetData<FfiClientDevice>(id);
151 if (clientDevice == nullptr) {
152 *errCode = BT_ERR_INTERNAL_ERROR;
153 return nullptr;
154 }
155 auto name = clientDevice->GetDeviceName(errCode);
156 return MallocCString(name);
157 }
158
FfiBluetoothBleGattClientDeviceGetServices(int64_t id,int32_t * errCode)159 CArrGattService FfiBluetoothBleGattClientDeviceGetServices(int64_t id, int32_t *errCode)
160 {
161 CArrGattService service{};
162 auto clientDevice = FFIData::GetData<FfiClientDevice>(id);
163 if (clientDevice == nullptr) {
164 *errCode = BT_ERR_INTERNAL_ERROR;
165 return service;
166 }
167 *errCode = clientDevice->GetServices(service);
168 return service;
169 }
170
FfiBluetoothBleGattClientDeviceReadCharacteristicValue(int64_t id,NativeBLECharacteristic characteristic,void (* callback)(),int32_t * errCode)171 void FfiBluetoothBleGattClientDeviceReadCharacteristicValue(int64_t id, NativeBLECharacteristic characteristic,
172 void (*callback)(), int32_t *errCode)
173 {
174 auto clientDevice = FFIData::GetData<FfiClientDevice>(id);
175 if (clientDevice == nullptr) {
176 *errCode = BT_ERR_INTERNAL_ERROR;
177 return;
178 }
179 *errCode = clientDevice->ReadCharacteristicValue(characteristic, callback);
180 return;
181 }
182
FfiBluetoothBleGattClientDeviceReadDescriptorValue(int64_t id,NativeBLEDescriptor descriptor,void (* callback)(),int32_t * errCode)183 void FfiBluetoothBleGattClientDeviceReadDescriptorValue(int64_t id, NativeBLEDescriptor descriptor, void (*callback)(),
184 int32_t *errCode)
185 {
186 auto clientDevice = FFIData::GetData<FfiClientDevice>(id);
187 if (clientDevice == nullptr) {
188 *errCode = BT_ERR_INTERNAL_ERROR;
189 return;
190 }
191 *errCode = clientDevice->ReadDescriptorValue(descriptor, callback);
192 return;
193 }
194
FfiBluetoothBleGattClientDeviceWriteCharacteristicValue(int64_t id,NativeBLECharacteristic characteristic,int32_t writeType,void (* callback)(),int32_t * errCode)195 void FfiBluetoothBleGattClientDeviceWriteCharacteristicValue(int64_t id, NativeBLECharacteristic characteristic,
196 int32_t writeType, void (*callback)(), int32_t *errCode)
197 {
198 auto clientDevice = FFIData::GetData<FfiClientDevice>(id);
199 if (clientDevice == nullptr) {
200 *errCode = BT_ERR_INTERNAL_ERROR;
201 return;
202 }
203 *errCode = clientDevice->WriteCharacteristicValue(characteristic, writeType, callback);
204 return;
205 }
206
FfiBluetoothBleGattClientDeviceWriteDescriptorValue(int64_t id,NativeBLEDescriptor descriptor,void (* callback)(),int32_t * errCode)207 void FfiBluetoothBleGattClientDeviceWriteDescriptorValue(int64_t id, NativeBLEDescriptor descriptor, void (*callback)(),
208 int32_t *errCode)
209 {
210 auto clientDevice = FFIData::GetData<FfiClientDevice>(id);
211 if (clientDevice == nullptr) {
212 *errCode = BT_ERR_INTERNAL_ERROR;
213 return;
214 }
215 *errCode = clientDevice->WriteDescriptorValue(descriptor, callback);
216 return;
217 }
218
FfiBluetoothBleGattClientDeviceGetRssiValue(int64_t id,void (* callback)(),int32_t * errCode)219 void FfiBluetoothBleGattClientDeviceGetRssiValue(int64_t id, void (*callback)(), int32_t *errCode)
220 {
221 auto clientDevice = FFIData::GetData<FfiClientDevice>(id);
222 if (clientDevice == nullptr) {
223 *errCode = BT_ERR_INTERNAL_ERROR;
224 return;
225 }
226 *errCode = clientDevice->GetRssiValue(callback);
227 return;
228 }
229
FfiBluetoothBleGattClientDeviceSetBLEMtuSize(int64_t id,int32_t mut,int32_t * errCode)230 void FfiBluetoothBleGattClientDeviceSetBLEMtuSize(int64_t id, int32_t mut, int32_t *errCode)
231 {
232 auto clientDevice = FFIData::GetData<FfiClientDevice>(id);
233 if (clientDevice == nullptr) {
234 *errCode = BT_ERR_INTERNAL_ERROR;
235 return;
236 }
237 *errCode = clientDevice->SetBLEMtuSize(mut);
238 return;
239 }
240
FfiBluetoothBleGattClientDeviceSetCharacteristicChangeNotification(int64_t id,NativeBLECharacteristic characteristic,bool enable,int32_t * errCode)241 void FfiBluetoothBleGattClientDeviceSetCharacteristicChangeNotification(int64_t id,
242 NativeBLECharacteristic characteristic,
243 bool enable, int32_t *errCode)
244 {
245 auto clientDevice = FFIData::GetData<FfiClientDevice>(id);
246 if (clientDevice == nullptr) {
247 *errCode = BT_ERR_INTERNAL_ERROR;
248 return;
249 }
250 *errCode = clientDevice->SetCharacteristicChangeNotification(characteristic, enable);
251 return;
252 }
253
FfiBluetoothBleGattClientDeviceSetCharacteristicChangeIndication(int64_t id,NativeBLECharacteristic characteristic,bool enable,int32_t * errCode)254 void FfiBluetoothBleGattClientDeviceSetCharacteristicChangeIndication(int64_t id,
255 NativeBLECharacteristic characteristic,
256 bool enable, int32_t *errCode)
257 {
258 auto clientDevice = FFIData::GetData<FfiClientDevice>(id);
259 if (clientDevice == nullptr) {
260 *errCode = BT_ERR_INTERNAL_ERROR;
261 return;
262 }
263 *errCode = clientDevice->SetCharacteristicChangeIndication(characteristic, enable);
264 return;
265 }
266
FfiBluetoothBleGattClientDeviceOn(int64_t id,int32_t callbackType,void (* callback)(),int32_t * errCode)267 void FfiBluetoothBleGattClientDeviceOn(int64_t id, int32_t callbackType, void (*callback)(), int32_t *errCode)
268 {
269 auto clientDevice = FFIData::GetData<FfiClientDevice>(id);
270 if (clientDevice == nullptr) {
271 *errCode = BT_ERR_INTERNAL_ERROR;
272 return;
273 }
274 *errCode = clientDevice->RegisterBleGattClientDeviceObserver(callbackType, callback);
275 return;
276 }
277
FfiBluetoothBleGattServerAddService(int64_t id,NativeGattService service,int32_t * errCode)278 void FfiBluetoothBleGattServerAddService(int64_t id, NativeGattService service, int32_t *errCode)
279 {
280 auto gattServer = FFIData::GetData<FfiGattServer>(id);
281 if (gattServer == nullptr) {
282 *errCode = BT_ERR_INTERNAL_ERROR;
283 return;
284 }
285 *errCode = gattServer->AddService(service);
286 return;
287 }
288
FfiBluetoothBleGattServerRemoveService(int64_t id,char * serviceUuid,int32_t * errCode)289 void FfiBluetoothBleGattServerRemoveService(int64_t id, char *serviceUuid, int32_t *errCode)
290 {
291 auto gattServer = FFIData::GetData<FfiGattServer>(id);
292 if (gattServer == nullptr) {
293 *errCode = BT_ERR_INTERNAL_ERROR;
294 return;
295 }
296 *errCode = gattServer->RemoveService(serviceUuid);
297 return;
298 }
299
FfiBluetoothBleGattServerClose(int64_t id,int32_t * errCode)300 void FfiBluetoothBleGattServerClose(int64_t id, int32_t *errCode)
301 {
302 auto gattServer = FFIData::GetData<FfiGattServer>(id);
303 if (gattServer == nullptr) {
304 *errCode = BT_ERR_INTERNAL_ERROR;
305 return;
306 }
307 *errCode = gattServer->Close();
308 return;
309 }
310
FfiBluetoothBleGattServerNotifyCharacteristicChanged(int64_t id,char * deviceId,NativeNotifyCharacteristic characteristic,int32_t * errCode)311 void FfiBluetoothBleGattServerNotifyCharacteristicChanged(int64_t id, char *deviceId,
312 NativeNotifyCharacteristic characteristic, int32_t *errCode)
313 {
314 auto gattServer = FFIData::GetData<FfiGattServer>(id);
315 if (gattServer == nullptr) {
316 *errCode = BT_ERR_INTERNAL_ERROR;
317 return;
318 }
319 *errCode = gattServer->NotifyCharacteristicChanged(deviceId, characteristic);
320 return;
321 }
322
FfiBluetoothBleGattServerSendResponse(int64_t id,NativeServerResponse serverResponse,int32_t * errCode)323 void FfiBluetoothBleGattServerSendResponse(int64_t id, NativeServerResponse serverResponse, int32_t *errCode)
324 {
325 auto gattServer = FFIData::GetData<FfiGattServer>(id);
326 if (gattServer == nullptr) {
327 *errCode = BT_ERR_INTERNAL_ERROR;
328 return;
329 }
330 *errCode = gattServer->SendResponse(serverResponse);
331 return;
332 }
333
FfiBluetoothBleGattServerOn(int64_t id,int32_t callbackType,void (* callback)(),int32_t * errCode)334 void FfiBluetoothBleGattServerOn(int64_t id, int32_t callbackType, void (*callback)(), int32_t *errCode)
335 {
336 auto gattServer = FFIData::GetData<FfiGattServer>(id);
337 if (gattServer == nullptr) {
338 *errCode = BT_ERR_INTERNAL_ERROR;
339 return;
340 }
341 *errCode = gattServer->RegisterBleGattServerObserver(callbackType, callback);
342 return;
343 }
344 }
345 } // namespace CJBluetoothBle
346 } // namespace CJSystemapi
347 } // namespace OHOS