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 "75004209-0000-0000-0000-000000000000"
51 #define CA_GATT_CUSTOM_UUID2 "75004204-0000-0000-0000-000000000000"
52 #define CUSTOM_UUID_LEN 9
54 uint64_t const TIMEOUT = 30 * MICROSECS_PER_SEC;
57 * Flag to check if scanning is in progress
59 static bool g_isScanningInProgress = false;
62 * Mutex to synchronize access to g_isScanningInProgress
64 static oc_mutex g_isScanningInProgressMutex = NULL;
67 * Flag to check if connection is in progress
69 static bool g_isConnectionInProgress = false;
72 * Mutex to synchronize access to g_isConnectionInProgress
74 static oc_mutex g_isConnectionInProgressMutex = NULL;
77 * Flag to check if multicast is already in progress.
79 static bool g_isMulticastInProgress = false;
82 * Flag to check if unicast scan is in progress
84 static bool g_isUnicastScanInProgress = false;
87 * Mutex to synchronize access to g_isMulticastInProgress
88 * and g_isUnicastScanInProgress
90 static oc_mutex g_scanMutex = NULL;
93 * Pending multicast data list to be sent.
95 static u_arraylist_t *g_multicastDataList = NULL;
98 * Mutex to synchronize the access to Pending multicast data list.
100 static oc_mutex g_multicastDataListMutex = NULL;
103 * Condition to start the timer for scanning.
105 static oc_cond g_startTimerCond = NULL;
108 * Condition for scanning Time interval.
110 static oc_cond g_scanningTimeCond = NULL;
113 * This contains the list of OIC services a client connect tot.
115 static LEServerInfoList *g_LEServerList = NULL;
118 * Mutex to synchronize access to BleServiceList.
120 static oc_mutex g_LEServerListMutex = NULL;
123 * Boolean variable to keep the state of the GATT Client.
125 static bool g_isLEGattClientStarted = false;
128 * Mutex to synchronize access to the requestResponse callback to be called
129 * when the data needs to be sent from GATTClient.
131 static oc_mutex g_LEReqRespClientCbMutex = NULL;
134 * Mutex to synchronize access to the requestResponse callback to be called
135 * when the data needs to be sent from GATTClient.
137 static oc_mutex g_LEClientConnectMutex = NULL;
140 * Mutex to synchronize the calls to be done to the platform from GATTClient
141 * interfaces from different threads.
143 static oc_mutex g_LEClientStateMutex = NULL;
146 * Mutex to synchronize the task to be pushed to thread pool.
148 static oc_mutex g_LEClientThreadPoolMutex = NULL;
151 * Mutex to synchronize the task to write characteristic one packet after another.
153 static oc_mutex g_threadWriteCharacteristicMutex = NULL;
156 * Condition for Writing characteristic.
158 static oc_cond g_threadWriteCharacteristicCond = NULL;
161 * Flag to check status of write characteristic.
163 static bool g_isSignalSetFlag = false;
166 * Maintains the callback to be notified on receival of network packets from other
169 static CABLEDataReceivedCallback g_LEClientDataReceivedCallback = NULL;
172 * callback to update the error to le adapter
174 static CABLEErrorHandleCallback g_clientErrorCallback;
177 * gmainLoop to manage the threads to receive the callback from the platfrom.
179 static GMainLoop *g_eventLoop = NULL;
182 * Reference to threadpool
184 static ca_thread_pool_t g_LEClientThreadPool = NULL;
186 void CALEGattCharacteristicChangedCb(bt_gatt_h characteristic,
188 int valueLen, void *userData)
190 (void)characteristic;
192 OIC_LOG(DEBUG, TAG, "IN");
193 OIC_LOG_V(DEBUG, TAG, "Changed characteristic value length [%d]", valueLen);
195 oc_mutex_lock(g_LEReqRespClientCbMutex);
196 if (NULL == g_LEClientDataReceivedCallback)
198 OIC_LOG(ERROR, TAG, "Request response callback is not set");
199 oc_mutex_unlock(g_LEReqRespClientCbMutex);
203 uint32_t sentLength = 0;
204 g_LEClientDataReceivedCallback(userData, (uint8_t *)value, valueLen, &sentLength);
206 OIC_LOG_V(DEBUG, TAG, "Recv data Length is %d", sentLength);
208 oc_mutex_unlock(g_LEReqRespClientCbMutex);
210 OIC_LOG(DEBUG, TAG, "OUT");
213 void CALEGattCharacteristicWriteCb(int result, bt_gatt_h reqHandle, void *userData)
218 OIC_LOG(DEBUG, TAG, "IN ");
220 if (BT_ERROR_NONE != result)
222 CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, "", 0, -1,
223 false, "writeChar failure");
225 OIC_LOG(ERROR, TAG, "Write failed Need Retry ");
226 //Need to Implement retry mechanism
230 oc_mutex_lock(g_threadWriteCharacteristicMutex);
231 OIC_LOG(DEBUG, TAG, "g_isSignalSetFlag is set true and signal");
232 g_isSignalSetFlag = true;
233 oc_cond_signal(g_threadWriteCharacteristicCond);
234 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
236 CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, "", 0, -1,
237 true, "writeChar success");
240 OIC_LOG(DEBUG, TAG, "OUT ");
243 CAResult_t CALEGattInitiateConnection(const char *remoteAddress)
245 OIC_LOG(DEBUG, TAG, "IN");
247 oc_mutex_lock(g_isConnectionInProgressMutex);
248 if (g_isConnectionInProgress)
250 oc_mutex_unlock(g_isConnectionInProgressMutex);
251 OIC_LOG(DEBUG, TAG, "Connection already in progress, cannot initiate new connection");
252 return CA_STATUS_FAILED;
254 g_isConnectionInProgress = true;
255 oc_mutex_unlock(g_isConnectionInProgressMutex);
257 // Pause the scanning
258 CALEGattStopDeviceScanning();
260 OIC_LOG_V(DEBUG, TAG,
261 "Trying to do Gatt connection to [%s]", remoteAddress);
263 oc_mutex_lock(g_LEClientThreadPoolMutex);
264 if (NULL == g_LEClientThreadPool)
266 oc_mutex_unlock(g_LEClientThreadPoolMutex);
267 OIC_LOG(ERROR, TAG, "g_LEClientThreadPool is NULL");
268 return CA_STATUS_FAILED;
271 char *addr = OICStrdup(remoteAddress);
274 oc_mutex_unlock(g_LEClientThreadPoolMutex);
275 OIC_LOG(ERROR, TAG, "OICStrdup failed");
276 return CA_STATUS_FAILED;
279 CAResult_t res = ca_thread_pool_add_task(g_LEClientThreadPool, CAGattConnectThread, addr, NULL);
280 oc_mutex_unlock(g_LEClientThreadPoolMutex);
281 if (CA_STATUS_OK != res)
283 OIC_LOG_V(ERROR, TAG,
284 "ca_thread_pool_add_task failed with ret [%d]", res);
286 return CA_STATUS_FAILED;
288 OIC_LOG(DEBUG, TAG, "OUT");
292 void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress)
294 OIC_LOG(DEBUG, TAG, "IN ");
296 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
300 OIC_LOG_V(DEBUG, TAG, "DisConnected from [%s] ", remoteAddress);
301 oc_mutex_lock(g_LEServerListMutex);
302 CARemoveLEServerInfoFromList(&g_LEServerList, remoteAddress);
303 oc_mutex_unlock(g_LEServerListMutex);
307 OIC_LOG_V(DEBUG, TAG, "Connected to [%s] ", remoteAddress);
309 oc_mutex_lock(g_isConnectionInProgressMutex);
310 g_isConnectionInProgress = false;
311 oc_mutex_unlock(g_isConnectionInProgressMutex);
314 // TODO:Disabling scanning for now.Need to check.
315 /*oc_mutex_lock(g_scanMutex);
316 if (g_isMulticastInProgress || g_isUnicastScanInProgress)
318 CAResult_t ret = CALEGattStartDeviceScanning();
319 if (CA_STATUS_OK != ret)
321 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning Failed");
324 oc_mutex_unlock(g_scanMutex);
326 LEServerInfo *serverInfo = NULL;
327 oc_mutex_lock(g_LEServerListMutex);
328 if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
330 oc_mutex_unlock(g_LEServerListMutex);
331 OIC_LOG_V(ERROR, TAG, "Could not get server info for [%s]", remoteAddress);
335 serverInfo->status = LE_STATUS_CONNECTED;
336 oc_mutex_unlock(g_LEServerListMutex);
338 oc_mutex_lock(g_LEClientThreadPoolMutex);
339 if (NULL == g_LEClientThreadPool)
341 oc_mutex_unlock(g_LEClientThreadPoolMutex);
342 OIC_LOG(ERROR, TAG, "g_LEClientThreadPool is NULL");
346 char *addr = OICStrdup(remoteAddress);
349 oc_mutex_unlock(g_LEClientThreadPoolMutex);
350 OIC_LOG(ERROR, TAG, "addr is NULL");
354 CAResult_t ret = ca_thread_pool_add_task(g_LEClientThreadPool, CADiscoverLEServicesThread,
356 oc_mutex_unlock(g_LEClientThreadPoolMutex);
357 if (CA_STATUS_OK != ret)
359 OIC_LOG_V(ERROR, TAG, "ca_thread_pool_add_task failed with ret [%d]", ret);
363 OIC_LOG(DEBUG, TAG, "OUT");
366 static bool CALEIsHaveServiceImpl(bt_adapter_le_device_scan_result_info_s *scanInfo,
367 const char *service_uuid,
368 bt_adapter_le_packet_type_e pkt_type)
375 result = bt_adapter_le_get_scan_result_service_uuids(scanInfo,
376 pkt_type, &uuids, &count);
377 if (result == BT_ERROR_NONE && NULL != uuids)
379 for (int i = 0; i < count; i++)
381 if (0 == strcasecmp(uuids[i], service_uuid))
383 OIC_LOG_V(DEBUG, TAG, "Service[%s] Found in %s",
384 uuids[i], scanInfo->remote_address);
386 }else if(0 == strncasecmp(uuids[i], service_uuid,CUSTOM_UUID_LEN))
388 OIC_LOG_V(DEBUG, TAG, "Custom Service[%s] Found in %s",
389 uuids[i], scanInfo->remote_address);
399 static bool CALEIsHaveService(bt_adapter_le_device_scan_result_info_s *scanInfo,
400 const char *service_uuid)
403 // For arduino servers, scan response will give the UUIDs advertised.
404 CALEIsHaveServiceImpl(scanInfo, service_uuid,
405 BT_ADAPTER_LE_PACKET_SCAN_RESPONSE) ||
406 // For android/tizen servers, advertising packet will give the UUIDs.
407 CALEIsHaveServiceImpl(scanInfo, service_uuid,
408 BT_ADAPTER_LE_PACKET_ADVERTISING);
411 void CALEAdapterScanResultCb(int result, bt_adapter_le_device_scan_result_info_s *scanInfo,
415 OIC_LOG(DEBUG, TAG, "IN");
417 VERIFY_NON_NULL_VOID(scanInfo, TAG, "scanInfo");
418 VERIFY_NON_NULL_VOID(scanInfo->remote_address, TAG, "scanInfo->remote_address");
420 OIC_LOG_V(DEBUG, TAG, "Remote Address [%s]", scanInfo->remote_address);
421 OIC_LOG_V(DEBUG, TAG, "Scan Result [%d]", result);
422 OIC_LOG_V(DEBUG, TAG,
423 " Adv data len [%d] Scan data len[%d]RSSI [%d] Addr_type [%d] ",
424 scanInfo->adv_data_len, scanInfo->scan_data_len, scanInfo->rssi,
425 scanInfo->address_type);
427 // Check if scanning was stopped (since this callback is
428 // being triggered even after stopping the scan)
429 oc_mutex_lock(g_isScanningInProgressMutex);
430 if (!g_isScanningInProgress)
432 oc_mutex_unlock(g_isScanningInProgressMutex);
433 OIC_LOG(DEBUG, TAG, "Scanning not in progress, so ignoring callback");
436 oc_mutex_unlock(g_isScanningInProgressMutex);
438 LEServerInfo *serverInfo = NULL;
439 oc_mutex_lock(g_LEServerListMutex);
440 CAResult_t ret = CAGetLEServerInfo(g_LEServerList, scanInfo->remote_address, &serverInfo);
441 if (CA_STATUS_OK != ret)
443 OIC_LOG_V(DEBUG, TAG,
444 "Newly discovered device with address [%s] ", scanInfo->remote_address);
446 if (CALEIsHaveService(scanInfo, CA_GATT_SERVICE_UUID) ||
447 CALEIsHaveService(scanInfo, CA_GATT_CUSTOM_UUID)||
448 CALEIsHaveService(scanInfo, CA_GATT_CUSTOM_UUID2))
450 OIC_LOG_V(DEBUG, TAG, "Device [%s] supports OIC or custom service", scanInfo->remote_address);
452 char *addr = OICStrdup(scanInfo->remote_address);
455 oc_mutex_unlock(g_LEServerListMutex);
456 OIC_LOG(ERROR, TAG, "Device address is NULL");
459 serverInfo = (LEServerInfo *)OICCalloc(1, sizeof(LEServerInfo));
460 if (NULL == serverInfo)
462 oc_mutex_unlock(g_LEServerListMutex);
463 OIC_LOG(ERROR, TAG, "Calloc failed");
467 serverInfo->remoteAddress = addr;
468 serverInfo->status = LE_STATUS_DISCOVERED;
470 if (CA_STATUS_OK != CAAddLEServerInfoToList(&g_LEServerList, serverInfo))
472 oc_mutex_unlock(g_LEServerListMutex);
473 OIC_LOG_V(ERROR, TAG, "Could not add [%s] to server list", scanInfo->remote_address);
474 CAFreeLEServerInfo(serverInfo);
479 oc_mutex_unlock(g_LEServerListMutex);
480 OIC_LOG(DEBUG, TAG, "OUT");
483 void CASetLEClientThreadPoolHandle(ca_thread_pool_t handle)
485 OIC_LOG(DEBUG, TAG, "IN");
487 oc_mutex_lock(g_LEClientThreadPoolMutex);
488 g_LEClientThreadPool = handle;
489 oc_mutex_unlock(g_LEClientThreadPoolMutex);
491 OIC_LOG(DEBUG, TAG, "OUT");
494 void CASetLEReqRespClientCallback(CABLEDataReceivedCallback callback)
496 OIC_LOG(DEBUG, TAG, "IN");
498 oc_mutex_lock(g_LEReqRespClientCbMutex);
500 g_LEClientDataReceivedCallback = callback;
502 oc_mutex_unlock(g_LEReqRespClientCbMutex);
504 OIC_LOG(DEBUG, TAG, "OUT");
507 void CASetBLEClientErrorHandleCallback(CABLEErrorHandleCallback callback)
509 g_clientErrorCallback = callback;
512 CAResult_t CAStartLEGattClient()
514 OIC_LOG(DEBUG, TAG, "IN");
516 oc_mutex_lock(g_LEClientStateMutex);
517 if (true == g_isLEGattClientStarted)
519 OIC_LOG(ERROR, TAG, "Gatt Client is already running!!");
520 oc_mutex_unlock(g_LEClientStateMutex);
521 return CA_STATUS_FAILED;
524 CAResult_t result = CALEGattSetCallbacks();
525 if (CA_STATUS_OK != result)
527 OIC_LOG(ERROR, TAG, "CABleGattSetCallbacks Failed");
528 oc_mutex_unlock(g_LEClientStateMutex);
529 CATerminateLEGattClient();
530 return CA_STATUS_FAILED;
533 g_isLEGattClientStarted = true;
534 oc_mutex_unlock(g_LEClientStateMutex);
536 oc_mutex_lock(g_LEClientThreadPoolMutex);
537 if (NULL == g_LEClientThreadPool)
539 OIC_LOG(ERROR, TAG, "gBleServerThreadPool is NULL");
540 CATerminateGattClientMutexVariables();
541 oc_mutex_unlock(g_LEClientThreadPoolMutex);
542 return CA_STATUS_FAILED;
545 result = ca_thread_pool_add_task(g_LEClientThreadPool, CAStartTimerThread,
547 if (CA_STATUS_OK != result)
549 OIC_LOG(ERROR, TAG, "ca_thread_pool_add_task failed");
550 CATerminateGattClientMutexVariables();
551 oc_mutex_unlock(g_LEClientThreadPoolMutex);
552 return CA_STATUS_FAILED;
555 result= ca_thread_pool_add_task(g_LEClientThreadPool, CALEClientScanThread,
557 if (CA_STATUS_OK != result)
559 OIC_LOG(ERROR, TAG, "ca_thread_pool_add_task failed");
560 CATerminateGattClientMutexVariables();
561 oc_mutex_unlock(g_LEClientThreadPoolMutex);
562 return CA_STATUS_FAILED;
564 oc_mutex_unlock(g_LEClientThreadPoolMutex);
566 OIC_LOG(DEBUG, TAG, "OUT");
570 CAResult_t CALEClientScanThread()
572 oc_mutex_lock(g_scanMutex);
573 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
575 CAResult_t result = CALEGattStartDeviceScanning();
576 if (CA_STATUS_OK != result)
578 oc_mutex_unlock(g_scanMutex);
579 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning failed");
580 return CA_STATUS_FAILED;
582 g_isUnicastScanInProgress = true;
584 oc_cond_signal(g_startTimerCond);
588 g_isUnicastScanInProgress = true;
590 oc_cond_signal(g_scanningTimeCond);
592 oc_mutex_unlock(g_scanMutex);
594 OIC_LOG(DEBUG, TAG, "OUT");
598 void CAStartTimerThread(void *data)
602 OIC_LOG(DEBUG, TAG, "IN");
603 while (g_isLEGattClientStarted)
605 oc_mutex_lock(g_scanMutex);
606 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
608 OIC_LOG(DEBUG, TAG, "waiting....");
609 oc_cond_wait(g_startTimerCond, g_scanMutex);
610 OIC_LOG(DEBUG, TAG, "Wake up");
613 // Timed conditional wait for stopping the scan.
614 OCWaitResult_t ret = oc_cond_wait_for(g_scanningTimeCond, g_scanMutex,
616 if (OC_WAIT_TIMEDOUT == ret)
618 OIC_LOG(DEBUG, TAG, "Scan is timed Out");
620 CALEGattStopDeviceScanning();
622 if (g_isMulticastInProgress)
624 oc_mutex_lock(g_multicastDataListMutex);
625 // Clear the data list and device list.
626 u_arraylist_destroy(g_multicastDataList);
627 g_multicastDataList = NULL;
628 oc_mutex_unlock(g_multicastDataListMutex);
629 g_isMulticastInProgress = false;
631 g_isUnicastScanInProgress = false;
633 oc_mutex_unlock(g_scanMutex);
636 OIC_LOG(DEBUG, TAG, "OUT");
639 void CAStopLEGattClient()
641 OIC_LOG(DEBUG, TAG, "IN");
643 oc_mutex_lock(g_LEClientStateMutex);
645 if (false == g_isLEGattClientStarted)
647 OIC_LOG(ERROR, TAG, "Gatt Client is not running to stop");
648 oc_mutex_unlock(g_LEClientStateMutex);
652 CALEGattUnSetCallbacks();
654 CALEGattStopDeviceScanning();
656 g_isLEGattClientStarted = false;
658 // Signal the conditions waiting in Start timer.
659 oc_cond_signal(g_startTimerCond);
660 oc_cond_signal(g_scanningTimeCond);
662 // Destroy the multicast data list and device list if not empty.
663 if (NULL != g_multicastDataList)
665 oc_mutex_lock(g_multicastDataListMutex);
666 u_arraylist_destroy(g_multicastDataList);
667 g_multicastDataList = NULL;
668 oc_mutex_unlock(g_multicastDataListMutex);
671 oc_mutex_lock(g_LEServerListMutex);
672 CAFreeLEServerList(g_LEServerList);
673 g_LEServerList = NULL;
674 oc_mutex_unlock(g_LEServerListMutex);
676 oc_mutex_lock(g_threadWriteCharacteristicMutex);
677 oc_cond_signal(g_threadWriteCharacteristicCond);
678 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
680 GMainContext *context_event_loop = NULL;
681 // Required for waking up the thread which is running in gmain loop
682 if (NULL != g_eventLoop)
684 context_event_loop = g_main_loop_get_context(g_eventLoop);
686 if (context_event_loop)
688 OIC_LOG_V(DEBUG, TAG, "g_eventLoop context %p", (void *)context_event_loop);
689 g_main_context_wakeup(context_event_loop);
691 // Kill g main loops and kill threads.
692 g_main_loop_quit(g_eventLoop);
696 OIC_LOG(ERROR, TAG, "g_eventLoop context is NULL");
699 oc_mutex_unlock(g_LEClientStateMutex);
701 OIC_LOG(DEBUG, TAG, "OUT");
704 CAResult_t CAInitializeLEGattClient()
706 OIC_LOG(DEBUG, TAG, "Initialize GATT Client");
707 CAResult_t res = CAInitGattClientMutexVariables();
708 if (CA_STATUS_OK != res)
710 OIC_LOG(ERROR, TAG, "CAInitGattClientMutexVariables failed!");
711 CATerminateGattClientMutexVariables();
712 return CA_STATUS_FAILED;
717 void CATerminateLEGattClient()
719 OIC_LOG(DEBUG, TAG, "IN");
721 CATerminateGattClientMutexVariables();
723 OIC_LOG(DEBUG, TAG, "OUT");
726 CAResult_t CAInitGattClientMutexVariables()
728 OIC_LOG(DEBUG, TAG, "IN");
729 if (NULL == g_LEClientStateMutex)
731 g_LEClientStateMutex = oc_mutex_new();
732 if (NULL == g_LEClientStateMutex)
734 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
735 return CA_STATUS_FAILED;
739 if (NULL == g_LEServerListMutex)
741 g_LEServerListMutex = oc_mutex_new();
742 if (NULL == g_LEServerListMutex)
744 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
745 return CA_STATUS_FAILED;
749 if (NULL == g_LEReqRespClientCbMutex)
751 g_LEReqRespClientCbMutex = oc_mutex_new();
752 if (NULL == g_LEReqRespClientCbMutex)
754 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
755 return CA_STATUS_FAILED;
759 if (NULL == g_LEClientThreadPoolMutex)
761 g_LEClientThreadPoolMutex = oc_mutex_new();
762 if (NULL == g_LEClientThreadPoolMutex)
764 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
765 return CA_STATUS_FAILED;
769 if (NULL == g_LEClientConnectMutex)
771 g_LEClientConnectMutex = oc_mutex_new();
772 if (NULL == g_LEClientConnectMutex)
774 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
775 return CA_STATUS_FAILED;
779 if (NULL == g_isScanningInProgressMutex)
781 g_isScanningInProgressMutex = oc_mutex_new();
782 if (NULL == g_isScanningInProgressMutex)
784 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
785 return CA_STATUS_FAILED;
789 if (NULL == g_isConnectionInProgressMutex)
791 g_isConnectionInProgressMutex = oc_mutex_new();
792 if (NULL == g_isConnectionInProgressMutex)
794 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
795 return CA_STATUS_FAILED;
799 if (NULL == g_multicastDataListMutex)
801 g_multicastDataListMutex = oc_mutex_new();
802 if (NULL == g_multicastDataListMutex)
804 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
805 return CA_STATUS_FAILED;
809 if (NULL == g_scanMutex)
811 g_scanMutex = oc_mutex_new();
812 if (NULL == g_scanMutex)
814 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
815 return CA_STATUS_FAILED;
819 if (NULL == g_threadWriteCharacteristicMutex)
821 g_threadWriteCharacteristicMutex = oc_mutex_new();
822 if (NULL == g_threadWriteCharacteristicMutex)
824 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
825 return CA_STATUS_FAILED;
829 if (NULL == g_startTimerCond)
831 g_startTimerCond = oc_cond_new();
832 if (NULL == g_startTimerCond)
834 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
835 return CA_STATUS_FAILED;
839 if (NULL == g_scanningTimeCond)
841 g_scanningTimeCond = oc_cond_new();
842 if (NULL == g_scanningTimeCond)
844 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
845 return CA_STATUS_FAILED;
849 if (NULL == g_threadWriteCharacteristicCond)
851 g_threadWriteCharacteristicCond = oc_cond_new();
852 if (NULL == g_threadWriteCharacteristicCond)
854 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
855 return CA_STATUS_FAILED;
859 OIC_LOG(DEBUG, TAG, "OUT");
863 void CATerminateGattClientMutexVariables()
865 OIC_LOG(DEBUG, TAG, "IN");
867 oc_mutex_free(g_LEClientStateMutex);
868 g_LEClientStateMutex = NULL;
870 oc_mutex_free(g_LEServerListMutex);
871 g_LEServerListMutex = NULL;
873 oc_mutex_free(g_LEReqRespClientCbMutex);
874 g_LEReqRespClientCbMutex = NULL;
876 oc_mutex_free(g_LEClientConnectMutex);
877 g_LEClientConnectMutex = NULL;
879 oc_mutex_free(g_LEClientThreadPoolMutex);
880 g_LEClientThreadPoolMutex = NULL;
882 oc_mutex_free(g_isScanningInProgressMutex);
883 g_isScanningInProgressMutex = NULL;
885 oc_mutex_free(g_isConnectionInProgressMutex);
886 g_isConnectionInProgressMutex = NULL;
888 oc_mutex_free(g_multicastDataListMutex);
889 g_multicastDataListMutex = NULL;
891 oc_mutex_free(g_scanMutex);
894 oc_mutex_free(g_threadWriteCharacteristicMutex);
895 g_threadWriteCharacteristicMutex = NULL;
897 oc_cond_free(g_startTimerCond);
898 g_startTimerCond = NULL;
900 oc_cond_free(g_scanningTimeCond);
901 g_scanningTimeCond = NULL;
903 oc_cond_free(g_threadWriteCharacteristicCond);
904 g_threadWriteCharacteristicCond = NULL;
905 g_isSignalSetFlag = false;
907 OIC_LOG(DEBUG, TAG, "OUT");
910 CAResult_t CALEGattSetCallbacks()
912 OIC_LOG(DEBUG, TAG, "IN");
914 OIC_LOG(DEBUG, TAG, "OUT");
918 void CALEGattUnSetCallbacks()
920 OIC_LOG(DEBUG, TAG, "IN");
922 bt_gatt_unset_connection_state_changed_cb();
924 oc_mutex_lock(g_LEServerListMutex);
925 LEServerInfoList *curNode = g_LEServerList;
928 LEServerInfo *serverInfo = curNode->serverInfo;
929 if (serverInfo->status >= LE_STATUS_SERVICES_DISCOVERED)
931 bt_gatt_client_unset_characteristic_value_changed_cb(serverInfo->readChar);
933 curNode = curNode->next;
935 oc_mutex_unlock(g_LEServerListMutex);
937 OIC_LOG(DEBUG, TAG, "OUT");
940 CAResult_t CALEGattStartDeviceScanning()
942 OIC_LOG(DEBUG, TAG, "IN");
944 oc_mutex_lock(g_isScanningInProgressMutex);
945 if (!g_isScanningInProgress)
947 int ret = bt_adapter_le_start_scan(CALEAdapterScanResultCb, NULL);
948 if (BT_ERROR_NONE != ret)
950 oc_mutex_unlock(g_isScanningInProgressMutex);
951 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_start_scan failed[%s]",
952 CALEGetErrorMsg(ret));
953 return CA_STATUS_FAILED;
955 g_isScanningInProgress = true;
959 OIC_LOG(DEBUG, TAG, "Ignore, scanning already in progress");
961 oc_mutex_unlock(g_isScanningInProgressMutex);
963 OIC_LOG(DEBUG, TAG, "OUT");
967 void CALEGattStopDeviceScanning()
969 OIC_LOG(DEBUG, TAG, "IN");
971 oc_mutex_lock(g_isScanningInProgressMutex);
972 if (g_isScanningInProgress)
974 int ret = bt_adapter_le_stop_scan();
975 if (BT_ERROR_NONE != ret)
977 oc_mutex_unlock(g_isScanningInProgressMutex);
978 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_stop_scan failed[%s]",
979 CALEGetErrorMsg(ret));
982 g_isScanningInProgress = false;
986 OIC_LOG(DEBUG, TAG, "Ignore, scanning not in progress");
988 oc_mutex_unlock(g_isScanningInProgressMutex);
990 OIC_LOG(DEBUG, TAG, "OUT");
993 void CAGattConnectThread (void *remoteAddress)
995 OIC_LOG(DEBUG, TAG, "IN ");
997 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
999 char *address = (char *)remoteAddress;
1001 OIC_LOG_V(DEBUG, TAG, "remote address is [%s]", address);
1003 CAResult_t result = CALEGattConnect(address);
1005 if (CA_STATUS_OK != result)
1007 OIC_LOG_V(ERROR, TAG, "bt_gatt_connect failed for [%s]", address);
1012 OIC_LOG(DEBUG, TAG, "OUT");
1015 CAResult_t CALEGattConnect(const char *remoteAddress)
1017 OIC_LOG(DEBUG, TAG, "IN");
1019 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1020 "remote address is NULL", CA_STATUS_FAILED);
1022 oc_mutex_lock(g_LEClientConnectMutex);
1023 CAResult_t result = CA_STATUS_OK;
1025 int ret = bt_gatt_connect(remoteAddress, false);
1026 if (BT_ERROR_NONE != ret)
1028 OIC_LOG_V(ERROR, TAG, "bt_gatt_connect Failed with ret value [%s] ",
1029 CALEGetErrorMsg(ret));
1030 oc_mutex_unlock(g_LEClientConnectMutex);
1031 return CA_STATUS_FAILED;
1034 oc_mutex_unlock(g_LEClientConnectMutex);
1036 OIC_LOG(DEBUG, TAG, "OUT");
1040 CAResult_t CALEGattDisConnect(const char *remoteAddress)
1042 OIC_LOG(DEBUG, TAG, "IN");
1044 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1045 "remote address is NULL", CA_STATUS_FAILED);
1047 int ret = bt_gatt_disconnect(remoteAddress);
1049 if (BT_ERROR_NONE != ret)
1051 OIC_LOG_V(ERROR, TAG, "bt_gatt_disconnect Failed with ret value [%s] ",
1052 CALEGetErrorMsg(ret));
1053 return CA_STATUS_FAILED;
1056 OIC_LOG(DEBUG, TAG, "OUT");
1057 return CA_STATUS_OK;
1060 CAResult_t CAUpdateCharacteristicsToGattServerImpl(LEServerInfo *serverInfo,
1061 const uint8_t *data, const uint32_t dataLen)
1063 OIC_LOG(DEBUG, TAG, "IN");
1065 VERIFY_NON_NULL(serverInfo, TAG, "Server Info is NULL");
1067 CALEGattStopDeviceScanning();
1069 OIC_LOG_V(DEBUG, TAG, "Updating the data of length [%d] to [%s] ", dataLen,
1070 serverInfo->remoteAddress);
1072 int result = bt_gatt_set_value(serverInfo->writeChar, (char *)data, dataLen);
1074 if (BT_ERROR_NONE != result)
1076 OIC_LOG_V(ERROR, TAG,
1077 "bt_gatt_set_value Failed with return val [%s]",
1078 CALEGetErrorMsg(result));
1082 result = bt_gatt_client_write_value(serverInfo->writeChar, CALEGattCharacteristicWriteCb,
1084 if (BT_ERROR_NONE != result)
1086 OIC_LOG_V(ERROR, TAG,
1087 "bt_gatt_client_write_value Failed with return val [%s]",
1088 CALEGetErrorMsg(result));
1092 // wait for callback for write Characteristic with success to sent data
1093 OIC_LOG_V(DEBUG, TAG, "callback flag is %d", g_isSignalSetFlag);
1094 oc_mutex_lock(g_threadWriteCharacteristicMutex);
1095 if (!g_isSignalSetFlag)
1097 OIC_LOG(DEBUG, TAG, "wait for callback to notify writeCharacteristic is success");
1098 if (OC_WAIT_SUCCESS != oc_cond_wait_for(g_threadWriteCharacteristicCond,
1099 g_threadWriteCharacteristicMutex,
1100 WAIT_TIME_WRITE_CHARACTERISTIC))
1102 g_isSignalSetFlag = false;
1103 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
1104 OIC_LOG(ERROR, TAG, "there is no response. write has failed");
1108 // reset flag set by writeCharacteristic Callback
1109 g_isSignalSetFlag = false;
1110 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
1112 oc_mutex_lock(g_scanMutex);
1113 if (g_isMulticastInProgress || g_isUnicastScanInProgress)
1115 if (CA_STATUS_OK != CALEGattStartDeviceScanning())
1117 OIC_LOG(ERROR, TAG, "Could not start device scanning");
1120 oc_mutex_unlock(g_scanMutex);
1121 OIC_LOG(DEBUG, TAG, "OUT");
1122 return CA_STATUS_OK;
1125 oc_mutex_lock(g_scanMutex);
1126 if (g_isMulticastInProgress || g_isUnicastScanInProgress)
1128 if (CA_STATUS_OK != CALEGattStartDeviceScanning())
1130 OIC_LOG(ERROR, TAG, "Could not start device scanning");
1133 oc_mutex_unlock(g_scanMutex);
1135 OIC_LOG(DEBUG, TAG, "OUT");
1136 return CA_STATUS_FAILED;
1139 void CADiscoverLEServicesThread(void *remoteAddress)
1141 OIC_LOG(DEBUG, TAG, "IN");
1142 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
1144 char *address = (char *)remoteAddress;
1146 CAResult_t result = CALEGattDiscoverServices(address);
1147 if (CA_STATUS_OK != result)
1149 OIC_LOG(ERROR, TAG, "CALEGattDiscoverServices failed");
1153 OIC_LOG(DEBUG, TAG, "OUT");
1156 CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
1158 OIC_LOG(DEBUG, TAG, "IN");
1160 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1161 "remote address is NULL", CA_STATUS_FAILED);
1163 LEServerInfo *serverInfo = NULL;
1164 oc_mutex_lock(g_LEServerListMutex);
1165 if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
1167 oc_mutex_unlock(g_LEServerListMutex);
1168 OIC_LOG_V(ERROR, TAG, "Could not get server info for [%s]", remoteAddress);
1169 CALEGattDisConnect(remoteAddress);
1170 return CA_STATUS_FAILED;
1173 bt_gatt_h serviceHandle = NULL;
1174 int32_t ret = bt_gatt_client_get_service(serverInfo->clientHandle, CA_GATT_SERVICE_UUID, &serviceHandle);
1175 if (BT_ERROR_NONE != ret || NULL == serviceHandle)
1177 OIC_LOG_V(ERROR, TAG,
1178 "bt_gatt_client_get_service Failed with ret value [%s] ", CALEGetErrorMsg(ret));
1179 bt_gatt_client_destroy(serverInfo->clientHandle);
1180 CALEGattDisConnect(remoteAddress);
1181 return CA_STATUS_FAILED;
1184 // Server will read data on this characteristic.
1185 bt_gatt_h writeChrHandle = NULL;
1186 ret = bt_gatt_service_get_characteristic(serviceHandle, CA_GATT_REQUEST_CHRC_UUID,
1188 if (BT_ERROR_NONE != ret || NULL == writeChrHandle)
1190 OIC_LOG_V(ERROR, TAG,
1191 "bt_gatt_service_get_characteristic Failed with ret value [%s] ",
1192 CALEGetErrorMsg(ret));
1193 bt_gatt_client_destroy(serverInfo->clientHandle);
1194 CALEGattDisConnect(remoteAddress);
1195 return CA_STATUS_FAILED;
1198 // Server will notify data on this characteristic.
1199 bt_gatt_h readChrHandle = NULL;
1200 ret = bt_gatt_service_get_characteristic(serviceHandle, CA_GATT_RESPONSE_CHRC_UUID,
1202 if (BT_ERROR_NONE != ret || NULL == readChrHandle)
1204 OIC_LOG_V(ERROR, TAG,
1205 "bt_gatt_service_get_characteristic Failed with ret value [%s] ",
1206 CALEGetErrorMsg(ret));
1207 bt_gatt_client_destroy(serverInfo->clientHandle);
1208 CALEGattDisConnect(remoteAddress);
1209 return CA_STATUS_FAILED;
1212 //TODO: This data has to be freed while unsetting the callback.
1213 char *addr = OICStrdup(remoteAddress);
1216 OIC_LOG(ERROR, TAG, "addr is NULL");
1217 bt_gatt_client_destroy(serverInfo->clientHandle);
1218 CALEGattDisConnect(remoteAddress);
1219 return CA_STATUS_FAILED;
1222 ret = bt_gatt_client_set_characteristic_value_changed_cb(readChrHandle,
1223 CALEGattCharacteristicChangedCb,
1225 if (BT_ERROR_NONE != ret)
1227 OIC_LOG_V(ERROR, TAG,
1228 "bt_gatt_client_set_characteristic_value_changed_cb Failed with ret value [%s]",
1229 CALEGetErrorMsg(ret));
1230 bt_gatt_client_destroy(serverInfo->clientHandle);
1231 CALEGattDisConnect(remoteAddress);
1232 return CA_STATUS_FAILED;
1235 serverInfo->serviceHandle = serviceHandle;
1236 serverInfo->readChar = readChrHandle;
1237 serverInfo->writeChar = writeChrHandle;
1238 serverInfo->status = LE_STATUS_SERVICES_DISCOVERED;
1240 while (serverInfo->pendingDataList)
1242 LEData *leData = serverInfo->pendingDataList->data;
1243 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(
1244 serverInfo, leData->data, leData->dataLength))
1246 OIC_LOG_V(ERROR, TAG, "Failed to send pending data to [%s]",
1247 serverInfo->remoteAddress);
1249 CADestroyLEDataList(&serverInfo->pendingDataList);
1252 CARemoveLEDataFromList(&serverInfo->pendingDataList);
1254 oc_mutex_unlock(g_LEServerListMutex);
1256 OIC_LOG(DEBUG, TAG, "OUT");
1257 return CA_STATUS_OK;
1260 CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
1261 const uint8_t *data, const uint32_t dataLen,
1262 CALETransferType_t type, const int32_t position)
1264 OIC_LOG(DEBUG, TAG, "IN");
1266 VERIFY_NON_NULL(data, TAG, "data is NULL");
1270 OIC_LOG(ERROR, TAG, "dataLen is less than or equal zero. Invalid input!");
1271 return CA_STATUS_INVALID_PARAM;
1274 LEServerInfo *serverInfo = NULL;
1275 oc_mutex_lock(g_LEServerListMutex);
1276 if (LE_UNICAST == type)
1278 if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
1280 OIC_LOG_V(DEBUG, TAG,
1281 "Device with address [%s] not yet found, initiating scan",
1284 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1286 oc_mutex_unlock(g_LEServerListMutex);
1287 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1288 return CA_STATUS_FAILED;
1291 oc_mutex_unlock(g_LEServerListMutex);
1293 oc_mutex_lock(g_scanMutex);
1294 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
1296 CAResult_t result = CALEGattStartDeviceScanning();
1297 if (CA_STATUS_OK != result)
1299 oc_mutex_unlock(g_scanMutex);
1300 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning failed");
1301 return CA_STATUS_FAILED;
1303 g_isUnicastScanInProgress = true;
1305 oc_cond_signal(g_startTimerCond);
1309 g_isUnicastScanInProgress = true;
1311 oc_cond_signal(g_scanningTimeCond);
1313 oc_mutex_unlock(g_scanMutex);
1315 OIC_LOG(DEBUG, TAG, "OUT");
1316 return CA_STATUS_OK;
1319 if (serverInfo->status == LE_STATUS_DISCOVERED)
1321 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1323 oc_mutex_unlock(g_LEServerListMutex);
1324 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1325 return CA_STATUS_FAILED;
1328 bt_gatt_client_h clientHandle = NULL;
1329 int32_t ret = bt_gatt_client_create(serverInfo->remoteAddress, &clientHandle);
1330 if (BT_ERROR_NONE != ret || NULL == clientHandle)
1332 OIC_LOG_V(ERROR, TAG,
1333 "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret));
1334 CALEGattDisConnect(serverInfo->remoteAddress);
1335 return CA_STATUS_FAILED;
1337 serverInfo->clientHandle = clientHandle;
1339 serverInfo->status = LE_STATUS_CONNECTION_INITIATED;
1340 if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress))
1342 OIC_LOG_V(ERROR, TAG, "Could not initiate connection to [%s]", serverInfo->remoteAddress);
1343 serverInfo->status = LE_STATUS_DISCOVERED;
1344 CADestroyLEDataList(&serverInfo->pendingDataList);
1345 oc_mutex_unlock(g_LEServerListMutex);
1346 return CA_STATUS_FAILED;
1349 else if (serverInfo->status < LE_STATUS_SERVICES_DISCOVERED)
1351 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1353 oc_mutex_unlock(g_LEServerListMutex);
1354 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1355 return CA_STATUS_FAILED;
1360 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(serverInfo, data, dataLen))
1362 OIC_LOG_V(ERROR, TAG, "Could not update characteristic to gatt server [%s]",
1363 serverInfo->remoteAddress);
1364 oc_mutex_unlock(g_LEServerListMutex);
1365 return CA_STATUS_FAILED;
1369 else if (LE_MULTICAST == type)
1371 OIC_LOG(ERROR, TAG, "LE_MULTICAST type Not used");
1373 oc_mutex_unlock(g_LEServerListMutex);
1374 OIC_LOG(DEBUG, TAG, "OUT");
1375 return CA_STATUS_OK;
1378 CAResult_t CAUpdateCharacteristicsToAllGattServers(const uint8_t *data, uint32_t dataLen)
1380 OIC_LOG(DEBUG, TAG, "IN");
1382 VERIFY_NON_NULL(data, TAG, "data is NULL");
1386 OIC_LOG(ERROR, TAG, "dataLen is less than or equal zero. Invalid input !");
1387 return CA_STATUS_INVALID_PARAM;
1390 oc_mutex_lock(g_LEServerListMutex);
1391 LEServerInfoList *curNode = g_LEServerList;
1394 LEServerInfo *serverInfo = curNode->serverInfo;
1395 if (serverInfo->status == LE_STATUS_SERVICES_DISCOVERED)
1397 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(serverInfo, data, dataLen))
1399 OIC_LOG_V(ERROR, TAG, "Failed to update characteristics to gatt server [%s]",
1400 serverInfo->remoteAddress);
1403 else if (serverInfo->status != LE_STATUS_INVALID)
1405 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1407 OIC_LOG(ERROR, TAG, "Failed to add to pending list");
1410 curNode = curNode->next;
1412 oc_mutex_unlock(g_LEServerListMutex);
1414 // Add the data to pending list.
1415 LEData *multicastData = (LEData *)OICCalloc(1, sizeof(LEData));
1416 if (NULL == multicastData)
1418 OIC_LOG(ERROR, TAG, "Calloc failed");
1421 multicastData->data = OICCalloc(1, dataLen);
1422 if (NULL == multicastData->data)
1424 OIC_LOG(ERROR, TAG, "Calloc failed");
1427 memcpy(multicastData->data, data, dataLen);
1428 multicastData->dataLength = dataLen;
1430 oc_mutex_lock(g_multicastDataListMutex);
1431 if (NULL == g_multicastDataList)
1433 g_multicastDataList = u_arraylist_create();
1435 u_arraylist_add(g_multicastDataList, (void *)multicastData);
1436 oc_mutex_unlock(g_multicastDataListMutex);
1438 // Start the scanning, if not started, else reset timer
1439 oc_mutex_lock(g_scanMutex);
1440 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
1442 CAResult_t result = CALEGattStartDeviceScanning();
1443 if (CA_STATUS_OK != result)
1445 oc_mutex_unlock(g_scanMutex);
1446 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning Failed");
1449 g_isMulticastInProgress = true;
1450 // Start the timer by signalling it
1451 oc_cond_signal(g_startTimerCond);
1455 g_isMulticastInProgress = true;
1457 oc_cond_signal(g_scanningTimeCond);
1459 oc_mutex_unlock(g_scanMutex);
1462 OIC_LOG(DEBUG, TAG, "OUT ");
1463 return CA_STATUS_OK;