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 static CAResult_t CAInitLEServerQueues()
412 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
414 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
416 CAResult_t result = CAInitLEServerSenderQueue();
417 if (CA_STATUS_OK != result)
419 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
420 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
421 return CA_STATUS_FAILED;
424 g_bleServerSenderInfo = u_arraylist_create();
425 if (!g_bleServerSenderInfo)
427 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
428 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
429 return CA_MEMORY_ALLOC_FAILED;
432 result = CAInitLEReceiverQueue();
433 if (CA_STATUS_OK != result)
435 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
436 u_arraylist_free(&g_bleServerSenderInfo);
437 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
438 return CA_STATUS_FAILED;
441 g_dataBleReceiverHandlerState = true;
443 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
445 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
449 static CAResult_t CAInitLEClientQueues()
451 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
453 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
455 CAResult_t result = CAInitLEClientSenderQueue();
456 if (CA_STATUS_OK != result)
458 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
459 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
460 return CA_STATUS_FAILED;
463 g_bleClientSenderInfo = u_arraylist_create();
464 if (!g_bleClientSenderInfo)
466 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
467 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
468 return CA_MEMORY_ALLOC_FAILED;
471 result = CAInitLEReceiverQueue();
472 if (CA_STATUS_OK != result)
474 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
475 u_arraylist_free(&g_bleClientSenderInfo);
476 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
477 return CA_STATUS_FAILED;
480 g_dataBleReceiverHandlerState = true;
482 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
484 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
488 static CAResult_t CAInitLEReceiverQueue()
490 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEReceiverQueue");
491 // Check if the message queue is already initialized
492 if (g_bleReceiverQueue)
494 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
498 // Create recv message queue
499 g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
500 if (!g_bleReceiverQueue)
502 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
503 return CA_MEMORY_ALLOC_FAILED;
506 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue,
507 g_bleAdapterThreadPool,
508 CALEDataReceiverHandler,
511 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
512 OICFree(g_bleReceiverQueue);
513 g_bleReceiverQueue = NULL;
514 return CA_STATUS_FAILED;
517 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue))
519 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
520 OICFree(g_bleReceiverQueue);
521 g_bleReceiverQueue = NULL;
522 return CA_STATUS_FAILED;
525 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
529 static CAResult_t CAInitLEServerSenderQueue()
531 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEServerSenderQueue");
532 // Check if the message queue is already initialized
533 if (g_bleServerSendQueueHandle)
535 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
539 // Create send message queue
540 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
541 if (!g_bleServerSendQueueHandle)
543 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
544 return CA_MEMORY_ALLOC_FAILED;
547 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
548 g_bleAdapterThreadPool,
549 CALEServerSendDataThread,
552 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
553 OICFree(g_bleServerSendQueueHandle);
554 g_bleServerSendQueueHandle = NULL;
555 return CA_STATUS_FAILED;
558 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
562 static void CALEClearSenderInfoImpl(u_arraylist_t ** list)
564 const size_t length = u_arraylist_length(*list);
565 for (size_t i = 0; i < length; ++i)
567 CABLESenderInfo_t * const info =
568 (CABLESenderInfo_t *) u_arraylist_get(*list, i);
571 OICFree(info->defragData);
572 CAFreeEndpoint(info->remoteEndpoint);
576 u_arraylist_free(list);
579 static void CALEClearSenderInfo()
581 CALEClearSenderInfoImpl(&g_bleServerSenderInfo);
582 CALEClearSenderInfoImpl(&g_bleClientSenderInfo);
585 static CAResult_t CAInitLEClientSenderQueue()
587 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEClientSenderQueue");
589 if (g_bleClientSendQueueHandle)
591 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
595 // Create send message queue
596 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
597 if (!g_bleClientSendQueueHandle)
599 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
600 return CA_MEMORY_ALLOC_FAILED;
603 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
604 g_bleAdapterThreadPool,
605 CALEClientSendDataThread, CALEDataDestroyer))
607 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
608 OICFree(g_bleClientSendQueueHandle);
609 g_bleClientSendQueueHandle = NULL;
610 return CA_STATUS_FAILED;
613 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAInitLEClientSenderQueue");
617 static void CAStopLEQueues()
619 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStopLEQueues");
621 ca_mutex_lock(g_bleReceiveDataMutex);
622 if (NULL != g_bleReceiverQueue)
624 CAQueueingThreadStop(g_bleReceiverQueue);
626 ca_mutex_unlock(g_bleReceiveDataMutex);
628 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAStopLEQueues");
631 static void CATerminateLEQueues()
633 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
635 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
636 OICFree(g_bleClientSendQueueHandle);
637 g_bleClientSendQueueHandle = NULL;
639 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
640 OICFree(g_bleServerSendQueueHandle);
641 g_bleServerSendQueueHandle = NULL;
643 CAQueueingThreadDestroy(g_bleReceiverQueue);
644 OICFree(g_bleReceiverQueue);
645 g_bleReceiverQueue = NULL;
647 CALEClearSenderInfo();
649 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
652 static CAResult_t CALEGetSenderInfo(const char *leAddress,
653 u_arraylist_t *senderInfoList,
654 CABLESenderInfo_t **senderInfo,
655 uint32_t *senderIndex)
657 VERIFY_NON_NULL_RET(leAddress,
659 "NULL BLE address argument",
660 CA_STATUS_INVALID_PARAM);
661 VERIFY_NON_NULL_RET(senderIndex,
663 "NULL index argument",
664 CA_STATUS_INVALID_PARAM);
666 const uint32_t listLength = u_arraylist_length(senderInfoList);
667 const uint32_t addrLength = strlen(leAddress);
668 for (uint32_t index = 0; index < listLength; index++)
670 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(senderInfoList, index);
671 if(!info || !(info->remoteEndpoint))
676 if(!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
678 *senderIndex = index;
687 return CA_STATUS_FAILED;
690 static void CALEDataReceiverHandler(void *threadData)
692 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDataReceiverHandler");
694 ca_mutex_lock(g_bleReceiveDataMutex);
696 if (g_dataBleReceiverHandlerState)
698 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
700 CALEData_t *bleData = (CALEData_t *) threadData;
703 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
704 ca_mutex_unlock(g_bleReceiveDataMutex);
708 if (!(bleData->senderInfo))
710 OIC_LOG(ERROR, CALEADAPTER_TAG, "sender info is not available");
711 ca_mutex_unlock(g_bleReceiveDataMutex);
715 if (!(bleData->remoteEndpoint))
717 OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!");
718 ca_mutex_unlock(g_bleReceiveDataMutex);
722 CABLESenderInfo_t *senderInfo = NULL;
723 uint32_t senderIndex = 0;
725 if(CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
727 &senderInfo, &senderIndex))
729 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s]",
730 bleData->remoteEndpoint->addr);
735 CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
738 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
739 ca_mutex_unlock(g_bleReceiveDataMutex);
742 newSender->recvDataLen = 0;
743 newSender->totalDataLen = 0;
744 newSender->defragData = NULL;
745 newSender->remoteEndpoint = NULL;
747 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
749 newSender->totalDataLen = coap_get_total_message_length(bleData->data, bleData->dataLen);
751 if(!(newSender->totalDataLen))
753 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
755 ca_mutex_unlock(g_bleReceiveDataMutex);
759 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
760 newSender->totalDataLen);
761 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
764 newSender->defragData = OICCalloc(newSender->totalDataLen + 1,
765 sizeof(*newSender->defragData));
767 if (NULL == newSender->defragData)
769 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
771 ca_mutex_unlock(g_bleReceiveDataMutex);
775 const char *remoteAddress = bleData->remoteEndpoint->addr;
776 newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
777 CA_ADAPTER_GATT_BTLE,
780 if (NULL == newSender->remoteEndpoint)
782 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
783 OICFree(newSender->defragData);
785 ca_mutex_unlock(g_bleReceiveDataMutex);
789 if (newSender->recvDataLen + bleData->dataLen > newSender->totalDataLen)
791 OIC_LOG(ERROR, CALEADAPTER_TAG, "buffer is smaller than received data");
792 OICFree(newSender->defragData);
793 CAFreeEndpoint(newSender->remoteEndpoint);
795 ca_mutex_unlock(g_bleReceiveDataMutex);
798 memcpy(newSender->defragData, bleData->data, bleData->dataLen);
799 newSender->recvDataLen += bleData->dataLen;
801 u_arraylist_add(bleData->senderInfo,(void *)newSender);
803 //Getting newSender index position in bleSenderInfo array list
805 CALEGetSenderInfo(newSender->remoteEndpoint->addr, bleData->senderInfo,
808 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
809 OICFree(newSender->defragData);
810 CAFreeEndpoint(newSender->remoteEndpoint);
812 ca_mutex_unlock(g_bleReceiveDataMutex);
815 senderInfo = newSender;
819 if(senderInfo->recvDataLen + bleData->dataLen > senderInfo->totalDataLen)
821 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
822 "Data Length exceeding error!! Receiving [%d] total length [%d]",
823 senderInfo->recvDataLen + bleData->dataLen, senderInfo->totalDataLen);
824 u_arraylist_remove(bleData->senderInfo, senderIndex);
825 OICFree(senderInfo->defragData);
827 ca_mutex_unlock(g_bleReceiveDataMutex);
830 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]",
832 memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data,
834 senderInfo->recvDataLen += bleData->dataLen ;
835 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] received Datalen [%d]",
836 senderInfo->totalDataLen, senderInfo->recvDataLen);
839 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
841 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
842 if (NULL == g_networkPacketReceivedCallback)
844 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
846 u_arraylist_remove(bleData->senderInfo, senderIndex);
847 OICFree(senderInfo->defragData);
849 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
850 ca_mutex_unlock(g_bleReceiveDataMutex);
854 OIC_LOG(DEBUG, CALEADAPTER_TAG, "[CALEDataReceiverHandler] Sending data up !");
856 const CASecureEndpoint_t tmp =
858 .endpoint = *senderInfo->remoteEndpoint
861 g_networkPacketReceivedCallback(&tmp,
862 senderInfo->defragData,
863 senderInfo->recvDataLen);
864 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
865 u_arraylist_remove(bleData->senderInfo, senderIndex);
866 senderInfo->remoteEndpoint = NULL;
867 senderInfo->defragData = NULL;
871 ca_mutex_unlock(g_bleReceiveDataMutex);
872 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
875 static void CALEServerSendDataThread(void *threadData)
877 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEServerSendDataThread");
879 CALEData_t * const bleData = (CALEData_t *) threadData;
882 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
886 const uint32_t totalLength = bleData->dataLen;
890 "Server total Data length with header is [%u]",
893 uint8_t * const dataSegment = OICCalloc(totalLength, 1);
895 if (NULL == dataSegment)
897 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
902 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
904 length = totalLength;
905 memcpy(dataSegment, bleData->data, bleData->dataLen);
909 length = CA_SUPPORTED_BLE_MTU_SIZE;
910 memcpy(dataSegment, bleData->data, CA_SUPPORTED_BLE_MTU_SIZE);
913 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
915 CAResult_t result = CA_STATUS_FAILED;
917 // Send the first segment with the header.
918 if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
920 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
922 result = CAUpdateCharacteristicsToGattClient(
923 bleData->remoteEndpoint->addr, dataSegment, length);
925 if (CA_STATUS_OK != result)
929 "Update characteristics failed, result [%d]",
932 g_errorHandler(bleData->remoteEndpoint,
936 OICFree(dataSegment);
942 "Server Sent data length [%u]",
944 for (index = 1; index < iter; index++)
946 // Send the remaining header.
949 "Sending the chunk number [%u]",
953 CAUpdateCharacteristicsToGattClient(
954 bleData->remoteEndpoint->addr,
955 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE)),
956 CA_SUPPORTED_BLE_MTU_SIZE);
958 if (CA_STATUS_OK != result)
960 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
961 "Update characteristics failed, result [%d]", result);
962 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
963 OICFree(dataSegment);
966 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
967 CA_SUPPORTED_BLE_MTU_SIZE);
970 const uint32_t remainingLen =
971 totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
973 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
975 // send the last segment of the data (Ex: 22 bytes of 622
976 // bytes of data when MTU is 200)
977 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
979 result = CAUpdateCharacteristicsToGattClient(
980 bleData->remoteEndpoint->addr,
981 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
984 if (CA_STATUS_OK != result)
988 "Update characteristics failed, result [%d]",
990 g_errorHandler(bleData->remoteEndpoint,
994 OICFree(dataSegment);
997 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1002 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
1003 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
1004 if (CA_STATUS_OK != result)
1006 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1008 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1009 OICFree(dataSegment);
1012 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1013 for (index = 1; index < iter; index++)
1015 // Send the remaining header.
1016 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1018 result = CAUpdateCharacteristicsToAllGattClients(
1019 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE)),
1020 CA_SUPPORTED_BLE_MTU_SIZE);
1022 if (CA_STATUS_OK != result)
1024 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1026 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1027 OICFree(dataSegment);
1030 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%u]",
1031 CA_SUPPORTED_BLE_MTU_SIZE);
1034 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1035 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1037 // send the last segment of the data
1038 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1040 result = CAUpdateCharacteristicsToAllGattClients(
1041 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1044 if (CA_STATUS_OK != result)
1046 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1048 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1049 OICFree(dataSegment);
1052 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1055 OICFree(dataSegment);
1057 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CALEServerSendDataThread");
1060 static void CALEClientSendDataThread(void *threadData)
1062 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEClientSendDataThread");
1064 CALEData_t *bleData = (CALEData_t *) threadData;
1067 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1071 const uint32_t totalLength = bleData->dataLen;
1073 uint8_t *dataSegment = OICCalloc(totalLength, 1);
1074 if (NULL == dataSegment)
1076 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1080 uint32_t length = 0;
1081 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1083 length = totalLength;
1090 length = CA_SUPPORTED_BLE_MTU_SIZE;
1093 CA_SUPPORTED_BLE_MTU_SIZE);
1096 CAResult_t result = CA_STATUS_FAILED;
1097 const uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1099 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1101 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1102 // Send the first segment with the header.
1104 CAUpdateCharacteristicsToGattServer(
1105 bleData->remoteEndpoint->addr,
1111 if (CA_STATUS_OK != result)
1115 "Update characteristics failed, result [%d]",
1117 g_errorHandler(bleData->remoteEndpoint,
1121 OICFree(dataSegment);
1127 "Client Sent Data length is [%u]",
1130 for (index = 1; index < iter; index++)
1132 // Send the remaining header.
1133 result = CAUpdateCharacteristicsToGattServer(
1134 bleData->remoteEndpoint->addr,
1135 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1136 CA_SUPPORTED_BLE_MTU_SIZE,
1139 if (CA_STATUS_OK != result)
1143 "Update characteristics failed, result [%d]",
1145 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1146 OICFree(dataSegment);
1149 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1150 CA_SUPPORTED_BLE_MTU_SIZE);
1153 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1154 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1156 // send the last segment of the data (Ex: 22 bytes of 622
1157 // bytes of data when MTU is 200)
1158 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1160 result = CAUpdateCharacteristicsToGattServer(
1161 bleData->remoteEndpoint->addr,
1162 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1166 if (CA_STATUS_OK != result)
1168 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1170 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1171 OICFree(dataSegment);
1174 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1179 //Sending Mulitcast Data
1180 // Send the first segment with the header.
1181 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1182 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1183 if (CA_STATUS_OK != result)
1185 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1186 "Update characteristics (all) failed, result [%d]", result);
1187 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1188 OICFree(dataSegment);
1191 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1192 // Send the remaining header.
1193 for (index = 1; index < iter; index++)
1195 result = CAUpdateCharacteristicsToAllGattServers(
1196 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1197 CA_SUPPORTED_BLE_MTU_SIZE);
1199 if (CA_STATUS_OK != result)
1201 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1203 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1204 OICFree(dataSegment);
1207 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1208 CA_SUPPORTED_BLE_MTU_SIZE);
1211 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1212 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1214 // send the last segment of the data (Ex: 22 bytes of 622
1215 // bytes of data when MTU is 200)
1216 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1218 CAUpdateCharacteristicsToAllGattServers(
1219 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1222 if (CA_STATUS_OK != result)
1224 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1225 "Update characteristics (all) failed, result [%d]", result);
1226 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1227 OICFree(dataSegment);
1230 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1235 OICFree(dataSegment);
1237 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1240 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
1241 const uint8_t *data,
1242 uint32_t dataLength,
1243 u_arraylist_t *senderInfo)
1245 CALEData_t * const bleData = OICMalloc(sizeof(CALEData_t));
1249 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1253 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1254 bleData->data = OICCalloc(dataLength + 1, 1);
1256 if (NULL == bleData->data)
1258 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1259 CAFreeLEData(bleData);
1263 memcpy(bleData->data, data, dataLength);
1264 bleData->dataLen = dataLength;
1267 bleData->senderInfo = senderInfo;
1273 static void CAFreeLEData(CALEData_t *bleData)
1275 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1277 CAFreeEndpoint(bleData->remoteEndpoint);
1278 OICFree(bleData->data);
1282 static void CALEDataDestroyer(void *data, uint32_t size)
1284 if ((size_t)size < sizeof(CALEData_t *))
1286 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1287 "Destroy data too small %p %d", data, size);
1289 CALEData_t *ledata = (CALEData_t *) data;
1291 CAFreeLEData(ledata);
1295 static CAResult_t CAInitLEAdapterMutex()
1297 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEAdapterMutex");
1299 if (NULL == g_bleIsServerMutex)
1301 g_bleIsServerMutex = ca_mutex_new();
1302 if (NULL == g_bleIsServerMutex)
1304 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1305 return CA_STATUS_FAILED;
1309 if (NULL == g_bleNetworkCbMutex)
1311 g_bleNetworkCbMutex = ca_mutex_new();
1312 if (NULL == g_bleNetworkCbMutex)
1314 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1315 CATerminateLEAdapterMutex();
1316 return CA_STATUS_FAILED;
1320 if (NULL == g_bleLocalAddressMutex)
1322 g_bleLocalAddressMutex = ca_mutex_new();
1323 if (NULL == g_bleLocalAddressMutex)
1325 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1326 CATerminateLEAdapterMutex();
1327 return CA_STATUS_FAILED;
1331 if (NULL == g_bleAdapterThreadPoolMutex)
1333 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1334 if (NULL == g_bleAdapterThreadPoolMutex)
1336 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1337 CATerminateLEAdapterMutex();
1338 return CA_STATUS_FAILED;
1342 if (NULL == g_bleClientSendDataMutex)
1344 g_bleClientSendDataMutex = ca_mutex_new();
1345 if (NULL == g_bleClientSendDataMutex)
1347 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1348 CATerminateLEAdapterMutex();
1349 return CA_STATUS_FAILED;
1353 if (NULL == g_bleServerSendDataMutex)
1355 g_bleServerSendDataMutex = ca_mutex_new();
1356 if (NULL == g_bleServerSendDataMutex)
1358 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1359 CATerminateLEAdapterMutex();
1360 return CA_STATUS_FAILED;
1364 if (NULL == g_bleAdapterReqRespCbMutex)
1366 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1367 if (NULL == g_bleAdapterReqRespCbMutex)
1369 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1370 CATerminateLEAdapterMutex();
1371 return CA_STATUS_FAILED;
1375 if (NULL == g_bleReceiveDataMutex)
1377 g_bleReceiveDataMutex = ca_mutex_new();
1378 if (NULL == g_bleReceiveDataMutex)
1380 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1381 return CA_STATUS_FAILED;
1385 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1386 return CA_STATUS_OK;
1389 static void CATerminateLEAdapterMutex()
1391 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CATerminateLEAdapterMutex");
1393 ca_mutex_free(g_bleIsServerMutex);
1394 g_bleIsServerMutex = NULL;
1396 ca_mutex_free(g_bleNetworkCbMutex);
1397 g_bleNetworkCbMutex = NULL;
1399 ca_mutex_free(g_bleLocalAddressMutex);
1400 g_bleLocalAddressMutex = NULL;
1402 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1403 g_bleAdapterThreadPoolMutex = NULL;
1405 ca_mutex_free(g_bleClientSendDataMutex);
1406 g_bleClientSendDataMutex = NULL;
1408 ca_mutex_free(g_bleServerSendDataMutex);
1409 g_bleServerSendDataMutex = NULL;
1411 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1412 g_bleAdapterReqRespCbMutex = NULL;
1414 ca_mutex_free(g_bleReceiveDataMutex);
1415 g_bleReceiveDataMutex = NULL;
1417 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1421 * Starting LE connectivity adapters.
1423 * As its peer to peer it does not require to start any servers.
1425 * @return ::CA_STATUS_OK or Appropriate error code.
1427 static CAResult_t CAStartLE();
1430 * Start listening server for receiving multicast search requests.
1432 * Transport Specific Behavior:
1433 * LE Starts GATT Server with prefixed UUID and Characteristics
1434 * per OIC Specification.
1435 * @return ::CA_STATUS_OK or Appropriate error code.
1437 static CAResult_t CAStartLEListeningServer();
1440 * Stops listening server from receiving multicast search requests.
1442 * Transport Specific Behavior:
1443 * LE Starts GATT Server with prefixed UUID and Characteristics
1444 * per OIC Specification.
1445 * @return ::CA_STATUS_OK or Appropriate error code.
1447 static CAResult_t CAStopLEListeningServer();
1450 * Sarting discovery of servers for receiving multicast
1453 * Transport Specific Behavior:
1454 * LE Starts GATT Server with prefixed UUID and Characteristics
1455 * per OIC Specification.
1457 * @return ::CA_STATUS_OK or Appropriate error code
1459 static CAResult_t CAStartLEDiscoveryServer();
1462 * Send data to the endpoint using the adapter connectivity.
1464 * @param[in] endpoint Remote Endpoint information (like MAC address,
1465 * reference URI and connectivity type) to which
1466 * the unicast data has to be sent.
1467 * @param[in] data Data which required to be sent.
1468 * @param[in] dataLen Size of data to be sent.
1470 * @note dataLen must be > 0.
1472 * @return The number of bytes sent on the network, or -1 on error.
1474 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1479 * Send multicast data to the endpoint using the LE connectivity.
1481 * @param[in] endpoint Remote Endpoint information to which the
1482 * multicast data has to be sent.
1483 * @param[in] data Data which required to be sent.
1484 * @param[in] dataLen Size of data to be sent.
1486 * @note dataLen must be > 0.
1488 * @return The number of bytes sent on the network, or -1 on error.
1490 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1495 * Get LE Connectivity network information.
1497 * @param[out] info Local connectivity information structures.
1498 * @param[out] size Number of local connectivity structures.
1500 * @return ::CA_STATUS_OK or Appropriate error code.
1502 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info,
1506 * Read Synchronous API callback.
1508 * @return ::CA_STATUS_OK or Appropriate error code.
1510 static CAResult_t CAReadLEData();
1513 * Stopping the adapters and close socket connections.
1515 * LE Stops all GATT servers and GATT Clients.
1517 * @return ::CA_STATUS_OK or Appropriate error code.
1519 static CAResult_t CAStopLE();
1522 * Terminate the LE connectivity adapter.
1524 * Configuration information will be deleted from further use.
1526 static void CATerminateLE();
1529 * This function will receive the data from the GattServer and add the
1530 * data to the Server receiver queue.
1532 * @param[in] remoteAddress Remote address of the device from where
1534 * @param[in] data Actual data received from the remote
1536 * @param[in] dataLength Length of the data received from the
1538 * @param[in] sentLength Length of the data sent from the remote
1541 * @return ::CA_STATUS_OK or Appropriate error code.
1542 * @retval ::CA_STATUS_OK Successful.
1543 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1544 * @retval ::CA_STATUS_FAILED Operation failed.
1547 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
1548 const uint8_t *data,
1549 uint32_t dataLength,
1550 uint32_t *sentLength);
1553 * This function will receive the data from the GattClient and add the
1554 * data into the Client receiver queue.
1556 * @param[in] remoteAddress Remote address of the device from where
1558 * @param[in] data Actual data recevied from the remote
1560 * @param[in] dataLength Length of the data received from the
1562 * @param[in] sentLength Length of the data sent from the remote
1565 * @return ::CA_STATUS_OK or Appropriate error code.
1566 * @retval ::CA_STATUS_OK Successful.
1567 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1568 * @retval ::CA_STATUS_FAILED Operation failed.
1570 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
1571 const uint8_t *data,
1572 uint32_t dataLength,
1573 uint32_t *sentLength);
1576 * Set the NetworkPacket received callback to CA layer from adapter
1579 * @param[in] callback Callback handle sent from the upper layer.
1581 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
1584 * Push the data from CA layer to the Sender processor queue.
1586 * @param[in] remoteEndpoint Remote endpoint information of the
1588 * @param[in] data Data to be transmitted from LE.
1589 * @param[in] dataLen Length of the Data being transmitted.
1591 * @return ::CA_STATUS_OK or Appropriate error code.
1592 * @retval ::CA_STATUS_OK Successful.
1593 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1594 * @retval ::CA_STATUS_FAILED Operation failed.
1596 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1597 const uint8_t *data,
1601 * Push the data from CA layer to the Sender processor queue.
1603 * @param[in] remoteEndpoint Remote endpoint information of the
1605 * @param[in] data Data to be transmitted from LE.
1606 * @param[in] dataLen Length of the Data being transmitted.
1608 * @return ::CA_STATUS_OK or Appropriate error code.
1609 * @retval ::CA_STATUS_OK Successful.
1610 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1611 * @retval ::CA_STATUS_FAILED Operation failed.
1613 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1614 const uint8_t *data,
1617 static CAResult_t CALEAdapterGattServerStart()
1619 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1621 CAResult_t result = CAStartLEGattServer();
1623 #ifndef SINGLE_THREAD
1625 Don't start the server side sending queue thread until the
1626 server itself has actually started.
1628 if (CA_STATUS_OK == result)
1630 ca_mutex_lock(g_bleServerSendDataMutex);
1631 result = CAQueueingThreadStart(g_bleServerSendQueueHandle);
1632 ca_mutex_unlock(g_bleServerSendDataMutex);
1634 if (CA_STATUS_OK != result)
1638 "Unable to start server queuing thread (%d)",
1647 static CAResult_t CALEAdapterGattServerStop()
1649 #ifndef SINGLE_THREAD
1650 ca_mutex_lock(g_bleServerSendDataMutex);
1651 CAResult_t result = CAQueueingThreadStop(g_bleServerSendQueueHandle);
1652 ca_mutex_unlock(g_bleServerSendDataMutex);
1653 if (CA_STATUS_OK == result)
1655 result = CAStopLEGattServer();
1660 return CAStopLEGattServer();
1664 static CAResult_t CALEAdapterGattClientStart()
1666 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattClient");
1668 CAResult_t result = CAStartLEGattClient();
1670 #ifndef SINGLE_THREAD
1672 Don't start the client side sending queue thread until the
1673 client itself has actually started.
1675 if (CA_STATUS_OK == result)
1677 ca_mutex_lock(g_bleClientSendDataMutex);
1678 result = CAQueueingThreadStart(g_bleClientSendQueueHandle);
1679 ca_mutex_unlock(g_bleClientSendDataMutex);
1681 if (CA_STATUS_OK != result)
1685 "Unable to start client queuing thread");
1693 static CAResult_t CALEAdapterGattClientStop()
1695 #ifndef SINGLE_THREAD
1696 ca_mutex_lock(g_bleClientSendDataMutex);
1697 CAResult_t result = CAQueueingThreadStop(g_bleClientSendQueueHandle);
1698 ca_mutex_unlock(g_bleClientSendDataMutex);
1699 if (CA_STATUS_OK == result)
1701 CAStopLEGattClient();
1706 CAStopLEGattClient();
1708 return CA_STATUS_OK;
1712 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1713 CANetworkPacketReceivedCallback reqRespCallback,
1714 CANetworkChangeCallback netCallback,
1715 CAErrorHandleCallback errorCallback,
1716 ca_thread_pool_t handle)
1718 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1721 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1722 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1723 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1725 CAResult_t result = CA_STATUS_OK;
1726 result = CAInitLEAdapterMutex();
1727 if (CA_STATUS_OK != result)
1729 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1730 return CA_STATUS_FAILED;
1733 result = CAInitializeLENetworkMonitor();
1734 if (CA_STATUS_OK != result)
1736 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1737 return CA_STATUS_FAILED;
1739 CAInitializeLEAdapter(handle);
1741 CASetLEClientThreadPoolHandle(handle);
1743 result = CAInitializeLEGattClient();
1744 if (CA_STATUS_OK != result)
1746 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattClient() failed");
1747 return CA_STATUS_FAILED;
1750 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1751 CASetLEServerThreadPoolHandle(handle);
1752 result = CAInitializeLEGattServer();
1753 if (CA_STATUS_OK != result)
1755 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattServer() failed");
1756 return CA_STATUS_FAILED;
1759 CASetLEAdapterThreadPoolHandle(handle);
1760 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1761 CASetLEReqRespAdapterCallback(reqRespCallback);
1763 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1764 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1765 CALERegisterNetworkNotifications(netCallback);
1767 g_errorHandler = errorCallback;
1769 static const CAConnectivityHandler_t connHandler =
1771 .startAdapter = CAStartLE,
1772 .stopAdapter = CAStopLE,
1773 .startListenServer = CAStartLEListeningServer,
1774 .stopListenServer = CAStopLEListeningServer,
1775 .startDiscoveryServer = CAStartLEDiscoveryServer,
1776 .sendData = CASendLEUnicastData,
1777 .sendDataToAll = CASendLEMulticastData,
1778 .GetnetInfo = CAGetLEInterfaceInformation,
1779 .readData = CAReadLEData,
1780 .terminate = CATerminateLE,
1781 .cType = CA_ADAPTER_GATT_BTLE
1784 registerCallback(connHandler);
1786 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1788 return CA_STATUS_OK;
1791 static CAResult_t CAStartLE()
1793 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE");
1795 return CAStartLEAdapter();
1798 static CAResult_t CAStopLE()
1800 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1801 #ifndef SINGLE_THREAD
1805 ca_mutex_lock(g_bleIsServerMutex);
1806 switch (g_adapterType)
1808 case ADAPTER_SERVER:
1809 CALEAdapterGattServerStop();
1811 case ADAPTER_CLIENT:
1812 CALEAdapterGattClientStop();
1814 case ADAPTER_BOTH_CLIENT_SERVER:
1815 CALEAdapterGattServerStop();
1816 CALEAdapterGattClientStop();
1821 ca_mutex_unlock(g_bleIsServerMutex);
1823 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1825 return CAStopLEAdapter();
1828 static void CATerminateLE()
1830 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1832 CASetLEReqRespServerCallback(NULL);
1833 CASetLEReqRespClientCallback(NULL);
1834 CALERegisterNetworkNotifications(NULL);
1835 CASetLEReqRespAdapterCallback(NULL);
1836 CATerminateLENetworkMonitor();
1838 ca_mutex_lock(g_bleIsServerMutex);
1839 switch (g_adapterType)
1841 case ADAPTER_SERVER:
1842 CATerminateLEGattServer();
1844 case ADAPTER_CLIENT:
1845 CATerminateLEGattClient();
1847 case ADAPTER_BOTH_CLIENT_SERVER:
1848 CATerminateLEGattServer();
1849 CATerminateLEGattClient();
1854 g_adapterType = ADAPTER_EMPTY;
1855 ca_mutex_unlock(g_bleIsServerMutex);
1857 #ifndef SINGLE_THREAD
1858 CATerminateLEQueues();
1860 CATerminateLEAdapterMutex();
1862 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1865 static CAResult_t CAStartLEListeningServer()
1867 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEListeningServer");
1868 #ifndef ROUTING_GATEWAY
1869 CAResult_t result = CA_STATUS_OK;
1870 #ifndef SINGLE_THREAD
1871 result = CAInitLEServerQueues();
1872 if (CA_STATUS_OK != result)
1874 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1879 ca_mutex_lock(g_bleIsServerMutex);
1880 switch (g_adapterType)
1882 case ADAPTER_CLIENT:
1883 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1885 case ADAPTER_BOTH_CLIENT_SERVER:
1888 g_adapterType = ADAPTER_SERVER;
1890 ca_mutex_unlock(g_bleIsServerMutex);
1892 result = CAGetLEAdapterState();
1893 if (CA_STATUS_OK != result)
1895 if (CA_ADAPTER_NOT_ENABLED == result)
1899 "Listen Server will be started once BT Adapter is enabled");
1904 result = CALEAdapterGattServerStart();
1907 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1910 // Routing Gateway only supports BLE client mode.
1911 OIC_LOG(ERROR, CALEADAPTER_TAG, "LE server not supported in Routing Gateway");
1912 return CA_NOT_SUPPORTED;
1916 static CAResult_t CAStopLEListeningServer()
1918 OIC_LOG(ERROR, CALEADAPTER_TAG, "Listen server stop not supported.");
1919 return CA_NOT_SUPPORTED;
1922 static CAResult_t CAStartLEDiscoveryServer()
1924 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEDiscoveryServer");
1925 CAResult_t result = CA_STATUS_OK;
1926 #ifndef SINGLE_THREAD
1927 result = CAInitLEClientQueues();
1928 if (CA_STATUS_OK != result)
1930 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1935 ca_mutex_lock(g_bleIsServerMutex);
1936 switch (g_adapterType)
1938 case ADAPTER_SERVER:
1939 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1941 case ADAPTER_BOTH_CLIENT_SERVER:
1944 g_adapterType = ADAPTER_CLIENT;
1946 ca_mutex_unlock(g_bleIsServerMutex);
1948 result = CAGetLEAdapterState();
1949 if (CA_STATUS_OK != result)
1951 if (CA_ADAPTER_NOT_ENABLED == result)
1955 "Discovery Server will be started once BT Adapter is enabled");
1960 result = CALEAdapterGattClientStart();
1963 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1967 static CAResult_t CAReadLEData()
1969 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1970 #ifdef SINGLE_THREAD
1973 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1974 return CA_STATUS_OK;
1977 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1981 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEUnicastData");
1984 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1985 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1987 CAResult_t result = CA_STATUS_FAILED;
1989 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
1990 if (ADAPTER_EMPTY == g_adapterType)
1992 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
1995 ca_mutex_lock(g_bleIsServerMutex);
1996 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
1998 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1999 if (CA_STATUS_OK != result)
2001 ca_mutex_unlock(g_bleIsServerMutex);
2002 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed");
2005 g_errorHandler(endpoint, data, dataLen, result);
2012 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2014 result = CALEAdapterClientSendData(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 client failed" );
2022 g_errorHandler(endpoint, data, dataLen, result);
2027 ca_mutex_unlock(g_bleIsServerMutex);
2029 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2033 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
2037 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEMulticastData");
2040 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
2044 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
2048 CAResult_t result = CA_STATUS_FAILED;
2050 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
2051 if (ADAPTER_EMPTY == g_adapterType)
2053 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2056 ca_mutex_lock(g_bleIsServerMutex);
2057 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2059 result = CALEAdapterServerSendData(NULL, data, dataLen);
2060 if (CA_STATUS_OK != result)
2062 ca_mutex_unlock(g_bleIsServerMutex);
2064 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data for server failed" );
2068 g_errorHandler(endpoint, data, dataLen, result);
2074 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2076 result = CALEAdapterClientSendData(NULL, data, dataLen);
2077 if (CA_STATUS_OK != result)
2079 ca_mutex_unlock(g_bleIsServerMutex);
2081 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data for client failed" );
2085 g_errorHandler(endpoint, data, dataLen, result);
2090 ca_mutex_unlock(g_bleIsServerMutex);
2092 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CASendLEMulticastData");
2096 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
2098 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2100 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
2102 char *local_address = NULL;
2104 CAResult_t res = CAGetLEAddress(&local_address);
2105 if (CA_STATUS_OK != res)
2107 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
2111 if (NULL == local_address)
2113 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
2114 return CA_STATUS_FAILED;
2118 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
2119 if (NULL == (*info))
2121 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
2122 OICFree(local_address);
2123 return CA_STATUS_FAILED;
2126 size_t local_address_len = strlen(local_address);
2128 if(local_address_len >= sizeof(g_localBLEAddress) ||
2129 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
2131 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
2133 OICFree(local_address);
2134 return CA_STATUS_FAILED;
2137 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
2138 ca_mutex_lock(g_bleLocalAddressMutex);
2139 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
2140 ca_mutex_unlock(g_bleLocalAddressMutex);
2142 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
2144 OICFree(local_address);
2146 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2147 return CA_STATUS_OK;
2150 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
2152 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2154 ca_mutex_lock(g_bleNetworkCbMutex);
2155 g_networkCallback = netCallback;
2156 ca_mutex_unlock(g_bleNetworkCbMutex);
2157 CAResult_t res = CA_STATUS_OK;
2160 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
2161 if (CA_STATUS_OK != res)
2163 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2166 res = CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCb);
2167 if (CA_STATUS_OK != res)
2169 OIC_LOG(ERROR, CALEADAPTER_TAG, "CALEConnectionStateChangedCb failed!");
2174 res = CAUnSetLEAdapterStateChangedCb();
2175 if (CA_STATUS_OK != res)
2177 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2181 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2185 static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
2188 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEConnectionStateChangedCb");
2190 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2194 ca_mutex_lock(g_bleIsServerMutex);
2195 switch (g_adapterType)
2197 case ADAPTER_SERVER:
2198 CALEGattServerConnectionStateChanged(isConnected, address);
2200 case ADAPTER_CLIENT:
2201 CALEGattConnectionStateChanged(isConnected, address);
2203 case ADAPTER_BOTH_CLIENT_SERVER:
2204 CALEGattConnectionStateChanged(isConnected, address);
2205 CALEGattServerConnectionStateChanged(isConnected, address);
2210 ca_mutex_unlock(g_bleIsServerMutex);
2215 #ifndef SINGLE_THREAD
2216 if(g_bleClientSenderInfo)
2218 CABLESenderInfo_t *senderInfo = NULL;
2219 uint32_t senderIndex = 0;
2221 if(CA_STATUS_OK == CALEGetSenderInfo(address, g_bleClientSenderInfo, &senderInfo,
2224 u_arraylist_remove(g_bleClientSenderInfo, senderIndex);
2225 OICFree(senderInfo->defragData);
2226 OICFree(senderInfo->remoteEndpoint);
2227 OICFree(senderInfo);
2229 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo is removed for disconnection");
2233 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo doesn't exist");
2237 // remove data of send queue.
2238 if (g_bleClientSendQueueHandle)
2240 CALERemoveSendQueueData(g_bleClientSendQueueHandle,
2241 g_bleClientSendDataMutex,
2245 if (g_bleServerSendQueueHandle)
2247 CALERemoveSendQueueData(g_bleServerSendQueueHandle,
2248 g_bleServerSendDataMutex,
2254 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2257 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
2259 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDeviceStateChangedCb");
2261 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
2262 CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE };
2264 ca_mutex_lock(g_bleLocalAddressMutex);
2265 OICStrcpy(localEndpoint.addr,
2266 sizeof(localEndpoint.addr),
2268 ca_mutex_unlock(g_bleLocalAddressMutex);
2270 if (CA_ADAPTER_ENABLED == adapter_state)
2272 ca_mutex_lock(g_bleIsServerMutex);
2273 switch (g_adapterType)
2275 case ADAPTER_SERVER:
2276 CALEAdapterGattServerStart();
2278 case ADAPTER_CLIENT:
2279 CALEAdapterGattClientStart();
2281 case ADAPTER_BOTH_CLIENT_SERVER:
2282 CALEAdapterGattServerStart();
2283 CALEAdapterGattClientStart();
2288 ca_mutex_unlock(g_bleIsServerMutex);
2292 ca_mutex_lock(g_bleIsServerMutex);
2293 switch (g_adapterType)
2295 case ADAPTER_SERVER:
2296 CALEAdapterGattServerStop();
2298 case ADAPTER_CLIENT:
2299 CALEAdapterGattClientStop();
2301 case ADAPTER_BOTH_CLIENT_SERVER:
2302 CALEAdapterGattServerStop();
2303 CALEAdapterGattClientStop();
2308 ca_mutex_unlock(g_bleIsServerMutex);
2311 ca_mutex_lock(g_bleNetworkCbMutex);
2312 if (NULL != g_networkCallback)
2314 g_networkCallback(&localEndpoint, adapter_state);
2318 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
2320 ca_mutex_unlock(g_bleNetworkCbMutex);
2322 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2325 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
2326 const uint8_t *data,
2329 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2331 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2332 #ifndef SINGLE_THREAD
2333 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2334 "g_bleClientSendQueueHandle is NULL",
2336 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
2337 "g_bleClientSendDataMutex is NULL",
2340 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2341 "g_bleClientSendQueueHandle",
2344 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
2346 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2349 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2350 return CA_MEMORY_ALLOC_FAILED;
2352 // Add message to send queue
2353 ca_mutex_lock(g_bleClientSendDataMutex);
2354 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
2355 ca_mutex_unlock(g_bleClientSendDataMutex);
2357 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2358 return CA_STATUS_OK;
2361 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2362 const uint8_t *data,
2365 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2367 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2369 #ifdef SINGLE_THREAD
2370 if (!CAIsLEConnected())
2372 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2373 return CA_STATUS_FAILED;
2376 CAResult_t result = CA_STATUS_OK;
2377 const uint32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2378 for (uint32_t iter = 0; iter < dataLimit; iter++)
2381 CAUpdateCharacteristicsToAllGattClients(
2382 data + (iter * CA_SUPPORTED_BLE_MTU_SIZE),
2383 CA_SUPPORTED_BLE_MTU_SIZE);
2385 if (CA_STATUS_OK != result)
2387 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2388 return CA_STATUS_FAILED;
2394 const uint32_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2398 CAUpdateCharacteristicsToAllGattClients(
2399 data + (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE),
2401 if (CA_STATUS_OK != result)
2403 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2404 return CA_STATUS_FAILED;
2409 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2410 "BleClientReceiverQueue is NULL",
2412 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2413 "BleClientSendDataMutex is NULL",
2416 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2419 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2421 CALEData_t * const bleData =
2422 CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2426 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2427 return CA_MEMORY_ALLOC_FAILED;
2430 // Add message to send queue
2431 ca_mutex_lock(g_bleServerSendDataMutex);
2432 CAQueueingThreadAddData(g_bleServerSendQueueHandle,
2434 sizeof(CALEData_t));
2435 ca_mutex_unlock(g_bleServerSendDataMutex);
2437 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2438 return CA_STATUS_OK;
2441 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2442 const uint8_t *data,
2443 uint32_t dataLength,
2444 uint32_t *sentLength)
2446 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2449 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2450 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2452 #ifdef SINGLE_THREAD
2453 if(g_networkPacketReceivedCallback)
2455 // will be filled by upper layer
2456 const CASecureEndpoint_t endpoint =
2457 { .endpoint = { .adapter = CA_ADAPTER_GATT_BTLE } };
2460 g_networkPacketReceivedCallback(&endpoint, data, dataLength);
2463 VERIFY_NON_NULL_RET(g_bleReceiverQueue,
2465 "g_bleReceiverQueue",
2468 //Add message to data queue
2469 CAEndpoint_t * const remoteEndpoint =
2470 CACreateEndpointObject(CA_DEFAULT_FLAGS,
2471 CA_ADAPTER_GATT_BTLE,
2475 if (NULL == remoteEndpoint)
2477 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2478 return CA_STATUS_FAILED;
2481 // Create bleData to add to queue
2484 "Data received from LE layer [%d]",
2487 CALEData_t * const bleData =
2488 CACreateLEData(remoteEndpoint, data, dataLength, g_bleServerSenderInfo);
2492 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2493 CAFreeEndpoint(remoteEndpoint);
2494 return CA_MEMORY_ALLOC_FAILED;
2497 CAFreeEndpoint(remoteEndpoint);
2498 // Add message to receiver queue
2499 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2501 *sentLength = dataLength;
2503 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2504 return CA_STATUS_OK;
2507 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2508 const uint8_t *data,
2509 uint32_t dataLength,
2510 uint32_t *sentLength)
2512 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2515 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2516 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2517 #ifndef SINGLE_THREAD
2518 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG,
2519 "g_bleReceiverQueue",
2522 //Add message to data queue
2523 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2524 CA_ADAPTER_GATT_BTLE,
2526 if (NULL == remoteEndpoint)
2528 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2529 return CA_STATUS_FAILED;
2532 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%u]", dataLength);
2534 // Create bleData to add to queue
2535 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data,
2536 dataLength, g_bleClientSenderInfo);
2539 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2540 CAFreeEndpoint(remoteEndpoint);
2541 return CA_MEMORY_ALLOC_FAILED;
2544 CAFreeEndpoint(remoteEndpoint);
2545 // Add message to receiver queue
2546 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2548 *sentLength = dataLength;
2550 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2551 return CA_STATUS_OK;
2554 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2556 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2558 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2559 g_bleAdapterThreadPool = handle;
2560 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2562 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2565 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2567 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2569 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2571 g_networkPacketReceivedCallback = callback;
2573 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2575 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2578 static void CALEErrorHandler(const char *remoteAddress,
2579 const uint8_t *data,
2583 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2585 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2587 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2588 CA_ADAPTER_GATT_BTLE,
2592 // if required, will be used to build remote endpoint
2593 g_errorHandler(rep, data, dataLen, result);
2595 CAFreeEndpoint(rep);
2597 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");
2600 #ifndef SINGLE_THREAD
2601 static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle, ca_mutex mutex,
2602 const char* address)
2604 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALERemoveSendQueueData");
2606 VERIFY_NON_NULL_VOID(queueHandle, CALEADAPTER_TAG, "queueHandle");
2607 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2609 ca_mutex_lock(mutex);
2610 while (u_queue_get_size(queueHandle->dataQueue) > 0)
2612 OIC_LOG(DEBUG, CALEADAPTER_TAG, "get data from queue");
2613 u_queue_message_t *message = u_queue_get_element(queueHandle->dataQueue);
2614 if (NULL != message)
2616 CALEData_t *bleData = (CALEData_t *) message->msg;
2617 if (bleData && bleData->remoteEndpoint)
2619 if (!strcmp(bleData->remoteEndpoint->addr, address))
2621 OIC_LOG(DEBUG, CALEADAPTER_TAG, "found the message of disconnected device");
2622 if (NULL != queueHandle->destroy)
2624 queueHandle->destroy(message->msg, message->size);
2628 OICFree(message->msg);
2636 ca_mutex_unlock(mutex);