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