1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #include <bluetooth.h>
22 #include <bluetooth_type.h>
23 #include <bluetooth_product.h>
25 #include "cableserver.h"
28 #include "caadapterutils.h"
31 #include "caqueueingthread.h"
32 #include "caadapterutils.h"
33 #include "cafragmentation.h"
34 #include "cagattservice.h"
35 #include "cableutil.h"
36 #include "oic_string.h"
37 #include "oic_malloc.h"
40 * @def TZ_BLE_SERVER_TAG
41 * @brief Logging tag for module name
43 #define TZ_BLE_SERVER_TAG "TZ_BLE_GATT_SERVER"
46 * @def CA_BLE_INITIAL_BUF_SIZE
47 * @brief Initial buffer size for Gatt Server.
49 #define CA_BLE_INITIAL_BUF_SIZE 512
53 * @brief attribute handler for OIC server attribute.
55 static char *g_gattSvcPath = NULL;
58 * @var g_gattReadCharPath
59 * @brief attribute handler for readCharacteristic of OIC server
61 static char *g_gattReadCharPath = NULL;
64 * @var g_gattWriteCharPath
65 * @brief attribute handler for writeCharacteristic of OIC server
67 static char *g_gattWriteCharPath = NULL;
71 * @brief handler for OIC advertiser.
73 static bt_advertiser_h g_hAdvertiser = NULL;
76 * @var g_bleServerDataReceivedCallback
77 * @brief Maintains the callback to be notified on receival of network packets from other
80 static CABLEDataReceivedCallback g_bleServerDataReceivedCallback = NULL;
83 * @var g_serverErrorCallback
84 * @brief callback to update the error to le adapter
86 static CABLEErrorHandleCallback g_serverErrorCallback;
89 * @var g_isBleGattServerStarted
90 * @brief Boolean variable to keep the state of the GATTServer
92 static bool g_isBleGattServerStarted = false;
95 * @var g_bleServerStateMutex
96 * @brief Mutex to synchronize the calls to be done to the platform from GATTServer
97 * interfaces from different threads.
99 static ca_mutex g_bleServerStateMutex = NULL;
102 * @var g_bleCharacteristicMutex
103 * @brief Mutex to synchronize writing operations on the characteristics.
105 static ca_mutex g_bleCharacteristicMutex = NULL;
108 * @var g_bleServiceMutex
109 * @brief Mutex to synchronize to create the OIC service..
111 static ca_mutex g_bleServiceMutex = NULL;
114 * @var g_bleReqRespCbMutex
115 * @brief Mutex to synchronize access to the requestResponse callback to be called
116 * when the data needs to be sent from GATTClient.
118 static ca_mutex g_bleReqRespCbMutex = NULL;
121 * @var g_bleServerThreadPoolMutex
122 * @brief Mutex to synchronize the task to be pushed to thread pool.
124 static ca_mutex g_bleServerThreadPoolMutex = NULL;
128 * @brief gmainLoop to manage the threads to receive the callback from the platfrom.
130 static GMainLoop *g_eventLoop = NULL;
133 * @var g_bleServerThreadPool
134 * @brief reference to threadpool
136 static ca_thread_pool_t g_bleServerThreadPool = NULL;
138 void CABleGattServerConnectionStateChangedCb(int result, bool connected,
139 const char *remoteAddress, void *userData)
141 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
143 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "CABleGattConnectionStateChangedCb result[%d]", result);
145 VERIFY_NON_NULL_VOID(remoteAddress, TZ_BLE_SERVER_TAG, "remote address is NULL");
149 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "Connected to [%s]", remoteAddress);
151 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
154 CAResult_t CAStartLEGattServer()
156 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
158 CAResult_t ret = CAInitGattServerMutexVariables();
159 if (CA_STATUS_OK != ret )
161 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "CAInitGattServerMutexVariables failed!");
162 CATerminateGattServerMutexVariables();
163 return CA_SERVER_NOT_STARTED;
166 ca_mutex_lock(g_bleServerThreadPoolMutex);
167 if (NULL == g_bleServerThreadPool)
169 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "g_bleServerThreadPool is NULL");
170 ca_mutex_unlock(g_bleServerThreadPoolMutex);
171 return CA_STATUS_FAILED;
174 ret = ca_thread_pool_add_task(g_bleServerThreadPool, CAStartBleGattServerThread,
176 if (CA_STATUS_OK != ret)
178 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "ca_thread_pool_add_task failed with ret [%d]", ret);
179 ca_mutex_unlock(g_bleServerThreadPoolMutex);
180 return CA_STATUS_FAILED;
183 ca_mutex_unlock(g_bleServerThreadPoolMutex);
184 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
188 void CAStartBleGattServerThread(void *data)
190 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
191 ca_mutex_lock(g_bleServerStateMutex);
192 if (true == g_isBleGattServerStarted)
194 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "Gatt Server is already running");
195 ca_mutex_unlock(g_bleServerStateMutex);
196 CATerminateLEGattServer();
200 CAResult_t ret = CAInitBleGattService();
201 if (CA_STATUS_OK != ret )
203 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "_bt_gatt_init_service failed");
204 ca_mutex_unlock(g_bleServerStateMutex);
205 CATerminateLEGattServer();
209 sleep(5); // Sleep is must because of the platform issue.
211 char *serviceUUID = CA_GATT_SERVICE_UUID;
213 ret = CAAddNewBleServiceInGattServer(serviceUUID);
214 if (CA_STATUS_OK != ret )
216 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "CAAddNewBleServiceInGattServer failed");
217 ca_mutex_unlock(g_bleServerStateMutex);
218 CATerminateLEGattServer();
222 static const char charReadUUID[] = CA_GATT_RESPONSE_CHRC_UUID;
223 uint8_t charReadValue[] = {33, 44, 55, 66}; // These are initial random values
225 ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charReadUUID, charReadValue,
226 CA_BLE_INITIAL_BUF_SIZE, true); // For Read Characteristics.
227 if (CA_STATUS_OK != ret )
229 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "CAAddNewCharacteristicsToGattServer failed");
230 ca_mutex_unlock(g_bleServerStateMutex);
231 CATerminateLEGattServer();
235 static const char charWriteUUID[] = CA_GATT_REQUEST_CHRC_UUID;
236 uint8_t charWriteValue[] = {33, 44, 55, 66}; // These are initial random values
239 ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charWriteUUID, charWriteValue,
240 CA_BLE_INITIAL_BUF_SIZE, false); // For Write Characteristics.
241 if (CA_STATUS_OK != ret )
243 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "CAAddNewCharacteristicsToGattServer failed");
244 ca_mutex_unlock(g_bleServerStateMutex);
245 CATerminateLEGattServer();
249 ret = CARegisterBleServicewithGattServer(g_gattSvcPath);
250 if (CA_STATUS_OK != ret )
252 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "CARegisterBleServicewithGattServer failed");
253 ca_mutex_unlock(g_bleServerStateMutex);
254 CATerminateLEGattServer();
258 int res = bt_gatt_set_connection_state_changed_cb(CABleGattServerConnectionStateChangedCb,
260 if (BT_ERROR_NONE != res)
262 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
263 "bt_gatt_set_connection_state_changed_cb Failed with return as [%s]",
264 CABTGetErrorMsg(res));
268 bt_adapter_le_create_advertiser(&g_hAdvertiser);
269 if (NULL == g_hAdvertiser)
271 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "g_hAdvertiser is NULL");
272 ca_mutex_unlock(g_bleServerStateMutex);
273 CATerminateLEGattServer();
277 res = bt_adapter_le_start_advertising(g_hAdvertiser, NULL, NULL, NULL);
278 if (BT_ERROR_NONE != res)
280 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "bt_adapter_le_start_advertising failed with ret [%d] ",
282 ca_mutex_unlock(g_bleServerStateMutex);
283 CATerminateLEGattServer();
287 g_isBleGattServerStarted = true;
289 ca_mutex_unlock(g_bleServerStateMutex);
291 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG,
292 "LE Server initialization complete.");
294 GMainContext *thread_context = NULL;
296 thread_context = g_main_context_new();
298 g_eventLoop = g_main_loop_new(thread_context, FALSE);
300 g_main_context_push_thread_default(thread_context);
302 g_main_loop_run(g_eventLoop);
304 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
307 CAResult_t CAStopLEGattServer()
309 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
311 ca_mutex_lock(g_bleServerStateMutex);
313 if (false == g_isBleGattServerStarted)
315 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "Gatt Server is not running to stop");
317 ca_mutex_unlock(g_bleServerStateMutex);
321 g_isBleGattServerStarted = false;
322 if (NULL != g_hAdvertiser )
325 ret = bt_adapter_le_stop_advertising(g_hAdvertiser);
328 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
329 "bt_adapter_le_stop_advertising failed with ret [%d]", ret);
332 ret = bt_adapter_le_destroy_advertiser(g_hAdvertiser);
335 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
336 "bt_adapter_le_destroy_advertiser failed with ret [%d]", ret);
338 g_hAdvertiser = NULL;
341 CAResult_t res = CARemoveAllBleServicesFromGattServer();
342 if (CA_STATUS_OK != res)
344 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "removeAllBleServicesFromGattServer failed");
347 res = CADeInitBleGattService();
348 if (CA_STATUS_OK != res)
350 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "_bt_gatt_deinit_service failed with ret [%d]", res);
353 GMainContext *context_event_loop = NULL;
354 // Required for waking up the thread which is running in gmain loop
355 if (NULL != g_eventLoop)
357 context_event_loop = g_main_loop_get_context(g_eventLoop);
359 if (context_event_loop)
361 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "g_eventLoop context %x", context_event_loop);
362 g_main_context_wakeup(context_event_loop);
364 // Kill g main loops and kill threads
365 g_main_loop_quit(g_eventLoop);
370 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "g_eventLoop context is NULL");
373 ca_mutex_unlock(g_bleServerStateMutex);
375 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
379 void CATerminateLEGattServer()
381 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
383 ca_mutex_lock(g_bleServerStateMutex);
385 // free service Path(unique identifier for ble service)
386 ca_mutex_lock(g_bleServiceMutex);
387 OICFree(g_gattSvcPath);
388 g_gattSvcPath = NULL;
389 ca_mutex_unlock(g_bleServiceMutex);
391 // freeing characteristics
392 ca_mutex_lock(g_bleCharacteristicMutex);
393 OICFree(g_gattReadCharPath);
394 g_gattReadCharPath = NULL;
395 OICFree(g_gattWriteCharPath);
396 g_gattWriteCharPath = NULL;
397 ca_mutex_unlock(g_bleCharacteristicMutex);
399 ca_mutex_unlock(g_bleServerStateMutex);
401 // Terminating all mutex variables.
402 CATerminateGattServerMutexVariables();
403 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
406 CAResult_t CAInitGattServerMutexVariables()
408 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
409 if (NULL == g_bleServerStateMutex)
411 g_bleServerStateMutex = ca_mutex_new();
412 if (NULL == g_bleServerStateMutex)
414 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "ca_mutex_new failed");
415 return CA_STATUS_FAILED;
419 if (NULL == g_bleServiceMutex)
421 g_bleServiceMutex = ca_mutex_new();
422 if (NULL == g_bleServiceMutex)
424 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "ca_mutex_new failed");
425 return CA_STATUS_FAILED;
429 if (NULL == g_bleCharacteristicMutex)
431 g_bleCharacteristicMutex = ca_mutex_new();
432 if (NULL == g_bleCharacteristicMutex)
434 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "ca_mutex_new failed");
435 return CA_STATUS_FAILED;
439 if (NULL == g_bleReqRespCbMutex)
441 g_bleReqRespCbMutex = ca_mutex_new();
442 if (NULL == g_bleReqRespCbMutex)
444 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "ca_mutex_new failed");
445 return CA_STATUS_FAILED;
448 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
452 void CATerminateGattServerMutexVariables()
454 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
455 ca_mutex_free(g_bleServerStateMutex);
456 g_bleServerStateMutex = NULL;
459 g_bleServerStateMutex = NULL;
460 ca_mutex_free(g_bleServiceMutex);
461 g_bleServiceMutex = NULL;
462 ca_mutex_free(g_bleCharacteristicMutex);
463 g_bleCharacteristicMutex = NULL;
464 ca_mutex_free(g_bleReqRespCbMutex);
465 g_bleReqRespCbMutex = NULL;
467 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
470 CAResult_t CAInitBleGattService()
472 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
474 int ret = _bt_gatt_init_service();
477 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "_bt_gatt_deinit_service failed with ret [%d]", ret);
478 return CA_STATUS_FAILED;
481 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
485 CAResult_t CADeInitBleGattService()
487 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
489 int ret = _bt_gatt_deinit_service();
492 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "_bt_gatt_deinit_service failed with ret [%d]", ret);
493 return CA_STATUS_FAILED;
496 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
500 void CASetLEServerThreadPoolHandle(ca_thread_pool_t handle)
502 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
503 ca_mutex_lock(g_bleServerThreadPoolMutex);
504 g_bleServerThreadPool = handle;
505 ca_mutex_unlock(g_bleServerThreadPoolMutex);
506 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
509 CAResult_t CAAddNewBleServiceInGattServer(const char *serviceUUID)
511 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
513 VERIFY_NON_NULL(serviceUUID, TZ_BLE_SERVER_TAG, "Param serviceUUID is NULL");
515 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "service uuid %s", serviceUUID);
517 char *svcPath = NULL;
519 int ret = bt_gatt_add_service(serviceUUID, &svcPath);
522 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "bt_gatt_add_service failed with ret [%d]", ret);
523 return CA_STATUS_FAILED;
528 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
529 "AddNewBleServiceInGattServer ServicePath obtained is %s", svcPath);
531 ca_mutex_lock(g_bleServiceMutex);
533 if (NULL != g_gattSvcPath)
535 OICFree(g_gattSvcPath);
536 g_gattSvcPath = NULL;
538 g_gattSvcPath = svcPath;
540 ca_mutex_unlock(g_bleServiceMutex);
543 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
547 CAResult_t CARemoveBleServiceFromGattServer(const char *svcPath)
549 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
551 VERIFY_NON_NULL(svcPath, TZ_BLE_SERVER_TAG, "Param svcPath is NULL");
553 int ret = bt_gatt_remove_service(svcPath);
557 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "bt_gatt_remove_service failed [%d]", ret);
558 return CA_STATUS_FAILED;
561 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
565 CAResult_t CARemoveAllBleServicesFromGattServer()
567 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
568 int ret = bt_gatt_delete_services();
571 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "bt_gatt_delete_services failed with ret [%d]", ret);
572 return CA_STATUS_FAILED;
575 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
579 void CABleGattRemoteCharacteristicWriteCb(char *charPath,
580 unsigned char *charValue,
582 const char *remoteAddress,
585 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
587 if (NULL == charPath || NULL == charValue || NULL == remoteAddress)
589 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "Param callback values are NULL");
595 "charPath = [%s] charValue = [%p] len [%d]",
600 uint8_t *data = OICMalloc(charValueLen);
603 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "Malloc failed!");
607 memcpy(data, charValue, charValueLen);
609 ca_mutex_lock(g_bleReqRespCbMutex);
610 if (NULL == g_bleServerDataReceivedCallback)
612 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "gReqRespCallback is NULL!");
613 ca_mutex_unlock(g_bleReqRespCbMutex);
618 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "Sending data up !");
619 uint32_t sentLength = 0;
620 g_bleServerDataReceivedCallback(remoteAddress, data, charValueLen,
623 ca_mutex_unlock(g_bleReqRespCbMutex);
626 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
629 CAResult_t CARegisterBleServicewithGattServer(const char *svcPath)
631 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
633 VERIFY_NON_NULL(svcPath, TZ_BLE_SERVER_TAG, "Param svcPath is NULL");
635 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "svcPath:%s", svcPath);
637 int ret = bt_gatt_register_service(svcPath, CABleGattRemoteCharacteristicWriteCb, NULL);
641 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "bt_gatt_register_service failed with ret [%d]", ret);
642 return CA_STATUS_FAILED;
645 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
649 CAResult_t CAAddNewCharacteristicsToGattServer(const char *svcPath, const char *charUUID,
650 const uint8_t *charValue, int charValueLen, bool read)
653 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
655 const char *charFlags[1];
658 charFlags[0] = "notify";
662 charFlags[0] = "write-without-response";
665 size_t flagLen = sizeof(charFlags) / sizeof(charFlags[0]);
667 char *charPath = NULL;
669 bt_gatt_add_characteristic(charUUID,
670 (const char *) charValue,
677 if (0 != ret || NULL == charPath)
679 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
680 "bt_gatt_add_characteristic failed with ret [%d]", ret);
681 return CA_STATUS_FAILED;
684 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG,
685 "bt_gatt_add_characteristic charPath obtained: %s", charPath);
687 ca_mutex_lock(g_bleCharacteristicMutex);
691 if (NULL != g_gattReadCharPath)
693 OICFree(g_gattReadCharPath);
694 g_gattReadCharPath = NULL;
696 g_gattReadCharPath = charPath;
701 if (NULL != g_gattWriteCharPath)
703 OICFree(g_gattWriteCharPath);
704 g_gattWriteCharPath = NULL;
706 g_gattWriteCharPath = charPath;
709 ca_mutex_unlock(g_bleCharacteristicMutex);
711 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
715 CAResult_t CARemoveCharacteristicsFromGattServer(const char *charPath)
717 ///TODO: There is no api provided in bluetooth.h for removing characteristics.
721 CAResult_t CAUpdateCharacteristicsToGattClient(const char *address,
722 const uint8_t *charValue,
723 uint32_t charValueLen)
725 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
727 VERIFY_NON_NULL(charValue, TZ_BLE_SERVER_TAG, "Param charValue is NULL");
729 VERIFY_NON_NULL(address, TZ_BLE_SERVER_TAG, "Param address is NULL");
731 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "Client's Unicast address for sending data [%s]", address);
733 ca_mutex_lock(g_bleCharacteristicMutex);
735 if (NULL == g_gattReadCharPath)
737 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "g_gattReadCharPath is NULL");
738 ca_mutex_unlock(g_bleCharacteristicMutex);
739 return CA_STATUS_FAILED;
742 char *data = OICCalloc(charValueLen, 1);
745 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "malloc failed!");
746 ca_mutex_unlock(g_bleCharacteristicMutex);
747 return CA_STATUS_FAILED;
750 memcpy(data, charValue, charValueLen); // Binary data
752 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "updating characteristics char [%s] data [%p] dataLen [%u]",
753 (const char *)g_gattReadCharPath, data, charValueLen);
756 bt_gatt_update_characteristic(g_gattReadCharPath,
762 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
763 "bt_gatt_update_characteristic failed with return [%d]", ret);
765 ca_mutex_unlock(g_bleCharacteristicMutex);
766 return CA_STATUS_FAILED;
770 ca_mutex_unlock(g_bleCharacteristicMutex);
772 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "OUT");
776 CAResult_t CAUpdateCharacteristicsToAllGattClients(const uint8_t *charValue, uint32_t charValueLen)
778 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
780 VERIFY_NON_NULL(charValue, TZ_BLE_SERVER_TAG, "Param charValue is NULL");
782 ca_mutex_lock(g_bleCharacteristicMutex);
784 if (NULL == g_gattReadCharPath)
786 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "g_gattReadCharPath is NULL");
787 ca_mutex_unlock(g_bleCharacteristicMutex);
788 return CA_STATUS_FAILED;
791 char *data = OICMalloc(charValueLen);
794 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "malloc failed!");
795 ca_mutex_unlock(g_bleCharacteristicMutex);
796 return CA_STATUS_FAILED;
799 memcpy(data, charValue, charValueLen); // Binary data
801 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "updating characteristics char [%s] data [%p] dataLen [%u]",
802 (const char *)g_gattReadCharPath, data, charValueLen);
805 bt_gatt_update_characteristic(g_gattReadCharPath,
811 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
812 "bt_gatt_update_characteristic failed with return [%d]", ret);
814 ca_mutex_unlock(g_bleCharacteristicMutex);
815 return CA_STATUS_FAILED;
819 ca_mutex_unlock(g_bleCharacteristicMutex);
821 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "OUT");
825 void CASetLEReqRespServerCallback(CABLEDataReceivedCallback callback)
827 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
829 ca_mutex_lock(g_bleReqRespCbMutex);
831 g_bleServerDataReceivedCallback = callback;
833 ca_mutex_unlock(g_bleReqRespCbMutex);
835 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
838 void CASetBLEServerErrorHandleCallback(CABLEErrorHandleCallback callback)
840 g_serverErrorCallback = callback;