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"
47 #define MICROSECS_PER_SEC 1000000
48 #define WAIT_TIME_WRITE_CHARACTERISTIC 10 * MICROSECS_PER_SEC
50 //For custom uuid ble server
51 #define CA_GATT_CUSTOM_UUID "4209"
52 #define CA_GATT_CUSTOM_UUID2 "4204"
53 #define CUSTOM_UUID_LEN 4
54 #define MICROSECS_PER_SEC 1000000
56 static const int samsung_code = 117;
57 static int g_retrycount = 0;
58 static int retry_flag = 0;
59 uint64_t const TIMEOUT = 30 * MICROSECS_PER_SEC;
62 * Mutex to call connect only after disconnect during retry
64 static oc_mutex g_isDisconnectedMutex = NULL;
67 * Condition for calling connect during connection retry
69 static oc_cond g_LEDisconnectedCond = NULL;
72 * Flag to check if scanning is in progress
74 static bool g_isScanningInProgress = false;
77 * Mutex to synchronize access to g_isScanningInProgress
79 static oc_mutex g_isScanningInProgressMutex = NULL;
82 * Flag to check if connection is in progress
84 static bool g_isConnectionInProgress = false;
87 * Mutex to synchronize access to g_isConnectionInProgress
89 static oc_mutex g_isConnectionInProgressMutex = NULL;
92 * Flag to check if multicast is already in progress.
94 static bool g_isMulticastInProgress = false;
97 * Flag to check if unicast scan is in progress
99 static bool g_isUnicastScanInProgress = false;
102 * Mutex to synchronize access to g_isMulticastInProgress
103 * and g_isUnicastScanInProgress
105 static oc_mutex g_scanMutex = NULL;
108 * Pending multicast data list to be sent.
110 static u_arraylist_t *g_multicastDataList = NULL;
113 * Mutex to synchronize the access to Pending multicast data list.
115 static oc_mutex g_multicastDataListMutex = NULL;
118 * Condition to start the timer for scanning.
120 static oc_cond g_startTimerCond = NULL;
123 * Condition for scanning Time interval.
125 static oc_cond g_scanningTimeCond = NULL;
128 * This contains the list of OIC services a client connect tot.
130 static LEServerInfoList *g_LEServerList = NULL;
133 * Mutex to synchronize access to BleServiceList.
135 static oc_mutex g_LEServerListMutex = NULL;
138 * Boolean variable to keep the state of the GATT Client.
140 static bool g_isLEGattClientStarted = false;
143 * Mutex to synchronize access to the requestResponse callback to be called
144 * when the data needs to be sent from GATTClient.
146 static oc_mutex g_LEReqRespClientCbMutex = NULL;
149 * Mutex to synchronize access to the requestResponse callback to be called
150 * when the data needs to be sent from GATTClient.
152 static oc_mutex g_LEClientConnectMutex = NULL;
155 * Mutex to synchronize the calls to be done to the platform from GATTClient
156 * interfaces from different threads.
158 static oc_mutex g_LEClientStateMutex = NULL;
161 * Mutex to synchronize the task to be pushed to thread pool.
163 static oc_mutex g_LEClientThreadPoolMutex = NULL;
166 * Mutex to synchronize the task to write characteristic one packet after another.
168 static oc_mutex g_threadWriteCharacteristicMutex = NULL;
171 * Condition for Writing characteristic.
173 static oc_cond g_threadWriteCharacteristicCond = NULL;
176 * Mutex to synchronize the task for MTU Changed.
178 static oc_mutex g_threadMTUChangedMutex = NULL;
181 * Condition for MTU Changed.
183 static oc_cond g_threadMTUChangedCond = NULL;
186 * Flag to check status of write characteristic.
188 static bool g_isSignalSetFlag = false;
191 * Maintains the callback to be notified on receival of network packets from other
194 static CABLEDataReceivedCallback g_LEClientDataReceivedCallback = NULL;
197 * callback to update the error to le adapter
199 static CABLEErrorHandleCallback g_clientErrorCallback;
202 * gmainLoop to manage the threads to receive the callback from the platfrom.
204 static GMainLoop *g_eventLoop = NULL;
207 * Reference to threadpool
209 static ca_thread_pool_t g_LEClientThreadPool = NULL;
211 void CALEGattCharacteristicChangedCb(bt_gatt_h characteristic,
213 int valueLen, void *userData)
215 (void)characteristic;
217 OIC_LOG(DEBUG, TAG, "IN");
218 OIC_LOG_V(DEBUG, TAG, "Changed characteristic value length [%d]", valueLen);
220 oc_mutex_lock(g_LEReqRespClientCbMutex);
221 if (NULL == g_LEClientDataReceivedCallback)
223 OIC_LOG(ERROR, TAG, "Request response callback is not set");
224 oc_mutex_unlock(g_LEReqRespClientCbMutex);
228 uint32_t sentLength = 0;
229 g_LEClientDataReceivedCallback(userData, (uint8_t *)value, valueLen, &sentLength);
231 OIC_LOG_V(DEBUG, TAG, "Recv data Length is %d", sentLength);
233 oc_mutex_unlock(g_LEReqRespClientCbMutex);
235 OIC_LOG(DEBUG, TAG, "OUT");
238 void CALEGattClientMTUChangedCb(bt_gatt_client_h client_handle, const bt_gatt_client_att_mtu_info_s *mtu_info, void *user_data)
240 OIC_LOG(DEBUG, TAG, "IN");
241 oc_mutex_lock(g_threadMTUChangedMutex);
242 OIC_LOG(DEBUG, TAG, "MTU changed signal");
243 oc_cond_signal(g_threadMTUChangedCond);
244 oc_mutex_unlock(g_threadMTUChangedMutex);
245 OIC_LOG(DEBUG, TAG, "OUT");
249 void CALEGattCharacteristicWriteCb(int result, bt_gatt_h reqHandle, void *userData)
254 OIC_LOG(DEBUG, TAG, "IN ");
256 if (BT_ERROR_NONE != result)
258 CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, "", 0, -1,
259 false, "writeChar failure");
261 OIC_LOG(ERROR, TAG, "Write failed Need Retry ");
262 //Need to Implement retry mechanism
266 oc_mutex_lock(g_threadWriteCharacteristicMutex);
267 OIC_LOG(DEBUG, TAG, "g_isSignalSetFlag is set true and signal");
268 g_isSignalSetFlag = true;
269 oc_cond_signal(g_threadWriteCharacteristicCond);
270 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
272 CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, "", 0, -1,
273 true, "writeChar success");
276 OIC_LOG(DEBUG, TAG, "OUT ");
279 CAResult_t CALEGattInitiateConnection(const char *remoteAddress)
281 OIC_LOG(DEBUG, TAG, "IN");
283 oc_mutex_lock(g_isConnectionInProgressMutex);
284 if (g_isConnectionInProgress)
286 oc_mutex_unlock(g_isConnectionInProgressMutex);
287 OIC_LOG(DEBUG, TAG, "Connection already in progress, cannot initiate new connection");
288 return CA_STATUS_FAILED;
290 g_isConnectionInProgress = true;
291 // Set gatt connect retry count
292 g_retrycount = RETRY_COUNT;
293 oc_mutex_unlock(g_isConnectionInProgressMutex);
295 // Pause the scanning
296 CALEGattStopDeviceScanning();
298 OIC_LOG_V(DEBUG, TAG,
299 "Trying to do Gatt connection to [%s]", remoteAddress);
301 oc_mutex_lock(g_LEClientThreadPoolMutex);
302 if (NULL == g_LEClientThreadPool)
304 oc_mutex_unlock(g_LEClientThreadPoolMutex);
305 OIC_LOG(ERROR, TAG, "g_LEClientThreadPool is NULL");
306 return CA_STATUS_FAILED;
309 char *addr = OICStrdup(remoteAddress);
312 oc_mutex_unlock(g_LEClientThreadPoolMutex);
313 OIC_LOG(ERROR, TAG, "OICStrdup failed");
314 return CA_STATUS_FAILED;
317 CAResult_t res = ca_thread_pool_add_task(g_LEClientThreadPool, CAGattConnectThread, addr, NULL);
318 oc_mutex_unlock(g_LEClientThreadPoolMutex);
319 if (CA_STATUS_OK != res)
321 OIC_LOG_V(ERROR, TAG,
322 "ca_thread_pool_add_task failed with ret [%d]", res);
324 return CA_STATUS_FAILED;
326 OIC_LOG(DEBUG, TAG, "OUT");
330 void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress)
332 OIC_LOG(DEBUG, TAG, "IN ");
334 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
338 OIC_LOG_V(DEBUG, TAG, "DisConnected from [%s] ", remoteAddress);
341 oc_mutex_lock(g_LEServerListMutex);
342 CARemoveLEServerInfoFromList(&g_LEServerList, remoteAddress);
343 oc_mutex_unlock(g_LEServerListMutex);
347 oc_mutex_lock(g_isDisconnectedMutex);
348 oc_cond_signal(g_LEDisconnectedCond);
349 oc_mutex_unlock(g_isDisconnectedMutex);
355 OIC_LOG_V(DEBUG, TAG, "Connected to [%s] ", remoteAddress);
357 oc_mutex_lock(g_isConnectionInProgressMutex);
358 g_isConnectionInProgress = false;
359 oc_mutex_unlock(g_isConnectionInProgressMutex);
361 LEServerInfo *serverInfo = NULL;
362 oc_mutex_lock(g_LEServerListMutex);
363 if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
365 oc_mutex_unlock(g_LEServerListMutex);
366 OIC_LOG_V(ERROR, TAG, "Could not get server info for [%s]", remoteAddress);
370 serverInfo->status = LE_STATUS_CONNECTED;
371 oc_mutex_unlock(g_LEServerListMutex);
372 oc_mutex_lock(g_LEClientThreadPoolMutex);
373 if (NULL == g_LEClientThreadPool)
375 oc_mutex_unlock(g_LEClientThreadPoolMutex);
376 OIC_LOG(ERROR, TAG, "g_LEClientThreadPool is NULL");
380 char *addr = OICStrdup(remoteAddress);
383 oc_mutex_unlock(g_LEClientThreadPoolMutex);
384 OIC_LOG(ERROR, TAG, "addr is NULL");
388 CAResult_t ret = ca_thread_pool_add_task(g_LEClientThreadPool, CADiscoverLEServicesThread,
390 oc_mutex_unlock(g_LEClientThreadPoolMutex);
391 if (CA_STATUS_OK != ret)
393 OIC_LOG_V(ERROR, TAG, "ca_thread_pool_add_task failed with ret [%d]", ret);
397 OIC_LOG(DEBUG, TAG, "OUT");
400 static bool CALEIsHaveServiceImpl(bt_adapter_le_device_scan_result_info_s *scanInfo,
401 const char *service_uuid,
402 bt_adapter_le_packet_type_e pkt_type)
409 result = bt_adapter_le_get_scan_result_service_uuids(scanInfo,
410 pkt_type, &uuids, &count);
411 if (result == BT_ERROR_NONE && NULL != uuids)
413 for (int i = 0; i < count; i++)
415 if (0 == strcasecmp(uuids[i], service_uuid))
417 OIC_LOG_V(DEBUG, TAG, "Service[%s] Found in %s",
418 uuids[i], scanInfo->remote_address);
427 char *man_data = NULL;
430 result = bt_adapter_le_get_scan_result_manufacturer_data(scanInfo,
431 pkt_type, &man_id, &man_data, &man_data_len);
433 if (result == BT_ERROR_NONE && NULL != man_data)
436 char *compare_man_data = OICCalloc(1, (man_data_len*2)+1);
437 if (!compare_man_data)
439 OIC_LOG(ERROR, TAG, "Memory allocation failed for compare_man_data");
444 for(int i=0;i<man_data_len;i++){
445 pos += snprintf(compare_man_data+pos, 2, "%.2x", man_data[i]);
448 if (man_id == samsung_code && 0 == strncasecmp(compare_man_data, service_uuid, CUSTOM_UUID_LEN))
450 OIC_LOG_V(DEBUG, TAG, "Manufacture Data[%s] Found in %s",
451 compare_man_data, scanInfo->remote_address);
454 OICFree(compare_man_data);
461 static bool CALEIsHaveService(bt_adapter_le_device_scan_result_info_s *scanInfo,
462 const char *service_uuid)
465 // For arduino servers, scan response will give the UUIDs advertised.
466 CALEIsHaveServiceImpl(scanInfo, service_uuid,
467 BT_ADAPTER_LE_PACKET_SCAN_RESPONSE) ||
468 // For android/tizen servers, advertising packet will give the UUIDs.
469 CALEIsHaveServiceImpl(scanInfo, service_uuid,
470 BT_ADAPTER_LE_PACKET_ADVERTISING);
473 void CALEAdapterScanResultCb(int result, bt_adapter_le_device_scan_result_info_s *scanInfo,
477 OIC_LOG(DEBUG, TAG, "IN");
479 VERIFY_NON_NULL_VOID(scanInfo, TAG, "scanInfo");
480 VERIFY_NON_NULL_VOID(scanInfo->remote_address, TAG, "scanInfo->remote_address");
482 OIC_LOG_V(DEBUG, TAG, "Remote Address [%s]", scanInfo->remote_address);
483 OIC_LOG_V(DEBUG, TAG, "Scan Result [%d]", result);
484 OIC_LOG_V(DEBUG, TAG,
485 " Adv data len [%d] Scan data len[%d]RSSI [%d] Addr_type [%d] ",
486 scanInfo->adv_data_len, scanInfo->scan_data_len, scanInfo->rssi,
487 scanInfo->address_type);
489 // Check if scanning was stopped (since this callback is
490 // being triggered even after stopping the scan)
491 oc_mutex_lock(g_isScanningInProgressMutex);
492 if (!g_isScanningInProgress)
494 oc_mutex_unlock(g_isScanningInProgressMutex);
495 OIC_LOG(DEBUG, TAG, "Scanning not in progress, so ignoring callback");
498 oc_mutex_unlock(g_isScanningInProgressMutex);
500 if (CALEIsHaveService(scanInfo, CA_GATT_SERVICE_UUID) ||
501 CALEIsHaveService(scanInfo, CA_GATT_CUSTOM_UUID)||
502 CALEIsHaveService(scanInfo, CA_GATT_CUSTOM_UUID2))
504 OIC_LOG_V(DEBUG, TAG, "Device [%s] supports OIC or custom service", scanInfo->remote_address);
506 LEServerInfo *serverInfo = NULL;
507 oc_mutex_lock(g_LEServerListMutex);
508 CAResult_t ret = CAGetLEServerInfo(g_LEServerList, scanInfo->remote_address, &serverInfo);
509 if (CA_STATUS_OK != ret)
511 OIC_LOG_V(DEBUG, TAG,
512 "Newly discovered device with address [%s] ", scanInfo->remote_address);
514 char *addr = OICStrdup(scanInfo->remote_address);
517 oc_mutex_unlock(g_LEServerListMutex);
518 OIC_LOG(ERROR, TAG, "Device address is NULL");
521 serverInfo = (LEServerInfo *)OICCalloc(1, sizeof(LEServerInfo));
522 if (NULL == serverInfo)
524 oc_mutex_unlock(g_LEServerListMutex);
525 OIC_LOG(ERROR, TAG, "Calloc failed");
529 serverInfo->remoteAddress = addr;
530 serverInfo->status = LE_STATUS_DISCOVERED;
532 if (CA_STATUS_OK != CAAddLEServerInfoToList(&g_LEServerList, serverInfo))
534 oc_mutex_unlock(g_LEServerListMutex);
535 OIC_LOG_V(ERROR, TAG, "Could not add [%s] to server list", scanInfo->remote_address);
536 CAFreeLEServerInfo(serverInfo);
540 OIC_LOG_V(DEBUG, TAG,
541 "Device Present with address [%s] ", scanInfo->remote_address);
543 if(serverInfo->status == LE_STATUS_UNICAST_PENDING){
544 bt_gatt_client_h clientHandle = NULL;
545 int32_t ret = bt_gatt_client_create(serverInfo->remoteAddress, &clientHandle);
546 if (BT_ERROR_NONE != ret || NULL == clientHandle)
548 OIC_LOG_V(ERROR, TAG,
549 "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret));
550 CALEGattDisConnect(serverInfo->remoteAddress);
551 oc_mutex_unlock(g_LEServerListMutex);
554 serverInfo->clientHandle = clientHandle;
556 serverInfo->status = LE_STATUS_CONNECTION_INITIATED;
557 if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress))
559 OIC_LOG_V(ERROR, TAG, "Could not initiate connection to [%s]", serverInfo->remoteAddress);
560 serverInfo->status = LE_STATUS_DISCOVERED;
561 CADestroyLEDataList(&serverInfo->pendingDataList);
562 oc_mutex_unlock(g_LEServerListMutex);
567 oc_mutex_unlock(g_LEServerListMutex);
570 OIC_LOG(DEBUG, TAG, "OUT");
573 void CASetLEClientThreadPoolHandle(ca_thread_pool_t handle)
575 OIC_LOG(DEBUG, TAG, "IN");
577 oc_mutex_lock(g_LEClientThreadPoolMutex);
578 g_LEClientThreadPool = handle;
579 oc_mutex_unlock(g_LEClientThreadPoolMutex);
581 OIC_LOG(DEBUG, TAG, "OUT");
584 void CASetLEReqRespClientCallback(CABLEDataReceivedCallback callback)
586 OIC_LOG(DEBUG, TAG, "IN");
588 oc_mutex_lock(g_LEReqRespClientCbMutex);
590 g_LEClientDataReceivedCallback = callback;
592 oc_mutex_unlock(g_LEReqRespClientCbMutex);
594 OIC_LOG(DEBUG, TAG, "OUT");
597 void CASetBLEClientErrorHandleCallback(CABLEErrorHandleCallback callback)
599 g_clientErrorCallback = callback;
602 CAResult_t CAStartLEGattClient()
604 OIC_LOG(DEBUG, TAG, "IN");
606 oc_mutex_lock(g_LEClientStateMutex);
607 if (true == g_isLEGattClientStarted)
609 OIC_LOG(ERROR, TAG, "Gatt Client is already running!!");
610 oc_mutex_unlock(g_LEClientStateMutex);
611 return CA_STATUS_FAILED;
614 CAResult_t result = CALEGattSetCallbacks();
615 if (CA_STATUS_OK != result)
617 OIC_LOG(ERROR, TAG, "CABleGattSetCallbacks Failed");
618 oc_mutex_unlock(g_LEClientStateMutex);
619 CATerminateLEGattClient();
620 return CA_STATUS_FAILED;
623 g_isLEGattClientStarted = true;
624 oc_mutex_unlock(g_LEClientStateMutex);
626 oc_mutex_lock(g_LEClientThreadPoolMutex);
627 if (NULL == g_LEClientThreadPool)
629 OIC_LOG(ERROR, TAG, "gBleServerThreadPool is NULL");
630 CATerminateGattClientMutexVariables();
631 oc_mutex_unlock(g_LEClientThreadPoolMutex);
632 return CA_STATUS_FAILED;
635 result = ca_thread_pool_add_task(g_LEClientThreadPool, CAStartTimerThread,
637 if (CA_STATUS_OK != result)
639 OIC_LOG(ERROR, TAG, "ca_thread_pool_add_task failed");
640 CATerminateGattClientMutexVariables();
641 oc_mutex_unlock(g_LEClientThreadPoolMutex);
642 return CA_STATUS_FAILED;
646 result= ca_thread_pool_add_task(g_LEClientThreadPool, CALEClientScanThread,
648 if (CA_STATUS_OK != result)
650 OIC_LOG(ERROR, TAG, "ca_thread_pool_add_task failed");
651 CATerminateGattClientMutexVariables();
652 oc_mutex_unlock(g_LEClientThreadPoolMutex);
653 return CA_STATUS_FAILED;
656 oc_mutex_unlock(g_LEClientThreadPoolMutex);
658 OIC_LOG(DEBUG, TAG, "OUT");
663 void CALEClientScanThread()
665 oc_mutex_lock(g_scanMutex);
666 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
668 CAResult_t result = CALEGattStartDeviceScanning();
669 if (CA_STATUS_OK != result)
671 oc_mutex_unlock(g_scanMutex);
672 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning failed");
675 g_isUnicastScanInProgress = true;
677 oc_cond_signal(g_startTimerCond);
681 g_isUnicastScanInProgress = true;
683 oc_cond_signal(g_scanningTimeCond);
685 oc_mutex_unlock(g_scanMutex);
687 OIC_LOG(DEBUG, TAG, "OUT");
692 void CAStartTimerThread(void *data)
696 OIC_LOG(DEBUG, TAG, "IN");
697 while (g_isLEGattClientStarted)
699 oc_mutex_lock(g_scanMutex);
700 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
702 OIC_LOG(DEBUG, TAG, "waiting....");
703 oc_cond_wait(g_startTimerCond, g_scanMutex);
704 OIC_LOG(DEBUG, TAG, "Wake up");
707 // Timed conditional wait for stopping the scan.
708 OCWaitResult_t ret = oc_cond_wait_for(g_scanningTimeCond, g_scanMutex,
710 if (OC_WAIT_TIMEDOUT == ret)
712 OIC_LOG(DEBUG, TAG, "Scan is timed Out");
714 CALEGattStopDeviceScanning();
716 if (g_isMulticastInProgress)
718 oc_mutex_lock(g_multicastDataListMutex);
719 // Clear the data list and device list.
720 u_arraylist_destroy(g_multicastDataList);
721 g_multicastDataList = NULL;
722 oc_mutex_unlock(g_multicastDataListMutex);
723 g_isMulticastInProgress = false;
725 g_isUnicastScanInProgress = false;
727 oc_mutex_unlock(g_scanMutex);
730 OIC_LOG(DEBUG, TAG, "OUT");
733 void CAStopLEGattClient()
735 OIC_LOG(DEBUG, TAG, "IN");
737 oc_mutex_lock(g_LEClientStateMutex);
739 if (false == g_isLEGattClientStarted)
741 OIC_LOG(ERROR, TAG, "Gatt Client is not running to stop");
742 oc_mutex_unlock(g_LEClientStateMutex);
746 CALEGattUnSetCallbacks();
748 CALEGattStopDeviceScanning();
750 g_isLEGattClientStarted = false;
752 // Signal the conditions waiting in Start timer.
753 oc_cond_signal(g_startTimerCond);
754 oc_cond_signal(g_scanningTimeCond);
756 // Destroy the multicast data list and device list if not empty.
757 if (NULL != g_multicastDataList)
759 oc_mutex_lock(g_multicastDataListMutex);
760 u_arraylist_destroy(g_multicastDataList);
761 g_multicastDataList = NULL;
762 oc_mutex_unlock(g_multicastDataListMutex);
765 oc_mutex_lock(g_LEServerListMutex);
766 CAFreeLEServerList(g_LEServerList);
767 g_LEServerList = NULL;
768 oc_mutex_unlock(g_LEServerListMutex);
770 oc_mutex_lock(g_threadWriteCharacteristicMutex);
771 oc_cond_signal(g_threadWriteCharacteristicCond);
772 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
774 GMainContext *context_event_loop = NULL;
775 // Required for waking up the thread which is running in gmain loop
776 if (NULL != g_eventLoop)
778 context_event_loop = g_main_loop_get_context(g_eventLoop);
780 if (context_event_loop)
782 OIC_LOG_V(DEBUG, TAG, "g_eventLoop context %p", (void *)context_event_loop);
783 g_main_context_wakeup(context_event_loop);
785 // Kill g main loops and kill threads.
786 g_main_loop_quit(g_eventLoop);
790 OIC_LOG(ERROR, TAG, "g_eventLoop context is NULL");
793 oc_mutex_unlock(g_LEClientStateMutex);
795 OIC_LOG(DEBUG, TAG, "OUT");
798 CAResult_t CAInitializeLEGattClient()
800 OIC_LOG(DEBUG, TAG, "Initialize GATT Client");
801 CAResult_t res = CAInitGattClientMutexVariables();
802 if (CA_STATUS_OK != res)
804 OIC_LOG(ERROR, TAG, "CAInitGattClientMutexVariables failed!");
805 CATerminateGattClientMutexVariables();
806 return CA_STATUS_FAILED;
811 void CATerminateLEGattClient()
813 OIC_LOG(DEBUG, TAG, "IN");
815 CATerminateGattClientMutexVariables();
817 OIC_LOG(DEBUG, TAG, "OUT");
820 CAResult_t CAInitGattClientMutexVariables()
822 OIC_LOG(DEBUG, TAG, "IN");
823 if (NULL == g_LEClientStateMutex)
825 g_LEClientStateMutex = oc_mutex_new();
826 if (NULL == g_LEClientStateMutex)
828 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
829 return CA_STATUS_FAILED;
833 if (NULL == g_LEServerListMutex)
835 g_LEServerListMutex = oc_mutex_new();
836 if (NULL == g_LEServerListMutex)
838 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
839 return CA_STATUS_FAILED;
843 if (NULL == g_LEReqRespClientCbMutex)
845 g_LEReqRespClientCbMutex = oc_mutex_new();
846 if (NULL == g_LEReqRespClientCbMutex)
848 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
849 return CA_STATUS_FAILED;
853 if (NULL == g_LEClientThreadPoolMutex)
855 g_LEClientThreadPoolMutex = oc_mutex_new();
856 if (NULL == g_LEClientThreadPoolMutex)
858 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
859 return CA_STATUS_FAILED;
863 if (NULL == g_LEClientConnectMutex)
865 g_LEClientConnectMutex = oc_mutex_new();
866 if (NULL == g_LEClientConnectMutex)
868 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
869 return CA_STATUS_FAILED;
873 if (NULL == g_isScanningInProgressMutex)
875 g_isScanningInProgressMutex = oc_mutex_new();
876 if (NULL == g_isScanningInProgressMutex)
878 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
879 return CA_STATUS_FAILED;
883 if (NULL == g_isConnectionInProgressMutex)
885 g_isConnectionInProgressMutex = oc_mutex_new();
886 if (NULL == g_isConnectionInProgressMutex)
888 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
889 return CA_STATUS_FAILED;
893 if (NULL == g_multicastDataListMutex)
895 g_multicastDataListMutex = oc_mutex_new();
896 if (NULL == g_multicastDataListMutex)
898 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
899 return CA_STATUS_FAILED;
903 if (NULL == g_scanMutex)
905 g_scanMutex = oc_mutex_new();
906 if (NULL == g_scanMutex)
908 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
909 return CA_STATUS_FAILED;
913 if (NULL == g_threadWriteCharacteristicMutex)
915 g_threadWriteCharacteristicMutex = oc_mutex_new();
916 if (NULL == g_threadWriteCharacteristicMutex)
918 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
919 return CA_STATUS_FAILED;
923 if (NULL == g_threadMTUChangedMutex)
925 g_threadMTUChangedMutex = oc_mutex_new();
926 if (NULL == g_threadMTUChangedMutex)
928 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
929 return CA_STATUS_FAILED;
933 if (NULL == g_isDisconnectedMutex)
935 g_isDisconnectedMutex = oc_mutex_new();
936 if (NULL == g_isDisconnectedMutex)
938 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
939 return CA_STATUS_FAILED;
943 if (NULL == g_LEDisconnectedCond)
945 g_LEDisconnectedCond = oc_cond_new();
946 if (NULL == g_LEDisconnectedCond)
948 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
949 return CA_STATUS_FAILED;
953 if (NULL == g_startTimerCond)
955 g_startTimerCond = oc_cond_new();
956 if (NULL == g_startTimerCond)
958 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
959 return CA_STATUS_FAILED;
963 if (NULL == g_scanningTimeCond)
965 g_scanningTimeCond = oc_cond_new();
966 if (NULL == g_scanningTimeCond)
968 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
969 return CA_STATUS_FAILED;
973 if (NULL == g_threadWriteCharacteristicCond)
975 g_threadWriteCharacteristicCond = oc_cond_new();
976 if (NULL == g_threadWriteCharacteristicCond)
978 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
979 return CA_STATUS_FAILED;
983 if (NULL == g_threadMTUChangedCond)
985 g_threadMTUChangedCond = oc_cond_new();
986 if (NULL == g_threadMTUChangedCond)
988 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
989 return CA_STATUS_FAILED;
994 OIC_LOG(DEBUG, TAG, "OUT");
998 void CATerminateGattClientMutexVariables()
1000 OIC_LOG(DEBUG, TAG, "IN");
1002 oc_mutex_free(g_LEClientStateMutex);
1003 g_LEClientStateMutex = NULL;
1005 oc_mutex_free(g_LEServerListMutex);
1006 g_LEServerListMutex = NULL;
1008 oc_mutex_free(g_LEReqRespClientCbMutex);
1009 g_LEReqRespClientCbMutex = NULL;
1011 oc_mutex_free(g_LEClientConnectMutex);
1012 g_LEClientConnectMutex = NULL;
1014 oc_mutex_free(g_LEClientThreadPoolMutex);
1015 g_LEClientThreadPoolMutex = NULL;
1017 oc_mutex_free(g_isScanningInProgressMutex);
1018 g_isScanningInProgressMutex = NULL;
1020 oc_mutex_free(g_isConnectionInProgressMutex);
1021 g_isConnectionInProgressMutex = NULL;
1023 oc_mutex_free(g_multicastDataListMutex);
1024 g_multicastDataListMutex = NULL;
1026 oc_mutex_free(g_scanMutex);
1029 oc_mutex_free(g_threadWriteCharacteristicMutex);
1030 g_threadWriteCharacteristicMutex = NULL;
1032 oc_mutex_free(g_threadMTUChangedMutex);
1033 g_threadMTUChangedMutex = NULL;
1035 oc_mutex_free(g_isDisconnectedMutex);
1036 g_isDisconnectedMutex = NULL;
1038 oc_cond_free(g_LEDisconnectedCond);
1039 g_LEDisconnectedCond = NULL;
1041 oc_cond_free(g_startTimerCond);
1042 g_startTimerCond = NULL;
1044 oc_cond_free(g_scanningTimeCond);
1045 g_scanningTimeCond = NULL;
1047 oc_cond_free(g_threadWriteCharacteristicCond);
1048 g_threadWriteCharacteristicCond = NULL;
1049 g_isSignalSetFlag = false;
1051 oc_cond_free(g_threadMTUChangedCond);
1052 g_threadMTUChangedCond = NULL;
1054 OIC_LOG(DEBUG, TAG, "OUT");
1057 CAResult_t CALEGattSetCallbacks()
1059 OIC_LOG(DEBUG, TAG, "IN");
1061 OIC_LOG(DEBUG, TAG, "OUT");
1062 return CA_STATUS_OK;
1065 void CALEGattUnSetCallbacks()
1067 OIC_LOG(DEBUG, TAG, "IN");
1069 bt_gatt_unset_connection_state_changed_cb();
1071 oc_mutex_lock(g_LEServerListMutex);
1072 LEServerInfoList *curNode = g_LEServerList;
1075 LEServerInfo *serverInfo = curNode->serverInfo;
1076 if (serverInfo->status >= LE_STATUS_SERVICES_DISCOVERED)
1078 bt_gatt_client_unset_characteristic_value_changed_cb(serverInfo->readChar);
1080 curNode = curNode->next;
1082 oc_mutex_unlock(g_LEServerListMutex);
1084 OIC_LOG(DEBUG, TAG, "OUT");
1087 CAResult_t CALEGattStartDeviceScanning()
1089 OIC_LOG(DEBUG, TAG, "IN");
1091 oc_mutex_lock(g_isScanningInProgressMutex);
1092 if (!g_isScanningInProgress)
1094 int ret = bt_adapter_le_start_scan(CALEAdapterScanResultCb, NULL);
1095 if (BT_ERROR_NONE != ret)
1097 oc_mutex_unlock(g_isScanningInProgressMutex);
1098 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_start_scan failed[%s]",
1099 CALEGetErrorMsg(ret));
1100 return CA_STATUS_FAILED;
1102 g_isScanningInProgress = true;
1106 OIC_LOG(DEBUG, TAG, "Ignore, scanning already in progress");
1108 oc_mutex_unlock(g_isScanningInProgressMutex);
1110 OIC_LOG(DEBUG, TAG, "OUT");
1111 return CA_STATUS_OK;
1114 void CALEGattStopDeviceScanning()
1116 OIC_LOG(DEBUG, TAG, "IN");
1118 oc_mutex_lock(g_isScanningInProgressMutex);
1119 if (g_isScanningInProgress)
1121 int ret = bt_adapter_le_stop_scan();
1122 if (BT_ERROR_NONE != ret)
1124 oc_mutex_unlock(g_isScanningInProgressMutex);
1125 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_stop_scan failed[%s]",
1126 CALEGetErrorMsg(ret));
1129 g_isScanningInProgress = false;
1130 g_isUnicastScanInProgress= false;
1134 OIC_LOG(DEBUG, TAG, "Ignore, scanning not in progress");
1136 oc_mutex_unlock(g_isScanningInProgressMutex);
1138 OIC_LOG(DEBUG, TAG, "OUT");
1141 void CAGattConnectThread (void *remoteAddress)
1143 OIC_LOG(DEBUG, TAG, "IN ");
1145 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
1147 char *address = (char *)remoteAddress;
1149 OIC_LOG_V(DEBUG, TAG, "remote address is [%s]", address);
1151 CAResult_t result = CALEGattConnect(address);
1153 if (CA_STATUS_OK != result)
1155 OIC_LOG_V(ERROR, TAG, "bt_gatt_connect failed for [%s]", address);
1160 OIC_LOG(DEBUG, TAG, "OUT");
1163 CAResult_t CALEGattConnect(const char *remoteAddress)
1165 OIC_LOG(DEBUG, TAG, "IN");
1167 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1168 "remote address is NULL", CA_STATUS_FAILED);
1170 oc_mutex_lock(g_LEClientConnectMutex);
1171 CAResult_t result = CA_STATUS_OK;
1174 int ret = bt_gatt_connect(remoteAddress, true);
1176 int ret = bt_gatt_connect(remoteAddress, false);
1178 if (BT_ERROR_NONE != ret)
1180 OIC_LOG_V(ERROR, TAG, "bt_gatt_connect Failed with ret value [%s] ",
1181 CALEGetErrorMsg(ret));
1182 oc_mutex_unlock(g_LEClientConnectMutex);
1183 return CA_STATUS_FAILED;
1186 oc_mutex_unlock(g_LEClientConnectMutex);
1188 OIC_LOG(DEBUG, TAG, "OUT");
1192 CAResult_t CALEGattDisConnect(const char *remoteAddress)
1194 OIC_LOG(DEBUG, TAG, "IN");
1196 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1197 "remote address is NULL", CA_STATUS_FAILED);
1199 int ret = bt_gatt_disconnect(remoteAddress);
1201 if (BT_ERROR_NONE != ret)
1203 OIC_LOG_V(ERROR, TAG, "bt_gatt_disconnect Failed with ret value [%s] ",
1204 CALEGetErrorMsg(ret));
1205 return CA_STATUS_FAILED;
1208 OIC_LOG(DEBUG, TAG, "OUT");
1209 return CA_STATUS_OK;
1212 CAResult_t CAUpdateCharacteristicsToGattServerImpl(LEServerInfo *serverInfo,
1213 const uint8_t *data, const uint32_t dataLen)
1215 OIC_LOG(DEBUG, TAG, "IN");
1217 VERIFY_NON_NULL(serverInfo, TAG, "Server Info is NULL");
1219 CALEGattStopDeviceScanning();
1221 OIC_LOG_V(DEBUG, TAG, "Updating the data of length [%d] to [%s] ", dataLen,
1222 serverInfo->remoteAddress);
1224 int result = bt_gatt_set_value(serverInfo->writeChar, (char *)data, dataLen);
1226 if (BT_ERROR_NONE != result)
1228 OIC_LOG_V(ERROR, TAG,
1229 "bt_gatt_set_value Failed with return val [%s]",
1230 CALEGetErrorMsg(result));
1234 result = bt_gatt_client_write_value(serverInfo->writeChar, CALEGattCharacteristicWriteCb,
1236 if (BT_ERROR_NONE != result)
1238 OIC_LOG_V(ERROR, TAG,
1239 "bt_gatt_client_write_value Failed with return val [%s]",
1240 CALEGetErrorMsg(result));
1244 // wait for callback for write Characteristic with success to sent data
1245 OIC_LOG_V(DEBUG, TAG, "callback flag is %d", g_isSignalSetFlag);
1246 oc_mutex_lock(g_threadWriteCharacteristicMutex);
1247 if (!g_isSignalSetFlag)
1249 OIC_LOG(DEBUG, TAG, "wait for callback to notify writeCharacteristic is success");
1250 if (OC_WAIT_SUCCESS != oc_cond_wait_for(g_threadWriteCharacteristicCond,
1251 g_threadWriteCharacteristicMutex,
1252 WAIT_TIME_WRITE_CHARACTERISTIC))
1254 g_isSignalSetFlag = false;
1255 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
1256 OIC_LOG(ERROR, TAG, "there is no response. write has failed");
1260 // reset flag set by writeCharacteristic Callback
1261 g_isSignalSetFlag = false;
1262 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
1264 oc_mutex_lock(g_scanMutex);
1265 if (g_isMulticastInProgress || g_isUnicastScanInProgress)
1267 if (CA_STATUS_OK != CALEGattStartDeviceScanning())
1269 OIC_LOG(ERROR, TAG, "Could not start device scanning");
1272 oc_mutex_unlock(g_scanMutex);
1273 OIC_LOG(DEBUG, TAG, "OUT");
1274 return CA_STATUS_OK;
1277 oc_mutex_lock(g_scanMutex);
1278 if (g_isMulticastInProgress || g_isUnicastScanInProgress)
1280 if (CA_STATUS_OK != CALEGattStartDeviceScanning())
1282 OIC_LOG(ERROR, TAG, "Could not start device scanning");
1285 oc_mutex_unlock(g_scanMutex);
1287 OIC_LOG(DEBUG, TAG, "OUT");
1288 return CA_STATUS_FAILED;
1291 void CADiscoverLEServicesThread(void *remoteAddress)
1293 OIC_LOG(DEBUG, TAG, "IN");
1295 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
1297 char *address = (char *)remoteAddress;
1299 CAResult_t result = CALEGattDiscoverServices(address);
1300 if (CA_STATUS_OK != result)
1302 OIC_LOG(ERROR, TAG, "CALEGattDiscoverServices failed");
1306 OIC_LOG(DEBUG, TAG, "OUT");
1309 static int CALEWaittillDisconnect(oc_mutex mutex, oc_cond cv, int wait_seconds)
1311 OIC_LOG(DEBUG, TAG, "Waiting for server to be disconnected...");
1312 oc_mutex_lock(mutex);
1313 uint64_t wait_time = wait_seconds * MICROSECS_PER_SEC;
1314 int ret = oc_cond_wait_for(cv, mutex, wait_time);
1315 oc_mutex_unlock(mutex);
1319 static CAResult_t CALEGattConnectionRetry(const char *remoteAddress)
1321 OIC_LOG(DEBUG, TAG, "IN");
1323 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL")
1325 CALEGattDisConnect(remoteAddress);
1326 CALEWaittillDisconnect(g_isDisconnectedMutex, g_LEDisconnectedCond, 10);
1327 CAResult_t result = CALEGattConnect(remoteAddress);
1328 OIC_LOG(DEBUG, TAG, "OUT");
1332 CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
1334 OIC_LOG(DEBUG, TAG, "IN");
1336 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1337 "remote address is NULL", CA_STATUS_FAILED);
1339 LEServerInfo *serverInfo = NULL;
1340 unsigned int mtu_size;
1341 oc_mutex_lock(g_LEServerListMutex);
1342 if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
1344 oc_mutex_unlock(g_LEServerListMutex);
1345 OIC_LOG_V(ERROR, TAG, "Could not get server info for [%s]", remoteAddress);
1346 CALEGattDisConnect(remoteAddress);
1347 return CA_STATUS_FAILED;
1350 bt_gatt_h serviceHandle = NULL;
1351 int32_t ret = bt_gatt_client_get_service(serverInfo->clientHandle, CA_GATT_SERVICE_UUID, &serviceHandle);
1352 if (BT_ERROR_NONE != ret || NULL == serviceHandle)
1354 OIC_LOG_V(ERROR, TAG,
1355 "bt_gatt_client_get_service Failed with ret value [%s] ", CALEGetErrorMsg(ret));
1359 OIC_LOG(DEBUG, TAG, "Retry will be attempted to connect Gatt Server");
1362 OIC_LOG_V(DEBUG, TAG, "Retry count left %d time(s)", g_retrycount);
1363 CAResult_t result = CALEGattConnectionRetry(remoteAddress);
1364 if(result == CA_STATUS_FAILED)
1370 oc_mutex_unlock(g_LEServerListMutex);
1371 OIC_LOG(ERROR, TAG, "Previous connection attempt failed, attempting to retry again");
1372 OIC_LOG_V(DEBUG, TAG, "Retry count left %d time(s)", g_retrycount);
1373 return CA_STATUS_FAILED;
1379 // Server will read data on this characteristic.
1380 bt_gatt_h writeChrHandle = NULL;
1381 ret = bt_gatt_service_get_characteristic(serviceHandle, CA_GATT_REQUEST_CHRC_UUID,
1383 if (BT_ERROR_NONE != ret || NULL == writeChrHandle)
1385 OIC_LOG_V(ERROR, TAG,
1386 "bt_gatt_service_get_characteristic Failed with ret value [%s] ",
1387 CALEGetErrorMsg(ret));
1391 // Server will notify data on this characteristic.
1392 bt_gatt_h readChrHandle = NULL;
1393 ret = bt_gatt_service_get_characteristic(serviceHandle, CA_GATT_RESPONSE_CHRC_UUID,
1395 if (BT_ERROR_NONE != ret || NULL == readChrHandle)
1397 OIC_LOG_V(ERROR, TAG,
1398 "bt_gatt_service_get_characteristic Failed with ret value [%s] ",
1399 CALEGetErrorMsg(ret));
1404 //TODO: This data has to be freed while unsetting the callback.
1405 char *addr = OICStrdup(remoteAddress);
1408 OIC_LOG(ERROR, TAG, "addr is NULL");
1412 ret = bt_gatt_client_set_characteristic_value_changed_cb(readChrHandle,
1413 CALEGattCharacteristicChangedCb,
1415 if (BT_ERROR_NONE != ret)
1417 OIC_LOG_V(ERROR, TAG,
1418 "bt_gatt_client_set_characteristic_value_changed_cb Failed with ret value [%s]",
1419 CALEGetErrorMsg(ret));
1423 ret = bt_gatt_client_set_att_mtu_changed_cb(serverInfo->clientHandle,
1424 CALEGattClientMTUChangedCb,
1425 (void *)serverInfo->remoteAddress);
1426 if (BT_ERROR_NONE != ret)
1428 OIC_LOG_V(ERROR, TAG,
1429 "bt_gatt_client_set_att_mtu_changed_cb Failed with ret value [%s]",
1430 CALEGetErrorMsg(ret));
1434 oc_mutex_lock(g_threadMTUChangedMutex);
1435 ret = bt_gatt_client_request_att_mtu_change(serverInfo->clientHandle, CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_MTU_HEADER_SIZE);
1436 if (BT_ERROR_NONE != ret)
1438 OIC_LOG_V(ERROR, TAG,
1439 "bt_gatt_client_request_att_mtu_change Failed with ret value [%s]",
1440 CALEGetErrorMsg(ret));
1441 oc_mutex_unlock(g_threadMTUChangedMutex);
1445 OIC_LOG(DEBUG, TAG, "wait for callback to notify MTU Changed Callback");
1446 oc_cond_wait(g_threadMTUChangedCond, g_threadMTUChangedMutex);
1447 oc_mutex_unlock(g_threadMTUChangedMutex);
1449 OIC_LOG(DEBUG, TAG, "Done MTU");
1450 ret = bt_gatt_client_get_att_mtu(serverInfo->clientHandle, &mtu_size);
1451 if (BT_ERROR_NONE != ret)
1453 OIC_LOG_V(ERROR, TAG,
1454 "bt_gatt_client_get_att_mtu Failed with ret value [%s]",
1455 CALEGetErrorMsg(ret));
1458 OIC_LOG_V(DEBUG, TAG,"Negotiated MTU Size %d",mtu_size);
1460 serverInfo->serviceHandle = serviceHandle;
1461 serverInfo->readChar = readChrHandle;
1462 serverInfo->writeChar = writeChrHandle;
1463 serverInfo->status = LE_STATUS_SERVICES_DISCOVERED;
1464 serverInfo->mtu_size = mtu_size;
1466 while (serverInfo->pendingDataList)
1468 LEData *leData = serverInfo->pendingDataList->data;
1469 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(
1470 serverInfo, leData->data, leData->dataLength))
1472 OIC_LOG_V(ERROR, TAG, "Failed to send pending data to [%s]",
1473 serverInfo->remoteAddress);
1475 CADestroyLEDataList(&serverInfo->pendingDataList);
1478 CARemoveLEDataFromList(&serverInfo->pendingDataList);
1480 oc_mutex_unlock(g_LEServerListMutex);
1482 OIC_LOG(DEBUG, TAG, "OUT");
1483 return CA_STATUS_OK;
1486 bt_gatt_client_destroy(serverInfo->clientHandle);
1487 serverInfo->clientHandle = NULL;
1488 oc_mutex_unlock(g_LEServerListMutex);
1489 CALEGattDisConnect(remoteAddress);
1490 return CA_STATUS_FAILED;
1493 CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
1494 const uint8_t *data, const uint32_t dataLen,
1495 CALETransferType_t type, const int32_t position)
1497 OIC_LOG(DEBUG, TAG, "IN");
1499 VERIFY_NON_NULL(data, TAG, "data is NULL");
1503 OIC_LOG(ERROR, TAG, "dataLen is less than or equal zero. Invalid input!");
1504 return CA_STATUS_INVALID_PARAM;
1507 LEServerInfo *serverInfo = NULL;
1508 bool isConnected = false;
1509 oc_mutex_lock(g_LEServerListMutex);
1510 if (LE_UNICAST == type)
1512 if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
1514 OIC_LOG_V(DEBUG, TAG,
1515 "Device with address [%s] not yet found, initiating scan",
1518 char *addr = OICStrdup(remoteAddress);
1521 oc_mutex_unlock(g_LEServerListMutex);
1522 OIC_LOG(ERROR, TAG, "Device address is NULL");
1523 return CA_STATUS_FAILED;
1525 serverInfo = (LEServerInfo *)OICCalloc(1, sizeof(LEServerInfo));
1526 if (NULL == serverInfo)
1528 oc_mutex_unlock(g_LEServerListMutex);
1529 OIC_LOG(ERROR, TAG, "Calloc failed");
1531 return CA_STATUS_FAILED;
1533 serverInfo->remoteAddress = addr;
1534 serverInfo->status = LE_STATUS_UNICAST_PENDING;
1536 if (CA_STATUS_OK != CAAddLEServerInfoToList(&g_LEServerList, serverInfo))
1538 oc_mutex_unlock(g_LEServerListMutex);
1539 OIC_LOG_V(ERROR, TAG, "Could not add [%s] to server list", remoteAddress);
1540 CAFreeLEServerInfo(serverInfo);
1541 return CA_STATUS_FAILED;
1544 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1546 oc_mutex_unlock(g_LEServerListMutex);
1547 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1548 return CA_STATUS_FAILED;
1552 if(serverInfo->status == LE_STATUS_UNICAST_PENDING)
1555 int ret = bt_device_is_profile_connected(remoteAddress, BT_PROFILE_GATT, &isConnected);
1556 if (BT_ERROR_NONE != ret)
1558 OIC_LOG_V(ERROR, TAG, "bt_device_is_profile_connected Failed with ret value [%s] ",
1559 CALEGetErrorMsg(ret));
1560 oc_mutex_unlock(g_LEServerListMutex);
1561 return CA_STATUS_FAILED;
1565 OIC_LOG_V(DEBUG, TAG, "Already connected to address [%s]", remoteAddress);
1566 serverInfo->status = LE_STATUS_CONNECTED;
1568 oc_mutex_lock(g_scanMutex);
1569 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
1571 CAResult_t result = CALEGattStartDeviceScanning();
1572 if (CA_STATUS_OK != result)
1574 oc_mutex_unlock(g_scanMutex);
1575 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning failed");
1576 oc_mutex_unlock(g_LEServerListMutex);
1577 return CA_STATUS_FAILED;
1579 g_isUnicastScanInProgress = true;
1581 oc_cond_signal(g_startTimerCond);
1585 g_isUnicastScanInProgress = true;
1587 oc_cond_signal(g_scanningTimeCond);
1589 oc_mutex_unlock(g_scanMutex);
1594 if (serverInfo->status == LE_STATUS_CONNECTED)
1596 oc_mutex_lock(g_LEClientThreadPoolMutex);
1597 if (NULL == g_LEClientThreadPool)
1599 oc_mutex_unlock(g_LEClientThreadPoolMutex);
1600 OIC_LOG(ERROR, TAG, "g_LEClientThreadPool is NULL");
1601 oc_mutex_unlock(g_LEServerListMutex);
1602 return CA_STATUS_FAILED;
1604 char *addr = OICStrdup(remoteAddress);
1607 oc_mutex_unlock(g_LEClientThreadPoolMutex);
1608 OIC_LOG(ERROR, TAG, "addr is NULL");
1609 oc_mutex_unlock(g_LEServerListMutex);
1610 return CA_STATUS_FAILED;
1612 CAResult_t ret = ca_thread_pool_add_task(g_LEClientThreadPool, CADiscoverLEServicesThread,
1614 oc_mutex_unlock(g_LEClientThreadPoolMutex);
1615 if (CA_STATUS_OK != ret)
1617 OIC_LOG_V(ERROR, TAG, "ca_thread_pool_add_task failed with ret [%d]", ret);
1622 if (serverInfo->status == LE_STATUS_DISCOVERED)
1624 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1626 oc_mutex_unlock(g_LEServerListMutex);
1627 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1628 return CA_STATUS_FAILED;
1631 bt_gatt_client_h clientHandle = NULL;
1632 int32_t ret = bt_gatt_client_create(serverInfo->remoteAddress, &clientHandle);
1633 if (BT_ERROR_NONE != ret || NULL == clientHandle)
1635 OIC_LOG_V(ERROR, TAG,
1636 "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret));
1637 CALEGattDisConnect(serverInfo->remoteAddress);
1638 oc_mutex_unlock(g_LEServerListMutex);
1639 return CA_STATUS_FAILED;
1641 serverInfo->clientHandle = clientHandle;
1643 serverInfo->status = LE_STATUS_CONNECTION_INITIATED;
1644 if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress))
1646 OIC_LOG_V(ERROR, TAG, "Could not initiate connection to [%s]", serverInfo->remoteAddress);
1647 serverInfo->status = LE_STATUS_DISCOVERED;
1648 CADestroyLEDataList(&serverInfo->pendingDataList);
1649 oc_mutex_unlock(g_LEServerListMutex);
1650 return CA_STATUS_FAILED;
1653 else if (serverInfo->status < LE_STATUS_SERVICES_DISCOVERED)
1655 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1657 oc_mutex_unlock(g_LEServerListMutex);
1658 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1659 return CA_STATUS_FAILED;
1664 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(serverInfo, data, dataLen))
1666 OIC_LOG_V(ERROR, TAG, "Could not update characteristic to gatt server [%s]",
1667 serverInfo->remoteAddress);
1668 oc_mutex_unlock(g_LEServerListMutex);
1669 return CA_STATUS_FAILED;
1673 else if (LE_MULTICAST == type)
1675 OIC_LOG(ERROR, TAG, "LE_MULTICAST type Not used");
1677 oc_mutex_unlock(g_LEServerListMutex);
1678 OIC_LOG(DEBUG, TAG, "OUT");
1679 return CA_STATUS_OK;
1682 CAResult_t CAUpdateCharacteristicsToAllGattServers(const uint8_t *data, uint32_t dataLen)
1684 OIC_LOG(DEBUG, TAG, "IN");
1686 VERIFY_NON_NULL(data, TAG, "data is NULL");
1690 OIC_LOG(ERROR, TAG, "dataLen is less than or equal zero. Invalid input !");
1691 return CA_STATUS_INVALID_PARAM;
1694 oc_mutex_lock(g_LEServerListMutex);
1695 LEServerInfoList *curNode = g_LEServerList;
1698 LEServerInfo *serverInfo = curNode->serverInfo;
1699 if (serverInfo->status == LE_STATUS_SERVICES_DISCOVERED)
1701 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(serverInfo, data, dataLen))
1703 OIC_LOG_V(ERROR, TAG, "Failed to update characteristics to gatt server [%s]",
1704 serverInfo->remoteAddress);
1707 else if (serverInfo->status != LE_STATUS_INVALID)
1709 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1711 OIC_LOG(ERROR, TAG, "Failed to add to pending list");
1714 curNode = curNode->next;
1716 oc_mutex_unlock(g_LEServerListMutex);
1718 // Add the data to pending list.
1719 LEData *multicastData = (LEData *)OICCalloc(1, sizeof(LEData));
1720 if (NULL == multicastData)
1722 OIC_LOG(ERROR, TAG, "Calloc failed");
1725 multicastData->data = OICCalloc(1, dataLen);
1726 if (NULL == multicastData->data)
1728 OIC_LOG(ERROR, TAG, "Calloc failed");
1731 memcpy(multicastData->data, data, dataLen);
1732 multicastData->dataLength = dataLen;
1734 oc_mutex_lock(g_multicastDataListMutex);
1735 if (NULL == g_multicastDataList)
1737 g_multicastDataList = u_arraylist_create();
1739 u_arraylist_add(g_multicastDataList, (void *)multicastData);
1740 oc_mutex_unlock(g_multicastDataListMutex);
1742 // Start the scanning, if not started, else reset timer
1743 oc_mutex_lock(g_scanMutex);
1744 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
1746 CAResult_t result = CALEGattStartDeviceScanning();
1747 if (CA_STATUS_OK != result)
1749 oc_mutex_unlock(g_scanMutex);
1750 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning Failed");
1753 g_isMulticastInProgress = true;
1754 // Start the timer by signalling it
1755 oc_cond_signal(g_startTimerCond);
1759 g_isMulticastInProgress = true;
1761 oc_cond_signal(g_scanningTimeCond);
1763 oc_mutex_unlock(g_scanMutex);
1766 OIC_LOG(DEBUG, TAG, "OUT ");
1767 return CA_STATUS_OK;
1770 uint16_t CALEClientGetMtuSize(const char* remote_address)
1772 LEServerInfo *serverInfo = NULL;
1773 oc_mutex_lock(g_LEServerListMutex);
1774 if (CA_STATUS_OK == CAGetLEServerInfo(g_LEServerList, remote_address, &serverInfo))
1776 if (serverInfo->status == LE_STATUS_SERVICES_DISCOVERED){
1777 OIC_LOG_V(DEBUG, TAG, "Mtu Size [%d]", serverInfo->mtu_size);
1778 oc_mutex_unlock(g_LEServerListMutex);
1779 OIC_LOG_V(INFO, TAG, "Returning Mtu Size [%d]", serverInfo->mtu_size - CA_BLE_MTU_HEADER_SIZE);
1780 return serverInfo->mtu_size - CA_BLE_MTU_HEADER_SIZE;
1783 oc_mutex_unlock(g_LEServerListMutex);
1784 return CA_DEFAULT_BLE_MTU_SIZE;
1788 bool CALEClientIsConnected(const char* address)
1795 CAResult_t CALEClientSetMtuSize(const char* address, uint16_t mtuSize)
1799 VERIFY_NON_NULL(address, TAG, "address is null");
1801 //it should be implemented after update Tizen 3.0
1802 return CA_NOT_SUPPORTED;
1805 CAResult_t CALEClientSendNegotiationMessage(const char* address)
1807 OIC_LOG_V(DEBUG, TAG, "CALEClientSendNegotiationMessage(%s)", address);
1809 //it will be implemented when tizen public 3.0 is released.
1810 return CA_NOT_SUPPORTED;