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"
33 #include "caremotehandler.h"
36 * @var CALEADAPTER_TAG
37 * @brief Logging tag for module name.
39 #define CALEADAPTER_TAG "CA_BLE_ADAPTER"
42 * @var g_networkCallback
43 * @brief Callback to provide the status of the network change to CA layer.
45 static CANetworkChangeCallback g_networkCallback = NULL;
48 * @var g_localBLEAddress
49 * @brief bleAddress of the local adapter. Value will be initialized to zero, and will be updated later.
51 static char g_localBLEAddress[18] = {0};
55 * @brief Variable to differentiate btw GattServer and GattClient.
57 static bool g_isServer = false;
60 * @var g_bleIsServerMutex
61 * @brief Mutex to synchronize the task to be executed on the GattServer function calls.
63 static ca_mutex g_bleIsServerMutex = NULL;
66 * @var g_bleNetworkCbMutex
67 * @brief Mutex to synchronize the callback to be called for the network changes.
69 static ca_mutex g_bleNetworkCbMutex = NULL;
72 * @var g_bleLocalAddressMutex
73 * @brief Mutex to synchronize the updation of the local LE address of the adapter.
75 static ca_mutex g_bleLocalAddressMutex = NULL;
78 * @var g_bleAdapterThreadPool
79 * @brief reference to threadpool
81 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
84 * @var g_bleAdapterThreadPoolMutex
85 * @brief Mutex to synchronize the task to be pushed to thread pool.
87 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
90 * @var g_bleClientSendQueueHandle
91 * @brief Queue to process the outgoing packets from GATTClient.
93 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
96 * @var g_bleClientReceiverQueue
97 * @brief Queue to process the incoming packets to GATT Client.
99 static CAQueueingThread_t *g_bleClientReceiverQueue = NULL;
102 * @var g_bleClientSendDataMutex
103 * @brief Mutex to synchronize the queing of the data from SenderQueue.
105 static ca_mutex g_bleClientSendDataMutex = NULL;
108 * @var g_bleClientReceiveDataMutex
109 * @brief Mutex to synchronize the queing of the data from ReceiverQueue.
111 static ca_mutex g_bleClientReceiveDataMutex = NULL;
114 * @var g_dataReceiverHandlerState
115 * @brief Stop condition of recvhandler.
117 static bool g_dataReceiverHandlerState = false;
120 * @var g_sendQueueHandle
121 * @brief Queue to process the outgoing packets from GATTServer.
123 static CAQueueingThread_t *g_sendQueueHandle = NULL;
126 * @var g_bleServerReceiverQueue
127 * @brief Queue to process the incoming packets to GATTServer
129 static CAQueueingThread_t *g_bleServerReceiverQueue = NULL;
132 * @var g_bleServerSendDataMutex
133 * @brief Mutex to synchronize the queing of the data from SenderQueue.
135 static ca_mutex g_bleServerSendDataMutex = NULL;
138 * @var g_bleServerReceiveDataMutex
139 * @brief Mutex to synchronize the queing of the data from ReceiverQueue.
141 static ca_mutex g_bleServerReceiveDataMutex = NULL;
144 * @var g_bleAdapterReqRespCbMutex
145 * @brief Mutex to synchronize the callback to be called for the adapterReqResponse.
147 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
150 * @var g_networkPacketReceivedCallback
151 * @brief Callback to be called when network packet recieved from either GattServer or GattClient.
153 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
156 * @var g_errorHandler
157 * @brief Callback to notify error from the BLE adapter
159 static CAErrorHandleCallback g_errorHandler = NULL;
162 * @var g_bleAdapterState
163 * @brief Storing Adapter state information
165 static CAAdapterState_t g_bleAdapterState = CA_ADAPTER_DISABLED;
168 * @ENUM CALeServerStatus
169 * @brief status of BLE Server Status
170 * This ENUM provides information of LE Adapter Server status
174 CA_SERVER_NOTSTARTED = 0,
180 * @var gLeServerStatus
181 * @brief structure to maintain the status of the server.
183 static CALeServerStatus gLeServerStatus = CA_SERVER_NOTSTARTED;
186 * @fn CALERegisterNetworkNotifications
187 * @brief This function is used to register network change notification callback.
189 * @param[in] netCallback CANetworkChangeCallback callback which will be set for the change in nwk.
191 * @return 0 on success otherwise a positive error value.
192 * @retval CA_STATUS_OK Successful
193 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
194 * @retval CA_STATUS_FAILED Operation failed
197 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
200 * @fn CASetBleAdapterThreadPoolHandle
201 * @brief Used to Set the gThreadPool handle which is required for spawning new thread.
203 * @param[in] handle - Thread pool handle which is given by above layer for using thread creation task.
208 void CASetBleAdapterThreadPoolHandle(ca_thread_pool_t handle);
211 * @fn CALEDeviceStateChangedCb
212 * @brief This function is used to call the callback to the upper layer when the device state gets
215 * @param[in] adapter_state New state of the adapter to be notified to the upper layer.
220 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state);
223 * @fn CAInitBleAdapterMutex
224 * @brief Used to initialize all required mutex variable for LE Adapter implementation.
226 * @return 0 on success otherwise a positive error value.
227 * @retval CA_STATUS_OK Successful
228 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
229 * @retval CA_STATUS_FAILED Operation failed
232 CAResult_t CAInitBleAdapterMutex();
235 * @fn CATerminateBleAdapterMutex
236 * @brief Used to terminate all required mutex variable for LE adapter implementation.
240 void CATerminateBleAdapterMutex();
243 * @fn CALEDataDestroyer
244 * @brief Used to free data
248 static void CALEDataDestroyer(void *data, uint32_t size);
251 * @fn CALEErrorHandler
252 * @brief prepares and notify error through error callback
256 static void CALEErrorHandler(const char *remoteAddress, const void *data, uint32_t dataLen,
260 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
261 CANetworkPacketReceivedCallback reqRespCallback,
262 CANetworkChangeCallback netCallback,
263 CAErrorHandleCallback errorCallback, ca_thread_pool_t handle)
265 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
268 VERIFY_NON_NULL(registerCallback, NULL, "RegisterConnectivity callback is null");
269 VERIFY_NON_NULL(reqRespCallback, NULL, "PacketReceived Callback is null");
270 VERIFY_NON_NULL(netCallback, NULL, "NetworkChange Callback is null");
272 CAResult_t result = CAInitBleAdapterMutex();
273 if (CA_STATUS_OK != result)
275 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
276 return CA_STATUS_FAILED;
279 result = CAInitializeLENetworkMonitor();
280 if (CA_STATUS_OK != result)
282 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
283 return CA_STATUS_FAILED;
286 CAInitializeLEAdapter();
288 CASetBleClientThreadPoolHandle(handle);
289 CASetBleServerThreadPoolHandle(handle);
290 CASetBleAdapterThreadPoolHandle(handle);
291 CASetBLEReqRespServerCallback(CABLEServerReceivedData);
292 CASetBLEReqRespClientCallback(CABLEClientReceivedData);
293 CASetBLEReqRespAdapterCallback(reqRespCallback);
295 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
296 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
297 CALERegisterNetworkNotifications(netCallback);
299 g_errorHandler = errorCallback;
301 CAConnectivityHandler_t connHandler;
302 connHandler.startAdapter = CAStartLE;
303 connHandler.stopAdapter = CAStopLE;
304 connHandler.startListenServer = CAStartLEListeningServer;
305 connHandler.startDiscoveryServer = CAStartLEDiscoveryServer;
306 connHandler.sendData = CASendLEUnicastData;
307 connHandler.sendDataToAll = CASendLEMulticastData;
308 connHandler.GetnetInfo = CAGetLEInterfaceInformation;
309 connHandler.readData = CAReadLEData;
310 connHandler.terminate = CATerminateLE;
311 registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
313 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
318 CAResult_t CAStartLE()
320 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
321 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE, not implemented");
322 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
326 CAResult_t CAStopLE()
328 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
332 ca_mutex_lock(g_bleIsServerMutex);
333 if (true == g_isServer)
335 CAStopBleGattServer();
339 CAStopBLEGattClient();
341 ca_mutex_unlock(g_bleIsServerMutex);
343 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
350 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
352 CASetBLEReqRespServerCallback(NULL);
353 CASetBLEReqRespClientCallback(NULL);
354 CALERegisterNetworkNotifications(NULL);
355 CASetBLEReqRespAdapterCallback(NULL);
356 CATerminateLENetworkMonitor();
358 ca_mutex_lock(g_bleIsServerMutex);
359 if (true == g_isServer)
361 CATerminateBleGattServer();
365 CATerminateBLEGattClient();
367 ca_mutex_unlock(g_bleIsServerMutex);
369 CATerminateBleQueues();
371 CATerminateBleAdapterMutex();
373 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
376 CAResult_t CAStartLEListeningServer()
378 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
380 CAResult_t result = CAInitBleServerQueues();
381 if (CA_STATUS_OK != result)
383 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
384 return CA_STATUS_FAILED;
387 result = CAGetLEAdapterState();
388 if (CA_ADAPTER_NOT_ENABLED == result)
390 gLeServerStatus = CA_LISTENING_SERVER;
391 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
395 if (CA_STATUS_FAILED == result)
397 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
398 return CA_STATUS_FAILED;
401 CAStartBleGattServer();
403 ca_mutex_lock(g_bleIsServerMutex);
405 ca_mutex_unlock(g_bleIsServerMutex);
407 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
411 CAResult_t CAStartLEDiscoveryServer()
413 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
415 CAResult_t result = CAInitBleClientQueues();
416 if (CA_STATUS_OK != result)
418 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
419 return CA_STATUS_FAILED;
422 result = CAGetLEAdapterState();
423 if (CA_ADAPTER_NOT_ENABLED == result)
425 gLeServerStatus = CA_DISCOVERY_SERVER;
426 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
430 if (CA_STATUS_FAILED == result)
432 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
433 return CA_STATUS_FAILED;
436 CAStartBLEGattClient();
438 ca_mutex_lock(g_bleIsServerMutex);
440 ca_mutex_unlock(g_bleIsServerMutex);
442 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
446 CAResult_t CAStartLENotifyServer()
448 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
450 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
454 uint32_t CASendLENotification(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
456 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
458 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
462 CAResult_t CAReadLEData()
464 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
466 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
470 int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
472 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
475 VERIFY_NON_NULL_RET(endpoint, NULL, "Remote endpoint is null", -1);
476 VERIFY_NON_NULL_RET(data, NULL, "Data is null", -1);
478 CAResult_t result = CA_STATUS_FAILED;
480 ca_mutex_lock(g_bleIsServerMutex);
481 if (true == g_isServer)
483 result = CABLEServerSendData(endpoint, data, dataLen);
484 if (CA_STATUS_OK != result)
486 OIC_LOG(ERROR, CALEADAPTER_TAG,
487 "[SendLEUnicastData] CABleServerSenderQueueEnqueueMessage failed \n");
488 g_errorHandler((void *) endpoint, (void *) data, dataLen, result);
489 ca_mutex_unlock(g_bleIsServerMutex);
495 result = CABLEClientSendData(endpoint, data, dataLen);
496 if (CA_STATUS_OK != result)
498 OIC_LOG(ERROR, CALEADAPTER_TAG,
499 "[SendLEUnicastData] CABleClientSenderQueueEnqueueMessage failed \n");
500 g_errorHandler(endpoint, data, dataLen, result);
501 ca_mutex_unlock(g_bleIsServerMutex);
505 ca_mutex_unlock(g_bleIsServerMutex);
507 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
511 int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
513 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
516 VERIFY_NON_NULL_RET(data, NULL, "Data is null", -1);
520 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
524 CAResult_t result = CA_STATUS_FAILED;
526 ca_mutex_lock(g_bleIsServerMutex);
527 if (true == g_isServer)
529 result = CABLEServerSendData(NULL, data, dataLen);
530 if (CA_STATUS_OK != result)
532 OIC_LOG(ERROR, CALEADAPTER_TAG,
533 "[SendLEMulticastDataToAll] CABleServerSenderQueueEnqueueMessage failed" );
534 ca_mutex_unlock(g_bleIsServerMutex);
535 g_errorHandler(endpoint, data, dataLen, result);
541 result = CABLEClientSendData(NULL, data, dataLen);
542 if (CA_STATUS_OK != result)
544 OIC_LOG(ERROR, CALEADAPTER_TAG,
545 "[SendLEMulticastDataToAll] CABleClientSenderQueueEnqueueMessage failed" );
546 g_errorHandler(endpoint, data, dataLen, result);
547 ca_mutex_unlock(g_bleIsServerMutex);
551 ca_mutex_unlock(g_bleIsServerMutex);
553 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
557 CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
559 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
561 VERIFY_NON_NULL(info, NULL, "CALocalConnectivity info is null");
563 char *local_address = NULL;
565 CAResult_t res = CAGetLEAddress(&local_address);
566 if (CA_STATUS_OK != res)
568 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
572 if (NULL == local_address)
574 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
575 return CA_STATUS_FAILED;
579 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof (CAEndpoint_t));
582 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
583 OICFree(local_address);
584 return CA_STATUS_FAILED;
587 size_t local_address_len = strlen(local_address);
589 if(local_address_len >= sizeof(g_localBLEAddress) ||
590 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
592 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
594 OICFree(local_address);
595 return CA_STATUS_FAILED;
598 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
599 ca_mutex_lock(g_bleLocalAddressMutex);
600 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
601 ca_mutex_unlock(g_bleLocalAddressMutex);
603 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
605 OICFree(local_address);
607 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
611 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
613 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
615 ca_mutex_lock(g_bleNetworkCbMutex);
616 g_networkCallback = netCallback;
617 ca_mutex_unlock(g_bleNetworkCbMutex);
618 CAResult_t res = CA_STATUS_OK;
621 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
622 if (CA_STATUS_OK != res)
624 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
629 res = CAUnSetLEAdapterStateChangedCb();
630 if (CA_STATUS_OK != res)
632 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
636 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
640 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
642 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
644 VERIFY_NON_NULL_VOID(g_localBLEAddress, NULL, "g_localBLEAddress is null");
645 CAEndpoint_t localEndpoint = {};
646 localEndpoint.adapter = CA_ADAPTER_GATT_BTLE;
647 ca_mutex_lock(g_bleLocalAddressMutex);
648 OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), g_localBLEAddress);
649 ca_mutex_unlock(g_bleLocalAddressMutex);
651 g_bleAdapterState = adapter_state;
652 // Start a GattServer/Client if gLeServerStatus is SET
653 if (CA_LISTENING_SERVER == gLeServerStatus)
655 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattServer");
656 CAStartBleGattServer();
658 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
660 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
661 CAStartBLEGattClient();
663 gLeServerStatus = CA_SERVER_NOTSTARTED;
665 ca_mutex_lock(g_bleNetworkCbMutex);
666 if (NULL != g_networkCallback)
668 g_networkCallback(&localEndpoint, adapter_state);
672 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
674 ca_mutex_unlock(g_bleNetworkCbMutex);
676 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
679 CAResult_t CAInitBleAdapterMutex()
681 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
683 if (NULL == g_bleIsServerMutex)
685 g_bleIsServerMutex = ca_mutex_new();
686 if (NULL == g_bleIsServerMutex)
688 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
689 return CA_STATUS_FAILED;
693 if (NULL == g_bleNetworkCbMutex)
695 g_bleNetworkCbMutex = ca_mutex_new();
696 if (NULL == g_bleNetworkCbMutex)
698 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
699 return CA_STATUS_FAILED;
703 if (NULL == g_bleLocalAddressMutex)
705 g_bleLocalAddressMutex = ca_mutex_new();
706 if (NULL == g_bleLocalAddressMutex)
708 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
709 return CA_STATUS_FAILED;
713 if (NULL == g_bleAdapterThreadPoolMutex)
715 g_bleAdapterThreadPoolMutex = ca_mutex_new();
716 if (NULL == g_bleAdapterThreadPoolMutex)
718 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
719 return CA_STATUS_FAILED;
723 if (NULL == g_bleClientSendDataMutex)
725 g_bleClientSendDataMutex = ca_mutex_new();
726 if (NULL == g_bleClientSendDataMutex)
728 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
729 return CA_STATUS_FAILED;
733 if (NULL == g_bleClientReceiveDataMutex)
735 g_bleClientReceiveDataMutex = ca_mutex_new();
736 if (NULL == g_bleClientReceiveDataMutex)
738 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
739 return CA_STATUS_FAILED;
743 if (NULL == g_bleServerSendDataMutex)
745 g_bleServerSendDataMutex = ca_mutex_new();
746 if (NULL == g_bleServerSendDataMutex)
748 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
749 return CA_STATUS_FAILED;
753 if (NULL == g_bleServerReceiveDataMutex)
755 g_bleServerReceiveDataMutex = ca_mutex_new();
756 if (NULL == g_bleServerReceiveDataMutex)
758 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
759 return CA_STATUS_FAILED;
763 if (NULL == g_bleAdapterReqRespCbMutex)
765 g_bleAdapterReqRespCbMutex = ca_mutex_new();
766 if (NULL == g_bleAdapterReqRespCbMutex)
768 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
769 return CA_STATUS_FAILED;
773 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
777 void CATerminateBleAdapterMutex()
779 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
781 ca_mutex_free(g_bleIsServerMutex);
782 g_bleIsServerMutex = NULL;
784 ca_mutex_free(g_bleNetworkCbMutex);
785 g_bleNetworkCbMutex = NULL;
787 ca_mutex_free(g_bleLocalAddressMutex);
788 g_bleLocalAddressMutex = NULL;
790 ca_mutex_free(g_bleAdapterThreadPoolMutex);
791 g_bleAdapterThreadPoolMutex = NULL;
793 ca_mutex_free(g_bleClientSendDataMutex);
794 g_bleClientSendDataMutex = NULL;
796 ca_mutex_free(g_bleClientReceiveDataMutex);
797 g_bleClientReceiveDataMutex = NULL;
799 ca_mutex_free(g_bleServerSendDataMutex);
800 g_bleServerSendDataMutex = NULL;
802 ca_mutex_free(g_bleServerReceiveDataMutex);
803 g_bleServerReceiveDataMutex = NULL;
805 ca_mutex_free(g_bleAdapterReqRespCbMutex);
806 g_bleAdapterReqRespCbMutex = NULL;
808 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
811 void CAInitBleQueues()
813 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
815 CAResult_t result = CAInitBleServerQueues();
816 if (CA_STATUS_OK != result)
818 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerQueues failed");
822 result = CAInitBleClientQueues();
823 if (CA_STATUS_OK != result)
825 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
829 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
832 CAResult_t CAInitBleServerQueues()
834 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
836 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
838 CAResult_t result = CAInitBleServerSenderQueue();
839 if (CA_STATUS_OK != result)
841 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
842 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
843 return CA_STATUS_FAILED;
846 result = CAInitBleServerReceiverQueue();
847 if (CA_STATUS_OK != result)
849 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerReceiverQueue failed");
850 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
851 return CA_STATUS_FAILED;
854 g_dataReceiverHandlerState = true;
856 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
858 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
862 CAResult_t CAInitBleClientQueues()
864 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
866 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
868 CAResult_t result = CAInitBleClientSenderQueue();
869 if (CA_STATUS_OK != result)
871 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
872 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
873 return CA_STATUS_FAILED;
876 result = CAInitBleClientReceiverQueue();
877 if (CA_STATUS_OK != result)
879 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientReceiverQueue failed");
880 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
881 return CA_STATUS_FAILED;
884 g_dataReceiverHandlerState = true;
886 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
888 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
892 CAResult_t CAInitBleServerSenderQueue()
894 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
895 // Check if the message queue is already initialized
896 if (g_sendQueueHandle)
898 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
902 // Create send message queue
903 g_sendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
904 if (!g_sendQueueHandle)
906 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
907 return CA_MEMORY_ALLOC_FAILED;
910 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_sendQueueHandle, g_bleAdapterThreadPool,
911 CABLEServerSendDataThread, CALEDataDestroyer))
913 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
914 OICFree(g_sendQueueHandle);
915 g_sendQueueHandle = NULL;
916 return CA_STATUS_FAILED;
919 if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle))
921 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
922 OICFree(g_sendQueueHandle);
923 g_sendQueueHandle = NULL;
924 return CA_STATUS_FAILED;
927 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
931 CAResult_t CAInitBleClientSenderQueue()
933 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
935 if (g_bleClientSendQueueHandle)
937 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
941 // Create send message queue
942 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
943 if (!g_bleClientSendQueueHandle)
945 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
946 return CA_MEMORY_ALLOC_FAILED;
949 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle, g_bleAdapterThreadPool,
950 CABLEClientSendDataThread, CALEDataDestroyer))
952 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
953 OICFree(g_bleClientSendQueueHandle);
954 g_bleClientSendQueueHandle = NULL;
955 return CA_STATUS_FAILED;
958 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientSendQueueHandle))
960 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
961 OICFree(g_bleClientSendQueueHandle);
962 g_bleClientSendQueueHandle = NULL;
963 return CA_STATUS_FAILED;
966 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
970 CAResult_t CAInitBleServerReceiverQueue()
972 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
973 // Check if the message queue is already initialized
974 if (g_bleServerReceiverQueue)
976 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
980 // Create send message queue
981 g_bleServerReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
982 if (!g_bleServerReceiverQueue)
984 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
985 OICFree(g_sendQueueHandle);
986 return CA_MEMORY_ALLOC_FAILED;
989 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerReceiverQueue, g_bleAdapterThreadPool,
990 CABLEServerDataReceiverHandler, CALEDataDestroyer))
992 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
993 OICFree(g_bleServerReceiverQueue);
994 g_bleServerReceiverQueue = NULL;
995 return CA_STATUS_FAILED;
998 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerReceiverQueue))
1000 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
1001 OICFree(g_bleServerReceiverQueue);
1002 g_bleServerReceiverQueue = NULL;
1003 return CA_STATUS_FAILED;
1006 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1007 return CA_STATUS_OK;
1010 CAResult_t CAInitBleClientReceiverQueue()
1012 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1014 // Check if the message queue is already initialized
1015 if (g_bleClientReceiverQueue)
1017 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
1021 // Create send message queue
1022 g_bleClientReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
1023 if (!g_bleClientReceiverQueue)
1025 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1026 OICFree(g_bleClientSendQueueHandle);
1027 return CA_MEMORY_ALLOC_FAILED;
1030 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientReceiverQueue, g_bleAdapterThreadPool,
1031 CABLEClientDataReceiverHandler, NULL))
1033 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
1034 OICFree(g_bleClientSendQueueHandle);
1035 OICFree(g_bleClientReceiverQueue);
1036 g_bleClientReceiverQueue = NULL;
1037 return CA_STATUS_FAILED;
1040 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientReceiverQueue))
1042 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
1043 OICFree(g_bleClientReceiverQueue);
1044 g_bleClientReceiverQueue = NULL;
1045 return CA_STATUS_FAILED;
1048 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1049 return CA_STATUS_OK;
1052 void CAStopBleQueues()
1054 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1056 ca_mutex_lock(g_bleClientSendDataMutex);
1057 if (NULL != g_bleClientSendQueueHandle)
1059 CAQueueingThreadStop(g_bleClientSendQueueHandle);
1061 ca_mutex_unlock(g_bleClientSendDataMutex);
1063 ca_mutex_lock(g_bleClientReceiveDataMutex);
1064 if (NULL != g_bleClientReceiverQueue)
1066 CAQueueingThreadStop(g_bleClientReceiverQueue);
1068 ca_mutex_unlock(g_bleClientReceiveDataMutex);
1070 ca_mutex_lock(g_bleServerSendDataMutex);
1071 if (NULL != g_sendQueueHandle)
1073 CAQueueingThreadStop(g_sendQueueHandle);
1075 ca_mutex_unlock(g_bleServerSendDataMutex);
1077 ca_mutex_lock(g_bleServerReceiveDataMutex);
1078 if (NULL != g_bleServerReceiverQueue)
1080 CAQueueingThreadStop(g_bleServerReceiverQueue);
1082 ca_mutex_unlock(g_bleServerReceiveDataMutex);
1084 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1087 void CATerminateBleQueues()
1089 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1091 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
1092 OICFree(g_bleClientSendQueueHandle);
1093 g_bleClientSendQueueHandle = NULL;
1096 CAQueueingThreadDestroy(g_bleClientReceiverQueue);
1097 OICFree(g_bleClientReceiverQueue);
1098 g_bleClientReceiverQueue = NULL;
1101 CAQueueingThreadDestroy(g_sendQueueHandle);
1102 OICFree(g_sendQueueHandle);
1103 g_sendQueueHandle = NULL;
1106 CAQueueingThreadDestroy(g_bleServerReceiverQueue);
1107 OICFree(g_bleServerReceiverQueue);
1108 g_bleServerReceiverQueue = NULL;
1110 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1112 void CABLEServerDataReceiverHandler(void *threadData)
1114 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1116 static uint32_t recvDataLen = 0;
1117 static uint32_t totalDataLen = 0;
1118 static char *defragData = NULL;
1119 static bool isHeaderAvailable = false;
1120 static CAEndpoint_t *remoteEndpoint = NULL;
1122 ca_mutex_lock(g_bleServerReceiveDataMutex);
1124 if (g_dataReceiverHandlerState)
1126 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1128 CALEData_t *bleData = (CALEData_t *) threadData;
1131 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
1135 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1137 if (!isHeaderAvailable)
1139 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1140 totalDataLen = CAParseHeader((char*)bleData->data);
1142 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes", totalDataLen);
1143 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%d] bytes", bleData->dataLen);
1145 defragData = (char *) OICCalloc(totalDataLen + 1, sizeof(char));
1146 if (NULL == defragData)
1148 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1152 const char *remoteAddress = bleData->remoteEndpoint->addr;
1154 remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_GATT_BTLE,
1157 memcpy(defragData + recvDataLen, bleData->data + CA_HEADER_LENGTH,
1158 bleData->dataLen - CA_HEADER_LENGTH);
1159 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1160 isHeaderAvailable = true;
1164 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1165 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1166 recvDataLen += bleData->dataLen ;
1167 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1168 totalDataLen, recvDataLen);
1170 if (totalDataLen == recvDataLen)
1172 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1173 if (NULL == g_networkPacketReceivedCallback)
1175 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1176 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1179 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1180 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1183 isHeaderAvailable = false;
1184 remoteEndpoint = NULL;
1186 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1189 if (false == g_dataReceiverHandlerState)
1191 OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1194 isHeaderAvailable = false;
1195 OICFree(defragData);
1196 CAFreeEndpoint(remoteEndpoint);
1197 ca_mutex_unlock(g_bleServerReceiveDataMutex);
1201 ca_mutex_unlock(g_bleServerReceiveDataMutex);
1202 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1205 void CABLEClientDataReceiverHandler(void *threadData)
1207 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1209 static const char *remoteAddress = NULL;
1210 static uint32_t recvDataLen = 0;
1211 static uint32_t totalDataLen = 0;
1212 static char *defragData = NULL;
1213 static bool isHeaderAvailable = false;
1214 static CAEndpoint_t *remoteEndpoint = NULL;
1216 ca_mutex_lock(g_bleClientReceiveDataMutex);
1218 if (g_dataReceiverHandlerState)
1220 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1222 CALEData_t *bleData = (CALEData_t *) threadData;
1225 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid wifidata!");
1229 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1231 if (!isHeaderAvailable)
1233 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1235 totalDataLen = CAParseHeader(bleData->data);
1236 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes",
1238 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received in the first packet [%d] bytes",
1241 defragData = (char *) OICMalloc(sizeof(char) * totalDataLen);
1242 if (NULL == defragData)
1244 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1248 remoteAddress = bleData->remoteEndpoint->addr;
1250 remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_GATT_BTLE,
1253 memcpy(defragData, bleData->data + CA_HEADER_LENGTH,
1254 bleData->dataLen - CA_HEADER_LENGTH);
1255 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1256 isHeaderAvailable = true;
1260 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1261 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1262 recvDataLen += bleData->dataLen ;
1263 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1264 totalDataLen, recvDataLen);
1266 if (totalDataLen == recvDataLen)
1268 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1269 if (NULL == g_networkPacketReceivedCallback)
1271 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1272 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1275 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1276 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1279 isHeaderAvailable = false;
1280 remoteEndpoint = NULL;
1282 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1285 if (false == g_dataReceiverHandlerState)
1287 OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1288 OICFree(defragData);
1289 CAFreeEndpoint(remoteEndpoint);
1290 ca_mutex_unlock(g_bleClientReceiveDataMutex);
1294 ca_mutex_unlock(g_bleClientReceiveDataMutex);
1295 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1298 void CABLEServerSendDataThread(void *threadData)
1300 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1302 CALEData_t *bleData = (CALEData_t *) threadData;
1305 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1309 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1310 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1312 int32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1314 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server total Data length with header is [%d]", totalLength);
1315 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
1316 if (NULL == dataSegment)
1318 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1323 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1324 if (CA_STATUS_OK != result )
1326 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1328 OICFree(dataSegment);
1332 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1336 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1338 length = totalLength;
1339 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1343 length = CA_SUPPORTED_BLE_MTU_SIZE;
1344 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1345 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1348 int32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1350 // Send the first segment with the header.
1351 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1353 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
1354 result = CAUpdateCharacteristicsToGattClient(
1355 bleData->remoteEndpoint->addr, dataSegment, length);
1356 if (CA_STATUS_OK != result)
1358 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1359 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1360 OICFree(dataSegment);
1364 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1365 for (index = 1; index < iter; index++)
1367 // Send the remaining header.
1368 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1369 result = CAUpdateCharacteristicsToGattClient(
1370 bleData->remoteEndpoint->addr,
1371 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1372 CA_SUPPORTED_BLE_MTU_SIZE);
1373 if (CA_STATUS_OK != result)
1375 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1376 "Update characteristics failed, result [%d]", result);
1377 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1378 OICFree(dataSegment);
1381 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
1382 CA_SUPPORTED_BLE_MTU_SIZE);
1385 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1386 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1388 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1389 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1390 result = CAUpdateCharacteristicsToGattClient(
1391 bleData->remoteEndpoint->addr,
1392 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1394 if (CA_STATUS_OK != result)
1396 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1398 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1399 OICFree(dataSegment);
1402 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1407 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
1408 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
1409 if (CA_STATUS_OK != result)
1411 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1413 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1414 OICFree(dataSegment);
1417 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1418 for (index = 1; index < iter; index++)
1420 // Send the remaining header.
1421 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1422 result = CAUpdateCharacteristicsToAllGattClients(
1423 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1424 CA_SUPPORTED_BLE_MTU_SIZE);
1425 if (CA_STATUS_OK != result)
1427 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1429 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1430 OICFree(dataSegment);
1433 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
1436 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1437 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1439 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1440 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1441 result = CAUpdateCharacteristicsToAllGattClients(
1442 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1444 if (CA_STATUS_OK != result)
1446 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1448 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1449 OICFree(dataSegment);
1452 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1455 OICFree(dataSegment);
1457 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1460 void CABLEClientSendDataThread(void *threadData)
1462 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1464 CALEData_t *bleData = (CALEData_t *) threadData;
1467 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1471 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1472 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1474 uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1475 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
1476 if (NULL == dataSegment)
1478 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1483 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1484 if (CA_STATUS_OK != result )
1486 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1488 OICFree(dataSegment);
1491 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1494 uint32_t length = 0;
1495 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1497 length = totalLength;
1498 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1499 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1503 length = CA_SUPPORTED_BLE_MTU_SIZE;
1504 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1505 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1506 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1509 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1511 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1513 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1514 // Send the first segment with the header.
1515 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addr,
1520 if (CA_STATUS_OK != result)
1522 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1523 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1524 OICFree(dataSegment);
1528 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1529 for (index = 1; index < iter; index++)
1531 // Send the remaining header.
1532 result = CAUpdateCharacteristicsToGattServer(
1533 bleData->remoteEndpoint->addr,
1534 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1535 CA_SUPPORTED_BLE_MTU_SIZE,
1537 if (CA_STATUS_OK != result)
1539 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1541 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1542 OICFree(dataSegment);
1545 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1546 CA_SUPPORTED_BLE_MTU_SIZE);
1549 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1550 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1552 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1553 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1554 result = CAUpdateCharacteristicsToGattServer(
1555 bleData->remoteEndpoint->addr,
1556 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1560 if (CA_STATUS_OK != result)
1562 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1564 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1565 OICFree(dataSegment);
1568 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1573 //Sending Mulitcast Data
1574 // Send the first segment with the header.
1575 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1576 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1577 if (CA_STATUS_OK != result)
1579 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1580 "Update characteristics (all) failed, result [%d]", result);
1581 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1582 OICFree(dataSegment);
1585 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1586 // Send the remaining header.
1587 for (index = 1; index < iter; index++)
1589 result = CAUpdateCharacteristicsToAllGattServers(
1590 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1591 CA_SUPPORTED_BLE_MTU_SIZE);
1592 if (CA_STATUS_OK != result)
1594 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]",
1596 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1597 OICFree(dataSegment);
1600 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1601 CA_SUPPORTED_BLE_MTU_SIZE);
1604 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1605 if ( remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1607 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1608 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1609 result = CAUpdateCharacteristicsToAllGattServers(
1610 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1612 if (CA_STATUS_OK != result)
1614 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1615 "Update characteristics (all) failed, result [%d]", result);
1616 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1617 OICFree(dataSegment);
1620 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1625 OICFree(dataSegment);
1627 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1630 CALEData_t *CACreateBLEData(const CAEndpoint_t *remoteEndpoint, const void *data,
1631 uint32_t dataLength)
1633 CALEData_t *bleData = (CALEData_t *) OICMalloc(sizeof(CALEData_t));
1636 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1640 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1641 bleData->data = (void *)OICCalloc(dataLength + 1, 1);
1642 if (NULL == bleData->data)
1644 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1645 CAFreeBLEData(bleData);
1648 memcpy(bleData->data, data, dataLength);
1649 bleData->dataLen = dataLength;
1654 void CAFreeBLEData(CALEData_t *bleData)
1656 VERIFY_NON_NULL_VOID(bleData, NULL, "Param bleData is NULL");
1658 CAFreeEndpoint(bleData->remoteEndpoint);
1659 OICFree(bleData->data);
1663 void CALEDataDestroyer(void *data, uint32_t size)
1665 CALEData_t *ledata = (CALEData_t *) data;
1667 CAFreeBLEData(ledata);
1671 CAResult_t CABLEClientSendData(const CAEndpoint_t *remoteEndpoint,
1675 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1677 VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1679 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
1680 "g_bleClientSendQueueHandle is NULL",
1682 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
1683 "g_bleClientSendDataMutex is NULL",
1686 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
1689 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1691 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1694 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1695 return CA_MEMORY_ALLOC_FAILED;
1697 // Add message to send queue
1698 ca_mutex_lock(g_bleClientSendDataMutex);
1699 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
1700 ca_mutex_unlock(g_bleClientSendDataMutex);
1702 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1703 return CA_STATUS_OK;
1707 CAResult_t CABLEServerSendData(const CAEndpoint_t *remoteEndpoint,
1711 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1713 VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1715 VERIFY_NON_NULL_RET(g_sendQueueHandle, CALEADAPTER_TAG,
1716 "BleClientReceiverQueue is NULL",
1718 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
1719 "BleClientSendDataMutex is NULL",
1722 VERIFY_NON_NULL_RET(g_sendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
1725 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1727 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1730 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1731 return CA_MEMORY_ALLOC_FAILED;
1733 // Add message to send queue
1734 ca_mutex_lock(g_bleServerSendDataMutex);
1735 CAQueueingThreadAddData(g_sendQueueHandle, bleData, sizeof(CALEData_t));
1736 ca_mutex_unlock(g_bleServerSendDataMutex);
1738 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1739 return CA_STATUS_OK;
1742 CAResult_t CABLEServerReceivedData(const char *remoteAddress, const char *serviceUUID,
1743 const void *data, uint32_t dataLength, uint32_t *sentLength)
1745 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1748 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1749 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1750 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1751 VERIFY_NON_NULL_RET(g_bleServerReceiverQueue, CALEADAPTER_TAG, "g_bleServerReceiverQueue",
1754 //Add message to data queue
1755 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
1756 CA_ADAPTER_GATT_BTLE,
1758 if (NULL == remoteEndpoint)
1760 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1761 return CA_STATUS_FAILED;
1764 // Create bleData to add to queue
1765 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1767 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1770 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1771 CAFreeEndpoint(remoteEndpoint);
1772 return CA_MEMORY_ALLOC_FAILED;
1775 CAFreeEndpoint(remoteEndpoint);
1776 // Add message to send queue
1777 CAQueueingThreadAddData(g_bleServerReceiverQueue, bleData, sizeof(CALEData_t));
1779 *sentLength = dataLength;
1781 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1782 return CA_STATUS_OK;
1785 CAResult_t CABLEClientReceivedData(const char *remoteAddress, const char *serviceUUID,
1786 const void *data, uint32_t dataLength, uint32_t *sentLength)
1788 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1791 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1792 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1793 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1794 VERIFY_NON_NULL_RET(g_bleClientReceiverQueue, CALEADAPTER_TAG, "g_bleClientReceiverQueue",
1797 //Add message to data queue
1798 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
1799 CA_ADAPTER_GATT_BTLE,
1801 if (NULL == remoteEndpoint)
1803 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1804 return CA_STATUS_FAILED;
1807 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1809 // Create bleData to add to queue
1810 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1813 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1814 CAFreeEndpoint(remoteEndpoint);
1815 return CA_MEMORY_ALLOC_FAILED;
1818 CAFreeEndpoint(remoteEndpoint);
1819 // Add message to send queue
1820 CAQueueingThreadAddData(g_bleClientReceiverQueue, bleData, sizeof(CALEData_t));
1822 *sentLength = dataLength;
1824 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1825 return CA_STATUS_OK;
1828 void CASetBleAdapterThreadPoolHandle(ca_thread_pool_t handle)
1830 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1832 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1833 g_bleAdapterThreadPool = handle;
1834 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1836 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1839 void CASetBLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
1841 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1843 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1845 g_networkPacketReceivedCallback = callback;
1847 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1849 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1852 void CALEErrorHandler(const char *remoteAddress, const void *data, uint32_t dataLen,
1855 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
1857 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
1858 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_GATT_BTLE,
1860 //if required, will be used to build remote end point
1861 g_errorHandler(rep, data, dataLen, result);
1863 CAFreeEndpoint(rep);
1864 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");