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 serverInfo->status = LE_STATUS_CONNECTION_INITIATED;
474 if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress))
476 serverInfo->status = LE_STATUS_DISCOVERED;
477 OIC_LOG_V(ERROR, TAG, "Could not initiate connection to [%s]", serverInfo->remoteAddress);
478 oc_mutex_unlock(g_LEServerListMutex);
481 oc_mutex_unlock(g_LEServerListMutex);
482 OIC_LOG(DEBUG, TAG, "OUT");
486 if (isMulticastInProgress)
488 if (CA_STATUS_OK != ret)
490 OIC_LOG_V(DEBUG, TAG,
491 "Newly discovered device with address [%s], adding to list", scanInfo->remote_address);
493 char *addr = OICStrdup(scanInfo->remote_address);
496 oc_mutex_unlock(g_LEServerListMutex);
497 OIC_LOG(ERROR, TAG, "Device address is NULL");
500 serverInfo = (LEServerInfo *)OICCalloc(1, sizeof(LEServerInfo));
501 if (NULL == serverInfo)
503 oc_mutex_unlock(g_LEServerListMutex);
504 OIC_LOG(ERROR, TAG, "Calloc failed");
508 serverInfo->remoteAddress = addr;
509 serverInfo->status = LE_STATUS_DISCOVERED;
511 if (CA_STATUS_OK != CAAddLEServerInfoToList(&g_LEServerList, serverInfo))
513 oc_mutex_unlock(g_LEServerListMutex);
514 OIC_LOG_V(ERROR, TAG, "Could not add [%s] to server list", scanInfo->remote_address);
515 CAFreeLEServerInfo(serverInfo);
519 if (!CALEIsHaveService(scanInfo, CA_GATT_SERVICE_UUID))
521 serverInfo->status = LE_STATUS_INVALID;
522 OIC_LOG_V(DEBUG, TAG, "Device [%s] does not support OIC service", serverInfo->remoteAddress);
523 oc_mutex_unlock(g_LEServerListMutex);
527 oc_mutex_lock(g_multicastDataListMutex);
528 uint32_t lengthData = u_arraylist_length(g_multicastDataList);
529 for (uint32_t len = 0; len < lengthData; ++len)
531 LEData *multicastData = (LEData *)u_arraylist_get(g_multicastDataList, len);
532 if (NULL == multicastData)
534 OIC_LOG(ERROR, TAG, "multicastData is NULL");
537 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList,
538 multicastData->data, multicastData->dataLength))
540 OIC_LOG(ERROR, TAG, "Failed to add to pending list");
544 oc_mutex_unlock(g_multicastDataListMutex);
547 if (serverInfo->status == LE_STATUS_DISCOVERED)
549 // Initiate connection if not yet initiated
550 serverInfo->status = LE_STATUS_CONNECTION_INITIATED;
551 if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress))
553 OIC_LOG_V(ERROR, TAG, "Could not initiate connection to [%s]", serverInfo->remoteAddress);
554 serverInfo->status = LE_STATUS_DISCOVERED;
559 OIC_LOG_V(DEBUG, TAG, "Device already discovered, status= [%d]", serverInfo->status);
563 oc_mutex_unlock(g_LEServerListMutex);
564 OIC_LOG(DEBUG, TAG, "OUT");
567 void CASetLEClientThreadPoolHandle(ca_thread_pool_t handle)
569 OIC_LOG(DEBUG, TAG, "IN");
571 oc_mutex_lock(g_LEClientThreadPoolMutex);
572 g_LEClientThreadPool = handle;
573 oc_mutex_unlock(g_LEClientThreadPoolMutex);
575 OIC_LOG(DEBUG, TAG, "OUT");
578 void CASetLEReqRespClientCallback(CABLEDataReceivedCallback callback)
580 OIC_LOG(DEBUG, TAG, "IN");
582 oc_mutex_lock(g_LEReqRespClientCbMutex);
584 g_LEClientDataReceivedCallback = callback;
586 oc_mutex_unlock(g_LEReqRespClientCbMutex);
588 OIC_LOG(DEBUG, TAG, "OUT");
591 void CASetBLEClientErrorHandleCallback(CABLEErrorHandleCallback callback)
593 g_clientErrorCallback = callback;
596 CAResult_t CAStartLEGattClient()
598 OIC_LOG(DEBUG, TAG, "IN");
600 oc_mutex_lock(g_LEClientStateMutex);
601 if (true == g_isLEGattClientStarted)
603 OIC_LOG(ERROR, TAG, "Gatt Client is already running!!");
604 oc_mutex_unlock(g_LEClientStateMutex);
605 return CA_STATUS_FAILED;
608 CAResult_t result = CALEGattSetCallbacks();
609 if (CA_STATUS_OK != result)
611 OIC_LOG(ERROR, TAG, "CABleGattSetCallbacks Failed");
612 oc_mutex_unlock(g_LEClientStateMutex);
613 CATerminateLEGattClient();
614 return CA_STATUS_FAILED;
617 g_isLEGattClientStarted = true;
618 oc_mutex_unlock(g_LEClientStateMutex);
620 oc_mutex_lock(g_LEClientThreadPoolMutex);
621 if (NULL == g_LEClientThreadPool)
623 OIC_LOG(ERROR, TAG, "gBleServerThreadPool is NULL");
624 CATerminateGattClientMutexVariables();
625 oc_mutex_unlock(g_LEClientThreadPoolMutex);
626 return CA_STATUS_FAILED;
629 result = ca_thread_pool_add_task(g_LEClientThreadPool, CAStartTimerThread,
631 if (CA_STATUS_OK != result)
633 OIC_LOG(ERROR, TAG, "ca_thread_pool_add_task failed");
634 CATerminateGattClientMutexVariables();
635 oc_mutex_unlock(g_LEClientThreadPoolMutex);
636 return CA_STATUS_FAILED;
638 oc_mutex_unlock(g_LEClientThreadPoolMutex);
640 OIC_LOG(DEBUG, TAG, "OUT");
644 void CAStartTimerThread(void *data)
648 OIC_LOG(DEBUG, TAG, "IN");
649 while (g_isLEGattClientStarted)
651 oc_mutex_lock(g_scanMutex);
652 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
654 OIC_LOG(DEBUG, TAG, "waiting....");
655 oc_cond_wait(g_startTimerCond, g_scanMutex);
656 OIC_LOG(DEBUG, TAG, "Wake up");
659 // Timed conditional wait for stopping the scan.
660 OCWaitResult_t ret = oc_cond_wait_for(g_scanningTimeCond, g_scanMutex,
662 if (OC_WAIT_TIMEDOUT == ret)
664 OIC_LOG(DEBUG, TAG, "Scan is timed Out");
666 CALEGattStopDeviceScanning();
668 if (g_isMulticastInProgress)
670 oc_mutex_lock(g_multicastDataListMutex);
671 // Clear the data list and device list.
672 u_arraylist_destroy(g_multicastDataList);
673 g_multicastDataList = NULL;
674 oc_mutex_unlock(g_multicastDataListMutex);
675 g_isMulticastInProgress = false;
677 g_isUnicastScanInProgress = false;
679 oc_mutex_unlock(g_scanMutex);
682 OIC_LOG(DEBUG, TAG, "OUT");
685 void CAStopLEGattClient()
687 OIC_LOG(DEBUG, TAG, "IN");
689 oc_mutex_lock(g_LEClientStateMutex);
691 if (false == g_isLEGattClientStarted)
693 OIC_LOG(ERROR, TAG, "Gatt Client is not running to stop");
694 oc_mutex_unlock(g_LEClientStateMutex);
698 CALEGattUnSetCallbacks();
700 CALEGattStopDeviceScanning();
702 g_isLEGattClientStarted = false;
704 // Signal the conditions waiting in Start timer.
705 oc_cond_signal(g_startTimerCond);
706 oc_cond_signal(g_scanningTimeCond);
708 // Destroy the multicast data list and device list if not empty.
709 if (NULL != g_multicastDataList)
711 oc_mutex_lock(g_multicastDataListMutex);
712 u_arraylist_destroy(g_multicastDataList);
713 g_multicastDataList = NULL;
714 oc_mutex_unlock(g_multicastDataListMutex);
717 oc_mutex_lock(g_LEServerListMutex);
718 CAFreeLEServerList(g_LEServerList);
719 g_LEServerList = NULL;
720 oc_mutex_unlock(g_LEServerListMutex);
722 oc_mutex_lock(g_threadWriteCharacteristicMutex);
723 oc_cond_signal(g_threadWriteCharacteristicCond);
724 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
726 GMainContext *context_event_loop = NULL;
727 // Required for waking up the thread which is running in gmain loop
728 if (NULL != g_eventLoop)
730 context_event_loop = g_main_loop_get_context(g_eventLoop);
732 if (context_event_loop)
734 OIC_LOG_V(DEBUG, TAG, "g_eventLoop context %p", (void *)context_event_loop);
735 g_main_context_wakeup(context_event_loop);
737 // Kill g main loops and kill threads.
738 g_main_loop_quit(g_eventLoop);
742 OIC_LOG(ERROR, TAG, "g_eventLoop context is NULL");
745 oc_mutex_unlock(g_LEClientStateMutex);
747 OIC_LOG(DEBUG, TAG, "OUT");
750 CAResult_t CAInitializeLEGattClient()
752 OIC_LOG(DEBUG, TAG, "Initialize GATT Client");
753 CAResult_t res = CAInitGattClientMutexVariables();
754 if (CA_STATUS_OK != res)
756 OIC_LOG(ERROR, TAG, "CAInitGattClientMutexVariables failed!");
757 CATerminateGattClientMutexVariables();
758 return CA_STATUS_FAILED;
763 void CATerminateLEGattClient()
765 OIC_LOG(DEBUG, TAG, "IN");
767 CATerminateGattClientMutexVariables();
769 OIC_LOG(DEBUG, TAG, "OUT");
772 CAResult_t CAInitGattClientMutexVariables()
774 OIC_LOG(DEBUG, TAG, "IN");
775 if (NULL == g_LEClientStateMutex)
777 g_LEClientStateMutex = oc_mutex_new();
778 if (NULL == g_LEClientStateMutex)
780 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
781 return CA_STATUS_FAILED;
785 if (NULL == g_LEServerListMutex)
787 g_LEServerListMutex = oc_mutex_new();
788 if (NULL == g_LEServerListMutex)
790 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
791 return CA_STATUS_FAILED;
795 if (NULL == g_LEReqRespClientCbMutex)
797 g_LEReqRespClientCbMutex = oc_mutex_new();
798 if (NULL == g_LEReqRespClientCbMutex)
800 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
801 return CA_STATUS_FAILED;
805 if (NULL == g_LEClientThreadPoolMutex)
807 g_LEClientThreadPoolMutex = oc_mutex_new();
808 if (NULL == g_LEClientThreadPoolMutex)
810 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
811 return CA_STATUS_FAILED;
815 if (NULL == g_LEClientConnectMutex)
817 g_LEClientConnectMutex = oc_mutex_new();
818 if (NULL == g_LEClientConnectMutex)
820 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
821 return CA_STATUS_FAILED;
825 if (NULL == g_isScanningInProgressMutex)
827 g_isScanningInProgressMutex = oc_mutex_new();
828 if (NULL == g_isScanningInProgressMutex)
830 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
831 return CA_STATUS_FAILED;
835 if (NULL == g_isConnectionInProgressMutex)
837 g_isConnectionInProgressMutex = oc_mutex_new();
838 if (NULL == g_isConnectionInProgressMutex)
840 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
841 return CA_STATUS_FAILED;
845 if (NULL == g_multicastDataListMutex)
847 g_multicastDataListMutex = oc_mutex_new();
848 if (NULL == g_multicastDataListMutex)
850 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
851 return CA_STATUS_FAILED;
855 if (NULL == g_scanMutex)
857 g_scanMutex = oc_mutex_new();
858 if (NULL == g_scanMutex)
860 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
861 return CA_STATUS_FAILED;
865 if (NULL == g_threadWriteCharacteristicMutex)
867 g_threadWriteCharacteristicMutex = oc_mutex_new();
868 if (NULL == g_threadWriteCharacteristicMutex)
870 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
871 return CA_STATUS_FAILED;
875 if (NULL == g_startTimerCond)
877 g_startTimerCond = oc_cond_new();
878 if (NULL == g_startTimerCond)
880 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
881 return CA_STATUS_FAILED;
885 if (NULL == g_scanningTimeCond)
887 g_scanningTimeCond = oc_cond_new();
888 if (NULL == g_scanningTimeCond)
890 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
891 return CA_STATUS_FAILED;
895 if (NULL == g_threadWriteCharacteristicCond)
897 g_threadWriteCharacteristicCond = oc_cond_new();
898 if (NULL == g_threadWriteCharacteristicCond)
900 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
901 return CA_STATUS_FAILED;
905 OIC_LOG(DEBUG, TAG, "OUT");
909 void CATerminateGattClientMutexVariables()
911 OIC_LOG(DEBUG, TAG, "IN");
913 oc_mutex_free(g_LEClientStateMutex);
914 g_LEClientStateMutex = NULL;
916 oc_mutex_free(g_LEServerListMutex);
917 g_LEServerListMutex = NULL;
919 oc_mutex_free(g_LEReqRespClientCbMutex);
920 g_LEReqRespClientCbMutex = NULL;
922 oc_mutex_free(g_LEClientConnectMutex);
923 g_LEClientConnectMutex = NULL;
925 oc_mutex_free(g_LEClientThreadPoolMutex);
926 g_LEClientThreadPoolMutex = NULL;
928 oc_mutex_free(g_isScanningInProgressMutex);
929 g_isScanningInProgressMutex = NULL;
931 oc_mutex_free(g_isConnectionInProgressMutex);
932 g_isConnectionInProgressMutex = NULL;
934 oc_mutex_free(g_multicastDataListMutex);
935 g_multicastDataListMutex = NULL;
937 oc_mutex_free(g_scanMutex);
940 oc_mutex_free(g_threadWriteCharacteristicMutex);
941 g_threadWriteCharacteristicMutex = NULL;
943 oc_cond_free(g_startTimerCond);
944 g_startTimerCond = NULL;
946 oc_cond_free(g_scanningTimeCond);
947 g_scanningTimeCond = NULL;
949 oc_cond_free(g_threadWriteCharacteristicCond);
950 g_threadWriteCharacteristicCond = NULL;
951 g_isSignalSetFlag = false;
953 OIC_LOG(DEBUG, TAG, "OUT");
956 CAResult_t CALEGattSetCallbacks()
958 OIC_LOG(DEBUG, TAG, "IN");
960 OIC_LOG(DEBUG, TAG, "OUT");
964 void CALEGattUnSetCallbacks()
966 OIC_LOG(DEBUG, TAG, "IN");
968 bt_gatt_unset_connection_state_changed_cb();
970 oc_mutex_lock(g_LEServerListMutex);
971 LEServerInfoList *curNode = g_LEServerList;
974 LEServerInfo *serverInfo = curNode->serverInfo;
975 if (serverInfo->status >= LE_STATUS_SERVICES_DISCOVERED)
977 bt_gatt_client_unset_characteristic_value_changed_cb(serverInfo->readChar);
979 curNode = curNode->next;
981 oc_mutex_unlock(g_LEServerListMutex);
983 OIC_LOG(DEBUG, TAG, "OUT");
986 CAResult_t CALEGattStartDeviceScanning()
988 OIC_LOG(DEBUG, TAG, "IN");
990 oc_mutex_lock(g_isScanningInProgressMutex);
991 if (!g_isScanningInProgress)
993 int ret = bt_adapter_le_start_scan(CALEAdapterScanResultCb, NULL);
994 if (BT_ERROR_NONE != ret)
996 oc_mutex_unlock(g_isScanningInProgressMutex);
997 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_start_scan failed[%s]",
998 CALEGetErrorMsg(ret));
999 return CA_STATUS_FAILED;
1001 g_isScanningInProgress = true;
1005 OIC_LOG(DEBUG, TAG, "Ignore, scanning already in progress");
1007 oc_mutex_unlock(g_isScanningInProgressMutex);
1009 OIC_LOG(DEBUG, TAG, "OUT");
1010 return CA_STATUS_OK;
1013 void CALEGattStopDeviceScanning()
1015 OIC_LOG(DEBUG, TAG, "IN");
1017 oc_mutex_lock(g_isScanningInProgressMutex);
1018 if (g_isScanningInProgress)
1020 int ret = bt_adapter_le_stop_scan();
1021 if (BT_ERROR_NONE != ret)
1023 oc_mutex_unlock(g_isScanningInProgressMutex);
1024 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_stop_scan failed[%s]",
1025 CALEGetErrorMsg(ret));
1028 g_isScanningInProgress = false;
1032 OIC_LOG(DEBUG, TAG, "Ignore, scanning not in progress");
1034 oc_mutex_unlock(g_isScanningInProgressMutex);
1036 OIC_LOG(DEBUG, TAG, "OUT");
1039 void CAGattConnectThread (void *remoteAddress)
1041 OIC_LOG(DEBUG, TAG, "IN ");
1043 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
1045 char *address = (char *)remoteAddress;
1047 OIC_LOG_V(DEBUG, TAG, "remote address is [%s]", address);
1049 CAResult_t result = CALEGattConnect(address);
1051 if (CA_STATUS_OK != result)
1053 OIC_LOG_V(ERROR, TAG, "bt_gatt_connect failed for [%s]", address);
1058 OIC_LOG(DEBUG, TAG, "OUT");
1061 CAResult_t CALEGattConnect(const char *remoteAddress)
1063 OIC_LOG(DEBUG, TAG, "IN");
1065 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1066 "remote address is NULL", CA_STATUS_FAILED);
1068 oc_mutex_lock(g_LEClientConnectMutex);
1069 CAResult_t result = CA_STATUS_OK;
1071 int ret = bt_gatt_connect(remoteAddress, false);
1072 if (BT_ERROR_NONE != ret)
1074 OIC_LOG_V(ERROR, TAG, "bt_gatt_connect Failed with ret value [%s] ",
1075 CALEGetErrorMsg(ret));
1076 oc_mutex_unlock(g_LEClientConnectMutex);
1077 return CA_STATUS_FAILED;
1080 oc_mutex_unlock(g_LEClientConnectMutex);
1082 OIC_LOG(DEBUG, TAG, "OUT");
1086 CAResult_t CALEGattDisConnect(const char *remoteAddress)
1088 OIC_LOG(DEBUG, TAG, "IN");
1090 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1091 "remote address is NULL", CA_STATUS_FAILED);
1093 int ret = bt_gatt_disconnect(remoteAddress);
1095 if (BT_ERROR_NONE != ret)
1097 OIC_LOG_V(ERROR, TAG, "bt_gatt_disconnect Failed with ret value [%s] ",
1098 CALEGetErrorMsg(ret));
1099 return CA_STATUS_FAILED;
1102 OIC_LOG(DEBUG, TAG, "OUT");
1103 return CA_STATUS_OK;
1106 CAResult_t CAUpdateCharacteristicsToGattServerImpl(LEServerInfo *serverInfo,
1107 const uint8_t *data, const uint32_t dataLen)
1109 OIC_LOG(DEBUG, TAG, "IN");
1111 VERIFY_NON_NULL(serverInfo, TAG, "Server Info is NULL");
1113 CALEGattStopDeviceScanning();
1115 OIC_LOG_V(DEBUG, TAG, "Updating the data of length [%d] to [%s] ", dataLen,
1116 serverInfo->remoteAddress);
1118 int result = bt_gatt_set_value(serverInfo->writeChar, (char *)data, dataLen);
1120 if (BT_ERROR_NONE != result)
1122 OIC_LOG_V(ERROR, TAG,
1123 "bt_gatt_set_value Failed with return val [%s]",
1124 CALEGetErrorMsg(result));
1128 result = bt_gatt_client_write_value(serverInfo->writeChar, CALEGattCharacteristicWriteCb,
1130 if (BT_ERROR_NONE != result)
1132 OIC_LOG_V(ERROR, TAG,
1133 "bt_gatt_client_write_value Failed with return val [%s]",
1134 CALEGetErrorMsg(result));
1138 // wait for callback for write Characteristic with success to sent data
1139 OIC_LOG_V(DEBUG, TAG, "callback flag is %d", g_isSignalSetFlag);
1140 oc_mutex_lock(g_threadWriteCharacteristicMutex);
1141 if (!g_isSignalSetFlag)
1143 OIC_LOG(DEBUG, TAG, "wait for callback to notify writeCharacteristic is success");
1144 if (OC_WAIT_SUCCESS != oc_cond_wait_for(g_threadWriteCharacteristicCond,
1145 g_threadWriteCharacteristicMutex,
1146 WAIT_TIME_WRITE_CHARACTERISTIC))
1148 g_isSignalSetFlag = false;
1149 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
1150 OIC_LOG(ERROR, TAG, "there is no response. write has failed");
1154 // reset flag set by writeCharacteristic Callback
1155 g_isSignalSetFlag = false;
1156 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
1158 oc_mutex_lock(g_scanMutex);
1159 if (g_isMulticastInProgress || g_isUnicastScanInProgress)
1161 if (CA_STATUS_OK != CALEGattStartDeviceScanning())
1163 OIC_LOG(ERROR, TAG, "Could not start device scanning");
1166 oc_mutex_unlock(g_scanMutex);
1167 OIC_LOG(DEBUG, TAG, "OUT");
1168 return CA_STATUS_OK;
1171 oc_mutex_lock(g_scanMutex);
1172 if (g_isMulticastInProgress || g_isUnicastScanInProgress)
1174 if (CA_STATUS_OK != CALEGattStartDeviceScanning())
1176 OIC_LOG(ERROR, TAG, "Could not start device scanning");
1179 oc_mutex_unlock(g_scanMutex);
1181 OIC_LOG(DEBUG, TAG, "OUT");
1182 return CA_STATUS_FAILED;
1185 void CADiscoverLEServicesThread(void *remoteAddress)
1187 OIC_LOG(DEBUG, TAG, "IN");
1189 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
1191 char *address = (char *)remoteAddress;
1193 CAResult_t result = CALEGattDiscoverServices(address);
1194 if (CA_STATUS_OK != result)
1196 OIC_LOG(ERROR, TAG, "CALEGattDiscoverServices failed");
1200 OIC_LOG(DEBUG, TAG, "OUT");
1203 CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
1205 OIC_LOG(DEBUG, TAG, "IN");
1207 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1208 "remote address is NULL", CA_STATUS_FAILED);
1210 bt_gatt_client_h clientHandle = NULL;
1211 int32_t ret = bt_gatt_client_create(remoteAddress, &clientHandle);
1212 if (BT_ERROR_NONE != ret || NULL == clientHandle)
1214 OIC_LOG_V(ERROR, TAG,
1215 "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret));
1216 CALEGattDisConnect(remoteAddress);
1217 return CA_STATUS_FAILED;
1220 bt_gatt_h serviceHandle = NULL;
1221 ret = bt_gatt_client_get_service(clientHandle, CA_GATT_SERVICE_UUID, &serviceHandle);
1222 if (BT_ERROR_NONE != ret || NULL == serviceHandle)
1224 OIC_LOG_V(ERROR, TAG,
1225 "bt_gatt_client_get_service Failed with ret value [%s] ", CALEGetErrorMsg(ret));
1226 bt_gatt_client_destroy(clientHandle);
1227 CALEGattDisConnect(remoteAddress);
1228 return CA_STATUS_FAILED;
1231 // Server will read data on this characteristic.
1232 bt_gatt_h writeChrHandle = NULL;
1233 ret = bt_gatt_service_get_characteristic(serviceHandle, CA_GATT_REQUEST_CHRC_UUID,
1235 if (BT_ERROR_NONE != ret || NULL == writeChrHandle)
1237 OIC_LOG_V(ERROR, TAG,
1238 "bt_gatt_service_get_characteristic Failed with ret value [%s] ",
1239 CALEGetErrorMsg(ret));
1240 bt_gatt_client_destroy(clientHandle);
1241 CALEGattDisConnect(remoteAddress);
1242 return CA_STATUS_FAILED;
1245 // Server will notify data on this characteristic.
1246 bt_gatt_h readChrHandle = NULL;
1247 ret = bt_gatt_service_get_characteristic(serviceHandle, CA_GATT_RESPONSE_CHRC_UUID,
1249 if (BT_ERROR_NONE != ret || NULL == readChrHandle)
1251 OIC_LOG_V(ERROR, TAG,
1252 "bt_gatt_service_get_characteristic Failed with ret value [%s] ",
1253 CALEGetErrorMsg(ret));
1254 bt_gatt_client_destroy(clientHandle);
1255 CALEGattDisConnect(remoteAddress);
1256 return CA_STATUS_FAILED;
1260 //TODO: This data has to be freed while unsetting the callback.
1261 char *addr = OICStrdup(remoteAddress);
1264 OIC_LOG(ERROR, TAG, "addr is NULL");
1265 bt_gatt_client_destroy(clientHandle);
1266 CALEGattDisConnect(remoteAddress);
1267 return CA_STATUS_FAILED;
1270 ret = bt_gatt_client_set_characteristic_value_changed_cb(readChrHandle,
1271 CALEGattCharacteristicChangedCb,
1273 if (BT_ERROR_NONE != ret)
1275 OIC_LOG_V(ERROR, TAG,
1276 "bt_gatt_client_set_characteristic_value_changed_cb Failed with ret value [%s]",
1277 CALEGetErrorMsg(ret));
1278 bt_gatt_client_destroy(clientHandle);
1279 CALEGattDisConnect(remoteAddress);
1280 return CA_STATUS_FAILED;
1283 LEServerInfo *serverInfo = NULL;
1284 oc_mutex_lock(g_LEServerListMutex);
1285 if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
1287 oc_mutex_unlock(g_LEServerListMutex);
1288 OIC_LOG_V(ERROR, TAG, "Could not get server info for [%s]", remoteAddress);
1289 bt_gatt_client_destroy(clientHandle);
1290 CALEGattDisConnect(remoteAddress);
1291 return CA_STATUS_FAILED;
1294 serverInfo->clientHandle = clientHandle;
1295 serverInfo->serviceHandle = serviceHandle;
1296 serverInfo->readChar = readChrHandle;
1297 serverInfo->writeChar = writeChrHandle;
1298 serverInfo->status = LE_STATUS_SERVICES_DISCOVERED;
1300 while (serverInfo->pendingDataList)
1302 LEData *leData = serverInfo->pendingDataList->data;
1303 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(
1304 serverInfo, leData->data, leData->dataLength))
1306 OIC_LOG_V(ERROR, TAG, "Failed to send pending data to [%s]",
1307 serverInfo->remoteAddress);
1309 CADestroyLEDataList(&serverInfo->pendingDataList);
1312 CARemoveLEDataFromList(&serverInfo->pendingDataList);
1314 oc_mutex_unlock(g_LEServerListMutex);
1316 OIC_LOG(DEBUG, TAG, "OUT");
1317 return CA_STATUS_OK;
1320 CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
1321 const uint8_t *data, const uint32_t dataLen,
1322 CALETransferType_t type, const int32_t position)
1324 OIC_LOG(DEBUG, TAG, "IN");
1326 VERIFY_NON_NULL(data, TAG, "data is NULL");
1330 OIC_LOG(ERROR, TAG, "dataLen is less than or equal zero. Invalid input!");
1331 return CA_STATUS_INVALID_PARAM;
1334 LEServerInfo *serverInfo = NULL;
1335 oc_mutex_lock(g_LEServerListMutex);
1336 if (LE_UNICAST == type)
1338 if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
1340 OIC_LOG_V(DEBUG, TAG,
1341 "Device with address [%s] not yet found, initiating scan",
1344 char *addr = OICStrdup(remoteAddress);
1347 oc_mutex_unlock(g_LEServerListMutex);
1348 OIC_LOG(ERROR, TAG, "Device address is NULL");
1349 return CA_STATUS_FAILED;
1352 serverInfo = (LEServerInfo *)OICCalloc(1, sizeof(LEServerInfo));
1353 if (NULL == serverInfo)
1355 oc_mutex_unlock(g_LEServerListMutex);
1356 OIC_LOG(ERROR, TAG, "Calloc failed");
1358 return CA_STATUS_FAILED;
1361 serverInfo->remoteAddress = addr;
1362 serverInfo->status = LE_STATUS_UNICAST_PENDING;
1364 if (CA_STATUS_OK != CAAddLEServerInfoToList(&g_LEServerList, serverInfo))
1366 oc_mutex_unlock(g_LEServerListMutex);
1367 OIC_LOG_V(ERROR, TAG, "Could not add [%s] to server list", serverInfo->remoteAddress);
1368 CAFreeLEServerInfo(serverInfo);
1369 return CA_STATUS_FAILED;
1372 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1374 oc_mutex_unlock(g_LEServerListMutex);
1375 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1376 return CA_STATUS_FAILED;
1379 oc_mutex_unlock(g_LEServerListMutex);
1381 oc_mutex_lock(g_scanMutex);
1382 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
1384 CAResult_t result = CALEGattStartDeviceScanning();
1385 if (CA_STATUS_OK != result)
1387 oc_mutex_unlock(g_scanMutex);
1388 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning failed");
1389 return CA_STATUS_FAILED;
1391 g_isUnicastScanInProgress = true;
1393 oc_cond_signal(g_startTimerCond);
1397 g_isUnicastScanInProgress = true;
1399 oc_cond_signal(g_scanningTimeCond);
1401 oc_mutex_unlock(g_scanMutex);
1403 OIC_LOG(DEBUG, TAG, "OUT");
1404 return CA_STATUS_OK;
1407 if (serverInfo->status == LE_STATUS_DISCOVERED)
1409 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1411 oc_mutex_unlock(g_LEServerListMutex);
1412 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1413 return CA_STATUS_FAILED;
1416 serverInfo->status = LE_STATUS_CONNECTION_INITIATED;
1417 if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress))
1419 OIC_LOG_V(ERROR, TAG, "Could not initiate connection to [%s]", serverInfo->remoteAddress);
1420 serverInfo->status = LE_STATUS_DISCOVERED;
1421 CADestroyLEDataList(&serverInfo->pendingDataList);
1422 oc_mutex_unlock(g_LEServerListMutex);
1423 return CA_STATUS_FAILED;
1426 else if (serverInfo->status < LE_STATUS_SERVICES_DISCOVERED)
1428 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1430 oc_mutex_unlock(g_LEServerListMutex);
1431 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1432 return CA_STATUS_FAILED;
1437 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(serverInfo, data, dataLen))
1439 OIC_LOG_V(ERROR, TAG, "Could not update characteristic to gatt server [%s]",
1440 serverInfo->remoteAddress);
1441 oc_mutex_unlock(g_LEServerListMutex);
1442 return CA_STATUS_FAILED;
1446 else if (LE_MULTICAST == type)
1448 OIC_LOG(ERROR, TAG, "LE_MULTICAST type Not used");
1450 oc_mutex_unlock(g_LEServerListMutex);
1451 OIC_LOG(DEBUG, TAG, "OUT");
1452 return CA_STATUS_OK;
1455 CAResult_t CAUpdateCharacteristicsToAllGattServers(const uint8_t *data, uint32_t dataLen)
1457 OIC_LOG(DEBUG, TAG, "IN");
1459 VERIFY_NON_NULL(data, TAG, "data is NULL");
1463 OIC_LOG(ERROR, TAG, "dataLen is less than or equal zero. Invalid input !");
1464 return CA_STATUS_INVALID_PARAM;
1467 oc_mutex_lock(g_LEServerListMutex);
1468 LEServerInfoList *curNode = g_LEServerList;
1471 LEServerInfo *serverInfo = curNode->serverInfo;
1472 if (serverInfo->status == LE_STATUS_SERVICES_DISCOVERED)
1474 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(serverInfo, data, dataLen))
1476 OIC_LOG_V(ERROR, TAG, "Failed to update characteristics to gatt server [%s]",
1477 serverInfo->remoteAddress);
1480 else if (serverInfo->status != LE_STATUS_INVALID)
1482 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1484 OIC_LOG(ERROR, TAG, "Failed to add to pending list");
1487 curNode = curNode->next;
1489 oc_mutex_unlock(g_LEServerListMutex);
1491 // Add the data to pending list.
1492 LEData *multicastData = (LEData *)OICCalloc(1, sizeof(LEData));
1493 if (NULL == multicastData)
1495 OIC_LOG(ERROR, TAG, "Calloc failed");
1498 multicastData->data = OICCalloc(1, dataLen);
1499 if (NULL == multicastData->data)
1501 OIC_LOG(ERROR, TAG, "Calloc failed");
1504 memcpy(multicastData->data, data, dataLen);
1505 multicastData->dataLength = dataLen;
1507 oc_mutex_lock(g_multicastDataListMutex);
1508 if (NULL == g_multicastDataList)
1510 g_multicastDataList = u_arraylist_create();
1512 u_arraylist_add(g_multicastDataList, (void *)multicastData);
1513 oc_mutex_unlock(g_multicastDataListMutex);
1515 // Start the scanning, if not started, else reset timer
1516 oc_mutex_lock(g_scanMutex);
1517 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
1519 CAResult_t result = CALEGattStartDeviceScanning();
1520 if (CA_STATUS_OK != result)
1522 oc_mutex_unlock(g_scanMutex);
1523 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning Failed");
1526 g_isMulticastInProgress = true;
1527 // Start the timer by signalling it
1528 oc_cond_signal(g_startTimerCond);
1532 g_isMulticastInProgress = true;
1534 oc_cond_signal(g_scanningTimeCond);
1536 oc_mutex_unlock(g_scanMutex);
1539 OIC_LOG(DEBUG, TAG, "OUT ");
1540 return CA_STATUS_OK;