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 "cafragmentation.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[18] = {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 * @var g_errorHandler
156 * @brief Callback to notify error from the BLE adapter
158 static CAErrorHandleCallback g_errorHandler = NULL;
161 * @var g_bleAdapterState
162 * @brief Storing Adapter state information
164 static CAAdapterState_t g_bleAdapterState = CA_ADAPTER_DISABLED;
167 * @ENUM CALeServerStatus
168 * @brief status of BLE Server Status
169 * This ENUM provides information of LE Adapter Server status
173 CA_SERVER_NOTSTARTED = 0,
179 * @var gLeServerStatus
180 * @brief structure to maintain the status of the server.
182 static CALeServerStatus gLeServerStatus = CA_SERVER_NOTSTARTED;
185 * @fn CALERegisterNetworkNotifications
186 * @brief This function is used to register network change notification callback.
188 * @param[in] netCallback CANetworkChangeCallback callback which will be set for the change in nwk.
190 * @return 0 on success otherwise a positive error value.
191 * @retval CA_STATUS_OK Successful
192 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
193 * @retval CA_STATUS_FAILED Operation failed
196 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
199 * @fn CASetBleAdapterThreadPoolHandle
200 * @brief Used to Set the gThreadPool handle which is required for spawning new thread.
202 * @param[in] handle - Thread pool handle which is given by above layer for using thread creation task.
207 void CASetBleAdapterThreadPoolHandle(ca_thread_pool_t handle);
210 * @fn CALEDeviceStateChangedCb
211 * @brief This function is used to call the callback to the upper layer when the device state gets
214 * @param[in] adapter_state New state of the adapter to be notified to the upper layer.
219 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state);
222 * @fn CAInitBleAdapterMutex
223 * @brief Used to initialize all required mutex variable for LE Adapter implementation.
225 * @return 0 on success otherwise a positive error value.
226 * @retval CA_STATUS_OK Successful
227 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
228 * @retval CA_STATUS_FAILED Operation failed
231 CAResult_t CAInitBleAdapterMutex();
234 * @fn CATerminateBleAdapterMutex
235 * @brief Used to terminate all required mutex variable for LE adapter implementation.
239 void CATerminateBleAdapterMutex();
242 * @fn CALEDataDestroyer
243 * @brief Used to free data
247 static void CALEDataDestroyer(void *data, uint32_t size);
250 * @fn CALEErrorHandler
251 * @brief prepares and notify error through error callback
255 static void CALEErrorHandler(const char *remoteAddress, const void *data, uint32_t dataLen,
259 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
260 CANetworkPacketReceivedCallback reqRespCallback,
261 CANetworkChangeCallback netCallback,
262 CAErrorHandleCallback errorCallback, ca_thread_pool_t handle)
264 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
267 VERIFY_NON_NULL(registerCallback, NULL, "RegisterConnectivity callback is null");
268 VERIFY_NON_NULL(reqRespCallback, NULL, "PacketReceived Callback is null");
269 VERIFY_NON_NULL(netCallback, NULL, "NetworkChange Callback is null");
271 CAResult_t result = CAInitBleAdapterMutex();
272 if (CA_STATUS_OK != result)
274 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
275 return CA_STATUS_FAILED;
278 result = CAInitializeLENetworkMonitor();
279 if (CA_STATUS_OK != result)
281 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
282 return CA_STATUS_FAILED;
285 CAInitializeLEAdapter();
287 CASetBleClientThreadPoolHandle(handle);
288 CASetBleServerThreadPoolHandle(handle);
289 CASetBleAdapterThreadPoolHandle(handle);
290 CASetBLEReqRespServerCallback(CABLEServerReceivedData);
291 CASetBLEReqRespClientCallback(CABLEClientReceivedData);
292 CASetBLEReqRespAdapterCallback(reqRespCallback);
294 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
295 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
296 CALERegisterNetworkNotifications(netCallback);
298 g_errorHandler = errorCallback;
300 CAConnectivityHandler_t connHandler;
301 connHandler.startAdapter = CAStartLE;
302 connHandler.stopAdapter = CAStopLE;
303 connHandler.startListenServer = CAStartLEListeningServer;
304 connHandler.startDiscoveryServer = CAStartLEDiscoveryServer;
305 connHandler.sendData = CASendLEUnicastData;
306 connHandler.sendDataToAll = CASendLEMulticastData;
307 connHandler.GetnetInfo = CAGetLEInterfaceInformation;
308 connHandler.readData = CAReadLEData;
309 connHandler.terminate = CATerminateLE;
310 registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
312 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
317 CAResult_t CAStartLE()
319 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
320 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE, not implemented");
321 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
325 CAResult_t CAStopLE()
327 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
331 ca_mutex_lock(g_bleIsServerMutex);
332 if (true == g_isServer)
334 CAStopBleGattServer();
338 CAStopBLEGattClient();
340 ca_mutex_unlock(g_bleIsServerMutex);
342 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
349 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
351 CASetBLEReqRespServerCallback(NULL);
352 CASetBLEReqRespClientCallback(NULL);
353 CALERegisterNetworkNotifications(NULL);
354 CASetBLEReqRespAdapterCallback(NULL);
355 CATerminateLENetworkMonitor();
357 ca_mutex_lock(g_bleIsServerMutex);
358 if (true == g_isServer)
360 CATerminateBleGattServer();
364 CATerminateBLEGattClient();
366 ca_mutex_unlock(g_bleIsServerMutex);
368 CATerminateBleQueues();
370 CATerminateBleAdapterMutex();
372 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
375 CAResult_t CAStartLEListeningServer()
377 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
379 CAResult_t result = CAInitBleServerQueues();
380 if (CA_STATUS_OK != result)
382 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
383 return CA_STATUS_FAILED;
386 result = CAGetLEAdapterState();
387 if (CA_ADAPTER_NOT_ENABLED == result)
389 gLeServerStatus = CA_LISTENING_SERVER;
390 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
394 if (CA_STATUS_FAILED == result)
396 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
397 return CA_STATUS_FAILED;
400 CAStartBleGattServer();
402 ca_mutex_lock(g_bleIsServerMutex);
404 ca_mutex_unlock(g_bleIsServerMutex);
406 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
410 CAResult_t CAStartLEDiscoveryServer()
412 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
414 CAResult_t result = CAInitBleClientQueues();
415 if (CA_STATUS_OK != result)
417 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
418 return CA_STATUS_FAILED;
421 result = CAGetLEAdapterState();
422 if (CA_ADAPTER_NOT_ENABLED == result)
424 gLeServerStatus = CA_DISCOVERY_SERVER;
425 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
429 if (CA_STATUS_FAILED == result)
431 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
432 return CA_STATUS_FAILED;
435 CAStartBLEGattClient();
437 ca_mutex_lock(g_bleIsServerMutex);
439 ca_mutex_unlock(g_bleIsServerMutex);
441 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
445 CAResult_t CAStartLENotifyServer()
447 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
449 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
453 uint32_t CASendLENotification(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
455 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
457 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
461 CAResult_t CAReadLEData()
463 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
465 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
469 int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
471 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
474 VERIFY_NON_NULL_RET(endpoint, NULL, "Remote endpoint is null", -1);
475 VERIFY_NON_NULL_RET(data, NULL, "Data is null", -1);
477 CAResult_t result = CA_STATUS_FAILED;
479 ca_mutex_lock(g_bleIsServerMutex);
480 if (true == g_isServer)
482 result = CABLEServerSendData(endpoint, data, dataLen);
483 if (CA_STATUS_OK != result)
485 OIC_LOG(ERROR, CALEADAPTER_TAG,
486 "[SendLEUnicastData] CABleServerSenderQueueEnqueueMessage failed \n");
487 g_errorHandler((void *) endpoint, (void *) data, dataLen, result);
488 ca_mutex_unlock(g_bleIsServerMutex);
494 result = CABLEClientSendData(endpoint, data, dataLen);
495 if (CA_STATUS_OK != result)
497 OIC_LOG(ERROR, CALEADAPTER_TAG,
498 "[SendLEUnicastData] CABleClientSenderQueueEnqueueMessage failed \n");
499 g_errorHandler(endpoint, data, dataLen, result);
500 ca_mutex_unlock(g_bleIsServerMutex);
504 ca_mutex_unlock(g_bleIsServerMutex);
506 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
510 int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
512 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
515 VERIFY_NON_NULL_RET(data, NULL, "Data is null", -1);
519 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
523 CAResult_t result = CA_STATUS_FAILED;
525 ca_mutex_lock(g_bleIsServerMutex);
526 if (true == g_isServer)
528 result = CABLEServerSendData(endpoint, data, dataLen);
529 if (CA_STATUS_OK != result)
531 OIC_LOG(ERROR, CALEADAPTER_TAG,
532 "[SendLEMulticastDataToAll] CABleServerSenderQueueEnqueueMessage failed" );
533 ca_mutex_unlock(g_bleIsServerMutex);
534 g_errorHandler(endpoint, data, dataLen, result);
540 result = CABLEClientSendData(endpoint, data, dataLen);
541 if (CA_STATUS_OK != result)
543 OIC_LOG(ERROR, CALEADAPTER_TAG,
544 "[SendLEMulticastDataToAll] CABleClientSenderQueueEnqueueMessage failed" );
545 g_errorHandler(endpoint, data, dataLen, result);
546 ca_mutex_unlock(g_bleIsServerMutex);
550 ca_mutex_unlock(g_bleIsServerMutex);
552 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
556 CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
558 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
560 VERIFY_NON_NULL(info, NULL, "CALocalConnectivity info is null");
562 char *local_address = NULL;
564 CAResult_t res = CAGetLEAddress(&local_address);
565 if (CA_STATUS_OK != res)
567 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
571 if (NULL == local_address)
573 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
574 return CA_STATUS_FAILED;
578 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof (CAEndpoint_t));
581 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
582 OICFree(local_address);
583 return CA_STATUS_FAILED;
586 size_t local_address_len = strlen(local_address);
588 if(local_address_len >= sizeof(g_localBLEAddress) ||
589 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
591 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
593 OICFree(local_address);
594 return CA_STATUS_FAILED;
597 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
598 ca_mutex_lock(g_bleLocalAddressMutex);
599 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
600 ca_mutex_unlock(g_bleLocalAddressMutex);
602 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
604 OICFree(local_address);
606 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
610 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
612 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
614 ca_mutex_lock(g_bleNetworkCbMutex);
615 g_networkCallback = netCallback;
616 ca_mutex_unlock(g_bleNetworkCbMutex);
617 CAResult_t res = CA_STATUS_OK;
620 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
621 if (CA_STATUS_OK != res)
623 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
628 res = CAUnSetLEAdapterStateChangedCb();
629 if (CA_STATUS_OK != res)
631 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
635 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
639 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
641 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
643 VERIFY_NON_NULL_VOID(g_localBLEAddress, NULL, "g_localBLEAddress is null");
644 CAEndpoint_t localEndpoint = {};
645 localEndpoint.adapter = CA_ADAPTER_GATT_BTLE;
646 ca_mutex_lock(g_bleLocalAddressMutex);
647 OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), g_localBLEAddress);
648 ca_mutex_unlock(g_bleLocalAddressMutex);
650 g_bleAdapterState = adapter_state;
651 // Start a GattServer/Client if gLeServerStatus is SET
652 if (CA_LISTENING_SERVER == gLeServerStatus)
654 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattServer");
655 CAStartBleGattServer();
657 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
659 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
660 CAStartBLEGattClient();
662 gLeServerStatus = CA_SERVER_NOTSTARTED;
664 ca_mutex_lock(g_bleNetworkCbMutex);
665 if (NULL != g_networkCallback)
667 g_networkCallback(&localEndpoint, adapter_state);
671 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
673 ca_mutex_unlock(g_bleNetworkCbMutex);
675 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
678 CAResult_t CAInitBleAdapterMutex()
680 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
682 if (NULL == g_bleIsServerMutex)
684 g_bleIsServerMutex = ca_mutex_new();
685 if (NULL == g_bleIsServerMutex)
687 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
688 return CA_STATUS_FAILED;
692 if (NULL == g_bleNetworkCbMutex)
694 g_bleNetworkCbMutex = ca_mutex_new();
695 if (NULL == g_bleNetworkCbMutex)
697 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
698 return CA_STATUS_FAILED;
702 if (NULL == g_bleLocalAddressMutex)
704 g_bleLocalAddressMutex = ca_mutex_new();
705 if (NULL == g_bleLocalAddressMutex)
707 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
708 return CA_STATUS_FAILED;
712 if (NULL == g_bleAdapterThreadPoolMutex)
714 g_bleAdapterThreadPoolMutex = ca_mutex_new();
715 if (NULL == g_bleAdapterThreadPoolMutex)
717 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
718 return CA_STATUS_FAILED;
722 if (NULL == g_bleClientSendDataMutex)
724 g_bleClientSendDataMutex = ca_mutex_new();
725 if (NULL == g_bleClientSendDataMutex)
727 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
728 return CA_STATUS_FAILED;
732 if (NULL == g_bleClientReceiveDataMutex)
734 g_bleClientReceiveDataMutex = ca_mutex_new();
735 if (NULL == g_bleClientReceiveDataMutex)
737 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
738 return CA_STATUS_FAILED;
742 if (NULL == g_bleServerSendDataMutex)
744 g_bleServerSendDataMutex = ca_mutex_new();
745 if (NULL == g_bleServerSendDataMutex)
747 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
748 return CA_STATUS_FAILED;
752 if (NULL == g_bleServerReceiveDataMutex)
754 g_bleServerReceiveDataMutex = ca_mutex_new();
755 if (NULL == g_bleServerReceiveDataMutex)
757 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
758 return CA_STATUS_FAILED;
762 if (NULL == g_bleAdapterReqRespCbMutex)
764 g_bleAdapterReqRespCbMutex = ca_mutex_new();
765 if (NULL == g_bleAdapterReqRespCbMutex)
767 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
768 return CA_STATUS_FAILED;
772 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
776 void CATerminateBleAdapterMutex()
778 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
780 ca_mutex_free(g_bleIsServerMutex);
781 g_bleIsServerMutex = NULL;
783 ca_mutex_free(g_bleNetworkCbMutex);
784 g_bleNetworkCbMutex = NULL;
786 ca_mutex_free(g_bleLocalAddressMutex);
787 g_bleLocalAddressMutex = NULL;
789 ca_mutex_free(g_bleAdapterThreadPoolMutex);
790 g_bleAdapterThreadPoolMutex = NULL;
792 ca_mutex_free(g_bleClientSendDataMutex);
793 g_bleClientSendDataMutex = NULL;
795 ca_mutex_free(g_bleClientReceiveDataMutex);
796 g_bleClientReceiveDataMutex = NULL;
798 ca_mutex_free(g_bleServerSendDataMutex);
799 g_bleServerSendDataMutex = NULL;
801 ca_mutex_free(g_bleServerReceiveDataMutex);
802 g_bleServerReceiveDataMutex = NULL;
804 ca_mutex_free(g_bleAdapterReqRespCbMutex);
805 g_bleAdapterReqRespCbMutex = NULL;
807 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
810 void CAInitBleQueues()
812 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
814 CAResult_t result = CAInitBleServerQueues();
815 if (CA_STATUS_OK != result)
817 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerQueues failed");
821 result = CAInitBleClientQueues();
822 if (CA_STATUS_OK != result)
824 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
828 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
831 CAResult_t CAInitBleServerQueues()
833 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
835 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
837 CAResult_t result = CAInitBleServerSenderQueue();
838 if (CA_STATUS_OK != result)
840 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
841 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
842 return CA_STATUS_FAILED;
845 result = CAInitBleServerReceiverQueue();
846 if (CA_STATUS_OK != result)
848 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerReceiverQueue failed");
849 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
850 return CA_STATUS_FAILED;
853 g_dataReceiverHandlerState = true;
855 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
857 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
861 CAResult_t CAInitBleClientQueues()
863 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
865 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
867 CAResult_t result = CAInitBleClientSenderQueue();
868 if (CA_STATUS_OK != result)
870 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
871 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
872 return CA_STATUS_FAILED;
875 result = CAInitBleClientReceiverQueue();
876 if (CA_STATUS_OK != result)
878 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientReceiverQueue failed");
879 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
880 return CA_STATUS_FAILED;
883 g_dataReceiverHandlerState = true;
885 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
887 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
891 CAResult_t CAInitBleServerSenderQueue()
893 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
894 // Check if the message queue is already initialized
895 if (g_sendQueueHandle)
897 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
901 // Create send message queue
902 g_sendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
903 if (!g_sendQueueHandle)
905 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
906 return CA_MEMORY_ALLOC_FAILED;
909 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_sendQueueHandle, g_bleAdapterThreadPool,
910 CABLEServerSendDataThread, CALEDataDestroyer))
912 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
913 OICFree(g_sendQueueHandle);
914 g_sendQueueHandle = NULL;
915 return CA_STATUS_FAILED;
918 if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle))
920 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
921 OICFree(g_sendQueueHandle);
922 g_sendQueueHandle = NULL;
923 return CA_STATUS_FAILED;
926 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
930 CAResult_t CAInitBleClientSenderQueue()
932 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
934 if (g_bleClientSendQueueHandle)
936 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
940 // Create send message queue
941 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
942 if (!g_bleClientSendQueueHandle)
944 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
945 return CA_MEMORY_ALLOC_FAILED;
948 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle, g_bleAdapterThreadPool,
949 CABLEClientSendDataThread, CALEDataDestroyer))
951 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
952 OICFree(g_bleClientSendQueueHandle);
953 g_bleClientSendQueueHandle = NULL;
954 return CA_STATUS_FAILED;
957 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientSendQueueHandle))
959 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
960 OICFree(g_bleClientSendQueueHandle);
961 g_bleClientSendQueueHandle = NULL;
962 return CA_STATUS_FAILED;
965 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
969 CAResult_t CAInitBleServerReceiverQueue()
971 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
972 // Check if the message queue is already initialized
973 if (g_bleServerReceiverQueue)
975 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
979 // Create send message queue
980 g_bleServerReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
981 if (!g_bleServerReceiverQueue)
983 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
984 OICFree(g_sendQueueHandle);
985 return CA_MEMORY_ALLOC_FAILED;
988 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerReceiverQueue, g_bleAdapterThreadPool,
989 CABLEServerDataReceiverHandler, CALEDataDestroyer))
991 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
992 OICFree(g_bleServerReceiverQueue);
993 g_bleServerReceiverQueue = NULL;
994 return CA_STATUS_FAILED;
997 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerReceiverQueue))
999 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
1000 OICFree(g_bleServerReceiverQueue);
1001 g_bleServerReceiverQueue = NULL;
1002 return CA_STATUS_FAILED;
1005 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1006 return CA_STATUS_OK;
1009 CAResult_t CAInitBleClientReceiverQueue()
1011 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1013 // Check if the message queue is already initialized
1014 if (g_bleClientReceiverQueue)
1016 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
1020 // Create send message queue
1021 g_bleClientReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
1022 if (!g_bleClientReceiverQueue)
1024 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1025 OICFree(g_bleClientSendQueueHandle);
1026 return CA_MEMORY_ALLOC_FAILED;
1029 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientReceiverQueue, g_bleAdapterThreadPool,
1030 CABLEClientDataReceiverHandler, NULL))
1032 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
1033 OICFree(g_bleClientSendQueueHandle);
1034 OICFree(g_bleClientReceiverQueue);
1035 g_bleClientReceiverQueue = NULL;
1036 return CA_STATUS_FAILED;
1039 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientReceiverQueue))
1041 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
1042 OICFree(g_bleClientReceiverQueue);
1043 g_bleClientReceiverQueue = NULL;
1044 return CA_STATUS_FAILED;
1047 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1048 return CA_STATUS_OK;
1051 void CAStopBleQueues()
1053 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1055 ca_mutex_lock(g_bleClientSendDataMutex);
1056 if (NULL != g_bleClientSendQueueHandle)
1058 CAQueueingThreadStop(g_bleClientSendQueueHandle);
1060 ca_mutex_unlock(g_bleClientSendDataMutex);
1062 ca_mutex_lock(g_bleClientReceiveDataMutex);
1063 if (NULL != g_bleClientReceiverQueue)
1065 CAQueueingThreadStop(g_bleClientReceiverQueue);
1067 ca_mutex_unlock(g_bleClientReceiveDataMutex);
1069 ca_mutex_lock(g_bleServerSendDataMutex);
1070 if (NULL != g_sendQueueHandle)
1072 CAQueueingThreadStop(g_sendQueueHandle);
1074 ca_mutex_unlock(g_bleServerSendDataMutex);
1076 ca_mutex_lock(g_bleServerReceiveDataMutex);
1077 if (NULL != g_bleServerReceiverQueue)
1079 CAQueueingThreadStop(g_bleServerReceiverQueue);
1081 ca_mutex_unlock(g_bleServerReceiveDataMutex);
1083 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1086 void CATerminateBleQueues()
1088 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1090 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
1091 OICFree(g_bleClientSendQueueHandle);
1092 g_bleClientSendQueueHandle = NULL;
1095 CAQueueingThreadDestroy(g_bleClientReceiverQueue);
1096 OICFree(g_bleClientReceiverQueue);
1097 g_bleClientReceiverQueue = NULL;
1100 CAQueueingThreadDestroy(g_sendQueueHandle);
1101 OICFree(g_sendQueueHandle);
1102 g_sendQueueHandle = NULL;
1105 CAQueueingThreadDestroy(g_bleServerReceiverQueue);
1106 OICFree(g_bleServerReceiverQueue);
1107 g_bleServerReceiverQueue = NULL;
1109 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1111 void CABLEServerDataReceiverHandler(void *threadData)
1113 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1115 static uint32_t recvDataLen = 0;
1116 static uint32_t totalDataLen = 0;
1117 static char *defragData = NULL;
1118 static bool isHeaderAvailable = false;
1119 static CAEndpoint_t *remoteEndpoint = NULL;
1121 ca_mutex_lock(g_bleServerReceiveDataMutex);
1123 if (g_dataReceiverHandlerState)
1125 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1127 CALEData_t *bleData = (CALEData_t *) threadData;
1130 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
1134 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1136 if (!isHeaderAvailable)
1138 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1139 totalDataLen = CAParseHeader((char*)bleData->data);
1141 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes", totalDataLen);
1142 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%d] bytes", bleData->dataLen);
1144 defragData = (char *) OICCalloc(totalDataLen + 1, sizeof(char));
1145 if (NULL == defragData)
1147 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1151 const char *remoteAddress = bleData->remoteEndpoint->addr;
1153 remoteEndpoint = CAAdapterCreateEndpoint(0, CA_ADAPTER_GATT_BTLE, remoteAddress, 0);
1155 memcpy(defragData + recvDataLen, bleData->data + CA_HEADER_LENGTH,
1156 bleData->dataLen - CA_HEADER_LENGTH);
1157 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1158 isHeaderAvailable = true;
1162 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1163 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1164 recvDataLen += bleData->dataLen ;
1165 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1166 totalDataLen, recvDataLen);
1168 if (totalDataLen == recvDataLen)
1170 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1171 if (NULL == g_networkPacketReceivedCallback)
1173 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1174 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1177 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1178 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1181 isHeaderAvailable = false;
1182 remoteEndpoint = NULL;
1184 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1187 if (false == g_dataReceiverHandlerState)
1189 OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1192 isHeaderAvailable = false;
1193 OICFree(defragData);
1194 CAAdapterFreeEndpoint(remoteEndpoint);
1195 ca_mutex_unlock(g_bleServerReceiveDataMutex);
1199 ca_mutex_unlock(g_bleServerReceiveDataMutex);
1200 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1203 void CABLEClientDataReceiverHandler(void *threadData)
1205 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1207 static const char *remoteAddress = NULL;
1208 static uint32_t recvDataLen = 0;
1209 static uint32_t totalDataLen = 0;
1210 static char *defragData = NULL;
1211 static bool isHeaderAvailable = false;
1212 static CAEndpoint_t *remoteEndpoint = NULL;
1214 ca_mutex_lock(g_bleClientReceiveDataMutex);
1216 if (g_dataReceiverHandlerState)
1218 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1220 CALEData_t *bleData = (CALEData_t *) threadData;
1223 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid wifidata!");
1227 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1229 if (!isHeaderAvailable)
1231 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1233 totalDataLen = CAParseHeader(bleData->data);
1234 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes",
1236 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received in the first packet [%d] bytes",
1239 defragData = (char *) OICMalloc(sizeof(char) * totalDataLen);
1240 if (NULL == defragData)
1242 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1246 remoteAddress = bleData->remoteEndpoint->addr;
1248 remoteEndpoint = CAAdapterCreateEndpoint(0, CA_ADAPTER_GATT_BTLE, remoteAddress, 0);
1250 memcpy(defragData, bleData->data + CA_HEADER_LENGTH,
1251 bleData->dataLen - CA_HEADER_LENGTH);
1252 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1253 isHeaderAvailable = true;
1257 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1258 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1259 recvDataLen += bleData->dataLen ;
1260 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1261 totalDataLen, recvDataLen);
1263 if (totalDataLen == recvDataLen)
1265 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1266 if (NULL == g_networkPacketReceivedCallback)
1268 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1269 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1272 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1273 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1276 isHeaderAvailable = false;
1277 remoteEndpoint = NULL;
1279 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1282 if (false == g_dataReceiverHandlerState)
1284 OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1285 OICFree(defragData);
1286 CAAdapterFreeEndpoint(remoteEndpoint);
1287 ca_mutex_unlock(g_bleClientReceiveDataMutex);
1291 ca_mutex_unlock(g_bleClientReceiveDataMutex);
1292 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1295 void CABLEServerSendDataThread(void *threadData)
1297 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1299 CALEData_t *bleData = (CALEData_t *) threadData;
1302 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1306 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1307 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1309 int32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1311 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server total Data length with header is [%d]", totalLength);
1312 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
1313 if (NULL == dataSegment)
1315 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1320 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1321 if (CA_STATUS_OK != result )
1323 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1325 OICFree(dataSegment);
1329 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1333 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1335 length = totalLength;
1336 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1340 length = CA_SUPPORTED_BLE_MTU_SIZE;
1341 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1342 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1345 int32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1347 // Send the first segment with the header.
1348 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1350 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
1351 result = CAUpdateCharacteristicsToGattClient(
1352 bleData->remoteEndpoint->addr, dataSegment, length);
1353 if (CA_STATUS_OK != result)
1355 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1356 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1357 OICFree(dataSegment);
1361 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1362 for (index = 1; index < iter; index++)
1364 // Send the remaining header.
1365 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1366 result = CAUpdateCharacteristicsToGattClient(
1367 bleData->remoteEndpoint->addr,
1368 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1369 CA_SUPPORTED_BLE_MTU_SIZE);
1370 if (CA_STATUS_OK != result)
1372 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1373 "Update characteristics failed, result [%d]", result);
1374 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1375 OICFree(dataSegment);
1378 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
1379 CA_SUPPORTED_BLE_MTU_SIZE);
1382 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1383 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1385 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1386 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1387 result = CAUpdateCharacteristicsToGattClient(
1388 bleData->remoteEndpoint->addr,
1389 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1391 if (CA_STATUS_OK != result)
1393 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1395 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1396 OICFree(dataSegment);
1399 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1404 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
1405 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
1406 if (CA_STATUS_OK != result)
1408 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1410 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1411 OICFree(dataSegment);
1414 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1415 for (index = 1; index < iter; index++)
1417 // Send the remaining header.
1418 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1419 result = CAUpdateCharacteristicsToAllGattClients(
1420 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1421 CA_SUPPORTED_BLE_MTU_SIZE);
1422 if (CA_STATUS_OK != result)
1424 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1426 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1427 OICFree(dataSegment);
1430 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
1433 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1434 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1436 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1437 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1438 result = CAUpdateCharacteristicsToAllGattClients(
1439 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1441 if (CA_STATUS_OK != result)
1443 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1445 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1446 OICFree(dataSegment);
1449 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1452 OICFree(dataSegment);
1454 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1457 void CABLEClientSendDataThread(void *threadData)
1459 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1461 CALEData_t *bleData = (CALEData_t *) threadData;
1464 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1468 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1469 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1471 uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1472 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
1473 if (NULL == dataSegment)
1475 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1480 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1481 if (CA_STATUS_OK != result )
1483 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1485 OICFree(dataSegment);
1488 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1491 uint32_t length = 0;
1492 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1494 length = totalLength;
1495 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1496 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1500 length = CA_SUPPORTED_BLE_MTU_SIZE;
1501 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1502 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1503 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1506 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1508 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1510 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1511 // Send the first segment with the header.
1512 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addr,
1517 if (CA_STATUS_OK != result)
1519 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1520 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1521 OICFree(dataSegment);
1525 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1526 for (index = 1; index < iter; index++)
1528 // Send the remaining header.
1529 result = CAUpdateCharacteristicsToGattServer(
1530 bleData->remoteEndpoint->addr,
1531 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1532 CA_SUPPORTED_BLE_MTU_SIZE,
1534 if (CA_STATUS_OK != result)
1536 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1538 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1539 OICFree(dataSegment);
1542 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1543 CA_SUPPORTED_BLE_MTU_SIZE);
1546 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1547 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1549 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1550 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1551 result = CAUpdateCharacteristicsToGattServer(
1552 bleData->remoteEndpoint->addr,
1553 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1557 if (CA_STATUS_OK != result)
1559 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1561 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1562 OICFree(dataSegment);
1565 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1570 //Sending Mulitcast Data
1571 // Send the first segment with the header.
1572 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1573 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1574 if (CA_STATUS_OK != result)
1576 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1577 "Update characteristics (all) failed, result [%d]", result);
1578 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1579 OICFree(dataSegment);
1582 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1583 // Send the remaining header.
1584 for (index = 1; index < iter; index++)
1586 result = CAUpdateCharacteristicsToAllGattServers(
1587 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1588 CA_SUPPORTED_BLE_MTU_SIZE);
1589 if (CA_STATUS_OK != result)
1591 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]",
1593 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1594 OICFree(dataSegment);
1597 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1598 CA_SUPPORTED_BLE_MTU_SIZE);
1601 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1602 if ( remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1604 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1605 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1606 result = CAUpdateCharacteristicsToAllGattServers(
1607 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1609 if (CA_STATUS_OK != result)
1611 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1612 "Update characteristics (all) failed, result [%d]", result);
1613 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1614 OICFree(dataSegment);
1617 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1622 OICFree(dataSegment);
1624 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1627 CALEData_t *CACreateBLEData(const CAEndpoint_t *remoteEndpoint, const void *data,
1628 uint32_t dataLength)
1630 CALEData_t *bleData = (CALEData_t *) OICMalloc(sizeof(CALEData_t));
1633 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1637 bleData->remoteEndpoint = CAAdapterCloneEndpoint(remoteEndpoint);
1638 bleData->data = (void *)OICCalloc(dataLength + 1, 1);
1639 if (NULL == bleData->data)
1641 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1642 CAFreeBLEData(bleData);
1645 memcpy(bleData->data, data, dataLength);
1646 bleData->dataLen = dataLength;
1651 void CAFreeBLEData(CALEData_t *bleData)
1653 VERIFY_NON_NULL_VOID(bleData, NULL, "Param bleData is NULL");
1655 CAAdapterFreeEndpoint(bleData->remoteEndpoint);
1656 OICFree(bleData->data);
1660 void CALEDataDestroyer(void *data, uint32_t size)
1662 CALEData_t *ledata = (CALEData_t *) data;
1664 CAFreeBLEData(ledata);
1668 CAResult_t CABLEClientSendData(const CAEndpoint_t *remoteEndpoint,
1672 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1674 VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1676 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
1677 "g_bleClientSendQueueHandle is NULL",
1679 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
1680 "g_bleClientSendDataMutex is NULL",
1683 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
1686 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1688 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1691 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1692 return CA_MEMORY_ALLOC_FAILED;
1694 // Add message to send queue
1695 ca_mutex_lock(g_bleClientSendDataMutex);
1696 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
1697 ca_mutex_unlock(g_bleClientSendDataMutex);
1699 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1700 return CA_STATUS_OK;
1704 CAResult_t CABLEServerSendData(const CAEndpoint_t *remoteEndpoint,
1708 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1710 VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1712 VERIFY_NON_NULL_RET(g_sendQueueHandle, CALEADAPTER_TAG,
1713 "BleClientReceiverQueue is NULL",
1715 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
1716 "BleClientSendDataMutex is NULL",
1719 VERIFY_NON_NULL_RET(g_sendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
1722 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1724 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1727 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1728 return CA_MEMORY_ALLOC_FAILED;
1730 // Add message to send queue
1731 ca_mutex_lock(g_bleServerSendDataMutex);
1732 CAQueueingThreadAddData(g_sendQueueHandle, bleData, sizeof(CALEData_t));
1733 ca_mutex_unlock(g_bleServerSendDataMutex);
1735 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1736 return CA_STATUS_OK;
1739 CAResult_t CABLEServerReceivedData(const char *remoteAddress, const char *serviceUUID,
1740 const void *data, uint32_t dataLength, uint32_t *sentLength)
1742 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1745 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1746 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1747 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1748 VERIFY_NON_NULL_RET(g_bleServerReceiverQueue, CALEADAPTER_TAG, "g_bleServerReceiverQueue",
1751 //Add message to data queue
1752 CAEndpoint_t *remoteEndpoint = CAAdapterCreateEndpoint(0, CA_ADAPTER_GATT_BTLE, remoteAddress, 0);
1753 if (NULL == remoteEndpoint)
1755 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1756 return CA_STATUS_FAILED;
1759 // Create bleData to add to queue
1760 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1762 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1765 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1766 CAAdapterFreeEndpoint(remoteEndpoint);
1767 return CA_MEMORY_ALLOC_FAILED;
1770 CAAdapterFreeEndpoint(remoteEndpoint);
1771 // Add message to send queue
1772 CAQueueingThreadAddData(g_bleServerReceiverQueue, bleData, sizeof(CALEData_t));
1774 *sentLength = dataLength;
1776 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1777 return CA_STATUS_OK;
1780 CAResult_t CABLEClientReceivedData(const char *remoteAddress, const char *serviceUUID,
1781 const void *data, uint32_t dataLength, uint32_t *sentLength)
1783 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1786 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1787 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1788 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1789 VERIFY_NON_NULL_RET(g_bleClientReceiverQueue, CALEADAPTER_TAG, "g_bleClientReceiverQueue",
1792 //Add message to data queue
1793 CAEndpoint_t *remoteEndpoint = CAAdapterCreateEndpoint(0, CA_ADAPTER_GATT_BTLE, remoteAddress, 0);
1794 if (NULL == remoteEndpoint)
1796 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1797 return CA_STATUS_FAILED;
1800 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1802 // Create bleData to add to queue
1803 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1806 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1807 CAAdapterFreeEndpoint(remoteEndpoint);
1808 return CA_MEMORY_ALLOC_FAILED;
1811 CAAdapterFreeEndpoint(remoteEndpoint);
1812 // Add message to send queue
1813 CAQueueingThreadAddData(g_bleClientReceiverQueue, bleData, sizeof(CALEData_t));
1815 *sentLength = dataLength;
1817 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1818 return CA_STATUS_OK;
1821 void CASetBleAdapterThreadPoolHandle(ca_thread_pool_t handle)
1823 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1825 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1826 g_bleAdapterThreadPool = handle;
1827 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1829 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1832 void CASetBLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
1834 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1836 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1838 g_networkPacketReceivedCallback = callback;
1840 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1842 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1845 void CALEErrorHandler(const char *remoteAddress, const void *data, uint32_t dataLen,
1848 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
1850 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
1851 CAEndpoint_t *rep = OICCalloc(1, sizeof(CAEndpoint_t));
1855 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1861 OICStrcpy(rep->addr, sizeof(rep->addr), remoteAddress);
1864 rep->adapter = CA_ADAPTER_GATT_BTLE;
1865 rep->flags = CA_DEFAULT_FLAGS;
1867 //if required, will be used to build remote end point
1868 g_errorHandler(rep, data, dataLen, result);
1869 CAAdapterFreeEndpoint(rep);
1871 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");