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"
34 * @var CALEADAPTER_TAG
35 * @brief Logging tag for module name.
37 #define CALEADAPTER_TAG "CA_BLE_ADAPTER"
40 * @var gNetworkCallback
41 * @brief Callback to provide the status of the network change to CA layer.
43 static CANetworkChangeCallback gNetworkCallback = NULL;
46 * @var gLocalBLEAddress
47 * @brief bleAddress of the local adapter. Value will be initialized to zero, and will be updated later.
49 static char gLocalBLEAddress[16] = { 0, };
53 * @brief Variable to differentiate btw GattServer and GattClient.
55 static CABool_t gIsServer = CA_FALSE;
58 * @var gBleIsServerMutex
59 * @brief Mutex to synchronize the task to be executed on the GattServer function calls.
61 static u_mutex gBleIsServerMutex = NULL;
64 * @var gBleNetworkCbMutex
65 * @brief Mutex to synchronize the callback to be called for the network changes.
67 static u_mutex gBleNetworkCbMutex = NULL;
70 * @var gBleLocalAddressMutex
71 * @brief Mutex to synchronize the updation of the local LE address of the adapter.
73 static u_mutex gBleLocalAddressMutex = NULL;
76 * @var gBleClientThreadPool
77 * @brief reference to threadpool
79 static u_thread_pool_t gBleAdapterThreadPool = NULL;
82 * @var gBleAdapterThreadPoolMutex
83 * @brief Mutex to synchronize the task to be pushed to thread pool.
85 static u_mutex gBleAdapterThreadPoolMutex = NULL;
88 * @var gBLEClientSendQueueHandle
89 * @brief Queue to process the outgoing packets from GATTClient.
91 static CAQueueingThread_t *gBLEClientSendQueueHandle = NULL;
94 * @var gCABleClientReceiverQueue
95 * @brief Queue to process the incoming packets to GATT Client.
97 static CAQueueingThread_t *gCABleClientReceiverQueue = NULL;
100 * @var gBleClientSendDataMutex
101 * @brief Mutex to synchronize the queing of the data from SenderQueue.
103 static u_mutex gBleClientSendDataMutex = NULL;
107 * @var gBleClientReceiveDataMutex
108 * @brief Mutex to synchronize the queing of the data from ReceiverQueue.
110 static u_mutex gBleClientReceiveDataMutex = NULL;
113 * @var gDataReceiverHandlerState
114 * @brief Stop condition of redvhandler.
116 static bool gDataReceiverHandlerState = false;
119 * @var gSendQueueHandle
120 * @brief Queue to process the outgoing packets from GATTServer.
122 static CAQueueingThread_t *gSendQueueHandle = NULL;
125 * @var gCABleServerReceiverQueue
126 * @brief Queue to process the incoming packets to GATTServer
128 static CAQueueingThread_t *gCABleServerReceiverQueue = NULL;
131 * @var gBleServerSendDataMutex
132 * @brief Mutex to synchronize the queing of the data from SenderQueue.
134 static u_mutex gBleServerSendDataMutex = NULL;
137 * @var gBleServerReceiveDataMutex
138 * @brief Mutex to synchronize the queing of the data from ReceiverQueue.
140 static u_mutex gBleServerReceiveDataMutex = NULL;
143 * @var gBleAdapterReqRespCbMutex
144 * @brief Mutex to synchronize the callback to be called for the adapterReqResponse.
146 static u_mutex gBleAdapterReqRespCbMutex = NULL;
149 * @var gNetworkPacketReceivedCallback
150 * @brief Callback to be called when the network packet recieved from either GattServer or GattClient.
152 static CANetworkPacketReceivedCallback gNetworkPacketReceivedCallback = NULL;
155 * @ENUM CALeServerStatus
156 * @brief status of BLE Server Status
158 * This ENUM provides information of LE Adapter Server status
162 CA_SERVER_NOTSTARTED = 0,
168 * @var gLeServerStatus
169 * @brief structure to maintain the status of the server.
171 static CALeServerStatus gLeServerStatus = CA_SERVER_NOTSTARTED;
174 * @fn CALERegisterNetworkNotifications
175 * @brief This function is used to register network change notification callback.
177 * @param[in] netCallback CANetworkChangeCallback callback which will be set for the change in nwk.
179 * @return 0 for success.
182 int32_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
185 * @fn CASetBleAdapterThreadPoolHandle
186 * @brief Used to Set the gThreadPool handle which is required for spawning new thread.
188 * @param[in] handle - Thread pool handle which is given by above layer for using thread creation task.
193 void CASetBleAdapterThreadPoolHandle(u_thread_pool_t handle);
196 * @fn CALEDeviceStateChangedCb
197 * @brief This function is used to call the callback to the upper layer when the device state gets changed.
199 * @param[in] adapter_state New state of the adapter to be notified to the upper layer.
204 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state);
207 * @fn CAInitBleAdapterMutex
208 * @brief Used to initialize all required mutex variable for LE Adapter implementation.
210 * @return 0 on success otherwise a positive error value.
211 * @retval CA_STATUS_OK Successful
212 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
213 * @retval CA_STATUS_FAILED Operation failed
216 CAResult_t CAInitBleAdapterMutex();
219 * @fn CATermiateBleAdapterMutex
220 * @brief Used to terminate all required mutex variable for LE adapter implementation.
225 void CATermiateBleAdapterMutex();
226 static void CALEDataDestroyer(void *data, uint32_t size);
228 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
229 CANetworkPacketReceivedCallback reqRespCallback,
230 CANetworkChangeCallback netCallback,
231 u_thread_pool_t handle)
233 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
236 VERIFY_NON_NULL(registerCallback, NULL, "RegisterConnectivity callback is null");
237 VERIFY_NON_NULL(reqRespCallback, NULL, "PacketReceived Callback is null");
238 VERIFY_NON_NULL(netCallback, NULL, "NetworkChange Callback is null");
240 CAResult_t result = CAInitBleAdapterMutex();
241 if (CA_STATUS_OK != result)
243 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
244 return CA_STATUS_FAILED;
247 result = CAInitializeLENwkMonitor();
248 if (CA_STATUS_OK != result)
250 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "CAInitializeLENwkMonitor() failed");
251 return CA_STATUS_FAILED;
254 CAInitializeLEAdapter();
256 CASetBleServerThreadPoolHandle(handle);
257 CASetBleClientThreadPoolHandle(handle);
258 CASetBleAdapterThreadPoolHandle(handle);
259 CASetBLEReqRespServerCallback(CABLEServerReceivedData);
260 CASetBLEReqRespClientCallback(CABLEClientReceivedData);
261 CASetBLEReqRespAdapterCallback(reqRespCallback);
263 CALERegisterNetworkNotifications(netCallback);
265 CAConnectivityHandler_t connHandler;
266 connHandler.startAdapter = NULL;
267 connHandler.stopAdapter = CAStopLE;
268 connHandler.startListenServer = CAStartLEListeningServer;
269 connHandler.startDiscoverServer = CAStartLEDiscoveryServer;
270 connHandler.sendData = CASendLEUnicastData;
271 connHandler.sendDataToAll = CASendLEMulticastData;
272 connHandler.GetnetInfo = CAGetLEInterfaceInformation;
273 connHandler.readData = CAReadLEData;
274 connHandler.terminate = CATerminateLE;
275 registerCallback(connHandler, CA_LE);
277 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
282 CAResult_t CAStopLE()
284 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
288 u_mutex_lock(gBleIsServerMutex);
289 if (CA_TRUE == gIsServer)
291 CAStopBleGattServer();
295 CAStopBLEGattClient();
297 u_mutex_unlock(gBleIsServerMutex);
299 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
306 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
308 CASetBLEReqRespServerCallback(NULL);
309 CASetBLEReqRespClientCallback(NULL);
310 CALERegisterNetworkNotifications(NULL);
311 CASetBLEReqRespAdapterCallback(NULL);
312 CATerminateLENwkMonitor();
314 u_mutex_lock(gBleIsServerMutex);
315 if (CA_TRUE == gIsServer)
317 CATerminateBleGattServer();
321 CATerminateBLEGattClient();
323 u_mutex_unlock(gBleIsServerMutex);
325 CATerminateBleQueues();
327 CATermiateBleAdapterMutex();
329 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
333 CAResult_t CAStartLEListeningServer()
335 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
337 CAResult_t result = CAInitBleServerQueues();
338 if (CA_STATUS_OK != result)
340 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
341 return CA_STATUS_FAILED;
344 result = CAGetLEAdapterState();
345 if (CA_ADAPTER_NOT_ENABLED == result)
347 gLeServerStatus = CA_LISTENING_SERVER;
348 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
352 if (CA_STATUS_FAILED == result)
354 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
355 return CA_STATUS_FAILED;
358 CAStartBleGattServer();
360 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
361 u_mutex_lock(gBleIsServerMutex);
363 u_mutex_unlock(gBleIsServerMutex);
367 CAResult_t CAStartLEDiscoveryServer()
369 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
371 CAResult_t result = CAInitBleClientQueues();
372 if (CA_STATUS_OK != result)
374 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
375 return CA_STATUS_FAILED;
378 result = CAGetLEAdapterState();
379 if (CA_ADAPTER_NOT_ENABLED == result)
381 gLeServerStatus = CA_DISCOVERY_SERVER;
382 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
386 if (CA_STATUS_FAILED == result)
388 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
389 return CA_STATUS_FAILED;
392 CAStartBLEGattClient();
394 u_mutex_lock(gBleIsServerMutex);
395 gIsServer = CA_FALSE;
396 u_mutex_unlock(gBleIsServerMutex);
398 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
402 CAResult_t CAStartLENotifyServer()
404 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
406 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
410 uint32_t CASendLENotification(const CARemoteEndpoint_t *endpoint, void *data, uint32_t dataLen)
412 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
414 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
418 CAResult_t CAReadLEData()
420 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
422 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
426 uint32_t CASendLEUnicastData(const CARemoteEndpoint_t *endpoint, void *data, uint32_t dataLen)
428 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
431 VERIFY_NON_NULL(endpoint, NULL, "Remote endpoint is null");
432 VERIFY_NON_NULL(data, NULL, "Data is null");
434 CAResult_t result = CA_STATUS_FAILED;
436 u_mutex_lock(gBleIsServerMutex);
437 if (CA_TRUE == gIsServer)
439 result = CABLEServerSendData(endpoint, data, dataLen);
440 if (CA_STATUS_OK != result)
442 OIC_LOG(ERROR, CALEADAPTER_TAG,
443 "[SendLEUnicastData] CABleServerSenderQueueEnqueueMessage failed \n");
444 u_mutex_unlock(gBleIsServerMutex);
450 result = CABLEClientSendData(endpoint, data, dataLen);
451 if (CA_STATUS_OK != result)
453 OIC_LOG(ERROR, CALEADAPTER_TAG,
454 "[SendLEUnicastData] CABleClientSenderQueueEnqueueMessage failed \n");
455 u_mutex_unlock(gBleIsServerMutex);
459 u_mutex_unlock(gBleIsServerMutex);
461 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
465 uint32_t CASendLEMulticastData(void *data, uint32_t dataLen)
467 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
470 VERIFY_NON_NULL(data, NULL, "Data is null");
474 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
478 CAResult_t result = CA_STATUS_FAILED;
480 u_mutex_lock(gBleIsServerMutex);
481 if (CA_TRUE == gIsServer)
483 result = CABLEServerSendData(NULL, data, dataLen);
484 if (CA_STATUS_OK != result)
486 OIC_LOG(ERROR, CALEADAPTER_TAG,
487 "[SendLEMulticastDataToAll] CABleServerSenderQueueEnqueueMessage failed" );
488 u_mutex_unlock(gBleIsServerMutex);
494 result = CABLEClientSendData(NULL, data, dataLen);
495 if (CA_STATUS_OK != result)
497 OIC_LOG(ERROR, CALEADAPTER_TAG,
498 "[SendLEMulticastDataToAll] CABleClientSenderQueueEnqueueMessage failed" );
499 u_mutex_unlock(gBleIsServerMutex);
503 u_mutex_unlock(gBleIsServerMutex);
505 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
509 CAResult_t CAGetLEInterfaceInformation(CALocalConnectivity_t **info, uint32_t *size)
511 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
513 VERIFY_NON_NULL(info, NULL, "CALocalConnectivity info is null");
515 char *local_address = NULL;
517 CAGetLEAddress(&local_address);
518 if (NULL == local_address)
520 OIC_LOG(ERROR, CALEADAPTER_TAG, "Get local bt adapter address failed");
521 return CA_STATUS_FAILED;
525 (*info) = (CALocalConnectivity_t *) OICMalloc(sizeof(CALocalConnectivity_t));
528 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
529 return CA_STATUS_FAILED;
531 memset((*info), 0x0, sizeof(CALocalConnectivity_t));
533 strncpy((*info)->addressInfo.BT.btMacAddress, local_address, strlen(local_address));
534 u_mutex_lock(gBleLocalAddressMutex);
535 strncpy(gLocalBLEAddress, local_address, sizeof(gLocalBLEAddress));
536 u_mutex_unlock(gBleLocalAddressMutex);
538 (*info)->type = CA_LE;
540 OICFree(local_address);
542 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
546 int32_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
548 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
550 u_mutex_lock(gBleNetworkCbMutex);
551 gNetworkCallback = netCallback;
552 u_mutex_unlock(gBleNetworkCbMutex);
556 CAResult_t res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
557 if (CA_STATUS_OK != res)
559 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
564 CAResult_t res = CAUnSetLEAdapterStateChangedCb();
565 if (CA_STATUS_OK != res)
567 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
571 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
575 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
577 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
579 CALocalConnectivity_t localEndpoint = {{{{0}}}};
581 u_mutex_lock(gBleLocalAddressMutex);
582 strncpy(localEndpoint.addressInfo.BT.btMacAddress, gLocalBLEAddress, strlen(gLocalBLEAddress));
583 u_mutex_unlock(gBleLocalAddressMutex);
585 // Start a GattServer/Client if gLeServerStatus is SET
586 if (CA_LISTENING_SERVER == gLeServerStatus)
588 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattServer");
589 CAStartBleGattServer();
591 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
593 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
594 CAStartBLEGattClient();
596 gLeServerStatus = CA_SERVER_NOTSTARTED;
598 u_mutex_lock(gBleNetworkCbMutex);
599 if (NULL != gNetworkCallback)
601 gNetworkCallback(&localEndpoint, adapter_state);
605 OIC_LOG(ERROR, CALEADAPTER_TAG, "gNetworkCallback is NULL");
607 u_mutex_unlock(gBleNetworkCbMutex);
609 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
613 CAResult_t CAInitBleAdapterMutex()
615 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
618 if (NULL == gBleIsServerMutex)
620 gBleIsServerMutex = u_mutex_new();
621 if (NULL == gBleIsServerMutex)
623 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
624 return CA_STATUS_FAILED;
628 if (NULL == gBleNetworkCbMutex)
630 gBleNetworkCbMutex = u_mutex_new();
631 if (NULL == gBleNetworkCbMutex)
633 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
634 return CA_STATUS_FAILED;
638 if (NULL == gBleLocalAddressMutex)
640 gBleLocalAddressMutex = u_mutex_new();
641 if (NULL == gBleLocalAddressMutex)
643 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
644 return CA_STATUS_FAILED;
648 if (NULL == gBleAdapterThreadPoolMutex)
650 gBleAdapterThreadPoolMutex = u_mutex_new();
651 if (NULL == gBleAdapterThreadPoolMutex)
653 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
654 return CA_STATUS_FAILED;
658 if (NULL == gBleClientSendDataMutex)
660 gBleClientSendDataMutex = u_mutex_new();
661 if (NULL == gBleClientSendDataMutex)
663 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
664 return CA_STATUS_FAILED;
668 if (NULL == gBleClientReceiveDataMutex)
670 gBleClientReceiveDataMutex = u_mutex_new();
671 if (NULL == gBleClientReceiveDataMutex)
673 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
674 return CA_STATUS_FAILED;
678 if (NULL == gBleServerSendDataMutex)
680 gBleServerSendDataMutex = u_mutex_new();
681 if (NULL == gBleServerSendDataMutex)
683 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
684 return CA_STATUS_FAILED;
688 if (NULL == gBleServerReceiveDataMutex)
690 gBleServerReceiveDataMutex = u_mutex_new();
691 if (NULL == gBleServerReceiveDataMutex)
693 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
694 return CA_STATUS_FAILED;
698 if (NULL == gBleAdapterReqRespCbMutex)
700 gBleAdapterReqRespCbMutex = u_mutex_new();
701 if (NULL == gBleAdapterReqRespCbMutex)
703 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
704 return CA_STATUS_FAILED;
708 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
712 void CATermiateBleAdapterMutex()
714 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
716 u_mutex_free(gBleIsServerMutex);
717 gBleIsServerMutex = NULL;
719 u_mutex_free(gBleNetworkCbMutex);
720 gBleNetworkCbMutex = NULL;
722 u_mutex_free(gBleLocalAddressMutex);
723 gBleLocalAddressMutex = NULL;
725 u_mutex_free(gBleAdapterThreadPoolMutex);
726 gBleAdapterThreadPoolMutex = NULL;
728 u_mutex_free(gBleClientSendDataMutex);
729 gBleClientSendDataMutex = NULL;
731 u_mutex_free(gBleClientReceiveDataMutex);
732 gBleClientReceiveDataMutex = NULL;
734 u_mutex_free(gBleServerSendDataMutex);
735 gBleServerSendDataMutex = NULL;
737 u_mutex_free(gBleServerReceiveDataMutex);
738 gBleServerReceiveDataMutex = NULL;
740 u_mutex_free(gBleAdapterReqRespCbMutex);
741 gBleAdapterReqRespCbMutex = NULL;
743 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
747 void CAInitBleQueues()
749 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
751 CAResult_t result = CAInitBleServerQueues();
752 if (CA_STATUS_OK != result)
754 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
758 result = CAInitBleClientQueues();
759 if (CA_STATUS_OK != result)
761 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
765 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
769 CAResult_t CAInitBleServerQueues()
771 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
773 u_mutex_lock(gBleAdapterThreadPoolMutex);
775 CAResult_t result = CAInitBleServerSenderQueue();
776 if (CA_STATUS_OK != result)
778 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
779 u_mutex_unlock(gBleAdapterThreadPoolMutex);
780 return CA_STATUS_FAILED;
783 result = CAInitBleServerReceiverQueue();
784 if (CA_STATUS_OK != result)
786 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
787 u_mutex_unlock(gBleAdapterThreadPoolMutex);
788 return CA_STATUS_FAILED;
791 gDataReceiverHandlerState = true;
793 u_mutex_unlock(gBleAdapterThreadPoolMutex);
795 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
799 CAResult_t CAInitBleClientQueues()
801 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
803 u_mutex_lock(gBleAdapterThreadPoolMutex);
805 CAResult_t result = CAInitBleClientSenderQueue();
806 if (CA_STATUS_OK != result)
808 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
809 u_mutex_unlock(gBleAdapterThreadPoolMutex);
810 return CA_STATUS_FAILED;
813 result = CAInitBleClientReceiverQueue();
814 if (CA_STATUS_OK != result)
816 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
817 u_mutex_unlock(gBleAdapterThreadPoolMutex);
818 return CA_STATUS_FAILED;
821 gDataReceiverHandlerState = true;
823 u_mutex_unlock(gBleAdapterThreadPoolMutex);
825 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
829 CAResult_t CAInitBleServerSenderQueue()
831 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
832 // Check if the message queue is already initialized
833 if (gSendQueueHandle)
835 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
839 // Create send message queue
840 gSendQueueHandle = OICMalloc(sizeof(CAQueueingThread_t));
841 if (!gSendQueueHandle)
843 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
844 return CA_MEMORY_ALLOC_FAILED;
847 if (CA_STATUS_OK != CAQueueingThreadInitialize(gSendQueueHandle, gBleAdapterThreadPool,
848 CABLEServerSendDataThread, CALEDataDestroyer))
850 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
851 OICFree(gSendQueueHandle);
852 gSendQueueHandle = NULL;
853 return CA_STATUS_FAILED;
856 if (CA_STATUS_OK != CAQueueingThreadStart(gSendQueueHandle))
858 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "u_thread_pool_add_task failed ");
859 OICFree(gSendQueueHandle);
860 gSendQueueHandle = NULL;
861 return CA_STATUS_FAILED;
864 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
868 CAResult_t CAInitBleClientSenderQueue()
870 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
872 if (gBLEClientSendQueueHandle)
874 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
878 // Create send message queue
879 gBLEClientSendQueueHandle = OICMalloc(sizeof(CAQueueingThread_t));
880 if (!gBLEClientSendQueueHandle)
882 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
883 return CA_MEMORY_ALLOC_FAILED;
886 if (CA_STATUS_OK != CAQueueingThreadInitialize(gBLEClientSendQueueHandle, gBleAdapterThreadPool,
887 CABLEClientSendDataThread, CALEDataDestroyer))
889 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
890 OICFree(gBLEClientSendQueueHandle);
891 gBLEClientSendQueueHandle = NULL;
892 return CA_STATUS_FAILED;
895 if (CA_STATUS_OK != CAQueueingThreadStart(gBLEClientSendQueueHandle))
897 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "u_thread_pool_add_task failed ");
898 OICFree(gBLEClientSendQueueHandle);
899 gBLEClientSendQueueHandle = NULL;
900 return CA_STATUS_FAILED;
903 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
907 CAResult_t CAInitBleServerReceiverQueue()
909 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
910 // Check if the message queue is already initialized
911 if (gCABleServerReceiverQueue)
913 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
917 // Create send message queue
918 gCABleServerReceiverQueue = OICMalloc(sizeof(CAQueueingThread_t));
919 if (!gCABleServerReceiverQueue)
921 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
922 OICFree(gSendQueueHandle);
923 return CA_MEMORY_ALLOC_FAILED;
926 if (CA_STATUS_OK != CAQueueingThreadInitialize(gCABleServerReceiverQueue, gBleAdapterThreadPool,
927 CABLEServerDataReceiverHandler, CALEDataDestroyer))
929 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
930 OICFree(gCABleServerReceiverQueue);
931 gCABleServerReceiverQueue = NULL;
932 return CA_STATUS_FAILED;
935 if (CA_STATUS_OK != CAQueueingThreadStart(gCABleServerReceiverQueue))
937 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "u_thread_pool_add_task failed ");
938 OICFree(gCABleServerReceiverQueue);
939 gCABleServerReceiverQueue = NULL;
940 return CA_STATUS_FAILED;
943 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
947 CAResult_t CAInitBleClientReceiverQueue()
949 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
951 // Check if the message queue is already initialized
952 if (gCABleClientReceiverQueue)
954 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
958 // Create send message queue
959 gCABleClientReceiverQueue = OICMalloc(sizeof(CAQueueingThread_t));
960 if (!gCABleClientReceiverQueue)
962 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
963 OICFree(gBLEClientSendQueueHandle);
964 return CA_MEMORY_ALLOC_FAILED;
967 if (CA_STATUS_OK != CAQueueingThreadInitialize(gCABleClientReceiverQueue, gBleAdapterThreadPool,
968 CABLEClientDataReceiverHandler, NULL))
970 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
971 OICFree(gBLEClientSendQueueHandle);
972 OICFree(gCABleClientReceiverQueue);
973 gCABleClientReceiverQueue = NULL;
974 return CA_STATUS_FAILED;
977 if (CA_STATUS_OK != CAQueueingThreadStart(gCABleClientReceiverQueue))
979 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "u_thread_pool_add_task failed ");
980 OICFree(gCABleClientReceiverQueue);
981 gCABleClientReceiverQueue = NULL;
982 return CA_STATUS_FAILED;
985 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
989 void CAStopBleQueues()
991 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
993 u_mutex_lock(gBleClientSendDataMutex);
994 if (NULL != gBLEClientSendQueueHandle)
996 CAQueueingThreadStop(gBLEClientSendQueueHandle);
998 u_mutex_unlock(gBleClientSendDataMutex);
1000 u_mutex_lock(gBleClientReceiveDataMutex);
1001 if (NULL != gCABleClientReceiverQueue)
1003 CAQueueingThreadStop(gCABleClientReceiverQueue);
1005 u_mutex_unlock(gBleClientReceiveDataMutex);
1007 u_mutex_lock(gBleServerSendDataMutex);
1008 if (NULL != gSendQueueHandle)
1010 CAQueueingThreadStop(gSendQueueHandle);
1012 u_mutex_unlock(gBleServerSendDataMutex);
1014 u_mutex_lock(gBleServerReceiveDataMutex);
1015 if (NULL != gCABleServerReceiverQueue)
1017 CAQueueingThreadStop(gCABleServerReceiverQueue);
1019 u_mutex_unlock(gBleServerReceiveDataMutex);
1021 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1024 void CATerminateBleQueues()
1026 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1028 CAQueueingThreadDestroy(gBLEClientSendQueueHandle);
1029 OICFree(gBLEClientSendQueueHandle);
1030 gBLEClientSendQueueHandle = NULL;
1033 CAQueueingThreadDestroy(gCABleClientReceiverQueue);
1034 OICFree(gCABleClientReceiverQueue);
1035 gCABleClientReceiverQueue = NULL;
1038 CAQueueingThreadDestroy(gSendQueueHandle);
1039 OICFree(gSendQueueHandle);
1040 gSendQueueHandle = NULL;
1043 CAQueueingThreadDestroy(gCABleServerReceiverQueue);
1044 OICFree(gCABleServerReceiverQueue);
1045 gCABleServerReceiverQueue = NULL;
1047 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1049 void CABLEServerDataReceiverHandler(void *threadData)
1051 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN");
1053 // CAAdapterMessage_t *message = NULL;
1054 static const char *remoteAddress = NULL;
1055 static const char *serviceUUID = NULL;
1056 static uint32_t recvDataLen = 0;
1057 static uint32_t totalDataLen = 0;
1058 static char *defragData = NULL;
1059 static bool isHeaderAvailable = false;
1060 static CARemoteEndpoint_t *remoteEndpoint = NULL;
1062 u_mutex_lock(gBleClientReceiveDataMutex);
1064 if (gDataReceiverHandlerState)
1066 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1068 CABLEData *bleData = (CABLEData *) threadData;
1071 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid wifidata!");
1075 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1077 if (!isHeaderAvailable)
1079 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1080 char *header = (char *) OICMalloc(sizeof(char) * CA_HEADER_LENGTH);
1081 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "header is NULL");
1083 memset(header, 0x0, sizeof(char) * CA_HEADER_LENGTH);
1084 memcpy(header, bleData->data, CA_HEADER_LENGTH);
1085 totalDataLen = CAParseHeader(header);
1087 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes", totalDataLen);
1088 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%d] bytes", bleData->dataLen);
1090 defragData = (char *) OICMalloc(sizeof(char) * totalDataLen + 1);
1091 if (NULL == defragData)
1093 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1097 memset(defragData, 0x0, sizeof(char) * totalDataLen + 1);
1100 remoteAddress = bleData->remoteEndpoint->addressInfo.LE.leMacAddress;
1101 serviceUUID = bleData->remoteEndpoint->resourceUri;
1103 remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1106 memcpy(defragData + recvDataLen, bleData->data + CA_HEADER_LENGTH,
1107 bleData->dataLen - CA_HEADER_LENGTH);
1108 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1109 isHeaderAvailable = true;
1113 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1114 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1115 recvDataLen += bleData->dataLen ;
1116 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1117 totalDataLen, recvDataLen);
1119 if (totalDataLen <= recvDataLen)
1121 u_mutex_lock(gBleAdapterReqRespCbMutex);
1122 if (NULL == gNetworkPacketReceivedCallback)
1124 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1125 u_mutex_unlock(gBleAdapterReqRespCbMutex);
1128 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1129 gNetworkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1132 isHeaderAvailable = false;
1133 remoteEndpoint = NULL;
1135 u_mutex_unlock(gBleAdapterReqRespCbMutex);
1138 if (false == gDataReceiverHandlerState)
1140 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1143 isHeaderAvailable = false;
1144 OICFree(defragData);
1145 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1146 u_mutex_unlock(gBleClientReceiveDataMutex);
1150 u_mutex_unlock(gBleClientReceiveDataMutex);
1151 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT");
1154 void CABLEClientDataReceiverHandler(void *threadData)
1156 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN");
1158 static const char *remoteAddress = NULL;
1159 static const char *serviceUUID = NULL;
1160 static uint32_t recvDataLen = 0;
1161 static uint32_t totalDataLen = 0;
1162 static char *defragData = NULL;
1163 static bool isHeaderAvailable = false;
1164 static CARemoteEndpoint_t *remoteEndpoint = NULL;
1166 u_mutex_lock(gBleClientReceiveDataMutex);
1168 if (gDataReceiverHandlerState)
1170 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1172 CABLEData *bleData = (CABLEData *) threadData;
1175 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid wifidata!");
1179 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1181 if (!isHeaderAvailable)
1183 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1184 char *header = (char *) OICMalloc(sizeof(char) * CA_HEADER_LENGTH);
1185 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "header is NULL");
1187 memcpy(header, bleData->data, CA_HEADER_LENGTH);
1188 totalDataLen = CAParseHeader(header);
1189 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes", totalDataLen);
1190 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received in the first packet [%d] bytes", bleData->dataLen);
1191 defragData = (char *) OICMalloc(sizeof(char) * totalDataLen);
1192 if (NULL == defragData)
1194 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1201 remoteAddress = bleData->remoteEndpoint->addressInfo.LE.leMacAddress;
1202 serviceUUID = bleData->remoteEndpoint->resourceUri;
1204 remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1207 memcpy(defragData , bleData->data + CA_HEADER_LENGTH,
1208 bleData->dataLen - CA_HEADER_LENGTH);
1209 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1210 isHeaderAvailable = true;
1214 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1215 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1216 recvDataLen += bleData->dataLen ;
1217 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1218 totalDataLen, recvDataLen);
1220 if (totalDataLen <= recvDataLen)
1222 u_mutex_lock(gBleAdapterReqRespCbMutex);
1223 if (NULL == gNetworkPacketReceivedCallback)
1225 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1226 u_mutex_unlock(gBleAdapterReqRespCbMutex);
1229 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1230 gNetworkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1233 isHeaderAvailable = false;
1234 remoteEndpoint = NULL;
1236 u_mutex_unlock(gBleAdapterReqRespCbMutex);
1239 if (false == gDataReceiverHandlerState)
1241 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1242 OICFree(defragData);
1243 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1244 u_mutex_unlock(gBleClientReceiveDataMutex);
1248 u_mutex_unlock(gBleClientReceiveDataMutex);
1249 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT");
1252 void CABLEServerSendDataThread(void *threadData)
1254 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1256 CABLEData *bleData = (CABLEData *) threadData;
1259 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1262 char *header = (char *) OICMalloc(sizeof(char) * CA_HEADER_LENGTH);
1263 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1265 char *dataSegment = (char *) OICMalloc(sizeof(char) * bleData->dataLen + CA_HEADER_LENGTH + 1);
1266 if (NULL == dataSegment)
1268 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1273 memset(header, 0x0, sizeof(char) * CA_HEADER_LENGTH );
1274 memset(dataSegment, 0x0, sizeof(char) * bleData->dataLen + CA_HEADER_LENGTH + 1 );
1276 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1277 if (CA_STATUS_OK != result )
1279 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1281 OICFree(dataSegment);
1285 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1289 if (CA_SUPPORTED_BLE_MTU_SIZE >= bleData->dataLen)
1291 length = bleData->dataLen + CA_HEADER_LENGTH;
1292 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1293 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1297 length = CA_SUPPORTED_BLE_MTU_SIZE;
1298 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1299 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1300 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1303 int32_t iter = bleData->dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
1305 u_mutex_lock(gBleServerSendDataMutex);
1306 // Send the first segment with the header.
1307 result = CAUpdateCharacteristicsInGattServer(dataSegment, length);
1308 if (CA_STATUS_OK != result)
1310 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1311 OICFree(dataSegment);
1312 u_mutex_unlock(gBleServerSendDataMutex);
1316 for (index = 1; index < iter; index++)
1318 // Send the remaining header.
1319 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1320 result = CAUpdateCharacteristicsInGattServer(
1321 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1322 CA_SUPPORTED_BLE_MTU_SIZE);
1323 if (CA_STATUS_OK != result)
1325 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1326 OICFree(dataSegment);
1327 u_mutex_unlock(gBleServerSendDataMutex);
1331 if (bleData->dataLen / CA_SUPPORTED_BLE_MTU_SIZE)
1333 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1334 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1335 result = CAUpdateCharacteristicsInGattServer(
1336 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1337 bleData->dataLen % CA_SUPPORTED_BLE_MTU_SIZE + CA_HEADER_LENGTH);
1338 if (CA_STATUS_OK != result)
1340 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1341 OICFree(dataSegment);
1342 u_mutex_unlock(gBleServerSendDataMutex);
1347 OICFree(dataSegment);
1348 u_mutex_unlock(gBleServerSendDataMutex); // TODO: this mutex required ?
1350 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1355 void CABLEClientSendDataThread(void *threadData)
1357 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1359 CABLEData *bleData = (CABLEData *) threadData;
1362 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1365 char *header = (char *) OICMalloc(sizeof(char) * CA_HEADER_LENGTH);
1366 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1368 char *dataSegment = (char *) OICMalloc(sizeof(char) * bleData->dataLen + CA_HEADER_LENGTH + 1);
1369 if (NULL == dataSegment)
1371 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1376 memset(header, 0x0, sizeof(char) * CA_HEADER_LENGTH );
1377 memset(dataSegment, 0x0, sizeof(char) * bleData->dataLen + CA_HEADER_LENGTH + 1);
1379 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1380 if (CA_STATUS_OK != result )
1382 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1384 OICFree(dataSegment);
1387 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1391 if (CA_SUPPORTED_BLE_MTU_SIZE >= bleData->dataLen)
1393 length = bleData->dataLen + CA_HEADER_LENGTH;
1394 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1395 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1399 length = CA_SUPPORTED_BLE_MTU_SIZE;
1400 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1401 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1402 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1405 int32_t iter = bleData->dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
1407 u_mutex_lock(gBleClientSendDataMutex);
1408 if (NULL != bleData->remoteEndpoint)
1410 // Send the first segment with the header.
1411 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1415 if (CA_STATUS_OK != result)
1417 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1418 OICFree(dataSegment);
1419 u_mutex_unlock(gBleClientSendDataMutex);
1422 for (index = 1; index < iter; index++)
1424 // Send the remaining header.
1425 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1426 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1427 CA_SUPPORTED_BLE_MTU_SIZE,
1429 if (CA_STATUS_OK != result)
1431 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1433 OICFree(dataSegment);
1434 u_mutex_unlock(gBleClientSendDataMutex);
1438 if (bleData->dataLen / CA_SUPPORTED_BLE_MTU_SIZE)
1440 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1441 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1442 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1443 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1444 bleData->dataLen % CA_SUPPORTED_BLE_MTU_SIZE + CA_HEADER_LENGTH,
1446 if (CA_STATUS_OK != result)
1448 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1450 OICFree(dataSegment);
1451 u_mutex_unlock(gBleClientSendDataMutex);
1458 // Send the first segment with the header.
1459 result = CAUpdateCharacteristicsToAllGattServers(dataSegment + (index * length),
1461 if (CA_STATUS_OK != result)
1463 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed (all), result [%d]",
1465 OICFree(dataSegment);
1466 u_mutex_unlock(gBleClientSendDataMutex);
1469 // Send the remaining header.
1470 for (index = 1; index < iter; index++)
1472 result = CAUpdateCharacteristicsToAllGattServers(
1473 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1474 CA_SUPPORTED_BLE_MTU_SIZE);
1475 if (CA_STATUS_OK != result)
1477 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]",
1479 OICFree(dataSegment);
1480 u_mutex_unlock(gBleClientSendDataMutex);
1484 if (bleData->dataLen / CA_SUPPORTED_BLE_MTU_SIZE)
1486 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1487 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1488 result = CAUpdateCharacteristicsToAllGattServers(
1489 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1490 bleData->dataLen % CA_SUPPORTED_BLE_MTU_SIZE + CA_HEADER_LENGTH);
1491 if (CA_STATUS_OK != result)
1493 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]",
1495 OICFree(dataSegment);
1496 u_mutex_unlock(gBleClientSendDataMutex);
1502 OICFree(dataSegment);
1503 u_mutex_unlock(gBleClientSendDataMutex);
1505 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1509 CABLEData *CACreateBLEData(const CARemoteEndpoint_t *remoteEndpoint, void *data,
1510 uint32_t dataLength)
1512 CABLEData *bleData = (CABLEData *) OICMalloc(sizeof(CABLEData));
1515 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1519 bleData->remoteEndpoint = CAAdapterCopyRemoteEndpoint(remoteEndpoint);
1520 bleData->data = (void *)OICMalloc(dataLength + 1);
1521 if (NULL == bleData->data)
1523 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1524 CAFreeBLEData(bleData);
1527 memset(bleData->data, 0x0, dataLength+1);
1528 memcpy(bleData->data, data, dataLength);
1529 bleData->dataLen = dataLength;
1534 void CAFreeBLEData(CABLEData *bleData)
1539 CAAdapterFreeRemoteEndpoint(bleData->remoteEndpoint);
1540 OICFree(bleData->data);
1544 void CALEDataDestroyer(void *data, uint32_t size)
1546 CABLEData *ledata = (CABLEData *) data;
1548 CAFreeBLEData(ledata);
1552 CAResult_t CABLEClientSendData(const CARemoteEndpoint_t *remoteEndpoint,
1556 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN ");
1558 VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1560 VERIFY_NON_NULL_RET(gBLEClientSendQueueHandle, CALEADAPTER_TAG,
1561 "gBLEClientSendQueueHandle is NULL",
1563 VERIFY_NON_NULL_RET(gBleClientSendDataMutex, CALEADAPTER_TAG,
1564 "gBleClientSendDataMutex is NULL",
1567 VERIFY_NON_NULL_RET(gBLEClientSendQueueHandle, CALEADAPTER_TAG, "gBLEClientSendQueueHandle",
1570 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1572 CABLEData *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1575 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1576 return CA_MEMORY_ALLOC_FAILED;
1578 // Add message to send queue
1579 u_mutex_lock(gBleClientSendDataMutex);
1580 CAQueueingThreadAddData(gBLEClientSendQueueHandle, bleData, sizeof(CABLEData));
1581 u_mutex_unlock(gBleClientSendDataMutex);
1583 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT ");
1584 return CA_STATUS_OK;
1588 CAResult_t CABLEServerSendData(const CARemoteEndpoint_t *remoteEndpoint,
1592 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN ");
1594 VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1596 VERIFY_NON_NULL_RET(gSendQueueHandle, CALEADAPTER_TAG,
1597 "BleClientReceiverQueue is NULL",
1599 VERIFY_NON_NULL_RET(gBleClientSendDataMutex, CALEADAPTER_TAG,
1600 "BleClientSendDataMutex is NULL",
1603 VERIFY_NON_NULL_RET(gSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
1606 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1608 CABLEData *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1611 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1612 return CA_MEMORY_ALLOC_FAILED;
1614 // Add message to send queue
1615 u_mutex_lock(gBleServerSendDataMutex);
1616 CAQueueingThreadAddData(gSendQueueHandle, bleData, sizeof(CABLEData));
1617 u_mutex_unlock(gBleServerSendDataMutex);
1619 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT ");
1620 return CA_STATUS_OK;
1623 CAResult_t CABLEServerReceivedData(const char *remoteAddress, const char *serviceUUID,
1624 void *data, uint32_t dataLength, uint32_t *sentLength)
1626 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN");
1629 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1630 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1631 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1632 VERIFY_NON_NULL_RET(gCABleServerReceiverQueue, CALEADAPTER_TAG, "gCABleServerReceiverQueue",
1635 //Add message to data queue
1636 CARemoteEndpoint_t *remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1638 if (NULL == remoteEndpoint)
1640 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1641 return CA_STATUS_FAILED;
1644 // Create bleData to add to queue
1645 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1647 CABLEData *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1650 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1651 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1652 return CA_MEMORY_ALLOC_FAILED;
1655 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1656 // Add message to send queue
1657 CAQueueingThreadAddData(gCABleServerReceiverQueue, bleData, sizeof(CABLEData));
1659 *sentLength = dataLength;
1661 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT");
1662 return CA_STATUS_OK;
1665 CAResult_t CABLEClientReceivedData(const char *remoteAddress, const char *serviceUUID,
1666 void *data, uint32_t dataLength, uint32_t *sentLength)
1668 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN");
1671 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1672 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1673 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1674 VERIFY_NON_NULL_RET(gCABleClientReceiverQueue, CALEADAPTER_TAG, "gCABleClientReceiverQueue",
1677 //Add message to data queue
1678 CARemoteEndpoint_t *remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1680 if (NULL == remoteEndpoint)
1682 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1683 return CA_STATUS_FAILED;
1686 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1688 // Create bleData to add to queue
1689 CABLEData *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1692 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1693 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1694 return CA_MEMORY_ALLOC_FAILED;
1697 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1698 // Add message to send queue
1699 CAQueueingThreadAddData(gCABleClientReceiverQueue, bleData, sizeof(CABLEData));
1701 *sentLength = dataLength;
1703 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT");
1704 return CA_STATUS_OK;
1707 void CASetBleAdapterThreadPoolHandle(u_thread_pool_t handle)
1709 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1711 u_mutex_unlock(gBleAdapterThreadPoolMutex);
1712 gBleAdapterThreadPool = handle;
1713 u_mutex_unlock(gBleAdapterThreadPoolMutex);
1715 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1718 void CASetBLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
1720 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1722 u_mutex_lock(gBleAdapterReqRespCbMutex);
1724 gNetworkPacketReceivedCallback = callback;
1726 u_mutex_unlock(gBleAdapterReqRespCbMutex);
1728 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");