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!");
593 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
595 if (!isHeaderAvailable)
597 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
598 totalDataLen = CAParseHeader((char*)bleData->data);
600 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes", totalDataLen);
601 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%d] bytes", bleData->dataLen);
603 defragData = (char *) OICCalloc(totalDataLen + 1, sizeof(char));
604 if (NULL == defragData)
606 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
610 const char *remoteAddress = bleData->remoteEndpoint->addr;
612 remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_GATT_BTLE,
615 memcpy(defragData + recvDataLen, bleData->data + CA_HEADER_LENGTH,
616 bleData->dataLen - CA_HEADER_LENGTH);
617 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
618 isHeaderAvailable = true;
622 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
623 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
624 recvDataLen += bleData->dataLen ;
625 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
626 totalDataLen, recvDataLen);
628 if (totalDataLen == recvDataLen)
630 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
631 if (NULL == g_networkPacketReceivedCallback)
633 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
634 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
637 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
638 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
641 isHeaderAvailable = false;
642 remoteEndpoint = NULL;
644 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
647 if (false == g_dataReceiverHandlerState)
649 OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
652 isHeaderAvailable = false;
654 CAFreeEndpoint(remoteEndpoint);
655 ca_mutex_unlock(g_bleServerReceiveDataMutex);
659 ca_mutex_unlock(g_bleServerReceiveDataMutex);
660 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
663 void CALEClientDataReceiverHandler(void *threadData)
665 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
667 static const char *remoteAddress = NULL;
668 static uint32_t recvDataLen = 0;
669 static uint32_t totalDataLen = 0;
670 static char *defragData = NULL;
671 static bool isHeaderAvailable = false;
672 static CAEndpoint_t *remoteEndpoint = NULL;
674 ca_mutex_lock(g_bleClientReceiveDataMutex);
676 if (g_dataReceiverHandlerState)
678 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
680 CALEData_t *bleData = (CALEData_t *) threadData;
683 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid wifidata!");
687 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
689 if (!isHeaderAvailable)
691 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
693 totalDataLen = CAParseHeader(bleData->data);
694 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes",
696 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received in the first packet [%d] bytes",
699 defragData = (char *) OICMalloc(sizeof(char) * totalDataLen);
700 if (NULL == defragData)
702 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
706 remoteAddress = bleData->remoteEndpoint->addr;
708 remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_GATT_BTLE,
711 memcpy(defragData, bleData->data + CA_HEADER_LENGTH,
712 bleData->dataLen - CA_HEADER_LENGTH);
713 recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
714 isHeaderAvailable = true;
718 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
719 memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
720 recvDataLen += bleData->dataLen ;
721 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
722 totalDataLen, recvDataLen);
724 if (totalDataLen == recvDataLen)
726 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
727 if (NULL == g_networkPacketReceivedCallback)
729 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
730 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
733 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
734 g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
737 isHeaderAvailable = false;
738 remoteEndpoint = NULL;
740 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
743 if (false == g_dataReceiverHandlerState)
745 OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
747 CAFreeEndpoint(remoteEndpoint);
748 ca_mutex_unlock(g_bleClientReceiveDataMutex);
752 ca_mutex_unlock(g_bleClientReceiveDataMutex);
753 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
756 void CALEServerSendDataThread(void *threadData)
758 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
760 CALEData_t *bleData = (CALEData_t *) threadData;
763 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
767 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
768 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
770 int32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
772 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server total Data length with header is [%d]", totalLength);
773 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
774 if (NULL == dataSegment)
776 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
781 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
782 if (CA_STATUS_OK != result )
784 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
786 OICFree(dataSegment);
790 memcpy(dataSegment, header, CA_HEADER_LENGTH);
794 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
796 length = totalLength;
797 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
801 length = CA_SUPPORTED_BLE_MTU_SIZE;
802 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
803 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
806 int32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
808 // Send the first segment with the header.
809 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
811 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
812 result = CAUpdateCharacteristicsToGattClient(
813 bleData->remoteEndpoint->addr, dataSegment, length);
814 if (CA_STATUS_OK != result)
816 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
817 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
818 OICFree(dataSegment);
822 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
823 for (index = 1; index < iter; index++)
825 // Send the remaining header.
826 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
827 result = CAUpdateCharacteristicsToGattClient(
828 bleData->remoteEndpoint->addr,
829 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
830 CA_SUPPORTED_BLE_MTU_SIZE);
831 if (CA_STATUS_OK != result)
833 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
834 "Update characteristics failed, result [%d]", result);
835 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
836 OICFree(dataSegment);
839 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
840 CA_SUPPORTED_BLE_MTU_SIZE);
843 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
844 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
846 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
847 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
848 result = CAUpdateCharacteristicsToGattClient(
849 bleData->remoteEndpoint->addr,
850 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
852 if (CA_STATUS_OK != result)
854 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
856 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
857 OICFree(dataSegment);
860 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
865 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
866 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
867 if (CA_STATUS_OK != result)
869 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
871 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
872 OICFree(dataSegment);
875 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
876 for (index = 1; index < iter; index++)
878 // Send the remaining header.
879 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
880 result = CAUpdateCharacteristicsToAllGattClients(
881 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
882 CA_SUPPORTED_BLE_MTU_SIZE);
883 if (CA_STATUS_OK != result)
885 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
887 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
888 OICFree(dataSegment);
891 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
894 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
895 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
897 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
898 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
899 result = CAUpdateCharacteristicsToAllGattClients(
900 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
902 if (CA_STATUS_OK != result)
904 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
906 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
907 OICFree(dataSegment);
910 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
913 OICFree(dataSegment);
915 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
918 void CALEClientSendDataThread(void *threadData)
920 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
922 CALEData_t *bleData = (CALEData_t *) threadData;
925 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
929 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
930 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
932 uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
933 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
934 if (NULL == dataSegment)
936 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
941 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
942 if (CA_STATUS_OK != result )
944 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
946 OICFree(dataSegment);
949 memcpy(dataSegment, header, CA_HEADER_LENGTH);
953 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
955 length = totalLength;
956 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
957 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
961 length = CA_SUPPORTED_BLE_MTU_SIZE;
962 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
963 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
964 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
967 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
969 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
971 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
972 // Send the first segment with the header.
973 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addr,
978 if (CA_STATUS_OK != result)
980 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
981 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
982 OICFree(dataSegment);
986 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
987 for (index = 1; index < iter; index++)
989 // Send the remaining header.
990 result = CAUpdateCharacteristicsToGattServer(
991 bleData->remoteEndpoint->addr,
992 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
993 CA_SUPPORTED_BLE_MTU_SIZE,
995 if (CA_STATUS_OK != result)
997 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
999 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1000 OICFree(dataSegment);
1003 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1004 CA_SUPPORTED_BLE_MTU_SIZE);
1007 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1008 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1010 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1011 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1012 result = CAUpdateCharacteristicsToGattServer(
1013 bleData->remoteEndpoint->addr,
1014 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1018 if (CA_STATUS_OK != result)
1020 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1022 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1023 OICFree(dataSegment);
1026 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1031 //Sending Mulitcast Data
1032 // Send the first segment with the header.
1033 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1034 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1035 if (CA_STATUS_OK != result)
1037 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1038 "Update characteristics (all) failed, result [%d]", result);
1039 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1040 OICFree(dataSegment);
1043 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1044 // Send the remaining header.
1045 for (index = 1; index < iter; index++)
1047 result = CAUpdateCharacteristicsToAllGattServers(
1048 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1049 CA_SUPPORTED_BLE_MTU_SIZE);
1050 if (CA_STATUS_OK != result)
1052 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]",
1054 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1055 OICFree(dataSegment);
1058 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1059 CA_SUPPORTED_BLE_MTU_SIZE);
1062 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1063 if ( remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1065 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1066 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1067 result = CAUpdateCharacteristicsToAllGattServers(
1068 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1070 if (CA_STATUS_OK != result)
1072 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1073 "Update characteristics (all) failed, result [%d]", result);
1074 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1075 OICFree(dataSegment);
1078 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1083 OICFree(dataSegment);
1085 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1088 CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint, const void *data,
1089 uint32_t dataLength)
1091 CALEData_t *bleData = (CALEData_t *) OICMalloc(sizeof(CALEData_t));
1094 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1098 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1099 bleData->data = (void *)OICCalloc(dataLength + 1, 1);
1100 if (NULL == bleData->data)
1102 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1103 CAFreeLEData(bleData);
1106 memcpy(bleData->data, data, dataLength);
1107 bleData->dataLen = dataLength;
1112 void CAFreeLEData(CALEData_t *bleData)
1114 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1116 CAFreeEndpoint(bleData->remoteEndpoint);
1117 OICFree(bleData->data);
1121 void CALEDataDestroyer(void *data, uint32_t size)
1123 CALEData_t *ledata = (CALEData_t *) data;
1125 CAFreeLEData(ledata);
1129 CAResult_t CAInitLEAdapterMutex()
1131 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1133 if (NULL == g_bleIsServerMutex)
1135 g_bleIsServerMutex = ca_mutex_new();
1136 if (NULL == g_bleIsServerMutex)
1138 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1139 return CA_STATUS_FAILED;
1143 if (NULL == g_bleNetworkCbMutex)
1145 g_bleNetworkCbMutex = ca_mutex_new();
1146 if (NULL == g_bleNetworkCbMutex)
1148 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1149 CATerminateLEAdapterMutex();
1150 return CA_STATUS_FAILED;
1154 if (NULL == g_bleLocalAddressMutex)
1156 g_bleLocalAddressMutex = ca_mutex_new();
1157 if (NULL == g_bleLocalAddressMutex)
1159 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1160 CATerminateLEAdapterMutex();
1161 return CA_STATUS_FAILED;
1165 if (NULL == g_bleAdapterThreadPoolMutex)
1167 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1168 if (NULL == g_bleAdapterThreadPoolMutex)
1170 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1171 CATerminateLEAdapterMutex();
1172 return CA_STATUS_FAILED;
1176 if (NULL == g_bleClientSendDataMutex)
1178 g_bleClientSendDataMutex = ca_mutex_new();
1179 if (NULL == g_bleClientSendDataMutex)
1181 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1182 CATerminateLEAdapterMutex();
1183 return CA_STATUS_FAILED;
1187 if (NULL == g_bleClientReceiveDataMutex)
1189 g_bleClientReceiveDataMutex = ca_mutex_new();
1190 if (NULL == g_bleClientReceiveDataMutex)
1192 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1193 CATerminateLEAdapterMutex();
1194 return CA_STATUS_FAILED;
1198 if (NULL == g_bleServerSendDataMutex)
1200 g_bleServerSendDataMutex = ca_mutex_new();
1201 if (NULL == g_bleServerSendDataMutex)
1203 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1204 CATerminateLEAdapterMutex();
1205 return CA_STATUS_FAILED;
1209 if (NULL == g_bleServerReceiveDataMutex)
1211 g_bleServerReceiveDataMutex = ca_mutex_new();
1212 if (NULL == g_bleServerReceiveDataMutex)
1214 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1215 CATerminateLEAdapterMutex();
1216 return CA_STATUS_FAILED;
1220 if (NULL == g_bleAdapterReqRespCbMutex)
1222 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1223 if (NULL == g_bleAdapterReqRespCbMutex)
1225 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1226 CATerminateLEAdapterMutex();
1227 return CA_STATUS_FAILED;
1231 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1232 return CA_STATUS_OK;
1235 void CATerminateLEAdapterMutex()
1237 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1239 ca_mutex_free(g_bleIsServerMutex);
1240 g_bleIsServerMutex = NULL;
1242 ca_mutex_free(g_bleNetworkCbMutex);
1243 g_bleNetworkCbMutex = NULL;
1245 ca_mutex_free(g_bleLocalAddressMutex);
1246 g_bleLocalAddressMutex = NULL;
1248 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1249 g_bleAdapterThreadPoolMutex = NULL;
1251 ca_mutex_free(g_bleClientSendDataMutex);
1252 g_bleClientSendDataMutex = NULL;
1254 ca_mutex_free(g_bleClientReceiveDataMutex);
1255 g_bleClientReceiveDataMutex = NULL;
1257 ca_mutex_free(g_bleServerSendDataMutex);
1258 g_bleServerSendDataMutex = NULL;
1260 ca_mutex_free(g_bleServerReceiveDataMutex);
1261 g_bleServerReceiveDataMutex = NULL;
1263 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1264 g_bleAdapterReqRespCbMutex = NULL;
1266 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1269 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1270 CANetworkPacketReceivedCallback reqRespCallback,
1271 CANetworkChangeCallback netCallback,
1272 CAErrorHandleCallback errorCallback, ca_thread_pool_t handle)
1274 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1277 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1278 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1279 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1281 CAResult_t result = CA_STATUS_OK;
1282 result = CAInitLEAdapterMutex();
1283 if (CA_STATUS_OK != result)
1285 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1286 return CA_STATUS_FAILED;
1288 result = CAInitializeLENetworkMonitor();
1289 if (CA_STATUS_OK != result)
1291 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1292 return CA_STATUS_FAILED;
1295 CAInitializeLEAdapter();
1297 CASetLEClientThreadPoolHandle(handle);
1298 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1299 CASetLEServerThreadPoolHandle(handle);
1300 CASetLEAdapterThreadPoolHandle(handle);
1301 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1302 CASetLEReqRespAdapterCallback(reqRespCallback);
1304 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1305 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1306 CALERegisterNetworkNotifications(netCallback);
1308 g_errorHandler = errorCallback;
1310 CAConnectivityHandler_t connHandler;
1311 connHandler.startAdapter = CAStartLE;
1312 connHandler.stopAdapter = CAStopLE;
1313 connHandler.startListenServer = CAStartLEListeningServer;
1314 connHandler.startDiscoveryServer = CAStartLEDiscoveryServer;
1315 connHandler.sendData = CASendLEUnicastData;
1316 connHandler.sendDataToAll = CASendLEMulticastData;
1317 connHandler.GetnetInfo = CAGetLEInterfaceInformation;
1318 connHandler.readData = CAReadLEData;
1319 connHandler.terminate = CATerminateLE;
1320 registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
1322 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1324 return CA_STATUS_OK;
1327 CAResult_t CAStartLE()
1329 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1330 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE, not implemented");
1331 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1332 return CA_STATUS_OK;
1335 CAResult_t CAStopLE()
1337 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1338 #ifndef SINGLE_THREAD
1342 ca_mutex_lock(g_bleIsServerMutex);
1343 if (true == g_isServer)
1345 CAStopLEGattServer();
1349 CAStopLEGattClient();
1351 ca_mutex_unlock(g_bleIsServerMutex);
1353 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1355 return CA_STATUS_OK;
1358 void CATerminateLE()
1360 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1362 CASetLEReqRespServerCallback(NULL);
1363 CASetLEReqRespClientCallback(NULL);
1364 CALERegisterNetworkNotifications(NULL);
1365 CASetLEReqRespAdapterCallback(NULL);
1366 CATerminateLENetworkMonitor();
1368 ca_mutex_lock(g_bleIsServerMutex);
1369 if (true == g_isServer)
1371 CATerminateLEGattServer();
1375 CATerminateLEGattClient();
1377 ca_mutex_unlock(g_bleIsServerMutex);
1379 #ifndef SINGLE_THREAD
1380 CATerminateLEQueues();
1382 CATerminateLEAdapterMutex();
1384 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1387 CAResult_t CAStartLEListeningServer()
1389 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1390 CAResult_t result = CA_STATUS_OK;
1391 #ifndef SINGLE_THREAD
1392 result = CAInitLEServerQueues();
1393 if (CA_STATUS_OK != result)
1395 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1396 return CA_STATUS_FAILED;
1400 result = CAGetLEAdapterState();
1401 if (CA_ADAPTER_NOT_ENABLED == result)
1403 gLeServerStatus = CA_LISTENING_SERVER;
1404 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1405 return CA_STATUS_OK;
1408 if (CA_STATUS_FAILED == result)
1410 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1411 return CA_STATUS_FAILED;
1414 CAStartLEGattServer();
1416 ca_mutex_lock(g_bleIsServerMutex);
1418 ca_mutex_unlock(g_bleIsServerMutex);
1420 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1421 return CA_STATUS_OK;
1424 CAResult_t CAStartLEDiscoveryServer()
1426 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1427 CAResult_t result = CA_STATUS_OK;
1428 #ifndef SINGLE_THREAD
1429 result = CAInitLEClientQueues();
1430 if (CA_STATUS_OK != result)
1432 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1433 return CA_STATUS_FAILED;
1436 result = CAGetLEAdapterState();
1437 if (CA_ADAPTER_NOT_ENABLED == result)
1439 gLeServerStatus = CA_DISCOVERY_SERVER;
1440 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1441 return CA_STATUS_OK;
1444 if (CA_STATUS_FAILED == result)
1446 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1447 return CA_STATUS_FAILED;
1450 CAStartLEGattClient();
1452 ca_mutex_lock(g_bleIsServerMutex);
1454 ca_mutex_unlock(g_bleIsServerMutex);
1456 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1457 return CA_STATUS_OK;
1460 CAResult_t CAStartLENotifyServer()
1462 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1464 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1465 return CA_STATUS_OK;
1468 uint32_t CASendLENotification(const CAEndpoint_t *endpoint, const void *data,
1471 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1473 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1477 CAResult_t CAReadLEData()
1479 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1480 #ifdef SINGLE_THREAD
1483 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1484 return CA_STATUS_OK;
1487 int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
1489 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1492 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1493 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1495 CAResult_t result = CA_STATUS_FAILED;
1497 ca_mutex_lock(g_bleIsServerMutex);
1498 if (true == g_isServer)
1500 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1501 if (CA_STATUS_OK != result)
1503 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed\n");
1506 g_errorHandler((void *) endpoint, (void *) data, dataLen, result);
1508 ca_mutex_unlock(g_bleIsServerMutex);
1514 result = CALEAdapterClientSendData(endpoint, data, dataLen);
1515 if (CA_STATUS_OK != result)
1517 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed \n");
1520 g_errorHandler(endpoint, data, dataLen, result);
1522 ca_mutex_unlock(g_bleIsServerMutex);
1526 ca_mutex_unlock(g_bleIsServerMutex);
1528 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1532 int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
1534 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1537 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1541 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
1545 CAResult_t result = CA_STATUS_FAILED;
1547 ca_mutex_lock(g_bleIsServerMutex);
1548 if (true == g_isServer)
1550 result = CALEAdapterServerSendData(NULL, data, dataLen);
1551 if (CA_STATUS_OK != result)
1553 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data failed" );
1555 ca_mutex_unlock(g_bleIsServerMutex);
1558 g_errorHandler(endpoint, data, dataLen, result);
1565 result = CALEAdapterClientSendData(NULL, data, dataLen);
1566 if (CA_STATUS_OK != result)
1568 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data failed" );
1571 g_errorHandler(endpoint, data, dataLen, result);
1573 ca_mutex_unlock(g_bleIsServerMutex);
1577 ca_mutex_unlock(g_bleIsServerMutex);
1579 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1583 CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
1585 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1587 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
1589 char *local_address = NULL;
1591 CAResult_t res = CAGetLEAddress(&local_address);
1592 if (CA_STATUS_OK != res)
1594 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
1598 if (NULL == local_address)
1600 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
1601 return CA_STATUS_FAILED;
1605 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
1606 if (NULL == (*info))
1608 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
1609 OICFree(local_address);
1610 return CA_STATUS_FAILED;
1613 size_t local_address_len = strlen(local_address);
1615 if(local_address_len >= sizeof(g_localBLEAddress) ||
1616 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
1618 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
1620 OICFree(local_address);
1621 return CA_STATUS_FAILED;
1624 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
1625 ca_mutex_lock(g_bleLocalAddressMutex);
1626 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
1627 ca_mutex_unlock(g_bleLocalAddressMutex);
1629 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
1631 OICFree(local_address);
1633 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1634 return CA_STATUS_OK;
1637 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
1639 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1641 ca_mutex_lock(g_bleNetworkCbMutex);
1642 g_networkCallback = netCallback;
1643 ca_mutex_unlock(g_bleNetworkCbMutex);
1644 CAResult_t res = CA_STATUS_OK;
1647 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
1648 if (CA_STATUS_OK != res)
1650 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1655 res = CAUnSetLEAdapterStateChangedCb();
1656 if (CA_STATUS_OK != res)
1658 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1662 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1666 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
1668 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1670 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
1671 CAEndpoint_t localEndpoint = {};
1673 ca_mutex_lock(g_bleLocalAddressMutex);
1674 OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), g_localBLEAddress);
1675 ca_mutex_unlock(g_bleLocalAddressMutex);
1677 g_bleAdapterState = adapter_state;
1678 // Start a GattServer/Client if gLeServerStatus is SET
1679 if (CA_LISTENING_SERVER == gLeServerStatus)
1681 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1682 CAStartLEGattServer();
1684 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
1686 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
1687 CAStartLEGattClient();
1689 gLeServerStatus = CA_SERVER_NOTSTARTED;
1691 ca_mutex_lock(g_bleNetworkCbMutex);
1692 if (NULL != g_networkCallback)
1694 g_networkCallback(&localEndpoint, adapter_state);
1698 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
1700 ca_mutex_unlock(g_bleNetworkCbMutex);
1702 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1705 CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1709 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1711 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
1712 #ifndef SINGLE_THREAD
1713 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
1714 "g_bleClientSendQueueHandle is NULL",
1716 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
1717 "g_bleClientSendDataMutex is NULL",
1720 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
1723 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1725 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
1728 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1729 return CA_MEMORY_ALLOC_FAILED;
1731 // Add message to send queue
1732 ca_mutex_lock(g_bleClientSendDataMutex);
1733 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
1734 ca_mutex_unlock(g_bleClientSendDataMutex);
1736 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1737 return CA_STATUS_OK;
1741 CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1745 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1747 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
1749 #ifdef SINGLE_THREAD
1750 char header[CA_HEADER_LENGTH] = {0};
1752 CAResult_t result = CAGenerateHeader(header, dataLen);
1754 if (CA_STATUS_OK != result)
1756 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1760 if (!CAIsLEConnected())
1762 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
1766 result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH);
1767 if (CA_STATUS_OK != result)
1769 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
1773 int32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
1774 for (int32_t iter = 0; iter < dataLimit; iter++)
1776 result = CAUpdateCharacteristicsToAllGattClients((data +
1777 (iter * CA_SUPPORTED_BLE_MTU_SIZE)),
1778 CA_SUPPORTED_BLE_MTU_SIZE);
1779 if (CA_STATUS_OK != result)
1781 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
1787 uint8_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
1790 result = CAUpdateCharacteristicsToAllGattClients((data +
1791 (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE)),
1793 if (CA_STATUS_OK != result)
1795 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
1801 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
1802 "BleClientReceiverQueue is NULL",
1804 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
1805 "BleClientSendDataMutex is NULL",
1808 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
1811 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1813 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
1816 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1817 return CA_MEMORY_ALLOC_FAILED;
1819 // Add message to send queue
1820 ca_mutex_lock(g_bleServerSendDataMutex);
1821 CAQueueingThreadAddData(g_bleServerSendQueueHandle, bleData, sizeof(CALEData_t));
1822 ca_mutex_unlock(g_bleServerSendDataMutex);
1824 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1825 return CA_STATUS_OK;
1828 CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress, const char *serviceUUID,
1829 const void *data, uint32_t dataLength,
1830 uint32_t *sentLength)
1832 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1835 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1836 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1837 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1839 #ifdef SINGLE_THREAD
1840 if(g_networkPacketReceivedCallback)
1842 CAEndpoint_t endPoint = { 0 }; // will be filled by upper layer
1843 endPoint.adapter = CA_ADAPTER_GATT_BTLE;
1844 g_networkPacketReceivedCallback(&endPoint, data, dataLength);
1847 VERIFY_NON_NULL_RET(g_bleServerReceiverQueue, CALEADAPTER_TAG, "g_bleServerReceiverQueue",
1850 //Add message to data queue
1851 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
1852 CA_ADAPTER_GATT_BTLE,
1854 if (NULL == remoteEndpoint)
1856 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1857 return CA_STATUS_FAILED;
1860 // Create bleData to add to queue
1861 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1863 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
1866 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1867 CAFreeEndpoint(remoteEndpoint);
1868 return CA_MEMORY_ALLOC_FAILED;
1871 CAFreeEndpoint(remoteEndpoint);
1872 // Add message to send queue
1873 CAQueueingThreadAddData(g_bleServerReceiverQueue, bleData, sizeof(CALEData_t));
1875 *sentLength = dataLength;
1877 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1878 return CA_STATUS_OK;
1881 CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress, const char *serviceUUID,
1882 const void *data, uint32_t dataLength,
1883 uint32_t *sentLength)
1885 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1888 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1889 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1890 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1891 #ifndef SINGLE_THREAD
1892 VERIFY_NON_NULL_RET(g_bleClientReceiverQueue, CALEADAPTER_TAG, "g_bleClientReceiverQueue",
1895 //Add message to data queue
1896 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
1897 CA_ADAPTER_GATT_BTLE,
1899 if (NULL == remoteEndpoint)
1901 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1902 return CA_STATUS_FAILED;
1905 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1907 // Create bleData to add to queue
1908 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
1911 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1912 CAFreeEndpoint(remoteEndpoint);
1913 return CA_MEMORY_ALLOC_FAILED;
1916 CAFreeEndpoint(remoteEndpoint);
1917 // Add message to send queue
1918 CAQueueingThreadAddData(g_bleClientReceiverQueue, bleData, sizeof(CALEData_t));
1920 *sentLength = dataLength;
1922 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1923 return CA_STATUS_OK;
1926 void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
1928 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1930 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1931 g_bleAdapterThreadPool = handle;
1932 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1934 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1937 void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
1939 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1941 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1943 g_networkPacketReceivedCallback = callback;
1945 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1947 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1950 void CALEErrorHandler(const char *remoteAddress, const void *data, uint32_t dataLen,
1953 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
1955 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
1956 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_GATT_BTLE,
1958 //if required, will be used to build remote end point
1959 g_errorHandler(rep, data, dataLen, result);
1961 CAFreeEndpoint(rep);
1962 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");