Imported Upstream version 0.9.1
[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     size_t local_address_len = strlen(local_address);
557
558     if(local_address_len >= sizeof(g_localBLEAddress) ||
559             local_address_len >= sizeof((*info)->addressInfo.BT.btMacAddress))
560     {
561         OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
562         OICFree(*info);
563         OICFree(local_address);
564         return CA_STATUS_FAILED;
565     }
566
567     strncpy((*info)->addressInfo.BT.btMacAddress, local_address,
568             sizeof((*info)->addressInfo.BT.btMacAddress) - 1);
569     (*info)->addressInfo.BT.btMacAddress[sizeof((*info)->addressInfo.BT.btMacAddress)-1] = '\0';
570     ca_mutex_lock(g_bleLocalAddressMutex);
571     strncpy(g_localBLEAddress, local_address, sizeof(g_localBLEAddress) - 1);
572     g_localBLEAddress[sizeof(g_localBLEAddress)-1] = '\0';
573     ca_mutex_unlock(g_bleLocalAddressMutex);
574
575     (*info)->type = CA_LE;
576     *size = 1;
577     OICFree(local_address);
578
579     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
580     return CA_STATUS_OK;
581 }
582
583 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
584 {
585     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
586
587     ca_mutex_lock(g_bleNetworkCbMutex);
588     g_networkCallback = netCallback;
589     ca_mutex_unlock(g_bleNetworkCbMutex);
590     CAResult_t res = CA_STATUS_OK;
591     if (netCallback)
592     {
593         res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
594         if (CA_STATUS_OK != res)
595         {
596             OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
597         }
598     }
599     else
600     {
601         res = CAUnSetLEAdapterStateChangedCb();
602         if (CA_STATUS_OK != res)
603         {
604             OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
605         }
606     }
607
608     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
609     return res;
610 }
611
612 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
613 {
614     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
615
616     VERIFY_NON_NULL_VOID(g_localBLEAddress, NULL, "g_localBLEAddress is null");
617     CALocalConnectivity_t localEndpoint = {};
618
619     ca_mutex_lock(g_bleLocalAddressMutex);
620     strncpy(localEndpoint.addressInfo.BT.btMacAddress, g_localBLEAddress, strlen(g_localBLEAddress));
621     ca_mutex_unlock(g_bleLocalAddressMutex);
622
623     // Start a GattServer/Client if gLeServerStatus is SET
624     if (CA_LISTENING_SERVER == gLeServerStatus)
625     {
626         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattServer");
627         CAStartBleGattServer();
628     }
629     else if (CA_DISCOVERY_SERVER == gLeServerStatus)
630     {
631         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
632         CAStartBLEGattClient();
633     }
634     gLeServerStatus = CA_SERVER_NOTSTARTED;
635
636     ca_mutex_lock(g_bleNetworkCbMutex);
637     if (NULL != g_networkCallback)
638     {
639         g_networkCallback(&localEndpoint, adapter_state);
640     }
641     else
642     {
643         OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
644     }
645     ca_mutex_unlock(g_bleNetworkCbMutex);
646
647     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
648 }
649
650 CAResult_t CAInitBleAdapterMutex()
651 {
652     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
653
654     if (NULL == g_bleIsServerMutex)
655     {
656         g_bleIsServerMutex = ca_mutex_new();
657         if (NULL == g_bleIsServerMutex)
658         {
659             OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
660             return CA_STATUS_FAILED;
661         }
662     }
663
664     if (NULL == g_bleNetworkCbMutex)
665     {
666         g_bleNetworkCbMutex = ca_mutex_new();
667         if (NULL == g_bleNetworkCbMutex)
668         {
669             OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
670             return CA_STATUS_FAILED;
671         }
672     }
673
674     if (NULL == g_bleLocalAddressMutex)
675     {
676         g_bleLocalAddressMutex = ca_mutex_new();
677         if (NULL == g_bleLocalAddressMutex)
678         {
679             OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
680             return CA_STATUS_FAILED;
681         }
682     }
683
684     if (NULL == g_bleAdapterThreadPoolMutex)
685     {
686         g_bleAdapterThreadPoolMutex = ca_mutex_new();
687         if (NULL == g_bleAdapterThreadPoolMutex)
688         {
689             OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
690             return CA_STATUS_FAILED;
691         }
692     }
693
694     if (NULL == g_bleClientSendDataMutex)
695     {
696         g_bleClientSendDataMutex = ca_mutex_new();
697         if (NULL == g_bleClientSendDataMutex)
698         {
699             OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
700             return CA_STATUS_FAILED;
701         }
702     }
703
704     if (NULL == g_bleClientReceiveDataMutex)
705     {
706         g_bleClientReceiveDataMutex = ca_mutex_new();
707         if (NULL == g_bleClientReceiveDataMutex)
708         {
709             OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
710             return CA_STATUS_FAILED;
711         }
712     }
713
714     if (NULL == g_bleServerSendDataMutex)
715     {
716         g_bleServerSendDataMutex = ca_mutex_new();
717         if (NULL == g_bleServerSendDataMutex)
718         {
719             OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
720             return CA_STATUS_FAILED;
721         }
722     }
723
724     if (NULL == g_bleServerReceiveDataMutex)
725     {
726         g_bleServerReceiveDataMutex = ca_mutex_new();
727         if (NULL == g_bleServerReceiveDataMutex)
728         {
729             OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
730             return CA_STATUS_FAILED;
731         }
732     }
733
734     if (NULL == g_bleAdapterReqRespCbMutex)
735     {
736         g_bleAdapterReqRespCbMutex = ca_mutex_new();
737         if (NULL == g_bleAdapterReqRespCbMutex)
738         {
739             OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
740             return CA_STATUS_FAILED;
741         }
742     }
743
744     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
745     return CA_STATUS_OK;
746 }
747
748 void CATerminateBleAdapterMutex()
749 {
750     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
751
752     ca_mutex_free(g_bleIsServerMutex);
753     g_bleIsServerMutex = NULL;
754
755     ca_mutex_free(g_bleNetworkCbMutex);
756     g_bleNetworkCbMutex = NULL;
757
758     ca_mutex_free(g_bleLocalAddressMutex);
759     g_bleLocalAddressMutex = NULL;
760
761     ca_mutex_free(g_bleAdapterThreadPoolMutex);
762     g_bleAdapterThreadPoolMutex = NULL;
763
764     ca_mutex_free(g_bleClientSendDataMutex);
765     g_bleClientSendDataMutex = NULL;
766
767     ca_mutex_free(g_bleClientReceiveDataMutex);
768     g_bleClientReceiveDataMutex = NULL;
769
770     ca_mutex_free(g_bleServerSendDataMutex);
771     g_bleServerSendDataMutex = NULL;
772
773     ca_mutex_free(g_bleServerReceiveDataMutex);
774     g_bleServerReceiveDataMutex = NULL;
775
776     ca_mutex_free(g_bleAdapterReqRespCbMutex);
777     g_bleAdapterReqRespCbMutex = NULL;
778
779     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
780 }
781
782 void CAInitBleQueues()
783 {
784     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
785
786     CAResult_t result = CAInitBleServerQueues();
787     if (CA_STATUS_OK != result)
788     {
789         OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerQueues failed");
790         return;
791     }
792
793     result = CAInitBleClientQueues();
794     if (CA_STATUS_OK != result)
795     {
796         OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
797         return;
798     }
799
800     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
801 }
802
803 CAResult_t CAInitBleServerQueues()
804 {
805     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
806
807     ca_mutex_lock(g_bleAdapterThreadPoolMutex);
808
809     CAResult_t result = CAInitBleServerSenderQueue();
810     if (CA_STATUS_OK != result)
811     {
812         OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
813         ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
814         return CA_STATUS_FAILED;
815     }
816
817     result = CAInitBleServerReceiverQueue();
818     if (CA_STATUS_OK != result)
819     {
820         OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerReceiverQueue failed");
821         ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
822         return CA_STATUS_FAILED;
823     }
824
825     g_dataReceiverHandlerState = true;
826
827     ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
828
829     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
830     return CA_STATUS_OK;
831 }
832
833 CAResult_t CAInitBleClientQueues()
834 {
835     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
836
837     ca_mutex_lock(g_bleAdapterThreadPoolMutex);
838
839     CAResult_t result = CAInitBleClientSenderQueue();
840     if (CA_STATUS_OK != result)
841     {
842         OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
843         ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
844         return CA_STATUS_FAILED;
845     }
846
847     result = CAInitBleClientReceiverQueue();
848     if (CA_STATUS_OK != result)
849     {
850         OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientReceiverQueue failed");
851         ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
852         return CA_STATUS_FAILED;
853     }
854
855     g_dataReceiverHandlerState = true;
856
857     ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
858
859     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
860     return CA_STATUS_OK;
861 }
862
863 CAResult_t CAInitBleServerSenderQueue()
864 {
865     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
866     // Check if the message queue is already initialized
867     if (g_sendQueueHandle)
868     {
869         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
870         return CA_STATUS_OK;
871     }
872
873     // Create send message queue
874     g_sendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
875     if (!g_sendQueueHandle)
876     {
877         OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
878         return CA_MEMORY_ALLOC_FAILED;
879     }
880
881     if (CA_STATUS_OK != CAQueueingThreadInitialize(g_sendQueueHandle, g_bleAdapterThreadPool,
882             CABLEServerSendDataThread, CALEDataDestroyer))
883     {
884         OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
885         OICFree(g_sendQueueHandle);
886         g_sendQueueHandle = NULL;
887         return CA_STATUS_FAILED;
888     }
889
890     if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle))
891     {
892         OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
893         OICFree(g_sendQueueHandle);
894         g_sendQueueHandle = NULL;
895         return CA_STATUS_FAILED;
896     }
897
898     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
899     return CA_STATUS_OK;
900 }
901
902 CAResult_t CAInitBleClientSenderQueue()
903 {
904     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
905
906     if (g_bleClientSendQueueHandle)
907     {
908         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
909         return CA_STATUS_OK;
910     }
911
912     // Create send message queue
913     g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
914     if (!g_bleClientSendQueueHandle)
915     {
916         OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
917         return CA_MEMORY_ALLOC_FAILED;
918     }
919
920     if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle, g_bleAdapterThreadPool,
921             CABLEClientSendDataThread, CALEDataDestroyer))
922     {
923         OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
924         OICFree(g_bleClientSendQueueHandle);
925         g_bleClientSendQueueHandle = NULL;
926         return CA_STATUS_FAILED;
927     }
928
929     if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientSendQueueHandle))
930     {
931         OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
932         OICFree(g_bleClientSendQueueHandle);
933         g_bleClientSendQueueHandle = NULL;
934         return CA_STATUS_FAILED;
935     }
936
937     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
938     return CA_STATUS_OK;
939 }
940
941 CAResult_t CAInitBleServerReceiverQueue()
942 {
943     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
944     // Check if the message queue is already initialized
945     if (g_bleServerReceiverQueue)
946     {
947         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
948         return CA_STATUS_OK;
949     }
950
951     // Create send message queue
952     g_bleServerReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
953     if (!g_bleServerReceiverQueue)
954     {
955         OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
956         OICFree(g_sendQueueHandle);
957         return CA_MEMORY_ALLOC_FAILED;
958     }
959
960     if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerReceiverQueue, g_bleAdapterThreadPool,
961             CABLEServerDataReceiverHandler, CALEDataDestroyer))
962     {
963         OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
964         OICFree(g_bleServerReceiverQueue);
965         g_bleServerReceiverQueue = NULL;
966         return CA_STATUS_FAILED;
967     }
968
969     if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerReceiverQueue))
970     {
971         OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
972         OICFree(g_bleServerReceiverQueue);
973         g_bleServerReceiverQueue = NULL;
974         return CA_STATUS_FAILED;
975     }
976
977     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
978     return CA_STATUS_OK;
979 }
980
981 CAResult_t CAInitBleClientReceiverQueue()
982 {
983     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
984
985     // Check if the message queue is already initialized
986     if (g_bleClientReceiverQueue)
987     {
988         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
989     }
990     else
991     {
992         // Create send message queue
993         g_bleClientReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
994         if (!g_bleClientReceiverQueue)
995         {
996             OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
997             OICFree(g_bleClientSendQueueHandle);
998             return CA_MEMORY_ALLOC_FAILED;
999         }
1000
1001         if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientReceiverQueue, g_bleAdapterThreadPool,
1002                 CABLEClientDataReceiverHandler, NULL))
1003         {
1004             OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
1005             OICFree(g_bleClientSendQueueHandle);
1006             OICFree(g_bleClientReceiverQueue);
1007             g_bleClientReceiverQueue = NULL;
1008             return CA_STATUS_FAILED;
1009         }
1010     }
1011     if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientReceiverQueue))
1012     {
1013         OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
1014         OICFree(g_bleClientReceiverQueue);
1015         g_bleClientReceiverQueue = NULL;
1016         return CA_STATUS_FAILED;
1017     }
1018
1019     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1020     return CA_STATUS_OK;
1021 }
1022
1023 void CAStopBleQueues()
1024 {
1025     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1026
1027     ca_mutex_lock(g_bleClientSendDataMutex);
1028     if (NULL != g_bleClientSendQueueHandle)
1029     {
1030         CAQueueingThreadStop(g_bleClientSendQueueHandle);
1031     }
1032     ca_mutex_unlock(g_bleClientSendDataMutex);
1033
1034     ca_mutex_lock(g_bleClientReceiveDataMutex);
1035     if (NULL != g_bleClientReceiverQueue)
1036     {
1037         CAQueueingThreadStop(g_bleClientReceiverQueue);
1038     }
1039     ca_mutex_unlock(g_bleClientReceiveDataMutex);
1040
1041     ca_mutex_lock(g_bleServerSendDataMutex);
1042     if (NULL != g_sendQueueHandle)
1043     {
1044         CAQueueingThreadStop(g_sendQueueHandle);
1045     }
1046     ca_mutex_unlock(g_bleServerSendDataMutex);
1047
1048     ca_mutex_lock(g_bleServerReceiveDataMutex);
1049     if (NULL != g_bleServerReceiverQueue)
1050     {
1051         CAQueueingThreadStop(g_bleServerReceiverQueue);
1052     }
1053     ca_mutex_unlock(g_bleServerReceiveDataMutex);
1054
1055     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1056 }
1057
1058 void CATerminateBleQueues()
1059 {
1060     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1061
1062     CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
1063     OICFree(g_bleClientSendQueueHandle);
1064     g_bleClientSendQueueHandle = NULL;
1065
1066
1067     CAQueueingThreadDestroy(g_bleClientReceiverQueue);
1068     OICFree(g_bleClientReceiverQueue);
1069     g_bleClientReceiverQueue = NULL;
1070
1071
1072     CAQueueingThreadDestroy(g_sendQueueHandle);
1073     OICFree(g_sendQueueHandle);
1074     g_sendQueueHandle = NULL;
1075
1076
1077     CAQueueingThreadDestroy(g_bleServerReceiverQueue);
1078     OICFree(g_bleServerReceiverQueue);
1079     g_bleServerReceiverQueue = NULL;
1080
1081     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1082 }
1083 void CABLEServerDataReceiverHandler(void *threadData)
1084 {
1085     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1086
1087     static uint32_t recvDataLen = 0;
1088     static uint32_t totalDataLen = 0;
1089     static char *defragData = NULL;
1090     static bool isHeaderAvailable = false;
1091     static CARemoteEndpoint_t *remoteEndpoint = NULL;
1092
1093     ca_mutex_lock(g_bleServerReceiveDataMutex);
1094
1095     if (g_dataReceiverHandlerState)
1096     {
1097         OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1098
1099         CALEData_t *bleData = (CALEData_t *) threadData;
1100         if (!bleData)
1101         {
1102             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
1103             return;
1104         }
1105
1106         OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1107
1108         if (!isHeaderAvailable)
1109         {
1110             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1111             totalDataLen = CAParseHeader((char*)bleData->data);
1112
1113             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes", totalDataLen);
1114             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%d] bytes", bleData->dataLen);
1115
1116             defragData = (char *) OICCalloc(totalDataLen + 1, sizeof(char));
1117             if (NULL == defragData)
1118             {
1119                 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1120                 return;
1121             }
1122
1123             const char *remoteAddress = bleData->remoteEndpoint->addressInfo.LE.leMacAddress;
1124             const char *serviceUUID = bleData->remoteEndpoint->resourceUri;
1125
1126             remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1127                              serviceUUID);
1128
1129             memcpy(defragData + recvDataLen, bleData->data + CA_HEADER_LENGTH,
1130                    bleData->dataLen - CA_HEADER_LENGTH);
1131             recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1132             isHeaderAvailable = true;
1133         }
1134         else
1135         {
1136             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1137             memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1138             recvDataLen += bleData->dataLen ;
1139             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength  [%d] recveived Datalen [%d]",
1140                       totalDataLen, recvDataLen);
1141         }
1142         if (totalDataLen == recvDataLen)
1143         {
1144             ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1145             if (NULL == g_networkPacketReceivedCallback)
1146             {
1147                 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1148                 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1149                 return;
1150             }
1151             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1152             g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1153             recvDataLen = 0;
1154             totalDataLen = 0;
1155             isHeaderAvailable = false;
1156             remoteEndpoint = NULL;
1157             defragData = NULL;
1158             ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1159         }
1160
1161         if (false == g_dataReceiverHandlerState)
1162         {
1163             OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1164             recvDataLen = 0;
1165             totalDataLen = 0;
1166             isHeaderAvailable = false;
1167             OICFree(defragData);
1168             CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1169             ca_mutex_unlock(g_bleServerReceiveDataMutex);
1170             return;
1171         }
1172     }
1173     ca_mutex_unlock(g_bleServerReceiveDataMutex);
1174     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1175 }
1176
1177 void CABLEClientDataReceiverHandler(void *threadData)
1178 {
1179     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1180
1181     static const char *remoteAddress = NULL;
1182     static const char *serviceUUID = NULL;
1183     static uint32_t recvDataLen = 0;
1184     static uint32_t totalDataLen = 0;
1185     static char *defragData = NULL;
1186     static bool isHeaderAvailable = false;
1187     static CARemoteEndpoint_t *remoteEndpoint = NULL;
1188
1189     ca_mutex_lock(g_bleClientReceiveDataMutex);
1190
1191     if (g_dataReceiverHandlerState)
1192     {
1193         OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1194
1195         CALEData_t *bleData = (CALEData_t *) threadData;
1196         if (!bleData)
1197         {
1198             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid wifidata!");
1199             return;
1200         }
1201
1202         OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1203
1204         if (!isHeaderAvailable)
1205         {
1206             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1207
1208             totalDataLen = CAParseHeader(bleData->data);
1209             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes",
1210                       totalDataLen);
1211             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received in the first packet [%d] bytes",
1212                       bleData->dataLen);
1213
1214             defragData = (char *) OICMalloc(sizeof(char) * totalDataLen);
1215             if (NULL == defragData)
1216             {
1217                 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1218                 return;
1219             }
1220
1221             remoteAddress = bleData->remoteEndpoint->addressInfo.LE.leMacAddress;
1222             serviceUUID = bleData->remoteEndpoint->resourceUri;
1223
1224             remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1225                                                            serviceUUID);
1226
1227             memcpy(defragData, bleData->data + CA_HEADER_LENGTH,
1228                    bleData->dataLen - CA_HEADER_LENGTH);
1229             recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1230             isHeaderAvailable = true;
1231         }
1232         else
1233         {
1234             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1235             memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1236             recvDataLen += bleData->dataLen ;
1237             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength  [%d] recveived Datalen [%d]",
1238                       totalDataLen, recvDataLen);
1239         }
1240         if (totalDataLen == recvDataLen)
1241         {
1242             ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1243             if (NULL == g_networkPacketReceivedCallback)
1244             {
1245                 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1246                 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1247                 return;
1248             }
1249             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1250             g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1251             recvDataLen = 0;
1252             totalDataLen = 0;
1253             isHeaderAvailable = false;
1254             remoteEndpoint = NULL;
1255             defragData = NULL;
1256             ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1257         }
1258
1259         if (false == g_dataReceiverHandlerState)
1260         {
1261             OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1262             OICFree(defragData);
1263             CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1264             ca_mutex_unlock(g_bleClientReceiveDataMutex);
1265             return;
1266         }
1267     }
1268     ca_mutex_unlock(g_bleClientReceiveDataMutex);
1269     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1270 }
1271
1272 void CABLEServerSendDataThread(void *threadData)
1273 {
1274     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1275
1276     CALEData_t *bleData = (CALEData_t *) threadData;
1277     if (!bleData)
1278     {
1279         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1280         return;
1281     }
1282
1283     char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1284     VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1285
1286     int32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1287
1288     OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server total Data length with header is [%d]", totalLength);
1289     char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
1290     if (NULL == dataSegment)
1291     {
1292         OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1293         OICFree(header);
1294         return;
1295     }
1296
1297     CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1298     if (CA_STATUS_OK != result )
1299     {
1300         OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1301         OICFree(header);
1302         OICFree(dataSegment);
1303         return ;
1304     }
1305
1306     memcpy(dataSegment, header, CA_HEADER_LENGTH);
1307     OICFree(header);
1308
1309     int32_t length = 0;
1310     if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1311     {
1312         length = totalLength;
1313         memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1314     }
1315     else
1316     {
1317         length =  CA_SUPPORTED_BLE_MTU_SIZE;
1318         memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1319                CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1320     }
1321
1322     int32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1323     int32_t index = 0;
1324     // Send the first segment with the header.
1325      if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1326     {
1327         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
1328         result = CAUpdateCharacteristicsToGattClient(
1329                     bleData->remoteEndpoint->addressInfo.LE.leMacAddress, dataSegment, length);
1330         if (CA_STATUS_OK != result)
1331         {
1332             OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1333             OICFree(dataSegment);
1334             return;
1335         }
1336
1337         OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1338         for (index = 1; index < iter; index++)
1339         {
1340             // Send the remaining header.
1341             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1342             result = CAUpdateCharacteristicsToGattClient(
1343                          bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1344                          bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1345                          CA_SUPPORTED_BLE_MTU_SIZE);
1346             if (CA_STATUS_OK != result)
1347             {
1348                 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1349                             "Update characteristics failed, result [%d]", result);
1350                 OICFree(dataSegment);
1351                 return;
1352             }
1353             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
1354                                                CA_SUPPORTED_BLE_MTU_SIZE);
1355         }
1356
1357         int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1358         if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1359         {
1360             // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1361             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1362             result = CAUpdateCharacteristicsToGattClient(
1363                          bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1364                          bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1365                          remainingLen);
1366             if (CA_STATUS_OK != result)
1367             {
1368                 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1369                                                    result);
1370                 OICFree(dataSegment);
1371                 return;
1372             }
1373             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1374         }
1375      }
1376     else
1377     {
1378         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
1379         result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
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]", length);
1387         for (index = 1; index < iter; index++)
1388         {
1389             // Send the remaining header.
1390             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1391             result = CAUpdateCharacteristicsToAllGattClients(
1392                          bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1393                          CA_SUPPORTED_BLE_MTU_SIZE);
1394             if (CA_STATUS_OK != result)
1395             {
1396                 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1397                 OICFree(dataSegment);
1398                 return;
1399             }
1400             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
1401         }
1402
1403         int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1404         if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1405         {
1406             // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1407             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1408             result = CAUpdateCharacteristicsToAllGattClients(
1409                          bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1410                          remainingLen);
1411             if (CA_STATUS_OK != result)
1412             {
1413                 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1414                                                    result);
1415                 OICFree(dataSegment);
1416                 return;
1417             }
1418             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1419         }
1420     }
1421     OICFree(dataSegment);
1422
1423     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1424 }
1425
1426 void CABLEClientSendDataThread(void *threadData)
1427 {
1428     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1429
1430     CALEData_t *bleData = (CALEData_t *) threadData;
1431     if (!bleData)
1432     {
1433         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1434         return;
1435     }
1436
1437     char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1438     VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1439
1440     uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1441     char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
1442     if (NULL == dataSegment)
1443     {
1444         OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1445         OICFree(header);
1446         return;
1447     }
1448
1449     CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1450     if (CA_STATUS_OK != result )
1451     {
1452         OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1453         OICFree(header);
1454         OICFree(dataSegment);
1455         return ;
1456     }
1457     memcpy(dataSegment, header, CA_HEADER_LENGTH);
1458     OICFree(header);
1459
1460     uint32_t length = 0;
1461     if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1462     {
1463         length = totalLength;
1464         OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1465         memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1466     }
1467     else
1468     {
1469         length = CA_SUPPORTED_BLE_MTU_SIZE;
1470         OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length  [%d]", length);
1471         memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1472                CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1473     }
1474
1475     uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1476     uint32_t index = 0;
1477     if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1478     {
1479         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1480         // Send the first segment with the header.
1481         result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1482                  dataSegment,
1483                  length,
1484                  LE_UNICAST, 0);
1485
1486         if (CA_STATUS_OK != result)
1487         {
1488             OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1489             OICFree(dataSegment);
1490             return ;
1491         }
1492
1493         OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length  is [%d]", length);
1494         for (index = 1; index < iter; index++)
1495         {
1496             // Send the remaining header.
1497             result = CAUpdateCharacteristicsToGattServer(
1498                      bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1499                      bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1500                      CA_SUPPORTED_BLE_MTU_SIZE,
1501                      LE_UNICAST, 0);
1502             if (CA_STATUS_OK != result)
1503             {
1504                 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1505                                                    result);
1506                 OICFree(dataSegment);
1507                 return;
1508             }
1509             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length  is [%d]",
1510                                                CA_SUPPORTED_BLE_MTU_SIZE);
1511         }
1512
1513         uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1514         if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1515         {
1516             // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1517             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1518             result = CAUpdateCharacteristicsToGattServer(
1519                      bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1520                      bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1521                      remainingLen,
1522                      LE_UNICAST, 0);
1523
1524             if (CA_STATUS_OK != result)
1525             {
1526                 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1527                                                    result);
1528                 OICFree(dataSegment);
1529                 return;
1530             }
1531             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length  is [%d]", remainingLen);
1532         }
1533     }
1534     else
1535     {
1536         //Sending Mulitcast Data
1537         // Send the first segment with the header.
1538         OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1539         result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1540         if (CA_STATUS_OK != result)
1541         {
1542             OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed (all), result [%d]", result);
1543             OICFree(dataSegment);
1544             return ;
1545         }
1546         OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length  is [%d]", length);
1547         // Send the remaining header.
1548         for (index = 1; index < iter; index++)
1549         {
1550             result = CAUpdateCharacteristicsToAllGattServers(
1551                          bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1552                          CA_SUPPORTED_BLE_MTU_SIZE);
1553             if (CA_STATUS_OK != result)
1554             {
1555                 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]", result);
1556                 OICFree(dataSegment);
1557                 return;
1558             }
1559             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length  is [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
1560         }
1561
1562         uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1563         if ( remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1564         {
1565             // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1566             OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1567             result = CAUpdateCharacteristicsToAllGattServers(
1568                          bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1569                           remainingLen);
1570             if (CA_STATUS_OK != result)
1571             {
1572                 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]", result);
1573                 OICFree(dataSegment);
1574                 return;
1575             }
1576             OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length  is [%d]", remainingLen);
1577         }
1578
1579     }
1580
1581     OICFree(dataSegment);
1582
1583     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1584 }
1585
1586 CALEData_t *CACreateBLEData(const CARemoteEndpoint_t *remoteEndpoint, const void *data,
1587                            uint32_t dataLength)
1588 {
1589     CALEData_t *bleData = (CALEData_t *) OICMalloc(sizeof(CALEData_t));
1590     if (!bleData)
1591     {
1592         OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1593         return NULL;
1594     }
1595
1596     bleData->remoteEndpoint = CAAdapterCopyRemoteEndpoint(remoteEndpoint);
1597     bleData->data = (void *)OICCalloc(dataLength + 1, 1);
1598     if (NULL == bleData->data)
1599     {
1600         OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1601         CAFreeBLEData(bleData);
1602         return NULL;
1603     }
1604     memcpy(bleData->data, data, dataLength);
1605     bleData->dataLen = dataLength;
1606
1607     return bleData;
1608 }
1609
1610 void CAFreeBLEData(CALEData_t *bleData)
1611 {
1612     VERIFY_NON_NULL_VOID(bleData, NULL, "Param bleData is NULL");
1613
1614     CAAdapterFreeRemoteEndpoint(bleData->remoteEndpoint);
1615     OICFree(bleData->data);
1616     OICFree(bleData);
1617 }
1618
1619 void CALEDataDestroyer(void *data, uint32_t size)
1620 {
1621     CALEData_t *ledata = (CALEData_t *) data;
1622
1623     CAFreeBLEData(ledata);
1624 }
1625
1626
1627 CAResult_t CABLEClientSendData(const CARemoteEndpoint_t *remoteEndpoint,
1628                                const void *data,
1629                                uint32_t dataLen)
1630 {
1631     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1632
1633     VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1634
1635     VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
1636                         "g_bleClientSendQueueHandle is  NULL",
1637                         CA_STATUS_FAILED);
1638     VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
1639                         "g_bleClientSendDataMutex is NULL",
1640                         CA_STATUS_FAILED);
1641
1642     VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
1643                         CA_STATUS_FAILED);
1644
1645     OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1646
1647     CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1648     if (!bleData)
1649     {
1650         OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1651         return CA_MEMORY_ALLOC_FAILED;
1652     }
1653     // Add message to send queue
1654     ca_mutex_lock(g_bleClientSendDataMutex);
1655     CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
1656     ca_mutex_unlock(g_bleClientSendDataMutex);
1657
1658     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1659     return CA_STATUS_OK;
1660 }
1661
1662
1663 CAResult_t CABLEServerSendData(const CARemoteEndpoint_t *remoteEndpoint,
1664                                const void *data,
1665                                uint32_t dataLen)
1666 {
1667     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1668
1669     VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1670
1671     VERIFY_NON_NULL_RET(g_sendQueueHandle, CALEADAPTER_TAG,
1672                         "BleClientReceiverQueue is NULL",
1673                         CA_STATUS_FAILED);
1674     VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
1675                         "BleClientSendDataMutex is NULL",
1676                         CA_STATUS_FAILED);
1677
1678     VERIFY_NON_NULL_RET(g_sendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
1679                         CA_STATUS_FAILED);
1680
1681     OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1682
1683     CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1684     if (!bleData)
1685     {
1686         OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1687         return CA_MEMORY_ALLOC_FAILED;
1688     }
1689     // Add message to send queue
1690     ca_mutex_lock(g_bleServerSendDataMutex);
1691     CAQueueingThreadAddData(g_sendQueueHandle, bleData, sizeof(CALEData_t));
1692     ca_mutex_unlock(g_bleServerSendDataMutex);
1693
1694     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1695     return CA_STATUS_OK;
1696 }
1697
1698 CAResult_t CABLEServerReceivedData(const char *remoteAddress, const char *serviceUUID,
1699                                    const void *data, uint32_t dataLength, uint32_t *sentLength)
1700 {
1701     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1702
1703     //Input validation
1704     VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1705     VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1706     VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1707     VERIFY_NON_NULL_RET(g_bleServerReceiverQueue, CALEADAPTER_TAG, "g_bleServerReceiverQueue",
1708                         CA_STATUS_FAILED);
1709
1710     //Add message to data queue
1711     CARemoteEndpoint_t *remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1712                                          serviceUUID);
1713     if (NULL == remoteEndpoint)
1714     {
1715         OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1716         return CA_STATUS_FAILED;
1717     }
1718
1719     // Create bleData to add to queue
1720     OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1721
1722     CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1723     if (!bleData)
1724     {
1725         OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1726         CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1727         return CA_MEMORY_ALLOC_FAILED;
1728     }
1729
1730     CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1731     // Add message to send queue
1732     CAQueueingThreadAddData(g_bleServerReceiverQueue, bleData, sizeof(CALEData_t));
1733
1734     *sentLength = dataLength;
1735
1736     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1737     return CA_STATUS_OK;
1738 }
1739
1740 CAResult_t CABLEClientReceivedData(const char *remoteAddress, const char *serviceUUID,
1741                                    const void *data, uint32_t dataLength, uint32_t *sentLength)
1742 {
1743     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1744
1745     //Input validation
1746     VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1747     VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1748     VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1749     VERIFY_NON_NULL_RET(g_bleClientReceiverQueue, CALEADAPTER_TAG, "g_bleClientReceiverQueue",
1750                         CA_STATUS_FAILED);
1751
1752     //Add message to data queue
1753     CARemoteEndpoint_t *remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1754                                          serviceUUID);
1755     if (NULL == remoteEndpoint)
1756     {
1757         OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1758         return CA_STATUS_FAILED;
1759     }
1760
1761     OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1762
1763     // Create bleData to add to queue
1764     CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1765     if (!bleData)
1766     {
1767         OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1768         CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1769         return CA_MEMORY_ALLOC_FAILED;
1770     }
1771
1772     CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1773     // Add message to send queue
1774     CAQueueingThreadAddData(g_bleClientReceiverQueue, bleData, sizeof(CALEData_t));
1775
1776     *sentLength = dataLength;
1777
1778     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1779     return CA_STATUS_OK;
1780 }
1781
1782 void CASetBleAdapterThreadPoolHandle(ca_thread_pool_t handle)
1783 {
1784     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1785
1786     ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1787     g_bleAdapterThreadPool = handle;
1788     ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1789
1790     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1791 }
1792
1793 void CASetBLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
1794 {
1795     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1796
1797     ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1798
1799     g_networkPacketReceivedCallback = callback;
1800
1801     ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1802
1803     OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1804 }
1805