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 void CALEGattServerConnectionStateChanged(bool connected, const char *remoteAddress)
119 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address");
123 OIC_LOG_V(DEBUG, TAG, "Connected to [%s]", remoteAddress);
127 OIC_LOG_V(DEBUG, TAG, "Disconnected from [%s]", remoteAddress);
131 void CALEServerNotificationSentCB(int result, char *remote_address, bt_gatt_server_h server,
132 bt_gatt_h characteristic, bool completed, void *user_data)
134 OIC_LOG_V(DEBUG, TAG, "Notification to the device[%s] result[%d]", remote_address, result);
137 CAResult_t CAStartLEGattServer()
139 OIC_LOG(DEBUG, TAG, "IN");
141 ca_mutex_lock(g_leServerThreadPoolMutex);
142 if (NULL == g_leServerThreadPool)
144 OIC_LOG(ERROR, TAG, "g_leServerThreadPool is NULL");
145 ca_mutex_unlock(g_leServerThreadPoolMutex);
146 return CA_STATUS_FAILED;
149 CAResult_t ret = ca_thread_pool_add_task(g_leServerThreadPool, CAStartLEGattServerThread,
151 if (CA_STATUS_OK != ret)
153 OIC_LOG_V(ERROR, TAG, "ca_thread_pool_add_task failed with ret [%d]", ret);
154 ca_mutex_unlock(g_leServerThreadPoolMutex);
155 return CA_STATUS_FAILED;
158 ca_mutex_unlock(g_leServerThreadPoolMutex);
159 OIC_LOG(DEBUG, TAG, "OUT");
163 void CALENotificationCb(bool notify, bt_gatt_server_h server, bt_gatt_h gatt_handle,
166 OIC_LOG(DEBUG, TAG, "IN");
169 OIC_LOG(DEBUG, TAG, "Notification is subscribed by the client");
171 OIC_LOG(DEBUG, TAG, "OUT");
174 CAResult_t CALEStartAdvertise(const char *serviceUUID)
176 OIC_LOG(DEBUG, TAG, "IN");
178 int res = bt_adapter_le_create_advertiser(&g_hAdvertiser);
179 if (NULL == g_hAdvertiser || BT_ERROR_NONE != res)
181 OIC_LOG_V(ERROR, TAG, "g_hAdvertiser is NULL/ Result is %d", res);
182 return CA_STATUS_FAILED;
185 res = bt_adapter_le_add_advertising_service_uuid(g_hAdvertiser,
186 BT_ADAPTER_LE_PACKET_ADVERTISING,
188 if (BT_ERROR_NONE != res)
190 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_add_advertising_service_uuid failed with ret[%s]",
191 CALEGetErrorMsg(res));
192 return CA_STATUS_FAILED;
195 res = bt_adapter_le_set_advertising_device_name(g_hAdvertiser,
196 BT_ADAPTER_LE_PACKET_SCAN_RESPONSE, true);
197 if (BT_ERROR_NONE != res)
199 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_set_advertising_device_name failed with ret[%s]",
200 CALEGetErrorMsg(res));
201 return CA_STATUS_FAILED;
204 res = bt_adapter_le_start_advertising_new(g_hAdvertiser, NULL, NULL);
205 if (BT_ERROR_NONE != res)
207 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_start_advertising_new failed with ret[%s]",
208 CALEGetErrorMsg(res));
209 return CA_STATUS_FAILED;
212 OIC_LOG(DEBUG, TAG, "OUT");
216 CAResult_t CALEStopAdvertise()
218 OIC_LOG(DEBUG, TAG, "IN");
219 if (NULL != g_hAdvertiser)
221 int ret = bt_adapter_le_stop_advertising(g_hAdvertiser);
224 OIC_LOG_V(ERROR, TAG,
225 "bt_adapter_le_stop_advertising failed with ret[%s]", CALEGetErrorMsg(ret));
228 ret = bt_adapter_le_destroy_advertiser(g_hAdvertiser);
231 OIC_LOG_V(ERROR, TAG,
232 "bt_adapter_le_destroy_advertiser failed with ret[%s]", CALEGetErrorMsg(ret));
234 g_hAdvertiser = NULL;
238 OIC_LOG(ERROR, TAG, "Advertising is not running");
239 return CA_STATUS_FAILED;
242 OIC_LOG(DEBUG, TAG, "OUT");
246 void CAStartLEGattServerThread(void *data)
248 OIC_LOG(DEBUG, TAG, "IN");
249 ca_mutex_lock(g_leServerStateMutex);
250 if (true == g_isLEGattServerStarted)
252 OIC_LOG(ERROR, TAG, "Gatt Server is already running");
253 ca_mutex_unlock(g_leServerStateMutex);
254 CATerminateLEGattServer();
258 CAResult_t ret = CAInitLEGattServer();
259 if (CA_STATUS_OK != ret)
261 OIC_LOG_V(ERROR, TAG, "CAInitLEGattService failed[%d]", ret);
262 ca_mutex_unlock(g_leServerStateMutex);
263 CATerminateLEGattServer();
267 char *serviceUUID = CA_GATT_SERVICE_UUID;
269 ret = CAAddNewLEServiceInGattServer(serviceUUID);
270 if (CA_STATUS_OK != ret)
272 OIC_LOG_V(ERROR, TAG, "CAAddNewLEServiceInGattServer failed[%d]", ret);
273 ca_mutex_unlock(g_leServerStateMutex);
274 CATerminateLEGattServer();
278 static const char charReadUUID[] = CA_GATT_RESPONSE_CHRC_UUID;
279 char charReadValue[] = {33, 44, 55, 66}; // These are initial random values
281 // For Read Characteristics.
282 ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charReadUUID, charReadValue,
283 CA_LE_INITIAL_BUF_SIZE, true);
284 if (CA_STATUS_OK != ret)
286 OIC_LOG_V(ERROR, TAG, "CAAddNewCharacteristicsToGattServer failed[%d]", ret);
287 ca_mutex_unlock(g_leServerStateMutex);
288 CATerminateLEGattServer();
292 static const char charWriteUUID[] = CA_GATT_REQUEST_CHRC_UUID;
293 char charWriteValue[] = {33, 44, 55, 66}; // These are initial random values
296 ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charWriteUUID, charWriteValue,
297 CA_LE_INITIAL_BUF_SIZE, false); // For Write Characteristics.
298 if (CA_STATUS_OK != ret )
300 OIC_LOG_V(ERROR, TAG, "CAAddNewCharacteristicsToGattServer failed[%d]", ret);
301 ca_mutex_unlock(g_leServerStateMutex);
302 CATerminateLEGattServer();
306 ret = CARegisterLEServicewithGattServer(g_gattSvcPath);
307 if (CA_STATUS_OK != ret )
309 OIC_LOG_V(ERROR, TAG, "CARegisterLEServicewithGattServer failed[%d]", ret);
310 ca_mutex_unlock(g_leServerStateMutex);
311 CATerminateLEGattServer();
315 ret = CALEStartAdvertise(serviceUUID);
316 if (CA_STATUS_OK != ret)
318 OIC_LOG_V(ERROR, TAG, "CALEStartAdvertise failed[%d]", ret);
319 ca_mutex_unlock(g_leServerStateMutex);
320 CATerminateLEGattServer();
324 g_isLEGattServerStarted = true;
326 ca_mutex_unlock(g_leServerStateMutex);
328 OIC_LOG(DEBUG, TAG, "LE Server initialization complete.");
330 GMainContext *thread_context = NULL;
332 thread_context = g_main_context_new();
334 g_eventLoop = g_main_loop_new(thread_context, FALSE);
336 g_main_context_push_thread_default(thread_context);
338 g_main_loop_run(g_eventLoop);
340 OIC_LOG(DEBUG, TAG, "OUT");
343 CAResult_t CAStopLEGattServer()
345 OIC_LOG(DEBUG, TAG, "IN");
347 ca_mutex_lock(g_leServerStateMutex);
349 if (false == g_isLEGattServerStarted)
351 OIC_LOG(ERROR, TAG, "Gatt Server is not running to stop");
352 ca_mutex_unlock(g_leServerStateMutex);
356 g_isLEGattServerStarted = false;
358 CAResult_t res = CALEStopAdvertise();
360 OIC_LOG_V(ERROR, TAG, "CALEStopAdvertise failed with ret[%d]", res);
363 res = CADeInitLEGattServer();
364 if (CA_STATUS_OK != res)
366 OIC_LOG_V(ERROR, TAG, "CADeInitLEGattService failed with ret[%d]", res);
369 GMainContext *context_event_loop = NULL;
370 // Required for waking up the thread which is running in gmain loop
371 if (NULL != g_eventLoop)
373 context_event_loop = g_main_loop_get_context(g_eventLoop);
375 if (context_event_loop)
377 OIC_LOG_V(DEBUG, TAG, "g_eventLoop context %x", context_event_loop);
378 g_main_context_wakeup(context_event_loop);
380 // Kill g main loops and kill threads
381 g_main_loop_quit(g_eventLoop);
386 OIC_LOG(ERROR, TAG, "g_eventLoop context is NULL");
389 ca_mutex_unlock(g_leServerStateMutex);
391 OIC_LOG(DEBUG, TAG, "OUT");
395 CAResult_t CAInitializeLEGattServer()
397 OIC_LOG(DEBUG, TAG, "IN");
399 CAResult_t ret = CAInitGattServerMutexVariables();
400 if (CA_STATUS_OK != ret )
402 OIC_LOG(ERROR, TAG, "CAInitGattServerMutexVariables failed!");
403 CATerminateGattServerMutexVariables();
404 return CA_SERVER_NOT_STARTED;
406 OIC_LOG(DEBUG, TAG, "OUT");
410 void CATerminateLEGattServer()
412 OIC_LOG(DEBUG, TAG, "IN");
414 // Service and characteristics path will be freed by the platform.
415 ca_mutex_lock(g_leServiceMutex);
416 g_gattSvcPath = NULL;
417 ca_mutex_unlock(g_leServiceMutex);
419 ca_mutex_lock(g_leCharacteristicMutex);
420 g_gattReadCharPath = NULL;
421 g_gattWriteCharPath = NULL;
422 ca_mutex_unlock(g_leCharacteristicMutex);
424 ca_mutex_lock(g_leServerThreadPoolMutex);
425 g_leServerThreadPool = NULL;
426 ca_mutex_unlock(g_leServerThreadPoolMutex);
428 // Terminating all mutex variables.
429 CATerminateGattServerMutexVariables();
430 OIC_LOG(DEBUG, TAG, "OUT");
433 CAResult_t CAInitGattServerMutexVariables()
435 OIC_LOG(DEBUG, TAG, "IN");
436 if (NULL == g_leServerStateMutex)
438 g_leServerStateMutex = ca_mutex_new();
439 if (NULL == g_leServerStateMutex)
441 OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
442 return CA_STATUS_FAILED;
446 if (NULL == g_leServiceMutex)
448 g_leServiceMutex = ca_mutex_new();
449 if (NULL == g_leServiceMutex)
451 OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
452 return CA_STATUS_FAILED;
456 if (NULL == g_leCharacteristicMutex)
458 g_leCharacteristicMutex = ca_mutex_new();
459 if (NULL == g_leCharacteristicMutex)
461 OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
462 return CA_STATUS_FAILED;
466 if (NULL == g_leReqRespCbMutex)
468 g_leReqRespCbMutex = ca_mutex_new();
469 if (NULL == g_leReqRespCbMutex)
471 OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
472 return CA_STATUS_FAILED;
475 OIC_LOG(DEBUG, TAG, "OUT");
479 void CATerminateGattServerMutexVariables()
481 OIC_LOG(DEBUG, TAG, "IN");
482 ca_mutex_free(g_leServerStateMutex);
483 g_leServerStateMutex = NULL;
485 ca_mutex_free(g_leServiceMutex);
486 g_leServiceMutex = NULL;
488 ca_mutex_free(g_leCharacteristicMutex);
489 g_leCharacteristicMutex = NULL;
491 ca_mutex_free(g_leReqRespCbMutex);
492 g_leReqRespCbMutex = NULL;
494 OIC_LOG(DEBUG, TAG, "OUT");
497 CAResult_t CAInitLEGattServer()
499 OIC_LOG(DEBUG, TAG, "IN");
501 int ret = bt_gatt_server_initialize();
504 OIC_LOG_V(ERROR, TAG, "bt_gatt_server_initialize failed with ret[%s]",
505 CALEGetErrorMsg(ret));
506 return CA_STATUS_FAILED;
509 bt_gatt_server_h server;
511 ret = bt_gatt_server_create(&server);
514 OIC_LOG_V(ERROR, TAG, "bt_gatt_server_create failed with ret[%s]",
515 CALEGetErrorMsg(ret));
516 return CA_STATUS_FAILED;
519 if (NULL != g_gattServer)
521 OICFree(g_gattServer);
524 g_gattServer = server;
526 OIC_LOG(DEBUG, TAG, "OUT");
530 CAResult_t CADeInitLEGattServer()
532 OIC_LOG(DEBUG, TAG, "IN");
534 int ret = bt_gatt_server_unregister_all_services(g_gattServer);
537 OIC_LOG_V(ERROR, TAG, "bt_gatt_server_unregister_all_services failed with ret[%s]",
538 CALEGetErrorMsg(ret));
539 return CA_STATUS_FAILED;
542 ret = bt_gatt_server_destroy(g_gattServer);
545 OIC_LOG_V(ERROR, TAG, "bt_gatt_server_destroy failed with ret[%s]",
546 CALEGetErrorMsg(ret));
547 return CA_STATUS_FAILED;
550 ret = bt_gatt_server_deinitialize();
553 OIC_LOG_V(ERROR, TAG, "bt_gatt_server_deinitialize failed with ret[%s]",
554 CALEGetErrorMsg(ret));
555 return CA_STATUS_FAILED;
558 OIC_LOG(DEBUG, TAG, "OUT");
562 void CASetLEServerThreadPoolHandle(ca_thread_pool_t handle)
564 OIC_LOG(DEBUG, TAG, "IN");
565 ca_mutex_lock(g_leServerThreadPoolMutex);
566 g_leServerThreadPool = handle;
567 ca_mutex_unlock(g_leServerThreadPoolMutex);
568 OIC_LOG(DEBUG, TAG, "OUT");
571 CAResult_t CAAddNewLEServiceInGattServer(const char *serviceUUID)
573 OIC_LOG(DEBUG, TAG, "IN");
575 VERIFY_NON_NULL(serviceUUID, TAG, "serviceUUID");
577 OIC_LOG_V(DEBUG, TAG, "service uuid %s", serviceUUID);
579 bt_gatt_h service = NULL;
580 bt_gatt_service_type_e type = BT_GATT_SERVICE_TYPE_PRIMARY;
582 int ret = bt_gatt_service_create(serviceUUID, type, &service);
585 OIC_LOG_V(ERROR, TAG, "bt_gatt_service_create failed with ret [%s]",
586 CALEGetErrorMsg(ret));
587 return CA_STATUS_FAILED;
592 OIC_LOG_V(DEBUG, TAG, "ServicePath obtained is %s", (char *)service);
594 ca_mutex_lock(g_leServiceMutex);
596 if (NULL != g_gattSvcPath)
598 OICFree(g_gattSvcPath);
599 g_gattSvcPath = NULL;
601 g_gattSvcPath = service;
603 ca_mutex_unlock(g_leServiceMutex);
606 OIC_LOG(DEBUG, TAG, "OUT");
610 void CALEGattRemoteCharacteristicWriteCb(char *remoteAddress, bt_gatt_server_h server,
611 bt_gatt_h charPath, int offset, char *charValue,
612 int charValueLen, void *userData)
614 OIC_LOG(DEBUG, TAG, "IN");
616 if (NULL == charValue || NULL == remoteAddress)
618 OIC_LOG(ERROR, TAG, "Param callback values are NULL");
622 OIC_LOG_V(DEBUG, TAG, "charPath = [%s] charValue = [%s] len [%d]", (char *)charPath,
623 charValue, charValueLen);
625 uint8_t *data = OICMalloc(charValueLen);
628 OIC_LOG(ERROR, TAG, "Malloc failed!");
632 memcpy(data, charValue, charValueLen);
634 ca_mutex_lock(g_leReqRespCbMutex);
635 if (NULL == g_leServerDataReceivedCallback)
637 OIC_LOG(ERROR, TAG, "gReqRespCallback is NULL!");
638 ca_mutex_unlock(g_leReqRespCbMutex);
643 OIC_LOG(DEBUG, TAG, "Sending data up !");
644 uint32_t sentLength = 0;
645 g_leServerDataReceivedCallback(remoteAddress, data, charValueLen,
647 ca_mutex_unlock(g_leReqRespCbMutex);
649 OIC_LOG(DEBUG, TAG, "OUT");
652 CAResult_t CARegisterLEServicewithGattServer(const bt_gatt_h svcPath)
654 OIC_LOG(DEBUG, TAG, "IN");
656 VERIFY_NON_NULL(svcPath, TAG, "svcPath");
658 OIC_LOG_V(DEBUG, TAG, "svcPath:%s", svcPath);
660 int ret = bt_gatt_server_register_service(g_gattServer, svcPath);
663 OIC_LOG_V(ERROR, TAG, "bt_gatt_server_register_service failed with ret[%s]",
664 CALEGetErrorMsg(ret));
665 return CA_STATUS_FAILED;
668 ret = bt_gatt_server_set_value_changed_cb(g_gattWriteCharPath,
669 CALEGattRemoteCharacteristicWriteCb, NULL);
673 OIC_LOG_V(ERROR, TAG, "bt_gatt_server_set_value_changed_cb failed with ret[%s]",
674 CALEGetErrorMsg(ret));
675 return CA_STATUS_FAILED;
678 OIC_LOG(DEBUG, TAG, "OUT");
682 CAResult_t CAAddNewCharacteristicsToGattServer(const bt_gatt_h svcPath, const char *charUUID,
683 const char *charValue, int charValueLen, bool read)
686 OIC_LOG(DEBUG, TAG, "IN");
688 int permissions = BT_GATT_PERMISSION_READ | BT_GATT_PERMISSION_WRITE;
692 properties = BT_GATT_PROPERTY_NOTIFY | BT_GATT_PROPERTY_READ;
696 properties = BT_GATT_PROPERTY_WRITE | BT_GATT_PROPERTY_READ;
701 int ret = bt_gatt_characteristic_create(charUUID, permissions, properties, charValue,
702 charValueLen, &charPath);
704 if (0 != ret || NULL == charPath)
706 OIC_LOG_V(ERROR, TAG,
707 "bt_gatt_add_characteristic failed with ret [%s]", CALEGetErrorMsg(ret));
708 return CA_STATUS_FAILED;
711 OIC_LOG_V(DEBUG, TAG,
712 "bt_gatt_characteristic_create charPath obtained: %s", (char *)charPath);
716 ret = bt_gatt_server_set_notification_state_change_cb(charPath, CALENotificationCb, NULL);
719 OIC_LOG_V(ERROR, TAG,
720 "bt_gatt_server_set_notification_state_change_cb failed with ret[%s]",
721 CALEGetErrorMsg(ret));
722 return CA_STATUS_FAILED;
726 ret = bt_gatt_service_add_characteristic(svcPath, charPath);
729 OIC_LOG_V(ERROR, TAG,
730 "bt_gatt_service_add_characteristic failed with ret[%s]",
731 CALEGetErrorMsg(ret));
732 return CA_STATUS_FAILED;
735 ca_mutex_lock(g_leCharacteristicMutex);
739 char desc_value[2] = {1, 0}; // Notification enabled.
740 bt_gatt_h descriptor = NULL;
741 permissions = BT_GATT_PERMISSION_READ | BT_GATT_PERMISSION_WRITE;
742 ret = bt_gatt_descriptor_create(CA_GATT_CONFIGURATION_DESC_UUID, permissions,
743 desc_value, sizeof(desc_value),
747 OIC_LOG_V(ERROR, TAG,
748 "bt_gatt_descriptor_create failed with ret[%s]",
749 CALEGetErrorMsg(ret));
750 return CA_STATUS_FAILED;
753 ret = bt_gatt_characteristic_add_descriptor(charPath, descriptor);
756 OIC_LOG_V(ERROR, TAG,
757 "bt_gatt_characteristic_add_descriptor failed with ret[%s]",
758 CALEGetErrorMsg(ret));
759 return CA_STATUS_FAILED;
762 if (NULL != g_gattReadCharPath)
764 OICFree(g_gattReadCharPath);
765 g_gattReadCharPath = NULL;
767 g_gattReadCharPath = charPath;
772 if (NULL != g_gattWriteCharPath)
774 OICFree(g_gattWriteCharPath);
775 g_gattWriteCharPath = NULL;
777 g_gattWriteCharPath = charPath;
780 ca_mutex_unlock(g_leCharacteristicMutex);
782 OIC_LOG(DEBUG, TAG, "OUT");
786 CAResult_t CAUpdateCharacteristicsToGattClient(const char *address, const uint8_t *charValue,
787 uint32_t charValueLen)
789 OIC_LOG(DEBUG, TAG, "IN");
791 VERIFY_NON_NULL(charValue, TAG, "charValue");
792 VERIFY_NON_NULL(address, TAG, "address");
794 OIC_LOG_V(DEBUG, TAG, "Client's Unicast address for sending data [%s]", address);
796 ca_mutex_lock(g_leCharacteristicMutex);
798 if (NULL == g_gattReadCharPath)
800 OIC_LOG(ERROR, TAG, "g_gattReadCharPath is NULL");
801 ca_mutex_unlock(g_leCharacteristicMutex);
802 return CA_STATUS_FAILED;
805 int ret = bt_gatt_set_value(g_gattReadCharPath, (char *)charValue, charValueLen);
808 OIC_LOG_V(ERROR, TAG,
809 "bt_gatt_set_value failed with return [%s]", CALEGetErrorMsg(ret));
810 ca_mutex_unlock(g_leCharacteristicMutex);
811 return CA_STATUS_FAILED;
814 ret = bt_gatt_server_notify(g_gattReadCharPath, false, CALEServerNotificationSentCB,
818 OIC_LOG_V(ERROR, TAG,
819 "bt_gatt_server_notify failed with return [%s]", CALEGetErrorMsg(ret));
820 ca_mutex_unlock(g_leCharacteristicMutex);
821 return CA_STATUS_FAILED;
824 ca_mutex_unlock(g_leCharacteristicMutex);
826 OIC_LOG(DEBUG, TAG, "OUT");
830 CAResult_t CAUpdateCharacteristicsToAllGattClients(const uint8_t *charValue, uint32_t charValueLen)
832 OIC_LOG(DEBUG, TAG, "IN");
834 VERIFY_NON_NULL(charValue, TAG, "charValue");
836 ca_mutex_lock(g_leCharacteristicMutex);
838 if (NULL == g_gattReadCharPath)
840 OIC_LOG(ERROR, TAG, "g_gattReadCharPath is NULL");
841 ca_mutex_unlock(g_leCharacteristicMutex);
842 return CA_STATUS_FAILED;
845 int ret = bt_gatt_set_value(g_gattReadCharPath, (char *)charValue, charValueLen);
848 OIC_LOG_V(ERROR, TAG, "bt_gatt_set_value failed with return[%s]", CALEGetErrorMsg(ret));
849 ca_mutex_unlock(g_leCharacteristicMutex);
850 return CA_STATUS_FAILED;
853 ret = bt_gatt_server_notify(g_gattReadCharPath, false, CALEServerNotificationSentCB,
857 OIC_LOG_V(ERROR, TAG,
858 "bt_gatt_server_notify failed with return[%s]", CALEGetErrorMsg(ret));
859 ca_mutex_unlock(g_leCharacteristicMutex);
860 return CA_STATUS_FAILED;
863 ca_mutex_unlock(g_leCharacteristicMutex);
865 OIC_LOG(DEBUG, TAG, "OUT");
869 void CASetLEReqRespServerCallback(CABLEDataReceivedCallback callback)
871 OIC_LOG(DEBUG, TAG, "IN");
873 ca_mutex_lock(g_leReqRespCbMutex);
874 g_leServerDataReceivedCallback = callback;
875 ca_mutex_unlock(g_leReqRespCbMutex);
877 OIC_LOG(DEBUG, TAG, "OUT");
880 void CASetBLEServerErrorHandleCallback(CABLEErrorHandleCallback callback)
882 g_serverErrorCallback = callback;