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, NULL, "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 return CA_STATUS_FAILED;
1153 if (NULL == g_bleLocalAddressMutex)
1155 g_bleLocalAddressMutex = ca_mutex_new();
1156 if (NULL == g_bleLocalAddressMutex)
1158 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1159 return CA_STATUS_FAILED;
1163 if (NULL == g_bleAdapterThreadPoolMutex)
1165 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1166 if (NULL == g_bleAdapterThreadPoolMutex)
1168 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1169 return CA_STATUS_FAILED;
1173 if (NULL == g_bleClientSendDataMutex)
1175 g_bleClientSendDataMutex = ca_mutex_new();
1176 if (NULL == g_bleClientSendDataMutex)
1178 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1179 return CA_STATUS_FAILED;
1183 if (NULL == g_bleClientReceiveDataMutex)
1185 g_bleClientReceiveDataMutex = ca_mutex_new();
1186 if (NULL == g_bleClientReceiveDataMutex)
1188 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1189 return CA_STATUS_FAILED;
1193 if (NULL == g_bleServerSendDataMutex)
1195 g_bleServerSendDataMutex = ca_mutex_new();
1196 if (NULL == g_bleServerSendDataMutex)
1198 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1199 return CA_STATUS_FAILED;
1203 if (NULL == g_bleServerReceiveDataMutex)
1205 g_bleServerReceiveDataMutex = ca_mutex_new();
1206 if (NULL == g_bleServerReceiveDataMutex)
1208 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1209 return CA_STATUS_FAILED;
1213 if (NULL == g_bleAdapterReqRespCbMutex)
1215 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1216 if (NULL == g_bleAdapterReqRespCbMutex)
1218 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1219 return CA_STATUS_FAILED;
1223 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1224 return CA_STATUS_OK;
1227 void CATerminateLEAdapterMutex()
1229 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1231 ca_mutex_free(g_bleIsServerMutex);
1232 g_bleIsServerMutex = NULL;
1234 ca_mutex_free(g_bleNetworkCbMutex);
1235 g_bleNetworkCbMutex = NULL;
1237 ca_mutex_free(g_bleLocalAddressMutex);
1238 g_bleLocalAddressMutex = NULL;
1240 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1241 g_bleAdapterThreadPoolMutex = NULL;
1243 ca_mutex_free(g_bleClientSendDataMutex);
1244 g_bleClientSendDataMutex = NULL;
1246 ca_mutex_free(g_bleClientReceiveDataMutex);
1247 g_bleClientReceiveDataMutex = NULL;
1249 ca_mutex_free(g_bleServerSendDataMutex);
1250 g_bleServerSendDataMutex = NULL;
1252 ca_mutex_free(g_bleServerReceiveDataMutex);
1253 g_bleServerReceiveDataMutex = NULL;
1255 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1256 g_bleAdapterReqRespCbMutex = NULL;
1258 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1261 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1262 CANetworkPacketReceivedCallback reqRespCallback,
1263 CANetworkChangeCallback netCallback,
1264 CAErrorHandleCallback errorCallback, ca_thread_pool_t handle)
1266 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1269 VERIFY_NON_NULL(registerCallback, NULL, "RegisterConnectivity callback is null");
1270 VERIFY_NON_NULL(reqRespCallback, NULL, "PacketReceived Callback is null");
1271 VERIFY_NON_NULL(netCallback, NULL, "NetworkChange Callback is null");
1273 CAResult_t result = CA_STATUS_OK;
1274 result = CAInitLEAdapterMutex();
1275 if (CA_STATUS_OK != result)
1277 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1278 return CA_STATUS_FAILED;
1280 result = CAInitializeLENetworkMonitor();
1281 if (CA_STATUS_OK != result)
1283 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1284 return CA_STATUS_FAILED;
1287 CAInitializeLEAdapter();
1289 CASetLEClientThreadPoolHandle(handle);
1290 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1291 CASetLEServerThreadPoolHandle(handle);
1292 CASetLEAdapterThreadPoolHandle(handle);
1293 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1294 CASetLEReqRespAdapterCallback(reqRespCallback);
1296 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1297 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1298 CALERegisterNetworkNotifications(netCallback);
1300 g_errorHandler = errorCallback;
1302 CAConnectivityHandler_t connHandler;
1303 connHandler.startAdapter = CAStartLE;
1304 connHandler.stopAdapter = CAStopLE;
1305 connHandler.startListenServer = CAStartLEListeningServer;
1306 connHandler.startDiscoveryServer = CAStartLEDiscoveryServer;
1307 connHandler.sendData = CASendLEUnicastData;
1308 connHandler.sendDataToAll = CASendLEMulticastData;
1309 connHandler.GetnetInfo = CAGetLEInterfaceInformation;
1310 connHandler.readData = CAReadLEData;
1311 connHandler.terminate = CATerminateLE;
1312 registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
1314 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1316 return CA_STATUS_OK;
1319 CAResult_t CAStartLE()
1321 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1322 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE, not implemented");
1323 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1324 return CA_STATUS_OK;
1327 CAResult_t CAStopLE()
1329 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1330 #ifndef SINGLE_THREAD
1334 ca_mutex_lock(g_bleIsServerMutex);
1335 if (true == g_isServer)
1337 CAStopLEGattServer();
1341 CAStopLEGattClient();
1343 ca_mutex_unlock(g_bleIsServerMutex);
1345 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1347 return CA_STATUS_OK;
1350 void CATerminateLE()
1352 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1354 CASetLEReqRespServerCallback(NULL);
1355 CASetLEReqRespClientCallback(NULL);
1356 CALERegisterNetworkNotifications(NULL);
1357 CASetLEReqRespAdapterCallback(NULL);
1358 CATerminateLENetworkMonitor();
1360 ca_mutex_lock(g_bleIsServerMutex);
1361 if (true == g_isServer)
1363 CATerminateLEGattServer();
1367 CATerminateLEGattClient();
1369 ca_mutex_unlock(g_bleIsServerMutex);
1371 #ifndef SINGLE_THREAD
1372 CATerminateLEQueues();
1374 CATerminateLEAdapterMutex();
1376 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1379 CAResult_t CAStartLEListeningServer()
1381 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1382 CAResult_t result = CA_STATUS_OK;
1383 #ifndef SINGLE_THREAD
1384 result = CAInitLEServerQueues();
1385 if (CA_STATUS_OK != result)
1387 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1388 return CA_STATUS_FAILED;
1392 result = CAGetLEAdapterState();
1393 if (CA_ADAPTER_NOT_ENABLED == result)
1395 gLeServerStatus = CA_LISTENING_SERVER;
1396 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1397 return CA_STATUS_OK;
1400 if (CA_STATUS_FAILED == result)
1402 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1403 return CA_STATUS_FAILED;
1406 CAStartLEGattServer();
1408 ca_mutex_lock(g_bleIsServerMutex);
1410 ca_mutex_unlock(g_bleIsServerMutex);
1412 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1413 return CA_STATUS_OK;
1416 CAResult_t CAStartLEDiscoveryServer()
1418 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1419 CAResult_t result = CA_STATUS_OK;
1420 #ifndef SINGLE_THREAD
1421 result = CAInitLEClientQueues();
1422 if (CA_STATUS_OK != result)
1424 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1425 return CA_STATUS_FAILED;
1428 result = CAGetLEAdapterState();
1429 if (CA_ADAPTER_NOT_ENABLED == result)
1431 gLeServerStatus = CA_DISCOVERY_SERVER;
1432 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1433 return CA_STATUS_OK;
1436 if (CA_STATUS_FAILED == result)
1438 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1439 return CA_STATUS_FAILED;
1442 CAStartLEGattClient();
1444 ca_mutex_lock(g_bleIsServerMutex);
1446 ca_mutex_unlock(g_bleIsServerMutex);
1448 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1449 return CA_STATUS_OK;
1452 CAResult_t CAStartLENotifyServer()
1454 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1456 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1457 return CA_STATUS_OK;
1460 uint32_t CASendLENotification(const CAEndpoint_t *endpoint, const void *data,
1463 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1465 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1469 CAResult_t CAReadLEData()
1471 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1472 #ifdef SINGLE_THREAD
1475 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1476 return CA_STATUS_OK;
1479 int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
1481 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1484 VERIFY_NON_NULL_RET(endpoint, NULL, "Remote endpoint is null", -1);
1485 VERIFY_NON_NULL_RET(data, NULL, "Data is null", -1);
1487 CAResult_t result = CA_STATUS_FAILED;
1489 ca_mutex_lock(g_bleIsServerMutex);
1490 if (true == g_isServer)
1492 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1493 if (CA_STATUS_OK != result)
1495 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed\n");
1498 g_errorHandler((void *) endpoint, (void *) data, dataLen, result);
1500 ca_mutex_unlock(g_bleIsServerMutex);
1506 result = CALEAdapterClientSendData(endpoint, data, dataLen);
1507 if (CA_STATUS_OK != result)
1509 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed \n");
1512 g_errorHandler(endpoint, data, dataLen, result);
1514 ca_mutex_unlock(g_bleIsServerMutex);
1518 ca_mutex_unlock(g_bleIsServerMutex);
1520 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1524 int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
1526 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1529 VERIFY_NON_NULL_RET(data, NULL, "Data is null", -1);
1533 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
1537 CAResult_t result = CA_STATUS_FAILED;
1539 ca_mutex_lock(g_bleIsServerMutex);
1540 if (true == g_isServer)
1542 result = CALEAdapterServerSendData(NULL, data, dataLen);
1543 if (CA_STATUS_OK != result)
1545 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data failed" );
1547 ca_mutex_unlock(g_bleIsServerMutex);
1550 g_errorHandler(endpoint, data, dataLen, result);
1557 result = CALEAdapterClientSendData(NULL, data, dataLen);
1558 if (CA_STATUS_OK != result)
1560 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data failed" );
1563 g_errorHandler(endpoint, data, dataLen, result);
1565 ca_mutex_unlock(g_bleIsServerMutex);
1569 ca_mutex_unlock(g_bleIsServerMutex);
1571 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1575 CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
1577 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1579 VERIFY_NON_NULL(info, NULL, "CALocalConnectivity info is null");
1581 char *local_address = NULL;
1583 CAResult_t res = CAGetLEAddress(&local_address);
1584 if (CA_STATUS_OK != res)
1586 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
1590 if (NULL == local_address)
1592 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
1593 return CA_STATUS_FAILED;
1597 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
1598 if (NULL == (*info))
1600 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
1601 OICFree(local_address);
1602 return CA_STATUS_FAILED;
1605 size_t local_address_len = strlen(local_address);
1607 if(local_address_len >= sizeof(g_localBLEAddress) ||
1608 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
1610 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
1612 OICFree(local_address);
1613 return CA_STATUS_FAILED;
1616 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
1617 ca_mutex_lock(g_bleLocalAddressMutex);
1618 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
1619 ca_mutex_unlock(g_bleLocalAddressMutex);
1621 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
1623 OICFree(local_address);
1625 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1626 return CA_STATUS_OK;
1629 CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
1631 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1633 ca_mutex_lock(g_bleNetworkCbMutex);
1634 g_networkCallback = netCallback;
1635 ca_mutex_unlock(g_bleNetworkCbMutex);
1636 CAResult_t res = CA_STATUS_OK;
1639 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
1640 if (CA_STATUS_OK != res)
1642 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1647 res = CAUnSetLEAdapterStateChangedCb();
1648 if (CA_STATUS_OK != res)
1650 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1654 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1658 void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
1660 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1662 VERIFY_NON_NULL_VOID(g_localBLEAddress, NULL, "g_localBLEAddress is null");
1663 CAEndpoint_t localEndpoint = {};
1665 ca_mutex_lock(g_bleLocalAddressMutex);
1666 OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), g_localBLEAddress);
1667 ca_mutex_unlock(g_bleLocalAddressMutex);
1669 g_bleAdapterState = adapter_state;
1670 // Start a GattServer/Client if gLeServerStatus is SET
1671 if (CA_LISTENING_SERVER == gLeServerStatus)
1673 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1674 CAStartLEGattServer();
1676 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
1678 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
1679 CAStartLEGattClient();
1681 gLeServerStatus = CA_SERVER_NOTSTARTED;
1683 ca_mutex_lock(g_bleNetworkCbMutex);
1684 if (NULL != g_networkCallback)
1686 g_networkCallback(&localEndpoint, adapter_state);
1690 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
1692 ca_mutex_unlock(g_bleNetworkCbMutex);
1694 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1697 CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1701 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1703 VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1704 #ifndef SINGLE_THREAD
1705 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
1706 "g_bleClientSendQueueHandle is NULL",
1708 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
1709 "g_bleClientSendDataMutex is NULL",
1712 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
1715 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1717 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
1720 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1721 return CA_MEMORY_ALLOC_FAILED;
1723 // Add message to send queue
1724 ca_mutex_lock(g_bleClientSendDataMutex);
1725 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
1726 ca_mutex_unlock(g_bleClientSendDataMutex);
1728 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1729 return CA_STATUS_OK;
1733 CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1737 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1739 VERIFY_NON_NULL(data, NULL, "Param data is NULL");
1741 #ifdef SINGLE_THREAD
1742 char header[CA_HEADER_LENGTH] = {0};
1744 CAResult_t result = CAGenerateHeader(header, dataLen);
1746 if (CA_STATUS_OK != result)
1748 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1752 if (!CAIsLEConnected())
1754 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
1758 result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH);
1759 if (CA_STATUS_OK != result)
1761 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
1765 int32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
1766 for (int32_t iter = 0; iter < dataLimit; iter++)
1768 result = CAUpdateCharacteristicsToAllGattClients((data +
1769 (iter * CA_SUPPORTED_BLE_MTU_SIZE)),
1770 CA_SUPPORTED_BLE_MTU_SIZE);
1771 if (CA_STATUS_OK != result)
1773 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
1779 uint8_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
1782 result = CAUpdateCharacteristicsToAllGattClients((data +
1783 (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE)),
1785 if (CA_STATUS_OK != result)
1787 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
1793 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
1794 "BleClientReceiverQueue is NULL",
1796 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
1797 "BleClientSendDataMutex is NULL",
1800 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
1803 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1805 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
1808 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1809 return CA_MEMORY_ALLOC_FAILED;
1811 // Add message to send queue
1812 ca_mutex_lock(g_bleServerSendDataMutex);
1813 CAQueueingThreadAddData(g_bleServerSendQueueHandle, bleData, sizeof(CALEData_t));
1814 ca_mutex_unlock(g_bleServerSendDataMutex);
1816 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1817 return CA_STATUS_OK;
1820 CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress, const char *serviceUUID,
1821 const void *data, uint32_t dataLength,
1822 uint32_t *sentLength)
1824 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1827 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1828 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1829 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1831 #ifdef SINGLE_THREAD
1832 if(g_networkPacketReceivedCallback)
1834 CAEndpoint_t endPoint = { 0 }; // will be filled by upper layer
1835 endPoint.adapter = CA_ADAPTER_GATT_BTLE;
1836 g_networkPacketReceivedCallback(&endPoint, data, dataLength);
1839 VERIFY_NON_NULL_RET(g_bleServerReceiverQueue, CALEADAPTER_TAG, "g_bleServerReceiverQueue",
1842 //Add message to data queue
1843 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
1844 CA_ADAPTER_GATT_BTLE,
1846 if (NULL == remoteEndpoint)
1848 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1849 return CA_STATUS_FAILED;
1852 // Create bleData to add to queue
1853 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1855 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
1858 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1859 CAFreeEndpoint(remoteEndpoint);
1860 return CA_MEMORY_ALLOC_FAILED;
1863 CAFreeEndpoint(remoteEndpoint);
1864 // Add message to send queue
1865 CAQueueingThreadAddData(g_bleServerReceiverQueue, bleData, sizeof(CALEData_t));
1867 *sentLength = dataLength;
1869 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1870 return CA_STATUS_OK;
1873 CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress, const char *serviceUUID,
1874 const void *data, uint32_t dataLength,
1875 uint32_t *sentLength)
1877 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1880 VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
1881 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
1882 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
1883 #ifndef SINGLE_THREAD
1884 VERIFY_NON_NULL_RET(g_bleClientReceiverQueue, CALEADAPTER_TAG, "g_bleClientReceiverQueue",
1887 //Add message to data queue
1888 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
1889 CA_ADAPTER_GATT_BTLE,
1891 if (NULL == remoteEndpoint)
1893 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
1894 return CA_STATUS_FAILED;
1897 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
1899 // Create bleData to add to queue
1900 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
1903 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1904 CAFreeEndpoint(remoteEndpoint);
1905 return CA_MEMORY_ALLOC_FAILED;
1908 CAFreeEndpoint(remoteEndpoint);
1909 // Add message to send queue
1910 CAQueueingThreadAddData(g_bleClientReceiverQueue, bleData, sizeof(CALEData_t));
1912 *sentLength = dataLength;
1914 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1915 return CA_STATUS_OK;
1918 void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
1920 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1922 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1923 g_bleAdapterThreadPool = handle;
1924 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
1926 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1929 void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
1931 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1933 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1935 g_networkPacketReceivedCallback = callback;
1937 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1939 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1942 void CALEErrorHandler(const char *remoteAddress, const void *data, uint32_t dataLen,
1945 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
1947 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
1948 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_GATT_BTLE,
1950 //if required, will be used to build remote end point
1951 g_errorHandler(rep, data, dataLen, result);
1953 CAFreeEndpoint(rep);
1954 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");