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[16] = {0};
54 * @brief Variable to differentiate btw GattServer and GattClient.
56 static bool g_isServer = false;
59 * @var g_bleIsServerMutex
60 * @brief Mutex to synchronize the task to be executed on the GattServer function calls.
62 static ca_mutex g_bleIsServerMutex = NULL;
65 * @var g_bleNetworkCbMutex
66 * @brief Mutex to synchronize the callback to be called for the network changes.
68 static ca_mutex g_bleNetworkCbMutex = NULL;
71 * @var g_bleLocalAddressMutex
72 * @brief Mutex to synchronize the updation of the local LE address of the adapter.
74 static ca_mutex g_bleLocalAddressMutex = NULL;
77 * @var g_bleAdapterThreadPool
78 * @brief reference to threadpool
80 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
83 * @var g_bleAdapterThreadPoolMutex
84 * @brief Mutex to synchronize the task to be pushed to thread pool.
86 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
89 * @var g_bleClientSendQueueHandle
90 * @brief Queue to process the outgoing packets from GATTClient.
92 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
95 * @var g_bleClientReceiverQueue
96 * @brief Queue to process the incoming packets to GATT Client.
98 static CAQueueingThread_t *g_bleClientReceiverQueue = NULL;
101 * @var g_bleClientSendDataMutex
102 * @brief Mutex to synchronize the queing of the data from SenderQueue.
104 static ca_mutex g_bleClientSendDataMutex = NULL;
107 * @var g_bleClientReceiveDataMutex
108 * @brief Mutex to synchronize the queing of the data from ReceiverQueue.
110 static ca_mutex g_bleClientReceiveDataMutex = NULL;
113 * @var g_dataReceiverHandlerState
114 * @brief Stop condition of recvhandler.
116 static bool g_dataReceiverHandlerState = false;
119 * @var g_sendQueueHandle
120 * @brief Queue to process the outgoing packets from GATTServer.
122 static CAQueueingThread_t *g_sendQueueHandle = NULL;
125 * @var g_bleServerReceiverQueue
126 * @brief Queue to process the incoming packets to GATTServer
128 static CAQueueingThread_t *g_bleServerReceiverQueue = NULL;
131 * @var g_bleServerSendDataMutex
132 * @brief Mutex to synchronize the queing of the data from SenderQueue.
134 static ca_mutex g_bleServerSendDataMutex = NULL;
137 * @var g_bleServerReceiveDataMutex
138 * @brief Mutex to synchronize the queing of the data from ReceiverQueue.
140 static ca_mutex g_bleServerReceiveDataMutex = NULL;
143 * @var g_bleAdapterReqRespCbMutex
144 * @brief Mutex to synchronize the callback to be called for the adapterReqResponse.
146 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
149 * @var g_networkPacketReceivedCallback
150 * @brief Callback to be called when network packet recieved from either GattServer or GattClient.
152 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
155 * @ENUM CALeServerStatus
156 * @brief status of BLE Server Status
157 * This ENUM provides information of LE Adapter Server status
161 CA_SERVER_NOTSTARTED = 0,
167 * @var gLeServerStatus
168 * @brief structure to maintain the status of the server.
170 static CALeServerStatus gLeServerStatus = CA_SERVER_NOTSTARTED;
173 * @fn CALERegisterNetworkNotifications
174 * @brief This function is used to register network change notification callback.
176 * @param[in] netCallback CANetworkChangeCallback callback which will be set for the change in nwk.
178 * @return 0 on success otherwise a positive error value.
179 * @retval CA_STATUS_OK Successful
180 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
181 * @retval CA_STATUS_FAILED Operation failed
184 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
187 * @fn CASetBleAdapterThreadPoolHandle
188 * @brief Used to Set the gThreadPool handle which is required for spawning new thread.
190 * @param[in] handle - Thread pool handle which is given by above layer for using thread creation task.
195 void CASetBleAdapterThreadPoolHandle(ca_thread_pool_t handle);
198 * @fn CALEDeviceStateChangedCb
199 * @brief This function is used to call the callback to the upper layer when the device state gets
202 * @param[in] adapter_state New state of the adapter to be notified to the upper layer.
207 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state);
210 * @fn CAInitBleAdapterMutex
211 * @brief Used to initialize all required mutex variable for LE Adapter implementation.
213 * @return 0 on success otherwise a positive error value.
214 * @retval CA_STATUS_OK Successful
215 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
216 * @retval CA_STATUS_FAILED Operation failed
219 CAResult_t CAInitBleAdapterMutex();
222 * @fn CATerminateBleAdapterMutex
223 * @brief Used to terminate all required mutex variable for LE adapter implementation.
227 void CATerminateBleAdapterMutex();
230 * @fn CALEDataDestroyer
231 * @brief Used to free data
235 static void CALEDataDestroyer(void *data, uint32_t size);
237 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
238 CANetworkPacketReceivedCallback reqRespCallback,
239 CANetworkChangeCallback netCallback,
240 ca_thread_pool_t handle)
242 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
245 VERIFY_NON_NULL(registerCallback, NULL, "RegisterConnectivity callback is null");
246 VERIFY_NON_NULL(reqRespCallback, NULL, "PacketReceived Callback is null");
247 VERIFY_NON_NULL(netCallback, NULL, "NetworkChange Callback is null");
249 CAResult_t result = CAInitBleAdapterMutex();
250 if (CA_STATUS_OK != result)
252 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
253 return CA_STATUS_FAILED;
256 result = CAInitializeLENetworkMonitor();
257 if (CA_STATUS_OK != result)
259 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
260 return CA_STATUS_FAILED;
263 CAInitializeLEAdapter();
265 CASetBleClientThreadPoolHandle(handle);
266 CASetBleServerThreadPoolHandle(handle);
267 CASetBleAdapterThreadPoolHandle(handle);
268 CASetBLEReqRespServerCallback(CABLEServerReceivedData);
269 CASetBLEReqRespClientCallback(CABLEClientReceivedData);
270 CASetBLEReqRespAdapterCallback(reqRespCallback);
272 CALERegisterNetworkNotifications(netCallback);
274 CAConnectivityHandler_t connHandler;
275 connHandler.startAdapter = CAStartLE;
276 connHandler.stopAdapter = CAStopLE;
277 connHandler.startListenServer = CAStartLEListeningServer;
278 connHandler.startDiscoveryServer = CAStartLEDiscoveryServer;
279 connHandler.sendData = CASendLEUnicastData;
280 connHandler.sendDataToAll = CASendLEMulticastData;
281 connHandler.GetnetInfo = CAGetLEInterfaceInformation;
282 connHandler.readData = CAReadLEData;
283 connHandler.terminate = CATerminateLE;
284 registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
286 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
291 CAResult_t CAStartLE()
293 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
294 OIC_LOG(DEBUG, CALEADAPTER_TAG,
295 "There is no concept of unicast/multicast in LE. So This function is not implemented");
296 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
300 CAResult_t CAStopLE()
302 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
306 ca_mutex_lock(g_bleIsServerMutex);
307 if (true == g_isServer)
309 CAStopBleGattServer();
313 CAStopBLEGattClient();
315 ca_mutex_unlock(g_bleIsServerMutex);
317 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
324 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
326 CASetBLEReqRespServerCallback(NULL);
327 CASetBLEReqRespClientCallback(NULL);
328 CALERegisterNetworkNotifications(NULL);
329 CASetBLEReqRespAdapterCallback(NULL);
330 CATerminateLENetworkMonitor();
332 ca_mutex_lock(g_bleIsServerMutex);
333 if (true == g_isServer)
335 CATerminateBleGattServer();
339 CATerminateBLEGattClient();
341 ca_mutex_unlock(g_bleIsServerMutex);
343 CATerminateBleQueues();
345 CATerminateBleAdapterMutex();
347 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
350 CAResult_t CAStartLEListeningServer()
352 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
354 CAResult_t result = CAInitBleServerQueues();
355 if (CA_STATUS_OK != result)
357 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
358 return CA_STATUS_FAILED;
361 result = CAGetLEAdapterState();
362 if (CA_ADAPTER_NOT_ENABLED == result)
364 gLeServerStatus = CA_LISTENING_SERVER;
365 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
369 if (CA_STATUS_FAILED == result)
371 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
372 return CA_STATUS_FAILED;
375 CAStartBleGattServer();
377 ca_mutex_lock(g_bleIsServerMutex);
379 ca_mutex_unlock(g_bleIsServerMutex);
381 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
385 CAResult_t CAStartLEDiscoveryServer()
387 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
389 CAResult_t result = CAInitBleClientQueues();
390 if (CA_STATUS_OK != result)
392 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
393 return CA_STATUS_FAILED;
396 result = CAGetLEAdapterState();
397 if (CA_ADAPTER_NOT_ENABLED == result)
399 gLeServerStatus = CA_DISCOVERY_SERVER;
400 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
404 if (CA_STATUS_FAILED == result)
406 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
407 return CA_STATUS_FAILED;
410 CAStartBLEGattClient();
412 ca_mutex_lock(g_bleIsServerMutex);
414 ca_mutex_unlock(g_bleIsServerMutex);
416 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
420 CAResult_t CAStartLENotifyServer()
422 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
424 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
428 uint32_t CASendLENotification(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
430 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
432 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
436 CAResult_t CAReadLEData()
438 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
440 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
444 int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
446 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
449 VERIFY_NON_NULL_RET(endpoint, NULL, "Remote endpoint is null", -1);
450 VERIFY_NON_NULL_RET(data, NULL, "Data is null", -1);
452 CAResult_t result = CA_STATUS_FAILED;
454 ca_mutex_lock(g_bleIsServerMutex);
455 if (true == g_isServer)
457 result = CABLEServerSendData(endpoint, data, dataLen);
458 if (CA_STATUS_OK != result)
460 OIC_LOG(ERROR, CALEADAPTER_TAG,
461 "[SendLEUnicastData] CABleServerSenderQueueEnqueueMessage failed \n");
462 ca_mutex_unlock(g_bleIsServerMutex);
468 result = CABLEClientSendData(endpoint, data, dataLen);
469 if (CA_STATUS_OK != result)
471 OIC_LOG(ERROR, CALEADAPTER_TAG,
472 "[SendLEUnicastData] CABleClientSenderQueueEnqueueMessage failed \n");
473 ca_mutex_unlock(g_bleIsServerMutex);
477 ca_mutex_unlock(g_bleIsServerMutex);
479 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
483 int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
485 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
488 VERIFY_NON_NULL_RET(data, NULL, "Data is null", -1);
492 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
496 CAResult_t result = CA_STATUS_FAILED;
498 ca_mutex_lock(g_bleIsServerMutex);
499 if (true == g_isServer)
501 result = CABLEServerSendData(NULL, data, dataLen);
502 if (CA_STATUS_OK != result)
504 OIC_LOG(ERROR, CALEADAPTER_TAG,
505 "[SendLEMulticastDataToAll] CABleServerSenderQueueEnqueueMessage failed" );
506 ca_mutex_unlock(g_bleIsServerMutex);
512 result = CABLEClientSendData(NULL, data, dataLen);
513 if (CA_STATUS_OK != result)
515 OIC_LOG(ERROR, CALEADAPTER_TAG,
516 "[SendLEMulticastDataToAll] CABleClientSenderQueueEnqueueMessage failed" );
517 ca_mutex_unlock(g_bleIsServerMutex);
521 ca_mutex_unlock(g_bleIsServerMutex);
523 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
527 CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
529 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
531 VERIFY_NON_NULL(info, NULL, "CALocalConnectivity info is null");
533 char *local_address = NULL;
535 CAResult_t res = CAGetLEAddress(&local_address);
536 if (CA_STATUS_OK != res)
538 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
542 if (NULL == local_address)
544 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
545 return CA_STATUS_FAILED;
549 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof (CAEndpoint_t));
552 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
553 OICFree(local_address);
554 return CA_STATUS_FAILED;
557 size_t local_address_len = strlen(local_address);
559 if(local_address_len >= sizeof(g_localBLEAddress) ||
560 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
562 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
564 OICFree(local_address);
565 return CA_STATUS_FAILED;
568 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
569 ca_mutex_lock(g_bleLocalAddressMutex);
570 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
571 ca_mutex_unlock(g_bleLocalAddressMutex);
573 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
575 OICFree(local_address);
577 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
581 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
583 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
585 ca_mutex_lock(g_bleNetworkCbMutex);
586 g_networkCallback = netCallback;
587 ca_mutex_unlock(g_bleNetworkCbMutex);
588 CAResult_t res = CA_STATUS_OK;
591 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
592 if (CA_STATUS_OK != res)
594 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
599 res = CAUnSetLEAdapterStateChangedCb();
600 if (CA_STATUS_OK != res)
602 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
606 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
610 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
612 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
614 VERIFY_NON_NULL_VOID(g_localBLEAddress, NULL, "g_localBLEAddress is null");
615 CAEndpoint_t localEndpoint = {};
616 localEndpoint.adapter = CA_ADAPTER_GATT_BTLE;
617 ca_mutex_lock(g_bleLocalAddressMutex);
618 OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), g_localBLEAddress);
619 ca_mutex_unlock(g_bleLocalAddressMutex);
621 // Start a GattServer/Client if gLeServerStatus is SET
622 if (CA_LISTENING_SERVER == gLeServerStatus)
624 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattServer");
625 CAStartBleGattServer();
627 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
629 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
630 CAStartBLEGattClient();
632 gLeServerStatus = CA_SERVER_NOTSTARTED;
634 ca_mutex_lock(g_bleNetworkCbMutex);
635 if (NULL != g_networkCallback)
637 g_networkCallback(&localEndpoint, adapter_state);
641 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
643 ca_mutex_unlock(g_bleNetworkCbMutex);
645 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
648 CAResult_t CAInitBleAdapterMutex()
650 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
652 if (NULL == g_bleIsServerMutex)
654 g_bleIsServerMutex = ca_mutex_new();
655 if (NULL == g_bleIsServerMutex)
657 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
658 return CA_STATUS_FAILED;
662 if (NULL == g_bleNetworkCbMutex)
664 g_bleNetworkCbMutex = ca_mutex_new();
665 if (NULL == g_bleNetworkCbMutex)
667 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
668 return CA_STATUS_FAILED;
672 if (NULL == g_bleLocalAddressMutex)
674 g_bleLocalAddressMutex = ca_mutex_new();
675 if (NULL == g_bleLocalAddressMutex)
677 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
678 return CA_STATUS_FAILED;
682 if (NULL == g_bleAdapterThreadPoolMutex)
684 g_bleAdapterThreadPoolMutex = ca_mutex_new();
685 if (NULL == g_bleAdapterThreadPoolMutex)
687 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
688 return CA_STATUS_FAILED;
692 if (NULL == g_bleClientSendDataMutex)
694 g_bleClientSendDataMutex = ca_mutex_new();
695 if (NULL == g_bleClientSendDataMutex)
697 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
698 return CA_STATUS_FAILED;
702 if (NULL == g_bleClientReceiveDataMutex)
704 g_bleClientReceiveDataMutex = ca_mutex_new();
705 if (NULL == g_bleClientReceiveDataMutex)
707 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
708 return CA_STATUS_FAILED;
712 if (NULL == g_bleServerSendDataMutex)
714 g_bleServerSendDataMutex = ca_mutex_new();
715 if (NULL == g_bleServerSendDataMutex)
717 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
718 return CA_STATUS_FAILED;
722 if (NULL == g_bleServerReceiveDataMutex)
724 g_bleServerReceiveDataMutex = ca_mutex_new();
725 if (NULL == g_bleServerReceiveDataMutex)
727 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
728 return CA_STATUS_FAILED;
732 if (NULL == g_bleAdapterReqRespCbMutex)
734 g_bleAdapterReqRespCbMutex = ca_mutex_new();
735 if (NULL == g_bleAdapterReqRespCbMutex)
737 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
738 return CA_STATUS_FAILED;
742 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
746 void CATerminateBleAdapterMutex()
748 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
750 ca_mutex_free(g_bleIsServerMutex);
751 g_bleIsServerMutex = NULL;
753 ca_mutex_free(g_bleNetworkCbMutex);
754 g_bleNetworkCbMutex = NULL;
756 ca_mutex_free(g_bleLocalAddressMutex);
757 g_bleLocalAddressMutex = NULL;
759 ca_mutex_free(g_bleAdapterThreadPoolMutex);
760 g_bleAdapterThreadPoolMutex = NULL;
762 ca_mutex_free(g_bleClientSendDataMutex);
763 g_bleClientSendDataMutex = NULL;
765 ca_mutex_free(g_bleClientReceiveDataMutex);
766 g_bleClientReceiveDataMutex = NULL;
768 ca_mutex_free(g_bleServerSendDataMutex);
769 g_bleServerSendDataMutex = NULL;
771 ca_mutex_free(g_bleServerReceiveDataMutex);
772 g_bleServerReceiveDataMutex = NULL;
774 ca_mutex_free(g_bleAdapterReqRespCbMutex);
775 g_bleAdapterReqRespCbMutex = NULL;
777 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
780 void CAInitBleQueues()
782 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
784 CAResult_t result = CAInitBleServerQueues();
785 if (CA_STATUS_OK != result)
787 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerQueues failed");
791 result = CAInitBleClientQueues();
792 if (CA_STATUS_OK != result)
794 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
798 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
801 CAResult_t CAInitBleServerQueues()
803 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
805 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
807 CAResult_t result = CAInitBleServerSenderQueue();
808 if (CA_STATUS_OK != result)
810 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
811 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
812 return CA_STATUS_FAILED;
815 result = CAInitBleServerReceiverQueue();
816 if (CA_STATUS_OK != result)
818 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerReceiverQueue failed");
819 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
820 return CA_STATUS_FAILED;
823 g_dataReceiverHandlerState = true;
825 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
827 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
831 CAResult_t CAInitBleClientQueues()
833 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
835 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
837 CAResult_t result = CAInitBleClientSenderQueue();
838 if (CA_STATUS_OK != result)
840 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
841 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
842 return CA_STATUS_FAILED;
845 result = CAInitBleClientReceiverQueue();
846 if (CA_STATUS_OK != result)
848 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientReceiverQueue 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 CAInitBleServerSenderQueue()
863 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
864 // Check if the message queue is already initialized
865 if (g_sendQueueHandle)
867 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
871 // Create send message queue
872 g_sendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
873 if (!g_sendQueueHandle)
875 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
876 return CA_MEMORY_ALLOC_FAILED;
879 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_sendQueueHandle, g_bleAdapterThreadPool,
880 CABLEServerSendDataThread, CALEDataDestroyer))
882 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
883 OICFree(g_sendQueueHandle);
884 g_sendQueueHandle = NULL;
885 return CA_STATUS_FAILED;
888 if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle))
890 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
891 OICFree(g_sendQueueHandle);
892 g_sendQueueHandle = NULL;
893 return CA_STATUS_FAILED;
896 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
900 CAResult_t CAInitBleClientSenderQueue()
902 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
904 if (g_bleClientSendQueueHandle)
906 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
910 // Create send message queue
911 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
912 if (!g_bleClientSendQueueHandle)
914 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
915 return CA_MEMORY_ALLOC_FAILED;
918 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle, g_bleAdapterThreadPool,
919 CABLEClientSendDataThread, CALEDataDestroyer))
921 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
922 OICFree(g_bleClientSendQueueHandle);
923 g_bleClientSendQueueHandle = NULL;
924 return CA_STATUS_FAILED;
927 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientSendQueueHandle))
929 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
930 OICFree(g_bleClientSendQueueHandle);
931 g_bleClientSendQueueHandle = NULL;
932 return CA_STATUS_FAILED;
935 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
939 CAResult_t CAInitBleServerReceiverQueue()
941 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
942 // Check if the message queue is already initialized
943 if (g_bleServerReceiverQueue)
945 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
949 // Create send message queue
950 g_bleServerReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
951 if (!g_bleServerReceiverQueue)
953 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
954 OICFree(g_sendQueueHandle);
955 return CA_MEMORY_ALLOC_FAILED;
958 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerReceiverQueue, g_bleAdapterThreadPool,
959 CABLEServerDataReceiverHandler, CALEDataDestroyer))
961 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
962 OICFree(g_bleServerReceiverQueue);
963 g_bleServerReceiverQueue = NULL;
964 return CA_STATUS_FAILED;
967 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerReceiverQueue))
969 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
970 OICFree(g_bleServerReceiverQueue);
971 g_bleServerReceiverQueue = NULL;
972 return CA_STATUS_FAILED;
975 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
979 CAResult_t CAInitBleClientReceiverQueue()
981 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
983 // Check if the message queue is already initialized
984 if (g_bleClientReceiverQueue)
986 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
990 // Create send message queue
991 g_bleClientReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
992 if (!g_bleClientReceiverQueue)
994 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
995 OICFree(g_bleClientSendQueueHandle);
996 return CA_MEMORY_ALLOC_FAILED;
999 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientReceiverQueue, g_bleAdapterThreadPool,
1000 CABLEClientDataReceiverHandler, NULL))
1002 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
1003 OICFree(g_bleClientSendQueueHandle);
1004 OICFree(g_bleClientReceiverQueue);
1005 g_bleClientReceiverQueue = NULL;
1006 return CA_STATUS_FAILED;
1009 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientReceiverQueue))
1011 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
1012 OICFree(g_bleClientReceiverQueue);
1013 g_bleClientReceiverQueue = NULL;
1014 return CA_STATUS_FAILED;
1017 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1018 return CA_STATUS_OK;
1021 void CAStopBleQueues()
1023 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1025 ca_mutex_lock(g_bleClientSendDataMutex);
1026 if (NULL != g_bleClientSendQueueHandle)
1028 CAQueueingThreadStop(g_bleClientSendQueueHandle);
1030 ca_mutex_unlock(g_bleClientSendDataMutex);
1032 ca_mutex_lock(g_bleClientReceiveDataMutex);
1033 if (NULL != g_bleClientReceiverQueue)
1035 CAQueueingThreadStop(g_bleClientReceiverQueue);
1037 ca_mutex_unlock(g_bleClientReceiveDataMutex);
1039 ca_mutex_lock(g_bleServerSendDataMutex);
1040 if (NULL != g_sendQueueHandle)
1042 CAQueueingThreadStop(g_sendQueueHandle);
1044 ca_mutex_unlock(g_bleServerSendDataMutex);
1046 ca_mutex_lock(g_bleServerReceiveDataMutex);
1047 if (NULL != g_bleServerReceiverQueue)
1049 CAQueueingThreadStop(g_bleServerReceiverQueue);
1051 ca_mutex_unlock(g_bleServerReceiveDataMutex);
1053 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1056 void CATerminateBleQueues()
1058 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1060 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
1061 OICFree(g_bleClientSendQueueHandle);
1062 g_bleClientSendQueueHandle = NULL;
1065 CAQueueingThreadDestroy(g_bleClientReceiverQueue);
1066 OICFree(g_bleClientReceiverQueue);
1067 g_bleClientReceiverQueue = NULL;
1070 CAQueueingThreadDestroy(g_sendQueueHandle);
1071 OICFree(g_sendQueueHandle);
1072 g_sendQueueHandle = NULL;
1075 CAQueueingThreadDestroy(g_bleServerReceiverQueue);
1076 OICFree(g_bleServerReceiverQueue);
1077 g_bleServerReceiverQueue = NULL;
1079 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1081 void CABLEServerDataReceiverHandler(void *threadData)
1083 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1085 static uint32_t recvDataLen = 0;
1086 static uint32_t totalDataLen = 0;
1087 static char *defragData = NULL;
1088 static bool isHeaderAvailable = false;
1089 static CAEndpoint_t *remoteEndpoint = NULL;
1091 ca_mutex_lock(g_bleServerReceiveDataMutex);
1093 if (g_dataReceiverHandlerState)
1095 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1097 CALEData_t *bleData = (CALEData_t *) threadData;
1100 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
1104 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1106 if (!isHeaderAvailable)
1108 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1109 totalDataLen = CAParseHeader((char*)bleData->data);
1111 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes", totalDataLen);
1112 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%d] bytes", bleData->dataLen);
1114 defragData = (char *) OICCalloc(totalDataLen + 1, sizeof(char));
1115 if (NULL == defragData)
1117 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1121 const char *remoteAddress = bleData->remoteEndpoint->addr;
1123 remoteEndpoint = CAAdapterCreateEndpoint(0, CA_ADAPTER_GATT_BTLE, remoteAddress, 0);
1125 memcpy(defragData + recvDataLen, bleData->data + CA_HEADER_LENGTH,
1126 bleData->dataLen - CA_HEADER_LENGTH);
1127 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1128 isHeaderAvailable = true;
1132 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1133 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1134 recvDataLen += bleData->dataLen ;
1135 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1136 totalDataLen, recvDataLen);
1138 if (totalDataLen == recvDataLen)
1140 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1141 if (NULL == g_networkPacketReceivedCallback)
1143 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1144 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1147 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1148 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1151 isHeaderAvailable = false;
1152 remoteEndpoint = NULL;
1154 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1157 if (false == g_dataReceiverHandlerState)
1159 OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1162 isHeaderAvailable = false;
1163 OICFree(defragData);
1164 CAAdapterFreeEndpoint(remoteEndpoint);
1165 ca_mutex_unlock(g_bleServerReceiveDataMutex);
1169 ca_mutex_unlock(g_bleServerReceiveDataMutex);
1170 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1173 void CABLEClientDataReceiverHandler(void *threadData)
1175 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1177 static const char *remoteAddress = NULL;
1178 static uint32_t recvDataLen = 0;
1179 static uint32_t totalDataLen = 0;
1180 static char *defragData = NULL;
1181 static bool isHeaderAvailable = false;
1182 static CAEndpoint_t *remoteEndpoint = NULL;
1184 ca_mutex_lock(g_bleClientReceiveDataMutex);
1186 if (g_dataReceiverHandlerState)
1188 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1190 CALEData_t *bleData = (CALEData_t *) threadData;
1193 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid wifidata!");
1197 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1199 if (!isHeaderAvailable)
1201 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1203 totalDataLen = CAParseHeader(bleData->data);
1204 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes",
1206 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received in the first packet [%d] bytes",
1209 defragData = (char *) OICMalloc(sizeof(char) * totalDataLen);
1210 if (NULL == defragData)
1212 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1216 remoteAddress = bleData->remoteEndpoint->addr;
1218 remoteEndpoint = CAAdapterCreateEndpoint(0, CA_ADAPTER_GATT_BTLE, remoteAddress, 0);
1220 memcpy(defragData, bleData->data + CA_HEADER_LENGTH,
1221 bleData->dataLen - CA_HEADER_LENGTH);
1222 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1223 isHeaderAvailable = true;
1227 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1228 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1229 recvDataLen += bleData->dataLen ;
1230 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1231 totalDataLen, recvDataLen);
1233 if (totalDataLen == recvDataLen)
1235 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1236 if (NULL == g_networkPacketReceivedCallback)
1238 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1239 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1242 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1243 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1246 isHeaderAvailable = false;
1247 remoteEndpoint = NULL;
1249 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1252 if (false == g_dataReceiverHandlerState)
1254 OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1255 OICFree(defragData);
1256 CAAdapterFreeEndpoint(remoteEndpoint);
1257 ca_mutex_unlock(g_bleClientReceiveDataMutex);
1261 ca_mutex_unlock(g_bleClientReceiveDataMutex);
1262 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1265 void CABLEServerSendDataThread(void *threadData)
1267 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1269 CALEData_t *bleData = (CALEData_t *) threadData;
1272 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1276 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1277 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1279 int32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1281 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server total Data length with header is [%d]", totalLength);
1282 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
1283 if (NULL == dataSegment)
1285 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1290 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1291 if (CA_STATUS_OK != result )
1293 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1295 OICFree(dataSegment);
1299 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1303 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1305 length = totalLength;
1306 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1310 length = CA_SUPPORTED_BLE_MTU_SIZE;
1311 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1312 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1315 int32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1317 // Send the first segment with the header.
1318 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1320 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
1321 result = CAUpdateCharacteristicsToGattClient(
1322 bleData->remoteEndpoint->addr, dataSegment, length);
1323 if (CA_STATUS_OK != result)
1325 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1326 OICFree(dataSegment);
1330 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1331 for (index = 1; index < iter; index++)
1333 // Send the remaining header.
1334 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1335 result = CAUpdateCharacteristicsToGattClient(
1336 bleData->remoteEndpoint->addr,
1337 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1338 CA_SUPPORTED_BLE_MTU_SIZE);
1339 if (CA_STATUS_OK != result)
1341 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1342 "Update characteristics failed, result [%d]", result);
1343 OICFree(dataSegment);
1346 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
1347 CA_SUPPORTED_BLE_MTU_SIZE);
1350 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1351 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1353 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1354 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1355 result = CAUpdateCharacteristicsToGattClient(
1356 bleData->remoteEndpoint->addr,
1357 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1359 if (CA_STATUS_OK != result)
1361 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1363 OICFree(dataSegment);
1366 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1371 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
1372 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
1373 if (CA_STATUS_OK != result)
1375 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1376 OICFree(dataSegment);
1379 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1380 for (index = 1; index < iter; index++)
1382 // Send the remaining header.
1383 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1384 result = CAUpdateCharacteristicsToAllGattClients(
1385 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1386 CA_SUPPORTED_BLE_MTU_SIZE);
1387 if (CA_STATUS_OK != result)
1389 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1390 OICFree(dataSegment);
1393 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
1396 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1397 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1399 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1400 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1401 result = CAUpdateCharacteristicsToAllGattClients(
1402 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1404 if (CA_STATUS_OK != result)
1406 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1408 OICFree(dataSegment);
1411 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1414 OICFree(dataSegment);
1416 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1419 void CABLEClientSendDataThread(void *threadData)
1421 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1423 CALEData_t *bleData = (CALEData_t *) threadData;
1426 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1430 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1431 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1433 uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1434 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
1435 if (NULL == dataSegment)
1437 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1442 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1443 if (CA_STATUS_OK != result )
1445 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1447 OICFree(dataSegment);
1450 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1453 uint32_t length = 0;
1454 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1456 length = totalLength;
1457 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1458 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1462 length = CA_SUPPORTED_BLE_MTU_SIZE;
1463 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1464 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1465 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1468 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1470 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1472 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1473 // Send the first segment with the header.
1474 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addr,
1479 if (CA_STATUS_OK != result)
1481 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1482 OICFree(dataSegment);
1486 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1487 for (index = 1; index < iter; index++)
1489 // Send the remaining header.
1490 result = CAUpdateCharacteristicsToGattServer(
1491 bleData->remoteEndpoint->addr,
1492 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1493 CA_SUPPORTED_BLE_MTU_SIZE,
1495 if (CA_STATUS_OK != result)
1497 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1499 OICFree(dataSegment);
1502 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1503 CA_SUPPORTED_BLE_MTU_SIZE);
1506 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1507 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1509 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1510 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1511 result = CAUpdateCharacteristicsToGattServer(
1512 bleData->remoteEndpoint->addr,
1513 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1517 if (CA_STATUS_OK != result)
1519 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1521 OICFree(dataSegment);
1524 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1529 //Sending Mulitcast Data
1530 // Send the first segment with the header.
1531 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1532 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1533 if (CA_STATUS_OK != result)
1535 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed (all), result [%d]", result);
1536 OICFree(dataSegment);
1539 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1540 // Send the remaining header.
1541 for (index = 1; index < iter; index++)
1543 result = CAUpdateCharacteristicsToAllGattServers(
1544 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1545 CA_SUPPORTED_BLE_MTU_SIZE);
1546 if (CA_STATUS_OK != result)
1548 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]", result);
1549 OICFree(dataSegment);
1552 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
1555 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1556 if ( remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1558 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1559 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1560 result = CAUpdateCharacteristicsToAllGattServers(
1561 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1563 if (CA_STATUS_OK != result)
1565 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]", result);
1566 OICFree(dataSegment);
1569 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1574 OICFree(dataSegment);
1576 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1579 CALEData_t *CACreateBLEData(const CAEndpoint_t *remoteEndpoint, const void *data,
1580 uint32_t dataLength)
1582 CALEData_t *bleData = (CALEData_t *) OICMalloc(sizeof(CALEData_t));
1585 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1589 bleData->remoteEndpoint = CAAdapterCloneEndpoint(remoteEndpoint);
1590 bleData->data = (void *)OICCalloc(dataLength + 1, 1);
1591 if (NULL == bleData->data)
1593 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1594 CAFreeBLEData(bleData);
1597 memcpy(bleData->data, data, dataLength);
1598 bleData->dataLen = dataLength;
1603 void CAFreeBLEData(CALEData_t *bleData)
1605 VERIFY_NON_NULL_VOID(bleData, NULL, "Param bleData is NULL");
1607 CAAdapterFreeEndpoint(bleData->remoteEndpoint);
1608 OICFree(bleData->data);
1612 void CALEDataDestroyer(void *data, uint32_t size)
1614 CALEData_t *ledata = (CALEData_t *) data;
1616 CAFreeBLEData(ledata);
1620 CAResult_t CABLEClientSendData(const CAEndpoint_t *remoteEndpoint,
1624 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1626 VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1628 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
1629 "g_bleClientSendQueueHandle is NULL",
1631 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
1632 "g_bleClientSendDataMutex is NULL",
1635 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
1638 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1640 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1643 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1644 return CA_MEMORY_ALLOC_FAILED;
1646 // Add message to send queue
1647 ca_mutex_lock(g_bleClientSendDataMutex);
1648 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
1649 ca_mutex_unlock(g_bleClientSendDataMutex);
1651 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1652 return CA_STATUS_OK;
1656 CAResult_t CABLEServerSendData(const CAEndpoint_t *remoteEndpoint,
1660 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1662 VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1664 VERIFY_NON_NULL_RET(g_sendQueueHandle, CALEADAPTER_TAG,
1665 "BleClientReceiverQueue is NULL",
1667 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
1668 "BleClientSendDataMutex is NULL",
1671 VERIFY_NON_NULL_RET(g_sendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
1674 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1676 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1679 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1680 return CA_MEMORY_ALLOC_FAILED;
1682 // Add message to send queue
1683 ca_mutex_lock(g_bleServerSendDataMutex);
1684 CAQueueingThreadAddData(g_sendQueueHandle, bleData, sizeof(CALEData_t));
1685 ca_mutex_unlock(g_bleServerSendDataMutex);
1687 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1688 return CA_STATUS_OK;
1691 CAResult_t CABLEServerReceivedData(const char *remoteAddress, const char *serviceUUID,
1692 const void *data, uint32_t dataLength, uint32_t *sentLength)
1694 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1697 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1698 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1699 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1700 VERIFY_NON_NULL_RET(g_bleServerReceiverQueue, CALEADAPTER_TAG, "g_bleServerReceiverQueue",
1703 //Add message to data queue
1704 CAEndpoint_t *remoteEndpoint = CAAdapterCreateEndpoint(0, CA_ADAPTER_GATT_BTLE, remoteAddress, 0);
1705 if (NULL == remoteEndpoint)
1707 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1708 return CA_STATUS_FAILED;
1711 // Create bleData to add to queue
1712 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1714 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1717 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1718 CAAdapterFreeEndpoint(remoteEndpoint);
1719 return CA_MEMORY_ALLOC_FAILED;
1722 CAAdapterFreeEndpoint(remoteEndpoint);
1723 // Add message to send queue
1724 CAQueueingThreadAddData(g_bleServerReceiverQueue, bleData, sizeof(CALEData_t));
1726 *sentLength = dataLength;
1728 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1729 return CA_STATUS_OK;
1732 CAResult_t CABLEClientReceivedData(const char *remoteAddress, const char *serviceUUID,
1733 const void *data, uint32_t dataLength, uint32_t *sentLength)
1735 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1738 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1739 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1740 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1741 VERIFY_NON_NULL_RET(g_bleClientReceiverQueue, CALEADAPTER_TAG, "g_bleClientReceiverQueue",
1744 //Add message to data queue
1745 CAEndpoint_t *remoteEndpoint = CAAdapterCreateEndpoint(0, CA_ADAPTER_GATT_BTLE, remoteAddress, 0);
1746 if (NULL == remoteEndpoint)
1748 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1749 return CA_STATUS_FAILED;
1752 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1754 // Create bleData to add to queue
1755 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1758 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1759 CAAdapterFreeEndpoint(remoteEndpoint);
1760 return CA_MEMORY_ALLOC_FAILED;
1763 CAAdapterFreeEndpoint(remoteEndpoint);
1764 // Add message to send queue
1765 CAQueueingThreadAddData(g_bleClientReceiverQueue, bleData, sizeof(CALEData_t));
1767 *sentLength = dataLength;
1769 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1770 return CA_STATUS_OK;
1773 void CASetBleAdapterThreadPoolHandle(ca_thread_pool_t handle)
1775 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1777 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1778 g_bleAdapterThreadPool = handle;
1779 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1781 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1784 void CASetBLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
1786 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1788 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1790 g_networkPacketReceivedCallback = callback;
1792 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1794 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");