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 //For custom uuid ble server
50 #define CA_GATT_CUSTOM_UUID "4209"
51 #define CA_GATT_CUSTOM_UUID2 "4204"
52 #define CUSTOM_UUID_LEN 4
54 static const int samsung_code = 117;
56 uint64_t const TIMEOUT = 30 * MICROSECS_PER_SEC;
59 * Flag to check if scanning is in progress
61 static bool g_isScanningInProgress = false;
64 * Mutex to synchronize access to g_isScanningInProgress
66 static oc_mutex g_isScanningInProgressMutex = NULL;
69 * Flag to check if connection is in progress
71 static bool g_isConnectionInProgress = false;
74 * Mutex to synchronize access to g_isConnectionInProgress
76 static oc_mutex g_isConnectionInProgressMutex = NULL;
79 * Flag to check if multicast is already in progress.
81 static bool g_isMulticastInProgress = false;
84 * Flag to check if unicast scan is in progress
86 static bool g_isUnicastScanInProgress = false;
89 * Mutex to synchronize access to g_isMulticastInProgress
90 * and g_isUnicastScanInProgress
92 static oc_mutex g_scanMutex = NULL;
95 * Pending multicast data list to be sent.
97 static u_arraylist_t *g_multicastDataList = NULL;
100 * Mutex to synchronize the access to Pending multicast data list.
102 static oc_mutex g_multicastDataListMutex = NULL;
105 * Condition to start the timer for scanning.
107 static oc_cond g_startTimerCond = NULL;
110 * Condition for scanning Time interval.
112 static oc_cond g_scanningTimeCond = NULL;
115 * This contains the list of OIC services a client connect tot.
117 static LEServerInfoList *g_LEServerList = NULL;
120 * Mutex to synchronize access to BleServiceList.
122 static oc_mutex g_LEServerListMutex = NULL;
125 * Boolean variable to keep the state of the GATT Client.
127 static bool g_isLEGattClientStarted = false;
130 * Mutex to synchronize access to the requestResponse callback to be called
131 * when the data needs to be sent from GATTClient.
133 static oc_mutex g_LEReqRespClientCbMutex = NULL;
136 * Mutex to synchronize access to the requestResponse callback to be called
137 * when the data needs to be sent from GATTClient.
139 static oc_mutex g_LEClientConnectMutex = NULL;
142 * Mutex to synchronize the calls to be done to the platform from GATTClient
143 * interfaces from different threads.
145 static oc_mutex g_LEClientStateMutex = NULL;
148 * Mutex to synchronize the task to be pushed to thread pool.
150 static oc_mutex g_LEClientThreadPoolMutex = NULL;
153 * Mutex to synchronize the task to write characteristic one packet after another.
155 static oc_mutex g_threadWriteCharacteristicMutex = NULL;
158 * Condition for Writing characteristic.
160 static oc_cond g_threadWriteCharacteristicCond = NULL;
163 * Mutex to synchronize the task for MTU Changed.
165 static oc_mutex g_threadMTUChangedMutex = NULL;
168 * Condition for MTU Changed.
170 static oc_cond g_threadMTUChangedCond = NULL;
173 * Flag to check status of write characteristic.
175 static bool g_isSignalSetFlag = false;
178 * Maintains the callback to be notified on receival of network packets from other
181 static CABLEDataReceivedCallback g_LEClientDataReceivedCallback = NULL;
184 * callback to update the error to le adapter
186 static CABLEErrorHandleCallback g_clientErrorCallback;
189 * gmainLoop to manage the threads to receive the callback from the platfrom.
191 static GMainLoop *g_eventLoop = NULL;
194 * Reference to threadpool
196 static ca_thread_pool_t g_LEClientThreadPool = NULL;
198 void CALEGattCharacteristicChangedCb(bt_gatt_h characteristic,
200 int valueLen, void *userData)
202 (void)characteristic;
204 OIC_LOG(DEBUG, TAG, "IN");
205 OIC_LOG_V(DEBUG, TAG, "Changed characteristic value length [%d]", valueLen);
207 oc_mutex_lock(g_LEReqRespClientCbMutex);
208 if (NULL == g_LEClientDataReceivedCallback)
210 OIC_LOG(ERROR, TAG, "Request response callback is not set");
211 oc_mutex_unlock(g_LEReqRespClientCbMutex);
215 uint32_t sentLength = 0;
216 g_LEClientDataReceivedCallback(userData, (uint8_t *)value, valueLen, &sentLength);
218 OIC_LOG_V(DEBUG, TAG, "Recv data Length is %d", sentLength);
220 oc_mutex_unlock(g_LEReqRespClientCbMutex);
222 OIC_LOG(DEBUG, TAG, "OUT");
225 void CALEGattClientMTUChangedCb(bt_gatt_client_h client_handle, const bt_gatt_client_att_mtu_info_s *mtu_info, void *user_data)
227 OIC_LOG(DEBUG, TAG, "IN");
228 oc_mutex_lock(g_threadMTUChangedMutex);
229 OIC_LOG(DEBUG, TAG, "MTU changed signal");
230 oc_cond_signal(g_threadMTUChangedCond);
231 oc_mutex_unlock(g_threadMTUChangedMutex);
232 OIC_LOG(DEBUG, TAG, "OUT");
236 void CALEGattCharacteristicWriteCb(int result, bt_gatt_h reqHandle, void *userData)
241 OIC_LOG(DEBUG, TAG, "IN ");
243 if (BT_ERROR_NONE != result)
245 CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, "", 0, -1,
246 false, "writeChar failure");
248 OIC_LOG(ERROR, TAG, "Write failed Need Retry ");
249 //Need to Implement retry mechanism
253 oc_mutex_lock(g_threadWriteCharacteristicMutex);
254 OIC_LOG(DEBUG, TAG, "g_isSignalSetFlag is set true and signal");
255 g_isSignalSetFlag = true;
256 oc_cond_signal(g_threadWriteCharacteristicCond);
257 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
259 CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, "", 0, -1,
260 true, "writeChar success");
263 OIC_LOG(DEBUG, TAG, "OUT ");
266 CAResult_t CALEGattInitiateConnection(const char *remoteAddress)
268 OIC_LOG(DEBUG, TAG, "IN");
270 oc_mutex_lock(g_isConnectionInProgressMutex);
271 if (g_isConnectionInProgress)
273 oc_mutex_unlock(g_isConnectionInProgressMutex);
274 OIC_LOG(DEBUG, TAG, "Connection already in progress, cannot initiate new connection");
275 return CA_STATUS_FAILED;
277 g_isConnectionInProgress = true;
278 oc_mutex_unlock(g_isConnectionInProgressMutex);
280 // Pause the scanning
281 CALEGattStopDeviceScanning();
283 OIC_LOG_V(DEBUG, TAG,
284 "Trying to do Gatt connection to [%s]", remoteAddress);
286 oc_mutex_lock(g_LEClientThreadPoolMutex);
287 if (NULL == g_LEClientThreadPool)
289 oc_mutex_unlock(g_LEClientThreadPoolMutex);
290 OIC_LOG(ERROR, TAG, "g_LEClientThreadPool is NULL");
291 return CA_STATUS_FAILED;
294 char *addr = OICStrdup(remoteAddress);
297 oc_mutex_unlock(g_LEClientThreadPoolMutex);
298 OIC_LOG(ERROR, TAG, "OICStrdup failed");
299 return CA_STATUS_FAILED;
302 CAResult_t res = ca_thread_pool_add_task(g_LEClientThreadPool, CAGattConnectThread, addr, NULL);
303 oc_mutex_unlock(g_LEClientThreadPoolMutex);
304 if (CA_STATUS_OK != res)
306 OIC_LOG_V(ERROR, TAG,
307 "ca_thread_pool_add_task failed with ret [%d]", res);
309 return CA_STATUS_FAILED;
311 OIC_LOG(DEBUG, TAG, "OUT");
315 void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress)
317 OIC_LOG(DEBUG, TAG, "IN ");
319 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
323 OIC_LOG_V(DEBUG, TAG, "DisConnected from [%s] ", remoteAddress);
324 oc_mutex_lock(g_LEServerListMutex);
325 CARemoveLEServerInfoFromList(&g_LEServerList, remoteAddress);
326 oc_mutex_unlock(g_LEServerListMutex);
330 OIC_LOG_V(DEBUG, TAG, "Connected to [%s] ", remoteAddress);
332 oc_mutex_lock(g_isConnectionInProgressMutex);
333 g_isConnectionInProgress = false;
334 oc_mutex_unlock(g_isConnectionInProgressMutex);
337 // TODO:Disabling scanning for now.Need to check.
338 /*oc_mutex_lock(g_scanMutex);
339 if (g_isMulticastInProgress || g_isUnicastScanInProgress)
341 CAResult_t ret = CALEGattStartDeviceScanning();
342 if (CA_STATUS_OK != ret)
344 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning Failed");
347 oc_mutex_unlock(g_scanMutex);
349 LEServerInfo *serverInfo = NULL;
350 oc_mutex_lock(g_LEServerListMutex);
351 if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
353 oc_mutex_unlock(g_LEServerListMutex);
354 OIC_LOG_V(ERROR, TAG, "Could not get server info for [%s]", remoteAddress);
358 serverInfo->status = LE_STATUS_CONNECTED;
359 oc_mutex_unlock(g_LEServerListMutex);
361 oc_mutex_lock(g_LEClientThreadPoolMutex);
362 if (NULL == g_LEClientThreadPool)
364 oc_mutex_unlock(g_LEClientThreadPoolMutex);
365 OIC_LOG(ERROR, TAG, "g_LEClientThreadPool is NULL");
369 char *addr = OICStrdup(remoteAddress);
372 oc_mutex_unlock(g_LEClientThreadPoolMutex);
373 OIC_LOG(ERROR, TAG, "addr is NULL");
377 CAResult_t ret = ca_thread_pool_add_task(g_LEClientThreadPool, CADiscoverLEServicesThread,
379 oc_mutex_unlock(g_LEClientThreadPoolMutex);
380 if (CA_STATUS_OK != ret)
382 OIC_LOG_V(ERROR, TAG, "ca_thread_pool_add_task failed with ret [%d]", ret);
386 OIC_LOG(DEBUG, TAG, "OUT");
389 static bool CALEIsHaveServiceImpl(bt_adapter_le_device_scan_result_info_s *scanInfo,
390 const char *service_uuid,
391 bt_adapter_le_packet_type_e pkt_type)
398 result = bt_adapter_le_get_scan_result_service_uuids(scanInfo,
399 pkt_type, &uuids, &count);
400 if (result == BT_ERROR_NONE && NULL != uuids)
402 for (int i = 0; i < count; i++)
404 if (0 == strcasecmp(uuids[i], service_uuid))
406 OIC_LOG_V(DEBUG, TAG, "Service[%s] Found in %s",
407 uuids[i], scanInfo->remote_address);
416 char *man_data = NULL;
419 result = bt_adapter_le_get_scan_result_manufacturer_data(scanInfo,
420 pkt_type, &man_id, &man_data, &man_data_len);
422 if (result == BT_ERROR_NONE && NULL != man_data)
424 char *compare_man_data = OICMalloc((man_data_len*2)+1);
426 for(int i=0;i<man_data_len;i++){
427 pos += sprintf(compare_man_data+pos, "%.2x", man_data[i]);
429 compare_man_data[(man_data_len*2)+1]='\0';
430 if (man_id == samsung_code && 0 == strncasecmp(compare_man_data, service_uuid, CUSTOM_UUID_LEN))
432 OIC_LOG_V(DEBUG, TAG, "Manufacture Data[%s] Found in %s",
433 compare_man_data, scanInfo->remote_address);
436 OICFree(compare_man_data);
443 static bool CALEIsHaveService(bt_adapter_le_device_scan_result_info_s *scanInfo,
444 const char *service_uuid)
447 // For arduino servers, scan response will give the UUIDs advertised.
448 CALEIsHaveServiceImpl(scanInfo, service_uuid,
449 BT_ADAPTER_LE_PACKET_SCAN_RESPONSE) ||
450 // For android/tizen servers, advertising packet will give the UUIDs.
451 CALEIsHaveServiceImpl(scanInfo, service_uuid,
452 BT_ADAPTER_LE_PACKET_ADVERTISING);
455 void CALEAdapterScanResultCb(int result, bt_adapter_le_device_scan_result_info_s *scanInfo,
459 OIC_LOG(DEBUG, TAG, "IN");
461 VERIFY_NON_NULL_VOID(scanInfo, TAG, "scanInfo");
462 VERIFY_NON_NULL_VOID(scanInfo->remote_address, TAG, "scanInfo->remote_address");
464 OIC_LOG_V(DEBUG, TAG, "Remote Address [%s]", scanInfo->remote_address);
465 OIC_LOG_V(DEBUG, TAG, "Scan Result [%d]", result);
466 OIC_LOG_V(DEBUG, TAG,
467 " Adv data len [%d] Scan data len[%d]RSSI [%d] Addr_type [%d] ",
468 scanInfo->adv_data_len, scanInfo->scan_data_len, scanInfo->rssi,
469 scanInfo->address_type);
471 // Check if scanning was stopped (since this callback is
472 // being triggered even after stopping the scan)
473 oc_mutex_lock(g_isScanningInProgressMutex);
474 if (!g_isScanningInProgress)
476 oc_mutex_unlock(g_isScanningInProgressMutex);
477 OIC_LOG(DEBUG, TAG, "Scanning not in progress, so ignoring callback");
480 oc_mutex_unlock(g_isScanningInProgressMutex);
482 if (CALEIsHaveService(scanInfo, CA_GATT_SERVICE_UUID) ||
483 CALEIsHaveService(scanInfo, CA_GATT_CUSTOM_UUID)||
484 CALEIsHaveService(scanInfo, CA_GATT_CUSTOM_UUID2))
486 OIC_LOG_V(DEBUG, TAG, "Device [%s] supports OIC or custom service", scanInfo->remote_address);
488 LEServerInfo *serverInfo = NULL;
489 oc_mutex_lock(g_LEServerListMutex);
490 CAResult_t ret = CAGetLEServerInfo(g_LEServerList, scanInfo->remote_address, &serverInfo);
491 if (CA_STATUS_OK != ret)
493 OIC_LOG_V(DEBUG, TAG,
494 "Newly discovered device with address [%s] ", scanInfo->remote_address);
496 char *addr = OICStrdup(scanInfo->remote_address);
499 oc_mutex_unlock(g_LEServerListMutex);
500 OIC_LOG(ERROR, TAG, "Device address is NULL");
503 serverInfo = (LEServerInfo *)OICCalloc(1, sizeof(LEServerInfo));
504 if (NULL == serverInfo)
506 oc_mutex_unlock(g_LEServerListMutex);
507 OIC_LOG(ERROR, TAG, "Calloc failed");
511 serverInfo->remoteAddress = addr;
512 serverInfo->status = LE_STATUS_DISCOVERED;
514 if (CA_STATUS_OK != CAAddLEServerInfoToList(&g_LEServerList, serverInfo))
516 oc_mutex_unlock(g_LEServerListMutex);
517 OIC_LOG_V(ERROR, TAG, "Could not add [%s] to server list", scanInfo->remote_address);
518 CAFreeLEServerInfo(serverInfo);
522 OIC_LOG_V(DEBUG, TAG,
523 "Device Present with address [%s] ", scanInfo->remote_address);
525 if(serverInfo->status == LE_STATUS_UNICAST_PENDING){
526 bt_gatt_client_h clientHandle = NULL;
527 int32_t ret = bt_gatt_client_create(serverInfo->remoteAddress, &clientHandle);
528 if (BT_ERROR_NONE != ret || NULL == clientHandle)
530 OIC_LOG_V(ERROR, TAG,
531 "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret));
532 CALEGattDisConnect(serverInfo->remoteAddress);
533 oc_mutex_unlock(g_LEServerListMutex);
536 serverInfo->clientHandle = clientHandle;
538 serverInfo->status = LE_STATUS_CONNECTION_INITIATED;
539 if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress))
541 OIC_LOG_V(ERROR, TAG, "Could not initiate connection to [%s]", serverInfo->remoteAddress);
542 serverInfo->status = LE_STATUS_DISCOVERED;
543 CADestroyLEDataList(&serverInfo->pendingDataList);
544 oc_mutex_unlock(g_LEServerListMutex);
550 oc_mutex_unlock(g_LEServerListMutex);
551 OIC_LOG(DEBUG, TAG, "OUT");
554 void CASetLEClientThreadPoolHandle(ca_thread_pool_t handle)
556 OIC_LOG(DEBUG, TAG, "IN");
558 oc_mutex_lock(g_LEClientThreadPoolMutex);
559 g_LEClientThreadPool = handle;
560 oc_mutex_unlock(g_LEClientThreadPoolMutex);
562 OIC_LOG(DEBUG, TAG, "OUT");
565 void CASetLEReqRespClientCallback(CABLEDataReceivedCallback callback)
567 OIC_LOG(DEBUG, TAG, "IN");
569 oc_mutex_lock(g_LEReqRespClientCbMutex);
571 g_LEClientDataReceivedCallback = callback;
573 oc_mutex_unlock(g_LEReqRespClientCbMutex);
575 OIC_LOG(DEBUG, TAG, "OUT");
578 void CASetBLEClientErrorHandleCallback(CABLEErrorHandleCallback callback)
580 g_clientErrorCallback = callback;
583 CAResult_t CAStartLEGattClient()
585 OIC_LOG(DEBUG, TAG, "IN");
587 oc_mutex_lock(g_LEClientStateMutex);
588 if (true == g_isLEGattClientStarted)
590 OIC_LOG(ERROR, TAG, "Gatt Client is already running!!");
591 oc_mutex_unlock(g_LEClientStateMutex);
592 return CA_STATUS_FAILED;
595 CAResult_t result = CALEGattSetCallbacks();
596 if (CA_STATUS_OK != result)
598 OIC_LOG(ERROR, TAG, "CABleGattSetCallbacks Failed");
599 oc_mutex_unlock(g_LEClientStateMutex);
600 CATerminateLEGattClient();
601 return CA_STATUS_FAILED;
604 g_isLEGattClientStarted = true;
605 oc_mutex_unlock(g_LEClientStateMutex);
607 oc_mutex_lock(g_LEClientThreadPoolMutex);
608 if (NULL == g_LEClientThreadPool)
610 OIC_LOG(ERROR, TAG, "gBleServerThreadPool is NULL");
611 CATerminateGattClientMutexVariables();
612 oc_mutex_unlock(g_LEClientThreadPoolMutex);
613 return CA_STATUS_FAILED;
616 result = ca_thread_pool_add_task(g_LEClientThreadPool, CAStartTimerThread,
618 if (CA_STATUS_OK != result)
620 OIC_LOG(ERROR, TAG, "ca_thread_pool_add_task failed");
621 CATerminateGattClientMutexVariables();
622 oc_mutex_unlock(g_LEClientThreadPoolMutex);
623 return CA_STATUS_FAILED;
626 result= ca_thread_pool_add_task(g_LEClientThreadPool, CALEClientScanThread,
628 if (CA_STATUS_OK != result)
630 OIC_LOG(ERROR, TAG, "ca_thread_pool_add_task failed");
631 CATerminateGattClientMutexVariables();
632 oc_mutex_unlock(g_LEClientThreadPoolMutex);
633 return CA_STATUS_FAILED;
635 oc_mutex_unlock(g_LEClientThreadPoolMutex);
637 OIC_LOG(DEBUG, TAG, "OUT");
641 void CALEClientScanThread()
643 oc_mutex_lock(g_scanMutex);
644 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
646 CAResult_t result = CALEGattStartDeviceScanning();
647 if (CA_STATUS_OK != result)
649 oc_mutex_unlock(g_scanMutex);
650 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning failed");
653 g_isUnicastScanInProgress = true;
655 oc_cond_signal(g_startTimerCond);
659 g_isUnicastScanInProgress = true;
661 oc_cond_signal(g_scanningTimeCond);
663 oc_mutex_unlock(g_scanMutex);
665 OIC_LOG(DEBUG, TAG, "OUT");
669 void CAStartTimerThread(void *data)
673 OIC_LOG(DEBUG, TAG, "IN");
674 while (g_isLEGattClientStarted)
676 oc_mutex_lock(g_scanMutex);
677 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
679 OIC_LOG(DEBUG, TAG, "waiting....");
680 oc_cond_wait(g_startTimerCond, g_scanMutex);
681 OIC_LOG(DEBUG, TAG, "Wake up");
684 // Timed conditional wait for stopping the scan.
685 OCWaitResult_t ret = oc_cond_wait_for(g_scanningTimeCond, g_scanMutex,
687 if (OC_WAIT_TIMEDOUT == ret)
689 OIC_LOG(DEBUG, TAG, "Scan is timed Out");
691 CALEGattStopDeviceScanning();
693 if (g_isMulticastInProgress)
695 oc_mutex_lock(g_multicastDataListMutex);
696 // Clear the data list and device list.
697 u_arraylist_destroy(g_multicastDataList);
698 g_multicastDataList = NULL;
699 oc_mutex_unlock(g_multicastDataListMutex);
700 g_isMulticastInProgress = false;
702 g_isUnicastScanInProgress = false;
704 oc_mutex_unlock(g_scanMutex);
707 OIC_LOG(DEBUG, TAG, "OUT");
710 void CAStopLEGattClient()
712 OIC_LOG(DEBUG, TAG, "IN");
714 oc_mutex_lock(g_LEClientStateMutex);
716 if (false == g_isLEGattClientStarted)
718 OIC_LOG(ERROR, TAG, "Gatt Client is not running to stop");
719 oc_mutex_unlock(g_LEClientStateMutex);
723 CALEGattUnSetCallbacks();
725 CALEGattStopDeviceScanning();
727 g_isLEGattClientStarted = false;
729 // Signal the conditions waiting in Start timer.
730 oc_cond_signal(g_startTimerCond);
731 oc_cond_signal(g_scanningTimeCond);
733 // Destroy the multicast data list and device list if not empty.
734 if (NULL != g_multicastDataList)
736 oc_mutex_lock(g_multicastDataListMutex);
737 u_arraylist_destroy(g_multicastDataList);
738 g_multicastDataList = NULL;
739 oc_mutex_unlock(g_multicastDataListMutex);
742 oc_mutex_lock(g_LEServerListMutex);
743 CAFreeLEServerList(g_LEServerList);
744 g_LEServerList = NULL;
745 oc_mutex_unlock(g_LEServerListMutex);
747 oc_mutex_lock(g_threadWriteCharacteristicMutex);
748 oc_cond_signal(g_threadWriteCharacteristicCond);
749 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
751 GMainContext *context_event_loop = NULL;
752 // Required for waking up the thread which is running in gmain loop
753 if (NULL != g_eventLoop)
755 context_event_loop = g_main_loop_get_context(g_eventLoop);
757 if (context_event_loop)
759 OIC_LOG_V(DEBUG, TAG, "g_eventLoop context %p", (void *)context_event_loop);
760 g_main_context_wakeup(context_event_loop);
762 // Kill g main loops and kill threads.
763 g_main_loop_quit(g_eventLoop);
767 OIC_LOG(ERROR, TAG, "g_eventLoop context is NULL");
770 oc_mutex_unlock(g_LEClientStateMutex);
772 OIC_LOG(DEBUG, TAG, "OUT");
775 CAResult_t CAInitializeLEGattClient()
777 OIC_LOG(DEBUG, TAG, "Initialize GATT Client");
778 CAResult_t res = CAInitGattClientMutexVariables();
779 if (CA_STATUS_OK != res)
781 OIC_LOG(ERROR, TAG, "CAInitGattClientMutexVariables failed!");
782 CATerminateGattClientMutexVariables();
783 return CA_STATUS_FAILED;
788 void CATerminateLEGattClient()
790 OIC_LOG(DEBUG, TAG, "IN");
792 CATerminateGattClientMutexVariables();
794 OIC_LOG(DEBUG, TAG, "OUT");
797 CAResult_t CAInitGattClientMutexVariables()
799 OIC_LOG(DEBUG, TAG, "IN");
800 if (NULL == g_LEClientStateMutex)
802 g_LEClientStateMutex = oc_mutex_new();
803 if (NULL == g_LEClientStateMutex)
805 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
806 return CA_STATUS_FAILED;
810 if (NULL == g_LEServerListMutex)
812 g_LEServerListMutex = oc_mutex_new();
813 if (NULL == g_LEServerListMutex)
815 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
816 return CA_STATUS_FAILED;
820 if (NULL == g_LEReqRespClientCbMutex)
822 g_LEReqRespClientCbMutex = oc_mutex_new();
823 if (NULL == g_LEReqRespClientCbMutex)
825 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
826 return CA_STATUS_FAILED;
830 if (NULL == g_LEClientThreadPoolMutex)
832 g_LEClientThreadPoolMutex = oc_mutex_new();
833 if (NULL == g_LEClientThreadPoolMutex)
835 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
836 return CA_STATUS_FAILED;
840 if (NULL == g_LEClientConnectMutex)
842 g_LEClientConnectMutex = oc_mutex_new();
843 if (NULL == g_LEClientConnectMutex)
845 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
846 return CA_STATUS_FAILED;
850 if (NULL == g_isScanningInProgressMutex)
852 g_isScanningInProgressMutex = oc_mutex_new();
853 if (NULL == g_isScanningInProgressMutex)
855 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
856 return CA_STATUS_FAILED;
860 if (NULL == g_isConnectionInProgressMutex)
862 g_isConnectionInProgressMutex = oc_mutex_new();
863 if (NULL == g_isConnectionInProgressMutex)
865 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
866 return CA_STATUS_FAILED;
870 if (NULL == g_multicastDataListMutex)
872 g_multicastDataListMutex = oc_mutex_new();
873 if (NULL == g_multicastDataListMutex)
875 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
876 return CA_STATUS_FAILED;
880 if (NULL == g_scanMutex)
882 g_scanMutex = oc_mutex_new();
883 if (NULL == g_scanMutex)
885 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
886 return CA_STATUS_FAILED;
890 if (NULL == g_threadWriteCharacteristicMutex)
892 g_threadWriteCharacteristicMutex = oc_mutex_new();
893 if (NULL == g_threadWriteCharacteristicMutex)
895 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
896 return CA_STATUS_FAILED;
900 if (NULL == g_threadMTUChangedMutex)
902 g_threadMTUChangedMutex = oc_mutex_new();
903 if (NULL == g_threadMTUChangedMutex)
905 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
906 return CA_STATUS_FAILED;
910 if (NULL == g_startTimerCond)
912 g_startTimerCond = oc_cond_new();
913 if (NULL == g_startTimerCond)
915 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
916 return CA_STATUS_FAILED;
920 if (NULL == g_scanningTimeCond)
922 g_scanningTimeCond = oc_cond_new();
923 if (NULL == g_scanningTimeCond)
925 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
926 return CA_STATUS_FAILED;
930 if (NULL == g_threadWriteCharacteristicCond)
932 g_threadWriteCharacteristicCond = oc_cond_new();
933 if (NULL == g_threadWriteCharacteristicCond)
935 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
936 return CA_STATUS_FAILED;
940 if (NULL == g_threadMTUChangedCond)
942 g_threadMTUChangedCond = oc_cond_new();
943 if (NULL == g_threadMTUChangedCond)
945 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
946 return CA_STATUS_FAILED;
951 OIC_LOG(DEBUG, TAG, "OUT");
955 void CATerminateGattClientMutexVariables()
957 OIC_LOG(DEBUG, TAG, "IN");
959 oc_mutex_free(g_LEClientStateMutex);
960 g_LEClientStateMutex = NULL;
962 oc_mutex_free(g_LEServerListMutex);
963 g_LEServerListMutex = NULL;
965 oc_mutex_free(g_LEReqRespClientCbMutex);
966 g_LEReqRespClientCbMutex = NULL;
968 oc_mutex_free(g_LEClientConnectMutex);
969 g_LEClientConnectMutex = NULL;
971 oc_mutex_free(g_LEClientThreadPoolMutex);
972 g_LEClientThreadPoolMutex = NULL;
974 oc_mutex_free(g_isScanningInProgressMutex);
975 g_isScanningInProgressMutex = NULL;
977 oc_mutex_free(g_isConnectionInProgressMutex);
978 g_isConnectionInProgressMutex = NULL;
980 oc_mutex_free(g_multicastDataListMutex);
981 g_multicastDataListMutex = NULL;
983 oc_mutex_free(g_scanMutex);
986 oc_mutex_free(g_threadWriteCharacteristicMutex);
987 g_threadWriteCharacteristicMutex = NULL;
989 oc_mutex_free(g_threadMTUChangedMutex);
990 g_threadMTUChangedMutex = NULL;
992 oc_cond_free(g_startTimerCond);
993 g_startTimerCond = NULL;
995 oc_cond_free(g_scanningTimeCond);
996 g_scanningTimeCond = NULL;
998 oc_cond_free(g_threadWriteCharacteristicCond);
999 g_threadWriteCharacteristicCond = NULL;
1000 g_isSignalSetFlag = false;
1002 oc_cond_free(g_threadMTUChangedCond);
1003 g_threadMTUChangedCond = NULL;
1005 OIC_LOG(DEBUG, TAG, "OUT");
1008 CAResult_t CALEGattSetCallbacks()
1010 OIC_LOG(DEBUG, TAG, "IN");
1012 OIC_LOG(DEBUG, TAG, "OUT");
1013 return CA_STATUS_OK;
1016 void CALEGattUnSetCallbacks()
1018 OIC_LOG(DEBUG, TAG, "IN");
1020 bt_gatt_unset_connection_state_changed_cb();
1022 oc_mutex_lock(g_LEServerListMutex);
1023 LEServerInfoList *curNode = g_LEServerList;
1026 LEServerInfo *serverInfo = curNode->serverInfo;
1027 if (serverInfo->status >= LE_STATUS_SERVICES_DISCOVERED)
1029 bt_gatt_client_unset_characteristic_value_changed_cb(serverInfo->readChar);
1031 curNode = curNode->next;
1033 oc_mutex_unlock(g_LEServerListMutex);
1035 OIC_LOG(DEBUG, TAG, "OUT");
1038 CAResult_t CALEGattStartDeviceScanning()
1040 OIC_LOG(DEBUG, TAG, "IN");
1042 oc_mutex_lock(g_isScanningInProgressMutex);
1043 if (!g_isScanningInProgress)
1045 int ret = bt_adapter_le_start_scan(CALEAdapterScanResultCb, NULL);
1046 if (BT_ERROR_NONE != ret)
1048 oc_mutex_unlock(g_isScanningInProgressMutex);
1049 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_start_scan failed[%s]",
1050 CALEGetErrorMsg(ret));
1051 return CA_STATUS_FAILED;
1053 g_isScanningInProgress = true;
1057 OIC_LOG(DEBUG, TAG, "Ignore, scanning already in progress");
1059 oc_mutex_unlock(g_isScanningInProgressMutex);
1061 OIC_LOG(DEBUG, TAG, "OUT");
1062 return CA_STATUS_OK;
1065 void CALEGattStopDeviceScanning()
1067 OIC_LOG(DEBUG, TAG, "IN");
1069 oc_mutex_lock(g_isScanningInProgressMutex);
1070 if (g_isScanningInProgress)
1072 int ret = bt_adapter_le_stop_scan();
1073 if (BT_ERROR_NONE != ret)
1075 oc_mutex_unlock(g_isScanningInProgressMutex);
1076 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_stop_scan failed[%s]",
1077 CALEGetErrorMsg(ret));
1080 g_isScanningInProgress = false;
1081 g_isUnicastScanInProgress= false;
1085 OIC_LOG(DEBUG, TAG, "Ignore, scanning not in progress");
1087 oc_mutex_unlock(g_isScanningInProgressMutex);
1089 OIC_LOG(DEBUG, TAG, "OUT");
1092 void CAGattConnectThread (void *remoteAddress)
1094 OIC_LOG(DEBUG, TAG, "IN ");
1096 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
1098 char *address = (char *)remoteAddress;
1100 OIC_LOG_V(DEBUG, TAG, "remote address is [%s]", address);
1102 CAResult_t result = CALEGattConnect(address);
1104 if (CA_STATUS_OK != result)
1106 OIC_LOG_V(ERROR, TAG, "bt_gatt_connect failed for [%s]", address);
1111 OIC_LOG(DEBUG, TAG, "OUT");
1114 CAResult_t CALEGattConnect(const char *remoteAddress)
1116 OIC_LOG(DEBUG, TAG, "IN");
1118 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1119 "remote address is NULL", CA_STATUS_FAILED);
1121 oc_mutex_lock(g_LEClientConnectMutex);
1122 CAResult_t result = CA_STATUS_OK;
1124 int ret = bt_gatt_connect(remoteAddress, false);
1125 if (BT_ERROR_NONE != ret)
1127 OIC_LOG_V(ERROR, TAG, "bt_gatt_connect Failed with ret value [%s] ",
1128 CALEGetErrorMsg(ret));
1129 oc_mutex_unlock(g_LEClientConnectMutex);
1130 return CA_STATUS_FAILED;
1133 oc_mutex_unlock(g_LEClientConnectMutex);
1135 OIC_LOG(DEBUG, TAG, "OUT");
1139 CAResult_t CALEGattDisConnect(const char *remoteAddress)
1141 OIC_LOG(DEBUG, TAG, "IN");
1143 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1144 "remote address is NULL", CA_STATUS_FAILED);
1146 int ret = bt_gatt_disconnect(remoteAddress);
1148 if (BT_ERROR_NONE != ret)
1150 OIC_LOG_V(ERROR, TAG, "bt_gatt_disconnect Failed with ret value [%s] ",
1151 CALEGetErrorMsg(ret));
1152 return CA_STATUS_FAILED;
1155 OIC_LOG(DEBUG, TAG, "OUT");
1156 return CA_STATUS_OK;
1159 CAResult_t CAUpdateCharacteristicsToGattServerImpl(LEServerInfo *serverInfo,
1160 const uint8_t *data, const uint32_t dataLen)
1162 OIC_LOG(DEBUG, TAG, "IN");
1164 VERIFY_NON_NULL(serverInfo, TAG, "Server Info is NULL");
1166 CALEGattStopDeviceScanning();
1168 OIC_LOG_V(DEBUG, TAG, "Updating the data of length [%d] to [%s] ", dataLen,
1169 serverInfo->remoteAddress);
1171 int result = bt_gatt_set_value(serverInfo->writeChar, (char *)data, dataLen);
1173 if (BT_ERROR_NONE != result)
1175 OIC_LOG_V(ERROR, TAG,
1176 "bt_gatt_set_value Failed with return val [%s]",
1177 CALEGetErrorMsg(result));
1181 result = bt_gatt_client_write_value(serverInfo->writeChar, CALEGattCharacteristicWriteCb,
1183 if (BT_ERROR_NONE != result)
1185 OIC_LOG_V(ERROR, TAG,
1186 "bt_gatt_client_write_value Failed with return val [%s]",
1187 CALEGetErrorMsg(result));
1191 // wait for callback for write Characteristic with success to sent data
1192 OIC_LOG_V(DEBUG, TAG, "callback flag is %d", g_isSignalSetFlag);
1193 oc_mutex_lock(g_threadWriteCharacteristicMutex);
1194 if (!g_isSignalSetFlag)
1196 OIC_LOG(DEBUG, TAG, "wait for callback to notify writeCharacteristic is success");
1197 if (OC_WAIT_SUCCESS != oc_cond_wait_for(g_threadWriteCharacteristicCond,
1198 g_threadWriteCharacteristicMutex,
1199 WAIT_TIME_WRITE_CHARACTERISTIC))
1201 g_isSignalSetFlag = false;
1202 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
1203 OIC_LOG(ERROR, TAG, "there is no response. write has failed");
1207 // reset flag set by writeCharacteristic Callback
1208 g_isSignalSetFlag = false;
1209 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
1211 oc_mutex_lock(g_scanMutex);
1212 if (g_isMulticastInProgress || g_isUnicastScanInProgress)
1214 if (CA_STATUS_OK != CALEGattStartDeviceScanning())
1216 OIC_LOG(ERROR, TAG, "Could not start device scanning");
1219 oc_mutex_unlock(g_scanMutex);
1220 OIC_LOG(DEBUG, TAG, "OUT");
1221 return CA_STATUS_OK;
1224 oc_mutex_lock(g_scanMutex);
1225 if (g_isMulticastInProgress || g_isUnicastScanInProgress)
1227 if (CA_STATUS_OK != CALEGattStartDeviceScanning())
1229 OIC_LOG(ERROR, TAG, "Could not start device scanning");
1232 oc_mutex_unlock(g_scanMutex);
1234 OIC_LOG(DEBUG, TAG, "OUT");
1235 return CA_STATUS_FAILED;
1238 void CADiscoverLEServicesThread(void *remoteAddress)
1240 OIC_LOG(DEBUG, TAG, "IN");
1242 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
1244 char *address = (char *)remoteAddress;
1246 CAResult_t result = CALEGattDiscoverServices(address);
1247 if (CA_STATUS_OK != result)
1249 OIC_LOG(ERROR, TAG, "CALEGattDiscoverServices failed");
1253 OIC_LOG(DEBUG, TAG, "OUT");
1256 CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
1258 OIC_LOG(DEBUG, TAG, "IN");
1260 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1261 "remote address is NULL", CA_STATUS_FAILED);
1263 LEServerInfo *serverInfo = NULL;
1264 unsigned int mtu_size;
1265 oc_mutex_lock(g_LEServerListMutex);
1266 if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
1268 oc_mutex_unlock(g_LEServerListMutex);
1269 OIC_LOG_V(ERROR, TAG, "Could not get server info for [%s]", remoteAddress);
1270 CALEGattDisConnect(remoteAddress);
1271 return CA_STATUS_FAILED;
1274 bt_gatt_h serviceHandle = NULL;
1275 int32_t ret = bt_gatt_client_get_service(serverInfo->clientHandle, CA_GATT_SERVICE_UUID, &serviceHandle);
1276 if (BT_ERROR_NONE != ret || NULL == serviceHandle)
1278 OIC_LOG_V(ERROR, TAG,
1279 "bt_gatt_client_get_service Failed with ret value [%s] ", CALEGetErrorMsg(ret));
1283 // Server will read data on this characteristic.
1284 bt_gatt_h writeChrHandle = NULL;
1285 ret = bt_gatt_service_get_characteristic(serviceHandle, CA_GATT_REQUEST_CHRC_UUID,
1287 if (BT_ERROR_NONE != ret || NULL == writeChrHandle)
1289 OIC_LOG_V(ERROR, TAG,
1290 "bt_gatt_service_get_characteristic Failed with ret value [%s] ",
1291 CALEGetErrorMsg(ret));
1295 // Server will notify data on this characteristic.
1296 bt_gatt_h readChrHandle = NULL;
1297 ret = bt_gatt_service_get_characteristic(serviceHandle, CA_GATT_RESPONSE_CHRC_UUID,
1299 if (BT_ERROR_NONE != ret || NULL == readChrHandle)
1301 OIC_LOG_V(ERROR, TAG,
1302 "bt_gatt_service_get_characteristic Failed with ret value [%s] ",
1303 CALEGetErrorMsg(ret));
1307 //TODO: This data has to be freed while unsetting the callback.
1308 char *addr = OICStrdup(remoteAddress);
1311 OIC_LOG(ERROR, TAG, "addr is NULL");
1315 ret = bt_gatt_client_set_characteristic_value_changed_cb(readChrHandle,
1316 CALEGattCharacteristicChangedCb,
1318 if (BT_ERROR_NONE != ret)
1320 OIC_LOG_V(ERROR, TAG,
1321 "bt_gatt_client_set_characteristic_value_changed_cb Failed with ret value [%s]",
1322 CALEGetErrorMsg(ret));
1326 ret = bt_gatt_client_set_att_mtu_changed_cb(serverInfo->clientHandle,
1327 CALEGattClientMTUChangedCb,
1328 (void *)serverInfo->remoteAddress);
1329 if (BT_ERROR_NONE != ret)
1331 OIC_LOG_V(ERROR, TAG,
1332 "bt_gatt_client_set_att_mtu_changed_cb Failed with ret value [%s]",
1333 CALEGetErrorMsg(ret));
1337 oc_mutex_lock(g_threadMTUChangedMutex);
1338 ret = bt_gatt_client_request_att_mtu_change(serverInfo->clientHandle, CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_MTU_HEADER_SIZE);
1339 if (BT_ERROR_NONE != ret)
1341 OIC_LOG_V(ERROR, TAG,
1342 "bt_gatt_client_request_att_mtu_change Failed with ret value [%s]",
1343 CALEGetErrorMsg(ret));
1344 oc_mutex_unlock(g_threadMTUChangedMutex);
1348 OIC_LOG(DEBUG, TAG, "wait for callback to notify MTU Changed Callback");
1349 oc_cond_wait(g_threadMTUChangedCond, g_threadMTUChangedMutex);
1350 oc_mutex_unlock(g_threadMTUChangedMutex);
1352 OIC_LOG(DEBUG, TAG, "Done MTU");
1353 ret = bt_gatt_client_get_att_mtu(serverInfo->clientHandle, &mtu_size);
1354 if (BT_ERROR_NONE != ret)
1356 OIC_LOG_V(ERROR, TAG,
1357 "bt_gatt_client_get_att_mtu Failed with ret value [%s]",
1358 CALEGetErrorMsg(ret));
1361 OIC_LOG_V(DEBUG, TAG,"Negotiated MTU Size %d",mtu_size);
1363 serverInfo->serviceHandle = serviceHandle;
1364 serverInfo->readChar = readChrHandle;
1365 serverInfo->writeChar = writeChrHandle;
1366 serverInfo->status = LE_STATUS_SERVICES_DISCOVERED;
1367 serverInfo->mtu_size = mtu_size;
1369 while (serverInfo->pendingDataList)
1371 LEData *leData = serverInfo->pendingDataList->data;
1372 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(
1373 serverInfo, leData->data, leData->dataLength))
1375 OIC_LOG_V(ERROR, TAG, "Failed to send pending data to [%s]",
1376 serverInfo->remoteAddress);
1378 CADestroyLEDataList(&serverInfo->pendingDataList);
1381 CARemoveLEDataFromList(&serverInfo->pendingDataList);
1383 oc_mutex_unlock(g_LEServerListMutex);
1385 OIC_LOG(DEBUG, TAG, "OUT");
1386 return CA_STATUS_OK;
1389 bt_gatt_client_destroy(serverInfo->clientHandle);
1390 serverInfo->clientHandle = NULL;
1391 oc_mutex_unlock(g_LEServerListMutex);
1392 CALEGattDisConnect(remoteAddress);
1393 return CA_STATUS_FAILED;
1396 CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
1397 const uint8_t *data, const uint32_t dataLen,
1398 CALETransferType_t type, const int32_t position)
1400 OIC_LOG(DEBUG, TAG, "IN");
1402 VERIFY_NON_NULL(data, TAG, "data is NULL");
1406 OIC_LOG(ERROR, TAG, "dataLen is less than or equal zero. Invalid input!");
1407 return CA_STATUS_INVALID_PARAM;
1410 LEServerInfo *serverInfo = NULL;
1411 oc_mutex_lock(g_LEServerListMutex);
1412 if (LE_UNICAST == type)
1414 if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
1416 OIC_LOG_V(DEBUG, TAG,
1417 "Device with address [%s] not yet found, initiating scan",
1420 char *addr = OICStrdup(remoteAddress);
1423 oc_mutex_unlock(g_LEServerListMutex);
1424 OIC_LOG(ERROR, TAG, "Device address is NULL");
1425 return CA_STATUS_FAILED;
1427 serverInfo = (LEServerInfo *)OICCalloc(1, sizeof(LEServerInfo));
1428 if (NULL == serverInfo)
1430 oc_mutex_unlock(g_LEServerListMutex);
1431 OIC_LOG(ERROR, TAG, "Calloc failed");
1433 return CA_STATUS_FAILED;
1435 serverInfo->remoteAddress = addr;
1436 serverInfo->status = LE_STATUS_UNICAST_PENDING;
1438 if (CA_STATUS_OK != CAAddLEServerInfoToList(&g_LEServerList, serverInfo))
1440 oc_mutex_unlock(g_LEServerListMutex);
1441 OIC_LOG_V(ERROR, TAG, "Could not add [%s] to server list", remoteAddress);
1442 CAFreeLEServerInfo(serverInfo);
1443 return CA_STATUS_FAILED;
1446 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1448 oc_mutex_unlock(g_LEServerListMutex);
1449 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1450 return CA_STATUS_FAILED;
1453 oc_mutex_lock(g_scanMutex);
1454 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
1456 CAResult_t result = CALEGattStartDeviceScanning();
1457 if (CA_STATUS_OK != result)
1459 oc_mutex_unlock(g_scanMutex);
1460 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning failed");
1461 return CA_STATUS_FAILED;
1463 g_isUnicastScanInProgress = true;
1465 oc_cond_signal(g_startTimerCond);
1469 g_isUnicastScanInProgress = true;
1471 oc_cond_signal(g_scanningTimeCond);
1473 oc_mutex_unlock(g_scanMutex);
1477 if (serverInfo->status == LE_STATUS_DISCOVERED)
1479 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1481 oc_mutex_unlock(g_LEServerListMutex);
1482 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1483 return CA_STATUS_FAILED;
1486 bt_gatt_client_h clientHandle = NULL;
1487 int32_t ret = bt_gatt_client_create(serverInfo->remoteAddress, &clientHandle);
1488 if (BT_ERROR_NONE != ret || NULL == clientHandle)
1490 OIC_LOG_V(ERROR, TAG,
1491 "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret));
1492 CALEGattDisConnect(serverInfo->remoteAddress);
1493 return CA_STATUS_FAILED;
1495 serverInfo->clientHandle = clientHandle;
1497 serverInfo->status = LE_STATUS_CONNECTION_INITIATED;
1498 if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress))
1500 OIC_LOG_V(ERROR, TAG, "Could not initiate connection to [%s]", serverInfo->remoteAddress);
1501 serverInfo->status = LE_STATUS_DISCOVERED;
1502 CADestroyLEDataList(&serverInfo->pendingDataList);
1503 oc_mutex_unlock(g_LEServerListMutex);
1504 return CA_STATUS_FAILED;
1507 else if (serverInfo->status < LE_STATUS_SERVICES_DISCOVERED)
1509 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1511 oc_mutex_unlock(g_LEServerListMutex);
1512 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1513 return CA_STATUS_FAILED;
1518 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(serverInfo, data, dataLen))
1520 OIC_LOG_V(ERROR, TAG, "Could not update characteristic to gatt server [%s]",
1521 serverInfo->remoteAddress);
1522 oc_mutex_unlock(g_LEServerListMutex);
1523 return CA_STATUS_FAILED;
1527 else if (LE_MULTICAST == type)
1529 OIC_LOG(ERROR, TAG, "LE_MULTICAST type Not used");
1531 oc_mutex_unlock(g_LEServerListMutex);
1532 OIC_LOG(DEBUG, TAG, "OUT");
1533 return CA_STATUS_OK;
1536 CAResult_t CAUpdateCharacteristicsToAllGattServers(const uint8_t *data, uint32_t dataLen)
1538 OIC_LOG(DEBUG, TAG, "IN");
1540 VERIFY_NON_NULL(data, TAG, "data is NULL");
1544 OIC_LOG(ERROR, TAG, "dataLen is less than or equal zero. Invalid input !");
1545 return CA_STATUS_INVALID_PARAM;
1548 oc_mutex_lock(g_LEServerListMutex);
1549 LEServerInfoList *curNode = g_LEServerList;
1552 LEServerInfo *serverInfo = curNode->serverInfo;
1553 if (serverInfo->status == LE_STATUS_SERVICES_DISCOVERED)
1555 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(serverInfo, data, dataLen))
1557 OIC_LOG_V(ERROR, TAG, "Failed to update characteristics to gatt server [%s]",
1558 serverInfo->remoteAddress);
1561 else if (serverInfo->status != LE_STATUS_INVALID)
1563 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1565 OIC_LOG(ERROR, TAG, "Failed to add to pending list");
1568 curNode = curNode->next;
1570 oc_mutex_unlock(g_LEServerListMutex);
1572 // Add the data to pending list.
1573 LEData *multicastData = (LEData *)OICCalloc(1, sizeof(LEData));
1574 if (NULL == multicastData)
1576 OIC_LOG(ERROR, TAG, "Calloc failed");
1579 multicastData->data = OICCalloc(1, dataLen);
1580 if (NULL == multicastData->data)
1582 OIC_LOG(ERROR, TAG, "Calloc failed");
1585 memcpy(multicastData->data, data, dataLen);
1586 multicastData->dataLength = dataLen;
1588 oc_mutex_lock(g_multicastDataListMutex);
1589 if (NULL == g_multicastDataList)
1591 g_multicastDataList = u_arraylist_create();
1593 u_arraylist_add(g_multicastDataList, (void *)multicastData);
1594 oc_mutex_unlock(g_multicastDataListMutex);
1596 // Start the scanning, if not started, else reset timer
1597 oc_mutex_lock(g_scanMutex);
1598 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
1600 CAResult_t result = CALEGattStartDeviceScanning();
1601 if (CA_STATUS_OK != result)
1603 oc_mutex_unlock(g_scanMutex);
1604 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning Failed");
1607 g_isMulticastInProgress = true;
1608 // Start the timer by signalling it
1609 oc_cond_signal(g_startTimerCond);
1613 g_isMulticastInProgress = true;
1615 oc_cond_signal(g_scanningTimeCond);
1617 oc_mutex_unlock(g_scanMutex);
1620 OIC_LOG(DEBUG, TAG, "OUT ");
1621 return CA_STATUS_OK;
1624 uint16_t CALEClientGetMtuSize(const char* remote_address)
1626 LEServerInfo *serverInfo = NULL;
1627 oc_mutex_lock(g_LEServerListMutex);
1628 if (CA_STATUS_OK == CAGetLEServerInfo(g_LEServerList, remote_address, &serverInfo))
1630 if (serverInfo->status == LE_STATUS_SERVICES_DISCOVERED){
1631 OIC_LOG_V(DEBUG, TAG, "Mtu Size [%d]", serverInfo->mtu_size);
1632 oc_mutex_unlock(g_LEServerListMutex);
1633 OIC_LOG_V(INFO, TAG, "Test: returning Mtu Size [%d]", serverInfo->mtu_size - CA_BLE_MTU_HEADER_SIZE);
1634 return serverInfo->mtu_size - CA_BLE_MTU_HEADER_SIZE;
1637 oc_mutex_unlock(g_LEServerListMutex);
1638 return CA_DEFAULT_BLE_MTU_SIZE;