1 /* ****************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #include "caleclient.h"
26 #include <arpa/inet.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
34 #include "uarraylist.h"
35 #include "caqueueingthread.h"
36 #include "caadapterutils.h"
37 #include "cagattservice.h"
38 #include "oic_string.h"
39 #include "oic_malloc.h"
42 * Logging tag for module name.
44 #define TAG "OIC_CA_LE_CLIENT"
46 #define MICROSECS_PER_SEC 1000000
47 #define WAIT_TIME_WRITE_CHARACTERISTIC 10 * MICROSECS_PER_SEC
49 uint64_t const TIMEOUT = 30 * MICROSECS_PER_SEC;
52 * Flag to check if multicast is already in progress.
54 static bool g_isMulticastInProgress = false;
57 * Pending multicast data list to be sent.
59 static u_arraylist_t *g_multicastDataList = NULL;
62 * Mutex to synchronize the access to Pending multicast data list.
64 static oc_mutex g_multicastDataListMutex = NULL;
67 * List of devices discovered.
69 static u_arraylist_t *g_deviceDiscoveredList = NULL;
72 * Mutex to synchronize the access to discovered devices list.
74 static oc_mutex g_deviceDiscoveredListMutex = NULL;
77 * Condition to start the timer for scanning.
79 static oc_cond g_startTimerCond = NULL;
82 * Condition for scanning Time interval.
84 static oc_cond g_scanningTimeCond = NULL;
87 * This contains the list of OIC services a client connect tot.
89 static LEServerInfoList *g_LEServerList = NULL;
92 * Mutex to synchronize access to BleServiceList.
94 static oc_mutex g_LEServerListMutex = NULL;
97 * Boolean variable to keep the state of the GATT Client.
99 static bool g_isLEGattClientStarted = false;
102 * Mutex to synchronize access to the requestResponse callback to be called
103 * when the data needs to be sent from GATTClient.
105 static oc_mutex g_LEReqRespClientCbMutex = NULL;
108 * Mutex to synchronize access to the requestResponse callback to be called
109 * when the data needs to be sent from GATTClient.
111 static oc_mutex g_LEClientConnectMutex = NULL;
114 * Mutex to synchronize the calls to be done to the platform from GATTClient
115 * interfaces from different threads.
117 static oc_mutex g_LEClientStateMutex = NULL;
120 * Mutex to synchronize the task to be pushed to thread pool.
122 static oc_mutex g_LEClientThreadPoolMutex = NULL;
125 * Mutex to synchronize the task to write characteristic one packet after another.
127 static oc_mutex g_threadWriteCharacteristicMutex = NULL;
130 * Condition for Writing characteristic.
132 static oc_cond g_threadWriteCharacteristicCond = NULL;
135 * Flag to check status of write characteristic.
137 static bool g_isSignalSetFlag = false;
140 * Maintains the callback to be notified on receival of network packets from other
143 static CABLEDataReceivedCallback g_LEClientDataReceivedCallback = NULL;
146 * callback to update the error to le adapter
148 static CABLEErrorHandleCallback g_clientErrorCallback;
151 * gmainLoop to manage the threads to receive the callback from the platfrom.
153 static GMainLoop *g_eventLoop = NULL;
156 * Reference to threadpool
158 static ca_thread_pool_t g_LEClientThreadPool = NULL;
160 bool CALEIsHaveService(bt_adapter_le_device_scan_result_info_s* scanInfo, char* service_uuid)
167 // For arduino servers, scan response will give the UUIDs advertised.
168 result = bt_adapter_le_get_scan_result_service_uuids(scanInfo,
169 BT_ADAPTER_LE_PACKET_SCAN_RESPONSE,
171 if (result == BT_ERROR_NONE && NULL != uuids)
174 for (i = 0; i < count; i++)
176 if (0 == strcasecmp(uuids[i], service_uuid))
178 OIC_LOG_V(DEBUG, TAG, "Service[%s] Found in %s",
179 uuids[i], scanInfo->remote_address);
187 // For android/tizen servers, advertising packet will give the UUIDs.
188 result = bt_adapter_le_get_scan_result_service_uuids(scanInfo,
189 BT_ADAPTER_LE_PACKET_ADVERTISING,
191 if (result == BT_ERROR_NONE && NULL != uuids)
194 for (i = 0; i < count; i++)
196 if (0 == strcasecmp(uuids[i], service_uuid))
198 OIC_LOG_V(DEBUG, TAG, "Service[%s] Found in %s",
199 uuids[i], scanInfo->remote_address);
210 bool CALEIsDeviceDiscovered(const char * address)
212 if (g_deviceDiscoveredList)
214 oc_mutex_lock(g_deviceDiscoveredListMutex);
215 uint32_t arrayLength = u_arraylist_length(g_deviceDiscoveredList);
216 for (uint32_t i = 0; i < arrayLength; i++)
218 char *deviceAddr = u_arraylist_get(g_deviceDiscoveredList, i);
219 if (0 == strcasecmp(deviceAddr, address))
221 oc_mutex_unlock(g_deviceDiscoveredListMutex);
226 oc_mutex_unlock(g_deviceDiscoveredListMutex);
231 void CALEGattCharacteristicChangedCb(bt_gatt_h characteristic,
233 int valueLen, void *userData)
235 OIC_LOG(DEBUG, TAG, "IN");
236 OIC_LOG_V(DEBUG, TAG, "Changed characteristic value length [%d]", valueLen);
238 oc_mutex_lock(g_LEReqRespClientCbMutex);
239 if (NULL == g_LEClientDataReceivedCallback)
241 OIC_LOG(ERROR, TAG, "Request response callback is not set");
242 oc_mutex_unlock(g_LEReqRespClientCbMutex);
246 uint32_t sentLength = 0;
247 g_LEClientDataReceivedCallback(userData, (uint8_t *)value, valueLen, &sentLength);
249 OIC_LOG_V(DEBUG, TAG, "Recv data Length is %d", sentLength);
251 oc_mutex_unlock(g_LEReqRespClientCbMutex);
253 OIC_LOG(DEBUG, TAG, "OUT");
256 void CALEGattCharacteristicWriteCb(int result, bt_gatt_h reqHandle, void *userData)
258 OIC_LOG(DEBUG, TAG, "IN ");
260 if (BT_ERROR_NONE != result)
262 CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, "", 0, -1,
263 false, "writeChar failure");
265 OIC_LOG(ERROR, TAG, "Write failed Need Retry ");
266 //Need to Implement retry mechanism
270 oc_mutex_lock(g_threadWriteCharacteristicMutex);
271 OIC_LOG(DEBUG, TAG, "g_isSignalSetFlag is set true and signal");
272 g_isSignalSetFlag = true;
273 oc_cond_signal(g_threadWriteCharacteristicCond);
274 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
276 CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, "", 0, -1,
277 true, "writeChar success");
280 OIC_LOG(DEBUG, TAG, "OUT ");
283 void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress)
285 OIC_LOG(DEBUG, TAG, "IN ");
287 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
289 // Start the scanning.
290 CAResult_t ret = CALEGattStartDeviceScanning();
291 if (CA_STATUS_OK != ret)
293 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceDiscovery Failed");
295 // Signal the start timer.
296 oc_cond_signal(g_scanningTimeCond);
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 char *addr = OICStrdup(remoteAddress);
312 OIC_LOG(ERROR, TAG, "addr is NULL");
316 oc_mutex_lock(g_LEClientThreadPoolMutex);
317 if (NULL == g_LEClientThreadPool)
319 OIC_LOG(ERROR, TAG, "g_LEClientThreadPool is NULL");
321 oc_mutex_unlock(g_LEClientThreadPoolMutex);
325 ret = ca_thread_pool_add_task(g_LEClientThreadPool, CADiscoverLEServicesThread,
327 if (CA_STATUS_OK != ret)
329 OIC_LOG_V(ERROR, TAG, "ca_thread_pool_add_task failed with ret [%d]", ret);
332 oc_mutex_unlock(g_LEClientThreadPoolMutex);
334 OIC_LOG(DEBUG, TAG, "OUT");
337 void CALEAdapterScanResultCb(int result, bt_adapter_le_device_scan_result_info_s *scanInfo,
340 OIC_LOG(DEBUG, TAG, "IN");
342 VERIFY_NON_NULL_VOID(scanInfo, TAG, "scanInfo");
343 VERIFY_NON_NULL_VOID(scanInfo->remote_address, TAG, "scanInfo->remote_address");
345 OIC_LOG_V(DEBUG, TAG, "Remote Address [%s]", scanInfo->remote_address);
346 OIC_LOG_V(DEBUG, TAG, "Scan Result [%d]", result);
347 OIC_LOG_V(DEBUG, TAG,
348 " Adv data len [%d] Scan data len[%d]RSSI [%d] Addr_type [%d] ",
349 scanInfo->adv_data_len, scanInfo->scan_data_len, scanInfo->rssi,
350 scanInfo->address_type);
352 // Check if device is already discovered.
353 if (CALEIsDeviceDiscovered(scanInfo->remote_address))
355 OIC_LOG_V(INFO, TAG, "Device[%s] is already discovered", scanInfo->remote_address);
359 if (!CALEIsHaveService(scanInfo, CA_GATT_SERVICE_UUID))
361 oc_mutex_lock(g_deviceDiscoveredListMutex);
362 // Add the the device Discovered list.
363 if (NULL == g_deviceDiscoveredList)
365 g_deviceDiscoveredList = u_arraylist_create();
367 char *deviceAddr = OICStrdup(scanInfo->remote_address);
368 if (NULL == deviceAddr)
370 OIC_LOG_V(ERROR, TAG, "Device address is NULL");
371 oc_mutex_unlock(g_deviceDiscoveredListMutex);
375 u_arraylist_add(g_deviceDiscoveredList, (void *) deviceAddr);
376 oc_mutex_unlock(g_deviceDiscoveredListMutex);
377 OIC_LOG_V(INFO, TAG, "Device[%s] is don't have service", scanInfo->remote_address);
381 // Stop the scan before invoking bt_gatt_connect().
382 CALEGattStopDeviceScanning();
384 size_t len = strlen(scanInfo->remote_address);
386 char *addr = (char *)OICMalloc(sizeof(char) * (len + 1));
387 VERIFY_NON_NULL_VOID(addr, TAG, "Malloc failed");
389 strncpy(addr, scanInfo->remote_address, len + 1);
391 OIC_LOG_V(DEBUG, TAG,
392 "Trying to do Gatt connection to [%s]", addr);
394 oc_mutex_lock(g_LEClientThreadPoolMutex);
395 if (NULL == g_LEClientThreadPool)
397 OIC_LOG(ERROR, TAG, "g_LEClientThreadPool is NULL");
399 oc_mutex_unlock(g_LEClientThreadPoolMutex);
403 CAResult_t res = ca_thread_pool_add_task(g_LEClientThreadPool, CAGattConnectThread, addr, NULL);
404 if (CA_STATUS_OK != res)
406 OIC_LOG_V(ERROR, TAG,
407 "ca_thread_pool_add_task failed with ret [%d]", res);
410 oc_mutex_unlock(g_LEClientThreadPoolMutex);
411 OIC_LOG(DEBUG, TAG, "OUT");
414 void CASetLEClientThreadPoolHandle(ca_thread_pool_t handle)
416 OIC_LOG(DEBUG, TAG, "IN");
418 oc_mutex_lock(g_LEClientThreadPoolMutex);
419 g_LEClientThreadPool = handle;
420 oc_mutex_unlock(g_LEClientThreadPoolMutex);
422 OIC_LOG(DEBUG, TAG, "OUT");
425 void CASetLEReqRespClientCallback(CABLEDataReceivedCallback callback)
427 OIC_LOG(DEBUG, TAG, "IN");
429 oc_mutex_lock(g_LEReqRespClientCbMutex);
431 g_LEClientDataReceivedCallback = callback;
433 oc_mutex_unlock(g_LEReqRespClientCbMutex);
435 OIC_LOG(DEBUG, TAG, "OUT");
438 void CASetBLEClientErrorHandleCallback(CABLEErrorHandleCallback callback)
440 g_clientErrorCallback = callback;
443 CAResult_t CAStartLEGattClient()
445 OIC_LOG(DEBUG, TAG, "IN");
447 oc_mutex_lock(g_LEClientStateMutex);
448 if (true == g_isLEGattClientStarted)
450 OIC_LOG(ERROR, TAG, "Gatt Client is already running!!");
451 oc_mutex_unlock(g_LEClientStateMutex);
452 return CA_STATUS_FAILED;
455 CAResult_t result = CALEGattSetCallbacks();
456 if (CA_STATUS_OK != result)
458 OIC_LOG(ERROR, TAG, "CABleGattSetCallbacks Failed");
459 oc_mutex_unlock(g_LEClientStateMutex);
460 CATerminateLEGattClient();
461 return CA_STATUS_FAILED;
464 g_isLEGattClientStarted = true;
465 oc_mutex_unlock(g_LEClientStateMutex);
467 oc_mutex_lock(g_LEClientThreadPoolMutex);
468 if (NULL == g_LEClientThreadPool)
470 OIC_LOG(ERROR, TAG, "gBleServerThreadPool is NULL");
471 CATerminateGattClientMutexVariables();
472 oc_mutex_unlock(g_LEClientThreadPoolMutex);
473 return CA_STATUS_FAILED;
476 result = ca_thread_pool_add_task(g_LEClientThreadPool, CAStartTimerThread,
478 if (CA_STATUS_OK != result)
480 OIC_LOG(ERROR, TAG, "ca_thread_pool_add_task failed");
481 CATerminateGattClientMutexVariables();
482 oc_mutex_unlock(g_LEClientThreadPoolMutex);
483 return CA_STATUS_FAILED;
485 oc_mutex_unlock(g_LEClientThreadPoolMutex);
487 OIC_LOG(DEBUG, TAG, "OUT");
491 void CAStartTimerThread(void *data)
493 OIC_LOG(DEBUG, TAG, "IN");
494 while (g_isLEGattClientStarted)
496 oc_mutex_lock(g_multicastDataListMutex);
497 if (!g_isMulticastInProgress)
499 OIC_LOG(DEBUG, TAG, "waiting....");
500 oc_cond_wait(g_startTimerCond, g_multicastDataListMutex);
501 OIC_LOG(DEBUG, TAG, "Wake up");
502 g_isMulticastInProgress = true;
504 if (!g_isLEGattClientStarted)
510 // Timed conditional wait for stopping the scan.
511 OCWaitResult_t ret = oc_cond_wait_for(g_scanningTimeCond, g_multicastDataListMutex,
513 if (OC_WAIT_TIMEDOUT == ret)
515 OIC_LOG(DEBUG, TAG, "Scan is timed Out");
517 CALEGattStopDeviceScanning();
519 // Clear the data list and device list.
520 u_arraylist_destroy(g_multicastDataList);
521 g_multicastDataList = NULL;
523 oc_mutex_lock(g_deviceDiscoveredListMutex);
524 u_arraylist_destroy(g_deviceDiscoveredList);
525 g_deviceDiscoveredList = NULL;
526 oc_mutex_unlock(g_deviceDiscoveredListMutex);
528 g_isMulticastInProgress = false;
530 oc_mutex_unlock(g_multicastDataListMutex);
533 OIC_LOG(DEBUG, TAG, "OUT");
536 void CAStopLEGattClient()
538 OIC_LOG(DEBUG, TAG, "IN");
540 oc_mutex_lock(g_LEClientStateMutex);
542 if (false == g_isLEGattClientStarted)
544 OIC_LOG(ERROR, TAG, "Gatt Client is not running to stop");
545 oc_mutex_unlock(g_LEClientStateMutex);
549 CALEGattUnSetCallbacks();
551 CALEGattStopDeviceScanning();
553 // this flag should be set before signal g_startTimerCond.
554 // since scan thread can be stopped through this flag.
555 g_isLEGattClientStarted = false;
557 // Signal the conditions waiting in Start timer.
558 oc_cond_signal(g_startTimerCond);
559 oc_cond_signal(g_scanningTimeCond);
561 // Destroy the multicast data list and device list if not empty.
562 if (NULL != g_multicastDataList)
564 oc_mutex_lock(g_multicastDataListMutex);
565 u_arraylist_destroy(g_multicastDataList);
566 g_multicastDataList = NULL;
567 oc_mutex_unlock(g_multicastDataListMutex);
570 if (NULL != g_deviceDiscoveredList)
572 oc_mutex_lock(g_deviceDiscoveredListMutex);
573 u_arraylist_destroy(g_deviceDiscoveredList);
574 g_deviceDiscoveredList = NULL;
575 oc_mutex_unlock(g_deviceDiscoveredListMutex);
578 oc_mutex_lock(g_LEServerListMutex);
579 CAFreeLEServerList(g_LEServerList);
580 g_LEServerList = NULL;
581 oc_mutex_unlock(g_LEServerListMutex);
583 oc_mutex_lock(g_threadWriteCharacteristicMutex);
584 oc_cond_signal(g_threadWriteCharacteristicCond);
585 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
587 CAResetRegisteredServiceCount();
589 GMainContext *context_event_loop = NULL;
590 // Required for waking up the thread which is running in gmain loop
591 if (NULL != g_eventLoop)
593 context_event_loop = g_main_loop_get_context(g_eventLoop);
595 if (context_event_loop)
597 OIC_LOG_V(DEBUG, TAG, "g_eventLoop context %x", context_event_loop);
598 g_main_context_wakeup(context_event_loop);
600 // Kill g main loops and kill threads.
601 g_main_loop_quit(g_eventLoop);
605 OIC_LOG(ERROR, TAG, "g_eventLoop context is NULL");
608 oc_mutex_unlock(g_LEClientStateMutex);
610 OIC_LOG(DEBUG, TAG, "OUT");
613 CAResult_t CAInitializeLEGattClient()
615 OIC_LOG(DEBUG, TAG, "Initialize GATT Client");
616 CAResult_t res = CAInitGattClientMutexVariables();
617 if (CA_STATUS_OK != res)
619 OIC_LOG(ERROR, TAG, "CAInitGattClientMutexVariables failed!");
620 CATerminateGattClientMutexVariables();
621 return CA_STATUS_FAILED;
626 void CATerminateLEGattClient()
628 OIC_LOG(DEBUG, TAG, "IN");
630 CATerminateGattClientMutexVariables();
632 OIC_LOG(DEBUG, TAG, "OUT");
635 CAResult_t CAInitGattClientMutexVariables()
637 OIC_LOG(DEBUG, TAG, "IN");
638 if (NULL == g_LEClientStateMutex)
640 g_LEClientStateMutex = oc_mutex_new();
641 if (NULL == g_LEClientStateMutex)
643 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
644 return CA_STATUS_FAILED;
648 if (NULL == g_LEServerListMutex)
650 g_LEServerListMutex = oc_mutex_new();
651 if (NULL == g_LEServerListMutex)
653 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
654 return CA_STATUS_FAILED;
658 if (NULL == g_LEReqRespClientCbMutex)
660 g_LEReqRespClientCbMutex = oc_mutex_new();
661 if (NULL == g_LEReqRespClientCbMutex)
663 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
664 return CA_STATUS_FAILED;
668 if (NULL == g_LEClientThreadPoolMutex)
670 g_LEClientThreadPoolMutex = oc_mutex_new();
671 if (NULL == g_LEClientThreadPoolMutex)
673 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
674 return CA_STATUS_FAILED;
678 if (NULL == g_LEClientConnectMutex)
680 g_LEClientConnectMutex = oc_mutex_new();
681 if (NULL == g_LEClientConnectMutex)
683 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
684 return CA_STATUS_FAILED;
688 if (NULL == g_multicastDataListMutex)
690 g_multicastDataListMutex = oc_mutex_new();
691 if (NULL == g_multicastDataListMutex)
693 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
694 return CA_STATUS_FAILED;
698 if (NULL == g_deviceDiscoveredListMutex)
700 g_deviceDiscoveredListMutex = oc_mutex_new();
701 if (NULL == g_deviceDiscoveredListMutex)
703 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
704 return CA_STATUS_FAILED;
708 if (NULL == g_threadWriteCharacteristicMutex)
710 g_threadWriteCharacteristicMutex = oc_mutex_new();
711 if (NULL == g_threadWriteCharacteristicMutex)
713 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
714 return CA_STATUS_FAILED;
718 if (NULL == g_startTimerCond)
720 g_startTimerCond = oc_cond_new();
721 if (NULL == g_startTimerCond)
723 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
724 return CA_STATUS_FAILED;
728 if (NULL == g_scanningTimeCond)
730 g_scanningTimeCond = oc_cond_new();
731 if (NULL == g_scanningTimeCond)
733 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
734 return CA_STATUS_FAILED;
738 if (NULL == g_threadWriteCharacteristicCond)
740 g_threadWriteCharacteristicCond = oc_cond_new();
741 if (NULL == g_threadWriteCharacteristicCond)
743 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
744 return CA_STATUS_FAILED;
748 OIC_LOG(DEBUG, TAG, "OUT");
752 void CATerminateGattClientMutexVariables()
754 OIC_LOG(DEBUG, TAG, "IN");
756 oc_mutex_free(g_LEClientStateMutex);
757 g_LEClientStateMutex = NULL;
759 oc_mutex_free(g_LEServerListMutex);
760 g_LEServerListMutex = NULL;
762 oc_mutex_free(g_LEReqRespClientCbMutex);
763 g_LEReqRespClientCbMutex = NULL;
765 oc_mutex_free(g_LEClientConnectMutex);
766 g_LEClientConnectMutex = NULL;
768 oc_mutex_free(g_LEClientThreadPoolMutex);
769 g_LEClientThreadPoolMutex = NULL;
771 oc_mutex_free(g_multicastDataListMutex);
772 g_multicastDataListMutex = NULL;
774 oc_mutex_free(g_deviceDiscoveredListMutex);
775 g_deviceDiscoveredListMutex = NULL;
777 oc_mutex_free(g_threadWriteCharacteristicMutex);
778 g_threadWriteCharacteristicMutex = NULL;
780 oc_cond_free(g_startTimerCond);
781 g_startTimerCond = NULL;
783 oc_cond_free(g_scanningTimeCond);
784 g_scanningTimeCond = NULL;
786 oc_cond_free(g_threadWriteCharacteristicCond);
787 g_threadWriteCharacteristicCond = NULL;
788 g_isSignalSetFlag = false;
790 OIC_LOG(DEBUG, TAG, "OUT");
793 CAResult_t CALEGattSetCallbacks()
795 OIC_LOG(DEBUG, TAG, "IN");
797 OIC_LOG(DEBUG, TAG, "OUT");
801 void CALEGattUnSetCallbacks()
803 OIC_LOG(DEBUG, TAG, "IN");
805 bt_gatt_unset_connection_state_changed_cb();
807 int numOfServersConnected = CAGetRegisteredServiceCount();
808 LEServerInfo *leServerInfo = NULL;
810 for (int32_t index = 0; index < numOfServersConnected; index++)
812 CAGetLEServerInfoByPosition(g_LEServerList, index, &leServerInfo);
813 bt_gatt_client_unset_characteristic_value_changed_cb(leServerInfo->readChar);
815 OIC_LOG(DEBUG, TAG, "OUT");
818 CAResult_t CALEGattStartDeviceScanning()
820 OIC_LOG(DEBUG, TAG, "IN");
822 int ret = bt_adapter_le_start_scan(CALEAdapterScanResultCb, NULL);
823 if(BT_ERROR_NONE != ret)
825 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_start_scan failed[%s]",
826 CALEGetErrorMsg(ret));
827 return CA_STATUS_FAILED;
830 OIC_LOG(DEBUG, TAG, "OUT");
834 void CALEGattStopDeviceScanning()
836 OIC_LOG(DEBUG, TAG, "IN");
838 int ret = bt_adapter_le_stop_scan();
839 if (BT_ERROR_NONE != ret)
841 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_stop_scan failed[%s]",
842 CALEGetErrorMsg(ret));
845 OIC_LOG(DEBUG, TAG, "OUT");
848 void CAGattConnectThread (void *remoteAddress)
850 OIC_LOG(DEBUG, TAG, "IN ");
852 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
854 char *address = (char *)remoteAddress;
856 OIC_LOG_V(DEBUG, TAG, "remote address is [%s]", address);
858 CAResult_t result = CALEGattConnect(address);
860 if (CA_STATUS_OK != result)
862 OIC_LOG_V(ERROR, TAG, "bt_gatt_connect failed for [%s]", address);
867 OIC_LOG(DEBUG, TAG, "OUT");
870 CAResult_t CALEGattConnect(const char *remoteAddress)
872 OIC_LOG(DEBUG, TAG, "IN");
874 VERIFY_NON_NULL_RET(remoteAddress, TAG,
875 "remote address is NULL", CA_STATUS_FAILED);
877 oc_mutex_lock(g_LEClientConnectMutex);
878 bool isConnected = false;
879 int ret = bt_device_is_profile_connected(remoteAddress, BT_PROFILE_GATT, &isConnected);
880 if (BT_ERROR_NONE != ret)
882 OIC_LOG_V(ERROR, TAG, "bt_device_is_profile_connected Failed with ret value [%s] ",
883 CALEGetErrorMsg(ret));
884 oc_mutex_unlock(g_LEClientConnectMutex);
885 return CA_STATUS_FAILED;
888 CAResult_t result = CA_STATUS_OK;
891 ret = bt_gatt_connect(remoteAddress, true);
893 if (BT_ERROR_NONE != ret)
895 OIC_LOG_V(ERROR, TAG, "bt_gatt_connect Failed with ret value [%s] ",
896 CALEGetErrorMsg(ret));
897 oc_mutex_unlock(g_LEClientConnectMutex);
898 return CA_STATUS_FAILED;
903 OIC_LOG_V(INFO, TAG, "Remote address[%s] is already connected",
905 char *addr = OICStrdup(remoteAddress);
908 OIC_LOG(ERROR, TAG, "addr is NULL");
909 oc_mutex_unlock(g_LEClientConnectMutex);
910 return CA_STATUS_FAILED;
913 oc_mutex_lock(g_LEClientThreadPoolMutex);
914 if (NULL == g_LEClientThreadPool)
916 OIC_LOG(ERROR, TAG, "g_LEClientThreadPool is NULL");
918 oc_mutex_unlock(g_LEClientThreadPoolMutex);
919 oc_mutex_unlock(g_LEClientConnectMutex);
920 return CA_STATUS_FAILED;
923 result = ca_thread_pool_add_task(g_LEClientThreadPool, CADiscoverLEServicesThread,
925 if (CA_STATUS_OK != result)
927 OIC_LOG_V(ERROR, TAG, "ca_thread_pool_add_task failed with ret [%d]", result);
930 oc_mutex_unlock(g_LEClientThreadPoolMutex);
932 oc_mutex_unlock(g_LEClientConnectMutex);
934 OIC_LOG(DEBUG, TAG, "OUT");
938 CAResult_t CALEGattDisConnect(const char *remoteAddress)
940 OIC_LOG(DEBUG, TAG, "IN");
942 VERIFY_NON_NULL_RET(remoteAddress, TAG,
943 "remote address is NULL", CA_STATUS_FAILED);
945 int ret = bt_gatt_disconnect(remoteAddress);
947 if (BT_ERROR_NONE != ret)
949 OIC_LOG_V(ERROR, TAG, "bt_gatt_disconnect Failed with ret value [%d] ",
950 CALEGetErrorMsg(ret));
951 return CA_STATUS_FAILED;
954 OIC_LOG(DEBUG, TAG, "OUT");
958 void CADiscoverLEServicesThread (void *remoteAddress)
960 OIC_LOG(DEBUG, TAG, "IN");
962 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
964 char *address = (char *)remoteAddress;
966 CAResult_t result = CALEGattDiscoverServices(address);
967 if (CA_STATUS_OK != result)
969 OIC_LOG(ERROR, TAG, "CALEGattDiscoverServices failed");
973 OIC_LOG(DEBUG, TAG, "OUT");
976 CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
978 OIC_LOG(DEBUG, TAG, "IN");
980 VERIFY_NON_NULL_RET(remoteAddress, TAG,
981 "remote address is NULL", CA_STATUS_FAILED);
983 LEServerInfo *leServerInfo = NULL;
984 CAResult_t result = CA_STATUS_FAILED;
986 oc_mutex_lock(g_LEServerListMutex);
987 result = CAGetLEServerInfo(g_LEServerList, remoteAddress, &leServerInfo);
988 oc_mutex_unlock(g_LEServerListMutex);
990 if (CA_STATUS_OK == result)
992 OIC_LOG_V(INFO, TAG, "Device[%s] is already discovered", leServerInfo->remoteAddress);
994 // Send the data of pending multicast data list if any.
995 if (g_multicastDataList)
997 oc_mutex_lock(g_multicastDataListMutex);
998 uint32_t arrayLength = u_arraylist_length(g_multicastDataList);
999 for (uint32_t i = 0; i < arrayLength; i++)
1001 CALEData_t *multicastData = u_arraylist_get(g_multicastDataList, i);
1002 if (NULL == multicastData)
1004 OIC_LOG(DEBUG, TAG, "multicastData is NULL");
1007 CAUpdateCharacteristicsToGattServer(remoteAddress, multicastData->data,
1008 multicastData->dataLen, LE_UNICAST, 0);
1010 oc_mutex_unlock(g_multicastDataListMutex);
1013 OIC_LOG(DEBUG, TAG, "OUT");
1014 return CA_STATUS_OK;
1018 OIC_LOG_V(INFO, TAG, "CAGetLEServerInfo [%s] is failed, %d", remoteAddress, result);
1021 bt_gatt_client_h clientHandle = NULL;
1022 int32_t ret = bt_gatt_client_create(remoteAddress, &clientHandle);
1023 if (BT_ERROR_NONE != ret || NULL == clientHandle)
1025 OIC_LOG_V(ERROR, TAG,
1026 "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret));
1027 return CA_STATUS_FAILED;
1030 bt_gatt_h serviceHandle = NULL;
1031 ret = bt_gatt_client_get_service(clientHandle, CA_GATT_SERVICE_UUID, &serviceHandle);
1032 if (BT_ERROR_NONE != ret || NULL == serviceHandle)
1034 OIC_LOG_V(ERROR, TAG,
1035 "bt_gatt_client_get_service Failed with ret value [%s] ", CALEGetErrorMsg(ret));
1036 bt_gatt_client_destroy(clientHandle);
1037 return CA_STATUS_FAILED;
1040 // Server will read data on this characteristic.
1041 bt_gatt_h writeChrHandle = NULL;
1042 ret = bt_gatt_service_get_characteristic(serviceHandle, CA_GATT_REQUEST_CHRC_UUID,
1044 if (BT_ERROR_NONE != ret || NULL == writeChrHandle)
1046 OIC_LOG_V(ERROR, TAG,
1047 "bt_gatt_service_get_characteristic Failed with ret value [%s] ",
1048 CALEGetErrorMsg(ret));
1049 bt_gatt_client_destroy(clientHandle);
1050 CALEGattDisConnect(remoteAddress);
1051 return CA_STATUS_FAILED;
1054 // Server will notify data on this characteristic.
1055 bt_gatt_h readChrHandle = NULL;
1056 ret = bt_gatt_service_get_characteristic(serviceHandle, CA_GATT_RESPONSE_CHRC_UUID,
1058 if (BT_ERROR_NONE != ret || NULL == readChrHandle)
1060 OIC_LOG_V(ERROR, TAG,
1061 "bt_gatt_service_get_characteristic Failed with ret value [%s] ",
1062 CALEGetErrorMsg(ret));
1063 bt_gatt_client_destroy(clientHandle);
1064 CALEGattDisConnect(remoteAddress);
1065 return CA_STATUS_FAILED;
1069 //TODO: This data has to be freed while unsetting the callback.
1070 char *addr = OICStrdup(remoteAddress);
1073 OIC_LOG(ERROR, TAG, "addr is NULL");
1074 bt_gatt_client_destroy(clientHandle);
1075 CALEGattDisConnect(remoteAddress);
1076 return CA_STATUS_FAILED;
1079 ret = bt_gatt_client_set_characteristic_value_changed_cb(readChrHandle,
1080 CALEGattCharacteristicChangedCb,
1082 if (BT_ERROR_NONE != ret)
1084 OIC_LOG_V(ERROR, TAG,
1085 "bt_gatt_client_set_characteristic_value_changed_cb Failed with ret value [%s]",
1086 CALEGetErrorMsg(ret));
1087 bt_gatt_client_destroy(clientHandle);
1088 CALEGattDisConnect(remoteAddress);
1089 return CA_STATUS_FAILED;
1092 LEServerInfo *serverInfo = (LEServerInfo *)OICCalloc(1, sizeof(LEServerInfo));
1093 if (NULL == serverInfo)
1095 OIC_LOG(ERROR, TAG, "Malloc failed");
1096 CALEGattDisConnect(remoteAddress);
1097 return CA_MEMORY_ALLOC_FAILED;
1099 serverInfo->clientHandle = clientHandle;
1100 serverInfo->serviceHandle = serviceHandle;
1101 serverInfo->readChar = readChrHandle;
1102 serverInfo->writeChar = writeChrHandle;
1103 serverInfo->remoteAddress = OICStrdup(remoteAddress);
1105 oc_mutex_lock(g_LEServerListMutex);
1106 result = CAAddLEServerInfoToList(&g_LEServerList, serverInfo);
1107 if (CA_STATUS_OK != result)
1109 OIC_LOG(ERROR, TAG, "CAAddLEServerInfoToList failed");
1110 bt_gatt_client_destroy(clientHandle);
1111 CALEGattDisConnect(remoteAddress);
1112 return CA_STATUS_FAILED;
1114 oc_mutex_unlock(g_LEServerListMutex);
1116 oc_mutex_lock(g_deviceDiscoveredListMutex);
1117 // Add the the device Discovered list.
1118 if (NULL == g_deviceDiscoveredList)
1120 g_deviceDiscoveredList = u_arraylist_create();
1122 char *deviceAddr = OICStrdup(remoteAddress);
1123 if (NULL == deviceAddr)
1125 OIC_LOG_V(ERROR, TAG, "Device address is NULL");
1126 oc_mutex_unlock(g_deviceDiscoveredListMutex);
1127 return CA_STATUS_FAILED;
1129 u_arraylist_add(g_deviceDiscoveredList, (void *) deviceAddr);
1130 oc_mutex_unlock(g_deviceDiscoveredListMutex);
1132 // Send the data of pending multicast data list if any.
1133 if (g_multicastDataList)
1135 oc_mutex_lock(g_multicastDataListMutex);
1136 uint32_t arrayLength = u_arraylist_length(g_multicastDataList);
1137 for (uint32_t i = 0; i < arrayLength; i++)
1139 CALEData_t *multicastData = u_arraylist_get(g_multicastDataList, i);
1140 if (NULL == multicastData)
1142 OIC_LOG(DEBUG, TAG, "multicastData is NULL");
1145 CAUpdateCharacteristicsToGattServer(remoteAddress, multicastData->data,
1146 multicastData->dataLen, LE_UNICAST, 0);
1148 oc_mutex_unlock(g_multicastDataListMutex);
1151 OIC_LOG(DEBUG, TAG, "OUT");
1152 return CA_STATUS_OK;
1155 CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
1156 const uint8_t *data, const uint32_t dataLen,
1157 CALETransferType_t type, const int32_t position)
1159 OIC_LOG(DEBUG, TAG, "IN");
1161 VERIFY_NON_NULL(data, TAG, "data is NULL");
1165 OIC_LOG(ERROR, TAG, "dataLen is less than or equal zero. Invalid input!");
1166 return CA_STATUS_INVALID_PARAM;
1169 LEServerInfo *leServerInfo = NULL;
1170 CAResult_t ret = CA_STATUS_FAILED;
1172 oc_mutex_lock(g_LEServerListMutex);
1173 if (LE_UNICAST == type)
1175 ret = CAGetLEServerInfo(g_LEServerList, remoteAddress, &leServerInfo);
1177 else if (LE_MULTICAST == type)
1179 ret = CAGetLEServerInfoByPosition(g_LEServerList, position, &leServerInfo);
1181 oc_mutex_unlock(g_LEServerListMutex);
1183 if (CA_STATUS_OK != ret)
1185 OIC_LOG(ERROR, TAG, "CAGetBLEServiceInfoByPosition is failed");
1186 return CA_STATUS_FAILED;
1189 VERIFY_NON_NULL(leServerInfo, TAG, "bleServiceInfo is NULL");
1191 OIC_LOG_V(DEBUG, TAG, "Updating the data of length [%d] to [%s] ", dataLen,
1192 leServerInfo->remoteAddress);
1194 int result = bt_gatt_set_value(leServerInfo->writeChar, (char *)data, dataLen);
1196 if (BT_ERROR_NONE != result)
1198 OIC_LOG_V(ERROR, TAG,
1199 "bt_gatt_set_value Failed with return val [%s]",
1200 CALEGetErrorMsg(result));
1201 return CA_STATUS_FAILED;
1204 result = bt_gatt_client_write_value(leServerInfo->writeChar, CALEGattCharacteristicWriteCb,
1206 if (BT_ERROR_NONE != result)
1208 OIC_LOG_V(ERROR, TAG,
1209 "bt_gatt_client_write_value Failed with return val [%s]",
1210 CALEGetErrorMsg(result));
1211 return CA_STATUS_FAILED;
1214 // wait for callback for write Characteristic with success to sent data
1215 OIC_LOG_V(DEBUG, TAG, "callback flag is %d", g_isSignalSetFlag);
1216 oc_mutex_lock(g_threadWriteCharacteristicMutex);
1217 if (!g_isSignalSetFlag)
1219 OIC_LOG(DEBUG, TAG, "wait for callback to notify writeCharacteristic is success");
1220 if (OC_WAIT_SUCCESS != oc_cond_wait_for(g_threadWriteCharacteristicCond,
1221 g_threadWriteCharacteristicMutex,
1222 WAIT_TIME_WRITE_CHARACTERISTIC))
1224 OIC_LOG(ERROR, TAG, "there is no response. write has failed");
1225 g_isSignalSetFlag = false;
1226 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
1227 return CA_SEND_FAILED;
1230 // reset flag set by writeCharacteristic Callback
1231 g_isSignalSetFlag = false;
1232 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
1234 OIC_LOG(DEBUG, TAG, "OUT");
1235 return CA_STATUS_OK;
1238 CAResult_t CAUpdateCharacteristicsToAllGattServers(const uint8_t *data, uint32_t dataLen)
1240 OIC_LOG(DEBUG, TAG, "IN");
1242 VERIFY_NON_NULL(data, TAG, "data is NULL");
1246 OIC_LOG(ERROR, TAG, "dataLen is less than or equal zero. Invalid input !");
1247 return CA_STATUS_INVALID_PARAM;
1250 int numOfServersConnected = CAGetRegisteredServiceCount();
1252 // Send data to already connected devices.
1253 for (int32_t pos = 0; pos < numOfServersConnected; pos++)
1255 /*remoteAddress will be NULL.
1256 Since we have to send to all destinations. pos will be used for getting remote address.
1258 int32_t ret = CAUpdateCharacteristicsToGattServer(NULL, data, dataLen, LE_MULTICAST, pos);
1260 if (CA_STATUS_OK != ret)
1262 OIC_LOG_V(ERROR, TAG,
1263 "CAUpdateCharacteristicsToGattServer Failed with return val [%d] ", ret);
1264 g_clientErrorCallback(NULL, data, dataLen, ret);
1268 // Add the data to pending list.
1269 CALEData_t *multicastData = (CALEData_t *)OICCalloc(1, sizeof(CALEData_t));
1270 if (NULL == multicastData)
1272 OIC_LOG(ERROR, TAG, "Calloc failed");
1275 multicastData->data = OICCalloc(1, dataLen);
1276 if (NULL == multicastData->data)
1278 OIC_LOG(ERROR, TAG, "Calloc failed");
1281 memcpy(multicastData->data, data, dataLen);
1282 multicastData->dataLen = dataLen;
1284 oc_mutex_lock(g_multicastDataListMutex);
1285 if (NULL == g_multicastDataList)
1287 g_multicastDataList = u_arraylist_create();
1289 u_arraylist_add(g_multicastDataList, (void *)multicastData);
1290 oc_mutex_unlock(g_multicastDataListMutex);
1292 // Start the scanning.
1293 CAResult_t result = CALEGattStartDeviceScanning();
1294 if (CA_STATUS_OK != result)
1296 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceDiscovery Failed");
1300 // Start the timer by signalling it.
1301 oc_cond_signal(g_startTimerCond);
1304 OIC_LOG(DEBUG, TAG, "OUT ");
1305 return CA_STATUS_OK;
1308 bool CALEClientIsConnected(const char* address)
1314 uint16_t CALEClientGetMtuSize(const char* address)
1316 VERIFY_NON_NULL_RET(address, TAG, "address is null", CA_DEFAULT_BLE_MTU_SIZE);
1318 //it should be implemented after update Tizen 3.0
1319 return CA_DEFAULT_BLE_MTU_SIZE;
1322 CAResult_t CALEClientSetMtuSize(const char* address, uint16_t mtuSize)
1324 VERIFY_NON_NULL(address, TAG, "address is null");
1326 //it should be implemented after update Tizen 3.0
1327 return CA_NOT_SUPPORTED;
1330 CAResult_t CALEClientSendNegotiationMessage(const char* address)
1332 OIC_LOG_V(DEBUG, TAG, "CALEClientSendNegotiationMessage(%s)", address);
1334 //it will be implemented when tizen public 3.0 is released.
1335 return CA_NOT_SUPPORTED;