Update snapshot(2017-12-06)
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / tizen / caleclient.c
1 /* ****************************************************************
2 *
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
4 *
5 *
6 *
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
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 *
19 ******************************************************************/
20
21 #include "caleclient.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <arpa/inet.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <pthread.h>
31 #include <gio/gio.h>
32
33 #include "octhread.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"
40
41 /**
42  * Logging tag for module name.
43  */
44 #define TAG "OIC_CA_LE_CLIENT"
45
46 #define MICROSECS_PER_SEC 1000000
47 #define WAIT_TIME_WRITE_CHARACTERISTIC 10 * MICROSECS_PER_SEC
48
49 uint64_t const TIMEOUT = 30 * MICROSECS_PER_SEC;
50
51 /**
52  * Flag to check if multicast is already in progress.
53  */
54 static bool g_isMulticastInProgress = false;
55
56 /**
57  * Pending multicast data list to be sent.
58  */
59 static u_arraylist_t *g_multicastDataList = NULL;
60
61 /**
62  * Mutex to synchronize the access to Pending multicast data list.
63  */
64 static oc_mutex g_multicastDataListMutex = NULL;
65
66 /**
67  * List of devices discovered.
68  */
69 static u_arraylist_t *g_deviceDiscoveredList = NULL;
70
71 /**
72  * Mutex to synchronize the access to discovered devices list.
73  */
74 static oc_mutex g_deviceDiscoveredListMutex = NULL;
75
76 /**
77  * Condition to start the timer for scanning.
78  */
79 static oc_cond g_startTimerCond = NULL;
80
81 /**
82  * Condition for scanning Time interval.
83  */
84 static oc_cond g_scanningTimeCond = NULL;
85
86 /**
87  * This contains the list of OIC services a client connect tot.
88  */
89 static LEServerInfoList *g_LEServerList = NULL;
90
91 /**
92  * Mutex to synchronize access to BleServiceList.
93  */
94 static oc_mutex g_LEServerListMutex = NULL;
95
96 /**
97  * Boolean variable to keep the state of the GATT Client.
98  */
99 static bool g_isLEGattClientStarted = false;
100
101 /**
102  * Mutex to synchronize access to the requestResponse callback to be called
103  * when the data needs to be sent from GATTClient.
104  */
105 static oc_mutex g_LEReqRespClientCbMutex = NULL;
106
107 /**
108  * Mutex to synchronize access to the requestResponse callback to be called
109  * when the data needs to be sent from GATTClient.
110  */
111 static oc_mutex g_LEClientConnectMutex = NULL;
112
113 /**
114  * Mutex to synchronize the calls to be done to the platform from GATTClient
115  * interfaces from different threads.
116  */
117 static oc_mutex g_LEClientStateMutex = NULL;
118
119 /**
120  * Mutex to synchronize the task to be pushed to thread pool.
121  */
122 static oc_mutex g_LEClientThreadPoolMutex = NULL;
123
124 /**
125  * Mutex to synchronize the task to write characteristic one packet after another.
126  */
127 static oc_mutex g_threadWriteCharacteristicMutex = NULL;
128
129 /**
130  * Condition for Writing characteristic.
131  */
132 static oc_cond g_threadWriteCharacteristicCond = NULL;
133
134 /**
135  * Flag to check status of write characteristic.
136  */
137 static bool g_isSignalSetFlag = false;
138
139 /**
140  * Maintains the callback to be notified on receival of network packets from other
141  *           BLE devices
142  */
143 static CABLEDataReceivedCallback g_LEClientDataReceivedCallback = NULL;
144
145 /**
146  * callback to update the error to le adapter
147  */
148 static CABLEErrorHandleCallback g_clientErrorCallback;
149
150 /**
151  * gmainLoop to manage the threads to receive the callback from the platfrom.
152  */
153 static GMainLoop *g_eventLoop = NULL;
154
155 /**
156  * Reference to threadpool
157  */
158 static ca_thread_pool_t g_LEClientThreadPool = NULL;
159
160 bool CALEIsHaveService(bt_adapter_le_device_scan_result_info_s* scanInfo, char* service_uuid)
161 {
162     bool ret = false;
163     char **uuids = NULL;
164     int count = 0;
165     int result = 0;
166
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,
170                                                          &uuids, &count);
171     if (result == BT_ERROR_NONE && NULL != uuids)
172     {
173         int i;
174         for (i = 0; i < count; i++)
175         {
176             if (0 == strcasecmp(uuids[i], service_uuid))
177             {
178                 OIC_LOG_V(DEBUG, TAG, "Service[%s] Found in %s",
179                           uuids[i], scanInfo->remote_address);
180                 ret = true;
181             }
182             OICFree(uuids[i]);
183         }
184         OICFree(uuids);
185     }
186
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,
190                                                          &uuids, &count);
191     if (result == BT_ERROR_NONE && NULL != uuids)
192     {
193         int i;
194         for (i = 0; i < count; i++)
195         {
196             if (0 == strcasecmp(uuids[i], service_uuid))
197             {
198                 OIC_LOG_V(DEBUG, TAG, "Service[%s] Found in %s",
199                           uuids[i], scanInfo->remote_address);
200                 ret = true;
201             }
202             OICFree(uuids[i]);
203         }
204         OICFree(uuids);
205     }
206
207     return ret;
208 }
209
210 bool CALEIsDeviceDiscovered(const char * address)
211 {
212     if (g_deviceDiscoveredList)
213     {
214         oc_mutex_lock(g_deviceDiscoveredListMutex);
215         uint32_t arrayLength = u_arraylist_length(g_deviceDiscoveredList);
216         for (uint32_t i = 0; i < arrayLength; i++)
217         {
218             char *deviceAddr = u_arraylist_get(g_deviceDiscoveredList, i);
219             if (0 == strcasecmp(deviceAddr, address))
220             {
221                 oc_mutex_unlock(g_deviceDiscoveredListMutex);
222                 return true;
223             }
224
225         }
226         oc_mutex_unlock(g_deviceDiscoveredListMutex);
227     }
228     return false;
229 }
230
231 void CALEGattCharacteristicChangedCb(bt_gatt_h characteristic,
232                                      char *value,
233                                      int valueLen, void *userData)
234 {
235     (void)characteristic;
236
237     OIC_LOG(DEBUG, TAG, "IN");
238     OIC_LOG_V(DEBUG, TAG, "Changed characteristic value length [%d]", valueLen);
239
240     oc_mutex_lock(g_LEReqRespClientCbMutex);
241     if (NULL == g_LEClientDataReceivedCallback)
242     {
243         OIC_LOG(ERROR, TAG, "Request response callback is not set");
244         oc_mutex_unlock(g_LEReqRespClientCbMutex);
245         return;
246     }
247
248     uint32_t sentLength = 0;
249     g_LEClientDataReceivedCallback(userData, (uint8_t *)value, valueLen, &sentLength);
250
251     OIC_LOG_V(DEBUG, TAG, "Recv data Length is %d", sentLength);
252
253     oc_mutex_unlock(g_LEReqRespClientCbMutex);
254
255     OIC_LOG(DEBUG, TAG, "OUT");
256 }
257
258 void CALEGattCharacteristicWriteCb(int result, bt_gatt_h reqHandle, void *userData)
259 {
260     (void)reqHandle;
261     (void)userData;
262
263     OIC_LOG(DEBUG, TAG, "IN ");
264
265     if (BT_ERROR_NONE != result)
266     {
267         CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, "", 0, -1,
268                            false, "writeChar failure");
269
270         OIC_LOG(ERROR, TAG, "Write failed Need Retry ");
271         //Need to Implement retry mechanism
272     }
273     else
274     {
275         oc_mutex_lock(g_threadWriteCharacteristicMutex);
276         OIC_LOG(DEBUG, TAG, "g_isSignalSetFlag is set true and signal");
277         g_isSignalSetFlag = true;
278         oc_cond_signal(g_threadWriteCharacteristicCond);
279         oc_mutex_unlock(g_threadWriteCharacteristicMutex);
280
281         CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, "", 0, -1,
282                            true, "writeChar success");
283     }
284
285     OIC_LOG(DEBUG, TAG, "OUT ");
286 }
287
288 void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress)
289 {
290     OIC_LOG(DEBUG, TAG, "IN ");
291
292     VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
293
294     // Start the scanning.
295     CAResult_t ret = CALEGattStartDeviceScanning();
296     if (CA_STATUS_OK != ret)
297     {
298         OIC_LOG(ERROR, TAG, "CALEGattStartDeviceDiscovery Failed");
299     }
300     // Signal the start timer.
301     oc_cond_signal(g_scanningTimeCond);
302
303     if (!connected)
304     {
305         OIC_LOG_V(DEBUG, TAG, "DisConnected from [%s] ", remoteAddress);
306         oc_mutex_lock(g_LEServerListMutex);
307         CARemoveLEServerInfoFromList(&g_LEServerList, remoteAddress);
308         oc_mutex_unlock(g_LEServerListMutex);
309     }
310     else
311     {
312         OIC_LOG_V(DEBUG, TAG, "Connected to [%s] ", remoteAddress);
313
314         char *addr = OICStrdup(remoteAddress);
315         if (NULL == addr)
316         {
317             OIC_LOG(ERROR, TAG, "addr is NULL");
318             return;
319         }
320
321         oc_mutex_lock(g_LEClientThreadPoolMutex);
322         if (NULL == g_LEClientThreadPool)
323         {
324             OIC_LOG(ERROR, TAG, "g_LEClientThreadPool is NULL");
325             OICFree(addr);
326             oc_mutex_unlock(g_LEClientThreadPoolMutex);
327             return;
328         }
329
330         ret = ca_thread_pool_add_task(g_LEClientThreadPool, CADiscoverLEServicesThread,
331                                       addr, NULL);
332         if (CA_STATUS_OK != ret)
333         {
334             OIC_LOG_V(ERROR, TAG, "ca_thread_pool_add_task failed with ret [%d]", ret);
335             OICFree(addr);
336         }
337         oc_mutex_unlock(g_LEClientThreadPoolMutex);
338     }
339     OIC_LOG(DEBUG, TAG, "OUT");
340 }
341
342 void CALEAdapterScanResultCb(int result, bt_adapter_le_device_scan_result_info_s *scanInfo,
343                              void *userData)
344 {
345     (void)userData;
346
347     OIC_LOG(DEBUG, TAG, "IN");
348
349     VERIFY_NON_NULL_VOID(scanInfo, TAG, "scanInfo");
350     VERIFY_NON_NULL_VOID(scanInfo->remote_address, TAG, "scanInfo->remote_address");
351
352     OIC_LOG_V(DEBUG, TAG, "Remote Address [%s]", scanInfo->remote_address);
353     OIC_LOG_V(DEBUG, TAG, "Scan Result [%d]", result);
354     OIC_LOG_V(DEBUG, TAG,
355               " Adv data len [%d] Scan data len[%d]RSSI [%d] Addr_type [%d] ",
356               scanInfo->adv_data_len, scanInfo->scan_data_len, scanInfo->rssi,
357               scanInfo->address_type);
358
359     // Check if device is already discovered.
360     if (CALEIsDeviceDiscovered(scanInfo->remote_address))
361     {
362         OIC_LOG_V(INFO, TAG, "Device[%s] is already discovered", scanInfo->remote_address);
363         return;
364     }
365
366     if (!CALEIsHaveService(scanInfo, CA_GATT_SERVICE_UUID))
367     {
368         oc_mutex_lock(g_deviceDiscoveredListMutex);
369         // Add the the device Discovered list.
370         if (NULL == g_deviceDiscoveredList)
371         {
372             g_deviceDiscoveredList = u_arraylist_create();
373         }
374         char *deviceAddr = OICStrdup(scanInfo->remote_address);
375         if (NULL == deviceAddr)
376         {
377             OIC_LOG_V(ERROR, TAG, "Device address is NULL");
378             oc_mutex_unlock(g_deviceDiscoveredListMutex);
379             return;
380         }
381
382         u_arraylist_add(g_deviceDiscoveredList, (void *) deviceAddr);
383         oc_mutex_unlock(g_deviceDiscoveredListMutex);
384         OIC_LOG_V(INFO, TAG, "Device[%s] is don't have service", scanInfo->remote_address);
385         return;
386     }
387
388     // Stop the scan before invoking bt_gatt_connect().
389     CALEGattStopDeviceScanning();
390
391     size_t len = strlen(scanInfo->remote_address);
392
393     char *addr = (char *)OICMalloc(sizeof(char) * (len + 1));
394     VERIFY_NON_NULL_VOID(addr, TAG, "Malloc failed");
395
396     strncpy(addr, scanInfo->remote_address, len + 1);
397
398     OIC_LOG_V(DEBUG, TAG,
399               "Trying to do Gatt connection to [%s]", addr);
400
401     oc_mutex_lock(g_LEClientThreadPoolMutex);
402     if (NULL == g_LEClientThreadPool)
403     {
404         OIC_LOG(ERROR, TAG, "g_LEClientThreadPool is NULL");
405         OICFree(addr);
406         oc_mutex_unlock(g_LEClientThreadPoolMutex);
407         return;
408     }
409
410     CAResult_t res = ca_thread_pool_add_task(g_LEClientThreadPool, CAGattConnectThread, addr, NULL);
411     if (CA_STATUS_OK != res)
412     {
413         OIC_LOG_V(ERROR, TAG,
414                   "ca_thread_pool_add_task failed with ret [%d]", res);
415         OICFree(addr);
416     }
417     oc_mutex_unlock(g_LEClientThreadPoolMutex);
418     OIC_LOG(DEBUG, TAG, "OUT");
419 }
420
421 void CASetLEClientThreadPoolHandle(ca_thread_pool_t handle)
422 {
423     OIC_LOG(DEBUG, TAG, "IN");
424
425     oc_mutex_lock(g_LEClientThreadPoolMutex);
426     g_LEClientThreadPool = handle;
427     oc_mutex_unlock(g_LEClientThreadPoolMutex);
428
429     OIC_LOG(DEBUG, TAG, "OUT");
430 }
431
432 void CASetLEReqRespClientCallback(CABLEDataReceivedCallback callback)
433 {
434     OIC_LOG(DEBUG, TAG, "IN");
435
436     oc_mutex_lock(g_LEReqRespClientCbMutex);
437
438     g_LEClientDataReceivedCallback = callback;
439
440     oc_mutex_unlock(g_LEReqRespClientCbMutex);
441
442     OIC_LOG(DEBUG, TAG, "OUT");
443 }
444
445 void CASetBLEClientErrorHandleCallback(CABLEErrorHandleCallback callback)
446 {
447     g_clientErrorCallback = callback;
448 }
449
450 CAResult_t CAStartLEGattClient()
451 {
452     OIC_LOG(DEBUG, TAG, "IN");
453
454     oc_mutex_lock(g_LEClientStateMutex);
455     if (true  == g_isLEGattClientStarted)
456     {
457         OIC_LOG(ERROR, TAG, "Gatt Client is already running!!");
458         oc_mutex_unlock(g_LEClientStateMutex);
459         return CA_STATUS_FAILED;
460     }
461
462     CAResult_t  result = CALEGattSetCallbacks();
463     if (CA_STATUS_OK != result)
464     {
465         OIC_LOG(ERROR, TAG, "CABleGattSetCallbacks Failed");
466         oc_mutex_unlock(g_LEClientStateMutex);
467         CATerminateLEGattClient();
468         return CA_STATUS_FAILED;
469     }
470
471     g_isLEGattClientStarted = true;
472     oc_mutex_unlock(g_LEClientStateMutex);
473
474     oc_mutex_lock(g_LEClientThreadPoolMutex);
475     if (NULL == g_LEClientThreadPool)
476     {
477         OIC_LOG(ERROR, TAG, "gBleServerThreadPool is NULL");
478         CATerminateGattClientMutexVariables();
479         oc_mutex_unlock(g_LEClientThreadPoolMutex);
480         return CA_STATUS_FAILED;
481     }
482
483     result = ca_thread_pool_add_task(g_LEClientThreadPool, CAStartTimerThread,
484                                      NULL, NULL);
485     if (CA_STATUS_OK != result)
486     {
487         OIC_LOG(ERROR, TAG, "ca_thread_pool_add_task failed");
488         CATerminateGattClientMutexVariables();
489         oc_mutex_unlock(g_LEClientThreadPoolMutex);
490         return CA_STATUS_FAILED;
491     }
492     oc_mutex_unlock(g_LEClientThreadPoolMutex);
493
494     OIC_LOG(DEBUG, TAG, "OUT");
495     return CA_STATUS_OK;
496 }
497
498 void CAStartTimerThread(void *data)
499 {
500     (void)data;
501
502     OIC_LOG(DEBUG, TAG, "IN");
503     while (g_isLEGattClientStarted)
504     {
505         oc_mutex_lock(g_multicastDataListMutex);
506         if (!g_isMulticastInProgress)
507         {
508             OIC_LOG(DEBUG, TAG, "waiting....");
509             oc_cond_wait(g_startTimerCond, g_multicastDataListMutex);
510             OIC_LOG(DEBUG, TAG, "Wake up");
511             g_isMulticastInProgress = true;
512
513             if (!g_isLEGattClientStarted)
514             {
515                 break;
516             }
517         }
518
519         // Timed conditional wait for stopping the scan.
520         OCWaitResult_t ret = oc_cond_wait_for(g_scanningTimeCond, g_multicastDataListMutex,
521                                               TIMEOUT);
522         if (OC_WAIT_TIMEDOUT == ret)
523         {
524             OIC_LOG(DEBUG, TAG, "Scan is timed Out");
525             // Call stop scan.
526             CALEGattStopDeviceScanning();
527
528             // Clear the data list and device list.
529             u_arraylist_destroy(g_multicastDataList);
530             g_multicastDataList = NULL;
531
532             oc_mutex_lock(g_deviceDiscoveredListMutex);
533             u_arraylist_destroy(g_deviceDiscoveredList);
534             g_deviceDiscoveredList = NULL;
535             oc_mutex_unlock(g_deviceDiscoveredListMutex);
536
537             g_isMulticastInProgress = false;
538         }
539         oc_mutex_unlock(g_multicastDataListMutex);
540     }
541
542     OIC_LOG(DEBUG, TAG, "OUT");
543 }
544
545 void CAStopLEGattClient()
546 {
547     OIC_LOG(DEBUG,  TAG, "IN");
548
549     oc_mutex_lock(g_LEClientStateMutex);
550
551     if (false == g_isLEGattClientStarted)
552     {
553         OIC_LOG(ERROR, TAG, "Gatt Client is not running to stop");
554         oc_mutex_unlock(g_LEClientStateMutex);
555         return;
556     }
557
558     CALEGattUnSetCallbacks();
559
560     CALEGattStopDeviceScanning();
561
562     // this flag should be set before signal g_startTimerCond.
563     // since scan thread can be stopped through this flag.
564     g_isLEGattClientStarted = false;
565
566     // Signal the conditions waiting in Start timer.
567     oc_cond_signal(g_startTimerCond);
568     oc_cond_signal(g_scanningTimeCond);
569
570     // Destroy the multicast data list and device list if not empty.
571     if (NULL != g_multicastDataList)
572     {
573         oc_mutex_lock(g_multicastDataListMutex);
574         u_arraylist_destroy(g_multicastDataList);
575         g_multicastDataList = NULL;
576         oc_mutex_unlock(g_multicastDataListMutex);
577     }
578
579     if (NULL != g_deviceDiscoveredList)
580     {
581         oc_mutex_lock(g_deviceDiscoveredListMutex);
582         u_arraylist_destroy(g_deviceDiscoveredList);
583         g_deviceDiscoveredList = NULL;
584         oc_mutex_unlock(g_deviceDiscoveredListMutex);
585     }
586
587     oc_mutex_lock(g_LEServerListMutex);
588     CAFreeLEServerList(g_LEServerList);
589     g_LEServerList = NULL;
590     oc_mutex_unlock(g_LEServerListMutex);
591
592     oc_mutex_lock(g_threadWriteCharacteristicMutex);
593     oc_cond_signal(g_threadWriteCharacteristicCond);
594     oc_mutex_unlock(g_threadWriteCharacteristicMutex);
595
596     CAResetRegisteredServiceCount();
597
598     GMainContext  *context_event_loop = NULL;
599     // Required for waking up the thread which is running in gmain loop
600     if (NULL != g_eventLoop)
601     {
602         context_event_loop = g_main_loop_get_context(g_eventLoop);
603     }
604     if (context_event_loop)
605     {
606         OIC_LOG_V(DEBUG,  TAG, "g_eventLoop context %x", context_event_loop);
607         g_main_context_wakeup(context_event_loop);
608
609         // Kill g main loops and kill threads.
610         g_main_loop_quit(g_eventLoop);
611     }
612     else
613     {
614         OIC_LOG(ERROR, TAG, "g_eventLoop context is NULL");
615     }
616
617     oc_mutex_unlock(g_LEClientStateMutex);
618
619     OIC_LOG(DEBUG,  TAG, "OUT");
620 }
621
622 CAResult_t CAInitializeLEGattClient()
623 {
624     OIC_LOG(DEBUG, TAG, "Initialize GATT Client");
625     CAResult_t res = CAInitGattClientMutexVariables();
626     if (CA_STATUS_OK != res)
627     {
628         OIC_LOG(ERROR, TAG, "CAInitGattClientMutexVariables failed!");
629         CATerminateGattClientMutexVariables();
630         return CA_STATUS_FAILED;
631     }
632     return res;
633 }
634
635 void CATerminateLEGattClient()
636 {
637     OIC_LOG(DEBUG,  TAG, "IN");
638
639     CATerminateGattClientMutexVariables();
640
641     OIC_LOG(DEBUG,  TAG, "OUT");
642 }
643
644 CAResult_t CAInitGattClientMutexVariables()
645 {
646     OIC_LOG(DEBUG,  TAG, "IN");
647     if (NULL == g_LEClientStateMutex)
648     {
649         g_LEClientStateMutex = oc_mutex_new();
650         if (NULL == g_LEClientStateMutex)
651         {
652             OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
653             return CA_STATUS_FAILED;
654         }
655     }
656
657     if (NULL == g_LEServerListMutex)
658     {
659         g_LEServerListMutex = oc_mutex_new();
660         if (NULL == g_LEServerListMutex)
661         {
662             OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
663             return CA_STATUS_FAILED;
664         }
665     }
666
667     if (NULL == g_LEReqRespClientCbMutex)
668     {
669         g_LEReqRespClientCbMutex = oc_mutex_new();
670         if (NULL == g_LEReqRespClientCbMutex)
671         {
672             OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
673             return CA_STATUS_FAILED;
674         }
675     }
676
677     if (NULL == g_LEClientThreadPoolMutex)
678     {
679         g_LEClientThreadPoolMutex = oc_mutex_new();
680         if (NULL == g_LEClientThreadPoolMutex)
681         {
682             OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
683             return CA_STATUS_FAILED;
684         }
685     }
686
687     if (NULL == g_LEClientConnectMutex)
688     {
689         g_LEClientConnectMutex = oc_mutex_new();
690         if (NULL == g_LEClientConnectMutex)
691         {
692             OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
693             return CA_STATUS_FAILED;
694         }
695     }
696
697     if (NULL == g_multicastDataListMutex)
698     {
699         g_multicastDataListMutex = oc_mutex_new();
700         if (NULL == g_multicastDataListMutex)
701         {
702             OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
703             return CA_STATUS_FAILED;
704         }
705     }
706
707     if (NULL == g_deviceDiscoveredListMutex)
708     {
709         g_deviceDiscoveredListMutex = oc_mutex_new();
710         if (NULL == g_deviceDiscoveredListMutex)
711         {
712             OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
713             return CA_STATUS_FAILED;
714         }
715     }
716
717     if (NULL == g_threadWriteCharacteristicMutex)
718     {
719         g_threadWriteCharacteristicMutex = oc_mutex_new();
720         if (NULL == g_threadWriteCharacteristicMutex)
721         {
722             OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
723             return CA_STATUS_FAILED;
724         }
725     }
726
727     if (NULL == g_startTimerCond)
728     {
729         g_startTimerCond = oc_cond_new();
730         if (NULL == g_startTimerCond)
731         {
732             OIC_LOG(ERROR, TAG, "oc_cond_new failed");
733             return CA_STATUS_FAILED;
734         }
735     }
736
737     if (NULL == g_scanningTimeCond)
738     {
739         g_scanningTimeCond = oc_cond_new();
740         if (NULL == g_scanningTimeCond)
741         {
742             OIC_LOG(ERROR, TAG, "oc_cond_new failed");
743             return CA_STATUS_FAILED;
744         }
745     }
746
747     if (NULL == g_threadWriteCharacteristicCond)
748     {
749         g_threadWriteCharacteristicCond = oc_cond_new();
750         if (NULL == g_threadWriteCharacteristicCond)
751         {
752             OIC_LOG(ERROR, TAG, "oc_cond_new failed");
753             return CA_STATUS_FAILED;
754         }
755     }
756
757     OIC_LOG(DEBUG,  TAG, "OUT");
758     return CA_STATUS_OK;
759 }
760
761 void CATerminateGattClientMutexVariables()
762 {
763     OIC_LOG(DEBUG,  TAG, "IN");
764
765     oc_mutex_free(g_LEClientStateMutex);
766     g_LEClientStateMutex = NULL;
767
768     oc_mutex_free(g_LEServerListMutex);
769     g_LEServerListMutex = NULL;
770
771     oc_mutex_free(g_LEReqRespClientCbMutex);
772     g_LEReqRespClientCbMutex = NULL;
773
774     oc_mutex_free(g_LEClientConnectMutex);
775     g_LEClientConnectMutex = NULL;
776
777     oc_mutex_free(g_LEClientThreadPoolMutex);
778     g_LEClientThreadPoolMutex = NULL;
779
780     oc_mutex_free(g_multicastDataListMutex);
781     g_multicastDataListMutex = NULL;
782
783     oc_mutex_free(g_deviceDiscoveredListMutex);
784     g_deviceDiscoveredListMutex = NULL;
785
786     oc_mutex_free(g_threadWriteCharacteristicMutex);
787     g_threadWriteCharacteristicMutex = NULL;
788
789     oc_cond_free(g_startTimerCond);
790     g_startTimerCond = NULL;
791
792     oc_cond_free(g_scanningTimeCond);
793     g_scanningTimeCond = NULL;
794
795     oc_cond_free(g_threadWriteCharacteristicCond);
796     g_threadWriteCharacteristicCond = NULL;
797     g_isSignalSetFlag = false;
798
799     OIC_LOG(DEBUG,  TAG, "OUT");
800 }
801
802 CAResult_t CALEGattSetCallbacks()
803 {
804     OIC_LOG(DEBUG, TAG, "IN");
805
806     OIC_LOG(DEBUG, TAG, "OUT");
807     return CA_STATUS_OK;
808 }
809
810 void CALEGattUnSetCallbacks()
811 {
812     OIC_LOG(DEBUG, TAG, "IN");
813
814     bt_gatt_unset_connection_state_changed_cb();
815
816     int numOfServersConnected = CAGetRegisteredServiceCount();
817     LEServerInfo *leServerInfo = NULL;
818
819     for (int32_t index = 0; index < numOfServersConnected; index++)
820     {
821         CAGetLEServerInfoByPosition(g_LEServerList, index, &leServerInfo);
822         bt_gatt_client_unset_characteristic_value_changed_cb(leServerInfo->readChar);
823     }
824     OIC_LOG(DEBUG, TAG, "OUT");
825 }
826
827 CAResult_t CALEGattStartDeviceScanning()
828 {
829     OIC_LOG(DEBUG, TAG, "IN");
830
831     int ret = bt_adapter_le_start_scan(CALEAdapterScanResultCb, NULL);
832     if(BT_ERROR_NONE != ret)
833     {
834         OIC_LOG_V(ERROR, TAG, "bt_adapter_le_start_scan failed[%s]",
835                   CALEGetErrorMsg(ret));
836         return CA_STATUS_FAILED;
837     }
838
839     OIC_LOG(DEBUG, TAG, "OUT");
840     return CA_STATUS_OK;
841 }
842
843 void CALEGattStopDeviceScanning()
844 {
845     OIC_LOG(DEBUG, TAG, "IN");
846
847     int ret = bt_adapter_le_stop_scan();
848     if (BT_ERROR_NONE != ret)
849     {
850         OIC_LOG_V(ERROR, TAG, "bt_adapter_le_stop_scan failed[%s]",
851                   CALEGetErrorMsg(ret));
852     }
853
854     OIC_LOG(DEBUG, TAG, "OUT");
855 }
856
857 void CAGattConnectThread (void *remoteAddress)
858 {
859     OIC_LOG(DEBUG, TAG, "IN ");
860
861     VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
862
863     char *address  = (char *)remoteAddress;
864
865     OIC_LOG_V(DEBUG, TAG, "remote address is [%s]", address);
866
867     CAResult_t result = CALEGattConnect(address);
868
869     if (CA_STATUS_OK != result)
870     {
871         OIC_LOG_V(ERROR, TAG, "bt_gatt_connect failed for [%s]", address);
872     }
873
874     OICFree(address);
875
876     OIC_LOG(DEBUG, TAG, "OUT");
877 }
878
879 CAResult_t CALEGattConnect(const char *remoteAddress)
880 {
881     OIC_LOG(DEBUG, TAG, "IN");
882
883     VERIFY_NON_NULL_RET(remoteAddress, TAG,
884                         "remote address is NULL", CA_STATUS_FAILED);
885
886     oc_mutex_lock(g_LEClientConnectMutex);
887     bool isConnected = false;
888     int ret = bt_device_is_profile_connected(remoteAddress, BT_PROFILE_GATT, &isConnected);
889     if (BT_ERROR_NONE != ret)
890     {
891         OIC_LOG_V(ERROR, TAG, "bt_device_is_profile_connected Failed with ret value [%s] ",
892                   CALEGetErrorMsg(ret));
893         oc_mutex_unlock(g_LEClientConnectMutex);
894         return CA_STATUS_FAILED;
895     }
896
897     CAResult_t result = CA_STATUS_OK;
898     if (!isConnected)
899     {
900         ret = bt_gatt_connect(remoteAddress, true);
901
902         if (BT_ERROR_NONE != ret)
903         {
904             OIC_LOG_V(ERROR, TAG, "bt_gatt_connect Failed with ret value [%s] ",
905                       CALEGetErrorMsg(ret));
906             oc_mutex_unlock(g_LEClientConnectMutex);
907             return CA_STATUS_FAILED;
908         }
909     }
910     else
911     {
912         OIC_LOG_V(INFO, TAG, "Remote address[%s] is already connected",
913                   remoteAddress);
914         char *addr = OICStrdup(remoteAddress);
915         if (NULL == addr)
916         {
917             OIC_LOG(ERROR, TAG, "addr is NULL");
918             oc_mutex_unlock(g_LEClientConnectMutex);
919             return CA_STATUS_FAILED;
920         }
921
922         oc_mutex_lock(g_LEClientThreadPoolMutex);
923         if (NULL == g_LEClientThreadPool)
924         {
925             OIC_LOG(ERROR, TAG, "g_LEClientThreadPool is NULL");
926             OICFree(addr);
927             oc_mutex_unlock(g_LEClientThreadPoolMutex);
928             oc_mutex_unlock(g_LEClientConnectMutex);
929             return CA_STATUS_FAILED;
930         }
931
932         result = ca_thread_pool_add_task(g_LEClientThreadPool, CADiscoverLEServicesThread,
933                                          addr, NULL);
934         if (CA_STATUS_OK != result)
935         {
936             OIC_LOG_V(ERROR, TAG, "ca_thread_pool_add_task failed with ret [%d]", result);
937             OICFree(addr);
938         }
939         oc_mutex_unlock(g_LEClientThreadPoolMutex);
940     }
941     oc_mutex_unlock(g_LEClientConnectMutex);
942
943     OIC_LOG(DEBUG, TAG, "OUT");
944     return result;
945 }
946
947 CAResult_t CALEGattDisConnect(const char *remoteAddress)
948 {
949     OIC_LOG(DEBUG, TAG, "IN");
950
951     VERIFY_NON_NULL_RET(remoteAddress, TAG,
952                         "remote address is NULL", CA_STATUS_FAILED);
953
954     int ret = bt_gatt_disconnect(remoteAddress);
955
956     if (BT_ERROR_NONE != ret)
957     {
958         OIC_LOG_V(ERROR, TAG, "bt_gatt_disconnect Failed with ret value [%d] ",
959                   CALEGetErrorMsg(ret));
960         return CA_STATUS_FAILED;
961     }
962
963     OIC_LOG(DEBUG, TAG, "OUT");
964     return CA_STATUS_OK;
965 }
966
967 void CADiscoverLEServicesThread (void *remoteAddress)
968 {
969     OIC_LOG(DEBUG, TAG, "IN");
970
971     VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
972
973     char *address  = (char *)remoteAddress;
974
975     CAResult_t result = CALEGattDiscoverServices(address);
976     if (CA_STATUS_OK != result)
977     {
978         OIC_LOG(ERROR, TAG, "CALEGattDiscoverServices failed");
979     }
980
981     OICFree(address);
982     OIC_LOG(DEBUG, TAG, "OUT");
983 }
984
985 CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
986 {
987     OIC_LOG(DEBUG, TAG, "IN");
988
989     VERIFY_NON_NULL_RET(remoteAddress, TAG,
990                         "remote address is NULL", CA_STATUS_FAILED);
991
992     LEServerInfo *leServerInfo = NULL;
993     CAResult_t result =  CA_STATUS_FAILED;
994
995     oc_mutex_lock(g_LEServerListMutex);
996     result = CAGetLEServerInfo(g_LEServerList, remoteAddress, &leServerInfo);
997     oc_mutex_unlock(g_LEServerListMutex);
998
999     if (CA_STATUS_OK == result)
1000     {
1001         OIC_LOG_V(INFO, TAG, "Device[%s] is already discovered", leServerInfo->remoteAddress);
1002
1003         // Send the data of pending multicast data list if any.
1004         if (g_multicastDataList)
1005         {
1006             oc_mutex_lock(g_multicastDataListMutex);
1007             uint32_t arrayLength = u_arraylist_length(g_multicastDataList);
1008             for (uint32_t i = 0; i < arrayLength; i++)
1009             {
1010                 CALEData_t *multicastData = u_arraylist_get(g_multicastDataList, i);
1011                 if (NULL == multicastData)
1012                 {
1013                     OIC_LOG(DEBUG, TAG, "multicastData is NULL");
1014                     continue;
1015                 }
1016                 CAUpdateCharacteristicsToGattServer(remoteAddress, multicastData->data,
1017                                                     multicastData->dataLen, LE_UNICAST, 0);
1018             }
1019             oc_mutex_unlock(g_multicastDataListMutex);
1020         }
1021
1022         OIC_LOG(DEBUG, TAG, "OUT");
1023         return CA_STATUS_OK;
1024     }
1025     else
1026     {
1027         OIC_LOG_V(INFO, TAG, "CAGetLEServerInfo [%s] is failed, %d", remoteAddress, result);
1028     }
1029
1030     bt_gatt_client_h clientHandle = NULL;
1031     int32_t ret = bt_gatt_client_create(remoteAddress, &clientHandle);
1032     if (BT_ERROR_NONE != ret || NULL == clientHandle)
1033     {
1034         OIC_LOG_V(ERROR, TAG,
1035                   "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret));
1036         return CA_STATUS_FAILED;
1037     }
1038
1039     bt_gatt_h serviceHandle = NULL;
1040     ret = bt_gatt_client_get_service(clientHandle, CA_GATT_SERVICE_UUID, &serviceHandle);
1041     if (BT_ERROR_NONE != ret || NULL == serviceHandle)
1042     {
1043         OIC_LOG_V(ERROR, TAG,
1044                   "bt_gatt_client_get_service Failed with ret value [%s] ", CALEGetErrorMsg(ret));
1045         bt_gatt_client_destroy(clientHandle);
1046         return CA_STATUS_FAILED;
1047     }
1048
1049     // Server will read data on this characteristic.
1050     bt_gatt_h writeChrHandle = NULL;
1051     ret = bt_gatt_service_get_characteristic(serviceHandle, CA_GATT_REQUEST_CHRC_UUID,
1052                                              &writeChrHandle);
1053     if (BT_ERROR_NONE != ret || NULL == writeChrHandle)
1054     {
1055         OIC_LOG_V(ERROR, TAG,
1056                   "bt_gatt_service_get_characteristic Failed with ret value [%s] ",
1057                   CALEGetErrorMsg(ret));
1058         bt_gatt_client_destroy(clientHandle);
1059         CALEGattDisConnect(remoteAddress);
1060         return CA_STATUS_FAILED;
1061     }
1062
1063     // Server will notify data on this characteristic.
1064     bt_gatt_h readChrHandle = NULL;
1065     ret = bt_gatt_service_get_characteristic(serviceHandle, CA_GATT_RESPONSE_CHRC_UUID,
1066                                              &readChrHandle);
1067     if (BT_ERROR_NONE != ret || NULL == readChrHandle)
1068     {
1069         OIC_LOG_V(ERROR, TAG,
1070                   "bt_gatt_service_get_characteristic Failed with ret value [%s] ",
1071                   CALEGetErrorMsg(ret));
1072         bt_gatt_client_destroy(clientHandle);
1073         CALEGattDisConnect(remoteAddress);
1074         return CA_STATUS_FAILED;
1075     }
1076
1077
1078     //TODO: This data has to be freed while unsetting the callback.
1079     char *addr = OICStrdup(remoteAddress);
1080     if (NULL == addr)
1081     {
1082         OIC_LOG(ERROR, TAG, "addr is NULL");
1083         bt_gatt_client_destroy(clientHandle);
1084         CALEGattDisConnect(remoteAddress);
1085         return CA_STATUS_FAILED;
1086     }
1087
1088     ret = bt_gatt_client_set_characteristic_value_changed_cb(readChrHandle,
1089                                                              CALEGattCharacteristicChangedCb,
1090                                                              (void *)addr);
1091     if (BT_ERROR_NONE != ret)
1092     {
1093         OIC_LOG_V(ERROR, TAG,
1094                   "bt_gatt_client_set_characteristic_value_changed_cb Failed with ret value [%s]",
1095                   CALEGetErrorMsg(ret));
1096         bt_gatt_client_destroy(clientHandle);
1097         CALEGattDisConnect(remoteAddress);
1098         return CA_STATUS_FAILED;
1099     }
1100
1101     LEServerInfo *serverInfo = (LEServerInfo *)OICCalloc(1, sizeof(LEServerInfo));
1102     if (NULL == serverInfo)
1103     {
1104         OIC_LOG(ERROR, TAG, "Malloc failed");
1105         CALEGattDisConnect(remoteAddress);
1106         return CA_MEMORY_ALLOC_FAILED;
1107     }
1108     serverInfo->clientHandle = clientHandle;
1109     serverInfo->serviceHandle = serviceHandle;
1110     serverInfo->readChar = readChrHandle;
1111     serverInfo->writeChar = writeChrHandle;
1112     serverInfo->remoteAddress = OICStrdup(remoteAddress);
1113
1114     oc_mutex_lock(g_LEServerListMutex);
1115     result = CAAddLEServerInfoToList(&g_LEServerList, serverInfo);
1116     if (CA_STATUS_OK != result)
1117     {
1118         OIC_LOG(ERROR, TAG, "CAAddLEServerInfoToList failed");
1119         bt_gatt_client_destroy(clientHandle);
1120         CALEGattDisConnect(remoteAddress);
1121         return CA_STATUS_FAILED;
1122     }
1123     oc_mutex_unlock(g_LEServerListMutex);
1124
1125     oc_mutex_lock(g_deviceDiscoveredListMutex);
1126     // Add the the device Discovered list.
1127     if (NULL == g_deviceDiscoveredList)
1128     {
1129         g_deviceDiscoveredList = u_arraylist_create();
1130     }
1131     char *deviceAddr = OICStrdup(remoteAddress);
1132     if (NULL == deviceAddr)
1133     {
1134         OIC_LOG_V(ERROR, TAG, "Device address is NULL");
1135         oc_mutex_unlock(g_deviceDiscoveredListMutex);
1136         return CA_STATUS_FAILED;
1137     }
1138     u_arraylist_add(g_deviceDiscoveredList, (void *) deviceAddr);
1139     oc_mutex_unlock(g_deviceDiscoveredListMutex);
1140
1141     // Send the data of pending multicast data list if any.
1142     if (g_multicastDataList)
1143     {
1144         oc_mutex_lock(g_multicastDataListMutex);
1145         uint32_t arrayLength = u_arraylist_length(g_multicastDataList);
1146         for (uint32_t i = 0; i < arrayLength; i++)
1147         {
1148             CALEData_t *multicastData = u_arraylist_get(g_multicastDataList, i);
1149             if (NULL == multicastData)
1150             {
1151                 OIC_LOG(DEBUG, TAG, "multicastData is NULL");
1152                 continue;
1153             }
1154             CAUpdateCharacteristicsToGattServer(remoteAddress, multicastData->data,
1155                                                 multicastData->dataLen, LE_UNICAST, 0);
1156         }
1157         oc_mutex_unlock(g_multicastDataListMutex);
1158     }
1159
1160     OIC_LOG(DEBUG, TAG, "OUT");
1161     return CA_STATUS_OK;
1162 }
1163
1164 CAResult_t  CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
1165                                                 const uint8_t *data, const uint32_t dataLen,
1166                                                 CALETransferType_t type, const int32_t position)
1167 {
1168     OIC_LOG(DEBUG, TAG, "IN");
1169
1170     VERIFY_NON_NULL(data, TAG, "data is NULL");
1171
1172     if (0 >= dataLen)
1173     {
1174         OIC_LOG(ERROR, TAG, "dataLen is less than or equal zero. Invalid input!");
1175         return CA_STATUS_INVALID_PARAM;
1176     }
1177
1178     LEServerInfo *leServerInfo = NULL;
1179     CAResult_t ret =  CA_STATUS_FAILED;
1180
1181     oc_mutex_lock(g_LEServerListMutex);
1182     if (LE_UNICAST == type)
1183     {
1184         ret = CAGetLEServerInfo(g_LEServerList, remoteAddress, &leServerInfo);
1185     }
1186     else if (LE_MULTICAST == type)
1187     {
1188         ret = CAGetLEServerInfoByPosition(g_LEServerList, position, &leServerInfo);
1189     }
1190     oc_mutex_unlock(g_LEServerListMutex);
1191
1192     if (CA_STATUS_OK != ret)
1193     {
1194         OIC_LOG(ERROR, TAG, "CAGetBLEServiceInfoByPosition is failed");
1195         return CA_STATUS_FAILED;
1196     }
1197
1198     VERIFY_NON_NULL(leServerInfo, TAG, "bleServiceInfo is NULL");
1199
1200     OIC_LOG_V(DEBUG, TAG, "Updating the data of length [%d] to [%s] ", dataLen,
1201               leServerInfo->remoteAddress);
1202
1203     int result = bt_gatt_set_value(leServerInfo->writeChar, (char *)data, dataLen);
1204
1205     if (BT_ERROR_NONE != result)
1206     {
1207         OIC_LOG_V(ERROR, TAG,
1208                   "bt_gatt_set_value Failed with return val [%s]",
1209                   CALEGetErrorMsg(result));
1210         return CA_STATUS_FAILED;
1211     }
1212
1213     result = bt_gatt_client_write_value(leServerInfo->writeChar, CALEGattCharacteristicWriteCb,
1214                                         NULL);
1215     if (BT_ERROR_NONE != result)
1216     {
1217         OIC_LOG_V(ERROR, TAG,
1218                   "bt_gatt_client_write_value Failed with return val [%s]",
1219                   CALEGetErrorMsg(result));
1220         return CA_STATUS_FAILED;
1221     }
1222
1223     // wait for callback for write Characteristic with success to sent data
1224     OIC_LOG_V(DEBUG, TAG, "callback flag is %d", g_isSignalSetFlag);
1225     oc_mutex_lock(g_threadWriteCharacteristicMutex);
1226     if (!g_isSignalSetFlag)
1227     {
1228         OIC_LOG(DEBUG, TAG, "wait for callback to notify writeCharacteristic is success");
1229         if (OC_WAIT_SUCCESS != oc_cond_wait_for(g_threadWriteCharacteristicCond,
1230                                   g_threadWriteCharacteristicMutex,
1231                                   WAIT_TIME_WRITE_CHARACTERISTIC))
1232         {
1233             OIC_LOG(ERROR, TAG, "there is no response. write has failed");
1234             g_isSignalSetFlag = false;
1235             oc_mutex_unlock(g_threadWriteCharacteristicMutex);
1236             return CA_SEND_FAILED;
1237         }
1238     }
1239     // reset flag set by writeCharacteristic Callback
1240     g_isSignalSetFlag = false;
1241     oc_mutex_unlock(g_threadWriteCharacteristicMutex);
1242
1243     OIC_LOG(DEBUG, TAG, "OUT");
1244     return CA_STATUS_OK;
1245 }
1246
1247 CAResult_t CAUpdateCharacteristicsToAllGattServers(const uint8_t *data, uint32_t dataLen)
1248 {
1249     OIC_LOG(DEBUG,  TAG, "IN");
1250
1251     VERIFY_NON_NULL(data, TAG, "data is NULL");
1252
1253     if (0 >= dataLen)
1254     {
1255         OIC_LOG(ERROR, TAG, "dataLen is less than or equal zero. Invalid input !");
1256         return CA_STATUS_INVALID_PARAM;
1257     }
1258
1259     int numOfServersConnected = CAGetRegisteredServiceCount();
1260
1261     // Send data to already connected devices.
1262     for (int32_t pos = 0; pos < numOfServersConnected; pos++)
1263     {
1264         /*remoteAddress will be NULL.
1265           Since we have to send to all destinations. pos will be used for getting remote address.
1266          */
1267         int32_t ret = CAUpdateCharacteristicsToGattServer(NULL, data, dataLen, LE_MULTICAST, pos);
1268
1269         if (CA_STATUS_OK != ret)
1270         {
1271             OIC_LOG_V(ERROR, TAG,
1272                       "CAUpdateCharacteristicsToGattServer Failed with return val [%d] ", ret);
1273             g_clientErrorCallback(NULL, data, dataLen, ret);
1274         }
1275     }
1276
1277     // Add the data to pending list.
1278     CALEData_t *multicastData = (CALEData_t *)OICCalloc(1, sizeof(CALEData_t));
1279     if (NULL == multicastData)
1280     {
1281         OIC_LOG(ERROR, TAG, "Calloc failed");
1282         goto exit;
1283     }
1284     multicastData->data = OICCalloc(1, dataLen);
1285     if (NULL == multicastData->data)
1286     {
1287         OIC_LOG(ERROR, TAG, "Calloc failed");
1288         goto exit;
1289     }
1290     memcpy(multicastData->data, data, dataLen);
1291     multicastData->dataLen = dataLen;
1292
1293     oc_mutex_lock(g_multicastDataListMutex);
1294     if (NULL == g_multicastDataList)
1295     {
1296         g_multicastDataList = u_arraylist_create();
1297     }
1298     u_arraylist_add(g_multicastDataList, (void *)multicastData);
1299     oc_mutex_unlock(g_multicastDataListMutex);
1300
1301     // Start the scanning.
1302     CAResult_t result = CALEGattStartDeviceScanning();
1303     if (CA_STATUS_OK != result)
1304     {
1305         OIC_LOG(ERROR, TAG, "CALEGattStartDeviceDiscovery Failed");
1306         goto exit;
1307     }
1308
1309     // Start the timer by signalling it.
1310     oc_cond_signal(g_startTimerCond);
1311
1312 exit:
1313     OIC_LOG(DEBUG, TAG, "OUT ");
1314     return CA_STATUS_OK;
1315 }
1316
1317 bool CALEClientIsConnected(const char* address)
1318 {
1319     (void)address;
1320     //@Todo
1321     return true;
1322 }
1323
1324 uint16_t CALEClientGetMtuSize(const char* address)
1325 {
1326     VERIFY_NON_NULL_RET(address, TAG, "address is null", CA_DEFAULT_BLE_MTU_SIZE);
1327     //@Todo
1328     //it should be implemented after update Tizen 3.0
1329     return CA_DEFAULT_BLE_MTU_SIZE;
1330 }
1331
1332 CAResult_t CALEClientSetMtuSize(const char* address, uint16_t mtuSize)
1333 {
1334     (void)mtuSize;
1335
1336     VERIFY_NON_NULL(address, TAG, "address is null");
1337     //@Todo
1338     //it should be implemented after update Tizen 3.0
1339     return CA_NOT_SUPPORTED;
1340 }
1341
1342 CAResult_t CALEClientSendNegotiationMessage(const char* address)
1343 {
1344     OIC_LOG_V(DEBUG, TAG, "CALEClientSendNegotiationMessage(%s)", address);
1345     //@Todo
1346     //it will be implemented when tizen public 3.0 is released.
1347     return CA_NOT_SUPPORTED;
1348 }