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 "caleserver.h"
23 #include "cacommonutil.h"
25 #include "caqueueingthread.h"
26 #include "cagattservice.h"
27 #include "oic_malloc.h"
31 * Logging tag for module name
33 #define TAG "OIC_CA_LE_SERVER"
36 * Initial buffer size for Gatt Server.
38 #define CA_LE_INITIAL_BUF_SIZE 512
41 * The handle of the OIC server.
43 static bt_gatt_server_h g_gattServer = NULL;
46 * The handle of the OIC service.
48 static bt_gatt_h g_gattSvcPath = NULL;
51 * The handle of the OIC read characteristics.
53 static bt_gatt_h g_gattReadCharPath = NULL;
56 * The handle of the OIC write characteristics.
58 static bt_gatt_h g_gattWriteCharPath = NULL;
61 * The handle to control Bluetooth LE advertising.
63 static bt_advertiser_h g_hAdvertiser = NULL;
66 * Callback register with LE adapter. This callback is called on reception of any
67 * data from the remote device.
69 static CABLEDataReceivedCallback g_leServerDataReceivedCallback = NULL;
72 * Callback to notify any error in LE adapter.
74 static CABLEErrorHandleCallback g_serverErrorCallback;
77 * To keep the state of GATT server if started or not.
79 static bool g_isLEGattServerStarted = false;
82 * Mutex to synchronize the calls to start and stop server.
84 static ca_mutex g_leServerStateMutex = NULL;
87 * Mutex to synchronize writing operations on the characteristics.
89 static ca_mutex g_leCharacteristicMutex = NULL;
92 * Mutex to synchronize to creation of OIC service.
94 static ca_mutex g_leServiceMutex = NULL;
97 * Mutex to synchronize access to the requestResponse callback to be called
98 * when the data needs to be sent from GATTClient.
100 static ca_mutex g_leReqRespCbMutex = NULL;
103 * Mutex to synchronize the task to be pushed to thread pool.
105 static ca_mutex g_leServerThreadPoolMutex = NULL;
108 * Reference to threadpool.
110 static ca_thread_pool_t g_leServerThreadPool = NULL;
113 * GmainLoop to manage the threads to receive the callback from the platfrom.
115 static GMainLoop *g_eventLoop = NULL;
117 static CALEConnectionStateChangedCallback g_connStateCb = NULL;
119 void CASetLEConnectionStateChangedCallback(CALEConnectionStateChangedCallback connStateCb)
121 g_connStateCb = connStateCb;
124 void CALEGattServerConnectionStateChanged(bool connected, const char *remoteAddress)
126 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address");
130 OIC_LOG_V(DEBUG, TAG, "Connected to [%s]", remoteAddress);
133 g_connStateCb(CA_ADAPTER_GATT_BTLE, remoteAddress, true);
138 OIC_LOG_V(DEBUG, TAG, "Disconnected from [%s]", remoteAddress);
141 g_connStateCb(CA_ADAPTER_GATT_BTLE, remoteAddress, false);
146 void CALEServerNotificationSentCB(int result, char *remote_address, bt_gatt_server_h server,
147 bt_gatt_h characteristic, bool completed, void *user_data)
149 OIC_LOG_V(DEBUG, TAG, "Notification to the device[%s] result[%d]", remote_address, result);
152 CAResult_t CAStartLEGattServer()
154 OIC_LOG(DEBUG, TAG, "IN");
156 ca_mutex_lock(g_leServerThreadPoolMutex);
157 if (NULL == g_leServerThreadPool)
159 OIC_LOG(ERROR, TAG, "g_leServerThreadPool is NULL");
160 ca_mutex_unlock(g_leServerThreadPoolMutex);
161 return CA_STATUS_FAILED;
164 CAResult_t ret = ca_thread_pool_add_task(g_leServerThreadPool, CAStartLEGattServerThread,
166 if (CA_STATUS_OK != ret)
168 OIC_LOG_V(ERROR, TAG, "ca_thread_pool_add_task failed with ret [%d]", ret);
169 ca_mutex_unlock(g_leServerThreadPoolMutex);
170 return CA_STATUS_FAILED;
173 ca_mutex_unlock(g_leServerThreadPoolMutex);
174 OIC_LOG(DEBUG, TAG, "OUT");
178 void CALENotificationCb(bool notify, bt_gatt_server_h server, bt_gatt_h gatt_handle,
181 OIC_LOG(DEBUG, TAG, "IN");
184 OIC_LOG(DEBUG, TAG, "Notification is subscribed by the client");
186 OIC_LOG(DEBUG, TAG, "OUT");
189 CAResult_t CALEStartAdvertise(const char *serviceUUID)
191 OIC_LOG(DEBUG, TAG, "IN");
193 int res = bt_adapter_le_create_advertiser(&g_hAdvertiser);
194 if (NULL == g_hAdvertiser || BT_ERROR_NONE != res)
196 OIC_LOG_V(ERROR, TAG, "g_hAdvertiser is NULL/ Result is %d", res);
197 return CA_STATUS_FAILED;
200 res = bt_adapter_le_add_advertising_service_uuid(g_hAdvertiser,
201 BT_ADAPTER_LE_PACKET_ADVERTISING,
203 if (BT_ERROR_NONE != res)
205 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_add_advertising_service_uuid failed with ret[%s]",
206 CALEGetErrorMsg(res));
207 return CA_STATUS_FAILED;
210 res = bt_adapter_le_start_advertising(g_hAdvertiser, NULL, NULL, NULL);
211 if (BT_ERROR_NONE != res)
213 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_start_advertising failed with ret[%s]",
214 CALEGetErrorMsg(res));
215 return CA_STATUS_FAILED;
218 OIC_LOG(DEBUG, TAG, "OUT");
222 CAResult_t CALEStopAdvertise()
224 OIC_LOG(DEBUG, TAG, "IN");
225 if (NULL != g_hAdvertiser)
227 int ret = bt_adapter_le_stop_advertising(g_hAdvertiser);
230 OIC_LOG_V(ERROR, TAG,
231 "bt_adapter_le_stop_advertising failed with ret[%s]", CALEGetErrorMsg(ret));
234 ret = bt_adapter_le_destroy_advertiser(g_hAdvertiser);
237 OIC_LOG_V(ERROR, TAG,
238 "bt_adapter_le_destroy_advertiser failed with ret[%s]", CALEGetErrorMsg(ret));
240 g_hAdvertiser = NULL;
244 OIC_LOG(ERROR, TAG, "Advertising is not running");
245 return CA_STATUS_FAILED;
248 OIC_LOG(DEBUG, TAG, "OUT");
252 void CAStartLEGattServerThread(void *data)
254 OIC_LOG(DEBUG, TAG, "IN");
255 ca_mutex_lock(g_leServerStateMutex);
256 if (true == g_isLEGattServerStarted)
258 OIC_LOG(ERROR, TAG, "Gatt Server is already running");
259 ca_mutex_unlock(g_leServerStateMutex);
260 CATerminateLEGattServer();
264 CAResult_t ret = CAInitLEGattServer();
265 if (CA_STATUS_OK != ret)
267 OIC_LOG_V(ERROR, TAG, "CAInitLEGattService failed[%d]", ret);
268 ca_mutex_unlock(g_leServerStateMutex);
269 CATerminateLEGattServer();
273 char *serviceUUID = CA_GATT_SERVICE_UUID;
275 ret = CAAddNewLEServiceInGattServer(serviceUUID);
276 if (CA_STATUS_OK != ret)
278 OIC_LOG_V(ERROR, TAG, "CAAddNewLEServiceInGattServer failed[%d]", ret);
279 ca_mutex_unlock(g_leServerStateMutex);
280 CATerminateLEGattServer();
284 static const char charReadUUID[] = CA_GATT_RESPONSE_CHRC_UUID;
285 char charReadValue[] = {33, 44, 55, 66}; // These are initial random values
287 // For Read Characteristics.
288 ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charReadUUID, charReadValue,
289 CA_LE_INITIAL_BUF_SIZE, true);
290 if (CA_STATUS_OK != ret)
292 OIC_LOG_V(ERROR, TAG, "CAAddNewCharacteristicsToGattServer failed[%d]", ret);
293 ca_mutex_unlock(g_leServerStateMutex);
294 CATerminateLEGattServer();
298 static const char charWriteUUID[] = CA_GATT_REQUEST_CHRC_UUID;
299 char charWriteValue[] = {33, 44, 55, 66}; // These are initial random values
302 ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charWriteUUID, charWriteValue,
303 CA_LE_INITIAL_BUF_SIZE, false); // For Write Characteristics.
304 if (CA_STATUS_OK != ret )
306 OIC_LOG_V(ERROR, TAG, "CAAddNewCharacteristicsToGattServer failed[%d]", ret);
307 ca_mutex_unlock(g_leServerStateMutex);
308 CATerminateLEGattServer();
312 ret = CARegisterLEServicewithGattServer(g_gattSvcPath);
313 if (CA_STATUS_OK != ret )
315 OIC_LOG_V(ERROR, TAG, "CARegisterLEServicewithGattServer failed[%d]", ret);
316 ca_mutex_unlock(g_leServerStateMutex);
317 CATerminateLEGattServer();
321 ret = CALEStartAdvertise(serviceUUID);
322 if (CA_STATUS_OK != ret)
324 OIC_LOG_V(ERROR, TAG, "CALEStartAdvertise failed[%d]", ret);
325 ca_mutex_unlock(g_leServerStateMutex);
326 CATerminateLEGattServer();
330 g_isLEGattServerStarted = true;
332 ca_mutex_unlock(g_leServerStateMutex);
334 OIC_LOG(DEBUG, TAG, "LE Server initialization complete.");
336 GMainContext *thread_context = NULL;
338 thread_context = g_main_context_new();
340 g_eventLoop = g_main_loop_new(thread_context, FALSE);
342 g_main_context_push_thread_default(thread_context);
344 g_main_loop_run(g_eventLoop);
346 OIC_LOG(DEBUG, TAG, "OUT");
349 CAResult_t CAStopLEGattServer()
351 OIC_LOG(DEBUG, TAG, "IN");
353 ca_mutex_lock(g_leServerStateMutex);
355 if (false == g_isLEGattServerStarted)
357 OIC_LOG(ERROR, TAG, "Gatt Server is not running to stop");
358 ca_mutex_unlock(g_leServerStateMutex);
362 g_isLEGattServerStarted = false;
364 CAResult_t res = CALEStopAdvertise();
366 OIC_LOG_V(ERROR, TAG, "CALEStopAdvertise failed with ret[%d]", res);
369 res = CADeInitLEGattServer();
370 if (CA_STATUS_OK != res)
372 OIC_LOG_V(ERROR, TAG, "CADeInitLEGattService failed with ret[%d]", res);
375 GMainContext *context_event_loop = NULL;
376 // Required for waking up the thread which is running in gmain loop
377 if (NULL != g_eventLoop)
379 context_event_loop = g_main_loop_get_context(g_eventLoop);
381 if (context_event_loop)
383 OIC_LOG_V(DEBUG, TAG, "g_eventLoop context %x", context_event_loop);
384 g_main_context_wakeup(context_event_loop);
386 // Kill g main loops and kill threads
387 g_main_loop_quit(g_eventLoop);
392 OIC_LOG(ERROR, TAG, "g_eventLoop context is NULL");
395 ca_mutex_unlock(g_leServerStateMutex);
397 OIC_LOG(DEBUG, TAG, "OUT");
401 CAResult_t CAInitializeLEGattServer()
403 OIC_LOG(DEBUG, TAG, "IN");
405 CAResult_t ret = CAInitGattServerMutexVariables();
406 if (CA_STATUS_OK != ret )
408 OIC_LOG(ERROR, TAG, "CAInitGattServerMutexVariables failed!");
409 CATerminateGattServerMutexVariables();
410 return CA_SERVER_NOT_STARTED;
412 OIC_LOG(DEBUG, TAG, "OUT");
416 void CATerminateLEGattServer()
418 OIC_LOG(DEBUG, TAG, "IN");
420 // Service and characteristics path will be freed by the platform.
421 ca_mutex_lock(g_leServiceMutex);
422 g_gattSvcPath = NULL;
423 ca_mutex_unlock(g_leServiceMutex);
425 ca_mutex_lock(g_leCharacteristicMutex);
426 g_gattReadCharPath = NULL;
427 g_gattWriteCharPath = NULL;
428 ca_mutex_unlock(g_leCharacteristicMutex);
430 ca_mutex_lock(g_leServerThreadPoolMutex);
431 g_leServerThreadPool = NULL;
432 ca_mutex_unlock(g_leServerThreadPoolMutex);
434 // Terminating all mutex variables.
435 CATerminateGattServerMutexVariables();
436 OIC_LOG(DEBUG, TAG, "OUT");
439 CAResult_t CAInitGattServerMutexVariables()
441 OIC_LOG(DEBUG, TAG, "IN");
442 if (NULL == g_leServerStateMutex)
444 g_leServerStateMutex = ca_mutex_new();
445 if (NULL == g_leServerStateMutex)
447 OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
448 return CA_STATUS_FAILED;
452 if (NULL == g_leServiceMutex)
454 g_leServiceMutex = ca_mutex_new();
455 if (NULL == g_leServiceMutex)
457 OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
458 return CA_STATUS_FAILED;
462 if (NULL == g_leCharacteristicMutex)
464 g_leCharacteristicMutex = ca_mutex_new();
465 if (NULL == g_leCharacteristicMutex)
467 OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
468 return CA_STATUS_FAILED;
472 if (NULL == g_leReqRespCbMutex)
474 g_leReqRespCbMutex = ca_mutex_new();
475 if (NULL == g_leReqRespCbMutex)
477 OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
478 return CA_STATUS_FAILED;
481 OIC_LOG(DEBUG, TAG, "OUT");
485 void CATerminateGattServerMutexVariables()
487 OIC_LOG(DEBUG, TAG, "IN");
488 ca_mutex_free(g_leServerStateMutex);
489 g_leServerStateMutex = NULL;
491 ca_mutex_free(g_leServiceMutex);
492 g_leServiceMutex = NULL;
494 ca_mutex_free(g_leCharacteristicMutex);
495 g_leCharacteristicMutex = NULL;
497 ca_mutex_free(g_leReqRespCbMutex);
498 g_leReqRespCbMutex = NULL;
500 OIC_LOG(DEBUG, TAG, "OUT");
503 CAResult_t CAInitLEGattServer()
505 OIC_LOG(DEBUG, TAG, "IN");
507 int ret = bt_gatt_server_initialize();
510 OIC_LOG_V(ERROR, TAG, "bt_gatt_server_initialize failed with ret[%s]",
511 CALEGetErrorMsg(ret));
512 return CA_STATUS_FAILED;
515 bt_gatt_server_h server;
517 ret = bt_gatt_server_create(&server);
520 OIC_LOG_V(ERROR, TAG, "bt_gatt_server_create failed with ret[%s]",
521 CALEGetErrorMsg(ret));
522 return CA_STATUS_FAILED;
525 if (NULL != g_gattServer)
527 OICFree(g_gattServer);
530 g_gattServer = server;
532 OIC_LOG(DEBUG, TAG, "OUT");
536 CAResult_t CADeInitLEGattServer()
538 OIC_LOG(DEBUG, TAG, "IN");
540 int ret = bt_gatt_server_unregister_all_services(g_gattServer);
543 OIC_LOG_V(ERROR, TAG, "bt_gatt_server_unregister_all_services failed with ret[%s]",
544 CALEGetErrorMsg(ret));
545 return CA_STATUS_FAILED;
548 ret = bt_gatt_server_destroy(g_gattServer);
551 OIC_LOG_V(ERROR, TAG, "bt_gatt_server_destroy failed with ret[%s]",
552 CALEGetErrorMsg(ret));
553 return CA_STATUS_FAILED;
556 ret = bt_gatt_server_deinitialize();
559 OIC_LOG_V(ERROR, TAG, "bt_gatt_server_deinitialize failed with ret[%s]",
560 CALEGetErrorMsg(ret));
561 return CA_STATUS_FAILED;
564 OIC_LOG(DEBUG, TAG, "OUT");
568 void CASetLEServerThreadPoolHandle(ca_thread_pool_t handle)
570 OIC_LOG(DEBUG, TAG, "IN");
571 ca_mutex_lock(g_leServerThreadPoolMutex);
572 g_leServerThreadPool = handle;
573 ca_mutex_unlock(g_leServerThreadPoolMutex);
574 OIC_LOG(DEBUG, TAG, "OUT");
577 CAResult_t CAAddNewLEServiceInGattServer(const char *serviceUUID)
579 OIC_LOG(DEBUG, TAG, "IN");
581 VERIFY_NON_NULL(serviceUUID, TAG, "serviceUUID");
583 OIC_LOG_V(DEBUG, TAG, "service uuid %s", serviceUUID);
585 bt_gatt_h service = NULL;
586 bt_gatt_service_type_e type = BT_GATT_SERVICE_TYPE_PRIMARY;
588 int ret = bt_gatt_service_create(serviceUUID, type, &service);
591 OIC_LOG_V(ERROR, TAG, "bt_gatt_service_create failed with ret [%s]",
592 CALEGetErrorMsg(ret));
593 return CA_STATUS_FAILED;
598 OIC_LOG_V(DEBUG, TAG, "ServicePath obtained is %s", (char *)service);
600 ca_mutex_lock(g_leServiceMutex);
602 if (NULL != g_gattSvcPath)
604 OICFree(g_gattSvcPath);
605 g_gattSvcPath = NULL;
607 g_gattSvcPath = service;
609 ca_mutex_unlock(g_leServiceMutex);
612 OIC_LOG(DEBUG, TAG, "OUT");
616 void CALEGattRemoteCharacteristicWriteCb(char *remoteAddress, bt_gatt_server_h server,
617 bt_gatt_h charPath, int offset, char *charValue,
618 int charValueLen, void *userData)
620 OIC_LOG(DEBUG, TAG, "IN");
622 if (NULL == charValue || NULL == remoteAddress)
624 OIC_LOG(ERROR, TAG, "Param callback values are NULL");
628 OIC_LOG_V(DEBUG, TAG, "charPath = [%s] charValue = [%s] len [%d]", (char *)charPath,
629 charValue, charValueLen);
631 uint8_t *data = OICMalloc(charValueLen);
634 OIC_LOG(ERROR, TAG, "Malloc failed!");
638 memcpy(data, charValue, charValueLen);
640 ca_mutex_lock(g_leReqRespCbMutex);
641 if (NULL == g_leServerDataReceivedCallback)
643 OIC_LOG(ERROR, TAG, "gReqRespCallback is NULL!");
644 ca_mutex_unlock(g_leReqRespCbMutex);
649 OIC_LOG(DEBUG, TAG, "Sending data up !");
650 uint32_t sentLength = 0;
651 g_leServerDataReceivedCallback(remoteAddress, data, charValueLen,
653 ca_mutex_unlock(g_leReqRespCbMutex);
655 OIC_LOG(DEBUG, TAG, "OUT");
658 CAResult_t CARegisterLEServicewithGattServer(const bt_gatt_h svcPath)
660 OIC_LOG(DEBUG, TAG, "IN");
662 VERIFY_NON_NULL(svcPath, TAG, "svcPath");
664 OIC_LOG_V(DEBUG, TAG, "svcPath:%s", svcPath);
666 int ret = bt_gatt_server_register_service(g_gattServer, svcPath);
669 OIC_LOG_V(ERROR, TAG, "bt_gatt_server_register_service failed with ret[%s]",
670 CALEGetErrorMsg(ret));
671 return CA_STATUS_FAILED;
674 ret = bt_gatt_server_set_value_changed_cb(g_gattWriteCharPath,
675 CALEGattRemoteCharacteristicWriteCb, NULL);
679 OIC_LOG_V(ERROR, TAG, "bt_gatt_server_set_value_changed_cb failed with ret[%s]",
680 CALEGetErrorMsg(ret));
681 return CA_STATUS_FAILED;
684 OIC_LOG(DEBUG, TAG, "OUT");
688 CAResult_t CAAddNewCharacteristicsToGattServer(const bt_gatt_h svcPath, const char *charUUID,
689 const char *charValue, int charValueLen, bool read)
692 OIC_LOG(DEBUG, TAG, "IN");
694 int permissions = BT_GATT_PERMISSION_READ | BT_GATT_PERMISSION_WRITE;
695 int properties = BT_GATT_PROPERTY_WRITE | BT_GATT_PROPERTY_NOTIFY;
698 properties = BT_GATT_PROPERTY_NOTIFY | BT_GATT_PROPERTY_READ;
702 properties = BT_GATT_PROPERTY_WRITE | BT_GATT_PROPERTY_READ;
707 int ret = bt_gatt_characteristic_create(charUUID, permissions, properties, charValue,
708 charValueLen, &charPath);
710 if (0 != ret || NULL == charPath)
712 OIC_LOG_V(ERROR, TAG,
713 "bt_gatt_add_characteristic failed with ret [%s]", CALEGetErrorMsg(ret));
714 return CA_STATUS_FAILED;
717 OIC_LOG_V(DEBUG, TAG,
718 "bt_gatt_characteristic_create charPath obtained: %s", (char *)charPath);
722 ret = bt_gatt_server_set_notification_state_change_cb(charPath, CALENotificationCb, NULL);
725 OIC_LOG_V(ERROR, TAG,
726 "bt_gatt_server_set_notification_state_change_cb failed with ret[%s]",
727 CALEGetErrorMsg(ret));
728 return CA_STATUS_FAILED;
732 ret = bt_gatt_service_add_characteristic(svcPath, charPath);
735 OIC_LOG_V(ERROR, TAG,
736 "bt_gatt_service_add_characteristic failed with ret[%s]",
737 CALEGetErrorMsg(ret));
738 return CA_STATUS_FAILED;
741 ca_mutex_lock(g_leCharacteristicMutex);
745 char desc_value[2] = {1, 0}; // Notification enabled.
746 bt_gatt_h descriptor = NULL;
747 permissions = BT_GATT_PERMISSION_READ | BT_GATT_PERMISSION_WRITE;
748 ret = bt_gatt_descriptor_create(CA_GATT_CONFIGURATION_DESC_UUID,
749 permissions, desc_value, sizeof(desc_value), &descriptor);
752 OIC_LOG_V(ERROR, TAG,
753 "bt_gatt_descriptor_create failed with ret[%s]",
754 CALEGetErrorMsg(ret));
755 return CA_STATUS_FAILED;
758 ret = bt_gatt_characteristic_add_descriptor(charPath, descriptor);
761 OIC_LOG_V(ERROR, TAG,
762 "bt_gatt_characteristic_add_descriptor failed with ret[%s]",
763 CALEGetErrorMsg(ret));
764 return CA_STATUS_FAILED;
767 if (NULL != g_gattReadCharPath)
769 OICFree(g_gattReadCharPath);
770 g_gattReadCharPath = NULL;
772 g_gattReadCharPath = charPath;
777 if (NULL != g_gattWriteCharPath)
779 OICFree(g_gattWriteCharPath);
780 g_gattWriteCharPath = NULL;
782 g_gattWriteCharPath = charPath;
785 ca_mutex_unlock(g_leCharacteristicMutex);
787 OIC_LOG(DEBUG, TAG, "OUT");
791 CAResult_t CAUpdateCharacteristicsToGattClient(const char *address, const uint8_t *charValue,
792 uint32_t charValueLen)
794 OIC_LOG(DEBUG, TAG, "IN");
796 VERIFY_NON_NULL(charValue, TAG, "charValue");
797 VERIFY_NON_NULL(address, TAG, "address");
799 OIC_LOG_V(DEBUG, TAG, "Client's Unicast address for sending data [%s]", address);
801 ca_mutex_lock(g_leCharacteristicMutex);
803 if (NULL == g_gattReadCharPath)
805 OIC_LOG(ERROR, TAG, "g_gattReadCharPath is NULL");
806 ca_mutex_unlock(g_leCharacteristicMutex);
807 return CA_STATUS_FAILED;
810 int ret = bt_gatt_set_value(g_gattReadCharPath, (char *)charValue, charValueLen);
813 OIC_LOG_V(ERROR, TAG,
814 "bt_gatt_set_value failed with return [%s]", CALEGetErrorMsg(ret));
815 ca_mutex_unlock(g_leCharacteristicMutex);
816 return CA_STATUS_FAILED;
819 ret = bt_gatt_server_notify(g_gattReadCharPath, false, CALEServerNotificationSentCB,
823 OIC_LOG_V(ERROR, TAG,
824 "bt_gatt_server_notify failed with return [%s]", CALEGetErrorMsg(ret));
825 ca_mutex_unlock(g_leCharacteristicMutex);
826 return CA_STATUS_FAILED;
829 ca_mutex_unlock(g_leCharacteristicMutex);
831 OIC_LOG(DEBUG, TAG, "OUT");
835 CAResult_t CAUpdateCharacteristicsToAllGattClients(const uint8_t *charValue, uint32_t charValueLen)
837 OIC_LOG(DEBUG, TAG, "IN");
839 VERIFY_NON_NULL(charValue, TAG, "charValue");
841 ca_mutex_lock(g_leCharacteristicMutex);
843 if (NULL == g_gattReadCharPath)
845 OIC_LOG(ERROR, TAG, "g_gattReadCharPath is NULL");
846 ca_mutex_unlock(g_leCharacteristicMutex);
847 return CA_STATUS_FAILED;
850 int ret = bt_gatt_set_value(g_gattReadCharPath, (char *)charValue, charValueLen);
853 OIC_LOG_V(ERROR, TAG, "bt_gatt_set_value failed with return[%s]", CALEGetErrorMsg(ret));
854 ca_mutex_unlock(g_leCharacteristicMutex);
855 return CA_STATUS_FAILED;
858 ret = bt_gatt_server_notify(g_gattReadCharPath, false, CALEServerNotificationSentCB,
862 OIC_LOG_V(ERROR, TAG,
863 "bt_gatt_server_notify failed with return[%s]", CALEGetErrorMsg(ret));
864 ca_mutex_unlock(g_leCharacteristicMutex);
865 return CA_STATUS_FAILED;
868 ca_mutex_unlock(g_leCharacteristicMutex);
870 OIC_LOG(DEBUG, TAG, "OUT");
874 void CASetLEReqRespServerCallback(CABLEDataReceivedCallback callback)
876 OIC_LOG(DEBUG, TAG, "IN");
878 ca_mutex_lock(g_leReqRespCbMutex);
879 g_leServerDataReceivedCallback = callback;
880 ca_mutex_unlock(g_leReqRespCbMutex);
882 OIC_LOG(DEBUG, TAG, "OUT");
885 void CASetBLEServerErrorHandleCallback(CABLEErrorHandleCallback callback)
887 g_serverErrorCallback = callback;