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 size_t local_address_len = strlen(local_address);
558 if(local_address_len >= sizeof(g_localBLEAddress) ||
559 local_address_len >= sizeof((*info)->addressInfo.BT.btMacAddress))
561 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
563 OICFree(local_address);
564 return CA_STATUS_FAILED;
567 strncpy((*info)->addressInfo.BT.btMacAddress, local_address,
568 sizeof((*info)->addressInfo.BT.btMacAddress) - 1);
569 (*info)->addressInfo.BT.btMacAddress[sizeof((*info)->addressInfo.BT.btMacAddress)-1] = '\0';
570 ca_mutex_lock(g_bleLocalAddressMutex);
571 strncpy(g_localBLEAddress, local_address, sizeof(g_localBLEAddress) - 1);
572 g_localBLEAddress[sizeof(g_localBLEAddress)-1] = '\0';
573 ca_mutex_unlock(g_bleLocalAddressMutex);
575 (*info)->type = CA_LE;
577 OICFree(local_address);
579 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
583 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
585 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
587 ca_mutex_lock(g_bleNetworkCbMutex);
588 g_networkCallback = netCallback;
589 ca_mutex_unlock(g_bleNetworkCbMutex);
590 CAResult_t res = CA_STATUS_OK;
593 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
594 if (CA_STATUS_OK != res)
596 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
601 res = CAUnSetLEAdapterStateChangedCb();
602 if (CA_STATUS_OK != res)
604 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
608 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
612 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
614 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
616 VERIFY_NON_NULL_VOID(g_localBLEAddress, NULL, "g_localBLEAddress is null");
617 CALocalConnectivity_t localEndpoint = {};
619 ca_mutex_lock(g_bleLocalAddressMutex);
620 strncpy(localEndpoint.addressInfo.BT.btMacAddress, g_localBLEAddress, strlen(g_localBLEAddress));
621 ca_mutex_unlock(g_bleLocalAddressMutex);
623 // Start a GattServer/Client if gLeServerStatus is SET
624 if (CA_LISTENING_SERVER == gLeServerStatus)
626 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattServer");
627 CAStartBleGattServer();
629 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
631 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
632 CAStartBLEGattClient();
634 gLeServerStatus = CA_SERVER_NOTSTARTED;
636 ca_mutex_lock(g_bleNetworkCbMutex);
637 if (NULL != g_networkCallback)
639 g_networkCallback(&localEndpoint, adapter_state);
643 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
645 ca_mutex_unlock(g_bleNetworkCbMutex);
647 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
650 CAResult_t CAInitBleAdapterMutex()
652 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
654 if (NULL == g_bleIsServerMutex)
656 g_bleIsServerMutex = ca_mutex_new();
657 if (NULL == g_bleIsServerMutex)
659 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
660 return CA_STATUS_FAILED;
664 if (NULL == g_bleNetworkCbMutex)
666 g_bleNetworkCbMutex = ca_mutex_new();
667 if (NULL == g_bleNetworkCbMutex)
669 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
670 return CA_STATUS_FAILED;
674 if (NULL == g_bleLocalAddressMutex)
676 g_bleLocalAddressMutex = ca_mutex_new();
677 if (NULL == g_bleLocalAddressMutex)
679 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
680 return CA_STATUS_FAILED;
684 if (NULL == g_bleAdapterThreadPoolMutex)
686 g_bleAdapterThreadPoolMutex = ca_mutex_new();
687 if (NULL == g_bleAdapterThreadPoolMutex)
689 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
690 return CA_STATUS_FAILED;
694 if (NULL == g_bleClientSendDataMutex)
696 g_bleClientSendDataMutex = ca_mutex_new();
697 if (NULL == g_bleClientSendDataMutex)
699 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
700 return CA_STATUS_FAILED;
704 if (NULL == g_bleClientReceiveDataMutex)
706 g_bleClientReceiveDataMutex = ca_mutex_new();
707 if (NULL == g_bleClientReceiveDataMutex)
709 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
710 return CA_STATUS_FAILED;
714 if (NULL == g_bleServerSendDataMutex)
716 g_bleServerSendDataMutex = ca_mutex_new();
717 if (NULL == g_bleServerSendDataMutex)
719 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
720 return CA_STATUS_FAILED;
724 if (NULL == g_bleServerReceiveDataMutex)
726 g_bleServerReceiveDataMutex = ca_mutex_new();
727 if (NULL == g_bleServerReceiveDataMutex)
729 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
730 return CA_STATUS_FAILED;
734 if (NULL == g_bleAdapterReqRespCbMutex)
736 g_bleAdapterReqRespCbMutex = ca_mutex_new();
737 if (NULL == g_bleAdapterReqRespCbMutex)
739 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
740 return CA_STATUS_FAILED;
744 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
748 void CATerminateBleAdapterMutex()
750 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
752 ca_mutex_free(g_bleIsServerMutex);
753 g_bleIsServerMutex = NULL;
755 ca_mutex_free(g_bleNetworkCbMutex);
756 g_bleNetworkCbMutex = NULL;
758 ca_mutex_free(g_bleLocalAddressMutex);
759 g_bleLocalAddressMutex = NULL;
761 ca_mutex_free(g_bleAdapterThreadPoolMutex);
762 g_bleAdapterThreadPoolMutex = NULL;
764 ca_mutex_free(g_bleClientSendDataMutex);
765 g_bleClientSendDataMutex = NULL;
767 ca_mutex_free(g_bleClientReceiveDataMutex);
768 g_bleClientReceiveDataMutex = NULL;
770 ca_mutex_free(g_bleServerSendDataMutex);
771 g_bleServerSendDataMutex = NULL;
773 ca_mutex_free(g_bleServerReceiveDataMutex);
774 g_bleServerReceiveDataMutex = NULL;
776 ca_mutex_free(g_bleAdapterReqRespCbMutex);
777 g_bleAdapterReqRespCbMutex = NULL;
779 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
782 void CAInitBleQueues()
784 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
786 CAResult_t result = CAInitBleServerQueues();
787 if (CA_STATUS_OK != result)
789 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerQueues failed");
793 result = CAInitBleClientQueues();
794 if (CA_STATUS_OK != result)
796 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
800 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
803 CAResult_t CAInitBleServerQueues()
805 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
807 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
809 CAResult_t result = CAInitBleServerSenderQueue();
810 if (CA_STATUS_OK != result)
812 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
813 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
814 return CA_STATUS_FAILED;
817 result = CAInitBleServerReceiverQueue();
818 if (CA_STATUS_OK != result)
820 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerReceiverQueue failed");
821 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
822 return CA_STATUS_FAILED;
825 g_dataReceiverHandlerState = true;
827 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
829 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
833 CAResult_t CAInitBleClientQueues()
835 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
837 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
839 CAResult_t result = CAInitBleClientSenderQueue();
840 if (CA_STATUS_OK != result)
842 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
843 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
844 return CA_STATUS_FAILED;
847 result = CAInitBleClientReceiverQueue();
848 if (CA_STATUS_OK != result)
850 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientReceiverQueue failed");
851 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
852 return CA_STATUS_FAILED;
855 g_dataReceiverHandlerState = true;
857 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
859 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
863 CAResult_t CAInitBleServerSenderQueue()
865 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
866 // Check if the message queue is already initialized
867 if (g_sendQueueHandle)
869 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
873 // Create send message queue
874 g_sendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
875 if (!g_sendQueueHandle)
877 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
878 return CA_MEMORY_ALLOC_FAILED;
881 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_sendQueueHandle, g_bleAdapterThreadPool,
882 CABLEServerSendDataThread, CALEDataDestroyer))
884 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
885 OICFree(g_sendQueueHandle);
886 g_sendQueueHandle = NULL;
887 return CA_STATUS_FAILED;
890 if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle))
892 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
893 OICFree(g_sendQueueHandle);
894 g_sendQueueHandle = NULL;
895 return CA_STATUS_FAILED;
898 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
902 CAResult_t CAInitBleClientSenderQueue()
904 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
906 if (g_bleClientSendQueueHandle)
908 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
912 // Create send message queue
913 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
914 if (!g_bleClientSendQueueHandle)
916 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
917 return CA_MEMORY_ALLOC_FAILED;
920 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle, g_bleAdapterThreadPool,
921 CABLEClientSendDataThread, CALEDataDestroyer))
923 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
924 OICFree(g_bleClientSendQueueHandle);
925 g_bleClientSendQueueHandle = NULL;
926 return CA_STATUS_FAILED;
929 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientSendQueueHandle))
931 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
932 OICFree(g_bleClientSendQueueHandle);
933 g_bleClientSendQueueHandle = NULL;
934 return CA_STATUS_FAILED;
937 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
941 CAResult_t CAInitBleServerReceiverQueue()
943 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
944 // Check if the message queue is already initialized
945 if (g_bleServerReceiverQueue)
947 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
951 // Create send message queue
952 g_bleServerReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
953 if (!g_bleServerReceiverQueue)
955 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
956 OICFree(g_sendQueueHandle);
957 return CA_MEMORY_ALLOC_FAILED;
960 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerReceiverQueue, g_bleAdapterThreadPool,
961 CABLEServerDataReceiverHandler, CALEDataDestroyer))
963 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
964 OICFree(g_bleServerReceiverQueue);
965 g_bleServerReceiverQueue = NULL;
966 return CA_STATUS_FAILED;
969 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerReceiverQueue))
971 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
972 OICFree(g_bleServerReceiverQueue);
973 g_bleServerReceiverQueue = NULL;
974 return CA_STATUS_FAILED;
977 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
981 CAResult_t CAInitBleClientReceiverQueue()
983 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
985 // Check if the message queue is already initialized
986 if (g_bleClientReceiverQueue)
988 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
992 // Create send message queue
993 g_bleClientReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
994 if (!g_bleClientReceiverQueue)
996 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
997 OICFree(g_bleClientSendQueueHandle);
998 return CA_MEMORY_ALLOC_FAILED;
1001 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientReceiverQueue, g_bleAdapterThreadPool,
1002 CABLEClientDataReceiverHandler, NULL))
1004 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
1005 OICFree(g_bleClientSendQueueHandle);
1006 OICFree(g_bleClientReceiverQueue);
1007 g_bleClientReceiverQueue = NULL;
1008 return CA_STATUS_FAILED;
1011 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientReceiverQueue))
1013 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
1014 OICFree(g_bleClientReceiverQueue);
1015 g_bleClientReceiverQueue = NULL;
1016 return CA_STATUS_FAILED;
1019 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1020 return CA_STATUS_OK;
1023 void CAStopBleQueues()
1025 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1027 ca_mutex_lock(g_bleClientSendDataMutex);
1028 if (NULL != g_bleClientSendQueueHandle)
1030 CAQueueingThreadStop(g_bleClientSendQueueHandle);
1032 ca_mutex_unlock(g_bleClientSendDataMutex);
1034 ca_mutex_lock(g_bleClientReceiveDataMutex);
1035 if (NULL != g_bleClientReceiverQueue)
1037 CAQueueingThreadStop(g_bleClientReceiverQueue);
1039 ca_mutex_unlock(g_bleClientReceiveDataMutex);
1041 ca_mutex_lock(g_bleServerSendDataMutex);
1042 if (NULL != g_sendQueueHandle)
1044 CAQueueingThreadStop(g_sendQueueHandle);
1046 ca_mutex_unlock(g_bleServerSendDataMutex);
1048 ca_mutex_lock(g_bleServerReceiveDataMutex);
1049 if (NULL != g_bleServerReceiverQueue)
1051 CAQueueingThreadStop(g_bleServerReceiverQueue);
1053 ca_mutex_unlock(g_bleServerReceiveDataMutex);
1055 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1058 void CATerminateBleQueues()
1060 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1062 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
1063 OICFree(g_bleClientSendQueueHandle);
1064 g_bleClientSendQueueHandle = NULL;
1067 CAQueueingThreadDestroy(g_bleClientReceiverQueue);
1068 OICFree(g_bleClientReceiverQueue);
1069 g_bleClientReceiverQueue = NULL;
1072 CAQueueingThreadDestroy(g_sendQueueHandle);
1073 OICFree(g_sendQueueHandle);
1074 g_sendQueueHandle = NULL;
1077 CAQueueingThreadDestroy(g_bleServerReceiverQueue);
1078 OICFree(g_bleServerReceiverQueue);
1079 g_bleServerReceiverQueue = NULL;
1081 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1083 void CABLEServerDataReceiverHandler(void *threadData)
1085 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1087 static uint32_t recvDataLen = 0;
1088 static uint32_t totalDataLen = 0;
1089 static char *defragData = NULL;
1090 static bool isHeaderAvailable = false;
1091 static CARemoteEndpoint_t *remoteEndpoint = NULL;
1093 ca_mutex_lock(g_bleServerReceiveDataMutex);
1095 if (g_dataReceiverHandlerState)
1097 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1099 CALEData_t *bleData = (CALEData_t *) threadData;
1102 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
1106 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1108 if (!isHeaderAvailable)
1110 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1111 totalDataLen = CAParseHeader((char*)bleData->data);
1113 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes", totalDataLen);
1114 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%d] bytes", bleData->dataLen);
1116 defragData = (char *) OICCalloc(totalDataLen + 1, sizeof(char));
1117 if (NULL == defragData)
1119 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1123 const char *remoteAddress = bleData->remoteEndpoint->addressInfo.LE.leMacAddress;
1124 const char *serviceUUID = bleData->remoteEndpoint->resourceUri;
1126 remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1129 memcpy(defragData + recvDataLen, bleData->data + CA_HEADER_LENGTH,
1130 bleData->dataLen - CA_HEADER_LENGTH);
1131 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1132 isHeaderAvailable = true;
1136 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1137 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1138 recvDataLen += bleData->dataLen ;
1139 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1140 totalDataLen, recvDataLen);
1142 if (totalDataLen == recvDataLen)
1144 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1145 if (NULL == g_networkPacketReceivedCallback)
1147 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1148 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1151 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1152 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1155 isHeaderAvailable = false;
1156 remoteEndpoint = NULL;
1158 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1161 if (false == g_dataReceiverHandlerState)
1163 OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1166 isHeaderAvailable = false;
1167 OICFree(defragData);
1168 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1169 ca_mutex_unlock(g_bleServerReceiveDataMutex);
1173 ca_mutex_unlock(g_bleServerReceiveDataMutex);
1174 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1177 void CABLEClientDataReceiverHandler(void *threadData)
1179 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1181 static const char *remoteAddress = NULL;
1182 static const char *serviceUUID = NULL;
1183 static uint32_t recvDataLen = 0;
1184 static uint32_t totalDataLen = 0;
1185 static char *defragData = NULL;
1186 static bool isHeaderAvailable = false;
1187 static CARemoteEndpoint_t *remoteEndpoint = NULL;
1189 ca_mutex_lock(g_bleClientReceiveDataMutex);
1191 if (g_dataReceiverHandlerState)
1193 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1195 CALEData_t *bleData = (CALEData_t *) threadData;
1198 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid wifidata!");
1202 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1204 if (!isHeaderAvailable)
1206 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1208 totalDataLen = CAParseHeader(bleData->data);
1209 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes",
1211 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received in the first packet [%d] bytes",
1214 defragData = (char *) OICMalloc(sizeof(char) * totalDataLen);
1215 if (NULL == defragData)
1217 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1221 remoteAddress = bleData->remoteEndpoint->addressInfo.LE.leMacAddress;
1222 serviceUUID = bleData->remoteEndpoint->resourceUri;
1224 remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1227 memcpy(defragData, bleData->data + CA_HEADER_LENGTH,
1228 bleData->dataLen - CA_HEADER_LENGTH);
1229 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1230 isHeaderAvailable = true;
1234 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1235 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1236 recvDataLen += bleData->dataLen ;
1237 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1238 totalDataLen, recvDataLen);
1240 if (totalDataLen == recvDataLen)
1242 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1243 if (NULL == g_networkPacketReceivedCallback)
1245 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1246 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1249 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1250 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1253 isHeaderAvailable = false;
1254 remoteEndpoint = NULL;
1256 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1259 if (false == g_dataReceiverHandlerState)
1261 OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1262 OICFree(defragData);
1263 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1264 ca_mutex_unlock(g_bleClientReceiveDataMutex);
1268 ca_mutex_unlock(g_bleClientReceiveDataMutex);
1269 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1272 void CABLEServerSendDataThread(void *threadData)
1274 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1276 CALEData_t *bleData = (CALEData_t *) threadData;
1279 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1283 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1284 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1286 int32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1288 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server total Data length with header is [%d]", totalLength);
1289 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
1290 if (NULL == dataSegment)
1292 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1297 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1298 if (CA_STATUS_OK != result )
1300 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1302 OICFree(dataSegment);
1306 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1310 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1312 length = totalLength;
1313 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1317 length = CA_SUPPORTED_BLE_MTU_SIZE;
1318 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1319 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1322 int32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1324 // Send the first segment with the header.
1325 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1327 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
1328 result = CAUpdateCharacteristicsToGattClient(
1329 bleData->remoteEndpoint->addressInfo.LE.leMacAddress, dataSegment, length);
1330 if (CA_STATUS_OK != result)
1332 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1333 OICFree(dataSegment);
1337 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1338 for (index = 1; index < iter; index++)
1340 // Send the remaining header.
1341 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1342 result = CAUpdateCharacteristicsToGattClient(
1343 bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1344 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1345 CA_SUPPORTED_BLE_MTU_SIZE);
1346 if (CA_STATUS_OK != result)
1348 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1349 "Update characteristics failed, result [%d]", result);
1350 OICFree(dataSegment);
1353 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
1354 CA_SUPPORTED_BLE_MTU_SIZE);
1357 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1358 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1360 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1361 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1362 result = CAUpdateCharacteristicsToGattClient(
1363 bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1364 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1366 if (CA_STATUS_OK != result)
1368 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1370 OICFree(dataSegment);
1373 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1378 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
1379 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
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]", length);
1387 for (index = 1; index < iter; index++)
1389 // Send the remaining header.
1390 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1391 result = CAUpdateCharacteristicsToAllGattClients(
1392 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1393 CA_SUPPORTED_BLE_MTU_SIZE);
1394 if (CA_STATUS_OK != result)
1396 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1397 OICFree(dataSegment);
1400 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
1403 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1404 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1406 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1407 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1408 result = CAUpdateCharacteristicsToAllGattClients(
1409 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1411 if (CA_STATUS_OK != result)
1413 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1415 OICFree(dataSegment);
1418 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1421 OICFree(dataSegment);
1423 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1426 void CABLEClientSendDataThread(void *threadData)
1428 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1430 CALEData_t *bleData = (CALEData_t *) threadData;
1433 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1437 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1438 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1440 uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1441 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
1442 if (NULL == dataSegment)
1444 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1449 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1450 if (CA_STATUS_OK != result )
1452 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1454 OICFree(dataSegment);
1457 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1460 uint32_t length = 0;
1461 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1463 length = totalLength;
1464 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1465 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1469 length = CA_SUPPORTED_BLE_MTU_SIZE;
1470 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1471 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1472 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1475 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1477 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1479 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1480 // Send the first segment with the header.
1481 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1486 if (CA_STATUS_OK != result)
1488 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1489 OICFree(dataSegment);
1493 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1494 for (index = 1; index < iter; index++)
1496 // Send the remaining header.
1497 result = CAUpdateCharacteristicsToGattServer(
1498 bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1499 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1500 CA_SUPPORTED_BLE_MTU_SIZE,
1502 if (CA_STATUS_OK != result)
1504 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1506 OICFree(dataSegment);
1509 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1510 CA_SUPPORTED_BLE_MTU_SIZE);
1513 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1514 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1516 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1517 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1518 result = CAUpdateCharacteristicsToGattServer(
1519 bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1520 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1524 if (CA_STATUS_OK != result)
1526 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1528 OICFree(dataSegment);
1531 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1536 //Sending Mulitcast Data
1537 // Send the first segment with the header.
1538 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1539 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1540 if (CA_STATUS_OK != result)
1542 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed (all), result [%d]", result);
1543 OICFree(dataSegment);
1546 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1547 // Send the remaining header.
1548 for (index = 1; index < iter; index++)
1550 result = CAUpdateCharacteristicsToAllGattServers(
1551 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1552 CA_SUPPORTED_BLE_MTU_SIZE);
1553 if (CA_STATUS_OK != result)
1555 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]", result);
1556 OICFree(dataSegment);
1559 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
1562 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1563 if ( remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1565 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1566 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1567 result = CAUpdateCharacteristicsToAllGattServers(
1568 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1570 if (CA_STATUS_OK != result)
1572 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]", result);
1573 OICFree(dataSegment);
1576 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1581 OICFree(dataSegment);
1583 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1586 CALEData_t *CACreateBLEData(const CARemoteEndpoint_t *remoteEndpoint, const void *data,
1587 uint32_t dataLength)
1589 CALEData_t *bleData = (CALEData_t *) OICMalloc(sizeof(CALEData_t));
1592 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1596 bleData->remoteEndpoint = CAAdapterCopyRemoteEndpoint(remoteEndpoint);
1597 bleData->data = (void *)OICCalloc(dataLength + 1, 1);
1598 if (NULL == bleData->data)
1600 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1601 CAFreeBLEData(bleData);
1604 memcpy(bleData->data, data, dataLength);
1605 bleData->dataLen = dataLength;
1610 void CAFreeBLEData(CALEData_t *bleData)
1612 VERIFY_NON_NULL_VOID(bleData, NULL, "Param bleData is NULL");
1614 CAAdapterFreeRemoteEndpoint(bleData->remoteEndpoint);
1615 OICFree(bleData->data);
1619 void CALEDataDestroyer(void *data, uint32_t size)
1621 CALEData_t *ledata = (CALEData_t *) data;
1623 CAFreeBLEData(ledata);
1627 CAResult_t CABLEClientSendData(const CARemoteEndpoint_t *remoteEndpoint,
1631 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1633 VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1635 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
1636 "g_bleClientSendQueueHandle is NULL",
1638 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
1639 "g_bleClientSendDataMutex is NULL",
1642 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
1645 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1647 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1650 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1651 return CA_MEMORY_ALLOC_FAILED;
1653 // Add message to send queue
1654 ca_mutex_lock(g_bleClientSendDataMutex);
1655 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
1656 ca_mutex_unlock(g_bleClientSendDataMutex);
1658 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1659 return CA_STATUS_OK;
1663 CAResult_t CABLEServerSendData(const CARemoteEndpoint_t *remoteEndpoint,
1667 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1669 VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1671 VERIFY_NON_NULL_RET(g_sendQueueHandle, CALEADAPTER_TAG,
1672 "BleClientReceiverQueue is NULL",
1674 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
1675 "BleClientSendDataMutex is NULL",
1678 VERIFY_NON_NULL_RET(g_sendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
1681 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1683 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1686 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1687 return CA_MEMORY_ALLOC_FAILED;
1689 // Add message to send queue
1690 ca_mutex_lock(g_bleServerSendDataMutex);
1691 CAQueueingThreadAddData(g_sendQueueHandle, bleData, sizeof(CALEData_t));
1692 ca_mutex_unlock(g_bleServerSendDataMutex);
1694 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1695 return CA_STATUS_OK;
1698 CAResult_t CABLEServerReceivedData(const char *remoteAddress, const char *serviceUUID,
1699 const void *data, uint32_t dataLength, uint32_t *sentLength)
1701 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1704 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1705 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1706 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1707 VERIFY_NON_NULL_RET(g_bleServerReceiverQueue, CALEADAPTER_TAG, "g_bleServerReceiverQueue",
1710 //Add message to data queue
1711 CARemoteEndpoint_t *remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1713 if (NULL == remoteEndpoint)
1715 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1716 return CA_STATUS_FAILED;
1719 // Create bleData to add to queue
1720 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1722 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1725 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1726 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1727 return CA_MEMORY_ALLOC_FAILED;
1730 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1731 // Add message to send queue
1732 CAQueueingThreadAddData(g_bleServerReceiverQueue, bleData, sizeof(CALEData_t));
1734 *sentLength = dataLength;
1736 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1737 return CA_STATUS_OK;
1740 CAResult_t CABLEClientReceivedData(const char *remoteAddress, const char *serviceUUID,
1741 const void *data, uint32_t dataLength, uint32_t *sentLength)
1743 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1746 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1747 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1748 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1749 VERIFY_NON_NULL_RET(g_bleClientReceiverQueue, CALEADAPTER_TAG, "g_bleClientReceiverQueue",
1752 //Add message to data queue
1753 CARemoteEndpoint_t *remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1755 if (NULL == remoteEndpoint)
1757 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1758 return CA_STATUS_FAILED;
1761 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1763 // Create bleData to add to queue
1764 CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1767 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1768 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1769 return CA_MEMORY_ALLOC_FAILED;
1772 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1773 // Add message to send queue
1774 CAQueueingThreadAddData(g_bleClientReceiverQueue, bleData, sizeof(CALEData_t));
1776 *sentLength = dataLength;
1778 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1779 return CA_STATUS_OK;
1782 void CASetBleAdapterThreadPoolHandle(ca_thread_pool_t handle)
1784 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1786 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1787 g_bleAdapterThreadPool = handle;
1788 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1790 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1793 void CASetBLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
1795 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1797 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1799 g_networkPacketReceivedCallback = callback;
1801 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1803 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");