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