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 "caleclient.h"
26 #include <arpa/inet.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
34 #include "uarraylist.h"
35 #include "caqueueingthread.h"
36 #include "caadapterutils.h"
37 #include "cagattservice.h"
38 #include "oic_string.h"
39 #include "oic_malloc.h"
42 * Logging tag for module name.
44 #define TAG "OIC_CA_LE_CLIENT"
46 #define MICROSECS_PER_SEC 1000000
47 #define WAIT_TIME_WRITE_CHARACTERISTIC 10 * MICROSECS_PER_SEC
49 uint64_t const TIMEOUT = 30 * MICROSECS_PER_SEC;
52 * Flag to check if scanning is in progress
54 static bool g_isScanningInProgress = false;
57 * Mutex to synchronize access to g_isScanningInProgress
59 static oc_mutex g_isScanningInProgressMutex = NULL;
62 * Flag to check if connection is in progress
64 static bool g_isConnectionInProgress = false;
67 * Mutex to synchronize access to g_isConnectionInProgress
69 static oc_mutex g_isConnectionInProgressMutex = NULL;
72 * Flag to check if multicast is already in progress.
74 static bool g_isMulticastInProgress = false;
77 * Flag to check if unicast scan is in progress
79 static bool g_isUnicastScanInProgress = false;
82 * Mutex to synchronize access to g_isMulticastInProgress
83 * and g_isUnicastScanInProgress
85 static oc_mutex g_scanMutex = NULL;
88 * Pending multicast data list to be sent.
90 static u_arraylist_t *g_multicastDataList = NULL;
93 * Mutex to synchronize the access to Pending multicast data list.
95 static oc_mutex g_multicastDataListMutex = NULL;
98 * Condition to start the timer for scanning.
100 static oc_cond g_startTimerCond = NULL;
103 * Condition for scanning Time interval.
105 static oc_cond g_scanningTimeCond = NULL;
108 * This contains the list of OIC services a client connect tot.
110 static LEServerInfoList *g_LEServerList = NULL;
113 * Mutex to synchronize access to BleServiceList.
115 static oc_mutex g_LEServerListMutex = NULL;
118 * Boolean variable to keep the state of the GATT Client.
120 static bool g_isLEGattClientStarted = false;
123 * Mutex to synchronize access to the requestResponse callback to be called
124 * when the data needs to be sent from GATTClient.
126 static oc_mutex g_LEReqRespClientCbMutex = NULL;
129 * Mutex to synchronize access to the requestResponse callback to be called
130 * when the data needs to be sent from GATTClient.
132 static oc_mutex g_LEClientConnectMutex = NULL;
135 * Mutex to synchronize the calls to be done to the platform from GATTClient
136 * interfaces from different threads.
138 static oc_mutex g_LEClientStateMutex = NULL;
141 * Mutex to synchronize the task to be pushed to thread pool.
143 static oc_mutex g_LEClientThreadPoolMutex = NULL;
146 * Mutex to synchronize the task to write characteristic one packet after another.
148 static oc_mutex g_threadWriteCharacteristicMutex = NULL;
151 * Condition for Writing characteristic.
153 static oc_cond g_threadWriteCharacteristicCond = NULL;
156 * Flag to check status of write characteristic.
158 static bool g_isSignalSetFlag = false;
161 * Maintains the callback to be notified on receival of network packets from other
164 static CABLEDataReceivedCallback g_LEClientDataReceivedCallback = NULL;
167 * callback to update the error to le adapter
169 static CABLEErrorHandleCallback g_clientErrorCallback;
172 * gmainLoop to manage the threads to receive the callback from the platfrom.
174 static GMainLoop *g_eventLoop = NULL;
177 * Reference to threadpool
179 static ca_thread_pool_t g_LEClientThreadPool = NULL;
181 void CALEGattCharacteristicChangedCb(bt_gatt_h characteristic,
183 int valueLen, void *userData)
185 (void)characteristic;
187 OIC_LOG(DEBUG, TAG, "IN");
188 OIC_LOG_V(DEBUG, TAG, "Changed characteristic value length [%d]", valueLen);
190 oc_mutex_lock(g_LEReqRespClientCbMutex);
191 if (NULL == g_LEClientDataReceivedCallback)
193 OIC_LOG(ERROR, TAG, "Request response callback is not set");
194 oc_mutex_unlock(g_LEReqRespClientCbMutex);
198 uint32_t sentLength = 0;
199 g_LEClientDataReceivedCallback(userData, (uint8_t *)value, valueLen, &sentLength);
201 OIC_LOG_V(DEBUG, TAG, "Recv data Length is %d", sentLength);
203 oc_mutex_unlock(g_LEReqRespClientCbMutex);
205 OIC_LOG(DEBUG, TAG, "OUT");
208 void CALEGattCharacteristicWriteCb(int result, bt_gatt_h reqHandle, void *userData)
213 OIC_LOG(DEBUG, TAG, "IN ");
215 if (BT_ERROR_NONE != result)
217 CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, "", 0, -1,
218 false, "writeChar failure");
220 OIC_LOG(ERROR, TAG, "Write failed Need Retry ");
221 //Need to Implement retry mechanism
225 oc_mutex_lock(g_threadWriteCharacteristicMutex);
226 OIC_LOG(DEBUG, TAG, "g_isSignalSetFlag is set true and signal");
227 g_isSignalSetFlag = true;
228 oc_cond_signal(g_threadWriteCharacteristicCond);
229 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
231 CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, "", 0, -1,
232 true, "writeChar success");
235 OIC_LOG(DEBUG, TAG, "OUT ");
238 CAResult_t CALEGattInitiateConnection(const char *remoteAddress)
240 OIC_LOG(DEBUG, TAG, "IN");
242 oc_mutex_lock(g_isConnectionInProgressMutex);
243 if (g_isConnectionInProgress)
245 oc_mutex_unlock(g_isConnectionInProgressMutex);
246 OIC_LOG(DEBUG, TAG, "Connection already in progress, cannot initiate new connection");
247 return CA_STATUS_FAILED;
249 g_isConnectionInProgress = true;
250 oc_mutex_unlock(g_isConnectionInProgressMutex);
252 // Pause the scanning
253 CALEGattStopDeviceScanning();
255 OIC_LOG_V(DEBUG, TAG,
256 "Trying to do Gatt connection to [%s]", remoteAddress);
258 oc_mutex_lock(g_LEClientThreadPoolMutex);
259 if (NULL == g_LEClientThreadPool)
261 oc_mutex_unlock(g_LEClientThreadPoolMutex);
262 OIC_LOG(ERROR, TAG, "g_LEClientThreadPool is NULL");
263 return CA_STATUS_FAILED;
266 char *addr = OICStrdup(remoteAddress);
269 oc_mutex_unlock(g_LEClientThreadPoolMutex);
270 OIC_LOG(ERROR, TAG, "OICStrdup failed");
271 return CA_STATUS_FAILED;
274 CAResult_t res = ca_thread_pool_add_task(g_LEClientThreadPool, CAGattConnectThread, addr, NULL);
275 oc_mutex_unlock(g_LEClientThreadPoolMutex);
276 if (CA_STATUS_OK != res)
278 OIC_LOG_V(ERROR, TAG,
279 "ca_thread_pool_add_task failed with ret [%d]", res);
281 return CA_STATUS_FAILED;
283 OIC_LOG(DEBUG, TAG, "OUT");
287 void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress)
289 OIC_LOG(DEBUG, TAG, "IN ");
291 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
295 OIC_LOG_V(DEBUG, TAG, "DisConnected from [%s] ", remoteAddress);
296 oc_mutex_lock(g_LEServerListMutex);
297 CARemoveLEServerInfoFromList(&g_LEServerList, remoteAddress);
298 oc_mutex_unlock(g_LEServerListMutex);
302 OIC_LOG_V(DEBUG, TAG, "Connected to [%s] ", remoteAddress);
304 oc_mutex_lock(g_isConnectionInProgressMutex);
305 g_isConnectionInProgress = false;
306 oc_mutex_unlock(g_isConnectionInProgressMutex);
308 // Resume the scanning
309 oc_mutex_lock(g_scanMutex);
310 if (g_isMulticastInProgress || g_isUnicastScanInProgress)
312 CAResult_t ret = CALEGattStartDeviceScanning();
313 if (CA_STATUS_OK != ret)
315 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning Failed");
318 oc_mutex_unlock(g_scanMutex);
320 LEServerInfo *serverInfo = NULL;
321 oc_mutex_lock(g_LEServerListMutex);
322 if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
324 oc_mutex_unlock(g_LEServerListMutex);
325 OIC_LOG_V(ERROR, TAG, "Could not get server info for [%s]", remoteAddress);
329 serverInfo->status = LE_STATUS_CONNECTED;
330 oc_mutex_unlock(g_LEServerListMutex);
332 oc_mutex_lock(g_LEClientThreadPoolMutex);
333 if (NULL == g_LEClientThreadPool)
335 oc_mutex_unlock(g_LEClientThreadPoolMutex);
336 OIC_LOG(ERROR, TAG, "g_LEClientThreadPool is NULL");
340 char *addr = OICStrdup(remoteAddress);
343 oc_mutex_unlock(g_LEClientThreadPoolMutex);
344 OIC_LOG(ERROR, TAG, "addr is NULL");
348 CAResult_t ret = ca_thread_pool_add_task(g_LEClientThreadPool, CADiscoverLEServicesThread,
350 oc_mutex_unlock(g_LEClientThreadPoolMutex);
351 if (CA_STATUS_OK != ret)
353 OIC_LOG_V(ERROR, TAG, "ca_thread_pool_add_task failed with ret [%d]", ret);
357 OIC_LOG(DEBUG, TAG, "OUT");
360 static bool CALEIsHaveServiceImpl(bt_adapter_le_device_scan_result_info_s *scanInfo,
361 const char *service_uuid,
362 bt_adapter_le_packet_type_e pkt_type)
369 result = bt_adapter_le_get_scan_result_service_uuids(scanInfo,
370 pkt_type, &uuids, &count);
371 if (result == BT_ERROR_NONE && NULL != uuids)
373 for (int i = 0; i < count; i++)
375 if (0 == strcasecmp(uuids[i], service_uuid))
377 OIC_LOG_V(DEBUG, TAG, "Service[%s] Found in %s",
378 uuids[i], scanInfo->remote_address);
388 static bool CALEIsHaveService(bt_adapter_le_device_scan_result_info_s *scanInfo,
389 const char *service_uuid)
392 // For arduino servers, scan response will give the UUIDs advertised.
393 CALEIsHaveServiceImpl(scanInfo, service_uuid,
394 BT_ADAPTER_LE_PACKET_SCAN_RESPONSE) ||
395 // For android/tizen servers, advertising packet will give the UUIDs.
396 CALEIsHaveServiceImpl(scanInfo, service_uuid,
397 BT_ADAPTER_LE_PACKET_ADVERTISING);
400 void CALEAdapterScanResultCb(int result, bt_adapter_le_device_scan_result_info_s *scanInfo,
405 OIC_LOG(DEBUG, TAG, "IN");
407 VERIFY_NON_NULL_VOID(scanInfo, TAG, "scanInfo");
408 VERIFY_NON_NULL_VOID(scanInfo->remote_address, TAG, "scanInfo->remote_address");
410 OIC_LOG_V(DEBUG, TAG, "Remote Address [%s]", scanInfo->remote_address);
411 OIC_LOG_V(DEBUG, TAG, "Scan Result [%d]", result);
412 OIC_LOG_V(DEBUG, TAG,
413 " Adv data len [%d] Scan data len[%d]RSSI [%d] Addr_type [%d] ",
414 scanInfo->adv_data_len, scanInfo->scan_data_len, scanInfo->rssi,
415 scanInfo->address_type);
417 // Check if scanning was stopped (since this callback is
418 // being triggered even after stopping the scan)
419 oc_mutex_lock(g_isScanningInProgressMutex);
420 if (!g_isScanningInProgress)
422 oc_mutex_unlock(g_isScanningInProgressMutex);
423 OIC_LOG(DEBUG, TAG, "Scanning not in progress, so ignoring callback");
426 oc_mutex_unlock(g_isScanningInProgressMutex);
428 oc_mutex_lock(g_scanMutex);
429 bool isMulticastInProgress = g_isMulticastInProgress;
430 oc_mutex_unlock(g_scanMutex);
432 LEServerInfo *serverInfo = NULL;
433 oc_mutex_lock(g_LEServerListMutex);
434 CAResult_t ret = CAGetLEServerInfo(g_LEServerList, scanInfo->remote_address, &serverInfo);
436 if (CA_STATUS_OK == ret && serverInfo->status == LE_STATUS_UNICAST_PENDING)
438 // Stop the scan if no other device is in unicast pending
439 // state and if multicast is not in progress
440 LEServerInfoList *curNode = g_LEServerList;
441 for (; curNode; curNode = curNode->next)
443 if (curNode->serverInfo == serverInfo)
447 if (curNode->serverInfo->status == LE_STATUS_UNICAST_PENDING)
455 oc_mutex_lock(g_scanMutex);
456 if (!g_isMulticastInProgress && g_isUnicastScanInProgress)
458 CALEGattStopDeviceScanning();
459 g_isUnicastScanInProgress = false;
460 oc_cond_signal(g_scanningTimeCond);
462 oc_mutex_unlock(g_scanMutex);
465 if (!CALEIsHaveService(scanInfo, CA_GATT_SERVICE_UUID))
467 serverInfo->status = LE_STATUS_INVALID;
468 OIC_LOG_V(DEBUG, TAG, "Device [%s] does not support OIC service", serverInfo->remoteAddress);
469 oc_mutex_unlock(g_LEServerListMutex);
473 bt_gatt_client_h clientHandle = NULL;
474 int32_t ret = bt_gatt_client_create(remoteAddress, &clientHandle);
475 if (BT_ERROR_NONE != ret || NULL == clientHandle)
477 OIC_LOG_V(ERROR, TAG,
478 "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret));
479 CALEGattDisConnect(remoteAddress);
480 return CA_STATUS_FAILED;
482 serverInfo->clientHandle = clientHandle;
484 serverInfo->status = LE_STATUS_CONNECTION_INITIATED;
485 if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress))
487 serverInfo->status = LE_STATUS_DISCOVERED;
488 OIC_LOG_V(ERROR, TAG, "Could not initiate connection to [%s]", serverInfo->remoteAddress);
489 oc_mutex_unlock(g_LEServerListMutex);
492 oc_mutex_unlock(g_LEServerListMutex);
493 OIC_LOG(DEBUG, TAG, "OUT");
497 if (isMulticastInProgress)
499 if (CA_STATUS_OK != ret)
501 OIC_LOG_V(DEBUG, TAG,
502 "Newly discovered device with address [%s], adding to list", scanInfo->remote_address);
504 char *addr = OICStrdup(scanInfo->remote_address);
507 oc_mutex_unlock(g_LEServerListMutex);
508 OIC_LOG(ERROR, TAG, "Device address is NULL");
511 serverInfo = (LEServerInfo *)OICCalloc(1, sizeof(LEServerInfo));
512 if (NULL == serverInfo)
514 oc_mutex_unlock(g_LEServerListMutex);
515 OIC_LOG(ERROR, TAG, "Calloc failed");
519 serverInfo->remoteAddress = addr;
520 serverInfo->status = LE_STATUS_DISCOVERED;
522 if (CA_STATUS_OK != CAAddLEServerInfoToList(&g_LEServerList, serverInfo))
524 oc_mutex_unlock(g_LEServerListMutex);
525 OIC_LOG_V(ERROR, TAG, "Could not add [%s] to server list", scanInfo->remote_address);
526 CAFreeLEServerInfo(serverInfo);
530 if (!CALEIsHaveService(scanInfo, CA_GATT_SERVICE_UUID))
532 serverInfo->status = LE_STATUS_INVALID;
533 OIC_LOG_V(DEBUG, TAG, "Device [%s] does not support OIC service", serverInfo->remoteAddress);
534 oc_mutex_unlock(g_LEServerListMutex);
538 oc_mutex_lock(g_multicastDataListMutex);
539 uint32_t lengthData = u_arraylist_length(g_multicastDataList);
540 for (uint32_t len = 0; len < lengthData; ++len)
542 LEData *multicastData = (LEData *)u_arraylist_get(g_multicastDataList, len);
543 if (NULL == multicastData)
545 OIC_LOG(ERROR, TAG, "multicastData is NULL");
548 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList,
549 multicastData->data, multicastData->dataLength))
551 OIC_LOG(ERROR, TAG, "Failed to add to pending list");
555 oc_mutex_unlock(g_multicastDataListMutex);
558 if (serverInfo->status == LE_STATUS_DISCOVERED)
560 // Initiate connection if not yet initiated
561 serverInfo->status = LE_STATUS_CONNECTION_INITIATED;
562 if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress))
564 OIC_LOG_V(ERROR, TAG, "Could not initiate connection to [%s]", serverInfo->remoteAddress);
565 serverInfo->status = LE_STATUS_DISCOVERED;
570 OIC_LOG_V(DEBUG, TAG, "Device already discovered, status= [%d]", serverInfo->status);
574 oc_mutex_unlock(g_LEServerListMutex);
575 OIC_LOG(DEBUG, TAG, "OUT");
578 void CASetLEClientThreadPoolHandle(ca_thread_pool_t handle)
580 OIC_LOG(DEBUG, TAG, "IN");
582 oc_mutex_lock(g_LEClientThreadPoolMutex);
583 g_LEClientThreadPool = handle;
584 oc_mutex_unlock(g_LEClientThreadPoolMutex);
586 OIC_LOG(DEBUG, TAG, "OUT");
589 void CASetLEReqRespClientCallback(CABLEDataReceivedCallback callback)
591 OIC_LOG(DEBUG, TAG, "IN");
593 oc_mutex_lock(g_LEReqRespClientCbMutex);
595 g_LEClientDataReceivedCallback = callback;
597 oc_mutex_unlock(g_LEReqRespClientCbMutex);
599 OIC_LOG(DEBUG, TAG, "OUT");
602 void CASetBLEClientErrorHandleCallback(CABLEErrorHandleCallback callback)
604 g_clientErrorCallback = callback;
607 CAResult_t CAStartLEGattClient()
609 OIC_LOG(DEBUG, TAG, "IN");
611 oc_mutex_lock(g_LEClientStateMutex);
612 if (true == g_isLEGattClientStarted)
614 OIC_LOG(ERROR, TAG, "Gatt Client is already running!!");
615 oc_mutex_unlock(g_LEClientStateMutex);
616 return CA_STATUS_FAILED;
619 CAResult_t result = CALEGattSetCallbacks();
620 if (CA_STATUS_OK != result)
622 OIC_LOG(ERROR, TAG, "CABleGattSetCallbacks Failed");
623 oc_mutex_unlock(g_LEClientStateMutex);
624 CATerminateLEGattClient();
625 return CA_STATUS_FAILED;
628 g_isLEGattClientStarted = true;
629 oc_mutex_unlock(g_LEClientStateMutex);
631 oc_mutex_lock(g_LEClientThreadPoolMutex);
632 if (NULL == g_LEClientThreadPool)
634 OIC_LOG(ERROR, TAG, "gBleServerThreadPool is NULL");
635 CATerminateGattClientMutexVariables();
636 oc_mutex_unlock(g_LEClientThreadPoolMutex);
637 return CA_STATUS_FAILED;
640 result = ca_thread_pool_add_task(g_LEClientThreadPool, CAStartTimerThread,
642 if (CA_STATUS_OK != result)
644 OIC_LOG(ERROR, TAG, "ca_thread_pool_add_task failed");
645 CATerminateGattClientMutexVariables();
646 oc_mutex_unlock(g_LEClientThreadPoolMutex);
647 return CA_STATUS_FAILED;
649 oc_mutex_unlock(g_LEClientThreadPoolMutex);
651 OIC_LOG(DEBUG, TAG, "OUT");
655 void CAStartTimerThread(void *data)
659 OIC_LOG(DEBUG, TAG, "IN");
660 while (g_isLEGattClientStarted)
662 oc_mutex_lock(g_scanMutex);
663 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
665 OIC_LOG(DEBUG, TAG, "waiting....");
666 oc_cond_wait(g_startTimerCond, g_scanMutex);
667 OIC_LOG(DEBUG, TAG, "Wake up");
670 // Timed conditional wait for stopping the scan.
671 OCWaitResult_t ret = oc_cond_wait_for(g_scanningTimeCond, g_scanMutex,
673 if (OC_WAIT_TIMEDOUT == ret)
675 OIC_LOG(DEBUG, TAG, "Scan is timed Out");
677 CALEGattStopDeviceScanning();
679 if (g_isMulticastInProgress)
681 oc_mutex_lock(g_multicastDataListMutex);
682 // Clear the data list and device list.
683 u_arraylist_destroy(g_multicastDataList);
684 g_multicastDataList = NULL;
685 oc_mutex_unlock(g_multicastDataListMutex);
686 g_isMulticastInProgress = false;
688 g_isUnicastScanInProgress = false;
690 oc_mutex_unlock(g_scanMutex);
693 OIC_LOG(DEBUG, TAG, "OUT");
696 void CAStopLEGattClient()
698 OIC_LOG(DEBUG, TAG, "IN");
700 oc_mutex_lock(g_LEClientStateMutex);
702 if (false == g_isLEGattClientStarted)
704 OIC_LOG(ERROR, TAG, "Gatt Client is not running to stop");
705 oc_mutex_unlock(g_LEClientStateMutex);
709 CALEGattUnSetCallbacks();
711 CALEGattStopDeviceScanning();
713 g_isLEGattClientStarted = false;
715 // Signal the conditions waiting in Start timer.
716 oc_cond_signal(g_startTimerCond);
717 oc_cond_signal(g_scanningTimeCond);
719 // Destroy the multicast data list and device list if not empty.
720 if (NULL != g_multicastDataList)
722 oc_mutex_lock(g_multicastDataListMutex);
723 u_arraylist_destroy(g_multicastDataList);
724 g_multicastDataList = NULL;
725 oc_mutex_unlock(g_multicastDataListMutex);
728 oc_mutex_lock(g_LEServerListMutex);
729 CAFreeLEServerList(g_LEServerList);
730 g_LEServerList = NULL;
731 oc_mutex_unlock(g_LEServerListMutex);
733 oc_mutex_lock(g_threadWriteCharacteristicMutex);
734 oc_cond_signal(g_threadWriteCharacteristicCond);
735 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
737 GMainContext *context_event_loop = NULL;
738 // Required for waking up the thread which is running in gmain loop
739 if (NULL != g_eventLoop)
741 context_event_loop = g_main_loop_get_context(g_eventLoop);
743 if (context_event_loop)
745 OIC_LOG_V(DEBUG, TAG, "g_eventLoop context %p", (void *)context_event_loop);
746 g_main_context_wakeup(context_event_loop);
748 // Kill g main loops and kill threads.
749 g_main_loop_quit(g_eventLoop);
753 OIC_LOG(ERROR, TAG, "g_eventLoop context is NULL");
756 oc_mutex_unlock(g_LEClientStateMutex);
758 OIC_LOG(DEBUG, TAG, "OUT");
761 CAResult_t CAInitializeLEGattClient()
763 OIC_LOG(DEBUG, TAG, "Initialize GATT Client");
764 CAResult_t res = CAInitGattClientMutexVariables();
765 if (CA_STATUS_OK != res)
767 OIC_LOG(ERROR, TAG, "CAInitGattClientMutexVariables failed!");
768 CATerminateGattClientMutexVariables();
769 return CA_STATUS_FAILED;
774 void CATerminateLEGattClient()
776 OIC_LOG(DEBUG, TAG, "IN");
778 CATerminateGattClientMutexVariables();
780 OIC_LOG(DEBUG, TAG, "OUT");
783 CAResult_t CAInitGattClientMutexVariables()
785 OIC_LOG(DEBUG, TAG, "IN");
786 if (NULL == g_LEClientStateMutex)
788 g_LEClientStateMutex = oc_mutex_new();
789 if (NULL == g_LEClientStateMutex)
791 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
792 return CA_STATUS_FAILED;
796 if (NULL == g_LEServerListMutex)
798 g_LEServerListMutex = oc_mutex_new();
799 if (NULL == g_LEServerListMutex)
801 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
802 return CA_STATUS_FAILED;
806 if (NULL == g_LEReqRespClientCbMutex)
808 g_LEReqRespClientCbMutex = oc_mutex_new();
809 if (NULL == g_LEReqRespClientCbMutex)
811 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
812 return CA_STATUS_FAILED;
816 if (NULL == g_LEClientThreadPoolMutex)
818 g_LEClientThreadPoolMutex = oc_mutex_new();
819 if (NULL == g_LEClientThreadPoolMutex)
821 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
822 return CA_STATUS_FAILED;
826 if (NULL == g_LEClientConnectMutex)
828 g_LEClientConnectMutex = oc_mutex_new();
829 if (NULL == g_LEClientConnectMutex)
831 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
832 return CA_STATUS_FAILED;
836 if (NULL == g_isScanningInProgressMutex)
838 g_isScanningInProgressMutex = oc_mutex_new();
839 if (NULL == g_isScanningInProgressMutex)
841 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
842 return CA_STATUS_FAILED;
846 if (NULL == g_isConnectionInProgressMutex)
848 g_isConnectionInProgressMutex = oc_mutex_new();
849 if (NULL == g_isConnectionInProgressMutex)
851 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
852 return CA_STATUS_FAILED;
856 if (NULL == g_multicastDataListMutex)
858 g_multicastDataListMutex = oc_mutex_new();
859 if (NULL == g_multicastDataListMutex)
861 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
862 return CA_STATUS_FAILED;
866 if (NULL == g_scanMutex)
868 g_scanMutex = oc_mutex_new();
869 if (NULL == g_scanMutex)
871 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
872 return CA_STATUS_FAILED;
876 if (NULL == g_threadWriteCharacteristicMutex)
878 g_threadWriteCharacteristicMutex = oc_mutex_new();
879 if (NULL == g_threadWriteCharacteristicMutex)
881 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
882 return CA_STATUS_FAILED;
886 if (NULL == g_startTimerCond)
888 g_startTimerCond = oc_cond_new();
889 if (NULL == g_startTimerCond)
891 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
892 return CA_STATUS_FAILED;
896 if (NULL == g_scanningTimeCond)
898 g_scanningTimeCond = oc_cond_new();
899 if (NULL == g_scanningTimeCond)
901 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
902 return CA_STATUS_FAILED;
906 if (NULL == g_threadWriteCharacteristicCond)
908 g_threadWriteCharacteristicCond = oc_cond_new();
909 if (NULL == g_threadWriteCharacteristicCond)
911 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
912 return CA_STATUS_FAILED;
916 OIC_LOG(DEBUG, TAG, "OUT");
920 void CATerminateGattClientMutexVariables()
922 OIC_LOG(DEBUG, TAG, "IN");
924 oc_mutex_free(g_LEClientStateMutex);
925 g_LEClientStateMutex = NULL;
927 oc_mutex_free(g_LEServerListMutex);
928 g_LEServerListMutex = NULL;
930 oc_mutex_free(g_LEReqRespClientCbMutex);
931 g_LEReqRespClientCbMutex = NULL;
933 oc_mutex_free(g_LEClientConnectMutex);
934 g_LEClientConnectMutex = NULL;
936 oc_mutex_free(g_LEClientThreadPoolMutex);
937 g_LEClientThreadPoolMutex = NULL;
939 oc_mutex_free(g_isScanningInProgressMutex);
940 g_isScanningInProgressMutex = NULL;
942 oc_mutex_free(g_isConnectionInProgressMutex);
943 g_isConnectionInProgressMutex = NULL;
945 oc_mutex_free(g_multicastDataListMutex);
946 g_multicastDataListMutex = NULL;
948 oc_mutex_free(g_scanMutex);
951 oc_mutex_free(g_threadWriteCharacteristicMutex);
952 g_threadWriteCharacteristicMutex = NULL;
954 oc_cond_free(g_startTimerCond);
955 g_startTimerCond = NULL;
957 oc_cond_free(g_scanningTimeCond);
958 g_scanningTimeCond = NULL;
960 oc_cond_free(g_threadWriteCharacteristicCond);
961 g_threadWriteCharacteristicCond = NULL;
962 g_isSignalSetFlag = false;
964 OIC_LOG(DEBUG, TAG, "OUT");
967 CAResult_t CALEGattSetCallbacks()
969 OIC_LOG(DEBUG, TAG, "IN");
971 OIC_LOG(DEBUG, TAG, "OUT");
975 void CALEGattUnSetCallbacks()
977 OIC_LOG(DEBUG, TAG, "IN");
979 bt_gatt_unset_connection_state_changed_cb();
981 oc_mutex_lock(g_LEServerListMutex);
982 LEServerInfoList *curNode = g_LEServerList;
985 LEServerInfo *serverInfo = curNode->serverInfo;
986 if (serverInfo->status >= LE_STATUS_SERVICES_DISCOVERED)
988 bt_gatt_client_unset_characteristic_value_changed_cb(serverInfo->readChar);
990 curNode = curNode->next;
992 oc_mutex_unlock(g_LEServerListMutex);
994 OIC_LOG(DEBUG, TAG, "OUT");
997 CAResult_t CALEGattStartDeviceScanning()
999 OIC_LOG(DEBUG, TAG, "IN");
1001 oc_mutex_lock(g_isScanningInProgressMutex);
1002 if (!g_isScanningInProgress)
1004 int ret = bt_adapter_le_start_scan(CALEAdapterScanResultCb, NULL);
1005 if (BT_ERROR_NONE != ret)
1007 oc_mutex_unlock(g_isScanningInProgressMutex);
1008 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_start_scan failed[%s]",
1009 CALEGetErrorMsg(ret));
1010 return CA_STATUS_FAILED;
1012 g_isScanningInProgress = true;
1016 OIC_LOG(DEBUG, TAG, "Ignore, scanning already in progress");
1018 oc_mutex_unlock(g_isScanningInProgressMutex);
1020 OIC_LOG(DEBUG, TAG, "OUT");
1021 return CA_STATUS_OK;
1024 void CALEGattStopDeviceScanning()
1026 OIC_LOG(DEBUG, TAG, "IN");
1028 oc_mutex_lock(g_isScanningInProgressMutex);
1029 if (g_isScanningInProgress)
1031 int ret = bt_adapter_le_stop_scan();
1032 if (BT_ERROR_NONE != ret)
1034 oc_mutex_unlock(g_isScanningInProgressMutex);
1035 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_stop_scan failed[%s]",
1036 CALEGetErrorMsg(ret));
1039 g_isScanningInProgress = false;
1043 OIC_LOG(DEBUG, TAG, "Ignore, scanning not in progress");
1045 oc_mutex_unlock(g_isScanningInProgressMutex);
1047 OIC_LOG(DEBUG, TAG, "OUT");
1050 void CAGattConnectThread (void *remoteAddress)
1052 OIC_LOG(DEBUG, TAG, "IN ");
1054 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
1056 char *address = (char *)remoteAddress;
1058 OIC_LOG_V(DEBUG, TAG, "remote address is [%s]", address);
1060 CAResult_t result = CALEGattConnect(address);
1062 if (CA_STATUS_OK != result)
1064 OIC_LOG_V(ERROR, TAG, "bt_gatt_connect failed for [%s]", address);
1069 OIC_LOG(DEBUG, TAG, "OUT");
1072 CAResult_t CALEGattConnect(const char *remoteAddress)
1074 OIC_LOG(DEBUG, TAG, "IN");
1076 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1077 "remote address is NULL", CA_STATUS_FAILED);
1079 oc_mutex_lock(g_LEClientConnectMutex);
1080 CAResult_t result = CA_STATUS_OK;
1082 int ret = bt_gatt_connect(remoteAddress, false);
1083 if (BT_ERROR_NONE != ret)
1085 OIC_LOG_V(ERROR, TAG, "bt_gatt_connect Failed with ret value [%s] ",
1086 CALEGetErrorMsg(ret));
1087 oc_mutex_unlock(g_LEClientConnectMutex);
1088 return CA_STATUS_FAILED;
1091 oc_mutex_unlock(g_LEClientConnectMutex);
1093 OIC_LOG(DEBUG, TAG, "OUT");
1097 CAResult_t CALEGattDisConnect(const char *remoteAddress)
1099 OIC_LOG(DEBUG, TAG, "IN");
1101 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1102 "remote address is NULL", CA_STATUS_FAILED);
1104 int ret = bt_gatt_disconnect(remoteAddress);
1106 if (BT_ERROR_NONE != ret)
1108 OIC_LOG_V(ERROR, TAG, "bt_gatt_disconnect Failed with ret value [%s] ",
1109 CALEGetErrorMsg(ret));
1110 return CA_STATUS_FAILED;
1113 OIC_LOG(DEBUG, TAG, "OUT");
1114 return CA_STATUS_OK;
1117 CAResult_t CAUpdateCharacteristicsToGattServerImpl(LEServerInfo *serverInfo,
1118 const uint8_t *data, const uint32_t dataLen)
1120 OIC_LOG(DEBUG, TAG, "IN");
1122 VERIFY_NON_NULL(serverInfo, TAG, "Server Info is NULL");
1124 CALEGattStopDeviceScanning();
1126 OIC_LOG_V(DEBUG, TAG, "Updating the data of length [%d] to [%s] ", dataLen,
1127 serverInfo->remoteAddress);
1129 int result = bt_gatt_set_value(serverInfo->writeChar, (char *)data, dataLen);
1131 if (BT_ERROR_NONE != result)
1133 OIC_LOG_V(ERROR, TAG,
1134 "bt_gatt_set_value Failed with return val [%s]",
1135 CALEGetErrorMsg(result));
1139 result = bt_gatt_client_write_value(serverInfo->writeChar, CALEGattCharacteristicWriteCb,
1141 if (BT_ERROR_NONE != result)
1143 OIC_LOG_V(ERROR, TAG,
1144 "bt_gatt_client_write_value Failed with return val [%s]",
1145 CALEGetErrorMsg(result));
1149 // wait for callback for write Characteristic with success to sent data
1150 OIC_LOG_V(DEBUG, TAG, "callback flag is %d", g_isSignalSetFlag);
1151 oc_mutex_lock(g_threadWriteCharacteristicMutex);
1152 if (!g_isSignalSetFlag)
1154 OIC_LOG(DEBUG, TAG, "wait for callback to notify writeCharacteristic is success");
1155 if (OC_WAIT_SUCCESS != oc_cond_wait_for(g_threadWriteCharacteristicCond,
1156 g_threadWriteCharacteristicMutex,
1157 WAIT_TIME_WRITE_CHARACTERISTIC))
1159 g_isSignalSetFlag = false;
1160 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
1161 OIC_LOG(ERROR, TAG, "there is no response. write has failed");
1165 // reset flag set by writeCharacteristic Callback
1166 g_isSignalSetFlag = false;
1167 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
1169 oc_mutex_lock(g_scanMutex);
1170 if (g_isMulticastInProgress || g_isUnicastScanInProgress)
1172 if (CA_STATUS_OK != CALEGattStartDeviceScanning())
1174 OIC_LOG(ERROR, TAG, "Could not start device scanning");
1177 oc_mutex_unlock(g_scanMutex);
1178 OIC_LOG(DEBUG, TAG, "OUT");
1179 return CA_STATUS_OK;
1182 oc_mutex_lock(g_scanMutex);
1183 if (g_isMulticastInProgress || g_isUnicastScanInProgress)
1185 if (CA_STATUS_OK != CALEGattStartDeviceScanning())
1187 OIC_LOG(ERROR, TAG, "Could not start device scanning");
1190 oc_mutex_unlock(g_scanMutex);
1192 OIC_LOG(DEBUG, TAG, "OUT");
1193 return CA_STATUS_FAILED;
1196 void CADiscoverLEServicesThread(void *remoteAddress)
1198 OIC_LOG(DEBUG, TAG, "IN");
1200 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
1202 char *address = (char *)remoteAddress;
1204 CAResult_t result = CALEGattDiscoverServices(address);
1205 if (CA_STATUS_OK != result)
1207 OIC_LOG(ERROR, TAG, "CALEGattDiscoverServices failed");
1211 OIC_LOG(DEBUG, TAG, "OUT");
1214 CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
1216 OIC_LOG(DEBUG, TAG, "IN");
1218 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1219 "remote address is NULL", CA_STATUS_FAILED);
1221 LEServerInfo *serverInfo = NULL;
1222 oc_mutex_lock(g_LEServerListMutex);
1223 if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
1225 oc_mutex_unlock(g_LEServerListMutex);
1226 OIC_LOG_V(ERROR, TAG, "Could not get server info for [%s]", remoteAddress);
1227 CALEGattDisConnect(remoteAddress);
1228 return CA_STATUS_FAILED;
1231 bt_gatt_h serviceHandle = NULL;
1232 ret = bt_gatt_client_get_service(serverInfo->clientHandle, CA_GATT_SERVICE_UUID, &serviceHandle);
1233 if (BT_ERROR_NONE != ret || NULL == serviceHandle)
1235 OIC_LOG_V(ERROR, TAG,
1236 "bt_gatt_client_get_service Failed with ret value [%s] ", CALEGetErrorMsg(ret));
1237 bt_gatt_client_destroy(serverInfo->clientHandle);
1238 CALEGattDisConnect(remoteAddress);
1239 return CA_STATUS_FAILED;
1242 // Server will read data on this characteristic.
1243 bt_gatt_h writeChrHandle = NULL;
1244 ret = bt_gatt_service_get_characteristic(serviceHandle, CA_GATT_REQUEST_CHRC_UUID,
1246 if (BT_ERROR_NONE != ret || NULL == writeChrHandle)
1248 OIC_LOG_V(ERROR, TAG,
1249 "bt_gatt_service_get_characteristic Failed with ret value [%s] ",
1250 CALEGetErrorMsg(ret));
1251 bt_gatt_client_destroy(serverInfo->clientHandle);
1252 CALEGattDisConnect(remoteAddress);
1253 return CA_STATUS_FAILED;
1256 // Server will notify data on this characteristic.
1257 bt_gatt_h readChrHandle = NULL;
1258 ret = bt_gatt_service_get_characteristic(serviceHandle, CA_GATT_RESPONSE_CHRC_UUID,
1260 if (BT_ERROR_NONE != ret || NULL == readChrHandle)
1262 OIC_LOG_V(ERROR, TAG,
1263 "bt_gatt_service_get_characteristic Failed with ret value [%s] ",
1264 CALEGetErrorMsg(ret));
1265 bt_gatt_client_destroy(serverInfo->clientHandle);
1266 CALEGattDisConnect(remoteAddress);
1267 return CA_STATUS_FAILED;
1271 //TODO: This data has to be freed while unsetting the callback.
1272 char *addr = OICStrdup(remoteAddress);
1275 OIC_LOG(ERROR, TAG, "addr is NULL");
1276 bt_gatt_client_destroy(serverInfo->clientHandle);
1277 CALEGattDisConnect(remoteAddress);
1278 return CA_STATUS_FAILED;
1281 ret = bt_gatt_client_set_characteristic_value_changed_cb(readChrHandle,
1282 CALEGattCharacteristicChangedCb,
1284 if (BT_ERROR_NONE != ret)
1286 OIC_LOG_V(ERROR, TAG,
1287 "bt_gatt_client_set_characteristic_value_changed_cb Failed with ret value [%s]",
1288 CALEGetErrorMsg(ret));
1289 bt_gatt_client_destroy(serverInfo->clientHandle);
1290 CALEGattDisConnect(remoteAddress);
1291 return CA_STATUS_FAILED;
1294 serverInfo->serviceHandle = serviceHandle;
1295 serverInfo->readChar = readChrHandle;
1296 serverInfo->writeChar = writeChrHandle;
1297 serverInfo->status = LE_STATUS_SERVICES_DISCOVERED;
1299 while (serverInfo->pendingDataList)
1301 LEData *leData = serverInfo->pendingDataList->data;
1302 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(
1303 serverInfo, leData->data, leData->dataLength))
1305 OIC_LOG_V(ERROR, TAG, "Failed to send pending data to [%s]",
1306 serverInfo->remoteAddress);
1308 CADestroyLEDataList(&serverInfo->pendingDataList);
1311 CARemoveLEDataFromList(&serverInfo->pendingDataList);
1313 oc_mutex_unlock(g_LEServerListMutex);
1315 OIC_LOG(DEBUG, TAG, "OUT");
1316 return CA_STATUS_OK;
1319 CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
1320 const uint8_t *data, const uint32_t dataLen,
1321 CALETransferType_t type, const int32_t position)
1323 OIC_LOG(DEBUG, TAG, "IN");
1325 VERIFY_NON_NULL(data, TAG, "data is NULL");
1329 OIC_LOG(ERROR, TAG, "dataLen is less than or equal zero. Invalid input!");
1330 return CA_STATUS_INVALID_PARAM;
1333 LEServerInfo *serverInfo = NULL;
1334 oc_mutex_lock(g_LEServerListMutex);
1335 if (LE_UNICAST == type)
1337 if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
1339 OIC_LOG_V(DEBUG, TAG,
1340 "Device with address [%s] not yet found, initiating scan",
1343 char *addr = OICStrdup(remoteAddress);
1346 oc_mutex_unlock(g_LEServerListMutex);
1347 OIC_LOG(ERROR, TAG, "Device address is NULL");
1348 return CA_STATUS_FAILED;
1351 serverInfo = (LEServerInfo *)OICCalloc(1, sizeof(LEServerInfo));
1352 if (NULL == serverInfo)
1354 oc_mutex_unlock(g_LEServerListMutex);
1355 OIC_LOG(ERROR, TAG, "Calloc failed");
1357 return CA_STATUS_FAILED;
1360 serverInfo->remoteAddress = addr;
1361 serverInfo->status = LE_STATUS_UNICAST_PENDING;
1363 if (CA_STATUS_OK != CAAddLEServerInfoToList(&g_LEServerList, serverInfo))
1365 oc_mutex_unlock(g_LEServerListMutex);
1366 OIC_LOG_V(ERROR, TAG, "Could not add [%s] to server list", serverInfo->remoteAddress);
1367 CAFreeLEServerInfo(serverInfo);
1368 return CA_STATUS_FAILED;
1371 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1373 oc_mutex_unlock(g_LEServerListMutex);
1374 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1375 return CA_STATUS_FAILED;
1378 oc_mutex_unlock(g_LEServerListMutex);
1380 oc_mutex_lock(g_scanMutex);
1381 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
1383 CAResult_t result = CALEGattStartDeviceScanning();
1384 if (CA_STATUS_OK != result)
1386 oc_mutex_unlock(g_scanMutex);
1387 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning failed");
1388 return CA_STATUS_FAILED;
1390 g_isUnicastScanInProgress = true;
1392 oc_cond_signal(g_startTimerCond);
1396 g_isUnicastScanInProgress = true;
1398 oc_cond_signal(g_scanningTimeCond);
1400 oc_mutex_unlock(g_scanMutex);
1402 OIC_LOG(DEBUG, TAG, "OUT");
1403 return CA_STATUS_OK;
1406 if (serverInfo->status == LE_STATUS_DISCOVERED)
1408 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1410 oc_mutex_unlock(g_LEServerListMutex);
1411 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1412 return CA_STATUS_FAILED;
1415 bt_gatt_client_h clientHandle = NULL;
1416 int32_t ret = bt_gatt_client_create(remoteAddress, &clientHandle);
1417 if (BT_ERROR_NONE != ret || NULL == clientHandle)
1419 OIC_LOG_V(ERROR, TAG,
1420 "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret));
1421 CALEGattDisConnect(remoteAddress);
1422 return CA_STATUS_FAILED;
1424 serverInfo->clientHandle = clientHandle;
1426 serverInfo->status = LE_STATUS_CONNECTION_INITIATED;
1427 if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress))
1429 OIC_LOG_V(ERROR, TAG, "Could not initiate connection to [%s]", serverInfo->remoteAddress);
1430 serverInfo->status = LE_STATUS_DISCOVERED;
1431 CADestroyLEDataList(&serverInfo->pendingDataList);
1432 oc_mutex_unlock(g_LEServerListMutex);
1433 return CA_STATUS_FAILED;
1436 else if (serverInfo->status < LE_STATUS_SERVICES_DISCOVERED)
1438 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1440 oc_mutex_unlock(g_LEServerListMutex);
1441 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1442 return CA_STATUS_FAILED;
1447 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(serverInfo, data, dataLen))
1449 OIC_LOG_V(ERROR, TAG, "Could not update characteristic to gatt server [%s]",
1450 serverInfo->remoteAddress);
1451 oc_mutex_unlock(g_LEServerListMutex);
1452 return CA_STATUS_FAILED;
1456 else if (LE_MULTICAST == type)
1458 OIC_LOG(ERROR, TAG, "LE_MULTICAST type Not used");
1460 oc_mutex_unlock(g_LEServerListMutex);
1461 OIC_LOG(DEBUG, TAG, "OUT");
1462 return CA_STATUS_OK;
1465 CAResult_t CAUpdateCharacteristicsToAllGattServers(const uint8_t *data, uint32_t dataLen)
1467 OIC_LOG(DEBUG, TAG, "IN");
1469 VERIFY_NON_NULL(data, TAG, "data is NULL");
1473 OIC_LOG(ERROR, TAG, "dataLen is less than or equal zero. Invalid input !");
1474 return CA_STATUS_INVALID_PARAM;
1477 oc_mutex_lock(g_LEServerListMutex);
1478 LEServerInfoList *curNode = g_LEServerList;
1481 LEServerInfo *serverInfo = curNode->serverInfo;
1482 if (serverInfo->status == LE_STATUS_SERVICES_DISCOVERED)
1484 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(serverInfo, data, dataLen))
1486 OIC_LOG_V(ERROR, TAG, "Failed to update characteristics to gatt server [%s]",
1487 serverInfo->remoteAddress);
1490 else if (serverInfo->status != LE_STATUS_INVALID)
1492 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1494 OIC_LOG(ERROR, TAG, "Failed to add to pending list");
1497 curNode = curNode->next;
1499 oc_mutex_unlock(g_LEServerListMutex);
1501 // Add the data to pending list.
1502 LEData *multicastData = (LEData *)OICCalloc(1, sizeof(LEData));
1503 if (NULL == multicastData)
1505 OIC_LOG(ERROR, TAG, "Calloc failed");
1508 multicastData->data = OICCalloc(1, dataLen);
1509 if (NULL == multicastData->data)
1511 OIC_LOG(ERROR, TAG, "Calloc failed");
1514 memcpy(multicastData->data, data, dataLen);
1515 multicastData->dataLength = dataLen;
1517 oc_mutex_lock(g_multicastDataListMutex);
1518 if (NULL == g_multicastDataList)
1520 g_multicastDataList = u_arraylist_create();
1522 u_arraylist_add(g_multicastDataList, (void *)multicastData);
1523 oc_mutex_unlock(g_multicastDataListMutex);
1525 // Start the scanning, if not started, else reset timer
1526 oc_mutex_lock(g_scanMutex);
1527 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
1529 CAResult_t result = CALEGattStartDeviceScanning();
1530 if (CA_STATUS_OK != result)
1532 oc_mutex_unlock(g_scanMutex);
1533 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning Failed");
1536 g_isMulticastInProgress = true;
1537 // Start the timer by signalling it
1538 oc_cond_signal(g_startTimerCond);
1542 g_isMulticastInProgress = true;
1544 oc_cond_signal(g_scanningTimeCond);
1546 oc_mutex_unlock(g_scanMutex);
1549 OIC_LOG(DEBUG, TAG, "OUT ");
1550 return CA_STATUS_OK;