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 "oic_malloc.h"
33 #include "oic_string.h"
34 #include "caremotehandler.h"
38 * Logging tag for module name.
40 #define CALEADAPTER_TAG "OIC_CA_LE_ADAP"
43 * The MTU supported for BLE adapter
45 #define CA_SUPPORTED_BLE_MTU_SIZE 20
48 * Stores information of all the senders.
50 * This structure will be used to track and defragment all incoming
56 uint32_t totalDataLen;
58 CAEndpoint_t *remoteEndpoint;
64 ADAPTER_BOTH_CLIENT_SERVER,
70 * Callback to provide the status of the network change to CA layer.
72 static CAAdapterChangeCallback g_networkCallback = NULL;
75 * Callback to provide the status of the connection change to CA layer.
77 static CAConnectionChangeCallback g_connectionCallback = NULL;
80 * bleAddress of the local adapter. Value will be initialized to zero,
81 * and will be updated later.
83 static char g_localBLEAddress[18] = { 0 };
86 * Variable to differentiate btw GattServer and GattClient.
88 static CABLEAdapter_t g_adapterType = ADAPTER_EMPTY;
91 * Mutex to synchronize the task to be executed on the GattServer
94 static ca_mutex g_bleIsServerMutex = NULL;
97 * Mutex to synchronize the callback to be called for the network
100 static ca_mutex g_bleNetworkCbMutex = NULL;
103 * Mutex to synchronize the updates of the local LE address of the
106 static ca_mutex g_bleLocalAddressMutex = NULL;
109 * Reference to thread pool.
111 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
114 * Mutex to synchronize the task to be pushed to thread pool.
116 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
119 * Mutex to synchronize the queing of the data from SenderQueue.
121 static ca_mutex g_bleClientSendDataMutex = NULL;
124 * Mutex to synchronize the queing of the data from ReceiverQueue.
126 static ca_mutex g_bleReceiveDataMutex = NULL;
129 * Mutex to synchronize the queing of the data from SenderQueue.
131 static ca_mutex g_bleServerSendDataMutex = NULL;
134 * Mutex to synchronize the callback to be called for the
135 * adapterReqResponse.
137 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
140 * Callback to be called when network packet received from either
141 * GattServer or GattClient.
143 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
146 * Callback to notify error from the BLE adapter.
148 static CAErrorHandleCallback g_errorHandler = NULL;
151 * Register network change notification callback.
153 * @param[in] netCallback CAAdapterChangeCallback callback which will
154 * be set for the change in adapter.
155 * @param[in] connCallback CAConnectionChangeCallback callback which will
156 * be set for the change in connection.
158 * @return 0 on success otherwise a positive error value.
159 * @retval ::CA_STATUS_OK Successful.
160 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
161 * @retval ::CA_STATUS_FAILED Operation failed.
164 static CAResult_t CALERegisterNetworkNotifications(CAAdapterChangeCallback netCallback,
165 CAConnectionChangeCallback connCallback);
168 * Set the thread pool handle which is required for spawning new
171 * @param[in] handle Thread pool handle which is given by above layer
172 * for using thread creation task.
175 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
178 * Call the callback to the upper layer when the adapter state gets
181 * @param[in] adapter_state New state of the adapter to be notified to
184 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state);
187 * Call the callback to the upper layer when the device connection state gets
190 * @param[in] address LE address of the device to be notified to the upper layer.
191 * @param[in] isConnected whether connection state is connected or not.
193 static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
197 * Used to initialize all required mutex variable for LE Adapter
200 * @return 0 on success otherwise a positive error value.
201 * @retval ::CA_STATUS_OK Successful.
202 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
203 * @retval ::CA_STATUS_FAILED Operation failed.
206 static CAResult_t CAInitLEAdapterMutex();
209 * Terminate all required mutex variables for LE adapter
212 static void CATerminateLEAdapterMutex();
215 * Prepares and notify error through error callback.
217 static void CALEErrorHandler(const char *remoteAddress,
222 #ifndef SINGLE_THREAD
224 * Stop condition of recvhandler.
226 static bool g_dataBleReceiverHandlerState = false;
229 * Sender information.
231 static u_arraylist_t *g_bleServerSenderInfo = NULL;
233 static u_arraylist_t *g_bleClientSenderInfo = NULL;
236 * Queue to process the outgoing packets from GATTClient.
238 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
241 * Queue to process the incoming packets.
243 static CAQueueingThread_t *g_bleReceiverQueue = NULL;
246 * Queue to process the outgoing packets from GATTServer.
248 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
251 * This function will be associated with the sender queue for
254 * This function will fragment the data to the MTU of the transport
255 * and send the data in fragments to the adapters. The function will
256 * be blocked until all data is sent out from the adapter.
258 * @param[in] threadData Data pushed to the queue which contains the
259 * info about RemoteEndpoint and Data.
261 static void CALEServerSendDataThread(void *threadData);
264 * This function will be associated with the sender queue for
267 * This function will fragment the data to the MTU of the transport
268 * and send the data in fragments to the adapters. The function will
269 * be blocked until all data is sent out from the adapter.
271 * @param[in] threadData Data pushed to the queue which contains the
272 * info about RemoteEndpoint and Data.
274 static void CALEClientSendDataThread(void *threadData);
277 * This function will be associated with the receiver queue.
279 * This function will defragment the received data from each sender
280 * respectively and will send it up to CA layer. Respective sender's
281 * header will provide the length of the data sent.
283 * @param[in] threadData Data pushed to the queue which contains the
284 * info about RemoteEndpoint and Data.
286 static void CALEDataReceiverHandler(void *threadData);
289 * This function will stop all queues created for GattServer and
290 * GattClient. All four queues will be be stopped with this function
293 static void CAStopLEQueues();
296 * This function will terminate all queues created for GattServer and
297 * GattClient. All four queues will be be terminated with this
298 * function invocations.
300 static void CATerminateLEQueues();
303 * This function will initalize the Receiver and Sender queues for
304 * GattServer. This function will in turn call the functions
305 * CAInitBleServerReceiverQueue() and CAInitBleServerSenderQueue() to
306 * initialize the queues.
308 * @return ::CA_STATUS_OK or Appropriate error code.
309 * @retval ::CA_STATUS_OK Successful.
310 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
311 * @retval ::CA_STATUS_FAILED Operation failed.
313 static CAResult_t CAInitLEServerQueues();
316 * This function will initalize the Receiver and Sender queues for
317 * GattClient. This function will inturn call the functions
318 * CAInitBleClientReceiverQueue() and CAInitBleClientSenderQueue() to
319 * initialize the queues.
321 * @return ::CA_STATUS_OK or Appropriate error code.
322 * @retval ::CA_STATUS_OK Successful.
323 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
324 * @retval ::CA_STATUS_FAILED Operation failed.
327 static CAResult_t CAInitLEClientQueues();
330 * This function will initalize the Receiver queue for
331 * GattServer. This will initialize the queue to process the function
332 * CABLEServerSendDataThread() when ever the task is added to this
335 * @return ::CA_STATUS_OK or Appropriate error code.
336 * @retval ::CA_STATUS_OK Successful.
337 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
338 * @retval ::CA_STATUS_FAILED Operation failed.
340 static CAResult_t CAInitLEServerSenderQueue();
343 * This function will initalize the Receiver queue for
344 * GattClient. This will initialize the queue to process the function
345 * CABLEClientSendDataThread() when ever the task is added to this
348 * @return ::CA_STATUS_OK or Appropriate error code.
349 * @retval ::CA_STATUS_OK Successful.
350 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
351 * @retval ::CA_STATUS_FAILED Operation failed.
353 static CAResult_t CAInitLEClientSenderQueue();
356 * This function will initialize the Receiver queue for
357 * LEAdapter. This will initialize the queue to process the function
358 * CABLEDataReceiverHandler() when ever the task is added to this
361 * @return ::CA_STATUS_OK or Appropriate error code
362 * @retval ::CA_STATUS_OK Successful
363 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
364 * @retval ::CA_STATUS_FAILED Operation failed
367 static CAResult_t CAInitLEReceiverQueue();
370 * This function will create the Data required to send it in the
373 * @param[in] remoteEndpoint Remote endpoint information of the
375 * @param[in] data Data to be transmitted from LE.
376 * @param[in] dataLength Length of the Data being transmitted.
378 * @return ::CA_STATUS_OK or Appropriate error code.
379 * @retval ::CA_STATUS_OK Successful.
380 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
381 * @retval ::CA_STATUS_FAILED Operation failed.
383 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
386 u_arraylist_t *senderInfo);
389 * Used to free the BLE information stored in the sender/receiver
392 * @param[in] bleData Information for a particular data segment.
394 static void CAFreeLEData(CALEData_t *bleData);
399 static void CALEDataDestroyer(void *data, uint32_t size);
401 #ifndef SINGLE_THREAD
403 * remove request or response data of send queue.
405 * @param[in] queueHandle queue to process the outgoing packets.
406 * @param[in] mutex mutex related to sender for client / server.
407 * @param[in] address target address to remove data in queue.
409 static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle,
411 const char* address);
414 * remove all received data of data list from receive queue.
416 * @param[in] dataInfoList received data list to remove for client / server.
417 * @param[in] address target address to remove data in queue.
419 static void CALERemoveReceiveQueueData(u_arraylist_t *dataInfoList,
420 const char* address);
423 static CAResult_t CAInitLEServerQueues()
425 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
427 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
429 CAResult_t result = CAInitLEServerSenderQueue();
430 if (CA_STATUS_OK != result)
432 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
433 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
434 return CA_STATUS_FAILED;
437 g_bleServerSenderInfo = u_arraylist_create();
438 if (!g_bleServerSenderInfo)
440 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
441 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
442 return CA_MEMORY_ALLOC_FAILED;
445 result = CAInitLEReceiverQueue();
446 if (CA_STATUS_OK != result)
448 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
449 u_arraylist_free(&g_bleServerSenderInfo);
450 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
451 return CA_STATUS_FAILED;
454 g_dataBleReceiverHandlerState = true;
456 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
458 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
462 static CAResult_t CAInitLEClientQueues()
464 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
466 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
468 CAResult_t result = CAInitLEClientSenderQueue();
469 if (CA_STATUS_OK != result)
471 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
472 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
473 return CA_STATUS_FAILED;
476 g_bleClientSenderInfo = u_arraylist_create();
477 if (!g_bleClientSenderInfo)
479 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
480 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
481 return CA_MEMORY_ALLOC_FAILED;
484 result = CAInitLEReceiverQueue();
485 if (CA_STATUS_OK != result)
487 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
488 u_arraylist_free(&g_bleClientSenderInfo);
489 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
490 return CA_STATUS_FAILED;
493 g_dataBleReceiverHandlerState = true;
495 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
497 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
501 static CAResult_t CAInitLEReceiverQueue()
503 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEReceiverQueue");
504 // Check if the message queue is already initialized
505 if (g_bleReceiverQueue)
507 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
511 // Create recv message queue
512 g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
513 if (!g_bleReceiverQueue)
515 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
516 return CA_MEMORY_ALLOC_FAILED;
519 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue,
520 g_bleAdapterThreadPool,
521 CALEDataReceiverHandler,
524 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
525 OICFree(g_bleReceiverQueue);
526 g_bleReceiverQueue = NULL;
527 return CA_STATUS_FAILED;
530 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue))
532 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
533 OICFree(g_bleReceiverQueue);
534 g_bleReceiverQueue = NULL;
535 return CA_STATUS_FAILED;
538 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
542 static CAResult_t CAInitLEServerSenderQueue()
544 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEServerSenderQueue");
545 // Check if the message queue is already initialized
546 if (g_bleServerSendQueueHandle)
548 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
552 // Create send message queue
553 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
554 if (!g_bleServerSendQueueHandle)
556 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
557 return CA_MEMORY_ALLOC_FAILED;
560 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
561 g_bleAdapterThreadPool,
562 CALEServerSendDataThread,
565 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
566 OICFree(g_bleServerSendQueueHandle);
567 g_bleServerSendQueueHandle = NULL;
568 return CA_STATUS_FAILED;
571 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
575 static void CALEClearSenderInfoImpl(u_arraylist_t ** list)
577 const size_t length = u_arraylist_length(*list);
578 for (size_t i = 0; i < length; ++i)
580 CABLESenderInfo_t * const info =
581 (CABLESenderInfo_t *) u_arraylist_get(*list, i);
584 OICFree(info->defragData);
585 CAFreeEndpoint(info->remoteEndpoint);
589 u_arraylist_free(list);
592 static void CALEClearSenderInfo()
594 CALEClearSenderInfoImpl(&g_bleServerSenderInfo);
595 CALEClearSenderInfoImpl(&g_bleClientSenderInfo);
598 static CAResult_t CAInitLEClientSenderQueue()
600 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEClientSenderQueue");
602 if (g_bleClientSendQueueHandle)
604 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
608 // Create send message queue
609 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
610 if (!g_bleClientSendQueueHandle)
612 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
613 return CA_MEMORY_ALLOC_FAILED;
616 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
617 g_bleAdapterThreadPool,
618 CALEClientSendDataThread, CALEDataDestroyer))
620 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
621 OICFree(g_bleClientSendQueueHandle);
622 g_bleClientSendQueueHandle = NULL;
623 return CA_STATUS_FAILED;
626 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAInitLEClientSenderQueue");
630 static void CAStopLEQueues()
632 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStopLEQueues");
634 ca_mutex_lock(g_bleReceiveDataMutex);
635 if (NULL != g_bleReceiverQueue)
637 CAQueueingThreadStop(g_bleReceiverQueue);
639 ca_mutex_unlock(g_bleReceiveDataMutex);
641 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAStopLEQueues");
644 static void CATerminateLEQueues()
646 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
648 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
649 OICFree(g_bleClientSendQueueHandle);
650 g_bleClientSendQueueHandle = NULL;
652 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
653 OICFree(g_bleServerSendQueueHandle);
654 g_bleServerSendQueueHandle = NULL;
656 CAQueueingThreadDestroy(g_bleReceiverQueue);
657 OICFree(g_bleReceiverQueue);
658 g_bleReceiverQueue = NULL;
660 CALEClearSenderInfo();
662 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
665 static CAResult_t CALEGetSenderInfo(const char *leAddress,
666 u_arraylist_t *senderInfoList,
667 CABLESenderInfo_t **senderInfo,
668 uint32_t *senderIndex)
670 VERIFY_NON_NULL_RET(leAddress,
672 "NULL BLE address argument",
673 CA_STATUS_INVALID_PARAM);
674 VERIFY_NON_NULL_RET(senderIndex,
676 "NULL index argument",
677 CA_STATUS_INVALID_PARAM);
679 const uint32_t listLength = u_arraylist_length(senderInfoList);
680 const uint32_t addrLength = strlen(leAddress);
681 for (uint32_t index = 0; index < listLength; index++)
683 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(senderInfoList, index);
684 if(!info || !(info->remoteEndpoint))
689 if(!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
691 *senderIndex = index;
700 return CA_STATUS_FAILED;
703 static void CALEDataReceiverHandler(void *threadData)
705 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDataReceiverHandler");
707 ca_mutex_lock(g_bleReceiveDataMutex);
709 if (g_dataBleReceiverHandlerState)
711 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
713 CALEData_t *bleData = (CALEData_t *) threadData;
716 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
717 ca_mutex_unlock(g_bleReceiveDataMutex);
721 if (!(bleData->senderInfo))
723 OIC_LOG(ERROR, CALEADAPTER_TAG, "sender info is not available");
724 ca_mutex_unlock(g_bleReceiveDataMutex);
728 if (!(bleData->remoteEndpoint))
730 OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!");
731 ca_mutex_unlock(g_bleReceiveDataMutex);
735 CABLESenderInfo_t *senderInfo = NULL;
736 uint32_t senderIndex = 0;
738 if(CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
740 &senderInfo, &senderIndex))
742 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s]",
743 bleData->remoteEndpoint->addr);
748 CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
751 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
752 ca_mutex_unlock(g_bleReceiveDataMutex);
755 newSender->recvDataLen = 0;
756 newSender->totalDataLen = 0;
757 newSender->defragData = NULL;
758 newSender->remoteEndpoint = NULL;
760 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
762 newSender->totalDataLen = coap_get_total_message_length(bleData->data, bleData->dataLen);
764 if(!(newSender->totalDataLen))
766 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
768 ca_mutex_unlock(g_bleReceiveDataMutex);
772 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
773 newSender->totalDataLen);
774 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
777 newSender->defragData = OICCalloc(newSender->totalDataLen + 1,
778 sizeof(*newSender->defragData));
780 if (NULL == newSender->defragData)
782 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
784 ca_mutex_unlock(g_bleReceiveDataMutex);
788 const char *remoteAddress = bleData->remoteEndpoint->addr;
789 newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
790 CA_ADAPTER_GATT_BTLE,
793 if (NULL == newSender->remoteEndpoint)
795 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
796 OICFree(newSender->defragData);
798 ca_mutex_unlock(g_bleReceiveDataMutex);
802 if (newSender->recvDataLen + bleData->dataLen > newSender->totalDataLen)
804 OIC_LOG(ERROR, CALEADAPTER_TAG, "buffer is smaller than received data");
805 OICFree(newSender->defragData);
806 CAFreeEndpoint(newSender->remoteEndpoint);
808 ca_mutex_unlock(g_bleReceiveDataMutex);
811 memcpy(newSender->defragData, bleData->data, bleData->dataLen);
812 newSender->recvDataLen += bleData->dataLen;
814 u_arraylist_add(bleData->senderInfo,(void *)newSender);
816 //Getting newSender index position in bleSenderInfo array list
818 CALEGetSenderInfo(newSender->remoteEndpoint->addr, bleData->senderInfo,
821 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
822 OICFree(newSender->defragData);
823 CAFreeEndpoint(newSender->remoteEndpoint);
825 ca_mutex_unlock(g_bleReceiveDataMutex);
828 senderInfo = newSender;
832 if(senderInfo->recvDataLen + bleData->dataLen > senderInfo->totalDataLen)
834 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
835 "Data Length exceeding error!! Receiving [%d] total length [%d]",
836 senderInfo->recvDataLen + bleData->dataLen, senderInfo->totalDataLen);
837 u_arraylist_remove(bleData->senderInfo, senderIndex);
838 OICFree(senderInfo->defragData);
840 ca_mutex_unlock(g_bleReceiveDataMutex);
843 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]",
845 memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data,
847 senderInfo->recvDataLen += bleData->dataLen ;
848 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] received Datalen [%d]",
849 senderInfo->totalDataLen, senderInfo->recvDataLen);
852 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
854 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
855 if (NULL == g_networkPacketReceivedCallback)
857 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
859 u_arraylist_remove(bleData->senderInfo, senderIndex);
860 OICFree(senderInfo->defragData);
862 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
863 ca_mutex_unlock(g_bleReceiveDataMutex);
867 OIC_LOG(DEBUG, CALEADAPTER_TAG, "[CALEDataReceiverHandler] Sending data up !");
869 const CASecureEndpoint_t tmp =
871 .endpoint = *senderInfo->remoteEndpoint
874 g_networkPacketReceivedCallback(&tmp,
875 senderInfo->defragData,
876 senderInfo->recvDataLen);
877 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
878 u_arraylist_remove(bleData->senderInfo, senderIndex);
879 senderInfo->remoteEndpoint = NULL;
880 senderInfo->defragData = NULL;
884 ca_mutex_unlock(g_bleReceiveDataMutex);
885 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
888 static void CALEServerSendDataThread(void *threadData)
890 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEServerSendDataThread");
892 CALEData_t * const bleData = (CALEData_t *) threadData;
895 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
899 const uint32_t totalLength = bleData->dataLen;
903 "Server total Data length with header is [%u]",
906 uint8_t * const dataSegment = OICCalloc(totalLength, 1);
908 if (NULL == dataSegment)
910 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
915 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
917 length = totalLength;
918 memcpy(dataSegment, bleData->data, bleData->dataLen);
922 length = CA_SUPPORTED_BLE_MTU_SIZE;
923 memcpy(dataSegment, bleData->data, CA_SUPPORTED_BLE_MTU_SIZE);
926 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
928 CAResult_t result = CA_STATUS_FAILED;
930 // Send the first segment with the header.
931 if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
933 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
935 result = CAUpdateCharacteristicsToGattClient(
936 bleData->remoteEndpoint->addr, dataSegment, length);
938 if (CA_STATUS_OK != result)
942 "Update characteristics failed, result [%d]",
945 g_errorHandler(bleData->remoteEndpoint,
949 OICFree(dataSegment);
955 "Server Sent data length [%u]",
957 for (index = 1; index < iter; index++)
959 // Send the remaining header.
962 "Sending the chunk number [%u]",
966 CAUpdateCharacteristicsToGattClient(
967 bleData->remoteEndpoint->addr,
968 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE)),
969 CA_SUPPORTED_BLE_MTU_SIZE);
971 if (CA_STATUS_OK != result)
973 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
974 "Update characteristics failed, result [%d]", result);
975 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
976 OICFree(dataSegment);
979 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
980 CA_SUPPORTED_BLE_MTU_SIZE);
983 const uint32_t remainingLen =
984 totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
986 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
988 // send the last segment of the data (Ex: 22 bytes of 622
989 // bytes of data when MTU is 200)
990 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
992 result = CAUpdateCharacteristicsToGattClient(
993 bleData->remoteEndpoint->addr,
994 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
997 if (CA_STATUS_OK != result)
1001 "Update characteristics failed, result [%d]",
1003 g_errorHandler(bleData->remoteEndpoint,
1007 OICFree(dataSegment);
1010 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1015 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
1016 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
1017 if (CA_STATUS_OK != result)
1019 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1021 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1022 OICFree(dataSegment);
1025 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1026 for (index = 1; index < iter; index++)
1028 // Send the remaining header.
1029 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1031 result = CAUpdateCharacteristicsToAllGattClients(
1032 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE)),
1033 CA_SUPPORTED_BLE_MTU_SIZE);
1035 if (CA_STATUS_OK != result)
1037 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1039 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1040 OICFree(dataSegment);
1043 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%u]",
1044 CA_SUPPORTED_BLE_MTU_SIZE);
1047 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1048 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1050 // send the last segment of the data
1051 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1053 result = CAUpdateCharacteristicsToAllGattClients(
1054 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1057 if (CA_STATUS_OK != result)
1059 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1061 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1062 OICFree(dataSegment);
1065 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1068 OICFree(dataSegment);
1070 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CALEServerSendDataThread");
1073 static void CALEClientSendDataThread(void *threadData)
1075 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEClientSendDataThread");
1077 CALEData_t *bleData = (CALEData_t *) threadData;
1080 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1084 const uint32_t totalLength = bleData->dataLen;
1086 uint8_t *dataSegment = OICCalloc(totalLength, 1);
1087 if (NULL == dataSegment)
1089 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1093 uint32_t length = 0;
1094 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1096 length = totalLength;
1103 length = CA_SUPPORTED_BLE_MTU_SIZE;
1106 CA_SUPPORTED_BLE_MTU_SIZE);
1109 CAResult_t result = CA_STATUS_FAILED;
1110 const uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1112 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1114 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1115 // Send the first segment with the header.
1117 CAUpdateCharacteristicsToGattServer(
1118 bleData->remoteEndpoint->addr,
1124 if (CA_STATUS_OK != result)
1128 "Update characteristics failed, result [%d]",
1130 g_errorHandler(bleData->remoteEndpoint,
1134 OICFree(dataSegment);
1140 "Client Sent Data length is [%u]",
1143 for (index = 1; index < iter; index++)
1145 // Send the remaining header.
1146 result = CAUpdateCharacteristicsToGattServer(
1147 bleData->remoteEndpoint->addr,
1148 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1149 CA_SUPPORTED_BLE_MTU_SIZE,
1152 if (CA_STATUS_OK != result)
1156 "Update characteristics failed, result [%d]",
1158 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1159 OICFree(dataSegment);
1162 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1163 CA_SUPPORTED_BLE_MTU_SIZE);
1166 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1167 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1169 // send the last segment of the data (Ex: 22 bytes of 622
1170 // bytes of data when MTU is 200)
1171 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1173 result = CAUpdateCharacteristicsToGattServer(
1174 bleData->remoteEndpoint->addr,
1175 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1179 if (CA_STATUS_OK != result)
1181 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1183 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1184 OICFree(dataSegment);
1187 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1192 //Sending Mulitcast Data
1193 // Send the first segment with the header.
1194 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1195 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1196 if (CA_STATUS_OK != result)
1198 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1199 "Update characteristics (all) failed, result [%d]", result);
1200 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1201 OICFree(dataSegment);
1204 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1205 // Send the remaining header.
1206 for (index = 1; index < iter; index++)
1208 result = CAUpdateCharacteristicsToAllGattServers(
1209 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1210 CA_SUPPORTED_BLE_MTU_SIZE);
1212 if (CA_STATUS_OK != result)
1214 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1216 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1217 OICFree(dataSegment);
1220 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1221 CA_SUPPORTED_BLE_MTU_SIZE);
1224 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1225 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1227 // send the last segment of the data (Ex: 22 bytes of 622
1228 // bytes of data when MTU is 200)
1229 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1231 CAUpdateCharacteristicsToAllGattServers(
1232 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1235 if (CA_STATUS_OK != result)
1237 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1238 "Update characteristics (all) failed, result [%d]", result);
1239 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1240 OICFree(dataSegment);
1243 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1248 OICFree(dataSegment);
1250 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1253 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
1254 const uint8_t *data,
1255 uint32_t dataLength,
1256 u_arraylist_t *senderInfo)
1258 CALEData_t * const bleData = OICMalloc(sizeof(CALEData_t));
1262 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1266 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1267 bleData->data = OICCalloc(dataLength + 1, 1);
1269 if (NULL == bleData->data)
1271 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1272 CAFreeLEData(bleData);
1276 memcpy(bleData->data, data, dataLength);
1277 bleData->dataLen = dataLength;
1280 bleData->senderInfo = senderInfo;
1286 static void CAFreeLEData(CALEData_t *bleData)
1288 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1290 CAFreeEndpoint(bleData->remoteEndpoint);
1291 OICFree(bleData->data);
1295 static void CALEDataDestroyer(void *data, uint32_t size)
1297 if ((size_t)size < sizeof(CALEData_t *))
1299 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1300 "Destroy data too small %p %d", data, size);
1302 CALEData_t *ledata = (CALEData_t *) data;
1304 CAFreeLEData(ledata);
1308 static CAResult_t CAInitLEAdapterMutex()
1310 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEAdapterMutex");
1312 if (NULL == g_bleIsServerMutex)
1314 g_bleIsServerMutex = ca_mutex_new();
1315 if (NULL == g_bleIsServerMutex)
1317 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1318 return CA_STATUS_FAILED;
1322 if (NULL == g_bleNetworkCbMutex)
1324 g_bleNetworkCbMutex = ca_mutex_new();
1325 if (NULL == g_bleNetworkCbMutex)
1327 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1328 CATerminateLEAdapterMutex();
1329 return CA_STATUS_FAILED;
1333 if (NULL == g_bleLocalAddressMutex)
1335 g_bleLocalAddressMutex = ca_mutex_new();
1336 if (NULL == g_bleLocalAddressMutex)
1338 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1339 CATerminateLEAdapterMutex();
1340 return CA_STATUS_FAILED;
1344 if (NULL == g_bleAdapterThreadPoolMutex)
1346 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1347 if (NULL == g_bleAdapterThreadPoolMutex)
1349 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1350 CATerminateLEAdapterMutex();
1351 return CA_STATUS_FAILED;
1355 if (NULL == g_bleClientSendDataMutex)
1357 g_bleClientSendDataMutex = ca_mutex_new();
1358 if (NULL == g_bleClientSendDataMutex)
1360 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1361 CATerminateLEAdapterMutex();
1362 return CA_STATUS_FAILED;
1366 if (NULL == g_bleServerSendDataMutex)
1368 g_bleServerSendDataMutex = ca_mutex_new();
1369 if (NULL == g_bleServerSendDataMutex)
1371 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1372 CATerminateLEAdapterMutex();
1373 return CA_STATUS_FAILED;
1377 if (NULL == g_bleAdapterReqRespCbMutex)
1379 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1380 if (NULL == g_bleAdapterReqRespCbMutex)
1382 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1383 CATerminateLEAdapterMutex();
1384 return CA_STATUS_FAILED;
1388 if (NULL == g_bleReceiveDataMutex)
1390 g_bleReceiveDataMutex = ca_mutex_new();
1391 if (NULL == g_bleReceiveDataMutex)
1393 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1394 return CA_STATUS_FAILED;
1398 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1399 return CA_STATUS_OK;
1402 static void CATerminateLEAdapterMutex()
1404 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CATerminateLEAdapterMutex");
1406 ca_mutex_free(g_bleIsServerMutex);
1407 g_bleIsServerMutex = NULL;
1409 ca_mutex_free(g_bleNetworkCbMutex);
1410 g_bleNetworkCbMutex = NULL;
1412 ca_mutex_free(g_bleLocalAddressMutex);
1413 g_bleLocalAddressMutex = NULL;
1415 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1416 g_bleAdapterThreadPoolMutex = NULL;
1418 ca_mutex_free(g_bleClientSendDataMutex);
1419 g_bleClientSendDataMutex = NULL;
1421 ca_mutex_free(g_bleServerSendDataMutex);
1422 g_bleServerSendDataMutex = NULL;
1424 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1425 g_bleAdapterReqRespCbMutex = NULL;
1427 ca_mutex_free(g_bleReceiveDataMutex);
1428 g_bleReceiveDataMutex = NULL;
1430 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1434 * Starting LE connectivity adapters.
1436 * As its peer to peer it does not require to start any servers.
1438 * @return ::CA_STATUS_OK or Appropriate error code.
1440 static CAResult_t CAStartLE();
1443 * Start listening server for receiving multicast search requests.
1445 * Transport Specific Behavior:
1446 * LE Starts GATT Server with prefixed UUID and Characteristics
1447 * per OIC Specification.
1448 * @return ::CA_STATUS_OK or Appropriate error code.
1450 static CAResult_t CAStartLEListeningServer();
1453 * Stops listening server from receiving multicast search requests.
1455 * Transport Specific Behavior:
1456 * LE Starts GATT Server with prefixed UUID and Characteristics
1457 * per OIC Specification.
1458 * @return ::CA_STATUS_OK or Appropriate error code.
1460 static CAResult_t CAStopLEListeningServer();
1463 * Sarting discovery of servers for receiving multicast
1466 * Transport Specific Behavior:
1467 * LE Starts GATT Server with prefixed UUID and Characteristics
1468 * per OIC Specification.
1470 * @return ::CA_STATUS_OK or Appropriate error code
1472 static CAResult_t CAStartLEDiscoveryServer();
1475 * Send data to the endpoint using the adapter connectivity.
1477 * @param[in] endpoint Remote Endpoint information (like MAC address,
1478 * reference URI and connectivity type) to which
1479 * the unicast data has to be sent.
1480 * @param[in] data Data which required to be sent.
1481 * @param[in] dataLen Size of data to be sent.
1483 * @note dataLen must be > 0.
1485 * @return The number of bytes sent on the network, or -1 on error.
1487 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1492 * Send multicast data to the endpoint using the LE connectivity.
1494 * @param[in] endpoint Remote Endpoint information to which the
1495 * multicast data has to be sent.
1496 * @param[in] data Data which required to be sent.
1497 * @param[in] dataLen Size of data to be sent.
1499 * @note dataLen must be > 0.
1501 * @return The number of bytes sent on the network, or -1 on error.
1503 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1508 * Get LE Connectivity network information.
1510 * @param[out] info Local connectivity information structures.
1511 * @param[out] size Number of local connectivity structures.
1513 * @return ::CA_STATUS_OK or Appropriate error code.
1515 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info,
1519 * Read Synchronous API callback.
1521 * @return ::CA_STATUS_OK or Appropriate error code.
1523 static CAResult_t CAReadLEData();
1526 * Stopping the adapters and close socket connections.
1528 * LE Stops all GATT servers and GATT Clients.
1530 * @return ::CA_STATUS_OK or Appropriate error code.
1532 static CAResult_t CAStopLE();
1535 * Terminate the LE connectivity adapter.
1537 * Configuration information will be deleted from further use.
1539 static void CATerminateLE();
1542 * This function will receive the data from the GattServer and add the
1543 * data to the Server receiver queue.
1545 * @param[in] remoteAddress Remote address of the device from where
1547 * @param[in] data Actual data received from the remote
1549 * @param[in] dataLength Length of the data received from the
1551 * @param[in] sentLength Length of the data sent from the remote
1554 * @return ::CA_STATUS_OK or Appropriate error code.
1555 * @retval ::CA_STATUS_OK Successful.
1556 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1557 * @retval ::CA_STATUS_FAILED Operation failed.
1560 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
1561 const uint8_t *data,
1562 uint32_t dataLength,
1563 uint32_t *sentLength);
1566 * This function will receive the data from the GattClient and add the
1567 * data into the Client receiver queue.
1569 * @param[in] remoteAddress Remote address of the device from where
1571 * @param[in] data Actual data recevied from the remote
1573 * @param[in] dataLength Length of the data received from the
1575 * @param[in] sentLength Length of the data sent from the remote
1578 * @return ::CA_STATUS_OK or Appropriate error code.
1579 * @retval ::CA_STATUS_OK Successful.
1580 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1581 * @retval ::CA_STATUS_FAILED Operation failed.
1583 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
1584 const uint8_t *data,
1585 uint32_t dataLength,
1586 uint32_t *sentLength);
1589 * Set the NetworkPacket received callback to CA layer from adapter
1592 * @param[in] callback Callback handle sent from the upper layer.
1594 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
1597 * Push the data from CA layer to the Sender processor queue.
1599 * @param[in] remoteEndpoint Remote endpoint information of the
1601 * @param[in] data Data to be transmitted from LE.
1602 * @param[in] dataLen Length of the Data being transmitted.
1604 * @return ::CA_STATUS_OK or Appropriate error code.
1605 * @retval ::CA_STATUS_OK Successful.
1606 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1607 * @retval ::CA_STATUS_FAILED Operation failed.
1609 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1610 const uint8_t *data,
1614 * Push the data from CA layer to the Sender processor queue.
1616 * @param[in] remoteEndpoint Remote endpoint information of the
1618 * @param[in] data Data to be transmitted from LE.
1619 * @param[in] dataLen Length of the Data being transmitted.
1621 * @return ::CA_STATUS_OK or Appropriate error code.
1622 * @retval ::CA_STATUS_OK Successful.
1623 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1624 * @retval ::CA_STATUS_FAILED Operation failed.
1626 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1627 const uint8_t *data,
1630 static CAResult_t CALEAdapterGattServerStart()
1632 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1634 CAResult_t result = CAStartLEGattServer();
1636 #ifndef SINGLE_THREAD
1638 Don't start the server side sending queue thread until the
1639 server itself has actually started.
1641 if (CA_STATUS_OK == result)
1643 ca_mutex_lock(g_bleServerSendDataMutex);
1644 result = CAQueueingThreadStart(g_bleServerSendQueueHandle);
1645 ca_mutex_unlock(g_bleServerSendDataMutex);
1647 if (CA_STATUS_OK != result)
1651 "Unable to start server queuing thread (%d)",
1660 static CAResult_t CALEAdapterGattServerStop()
1662 #ifndef SINGLE_THREAD
1663 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEAdapterGattServerStop");
1665 CAResult_t result = CAStopLEGattServer();
1666 ca_mutex_lock(g_bleServerSendDataMutex);
1667 if (CA_STATUS_OK == result)
1669 result = CAQueueingThreadStop(g_bleServerSendQueueHandle);
1671 ca_mutex_unlock(g_bleServerSendDataMutex);
1675 return CAStopLEGattServer();
1679 static CAResult_t CALEAdapterGattClientStart()
1681 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattClient");
1683 CAResult_t result = CAStartLEGattClient();
1685 #ifndef SINGLE_THREAD
1687 Don't start the client side sending queue thread until the
1688 client itself has actually started.
1690 if (CA_STATUS_OK == result)
1692 ca_mutex_lock(g_bleClientSendDataMutex);
1693 result = CAQueueingThreadStart(g_bleClientSendQueueHandle);
1694 ca_mutex_unlock(g_bleClientSendDataMutex);
1696 if (CA_STATUS_OK != result)
1700 "Unable to start client queuing thread");
1708 static CAResult_t CALEAdapterGattClientStop()
1710 #ifndef SINGLE_THREAD
1711 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEAdapterGattClientStop");
1712 CAStopLEGattClient();
1714 ca_mutex_lock(g_bleClientSendDataMutex);
1715 CAResult_t result = CAQueueingThreadStop(g_bleClientSendQueueHandle);
1716 ca_mutex_unlock(g_bleClientSendDataMutex);
1720 CAStopLEGattClient();
1722 return CA_STATUS_OK;
1726 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1727 CANetworkPacketReceivedCallback reqRespCallback,
1728 CAAdapterChangeCallback netCallback,
1729 CAConnectionChangeCallback connCallback,
1730 CAErrorHandleCallback errorCallback,
1731 ca_thread_pool_t handle)
1733 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1736 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1737 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1738 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1739 VERIFY_NON_NULL(connCallback, CALEADAPTER_TAG, "ConnectionChange Callback is null");
1741 CAResult_t result = CA_STATUS_OK;
1742 result = CAInitLEAdapterMutex();
1743 if (CA_STATUS_OK != result)
1745 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1746 return CA_STATUS_FAILED;
1749 result = CAInitializeLENetworkMonitor();
1750 if (CA_STATUS_OK != result)
1752 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1753 return CA_STATUS_FAILED;
1755 CAInitializeLEAdapter(handle);
1757 CASetLEClientThreadPoolHandle(handle);
1759 result = CAInitializeLEGattClient();
1760 if (CA_STATUS_OK != result)
1762 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattClient() failed");
1763 return CA_STATUS_FAILED;
1766 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1767 CASetLEServerThreadPoolHandle(handle);
1768 result = CAInitializeLEGattServer();
1769 if (CA_STATUS_OK != result)
1771 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattServer() failed");
1772 return CA_STATUS_FAILED;
1775 CASetLEAdapterThreadPoolHandle(handle);
1776 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1777 CASetLEReqRespAdapterCallback(reqRespCallback);
1779 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1780 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1781 CALERegisterNetworkNotifications(netCallback, connCallback);
1783 g_errorHandler = errorCallback;
1785 static const CAConnectivityHandler_t connHandler =
1787 .startAdapter = CAStartLE,
1788 .stopAdapter = CAStopLE,
1789 .startListenServer = CAStartLEListeningServer,
1790 .stopListenServer = CAStopLEListeningServer,
1791 .startDiscoveryServer = CAStartLEDiscoveryServer,
1792 .sendData = CASendLEUnicastData,
1793 .sendDataToAll = CASendLEMulticastData,
1794 .GetnetInfo = CAGetLEInterfaceInformation,
1795 .readData = CAReadLEData,
1796 .terminate = CATerminateLE,
1797 .cType = CA_ADAPTER_GATT_BTLE
1800 registerCallback(connHandler);
1802 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1804 return CA_STATUS_OK;
1807 static CAResult_t CAStartLE()
1809 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE");
1811 return CAStartLEAdapter();
1814 static CAResult_t CAStopLE()
1816 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1817 #ifndef SINGLE_THREAD
1821 ca_mutex_lock(g_bleIsServerMutex);
1822 switch (g_adapterType)
1824 case ADAPTER_SERVER:
1825 CALEAdapterGattServerStop();
1827 case ADAPTER_CLIENT:
1828 CALEAdapterGattClientStop();
1830 case ADAPTER_BOTH_CLIENT_SERVER:
1831 CALEAdapterGattServerStop();
1832 CALEAdapterGattClientStop();
1837 ca_mutex_unlock(g_bleIsServerMutex);
1839 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1841 return CAStopLEAdapter();
1844 static void CATerminateLE()
1846 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1848 CASetLEReqRespServerCallback(NULL);
1849 CASetLEReqRespClientCallback(NULL);
1850 CALERegisterNetworkNotifications(NULL, NULL);
1851 CASetLEReqRespAdapterCallback(NULL);
1852 CATerminateLENetworkMonitor();
1854 ca_mutex_lock(g_bleIsServerMutex);
1855 switch (g_adapterType)
1857 case ADAPTER_SERVER:
1858 CATerminateLEGattServer();
1860 case ADAPTER_CLIENT:
1861 CATerminateLEGattClient();
1863 case ADAPTER_BOTH_CLIENT_SERVER:
1864 CATerminateLEGattServer();
1865 CATerminateLEGattClient();
1870 g_adapterType = ADAPTER_EMPTY;
1871 ca_mutex_unlock(g_bleIsServerMutex);
1873 #ifndef SINGLE_THREAD
1874 CATerminateLEQueues();
1876 CATerminateLEAdapterMutex();
1878 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1881 static CAResult_t CAStartLEListeningServer()
1883 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEListeningServer");
1884 #ifndef ROUTING_GATEWAY
1885 CAResult_t result = CA_STATUS_OK;
1886 #ifndef SINGLE_THREAD
1887 result = CAInitLEServerQueues();
1888 if (CA_STATUS_OK != result)
1890 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1895 ca_mutex_lock(g_bleIsServerMutex);
1896 switch (g_adapterType)
1898 case ADAPTER_CLIENT:
1899 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1901 case ADAPTER_BOTH_CLIENT_SERVER:
1904 g_adapterType = ADAPTER_SERVER;
1906 ca_mutex_unlock(g_bleIsServerMutex);
1908 result = CAGetLEAdapterState();
1909 if (CA_STATUS_OK != result)
1911 if (CA_ADAPTER_NOT_ENABLED == result)
1915 "Listen Server will be started once BT Adapter is enabled");
1920 result = CALEAdapterGattServerStart();
1923 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1926 // Routing Gateway only supports BLE client mode.
1927 OIC_LOG(ERROR, CALEADAPTER_TAG, "LE server not supported in Routing Gateway");
1928 return CA_NOT_SUPPORTED;
1932 static CAResult_t CAStopLEListeningServer()
1934 OIC_LOG(ERROR, CALEADAPTER_TAG, "Listen server stop not supported.");
1935 return CA_NOT_SUPPORTED;
1938 static CAResult_t CAStartLEDiscoveryServer()
1940 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEDiscoveryServer");
1941 CAResult_t result = CA_STATUS_OK;
1942 #ifndef SINGLE_THREAD
1943 result = CAInitLEClientQueues();
1944 if (CA_STATUS_OK != result)
1946 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1951 ca_mutex_lock(g_bleIsServerMutex);
1952 switch (g_adapterType)
1954 case ADAPTER_SERVER:
1955 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1957 case ADAPTER_BOTH_CLIENT_SERVER:
1960 g_adapterType = ADAPTER_CLIENT;
1962 ca_mutex_unlock(g_bleIsServerMutex);
1964 result = CAGetLEAdapterState();
1965 if (CA_STATUS_OK != result)
1967 if (CA_ADAPTER_NOT_ENABLED == result)
1971 "Discovery Server will be started once BT Adapter is enabled");
1976 result = CALEAdapterGattClientStart();
1979 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1983 static CAResult_t CAReadLEData()
1985 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1986 #ifdef SINGLE_THREAD
1989 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1990 return CA_STATUS_OK;
1993 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1997 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEUnicastData");
2000 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
2001 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
2003 CAResult_t result = CA_STATUS_FAILED;
2005 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
2006 if (ADAPTER_EMPTY == g_adapterType)
2008 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2011 ca_mutex_lock(g_bleIsServerMutex);
2012 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2014 result = CALEAdapterServerSendData(endpoint, data, dataLen);
2015 if (CA_STATUS_OK != result)
2017 ca_mutex_unlock(g_bleIsServerMutex);
2018 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed");
2021 g_errorHandler(endpoint, data, dataLen, result);
2028 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2030 result = CALEAdapterClientSendData(endpoint, data, dataLen);
2031 if (CA_STATUS_OK != result)
2033 ca_mutex_unlock(g_bleIsServerMutex);
2034 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for client failed" );
2038 g_errorHandler(endpoint, data, dataLen, result);
2043 ca_mutex_unlock(g_bleIsServerMutex);
2045 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2049 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
2053 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEMulticastData");
2056 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
2060 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
2064 CAResult_t result = CA_STATUS_FAILED;
2066 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
2067 if (ADAPTER_EMPTY == g_adapterType)
2069 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2072 ca_mutex_lock(g_bleIsServerMutex);
2073 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2075 result = CALEAdapterServerSendData(NULL, data, dataLen);
2076 if (CA_STATUS_OK != result)
2078 ca_mutex_unlock(g_bleIsServerMutex);
2080 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data for server failed" );
2084 g_errorHandler(endpoint, data, dataLen, result);
2090 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2092 result = CALEAdapterClientSendData(NULL, data, dataLen);
2093 if (CA_STATUS_OK != result)
2095 ca_mutex_unlock(g_bleIsServerMutex);
2097 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data for client failed" );
2101 g_errorHandler(endpoint, data, dataLen, result);
2106 ca_mutex_unlock(g_bleIsServerMutex);
2108 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CASendLEMulticastData");
2112 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
2114 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2116 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
2118 char *local_address = NULL;
2120 CAResult_t res = CAGetLEAddress(&local_address);
2121 if (CA_STATUS_OK != res)
2123 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
2127 if (NULL == local_address)
2129 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
2130 return CA_STATUS_FAILED;
2134 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
2135 if (NULL == (*info))
2137 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
2138 OICFree(local_address);
2139 return CA_STATUS_FAILED;
2142 size_t local_address_len = strlen(local_address);
2144 if(local_address_len >= sizeof(g_localBLEAddress) ||
2145 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
2147 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
2149 OICFree(local_address);
2150 return CA_STATUS_FAILED;
2153 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
2154 ca_mutex_lock(g_bleLocalAddressMutex);
2155 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
2156 ca_mutex_unlock(g_bleLocalAddressMutex);
2158 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
2160 OICFree(local_address);
2162 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2163 return CA_STATUS_OK;
2166 static CAResult_t CALERegisterNetworkNotifications(CAAdapterChangeCallback netCallback,
2167 CAConnectionChangeCallback connCallback)
2169 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2171 ca_mutex_lock(g_bleNetworkCbMutex);
2172 g_networkCallback = netCallback;
2173 g_connectionCallback = connCallback;
2174 ca_mutex_unlock(g_bleNetworkCbMutex);
2175 CAResult_t res = CA_STATUS_OK;
2178 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
2179 if (CA_STATUS_OK != res)
2181 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2186 res = CAUnSetLEAdapterStateChangedCb();
2187 if (CA_STATUS_OK != res)
2189 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2193 if (g_connectionCallback)
2195 res = CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCb);
2196 if (CA_STATUS_OK != res)
2198 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLENWConnectionStateChangedCb failed!");
2202 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2206 static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
2209 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEConnectionStateChangedCb");
2211 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2215 ca_mutex_lock(g_bleIsServerMutex);
2216 switch (g_adapterType)
2218 case ADAPTER_SERVER:
2219 CALEGattServerConnectionStateChanged(isConnected, address);
2221 case ADAPTER_CLIENT:
2222 CALEGattConnectionStateChanged(isConnected, address);
2224 case ADAPTER_BOTH_CLIENT_SERVER:
2225 CALEGattConnectionStateChanged(isConnected, address);
2226 CALEGattServerConnectionStateChanged(isConnected, address);
2231 ca_mutex_unlock(g_bleIsServerMutex);
2236 #ifndef SINGLE_THREAD
2237 if(g_bleClientSenderInfo)
2239 CALERemoveReceiveQueueData(g_bleClientSenderInfo, address);
2242 if(g_bleServerSenderInfo)
2244 CALERemoveReceiveQueueData(g_bleServerSenderInfo, address);
2247 // remove data of send queue.
2248 if (g_bleClientSendQueueHandle)
2250 CALERemoveSendQueueData(g_bleClientSendQueueHandle,
2251 g_bleClientSendDataMutex,
2255 if (g_bleServerSendQueueHandle)
2257 CALERemoveSendQueueData(g_bleServerSendQueueHandle,
2258 g_bleServerSendDataMutex,
2264 CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE };
2265 OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), address);
2267 ca_mutex_lock(g_bleNetworkCbMutex);
2268 if (g_connectionCallback)
2270 g_connectionCallback(&localEndpoint, isConnected);
2272 ca_mutex_unlock(g_bleNetworkCbMutex);
2274 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2277 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
2279 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDeviceStateChangedCb");
2281 if (CA_ADAPTER_ENABLED == adapter_state)
2283 ca_mutex_lock(g_bleIsServerMutex);
2284 switch (g_adapterType)
2286 case ADAPTER_SERVER:
2287 CALEAdapterGattServerStart();
2289 case ADAPTER_CLIENT:
2290 CALEAdapterGattClientStart();
2292 case ADAPTER_BOTH_CLIENT_SERVER:
2293 CALEAdapterGattServerStart();
2294 CALEAdapterGattClientStart();
2299 ca_mutex_unlock(g_bleIsServerMutex);
2303 ca_mutex_lock(g_bleIsServerMutex);
2304 switch (g_adapterType)
2306 case ADAPTER_SERVER:
2307 CALEAdapterGattServerStop();
2309 case ADAPTER_CLIENT:
2310 CALEAdapterGattClientStop();
2312 case ADAPTER_BOTH_CLIENT_SERVER:
2313 CALEAdapterGattServerStop();
2314 CALEAdapterGattClientStop();
2319 ca_mutex_unlock(g_bleIsServerMutex);
2322 ca_mutex_lock(g_bleNetworkCbMutex);
2323 if (NULL != g_networkCallback)
2325 g_networkCallback(CA_ADAPTER_GATT_BTLE, adapter_state);
2329 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
2331 ca_mutex_unlock(g_bleNetworkCbMutex);
2333 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2336 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
2337 const uint8_t *data,
2340 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2342 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2343 #ifndef SINGLE_THREAD
2344 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2345 "g_bleClientSendQueueHandle is NULL",
2347 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
2348 "g_bleClientSendDataMutex is NULL",
2351 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2352 "g_bleClientSendQueueHandle",
2355 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
2357 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2360 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2361 return CA_MEMORY_ALLOC_FAILED;
2363 // Add message to send queue
2364 ca_mutex_lock(g_bleClientSendDataMutex);
2365 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
2366 ca_mutex_unlock(g_bleClientSendDataMutex);
2368 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2369 return CA_STATUS_OK;
2372 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2373 const uint8_t *data,
2376 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2378 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2380 #ifdef SINGLE_THREAD
2381 if (!CAIsLEConnected())
2383 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2384 return CA_STATUS_FAILED;
2387 CAResult_t result = CA_STATUS_OK;
2388 const uint32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2389 for (uint32_t iter = 0; iter < dataLimit; iter++)
2392 CAUpdateCharacteristicsToAllGattClients(
2393 data + (iter * CA_SUPPORTED_BLE_MTU_SIZE),
2394 CA_SUPPORTED_BLE_MTU_SIZE);
2396 if (CA_STATUS_OK != result)
2398 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2399 return CA_STATUS_FAILED;
2405 const uint32_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2409 CAUpdateCharacteristicsToAllGattClients(
2410 data + (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE),
2412 if (CA_STATUS_OK != result)
2414 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2415 return CA_STATUS_FAILED;
2420 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2421 "BleClientReceiverQueue is NULL",
2423 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2424 "BleClientSendDataMutex is NULL",
2427 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2430 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2432 CALEData_t * const bleData =
2433 CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2437 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2438 return CA_MEMORY_ALLOC_FAILED;
2441 // Add message to send queue
2442 ca_mutex_lock(g_bleServerSendDataMutex);
2443 CAQueueingThreadAddData(g_bleServerSendQueueHandle,
2445 sizeof(CALEData_t));
2446 ca_mutex_unlock(g_bleServerSendDataMutex);
2448 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2449 return CA_STATUS_OK;
2452 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2453 const uint8_t *data,
2454 uint32_t dataLength,
2455 uint32_t *sentLength)
2457 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2460 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2461 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2463 #ifdef SINGLE_THREAD
2464 if(g_networkPacketReceivedCallback)
2466 // will be filled by upper layer
2467 const CASecureEndpoint_t endpoint =
2468 { .endpoint = { .adapter = CA_ADAPTER_GATT_BTLE } };
2471 g_networkPacketReceivedCallback(&endpoint, data, dataLength);
2474 VERIFY_NON_NULL_RET(g_bleReceiverQueue,
2476 "g_bleReceiverQueue",
2479 //Add message to data queue
2480 CAEndpoint_t * const remoteEndpoint =
2481 CACreateEndpointObject(CA_DEFAULT_FLAGS,
2482 CA_ADAPTER_GATT_BTLE,
2486 if (NULL == remoteEndpoint)
2488 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2489 return CA_STATUS_FAILED;
2492 // Create bleData to add to queue
2495 "Data received from LE layer [%d]",
2498 CALEData_t * const bleData =
2499 CACreateLEData(remoteEndpoint, data, dataLength, g_bleServerSenderInfo);
2503 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2504 CAFreeEndpoint(remoteEndpoint);
2505 return CA_MEMORY_ALLOC_FAILED;
2508 CAFreeEndpoint(remoteEndpoint);
2509 // Add message to receiver queue
2510 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2512 *sentLength = dataLength;
2514 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2515 return CA_STATUS_OK;
2518 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2519 const uint8_t *data,
2520 uint32_t dataLength,
2521 uint32_t *sentLength)
2523 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2526 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2527 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2528 #ifndef SINGLE_THREAD
2529 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG,
2530 "g_bleReceiverQueue",
2533 //Add message to data queue
2534 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2535 CA_ADAPTER_GATT_BTLE,
2537 if (NULL == remoteEndpoint)
2539 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2540 return CA_STATUS_FAILED;
2543 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%u]", dataLength);
2545 // Create bleData to add to queue
2546 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data,
2547 dataLength, g_bleClientSenderInfo);
2550 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2551 CAFreeEndpoint(remoteEndpoint);
2552 return CA_MEMORY_ALLOC_FAILED;
2555 CAFreeEndpoint(remoteEndpoint);
2556 // Add message to receiver queue
2557 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2559 *sentLength = dataLength;
2561 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2562 return CA_STATUS_OK;
2565 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2567 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2569 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2570 g_bleAdapterThreadPool = handle;
2571 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2573 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2576 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2578 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2580 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2582 g_networkPacketReceivedCallback = callback;
2584 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2586 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2589 static void CALEErrorHandler(const char *remoteAddress,
2590 const uint8_t *data,
2594 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2596 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2598 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2599 CA_ADAPTER_GATT_BTLE,
2603 // if required, will be used to build remote endpoint
2604 g_errorHandler(rep, data, dataLen, result);
2606 CAFreeEndpoint(rep);
2608 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");
2611 #ifndef SINGLE_THREAD
2612 static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle, ca_mutex mutex,
2613 const char* address)
2615 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALERemoveSendQueueData");
2617 VERIFY_NON_NULL_VOID(queueHandle, CALEADAPTER_TAG, "queueHandle");
2618 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2620 ca_mutex_lock(mutex);
2621 while (u_queue_get_size(queueHandle->dataQueue) > 0)
2623 OIC_LOG(DEBUG, CALEADAPTER_TAG, "get data from queue");
2624 u_queue_message_t *message = u_queue_get_element(queueHandle->dataQueue);
2625 if (NULL != message)
2627 CALEData_t *bleData = (CALEData_t *) message->msg;
2628 if (bleData && bleData->remoteEndpoint)
2630 if (!strcmp(bleData->remoteEndpoint->addr, address))
2632 OIC_LOG(DEBUG, CALEADAPTER_TAG, "found the message of disconnected device");
2633 if (NULL != queueHandle->destroy)
2635 queueHandle->destroy(message->msg, message->size);
2639 OICFree(message->msg);
2647 ca_mutex_unlock(mutex);
2650 static void CALERemoveReceiveQueueData(u_arraylist_t *dataInfoList, const char* address)
2652 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALERemoveReceiveQueueData");
2654 VERIFY_NON_NULL_VOID(dataInfoList, CALEADAPTER_TAG, "dataInfoList");
2655 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2657 CABLESenderInfo_t *senderInfo = NULL;
2658 uint32_t senderIndex = 0;
2660 if(CA_STATUS_OK == CALEGetSenderInfo(address, dataInfoList, &senderInfo,
2663 u_arraylist_remove(dataInfoList, senderIndex);
2664 OICFree(senderInfo->defragData);
2665 OICFree(senderInfo->remoteEndpoint);
2666 OICFree(senderInfo);
2668 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo is removed for disconnection");
2672 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo doesn't exist");