1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
20 #include "caleadapter.h"
25 #include "caleinterface.h"
28 #include "caadapterutils.h"
29 #include "caqueueingthread.h"
30 #include "camsgparser.h"
31 #include "oic_malloc.h"
34 * @var CALEADAPTER_TAG
35 * @brief Logging tag for module name.
37 #define CALEADAPTER_TAG "CA_BLE_ADAPTER"
40 * @var g_networkCallback
41 * @brief Callback to provide the status of the network change to CA layer.
43 static CANetworkChangeCallback g_networkCallback = NULL;
46 * @var g_localBLEAddress
47 * @brief bleAddress of the local adapter. Value will be initialized to zero, and will be updated later.
49 static char g_localBLEAddress[16] = { 0, };
53 * @brief Variable to differentiate btw GattServer and GattClient.
55 static bool g_isServer = false;
58 * @var g_bleIsServerMutex
59 * @brief Mutex to synchronize the task to be executed on the GattServer function calls.
61 static u_mutex g_bleIsServerMutex = NULL;
64 * @var g_bleNetworkCbMutex
65 * @brief Mutex to synchronize the callback to be called for the network changes.
67 static u_mutex g_bleNetworkCbMutex = NULL;
70 * @var g_bleLocalAddressMutex
71 * @brief Mutex to synchronize the updation of the local LE address of the adapter.
73 static u_mutex g_bleLocalAddressMutex = NULL;
76 * @var gBleClientThreadPool
77 * @brief reference to threadpool
79 static u_thread_pool_t g_bleAdapterThreadPool = NULL;
82 * @var g_bleAdapterThreadPoolMutex
83 * @brief Mutex to synchronize the task to be pushed to thread pool.
85 static u_mutex g_bleAdapterThreadPoolMutex = NULL;
88 * @var g_bLEClientSendQueueHandle
89 * @brief Queue to process the outgoing packets from GATTClient.
91 static CAQueueingThread_t *g_bLEClientSendQueueHandle = NULL;
94 * @var g_bleClientReceiverQueue
95 * @brief Queue to process the incoming packets to GATT Client.
97 static CAQueueingThread_t *g_bleClientReceiverQueue = NULL;
100 * @var g_bleClientSendDataMutex
101 * @brief Mutex to synchronize the queing of the data from SenderQueue.
103 static u_mutex g_bleClientSendDataMutex = NULL;
107 * @var g_bleClientReceiveDataMutex
108 * @brief Mutex to synchronize the queing of the data from ReceiverQueue.
110 static u_mutex g_bleClientReceiveDataMutex = NULL;
113 * @var g_dataReceiverHandlerState
114 * @brief Stop condition of redvhandler.
116 static bool g_dataReceiverHandlerState = false;
119 * @var g_sendQueueHandle
120 * @brief Queue to process the outgoing packets from GATTServer.
122 static CAQueueingThread_t *g_sendQueueHandle = NULL;
125 * @var g_bleServerReceiverQueue
126 * @brief Queue to process the incoming packets to GATTServer
128 static CAQueueingThread_t *g_bleServerReceiverQueue = NULL;
131 * @var g_bleServerSendDataMutex
132 * @brief Mutex to synchronize the queing of the data from SenderQueue.
134 static u_mutex g_bleServerSendDataMutex = NULL;
137 * @var g_bleServerReceiveDataMutex
138 * @brief Mutex to synchronize the queing of the data from ReceiverQueue.
140 static u_mutex g_bleServerReceiveDataMutex = NULL;
143 * @var g_bleAdapterReqRespCbMutex
144 * @brief Mutex to synchronize the callback to be called for the adapterReqResponse.
146 static u_mutex g_bleAdapterReqRespCbMutex = NULL;
149 * @var g_networkPacketReceivedCallback
150 * @brief Callback to be called when the network packet recieved from either GattServer or GattClient.
152 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
155 * @ENUM CALeServerStatus
156 * @brief status of BLE Server Status
158 * This ENUM provides information of LE Adapter Server status
162 CA_SERVER_NOTSTARTED = 0,
168 * @var gLeServerStatus
169 * @brief structure to maintain the status of the server.
171 static CALeServerStatus gLeServerStatus = CA_SERVER_NOTSTARTED;
174 * @fn CALERegisterNetworkNotifications
175 * @brief This function is used to register network change notification callback.
177 * @param[in] netCallback CANetworkChangeCallback callback which will be set for the change in nwk.
179 * @return 0 for success.
182 int32_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
185 * @fn CASetBleAdapterThreadPoolHandle
186 * @brief Used to Set the gThreadPool handle which is required for spawning new thread.
188 * @param[in] handle - Thread pool handle which is given by above layer for using thread creation task.
193 void CASetBleAdapterThreadPoolHandle(u_thread_pool_t handle);
196 * @fn CALEDeviceStateChangedCb
197 * @brief This function is used to call the callback to the upper layer when the device state gets changed.
199 * @param[in] adapter_state New state of the adapter to be notified to the upper layer.
204 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state);
207 * @fn CAInitBleAdapterMutex
208 * @brief Used to initialize all required mutex variable for LE Adapter implementation.
210 * @return 0 on success otherwise a positive error value.
211 * @retval CA_STATUS_OK Successful
212 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
213 * @retval CA_STATUS_FAILED Operation failed
216 CAResult_t CAInitBleAdapterMutex();
219 * @fn CATerminateBleAdapterMutex
220 * @brief Used to terminate all required mutex variable for LE adapter implementation.
225 void CATerminateBleAdapterMutex();
226 static void CALEDataDestroyer(void *data, uint32_t size);
228 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
229 CANetworkPacketReceivedCallback reqRespCallback,
230 CANetworkChangeCallback netCallback,
231 u_thread_pool_t handle)
233 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
236 VERIFY_NON_NULL(registerCallback, NULL, "RegisterConnectivity callback is null");
237 VERIFY_NON_NULL(reqRespCallback, NULL, "PacketReceived Callback is null");
238 VERIFY_NON_NULL(netCallback, NULL, "NetworkChange Callback is null");
240 CAResult_t result = CAInitBleAdapterMutex();
241 if (CA_STATUS_OK != result)
243 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
244 return CA_STATUS_FAILED;
247 result = CAInitializeLENetworkMonitor();
248 if (CA_STATUS_OK != result)
250 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
251 return CA_STATUS_FAILED;
254 CAInitializeLEAdapter();
256 CASetBleServerThreadPoolHandle(handle);
257 CASetBleClientThreadPoolHandle(handle);
258 CASetBleAdapterThreadPoolHandle(handle);
259 CASetBLEReqRespServerCallback(CABLEServerReceivedData);
260 CASetBLEReqRespClientCallback(CABLEClientReceivedData);
261 CASetBLEReqRespAdapterCallback(reqRespCallback);
263 CALERegisterNetworkNotifications(netCallback);
265 CAConnectivityHandler_t connHandler;
266 connHandler.startAdapter = NULL;
267 connHandler.stopAdapter = CAStopLE;
268 connHandler.startListenServer = CAStartLEListeningServer;
269 connHandler.startDiscoveryServer = CAStartLEDiscoveryServer;
270 connHandler.sendData = CASendLEUnicastData;
271 connHandler.sendDataToAll = CASendLEMulticastData;
272 connHandler.GetnetInfo = CAGetLEInterfaceInformation;
273 connHandler.readData = CAReadLEData;
274 connHandler.terminate = CATerminateLE;
275 registerCallback(connHandler, CA_LE);
277 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
282 CAResult_t CAStopLE()
284 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
288 u_mutex_lock(g_bleIsServerMutex);
289 if (true == g_isServer)
291 CAStopBleGattServer();
295 CAStopBLEGattClient();
297 u_mutex_unlock(g_bleIsServerMutex);
299 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
306 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
308 CASetBLEReqRespServerCallback(NULL);
309 CASetBLEReqRespClientCallback(NULL);
310 CALERegisterNetworkNotifications(NULL);
311 CASetBLEReqRespAdapterCallback(NULL);
312 CATerminateLENetworkMonitor();
314 u_mutex_lock(g_bleIsServerMutex);
315 if (true == g_isServer)
317 CATerminateBleGattServer();
321 CATerminateBLEGattClient();
323 u_mutex_unlock(g_bleIsServerMutex);
325 CATerminateBleQueues();
327 CATerminateBleAdapterMutex();
329 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
333 CAResult_t CAStartLEListeningServer()
335 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
337 CAResult_t result = CAInitBleServerQueues();
338 if (CA_STATUS_OK != result)
340 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
341 return CA_STATUS_FAILED;
344 result = CAGetLEAdapterState();
345 if (CA_ADAPTER_NOT_ENABLED == result)
347 gLeServerStatus = CA_LISTENING_SERVER;
348 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
352 if (CA_STATUS_FAILED == result)
354 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
355 return CA_STATUS_FAILED;
358 CAStartBleGattServer();
360 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
361 u_mutex_lock(g_bleIsServerMutex);
363 u_mutex_unlock(g_bleIsServerMutex);
367 CAResult_t CAStartLEDiscoveryServer()
369 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
371 CAResult_t result = CAInitBleClientQueues();
372 if (CA_STATUS_OK != result)
374 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
375 return CA_STATUS_FAILED;
378 result = CAGetLEAdapterState();
379 if (CA_ADAPTER_NOT_ENABLED == result)
381 gLeServerStatus = CA_DISCOVERY_SERVER;
382 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
386 if (CA_STATUS_FAILED == result)
388 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
389 return CA_STATUS_FAILED;
392 CAStartBLEGattClient();
394 u_mutex_lock(g_bleIsServerMutex);
396 u_mutex_unlock(g_bleIsServerMutex);
398 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
402 CAResult_t CAStartLENotifyServer()
404 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
406 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
410 uint32_t CASendLENotification(const CARemoteEndpoint_t *endpoint, const void *data, uint32_t dataLen)
412 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
414 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
418 CAResult_t CAReadLEData()
420 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
422 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
426 int32_t CASendLEUnicastData(const CARemoteEndpoint_t *endpoint, const void *data, uint32_t dataLen)
428 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
431 VERIFY_NON_NULL_RET(endpoint, NULL, "Remote endpoint is null", -1);
432 VERIFY_NON_NULL_RET(data, NULL, "Data is null", -1);
434 CAResult_t result = CA_STATUS_FAILED;
436 u_mutex_lock(g_bleIsServerMutex);
437 if (true == g_isServer)
439 result = CABLEServerSendData(endpoint, data, dataLen);
440 if (CA_STATUS_OK != result)
442 OIC_LOG(ERROR, CALEADAPTER_TAG,
443 "[SendLEUnicastData] CABleServerSenderQueueEnqueueMessage failed \n");
444 u_mutex_unlock(g_bleIsServerMutex);
450 result = CABLEClientSendData(endpoint, data, dataLen);
451 if (CA_STATUS_OK != result)
453 OIC_LOG(ERROR, CALEADAPTER_TAG,
454 "[SendLEUnicastData] CABleClientSenderQueueEnqueueMessage failed \n");
455 u_mutex_unlock(g_bleIsServerMutex);
459 u_mutex_unlock(g_bleIsServerMutex);
461 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
465 int32_t CASendLEMulticastData(const void *data, uint32_t dataLen)
467 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
470 VERIFY_NON_NULL_RET(data, NULL, "Data is null", -1);
474 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
478 CAResult_t result = CA_STATUS_FAILED;
480 u_mutex_lock(g_bleIsServerMutex);
481 if (true == g_isServer)
483 result = CABLEServerSendData(NULL, data, dataLen);
484 if (CA_STATUS_OK != result)
486 OIC_LOG(ERROR, CALEADAPTER_TAG,
487 "[SendLEMulticastDataToAll] CABleServerSenderQueueEnqueueMessage failed" );
488 u_mutex_unlock(g_bleIsServerMutex);
494 result = CABLEClientSendData(NULL, data, dataLen);
495 if (CA_STATUS_OK != result)
497 OIC_LOG(ERROR, CALEADAPTER_TAG,
498 "[SendLEMulticastDataToAll] CABleClientSenderQueueEnqueueMessage failed" );
499 u_mutex_unlock(g_bleIsServerMutex);
503 u_mutex_unlock(g_bleIsServerMutex);
505 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
509 CAResult_t CAGetLEInterfaceInformation(CALocalConnectivity_t **info, uint32_t *size)
511 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
513 VERIFY_NON_NULL(info, NULL, "CALocalConnectivity info is null");
515 char *local_address = NULL;
517 CAGetLEAddress(&local_address);
518 if (NULL == local_address)
520 OIC_LOG(ERROR, CALEADAPTER_TAG, "Get local bt adapter address failed");
521 return CA_STATUS_FAILED;
525 (*info) = (CALocalConnectivity_t *) OICCalloc(1, sizeof(CALocalConnectivity_t));
528 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
529 return CA_STATUS_FAILED;
532 strncpy((*info)->addressInfo.BT.btMacAddress, local_address, strlen(local_address));
533 u_mutex_lock(g_bleLocalAddressMutex);
534 strncpy(g_localBLEAddress, local_address, sizeof(g_localBLEAddress));
535 u_mutex_unlock(g_bleLocalAddressMutex);
537 (*info)->type = CA_LE;
539 OICFree(local_address);
541 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
545 int32_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
547 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
549 u_mutex_lock(g_bleNetworkCbMutex);
550 g_networkCallback = netCallback;
551 u_mutex_unlock(g_bleNetworkCbMutex);
555 CAResult_t res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
556 if (CA_STATUS_OK != res)
558 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
563 CAResult_t res = CAUnSetLEAdapterStateChangedCb();
564 if (CA_STATUS_OK != res)
566 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
570 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
574 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
576 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
578 CALocalConnectivity_t localEndpoint = {};
580 u_mutex_lock(g_bleLocalAddressMutex);
581 strncpy(localEndpoint.addressInfo.BT.btMacAddress, g_localBLEAddress, strlen(g_localBLEAddress));
582 u_mutex_unlock(g_bleLocalAddressMutex);
584 // Start a GattServer/Client if gLeServerStatus is SET
585 if (CA_LISTENING_SERVER == gLeServerStatus)
587 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattServer");
588 CAStartBleGattServer();
590 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
592 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
593 CAStartBLEGattClient();
595 gLeServerStatus = CA_SERVER_NOTSTARTED;
597 u_mutex_lock(g_bleNetworkCbMutex);
598 if (NULL != g_networkCallback)
600 g_networkCallback(&localEndpoint, adapter_state);
604 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
606 u_mutex_unlock(g_bleNetworkCbMutex);
608 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
612 CAResult_t CAInitBleAdapterMutex()
614 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
616 if (NULL == g_bleIsServerMutex)
618 g_bleIsServerMutex = u_mutex_new();
619 if (NULL == g_bleIsServerMutex)
621 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
622 return CA_STATUS_FAILED;
626 if (NULL == g_bleNetworkCbMutex)
628 g_bleNetworkCbMutex = u_mutex_new();
629 if (NULL == g_bleNetworkCbMutex)
631 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
632 return CA_STATUS_FAILED;
636 if (NULL == g_bleLocalAddressMutex)
638 g_bleLocalAddressMutex = u_mutex_new();
639 if (NULL == g_bleLocalAddressMutex)
641 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
642 return CA_STATUS_FAILED;
646 if (NULL == g_bleAdapterThreadPoolMutex)
648 g_bleAdapterThreadPoolMutex = u_mutex_new();
649 if (NULL == g_bleAdapterThreadPoolMutex)
651 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
652 return CA_STATUS_FAILED;
656 if (NULL == g_bleClientSendDataMutex)
658 g_bleClientSendDataMutex = u_mutex_new();
659 if (NULL == g_bleClientSendDataMutex)
661 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
662 return CA_STATUS_FAILED;
666 if (NULL == g_bleClientReceiveDataMutex)
668 g_bleClientReceiveDataMutex = u_mutex_new();
669 if (NULL == g_bleClientReceiveDataMutex)
671 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
672 return CA_STATUS_FAILED;
676 if (NULL == g_bleServerSendDataMutex)
678 g_bleServerSendDataMutex = u_mutex_new();
679 if (NULL == g_bleServerSendDataMutex)
681 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
682 return CA_STATUS_FAILED;
686 if (NULL == g_bleServerReceiveDataMutex)
688 g_bleServerReceiveDataMutex = u_mutex_new();
689 if (NULL == g_bleServerReceiveDataMutex)
691 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
692 return CA_STATUS_FAILED;
696 if (NULL == g_bleAdapterReqRespCbMutex)
698 g_bleAdapterReqRespCbMutex = u_mutex_new();
699 if (NULL == g_bleAdapterReqRespCbMutex)
701 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
702 return CA_STATUS_FAILED;
706 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
710 void CATerminateBleAdapterMutex()
712 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
714 u_mutex_free(g_bleIsServerMutex);
715 g_bleIsServerMutex = NULL;
717 u_mutex_free(g_bleNetworkCbMutex);
718 g_bleNetworkCbMutex = NULL;
720 u_mutex_free(g_bleLocalAddressMutex);
721 g_bleLocalAddressMutex = NULL;
723 u_mutex_free(g_bleAdapterThreadPoolMutex);
724 g_bleAdapterThreadPoolMutex = NULL;
726 u_mutex_free(g_bleClientSendDataMutex);
727 g_bleClientSendDataMutex = NULL;
729 u_mutex_free(g_bleClientReceiveDataMutex);
730 g_bleClientReceiveDataMutex = NULL;
732 u_mutex_free(g_bleServerSendDataMutex);
733 g_bleServerSendDataMutex = NULL;
735 u_mutex_free(g_bleServerReceiveDataMutex);
736 g_bleServerReceiveDataMutex = NULL;
738 u_mutex_free(g_bleAdapterReqRespCbMutex);
739 g_bleAdapterReqRespCbMutex = NULL;
741 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
745 void CAInitBleQueues()
747 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
749 CAResult_t result = CAInitBleServerQueues();
750 if (CA_STATUS_OK != result)
752 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerQueues failed");
756 result = CAInitBleClientQueues();
757 if (CA_STATUS_OK != result)
759 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
763 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
767 CAResult_t CAInitBleServerQueues()
769 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
771 u_mutex_lock(g_bleAdapterThreadPoolMutex);
773 CAResult_t result = CAInitBleServerSenderQueue();
774 if (CA_STATUS_OK != result)
776 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
777 u_mutex_unlock(g_bleAdapterThreadPoolMutex);
778 return CA_STATUS_FAILED;
781 result = CAInitBleServerReceiverQueue();
782 if (CA_STATUS_OK != result)
784 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerReceiverQueue failed");
785 u_mutex_unlock(g_bleAdapterThreadPoolMutex);
786 return CA_STATUS_FAILED;
789 g_dataReceiverHandlerState = true;
791 u_mutex_unlock(g_bleAdapterThreadPoolMutex);
793 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
797 CAResult_t CAInitBleClientQueues()
799 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
801 u_mutex_lock(g_bleAdapterThreadPoolMutex);
803 CAResult_t result = CAInitBleClientSenderQueue();
804 if (CA_STATUS_OK != result)
806 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
807 u_mutex_unlock(g_bleAdapterThreadPoolMutex);
808 return CA_STATUS_FAILED;
811 result = CAInitBleClientReceiverQueue();
812 if (CA_STATUS_OK != result)
814 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientReceiverQueue failed");
815 u_mutex_unlock(g_bleAdapterThreadPoolMutex);
816 return CA_STATUS_FAILED;
819 g_dataReceiverHandlerState = true;
821 u_mutex_unlock(g_bleAdapterThreadPoolMutex);
823 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
827 CAResult_t CAInitBleServerSenderQueue()
829 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
830 // Check if the message queue is already initialized
831 if (g_sendQueueHandle)
833 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
837 // Create send message queue
838 g_sendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
839 if (!g_sendQueueHandle)
841 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
842 return CA_MEMORY_ALLOC_FAILED;
845 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_sendQueueHandle, g_bleAdapterThreadPool,
846 CABLEServerSendDataThread, CALEDataDestroyer))
848 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
849 OICFree(g_sendQueueHandle);
850 g_sendQueueHandle = NULL;
851 return CA_STATUS_FAILED;
854 if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle))
856 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "u_thread_pool_add_task failed ");
857 OICFree(g_sendQueueHandle);
858 g_sendQueueHandle = NULL;
859 return CA_STATUS_FAILED;
862 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
866 CAResult_t CAInitBleClientSenderQueue()
868 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
870 if (g_bLEClientSendQueueHandle)
872 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
876 // Create send message queue
877 g_bLEClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
878 if (!g_bLEClientSendQueueHandle)
880 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
881 return CA_MEMORY_ALLOC_FAILED;
884 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bLEClientSendQueueHandle, g_bleAdapterThreadPool,
885 CABLEClientSendDataThread, CALEDataDestroyer))
887 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
888 OICFree(g_bLEClientSendQueueHandle);
889 g_bLEClientSendQueueHandle = NULL;
890 return CA_STATUS_FAILED;
893 if (CA_STATUS_OK != CAQueueingThreadStart(g_bLEClientSendQueueHandle))
895 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "u_thread_pool_add_task failed ");
896 OICFree(g_bLEClientSendQueueHandle);
897 g_bLEClientSendQueueHandle = NULL;
898 return CA_STATUS_FAILED;
901 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
905 CAResult_t CAInitBleServerReceiverQueue()
907 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
908 // Check if the message queue is already initialized
909 if (g_bleServerReceiverQueue)
911 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
915 // Create send message queue
916 g_bleServerReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
917 if (!g_bleServerReceiverQueue)
919 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
920 OICFree(g_sendQueueHandle);
921 return CA_MEMORY_ALLOC_FAILED;
924 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerReceiverQueue, g_bleAdapterThreadPool,
925 CABLEServerDataReceiverHandler, CALEDataDestroyer))
927 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
928 OICFree(g_bleServerReceiverQueue);
929 g_bleServerReceiverQueue = NULL;
930 return CA_STATUS_FAILED;
933 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerReceiverQueue))
935 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "u_thread_pool_add_task failed ");
936 OICFree(g_bleServerReceiverQueue);
937 g_bleServerReceiverQueue = NULL;
938 return CA_STATUS_FAILED;
941 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
945 CAResult_t CAInitBleClientReceiverQueue()
947 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
949 // Check if the message queue is already initialized
950 if (g_bleClientReceiverQueue)
952 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
956 // Create send message queue
957 g_bleClientReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
958 if (!g_bleClientReceiverQueue)
960 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
961 OICFree(g_bLEClientSendQueueHandle);
962 return CA_MEMORY_ALLOC_FAILED;
965 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientReceiverQueue, g_bleAdapterThreadPool,
966 CABLEClientDataReceiverHandler, NULL))
968 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
969 OICFree(g_bLEClientSendQueueHandle);
970 OICFree(g_bleClientReceiverQueue);
971 g_bleClientReceiverQueue = NULL;
972 return CA_STATUS_FAILED;
975 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientReceiverQueue))
977 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "u_thread_pool_add_task failed ");
978 OICFree(g_bleClientReceiverQueue);
979 g_bleClientReceiverQueue = NULL;
980 return CA_STATUS_FAILED;
983 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
987 void CAStopBleQueues()
989 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
991 u_mutex_lock(g_bleClientSendDataMutex);
992 if (NULL != g_bLEClientSendQueueHandle)
994 CAQueueingThreadStop(g_bLEClientSendQueueHandle);
996 u_mutex_unlock(g_bleClientSendDataMutex);
998 u_mutex_lock(g_bleClientReceiveDataMutex);
999 if (NULL != g_bleClientReceiverQueue)
1001 CAQueueingThreadStop(g_bleClientReceiverQueue);
1003 u_mutex_unlock(g_bleClientReceiveDataMutex);
1005 u_mutex_lock(g_bleServerSendDataMutex);
1006 if (NULL != g_sendQueueHandle)
1008 CAQueueingThreadStop(g_sendQueueHandle);
1010 u_mutex_unlock(g_bleServerSendDataMutex);
1012 u_mutex_lock(g_bleServerReceiveDataMutex);
1013 if (NULL != g_bleServerReceiverQueue)
1015 CAQueueingThreadStop(g_bleServerReceiverQueue);
1017 u_mutex_unlock(g_bleServerReceiveDataMutex);
1019 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1022 void CATerminateBleQueues()
1024 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1026 CAQueueingThreadDestroy(g_bLEClientSendQueueHandle);
1027 OICFree(g_bLEClientSendQueueHandle);
1028 g_bLEClientSendQueueHandle = NULL;
1031 CAQueueingThreadDestroy(g_bleClientReceiverQueue);
1032 OICFree(g_bleClientReceiverQueue);
1033 g_bleClientReceiverQueue = NULL;
1036 CAQueueingThreadDestroy(g_sendQueueHandle);
1037 OICFree(g_sendQueueHandle);
1038 g_sendQueueHandle = NULL;
1041 CAQueueingThreadDestroy(g_bleServerReceiverQueue);
1042 OICFree(g_bleServerReceiverQueue);
1043 g_bleServerReceiverQueue = NULL;
1045 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1047 void CABLEServerDataReceiverHandler(void *threadData)
1049 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN");
1051 static uint32_t recvDataLen = 0;
1052 static uint32_t totalDataLen = 0;
1053 static char *defragData = NULL;
1054 static bool isHeaderAvailable = false;
1055 static CARemoteEndpoint_t *remoteEndpoint = NULL;
1057 u_mutex_lock(g_bleClientReceiveDataMutex);
1059 if (g_dataReceiverHandlerState)
1061 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1063 CALEData_t *bleData = (CALEData_t *) threadData;
1066 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid wifidata!");
1070 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1072 if (!isHeaderAvailable)
1074 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1075 totalDataLen = CAParseHeader((char*)bleData->data);
1077 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes", totalDataLen);
1078 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%d] bytes", bleData->dataLen);
1080 defragData = (char *) OICCalloc(totalDataLen + 1, sizeof(char));
1081 if (NULL == defragData)
1083 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1087 const char *remoteAddress = bleData->remoteEndpoint->addressInfo.LE.leMacAddress;
1088 const char *serviceUUID = bleData->remoteEndpoint->resourceUri;
1090 remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1093 memcpy(defragData + recvDataLen, bleData->data + CA_HEADER_LENGTH,
1094 bleData->dataLen - CA_HEADER_LENGTH);
1095 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1096 isHeaderAvailable = true;
1100 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1101 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1102 recvDataLen += bleData->dataLen ;
1103 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1104 totalDataLen, recvDataLen);
1106 if (totalDataLen == recvDataLen)
1108 u_mutex_lock(g_bleAdapterReqRespCbMutex);
1109 if (NULL == g_networkPacketReceivedCallback)
1111 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1112 u_mutex_unlock(g_bleAdapterReqRespCbMutex);
1115 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1116 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1119 isHeaderAvailable = false;
1120 remoteEndpoint = NULL;
1122 u_mutex_unlock(g_bleAdapterReqRespCbMutex);
1125 if (false == g_dataReceiverHandlerState)
1127 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1130 isHeaderAvailable = false;
1131 OICFree(defragData);
1132 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1133 u_mutex_unlock(g_bleClientReceiveDataMutex);
1137 u_mutex_unlock(g_bleClientReceiveDataMutex);
1138 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT");
1141 void CABLEClientDataReceiverHandler(void *threadData)
1143 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN");
1145 static const char *remoteAddress = NULL;
1146 static const char *serviceUUID = NULL;
1147 static uint32_t recvDataLen = 0;
1148 static uint32_t totalDataLen = 0;
1149 static char *defragData = NULL;
1150 static bool isHeaderAvailable = false;
1151 static CARemoteEndpoint_t *remoteEndpoint = NULL;
1153 u_mutex_lock(g_bleClientReceiveDataMutex);
1155 if (g_dataReceiverHandlerState)
1157 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1159 CALEData_t *bleData = (CALEData_t *) threadData;
1162 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid wifidata!");
1166 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1168 if (!isHeaderAvailable)
1170 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1172 totalDataLen = CAParseHeader(bleData->data);
1173 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes",
1175 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received in the first packet [%d] bytes",
1178 defragData = (char *) OICMalloc(sizeof(char) * totalDataLen);
1179 if (NULL == defragData)
1181 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1185 remoteAddress = bleData->remoteEndpoint->addressInfo.LE.leMacAddress;
1186 serviceUUID = bleData->remoteEndpoint->resourceUri;
1188 remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1191 memcpy(defragData, bleData->data + CA_HEADER_LENGTH,
1192 bleData->dataLen - CA_HEADER_LENGTH);
1193 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1194 isHeaderAvailable = true;
1198 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1199 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1200 recvDataLen += bleData->dataLen ;
1201 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1202 totalDataLen, recvDataLen);
1204 if (totalDataLen == recvDataLen)
1206 u_mutex_lock(g_bleAdapterReqRespCbMutex);
1207 if (NULL == g_networkPacketReceivedCallback)
1209 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1210 u_mutex_unlock(g_bleAdapterReqRespCbMutex);
1213 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1214 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1217 isHeaderAvailable = false;
1218 remoteEndpoint = NULL;
1220 u_mutex_unlock(g_bleAdapterReqRespCbMutex);
1223 if (false == g_dataReceiverHandlerState)
1225 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1226 OICFree(defragData);
1227 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1228 u_mutex_unlock(g_bleClientReceiveDataMutex);
1232 u_mutex_unlock(g_bleClientReceiveDataMutex);
1233 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT");
1236 void CABLEServerSendDataThread(void *threadData)
1238 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1240 CALEData_t *bleData = (CALEData_t *) threadData;
1243 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1247 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1248 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1250 char *dataSegment = (char *) OICCalloc(bleData->dataLen + CA_HEADER_LENGTH + 1, sizeof(char));
1251 if (NULL == dataSegment)
1253 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1258 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1259 if (CA_STATUS_OK != result )
1261 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1263 OICFree(dataSegment);
1267 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1271 if (CA_SUPPORTED_BLE_MTU_SIZE >= bleData->dataLen)
1273 length = bleData->dataLen + CA_HEADER_LENGTH;
1274 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1275 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1279 length = CA_SUPPORTED_BLE_MTU_SIZE;
1280 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1281 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1282 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1285 int32_t iter = bleData->dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
1287 u_mutex_lock(g_bleServerSendDataMutex);
1288 // Send the first segment with the header.
1289 result = CAUpdateCharacteristicsInGattServer(dataSegment, length);
1290 if (CA_STATUS_OK != result )
1292 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAUpdateCharacteristicsInGattServer failed");
1293 OICFree(dataSegment);
1294 u_mutex_unlock(g_bleServerSendDataMutex);
1298 for (index = 1; index < iter; index++)
1300 // Send the remaining header.
1301 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1302 result = CAUpdateCharacteristicsInGattServer(
1303 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1304 CA_SUPPORTED_BLE_MTU_SIZE);
1305 if (CA_STATUS_OK != result )
1307 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAUpdateCharacteristicsInGattServer failed");
1308 OICFree(dataSegment);
1309 u_mutex_unlock(g_bleServerSendDataMutex);
1314 if (bleData->dataLen / CA_SUPPORTED_BLE_MTU_SIZE)
1316 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1317 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1318 result = CAUpdateCharacteristicsInGattServer(
1319 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1320 bleData->dataLen % CA_SUPPORTED_BLE_MTU_SIZE + CA_HEADER_LENGTH);
1321 if (CA_STATUS_OK != result )
1323 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAUpdateCharacteristicsInGattServer failed");
1324 OICFree(dataSegment);
1325 u_mutex_unlock(g_bleServerSendDataMutex);
1330 OICFree(dataSegment);
1331 u_mutex_unlock(g_bleServerSendDataMutex); // TODO: this mutex required ?
1333 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1336 void CABLEClientSendDataThread(void *threadData)
1338 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1340 CALEData_t *bleData = (CALEData_t *) threadData;
1343 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1347 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1348 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1350 char *dataSegment = (char *) OICCalloc(bleData->dataLen + CA_HEADER_LENGTH + 1, sizeof(char));
1351 if (NULL == dataSegment)
1353 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1358 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1359 if (CA_STATUS_OK != result )
1361 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1363 OICFree(dataSegment);
1366 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1370 if (CA_SUPPORTED_BLE_MTU_SIZE >= bleData->dataLen)
1372 length = bleData->dataLen + CA_HEADER_LENGTH;
1373 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1374 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1378 length = CA_SUPPORTED_BLE_MTU_SIZE;
1379 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1380 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1381 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1384 int32_t iter = bleData->dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
1386 u_mutex_lock(g_bleClientSendDataMutex);
1387 if (NULL != bleData->remoteEndpoint)
1389 // Send the first segment with the header.
1390 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1394 if (CA_STATUS_OK != result )
1396 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAUpdateCharacteristicsToGattServer failed");
1397 OICFree(dataSegment);
1398 u_mutex_unlock(g_bleClientSendDataMutex);
1402 for (index = 1; index < iter; index++)
1404 // Send the remaining header.
1405 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1406 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1407 CA_SUPPORTED_BLE_MTU_SIZE,
1409 if (CA_STATUS_OK != result )
1411 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAUpdateCharacteristicsToGattServer failed");
1412 OICFree(dataSegment);
1413 u_mutex_unlock(g_bleClientSendDataMutex);
1418 if (bleData->dataLen / CA_SUPPORTED_BLE_MTU_SIZE)
1420 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1421 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1422 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1423 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1424 bleData->dataLen % CA_SUPPORTED_BLE_MTU_SIZE + CA_HEADER_LENGTH,
1426 if (CA_STATUS_OK != result )
1428 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAUpdateCharacteristicsToGattServer failed");
1429 OICFree(dataSegment);
1430 u_mutex_unlock(g_bleClientSendDataMutex);
1437 // Send the first segment with the header.
1438 result = CAUpdateCharacteristicsToAllGattServers(dataSegment + (index * length),
1440 if (CA_STATUS_OK != result )
1442 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAUpdateCharacteristicsToGattServer failed");
1443 OICFree(dataSegment);
1444 u_mutex_unlock(g_bleClientSendDataMutex);
1447 // Send the remaining header.
1448 for (index = 1; index < iter; index++)
1450 result = CAUpdateCharacteristicsToAllGattServers(
1451 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1452 CA_SUPPORTED_BLE_MTU_SIZE);
1453 if (CA_STATUS_OK != result )
1455 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAUpdateCharacteristicsToGattServer failed");
1456 OICFree(dataSegment);
1457 u_mutex_unlock(g_bleClientSendDataMutex);
1461 if (bleData->dataLen / CA_SUPPORTED_BLE_MTU_SIZE)
1463 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1464 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1465 result = CAUpdateCharacteristicsToAllGattServers(
1466 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1467 bleData->dataLen % CA_SUPPORTED_BLE_MTU_SIZE + CA_HEADER_LENGTH);
1468 if (CA_STATUS_OK != result )
1470 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAUpdateCharacteristicsToGattServer failed");
1471 OICFree(dataSegment);
1472 u_mutex_unlock(g_bleClientSendDataMutex);
1478 OICFree(dataSegment);
1479 u_mutex_unlock(g_bleClientSendDataMutex);
1481 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1484 CALEData_t *CACreateBLEData(const CARemoteEndpoint_t *remoteEndpoint, const void *data,
1485 uint32_t dataLength)
1487 CALEData_t *bleData = (CALEData_t *) OICMalloc(sizeof(CALEData_t));
1490 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1494 bleData->remoteEndpoint = CAAdapterCopyRemoteEndpoint(remoteEndpoint);
1495 bleData->data = (void *)OICCalloc(dataLength + 1, 1);
1496 if (NULL == bleData->data)
1498 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1499 CAFreeBLEData(bleData);
1502 memcpy(bleData->data, data, dataLength);
1503 bleData->dataLen = dataLength;
1508 void CAFreeBLEData(CALEData_t *bleData)
1513 CAAdapterFreeRemoteEndpoint(bleData->remoteEndpoint);
1514 OICFree(bleData->data);
1518 void CALEDataDestroyer(void *data, uint32_t size)
1520 CALEData_t *ledata = (CALEData_t *) data;
1522 CAFreeBLEData(ledata);
1526 CAResult_t CABLEClientSendData(const CARemoteEndpoint_t *remoteEndpoint,
1530 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1532 VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1534 VERIFY_NON_NULL_RET(g_bLEClientSendQueueHandle, CALEADAPTER_TAG,
1535 "g_bLEClientSendQueueHandle is NULL",
1537 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
1538 "g_bleClientSendDataMutex is NULL",
1541 VERIFY_NON_NULL_RET(g_bLEClientSendQueueHandle, CALEADAPTER_TAG, "g_bLEClientSendQueueHandle",
1544 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1546 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1549 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1550 return CA_MEMORY_ALLOC_FAILED;
1552 // Add message to send queue
1553 u_mutex_lock(g_bleClientSendDataMutex);
1554 CAQueueingThreadAddData(g_bLEClientSendQueueHandle, bleData, sizeof(CALEData_t));
1555 u_mutex_unlock(g_bleClientSendDataMutex);
1557 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1558 return CA_STATUS_OK;
1562 CAResult_t CABLEServerSendData(const CARemoteEndpoint_t *remoteEndpoint,
1566 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1568 VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1570 VERIFY_NON_NULL_RET(g_sendQueueHandle, CALEADAPTER_TAG,
1571 "BleClientReceiverQueue is NULL",
1573 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
1574 "BleClientSendDataMutex is NULL",
1577 VERIFY_NON_NULL_RET(g_sendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
1580 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1582 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1585 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1586 return CA_MEMORY_ALLOC_FAILED;
1588 // Add message to send queue
1589 u_mutex_lock(g_bleServerSendDataMutex);
1590 CAQueueingThreadAddData(g_sendQueueHandle, bleData, sizeof(CALEData_t));
1591 u_mutex_unlock(g_bleServerSendDataMutex);
1593 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1594 return CA_STATUS_OK;
1597 CAResult_t CABLEServerReceivedData(const char *remoteAddress, const char *serviceUUID,
1598 const void *data, uint32_t dataLength, uint32_t *sentLength)
1600 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN");
1603 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1604 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1605 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1606 VERIFY_NON_NULL_RET(g_bleServerReceiverQueue, CALEADAPTER_TAG, "g_bleServerReceiverQueue",
1609 //Add message to data queue
1610 CARemoteEndpoint_t *remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1612 if (NULL == remoteEndpoint)
1614 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1615 return CA_STATUS_FAILED;
1618 // Create bleData to add to queue
1619 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1621 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1624 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1625 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1626 return CA_MEMORY_ALLOC_FAILED;
1629 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1630 // Add message to send queue
1631 CAQueueingThreadAddData(g_bleServerReceiverQueue, bleData, sizeof(CALEData_t));
1633 *sentLength = dataLength;
1635 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT");
1636 return CA_STATUS_OK;
1639 CAResult_t CABLEClientReceivedData(const char *remoteAddress, const char *serviceUUID,
1640 const void *data, uint32_t dataLength, uint32_t *sentLength)
1642 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN");
1645 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1646 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1647 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1648 VERIFY_NON_NULL_RET(g_bleClientReceiverQueue, CALEADAPTER_TAG, "g_bleClientReceiverQueue",
1651 //Add message to data queue
1652 CARemoteEndpoint_t *remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1654 if (NULL == remoteEndpoint)
1656 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1657 return CA_STATUS_FAILED;
1660 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1662 // Create bleData to add to queue
1663 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1666 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1667 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1668 return CA_MEMORY_ALLOC_FAILED;
1671 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1672 // Add message to send queue
1673 CAQueueingThreadAddData(g_bleClientReceiverQueue, bleData, sizeof(CALEData_t));
1675 *sentLength = dataLength;
1677 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT");
1678 return CA_STATUS_OK;
1681 void CASetBleAdapterThreadPoolHandle(u_thread_pool_t handle)
1683 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1685 u_mutex_unlock(g_bleAdapterThreadPoolMutex);
1686 g_bleAdapterThreadPool = handle;
1687 u_mutex_unlock(g_bleAdapterThreadPoolMutex);
1689 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1692 void CASetBLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
1694 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1696 u_mutex_lock(g_bleAdapterReqRespCbMutex);
1698 g_networkPacketReceivedCallback = callback;
1700 u_mutex_unlock(g_bleAdapterReqRespCbMutex);
1702 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");