1 /* ****************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #include "caleclient.h"
26 #include <arpa/inet.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
34 #include "uarraylist.h"
35 #include "caqueueingthread.h"
36 #include "caadapterutils.h"
37 #include "cagattservice.h"
38 #include "oic_string.h"
39 #include "oic_malloc.h"
42 * Logging tag for module name.
44 #define TAG "OIC_CA_LE_CLIENT"
46 #define MICROSECS_PER_SEC 1000000
47 #define WAIT_TIME_WRITE_CHARACTERISTIC 10 * MICROSECS_PER_SEC
49 //For custom uuid ble server
50 #define CA_GATT_CUSTOM_UUID "4209"
51 #define CA_GATT_CUSTOM_UUID2 "4204"
52 #define CUSTOM_UUID_LEN 4
54 static const int samsung_code = 117;
56 uint64_t const TIMEOUT = 30 * MICROSECS_PER_SEC;
59 * Flag to check if scanning is in progress
61 static bool g_isScanningInProgress = false;
64 * Mutex to synchronize access to g_isScanningInProgress
66 static oc_mutex g_isScanningInProgressMutex = NULL;
69 * Flag to check if connection is in progress
71 static bool g_isConnectionInProgress = false;
74 * Mutex to synchronize access to g_isConnectionInProgress
76 static oc_mutex g_isConnectionInProgressMutex = NULL;
79 * Flag to check if multicast is already in progress.
81 static bool g_isMulticastInProgress = false;
84 * Flag to check if unicast scan is in progress
86 static bool g_isUnicastScanInProgress = false;
89 * Mutex to synchronize access to g_isMulticastInProgress
90 * and g_isUnicastScanInProgress
92 static oc_mutex g_scanMutex = NULL;
95 * Pending multicast data list to be sent.
97 static u_arraylist_t *g_multicastDataList = NULL;
100 * Mutex to synchronize the access to Pending multicast data list.
102 static oc_mutex g_multicastDataListMutex = NULL;
105 * Condition to start the timer for scanning.
107 static oc_cond g_startTimerCond = NULL;
110 * Condition for scanning Time interval.
112 static oc_cond g_scanningTimeCond = NULL;
115 * This contains the list of OIC services a client connect tot.
117 static LEServerInfoList *g_LEServerList = NULL;
120 * Mutex to synchronize access to BleServiceList.
122 static oc_mutex g_LEServerListMutex = NULL;
125 * Boolean variable to keep the state of the GATT Client.
127 static bool g_isLEGattClientStarted = false;
130 * Mutex to synchronize access to the requestResponse callback to be called
131 * when the data needs to be sent from GATTClient.
133 static oc_mutex g_LEReqRespClientCbMutex = NULL;
136 * Mutex to synchronize access to the requestResponse callback to be called
137 * when the data needs to be sent from GATTClient.
139 static oc_mutex g_LEClientConnectMutex = NULL;
142 * Mutex to synchronize the calls to be done to the platform from GATTClient
143 * interfaces from different threads.
145 static oc_mutex g_LEClientStateMutex = NULL;
148 * Mutex to synchronize the task to be pushed to thread pool.
150 static oc_mutex g_LEClientThreadPoolMutex = NULL;
153 * Mutex to synchronize the task to write characteristic one packet after another.
155 static oc_mutex g_threadWriteCharacteristicMutex = NULL;
158 * Condition for Writing characteristic.
160 static oc_cond g_threadWriteCharacteristicCond = NULL;
163 * Mutex to synchronize the task for MTU Changed.
165 static oc_mutex g_threadMTUChangedMutex = NULL;
168 * Condition for MTU Changed.
170 static oc_cond g_threadMTUChangedCond = NULL;
173 * Flag to check status of write characteristic.
175 static bool g_isSignalSetFlag = false;
178 * Maintains the callback to be notified on receival of network packets from other
181 static CABLEDataReceivedCallback g_LEClientDataReceivedCallback = NULL;
184 * callback to update the error to le adapter
186 static CABLEErrorHandleCallback g_clientErrorCallback;
189 * gmainLoop to manage the threads to receive the callback from the platfrom.
191 static GMainLoop *g_eventLoop = NULL;
194 * Reference to threadpool
196 static ca_thread_pool_t g_LEClientThreadPool = NULL;
198 void CALEGattCharacteristicChangedCb(bt_gatt_h characteristic,
200 int valueLen, void *userData)
202 (void)characteristic;
204 OIC_LOG(DEBUG, TAG, "IN");
205 OIC_LOG_V(DEBUG, TAG, "Changed characteristic value length [%d]", valueLen);
207 oc_mutex_lock(g_LEReqRespClientCbMutex);
208 if (NULL == g_LEClientDataReceivedCallback)
210 OIC_LOG(ERROR, TAG, "Request response callback is not set");
211 oc_mutex_unlock(g_LEReqRespClientCbMutex);
215 uint32_t sentLength = 0;
216 g_LEClientDataReceivedCallback(userData, (uint8_t *)value, valueLen, &sentLength);
218 OIC_LOG_V(DEBUG, TAG, "Recv data Length is %d", sentLength);
220 oc_mutex_unlock(g_LEReqRespClientCbMutex);
222 OIC_LOG(DEBUG, TAG, "OUT");
225 void CALEGattClientMTUChangedCb(bt_gatt_client_h client_handle, const bt_gatt_client_att_mtu_info_s *mtu_info, void *user_data)
227 OIC_LOG(DEBUG, TAG, "IN");
228 oc_mutex_lock(g_threadMTUChangedMutex);
229 OIC_LOG(DEBUG, TAG, "MTU changed signal");
230 oc_cond_signal(g_threadMTUChangedCond);
231 oc_mutex_unlock(g_threadMTUChangedMutex);
232 OIC_LOG(DEBUG, TAG, "OUT");
236 void CALEGattCharacteristicWriteCb(int result, bt_gatt_h reqHandle, void *userData)
241 OIC_LOG(DEBUG, TAG, "IN ");
243 if (BT_ERROR_NONE != result)
245 CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, "", 0, -1,
246 false, "writeChar failure");
248 OIC_LOG(ERROR, TAG, "Write failed Need Retry ");
249 //Need to Implement retry mechanism
253 oc_mutex_lock(g_threadWriteCharacteristicMutex);
254 OIC_LOG(DEBUG, TAG, "g_isSignalSetFlag is set true and signal");
255 g_isSignalSetFlag = true;
256 oc_cond_signal(g_threadWriteCharacteristicCond);
257 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
259 CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, "", 0, -1,
260 true, "writeChar success");
263 OIC_LOG(DEBUG, TAG, "OUT ");
266 CAResult_t CALEGattInitiateConnection(const char *remoteAddress)
268 OIC_LOG(DEBUG, TAG, "IN");
270 oc_mutex_lock(g_isConnectionInProgressMutex);
271 if (g_isConnectionInProgress)
273 oc_mutex_unlock(g_isConnectionInProgressMutex);
274 OIC_LOG(DEBUG, TAG, "Connection already in progress, cannot initiate new connection");
275 return CA_STATUS_FAILED;
277 g_isConnectionInProgress = true;
278 oc_mutex_unlock(g_isConnectionInProgressMutex);
280 // Pause the scanning
281 CALEGattStopDeviceScanning();
283 OIC_LOG_V(DEBUG, TAG,
284 "Trying to do Gatt connection to [%s]", remoteAddress);
286 oc_mutex_lock(g_LEClientThreadPoolMutex);
287 if (NULL == g_LEClientThreadPool)
289 oc_mutex_unlock(g_LEClientThreadPoolMutex);
290 OIC_LOG(ERROR, TAG, "g_LEClientThreadPool is NULL");
291 return CA_STATUS_FAILED;
294 char *addr = OICStrdup(remoteAddress);
297 oc_mutex_unlock(g_LEClientThreadPoolMutex);
298 OIC_LOG(ERROR, TAG, "OICStrdup failed");
299 return CA_STATUS_FAILED;
302 CAResult_t res = ca_thread_pool_add_task(g_LEClientThreadPool, CAGattConnectThread, addr, NULL);
303 oc_mutex_unlock(g_LEClientThreadPoolMutex);
304 if (CA_STATUS_OK != res)
306 OIC_LOG_V(ERROR, TAG,
307 "ca_thread_pool_add_task failed with ret [%d]", res);
309 return CA_STATUS_FAILED;
311 OIC_LOG(DEBUG, TAG, "OUT");
315 void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress)
317 OIC_LOG(DEBUG, TAG, "IN ");
319 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
323 OIC_LOG_V(DEBUG, TAG, "DisConnected from [%s] ", remoteAddress);
324 oc_mutex_lock(g_LEServerListMutex);
325 CARemoveLEServerInfoFromList(&g_LEServerList, remoteAddress);
326 oc_mutex_unlock(g_LEServerListMutex);
330 OIC_LOG_V(DEBUG, TAG, "Connected to [%s] ", remoteAddress);
332 oc_mutex_lock(g_isConnectionInProgressMutex);
333 g_isConnectionInProgress = false;
334 oc_mutex_unlock(g_isConnectionInProgressMutex);
337 // TODO:Disabling scanning for now.Need to check.
338 /*oc_mutex_lock(g_scanMutex);
339 if (g_isMulticastInProgress || g_isUnicastScanInProgress)
341 CAResult_t ret = CALEGattStartDeviceScanning();
342 if (CA_STATUS_OK != ret)
344 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning Failed");
347 oc_mutex_unlock(g_scanMutex);
349 LEServerInfo *serverInfo = NULL;
350 oc_mutex_lock(g_LEServerListMutex);
351 if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
353 oc_mutex_unlock(g_LEServerListMutex);
354 OIC_LOG_V(ERROR, TAG, "Could not get server info for [%s]", remoteAddress);
358 serverInfo->status = LE_STATUS_CONNECTED;
359 oc_mutex_unlock(g_LEServerListMutex);
361 oc_mutex_lock(g_LEClientThreadPoolMutex);
362 if (NULL == g_LEClientThreadPool)
364 oc_mutex_unlock(g_LEClientThreadPoolMutex);
365 OIC_LOG(ERROR, TAG, "g_LEClientThreadPool is NULL");
369 char *addr = OICStrdup(remoteAddress);
372 oc_mutex_unlock(g_LEClientThreadPoolMutex);
373 OIC_LOG(ERROR, TAG, "addr is NULL");
377 CAResult_t ret = ca_thread_pool_add_task(g_LEClientThreadPool, CADiscoverLEServicesThread,
379 oc_mutex_unlock(g_LEClientThreadPoolMutex);
380 if (CA_STATUS_OK != ret)
382 OIC_LOG_V(ERROR, TAG, "ca_thread_pool_add_task failed with ret [%d]", ret);
386 OIC_LOG(DEBUG, TAG, "OUT");
389 static bool CALEIsHaveServiceImpl(bt_adapter_le_device_scan_result_info_s *scanInfo,
390 const char *service_uuid,
391 bt_adapter_le_packet_type_e pkt_type)
398 result = bt_adapter_le_get_scan_result_service_uuids(scanInfo,
399 pkt_type, &uuids, &count);
400 if (result == BT_ERROR_NONE && NULL != uuids)
402 for (int i = 0; i < count; i++)
404 if (0 == strcasecmp(uuids[i], service_uuid))
406 OIC_LOG_V(DEBUG, TAG, "Service[%s] Found in %s",
407 uuids[i], scanInfo->remote_address);
416 char *man_data = NULL;
419 result = bt_adapter_le_get_scan_result_manufacturer_data(scanInfo,
420 pkt_type, &man_id, &man_data, &man_data_len);
422 if (result == BT_ERROR_NONE && NULL != man_data)
425 char *compare_man_data = OICCalloc(1, (man_data_len*2)+1);
426 if (!compare_man_data)
428 OIC_LOG(ERROR, TAG, "Memory allocation failed for compare_man_data");
433 for(int i=0;i<man_data_len;i++){
434 pos += snprintf(compare_man_data+pos, 2, "%.2x", man_data[i]);
437 if (man_id == samsung_code && 0 == strncasecmp(compare_man_data, service_uuid, CUSTOM_UUID_LEN))
439 OIC_LOG_V(DEBUG, TAG, "Manufacture Data[%s] Found in %s",
440 compare_man_data, scanInfo->remote_address);
443 OICFree(compare_man_data);
450 static bool CALEIsHaveService(bt_adapter_le_device_scan_result_info_s *scanInfo,
451 const char *service_uuid)
454 // For arduino servers, scan response will give the UUIDs advertised.
455 CALEIsHaveServiceImpl(scanInfo, service_uuid,
456 BT_ADAPTER_LE_PACKET_SCAN_RESPONSE) ||
457 // For android/tizen servers, advertising packet will give the UUIDs.
458 CALEIsHaveServiceImpl(scanInfo, service_uuid,
459 BT_ADAPTER_LE_PACKET_ADVERTISING);
462 void CALEAdapterScanResultCb(int result, bt_adapter_le_device_scan_result_info_s *scanInfo,
466 OIC_LOG(DEBUG, TAG, "IN");
468 VERIFY_NON_NULL_VOID(scanInfo, TAG, "scanInfo");
469 VERIFY_NON_NULL_VOID(scanInfo->remote_address, TAG, "scanInfo->remote_address");
471 OIC_LOG_V(DEBUG, TAG, "Remote Address [%s]", scanInfo->remote_address);
472 OIC_LOG_V(DEBUG, TAG, "Scan Result [%d]", result);
473 OIC_LOG_V(DEBUG, TAG,
474 " Adv data len [%d] Scan data len[%d]RSSI [%d] Addr_type [%d] ",
475 scanInfo->adv_data_len, scanInfo->scan_data_len, scanInfo->rssi,
476 scanInfo->address_type);
478 // Check if scanning was stopped (since this callback is
479 // being triggered even after stopping the scan)
480 oc_mutex_lock(g_isScanningInProgressMutex);
481 if (!g_isScanningInProgress)
483 oc_mutex_unlock(g_isScanningInProgressMutex);
484 OIC_LOG(DEBUG, TAG, "Scanning not in progress, so ignoring callback");
487 oc_mutex_unlock(g_isScanningInProgressMutex);
489 if (CALEIsHaveService(scanInfo, CA_GATT_SERVICE_UUID) ||
490 CALEIsHaveService(scanInfo, CA_GATT_CUSTOM_UUID)||
491 CALEIsHaveService(scanInfo, CA_GATT_CUSTOM_UUID2))
493 OIC_LOG_V(DEBUG, TAG, "Device [%s] supports OIC or custom service", scanInfo->remote_address);
495 LEServerInfo *serverInfo = NULL;
496 oc_mutex_lock(g_LEServerListMutex);
497 CAResult_t ret = CAGetLEServerInfo(g_LEServerList, scanInfo->remote_address, &serverInfo);
498 if (CA_STATUS_OK != ret)
500 OIC_LOG_V(DEBUG, TAG,
501 "Newly discovered device with address [%s] ", scanInfo->remote_address);
503 char *addr = OICStrdup(scanInfo->remote_address);
506 oc_mutex_unlock(g_LEServerListMutex);
507 OIC_LOG(ERROR, TAG, "Device address is NULL");
510 serverInfo = (LEServerInfo *)OICCalloc(1, sizeof(LEServerInfo));
511 if (NULL == serverInfo)
513 oc_mutex_unlock(g_LEServerListMutex);
514 OIC_LOG(ERROR, TAG, "Calloc failed");
518 serverInfo->remoteAddress = addr;
519 serverInfo->status = LE_STATUS_DISCOVERED;
521 if (CA_STATUS_OK != CAAddLEServerInfoToList(&g_LEServerList, serverInfo))
523 oc_mutex_unlock(g_LEServerListMutex);
524 OIC_LOG_V(ERROR, TAG, "Could not add [%s] to server list", scanInfo->remote_address);
525 CAFreeLEServerInfo(serverInfo);
529 OIC_LOG_V(DEBUG, TAG,
530 "Device Present with address [%s] ", scanInfo->remote_address);
532 if(serverInfo->status == LE_STATUS_UNICAST_PENDING){
533 bt_gatt_client_h clientHandle = NULL;
534 int32_t ret = bt_gatt_client_create(serverInfo->remoteAddress, &clientHandle);
535 if (BT_ERROR_NONE != ret || NULL == clientHandle)
537 OIC_LOG_V(ERROR, TAG,
538 "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret));
539 CALEGattDisConnect(serverInfo->remoteAddress);
540 oc_mutex_unlock(g_LEServerListMutex);
543 serverInfo->clientHandle = clientHandle;
545 serverInfo->status = LE_STATUS_CONNECTION_INITIATED;
546 if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress))
548 OIC_LOG_V(ERROR, TAG, "Could not initiate connection to [%s]", serverInfo->remoteAddress);
549 serverInfo->status = LE_STATUS_DISCOVERED;
550 CADestroyLEDataList(&serverInfo->pendingDataList);
551 oc_mutex_unlock(g_LEServerListMutex);
557 oc_mutex_unlock(g_LEServerListMutex);
558 OIC_LOG(DEBUG, TAG, "OUT");
561 void CASetLEClientThreadPoolHandle(ca_thread_pool_t handle)
563 OIC_LOG(DEBUG, TAG, "IN");
565 oc_mutex_lock(g_LEClientThreadPoolMutex);
566 g_LEClientThreadPool = handle;
567 oc_mutex_unlock(g_LEClientThreadPoolMutex);
569 OIC_LOG(DEBUG, TAG, "OUT");
572 void CASetLEReqRespClientCallback(CABLEDataReceivedCallback callback)
574 OIC_LOG(DEBUG, TAG, "IN");
576 oc_mutex_lock(g_LEReqRespClientCbMutex);
578 g_LEClientDataReceivedCallback = callback;
580 oc_mutex_unlock(g_LEReqRespClientCbMutex);
582 OIC_LOG(DEBUG, TAG, "OUT");
585 void CASetBLEClientErrorHandleCallback(CABLEErrorHandleCallback callback)
587 g_clientErrorCallback = callback;
590 CAResult_t CAStartLEGattClient()
592 OIC_LOG(DEBUG, TAG, "IN");
594 oc_mutex_lock(g_LEClientStateMutex);
595 if (true == g_isLEGattClientStarted)
597 OIC_LOG(ERROR, TAG, "Gatt Client is already running!!");
598 oc_mutex_unlock(g_LEClientStateMutex);
599 return CA_STATUS_FAILED;
602 CAResult_t result = CALEGattSetCallbacks();
603 if (CA_STATUS_OK != result)
605 OIC_LOG(ERROR, TAG, "CABleGattSetCallbacks Failed");
606 oc_mutex_unlock(g_LEClientStateMutex);
607 CATerminateLEGattClient();
608 return CA_STATUS_FAILED;
611 g_isLEGattClientStarted = true;
612 oc_mutex_unlock(g_LEClientStateMutex);
614 oc_mutex_lock(g_LEClientThreadPoolMutex);
615 if (NULL == g_LEClientThreadPool)
617 OIC_LOG(ERROR, TAG, "gBleServerThreadPool is NULL");
618 CATerminateGattClientMutexVariables();
619 oc_mutex_unlock(g_LEClientThreadPoolMutex);
620 return CA_STATUS_FAILED;
623 result = ca_thread_pool_add_task(g_LEClientThreadPool, CAStartTimerThread,
625 if (CA_STATUS_OK != result)
627 OIC_LOG(ERROR, TAG, "ca_thread_pool_add_task failed");
628 CATerminateGattClientMutexVariables();
629 oc_mutex_unlock(g_LEClientThreadPoolMutex);
630 return CA_STATUS_FAILED;
633 result= ca_thread_pool_add_task(g_LEClientThreadPool, CALEClientScanThread,
635 if (CA_STATUS_OK != result)
637 OIC_LOG(ERROR, TAG, "ca_thread_pool_add_task failed");
638 CATerminateGattClientMutexVariables();
639 oc_mutex_unlock(g_LEClientThreadPoolMutex);
640 return CA_STATUS_FAILED;
642 oc_mutex_unlock(g_LEClientThreadPoolMutex);
644 OIC_LOG(DEBUG, TAG, "OUT");
648 void CALEClientScanThread()
650 oc_mutex_lock(g_scanMutex);
651 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
653 CAResult_t result = CALEGattStartDeviceScanning();
654 if (CA_STATUS_OK != result)
656 oc_mutex_unlock(g_scanMutex);
657 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning failed");
660 g_isUnicastScanInProgress = true;
662 oc_cond_signal(g_startTimerCond);
666 g_isUnicastScanInProgress = true;
668 oc_cond_signal(g_scanningTimeCond);
670 oc_mutex_unlock(g_scanMutex);
672 OIC_LOG(DEBUG, TAG, "OUT");
676 void CAStartTimerThread(void *data)
680 OIC_LOG(DEBUG, TAG, "IN");
681 while (g_isLEGattClientStarted)
683 oc_mutex_lock(g_scanMutex);
684 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
686 OIC_LOG(DEBUG, TAG, "waiting....");
687 oc_cond_wait(g_startTimerCond, g_scanMutex);
688 OIC_LOG(DEBUG, TAG, "Wake up");
691 // Timed conditional wait for stopping the scan.
692 OCWaitResult_t ret = oc_cond_wait_for(g_scanningTimeCond, g_scanMutex,
694 if (OC_WAIT_TIMEDOUT == ret)
696 OIC_LOG(DEBUG, TAG, "Scan is timed Out");
698 CALEGattStopDeviceScanning();
700 if (g_isMulticastInProgress)
702 oc_mutex_lock(g_multicastDataListMutex);
703 // Clear the data list and device list.
704 u_arraylist_destroy(g_multicastDataList);
705 g_multicastDataList = NULL;
706 oc_mutex_unlock(g_multicastDataListMutex);
707 g_isMulticastInProgress = false;
709 g_isUnicastScanInProgress = false;
711 oc_mutex_unlock(g_scanMutex);
714 OIC_LOG(DEBUG, TAG, "OUT");
717 void CAStopLEGattClient()
719 OIC_LOG(DEBUG, TAG, "IN");
721 oc_mutex_lock(g_LEClientStateMutex);
723 if (false == g_isLEGattClientStarted)
725 OIC_LOG(ERROR, TAG, "Gatt Client is not running to stop");
726 oc_mutex_unlock(g_LEClientStateMutex);
730 CALEGattUnSetCallbacks();
732 CALEGattStopDeviceScanning();
734 g_isLEGattClientStarted = false;
736 // Signal the conditions waiting in Start timer.
737 oc_cond_signal(g_startTimerCond);
738 oc_cond_signal(g_scanningTimeCond);
740 // Destroy the multicast data list and device list if not empty.
741 if (NULL != g_multicastDataList)
743 oc_mutex_lock(g_multicastDataListMutex);
744 u_arraylist_destroy(g_multicastDataList);
745 g_multicastDataList = NULL;
746 oc_mutex_unlock(g_multicastDataListMutex);
749 oc_mutex_lock(g_LEServerListMutex);
750 CAFreeLEServerList(g_LEServerList);
751 g_LEServerList = NULL;
752 oc_mutex_unlock(g_LEServerListMutex);
754 oc_mutex_lock(g_threadWriteCharacteristicMutex);
755 oc_cond_signal(g_threadWriteCharacteristicCond);
756 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
758 GMainContext *context_event_loop = NULL;
759 // Required for waking up the thread which is running in gmain loop
760 if (NULL != g_eventLoop)
762 context_event_loop = g_main_loop_get_context(g_eventLoop);
764 if (context_event_loop)
766 OIC_LOG_V(DEBUG, TAG, "g_eventLoop context %p", (void *)context_event_loop);
767 g_main_context_wakeup(context_event_loop);
769 // Kill g main loops and kill threads.
770 g_main_loop_quit(g_eventLoop);
774 OIC_LOG(ERROR, TAG, "g_eventLoop context is NULL");
777 oc_mutex_unlock(g_LEClientStateMutex);
779 OIC_LOG(DEBUG, TAG, "OUT");
782 CAResult_t CAInitializeLEGattClient()
784 OIC_LOG(DEBUG, TAG, "Initialize GATT Client");
785 CAResult_t res = CAInitGattClientMutexVariables();
786 if (CA_STATUS_OK != res)
788 OIC_LOG(ERROR, TAG, "CAInitGattClientMutexVariables failed!");
789 CATerminateGattClientMutexVariables();
790 return CA_STATUS_FAILED;
795 void CATerminateLEGattClient()
797 OIC_LOG(DEBUG, TAG, "IN");
799 CATerminateGattClientMutexVariables();
801 OIC_LOG(DEBUG, TAG, "OUT");
804 CAResult_t CAInitGattClientMutexVariables()
806 OIC_LOG(DEBUG, TAG, "IN");
807 if (NULL == g_LEClientStateMutex)
809 g_LEClientStateMutex = oc_mutex_new();
810 if (NULL == g_LEClientStateMutex)
812 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
813 return CA_STATUS_FAILED;
817 if (NULL == g_LEServerListMutex)
819 g_LEServerListMutex = oc_mutex_new();
820 if (NULL == g_LEServerListMutex)
822 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
823 return CA_STATUS_FAILED;
827 if (NULL == g_LEReqRespClientCbMutex)
829 g_LEReqRespClientCbMutex = oc_mutex_new();
830 if (NULL == g_LEReqRespClientCbMutex)
832 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
833 return CA_STATUS_FAILED;
837 if (NULL == g_LEClientThreadPoolMutex)
839 g_LEClientThreadPoolMutex = oc_mutex_new();
840 if (NULL == g_LEClientThreadPoolMutex)
842 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
843 return CA_STATUS_FAILED;
847 if (NULL == g_LEClientConnectMutex)
849 g_LEClientConnectMutex = oc_mutex_new();
850 if (NULL == g_LEClientConnectMutex)
852 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
853 return CA_STATUS_FAILED;
857 if (NULL == g_isScanningInProgressMutex)
859 g_isScanningInProgressMutex = oc_mutex_new();
860 if (NULL == g_isScanningInProgressMutex)
862 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
863 return CA_STATUS_FAILED;
867 if (NULL == g_isConnectionInProgressMutex)
869 g_isConnectionInProgressMutex = oc_mutex_new();
870 if (NULL == g_isConnectionInProgressMutex)
872 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
873 return CA_STATUS_FAILED;
877 if (NULL == g_multicastDataListMutex)
879 g_multicastDataListMutex = oc_mutex_new();
880 if (NULL == g_multicastDataListMutex)
882 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
883 return CA_STATUS_FAILED;
887 if (NULL == g_scanMutex)
889 g_scanMutex = oc_mutex_new();
890 if (NULL == g_scanMutex)
892 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
893 return CA_STATUS_FAILED;
897 if (NULL == g_threadWriteCharacteristicMutex)
899 g_threadWriteCharacteristicMutex = oc_mutex_new();
900 if (NULL == g_threadWriteCharacteristicMutex)
902 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
903 return CA_STATUS_FAILED;
907 if (NULL == g_threadMTUChangedMutex)
909 g_threadMTUChangedMutex = oc_mutex_new();
910 if (NULL == g_threadMTUChangedMutex)
912 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
913 return CA_STATUS_FAILED;
917 if (NULL == g_startTimerCond)
919 g_startTimerCond = oc_cond_new();
920 if (NULL == g_startTimerCond)
922 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
923 return CA_STATUS_FAILED;
927 if (NULL == g_scanningTimeCond)
929 g_scanningTimeCond = oc_cond_new();
930 if (NULL == g_scanningTimeCond)
932 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
933 return CA_STATUS_FAILED;
937 if (NULL == g_threadWriteCharacteristicCond)
939 g_threadWriteCharacteristicCond = oc_cond_new();
940 if (NULL == g_threadWriteCharacteristicCond)
942 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
943 return CA_STATUS_FAILED;
947 if (NULL == g_threadMTUChangedCond)
949 g_threadMTUChangedCond = oc_cond_new();
950 if (NULL == g_threadMTUChangedCond)
952 OIC_LOG(ERROR, TAG, "oc_cond_new failed");
953 return CA_STATUS_FAILED;
958 OIC_LOG(DEBUG, TAG, "OUT");
962 void CATerminateGattClientMutexVariables()
964 OIC_LOG(DEBUG, TAG, "IN");
966 oc_mutex_free(g_LEClientStateMutex);
967 g_LEClientStateMutex = NULL;
969 oc_mutex_free(g_LEServerListMutex);
970 g_LEServerListMutex = NULL;
972 oc_mutex_free(g_LEReqRespClientCbMutex);
973 g_LEReqRespClientCbMutex = NULL;
975 oc_mutex_free(g_LEClientConnectMutex);
976 g_LEClientConnectMutex = NULL;
978 oc_mutex_free(g_LEClientThreadPoolMutex);
979 g_LEClientThreadPoolMutex = NULL;
981 oc_mutex_free(g_isScanningInProgressMutex);
982 g_isScanningInProgressMutex = NULL;
984 oc_mutex_free(g_isConnectionInProgressMutex);
985 g_isConnectionInProgressMutex = NULL;
987 oc_mutex_free(g_multicastDataListMutex);
988 g_multicastDataListMutex = NULL;
990 oc_mutex_free(g_scanMutex);
993 oc_mutex_free(g_threadWriteCharacteristicMutex);
994 g_threadWriteCharacteristicMutex = NULL;
996 oc_mutex_free(g_threadMTUChangedMutex);
997 g_threadMTUChangedMutex = NULL;
999 oc_cond_free(g_startTimerCond);
1000 g_startTimerCond = NULL;
1002 oc_cond_free(g_scanningTimeCond);
1003 g_scanningTimeCond = NULL;
1005 oc_cond_free(g_threadWriteCharacteristicCond);
1006 g_threadWriteCharacteristicCond = NULL;
1007 g_isSignalSetFlag = false;
1009 oc_cond_free(g_threadMTUChangedCond);
1010 g_threadMTUChangedCond = NULL;
1012 OIC_LOG(DEBUG, TAG, "OUT");
1015 CAResult_t CALEGattSetCallbacks()
1017 OIC_LOG(DEBUG, TAG, "IN");
1019 OIC_LOG(DEBUG, TAG, "OUT");
1020 return CA_STATUS_OK;
1023 void CALEGattUnSetCallbacks()
1025 OIC_LOG(DEBUG, TAG, "IN");
1027 bt_gatt_unset_connection_state_changed_cb();
1029 oc_mutex_lock(g_LEServerListMutex);
1030 LEServerInfoList *curNode = g_LEServerList;
1033 LEServerInfo *serverInfo = curNode->serverInfo;
1034 if (serverInfo->status >= LE_STATUS_SERVICES_DISCOVERED)
1036 bt_gatt_client_unset_characteristic_value_changed_cb(serverInfo->readChar);
1038 curNode = curNode->next;
1040 oc_mutex_unlock(g_LEServerListMutex);
1042 OIC_LOG(DEBUG, TAG, "OUT");
1045 CAResult_t CALEGattStartDeviceScanning()
1047 OIC_LOG(DEBUG, TAG, "IN");
1049 oc_mutex_lock(g_isScanningInProgressMutex);
1050 if (!g_isScanningInProgress)
1052 int ret = bt_adapter_le_start_scan(CALEAdapterScanResultCb, NULL);
1053 if (BT_ERROR_NONE != ret)
1055 oc_mutex_unlock(g_isScanningInProgressMutex);
1056 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_start_scan failed[%s]",
1057 CALEGetErrorMsg(ret));
1058 return CA_STATUS_FAILED;
1060 g_isScanningInProgress = true;
1064 OIC_LOG(DEBUG, TAG, "Ignore, scanning already in progress");
1066 oc_mutex_unlock(g_isScanningInProgressMutex);
1068 OIC_LOG(DEBUG, TAG, "OUT");
1069 return CA_STATUS_OK;
1072 void CALEGattStopDeviceScanning()
1074 OIC_LOG(DEBUG, TAG, "IN");
1076 oc_mutex_lock(g_isScanningInProgressMutex);
1077 if (g_isScanningInProgress)
1079 int ret = bt_adapter_le_stop_scan();
1080 if (BT_ERROR_NONE != ret)
1082 oc_mutex_unlock(g_isScanningInProgressMutex);
1083 OIC_LOG_V(ERROR, TAG, "bt_adapter_le_stop_scan failed[%s]",
1084 CALEGetErrorMsg(ret));
1087 g_isScanningInProgress = false;
1088 g_isUnicastScanInProgress= false;
1092 OIC_LOG(DEBUG, TAG, "Ignore, scanning not in progress");
1094 oc_mutex_unlock(g_isScanningInProgressMutex);
1096 OIC_LOG(DEBUG, TAG, "OUT");
1099 void CAGattConnectThread (void *remoteAddress)
1101 OIC_LOG(DEBUG, TAG, "IN ");
1103 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
1105 char *address = (char *)remoteAddress;
1107 OIC_LOG_V(DEBUG, TAG, "remote address is [%s]", address);
1109 CAResult_t result = CALEGattConnect(address);
1111 if (CA_STATUS_OK != result)
1113 OIC_LOG_V(ERROR, TAG, "bt_gatt_connect failed for [%s]", address);
1118 OIC_LOG(DEBUG, TAG, "OUT");
1121 CAResult_t CALEGattConnect(const char *remoteAddress)
1123 OIC_LOG(DEBUG, TAG, "IN");
1125 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1126 "remote address is NULL", CA_STATUS_FAILED);
1128 oc_mutex_lock(g_LEClientConnectMutex);
1129 CAResult_t result = CA_STATUS_OK;
1131 int ret = bt_gatt_connect(remoteAddress, true);
1132 if (BT_ERROR_NONE != ret)
1134 OIC_LOG_V(ERROR, TAG, "bt_gatt_connect Failed with ret value [%s] ",
1135 CALEGetErrorMsg(ret));
1136 oc_mutex_unlock(g_LEClientConnectMutex);
1137 return CA_STATUS_FAILED;
1140 oc_mutex_unlock(g_LEClientConnectMutex);
1142 OIC_LOG(DEBUG, TAG, "OUT");
1146 CAResult_t CALEGattDisConnect(const char *remoteAddress)
1148 OIC_LOG(DEBUG, TAG, "IN");
1150 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1151 "remote address is NULL", CA_STATUS_FAILED);
1153 int ret = bt_gatt_disconnect(remoteAddress);
1155 if (BT_ERROR_NONE != ret)
1157 OIC_LOG_V(ERROR, TAG, "bt_gatt_disconnect Failed with ret value [%s] ",
1158 CALEGetErrorMsg(ret));
1159 return CA_STATUS_FAILED;
1162 OIC_LOG(DEBUG, TAG, "OUT");
1163 return CA_STATUS_OK;
1166 CAResult_t CAUpdateCharacteristicsToGattServerImpl(LEServerInfo *serverInfo,
1167 const uint8_t *data, const uint32_t dataLen)
1169 OIC_LOG(DEBUG, TAG, "IN");
1171 VERIFY_NON_NULL(serverInfo, TAG, "Server Info is NULL");
1173 CALEGattStopDeviceScanning();
1175 OIC_LOG_V(DEBUG, TAG, "Updating the data of length [%d] to [%s] ", dataLen,
1176 serverInfo->remoteAddress);
1178 int result = bt_gatt_set_value(serverInfo->writeChar, (char *)data, dataLen);
1180 if (BT_ERROR_NONE != result)
1182 OIC_LOG_V(ERROR, TAG,
1183 "bt_gatt_set_value Failed with return val [%s]",
1184 CALEGetErrorMsg(result));
1188 result = bt_gatt_client_write_value(serverInfo->writeChar, CALEGattCharacteristicWriteCb,
1190 if (BT_ERROR_NONE != result)
1192 OIC_LOG_V(ERROR, TAG,
1193 "bt_gatt_client_write_value Failed with return val [%s]",
1194 CALEGetErrorMsg(result));
1198 // wait for callback for write Characteristic with success to sent data
1199 OIC_LOG_V(DEBUG, TAG, "callback flag is %d", g_isSignalSetFlag);
1200 oc_mutex_lock(g_threadWriteCharacteristicMutex);
1201 if (!g_isSignalSetFlag)
1203 OIC_LOG(DEBUG, TAG, "wait for callback to notify writeCharacteristic is success");
1204 if (OC_WAIT_SUCCESS != oc_cond_wait_for(g_threadWriteCharacteristicCond,
1205 g_threadWriteCharacteristicMutex,
1206 WAIT_TIME_WRITE_CHARACTERISTIC))
1208 g_isSignalSetFlag = false;
1209 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
1210 OIC_LOG(ERROR, TAG, "there is no response. write has failed");
1214 // reset flag set by writeCharacteristic Callback
1215 g_isSignalSetFlag = false;
1216 oc_mutex_unlock(g_threadWriteCharacteristicMutex);
1218 oc_mutex_lock(g_scanMutex);
1219 if (g_isMulticastInProgress || g_isUnicastScanInProgress)
1221 if (CA_STATUS_OK != CALEGattStartDeviceScanning())
1223 OIC_LOG(ERROR, TAG, "Could not start device scanning");
1226 oc_mutex_unlock(g_scanMutex);
1227 OIC_LOG(DEBUG, TAG, "OUT");
1228 return CA_STATUS_OK;
1231 oc_mutex_lock(g_scanMutex);
1232 if (g_isMulticastInProgress || g_isUnicastScanInProgress)
1234 if (CA_STATUS_OK != CALEGattStartDeviceScanning())
1236 OIC_LOG(ERROR, TAG, "Could not start device scanning");
1239 oc_mutex_unlock(g_scanMutex);
1241 OIC_LOG(DEBUG, TAG, "OUT");
1242 return CA_STATUS_FAILED;
1245 void CADiscoverLEServicesThread(void *remoteAddress)
1247 OIC_LOG(DEBUG, TAG, "IN");
1249 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
1251 char *address = (char *)remoteAddress;
1253 CAResult_t result = CALEGattDiscoverServices(address);
1254 if (CA_STATUS_OK != result)
1256 OIC_LOG(ERROR, TAG, "CALEGattDiscoverServices failed");
1260 OIC_LOG(DEBUG, TAG, "OUT");
1263 CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
1265 OIC_LOG(DEBUG, TAG, "IN");
1267 VERIFY_NON_NULL_RET(remoteAddress, TAG,
1268 "remote address is NULL", CA_STATUS_FAILED);
1270 LEServerInfo *serverInfo = NULL;
1271 unsigned int mtu_size;
1272 oc_mutex_lock(g_LEServerListMutex);
1273 if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
1275 oc_mutex_unlock(g_LEServerListMutex);
1276 OIC_LOG_V(ERROR, TAG, "Could not get server info for [%s]", remoteAddress);
1277 CALEGattDisConnect(remoteAddress);
1278 return CA_STATUS_FAILED;
1281 bt_gatt_h serviceHandle = NULL;
1282 int32_t ret = bt_gatt_client_get_service(serverInfo->clientHandle, CA_GATT_SERVICE_UUID, &serviceHandle);
1283 if (BT_ERROR_NONE != ret || NULL == serviceHandle)
1285 OIC_LOG_V(ERROR, TAG,
1286 "bt_gatt_client_get_service Failed with ret value [%s] ", CALEGetErrorMsg(ret));
1290 // Server will read data on this characteristic.
1291 bt_gatt_h writeChrHandle = NULL;
1292 ret = bt_gatt_service_get_characteristic(serviceHandle, CA_GATT_REQUEST_CHRC_UUID,
1294 if (BT_ERROR_NONE != ret || NULL == writeChrHandle)
1296 OIC_LOG_V(ERROR, TAG,
1297 "bt_gatt_service_get_characteristic Failed with ret value [%s] ",
1298 CALEGetErrorMsg(ret));
1302 // Server will notify data on this characteristic.
1303 bt_gatt_h readChrHandle = NULL;
1304 ret = bt_gatt_service_get_characteristic(serviceHandle, CA_GATT_RESPONSE_CHRC_UUID,
1306 if (BT_ERROR_NONE != ret || NULL == readChrHandle)
1308 OIC_LOG_V(ERROR, TAG,
1309 "bt_gatt_service_get_characteristic Failed with ret value [%s] ",
1310 CALEGetErrorMsg(ret));
1314 //TODO: This data has to be freed while unsetting the callback.
1315 char *addr = OICStrdup(remoteAddress);
1318 OIC_LOG(ERROR, TAG, "addr is NULL");
1322 ret = bt_gatt_client_set_characteristic_value_changed_cb(readChrHandle,
1323 CALEGattCharacteristicChangedCb,
1325 if (BT_ERROR_NONE != ret)
1327 OIC_LOG_V(ERROR, TAG,
1328 "bt_gatt_client_set_characteristic_value_changed_cb Failed with ret value [%s]",
1329 CALEGetErrorMsg(ret));
1333 ret = bt_gatt_client_set_att_mtu_changed_cb(serverInfo->clientHandle,
1334 CALEGattClientMTUChangedCb,
1335 (void *)serverInfo->remoteAddress);
1336 if (BT_ERROR_NONE != ret)
1338 OIC_LOG_V(ERROR, TAG,
1339 "bt_gatt_client_set_att_mtu_changed_cb Failed with ret value [%s]",
1340 CALEGetErrorMsg(ret));
1344 oc_mutex_lock(g_threadMTUChangedMutex);
1345 ret = bt_gatt_client_request_att_mtu_change(serverInfo->clientHandle, CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_MTU_HEADER_SIZE);
1346 if (BT_ERROR_NONE != ret)
1348 OIC_LOG_V(ERROR, TAG,
1349 "bt_gatt_client_request_att_mtu_change Failed with ret value [%s]",
1350 CALEGetErrorMsg(ret));
1351 oc_mutex_unlock(g_threadMTUChangedMutex);
1355 OIC_LOG(DEBUG, TAG, "wait for callback to notify MTU Changed Callback");
1356 oc_cond_wait(g_threadMTUChangedCond, g_threadMTUChangedMutex);
1357 oc_mutex_unlock(g_threadMTUChangedMutex);
1359 OIC_LOG(DEBUG, TAG, "Done MTU");
1360 ret = bt_gatt_client_get_att_mtu(serverInfo->clientHandle, &mtu_size);
1361 if (BT_ERROR_NONE != ret)
1363 OIC_LOG_V(ERROR, TAG,
1364 "bt_gatt_client_get_att_mtu Failed with ret value [%s]",
1365 CALEGetErrorMsg(ret));
1368 OIC_LOG_V(DEBUG, TAG,"Negotiated MTU Size %d",mtu_size);
1370 serverInfo->serviceHandle = serviceHandle;
1371 serverInfo->readChar = readChrHandle;
1372 serverInfo->writeChar = writeChrHandle;
1373 serverInfo->status = LE_STATUS_SERVICES_DISCOVERED;
1374 serverInfo->mtu_size = mtu_size;
1376 while (serverInfo->pendingDataList)
1378 LEData *leData = serverInfo->pendingDataList->data;
1379 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(
1380 serverInfo, leData->data, leData->dataLength))
1382 OIC_LOG_V(ERROR, TAG, "Failed to send pending data to [%s]",
1383 serverInfo->remoteAddress);
1385 CADestroyLEDataList(&serverInfo->pendingDataList);
1388 CARemoveLEDataFromList(&serverInfo->pendingDataList);
1390 oc_mutex_unlock(g_LEServerListMutex);
1392 OIC_LOG(DEBUG, TAG, "OUT");
1393 return CA_STATUS_OK;
1396 bt_gatt_client_destroy(serverInfo->clientHandle);
1397 serverInfo->clientHandle = NULL;
1398 oc_mutex_unlock(g_LEServerListMutex);
1399 CALEGattDisConnect(remoteAddress);
1400 return CA_STATUS_FAILED;
1403 CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
1404 const uint8_t *data, const uint32_t dataLen,
1405 CALETransferType_t type, const int32_t position)
1407 OIC_LOG(DEBUG, TAG, "IN");
1409 VERIFY_NON_NULL(data, TAG, "data is NULL");
1413 OIC_LOG(ERROR, TAG, "dataLen is less than or equal zero. Invalid input!");
1414 return CA_STATUS_INVALID_PARAM;
1417 LEServerInfo *serverInfo = NULL;
1418 oc_mutex_lock(g_LEServerListMutex);
1419 if (LE_UNICAST == type)
1421 if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
1423 OIC_LOG_V(DEBUG, TAG,
1424 "Device with address [%s] not yet found, initiating scan",
1427 char *addr = OICStrdup(remoteAddress);
1430 oc_mutex_unlock(g_LEServerListMutex);
1431 OIC_LOG(ERROR, TAG, "Device address is NULL");
1432 return CA_STATUS_FAILED;
1434 serverInfo = (LEServerInfo *)OICCalloc(1, sizeof(LEServerInfo));
1435 if (NULL == serverInfo)
1437 oc_mutex_unlock(g_LEServerListMutex);
1438 OIC_LOG(ERROR, TAG, "Calloc failed");
1440 return CA_STATUS_FAILED;
1442 serverInfo->remoteAddress = addr;
1443 serverInfo->status = LE_STATUS_UNICAST_PENDING;
1445 if (CA_STATUS_OK != CAAddLEServerInfoToList(&g_LEServerList, serverInfo))
1447 oc_mutex_unlock(g_LEServerListMutex);
1448 OIC_LOG_V(ERROR, TAG, "Could not add [%s] to server list", remoteAddress);
1449 CAFreeLEServerInfo(serverInfo);
1450 return CA_STATUS_FAILED;
1453 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1455 oc_mutex_unlock(g_LEServerListMutex);
1456 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1457 return CA_STATUS_FAILED;
1461 if(serverInfo->status == LE_STATUS_UNICAST_PENDING)
1463 oc_mutex_lock(g_scanMutex);
1464 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
1466 CAResult_t result = CALEGattStartDeviceScanning();
1467 if (CA_STATUS_OK != result)
1469 oc_mutex_unlock(g_scanMutex);
1470 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning failed");
1471 return CA_STATUS_FAILED;
1473 g_isUnicastScanInProgress = true;
1475 oc_cond_signal(g_startTimerCond);
1479 g_isUnicastScanInProgress = true;
1481 oc_cond_signal(g_scanningTimeCond);
1483 oc_mutex_unlock(g_scanMutex);
1487 if (serverInfo->status == LE_STATUS_DISCOVERED)
1489 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1491 oc_mutex_unlock(g_LEServerListMutex);
1492 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1493 return CA_STATUS_FAILED;
1496 bt_gatt_client_h clientHandle = NULL;
1497 int32_t ret = bt_gatt_client_create(serverInfo->remoteAddress, &clientHandle);
1498 if (BT_ERROR_NONE != ret || NULL == clientHandle)
1500 OIC_LOG_V(ERROR, TAG,
1501 "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret));
1502 CALEGattDisConnect(serverInfo->remoteAddress);
1503 return CA_STATUS_FAILED;
1505 serverInfo->clientHandle = clientHandle;
1507 serverInfo->status = LE_STATUS_CONNECTION_INITIATED;
1508 if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress))
1510 OIC_LOG_V(ERROR, TAG, "Could not initiate connection to [%s]", serverInfo->remoteAddress);
1511 serverInfo->status = LE_STATUS_DISCOVERED;
1512 CADestroyLEDataList(&serverInfo->pendingDataList);
1513 oc_mutex_unlock(g_LEServerListMutex);
1514 return CA_STATUS_FAILED;
1517 else if (serverInfo->status < LE_STATUS_SERVICES_DISCOVERED)
1519 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1521 oc_mutex_unlock(g_LEServerListMutex);
1522 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
1523 return CA_STATUS_FAILED;
1528 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(serverInfo, data, dataLen))
1530 OIC_LOG_V(ERROR, TAG, "Could not update characteristic to gatt server [%s]",
1531 serverInfo->remoteAddress);
1532 oc_mutex_unlock(g_LEServerListMutex);
1533 return CA_STATUS_FAILED;
1537 else if (LE_MULTICAST == type)
1539 OIC_LOG(ERROR, TAG, "LE_MULTICAST type Not used");
1541 oc_mutex_unlock(g_LEServerListMutex);
1542 OIC_LOG(DEBUG, TAG, "OUT");
1543 return CA_STATUS_OK;
1546 CAResult_t CAUpdateCharacteristicsToAllGattServers(const uint8_t *data, uint32_t dataLen)
1548 OIC_LOG(DEBUG, TAG, "IN");
1550 VERIFY_NON_NULL(data, TAG, "data is NULL");
1554 OIC_LOG(ERROR, TAG, "dataLen is less than or equal zero. Invalid input !");
1555 return CA_STATUS_INVALID_PARAM;
1558 oc_mutex_lock(g_LEServerListMutex);
1559 LEServerInfoList *curNode = g_LEServerList;
1562 LEServerInfo *serverInfo = curNode->serverInfo;
1563 if (serverInfo->status == LE_STATUS_SERVICES_DISCOVERED)
1565 if (CA_STATUS_OK != CAUpdateCharacteristicsToGattServerImpl(serverInfo, data, dataLen))
1567 OIC_LOG_V(ERROR, TAG, "Failed to update characteristics to gatt server [%s]",
1568 serverInfo->remoteAddress);
1571 else if (serverInfo->status != LE_STATUS_INVALID)
1573 if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
1575 OIC_LOG(ERROR, TAG, "Failed to add to pending list");
1578 curNode = curNode->next;
1580 oc_mutex_unlock(g_LEServerListMutex);
1582 // Add the data to pending list.
1583 LEData *multicastData = (LEData *)OICCalloc(1, sizeof(LEData));
1584 if (NULL == multicastData)
1586 OIC_LOG(ERROR, TAG, "Calloc failed");
1589 multicastData->data = OICCalloc(1, dataLen);
1590 if (NULL == multicastData->data)
1592 OIC_LOG(ERROR, TAG, "Calloc failed");
1595 memcpy(multicastData->data, data, dataLen);
1596 multicastData->dataLength = dataLen;
1598 oc_mutex_lock(g_multicastDataListMutex);
1599 if (NULL == g_multicastDataList)
1601 g_multicastDataList = u_arraylist_create();
1603 u_arraylist_add(g_multicastDataList, (void *)multicastData);
1604 oc_mutex_unlock(g_multicastDataListMutex);
1606 // Start the scanning, if not started, else reset timer
1607 oc_mutex_lock(g_scanMutex);
1608 if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
1610 CAResult_t result = CALEGattStartDeviceScanning();
1611 if (CA_STATUS_OK != result)
1613 oc_mutex_unlock(g_scanMutex);
1614 OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning Failed");
1617 g_isMulticastInProgress = true;
1618 // Start the timer by signalling it
1619 oc_cond_signal(g_startTimerCond);
1623 g_isMulticastInProgress = true;
1625 oc_cond_signal(g_scanningTimeCond);
1627 oc_mutex_unlock(g_scanMutex);
1630 OIC_LOG(DEBUG, TAG, "OUT ");
1631 return CA_STATUS_OK;
1634 uint16_t CALEClientGetMtuSize(const char* remote_address)
1636 LEServerInfo *serverInfo = NULL;
1637 oc_mutex_lock(g_LEServerListMutex);
1638 if (CA_STATUS_OK == CAGetLEServerInfo(g_LEServerList, remote_address, &serverInfo))
1640 if (serverInfo->status == LE_STATUS_SERVICES_DISCOVERED){
1641 OIC_LOG_V(DEBUG, TAG, "Mtu Size [%d]", serverInfo->mtu_size);
1642 oc_mutex_unlock(g_LEServerListMutex);
1643 OIC_LOG_V(INFO, TAG, "Test: returning Mtu Size [%d]", serverInfo->mtu_size - CA_BLE_MTU_HEADER_SIZE);
1644 return serverInfo->mtu_size - CA_BLE_MTU_HEADER_SIZE;
1647 oc_mutex_unlock(g_LEServerListMutex);
1648 return CA_DEFAULT_BLE_MTU_SIZE;
1651 bool CALEClientIsConnected(const char* address)
1658 CAResult_t CALEClientSendNegotiationMessage(const char* address)
1660 /* MCD : change the log as INFO_PRIVATE for security */
1661 OIC_LOG_V(INFO_PRIVATE, TAG, "CALEClientSendNegotiationMessage(%s)", address);
1663 //it will be implemented when tizen public 3.0 is released.
1664 return CA_NOT_SUPPORTED;