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 CAResult_t CALEClientScanThread();
188 void CALEGattCharacteristicChangedCb(bt_gatt_h characteristic,
190 int valueLen, void *userData)
192 (void)characteristic;
194 OIC_LOG(DEBUG, TAG, "IN");
195 OIC_LOG_V(DEBUG, TAG, "Changed characteristic value length [%d]", valueLen);
197 oc_mutex_lock(g_LEReqRespClientCbMutex);
198 if (NULL == g_LEClientDataReceivedCallback)
200 OIC_LOG(ERROR, TAG, "Request response callback is not set");
201 oc_mutex_unlock(g_LEReqRespClientCbMutex);
205 uint32_t sentLength = 0;
206 g_LEClientDataReceivedCallback(userData, (uint8_t *)value, valueLen, &sentLength);
208 OIC_LOG_V(DEBUG, TAG, "Recv data Length is %d", sentLength);
210 oc_mutex_unlock(g_LEReqRespClientCbMutex);
212 OIC_LOG(DEBUG, TAG, "OUT");
215 void CALEGattCharacteristicWriteCb(int result, bt_gatt_h reqHandle, void *userData)
220 OIC_LOG(DEBUG, TAG, "IN ");
222 if (BT_ERROR_NONE != result)
224 CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, "", 0, -1,
225 false, "writeChar failure");
227 OIC_LOG(ERROR, TAG, "Write failed Need Retry ");
228 //Need to Implement retry mechanism
232 oc_mutex_lock(g_threadWriteCharacteristicMutex);
233 OIC_LOG(DEBUG, TAG, "g_isSignalSetFlag is set true and signal");
234 g_isSignalSetFlag = true;
235 oc_cond_signal(g_threadWriteCharacteristicCond);
236 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
238 CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, "", 0, -1,
239 true, "writeChar success");
242 OIC_LOG(DEBUG, TAG, "OUT ");
245 CAResult_t CALEGattInitiateConnection(const char *remoteAddress)
247 OIC_LOG(DEBUG, TAG, "IN");
249 oc_mutex_lock(g_isConnectionInProgressMutex);
250 if (g_isConnectionInProgress)
252 oc_mutex_unlock(g_isConnectionInProgressMutex);
253 OIC_LOG(DEBUG, TAG, "Connection already in progress, cannot initiate new connection");
254 return CA_STATUS_FAILED;
256 g_isConnectionInProgress = true;
257 oc_mutex_unlock(g_isConnectionInProgressMutex);
259 // Pause the scanning
260 CALEGattStopDeviceScanning();
262 OIC_LOG_V(DEBUG, TAG,
263 "Trying to do Gatt connection to [%s]", remoteAddress);
265 oc_mutex_lock(g_LEClientThreadPoolMutex);
266 if (NULL == g_LEClientThreadPool)
268 oc_mutex_unlock(g_LEClientThreadPoolMutex);
269 OIC_LOG(ERROR, TAG, "g_LEClientThreadPool is NULL");
270 return CA_STATUS_FAILED;
273 char *addr = OICStrdup(remoteAddress);
276 oc_mutex_unlock(g_LEClientThreadPoolMutex);
277 OIC_LOG(ERROR, TAG, "OICStrdup failed");
278 return CA_STATUS_FAILED;
281 CAResult_t res = ca_thread_pool_add_task(g_LEClientThreadPool, CAGattConnectThread, addr, NULL);
282 oc_mutex_unlock(g_LEClientThreadPoolMutex);
283 if (CA_STATUS_OK != res)
285 OIC_LOG_V(ERROR, TAG,
286 "ca_thread_pool_add_task failed with ret [%d]", res);
288 return CA_STATUS_FAILED;
290 OIC_LOG(DEBUG, TAG, "OUT");
294 void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress)
296 OIC_LOG(DEBUG, TAG, "IN ");
298 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
302 OIC_LOG_V(DEBUG, TAG, "DisConnected from [%s] ", remoteAddress);
303 oc_mutex_lock(g_LEServerListMutex);
304 CARemoveLEServerInfoFromList(&g_LEServerList, remoteAddress);
305 oc_mutex_unlock(g_LEServerListMutex);
309 OIC_LOG_V(DEBUG, TAG, "Connected to [%s] ", remoteAddress);
311 oc_mutex_lock(g_isConnectionInProgressMutex);
312 g_isConnectionInProgress = false;
313 oc_mutex_unlock(g_isConnectionInProgressMutex);
316 // TODO:Disabling scanning for now.Need to check.
317 /*oc_mutex_lock(g_scanMutex);
318 if (g_isMulticastInProgress || g_isUnicastScanInProgress)
320 CAResult_t ret = CALEGattStartDeviceScanning();
321 if (CA_STATUS_OK != ret)
323 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning Failed");
326 oc_mutex_unlock(g_scanMutex);
328 LEServerInfo *serverInfo = NULL;
329 oc_mutex_lock(g_LEServerListMutex);
330 if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
332 oc_mutex_unlock(g_LEServerListMutex);
333 OIC_LOG_V(ERROR, TAG, "Could not get server info for [%s]", remoteAddress);
337 serverInfo->status = LE_STATUS_CONNECTED;
338 oc_mutex_unlock(g_LEServerListMutex);
340 oc_mutex_lock(g_LEClientThreadPoolMutex);
341 if (NULL == g_LEClientThreadPool)
343 oc_mutex_unlock(g_LEClientThreadPoolMutex);
344 OIC_LOG(ERROR, TAG, "g_LEClientThreadPool is NULL");
348 char *addr = OICStrdup(remoteAddress);
351 oc_mutex_unlock(g_LEClientThreadPoolMutex);
352 OIC_LOG(ERROR, TAG, "addr is NULL");
356 CAResult_t ret = ca_thread_pool_add_task(g_LEClientThreadPool, CADiscoverLEServicesThread,
358 oc_mutex_unlock(g_LEClientThreadPoolMutex);
359 if (CA_STATUS_OK != ret)
361 OIC_LOG_V(ERROR, TAG, "ca_thread_pool_add_task failed with ret [%d]", ret);
365 OIC_LOG(DEBUG, TAG, "OUT");
368 static bool CALEIsHaveServiceImpl(bt_adapter_le_device_scan_result_info_s *scanInfo,
369 const char *service_uuid,
370 bt_adapter_le_packet_type_e pkt_type)
377 result = bt_adapter_le_get_scan_result_service_uuids(scanInfo,
378 pkt_type, &uuids, &count);
379 if (result == BT_ERROR_NONE && NULL != uuids)
381 for (int i = 0; i < count; i++)
383 if (0 == strcasecmp(uuids[i], service_uuid))
385 OIC_LOG_V(DEBUG, TAG, "Service[%s] Found in %s",
386 uuids[i], scanInfo->remote_address);
388 }else if(0 == strncasecmp(uuids[i], service_uuid,CUSTOM_UUID_LEN))
390 OIC_LOG_V(DEBUG, TAG, "Custom Service[%s] Found in %s",
391 uuids[i], scanInfo->remote_address);
401 static bool CALEIsHaveService(bt_adapter_le_device_scan_result_info_s *scanInfo,
402 const char *service_uuid)
405 // For arduino servers, scan response will give the UUIDs advertised.
406 CALEIsHaveServiceImpl(scanInfo, service_uuid,
407 BT_ADAPTER_LE_PACKET_SCAN_RESPONSE) ||
408 // For android/tizen servers, advertising packet will give the UUIDs.
409 CALEIsHaveServiceImpl(scanInfo, service_uuid,
410 BT_ADAPTER_LE_PACKET_ADVERTISING);
413 void CALEAdapterScanResultCb(int result, bt_adapter_le_device_scan_result_info_s *scanInfo,
417 OIC_LOG(DEBUG, TAG, "IN");
419 VERIFY_NON_NULL_VOID(scanInfo, TAG, "scanInfo");
420 VERIFY_NON_NULL_VOID(scanInfo->remote_address, TAG, "scanInfo->remote_address");
422 OIC_LOG_V(DEBUG, TAG, "Remote Address [%s]", scanInfo->remote_address);
423 OIC_LOG_V(DEBUG, TAG, "Scan Result [%d]", result);
424 OIC_LOG_V(DEBUG, TAG,
425 " Adv data len [%d] Scan data len[%d]RSSI [%d] Addr_type [%d] ",
426 scanInfo->adv_data_len, scanInfo->scan_data_len, scanInfo->rssi,
427 scanInfo->address_type);
429 // Check if scanning was stopped (since this callback is
430 // being triggered even after stopping the scan)
431 oc_mutex_lock(g_isScanningInProgressMutex);
432 if (!g_isScanningInProgress)
434 oc_mutex_unlock(g_isScanningInProgressMutex);
435 OIC_LOG(DEBUG, TAG, "Scanning not in progress, so ignoring callback");
438 oc_mutex_unlock(g_isScanningInProgressMutex);
440 LEServerInfo *serverInfo = NULL;
441 oc_mutex_lock(g_LEServerListMutex);
442 CAResult_t ret = CAGetLEServerInfo(g_LEServerList, scanInfo->remote_address, &serverInfo);
443 if (CA_STATUS_OK != ret)
445 OIC_LOG_V(DEBUG, TAG,
446 "Newly discovered device with address [%s] ", scanInfo->remote_address);
448 if (CALEIsHaveService(scanInfo, CA_GATT_SERVICE_UUID) ||
449 CALEIsHaveService(scanInfo, CA_GATT_CUSTOM_UUID)||
450 CALEIsHaveService(scanInfo, CA_GATT_CUSTOM_UUID2))
452 OIC_LOG_V(DEBUG, TAG, "Device [%s] supports OIC or custom service", scanInfo->remote_address);
454 char *addr = OICStrdup(scanInfo->remote_address);
457 oc_mutex_unlock(g_LEServerListMutex);
458 OIC_LOG(ERROR, TAG, "Device address is NULL");
461 serverInfo = (LEServerInfo *)OICCalloc(1, sizeof(LEServerInfo));
462 if (NULL == serverInfo)
464 oc_mutex_unlock(g_LEServerListMutex);
465 OIC_LOG(ERROR, TAG, "Calloc failed");
469 serverInfo->remoteAddress = addr;
470 serverInfo->status = LE_STATUS_DISCOVERED;
472 if (CA_STATUS_OK != CAAddLEServerInfoToList(&g_LEServerList, serverInfo))
474 oc_mutex_unlock(g_LEServerListMutex);
475 OIC_LOG_V(ERROR, TAG, "Could not add [%s] to server list", scanInfo->remote_address);
476 CAFreeLEServerInfo(serverInfo);
481 oc_mutex_unlock(g_LEServerListMutex);
482 OIC_LOG(DEBUG, TAG, "OUT");
485 void CASetLEClientThreadPoolHandle(ca_thread_pool_t handle)
487 OIC_LOG(DEBUG, TAG, "IN");
489 oc_mutex_lock(g_LEClientThreadPoolMutex);
490 g_LEClientThreadPool = handle;
491 oc_mutex_unlock(g_LEClientThreadPoolMutex);
493 OIC_LOG(DEBUG, TAG, "OUT");
496 void CASetLEReqRespClientCallback(CABLEDataReceivedCallback callback)
498 OIC_LOG(DEBUG, TAG, "IN");
500 oc_mutex_lock(g_LEReqRespClientCbMutex);
502 g_LEClientDataReceivedCallback = callback;
504 oc_mutex_unlock(g_LEReqRespClientCbMutex);
506 OIC_LOG(DEBUG, TAG, "OUT");
509 void CASetBLEClientErrorHandleCallback(CABLEErrorHandleCallback callback)
511 g_clientErrorCallback = callback;
514 CAResult_t CAStartLEGattClient()
516 OIC_LOG(DEBUG, TAG, "IN");
518 oc_mutex_lock(g_LEClientStateMutex);
519 if (true == g_isLEGattClientStarted)
521 OIC_LOG(ERROR, TAG, "Gatt Client is already running!!");
522 oc_mutex_unlock(g_LEClientStateMutex);
523 return CA_STATUS_FAILED;
526 CAResult_t result = CALEGattSetCallbacks();
527 if (CA_STATUS_OK != result)
529 OIC_LOG(ERROR, TAG, "CABleGattSetCallbacks Failed");
530 oc_mutex_unlock(g_LEClientStateMutex);
531 CATerminateLEGattClient();
532 return CA_STATUS_FAILED;
535 g_isLEGattClientStarted = true;
536 oc_mutex_unlock(g_LEClientStateMutex);
538 oc_mutex_lock(g_LEClientThreadPoolMutex);
539 if (NULL == g_LEClientThreadPool)
541 OIC_LOG(ERROR, TAG, "gBleServerThreadPool is NULL");
542 CATerminateGattClientMutexVariables();
543 oc_mutex_unlock(g_LEClientThreadPoolMutex);
544 return CA_STATUS_FAILED;
547 result = ca_thread_pool_add_task(g_LEClientThreadPool, CAStartTimerThread,
549 if (CA_STATUS_OK != result)
551 OIC_LOG(ERROR, TAG, "ca_thread_pool_add_task failed");
552 CATerminateGattClientMutexVariables();
553 oc_mutex_unlock(g_LEClientThreadPoolMutex);
554 return CA_STATUS_FAILED;
557 result= ca_thread_pool_add_task(g_LEClientThreadPool, CALEClientScanThread,
559 if (CA_STATUS_OK != result)
561 OIC_LOG(ERROR, TAG, "ca_thread_pool_add_task failed");
562 CATerminateGattClientMutexVariables();
563 oc_mutex_unlock(g_LEClientThreadPoolMutex);
564 return CA_STATUS_FAILED;
566 oc_mutex_unlock(g_LEClientThreadPoolMutex);
568 OIC_LOG(DEBUG, TAG, "OUT");
572 CAResult_t CALEClientScanThread()
574 oc_mutex_lock(g_scanMutex);
575 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
577 CAResult_t result = CALEGattStartDeviceScanning();
578 if (CA_STATUS_OK != result)
580 oc_mutex_unlock(g_scanMutex);
581 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning failed");
582 return CA_STATUS_FAILED;
584 g_isUnicastScanInProgress = true;
586 oc_cond_signal(g_startTimerCond);
590 g_isUnicastScanInProgress = true;
592 oc_cond_signal(g_scanningTimeCond);
594 oc_mutex_unlock(g_scanMutex);
596 OIC_LOG(DEBUG, TAG, "OUT");
600 void CAStartTimerThread(void *data)
604 OIC_LOG(DEBUG, TAG, "IN");
605 while (g_isLEGattClientStarted)
607 oc_mutex_lock(g_scanMutex);
608 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
610 OIC_LOG(DEBUG, TAG, "waiting....");
611 oc_cond_wait(g_startTimerCond, g_scanMutex);
612 OIC_LOG(DEBUG, TAG, "Wake up");
615 // Timed conditional wait for stopping the scan.
616 OCWaitResult_t ret = oc_cond_wait_for(g_scanningTimeCond, g_scanMutex,
618 if (OC_WAIT_TIMEDOUT == ret)
620 OIC_LOG(DEBUG, TAG, "Scan is timed Out");
622 CALEGattStopDeviceScanning();
624 if (g_isMulticastInProgress)
626 oc_mutex_lock(g_multicastDataListMutex);
627 // Clear the data list and device list.
628 u_arraylist_destroy(g_multicastDataList);
629 g_multicastDataList = NULL;
630 oc_mutex_unlock(g_multicastDataListMutex);
631 g_isMulticastInProgress = false;
633 g_isUnicastScanInProgress = false;
635 oc_mutex_unlock(g_scanMutex);
638 OIC_LOG(DEBUG, TAG, "OUT");
641 void CAStopLEGattClient()
643 OIC_LOG(DEBUG, TAG, "IN");
645 oc_mutex_lock(g_LEClientStateMutex);
647 if (false == g_isLEGattClientStarted)
649 OIC_LOG(ERROR, TAG, "Gatt Client is not running to stop");
650 oc_mutex_unlock(g_LEClientStateMutex);
654 CALEGattUnSetCallbacks();
656 CALEGattStopDeviceScanning();
658 g_isLEGattClientStarted = false;
660 // Signal the conditions waiting in Start timer.
661 oc_cond_signal(g_startTimerCond);
662 oc_cond_signal(g_scanningTimeCond);
664 // Destroy the multicast data list and device list if not empty.
665 if (NULL != g_multicastDataList)
667 oc_mutex_lock(g_multicastDataListMutex);
668 u_arraylist_destroy(g_multicastDataList);
669 g_multicastDataList = NULL;
670 oc_mutex_unlock(g_multicastDataListMutex);
673 oc_mutex_lock(g_LEServerListMutex);
674 CAFreeLEServerList(g_LEServerList);
675 g_LEServerList = NULL;
676 oc_mutex_unlock(g_LEServerListMutex);
678 oc_mutex_lock(g_threadWriteCharacteristicMutex);
679 oc_cond_signal(g_threadWriteCharacteristicCond);
680 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
682 GMainContext *context_event_loop = NULL;
683 // Required for waking up the thread which is running in gmain loop
684 if (NULL != g_eventLoop)
686 context_event_loop = g_main_loop_get_context(g_eventLoop);
688 if (context_event_loop)
690 OIC_LOG_V(DEBUG, TAG, "g_eventLoop context %p", (void *)context_event_loop);
691 g_main_context_wakeup(context_event_loop);
693 // Kill g main loops and kill threads.
694 g_main_loop_quit(g_eventLoop);
698 OIC_LOG(ERROR, TAG, "g_eventLoop context is NULL");
701 oc_mutex_unlock(g_LEClientStateMutex);
703 OIC_LOG(DEBUG, TAG, "OUT");
706 CAResult_t CAInitializeLEGattClient()
708 OIC_LOG(DEBUG, TAG, "Initialize GATT Client");
709 CAResult_t res = CAInitGattClientMutexVariables();
710 if (CA_STATUS_OK != res)
712 OIC_LOG(ERROR, TAG, "CAInitGattClientMutexVariables failed!");
713 CATerminateGattClientMutexVariables();
714 return CA_STATUS_FAILED;
719 void CATerminateLEGattClient()
721 OIC_LOG(DEBUG, TAG, "IN");
723 CATerminateGattClientMutexVariables();
725 OIC_LOG(DEBUG, TAG, "OUT");
728 CAResult_t CAInitGattClientMutexVariables()
730 OIC_LOG(DEBUG, TAG, "IN");
731 if (NULL == g_LEClientStateMutex)
733 g_LEClientStateMutex = oc_mutex_new();
734 if (NULL == g_LEClientStateMutex)
736 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
737 return CA_STATUS_FAILED;
741 if (NULL == g_LEServerListMutex)
743 g_LEServerListMutex = oc_mutex_new();
744 if (NULL == g_LEServerListMutex)
746 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
747 return CA_STATUS_FAILED;
751 if (NULL == g_LEReqRespClientCbMutex)
753 g_LEReqRespClientCbMutex = oc_mutex_new();
754 if (NULL == g_LEReqRespClientCbMutex)
756 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
757 return CA_STATUS_FAILED;
761 if (NULL == g_LEClientThreadPoolMutex)
763 g_LEClientThreadPoolMutex = oc_mutex_new();
764 if (NULL == g_LEClientThreadPoolMutex)
766 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
767 return CA_STATUS_FAILED;
771 if (NULL == g_LEClientConnectMutex)
773 g_LEClientConnectMutex = oc_mutex_new();
774 if (NULL == g_LEClientConnectMutex)
776 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
777 return CA_STATUS_FAILED;
781 if (NULL == g_isScanningInProgressMutex)
783 g_isScanningInProgressMutex = oc_mutex_new();
784 if (NULL == g_isScanningInProgressMutex)
786 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
787 return CA_STATUS_FAILED;
791 if (NULL == g_isConnectionInProgressMutex)
793 g_isConnectionInProgressMutex = oc_mutex_new();
794 if (NULL == g_isConnectionInProgressMutex)
796 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
797 return CA_STATUS_FAILED;
801 if (NULL == g_multicastDataListMutex)
803 g_multicastDataListMutex = oc_mutex_new();
804 if (NULL == g_multicastDataListMutex)
806 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
807 return CA_STATUS_FAILED;
811 if (NULL == g_scanMutex)
813 g_scanMutex = oc_mutex_new();
814 if (NULL == g_scanMutex)
816 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
817 return CA_STATUS_FAILED;
821 if (NULL == g_threadWriteCharacteristicMutex)
823 g_threadWriteCharacteristicMutex = oc_mutex_new();
824 if (NULL == g_threadWriteCharacteristicMutex)
826 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
827 return CA_STATUS_FAILED;
831 if (NULL == g_startTimerCond)
833 g_startTimerCond = oc_cond_new();
834 if (NULL == g_startTimerCond)
836 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
837 return CA_STATUS_FAILED;
841 if (NULL == g_scanningTimeCond)
843 g_scanningTimeCond = oc_cond_new();
844 if (NULL == g_scanningTimeCond)
846 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
847 return CA_STATUS_FAILED;
851 if (NULL == g_threadWriteCharacteristicCond)
853 g_threadWriteCharacteristicCond = oc_cond_new();
854 if (NULL == g_threadWriteCharacteristicCond)
856 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
857 return CA_STATUS_FAILED;
861 OIC_LOG(DEBUG, TAG, "OUT");
865 void CATerminateGattClientMutexVariables()
867 OIC_LOG(DEBUG, TAG, "IN");
869 oc_mutex_free(g_LEClientStateMutex);
870 g_LEClientStateMutex = NULL;
872 oc_mutex_free(g_LEServerListMutex);
873 g_LEServerListMutex = NULL;
875 oc_mutex_free(g_LEReqRespClientCbMutex);
876 g_LEReqRespClientCbMutex = NULL;
878 oc_mutex_free(g_LEClientConnectMutex);
879 g_LEClientConnectMutex = NULL;
881 oc_mutex_free(g_LEClientThreadPoolMutex);
882 g_LEClientThreadPoolMutex = NULL;
884 oc_mutex_free(g_isScanningInProgressMutex);
885 g_isScanningInProgressMutex = NULL;
887 oc_mutex_free(g_isConnectionInProgressMutex);
888 g_isConnectionInProgressMutex = NULL;
890 oc_mutex_free(g_multicastDataListMutex);
891 g_multicastDataListMutex = NULL;
893 oc_mutex_free(g_scanMutex);
896 oc_mutex_free(g_threadWriteCharacteristicMutex);
897 g_threadWriteCharacteristicMutex = NULL;
899 oc_cond_free(g_startTimerCond);
900 g_startTimerCond = NULL;
902 oc_cond_free(g_scanningTimeCond);
903 g_scanningTimeCond = NULL;
905 oc_cond_free(g_threadWriteCharacteristicCond);
906 g_threadWriteCharacteristicCond = NULL;
907 g_isSignalSetFlag = false;
909 OIC_LOG(DEBUG, TAG, "OUT");
912 CAResult_t CALEGattSetCallbacks()
914 OIC_LOG(DEBUG, TAG, "IN");
916 OIC_LOG(DEBUG, TAG, "OUT");
920 void CALEGattUnSetCallbacks()
922 OIC_LOG(DEBUG, TAG, "IN");
924 bt_gatt_unset_connection_state_changed_cb();
926 oc_mutex_lock(g_LEServerListMutex);
927 LEServerInfoList *curNode = g_LEServerList;
930 LEServerInfo *serverInfo = curNode->serverInfo;
931 if (serverInfo->status >= LE_STATUS_SERVICES_DISCOVERED)
933 bt_gatt_client_unset_characteristic_value_changed_cb(serverInfo->readChar);
935 curNode = curNode->next;
937 oc_mutex_unlock(g_LEServerListMutex);
939 OIC_LOG(DEBUG, TAG, "OUT");
942 CAResult_t CALEGattStartDeviceScanning()
944 OIC_LOG(DEBUG, TAG, "IN");
946 oc_mutex_lock(g_isScanningInProgressMutex);
947 if (!g_isScanningInProgress)
949 int ret = bt_adapter_le_start_scan(CALEAdapterScanResultCb, NULL);
950 if (BT_ERROR_NONE != ret)
952 oc_mutex_unlock(g_isScanningInProgressMutex);
953 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_start_scan failed[%s]",
954 CALEGetErrorMsg(ret));
955 return CA_STATUS_FAILED;
957 g_isScanningInProgress = true;
961 OIC_LOG(DEBUG, TAG, "Ignore, scanning already in progress");
963 oc_mutex_unlock(g_isScanningInProgressMutex);
965 OIC_LOG(DEBUG, TAG, "OUT");
969 void CALEGattStopDeviceScanning()
971 OIC_LOG(DEBUG, TAG, "IN");
973 oc_mutex_lock(g_isScanningInProgressMutex);
974 if (g_isScanningInProgress)
976 int ret = bt_adapter_le_stop_scan();
977 if (BT_ERROR_NONE != ret)
979 oc_mutex_unlock(g_isScanningInProgressMutex);
980 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_stop_scan failed[%s]",
981 CALEGetErrorMsg(ret));
984 g_isScanningInProgress = false;
988 OIC_LOG(DEBUG, TAG, "Ignore, scanning not in progress");
990 oc_mutex_unlock(g_isScanningInProgressMutex);
992 OIC_LOG(DEBUG, TAG, "OUT");
995 void CAGattConnectThread (void *remoteAddress)
997 OIC_LOG(DEBUG, TAG, "IN ");
999 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
1001 char *address = (char *)remoteAddress;
1003 OIC_LOG_V(DEBUG, TAG, "remote address is [%s]", address);
1005 CAResult_t result = CALEGattConnect(address);
1007 if (CA_STATUS_OK != result)
1009 OIC_LOG_V(ERROR, TAG, "bt_gatt_connect failed for [%s]", address);
1014 OIC_LOG(DEBUG, TAG, "OUT");
1017 CAResult_t CALEGattConnect(const char *remoteAddress)
1019 OIC_LOG(DEBUG, TAG, "IN");
1021 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1022 "remote address is NULL", CA_STATUS_FAILED);
1024 oc_mutex_lock(g_LEClientConnectMutex);
1025 CAResult_t result = CA_STATUS_OK;
1027 int ret = bt_gatt_connect(remoteAddress, false);
1028 if (BT_ERROR_NONE != ret)
1030 OIC_LOG_V(ERROR, TAG, "bt_gatt_connect Failed with ret value [%s] ",
1031 CALEGetErrorMsg(ret));
1032 oc_mutex_unlock(g_LEClientConnectMutex);
1033 return CA_STATUS_FAILED;
1036 oc_mutex_unlock(g_LEClientConnectMutex);
1038 OIC_LOG(DEBUG, TAG, "OUT");
1042 CAResult_t CALEGattDisConnect(const char *remoteAddress)
1044 OIC_LOG(DEBUG, TAG, "IN");
1046 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1047 "remote address is NULL", CA_STATUS_FAILED);
1049 int ret = bt_gatt_disconnect(remoteAddress);
1051 if (BT_ERROR_NONE != ret)
1053 OIC_LOG_V(ERROR, TAG, "bt_gatt_disconnect Failed with ret value [%s] ",
1054 CALEGetErrorMsg(ret));
1055 return CA_STATUS_FAILED;
1058 OIC_LOG(DEBUG, TAG, "OUT");
1059 return CA_STATUS_OK;
1062 CAResult_t CAUpdateCharacteristicsToGattServerImpl(LEServerInfo *serverInfo,
1063 const uint8_t *data, const uint32_t dataLen)
1065 OIC_LOG(DEBUG, TAG, "IN");
1067 VERIFY_NON_NULL(serverInfo, TAG, "Server Info is NULL");
1069 CALEGattStopDeviceScanning();
1071 OIC_LOG_V(DEBUG, TAG, "Updating the data of length [%d] to [%s] ", dataLen,
1072 serverInfo->remoteAddress);
1074 int result = bt_gatt_set_value(serverInfo->writeChar, (char *)data, dataLen);
1076 if (BT_ERROR_NONE != result)
1078 OIC_LOG_V(ERROR, TAG,
1079 "bt_gatt_set_value Failed with return val [%s]",
1080 CALEGetErrorMsg(result));
1084 result = bt_gatt_client_write_value(serverInfo->writeChar, CALEGattCharacteristicWriteCb,
1086 if (BT_ERROR_NONE != result)
1088 OIC_LOG_V(ERROR, TAG,
1089 "bt_gatt_client_write_value Failed with return val [%s]",
1090 CALEGetErrorMsg(result));
1094 // wait for callback for write Characteristic with success to sent data
1095 OIC_LOG_V(DEBUG, TAG, "callback flag is %d", g_isSignalSetFlag);
1096 oc_mutex_lock(g_threadWriteCharacteristicMutex);
1097 if (!g_isSignalSetFlag)
1099 OIC_LOG(DEBUG, TAG, "wait for callback to notify writeCharacteristic is success");
1100 if (OC_WAIT_SUCCESS != oc_cond_wait_for(g_threadWriteCharacteristicCond,
1101 g_threadWriteCharacteristicMutex,
1102 WAIT_TIME_WRITE_CHARACTERISTIC))
1104 g_isSignalSetFlag = false;
1105 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
1106 OIC_LOG(ERROR, TAG, "there is no response. write has failed");
1110 // reset flag set by writeCharacteristic Callback
1111 g_isSignalSetFlag = false;
1112 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
1114 oc_mutex_lock(g_scanMutex);
1115 if (g_isMulticastInProgress || g_isUnicastScanInProgress)
1117 if (CA_STATUS_OK != CALEGattStartDeviceScanning())
1119 OIC_LOG(ERROR, TAG, "Could not start device scanning");
1122 oc_mutex_unlock(g_scanMutex);
1123 OIC_LOG(DEBUG, TAG, "OUT");
1124 return CA_STATUS_OK;
1127 oc_mutex_lock(g_scanMutex);
1128 if (g_isMulticastInProgress || g_isUnicastScanInProgress)
1130 if (CA_STATUS_OK != CALEGattStartDeviceScanning())
1132 OIC_LOG(ERROR, TAG, "Could not start device scanning");
1135 oc_mutex_unlock(g_scanMutex);
1137 OIC_LOG(DEBUG, TAG, "OUT");
1138 return CA_STATUS_FAILED;
1141 void CADiscoverLEServicesThread(void *remoteAddress)
1143 OIC_LOG(DEBUG, TAG, "IN");
1144 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
1146 char *address = (char *)remoteAddress;
1148 CAResult_t result = CALEGattDiscoverServices(address);
1149 if (CA_STATUS_OK != result)
1151 OIC_LOG(ERROR, TAG, "CALEGattDiscoverServices failed");
1155 OIC_LOG(DEBUG, TAG, "OUT");
1158 CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
1160 OIC_LOG(DEBUG, TAG, "IN");
1162 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1163 "remote address is NULL", CA_STATUS_FAILED);
1165 LEServerInfo *serverInfo = NULL;
1166 oc_mutex_lock(g_LEServerListMutex);
1167 if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
1169 oc_mutex_unlock(g_LEServerListMutex);
1170 OIC_LOG_V(ERROR, TAG, "Could not get server info for [%s]", remoteAddress);
1171 CALEGattDisConnect(remoteAddress);
1172 return CA_STATUS_FAILED;
1175 bt_gatt_h serviceHandle = NULL;
1176 int32_t ret = bt_gatt_client_get_service(serverInfo->clientHandle, CA_GATT_SERVICE_UUID, &serviceHandle);
1177 if (BT_ERROR_NONE != ret || NULL == serviceHandle)
1179 OIC_LOG_V(ERROR, TAG,
1180 "bt_gatt_client_get_service Failed with ret value [%s] ", CALEGetErrorMsg(ret));
1181 bt_gatt_client_destroy(serverInfo->clientHandle);
1182 CALEGattDisConnect(remoteAddress);
1183 return CA_STATUS_FAILED;
1186 // Server will read data on this characteristic.
1187 bt_gatt_h writeChrHandle = NULL;
1188 ret = bt_gatt_service_get_characteristic(serviceHandle, CA_GATT_REQUEST_CHRC_UUID,
1190 if (BT_ERROR_NONE != ret || NULL == writeChrHandle)
1192 OIC_LOG_V(ERROR, TAG,
1193 "bt_gatt_service_get_characteristic Failed with ret value [%s] ",
1194 CALEGetErrorMsg(ret));
1195 bt_gatt_client_destroy(serverInfo->clientHandle);
1196 CALEGattDisConnect(remoteAddress);
1197 return CA_STATUS_FAILED;
1200 // Server will notify data on this characteristic.
1201 bt_gatt_h readChrHandle = NULL;
1202 ret = bt_gatt_service_get_characteristic(serviceHandle, CA_GATT_RESPONSE_CHRC_UUID,
1204 if (BT_ERROR_NONE != ret || NULL == readChrHandle)
1206 OIC_LOG_V(ERROR, TAG,
1207 "bt_gatt_service_get_characteristic Failed with ret value [%s] ",
1208 CALEGetErrorMsg(ret));
1209 bt_gatt_client_destroy(serverInfo->clientHandle);
1210 CALEGattDisConnect(remoteAddress);
1211 return CA_STATUS_FAILED;
1214 //TODO: This data has to be freed while unsetting the callback.
1215 char *addr = OICStrdup(remoteAddress);
1218 OIC_LOG(ERROR, TAG, "addr is NULL");
1219 bt_gatt_client_destroy(serverInfo->clientHandle);
1220 CALEGattDisConnect(remoteAddress);
1221 return CA_STATUS_FAILED;
1224 ret = bt_gatt_client_set_characteristic_value_changed_cb(readChrHandle,
1225 CALEGattCharacteristicChangedCb,
1227 if (BT_ERROR_NONE != ret)
1229 OIC_LOG_V(ERROR, TAG,
1230 "bt_gatt_client_set_characteristic_value_changed_cb Failed with ret value [%s]",
1231 CALEGetErrorMsg(ret));
1232 bt_gatt_client_destroy(serverInfo->clientHandle);
1233 CALEGattDisConnect(remoteAddress);
1234 return CA_STATUS_FAILED;
1237 serverInfo->serviceHandle = serviceHandle;
1238 serverInfo->readChar = readChrHandle;
1239 serverInfo->writeChar = writeChrHandle;
1240 serverInfo->status = LE_STATUS_SERVICES_DISCOVERED;
1242 while (serverInfo->pendingDataList)
1244 LEData *leData = serverInfo->pendingDataList->data;
1245 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(
1246 serverInfo, leData->data, leData->dataLength))
1248 OIC_LOG_V(ERROR, TAG, "Failed to send pending data to [%s]",
1249 serverInfo->remoteAddress);
1251 CADestroyLEDataList(&serverInfo->pendingDataList);
1254 CARemoveLEDataFromList(&serverInfo->pendingDataList);
1256 oc_mutex_unlock(g_LEServerListMutex);
1258 OIC_LOG(DEBUG, TAG, "OUT");
1259 return CA_STATUS_OK;
1262 CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
1263 const uint8_t *data, const uint32_t dataLen,
1264 CALETransferType_t type, const int32_t position)
1266 OIC_LOG(DEBUG, TAG, "IN");
1268 VERIFY_NON_NULL(data, TAG, "data is NULL");
1272 OIC_LOG(ERROR, TAG, "dataLen is less than or equal zero. Invalid input!");
1273 return CA_STATUS_INVALID_PARAM;
1276 LEServerInfo *serverInfo = NULL;
1277 oc_mutex_lock(g_LEServerListMutex);
1278 if (LE_UNICAST == type)
1280 if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
1282 OIC_LOG_V(DEBUG, TAG,
1283 "Device with address [%s] not yet found, initiating scan",
1286 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1288 oc_mutex_unlock(g_LEServerListMutex);
1289 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1290 return CA_STATUS_FAILED;
1293 oc_mutex_unlock(g_LEServerListMutex);
1295 oc_mutex_lock(g_scanMutex);
1296 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
1298 CAResult_t result = CALEGattStartDeviceScanning();
1299 if (CA_STATUS_OK != result)
1301 oc_mutex_unlock(g_scanMutex);
1302 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning failed");
1303 return CA_STATUS_FAILED;
1305 g_isUnicastScanInProgress = true;
1307 oc_cond_signal(g_startTimerCond);
1311 g_isUnicastScanInProgress = true;
1313 oc_cond_signal(g_scanningTimeCond);
1315 oc_mutex_unlock(g_scanMutex);
1317 OIC_LOG(DEBUG, TAG, "OUT");
1318 return CA_STATUS_OK;
1321 if (serverInfo->status == LE_STATUS_DISCOVERED)
1323 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1325 oc_mutex_unlock(g_LEServerListMutex);
1326 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1327 return CA_STATUS_FAILED;
1330 bt_gatt_client_h clientHandle = NULL;
1331 int32_t ret = bt_gatt_client_create(serverInfo->remoteAddress, &clientHandle);
1332 if (BT_ERROR_NONE != ret || NULL == clientHandle)
1334 OIC_LOG_V(ERROR, TAG,
1335 "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret));
1336 CALEGattDisConnect(serverInfo->remoteAddress);
1337 return CA_STATUS_FAILED;
1339 serverInfo->clientHandle = clientHandle;
1341 serverInfo->status = LE_STATUS_CONNECTION_INITIATED;
1342 if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress))
1344 OIC_LOG_V(ERROR, TAG, "Could not initiate connection to [%s]", serverInfo->remoteAddress);
1345 serverInfo->status = LE_STATUS_DISCOVERED;
1346 CADestroyLEDataList(&serverInfo->pendingDataList);
1347 oc_mutex_unlock(g_LEServerListMutex);
1348 return CA_STATUS_FAILED;
1351 else if (serverInfo->status < LE_STATUS_SERVICES_DISCOVERED)
1353 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1355 oc_mutex_unlock(g_LEServerListMutex);
1356 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1357 return CA_STATUS_FAILED;
1362 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(serverInfo, data, dataLen))
1364 OIC_LOG_V(ERROR, TAG, "Could not update characteristic to gatt server [%s]",
1365 serverInfo->remoteAddress);
1366 oc_mutex_unlock(g_LEServerListMutex);
1367 return CA_STATUS_FAILED;
1371 else if (LE_MULTICAST == type)
1373 OIC_LOG(ERROR, TAG, "LE_MULTICAST type Not used");
1375 oc_mutex_unlock(g_LEServerListMutex);
1376 OIC_LOG(DEBUG, TAG, "OUT");
1377 return CA_STATUS_OK;
1380 CAResult_t CAUpdateCharacteristicsToAllGattServers(const uint8_t *data, uint32_t dataLen)
1382 OIC_LOG(DEBUG, TAG, "IN");
1384 VERIFY_NON_NULL(data, TAG, "data is NULL");
1388 OIC_LOG(ERROR, TAG, "dataLen is less than or equal zero. Invalid input !");
1389 return CA_STATUS_INVALID_PARAM;
1392 oc_mutex_lock(g_LEServerListMutex);
1393 LEServerInfoList *curNode = g_LEServerList;
1396 LEServerInfo *serverInfo = curNode->serverInfo;
1397 if (serverInfo->status == LE_STATUS_SERVICES_DISCOVERED)
1399 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(serverInfo, data, dataLen))
1401 OIC_LOG_V(ERROR, TAG, "Failed to update characteristics to gatt server [%s]",
1402 serverInfo->remoteAddress);
1405 else if (serverInfo->status != LE_STATUS_INVALID)
1407 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1409 OIC_LOG(ERROR, TAG, "Failed to add to pending list");
1412 curNode = curNode->next;
1414 oc_mutex_unlock(g_LEServerListMutex);
1416 // Add the data to pending list.
1417 LEData *multicastData = (LEData *)OICCalloc(1, sizeof(LEData));
1418 if (NULL == multicastData)
1420 OIC_LOG(ERROR, TAG, "Calloc failed");
1423 multicastData->data = OICCalloc(1, dataLen);
1424 if (NULL == multicastData->data)
1426 OIC_LOG(ERROR, TAG, "Calloc failed");
1429 memcpy(multicastData->data, data, dataLen);
1430 multicastData->dataLength = dataLen;
1432 oc_mutex_lock(g_multicastDataListMutex);
1433 if (NULL == g_multicastDataList)
1435 g_multicastDataList = u_arraylist_create();
1437 u_arraylist_add(g_multicastDataList, (void *)multicastData);
1438 oc_mutex_unlock(g_multicastDataListMutex);
1440 // Start the scanning, if not started, else reset timer
1441 oc_mutex_lock(g_scanMutex);
1442 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
1444 CAResult_t result = CALEGattStartDeviceScanning();
1445 if (CA_STATUS_OK != result)
1447 oc_mutex_unlock(g_scanMutex);
1448 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning Failed");
1451 g_isMulticastInProgress = true;
1452 // Start the timer by signalling it
1453 oc_cond_signal(g_startTimerCond);
1457 g_isMulticastInProgress = true;
1459 oc_cond_signal(g_scanningTimeCond);
1461 oc_mutex_unlock(g_scanMutex);
1464 OIC_LOG(DEBUG, TAG, "OUT ");
1465 return CA_STATUS_OK;