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"
32 #include "oic_string.h"
35 * @var CALEADAPTER_TAG
36 * @brief Logging tag for module name.
38 #define CALEADAPTER_TAG "CA_BLE_ADAPTER"
41 * @var g_networkCallback
42 * @brief Callback to provide the status of the network change to CA layer.
44 static CANetworkChangeCallback g_networkCallback = NULL;
47 * @var g_localBLEAddress
48 * @brief bleAddress of the local adapter. Value will be initialized to zero, and will be updated later.
50 static char g_localBLEAddress[16] = {0};
54 * @brief Variable to differentiate btw GattServer and GattClient.
56 static bool g_isServer = false;
59 * @var g_bleIsServerMutex
60 * @brief Mutex to synchronize the task to be executed on the GattServer function calls.
62 static ca_mutex g_bleIsServerMutex = NULL;
65 * @var g_bleNetworkCbMutex
66 * @brief Mutex to synchronize the callback to be called for the network changes.
68 static ca_mutex g_bleNetworkCbMutex = NULL;
71 * @var g_bleLocalAddressMutex
72 * @brief Mutex to synchronize the updation of the local LE address of the adapter.
74 static ca_mutex g_bleLocalAddressMutex = NULL;
77 * @var g_bleAdapterThreadPool
78 * @brief reference to threadpool
80 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
83 * @var g_bleAdapterThreadPoolMutex
84 * @brief Mutex to synchronize the task to be pushed to thread pool.
86 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
89 * @var g_bleClientSendQueueHandle
90 * @brief Queue to process the outgoing packets from GATTClient.
92 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
95 * @var g_bleClientReceiverQueue
96 * @brief Queue to process the incoming packets to GATT Client.
98 static CAQueueingThread_t *g_bleClientReceiverQueue = NULL;
101 * @var g_bleClientSendDataMutex
102 * @brief Mutex to synchronize the queing of the data from SenderQueue.
104 static ca_mutex g_bleClientSendDataMutex = NULL;
107 * @var g_bleClientReceiveDataMutex
108 * @brief Mutex to synchronize the queing of the data from ReceiverQueue.
110 static ca_mutex g_bleClientReceiveDataMutex = NULL;
113 * @var g_dataReceiverHandlerState
114 * @brief Stop condition of recvhandler.
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 ca_mutex g_bleServerSendDataMutex = NULL;
137 * @var g_bleServerReceiveDataMutex
138 * @brief Mutex to synchronize the queing of the data from ReceiverQueue.
140 static ca_mutex g_bleServerReceiveDataMutex = NULL;
143 * @var g_bleAdapterReqRespCbMutex
144 * @brief Mutex to synchronize the callback to be called for the adapterReqResponse.
146 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
149 * @var g_networkPacketReceivedCallback
150 * @brief Callback to be called when network packet recieved from either GattServer or GattClient.
152 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
155 * @ENUM CALeServerStatus
156 * @brief status of BLE Server Status
157 * This ENUM provides information of LE Adapter Server status
161 CA_SERVER_NOTSTARTED = 0,
167 * @var gLeServerStatus
168 * @brief structure to maintain the status of the server.
170 static CALeServerStatus gLeServerStatus = CA_SERVER_NOTSTARTED;
173 * @fn CALERegisterNetworkNotifications
174 * @brief This function is used to register network change notification callback.
176 * @param[in] netCallback CANetworkChangeCallback callback which will be set for the change in nwk.
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
184 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
187 * @fn CASetBleAdapterThreadPoolHandle
188 * @brief Used to Set the gThreadPool handle which is required for spawning new thread.
190 * @param[in] handle - Thread pool handle which is given by above layer for using thread creation task.
195 void CASetBleAdapterThreadPoolHandle(ca_thread_pool_t handle);
198 * @fn CALEDeviceStateChangedCb
199 * @brief This function is used to call the callback to the upper layer when the device state gets
202 * @param[in] adapter_state New state of the adapter to be notified to the upper layer.
207 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state);
210 * @fn CAInitBleAdapterMutex
211 * @brief Used to initialize all required mutex variable for LE Adapter implementation.
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
219 CAResult_t CAInitBleAdapterMutex();
222 * @fn CATerminateBleAdapterMutex
223 * @brief Used to terminate all required mutex variable for LE adapter implementation.
227 void CATerminateBleAdapterMutex();
230 * @fn CALEDataDestroyer
231 * @brief Used to free data
235 static void CALEDataDestroyer(void *data, uint32_t size);
237 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
238 CANetworkPacketReceivedCallback reqRespCallback,
239 CANetworkChangeCallback netCallback,
240 ca_thread_pool_t handle)
242 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
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");
249 CAResult_t result = CAInitBleAdapterMutex();
250 if (CA_STATUS_OK != result)
252 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
253 return CA_STATUS_FAILED;
256 result = CAInitializeLENetworkMonitor();
257 if (CA_STATUS_OK != result)
259 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
260 return CA_STATUS_FAILED;
263 CAInitializeLEAdapter();
265 CASetBleClientThreadPoolHandle(handle);
266 CASetBleServerThreadPoolHandle(handle);
267 CASetBleAdapterThreadPoolHandle(handle);
268 CASetBLEReqRespServerCallback(CABLEServerReceivedData);
269 CASetBLEReqRespClientCallback(CABLEClientReceivedData);
270 CASetBLEReqRespAdapterCallback(reqRespCallback);
272 CALERegisterNetworkNotifications(netCallback);
274 CAConnectivityHandler_t connHandler;
275 connHandler.startAdapter = CAStartLE;
276 connHandler.stopAdapter = CAStopLE;
277 connHandler.startListenServer = CAStartLEListeningServer;
278 connHandler.startDiscoveryServer = CAStartLEDiscoveryServer;
279 connHandler.sendData = CASendLEUnicastData;
280 connHandler.sendDataToAll = CASendLEMulticastData;
281 connHandler.GetnetInfo = CAGetLEInterfaceInformation;
282 connHandler.readData = CAReadLEData;
283 connHandler.terminate = CATerminateLE;
284 registerCallback(connHandler, CA_LE);
286 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
291 CAResult_t CAStartLE()
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");
300 CAResult_t CAStopLE()
302 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
306 ca_mutex_lock(g_bleIsServerMutex);
307 if (true == g_isServer)
309 CAStopBleGattServer();
313 CAStopBLEGattClient();
315 ca_mutex_unlock(g_bleIsServerMutex);
317 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
324 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
326 CASetBLEReqRespServerCallback(NULL);
327 CASetBLEReqRespClientCallback(NULL);
328 CALERegisterNetworkNotifications(NULL);
329 CASetBLEReqRespAdapterCallback(NULL);
330 CATerminateLENetworkMonitor();
332 ca_mutex_lock(g_bleIsServerMutex);
333 if (true == g_isServer)
335 CATerminateBleGattServer();
339 CATerminateBLEGattClient();
341 ca_mutex_unlock(g_bleIsServerMutex);
343 CATerminateBleQueues();
345 CATerminateBleAdapterMutex();
347 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
350 CAResult_t CAStartLEListeningServer()
352 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
354 CAResult_t result = CAInitBleServerQueues();
355 if (CA_STATUS_OK != result)
357 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
358 return CA_STATUS_FAILED;
361 result = CAGetLEAdapterState();
362 if (CA_ADAPTER_NOT_ENABLED == result)
364 gLeServerStatus = CA_LISTENING_SERVER;
365 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
369 if (CA_STATUS_FAILED == result)
371 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
372 return CA_STATUS_FAILED;
375 CAStartBleGattServer();
377 ca_mutex_lock(g_bleIsServerMutex);
379 ca_mutex_unlock(g_bleIsServerMutex);
381 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
385 CAResult_t CAStartLEDiscoveryServer()
387 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
389 CAResult_t result = CAInitBleClientQueues();
390 if (CA_STATUS_OK != result)
392 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
393 return CA_STATUS_FAILED;
396 result = CAGetLEAdapterState();
397 if (CA_ADAPTER_NOT_ENABLED == result)
399 gLeServerStatus = CA_DISCOVERY_SERVER;
400 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
404 if (CA_STATUS_FAILED == result)
406 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
407 return CA_STATUS_FAILED;
410 CAStartBLEGattClient();
412 ca_mutex_lock(g_bleIsServerMutex);
414 ca_mutex_unlock(g_bleIsServerMutex);
416 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
420 CAResult_t CAStartLENotifyServer()
422 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
424 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
428 uint32_t CASendLENotification(const CARemoteEndpoint_t *endpoint, const void *data, uint32_t dataLen)
430 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
432 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
436 CAResult_t CAReadLEData()
438 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
440 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
444 int32_t CASendLEUnicastData(const CARemoteEndpoint_t *endpoint, const void *data, uint32_t dataLen)
446 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
449 VERIFY_NON_NULL_RET(endpoint, NULL, "Remote endpoint is null", -1);
450 VERIFY_NON_NULL_RET(data, NULL, "Data is null", -1);
452 CAResult_t result = CA_STATUS_FAILED;
454 ca_mutex_lock(g_bleIsServerMutex);
455 if (true == g_isServer)
457 result = CABLEServerSendData(endpoint, data, dataLen);
458 if (CA_STATUS_OK != result)
460 OIC_LOG(ERROR, CALEADAPTER_TAG,
461 "[SendLEUnicastData] CABleServerSenderQueueEnqueueMessage failed \n");
462 ca_mutex_unlock(g_bleIsServerMutex);
468 result = CABLEClientSendData(endpoint, data, dataLen);
469 if (CA_STATUS_OK != result)
471 OIC_LOG(ERROR, CALEADAPTER_TAG,
472 "[SendLEUnicastData] CABleClientSenderQueueEnqueueMessage failed \n");
473 ca_mutex_unlock(g_bleIsServerMutex);
477 ca_mutex_unlock(g_bleIsServerMutex);
479 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
483 int32_t CASendLEMulticastData(const void *data, uint32_t dataLen)
485 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
488 VERIFY_NON_NULL_RET(data, NULL, "Data is null", -1);
492 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
496 CAResult_t result = CA_STATUS_FAILED;
498 ca_mutex_lock(g_bleIsServerMutex);
499 if (true == g_isServer)
501 result = CABLEServerSendData(NULL, data, dataLen);
502 if (CA_STATUS_OK != result)
504 OIC_LOG(ERROR, CALEADAPTER_TAG,
505 "[SendLEMulticastDataToAll] CABleServerSenderQueueEnqueueMessage failed" );
506 ca_mutex_unlock(g_bleIsServerMutex);
512 result = CABLEClientSendData(NULL, data, dataLen);
513 if (CA_STATUS_OK != result)
515 OIC_LOG(ERROR, CALEADAPTER_TAG,
516 "[SendLEMulticastDataToAll] CABleClientSenderQueueEnqueueMessage failed" );
517 ca_mutex_unlock(g_bleIsServerMutex);
521 ca_mutex_unlock(g_bleIsServerMutex);
523 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
527 CAResult_t CAGetLEInterfaceInformation(CALocalConnectivity_t **info, uint32_t *size)
529 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
531 VERIFY_NON_NULL(info, NULL, "CALocalConnectivity info is null");
533 char *local_address = NULL;
535 CAResult_t res = CAGetLEAddress(&local_address);
536 if (CA_STATUS_OK != res)
538 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
542 if (NULL == local_address)
544 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
545 return CA_STATUS_FAILED;
549 (*info) = (CALocalConnectivity_t *) OICCalloc(1, sizeof(CALocalConnectivity_t));
552 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
553 OICFree(local_address);
554 return CA_STATUS_FAILED;
557 size_t local_address_len = strlen(local_address);
559 if(local_address_len >= sizeof(g_localBLEAddress) ||
560 local_address_len >= sizeof((*info)->addressInfo.BT.btMacAddress))
562 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
564 OICFree(local_address);
565 return CA_STATUS_FAILED;
568 OICStrcpy((*info)->addressInfo.BT.btMacAddress, sizeof ((*info)->addressInfo.BT.btMacAddress),
570 ca_mutex_lock(g_bleLocalAddressMutex);
571 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
572 ca_mutex_unlock(g_bleLocalAddressMutex);
574 (*info)->type = CA_LE;
576 OICFree(local_address);
578 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
582 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
584 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
586 ca_mutex_lock(g_bleNetworkCbMutex);
587 g_networkCallback = netCallback;
588 ca_mutex_unlock(g_bleNetworkCbMutex);
589 CAResult_t res = CA_STATUS_OK;
592 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
593 if (CA_STATUS_OK != res)
595 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
600 res = CAUnSetLEAdapterStateChangedCb();
601 if (CA_STATUS_OK != res)
603 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
607 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
611 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
613 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
615 VERIFY_NON_NULL_VOID(g_localBLEAddress, NULL, "g_localBLEAddress is null");
616 CALocalConnectivity_t localEndpoint = {};
618 ca_mutex_lock(g_bleLocalAddressMutex);
619 OICStrcpy(localEndpoint.addressInfo.BT.btMacAddress,
620 sizeof(localEndpoint.addressInfo.BT.btMacAddress),
622 ca_mutex_unlock(g_bleLocalAddressMutex);
624 // Start a GattServer/Client if gLeServerStatus is SET
625 if (CA_LISTENING_SERVER == gLeServerStatus)
627 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattServer");
628 CAStartBleGattServer();
630 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
632 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
633 CAStartBLEGattClient();
635 gLeServerStatus = CA_SERVER_NOTSTARTED;
637 ca_mutex_lock(g_bleNetworkCbMutex);
638 if (NULL != g_networkCallback)
640 g_networkCallback(&localEndpoint, adapter_state);
644 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
646 ca_mutex_unlock(g_bleNetworkCbMutex);
648 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
651 CAResult_t CAInitBleAdapterMutex()
653 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
655 if (NULL == g_bleIsServerMutex)
657 g_bleIsServerMutex = ca_mutex_new();
658 if (NULL == g_bleIsServerMutex)
660 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
661 return CA_STATUS_FAILED;
665 if (NULL == g_bleNetworkCbMutex)
667 g_bleNetworkCbMutex = ca_mutex_new();
668 if (NULL == g_bleNetworkCbMutex)
670 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
671 return CA_STATUS_FAILED;
675 if (NULL == g_bleLocalAddressMutex)
677 g_bleLocalAddressMutex = ca_mutex_new();
678 if (NULL == g_bleLocalAddressMutex)
680 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
681 return CA_STATUS_FAILED;
685 if (NULL == g_bleAdapterThreadPoolMutex)
687 g_bleAdapterThreadPoolMutex = ca_mutex_new();
688 if (NULL == g_bleAdapterThreadPoolMutex)
690 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
691 return CA_STATUS_FAILED;
695 if (NULL == g_bleClientSendDataMutex)
697 g_bleClientSendDataMutex = ca_mutex_new();
698 if (NULL == g_bleClientSendDataMutex)
700 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
701 return CA_STATUS_FAILED;
705 if (NULL == g_bleClientReceiveDataMutex)
707 g_bleClientReceiveDataMutex = ca_mutex_new();
708 if (NULL == g_bleClientReceiveDataMutex)
710 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
711 return CA_STATUS_FAILED;
715 if (NULL == g_bleServerSendDataMutex)
717 g_bleServerSendDataMutex = ca_mutex_new();
718 if (NULL == g_bleServerSendDataMutex)
720 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
721 return CA_STATUS_FAILED;
725 if (NULL == g_bleServerReceiveDataMutex)
727 g_bleServerReceiveDataMutex = ca_mutex_new();
728 if (NULL == g_bleServerReceiveDataMutex)
730 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
731 return CA_STATUS_FAILED;
735 if (NULL == g_bleAdapterReqRespCbMutex)
737 g_bleAdapterReqRespCbMutex = ca_mutex_new();
738 if (NULL == g_bleAdapterReqRespCbMutex)
740 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
741 return CA_STATUS_FAILED;
745 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
749 void CATerminateBleAdapterMutex()
751 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
753 ca_mutex_free(g_bleIsServerMutex);
754 g_bleIsServerMutex = NULL;
756 ca_mutex_free(g_bleNetworkCbMutex);
757 g_bleNetworkCbMutex = NULL;
759 ca_mutex_free(g_bleLocalAddressMutex);
760 g_bleLocalAddressMutex = NULL;
762 ca_mutex_free(g_bleAdapterThreadPoolMutex);
763 g_bleAdapterThreadPoolMutex = NULL;
765 ca_mutex_free(g_bleClientSendDataMutex);
766 g_bleClientSendDataMutex = NULL;
768 ca_mutex_free(g_bleClientReceiveDataMutex);
769 g_bleClientReceiveDataMutex = NULL;
771 ca_mutex_free(g_bleServerSendDataMutex);
772 g_bleServerSendDataMutex = NULL;
774 ca_mutex_free(g_bleServerReceiveDataMutex);
775 g_bleServerReceiveDataMutex = NULL;
777 ca_mutex_free(g_bleAdapterReqRespCbMutex);
778 g_bleAdapterReqRespCbMutex = NULL;
780 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
783 void CAInitBleQueues()
785 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
787 CAResult_t result = CAInitBleServerQueues();
788 if (CA_STATUS_OK != result)
790 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerQueues failed");
794 result = CAInitBleClientQueues();
795 if (CA_STATUS_OK != result)
797 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
801 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
804 CAResult_t CAInitBleServerQueues()
806 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
808 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
810 CAResult_t result = CAInitBleServerSenderQueue();
811 if (CA_STATUS_OK != result)
813 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
814 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
815 return CA_STATUS_FAILED;
818 result = CAInitBleServerReceiverQueue();
819 if (CA_STATUS_OK != result)
821 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerReceiverQueue failed");
822 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
823 return CA_STATUS_FAILED;
826 g_dataReceiverHandlerState = true;
828 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
830 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
834 CAResult_t CAInitBleClientQueues()
836 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
838 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
840 CAResult_t result = CAInitBleClientSenderQueue();
841 if (CA_STATUS_OK != result)
843 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
844 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
845 return CA_STATUS_FAILED;
848 result = CAInitBleClientReceiverQueue();
849 if (CA_STATUS_OK != result)
851 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientReceiverQueue failed");
852 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
853 return CA_STATUS_FAILED;
856 g_dataReceiverHandlerState = true;
858 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
860 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
864 CAResult_t CAInitBleServerSenderQueue()
866 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
867 // Check if the message queue is already initialized
868 if (g_sendQueueHandle)
870 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
874 // Create send message queue
875 g_sendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
876 if (!g_sendQueueHandle)
878 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
879 return CA_MEMORY_ALLOC_FAILED;
882 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_sendQueueHandle, g_bleAdapterThreadPool,
883 CABLEServerSendDataThread, CALEDataDestroyer))
885 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
886 OICFree(g_sendQueueHandle);
887 g_sendQueueHandle = NULL;
888 return CA_STATUS_FAILED;
891 if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle))
893 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
894 OICFree(g_sendQueueHandle);
895 g_sendQueueHandle = NULL;
896 return CA_STATUS_FAILED;
899 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
903 CAResult_t CAInitBleClientSenderQueue()
905 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
907 if (g_bleClientSendQueueHandle)
909 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
913 // Create send message queue
914 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
915 if (!g_bleClientSendQueueHandle)
917 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
918 return CA_MEMORY_ALLOC_FAILED;
921 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle, g_bleAdapterThreadPool,
922 CABLEClientSendDataThread, CALEDataDestroyer))
924 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
925 OICFree(g_bleClientSendQueueHandle);
926 g_bleClientSendQueueHandle = NULL;
927 return CA_STATUS_FAILED;
930 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientSendQueueHandle))
932 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
933 OICFree(g_bleClientSendQueueHandle);
934 g_bleClientSendQueueHandle = NULL;
935 return CA_STATUS_FAILED;
938 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
942 CAResult_t CAInitBleServerReceiverQueue()
944 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
945 // Check if the message queue is already initialized
946 if (g_bleServerReceiverQueue)
948 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
952 // Create send message queue
953 g_bleServerReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
954 if (!g_bleServerReceiverQueue)
956 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
957 OICFree(g_sendQueueHandle);
958 return CA_MEMORY_ALLOC_FAILED;
961 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerReceiverQueue, g_bleAdapterThreadPool,
962 CABLEServerDataReceiverHandler, CALEDataDestroyer))
964 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
965 OICFree(g_bleServerReceiverQueue);
966 g_bleServerReceiverQueue = NULL;
967 return CA_STATUS_FAILED;
970 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerReceiverQueue))
972 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
973 OICFree(g_bleServerReceiverQueue);
974 g_bleServerReceiverQueue = NULL;
975 return CA_STATUS_FAILED;
978 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
982 CAResult_t CAInitBleClientReceiverQueue()
984 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
986 // Check if the message queue is already initialized
987 if (g_bleClientReceiverQueue)
989 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
993 // Create send message queue
994 g_bleClientReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
995 if (!g_bleClientReceiverQueue)
997 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
998 OICFree(g_bleClientSendQueueHandle);
999 return CA_MEMORY_ALLOC_FAILED;
1002 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientReceiverQueue, g_bleAdapterThreadPool,
1003 CABLEClientDataReceiverHandler, NULL))
1005 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
1006 OICFree(g_bleClientSendQueueHandle);
1007 OICFree(g_bleClientReceiverQueue);
1008 g_bleClientReceiverQueue = NULL;
1009 return CA_STATUS_FAILED;
1012 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientReceiverQueue))
1014 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
1015 OICFree(g_bleClientReceiverQueue);
1016 g_bleClientReceiverQueue = NULL;
1017 return CA_STATUS_FAILED;
1020 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1021 return CA_STATUS_OK;
1024 void CAStopBleQueues()
1026 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1028 ca_mutex_lock(g_bleClientSendDataMutex);
1029 if (NULL != g_bleClientSendQueueHandle)
1031 CAQueueingThreadStop(g_bleClientSendQueueHandle);
1033 ca_mutex_unlock(g_bleClientSendDataMutex);
1035 ca_mutex_lock(g_bleClientReceiveDataMutex);
1036 if (NULL != g_bleClientReceiverQueue)
1038 CAQueueingThreadStop(g_bleClientReceiverQueue);
1040 ca_mutex_unlock(g_bleClientReceiveDataMutex);
1042 ca_mutex_lock(g_bleServerSendDataMutex);
1043 if (NULL != g_sendQueueHandle)
1045 CAQueueingThreadStop(g_sendQueueHandle);
1047 ca_mutex_unlock(g_bleServerSendDataMutex);
1049 ca_mutex_lock(g_bleServerReceiveDataMutex);
1050 if (NULL != g_bleServerReceiverQueue)
1052 CAQueueingThreadStop(g_bleServerReceiverQueue);
1054 ca_mutex_unlock(g_bleServerReceiveDataMutex);
1056 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1059 void CATerminateBleQueues()
1061 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1063 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
1064 OICFree(g_bleClientSendQueueHandle);
1065 g_bleClientSendQueueHandle = NULL;
1068 CAQueueingThreadDestroy(g_bleClientReceiverQueue);
1069 OICFree(g_bleClientReceiverQueue);
1070 g_bleClientReceiverQueue = NULL;
1073 CAQueueingThreadDestroy(g_sendQueueHandle);
1074 OICFree(g_sendQueueHandle);
1075 g_sendQueueHandle = NULL;
1078 CAQueueingThreadDestroy(g_bleServerReceiverQueue);
1079 OICFree(g_bleServerReceiverQueue);
1080 g_bleServerReceiverQueue = NULL;
1082 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1084 void CABLEServerDataReceiverHandler(void *threadData)
1086 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1088 static uint32_t recvDataLen = 0;
1089 static uint32_t totalDataLen = 0;
1090 static char *defragData = NULL;
1091 static bool isHeaderAvailable = false;
1092 static CARemoteEndpoint_t *remoteEndpoint = NULL;
1094 ca_mutex_lock(g_bleServerReceiveDataMutex);
1096 if (g_dataReceiverHandlerState)
1098 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1100 CALEData_t *bleData = (CALEData_t *) threadData;
1103 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
1107 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1109 if (!isHeaderAvailable)
1111 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1112 totalDataLen = CAParseHeader((char*)bleData->data);
1114 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes", totalDataLen);
1115 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%d] bytes", bleData->dataLen);
1117 defragData = (char *) OICCalloc(totalDataLen + 1, sizeof(char));
1118 if (NULL == defragData)
1120 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1124 const char *remoteAddress = bleData->remoteEndpoint->addressInfo.LE.leMacAddress;
1125 const char *serviceUUID = bleData->remoteEndpoint->resourceUri;
1127 remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1130 memcpy(defragData + recvDataLen, bleData->data + CA_HEADER_LENGTH,
1131 bleData->dataLen - CA_HEADER_LENGTH);
1132 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1133 isHeaderAvailable = true;
1137 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1138 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1139 recvDataLen += bleData->dataLen ;
1140 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1141 totalDataLen, recvDataLen);
1143 if (totalDataLen == recvDataLen)
1145 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1146 if (NULL == g_networkPacketReceivedCallback)
1148 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1149 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1152 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1153 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1156 isHeaderAvailable = false;
1157 remoteEndpoint = NULL;
1159 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1162 if (false == g_dataReceiverHandlerState)
1164 OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1167 isHeaderAvailable = false;
1168 OICFree(defragData);
1169 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1170 ca_mutex_unlock(g_bleServerReceiveDataMutex);
1174 ca_mutex_unlock(g_bleServerReceiveDataMutex);
1175 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1178 void CABLEClientDataReceiverHandler(void *threadData)
1180 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1182 static const char *remoteAddress = NULL;
1183 static const char *serviceUUID = NULL;
1184 static uint32_t recvDataLen = 0;
1185 static uint32_t totalDataLen = 0;
1186 static char *defragData = NULL;
1187 static bool isHeaderAvailable = false;
1188 static CARemoteEndpoint_t *remoteEndpoint = NULL;
1190 ca_mutex_lock(g_bleClientReceiveDataMutex);
1192 if (g_dataReceiverHandlerState)
1194 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1196 CALEData_t *bleData = (CALEData_t *) threadData;
1199 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid wifidata!");
1203 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1205 if (!isHeaderAvailable)
1207 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1209 totalDataLen = CAParseHeader(bleData->data);
1210 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes",
1212 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received in the first packet [%d] bytes",
1215 defragData = (char *) OICMalloc(sizeof(char) * totalDataLen);
1216 if (NULL == defragData)
1218 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1222 remoteAddress = bleData->remoteEndpoint->addressInfo.LE.leMacAddress;
1223 serviceUUID = bleData->remoteEndpoint->resourceUri;
1225 remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1228 memcpy(defragData, bleData->data + CA_HEADER_LENGTH,
1229 bleData->dataLen - CA_HEADER_LENGTH);
1230 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1231 isHeaderAvailable = true;
1235 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1236 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1237 recvDataLen += bleData->dataLen ;
1238 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1239 totalDataLen, recvDataLen);
1241 if (totalDataLen == recvDataLen)
1243 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1244 if (NULL == g_networkPacketReceivedCallback)
1246 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1247 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1250 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1251 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1254 isHeaderAvailable = false;
1255 remoteEndpoint = NULL;
1257 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1260 if (false == g_dataReceiverHandlerState)
1262 OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1263 OICFree(defragData);
1264 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1265 ca_mutex_unlock(g_bleClientReceiveDataMutex);
1269 ca_mutex_unlock(g_bleClientReceiveDataMutex);
1270 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1273 void CABLEServerSendDataThread(void *threadData)
1275 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1277 CALEData_t *bleData = (CALEData_t *) threadData;
1280 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1284 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1285 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1287 int32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1289 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server total Data length with header is [%d]", totalLength);
1290 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
1291 if (NULL == dataSegment)
1293 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1298 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1299 if (CA_STATUS_OK != result )
1301 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1303 OICFree(dataSegment);
1307 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1311 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1313 length = totalLength;
1314 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1318 length = CA_SUPPORTED_BLE_MTU_SIZE;
1319 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1320 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1323 int32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1325 // Send the first segment with the header.
1326 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1328 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
1329 result = CAUpdateCharacteristicsToGattClient(
1330 bleData->remoteEndpoint->addressInfo.LE.leMacAddress, dataSegment, length);
1331 if (CA_STATUS_OK != result)
1333 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1334 OICFree(dataSegment);
1338 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1339 for (index = 1; index < iter; index++)
1341 // Send the remaining header.
1342 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1343 result = CAUpdateCharacteristicsToGattClient(
1344 bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1345 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1346 CA_SUPPORTED_BLE_MTU_SIZE);
1347 if (CA_STATUS_OK != result)
1349 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1350 "Update characteristics failed, result [%d]", result);
1351 OICFree(dataSegment);
1354 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
1355 CA_SUPPORTED_BLE_MTU_SIZE);
1358 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1359 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1361 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1362 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1363 result = CAUpdateCharacteristicsToGattClient(
1364 bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1365 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1367 if (CA_STATUS_OK != result)
1369 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1371 OICFree(dataSegment);
1374 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1379 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
1380 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
1381 if (CA_STATUS_OK != result)
1383 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1384 OICFree(dataSegment);
1387 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1388 for (index = 1; index < iter; index++)
1390 // Send the remaining header.
1391 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1392 result = CAUpdateCharacteristicsToAllGattClients(
1393 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1394 CA_SUPPORTED_BLE_MTU_SIZE);
1395 if (CA_STATUS_OK != result)
1397 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1398 OICFree(dataSegment);
1401 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
1404 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1405 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1407 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1408 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1409 result = CAUpdateCharacteristicsToAllGattClients(
1410 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1412 if (CA_STATUS_OK != result)
1414 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1416 OICFree(dataSegment);
1419 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1422 OICFree(dataSegment);
1424 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1427 void CABLEClientSendDataThread(void *threadData)
1429 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1431 CALEData_t *bleData = (CALEData_t *) threadData;
1434 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1438 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1439 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1441 uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1442 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
1443 if (NULL == dataSegment)
1445 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1450 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1451 if (CA_STATUS_OK != result )
1453 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1455 OICFree(dataSegment);
1458 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1461 uint32_t length = 0;
1462 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1464 length = totalLength;
1465 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1466 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1470 length = CA_SUPPORTED_BLE_MTU_SIZE;
1471 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1472 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1473 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1476 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1478 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1480 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1481 // Send the first segment with the header.
1482 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1487 if (CA_STATUS_OK != result)
1489 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1490 OICFree(dataSegment);
1494 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1495 for (index = 1; index < iter; index++)
1497 // Send the remaining header.
1498 result = CAUpdateCharacteristicsToGattServer(
1499 bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1500 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1501 CA_SUPPORTED_BLE_MTU_SIZE,
1503 if (CA_STATUS_OK != result)
1505 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1507 OICFree(dataSegment);
1510 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1511 CA_SUPPORTED_BLE_MTU_SIZE);
1514 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1515 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1517 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1518 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1519 result = CAUpdateCharacteristicsToGattServer(
1520 bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1521 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1525 if (CA_STATUS_OK != result)
1527 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1529 OICFree(dataSegment);
1532 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1537 //Sending Mulitcast Data
1538 // Send the first segment with the header.
1539 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1540 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1541 if (CA_STATUS_OK != result)
1543 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed (all), result [%d]", result);
1544 OICFree(dataSegment);
1547 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1548 // Send the remaining header.
1549 for (index = 1; index < iter; index++)
1551 result = CAUpdateCharacteristicsToAllGattServers(
1552 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1553 CA_SUPPORTED_BLE_MTU_SIZE);
1554 if (CA_STATUS_OK != result)
1556 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]", result);
1557 OICFree(dataSegment);
1560 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
1563 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1564 if ( remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1566 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1567 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1568 result = CAUpdateCharacteristicsToAllGattServers(
1569 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1571 if (CA_STATUS_OK != result)
1573 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]", result);
1574 OICFree(dataSegment);
1577 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1582 OICFree(dataSegment);
1584 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1587 CALEData_t *CACreateBLEData(const CARemoteEndpoint_t *remoteEndpoint, const void *data,
1588 uint32_t dataLength)
1590 CALEData_t *bleData = (CALEData_t *) OICMalloc(sizeof(CALEData_t));
1593 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1597 bleData->remoteEndpoint = CAAdapterCopyRemoteEndpoint(remoteEndpoint);
1598 bleData->data = (void *)OICCalloc(dataLength + 1, 1);
1599 if (NULL == bleData->data)
1601 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1602 CAFreeBLEData(bleData);
1605 memcpy(bleData->data, data, dataLength);
1606 bleData->dataLen = dataLength;
1611 void CAFreeBLEData(CALEData_t *bleData)
1613 VERIFY_NON_NULL_VOID(bleData, NULL, "Param bleData is NULL");
1615 CAAdapterFreeRemoteEndpoint(bleData->remoteEndpoint);
1616 OICFree(bleData->data);
1620 void CALEDataDestroyer(void *data, uint32_t size)
1622 CALEData_t *ledata = (CALEData_t *) data;
1624 CAFreeBLEData(ledata);
1628 CAResult_t CABLEClientSendData(const CARemoteEndpoint_t *remoteEndpoint,
1632 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1634 VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1636 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
1637 "g_bleClientSendQueueHandle is NULL",
1639 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
1640 "g_bleClientSendDataMutex is NULL",
1643 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
1646 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1648 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1651 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1652 return CA_MEMORY_ALLOC_FAILED;
1654 // Add message to send queue
1655 ca_mutex_lock(g_bleClientSendDataMutex);
1656 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
1657 ca_mutex_unlock(g_bleClientSendDataMutex);
1659 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1660 return CA_STATUS_OK;
1664 CAResult_t CABLEServerSendData(const CARemoteEndpoint_t *remoteEndpoint,
1668 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1670 VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1672 VERIFY_NON_NULL_RET(g_sendQueueHandle, CALEADAPTER_TAG,
1673 "BleClientReceiverQueue is NULL",
1675 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
1676 "BleClientSendDataMutex is NULL",
1679 VERIFY_NON_NULL_RET(g_sendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
1682 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1684 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1687 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1688 return CA_MEMORY_ALLOC_FAILED;
1690 // Add message to send queue
1691 ca_mutex_lock(g_bleServerSendDataMutex);
1692 CAQueueingThreadAddData(g_sendQueueHandle, bleData, sizeof(CALEData_t));
1693 ca_mutex_unlock(g_bleServerSendDataMutex);
1695 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1696 return CA_STATUS_OK;
1699 CAResult_t CABLEServerReceivedData(const char *remoteAddress, const char *serviceUUID,
1700 const void *data, uint32_t dataLength, uint32_t *sentLength)
1702 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1705 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1706 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1707 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1708 VERIFY_NON_NULL_RET(g_bleServerReceiverQueue, CALEADAPTER_TAG, "g_bleServerReceiverQueue",
1711 //Add message to data queue
1712 CARemoteEndpoint_t *remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1714 if (NULL == remoteEndpoint)
1716 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1717 return CA_STATUS_FAILED;
1720 // Create bleData to add to queue
1721 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1723 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1726 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1727 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1728 return CA_MEMORY_ALLOC_FAILED;
1731 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1732 // Add message to send queue
1733 CAQueueingThreadAddData(g_bleServerReceiverQueue, bleData, sizeof(CALEData_t));
1735 *sentLength = dataLength;
1737 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1738 return CA_STATUS_OK;
1741 CAResult_t CABLEClientReceivedData(const char *remoteAddress, const char *serviceUUID,
1742 const void *data, uint32_t dataLength, uint32_t *sentLength)
1744 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1747 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1748 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1749 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1750 VERIFY_NON_NULL_RET(g_bleClientReceiverQueue, CALEADAPTER_TAG, "g_bleClientReceiverQueue",
1753 //Add message to data queue
1754 CARemoteEndpoint_t *remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1756 if (NULL == remoteEndpoint)
1758 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1759 return CA_STATUS_FAILED;
1762 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1764 // Create bleData to add to queue
1765 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1768 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1769 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1770 return CA_MEMORY_ALLOC_FAILED;
1773 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1774 // Add message to send queue
1775 CAQueueingThreadAddData(g_bleClientReceiverQueue, bleData, sizeof(CALEData_t));
1777 *sentLength = dataLength;
1779 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1780 return CA_STATUS_OK;
1783 void CASetBleAdapterThreadPoolHandle(ca_thread_pool_t handle)
1785 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1787 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1788 g_bleAdapterThreadPool = handle;
1789 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1791 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1794 void CASetBLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
1796 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1798 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1800 g_networkPacketReceivedCallback = callback;
1802 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1804 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");