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 platform for LE transport.
48 #define CA_BLE_SERVICE_UUID "713D0000-503E-4C75-BA94-3148F18D941E"
51 * @def CA_BLE_INITIAL_BUF_SIZE
52 * @brief Initial buffer size for Gatt Server.
54 #define CA_BLE_INITIAL_BUF_SIZE 512
58 * @brief attribute handler for OIC server attribute.
60 static char *g_gattSvcPath = NULL;
63 * @var g_gattReadCharPath
64 * @brief attribute handler for readCharacteristic of OIC server
66 static char *g_gattReadCharPath = NULL;
69 * @var g_gattWriteCharPath
70 * @brief attribute handler for writeCharacteristic of OIC server
72 static char *g_gattWriteCharPath = NULL;
76 * @brief handler for OIC advertiser.
78 static bt_advertiser_h g_hAdvertiser = NULL;
81 * @var g_bleServerDataReceivedCallback
82 * @brief Maintains the callback to be notified on receival of network packets from other
85 static CABLEServerDataReceivedCallback g_bleServerDataReceivedCallback = NULL;
88 * @var g_isBleGattServerStarted
89 * @brief Boolean variable to keep the state of the GATTServer
91 static bool g_isBleGattServerStarted = false;
94 * @var g_bleServerStateMutex
95 * @brief Mutex to synchronize the calls to be done to the platform from GATTServer
96 * interfaces from different threads.
98 static ca_mutex g_bleServerStateMutex = NULL;
101 * @var g_bleCharacteristicMutex
102 * @brief Mutex to synchronize writing operations on the characteristics.
104 static ca_mutex g_bleCharacteristicMutex = NULL;
107 * @var g_bleServiceMutex
108 * @brief Mutex to synchronize to create the OIC service..
110 static ca_mutex g_bleServiceMutex = NULL;
113 * @var g_bleReqRespCbMutex
114 * @brief Mutex to synchronize access to the requestResponse callback to be called
115 * when the data needs to be sent from GATTClient.
117 static ca_mutex g_bleReqRespCbMutex = NULL;
120 * @var g_bleServerThreadPoolMutex
121 * @brief Mutex to synchronize the task to be pushed to thread pool.
123 static ca_mutex g_bleServerThreadPoolMutex = NULL;
127 * @brief gmainLoop to manage the threads to receive the callback from the platfrom.
129 static GMainLoop *g_eventLoop = NULL;
132 * @var g_bleServerThreadPool
133 * @brief reference to threadpool
135 static ca_thread_pool_t g_bleServerThreadPool = NULL;
137 void CABleGattServerConnectionStateChangedCb(int result, bool connected,
138 const char *remoteAddress, void *userData)
140 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
142 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "CABleGattConnectionStateChangedCb result[%d]", result);
144 VERIFY_NON_NULL_VOID(remoteAddress, TZ_BLE_SERVER_TAG, "remote address is NULL");
148 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "Connected to [%s]", remoteAddress);
150 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
153 CAResult_t CAStartBleGattServer()
155 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
157 CAResult_t ret = CAInitGattServerMutexVariables();
158 if (CA_STATUS_OK != ret )
160 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "CAInitGattServerMutexVariables failed!");
161 CATerminateGattServerMutexVariables();
162 return CA_SERVER_NOT_STARTED;
165 ca_mutex_lock(g_bleServerThreadPoolMutex);
166 if (NULL == g_bleServerThreadPool)
168 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "g_bleServerThreadPool is NULL");
169 ca_mutex_unlock(g_bleServerThreadPoolMutex);
170 return CA_STATUS_FAILED;
173 ret = ca_thread_pool_add_task(g_bleServerThreadPool, CAStartBleGattServerThread,
175 if (CA_STATUS_OK != ret)
177 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "ca_thread_pool_add_task failed with ret [%d]", ret);
178 ca_mutex_unlock(g_bleServerThreadPoolMutex);
179 return CA_STATUS_FAILED;
182 ca_mutex_unlock(g_bleServerThreadPoolMutex);
183 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
187 void CAStartBleGattServerThread(void *data)
189 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
190 ca_mutex_lock(g_bleServerStateMutex);
191 if (true == g_isBleGattServerStarted)
193 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "Gatt Server is already running");
194 ca_mutex_unlock(g_bleServerStateMutex);
195 CATerminateBleGattServer();
199 CAResult_t ret = CAInitBleGattService();
200 if (CA_STATUS_OK != ret )
202 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "_bt_gatt_init_service failed");
203 ca_mutex_unlock(g_bleServerStateMutex);
204 CATerminateBleGattServer();
208 sleep(5); // Sleep is must because of the platform issue.
210 char *serviceUUID = CA_BLE_SERVICE_UUID;
212 ret = CAAddNewBleServiceInGattServer(serviceUUID);
213 if (CA_STATUS_OK != ret )
215 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "CAAddNewBleServiceInGattServer failed");
216 ca_mutex_unlock(g_bleServerStateMutex);
217 CATerminateBleGattServer();
221 char *charReadUUID = CA_BLE_READ_CHAR_UUID;
222 char charReadValue[] = {33, 44, 55, 66}; // These are initial random values
224 ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charReadUUID, charReadValue,
225 CA_BLE_INITIAL_BUF_SIZE, true); // For Read Characteristics.
226 if (CA_STATUS_OK != ret )
228 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "CAAddNewCharacteristicsToGattServer failed");
229 ca_mutex_unlock(g_bleServerStateMutex);
230 CATerminateBleGattServer();
234 char *charWriteUUID = CA_BLE_WRITE_CHAR_UUID;
235 char charWriteValue[] = {33, 44, 55, 66}; // These are initial random values
238 ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charWriteUUID, charWriteValue,
239 CA_BLE_INITIAL_BUF_SIZE, false); // For Write Characteristics.
240 if (CA_STATUS_OK != ret )
242 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "CAAddNewCharacteristicsToGattServer failed");
243 ca_mutex_unlock(g_bleServerStateMutex);
244 CATerminateBleGattServer();
248 ret = CARegisterBleServicewithGattServer(g_gattSvcPath);
249 if (CA_STATUS_OK != ret )
251 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "CARegisterBleServicewithGattServer failed");
252 ca_mutex_unlock(g_bleServerStateMutex);
253 CATerminateBleGattServer();
257 int res = bt_gatt_set_connection_state_changed_cb(CABleGattServerConnectionStateChangedCb,
259 if (BT_ERROR_NONE != res)
261 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
262 "bt_gatt_set_connection_state_changed_cb Failed with return as [%s]",
263 CABTGetErrorMsg(res));
267 bt_adapter_le_create_advertiser(&g_hAdvertiser);
268 if (NULL == g_hAdvertiser)
270 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "g_hAdvertiser is NULL");
271 ca_mutex_unlock(g_bleServerStateMutex);
272 CATerminateBleGattServer();
276 res = bt_adapter_le_start_advertising(g_hAdvertiser, NULL, NULL, NULL);
277 if (BT_ERROR_NONE != res)
279 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "bt_adapter_le_start_advertising failed with ret [%d] ",
281 ca_mutex_unlock(g_bleServerStateMutex);
282 CATerminateBleGattServer();
286 g_isBleGattServerStarted = true;
288 ca_mutex_unlock(g_bleServerStateMutex);
290 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG,
291 "LE Server initialization complete.");
293 GMainContext *thread_context = NULL;
295 thread_context = g_main_context_new();
297 g_eventLoop = g_main_loop_new(thread_context, FALSE);
299 g_main_context_push_thread_default(thread_context);
301 g_main_loop_run(g_eventLoop);
303 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
306 CAResult_t CAStopBleGattServer()
308 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
310 ca_mutex_lock(g_bleServerStateMutex);
312 if (false == g_isBleGattServerStarted)
314 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "Gatt Server is not running to stop");
316 ca_mutex_unlock(g_bleServerStateMutex);
320 g_isBleGattServerStarted = false;
321 if (NULL != g_hAdvertiser )
324 ret = bt_adapter_le_stop_advertising(g_hAdvertiser);
327 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
328 "bt_adapter_le_stop_advertising failed with ret [%d]", ret);
331 ret = bt_adapter_le_destroy_advertiser(g_hAdvertiser);
334 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
335 "bt_adapter_le_destroy_advertiser failed with ret [%d]", ret);
337 g_hAdvertiser = NULL;
340 CAResult_t res = CARemoveAllBleServicesFromGattServer();
341 if (CA_STATUS_OK != res)
343 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "removeAllBleServicesFromGattServer failed");
346 res = CADeInitBleGattService();
347 if (CA_STATUS_OK != res)
349 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "_bt_gatt_deinit_service failed with ret [%d]", res);
352 GMainContext *context_event_loop = NULL;
353 // Required for waking up the thread which is running in gmain loop
354 if (NULL != g_eventLoop)
356 context_event_loop = g_main_loop_get_context(g_eventLoop);
358 if (context_event_loop)
360 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "g_eventLoop context %x", context_event_loop);
361 g_main_context_wakeup(context_event_loop);
363 // Kill g main loops and kill threads
364 g_main_loop_quit(g_eventLoop);
369 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "g_eventLoop context is NULL");
372 ca_mutex_unlock(g_bleServerStateMutex);
374 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
378 void CATerminateBleGattServer()
380 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
382 ca_mutex_lock(g_bleServerStateMutex);
384 // free service Path(unique identifier for ble service)
385 ca_mutex_lock(g_bleServiceMutex);
386 OICFree(g_gattSvcPath);
387 g_gattSvcPath = NULL;
388 ca_mutex_unlock(g_bleServiceMutex);
390 // freeing characteristics
391 ca_mutex_lock(g_bleCharacteristicMutex);
392 OICFree(g_gattReadCharPath);
393 g_gattReadCharPath = NULL;
394 OICFree(g_gattWriteCharPath);
395 g_gattWriteCharPath = NULL;
396 ca_mutex_unlock(g_bleCharacteristicMutex);
398 ca_mutex_unlock(g_bleServerStateMutex);
400 // Terminating all mutex variables.
401 CATerminateGattServerMutexVariables();
402 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
405 CAResult_t CAInitGattServerMutexVariables()
407 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
408 if (NULL == g_bleServerStateMutex)
410 g_bleServerStateMutex = ca_mutex_new();
411 if (NULL == g_bleServerStateMutex)
413 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "ca_mutex_new failed");
414 return CA_STATUS_FAILED;
418 if (NULL == g_bleServiceMutex)
420 g_bleServiceMutex = ca_mutex_new();
421 if (NULL == g_bleServiceMutex)
423 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "ca_mutex_new failed");
424 return CA_STATUS_FAILED;
428 if (NULL == g_bleCharacteristicMutex)
430 g_bleCharacteristicMutex = ca_mutex_new();
431 if (NULL == g_bleCharacteristicMutex)
433 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "ca_mutex_new failed");
434 return CA_STATUS_FAILED;
438 if (NULL == g_bleReqRespCbMutex)
440 g_bleReqRespCbMutex = ca_mutex_new();
441 if (NULL == g_bleReqRespCbMutex)
443 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "ca_mutex_new failed");
444 return CA_STATUS_FAILED;
447 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
451 void CATerminateGattServerMutexVariables()
453 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
454 ca_mutex_free(g_bleServerStateMutex);
455 g_bleServerStateMutex = NULL;
458 g_bleServerStateMutex = NULL;
459 ca_mutex_free(g_bleServiceMutex);
460 g_bleServiceMutex = NULL;
461 ca_mutex_free(g_bleCharacteristicMutex);
462 g_bleCharacteristicMutex = NULL;
463 ca_mutex_free(g_bleReqRespCbMutex);
464 g_bleReqRespCbMutex = NULL;
466 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
469 CAResult_t CAInitBleGattService()
471 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
473 int ret = _bt_gatt_init_service();
476 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "_bt_gatt_deinit_service failed with ret [%d]", ret);
477 return CA_STATUS_FAILED;
480 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
484 CAResult_t CADeInitBleGattService()
486 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
488 int ret = _bt_gatt_deinit_service();
491 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "_bt_gatt_deinit_service failed with ret [%d]", ret);
492 return CA_STATUS_FAILED;
495 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
499 void CASetBleServerThreadPoolHandle(ca_thread_pool_t handle)
501 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
502 ca_mutex_lock(g_bleServerThreadPoolMutex);
503 g_bleServerThreadPool = handle;
504 ca_mutex_unlock(g_bleServerThreadPoolMutex);
505 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
508 CAResult_t CAAddNewBleServiceInGattServer(const char *serviceUUID)
510 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
512 VERIFY_NON_NULL(serviceUUID, NULL, "Param serviceUUID is NULL");
514 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "service uuid %s", serviceUUID);
516 char *svcPath = NULL;
518 int ret = bt_gatt_add_service(serviceUUID, &svcPath);
521 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "bt_gatt_add_service failed with ret [%d]", ret);
522 return CA_STATUS_FAILED;
527 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
528 "AddNewBleServiceInGattServer ServicePath obtained is %s", svcPath);
530 ca_mutex_lock(g_bleServiceMutex);
532 if (NULL != g_gattSvcPath)
534 OICFree(g_gattSvcPath);
535 g_gattSvcPath = NULL;
537 g_gattSvcPath = svcPath;
539 ca_mutex_unlock(g_bleServiceMutex);
542 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
546 CAResult_t CARemoveBleServiceFromGattServer(const char *svcPath)
548 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
550 VERIFY_NON_NULL(svcPath, NULL, "Param svcPath is NULL");
552 int ret = bt_gatt_remove_service(svcPath);
556 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "bt_gatt_remove_service failed [%d]", ret);
557 return CA_STATUS_FAILED;
560 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
564 CAResult_t CARemoveAllBleServicesFromGattServer()
566 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
567 int ret = bt_gatt_delete_services();
570 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "bt_gatt_delete_services failed with ret [%d]", ret);
571 return CA_STATUS_FAILED;
574 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
578 void CABleGattRemoteCharacteristicWriteCb(char *charPath,
579 unsigned char *charValue,
580 int charValueLen, const char *remoteAddress, void *userData)
582 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
584 if (NULL == charPath || NULL == charValue || NULL == remoteAddress)
586 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "Param callback values are NULL");
590 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "charPath = [%s] charValue = [%s] len [%d]", charPath,
591 charValue, charValueLen);
593 char *data = (char *)OICMalloc(sizeof(char) * charValueLen);
596 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "Malloc failed!");
600 strncpy(data, (char *)charValue, charValueLen);
602 ca_mutex_lock(g_bleReqRespCbMutex);
603 if (NULL == g_bleServerDataReceivedCallback)
605 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "gReqRespCallback is NULL!");
606 ca_mutex_unlock(g_bleReqRespCbMutex);
611 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "Sending data up !");
612 uint32_t sentLength = 0;
613 g_bleServerDataReceivedCallback(remoteAddress, OIC_BLE_SERVICE_ID,
614 data, charValueLen, &sentLength);
616 ca_mutex_unlock(g_bleReqRespCbMutex);
619 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
622 CAResult_t CARegisterBleServicewithGattServer(const char *svcPath)
624 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
626 VERIFY_NON_NULL(svcPath, NULL, "Param svcPath is NULL");
628 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "svcPath:%s", svcPath);
630 int ret = bt_gatt_register_service(svcPath, CABleGattRemoteCharacteristicWriteCb, NULL);
634 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "bt_gatt_register_service failed with ret [%d]", ret);
635 return CA_STATUS_FAILED;
638 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
642 CAResult_t CAAddNewCharacteristicsToGattServer(const char *svcPath, const char *charUUID,
643 const char *charValue, int charValueLen, bool read)
646 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
651 charFlags[0] = "notify";
655 charFlags[0] = "write-without-response";
658 size_t flagLen = sizeof(charFlags) / sizeof(charFlags[0]);
660 char *charPath = NULL;
661 int ret = bt_gatt_add_characteristic(charUUID, charValue, charValueLen, charFlags, flagLen,
664 if (0 != ret || NULL == charPath)
666 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
667 "bt_gatt_add_characteristic failed with ret [%d]", ret);
668 return CA_STATUS_FAILED;
671 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG,
672 "bt_gatt_add_characteristic charPath obtained: %s", charPath);
674 ca_mutex_lock(g_bleCharacteristicMutex);
678 if (NULL != g_gattReadCharPath)
680 OICFree(g_gattReadCharPath);
681 g_gattReadCharPath = NULL;
683 g_gattReadCharPath = charPath;
688 if (NULL != g_gattWriteCharPath)
690 OICFree(g_gattWriteCharPath);
691 g_gattWriteCharPath = NULL;
693 g_gattWriteCharPath = charPath;
696 ca_mutex_unlock(g_bleCharacteristicMutex);
698 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
702 CAResult_t CARemoveCharacteristicsFromGattServer(const char *charPath)
704 ///TODO: There is no api provided in bluetooth.h for removing characteristics.
708 CAResult_t CAUpdateCharacteristicsToGattClient(const char* address, const char *charValue,
709 const uint32_t charValueLen)
711 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
713 VERIFY_NON_NULL(charValue, NULL, "Param charValue is NULL");
715 VERIFY_NON_NULL(address, NULL, "Param address is NULL");
717 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "Client's Unicast address for sending data [%s]", address);
719 ca_mutex_lock(g_bleCharacteristicMutex);
721 if (NULL == g_gattReadCharPath)
723 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "g_gattReadCharPath is NULL");
724 ca_mutex_unlock(g_bleCharacteristicMutex);
725 return CA_STATUS_FAILED;
728 char *data = (char *) OICMalloc(sizeof(char) * (charValueLen + 1));
731 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "malloc failed!");
732 ca_mutex_unlock(g_bleCharacteristicMutex);
733 return CA_STATUS_FAILED;
735 memset(data, 0x0, (charValueLen + 1));
737 strncpy(data, charValue, charValueLen);
739 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "updating characteristics char [%s] data [%s] dataLen [%d]",
740 (const char *)g_gattReadCharPath, data, charValueLen);
742 int ret = bt_gatt_update_characteristic(g_gattReadCharPath, data, charValueLen, address);
745 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
746 "bt_gatt_update_characteristic failed with return [%d]", ret);
748 ca_mutex_unlock(g_bleCharacteristicMutex);
749 return CA_STATUS_FAILED;
753 ca_mutex_unlock(g_bleCharacteristicMutex);
755 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "OUT");
759 CAResult_t CAUpdateCharacteristicsToAllGattClients(const char *charValue,
760 const uint32_t charValueLen)
762 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
764 VERIFY_NON_NULL(charValue, NULL, "Param charValue is NULL");
766 ca_mutex_lock(g_bleCharacteristicMutex);
768 if (NULL == g_gattReadCharPath)
770 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "g_gattReadCharPath is NULL");
771 ca_mutex_unlock(g_bleCharacteristicMutex);
772 return CA_STATUS_FAILED;
775 char *data = (char *) OICMalloc(sizeof(char) * (charValueLen + 1));
778 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "malloc failed!");
779 ca_mutex_unlock(g_bleCharacteristicMutex);
780 return CA_STATUS_FAILED;
783 strncpy(data, charValue, charValueLen + 1);
785 OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "updating characteristics char [%s] data [%s] dataLen [%d]",
786 (const char *)g_gattReadCharPath, data, charValueLen);
788 int ret = bt_gatt_update_characteristic(g_gattReadCharPath, data, charValueLen, NULL);
791 OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
792 "bt_gatt_update_characteristic failed with return [%d]", ret);
794 ca_mutex_unlock(g_bleCharacteristicMutex);
795 return CA_STATUS_FAILED;
799 ca_mutex_unlock(g_bleCharacteristicMutex);
801 OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "OUT");
805 void CASetBLEReqRespServerCallback(CABLEServerDataReceivedCallback callback)
807 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
809 ca_mutex_lock(g_bleReqRespCbMutex);
811 g_bleServerDataReceivedCallback = callback;
813 ca_mutex_unlock(g_bleReqRespCbMutex);
815 OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");