Resolved compilation issues related to pthread changes
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / caleadapter.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 #include "caleadapter.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include "caleinterface.h"
26 #include "cacommon.h"
27 #include "camutex.h"
28 #include "caadapterutils.h"
29 #include "caqueueingthread.h"
30 #include "camsgparser.h"
31 #include "oic_malloc.h"
32
33 /**
34  * @var CALEADAPTER_TAG
35  * @brief Logging tag for module name.
36  */
37 #define CALEADAPTER_TAG "CA_BLE_ADAPTER"
38
39 /**
40  * @var g_networkCallback
41  * @brief Callback to provide the status of the network change to CA layer.
42  */
43 static CANetworkChangeCallback g_networkCallback = NULL;
44
45 /**
46  * @var g_localBLEAddress
47  * @brief bleAddress of the local adapter. Value will be initialized to zero, and will be updated later.
48  */
49 static char g_localBLEAddress[16] = {0};
50
51 /**
52  * @var g_isServer
53  * @brief Variable to differentiate btw GattServer and GattClient.
54  */
55 static bool g_isServer = false;
56
57 /**
58  * @var g_bleIsServerMutex
59  * @brief Mutex to synchronize the task to be executed on the GattServer function calls.
60  */
61 static ca_mutex g_bleIsServerMutex = NULL;
62
63 /**
64  * @var g_bleNetworkCbMutex
65  * @brief Mutex to synchronize the callback to be called for the network changes.
66  */
67 static ca_mutex g_bleNetworkCbMutex = NULL;
68
69 /**
70  * @var g_bleLocalAddressMutex
71  * @brief Mutex to synchronize the updation of the local LE address of the adapter.
72  */
73 static ca_mutex g_bleLocalAddressMutex = NULL;
74
75 /**
76  * @var g_bleAdapterThreadPool
77  * @brief reference to threadpool
78  */
79 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
80
81 /**
82  * @var g_bleAdapterThreadPoolMutex
83  * @brief Mutex to synchronize the task to be pushed to thread pool.
84  */
85 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
86
87 /**
88  * @var g_bleClientSendQueueHandle
89  * @brief Queue to process the outgoing packets from GATTClient.
90  */
91 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
92
93 /**
94  * @var g_bleClientReceiverQueue
95  * @brief Queue to process the incoming packets to GATT Client.
96  */
97 static CAQueueingThread_t *g_bleClientReceiverQueue = NULL;
98
99 /**
100  * @var g_bleClientSendDataMutex
101  * @brief Mutex to synchronize the queing of the data from SenderQueue.
102  */
103 static ca_mutex g_bleClientSendDataMutex = NULL;
104
105 /**
106  * @var g_bleClientReceiveDataMutex
107  * @brief Mutex to synchronize the queing of the data from ReceiverQueue.
108  */
109 static ca_mutex g_bleClientReceiveDataMutex = NULL;
110
111 /**
112  * @var g_dataReceiverHandlerState
113  * @brief Stop condition of recvhandler.
114  */
115 static bool g_dataReceiverHandlerState = false;
116
117 /**
118  * @var g_sendQueueHandle
119  * @brief Queue to process the outgoing packets from GATTServer.
120  */
121 static CAQueueingThread_t *g_sendQueueHandle = NULL;
122
123 /**
124  * @var g_bleServerReceiverQueue
125  * @brief Queue to process the incoming packets to GATTServer
126  */
127 static CAQueueingThread_t *g_bleServerReceiverQueue = NULL;
128
129 /**
130  * @var g_bleServerSendDataMutex
131  * @brief Mutex to synchronize the queing of the data from SenderQueue.
132  */
133 static ca_mutex g_bleServerSendDataMutex = NULL;
134
135 /**
136  * @var g_bleServerReceiveDataMutex
137  * @brief Mutex to synchronize the queing of the data from ReceiverQueue.
138  */
139 static ca_mutex g_bleServerReceiveDataMutex = NULL;
140
141 /**
142  * @var g_bleAdapterReqRespCbMutex
143  * @brief Mutex to synchronize the callback to be called for the adapterReqResponse.
144  */
145 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
146
147 /**
148  * @var g_networkPacketReceivedCallback
149  * @brief Callback to be called when network packet recieved from either GattServer or GattClient.
150  */
151 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
152
153 /**
154  * @ENUM CALeServerStatus
155  * @brief status of BLE Server Status
156  *  This ENUM provides information of LE Adapter Server status
157  */
158 typedef enum
159 {
160     CA_SERVER_NOTSTARTED = 0,
161     CA_LISTENING_SERVER,
162     CA_DISCOVERY_SERVER
163 } CALeServerStatus;
164
165 /**
166  * @var gLeServerStatus
167  * @brief structure to maintain the status of the server.
168  */
169 static CALeServerStatus gLeServerStatus = CA_SERVER_NOTSTARTED;
170
171 /**
172 * @fn  CALERegisterNetworkNotifications
173 * @brief  This function is used to register network change notification callback.
174 *
175 * @param[in]  netCallback CANetworkChangeCallback callback which will be set for the change in nwk.
176 *
177 * @return  0 on success otherwise a positive error value.
178 * @retval  CA_STATUS_OK  Successful
179 * @retval  CA_STATUS_INVALID_PARAM  Invalid input argumets
180 * @retval  CA_STATUS_FAILED Operation failed
181 *
182 */
183 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
184
185 /**
186 * @fn  CASetBleAdapterThreadPoolHandle
187 * @brief  Used to Set the gThreadPool handle which is required for spawning new thread.
188 *
189 * @param[in] handle - Thread pool handle which is given by above layer for using thread creation task.
190 *
191 * @return  void
192 *
193 */
194 void CASetBleAdapterThreadPoolHandle(ca_thread_pool_t handle);
195
196 /**
197 * @fn  CALEDeviceStateChangedCb
198 * @brief  This function is used to call the callback to the upper layer when the device state gets
199 *         changed.
200 *
201 * @param[in]  adapter_state New state of the adapter to be notified to the upper layer.
202 *
203 * @return  None.
204 *
205 */
206 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state);
207
208 /**
209 * @fn  CAInitBleAdapterMutex
210 * @brief  Used to initialize all required mutex variable for LE Adapter implementation.
211 *
212 * @return  0 on success otherwise a positive error value.
213 * @retval  CA_STATUS_OK  Successful
214 * @retval  CA_STATUS_INVALID_PARAM  Invalid input argumets
215 * @retval  CA_STATUS_FAILED Operation failed
216 *
217 */
218 CAResult_t CAInitBleAdapterMutex();
219
220 /**
221 * @fn  CATerminateBleAdapterMutex
222 * @brief  Used to terminate all required mutex variable for LE adapter implementation.
223 *
224 * @return  void
225 */
226 void CATerminateBleAdapterMutex();
227
228 /**
229 * @fn  CALEDataDestroyer
230 * @brief  Used to free data
231 *
232 * @return  void
233 */
234 static void CALEDataDestroyer(void *data, uint32_t size);
235
236 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
237                           CANetworkPacketReceivedCallback reqRespCallback,
238                           CANetworkChangeCallback netCallback,
239                           ca_thread_pool_t handle)
240 {
241     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
242
243     //Input validation
244     VERIFY_NON_NULL(registerCallback, NULL, "RegisterConnectivity callback is null");
245     VERIFY_NON_NULL(reqRespCallback, NULL, "PacketReceived Callback is null");
246     VERIFY_NON_NULL(netCallback, NULL, "NetworkChange Callback is null");
247
248     CAResult_t result = CAInitBleAdapterMutex();
249     if (CA_STATUS_OK != result)
250     {
251         OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
252         return CA_STATUS_FAILED;
253     }
254
255     result = CAInitializeLENetworkMonitor();
256     if (CA_STATUS_OK != result)
257     {
258         OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
259         return CA_STATUS_FAILED;
260     }
261
262     CAInitializeLEAdapter();
263
264     CASetBleClientThreadPoolHandle(handle);
265     CASetBleServerThreadPoolHandle(handle);
266     CASetBleAdapterThreadPoolHandle(handle);
267     CASetBLEReqRespServerCallback(CABLEServerReceivedData);
268     CASetBLEReqRespClientCallback(CABLEClientReceivedData);
269     CASetBLEReqRespAdapterCallback(reqRespCallback);
270
271     CALERegisterNetworkNotifications(netCallback);
272
273     CAConnectivityHandler_t connHandler;
274     connHandler.startAdapter = CAStartLE;
275     connHandler.stopAdapter = CAStopLE;
276     connHandler.startListenServer = CAStartLEListeningServer;
277     connHandler.startDiscoveryServer = CAStartLEDiscoveryServer;
278     connHandler.sendData = CASendLEUnicastData;
279     connHandler.sendDataToAll = CASendLEMulticastData;
280     connHandler.GetnetInfo = CAGetLEInterfaceInformation;
281     connHandler.readData = CAReadLEData;
282     connHandler.terminate = CATerminateLE;
283     registerCallback(connHandler, CA_LE);
284
285     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
286
287     return CA_STATUS_OK;
288 }
289
290 CAResult_t CAStartLE()
291 {
292     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
293     OIC_LOG(DEBUG, CALEADAPTER_TAG,
294         "There is no concept of unicast/multicast in LE. So This function is not implemented");
295     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
296     return CA_STATUS_OK;
297 }
298
299 CAResult_t CAStopLE()
300 {
301     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
302
303     CAStopBleQueues();
304
305     ca_mutex_lock(g_bleIsServerMutex);
306     if (true == g_isServer)
307     {
308         CAStopBleGattServer();
309     }
310     else
311     {
312         CAStopBLEGattClient();
313     }
314     ca_mutex_unlock(g_bleIsServerMutex);
315
316     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
317
318     return CA_STATUS_OK;
319 }
320
321 void CATerminateLE()
322 {
323     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
324
325     CASetBLEReqRespServerCallback(NULL);
326     CASetBLEReqRespClientCallback(NULL);
327     CALERegisterNetworkNotifications(NULL);
328     CASetBLEReqRespAdapterCallback(NULL);
329     CATerminateLENetworkMonitor();
330
331     ca_mutex_lock(g_bleIsServerMutex);
332     if (true == g_isServer)
333     {
334         CATerminateBleGattServer();
335     }
336     else
337     {
338         CATerminateBLEGattClient();
339     }
340     ca_mutex_unlock(g_bleIsServerMutex);
341
342     CATerminateBleQueues();
343
344     CATerminateBleAdapterMutex();
345
346     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
347 }
348
349 CAResult_t CAStartLEListeningServer()
350 {
351     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
352
353     CAResult_t result = CAInitBleServerQueues();
354     if (CA_STATUS_OK != result)
355     {
356         OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
357         return CA_STATUS_FAILED;
358     }
359
360     result = CAGetLEAdapterState();
361     if (CA_ADAPTER_NOT_ENABLED == result)
362     {
363         gLeServerStatus = CA_LISTENING_SERVER;
364         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
365         return CA_STATUS_OK;
366     }
367
368     if (CA_STATUS_FAILED == result)
369     {
370         OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
371         return CA_STATUS_FAILED;
372     }
373
374     CAStartBleGattServer();
375
376     ca_mutex_lock(g_bleIsServerMutex);
377     g_isServer = true;
378     ca_mutex_unlock(g_bleIsServerMutex);
379
380     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
381     return CA_STATUS_OK;
382 }
383
384 CAResult_t CAStartLEDiscoveryServer()
385 {
386     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
387
388     CAResult_t result = CAInitBleClientQueues();
389     if (CA_STATUS_OK != result)
390     {
391         OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
392         return CA_STATUS_FAILED;
393     }
394
395     result = CAGetLEAdapterState();
396     if (CA_ADAPTER_NOT_ENABLED == result)
397     {
398         gLeServerStatus = CA_DISCOVERY_SERVER;
399         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
400         return CA_STATUS_OK;
401     }
402
403     if (CA_STATUS_FAILED == result)
404     {
405         OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
406         return CA_STATUS_FAILED;
407     }
408
409     CAStartBLEGattClient();
410
411     ca_mutex_lock(g_bleIsServerMutex);
412     g_isServer = false;
413     ca_mutex_unlock(g_bleIsServerMutex);
414
415     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
416     return CA_STATUS_OK;
417 }
418
419 CAResult_t CAStartLENotifyServer()
420 {
421     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
422
423     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
424     return CA_STATUS_OK;
425 }
426
427 uint32_t CASendLENotification(const CARemoteEndpoint_t *endpoint, const void *data, uint32_t dataLen)
428 {
429     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
430
431     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
432     return 0;
433 }
434
435 CAResult_t CAReadLEData()
436 {
437     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
438
439     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
440     return CA_STATUS_OK;
441 }
442
443 int32_t CASendLEUnicastData(const CARemoteEndpoint_t *endpoint, const void *data, uint32_t dataLen)
444 {
445     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
446
447     //Input validation
448     VERIFY_NON_NULL_RET(endpoint, NULL, "Remote endpoint is null", -1);
449     VERIFY_NON_NULL_RET(data, NULL, "Data is null", -1);
450
451     CAResult_t result = CA_STATUS_FAILED;
452
453     ca_mutex_lock(g_bleIsServerMutex);
454     if (true  == g_isServer)
455     {
456         result = CABLEServerSendData(endpoint, data, dataLen);
457         if (CA_STATUS_OK != result)
458         {
459             OIC_LOG(ERROR, CALEADAPTER_TAG,
460                     "[SendLEUnicastData] CABleServerSenderQueueEnqueueMessage failed \n");
461             ca_mutex_unlock(g_bleIsServerMutex);
462             return -1;
463         }
464     }
465     else
466     {
467         result = CABLEClientSendData(endpoint, data, dataLen);
468         if (CA_STATUS_OK != result)
469         {
470             OIC_LOG(ERROR, CALEADAPTER_TAG,
471                     "[SendLEUnicastData] CABleClientSenderQueueEnqueueMessage failed \n");
472             ca_mutex_unlock(g_bleIsServerMutex);
473             return -1;
474         }
475     }
476     ca_mutex_unlock(g_bleIsServerMutex);
477
478     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
479     return dataLen;
480 }
481
482 int32_t CASendLEMulticastData(const void *data, uint32_t dataLen)
483 {
484     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
485
486     //Input validation
487     VERIFY_NON_NULL_RET(data, NULL, "Data is null", -1);
488
489     if (0 >= dataLen)
490     {
491         OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
492         return -1;
493     }
494
495     CAResult_t result = CA_STATUS_FAILED;
496
497     ca_mutex_lock(g_bleIsServerMutex);
498     if (true  == g_isServer)
499     {
500         result = CABLEServerSendData(NULL, data, dataLen);
501         if (CA_STATUS_OK != result)
502         {
503             OIC_LOG(ERROR, CALEADAPTER_TAG,
504                     "[SendLEMulticastDataToAll] CABleServerSenderQueueEnqueueMessage failed" );
505             ca_mutex_unlock(g_bleIsServerMutex);
506             return -1;
507         }
508     }
509     else
510     {
511         result = CABLEClientSendData(NULL, data, dataLen);
512         if (CA_STATUS_OK != result)
513         {
514             OIC_LOG(ERROR, CALEADAPTER_TAG,
515                     "[SendLEMulticastDataToAll] CABleClientSenderQueueEnqueueMessage failed" );
516             ca_mutex_unlock(g_bleIsServerMutex);
517             return -1;
518         }
519     }
520     ca_mutex_unlock(g_bleIsServerMutex);
521
522     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
523     return dataLen;
524 }
525
526 CAResult_t CAGetLEInterfaceInformation(CALocalConnectivity_t **info, uint32_t *size)
527 {
528     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
529
530     VERIFY_NON_NULL(info, NULL, "CALocalConnectivity info is null");
531
532     char *local_address = NULL;
533
534     CAResult_t res = CAGetLEAddress(&local_address);
535     if (CA_STATUS_OK != res)
536     {
537         OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
538         return res;
539     }
540
541     if (NULL == local_address)
542     {
543         OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
544         return CA_STATUS_FAILED;
545     }
546
547     *size = 0;
548     (*info) = (CALocalConnectivity_t *) OICCalloc(1, sizeof(CALocalConnectivity_t));
549     if (NULL == (*info))
550     {
551         OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
552         OICFree(local_address);
553         return CA_STATUS_FAILED;
554     }
555
556     strncpy((*info)->addressInfo.BT.btMacAddress, local_address, strlen(local_address));
557     ca_mutex_lock(g_bleLocalAddressMutex);
558     strncpy(g_localBLEAddress, local_address, sizeof(g_localBLEAddress));
559     ca_mutex_unlock(g_bleLocalAddressMutex);
560
561     (*info)->type = CA_LE;
562     *size = 1;
563     OICFree(local_address);
564
565     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
566     return CA_STATUS_OK;
567 }
568
569 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
570 {
571     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
572
573     ca_mutex_lock(g_bleNetworkCbMutex);
574     g_networkCallback = netCallback;
575     ca_mutex_unlock(g_bleNetworkCbMutex);
576     CAResult_t res = CA_STATUS_OK;
577     if (netCallback)
578     {
579         res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
580         if (CA_STATUS_OK != res)
581         {
582             OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
583         }
584     }
585     else
586     {
587         res = CAUnSetLEAdapterStateChangedCb();
588         if (CA_STATUS_OK != res)
589         {
590             OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
591         }
592     }
593
594     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
595     return res;
596 }
597
598 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
599 {
600     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
601
602     VERIFY_NON_NULL_VOID(g_localBLEAddress, NULL, "g_localBLEAddress is null");
603     CALocalConnectivity_t localEndpoint = {};
604
605     ca_mutex_lock(g_bleLocalAddressMutex);
606     strncpy(localEndpoint.addressInfo.BT.btMacAddress, g_localBLEAddress, strlen(g_localBLEAddress));
607     ca_mutex_unlock(g_bleLocalAddressMutex);
608
609     // Start a GattServer/Client if gLeServerStatus is SET
610     if (CA_LISTENING_SERVER == gLeServerStatus)
611     {
612         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattServer");
613         CAStartBleGattServer();
614     }
615     else if (CA_DISCOVERY_SERVER == gLeServerStatus)
616     {
617         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
618         CAStartBLEGattClient();
619     }
620     gLeServerStatus = CA_SERVER_NOTSTARTED;
621
622     ca_mutex_lock(g_bleNetworkCbMutex);
623     if (NULL != g_networkCallback)
624     {
625         g_networkCallback(&localEndpoint, adapter_state);
626     }
627     else
628     {
629         OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
630     }
631     ca_mutex_unlock(g_bleNetworkCbMutex);
632
633     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
634 }
635
636 CAResult_t CAInitBleAdapterMutex()
637 {
638     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
639
640     if (NULL == g_bleIsServerMutex)
641     {
642         g_bleIsServerMutex = ca_mutex_new();
643         if (NULL == g_bleIsServerMutex)
644         {
645             OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
646             return CA_STATUS_FAILED;
647         }
648     }
649
650     if (NULL == g_bleNetworkCbMutex)
651     {
652         g_bleNetworkCbMutex = ca_mutex_new();
653         if (NULL == g_bleNetworkCbMutex)
654         {
655             OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
656             return CA_STATUS_FAILED;
657         }
658     }
659
660     if (NULL == g_bleLocalAddressMutex)
661     {
662         g_bleLocalAddressMutex = ca_mutex_new();
663         if (NULL == g_bleLocalAddressMutex)
664         {
665             OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
666             return CA_STATUS_FAILED;
667         }
668     }
669
670     if (NULL == g_bleAdapterThreadPoolMutex)
671     {
672         g_bleAdapterThreadPoolMutex = ca_mutex_new();
673         if (NULL == g_bleAdapterThreadPoolMutex)
674         {
675             OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
676             return CA_STATUS_FAILED;
677         }
678     }
679
680     if (NULL == g_bleClientSendDataMutex)
681     {
682         g_bleClientSendDataMutex = ca_mutex_new();
683         if (NULL == g_bleClientSendDataMutex)
684         {
685             OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
686             return CA_STATUS_FAILED;
687         }
688     }
689
690     if (NULL == g_bleClientReceiveDataMutex)
691     {
692         g_bleClientReceiveDataMutex = ca_mutex_new();
693         if (NULL == g_bleClientReceiveDataMutex)
694         {
695             OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
696             return CA_STATUS_FAILED;
697         }
698     }
699
700     if (NULL == g_bleServerSendDataMutex)
701     {
702         g_bleServerSendDataMutex = ca_mutex_new();
703         if (NULL == g_bleServerSendDataMutex)
704         {
705             OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
706             return CA_STATUS_FAILED;
707         }
708     }
709
710     if (NULL == g_bleServerReceiveDataMutex)
711     {
712         g_bleServerReceiveDataMutex = ca_mutex_new();
713         if (NULL == g_bleServerReceiveDataMutex)
714         {
715             OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
716             return CA_STATUS_FAILED;
717         }
718     }
719
720     if (NULL == g_bleAdapterReqRespCbMutex)
721     {
722         g_bleAdapterReqRespCbMutex = ca_mutex_new();
723         if (NULL == g_bleAdapterReqRespCbMutex)
724         {
725             OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
726             return CA_STATUS_FAILED;
727         }
728     }
729
730     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
731     return CA_STATUS_OK;
732 }
733
734 void CATerminateBleAdapterMutex()
735 {
736     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
737
738     ca_mutex_free(g_bleIsServerMutex);
739     g_bleIsServerMutex = NULL;
740
741     ca_mutex_free(g_bleNetworkCbMutex);
742     g_bleNetworkCbMutex = NULL;
743
744     ca_mutex_free(g_bleLocalAddressMutex);
745     g_bleLocalAddressMutex = NULL;
746
747     ca_mutex_free(g_bleAdapterThreadPoolMutex);
748     g_bleAdapterThreadPoolMutex = NULL;
749
750     ca_mutex_free(g_bleClientSendDataMutex);
751     g_bleClientSendDataMutex = NULL;
752
753     ca_mutex_free(g_bleClientReceiveDataMutex);
754     g_bleClientReceiveDataMutex = NULL;
755
756     ca_mutex_free(g_bleServerSendDataMutex);
757     g_bleServerSendDataMutex = NULL;
758
759     ca_mutex_free(g_bleServerReceiveDataMutex);
760     g_bleServerReceiveDataMutex = NULL;
761
762     ca_mutex_free(g_bleAdapterReqRespCbMutex);
763     g_bleAdapterReqRespCbMutex = NULL;
764
765     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
766 }
767
768 void CAInitBleQueues()
769 {
770     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
771
772     CAResult_t result = CAInitBleServerQueues();
773     if (CA_STATUS_OK != result)
774     {
775         OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerQueues failed");
776         return;
777     }
778
779     result = CAInitBleClientQueues();
780     if (CA_STATUS_OK != result)
781     {
782         OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
783         return;
784     }
785
786     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
787 }
788
789 CAResult_t CAInitBleServerQueues()
790 {
791     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
792
793     ca_mutex_lock(g_bleAdapterThreadPoolMutex);
794
795     CAResult_t result = CAInitBleServerSenderQueue();
796     if (CA_STATUS_OK != result)
797     {
798         OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
799         ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
800         return CA_STATUS_FAILED;
801     }
802
803     result = CAInitBleServerReceiverQueue();
804     if (CA_STATUS_OK != result)
805     {
806         OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerReceiverQueue failed");
807         ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
808         return CA_STATUS_FAILED;
809     }
810
811     g_dataReceiverHandlerState = true;
812
813     ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
814
815     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
816     return CA_STATUS_OK;
817 }
818
819 CAResult_t CAInitBleClientQueues()
820 {
821     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
822
823     ca_mutex_lock(g_bleAdapterThreadPoolMutex);
824
825     CAResult_t result = CAInitBleClientSenderQueue();
826     if (CA_STATUS_OK != result)
827     {
828         OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
829         ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
830         return CA_STATUS_FAILED;
831     }
832
833     result = CAInitBleClientReceiverQueue();
834     if (CA_STATUS_OK != result)
835     {
836         OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientReceiverQueue failed");
837         ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
838         return CA_STATUS_FAILED;
839     }
840
841     g_dataReceiverHandlerState = true;
842
843     ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
844
845     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
846     return CA_STATUS_OK;
847 }
848
849 CAResult_t CAInitBleServerSenderQueue()
850 {
851     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
852     // Check if the message queue is already initialized
853     if (g_sendQueueHandle)
854     {
855         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
856         return CA_STATUS_OK;
857     }
858
859     // Create send message queue
860     g_sendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
861     if (!g_sendQueueHandle)
862     {
863         OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
864         return CA_MEMORY_ALLOC_FAILED;
865     }
866
867     if (CA_STATUS_OK != CAQueueingThreadInitialize(g_sendQueueHandle, g_bleAdapterThreadPool,
868             CABLEServerSendDataThread, CALEDataDestroyer))
869     {
870         OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
871         OICFree(g_sendQueueHandle);
872         g_sendQueueHandle = NULL;
873         return CA_STATUS_FAILED;
874     }
875
876     if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle))
877     {
878         OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
879         OICFree(g_sendQueueHandle);
880         g_sendQueueHandle = NULL;
881         return CA_STATUS_FAILED;
882     }
883
884     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
885     return CA_STATUS_OK;
886 }
887
888 CAResult_t CAInitBleClientSenderQueue()
889 {
890     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
891
892     if (g_bleClientSendQueueHandle)
893     {
894         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
895         return CA_STATUS_OK;
896     }
897
898     // Create send message queue
899     g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
900     if (!g_bleClientSendQueueHandle)
901     {
902         OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
903         return CA_MEMORY_ALLOC_FAILED;
904     }
905
906     if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle, g_bleAdapterThreadPool,
907             CABLEClientSendDataThread, CALEDataDestroyer))
908     {
909         OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
910         OICFree(g_bleClientSendQueueHandle);
911         g_bleClientSendQueueHandle = NULL;
912         return CA_STATUS_FAILED;
913     }
914
915     if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientSendQueueHandle))
916     {
917         OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
918         OICFree(g_bleClientSendQueueHandle);
919         g_bleClientSendQueueHandle = NULL;
920         return CA_STATUS_FAILED;
921     }
922
923     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
924     return CA_STATUS_OK;
925 }
926
927 CAResult_t CAInitBleServerReceiverQueue()
928 {
929     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
930     // Check if the message queue is already initialized
931     if (g_bleServerReceiverQueue)
932     {
933         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
934         return CA_STATUS_OK;
935     }
936
937     // Create send message queue
938     g_bleServerReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
939     if (!g_bleServerReceiverQueue)
940     {
941         OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
942         OICFree(g_sendQueueHandle);
943         return CA_MEMORY_ALLOC_FAILED;
944     }
945
946     if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerReceiverQueue, g_bleAdapterThreadPool,
947             CABLEServerDataReceiverHandler, CALEDataDestroyer))
948     {
949         OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
950         OICFree(g_bleServerReceiverQueue);
951         g_bleServerReceiverQueue = NULL;
952         return CA_STATUS_FAILED;
953     }
954
955     if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerReceiverQueue))
956     {
957         OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
958         OICFree(g_bleServerReceiverQueue);
959         g_bleServerReceiverQueue = NULL;
960         return CA_STATUS_FAILED;
961     }
962
963     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
964     return CA_STATUS_OK;
965 }
966
967 CAResult_t CAInitBleClientReceiverQueue()
968 {
969     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
970
971     // Check if the message queue is already initialized
972     if (g_bleClientReceiverQueue)
973     {
974         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
975     }
976     else
977     {
978         // Create send message queue
979         g_bleClientReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
980         if (!g_bleClientReceiverQueue)
981         {
982             OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
983             OICFree(g_bleClientSendQueueHandle);
984             return CA_MEMORY_ALLOC_FAILED;
985         }
986
987         if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientReceiverQueue, g_bleAdapterThreadPool,
988                 CABLEClientDataReceiverHandler, NULL))
989         {
990             OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
991             OICFree(g_bleClientSendQueueHandle);
992             OICFree(g_bleClientReceiverQueue);
993             g_bleClientReceiverQueue = NULL;
994             return CA_STATUS_FAILED;
995         }
996     }
997     if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientReceiverQueue))
998     {
999         OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
1000         OICFree(g_bleClientReceiverQueue);
1001         g_bleClientReceiverQueue = NULL;
1002         return CA_STATUS_FAILED;
1003     }
1004
1005     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1006     return CA_STATUS_OK;
1007 }
1008
1009 void CAStopBleQueues()
1010 {
1011     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1012
1013     ca_mutex_lock(g_bleClientSendDataMutex);
1014     if (NULL != g_bleClientSendQueueHandle)
1015     {
1016         CAQueueingThreadStop(g_bleClientSendQueueHandle);
1017     }
1018     ca_mutex_unlock(g_bleClientSendDataMutex);
1019
1020     ca_mutex_lock(g_bleClientReceiveDataMutex);
1021     if (NULL != g_bleClientReceiverQueue)
1022     {
1023         CAQueueingThreadStop(g_bleClientReceiverQueue);
1024     }
1025     ca_mutex_unlock(g_bleClientReceiveDataMutex);
1026
1027     ca_mutex_lock(g_bleServerSendDataMutex);
1028     if (NULL != g_sendQueueHandle)
1029     {
1030         CAQueueingThreadStop(g_sendQueueHandle);
1031     }
1032     ca_mutex_unlock(g_bleServerSendDataMutex);
1033
1034     ca_mutex_lock(g_bleServerReceiveDataMutex);
1035     if (NULL != g_bleServerReceiverQueue)
1036     {
1037         CAQueueingThreadStop(g_bleServerReceiverQueue);
1038     }
1039     ca_mutex_unlock(g_bleServerReceiveDataMutex);
1040
1041     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1042 }
1043
1044 void CATerminateBleQueues()
1045 {
1046     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1047
1048     CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
1049     OICFree(g_bleClientSendQueueHandle);
1050     g_bleClientSendQueueHandle = NULL;
1051
1052
1053     CAQueueingThreadDestroy(g_bleClientReceiverQueue);
1054     OICFree(g_bleClientReceiverQueue);
1055     g_bleClientReceiverQueue = NULL;
1056
1057
1058     CAQueueingThreadDestroy(g_sendQueueHandle);
1059     OICFree(g_sendQueueHandle);
1060     g_sendQueueHandle = NULL;
1061
1062
1063     CAQueueingThreadDestroy(g_bleServerReceiverQueue);
1064     OICFree(g_bleServerReceiverQueue);
1065     g_bleServerReceiverQueue = NULL;
1066
1067     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1068 }
1069 void CABLEServerDataReceiverHandler(void *threadData)
1070 {
1071     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1072
1073     static uint32_t recvDataLen = 0;
1074     static uint32_t totalDataLen = 0;
1075     static char *defragData = NULL;
1076     static bool isHeaderAvailable = false;
1077     static CARemoteEndpoint_t *remoteEndpoint = NULL;
1078
1079     ca_mutex_lock(g_bleServerReceiveDataMutex);
1080
1081     if (g_dataReceiverHandlerState)
1082     {
1083         OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1084
1085         CALEData_t *bleData = (CALEData_t *) threadData;
1086         if (!bleData)
1087         {
1088             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
1089             return;
1090         }
1091
1092         OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1093
1094         if (!isHeaderAvailable)
1095         {
1096             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1097             totalDataLen = CAParseHeader((char*)bleData->data);
1098
1099             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes", totalDataLen);
1100             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%d] bytes", bleData->dataLen);
1101
1102             defragData = (char *) OICCalloc(totalDataLen + 1, sizeof(char));
1103             if (NULL == defragData)
1104             {
1105                 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1106                 return;
1107             }
1108
1109             const char *remoteAddress = bleData->remoteEndpoint->addressInfo.LE.leMacAddress;
1110             const char *serviceUUID = bleData->remoteEndpoint->resourceUri;
1111
1112             remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1113                              serviceUUID);
1114
1115             memcpy(defragData + recvDataLen, bleData->data + CA_HEADER_LENGTH,
1116                    bleData->dataLen - CA_HEADER_LENGTH);
1117             recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1118             isHeaderAvailable = true;
1119         }
1120         else
1121         {
1122             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1123             memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1124             recvDataLen += bleData->dataLen ;
1125             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength  [%d] recveived Datalen [%d]",
1126                       totalDataLen, recvDataLen);
1127         }
1128         if (totalDataLen == recvDataLen)
1129         {
1130             ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1131             if (NULL == g_networkPacketReceivedCallback)
1132             {
1133                 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1134                 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1135                 return;
1136             }
1137             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1138             g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1139             recvDataLen = 0;
1140             totalDataLen = 0;
1141             isHeaderAvailable = false;
1142             remoteEndpoint = NULL;
1143             defragData = NULL;
1144             ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1145         }
1146
1147         if (false == g_dataReceiverHandlerState)
1148         {
1149             OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1150             recvDataLen = 0;
1151             totalDataLen = 0;
1152             isHeaderAvailable = false;
1153             OICFree(defragData);
1154             CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1155             ca_mutex_unlock(g_bleServerReceiveDataMutex);
1156             return;
1157         }
1158     }
1159     ca_mutex_unlock(g_bleServerReceiveDataMutex);
1160     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1161 }
1162
1163 void CABLEClientDataReceiverHandler(void *threadData)
1164 {
1165     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1166
1167     static const char *remoteAddress = NULL;
1168     static const char *serviceUUID = NULL;
1169     static uint32_t recvDataLen = 0;
1170     static uint32_t totalDataLen = 0;
1171     static char *defragData = NULL;
1172     static bool isHeaderAvailable = false;
1173     static CARemoteEndpoint_t *remoteEndpoint = NULL;
1174
1175     ca_mutex_lock(g_bleClientReceiveDataMutex);
1176
1177     if (g_dataReceiverHandlerState)
1178     {
1179         OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1180
1181         CALEData_t *bleData = (CALEData_t *) threadData;
1182         if (!bleData)
1183         {
1184             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid wifidata!");
1185             return;
1186         }
1187
1188         OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1189
1190         if (!isHeaderAvailable)
1191         {
1192             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1193
1194             totalDataLen = CAParseHeader(bleData->data);
1195             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes",
1196                       totalDataLen);
1197             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received in the first packet [%d] bytes",
1198                       bleData->dataLen);
1199
1200             defragData = (char *) OICMalloc(sizeof(char) * totalDataLen);
1201             if (NULL == defragData)
1202             {
1203                 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1204                 return;
1205             }
1206
1207             remoteAddress = bleData->remoteEndpoint->addressInfo.LE.leMacAddress;
1208             serviceUUID = bleData->remoteEndpoint->resourceUri;
1209
1210             remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1211                                                            serviceUUID);
1212
1213             memcpy(defragData, bleData->data + CA_HEADER_LENGTH,
1214                    bleData->dataLen - CA_HEADER_LENGTH);
1215             recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1216             isHeaderAvailable = true;
1217         }
1218         else
1219         {
1220             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1221             memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1222             recvDataLen += bleData->dataLen ;
1223             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength  [%d] recveived Datalen [%d]",
1224                       totalDataLen, recvDataLen);
1225         }
1226         if (totalDataLen == recvDataLen)
1227         {
1228             ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1229             if (NULL == g_networkPacketReceivedCallback)
1230             {
1231                 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1232                 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1233                 return;
1234             }
1235             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1236             g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1237             recvDataLen = 0;
1238             totalDataLen = 0;
1239             isHeaderAvailable = false;
1240             remoteEndpoint = NULL;
1241             defragData = NULL;
1242             ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1243         }
1244
1245         if (false == g_dataReceiverHandlerState)
1246         {
1247             OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1248             OICFree(defragData);
1249             CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1250             ca_mutex_unlock(g_bleClientReceiveDataMutex);
1251             return;
1252         }
1253     }
1254     ca_mutex_unlock(g_bleClientReceiveDataMutex);
1255     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1256 }
1257
1258 void CABLEServerSendDataThread(void *threadData)
1259 {
1260     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1261
1262     CALEData_t *bleData = (CALEData_t *) threadData;
1263     if (!bleData)
1264     {
1265         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1266         return;
1267     }
1268
1269     char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1270     VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1271
1272     int32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1273
1274     OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server total Data length with header is [%d]", totalLength);
1275     char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
1276     if (NULL == dataSegment)
1277     {
1278         OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1279         OICFree(header);
1280         return;
1281     }
1282
1283     CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1284     if (CA_STATUS_OK != result )
1285     {
1286         OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1287         OICFree(header);
1288         OICFree(dataSegment);
1289         return ;
1290     }
1291
1292     memcpy(dataSegment, header, CA_HEADER_LENGTH);
1293     OICFree(header);
1294
1295     int32_t length = 0;
1296     if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1297     {
1298         length = totalLength;
1299         memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1300     }
1301     else
1302     {
1303         length =  CA_SUPPORTED_BLE_MTU_SIZE;
1304         memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1305                CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1306     }
1307
1308     int32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1309     int32_t index = 0;
1310     // Send the first segment with the header.
1311      if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1312     {
1313         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
1314         result = CAUpdateCharacteristicsToGattClient(
1315                     bleData->remoteEndpoint->addressInfo.LE.leMacAddress, dataSegment, length);
1316         if (CA_STATUS_OK != result)
1317         {
1318             OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1319             OICFree(dataSegment);
1320             return;
1321         }
1322
1323         OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1324         for (index = 1; index < iter; index++)
1325         {
1326             // Send the remaining header.
1327             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1328             result = CAUpdateCharacteristicsToGattClient(
1329                          bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1330                          bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1331                          CA_SUPPORTED_BLE_MTU_SIZE);
1332             if (CA_STATUS_OK != result)
1333             {
1334                 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1335                             "Update characteristics failed, result [%d]", result);
1336                 OICFree(dataSegment);
1337                 return;
1338             }
1339             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
1340                                                CA_SUPPORTED_BLE_MTU_SIZE);
1341         }
1342
1343         int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1344         if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1345         {
1346             // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1347             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1348             result = CAUpdateCharacteristicsToGattClient(
1349                          bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1350                          bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1351                          remainingLen);
1352             if (CA_STATUS_OK != result)
1353             {
1354                 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1355                                                    result);
1356                 OICFree(dataSegment);
1357                 return;
1358             }
1359             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1360         }
1361      }
1362     else
1363     {
1364         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
1365         result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
1366         if (CA_STATUS_OK != result)
1367         {
1368             OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1369             OICFree(dataSegment);
1370             return;
1371         }
1372         OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1373         for (index = 1; index < iter; index++)
1374         {
1375             // Send the remaining header.
1376             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1377             result = CAUpdateCharacteristicsToAllGattClients(
1378                          bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1379                          CA_SUPPORTED_BLE_MTU_SIZE);
1380             if (CA_STATUS_OK != result)
1381             {
1382                 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1383                 OICFree(dataSegment);
1384                 return;
1385             }
1386             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
1387         }
1388
1389         int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1390         if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1391         {
1392             // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1393             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1394             result = CAUpdateCharacteristicsToAllGattClients(
1395                          bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1396                          remainingLen);
1397             if (CA_STATUS_OK != result)
1398             {
1399                 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1400                                                    result);
1401                 OICFree(dataSegment);
1402                 return;
1403             }
1404             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1405         }
1406     }
1407     OICFree(dataSegment);
1408
1409     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1410 }
1411
1412 void CABLEClientSendDataThread(void *threadData)
1413 {
1414     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1415
1416     CALEData_t *bleData = (CALEData_t *) threadData;
1417     if (!bleData)
1418     {
1419         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1420         return;
1421     }
1422
1423     char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1424     VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1425
1426     uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1427     char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
1428     if (NULL == dataSegment)
1429     {
1430         OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1431         OICFree(header);
1432         return;
1433     }
1434
1435     CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1436     if (CA_STATUS_OK != result )
1437     {
1438         OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1439         OICFree(header);
1440         OICFree(dataSegment);
1441         return ;
1442     }
1443     memcpy(dataSegment, header, CA_HEADER_LENGTH);
1444     OICFree(header);
1445
1446     uint32_t length = 0;
1447     if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1448     {
1449         length = totalLength;
1450         OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1451         memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1452     }
1453     else
1454     {
1455         length = CA_SUPPORTED_BLE_MTU_SIZE;
1456         OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length  [%d]", length);
1457         memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1458                CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1459     }
1460
1461     uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1462     uint32_t index = 0;
1463     if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1464     {
1465         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1466         // Send the first segment with the header.
1467         result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1468                  dataSegment,
1469                  length,
1470                  LE_UNICAST, 0);
1471
1472         if (CA_STATUS_OK != result)
1473         {
1474             OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1475             OICFree(dataSegment);
1476             return ;
1477         }
1478
1479         OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length  is [%d]", length);
1480         for (index = 1; index < iter; index++)
1481         {
1482             // Send the remaining header.
1483             result = CAUpdateCharacteristicsToGattServer(
1484                      bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1485                      bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1486                      CA_SUPPORTED_BLE_MTU_SIZE,
1487                      LE_UNICAST, 0);
1488             if (CA_STATUS_OK != result)
1489             {
1490                 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1491                                                    result);
1492                 OICFree(dataSegment);
1493                 return;
1494             }
1495             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length  is [%d]",
1496                                                CA_SUPPORTED_BLE_MTU_SIZE);
1497         }
1498
1499         uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1500         if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1501         {
1502             // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1503             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1504             result = CAUpdateCharacteristicsToGattServer(
1505                      bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1506                      bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1507                      remainingLen,
1508                      LE_UNICAST, 0);
1509
1510             if (CA_STATUS_OK != result)
1511             {
1512                 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1513                                                    result);
1514                 OICFree(dataSegment);
1515                 return;
1516             }
1517             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length  is [%d]", remainingLen);
1518         }
1519     }
1520     else
1521     {
1522         //Sending Mulitcast Data
1523         // Send the first segment with the header.
1524         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1525         result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1526         if (CA_STATUS_OK != result)
1527         {
1528             OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed (all), result [%d]", result);
1529             OICFree(dataSegment);
1530             return ;
1531         }
1532         OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length  is [%d]", length);
1533         // Send the remaining header.
1534         for (index = 1; index < iter; index++)
1535         {
1536             result = CAUpdateCharacteristicsToAllGattServers(
1537                          bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1538                          CA_SUPPORTED_BLE_MTU_SIZE);
1539             if (CA_STATUS_OK != result)
1540             {
1541                 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]", result);
1542                 OICFree(dataSegment);
1543                 return;
1544             }
1545             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length  is [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
1546         }
1547
1548         uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1549         if ( remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1550         {
1551             // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1552             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1553             result = CAUpdateCharacteristicsToAllGattServers(
1554                          bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1555                           remainingLen);
1556             if (CA_STATUS_OK != result)
1557             {
1558                 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]", result);
1559                 OICFree(dataSegment);
1560                 return;
1561             }
1562             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length  is [%d]", remainingLen);
1563         }
1564
1565     }
1566
1567     OICFree(dataSegment);
1568
1569     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1570 }
1571
1572 CALEData_t *CACreateBLEData(const CARemoteEndpoint_t *remoteEndpoint, const void *data,
1573                            uint32_t dataLength)
1574 {
1575     CALEData_t *bleData = (CALEData_t *) OICMalloc(sizeof(CALEData_t));
1576     if (!bleData)
1577     {
1578         OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1579         return NULL;
1580     }
1581
1582     bleData->remoteEndpoint = CAAdapterCopyRemoteEndpoint(remoteEndpoint);
1583     bleData->data = (void *)OICCalloc(dataLength + 1, 1);
1584     if (NULL == bleData->data)
1585     {
1586         OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1587         CAFreeBLEData(bleData);
1588         return NULL;
1589     }
1590     memcpy(bleData->data, data, dataLength);
1591     bleData->dataLen = dataLength;
1592
1593     return bleData;
1594 }
1595
1596 void CAFreeBLEData(CALEData_t *bleData)
1597 {
1598     VERIFY_NON_NULL_VOID(bleData, NULL, "Param bleData is NULL");
1599
1600     CAAdapterFreeRemoteEndpoint(bleData->remoteEndpoint);
1601     OICFree(bleData->data);
1602     OICFree(bleData);
1603 }
1604
1605 void CALEDataDestroyer(void *data, uint32_t size)
1606 {
1607     CALEData_t *ledata = (CALEData_t *) data;
1608
1609     CAFreeBLEData(ledata);
1610 }
1611
1612
1613 CAResult_t CABLEClientSendData(const CARemoteEndpoint_t *remoteEndpoint,
1614                                const void *data,
1615                                uint32_t dataLen)
1616 {
1617     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1618
1619     VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1620
1621     VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
1622                         "g_bleClientSendQueueHandle is  NULL",
1623                         CA_STATUS_FAILED);
1624     VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
1625                         "g_bleClientSendDataMutex is NULL",
1626                         CA_STATUS_FAILED);
1627
1628     VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
1629                         CA_STATUS_FAILED);
1630
1631     OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1632
1633     CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1634     if (!bleData)
1635     {
1636         OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1637         return CA_MEMORY_ALLOC_FAILED;
1638     }
1639     // Add message to send queue
1640     ca_mutex_lock(g_bleClientSendDataMutex);
1641     CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
1642     ca_mutex_unlock(g_bleClientSendDataMutex);
1643
1644     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1645     return CA_STATUS_OK;
1646 }
1647
1648
1649 CAResult_t CABLEServerSendData(const CARemoteEndpoint_t *remoteEndpoint,
1650                                const void *data,
1651                                uint32_t dataLen)
1652 {
1653     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1654
1655     VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1656
1657     VERIFY_NON_NULL_RET(g_sendQueueHandle, CALEADAPTER_TAG,
1658                         "BleClientReceiverQueue is NULL",
1659                         CA_STATUS_FAILED);
1660     VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
1661                         "BleClientSendDataMutex is NULL",
1662                         CA_STATUS_FAILED);
1663
1664     VERIFY_NON_NULL_RET(g_sendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
1665                         CA_STATUS_FAILED);
1666
1667     OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1668
1669     CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1670     if (!bleData)
1671     {
1672         OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1673         return CA_MEMORY_ALLOC_FAILED;
1674     }
1675     // Add message to send queue
1676     ca_mutex_lock(g_bleServerSendDataMutex);
1677     CAQueueingThreadAddData(g_sendQueueHandle, bleData, sizeof(CALEData_t));
1678     ca_mutex_unlock(g_bleServerSendDataMutex);
1679
1680     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1681     return CA_STATUS_OK;
1682 }
1683
1684 CAResult_t CABLEServerReceivedData(const char *remoteAddress, const char *serviceUUID,
1685                                    const void *data, uint32_t dataLength, uint32_t *sentLength)
1686 {
1687     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1688
1689     //Input validation
1690     VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1691     VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1692     VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1693     VERIFY_NON_NULL_RET(g_bleServerReceiverQueue, CALEADAPTER_TAG, "g_bleServerReceiverQueue",
1694                         CA_STATUS_FAILED);
1695
1696     //Add message to data queue
1697     CARemoteEndpoint_t *remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1698                                          serviceUUID);
1699     if (NULL == remoteEndpoint)
1700     {
1701         OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1702         return CA_STATUS_FAILED;
1703     }
1704
1705     // Create bleData to add to queue
1706     OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1707
1708     CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1709     if (!bleData)
1710     {
1711         OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1712         CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1713         return CA_MEMORY_ALLOC_FAILED;
1714     }
1715
1716     CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1717     // Add message to send queue
1718     CAQueueingThreadAddData(g_bleServerReceiverQueue, bleData, sizeof(CALEData_t));
1719
1720     *sentLength = dataLength;
1721
1722     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1723     return CA_STATUS_OK;
1724 }
1725
1726 CAResult_t CABLEClientReceivedData(const char *remoteAddress, const char *serviceUUID,
1727                                    const void *data, uint32_t dataLength, uint32_t *sentLength)
1728 {
1729     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1730
1731     //Input validation
1732     VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1733     VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1734     VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1735     VERIFY_NON_NULL_RET(g_bleClientReceiverQueue, CALEADAPTER_TAG, "g_bleClientReceiverQueue",
1736                         CA_STATUS_FAILED);
1737
1738     //Add message to data queue
1739     CARemoteEndpoint_t *remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1740                                          serviceUUID);
1741     if (NULL == remoteEndpoint)
1742     {
1743         OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1744         return CA_STATUS_FAILED;
1745     }
1746
1747     OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1748
1749     // Create bleData to add to queue
1750     CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1751     if (!bleData)
1752     {
1753         OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1754         CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1755         return CA_MEMORY_ALLOC_FAILED;
1756     }
1757
1758     CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1759     // Add message to send queue
1760     CAQueueingThreadAddData(g_bleClientReceiverQueue, bleData, sizeof(CALEData_t));
1761
1762     *sentLength = dataLength;
1763
1764     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1765     return CA_STATUS_OK;
1766 }
1767
1768 void CASetBleAdapterThreadPoolHandle(ca_thread_pool_t handle)
1769 {
1770     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1771
1772     ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1773     g_bleAdapterThreadPool = handle;
1774     ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1775
1776     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1777 }
1778
1779 void CASetBLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
1780 {
1781     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1782
1783     ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1784
1785     g_networkPacketReceivedCallback = callback;
1786
1787     ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1788
1789     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1790 }
1791