1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
20 #include "caleadapter.h"
25 #include "caleinterface.h"
28 #include "caadapterutils.h"
29 #include "caqueueingthread.h"
30 #include "camsgparser.h"
31 #include "oic_malloc.h"
34 * @var CALEADAPTER_TAG
35 * @brief Logging tag for module name.
37 #define CALEADAPTER_TAG "CA_BLE_ADAPTER"
40 * @var g_networkCallback
41 * @brief Callback to provide the status of the network change to CA layer.
43 static CANetworkChangeCallback g_networkCallback = NULL;
46 * @var g_localBLEAddress
47 * @brief bleAddress of the local adapter. Value will be initialized to zero, and will be updated later.
49 static char g_localBLEAddress[16] = {0};
53 * @brief Variable to differentiate btw GattServer and GattClient.
55 static bool g_isServer = false;
58 * @var g_bleIsServerMutex
59 * @brief Mutex to synchronize the task to be executed on the GattServer function calls.
61 static ca_mutex g_bleIsServerMutex = NULL;
64 * @var g_bleNetworkCbMutex
65 * @brief Mutex to synchronize the callback to be called for the network changes.
67 static ca_mutex g_bleNetworkCbMutex = NULL;
70 * @var g_bleLocalAddressMutex
71 * @brief Mutex to synchronize the updation of the local LE address of the adapter.
73 static ca_mutex g_bleLocalAddressMutex = NULL;
76 * @var g_bleAdapterThreadPool
77 * @brief reference to threadpool
79 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
82 * @var g_bleAdapterThreadPoolMutex
83 * @brief Mutex to synchronize the task to be pushed to thread pool.
85 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
88 * @var g_bleClientSendQueueHandle
89 * @brief Queue to process the outgoing packets from GATTClient.
91 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
94 * @var g_bleClientReceiverQueue
95 * @brief Queue to process the incoming packets to GATT Client.
97 static CAQueueingThread_t *g_bleClientReceiverQueue = NULL;
100 * @var g_bleClientSendDataMutex
101 * @brief Mutex to synchronize the queing of the data from SenderQueue.
103 static ca_mutex g_bleClientSendDataMutex = NULL;
106 * @var g_bleClientReceiveDataMutex
107 * @brief Mutex to synchronize the queing of the data from ReceiverQueue.
109 static ca_mutex g_bleClientReceiveDataMutex = NULL;
112 * @var g_dataReceiverHandlerState
113 * @brief Stop condition of recvhandler.
115 static bool g_dataReceiverHandlerState = false;
118 * @var g_sendQueueHandle
119 * @brief Queue to process the outgoing packets from GATTServer.
121 static CAQueueingThread_t *g_sendQueueHandle = NULL;
124 * @var g_bleServerReceiverQueue
125 * @brief Queue to process the incoming packets to GATTServer
127 static CAQueueingThread_t *g_bleServerReceiverQueue = NULL;
130 * @var g_bleServerSendDataMutex
131 * @brief Mutex to synchronize the queing of the data from SenderQueue.
133 static ca_mutex g_bleServerSendDataMutex = NULL;
136 * @var g_bleServerReceiveDataMutex
137 * @brief Mutex to synchronize the queing of the data from ReceiverQueue.
139 static ca_mutex g_bleServerReceiveDataMutex = NULL;
142 * @var g_bleAdapterReqRespCbMutex
143 * @brief Mutex to synchronize the callback to be called for the adapterReqResponse.
145 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
148 * @var g_networkPacketReceivedCallback
149 * @brief Callback to be called when network packet recieved from either GattServer or GattClient.
151 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
154 * @ENUM CALeServerStatus
155 * @brief status of BLE Server Status
156 * This ENUM provides information of LE Adapter Server status
160 CA_SERVER_NOTSTARTED = 0,
166 * @var gLeServerStatus
167 * @brief structure to maintain the status of the server.
169 static CALeServerStatus gLeServerStatus = CA_SERVER_NOTSTARTED;
172 * @fn CALERegisterNetworkNotifications
173 * @brief This function is used to register network change notification callback.
175 * @param[in] netCallback CANetworkChangeCallback callback which will be set for the change in nwk.
177 * @return 0 on success otherwise a positive error value.
178 * @retval CA_STATUS_OK Successful
179 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
180 * @retval CA_STATUS_FAILED Operation failed
183 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
186 * @fn CASetBleAdapterThreadPoolHandle
187 * @brief Used to Set the gThreadPool handle which is required for spawning new thread.
189 * @param[in] handle - Thread pool handle which is given by above layer for using thread creation task.
194 void CASetBleAdapterThreadPoolHandle(ca_thread_pool_t handle);
197 * @fn CALEDeviceStateChangedCb
198 * @brief This function is used to call the callback to the upper layer when the device state gets
201 * @param[in] adapter_state New state of the adapter to be notified to the upper layer.
206 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state);
209 * @fn CAInitBleAdapterMutex
210 * @brief Used to initialize all required mutex variable for LE Adapter implementation.
212 * @return 0 on success otherwise a positive error value.
213 * @retval CA_STATUS_OK Successful
214 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
215 * @retval CA_STATUS_FAILED Operation failed
218 CAResult_t CAInitBleAdapterMutex();
221 * @fn CATerminateBleAdapterMutex
222 * @brief Used to terminate all required mutex variable for LE adapter implementation.
226 void CATerminateBleAdapterMutex();
229 * @fn CALEDataDestroyer
230 * @brief Used to free data
234 static void CALEDataDestroyer(void *data, uint32_t size);
236 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
237 CANetworkPacketReceivedCallback reqRespCallback,
238 CANetworkChangeCallback netCallback,
239 ca_thread_pool_t handle)
241 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
244 VERIFY_NON_NULL(registerCallback, NULL, "RegisterConnectivity callback is null");
245 VERIFY_NON_NULL(reqRespCallback, NULL, "PacketReceived Callback is null");
246 VERIFY_NON_NULL(netCallback, NULL, "NetworkChange Callback is null");
248 CAResult_t result = CAInitBleAdapterMutex();
249 if (CA_STATUS_OK != result)
251 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
252 return CA_STATUS_FAILED;
255 result = CAInitializeLENetworkMonitor();
256 if (CA_STATUS_OK != result)
258 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
259 return CA_STATUS_FAILED;
262 CAInitializeLEAdapter();
264 CASetBleClientThreadPoolHandle(handle);
265 CASetBleServerThreadPoolHandle(handle);
266 CASetBleAdapterThreadPoolHandle(handle);
267 CASetBLEReqRespServerCallback(CABLEServerReceivedData);
268 CASetBLEReqRespClientCallback(CABLEClientReceivedData);
269 CASetBLEReqRespAdapterCallback(reqRespCallback);
271 CALERegisterNetworkNotifications(netCallback);
273 CAConnectivityHandler_t connHandler;
274 connHandler.startAdapter = CAStartLE;
275 connHandler.stopAdapter = CAStopLE;
276 connHandler.startListenServer = CAStartLEListeningServer;
277 connHandler.startDiscoveryServer = CAStartLEDiscoveryServer;
278 connHandler.sendData = CASendLEUnicastData;
279 connHandler.sendDataToAll = CASendLEMulticastData;
280 connHandler.GetnetInfo = CAGetLEInterfaceInformation;
281 connHandler.readData = CAReadLEData;
282 connHandler.terminate = CATerminateLE;
283 registerCallback(connHandler, CA_LE);
285 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
290 CAResult_t CAStartLE()
292 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
293 OIC_LOG(DEBUG, CALEADAPTER_TAG,
294 "There is no concept of unicast/multicast in LE. So This function is not implemented");
295 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
299 CAResult_t CAStopLE()
301 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
305 ca_mutex_lock(g_bleIsServerMutex);
306 if (true == g_isServer)
308 CAStopBleGattServer();
312 CAStopBLEGattClient();
314 ca_mutex_unlock(g_bleIsServerMutex);
316 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
323 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
325 CASetBLEReqRespServerCallback(NULL);
326 CASetBLEReqRespClientCallback(NULL);
327 CALERegisterNetworkNotifications(NULL);
328 CASetBLEReqRespAdapterCallback(NULL);
329 CATerminateLENetworkMonitor();
331 ca_mutex_lock(g_bleIsServerMutex);
332 if (true == g_isServer)
334 CATerminateBleGattServer();
338 CATerminateBLEGattClient();
340 ca_mutex_unlock(g_bleIsServerMutex);
342 CATerminateBleQueues();
344 CATerminateBleAdapterMutex();
346 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
349 CAResult_t CAStartLEListeningServer()
351 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
353 CAResult_t result = CAInitBleServerQueues();
354 if (CA_STATUS_OK != result)
356 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
357 return CA_STATUS_FAILED;
360 result = CAGetLEAdapterState();
361 if (CA_ADAPTER_NOT_ENABLED == result)
363 gLeServerStatus = CA_LISTENING_SERVER;
364 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
368 if (CA_STATUS_FAILED == result)
370 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
371 return CA_STATUS_FAILED;
374 CAStartBleGattServer();
376 ca_mutex_lock(g_bleIsServerMutex);
378 ca_mutex_unlock(g_bleIsServerMutex);
380 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
384 CAResult_t CAStartLEDiscoveryServer()
386 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
388 CAResult_t result = CAInitBleClientQueues();
389 if (CA_STATUS_OK != result)
391 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
392 return CA_STATUS_FAILED;
395 result = CAGetLEAdapterState();
396 if (CA_ADAPTER_NOT_ENABLED == result)
398 gLeServerStatus = CA_DISCOVERY_SERVER;
399 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
403 if (CA_STATUS_FAILED == result)
405 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
406 return CA_STATUS_FAILED;
409 CAStartBLEGattClient();
411 ca_mutex_lock(g_bleIsServerMutex);
413 ca_mutex_unlock(g_bleIsServerMutex);
415 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
419 CAResult_t CAStartLENotifyServer()
421 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
423 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
427 uint32_t CASendLENotification(const CARemoteEndpoint_t *endpoint, const void *data, uint32_t dataLen)
429 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
431 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
435 CAResult_t CAReadLEData()
437 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
439 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
443 int32_t CASendLEUnicastData(const CARemoteEndpoint_t *endpoint, const void *data, uint32_t dataLen)
445 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
448 VERIFY_NON_NULL_RET(endpoint, NULL, "Remote endpoint is null", -1);
449 VERIFY_NON_NULL_RET(data, NULL, "Data is null", -1);
451 CAResult_t result = CA_STATUS_FAILED;
453 ca_mutex_lock(g_bleIsServerMutex);
454 if (true == g_isServer)
456 result = CABLEServerSendData(endpoint, data, dataLen);
457 if (CA_STATUS_OK != result)
459 OIC_LOG(ERROR, CALEADAPTER_TAG,
460 "[SendLEUnicastData] CABleServerSenderQueueEnqueueMessage failed \n");
461 ca_mutex_unlock(g_bleIsServerMutex);
467 result = CABLEClientSendData(endpoint, data, dataLen);
468 if (CA_STATUS_OK != result)
470 OIC_LOG(ERROR, CALEADAPTER_TAG,
471 "[SendLEUnicastData] CABleClientSenderQueueEnqueueMessage failed \n");
472 ca_mutex_unlock(g_bleIsServerMutex);
476 ca_mutex_unlock(g_bleIsServerMutex);
478 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
482 int32_t CASendLEMulticastData(const void *data, uint32_t dataLen)
484 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
487 VERIFY_NON_NULL_RET(data, NULL, "Data is null", -1);
491 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
495 CAResult_t result = CA_STATUS_FAILED;
497 ca_mutex_lock(g_bleIsServerMutex);
498 if (true == g_isServer)
500 result = CABLEServerSendData(NULL, data, dataLen);
501 if (CA_STATUS_OK != result)
503 OIC_LOG(ERROR, CALEADAPTER_TAG,
504 "[SendLEMulticastDataToAll] CABleServerSenderQueueEnqueueMessage failed" );
505 ca_mutex_unlock(g_bleIsServerMutex);
511 result = CABLEClientSendData(NULL, data, dataLen);
512 if (CA_STATUS_OK != result)
514 OIC_LOG(ERROR, CALEADAPTER_TAG,
515 "[SendLEMulticastDataToAll] CABleClientSenderQueueEnqueueMessage failed" );
516 ca_mutex_unlock(g_bleIsServerMutex);
520 ca_mutex_unlock(g_bleIsServerMutex);
522 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
526 CAResult_t CAGetLEInterfaceInformation(CALocalConnectivity_t **info, uint32_t *size)
528 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
530 VERIFY_NON_NULL(info, NULL, "CALocalConnectivity info is null");
532 char *local_address = NULL;
534 CAResult_t res = CAGetLEAddress(&local_address);
535 if (CA_STATUS_OK != res)
537 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
541 if (NULL == local_address)
543 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
544 return CA_STATUS_FAILED;
548 (*info) = (CALocalConnectivity_t *) OICCalloc(1, sizeof(CALocalConnectivity_t));
551 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
552 OICFree(local_address);
553 return CA_STATUS_FAILED;
556 strncpy((*info)->addressInfo.BT.btMacAddress, local_address, strlen(local_address));
557 ca_mutex_lock(g_bleLocalAddressMutex);
558 strncpy(g_localBLEAddress, local_address, sizeof(g_localBLEAddress));
559 ca_mutex_unlock(g_bleLocalAddressMutex);
561 (*info)->type = CA_LE;
563 OICFree(local_address);
565 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
569 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
571 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
573 ca_mutex_lock(g_bleNetworkCbMutex);
574 g_networkCallback = netCallback;
575 ca_mutex_unlock(g_bleNetworkCbMutex);
576 CAResult_t res = CA_STATUS_OK;
579 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
580 if (CA_STATUS_OK != res)
582 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
587 res = CAUnSetLEAdapterStateChangedCb();
588 if (CA_STATUS_OK != res)
590 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
594 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
598 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
600 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
602 VERIFY_NON_NULL_VOID(g_localBLEAddress, NULL, "g_localBLEAddress is null");
603 CALocalConnectivity_t localEndpoint = {};
605 ca_mutex_lock(g_bleLocalAddressMutex);
606 strncpy(localEndpoint.addressInfo.BT.btMacAddress, g_localBLEAddress, strlen(g_localBLEAddress));
607 ca_mutex_unlock(g_bleLocalAddressMutex);
609 // Start a GattServer/Client if gLeServerStatus is SET
610 if (CA_LISTENING_SERVER == gLeServerStatus)
612 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattServer");
613 CAStartBleGattServer();
615 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
617 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
618 CAStartBLEGattClient();
620 gLeServerStatus = CA_SERVER_NOTSTARTED;
622 ca_mutex_lock(g_bleNetworkCbMutex);
623 if (NULL != g_networkCallback)
625 g_networkCallback(&localEndpoint, adapter_state);
629 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
631 ca_mutex_unlock(g_bleNetworkCbMutex);
633 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
636 CAResult_t CAInitBleAdapterMutex()
638 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
640 if (NULL == g_bleIsServerMutex)
642 g_bleIsServerMutex = ca_mutex_new();
643 if (NULL == g_bleIsServerMutex)
645 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
646 return CA_STATUS_FAILED;
650 if (NULL == g_bleNetworkCbMutex)
652 g_bleNetworkCbMutex = ca_mutex_new();
653 if (NULL == g_bleNetworkCbMutex)
655 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
656 return CA_STATUS_FAILED;
660 if (NULL == g_bleLocalAddressMutex)
662 g_bleLocalAddressMutex = ca_mutex_new();
663 if (NULL == g_bleLocalAddressMutex)
665 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
666 return CA_STATUS_FAILED;
670 if (NULL == g_bleAdapterThreadPoolMutex)
672 g_bleAdapterThreadPoolMutex = ca_mutex_new();
673 if (NULL == g_bleAdapterThreadPoolMutex)
675 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
676 return CA_STATUS_FAILED;
680 if (NULL == g_bleClientSendDataMutex)
682 g_bleClientSendDataMutex = ca_mutex_new();
683 if (NULL == g_bleClientSendDataMutex)
685 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
686 return CA_STATUS_FAILED;
690 if (NULL == g_bleClientReceiveDataMutex)
692 g_bleClientReceiveDataMutex = ca_mutex_new();
693 if (NULL == g_bleClientReceiveDataMutex)
695 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
696 return CA_STATUS_FAILED;
700 if (NULL == g_bleServerSendDataMutex)
702 g_bleServerSendDataMutex = ca_mutex_new();
703 if (NULL == g_bleServerSendDataMutex)
705 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
706 return CA_STATUS_FAILED;
710 if (NULL == g_bleServerReceiveDataMutex)
712 g_bleServerReceiveDataMutex = ca_mutex_new();
713 if (NULL == g_bleServerReceiveDataMutex)
715 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
716 return CA_STATUS_FAILED;
720 if (NULL == g_bleAdapterReqRespCbMutex)
722 g_bleAdapterReqRespCbMutex = ca_mutex_new();
723 if (NULL == g_bleAdapterReqRespCbMutex)
725 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
726 return CA_STATUS_FAILED;
730 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
734 void CATerminateBleAdapterMutex()
736 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
738 ca_mutex_free(g_bleIsServerMutex);
739 g_bleIsServerMutex = NULL;
741 ca_mutex_free(g_bleNetworkCbMutex);
742 g_bleNetworkCbMutex = NULL;
744 ca_mutex_free(g_bleLocalAddressMutex);
745 g_bleLocalAddressMutex = NULL;
747 ca_mutex_free(g_bleAdapterThreadPoolMutex);
748 g_bleAdapterThreadPoolMutex = NULL;
750 ca_mutex_free(g_bleClientSendDataMutex);
751 g_bleClientSendDataMutex = NULL;
753 ca_mutex_free(g_bleClientReceiveDataMutex);
754 g_bleClientReceiveDataMutex = NULL;
756 ca_mutex_free(g_bleServerSendDataMutex);
757 g_bleServerSendDataMutex = NULL;
759 ca_mutex_free(g_bleServerReceiveDataMutex);
760 g_bleServerReceiveDataMutex = NULL;
762 ca_mutex_free(g_bleAdapterReqRespCbMutex);
763 g_bleAdapterReqRespCbMutex = NULL;
765 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
768 void CAInitBleQueues()
770 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
772 CAResult_t result = CAInitBleServerQueues();
773 if (CA_STATUS_OK != result)
775 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerQueues failed");
779 result = CAInitBleClientQueues();
780 if (CA_STATUS_OK != result)
782 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
786 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
789 CAResult_t CAInitBleServerQueues()
791 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
793 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
795 CAResult_t result = CAInitBleServerSenderQueue();
796 if (CA_STATUS_OK != result)
798 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
799 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
800 return CA_STATUS_FAILED;
803 result = CAInitBleServerReceiverQueue();
804 if (CA_STATUS_OK != result)
806 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerReceiverQueue failed");
807 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
808 return CA_STATUS_FAILED;
811 g_dataReceiverHandlerState = true;
813 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
815 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
819 CAResult_t CAInitBleClientQueues()
821 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
823 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
825 CAResult_t result = CAInitBleClientSenderQueue();
826 if (CA_STATUS_OK != result)
828 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
829 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
830 return CA_STATUS_FAILED;
833 result = CAInitBleClientReceiverQueue();
834 if (CA_STATUS_OK != result)
836 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientReceiverQueue failed");
837 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
838 return CA_STATUS_FAILED;
841 g_dataReceiverHandlerState = true;
843 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
845 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
849 CAResult_t CAInitBleServerSenderQueue()
851 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
852 // Check if the message queue is already initialized
853 if (g_sendQueueHandle)
855 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
859 // Create send message queue
860 g_sendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
861 if (!g_sendQueueHandle)
863 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
864 return CA_MEMORY_ALLOC_FAILED;
867 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_sendQueueHandle, g_bleAdapterThreadPool,
868 CABLEServerSendDataThread, CALEDataDestroyer))
870 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
871 OICFree(g_sendQueueHandle);
872 g_sendQueueHandle = NULL;
873 return CA_STATUS_FAILED;
876 if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle))
878 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
879 OICFree(g_sendQueueHandle);
880 g_sendQueueHandle = NULL;
881 return CA_STATUS_FAILED;
884 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
888 CAResult_t CAInitBleClientSenderQueue()
890 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
892 if (g_bleClientSendQueueHandle)
894 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
898 // Create send message queue
899 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
900 if (!g_bleClientSendQueueHandle)
902 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
903 return CA_MEMORY_ALLOC_FAILED;
906 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle, g_bleAdapterThreadPool,
907 CABLEClientSendDataThread, CALEDataDestroyer))
909 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
910 OICFree(g_bleClientSendQueueHandle);
911 g_bleClientSendQueueHandle = NULL;
912 return CA_STATUS_FAILED;
915 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientSendQueueHandle))
917 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
918 OICFree(g_bleClientSendQueueHandle);
919 g_bleClientSendQueueHandle = NULL;
920 return CA_STATUS_FAILED;
923 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
927 CAResult_t CAInitBleServerReceiverQueue()
929 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
930 // Check if the message queue is already initialized
931 if (g_bleServerReceiverQueue)
933 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
937 // Create send message queue
938 g_bleServerReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
939 if (!g_bleServerReceiverQueue)
941 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
942 OICFree(g_sendQueueHandle);
943 return CA_MEMORY_ALLOC_FAILED;
946 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerReceiverQueue, g_bleAdapterThreadPool,
947 CABLEServerDataReceiverHandler, CALEDataDestroyer))
949 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
950 OICFree(g_bleServerReceiverQueue);
951 g_bleServerReceiverQueue = NULL;
952 return CA_STATUS_FAILED;
955 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerReceiverQueue))
957 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
958 OICFree(g_bleServerReceiverQueue);
959 g_bleServerReceiverQueue = NULL;
960 return CA_STATUS_FAILED;
963 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
967 CAResult_t CAInitBleClientReceiverQueue()
969 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
971 // Check if the message queue is already initialized
972 if (g_bleClientReceiverQueue)
974 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
978 // Create send message queue
979 g_bleClientReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
980 if (!g_bleClientReceiverQueue)
982 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
983 OICFree(g_bleClientSendQueueHandle);
984 return CA_MEMORY_ALLOC_FAILED;
987 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientReceiverQueue, g_bleAdapterThreadPool,
988 CABLEClientDataReceiverHandler, NULL))
990 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
991 OICFree(g_bleClientSendQueueHandle);
992 OICFree(g_bleClientReceiverQueue);
993 g_bleClientReceiverQueue = NULL;
994 return CA_STATUS_FAILED;
997 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientReceiverQueue))
999 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
1000 OICFree(g_bleClientReceiverQueue);
1001 g_bleClientReceiverQueue = NULL;
1002 return CA_STATUS_FAILED;
1005 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1006 return CA_STATUS_OK;
1009 void CAStopBleQueues()
1011 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1013 ca_mutex_lock(g_bleClientSendDataMutex);
1014 if (NULL != g_bleClientSendQueueHandle)
1016 CAQueueingThreadStop(g_bleClientSendQueueHandle);
1018 ca_mutex_unlock(g_bleClientSendDataMutex);
1020 ca_mutex_lock(g_bleClientReceiveDataMutex);
1021 if (NULL != g_bleClientReceiverQueue)
1023 CAQueueingThreadStop(g_bleClientReceiverQueue);
1025 ca_mutex_unlock(g_bleClientReceiveDataMutex);
1027 ca_mutex_lock(g_bleServerSendDataMutex);
1028 if (NULL != g_sendQueueHandle)
1030 CAQueueingThreadStop(g_sendQueueHandle);
1032 ca_mutex_unlock(g_bleServerSendDataMutex);
1034 ca_mutex_lock(g_bleServerReceiveDataMutex);
1035 if (NULL != g_bleServerReceiverQueue)
1037 CAQueueingThreadStop(g_bleServerReceiverQueue);
1039 ca_mutex_unlock(g_bleServerReceiveDataMutex);
1041 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1044 void CATerminateBleQueues()
1046 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1048 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
1049 OICFree(g_bleClientSendQueueHandle);
1050 g_bleClientSendQueueHandle = NULL;
1053 CAQueueingThreadDestroy(g_bleClientReceiverQueue);
1054 OICFree(g_bleClientReceiverQueue);
1055 g_bleClientReceiverQueue = NULL;
1058 CAQueueingThreadDestroy(g_sendQueueHandle);
1059 OICFree(g_sendQueueHandle);
1060 g_sendQueueHandle = NULL;
1063 CAQueueingThreadDestroy(g_bleServerReceiverQueue);
1064 OICFree(g_bleServerReceiverQueue);
1065 g_bleServerReceiverQueue = NULL;
1067 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1069 void CABLEServerDataReceiverHandler(void *threadData)
1071 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1073 static uint32_t recvDataLen = 0;
1074 static uint32_t totalDataLen = 0;
1075 static char *defragData = NULL;
1076 static bool isHeaderAvailable = false;
1077 static CARemoteEndpoint_t *remoteEndpoint = NULL;
1079 ca_mutex_lock(g_bleServerReceiveDataMutex);
1081 if (g_dataReceiverHandlerState)
1083 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1085 CALEData_t *bleData = (CALEData_t *) threadData;
1088 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
1092 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1094 if (!isHeaderAvailable)
1096 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1097 totalDataLen = CAParseHeader((char*)bleData->data);
1099 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes", totalDataLen);
1100 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%d] bytes", bleData->dataLen);
1102 defragData = (char *) OICCalloc(totalDataLen + 1, sizeof(char));
1103 if (NULL == defragData)
1105 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1109 const char *remoteAddress = bleData->remoteEndpoint->addressInfo.LE.leMacAddress;
1110 const char *serviceUUID = bleData->remoteEndpoint->resourceUri;
1112 remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1115 memcpy(defragData + recvDataLen, bleData->data + CA_HEADER_LENGTH,
1116 bleData->dataLen - CA_HEADER_LENGTH);
1117 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1118 isHeaderAvailable = true;
1122 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1123 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1124 recvDataLen += bleData->dataLen ;
1125 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1126 totalDataLen, recvDataLen);
1128 if (totalDataLen == recvDataLen)
1130 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1131 if (NULL == g_networkPacketReceivedCallback)
1133 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1134 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1137 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1138 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1141 isHeaderAvailable = false;
1142 remoteEndpoint = NULL;
1144 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1147 if (false == g_dataReceiverHandlerState)
1149 OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1152 isHeaderAvailable = false;
1153 OICFree(defragData);
1154 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1155 u_mutex_unlock(g_bleServerReceiveDataMutex);
1159 u_mutex_unlock(g_bleServerReceiveDataMutex);
1160 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1163 void CABLEClientDataReceiverHandler(void *threadData)
1165 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1167 static const char *remoteAddress = NULL;
1168 static const char *serviceUUID = NULL;
1169 static uint32_t recvDataLen = 0;
1170 static uint32_t totalDataLen = 0;
1171 static char *defragData = NULL;
1172 static bool isHeaderAvailable = false;
1173 static CARemoteEndpoint_t *remoteEndpoint = NULL;
1175 ca_mutex_lock(g_bleClientReceiveDataMutex);
1177 if (g_dataReceiverHandlerState)
1179 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1181 CALEData_t *bleData = (CALEData_t *) threadData;
1184 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid wifidata!");
1188 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1190 if (!isHeaderAvailable)
1192 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1194 totalDataLen = CAParseHeader(bleData->data);
1195 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes",
1197 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received in the first packet [%d] bytes",
1200 defragData = (char *) OICMalloc(sizeof(char) * totalDataLen);
1201 if (NULL == defragData)
1203 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1207 remoteAddress = bleData->remoteEndpoint->addressInfo.LE.leMacAddress;
1208 serviceUUID = bleData->remoteEndpoint->resourceUri;
1210 remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1213 memcpy(defragData, bleData->data + CA_HEADER_LENGTH,
1214 bleData->dataLen - CA_HEADER_LENGTH);
1215 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1216 isHeaderAvailable = true;
1220 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1221 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1222 recvDataLen += bleData->dataLen ;
1223 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1224 totalDataLen, recvDataLen);
1226 if (totalDataLen == recvDataLen)
1228 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1229 if (NULL == g_networkPacketReceivedCallback)
1231 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1232 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1235 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1236 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1239 isHeaderAvailable = false;
1240 remoteEndpoint = NULL;
1242 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1245 if (false == g_dataReceiverHandlerState)
1247 OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1248 OICFree(defragData);
1249 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1250 ca_mutex_unlock(g_bleClientReceiveDataMutex);
1254 ca_mutex_unlock(g_bleClientReceiveDataMutex);
1255 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1258 void CABLEServerSendDataThread(void *threadData)
1260 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1262 CALEData_t *bleData = (CALEData_t *) threadData;
1265 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1269 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1270 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1272 int32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1274 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server total Data length with header is [%d]", totalLength);
1275 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
1276 if (NULL == dataSegment)
1278 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1283 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1284 if (CA_STATUS_OK != result )
1286 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1288 OICFree(dataSegment);
1292 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1296 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1298 length = totalLength;
1299 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1303 length = CA_SUPPORTED_BLE_MTU_SIZE;
1304 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1305 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1308 int32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1310 // Send the first segment with the header.
1311 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1313 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
1314 result = CAUpdateCharacteristicsToGattClient(
1315 bleData->remoteEndpoint->addressInfo.LE.leMacAddress, dataSegment, length);
1316 if (CA_STATUS_OK != result)
1318 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1319 OICFree(dataSegment);
1323 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1324 for (index = 1; index < iter; index++)
1326 // Send the remaining header.
1327 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1328 result = CAUpdateCharacteristicsToGattClient(
1329 bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1330 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1331 CA_SUPPORTED_BLE_MTU_SIZE);
1332 if (CA_STATUS_OK != result)
1334 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1335 "Update characteristics failed, result [%d]", result);
1336 OICFree(dataSegment);
1339 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
1340 CA_SUPPORTED_BLE_MTU_SIZE);
1343 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1344 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1346 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1347 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1348 result = CAUpdateCharacteristicsToGattClient(
1349 bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1350 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1352 if (CA_STATUS_OK != result)
1354 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1356 OICFree(dataSegment);
1359 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1364 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
1365 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
1366 if (CA_STATUS_OK != result)
1368 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1369 OICFree(dataSegment);
1372 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1373 for (index = 1; index < iter; index++)
1375 // Send the remaining header.
1376 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1377 result = CAUpdateCharacteristicsToAllGattClients(
1378 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1379 CA_SUPPORTED_BLE_MTU_SIZE);
1380 if (CA_STATUS_OK != result)
1382 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1383 OICFree(dataSegment);
1386 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
1389 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1390 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1392 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1393 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1394 result = CAUpdateCharacteristicsToAllGattClients(
1395 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1397 if (CA_STATUS_OK != result)
1399 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1401 OICFree(dataSegment);
1404 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1407 OICFree(dataSegment);
1409 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1412 void CABLEClientSendDataThread(void *threadData)
1414 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1416 CALEData_t *bleData = (CALEData_t *) threadData;
1419 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1423 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1424 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1426 uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1427 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
1428 if (NULL == dataSegment)
1430 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1435 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1436 if (CA_STATUS_OK != result )
1438 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1440 OICFree(dataSegment);
1443 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1446 uint32_t length = 0;
1447 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1449 length = totalLength;
1450 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1451 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1455 length = CA_SUPPORTED_BLE_MTU_SIZE;
1456 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1457 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1458 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1461 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1463 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1465 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1466 // Send the first segment with the header.
1467 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1472 if (CA_STATUS_OK != result)
1474 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1475 OICFree(dataSegment);
1479 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1480 for (index = 1; index < iter; index++)
1482 // Send the remaining header.
1483 result = CAUpdateCharacteristicsToGattServer(
1484 bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1485 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1486 CA_SUPPORTED_BLE_MTU_SIZE,
1488 if (CA_STATUS_OK != result)
1490 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1492 OICFree(dataSegment);
1495 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1496 CA_SUPPORTED_BLE_MTU_SIZE);
1499 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1500 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1502 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1503 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1504 result = CAUpdateCharacteristicsToGattServer(
1505 bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1506 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1510 if (CA_STATUS_OK != result)
1512 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1514 OICFree(dataSegment);
1517 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1522 //Sending Mulitcast Data
1523 // Send the first segment with the header.
1524 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1525 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1526 if (CA_STATUS_OK != result)
1528 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed (all), result [%d]", result);
1529 OICFree(dataSegment);
1532 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1533 // Send the remaining header.
1534 for (index = 1; index < iter; index++)
1536 result = CAUpdateCharacteristicsToAllGattServers(
1537 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1538 CA_SUPPORTED_BLE_MTU_SIZE);
1539 if (CA_STATUS_OK != result)
1541 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]", result);
1542 OICFree(dataSegment);
1545 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
1548 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1549 if ( remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1551 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1552 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1553 result = CAUpdateCharacteristicsToAllGattServers(
1554 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1556 if (CA_STATUS_OK != result)
1558 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]", result);
1559 OICFree(dataSegment);
1562 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1567 OICFree(dataSegment);
1569 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1572 CALEData_t *CACreateBLEData(const CARemoteEndpoint_t *remoteEndpoint, const void *data,
1573 uint32_t dataLength)
1575 CALEData_t *bleData = (CALEData_t *) OICMalloc(sizeof(CALEData_t));
1578 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1582 bleData->remoteEndpoint = CAAdapterCopyRemoteEndpoint(remoteEndpoint);
1583 bleData->data = (void *)OICCalloc(dataLength + 1, 1);
1584 if (NULL == bleData->data)
1586 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1587 CAFreeBLEData(bleData);
1590 memcpy(bleData->data, data, dataLength);
1591 bleData->dataLen = dataLength;
1596 void CAFreeBLEData(CALEData_t *bleData)
1598 VERIFY_NON_NULL_VOID(bleData, NULL, "Param bleData is NULL");
1600 CAAdapterFreeRemoteEndpoint(bleData->remoteEndpoint);
1601 OICFree(bleData->data);
1605 void CALEDataDestroyer(void *data, uint32_t size)
1607 CALEData_t *ledata = (CALEData_t *) data;
1609 CAFreeBLEData(ledata);
1613 CAResult_t CABLEClientSendData(const CARemoteEndpoint_t *remoteEndpoint,
1617 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1619 VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1621 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
1622 "g_bleClientSendQueueHandle is NULL",
1624 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
1625 "g_bleClientSendDataMutex is NULL",
1628 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
1631 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1633 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1636 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1637 return CA_MEMORY_ALLOC_FAILED;
1639 // Add message to send queue
1640 ca_mutex_lock(g_bleClientSendDataMutex);
1641 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
1642 ca_mutex_unlock(g_bleClientSendDataMutex);
1644 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1645 return CA_STATUS_OK;
1649 CAResult_t CABLEServerSendData(const CARemoteEndpoint_t *remoteEndpoint,
1653 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1655 VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1657 VERIFY_NON_NULL_RET(g_sendQueueHandle, CALEADAPTER_TAG,
1658 "BleClientReceiverQueue is NULL",
1660 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
1661 "BleClientSendDataMutex is NULL",
1664 VERIFY_NON_NULL_RET(g_sendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
1667 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1669 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1672 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1673 return CA_MEMORY_ALLOC_FAILED;
1675 // Add message to send queue
1676 ca_mutex_lock(g_bleServerSendDataMutex);
1677 CAQueueingThreadAddData(g_sendQueueHandle, bleData, sizeof(CALEData_t));
1678 ca_mutex_unlock(g_bleServerSendDataMutex);
1680 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1681 return CA_STATUS_OK;
1684 CAResult_t CABLEServerReceivedData(const char *remoteAddress, const char *serviceUUID,
1685 const void *data, uint32_t dataLength, uint32_t *sentLength)
1687 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1690 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1691 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1692 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1693 VERIFY_NON_NULL_RET(g_bleServerReceiverQueue, CALEADAPTER_TAG, "g_bleServerReceiverQueue",
1696 //Add message to data queue
1697 CARemoteEndpoint_t *remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1699 if (NULL == remoteEndpoint)
1701 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1702 return CA_STATUS_FAILED;
1705 // Create bleData to add to queue
1706 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1708 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1711 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1712 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1713 return CA_MEMORY_ALLOC_FAILED;
1716 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1717 // Add message to send queue
1718 CAQueueingThreadAddData(g_bleServerReceiverQueue, bleData, sizeof(CALEData_t));
1720 *sentLength = dataLength;
1722 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1723 return CA_STATUS_OK;
1726 CAResult_t CABLEClientReceivedData(const char *remoteAddress, const char *serviceUUID,
1727 const void *data, uint32_t dataLength, uint32_t *sentLength)
1729 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1732 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1733 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1734 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1735 VERIFY_NON_NULL_RET(g_bleClientReceiverQueue, CALEADAPTER_TAG, "g_bleClientReceiverQueue",
1738 //Add message to data queue
1739 CARemoteEndpoint_t *remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1741 if (NULL == remoteEndpoint)
1743 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1744 return CA_STATUS_FAILED;
1747 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1749 // Create bleData to add to queue
1750 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1753 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1754 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1755 return CA_MEMORY_ALLOC_FAILED;
1758 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1759 // Add message to send queue
1760 CAQueueingThreadAddData(g_bleClientReceiverQueue, bleData, sizeof(CALEData_t));
1762 *sentLength = dataLength;
1764 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1765 return CA_STATUS_OK;
1768 void CASetBleAdapterThreadPoolHandle(ca_thread_pool_t handle)
1770 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1772 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1773 g_bleAdapterThreadPool = handle;
1774 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1776 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1779 void CASetBLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
1781 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1783 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1785 g_networkPacketReceivedCallback = callback;
1787 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1789 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");