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"
30 #include "caqueueingthread.h"
32 #include "cafragmentation.h"
33 #include "oic_malloc.h"
34 #include "oic_string.h"
35 #include "caremotehandler.h"
38 * @var CALEADAPTER_TAG
39 * @brief Logging tag for module name.
41 #define CALEADAPTER_TAG "LAD"
44 * @var g_networkCallback
45 * @brief Callback to provide the status of the network change to CA layer.
47 static CANetworkChangeCallback g_networkCallback = NULL;
50 * @var g_localBLEAddress
51 * @brief bleAddress of the local adapter. Value will be initialized to zero, and will
54 static char g_localBLEAddress[18] = {0};
58 * @brief Variable to differentiate btw GattServer and GattClient.
60 static bool g_isServer = false;
63 * @var g_bleIsServerMutex
64 * @brief Mutex to synchronize the task to be executed on the GattServer function calls.
66 static ca_mutex g_bleIsServerMutex = NULL;
69 * @var g_bleNetworkCbMutex
70 * @brief Mutex to synchronize the callback to be called for the network changes.
72 static ca_mutex g_bleNetworkCbMutex = NULL;
75 * @var g_bleLocalAddressMutex
76 * @brief Mutex to synchronize the updation of the local LE address of the adapter.
78 static ca_mutex g_bleLocalAddressMutex = NULL;
81 * @var g_bleAdapterThreadPool
82 * @brief reference to threadpool
84 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
87 * @var g_bleAdapterThreadPoolMutex
88 * @brief Mutex to synchronize the task to be pushed to thread pool.
90 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
93 * @var g_bleClientSendDataMutex
94 * @brief Mutex to synchronize the queing of the data from SenderQueue.
96 static ca_mutex g_bleClientSendDataMutex = NULL;
99 * @var g_bleClientReceiveDataMutex
100 * @brief Mutex to synchronize the queing of the data from ReceiverQueue.
102 static ca_mutex g_bleClientReceiveDataMutex = NULL;
106 * @var g_bleServerSendDataMutex
107 * @brief Mutex to synchronize the queing of the data from SenderQueue.
109 static ca_mutex g_bleServerSendDataMutex = NULL;
112 * @var g_bleServerReceiveDataMutex
113 * @brief Mutex to synchronize the queing of the data from ReceiverQueue.
115 static ca_mutex g_bleServerReceiveDataMutex = NULL;
118 * @var g_bleAdapterReqRespCbMutex
119 * @brief Mutex to synchronize the callback to be called for the adapterReqResponse.
121 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
124 * @var g_networkPacketReceivedCallback
125 * @brief Callback to be called when network packet recieved from either GattServer or GattClient.
127 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
130 * @var g_errorHandler
131 * @brief Callback to notify error from the BLE adapter
133 static CAErrorHandleCallback g_errorHandler = NULL;
136 * @var g_bleAdapterState
137 * @brief Storing Adapter state information
139 static CAAdapterState_t g_bleAdapterState = CA_ADAPTER_DISABLED;
142 * @ENUM CALeServerStatus
143 * @brief status of BLE Server Status
144 * This ENUM provides information of LE Adapter Server status
148 CA_SERVER_NOTSTARTED = 0,
154 * @var gLeServerStatus
155 * @brief structure to maintain the status of the server.
157 static CALeServerStatus gLeServerStatus = CA_SERVER_NOTSTARTED;
160 * @fn CALERegisterNetworkNotifications
161 * @brief This function is used to register network change notification callback.
163 * @param[in] netCallback CANetworkChangeCallback callback which will be set for the change in nwk.
165 * @return 0 on success otherwise a positive error value.
166 * @retval CA_STATUS_OK Successful
167 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
168 * @retval CA_STATUS_FAILED Operation failed
171 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
174 * @fn CASetBleAdapterThreadPoolHandle
175 * @brief Used to Set the gThreadPool handle which is required for spawning new thread.
177 * @param[in] handle - Thread pool handle which is given by above layer for using thread
183 void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
186 * @fn CALEDeviceStateChangedCb
187 * @brief This function is used to call the callback to the upper layer when the device state gets
190 * @param[in] adapter_state New state of the adapter to be notified to the upper layer.
195 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state);
198 * @fn CAInitBleAdapterMutex
199 * @brief Used to initialize all required mutex variable for LE Adapter implementation.
201 * @return 0 on success otherwise a positive error value.
202 * @retval CA_STATUS_OK Successful
203 * @retval CA_STATUS_INVALID_PARAM Invalid input argumets
204 * @retval CA_STATUS_FAILED Operation failed
207 CAResult_t CAInitLEAdapterMutex();
210 * @fn CATerminateBleAdapterMutex
211 * @brief Used to terminate all required mutex variable for LE adapter implementation.
215 void CATerminateLEAdapterMutex();
218 * @fn CALEErrorHandler
219 * @brief prepares and notify error through error callback
223 static void CALEErrorHandler(const char *remoteAddress, const void *data, uint32_t dataLen,
226 #ifndef SINGLE_THREAD
228 * @var g_dataReceiverHandlerState
229 * @brief Stop condition of recvhandler.
231 static bool g_dataReceiverHandlerState = false;
234 * @var g_bleClientSendQueueHandle
235 * @brief Queue to process the outgoing packets from GATTClient.
237 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
240 * @var g_bleClientReceiverQueue
241 * @brief Queue to process the incoming packets to GATT Client.
243 static CAQueueingThread_t *g_bleClientReceiverQueue = NULL;
246 * @var g_bleServerSendQueueHandle
247 * @brief Queue to process the outgoing packets from GATTServer.
249 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
252 * @var g_bleServerReceiverQueue
253 * @brief Queue to process the incoming packets to GATTServer
255 static CAQueueingThread_t *g_bleServerReceiverQueue = NULL;
258 * @fn CALEDataDestroyer
259 * @brief Used to free data
263 static void CALEDataDestroyer(void *data, uint32_t size);
265 void CAInitLEQueues()
267 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
269 CAResult_t result = CAInitLEServerQueues();
270 if (CA_STATUS_OK != result)
272 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerQueues failed");
276 result = CAInitLEClientQueues();
277 if (CA_STATUS_OK != result)
279 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
283 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
286 CAResult_t CAInitLEServerQueues()
288 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
290 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
292 CAResult_t result = CAInitLEServerSenderQueue();
293 if (CA_STATUS_OK != result)
295 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
296 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
297 return CA_STATUS_FAILED;
300 result = CAInitLEServerReceiverQueue();
301 if (CA_STATUS_OK != result)
303 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerReceiverQueue failed");
304 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
305 return CA_STATUS_FAILED;
308 g_dataReceiverHandlerState = true;
310 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
312 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
316 CAResult_t CAInitLEClientQueues()
318 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
320 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
322 CAResult_t result = CAInitLEClientSenderQueue();
323 if (CA_STATUS_OK != result)
325 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
326 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
327 return CA_STATUS_FAILED;
330 result = CAInitLEClientReceiverQueue();
331 if (CA_STATUS_OK != result)
333 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientReceiverQueue failed");
334 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
335 return CA_STATUS_FAILED;
338 g_dataReceiverHandlerState = true;
340 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
342 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
346 CAResult_t CAInitLEServerSenderQueue()
348 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
349 // Check if the message queue is already initialized
350 if (g_bleServerSendQueueHandle)
352 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
356 // Create send message queue
357 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
358 if (!g_bleServerSendQueueHandle)
360 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
361 return CA_MEMORY_ALLOC_FAILED;
364 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
365 g_bleAdapterThreadPool,
366 CALEServerSendDataThread, CALEDataDestroyer))
368 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
369 OICFree(g_bleServerSendQueueHandle);
370 g_bleServerSendQueueHandle = NULL;
371 return CA_STATUS_FAILED;
374 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerSendQueueHandle))
376 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
377 OICFree(g_bleServerSendQueueHandle);
378 g_bleServerSendQueueHandle = NULL;
379 return CA_STATUS_FAILED;
382 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
386 CAResult_t CAInitLEClientSenderQueue()
388 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
390 if (g_bleClientSendQueueHandle)
392 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
396 // Create send message queue
397 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
398 if (!g_bleClientSendQueueHandle)
400 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
401 return CA_MEMORY_ALLOC_FAILED;
404 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
405 g_bleAdapterThreadPool,
406 CALEClientSendDataThread, CALEDataDestroyer))
408 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
409 OICFree(g_bleClientSendQueueHandle);
410 g_bleClientSendQueueHandle = NULL;
411 return CA_STATUS_FAILED;
414 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientSendQueueHandle))
416 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
417 OICFree(g_bleClientSendQueueHandle);
418 g_bleClientSendQueueHandle = NULL;
419 return CA_STATUS_FAILED;
422 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
426 CAResult_t CAInitLEServerReceiverQueue()
428 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
429 // Check if the message queue is already initialized
430 if (g_bleServerReceiverQueue)
432 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
436 // Create send message queue
437 g_bleServerReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
438 if (!g_bleServerReceiverQueue)
440 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
441 OICFree(g_bleServerSendQueueHandle);
442 return CA_MEMORY_ALLOC_FAILED;
445 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerReceiverQueue, g_bleAdapterThreadPool,
446 CALEServerDataReceiverHandler, CALEDataDestroyer))
448 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
449 OICFree(g_bleServerReceiverQueue);
450 g_bleServerReceiverQueue = NULL;
451 return CA_STATUS_FAILED;
454 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerReceiverQueue))
456 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
457 OICFree(g_bleServerReceiverQueue);
458 g_bleServerReceiverQueue = NULL;
459 return CA_STATUS_FAILED;
462 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
466 CAResult_t CAInitLEClientReceiverQueue()
468 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
470 // Check if the message queue is already initialized
471 if (g_bleClientReceiverQueue)
473 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
477 // Create send message queue
478 g_bleClientReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
479 if (!g_bleClientReceiverQueue)
481 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
482 OICFree(g_bleClientSendQueueHandle);
483 return CA_MEMORY_ALLOC_FAILED;
486 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientReceiverQueue,
487 g_bleAdapterThreadPool,
488 CALEClientDataReceiverHandler, NULL))
490 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
491 OICFree(g_bleClientSendQueueHandle);
492 OICFree(g_bleClientReceiverQueue);
493 g_bleClientReceiverQueue = NULL;
494 return CA_STATUS_FAILED;
497 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientReceiverQueue))
499 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
500 OICFree(g_bleClientReceiverQueue);
501 g_bleClientReceiverQueue = NULL;
502 return CA_STATUS_FAILED;
505 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
509 void CAStopLEQueues()
511 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
513 ca_mutex_lock(g_bleClientSendDataMutex);
514 if (NULL != g_bleClientSendQueueHandle)
516 CAQueueingThreadStop(g_bleClientSendQueueHandle);
518 ca_mutex_unlock(g_bleClientSendDataMutex);
520 ca_mutex_lock(g_bleClientReceiveDataMutex);
521 if (NULL != g_bleClientReceiverQueue)
523 CAQueueingThreadStop(g_bleClientReceiverQueue);
525 ca_mutex_unlock(g_bleClientReceiveDataMutex);
527 ca_mutex_lock(g_bleServerSendDataMutex);
528 if (NULL != g_bleServerSendQueueHandle)
530 CAQueueingThreadStop(g_bleServerSendQueueHandle);
532 ca_mutex_unlock(g_bleServerSendDataMutex);
534 ca_mutex_lock(g_bleServerReceiveDataMutex);
535 if (NULL != g_bleServerReceiverQueue)
537 CAQueueingThreadStop(g_bleServerReceiverQueue);
539 ca_mutex_unlock(g_bleServerReceiveDataMutex);
541 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
544 void CATerminateLEQueues()
546 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
548 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
549 OICFree(g_bleClientSendQueueHandle);
550 g_bleClientSendQueueHandle = NULL;
553 CAQueueingThreadDestroy(g_bleClientReceiverQueue);
554 OICFree(g_bleClientReceiverQueue);
555 g_bleClientReceiverQueue = NULL;
558 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
559 OICFree(g_bleServerSendQueueHandle);
560 g_bleServerSendQueueHandle = NULL;
563 CAQueueingThreadDestroy(g_bleServerReceiverQueue);
564 OICFree(g_bleServerReceiverQueue);
565 g_bleServerReceiverQueue = NULL;
567 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
570 void CALEServerDataReceiverHandler(void *threadData)
572 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
574 static uint32_t recvDataLen = 0;
575 static uint32_t totalDataLen = 0;
576 static char *defragData = NULL;
577 static bool isHeaderAvailable = false;
578 static CAEndpoint_t *remoteEndpoint = NULL;
580 ca_mutex_lock(g_bleServerReceiveDataMutex);
582 if (g_dataReceiverHandlerState)
584 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
586 CALEData_t *bleData = (CALEData_t *) threadData;
589 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
590 ca_mutex_unlock(g_bleServerReceiveDataMutex);
594 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
596 if (!isHeaderAvailable)
598 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
599 totalDataLen = CAParseHeader((char*)bleData->data);
601 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes", totalDataLen);
602 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%d] bytes", bleData->dataLen);
604 defragData = (char *) OICCalloc(totalDataLen + 1, sizeof(char));
605 if (NULL == defragData)
607 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
608 ca_mutex_unlock(g_bleServerReceiveDataMutex);
612 const char *remoteAddress = bleData->remoteEndpoint->addr;
614 remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_GATT_BTLE,
617 memcpy(defragData + recvDataLen, bleData->data + CA_HEADER_LENGTH,
618 bleData->dataLen - CA_HEADER_LENGTH);
619 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
620 isHeaderAvailable = true;
624 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
625 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
626 recvDataLen += bleData->dataLen ;
627 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
628 totalDataLen, recvDataLen);
630 if (totalDataLen == recvDataLen)
632 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
633 if (NULL == g_networkPacketReceivedCallback)
635 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
637 CAFreeEndpoint(remoteEndpoint);
638 remoteEndpoint = NULL;
640 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
641 ca_mutex_unlock(g_bleServerReceiveDataMutex);
644 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
645 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
648 CAFreeEndpoint(remoteEndpoint);
652 isHeaderAvailable = false;
653 remoteEndpoint = NULL;
655 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
658 if (false == g_dataReceiverHandlerState)
660 OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
663 isHeaderAvailable = false;
665 CAFreeEndpoint(remoteEndpoint);
666 remoteEndpoint = NULL;
668 ca_mutex_unlock(g_bleServerReceiveDataMutex);
672 ca_mutex_unlock(g_bleServerReceiveDataMutex);
673 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
676 void CALEClientDataReceiverHandler(void *threadData)
678 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
680 static const char *remoteAddress = NULL;
681 static uint32_t recvDataLen = 0;
682 static uint32_t totalDataLen = 0;
683 static char *defragData = NULL;
684 static bool isHeaderAvailable = false;
685 static CAEndpoint_t *remoteEndpoint = NULL;
687 ca_mutex_lock(g_bleClientReceiveDataMutex);
689 if (g_dataReceiverHandlerState)
691 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
693 CALEData_t *bleData = (CALEData_t *) threadData;
696 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid wifidata!");
697 ca_mutex_unlock(g_bleClientReceiveDataMutex);
701 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
703 if (!isHeaderAvailable)
705 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
707 totalDataLen = CAParseHeader(bleData->data);
708 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes",
710 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received in the first packet [%d] bytes",
713 defragData = (char *) OICMalloc(sizeof(char) * totalDataLen);
714 if (NULL == defragData)
716 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
717 ca_mutex_unlock(g_bleClientReceiveDataMutex);
721 remoteAddress = bleData->remoteEndpoint->addr;
723 remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_GATT_BTLE,
726 memcpy(defragData, bleData->data + CA_HEADER_LENGTH,
727 bleData->dataLen - CA_HEADER_LENGTH);
728 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
729 isHeaderAvailable = true;
733 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
734 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
735 recvDataLen += bleData->dataLen ;
736 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
737 totalDataLen, recvDataLen);
739 if (totalDataLen == recvDataLen)
741 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
742 if (NULL == g_networkPacketReceivedCallback)
744 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
746 CAFreeEndpoint(remoteEndpoint);
747 remoteEndpoint = NULL;
749 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
750 ca_mutex_unlock(g_bleClientReceiveDataMutex);
753 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
754 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
757 isHeaderAvailable = false;
759 CAFreeEndpoint(remoteEndpoint);
760 remoteEndpoint = NULL;
762 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
765 if (false == g_dataReceiverHandlerState)
767 OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
769 CAFreeEndpoint(remoteEndpoint);
770 remoteEndpoint = NULL;
772 ca_mutex_unlock(g_bleClientReceiveDataMutex);
776 ca_mutex_unlock(g_bleClientReceiveDataMutex);
777 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
780 void CALEServerSendDataThread(void *threadData)
782 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
784 CALEData_t *bleData = (CALEData_t *) threadData;
787 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
791 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
792 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
794 int32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
796 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server total Data length with header is [%d]", totalLength);
797 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
798 if (NULL == dataSegment)
800 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
805 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
806 if (CA_STATUS_OK != result )
808 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
810 OICFree(dataSegment);
814 memcpy(dataSegment, header, CA_HEADER_LENGTH);
818 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
820 length = totalLength;
821 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
825 length = CA_SUPPORTED_BLE_MTU_SIZE;
826 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
827 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
830 int32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
832 // Send the first segment with the header.
833 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
835 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
836 result = CAUpdateCharacteristicsToGattClient(
837 bleData->remoteEndpoint->addr, dataSegment, length);
838 if (CA_STATUS_OK != result)
840 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
841 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
842 OICFree(dataSegment);
846 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
847 for (index = 1; index < iter; index++)
849 // Send the remaining header.
850 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
851 result = CAUpdateCharacteristicsToGattClient(
852 bleData->remoteEndpoint->addr,
853 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
854 CA_SUPPORTED_BLE_MTU_SIZE);
855 if (CA_STATUS_OK != result)
857 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
858 "Update characteristics failed, result [%d]", result);
859 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
860 OICFree(dataSegment);
863 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
864 CA_SUPPORTED_BLE_MTU_SIZE);
867 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
868 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
870 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
871 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
872 result = CAUpdateCharacteristicsToGattClient(
873 bleData->remoteEndpoint->addr,
874 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
876 if (CA_STATUS_OK != result)
878 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
880 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
881 OICFree(dataSegment);
884 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
889 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
890 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
891 if (CA_STATUS_OK != result)
893 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
895 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
896 OICFree(dataSegment);
899 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
900 for (index = 1; index < iter; index++)
902 // Send the remaining header.
903 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
904 result = CAUpdateCharacteristicsToAllGattClients(
905 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
906 CA_SUPPORTED_BLE_MTU_SIZE);
907 if (CA_STATUS_OK != result)
909 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
911 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
912 OICFree(dataSegment);
915 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
918 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
919 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
921 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
922 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
923 result = CAUpdateCharacteristicsToAllGattClients(
924 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
926 if (CA_STATUS_OK != result)
928 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
930 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
931 OICFree(dataSegment);
934 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
937 OICFree(dataSegment);
939 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
942 void CALEClientSendDataThread(void *threadData)
944 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
946 CALEData_t *bleData = (CALEData_t *) threadData;
949 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
953 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
954 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
956 uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
957 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
958 if (NULL == dataSegment)
960 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
965 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
966 if (CA_STATUS_OK != result )
968 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
970 OICFree(dataSegment);
973 memcpy(dataSegment, header, CA_HEADER_LENGTH);
977 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
979 length = totalLength;
980 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
981 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
985 length = CA_SUPPORTED_BLE_MTU_SIZE;
986 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
987 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
988 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
991 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
993 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
995 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
996 // Send the first segment with the header.
997 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addr,
1002 if (CA_STATUS_OK != result)
1004 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1005 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1006 OICFree(dataSegment);
1010 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1011 for (index = 1; index < iter; index++)
1013 // Send the remaining header.
1014 result = CAUpdateCharacteristicsToGattServer(
1015 bleData->remoteEndpoint->addr,
1016 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1017 CA_SUPPORTED_BLE_MTU_SIZE,
1019 if (CA_STATUS_OK != result)
1021 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1023 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1024 OICFree(dataSegment);
1027 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1028 CA_SUPPORTED_BLE_MTU_SIZE);
1031 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1032 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1034 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1035 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1036 result = CAUpdateCharacteristicsToGattServer(
1037 bleData->remoteEndpoint->addr,
1038 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1042 if (CA_STATUS_OK != result)
1044 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1046 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1047 OICFree(dataSegment);
1050 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1055 //Sending Mulitcast Data
1056 // Send the first segment with the header.
1057 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1058 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1059 if (CA_STATUS_OK != result)
1061 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1062 "Update characteristics (all) failed, result [%d]", result);
1063 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1064 OICFree(dataSegment);
1067 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1068 // Send the remaining header.
1069 for (index = 1; index < iter; index++)
1071 result = CAUpdateCharacteristicsToAllGattServers(
1072 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1073 CA_SUPPORTED_BLE_MTU_SIZE);
1074 if (CA_STATUS_OK != result)
1076 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]",
1078 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1079 OICFree(dataSegment);
1082 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1083 CA_SUPPORTED_BLE_MTU_SIZE);
1086 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1087 if ( remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1089 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1090 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1091 result = CAUpdateCharacteristicsToAllGattServers(
1092 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1094 if (CA_STATUS_OK != result)
1096 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1097 "Update characteristics (all) failed, result [%d]", result);
1098 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1099 OICFree(dataSegment);
1102 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1107 OICFree(dataSegment);
1109 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1112 CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint, const void *data,
1113 uint32_t dataLength)
1115 CALEData_t *bleData = (CALEData_t *) OICMalloc(sizeof(CALEData_t));
1118 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1122 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1123 bleData->data = (void *)OICCalloc(dataLength + 1, 1);
1124 if (NULL == bleData->data)
1126 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1127 CAFreeLEData(bleData);
1130 memcpy(bleData->data, data, dataLength);
1131 bleData->dataLen = dataLength;
1136 void CAFreeLEData(CALEData_t *bleData)
1138 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1140 CAFreeEndpoint(bleData->remoteEndpoint);
1141 OICFree(bleData->data);
1145 void CALEDataDestroyer(void *data, uint32_t size)
1147 CALEData_t *ledata = (CALEData_t *) data;
1149 CAFreeLEData(ledata);
1153 CAResult_t CAInitLEAdapterMutex()
1155 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1157 if (NULL == g_bleIsServerMutex)
1159 g_bleIsServerMutex = ca_mutex_new();
1160 if (NULL == g_bleIsServerMutex)
1162 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1163 return CA_STATUS_FAILED;
1167 if (NULL == g_bleNetworkCbMutex)
1169 g_bleNetworkCbMutex = ca_mutex_new();
1170 if (NULL == g_bleNetworkCbMutex)
1172 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1173 CATerminateLEAdapterMutex();
1174 return CA_STATUS_FAILED;
1178 if (NULL == g_bleLocalAddressMutex)
1180 g_bleLocalAddressMutex = ca_mutex_new();
1181 if (NULL == g_bleLocalAddressMutex)
1183 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1184 CATerminateLEAdapterMutex();
1185 return CA_STATUS_FAILED;
1189 if (NULL == g_bleAdapterThreadPoolMutex)
1191 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1192 if (NULL == g_bleAdapterThreadPoolMutex)
1194 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1195 CATerminateLEAdapterMutex();
1196 return CA_STATUS_FAILED;
1200 if (NULL == g_bleClientSendDataMutex)
1202 g_bleClientSendDataMutex = ca_mutex_new();
1203 if (NULL == g_bleClientSendDataMutex)
1205 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1206 CATerminateLEAdapterMutex();
1207 return CA_STATUS_FAILED;
1211 if (NULL == g_bleClientReceiveDataMutex)
1213 g_bleClientReceiveDataMutex = ca_mutex_new();
1214 if (NULL == g_bleClientReceiveDataMutex)
1216 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1217 CATerminateLEAdapterMutex();
1218 return CA_STATUS_FAILED;
1222 if (NULL == g_bleServerSendDataMutex)
1224 g_bleServerSendDataMutex = ca_mutex_new();
1225 if (NULL == g_bleServerSendDataMutex)
1227 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1228 CATerminateLEAdapterMutex();
1229 return CA_STATUS_FAILED;
1233 if (NULL == g_bleServerReceiveDataMutex)
1235 g_bleServerReceiveDataMutex = ca_mutex_new();
1236 if (NULL == g_bleServerReceiveDataMutex)
1238 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1239 CATerminateLEAdapterMutex();
1240 return CA_STATUS_FAILED;
1244 if (NULL == g_bleAdapterReqRespCbMutex)
1246 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1247 if (NULL == g_bleAdapterReqRespCbMutex)
1249 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1250 CATerminateLEAdapterMutex();
1251 return CA_STATUS_FAILED;
1255 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1256 return CA_STATUS_OK;
1259 void CATerminateLEAdapterMutex()
1261 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1263 ca_mutex_free(g_bleIsServerMutex);
1264 g_bleIsServerMutex = NULL;
1266 ca_mutex_free(g_bleNetworkCbMutex);
1267 g_bleNetworkCbMutex = NULL;
1269 ca_mutex_free(g_bleLocalAddressMutex);
1270 g_bleLocalAddressMutex = NULL;
1272 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1273 g_bleAdapterThreadPoolMutex = NULL;
1275 ca_mutex_free(g_bleClientSendDataMutex);
1276 g_bleClientSendDataMutex = NULL;
1278 ca_mutex_free(g_bleClientReceiveDataMutex);
1279 g_bleClientReceiveDataMutex = NULL;
1281 ca_mutex_free(g_bleServerSendDataMutex);
1282 g_bleServerSendDataMutex = NULL;
1284 ca_mutex_free(g_bleServerReceiveDataMutex);
1285 g_bleServerReceiveDataMutex = NULL;
1287 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1288 g_bleAdapterReqRespCbMutex = NULL;
1290 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1293 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1294 CANetworkPacketReceivedCallback reqRespCallback,
1295 CANetworkChangeCallback netCallback,
1296 CAErrorHandleCallback errorCallback, ca_thread_pool_t handle)
1298 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1301 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1302 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1303 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1305 CAResult_t result = CA_STATUS_OK;
1306 result = CAInitLEAdapterMutex();
1307 if (CA_STATUS_OK != result)
1309 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1310 return CA_STATUS_FAILED;
1312 result = CAInitializeLENetworkMonitor();
1313 if (CA_STATUS_OK != result)
1315 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1316 return CA_STATUS_FAILED;
1319 CAInitializeLEAdapter();
1321 CASetLEClientThreadPoolHandle(handle);
1322 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1323 CASetLEServerThreadPoolHandle(handle);
1324 CASetLEAdapterThreadPoolHandle(handle);
1325 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1326 CASetLEReqRespAdapterCallback(reqRespCallback);
1328 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1329 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1330 CALERegisterNetworkNotifications(netCallback);
1332 g_errorHandler = errorCallback;
1334 CAConnectivityHandler_t connHandler;
1335 connHandler.startAdapter = CAStartLE;
1336 connHandler.stopAdapter = CAStopLE;
1337 connHandler.startListenServer = CAStartLEListeningServer;
1338 connHandler.startDiscoveryServer = CAStartLEDiscoveryServer;
1339 connHandler.sendData = CASendLEUnicastData;
1340 connHandler.sendDataToAll = CASendLEMulticastData;
1341 connHandler.GetnetInfo = CAGetLEInterfaceInformation;
1342 connHandler.readData = CAReadLEData;
1343 connHandler.terminate = CATerminateLE;
1344 registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
1346 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1348 return CA_STATUS_OK;
1351 CAResult_t CAStartLE()
1353 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1354 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE, not implemented");
1355 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1356 return CA_STATUS_OK;
1359 CAResult_t CAStopLE()
1361 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1362 #ifndef SINGLE_THREAD
1366 ca_mutex_lock(g_bleIsServerMutex);
1367 if (true == g_isServer)
1369 CAStopLEGattServer();
1373 CAStopLEGattClient();
1375 ca_mutex_unlock(g_bleIsServerMutex);
1377 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1379 return CA_STATUS_OK;
1382 void CATerminateLE()
1384 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1386 CASetLEReqRespServerCallback(NULL);
1387 CASetLEReqRespClientCallback(NULL);
1388 CALERegisterNetworkNotifications(NULL);
1389 CASetLEReqRespAdapterCallback(NULL);
1390 CATerminateLENetworkMonitor();
1392 ca_mutex_lock(g_bleIsServerMutex);
1393 if (true == g_isServer)
1395 CATerminateLEGattServer();
1399 CATerminateLEGattClient();
1401 ca_mutex_unlock(g_bleIsServerMutex);
1403 #ifndef SINGLE_THREAD
1404 CATerminateLEQueues();
1406 CATerminateLEAdapterMutex();
1408 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1411 CAResult_t CAStartLEListeningServer()
1413 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1414 CAResult_t result = CA_STATUS_OK;
1415 #ifndef SINGLE_THREAD
1416 result = CAInitLEServerQueues();
1417 if (CA_STATUS_OK != result)
1419 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1420 return CA_STATUS_FAILED;
1424 result = CAGetLEAdapterState();
1425 if (CA_ADAPTER_NOT_ENABLED == result)
1427 gLeServerStatus = CA_LISTENING_SERVER;
1428 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1429 return CA_STATUS_OK;
1432 if (CA_STATUS_FAILED == result)
1434 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1435 return CA_STATUS_FAILED;
1438 CAStartLEGattServer();
1440 ca_mutex_lock(g_bleIsServerMutex);
1442 ca_mutex_unlock(g_bleIsServerMutex);
1444 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1445 return CA_STATUS_OK;
1448 CAResult_t CAStartLEDiscoveryServer()
1450 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1451 CAResult_t result = CA_STATUS_OK;
1452 #ifndef SINGLE_THREAD
1453 result = CAInitLEClientQueues();
1454 if (CA_STATUS_OK != result)
1456 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1457 return CA_STATUS_FAILED;
1460 result = CAGetLEAdapterState();
1461 if (CA_ADAPTER_NOT_ENABLED == result)
1463 gLeServerStatus = CA_DISCOVERY_SERVER;
1464 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1465 return CA_STATUS_OK;
1468 if (CA_STATUS_FAILED == result)
1470 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1471 return CA_STATUS_FAILED;
1474 CAStartLEGattClient();
1476 ca_mutex_lock(g_bleIsServerMutex);
1478 ca_mutex_unlock(g_bleIsServerMutex);
1480 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1481 return CA_STATUS_OK;
1484 CAResult_t CAStartLENotifyServer()
1486 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1488 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1489 return CA_STATUS_OK;
1492 uint32_t CASendLENotification(const CAEndpoint_t *endpoint, const void *data,
1495 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1497 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1501 CAResult_t CAReadLEData()
1503 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1504 #ifdef SINGLE_THREAD
1507 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1508 return CA_STATUS_OK;
1511 int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
1513 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1516 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1517 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1519 CAResult_t result = CA_STATUS_FAILED;
1521 ca_mutex_lock(g_bleIsServerMutex);
1522 if (true == g_isServer)
1524 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1525 if (CA_STATUS_OK != result)
1527 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed\n");
1530 g_errorHandler((void *) endpoint, (void *) data, dataLen, result);
1532 ca_mutex_unlock(g_bleIsServerMutex);
1538 result = CALEAdapterClientSendData(endpoint, data, dataLen);
1539 if (CA_STATUS_OK != result)
1541 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed \n");
1544 g_errorHandler(endpoint, data, dataLen, result);
1546 ca_mutex_unlock(g_bleIsServerMutex);
1550 ca_mutex_unlock(g_bleIsServerMutex);
1552 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1556 int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
1558 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1561 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1565 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
1569 CAResult_t result = CA_STATUS_FAILED;
1571 ca_mutex_lock(g_bleIsServerMutex);
1572 if (true == g_isServer)
1574 result = CALEAdapterServerSendData(NULL, data, dataLen);
1575 if (CA_STATUS_OK != result)
1577 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data failed" );
1579 ca_mutex_unlock(g_bleIsServerMutex);
1582 g_errorHandler(endpoint, data, dataLen, result);
1589 result = CALEAdapterClientSendData(NULL, data, dataLen);
1590 if (CA_STATUS_OK != result)
1592 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data failed" );
1595 g_errorHandler(endpoint, data, dataLen, result);
1597 ca_mutex_unlock(g_bleIsServerMutex);
1601 ca_mutex_unlock(g_bleIsServerMutex);
1603 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1607 CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
1609 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1611 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
1613 char *local_address = NULL;
1615 CAResult_t res = CAGetLEAddress(&local_address);
1616 if (CA_STATUS_OK != res)
1618 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
1622 if (NULL == local_address)
1624 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
1625 return CA_STATUS_FAILED;
1629 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
1630 if (NULL == (*info))
1632 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
1633 OICFree(local_address);
1634 return CA_STATUS_FAILED;
1637 size_t local_address_len = strlen(local_address);
1639 if(local_address_len >= sizeof(g_localBLEAddress) ||
1640 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
1642 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
1644 OICFree(local_address);
1645 return CA_STATUS_FAILED;
1648 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
1649 ca_mutex_lock(g_bleLocalAddressMutex);
1650 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
1651 ca_mutex_unlock(g_bleLocalAddressMutex);
1653 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
1655 OICFree(local_address);
1657 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1658 return CA_STATUS_OK;
1661 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
1663 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1665 ca_mutex_lock(g_bleNetworkCbMutex);
1666 g_networkCallback = netCallback;
1667 ca_mutex_unlock(g_bleNetworkCbMutex);
1668 CAResult_t res = CA_STATUS_OK;
1671 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
1672 if (CA_STATUS_OK != res)
1674 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1679 res = CAUnSetLEAdapterStateChangedCb();
1680 if (CA_STATUS_OK != res)
1682 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1686 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1690 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
1692 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1694 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
1695 CAEndpoint_t localEndpoint = {};
1697 ca_mutex_lock(g_bleLocalAddressMutex);
1698 OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), g_localBLEAddress);
1699 ca_mutex_unlock(g_bleLocalAddressMutex);
1701 g_bleAdapterState = adapter_state;
1702 // Start a GattServer/Client if gLeServerStatus is SET
1703 if (CA_LISTENING_SERVER == gLeServerStatus)
1705 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1706 CAStartLEGattServer();
1708 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
1710 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
1711 CAStartLEGattClient();
1713 gLeServerStatus = CA_SERVER_NOTSTARTED;
1715 ca_mutex_lock(g_bleNetworkCbMutex);
1716 if (NULL != g_networkCallback)
1718 g_networkCallback(&localEndpoint, adapter_state);
1722 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
1724 ca_mutex_unlock(g_bleNetworkCbMutex);
1726 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1729 CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1733 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1735 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
1736 #ifndef SINGLE_THREAD
1737 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
1738 "g_bleClientSendQueueHandle is NULL",
1740 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
1741 "g_bleClientSendDataMutex is NULL",
1744 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
1747 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1749 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
1752 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1753 return CA_MEMORY_ALLOC_FAILED;
1755 // Add message to send queue
1756 ca_mutex_lock(g_bleClientSendDataMutex);
1757 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
1758 ca_mutex_unlock(g_bleClientSendDataMutex);
1760 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1761 return CA_STATUS_OK;
1765 CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1769 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1771 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
1773 #ifdef SINGLE_THREAD
1774 char header[CA_HEADER_LENGTH] = {0};
1776 CAResult_t result = CAGenerateHeader(header, dataLen);
1778 if (CA_STATUS_OK != result)
1780 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1784 if (!CAIsLEConnected())
1786 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
1790 result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH);
1791 if (CA_STATUS_OK != result)
1793 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
1797 int32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
1798 for (int32_t iter = 0; iter < dataLimit; iter++)
1800 result = CAUpdateCharacteristicsToAllGattClients((data +
1801 (iter * CA_SUPPORTED_BLE_MTU_SIZE)),
1802 CA_SUPPORTED_BLE_MTU_SIZE);
1803 if (CA_STATUS_OK != result)
1805 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
1811 uint8_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
1814 result = CAUpdateCharacteristicsToAllGattClients((data +
1815 (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE)),
1817 if (CA_STATUS_OK != result)
1819 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
1825 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
1826 "BleClientReceiverQueue is NULL",
1828 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
1829 "BleClientSendDataMutex is NULL",
1832 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
1835 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1837 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
1840 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1841 return CA_MEMORY_ALLOC_FAILED;
1843 // Add message to send queue
1844 ca_mutex_lock(g_bleServerSendDataMutex);
1845 CAQueueingThreadAddData(g_bleServerSendQueueHandle, bleData, sizeof(CALEData_t));
1846 ca_mutex_unlock(g_bleServerSendDataMutex);
1848 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1849 return CA_STATUS_OK;
1852 CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress, const char *serviceUUID,
1853 const void *data, uint32_t dataLength,
1854 uint32_t *sentLength)
1856 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1859 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1860 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1861 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1863 #ifdef SINGLE_THREAD
1864 if(g_networkPacketReceivedCallback)
1866 CAEndpoint_t endPoint = { 0 }; // will be filled by upper layer
1867 endPoint.adapter = CA_ADAPTER_GATT_BTLE;
1868 g_networkPacketReceivedCallback(&endPoint, data, dataLength);
1871 VERIFY_NON_NULL_RET(g_bleServerReceiverQueue, CALEADAPTER_TAG, "g_bleServerReceiverQueue",
1874 //Add message to data queue
1875 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
1876 CA_ADAPTER_GATT_BTLE,
1878 if (NULL == remoteEndpoint)
1880 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1881 return CA_STATUS_FAILED;
1884 // Create bleData to add to queue
1885 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1887 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
1890 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1891 CAFreeEndpoint(remoteEndpoint);
1892 return CA_MEMORY_ALLOC_FAILED;
1895 CAFreeEndpoint(remoteEndpoint);
1896 // Add message to send queue
1897 CAQueueingThreadAddData(g_bleServerReceiverQueue, bleData, sizeof(CALEData_t));
1899 *sentLength = dataLength;
1901 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1902 return CA_STATUS_OK;
1905 CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress, const char *serviceUUID,
1906 const void *data, uint32_t dataLength,
1907 uint32_t *sentLength)
1909 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1912 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1913 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1914 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1915 #ifndef SINGLE_THREAD
1916 VERIFY_NON_NULL_RET(g_bleClientReceiverQueue, CALEADAPTER_TAG, "g_bleClientReceiverQueue",
1919 //Add message to data queue
1920 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
1921 CA_ADAPTER_GATT_BTLE,
1923 if (NULL == remoteEndpoint)
1925 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1926 return CA_STATUS_FAILED;
1929 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1931 // Create bleData to add to queue
1932 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
1935 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1936 CAFreeEndpoint(remoteEndpoint);
1937 return CA_MEMORY_ALLOC_FAILED;
1940 CAFreeEndpoint(remoteEndpoint);
1941 // Add message to send queue
1942 CAQueueingThreadAddData(g_bleClientReceiverQueue, bleData, sizeof(CALEData_t));
1944 *sentLength = dataLength;
1946 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1947 return CA_STATUS_OK;
1950 void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
1952 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1954 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1955 g_bleAdapterThreadPool = handle;
1956 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1958 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1961 void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
1963 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1965 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1967 g_networkPacketReceivedCallback = callback;
1969 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1971 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1974 void CALEErrorHandler(const char *remoteAddress, const void *data, uint32_t dataLen,
1977 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
1979 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
1980 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_GATT_BTLE,
1982 //if required, will be used to build remote end point
1983 g_errorHandler(rep, data, dataLen, result);
1985 CAFreeEndpoint(rep);
1986 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");