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