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 "camsgparser.h"
34 #include "cableutil.h"
35 #include "oic_string.h"
36 #include "oic_malloc.h"
39 * @def TZ_BLE_SERVER_TAG
40 * @brief Logging tag for module name
42 #define TZ_BLE_SERVER_TAG "TZ_BLE_GATT_SERVER"
45 * @def CA_BLE_SERVICE_UUID
46 * @brief UUID of OIC service. This UUID is common across all platoform for LE transport.
48 #define CA_BLE_SERVICE_UUID "713D0000-503E-4C75-BA94-3148F18D941E"
51 * @def CA_BLE_READ_CHAR_UUID
52 * @brief UUID of read characteristic. This UUID is common across all platoform for LE transport.
54 #define CA_BLE_READ_CHAR_UUID "713D0002-503E-4C75-BA94-3148F18D941E"
57 * @def CA_BLE_WRITE_CHAR_UUID
58 * @brief UUID of write characteristic. This UUID is common across all platoform for LE transport.
60 #define CA_BLE_WRITE_CHAR_UUID "713D0003-503E-4C75-BA94-3148F18D941E"
63 * @def CA_BLE_INITIAL_BUF_SIZE
64 * @brief Initial buffer size for Gatt Server.
66 #define CA_BLE_INITIAL_BUF_SIZE 512
70 * @brief attribute handler for OIC server attribute.
72 static char *g_gattSvcPath = NULL;
75 * @var g_gattReadCharPath
76 * @brief attribute handler for readCharacteristic of OIC server
78 static char *g_gattReadCharPath = NULL;
81 * @var g_gattWriteCharPath
82 * @brief attribute handler for writeCharacteristic of OIC server
84 static char *g_gattWriteCharPath = NULL;
88 * @brief handler for OIC advertiser.
90 static bt_advertiser_h g_hAdvertiser = NULL;
93 * @var g_bleServerDataReceivedCallback
94 * @brief Maintains the callback to be notified on receival of network packets from other
97 static CABLEServerDataReceivedCallback g_bleServerDataReceivedCallback = NULL;
100 * @var g_isBleGattServerStarted
101 * @brief Boolean variable to keep the state of the GATTServer
103 static bool g_isBleGattServerStarted = false;
106 * @var g_bleServerStateMutex
107 * @brief Mutex to synchronize the calls to be done to the platform from GATTServer
108 * interfaces from different threads.
110 static u_mutex g_bleServerStateMutex = NULL;
113 * @var g_bleCharacteristicMutex
114 * @brief Mutex to synchronize writing operations on the characteristics.
116 static u_mutex g_bleCharacteristicMutex = NULL;
119 * @var g_bleServiceMutex
120 * @brief Mutex to synchronize to create the OIC service..
122 static u_mutex g_bleServiceMutex = NULL;
125 * @var g_bleClientBDAddressMutex
126 * @brief Mutex to synchronize the client BD Address update on server side.
128 static u_mutex g_bleClientBDAddressMutex = NULL;
132 * @var g_bleReqRespCbMutex
133 * @brief Mutex to synchronize access to the requestResponse callback to be called
134 * when the data needs to be sent from GATTClient.
136 static u_mutex g_bleReqRespCbMutex = NULL;
139 * @var g_bleServerThreadPoolMutex
140 * @brief Mutex to synchronize the task to be pushed to thread pool.
142 static u_mutex g_bleServerThreadPoolMutex = NULL;
145 * @struct gRemoteAddress
146 * @brief Remote address of Gatt client
149 static char *g_remoteClientAddress = NULL;
153 * @brief gmainLoop to manage the threads to receive the callback from the platfrom.
155 static GMainLoop *g_eventLoop = NULL;
158 * @var g_bleServerThreadPool
159 * @brief reference to threadpool
161 static u_thread_pool_t g_bleServerThreadPool = NULL;
163 void CABleGattServerConnectionStateChangedCb(int result, bool connected,
164 const char *remoteAddress, void *userData)
166 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
168 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "CABleGattConnectionStateChangedCb result[%d]", result);
170 VERIFY_NON_NULL_VOID(remoteAddress, TZ_BLE_SERVER_TAG, "remote address is NULL");
174 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "Connected to [%s]", remoteAddress);
176 u_mutex_lock(g_bleClientBDAddressMutex);
177 g_remoteClientAddress = OICStrdup(remoteAddress);
178 u_mutex_unlock(g_bleClientBDAddressMutex);
180 VERIFY_NON_NULL_VOID(g_remoteClientAddress, TZ_BLE_SERVER_TAG, "Malloc failed");
182 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
185 CAResult_t CAStartBleGattServer()
187 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
189 CAResult_t ret = CAInitGattServerMutexVariables();
190 if (CA_STATUS_OK != ret )
192 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "CAInitGattServerMutexVariables failed!");
193 CATerminateGattServerMutexVariables();
194 return CA_SERVER_NOT_STARTED;
197 u_mutex_lock(g_bleServerThreadPoolMutex);
198 if (NULL == g_bleServerThreadPool)
200 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "g_bleServerThreadPool is NULL");
201 u_mutex_unlock(g_bleServerThreadPoolMutex);
202 return CA_STATUS_FAILED;
205 ret = u_thread_pool_add_task(g_bleServerThreadPool, CAStartBleGattServerThread,
207 if (CA_STATUS_OK != ret)
209 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "u_thread_pool_add_task failed with ret [%d]", ret);
210 u_mutex_unlock(g_bleServerThreadPoolMutex);
211 return CA_STATUS_FAILED;
214 u_mutex_unlock(g_bleServerThreadPoolMutex);
215 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
219 void CAStartBleGattServerThread(void *data)
221 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
222 u_mutex_lock(g_bleServerStateMutex);
223 if (true == g_isBleGattServerStarted)
225 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "Gatt Server is already running");
226 u_mutex_unlock(g_bleServerStateMutex);
227 CATerminateBleGattServer();
231 CAResult_t ret = CAInitBleGattService();
232 if (CA_STATUS_OK != ret )
234 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "_bt_gatt_init_service failed");
235 u_mutex_unlock(g_bleServerStateMutex);
236 CATerminateBleGattServer();
240 sleep(5); // Sleep is must because of the platform issue.
242 char *serviceUUID = CA_BLE_SERVICE_UUID;
244 ret = CAAddNewBleServiceInGattServer(serviceUUID);
245 if (CA_STATUS_OK != ret )
247 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "CAAddNewBleServiceInGattServer failed");
248 u_mutex_unlock(g_bleServerStateMutex);
249 CATerminateBleGattServer();
253 char *charReadUUID = CA_BLE_READ_CHAR_UUID;
254 char charReadValue[] = {33, 44, 55, 66}; // These are initial random values
256 ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charReadUUID, charReadValue,
257 CA_BLE_INITIAL_BUF_SIZE, false); // For Read Characteristics.
258 if (CA_STATUS_OK != ret )
260 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "CAAddNewCharacteristicsToGattServer failed");
261 u_mutex_unlock(g_bleServerStateMutex);
262 CATerminateBleGattServer();
266 char *charWriteUUID = CA_BLE_WRITE_CHAR_UUID;
267 char charWriteValue[] = {33, 44, 55, 66}; // These are initial random values
270 ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charWriteUUID, charWriteValue,
271 CA_BLE_INITIAL_BUF_SIZE, true); // For Write Characteristics.
272 if (CA_STATUS_OK != ret )
274 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "CAAddNewCharacteristicsToGattServer failed");
275 u_mutex_unlock(g_bleServerStateMutex);
276 CATerminateBleGattServer();
280 ret = CARegisterBleServicewithGattServer(g_gattSvcPath);
281 if (CA_STATUS_OK != ret )
283 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "CARegisterBleServicewithGattServer failed");
284 u_mutex_unlock(g_bleServerStateMutex);
285 CATerminateBleGattServer();
289 int res = bt_gatt_set_connection_state_changed_cb(CABleGattServerConnectionStateChangedCb,
291 if (BT_ERROR_NONE != res)
293 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
294 "bt_gatt_set_connection_state_changed_cb Failed with return as [%s]",
295 CABTGetErrorMsg(res));
299 bt_adapter_le_create_advertiser(&g_hAdvertiser);
300 if (NULL == g_hAdvertiser)
302 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "g_hAdvertiser is NULL");
303 u_mutex_unlock(g_bleServerStateMutex);
304 CATerminateBleGattServer();
308 res = bt_adapter_le_start_advertising(g_hAdvertiser, NULL, NULL, NULL);
309 if (BT_ERROR_NONE != res)
311 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "bt_adapter_le_start_advertising failed with ret [%d] ",
313 u_mutex_unlock(g_bleServerStateMutex);
314 CATerminateBleGattServer();
318 g_isBleGattServerStarted = true;
320 u_mutex_unlock(g_bleServerStateMutex);
322 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG,
323 "LE Server initialization complete.");
325 GMainContext *thread_context = NULL;
327 thread_context = g_main_context_new();
329 g_eventLoop = g_main_loop_new(thread_context, FALSE);
331 g_main_context_push_thread_default(thread_context);
333 g_main_loop_run(g_eventLoop);
335 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
338 CAResult_t CAStopBleGattServer()
340 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
342 u_mutex_lock(g_bleServerStateMutex);
344 if (false == g_isBleGattServerStarted)
346 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "Gatt Server is not running to stop");
348 u_mutex_unlock(g_bleServerStateMutex);
351 u_mutex_unlock(g_bleServerStateMutex);
353 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
357 void CATerminateBleGattServer()
359 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
361 g_isBleGattServerStarted = false;
363 u_mutex_lock(g_bleServerStateMutex);
364 GMainContext *context_event_loop = NULL;
365 // Required for waking up the thread which is running in gmain loop
366 if ( NULL != g_eventLoop)
368 context_event_loop = g_main_loop_get_context(g_eventLoop);
370 if (context_event_loop)
372 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "g_eventLoop context %x", context_event_loop);
373 g_main_context_wakeup(context_event_loop);
375 // Kill g main loops and kill threads
376 g_main_loop_quit(g_eventLoop);
380 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "g_eventLoop context is NULL");
383 if (NULL != g_hAdvertiser )
386 ret = bt_adapter_le_stop_advertising(g_hAdvertiser);
389 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
390 "bt_adapter_le_stop_advertising failed with ret [%d]", ret);
393 ret = bt_adapter_le_destroy_advertiser(g_hAdvertiser);
396 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
397 "bt_adapter_le_destroy_advertiser failed with ret [%d]", ret);
399 g_hAdvertiser = NULL;
402 CAResult_t res = CARemoveAllBleServicesFromGattServer();
403 if (CA_STATUS_OK != res)
405 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "removeAllBleServicesFromGattServer failed");
408 res = CADeInitBleGattService();
409 if (CA_STATUS_OK != res)
411 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "_bt_gatt_deinit_service failed with ret [%d]", res);
414 u_mutex_lock(g_bleClientBDAddressMutex);
415 OICFree(g_remoteClientAddress);
416 u_mutex_unlock(g_bleClientBDAddressMutex);
417 // free service Path(unique identifier for ble service)
418 u_mutex_lock(g_bleServiceMutex);
419 OICFree(g_gattSvcPath);
420 g_gattSvcPath = NULL;
421 u_mutex_unlock(g_bleServiceMutex);
423 // freeing characteristics
424 u_mutex_lock(g_bleCharacteristicMutex);
425 OICFree(g_gattReadCharPath);
426 g_gattReadCharPath = NULL;
427 OICFree(g_gattWriteCharPath);
428 g_gattWriteCharPath = NULL;
429 u_mutex_unlock(g_bleCharacteristicMutex);
431 u_mutex_unlock(g_bleServerStateMutex);
433 // Terminating all mutex variables.
434 CATerminateGattServerMutexVariables();
435 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
438 CAResult_t CAInitGattServerMutexVariables()
440 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
441 if (NULL == g_bleServerStateMutex)
443 g_bleServerStateMutex = u_mutex_new();
444 if (NULL == g_bleServerStateMutex)
446 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "u_mutex_new failed");
447 return CA_STATUS_FAILED;
451 if (NULL == g_bleServiceMutex)
453 g_bleServiceMutex = u_mutex_new();
454 if (NULL == g_bleServiceMutex)
456 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "u_mutex_new failed");
457 return CA_STATUS_FAILED;
461 if (NULL == g_bleCharacteristicMutex)
463 g_bleCharacteristicMutex = u_mutex_new();
464 if (NULL == g_bleCharacteristicMutex)
466 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "u_mutex_new failed");
467 return CA_STATUS_FAILED;
471 if (NULL == g_bleReqRespCbMutex)
473 g_bleReqRespCbMutex = u_mutex_new();
474 if (NULL == g_bleReqRespCbMutex)
476 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "u_mutex_new failed");
477 return CA_STATUS_FAILED;
481 if (NULL == g_bleClientBDAddressMutex)
483 g_bleClientBDAddressMutex = u_mutex_new();
484 if (NULL == g_bleClientBDAddressMutex)
486 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "u_mutex_new failed");
487 return CA_STATUS_FAILED;
494 void CATerminateGattServerMutexVariables()
496 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
497 u_mutex_free(g_bleServerStateMutex);
498 g_bleServerStateMutex = NULL;
500 u_mutex_free(g_bleClientBDAddressMutex);
501 g_bleClientBDAddressMutex = NULL;
503 g_bleServerStateMutex = NULL;
504 u_mutex_free(g_bleServiceMutex);
505 g_bleServiceMutex = NULL;
506 u_mutex_free(g_bleCharacteristicMutex);
507 g_bleCharacteristicMutex = NULL;
508 u_mutex_free(g_bleReqRespCbMutex);
509 g_bleReqRespCbMutex = NULL;
511 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
514 CAResult_t CAInitBleGattService()
516 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
518 int ret = _bt_gatt_init_service();
521 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "_bt_gatt_deinit_service failed with ret [%d]", ret);
522 return CA_STATUS_FAILED;
525 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
529 CAResult_t CADeInitBleGattService()
531 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
533 int ret = _bt_gatt_deinit_service();
536 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "_bt_gatt_deinit_service failed with ret [%d]", ret);
537 return CA_STATUS_FAILED;
540 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
544 void CASetBleServerThreadPoolHandle(u_thread_pool_t handle)
546 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
547 u_mutex_lock(g_bleServerThreadPoolMutex);
548 g_bleServerThreadPool = handle;
549 u_mutex_unlock(g_bleServerThreadPoolMutex);
550 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
553 CAResult_t CAAddNewBleServiceInGattServer(const char *serviceUUID)
555 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
557 VERIFY_NON_NULL(serviceUUID, NULL, "Param serviceUUID is NULL");
559 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "service uuid %s", serviceUUID);
561 char *svcPath = NULL;
563 int ret = bt_gatt_add_service(serviceUUID, &svcPath);
566 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "bt_gatt_add_service failed with ret [%d]", ret);
567 return CA_STATUS_FAILED;
572 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
573 "AddNewBleServiceInGattServer ServicePath obtained is %s", svcPath);
575 u_mutex_lock(g_bleServiceMutex);
577 if (NULL != g_gattSvcPath)
579 OICFree(g_gattSvcPath);
580 g_gattSvcPath = NULL;
582 g_gattSvcPath = svcPath;
584 u_mutex_unlock(g_bleServiceMutex);
587 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
591 CAResult_t CARemoveBleServiceFromGattServer(const char *svcPath)
593 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
595 VERIFY_NON_NULL(svcPath, NULL, "Param svcPath is NULL");
597 int ret = bt_gatt_remove_service(svcPath);
601 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "bt_gatt_remove_service failed [%d]", ret);
602 return CA_STATUS_FAILED;
605 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
609 CAResult_t CARemoveAllBleServicesFromGattServer()
611 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
612 int ret = bt_gatt_delete_services();
615 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "bt_gatt_delete_services failed with ret [%d]", ret);
616 return CA_STATUS_FAILED;
619 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
623 void CABleGattRemoteCharacteristicWriteCb(char *charPath,
624 unsigned char *charValue,
625 int charValueLen, void *userData)
627 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
629 if (NULL == charPath || NULL == charValue)
631 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "Param callback values are NULL");
635 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "charPath = [%s] charValue = [%s] len [%d]", charPath,
636 charValue, charValueLen);
638 char *data = (char *)OICMalloc(sizeof(char) * charValueLen);
641 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "Malloc failed!");
645 strncpy(data, (char *)charValue, charValueLen);
647 u_mutex_lock(g_bleReqRespCbMutex);
648 if (NULL == g_bleServerDataReceivedCallback)
650 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "gReqRespCallback is NULL!");
651 u_mutex_unlock(g_bleReqRespCbMutex);
656 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "Sending data up !");
657 u_mutex_lock(g_bleClientBDAddressMutex);
658 uint32_t sentLength = 0;
659 g_bleServerDataReceivedCallback(g_remoteClientAddress, OIC_BLE_SERVICE_ID,
660 data, charValueLen, &sentLength);
661 u_mutex_unlock(g_bleClientBDAddressMutex);
663 u_mutex_unlock(g_bleReqRespCbMutex);
666 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
669 CAResult_t CARegisterBleServicewithGattServer(const char *svcPath)
671 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
673 VERIFY_NON_NULL(svcPath, NULL, "Param svcPath is NULL");
675 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "svcPath:%s", svcPath);
677 int ret = bt_gatt_register_service(svcPath, CABleGattRemoteCharacteristicWriteCb, NULL);
681 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "bt_gatt_register_service failed with ret [%d]", ret);
682 return CA_STATUS_FAILED;
685 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
689 CAResult_t CAAddNewCharacteristicsToGattServer(const char *svcPath, const char *charUUID,
690 const char *charValue, int charValueLen, bool read)
693 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
695 static const char *charFlags[] = {"read", "write", "notify", "indicate"};
697 static const size_t flagLen = sizeof(charFlags) / sizeof(charFlags[0]);
699 char *charPath = NULL;
700 int ret = bt_gatt_add_characteristic(charUUID, charValue, charValueLen, charFlags, flagLen,
703 if (0 != ret || NULL == charPath)
705 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
706 "bt_gatt_add_characteristic failed with ret [%d]", ret);
707 return CA_STATUS_FAILED;
710 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG,
711 "bt_gatt_add_characteristic charPath obtained: %s", charPath);
713 u_mutex_lock(g_bleCharacteristicMutex);
717 if (NULL != g_gattReadCharPath)
719 OICFree(g_gattReadCharPath);
720 g_gattReadCharPath = NULL;
722 g_gattReadCharPath = charPath;
727 if (NULL != g_gattWriteCharPath)
729 OICFree(g_gattWriteCharPath);
730 g_gattWriteCharPath = NULL;
732 g_gattWriteCharPath = charPath;
735 u_mutex_unlock(g_bleCharacteristicMutex);
737 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
741 CAResult_t CARemoveCharacteristicsFromGattServer(const char *charPath)
743 ///TODO: There is no api provided in bluetooth.h for removing characteristics.
747 CAResult_t CAUpdateCharacteristicsInGattServer(const char *charValue,
748 const uint32_t charValueLen)
750 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
752 VERIFY_NON_NULL(charValue, NULL, "Param charValue is NULL");
754 u_mutex_lock(g_bleCharacteristicMutex);
756 if (NULL == g_gattWriteCharPath)
758 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "g_gattWriteCharPath is NULL");
759 u_mutex_unlock(g_bleCharacteristicMutex);
760 return CA_STATUS_FAILED;
763 char *data = (char *) OICMalloc(sizeof(char) * (charValueLen + 1));
766 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "malloc failed!");
767 u_mutex_unlock(g_bleCharacteristicMutex);
768 return CA_STATUS_FAILED;
771 strncpy(data, charValue, charValueLen + 1);
773 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "updating characteristics char [%s] data [%s] dataLen [%d]",
774 (const char *)g_gattWriteCharPath, data, charValueLen);
776 int ret = bt_gatt_update_characteristic(g_gattWriteCharPath, data, charValueLen);
779 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
780 "bt_gatt_update_characteristic failed with return [%d]", ret);
783 u_mutex_unlock(g_bleCharacteristicMutex);
784 return CA_STATUS_FAILED;
788 u_mutex_unlock(g_bleCharacteristicMutex);
790 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "OUT");
794 void CASetBLEReqRespServerCallback(CABLEServerDataReceivedCallback callback)
796 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
798 u_mutex_lock(g_bleReqRespCbMutex);
800 g_bleServerDataReceivedCallback = callback;
802 u_mutex_unlock(g_bleReqRespCbMutex);
804 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");