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 #if defined(__TIZEN__) || defined(__ANDROID__)
33 #include "caleserver.h"
34 #include "caleclient.h"
36 #include "oic_malloc.h"
37 #include "oic_string.h"
38 #include "caremotehandler.h"
42 * Logging tag for module name.
44 #define CALEADAPTER_TAG "OIC_CA_LE_ADAP"
47 * The MTU supported for BLE adapter
49 #define CA_SUPPORTED_BLE_MTU_SIZE 20
52 * Stores information of all the senders.
54 * This structure will be used to track and defragment all incoming
60 uint32_t totalDataLen;
62 CAEndpoint_t *remoteEndpoint;
68 ADAPTER_BOTH_CLIENT_SERVER,
74 * Callback to provide the status of the network change to CA layer.
76 static CANetworkChangeCallback g_networkCallback = NULL;
79 * bleAddress of the local adapter. Value will be initialized to zero,
80 * and will be updated later.
82 static char g_localBLEAddress[18] = { 0 };
85 * Variable to differentiate btw GattServer and GattClient.
87 static CABLEAdapter_t g_adapterType = ADAPTER_EMPTY;
90 * Mutex to synchronize the task to be executed on the GattServer
93 static ca_mutex g_bleIsServerMutex = NULL;
96 * Mutex to synchronize the callback to be called for the network
99 static ca_mutex g_bleNetworkCbMutex = NULL;
102 * Mutex to synchronize the updates of the local LE address of the
105 static ca_mutex g_bleLocalAddressMutex = NULL;
108 * Reference to thread pool.
110 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
113 * Mutex to synchronize the task to be pushed to thread pool.
115 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
118 * Mutex to synchronize the queing of the data from SenderQueue.
120 static ca_mutex g_bleClientSendDataMutex = NULL;
123 * Mutex to synchronize the queing of the data from ReceiverQueue.
125 static ca_mutex g_bleReceiveDataMutex = NULL;
128 * Mutex to synchronize the queing of the data from SenderQueue.
130 static ca_mutex g_bleServerSendDataMutex = NULL;
133 * Mutex to synchronize the callback to be called for the
134 * adapterReqResponse.
136 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
139 * Callback to be called when network packet received from either
140 * GattServer or GattClient.
142 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
145 * Callback to notify error from the BLE adapter.
147 static CAErrorHandleCallback g_errorHandler = NULL;
150 * Register network change notification callback.
152 * @param[in] netCallback CANetworkChangeCallback callback which will
153 * be set for the change in network.
155 * @return 0 on success otherwise a positive error value.
156 * @retval ::CA_STATUS_OK Successful.
157 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
158 * @retval ::CA_STATUS_FAILED Operation failed.
161 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
164 * Set the thread pool handle which is required for spawning new
167 * @param[in] handle Thread pool handle which is given by above layer
168 * for using thread creation task.
171 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
174 * Call the callback to the upper layer when the adapter state gets
177 * @param[in] adapter_state New state of the adapter to be notified to
180 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state);
183 * Call the callback to the upper layer when the device connection state gets
186 * @param[in] address LE address of the device to be notified to the upper layer.
187 * @param[in] isConnected whether connection state is connected or not.
189 static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
193 * Used to initialize all required mutex variable for LE Adapter
196 * @return 0 on success otherwise a positive error value.
197 * @retval ::CA_STATUS_OK Successful.
198 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
199 * @retval ::CA_STATUS_FAILED Operation failed.
202 static CAResult_t CAInitLEAdapterMutex();
205 * Terminate all required mutex variables for LE adapter
208 static void CATerminateLEAdapterMutex();
211 * Prepares and notify error through error callback.
213 static void CALEErrorHandler(const char *remoteAddress,
218 #ifndef SINGLE_THREAD
220 * Stop condition of recvhandler.
222 static bool g_dataBleReceiverHandlerState = false;
225 * Sender information.
227 static u_arraylist_t *g_bleServerSenderInfo = NULL;
229 static u_arraylist_t *g_bleClientSenderInfo = NULL;
232 * Queue to process the outgoing packets from GATTClient.
234 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
237 * Queue to process the incoming packets.
239 static CAQueueingThread_t *g_bleReceiverQueue = NULL;
242 * Queue to process the outgoing packets from GATTServer.
244 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
247 * This function will be associated with the sender queue for
250 * This function will fragment the data to the MTU of the transport
251 * and send the data in fragments to the adapters. The function will
252 * be blocked until all data is sent out from the adapter.
254 * @param[in] threadData Data pushed to the queue which contains the
255 * info about RemoteEndpoint and Data.
257 static void CALEServerSendDataThread(void *threadData);
260 * This function will be associated with the sender queue for
263 * This function will fragment the data to the MTU of the transport
264 * and send the data in fragments to the adapters. The function will
265 * be blocked until all data is sent out from the adapter.
267 * @param[in] threadData Data pushed to the queue which contains the
268 * info about RemoteEndpoint and Data.
270 static void CALEClientSendDataThread(void *threadData);
273 * This function will be associated with the receiver queue.
275 * This function will defragment the received data from each sender
276 * respectively and will send it up to CA layer. Respective sender's
277 * header will provide the length of the data sent.
279 * @param[in] threadData Data pushed to the queue which contains the
280 * info about RemoteEndpoint and Data.
282 static void CALEDataReceiverHandler(void *threadData);
285 * This function will stop all queues created for GattServer and
286 * GattClient. All four queues will be be stopped with this function
289 static void CAStopLEQueues();
292 * This function will terminate all queues created for GattServer and
293 * GattClient. All four queues will be be terminated with this
294 * function invocations.
296 static void CATerminateLEQueues();
299 * This function will initalize the Receiver and Sender queues for
300 * GattServer. This function will in turn call the functions
301 * CAInitBleServerReceiverQueue() and CAInitBleServerSenderQueue() to
302 * initialize the queues.
304 * @return ::CA_STATUS_OK or Appropriate error code.
305 * @retval ::CA_STATUS_OK Successful.
306 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
307 * @retval ::CA_STATUS_FAILED Operation failed.
309 static CAResult_t CAInitLEServerQueues();
312 * This function will initalize the Receiver and Sender queues for
313 * GattClient. This function will inturn call the functions
314 * CAInitBleClientReceiverQueue() and CAInitBleClientSenderQueue() to
315 * initialize the queues.
317 * @return ::CA_STATUS_OK or Appropriate error code.
318 * @retval ::CA_STATUS_OK Successful.
319 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
320 * @retval ::CA_STATUS_FAILED Operation failed.
323 static CAResult_t CAInitLEClientQueues();
326 * This function will initalize the Receiver queue for
327 * GattServer. This will initialize the queue to process the function
328 * CABLEServerSendDataThread() when ever the task is added to this
331 * @return ::CA_STATUS_OK or Appropriate error code.
332 * @retval ::CA_STATUS_OK Successful.
333 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
334 * @retval ::CA_STATUS_FAILED Operation failed.
336 static CAResult_t CAInitLEServerSenderQueue();
339 * This function will initalize the Receiver queue for
340 * GattClient. This will initialize the queue to process the function
341 * CABLEClientSendDataThread() when ever the task is added to this
344 * @return ::CA_STATUS_OK or Appropriate error code.
345 * @retval ::CA_STATUS_OK Successful.
346 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
347 * @retval ::CA_STATUS_FAILED Operation failed.
349 static CAResult_t CAInitLEClientSenderQueue();
352 * This function will initialize the Receiver queue for
353 * LEAdapter. This will initialize the queue to process the function
354 * CABLEDataReceiverHandler() when ever the task is added to this
357 * @return ::CA_STATUS_OK or Appropriate error code
358 * @retval ::CA_STATUS_OK Successful
359 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
360 * @retval ::CA_STATUS_FAILED Operation failed
363 static CAResult_t CAInitLEReceiverQueue();
366 * This function will create the Data required to send it in the
369 * @param[in] remoteEndpoint Remote endpoint information of the
371 * @param[in] data Data to be transmitted from LE.
372 * @param[in] dataLength Length of the Data being transmitted.
374 * @return ::CA_STATUS_OK or Appropriate error code.
375 * @retval ::CA_STATUS_OK Successful.
376 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
377 * @retval ::CA_STATUS_FAILED Operation failed.
379 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
382 u_arraylist_t *senderInfo);
385 * Used to free the BLE information stored in the sender/receiver
388 * @param[in] bleData Information for a particular data segment.
390 static void CAFreeLEData(CALEData_t *bleData);
395 static void CALEDataDestroyer(void *data, uint32_t size);
397 #ifndef SINGLE_THREAD
399 * remove request or response data of send queue.
401 * @param[in] queueHandle queue to process the outgoing packets.
402 * @param[in] mutex mutex related to sender for client / server.
403 * @param[in] address target address to remove data in queue.
405 static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle,
407 const char* address);
410 * remove all received data of data list from receive queue.
412 * @param[in] dataInfoList received data list to remove for client / server.
413 * @param[in] address target address to remove data in queue.
415 static void CALERemoveReceiveQueueData(u_arraylist_t *dataInfoList,
416 const char* address);
419 static CAResult_t CAInitLEServerQueues()
421 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
423 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
425 CAResult_t result = CAInitLEServerSenderQueue();
426 if (CA_STATUS_OK != result)
428 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
429 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
430 return CA_STATUS_FAILED;
433 g_bleServerSenderInfo = u_arraylist_create();
434 if (!g_bleServerSenderInfo)
436 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
437 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
438 return CA_MEMORY_ALLOC_FAILED;
441 result = CAInitLEReceiverQueue();
442 if (CA_STATUS_OK != result)
444 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
445 u_arraylist_free(&g_bleServerSenderInfo);
446 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
447 return CA_STATUS_FAILED;
450 g_dataBleReceiverHandlerState = true;
452 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
454 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
458 static CAResult_t CAInitLEClientQueues()
460 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
462 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
464 CAResult_t result = CAInitLEClientSenderQueue();
465 if (CA_STATUS_OK != result)
467 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
468 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
469 return CA_STATUS_FAILED;
472 g_bleClientSenderInfo = u_arraylist_create();
473 if (!g_bleClientSenderInfo)
475 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
476 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
477 return CA_MEMORY_ALLOC_FAILED;
480 result = CAInitLEReceiverQueue();
481 if (CA_STATUS_OK != result)
483 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
484 u_arraylist_free(&g_bleClientSenderInfo);
485 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
486 return CA_STATUS_FAILED;
489 g_dataBleReceiverHandlerState = true;
491 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
493 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
497 static CAResult_t CAInitLEReceiverQueue()
499 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEReceiverQueue");
500 // Check if the message queue is already initialized
501 if (g_bleReceiverQueue)
503 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
507 // Create recv message queue
508 g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
509 if (!g_bleReceiverQueue)
511 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
512 return CA_MEMORY_ALLOC_FAILED;
515 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue,
516 g_bleAdapterThreadPool,
517 CALEDataReceiverHandler,
520 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
521 OICFree(g_bleReceiverQueue);
522 g_bleReceiverQueue = NULL;
523 return CA_STATUS_FAILED;
526 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue))
528 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
529 OICFree(g_bleReceiverQueue);
530 g_bleReceiverQueue = NULL;
531 return CA_STATUS_FAILED;
534 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
538 static CAResult_t CAInitLEServerSenderQueue()
540 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEServerSenderQueue");
541 // Check if the message queue is already initialized
542 if (g_bleServerSendQueueHandle)
544 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
548 // Create send message queue
549 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
550 if (!g_bleServerSendQueueHandle)
552 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
553 return CA_MEMORY_ALLOC_FAILED;
556 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
557 g_bleAdapterThreadPool,
558 CALEServerSendDataThread,
561 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
562 OICFree(g_bleServerSendQueueHandle);
563 g_bleServerSendQueueHandle = NULL;
564 return CA_STATUS_FAILED;
567 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
571 static void CALEClearSenderInfoImpl(u_arraylist_t ** list)
573 const size_t length = u_arraylist_length(*list);
574 for (size_t i = 0; i < length; ++i)
576 CABLESenderInfo_t * const info =
577 (CABLESenderInfo_t *) u_arraylist_get(*list, i);
580 OICFree(info->defragData);
581 CAFreeEndpoint(info->remoteEndpoint);
585 u_arraylist_free(list);
588 static void CALEClearSenderInfo()
590 CALEClearSenderInfoImpl(&g_bleServerSenderInfo);
591 CALEClearSenderInfoImpl(&g_bleClientSenderInfo);
594 static CAResult_t CAInitLEClientSenderQueue()
596 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEClientSenderQueue");
598 if (g_bleClientSendQueueHandle)
600 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
604 // Create send message queue
605 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
606 if (!g_bleClientSendQueueHandle)
608 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
609 return CA_MEMORY_ALLOC_FAILED;
612 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
613 g_bleAdapterThreadPool,
614 CALEClientSendDataThread, CALEDataDestroyer))
616 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
617 OICFree(g_bleClientSendQueueHandle);
618 g_bleClientSendQueueHandle = NULL;
619 return CA_STATUS_FAILED;
622 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAInitLEClientSenderQueue");
626 static void CAStopLEQueues()
628 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStopLEQueues");
630 ca_mutex_lock(g_bleReceiveDataMutex);
631 if (NULL != g_bleReceiverQueue)
633 CAQueueingThreadStop(g_bleReceiverQueue);
635 ca_mutex_unlock(g_bleReceiveDataMutex);
637 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAStopLEQueues");
640 static void CATerminateLEQueues()
642 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
644 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
645 OICFree(g_bleClientSendQueueHandle);
646 g_bleClientSendQueueHandle = NULL;
648 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
649 OICFree(g_bleServerSendQueueHandle);
650 g_bleServerSendQueueHandle = NULL;
652 CAQueueingThreadDestroy(g_bleReceiverQueue);
653 OICFree(g_bleReceiverQueue);
654 g_bleReceiverQueue = NULL;
656 CALEClearSenderInfo();
658 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
661 static CAResult_t CALEGetSenderInfo(const char *leAddress,
662 u_arraylist_t *senderInfoList,
663 CABLESenderInfo_t **senderInfo,
664 uint32_t *senderIndex)
666 VERIFY_NON_NULL_RET(leAddress,
668 "NULL BLE address argument",
669 CA_STATUS_INVALID_PARAM);
670 VERIFY_NON_NULL_RET(senderIndex,
672 "NULL index argument",
673 CA_STATUS_INVALID_PARAM);
675 const uint32_t listLength = u_arraylist_length(senderInfoList);
676 const uint32_t addrLength = strlen(leAddress);
677 for (uint32_t index = 0; index < listLength; index++)
679 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(senderInfoList, index);
680 if(!info || !(info->remoteEndpoint))
685 if(!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
687 *senderIndex = index;
696 return CA_STATUS_FAILED;
699 static void CALEDataReceiverHandler(void *threadData)
701 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDataReceiverHandler");
703 ca_mutex_lock(g_bleReceiveDataMutex);
705 if (g_dataBleReceiverHandlerState)
707 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
709 CALEData_t *bleData = (CALEData_t *) threadData;
712 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
713 ca_mutex_unlock(g_bleReceiveDataMutex);
717 if (!(bleData->senderInfo))
719 OIC_LOG(ERROR, CALEADAPTER_TAG, "sender info is not available");
720 ca_mutex_unlock(g_bleReceiveDataMutex);
724 if (!(bleData->remoteEndpoint))
726 OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!");
727 ca_mutex_unlock(g_bleReceiveDataMutex);
731 CABLESenderInfo_t *senderInfo = NULL;
732 uint32_t senderIndex = 0;
734 if(CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
736 &senderInfo, &senderIndex))
738 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s]",
739 bleData->remoteEndpoint->addr);
744 CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
747 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
748 ca_mutex_unlock(g_bleReceiveDataMutex);
751 newSender->recvDataLen = 0;
752 newSender->totalDataLen = 0;
753 newSender->defragData = NULL;
754 newSender->remoteEndpoint = NULL;
756 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
758 newSender->totalDataLen = coap_get_total_message_length(bleData->data, bleData->dataLen);
760 if(!(newSender->totalDataLen))
762 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
764 ca_mutex_unlock(g_bleReceiveDataMutex);
768 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
769 newSender->totalDataLen);
770 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
773 newSender->defragData = OICCalloc(newSender->totalDataLen + 1,
774 sizeof(*newSender->defragData));
776 if (NULL == newSender->defragData)
778 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
780 ca_mutex_unlock(g_bleReceiveDataMutex);
784 const char *remoteAddress = bleData->remoteEndpoint->addr;
785 newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
786 CA_ADAPTER_GATT_BTLE,
789 if (NULL == newSender->remoteEndpoint)
791 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
792 OICFree(newSender->defragData);
794 ca_mutex_unlock(g_bleReceiveDataMutex);
798 if (newSender->recvDataLen + bleData->dataLen > newSender->totalDataLen)
800 OIC_LOG(ERROR, CALEADAPTER_TAG, "buffer is smaller than received data");
801 OICFree(newSender->defragData);
802 CAFreeEndpoint(newSender->remoteEndpoint);
804 ca_mutex_unlock(g_bleReceiveDataMutex);
807 memcpy(newSender->defragData, bleData->data, bleData->dataLen);
808 newSender->recvDataLen += bleData->dataLen;
810 u_arraylist_add(bleData->senderInfo,(void *)newSender);
812 //Getting newSender index position in bleSenderInfo array list
814 CALEGetSenderInfo(newSender->remoteEndpoint->addr, bleData->senderInfo,
817 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
818 OICFree(newSender->defragData);
819 CAFreeEndpoint(newSender->remoteEndpoint);
821 ca_mutex_unlock(g_bleReceiveDataMutex);
824 senderInfo = newSender;
828 if(senderInfo->recvDataLen + bleData->dataLen > senderInfo->totalDataLen)
830 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
831 "Data Length exceeding error!! Receiving [%d] total length [%d]",
832 senderInfo->recvDataLen + bleData->dataLen, senderInfo->totalDataLen);
833 u_arraylist_remove(bleData->senderInfo, senderIndex);
834 OICFree(senderInfo->defragData);
836 ca_mutex_unlock(g_bleReceiveDataMutex);
839 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]",
841 memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data,
843 senderInfo->recvDataLen += bleData->dataLen ;
844 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] received Datalen [%d]",
845 senderInfo->totalDataLen, senderInfo->recvDataLen);
848 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
850 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
851 if (NULL == g_networkPacketReceivedCallback)
853 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
855 u_arraylist_remove(bleData->senderInfo, senderIndex);
856 OICFree(senderInfo->defragData);
858 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
859 ca_mutex_unlock(g_bleReceiveDataMutex);
863 OIC_LOG(DEBUG, CALEADAPTER_TAG, "[CALEDataReceiverHandler] Sending data up !");
865 const CASecureEndpoint_t tmp =
867 .endpoint = *senderInfo->remoteEndpoint
870 g_networkPacketReceivedCallback(&tmp,
871 senderInfo->defragData,
872 senderInfo->recvDataLen);
873 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
874 u_arraylist_remove(bleData->senderInfo, senderIndex);
875 senderInfo->remoteEndpoint = NULL;
876 senderInfo->defragData = NULL;
880 ca_mutex_unlock(g_bleReceiveDataMutex);
881 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
884 static void CALEServerSendDataThread(void *threadData)
886 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEServerSendDataThread");
888 CALEData_t * const bleData = (CALEData_t *) threadData;
891 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
895 const uint32_t totalLength = bleData->dataLen;
899 "Server total Data length with header is [%u]",
902 uint8_t * const dataSegment = OICCalloc(totalLength, 1);
904 if (NULL == dataSegment)
906 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
911 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
913 length = totalLength;
914 memcpy(dataSegment, bleData->data, bleData->dataLen);
918 length = CA_SUPPORTED_BLE_MTU_SIZE;
919 memcpy(dataSegment, bleData->data, CA_SUPPORTED_BLE_MTU_SIZE);
922 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
924 CAResult_t result = CA_STATUS_FAILED;
926 // Send the first segment with the header.
927 if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
929 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
931 result = CAUpdateCharacteristicsToGattClient(
932 bleData->remoteEndpoint->addr, dataSegment, length);
934 if (CA_STATUS_OK != result)
938 "Update characteristics failed, result [%d]",
941 g_errorHandler(bleData->remoteEndpoint,
945 OICFree(dataSegment);
951 "Server Sent data length [%u]",
953 for (index = 1; index < iter; index++)
955 // Send the remaining header.
958 "Sending the chunk number [%u]",
962 CAUpdateCharacteristicsToGattClient(
963 bleData->remoteEndpoint->addr,
964 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE)),
965 CA_SUPPORTED_BLE_MTU_SIZE);
967 if (CA_STATUS_OK != result)
969 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
970 "Update characteristics failed, result [%d]", result);
971 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
972 OICFree(dataSegment);
975 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
976 CA_SUPPORTED_BLE_MTU_SIZE);
979 const uint32_t remainingLen =
980 totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
982 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
984 // send the last segment of the data (Ex: 22 bytes of 622
985 // bytes of data when MTU is 200)
986 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
988 result = CAUpdateCharacteristicsToGattClient(
989 bleData->remoteEndpoint->addr,
990 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
993 if (CA_STATUS_OK != result)
997 "Update characteristics failed, result [%d]",
999 g_errorHandler(bleData->remoteEndpoint,
1003 OICFree(dataSegment);
1006 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1011 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
1012 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
1013 if (CA_STATUS_OK != result)
1015 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1017 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1018 OICFree(dataSegment);
1021 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1022 for (index = 1; index < iter; index++)
1024 // Send the remaining header.
1025 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1027 result = CAUpdateCharacteristicsToAllGattClients(
1028 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE)),
1029 CA_SUPPORTED_BLE_MTU_SIZE);
1031 if (CA_STATUS_OK != result)
1033 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1035 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1036 OICFree(dataSegment);
1039 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%u]",
1040 CA_SUPPORTED_BLE_MTU_SIZE);
1043 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1044 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1046 // send the last segment of the data
1047 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1049 result = CAUpdateCharacteristicsToAllGattClients(
1050 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1053 if (CA_STATUS_OK != result)
1055 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1057 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1058 OICFree(dataSegment);
1061 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1064 OICFree(dataSegment);
1066 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CALEServerSendDataThread");
1069 static void CALEClientSendDataThread(void *threadData)
1071 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEClientSendDataThread");
1073 CALEData_t *bleData = (CALEData_t *) threadData;
1076 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1080 const uint32_t totalLength = bleData->dataLen;
1082 uint8_t *dataSegment = OICCalloc(totalLength, 1);
1083 if (NULL == dataSegment)
1085 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1089 uint32_t length = 0;
1090 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1092 length = totalLength;
1099 length = CA_SUPPORTED_BLE_MTU_SIZE;
1102 CA_SUPPORTED_BLE_MTU_SIZE);
1105 CAResult_t result = CA_STATUS_FAILED;
1106 const uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1108 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1110 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1111 // Send the first segment with the header.
1113 CAUpdateCharacteristicsToGattServer(
1114 bleData->remoteEndpoint->addr,
1120 if (CA_STATUS_OK != result)
1124 "Update characteristics failed, result [%d]",
1126 g_errorHandler(bleData->remoteEndpoint,
1130 OICFree(dataSegment);
1136 "Client Sent Data length is [%u]",
1139 for (index = 1; index < iter; index++)
1141 // Send the remaining header.
1142 result = CAUpdateCharacteristicsToGattServer(
1143 bleData->remoteEndpoint->addr,
1144 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1145 CA_SUPPORTED_BLE_MTU_SIZE,
1148 if (CA_STATUS_OK != result)
1152 "Update characteristics failed, result [%d]",
1154 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1155 OICFree(dataSegment);
1158 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1159 CA_SUPPORTED_BLE_MTU_SIZE);
1162 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1163 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1165 // send the last segment of the data (Ex: 22 bytes of 622
1166 // bytes of data when MTU is 200)
1167 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1169 result = CAUpdateCharacteristicsToGattServer(
1170 bleData->remoteEndpoint->addr,
1171 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1175 if (CA_STATUS_OK != result)
1177 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1179 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1180 OICFree(dataSegment);
1183 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1188 //Sending Mulitcast Data
1189 // Send the first segment with the header.
1190 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1191 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1192 if (CA_STATUS_OK != result)
1194 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1195 "Update characteristics (all) failed, result [%d]", result);
1196 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1197 OICFree(dataSegment);
1200 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1201 // Send the remaining header.
1202 for (index = 1; index < iter; index++)
1204 result = CAUpdateCharacteristicsToAllGattServers(
1205 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1206 CA_SUPPORTED_BLE_MTU_SIZE);
1208 if (CA_STATUS_OK != result)
1210 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1212 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1213 OICFree(dataSegment);
1216 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1217 CA_SUPPORTED_BLE_MTU_SIZE);
1220 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1221 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1223 // send the last segment of the data (Ex: 22 bytes of 622
1224 // bytes of data when MTU is 200)
1225 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1227 CAUpdateCharacteristicsToAllGattServers(
1228 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1231 if (CA_STATUS_OK != result)
1233 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1234 "Update characteristics (all) failed, result [%d]", result);
1235 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1236 OICFree(dataSegment);
1239 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1244 OICFree(dataSegment);
1246 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1249 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
1250 const uint8_t *data,
1251 uint32_t dataLength,
1252 u_arraylist_t *senderInfo)
1254 CALEData_t * const bleData = OICMalloc(sizeof(CALEData_t));
1258 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1262 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1263 bleData->data = OICCalloc(dataLength + 1, 1);
1265 if (NULL == bleData->data)
1267 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1268 CAFreeLEData(bleData);
1272 memcpy(bleData->data, data, dataLength);
1273 bleData->dataLen = dataLength;
1276 bleData->senderInfo = senderInfo;
1282 static void CAFreeLEData(CALEData_t *bleData)
1284 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1286 CAFreeEndpoint(bleData->remoteEndpoint);
1287 OICFree(bleData->data);
1291 static void CALEDataDestroyer(void *data, uint32_t size)
1293 if ((size_t)size < sizeof(CALEData_t *))
1295 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1296 "Destroy data too small %p %d", data, size);
1298 CALEData_t *ledata = (CALEData_t *) data;
1300 CAFreeLEData(ledata);
1304 static CAResult_t CAInitLEAdapterMutex()
1306 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEAdapterMutex");
1308 if (NULL == g_bleIsServerMutex)
1310 g_bleIsServerMutex = ca_mutex_new();
1311 if (NULL == g_bleIsServerMutex)
1313 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1314 return CA_STATUS_FAILED;
1318 if (NULL == g_bleNetworkCbMutex)
1320 g_bleNetworkCbMutex = ca_mutex_new();
1321 if (NULL == g_bleNetworkCbMutex)
1323 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1324 CATerminateLEAdapterMutex();
1325 return CA_STATUS_FAILED;
1329 if (NULL == g_bleLocalAddressMutex)
1331 g_bleLocalAddressMutex = ca_mutex_new();
1332 if (NULL == g_bleLocalAddressMutex)
1334 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1335 CATerminateLEAdapterMutex();
1336 return CA_STATUS_FAILED;
1340 if (NULL == g_bleAdapterThreadPoolMutex)
1342 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1343 if (NULL == g_bleAdapterThreadPoolMutex)
1345 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1346 CATerminateLEAdapterMutex();
1347 return CA_STATUS_FAILED;
1351 if (NULL == g_bleClientSendDataMutex)
1353 g_bleClientSendDataMutex = ca_mutex_new();
1354 if (NULL == g_bleClientSendDataMutex)
1356 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1357 CATerminateLEAdapterMutex();
1358 return CA_STATUS_FAILED;
1362 if (NULL == g_bleServerSendDataMutex)
1364 g_bleServerSendDataMutex = ca_mutex_new();
1365 if (NULL == g_bleServerSendDataMutex)
1367 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1368 CATerminateLEAdapterMutex();
1369 return CA_STATUS_FAILED;
1373 if (NULL == g_bleAdapterReqRespCbMutex)
1375 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1376 if (NULL == g_bleAdapterReqRespCbMutex)
1378 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1379 CATerminateLEAdapterMutex();
1380 return CA_STATUS_FAILED;
1384 if (NULL == g_bleReceiveDataMutex)
1386 g_bleReceiveDataMutex = ca_mutex_new();
1387 if (NULL == g_bleReceiveDataMutex)
1389 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1390 return CA_STATUS_FAILED;
1394 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1395 return CA_STATUS_OK;
1398 static void CATerminateLEAdapterMutex()
1400 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CATerminateLEAdapterMutex");
1402 ca_mutex_free(g_bleIsServerMutex);
1403 g_bleIsServerMutex = NULL;
1405 ca_mutex_free(g_bleNetworkCbMutex);
1406 g_bleNetworkCbMutex = NULL;
1408 ca_mutex_free(g_bleLocalAddressMutex);
1409 g_bleLocalAddressMutex = NULL;
1411 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1412 g_bleAdapterThreadPoolMutex = NULL;
1414 ca_mutex_free(g_bleClientSendDataMutex);
1415 g_bleClientSendDataMutex = NULL;
1417 ca_mutex_free(g_bleServerSendDataMutex);
1418 g_bleServerSendDataMutex = NULL;
1420 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1421 g_bleAdapterReqRespCbMutex = NULL;
1423 ca_mutex_free(g_bleReceiveDataMutex);
1424 g_bleReceiveDataMutex = NULL;
1426 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1430 * Starting LE connectivity adapters.
1432 * As its peer to peer it does not require to start any servers.
1434 * @return ::CA_STATUS_OK or Appropriate error code.
1436 static CAResult_t CAStartLE();
1439 * Start listening server for receiving multicast search requests.
1441 * Transport Specific Behavior:
1442 * LE Starts GATT Server with prefixed UUID and Characteristics
1443 * per OIC Specification.
1444 * @return ::CA_STATUS_OK or Appropriate error code.
1446 static CAResult_t CAStartLEListeningServer();
1449 * Stops listening server from receiving multicast search requests.
1451 * Transport Specific Behavior:
1452 * LE Starts GATT Server with prefixed UUID and Characteristics
1453 * per OIC Specification.
1454 * @return ::CA_STATUS_OK or Appropriate error code.
1456 static CAResult_t CAStopLEListeningServer();
1459 * Sarting discovery of servers for receiving multicast
1462 * Transport Specific Behavior:
1463 * LE Starts GATT Server with prefixed UUID and Characteristics
1464 * per OIC Specification.
1466 * @return ::CA_STATUS_OK or Appropriate error code
1468 static CAResult_t CAStartLEDiscoveryServer();
1471 * Send data to the endpoint using the adapter connectivity.
1473 * @param[in] endpoint Remote Endpoint information (like MAC address,
1474 * reference URI and connectivity type) to which
1475 * the unicast data has to be sent.
1476 * @param[in] data Data which required to be sent.
1477 * @param[in] dataLen Size of data to be sent.
1479 * @note dataLen must be > 0.
1481 * @return The number of bytes sent on the network, or -1 on error.
1483 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1488 * Send multicast data to the endpoint using the LE connectivity.
1490 * @param[in] endpoint Remote Endpoint information to which the
1491 * multicast data has to be sent.
1492 * @param[in] data Data which required to be sent.
1493 * @param[in] dataLen Size of data to be sent.
1495 * @note dataLen must be > 0.
1497 * @return The number of bytes sent on the network, or -1 on error.
1499 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1504 * Get LE Connectivity network information.
1506 * @param[out] info Local connectivity information structures.
1507 * @param[out] size Number of local connectivity structures.
1509 * @return ::CA_STATUS_OK or Appropriate error code.
1511 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info,
1515 * Read Synchronous API callback.
1517 * @return ::CA_STATUS_OK or Appropriate error code.
1519 static CAResult_t CAReadLEData();
1522 * Stopping the adapters and close socket connections.
1524 * LE Stops all GATT servers and GATT Clients.
1526 * @return ::CA_STATUS_OK or Appropriate error code.
1528 static CAResult_t CAStopLE();
1531 * Terminate the LE connectivity adapter.
1533 * Configuration information will be deleted from further use.
1535 static void CATerminateLE();
1538 * This function will receive the data from the GattServer and add the
1539 * data to the Server receiver queue.
1541 * @param[in] remoteAddress Remote address of the device from where
1543 * @param[in] data Actual data received from the remote
1545 * @param[in] dataLength Length of the data received from the
1547 * @param[in] sentLength Length of the data sent from the remote
1550 * @return ::CA_STATUS_OK or Appropriate error code.
1551 * @retval ::CA_STATUS_OK Successful.
1552 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1553 * @retval ::CA_STATUS_FAILED Operation failed.
1556 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
1557 const uint8_t *data,
1558 uint32_t dataLength,
1559 uint32_t *sentLength);
1562 * This function will receive the data from the GattClient and add the
1563 * data into the Client receiver queue.
1565 * @param[in] remoteAddress Remote address of the device from where
1567 * @param[in] data Actual data recevied from the remote
1569 * @param[in] dataLength Length of the data received from the
1571 * @param[in] sentLength Length of the data sent from the remote
1574 * @return ::CA_STATUS_OK or Appropriate error code.
1575 * @retval ::CA_STATUS_OK Successful.
1576 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1577 * @retval ::CA_STATUS_FAILED Operation failed.
1579 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
1580 const uint8_t *data,
1581 uint32_t dataLength,
1582 uint32_t *sentLength);
1585 * Set the NetworkPacket received callback to CA layer from adapter
1588 * @param[in] callback Callback handle sent from the upper layer.
1590 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
1593 * Push the data from CA layer to the Sender processor queue.
1595 * @param[in] remoteEndpoint Remote endpoint information of the
1597 * @param[in] data Data to be transmitted from LE.
1598 * @param[in] dataLen Length of the Data being transmitted.
1600 * @return ::CA_STATUS_OK or Appropriate error code.
1601 * @retval ::CA_STATUS_OK Successful.
1602 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1603 * @retval ::CA_STATUS_FAILED Operation failed.
1605 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1606 const uint8_t *data,
1610 * Push the data from CA layer to the Sender processor queue.
1612 * @param[in] remoteEndpoint Remote endpoint information of the
1614 * @param[in] data Data to be transmitted from LE.
1615 * @param[in] dataLen Length of the Data being transmitted.
1617 * @return ::CA_STATUS_OK or Appropriate error code.
1618 * @retval ::CA_STATUS_OK Successful.
1619 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1620 * @retval ::CA_STATUS_FAILED Operation failed.
1622 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1623 const uint8_t *data,
1626 static CAResult_t CALEAdapterGattServerStart()
1628 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1630 CAResult_t result = CAStartLEGattServer();
1632 #ifndef SINGLE_THREAD
1634 Don't start the server side sending queue thread until the
1635 server itself has actually started.
1637 if (CA_STATUS_OK == result)
1639 ca_mutex_lock(g_bleServerSendDataMutex);
1640 result = CAQueueingThreadStart(g_bleServerSendQueueHandle);
1641 ca_mutex_unlock(g_bleServerSendDataMutex);
1643 if (CA_STATUS_OK != result)
1647 "Unable to start server queuing thread (%d)",
1656 static CAResult_t CALEAdapterGattServerStop()
1658 #ifndef SINGLE_THREAD
1659 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEAdapterGattServerStop");
1661 CAResult_t result = CAStopLEGattServer();
1662 ca_mutex_lock(g_bleServerSendDataMutex);
1663 if (CA_STATUS_OK == result)
1665 result = CAQueueingThreadStop(g_bleServerSendQueueHandle);
1667 ca_mutex_unlock(g_bleServerSendDataMutex);
1671 return CAStopLEGattServer();
1675 static CAResult_t CALEAdapterGattClientStart()
1677 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattClient");
1679 CAResult_t result = CAStartLEGattClient();
1681 #ifndef SINGLE_THREAD
1683 Don't start the client side sending queue thread until the
1684 client itself has actually started.
1686 if (CA_STATUS_OK == result)
1688 ca_mutex_lock(g_bleClientSendDataMutex);
1689 result = CAQueueingThreadStart(g_bleClientSendQueueHandle);
1690 ca_mutex_unlock(g_bleClientSendDataMutex);
1692 if (CA_STATUS_OK != result)
1696 "Unable to start client queuing thread");
1704 static CAResult_t CALEAdapterGattClientStop()
1706 #ifndef SINGLE_THREAD
1707 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEAdapterGattClientStop");
1708 CAStopLEGattClient();
1710 ca_mutex_lock(g_bleClientSendDataMutex);
1711 CAResult_t result = CAQueueingThreadStop(g_bleClientSendQueueHandle);
1712 ca_mutex_unlock(g_bleClientSendDataMutex);
1716 CAStopLEGattClient();
1718 return CA_STATUS_OK;
1722 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1723 CANetworkPacketReceivedCallback reqRespCallback,
1724 CANetworkChangeCallback netCallback,
1725 CAErrorHandleCallback errorCallback,
1726 ca_thread_pool_t handle)
1728 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1731 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1732 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1733 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1735 CAResult_t result = CA_STATUS_OK;
1736 result = CAInitLEAdapterMutex();
1737 if (CA_STATUS_OK != result)
1739 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1740 return CA_STATUS_FAILED;
1743 result = CAInitializeLENetworkMonitor();
1744 if (CA_STATUS_OK != result)
1746 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1747 return CA_STATUS_FAILED;
1749 CAInitializeLEAdapter(handle);
1751 CASetLEClientThreadPoolHandle(handle);
1753 result = CAInitializeLEGattClient();
1754 if (CA_STATUS_OK != result)
1756 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattClient() failed");
1757 return CA_STATUS_FAILED;
1760 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1761 CASetLEServerThreadPoolHandle(handle);
1762 result = CAInitializeLEGattServer();
1763 if (CA_STATUS_OK != result)
1765 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattServer() failed");
1766 return CA_STATUS_FAILED;
1769 CASetLEAdapterThreadPoolHandle(handle);
1770 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1771 CASetLEReqRespAdapterCallback(reqRespCallback);
1773 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1774 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1775 CALERegisterNetworkNotifications(netCallback);
1777 g_errorHandler = errorCallback;
1779 static const CAConnectivityHandler_t connHandler =
1781 .startAdapter = CAStartLE,
1782 .stopAdapter = CAStopLE,
1783 .startListenServer = CAStartLEListeningServer,
1784 .stopListenServer = CAStopLEListeningServer,
1785 .startDiscoveryServer = CAStartLEDiscoveryServer,
1786 .sendData = CASendLEUnicastData,
1787 .sendDataToAll = CASendLEMulticastData,
1788 .GetnetInfo = CAGetLEInterfaceInformation,
1789 .readData = CAReadLEData,
1790 .terminate = CATerminateLE,
1791 .cType = CA_ADAPTER_GATT_BTLE
1794 registerCallback(connHandler);
1796 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1798 return CA_STATUS_OK;
1801 static CAResult_t CAStartLE()
1803 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE");
1805 return CAStartLEAdapter();
1808 static CAResult_t CAStopLE()
1810 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1811 #ifndef SINGLE_THREAD
1815 ca_mutex_lock(g_bleIsServerMutex);
1816 switch (g_adapterType)
1818 case ADAPTER_SERVER:
1819 CALEAdapterGattServerStop();
1821 case ADAPTER_CLIENT:
1822 CALEAdapterGattClientStop();
1824 case ADAPTER_BOTH_CLIENT_SERVER:
1825 CALEAdapterGattServerStop();
1826 CALEAdapterGattClientStop();
1831 ca_mutex_unlock(g_bleIsServerMutex);
1833 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1835 return CAStopLEAdapter();
1838 static void CATerminateLE()
1840 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1842 CASetLEReqRespServerCallback(NULL);
1843 CASetLEReqRespClientCallback(NULL);
1844 CALERegisterNetworkNotifications(NULL);
1845 CASetLEReqRespAdapterCallback(NULL);
1846 CATerminateLENetworkMonitor();
1848 ca_mutex_lock(g_bleIsServerMutex);
1849 switch (g_adapterType)
1851 case ADAPTER_SERVER:
1852 CATerminateLEGattServer();
1854 case ADAPTER_CLIENT:
1855 CATerminateLEGattClient();
1857 case ADAPTER_BOTH_CLIENT_SERVER:
1858 CATerminateLEGattServer();
1859 CATerminateLEGattClient();
1864 g_adapterType = ADAPTER_EMPTY;
1865 ca_mutex_unlock(g_bleIsServerMutex);
1867 #ifndef SINGLE_THREAD
1868 CATerminateLEQueues();
1870 CATerminateLEAdapterMutex();
1872 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1875 static CAResult_t CAStartLEListeningServer()
1877 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEListeningServer");
1878 #ifndef ROUTING_GATEWAY
1879 CAResult_t result = CA_STATUS_OK;
1880 #ifndef SINGLE_THREAD
1881 result = CAInitLEServerQueues();
1882 if (CA_STATUS_OK != result)
1884 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1889 ca_mutex_lock(g_bleIsServerMutex);
1890 switch (g_adapterType)
1892 case ADAPTER_CLIENT:
1893 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1895 case ADAPTER_BOTH_CLIENT_SERVER:
1898 g_adapterType = ADAPTER_SERVER;
1900 ca_mutex_unlock(g_bleIsServerMutex);
1902 result = CAGetLEAdapterState();
1903 if (CA_STATUS_OK != result)
1905 if (CA_ADAPTER_NOT_ENABLED == result)
1909 "Listen Server will be started once BT Adapter is enabled");
1914 result = CALEAdapterGattServerStart();
1917 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1920 // Routing Gateway only supports BLE client mode.
1921 OIC_LOG(ERROR, CALEADAPTER_TAG, "LE server not supported in Routing Gateway");
1922 return CA_NOT_SUPPORTED;
1926 static CAResult_t CAStopLEListeningServer()
1928 OIC_LOG(ERROR, CALEADAPTER_TAG, "Listen server stop not supported.");
1929 return CA_NOT_SUPPORTED;
1932 static CAResult_t CAStartLEDiscoveryServer()
1934 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEDiscoveryServer");
1935 CAResult_t result = CA_STATUS_OK;
1936 #ifndef SINGLE_THREAD
1937 result = CAInitLEClientQueues();
1938 if (CA_STATUS_OK != result)
1940 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1945 ca_mutex_lock(g_bleIsServerMutex);
1946 switch (g_adapterType)
1948 case ADAPTER_SERVER:
1949 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1951 case ADAPTER_BOTH_CLIENT_SERVER:
1954 g_adapterType = ADAPTER_CLIENT;
1956 ca_mutex_unlock(g_bleIsServerMutex);
1958 result = CAGetLEAdapterState();
1959 if (CA_STATUS_OK != result)
1961 if (CA_ADAPTER_NOT_ENABLED == result)
1965 "Discovery Server will be started once BT Adapter is enabled");
1970 result = CALEAdapterGattClientStart();
1973 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1977 static CAResult_t CAReadLEData()
1979 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1980 #ifdef SINGLE_THREAD
1983 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1984 return CA_STATUS_OK;
1987 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1991 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEUnicastData");
1994 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1995 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1997 CAResult_t result = CA_STATUS_FAILED;
1999 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
2000 if (ADAPTER_EMPTY == g_adapterType)
2002 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2005 ca_mutex_lock(g_bleIsServerMutex);
2006 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2008 result = CALEAdapterServerSendData(endpoint, data, dataLen);
2009 if (CA_STATUS_OK != result)
2011 ca_mutex_unlock(g_bleIsServerMutex);
2012 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed");
2015 g_errorHandler(endpoint, data, dataLen, result);
2022 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2024 result = CALEAdapterClientSendData(endpoint, data, dataLen);
2025 if (CA_STATUS_OK != result)
2027 ca_mutex_unlock(g_bleIsServerMutex);
2028 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for client failed" );
2032 g_errorHandler(endpoint, data, dataLen, result);
2037 ca_mutex_unlock(g_bleIsServerMutex);
2039 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2043 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
2047 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEMulticastData");
2050 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
2054 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
2058 CAResult_t result = CA_STATUS_FAILED;
2060 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
2061 if (ADAPTER_EMPTY == g_adapterType)
2063 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2066 ca_mutex_lock(g_bleIsServerMutex);
2067 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2069 result = CALEAdapterServerSendData(NULL, data, dataLen);
2070 if (CA_STATUS_OK != result)
2072 ca_mutex_unlock(g_bleIsServerMutex);
2074 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data for server failed" );
2078 g_errorHandler(endpoint, data, dataLen, result);
2084 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2086 result = CALEAdapterClientSendData(NULL, data, dataLen);
2087 if (CA_STATUS_OK != result)
2089 ca_mutex_unlock(g_bleIsServerMutex);
2091 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data for client failed" );
2095 g_errorHandler(endpoint, data, dataLen, result);
2100 ca_mutex_unlock(g_bleIsServerMutex);
2102 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CASendLEMulticastData");
2106 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
2108 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2110 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
2112 char *local_address = NULL;
2114 CAResult_t res = CAGetLEAddress(&local_address);
2115 if (CA_STATUS_OK != res)
2117 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
2121 if (NULL == local_address)
2123 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
2124 return CA_STATUS_FAILED;
2128 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
2129 if (NULL == (*info))
2131 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
2132 OICFree(local_address);
2133 return CA_STATUS_FAILED;
2136 size_t local_address_len = strlen(local_address);
2138 if(local_address_len >= sizeof(g_localBLEAddress) ||
2139 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
2141 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
2143 OICFree(local_address);
2144 return CA_STATUS_FAILED;
2147 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
2148 ca_mutex_lock(g_bleLocalAddressMutex);
2149 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
2150 ca_mutex_unlock(g_bleLocalAddressMutex);
2152 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
2154 OICFree(local_address);
2156 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2157 return CA_STATUS_OK;
2160 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
2162 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2164 ca_mutex_lock(g_bleNetworkCbMutex);
2165 g_networkCallback = netCallback;
2166 ca_mutex_unlock(g_bleNetworkCbMutex);
2167 CAResult_t res = CA_STATUS_OK;
2170 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
2171 if (CA_STATUS_OK != res)
2173 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2176 res = CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCb);
2177 if (CA_STATUS_OK != res)
2179 OIC_LOG(ERROR, CALEADAPTER_TAG, "CALEConnectionStateChangedCb failed!");
2184 res = CAUnSetLEAdapterStateChangedCb();
2185 if (CA_STATUS_OK != res)
2187 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2191 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2195 static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
2198 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEConnectionStateChangedCb");
2200 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2204 ca_mutex_lock(g_bleIsServerMutex);
2205 switch (g_adapterType)
2207 case ADAPTER_SERVER:
2208 CALEGattServerConnectionStateChanged(isConnected, address);
2210 case ADAPTER_CLIENT:
2211 CALEGattConnectionStateChanged(isConnected, address);
2213 case ADAPTER_BOTH_CLIENT_SERVER:
2214 CALEGattConnectionStateChanged(isConnected, address);
2215 CALEGattServerConnectionStateChanged(isConnected, address);
2220 ca_mutex_unlock(g_bleIsServerMutex);
2225 #ifndef SINGLE_THREAD
2226 if(g_bleClientSenderInfo)
2228 CALERemoveReceiveQueueData(g_bleClientSenderInfo, address);
2231 if(g_bleServerSenderInfo)
2233 CALERemoveReceiveQueueData(g_bleServerSenderInfo, address);
2236 // remove data of send queue.
2237 if (g_bleClientSendQueueHandle)
2239 CALERemoveSendQueueData(g_bleClientSendQueueHandle,
2240 g_bleClientSendDataMutex,
2244 if (g_bleServerSendQueueHandle)
2246 CALERemoveSendQueueData(g_bleServerSendQueueHandle,
2247 g_bleServerSendDataMutex,
2253 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2256 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
2258 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDeviceStateChangedCb");
2260 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
2261 CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE };
2263 ca_mutex_lock(g_bleLocalAddressMutex);
2264 OICStrcpy(localEndpoint.addr,
2265 sizeof(localEndpoint.addr),
2267 ca_mutex_unlock(g_bleLocalAddressMutex);
2269 if (CA_ADAPTER_ENABLED == adapter_state)
2271 ca_mutex_lock(g_bleIsServerMutex);
2272 switch (g_adapterType)
2274 case ADAPTER_SERVER:
2275 CALEAdapterGattServerStart();
2277 case ADAPTER_CLIENT:
2278 CALEAdapterGattClientStart();
2280 case ADAPTER_BOTH_CLIENT_SERVER:
2281 CALEAdapterGattServerStart();
2282 CALEAdapterGattClientStart();
2287 ca_mutex_unlock(g_bleIsServerMutex);
2291 ca_mutex_lock(g_bleIsServerMutex);
2292 switch (g_adapterType)
2294 case ADAPTER_SERVER:
2295 CALEAdapterGattServerStop();
2297 case ADAPTER_CLIENT:
2298 CALEAdapterGattClientStop();
2300 case ADAPTER_BOTH_CLIENT_SERVER:
2301 CALEAdapterGattServerStop();
2302 CALEAdapterGattClientStop();
2307 ca_mutex_unlock(g_bleIsServerMutex);
2310 ca_mutex_lock(g_bleNetworkCbMutex);
2311 if (NULL != g_networkCallback)
2313 g_networkCallback(&localEndpoint, adapter_state);
2317 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
2319 ca_mutex_unlock(g_bleNetworkCbMutex);
2321 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2324 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
2325 const uint8_t *data,
2328 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2330 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2331 #ifndef SINGLE_THREAD
2332 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2333 "g_bleClientSendQueueHandle is NULL",
2335 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
2336 "g_bleClientSendDataMutex is NULL",
2339 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2340 "g_bleClientSendQueueHandle",
2343 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
2345 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2348 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2349 return CA_MEMORY_ALLOC_FAILED;
2351 // Add message to send queue
2352 ca_mutex_lock(g_bleClientSendDataMutex);
2353 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
2354 ca_mutex_unlock(g_bleClientSendDataMutex);
2356 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2357 return CA_STATUS_OK;
2360 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2361 const uint8_t *data,
2364 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2366 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2368 #ifdef SINGLE_THREAD
2369 if (!CAIsLEConnected())
2371 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2372 return CA_STATUS_FAILED;
2375 CAResult_t result = CA_STATUS_OK;
2376 const uint32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2377 for (uint32_t iter = 0; iter < dataLimit; iter++)
2380 CAUpdateCharacteristicsToAllGattClients(
2381 data + (iter * CA_SUPPORTED_BLE_MTU_SIZE),
2382 CA_SUPPORTED_BLE_MTU_SIZE);
2384 if (CA_STATUS_OK != result)
2386 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2387 return CA_STATUS_FAILED;
2393 const uint32_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2397 CAUpdateCharacteristicsToAllGattClients(
2398 data + (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE),
2400 if (CA_STATUS_OK != result)
2402 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2403 return CA_STATUS_FAILED;
2408 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2409 "BleClientReceiverQueue is NULL",
2411 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2412 "BleClientSendDataMutex is NULL",
2415 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2418 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2420 CALEData_t * const bleData =
2421 CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2425 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2426 return CA_MEMORY_ALLOC_FAILED;
2429 // Add message to send queue
2430 ca_mutex_lock(g_bleServerSendDataMutex);
2431 CAQueueingThreadAddData(g_bleServerSendQueueHandle,
2433 sizeof(CALEData_t));
2434 ca_mutex_unlock(g_bleServerSendDataMutex);
2436 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2437 return CA_STATUS_OK;
2440 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2441 const uint8_t *data,
2442 uint32_t dataLength,
2443 uint32_t *sentLength)
2445 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2448 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2449 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2451 #ifdef SINGLE_THREAD
2452 if(g_networkPacketReceivedCallback)
2454 // will be filled by upper layer
2455 const CASecureEndpoint_t endpoint =
2456 { .endpoint = { .adapter = CA_ADAPTER_GATT_BTLE } };
2459 g_networkPacketReceivedCallback(&endpoint, data, dataLength);
2462 VERIFY_NON_NULL_RET(g_bleReceiverQueue,
2464 "g_bleReceiverQueue",
2467 //Add message to data queue
2468 CAEndpoint_t * const remoteEndpoint =
2469 CACreateEndpointObject(CA_DEFAULT_FLAGS,
2470 CA_ADAPTER_GATT_BTLE,
2474 if (NULL == remoteEndpoint)
2476 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2477 return CA_STATUS_FAILED;
2480 // Create bleData to add to queue
2483 "Data received from LE layer [%d]",
2486 CALEData_t * const bleData =
2487 CACreateLEData(remoteEndpoint, data, dataLength, g_bleServerSenderInfo);
2491 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2492 CAFreeEndpoint(remoteEndpoint);
2493 return CA_MEMORY_ALLOC_FAILED;
2496 CAFreeEndpoint(remoteEndpoint);
2497 // Add message to receiver queue
2498 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2500 *sentLength = dataLength;
2502 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2503 return CA_STATUS_OK;
2506 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2507 const uint8_t *data,
2508 uint32_t dataLength,
2509 uint32_t *sentLength)
2511 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2514 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2515 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2516 #ifndef SINGLE_THREAD
2517 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG,
2518 "g_bleReceiverQueue",
2521 //Add message to data queue
2522 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2523 CA_ADAPTER_GATT_BTLE,
2525 if (NULL == remoteEndpoint)
2527 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2528 return CA_STATUS_FAILED;
2531 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%u]", dataLength);
2533 // Create bleData to add to queue
2534 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data,
2535 dataLength, g_bleClientSenderInfo);
2538 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2539 CAFreeEndpoint(remoteEndpoint);
2540 return CA_MEMORY_ALLOC_FAILED;
2543 CAFreeEndpoint(remoteEndpoint);
2544 // Add message to receiver queue
2545 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2547 *sentLength = dataLength;
2549 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2550 return CA_STATUS_OK;
2553 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2555 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2557 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2558 g_bleAdapterThreadPool = handle;
2559 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2561 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2564 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2566 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2568 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2570 g_networkPacketReceivedCallback = callback;
2572 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2574 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2577 static void CALEErrorHandler(const char *remoteAddress,
2578 const uint8_t *data,
2582 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2584 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2586 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2587 CA_ADAPTER_GATT_BTLE,
2591 // if required, will be used to build remote endpoint
2592 g_errorHandler(rep, data, dataLen, result);
2594 CAFreeEndpoint(rep);
2596 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");
2599 #ifndef SINGLE_THREAD
2600 static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle, ca_mutex mutex,
2601 const char* address)
2603 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALERemoveSendQueueData");
2605 VERIFY_NON_NULL_VOID(queueHandle, CALEADAPTER_TAG, "queueHandle");
2606 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2608 ca_mutex_lock(mutex);
2609 while (u_queue_get_size(queueHandle->dataQueue) > 0)
2611 OIC_LOG(DEBUG, CALEADAPTER_TAG, "get data from queue");
2612 u_queue_message_t *message = u_queue_get_element(queueHandle->dataQueue);
2613 if (NULL != message)
2615 CALEData_t *bleData = (CALEData_t *) message->msg;
2616 if (bleData && bleData->remoteEndpoint)
2618 if (!strcmp(bleData->remoteEndpoint->addr, address))
2620 OIC_LOG(DEBUG, CALEADAPTER_TAG, "found the message of disconnected device");
2621 if (NULL != queueHandle->destroy)
2623 queueHandle->destroy(message->msg, message->size);
2627 OICFree(message->msg);
2635 ca_mutex_unlock(mutex);
2638 static void CALERemoveReceiveQueueData(u_arraylist_t *dataInfoList, const char* address)
2640 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALERemoveSendQueueData");
2642 VERIFY_NON_NULL_VOID(dataInfoList, CALEADAPTER_TAG, "dataInfoList");
2643 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2645 CABLESenderInfo_t *senderInfo = NULL;
2646 uint32_t senderIndex = 0;
2648 if(CA_STATUS_OK == CALEGetSenderInfo(address, dataInfoList, &senderInfo,
2651 u_arraylist_remove(dataInfoList, senderIndex);
2652 OICFree(senderInfo->defragData);
2653 OICFree(senderInfo->remoteEndpoint);
2654 OICFree(senderInfo);
2656 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo is removed for disconnection");
2660 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo doesn't exist");