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();
227 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
228 CANetworkPacketReceivedCallback reqRespCallback,
229 CANetworkChangeCallback netCallback,
230 u_thread_pool_t handle)
232 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
235 VERIFY_NON_NULL(registerCallback, NULL, "RegisterConnectivity callback is null");
236 VERIFY_NON_NULL(reqRespCallback, NULL, "PacketReceived Callback is null");
237 VERIFY_NON_NULL(netCallback, NULL, "NetworkChange Callback is null");
239 CAResult_t result = CAInitBleAdapterMutex();
240 if (CA_STATUS_OK != result)
242 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
243 return CA_STATUS_FAILED;
246 result = CAInitializeLENwkMonitor();
247 if (CA_STATUS_OK != result)
249 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "CAInitializeLENwkMonitor() failed");
250 return CA_STATUS_FAILED;
253 CAInitializeLEAdapter();
255 CASetBleServerThreadPoolHandle(handle);
256 CASetBleClientThreadPoolHandle(handle);
257 CASetBleAdapterThreadPoolHandle(handle);
258 CASetBLEReqRespServerCallback(CABLEServerReceivedData);
259 CASetBLEReqRespClientCallback(CABLEClientReceivedData);
260 CASetBLEReqRespAdapterCallback(reqRespCallback);
262 CALERegisterNetworkNotifications(netCallback);
264 CAConnectivityHandler_t connHandler;
265 connHandler.startAdapter = NULL;
266 connHandler.stopAdapter = CAStopLE;
267 connHandler.startListenServer = CAStartLEListeningServer;
268 connHandler.startDiscoverServer = CAStartLEDiscoveryServer;
269 connHandler.sendData = CASendLEUnicastData;
270 connHandler.sendDataToAll = CASendLEMulticastData;
271 connHandler.GetnetInfo = CAGetLEInterfaceInformation;
272 connHandler.readData = CAReadLEData;
273 connHandler.terminate = CATerminateLE;
274 registerCallback(connHandler, CA_LE);
276 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
281 CAResult_t CAStopLE()
283 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
287 u_mutex_lock(gBleIsServerMutex);
288 if (CA_TRUE == gIsServer)
290 CAStopBleGattServer();
294 CAStopBLEGattClient();
296 u_mutex_unlock(gBleIsServerMutex);
298 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
305 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
307 CASetBLEReqRespServerCallback(NULL);
308 CASetBLEReqRespClientCallback(NULL);
309 CALERegisterNetworkNotifications(NULL);
310 CASetBLEReqRespAdapterCallback(NULL);
311 CATerminateLENwkMonitor();
313 u_mutex_lock(gBleIsServerMutex);
314 if (CA_TRUE == gIsServer)
316 CATerminateBleGattServer();
320 CATerminateBLEGattClient();
322 u_mutex_unlock(gBleIsServerMutex);
324 CATerminateBleQueues();
326 CATermiateBleAdapterMutex();
328 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
332 CAResult_t CAStartLEListeningServer()
334 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
336 CAResult_t result = CAInitBleServerQueues();
337 if (CA_STATUS_OK != result)
339 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
340 return CA_STATUS_FAILED;
343 result = CAGetLEAdapterState();
344 if (CA_ADAPTER_NOT_ENABLED == result)
346 gLeServerStatus = CA_LISTENING_SERVER;
347 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
351 if (CA_STATUS_FAILED == result)
353 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
354 return CA_STATUS_FAILED;
357 CAStartBleGattServer();
359 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
360 u_mutex_lock(gBleIsServerMutex);
362 u_mutex_unlock(gBleIsServerMutex);
366 CAResult_t CAStartLEDiscoveryServer()
368 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
370 CAResult_t result = CAInitBleClientQueues();
371 if (CA_STATUS_OK != result)
373 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
374 return CA_STATUS_FAILED;
377 result = CAGetLEAdapterState();
378 if (CA_ADAPTER_NOT_ENABLED == result)
380 gLeServerStatus = CA_DISCOVERY_SERVER;
381 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
385 if (CA_STATUS_FAILED == result)
387 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
388 return CA_STATUS_FAILED;
391 CAStartBLEGattClient();
393 u_mutex_lock(gBleIsServerMutex);
394 gIsServer = CA_FALSE;
395 u_mutex_unlock(gBleIsServerMutex);
397 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
401 CAResult_t CAStartLENotifyServer()
403 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
405 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
409 uint32_t CASendLENotification(const CARemoteEndpoint_t *endpoint, void *data, uint32_t dataLen)
411 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
413 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
417 CAResult_t CAReadLEData()
419 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
421 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
425 uint32_t CASendLEUnicastData(const CARemoteEndpoint_t *endpoint, void *data, uint32_t dataLen)
427 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
430 VERIFY_NON_NULL(endpoint, NULL, "Remote endpoint is null");
431 VERIFY_NON_NULL(data, NULL, "Data is null");
433 CAResult_t result = CA_STATUS_FAILED;
435 u_mutex_lock(gBleIsServerMutex);
436 if (CA_TRUE == gIsServer)
438 result = CABLEServerSendData(endpoint, data, dataLen);
439 if (CA_STATUS_OK != result)
441 OIC_LOG(ERROR, CALEADAPTER_TAG,
442 "[SendLEUnicastData] CABleServerSenderQueueEnqueueMessage failed \n");
443 u_mutex_unlock(gBleIsServerMutex);
449 result = CABLEClientSendData(endpoint, data, dataLen);
450 if (CA_STATUS_OK != result)
452 OIC_LOG(ERROR, CALEADAPTER_TAG,
453 "[SendLEUnicastData] CABleClientSenderQueueEnqueueMessage failed \n");
454 u_mutex_unlock(gBleIsServerMutex);
458 u_mutex_unlock(gBleIsServerMutex);
460 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
464 uint32_t CASendLEMulticastData(void *data, uint32_t dataLen)
466 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
469 VERIFY_NON_NULL(data, NULL, "Data is null");
473 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
477 CAResult_t result = CA_STATUS_FAILED;
479 u_mutex_lock(gBleIsServerMutex);
480 if (CA_TRUE == gIsServer)
482 result = CABLEServerSendData(NULL, data, dataLen);
483 if (CA_STATUS_OK != result)
485 OIC_LOG(ERROR, CALEADAPTER_TAG,
486 "[SendLEMulticastDataToAll] CABleServerSenderQueueEnqueueMessage failed" );
487 u_mutex_unlock(gBleIsServerMutex);
493 result = CABLEClientSendData(NULL, data, dataLen);
494 if (CA_STATUS_OK != result)
496 OIC_LOG(ERROR, CALEADAPTER_TAG,
497 "[SendLEMulticastDataToAll] CABleClientSenderQueueEnqueueMessage failed" );
498 u_mutex_unlock(gBleIsServerMutex);
502 u_mutex_unlock(gBleIsServerMutex);
504 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
508 CAResult_t CAGetLEInterfaceInformation(CALocalConnectivity_t **info, uint32_t *size)
510 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
512 VERIFY_NON_NULL(info, NULL, "CALocalConnectivity info is null");
514 char *local_address = NULL;
516 CAGetLEAddress(&local_address);
517 if (NULL == local_address)
519 OIC_LOG(ERROR, CALEADAPTER_TAG, "Get local bt adapter address failed");
520 return CA_STATUS_FAILED;
524 (*info) = (CALocalConnectivity_t *) OICMalloc(sizeof(CALocalConnectivity_t));
527 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
528 return CA_STATUS_FAILED;
530 memset((*info), 0x0, sizeof(CALocalConnectivity_t));
532 strncpy((*info)->addressInfo.BT.btMacAddress, local_address, strlen(local_address));
533 u_mutex_lock(gBleLocalAddressMutex);
534 strncpy(gLocalBLEAddress, local_address, sizeof(gLocalBLEAddress));
535 u_mutex_unlock(gBleLocalAddressMutex);
537 (*info)->type = CA_LE;
539 OICFree(local_address);
541 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
545 int32_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
547 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
549 u_mutex_lock(gBleNetworkCbMutex);
550 gNetworkCallback = netCallback;
551 u_mutex_unlock(gBleNetworkCbMutex);
555 CAResult_t res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
556 if (CA_STATUS_OK != res)
558 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
563 CAResult_t res = CAUnSetLEAdapterStateChangedCb();
564 if (CA_STATUS_OK != res)
566 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
570 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
574 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
576 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
578 CALocalConnectivity_t localEndpoint;
580 u_mutex_lock(gBleLocalAddressMutex);
581 strncpy(localEndpoint.addressInfo.BT.btMacAddress, gLocalBLEAddress, strlen(gLocalBLEAddress));
582 u_mutex_unlock(gBleLocalAddressMutex);
584 // Start a GattServer/Client if gLeServerStatus is SET
585 if (CA_LISTENING_SERVER == gLeServerStatus)
587 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattServer");
588 CAStartBleGattServer();
590 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
592 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
593 CAStartBLEGattClient();
595 gLeServerStatus = CA_SERVER_NOTSTARTED;
597 u_mutex_lock(gBleNetworkCbMutex);
598 if (NULL != gNetworkCallback)
600 gNetworkCallback(&localEndpoint, adapter_state);
604 OIC_LOG(ERROR, CALEADAPTER_TAG, "gNetworkCallback is NULL");
606 u_mutex_unlock(gBleNetworkCbMutex);
608 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
612 CAResult_t CAInitBleAdapterMutex()
614 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
617 if (NULL == gBleIsServerMutex)
619 gBleIsServerMutex = u_mutex_new();
620 if (NULL == gBleIsServerMutex)
622 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
623 return CA_STATUS_FAILED;
627 if (NULL == gBleNetworkCbMutex)
629 gBleNetworkCbMutex = u_mutex_new();
630 if (NULL == gBleNetworkCbMutex)
632 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
633 return CA_STATUS_FAILED;
637 if (NULL == gBleLocalAddressMutex)
639 gBleLocalAddressMutex = u_mutex_new();
640 if (NULL == gBleLocalAddressMutex)
642 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
643 return CA_STATUS_FAILED;
647 if (NULL == gBleAdapterThreadPoolMutex)
649 gBleAdapterThreadPoolMutex = u_mutex_new();
650 if (NULL == gBleAdapterThreadPoolMutex)
652 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
653 return CA_STATUS_FAILED;
657 if (NULL == gBleClientSendDataMutex)
659 gBleClientSendDataMutex = u_mutex_new();
660 if (NULL == gBleClientSendDataMutex)
662 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
663 return CA_STATUS_FAILED;
667 if (NULL == gBleClientReceiveDataMutex)
669 gBleClientReceiveDataMutex = u_mutex_new();
670 if (NULL == gBleClientReceiveDataMutex)
672 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
673 return CA_STATUS_FAILED;
677 if (NULL == gBleServerSendDataMutex)
679 gBleServerSendDataMutex = u_mutex_new();
680 if (NULL == gBleServerSendDataMutex)
682 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
683 return CA_STATUS_FAILED;
687 if (NULL == gBleServerReceiveDataMutex)
689 gBleServerReceiveDataMutex = u_mutex_new();
690 if (NULL == gBleServerReceiveDataMutex)
692 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
693 return CA_STATUS_FAILED;
697 if (NULL == gBleAdapterReqRespCbMutex)
699 gBleAdapterReqRespCbMutex = u_mutex_new();
700 if (NULL == gBleAdapterReqRespCbMutex)
702 OIC_LOG(ERROR, CALEADAPTER_TAG, "u_mutex_new failed");
703 return CA_STATUS_FAILED;
707 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
711 void CATermiateBleAdapterMutex()
713 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
715 u_mutex_free(gBleIsServerMutex);
716 gBleIsServerMutex = NULL;
718 u_mutex_free(gBleNetworkCbMutex);
719 gBleNetworkCbMutex = NULL;
721 u_mutex_free(gBleLocalAddressMutex);
722 gBleLocalAddressMutex = NULL;
724 u_mutex_free(gBleAdapterThreadPoolMutex);
725 gBleAdapterThreadPoolMutex = NULL;
727 u_mutex_free(gBleClientSendDataMutex);
728 gBleClientSendDataMutex = NULL;
730 u_mutex_free(gBleClientReceiveDataMutex);
731 gBleClientReceiveDataMutex = NULL;
733 u_mutex_free(gBleServerSendDataMutex);
734 gBleServerSendDataMutex = NULL;
736 u_mutex_free(gBleServerReceiveDataMutex);
737 gBleServerReceiveDataMutex = NULL;
739 u_mutex_free(gBleAdapterReqRespCbMutex);
740 gBleAdapterReqRespCbMutex = NULL;
742 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
746 void CAInitBleQueues()
748 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
750 CAResult_t result = CAInitBleServerQueues();
751 if (CA_STATUS_OK != result)
753 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
757 result = CAInitBleClientQueues();
758 if (CA_STATUS_OK != result)
760 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
764 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
768 CAResult_t CAInitBleServerQueues()
770 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
772 u_mutex_lock(gBleAdapterThreadPoolMutex);
774 CAResult_t result = CAInitBleServerSenderQueue();
775 if (CA_STATUS_OK != result)
777 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
778 u_mutex_unlock(gBleAdapterThreadPoolMutex);
779 return CA_STATUS_FAILED;
782 result = CAInitBleServerReceiverQueue();
783 if (CA_STATUS_OK != result)
785 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
786 u_mutex_unlock(gBleAdapterThreadPoolMutex);
787 return CA_STATUS_FAILED;
790 gDataReceiverHandlerState = true;
792 u_mutex_unlock(gBleAdapterThreadPoolMutex);
794 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
798 CAResult_t CAInitBleClientQueues()
800 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
802 u_mutex_lock(gBleAdapterThreadPoolMutex);
804 CAResult_t result = CAInitBleClientSenderQueue();
805 if (CA_STATUS_OK != result)
807 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
808 u_mutex_unlock(gBleAdapterThreadPoolMutex);
809 return CA_STATUS_FAILED;
812 result = CAInitBleClientReceiverQueue();
813 if (CA_STATUS_OK != result)
815 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
816 u_mutex_unlock(gBleAdapterThreadPoolMutex);
817 return CA_STATUS_FAILED;
820 gDataReceiverHandlerState = true;
822 u_mutex_unlock(gBleAdapterThreadPoolMutex);
824 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
828 CAResult_t CAInitBleServerSenderQueue()
830 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
831 // Check if the message queue is already initialized
832 if (gSendQueueHandle)
834 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
838 // Create send message queue
839 gSendQueueHandle = OICMalloc(sizeof(CAQueueingThread_t));
840 if (!gSendQueueHandle)
842 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
843 return CA_MEMORY_ALLOC_FAILED;
846 if (CA_STATUS_OK != CAQueueingThreadInitialize(gSendQueueHandle, gBleAdapterThreadPool,
847 CABLEServerSendDataThread, NULL))
849 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
850 OICFree(gSendQueueHandle);
851 gSendQueueHandle = NULL;
852 return CA_STATUS_FAILED;
855 if (CA_STATUS_OK != CAQueueingThreadStart(gSendQueueHandle))
857 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "u_thread_pool_add_task failed ");
858 OICFree(gSendQueueHandle);
859 gSendQueueHandle = NULL;
860 return CA_STATUS_FAILED;
863 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
867 CAResult_t CAInitBleClientSenderQueue()
869 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
871 if (gBLEClientSendQueueHandle)
873 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
877 // Create send message queue
878 gBLEClientSendQueueHandle = OICMalloc(sizeof(CAQueueingThread_t));
879 if (!gBLEClientSendQueueHandle)
881 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
882 return CA_MEMORY_ALLOC_FAILED;
885 if (CA_STATUS_OK != CAQueueingThreadInitialize(gBLEClientSendQueueHandle, gBleAdapterThreadPool,
886 CABLEClientSendDataThread, NULL))
888 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
889 OICFree(gBLEClientSendQueueHandle);
890 gBLEClientSendQueueHandle = NULL;
891 return CA_STATUS_FAILED;
894 if (CA_STATUS_OK != CAQueueingThreadStart(gBLEClientSendQueueHandle))
896 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "u_thread_pool_add_task failed ");
897 OICFree(gBLEClientSendQueueHandle);
898 gBLEClientSendQueueHandle = NULL;
899 return CA_STATUS_FAILED;
902 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
906 CAResult_t CAInitBleServerReceiverQueue()
908 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
909 // Check if the message queue is already initialized
910 if (gCABleServerReceiverQueue)
912 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
916 // Create send message queue
917 gCABleServerReceiverQueue = OICMalloc(sizeof(CAQueueingThread_t));
918 if (!gCABleServerReceiverQueue)
920 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
921 OICFree(gSendQueueHandle);
922 return CA_MEMORY_ALLOC_FAILED;
925 if (CA_STATUS_OK != CAQueueingThreadInitialize(gCABleServerReceiverQueue, gBleAdapterThreadPool,
926 CABLEServerDataReceiverHandler, NULL))
928 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
929 OICFree(gCABleServerReceiverQueue);
930 gCABleServerReceiverQueue = NULL;
931 return CA_STATUS_FAILED;
934 if (CA_STATUS_OK != CAQueueingThreadStart(gCABleServerReceiverQueue))
936 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "u_thread_pool_add_task failed ");
937 OICFree(gCABleServerReceiverQueue);
938 gCABleServerReceiverQueue = NULL;
939 return CA_STATUS_FAILED;
942 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
946 CAResult_t CAInitBleClientReceiverQueue()
948 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
950 // Check if the message queue is already initialized
951 if (gCABleClientReceiverQueue)
953 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
957 // Create send message queue
958 gCABleClientReceiverQueue = OICMalloc(sizeof(CAQueueingThread_t));
959 if (!gCABleClientReceiverQueue)
961 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
962 OICFree(gBLEClientSendQueueHandle);
963 return CA_MEMORY_ALLOC_FAILED;
966 if (CA_STATUS_OK != CAQueueingThreadInitialize(gCABleClientReceiverQueue, gBleAdapterThreadPool,
967 CABLEClientDataReceiverHandler, NULL))
969 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
970 OICFree(gBLEClientSendQueueHandle);
971 OICFree(gCABleClientReceiverQueue);
972 gCABleClientReceiverQueue = NULL;
973 return CA_STATUS_FAILED;
976 if (CA_STATUS_OK != CAQueueingThreadStart(gCABleClientReceiverQueue))
978 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "u_thread_pool_add_task failed ");
979 OICFree(gCABleClientReceiverQueue);
980 gCABleClientReceiverQueue = NULL;
981 return CA_STATUS_FAILED;
984 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
988 void CAStopBleQueues()
990 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
992 u_mutex_lock(gBleClientSendDataMutex);
993 if (NULL != gBLEClientSendQueueHandle)
995 CAQueueingThreadStop(gBLEClientSendQueueHandle);
997 u_mutex_unlock(gBleClientSendDataMutex);
999 u_mutex_lock(gBleClientReceiveDataMutex);
1000 if (NULL != gCABleClientReceiverQueue)
1002 CAQueueingThreadStop(gCABleClientReceiverQueue);
1004 u_mutex_unlock(gBleClientReceiveDataMutex);
1006 u_mutex_lock(gBleServerSendDataMutex);
1007 if (NULL != gSendQueueHandle)
1009 CAQueueingThreadStop(gSendQueueHandle);
1011 u_mutex_unlock(gBleServerSendDataMutex);
1013 u_mutex_lock(gBleServerReceiveDataMutex);
1014 if (NULL != gCABleServerReceiverQueue)
1016 CAQueueingThreadStop(gCABleServerReceiverQueue);
1018 u_mutex_unlock(gBleServerReceiveDataMutex);
1020 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1023 void CATerminateBleQueues()
1025 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1027 CAQueueingThreadDestroy(gBLEClientSendQueueHandle);
1028 OICFree(gBLEClientSendQueueHandle);
1029 gBLEClientSendQueueHandle = NULL;
1032 CAQueueingThreadDestroy(gCABleClientReceiverQueue);
1033 OICFree(gCABleClientReceiverQueue);
1034 gCABleClientReceiverQueue = NULL;
1037 CAQueueingThreadDestroy(gSendQueueHandle);
1038 OICFree(gSendQueueHandle);
1039 gSendQueueHandle = NULL;
1042 CAQueueingThreadDestroy(gCABleServerReceiverQueue);
1043 OICFree(gCABleServerReceiverQueue);
1044 gCABleServerReceiverQueue = NULL;
1046 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1048 void CABLEServerDataReceiverHandler(void *threadData)
1050 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN");
1052 // CAAdapterMessage_t *message = NULL;
1053 static const char *remoteAddress = NULL;
1054 static const char *serviceUUID = NULL;
1055 static uint32_t recvDataLen = 0;
1056 static uint32_t totalDataLen = 0;
1057 static char *defragData = NULL;
1058 static bool isHeaderAvailable = false;
1059 static CARemoteEndpoint_t *remoteEndpoint = NULL;
1061 u_mutex_lock(gBleClientReceiveDataMutex);
1063 if (gDataReceiverHandlerState)
1065 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1067 CABLEData *bleData = (CABLEData *) threadData;
1070 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid wifidata!");
1074 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1076 if (!isHeaderAvailable)
1078 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1079 char *header = (char *) OICMalloc(sizeof(char) * CA_HEADER_LENGTH);
1080 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "header is NULL");
1082 memcpy(header, bleData->data, CA_HEADER_LENGTH);
1083 totalDataLen = CAParseHeader(header);
1084 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes", totalDataLen);
1085 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%d] bytes", bleData->dataLen);
1086 defragData = (char *) OICMalloc(sizeof(char) * totalDataLen);
1087 if (NULL == defragData)
1089 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1096 remoteAddress = bleData->remoteEndpoint->addressInfo.LE.leMacAddress;
1097 serviceUUID = bleData->remoteEndpoint->resourceUri;
1099 remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1102 memcpy(defragData + recvDataLen, bleData->data + CA_HEADER_LENGTH,
1103 bleData->dataLen - CA_HEADER_LENGTH);
1104 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1105 isHeaderAvailable = true;
1109 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1110 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1111 recvDataLen += bleData->dataLen ;
1112 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1113 totalDataLen, recvDataLen);
1115 CAFreeBLEData(bleData);
1116 if (totalDataLen == recvDataLen)
1118 u_mutex_lock(gBleAdapterReqRespCbMutex);
1119 if (NULL == gNetworkPacketReceivedCallback)
1121 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1122 u_mutex_unlock(gBleAdapterReqRespCbMutex);
1125 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1126 gNetworkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1129 isHeaderAvailable = false;
1130 u_mutex_unlock(gBleAdapterReqRespCbMutex);
1133 if (false == gDataReceiverHandlerState)
1135 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1136 OICFree(defragData);
1137 OICFree(remoteEndpoint);
1138 u_mutex_unlock(gBleClientReceiveDataMutex);
1142 u_mutex_unlock(gBleClientReceiveDataMutex);
1143 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT");
1146 void CABLEClientDataReceiverHandler(void *threadData)
1148 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN");
1150 static const char *remoteAddress = NULL;
1151 static const char *serviceUUID = NULL;
1152 static uint32_t recvDataLen = 0;
1153 static uint32_t totalDataLen = 0;
1154 static char *defragData = NULL;
1155 static bool isHeaderAvailable = false;
1156 static CARemoteEndpoint_t *remoteEndpoint = NULL;
1158 u_mutex_lock(gBleClientReceiveDataMutex);
1160 if (gDataReceiverHandlerState)
1162 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1164 CABLEData *bleData = (CABLEData *) threadData;
1167 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid wifidata!");
1171 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
1173 if (!isHeaderAvailable)
1175 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1176 char *header = (char *) OICMalloc(sizeof(char) * CA_HEADER_LENGTH);
1177 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "header is NULL");
1179 memcpy(header, bleData->data, CA_HEADER_LENGTH);
1180 totalDataLen = CAParseHeader(header);
1181 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes", totalDataLen);
1182 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received in the first packet [%d] bytes", bleData->dataLen);
1183 defragData = (char *) OICMalloc(sizeof(char) * totalDataLen);
1184 if (NULL == defragData)
1186 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1193 remoteAddress = bleData->remoteEndpoint->addressInfo.LE.leMacAddress;
1194 serviceUUID = bleData->remoteEndpoint->resourceUri;
1196 remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1199 memcpy(defragData , bleData->data + CA_HEADER_LENGTH,
1200 bleData->dataLen - CA_HEADER_LENGTH);
1201 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
1202 isHeaderAvailable = true;
1206 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1207 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
1208 recvDataLen += bleData->dataLen ;
1209 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1210 totalDataLen, recvDataLen);
1212 CAFreeBLEData(bleData);
1213 if (totalDataLen == recvDataLen)
1215 u_mutex_lock(gBleAdapterReqRespCbMutex);
1216 if (NULL == gNetworkPacketReceivedCallback)
1218 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1219 u_mutex_unlock(gBleAdapterReqRespCbMutex);
1222 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1223 gNetworkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
1226 isHeaderAvailable = false;
1227 u_mutex_unlock(gBleAdapterReqRespCbMutex);
1230 if (false == gDataReceiverHandlerState)
1232 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
1233 OICFree(defragData);
1234 OICFree(remoteEndpoint);
1235 u_mutex_unlock(gBleClientReceiveDataMutex);
1239 u_mutex_unlock(gBleClientReceiveDataMutex);
1240 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT");
1243 void CABLEServerSendDataThread(void *threadData)
1245 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1247 CABLEData *bleData = (CABLEData *) threadData;
1250 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1253 char *header = (char *) OICMalloc(sizeof(char) * CA_HEADER_LENGTH);
1254 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1256 char *dataSegment = (char *) OICMalloc(sizeof(char) * bleData->dataLen + CA_HEADER_LENGTH);
1257 if (NULL == dataSegment)
1259 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1264 memset(header, 0x0, sizeof(char) * CA_HEADER_LENGTH );
1265 memset(dataSegment, 0x0, sizeof(char) * bleData->dataLen );
1267 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1268 if (CA_STATUS_OK != result )
1270 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1272 OICFree(dataSegment);
1276 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1279 if (CA_SUPPORTED_BLE_MTU_SIZE >= bleData->dataLen)
1281 length = bleData->dataLen + CA_HEADER_LENGTH;
1282 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1283 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1287 length = CA_SUPPORTED_BLE_MTU_SIZE;
1288 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1289 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1290 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1293 int32_t iter = bleData->dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
1295 u_mutex_lock(gBleServerSendDataMutex);
1296 // Send the first segment with the header.
1297 result = CAUpdateCharacteristicsInGattServer(dataSegment, length);
1298 for (index = 1; index < iter; index++)
1300 // Send the remaining header.
1301 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1302 result = CAUpdateCharacteristicsInGattServer(
1303 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1304 CA_SUPPORTED_BLE_MTU_SIZE);
1306 if (bleData->dataLen / CA_SUPPORTED_BLE_MTU_SIZE)
1308 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1309 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1310 result = CAUpdateCharacteristicsInGattServer(
1311 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1312 bleData->dataLen % CA_SUPPORTED_BLE_MTU_SIZE + CA_HEADER_LENGTH);
1315 OICFree(bleData->remoteEndpoint);
1317 u_mutex_unlock(gBleServerSendDataMutex); // TODO: this mutex required ?
1319 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1324 void CABLEClientSendDataThread(void *threadData)
1326 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1328 CABLEData *bleData = (CABLEData *) threadData;
1331 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1334 char *header = (char *) OICMalloc(sizeof(char) * CA_HEADER_LENGTH);
1335 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1337 char *dataSegment = (char *) OICMalloc(sizeof(char) * bleData->dataLen + CA_HEADER_LENGTH + 1);
1338 if (NULL == dataSegment)
1340 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1345 memset(header, 0x0, sizeof(char) * CA_HEADER_LENGTH );
1346 memset(dataSegment, 0x0, sizeof(char) * bleData->dataLen + CA_HEADER_LENGTH + 1);
1348 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1349 if (CA_STATUS_OK != result )
1351 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1353 OICFree(dataSegment);
1356 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1359 if (CA_SUPPORTED_BLE_MTU_SIZE >= bleData->dataLen)
1361 length = bleData->dataLen + CA_HEADER_LENGTH;
1362 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1363 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1367 length = CA_SUPPORTED_BLE_MTU_SIZE;
1368 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1369 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1370 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1373 int32_t iter = bleData->dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
1375 u_mutex_lock(gBleClientSendDataMutex);
1376 if (NULL != bleData->remoteEndpoint)
1378 // Send the first segment with the header.
1379 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1383 for (index = 1; index < iter; index++)
1385 // Send the remaining header.
1386 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1387 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1388 CA_SUPPORTED_BLE_MTU_SIZE,
1391 if (bleData->dataLen / CA_SUPPORTED_BLE_MTU_SIZE)
1393 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1394 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1395 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addressInfo.LE.leMacAddress,
1396 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1397 bleData->dataLen % CA_SUPPORTED_BLE_MTU_SIZE + CA_HEADER_LENGTH,
1403 // Send the first segment with the header.
1404 result = CAUpdateCharacteristicsToAllGattServers(dataSegment + (index * length),
1406 // Send the remaining header.
1407 for (index = 1; index < iter; index++)
1409 result = CAUpdateCharacteristicsToAllGattServers(
1410 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1411 CA_SUPPORTED_BLE_MTU_SIZE);
1413 if (bleData->dataLen / CA_SUPPORTED_BLE_MTU_SIZE)
1415 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1416 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1417 result = CAUpdateCharacteristicsToAllGattServers(
1418 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1419 bleData->dataLen % CA_SUPPORTED_BLE_MTU_SIZE + CA_HEADER_LENGTH);
1423 OICFree(bleData->remoteEndpoint);
1425 u_mutex_unlock(gBleClientSendDataMutex);
1427 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1431 CABLEData *CACreateBLEData(const CARemoteEndpoint_t *remoteEndpoint, void *data,
1432 uint32_t dataLength)
1434 CABLEData *bleData = (CABLEData *) OICMalloc(sizeof(CABLEData));
1437 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1441 bleData->remoteEndpoint = CAAdapterCopyRemoteEndpoint(remoteEndpoint);
1442 bleData->data = (void *)OICMalloc(dataLength);
1443 if (NULL == bleData->data)
1445 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1446 CAFreeBLEData(bleData);
1449 memcpy(bleData->data, data, dataLength);
1450 bleData->dataLen = dataLength;
1455 void CAFreeBLEData(CABLEData *bleData)
1460 CAAdapterFreeRemoteEndpoint(bleData->remoteEndpoint);
1461 OICFree(bleData->data);
1465 CAResult_t CABLEClientSendData(const CARemoteEndpoint_t *remoteEndpoint,
1469 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN ");
1471 VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1473 VERIFY_NON_NULL_RET(gBLEClientSendQueueHandle, CALEADAPTER_TAG,
1474 "gBLEClientSendQueueHandle is NULL",
1476 VERIFY_NON_NULL_RET(gBleClientSendDataMutex, CALEADAPTER_TAG,
1477 "gBleClientSendDataMutex is NULL",
1480 VERIFY_NON_NULL_RET(gBLEClientSendQueueHandle, CALEADAPTER_TAG, "gBLEClientSendQueueHandle",
1483 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1485 CABLEData *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1488 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1489 return CA_MEMORY_ALLOC_FAILED;
1491 // Add message to send queue
1492 u_mutex_lock(gBleClientSendDataMutex);
1493 CAQueueingThreadAddData(gBLEClientSendQueueHandle, bleData, sizeof(CABLEData));
1494 u_mutex_unlock(gBleClientSendDataMutex);
1496 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT ");
1497 return CA_STATUS_OK;
1501 CAResult_t CABLEServerSendData(const CARemoteEndpoint_t *remoteEndpoint,
1505 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN ");
1507 VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1509 VERIFY_NON_NULL_RET(gSendQueueHandle, CALEADAPTER_TAG,
1510 "BleClientReceiverQueue is NULL",
1512 VERIFY_NON_NULL_RET(gBleClientSendDataMutex, CALEADAPTER_TAG,
1513 "BleClientSendDataMutex is NULL",
1516 VERIFY_NON_NULL_RET(gSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
1519 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1521 CABLEData *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
1524 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1525 return CA_MEMORY_ALLOC_FAILED;
1527 // Add message to send queue
1528 u_mutex_lock(gBleServerSendDataMutex);
1529 CAQueueingThreadAddData(gSendQueueHandle, bleData, sizeof(CABLEData));
1530 u_mutex_unlock(gBleServerSendDataMutex);
1532 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT ");
1533 return CA_STATUS_OK;
1536 CAResult_t CABLEServerReceivedData(const char *remoteAddress, const char *serviceUUID,
1537 void *data, uint32_t dataLength, uint32_t *sentLength)
1539 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN");
1542 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1543 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1544 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1545 VERIFY_NON_NULL_RET(gCABleServerReceiverQueue, CALEADAPTER_TAG, "gCABleServerReceiverQueue",
1548 //Add message to data queue
1549 CARemoteEndpoint_t *remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1551 if (NULL == remoteEndpoint)
1553 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1554 return CA_STATUS_FAILED;
1557 // Create bleData to add to queue
1558 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1560 CABLEData *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1563 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1564 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1565 return CA_MEMORY_ALLOC_FAILED;
1568 // Add message to send queue
1569 CAQueueingThreadAddData(gCABleServerReceiverQueue, bleData, sizeof(CABLEData));
1571 *sentLength = dataLength;
1576 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT");
1577 return CA_STATUS_OK;
1580 CAResult_t CABLEClientReceivedData(const char *remoteAddress, const char *serviceUUID,
1581 void *data, uint32_t dataLength, uint32_t *sentLength)
1583 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN");
1586 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1587 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1588 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1589 VERIFY_NON_NULL_RET(gCABleClientReceiverQueue, CALEADAPTER_TAG, "gCABleClientReceiverQueue",
1592 //Add message to data queue
1593 CARemoteEndpoint_t *remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_LE, remoteAddress,
1595 if (NULL == remoteEndpoint)
1597 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1598 return CA_STATUS_FAILED;
1601 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1603 // Create bleData to add to queue
1604 CABLEData *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
1607 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1608 CAAdapterFreeRemoteEndpoint(remoteEndpoint);
1609 return CA_MEMORY_ALLOC_FAILED;
1612 // Add message to send queue
1613 CAQueueingThreadAddData(gCABleClientReceiverQueue, bleData, sizeof(CABLEData));
1615 *sentLength = dataLength;
1620 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT");
1621 return CA_STATUS_OK;
1624 void CASetBleAdapterThreadPoolHandle(u_thread_pool_t handle)
1626 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1628 u_mutex_unlock(gBleAdapterThreadPoolMutex);
1629 gBleAdapterThreadPool = handle;
1630 u_mutex_unlock(gBleAdapterThreadPoolMutex);
1632 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1635 void CASetBLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
1637 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1639 u_mutex_lock(gBleAdapterReqRespCbMutex);
1641 gNetworkPacketReceivedCallback = callback;
1643 u_mutex_unlock(gBleAdapterReqRespCbMutex);
1645 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");