1 /* ****************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
20 #include "caleadapter.h"
25 #include "caleinterface.h"
28 #include "caadapterutils.h"
30 #include "caqueueingthread.h"
32 #include "oic_malloc.h"
33 #include "oic_string.h"
34 #include "caremotehandler.h"
38 * Logging tag for module name.
40 #define CALEADAPTER_TAG "OIC_CA_LE_ADAP"
43 * The MTU supported for BLE adapter
45 #define CA_SUPPORTED_BLE_MTU_SIZE 20
48 * Stores information of all the senders.
50 * This structure will be used to track and defragment all incoming
56 uint32_t totalDataLen;
58 CAEndpoint_t *remoteEndpoint;
64 ADAPTER_BOTH_CLIENT_SERVER,
70 * Callback to provide the status of the network change to CA layer.
72 static CANetworkChangeCallback g_networkCallback = NULL;
75 * bleAddress of the local adapter. Value will be initialized to zero,
76 * and will be updated later.
78 static char g_localBLEAddress[18] = { 0 };
81 * Variable to differentiate btw GattServer and GattClient.
83 static CABLEAdapter_t g_adapterType = ADAPTER_EMPTY;
86 * Mutex to synchronize the task to be executed on the GattServer
89 static ca_mutex g_bleIsServerMutex = NULL;
92 * Mutex to synchronize the callback to be called for the network
95 static ca_mutex g_bleNetworkCbMutex = NULL;
98 * Mutex to synchronize the updates of the local LE address of the
101 static ca_mutex g_bleLocalAddressMutex = NULL;
104 * Reference to thread pool.
106 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
109 * Mutex to synchronize the task to be pushed to thread pool.
111 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
114 * Mutex to synchronize the queing of the data from SenderQueue.
116 static ca_mutex g_bleClientSendDataMutex = NULL;
119 * Mutex to synchronize the queing of the data from ReceiverQueue.
121 static ca_mutex g_bleReceiveDataMutex = NULL;
124 * Mutex to synchronize the queing of the data from SenderQueue.
126 static ca_mutex g_bleServerSendDataMutex = NULL;
129 * Mutex to synchronize the callback to be called for the
130 * adapterReqResponse.
132 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
135 * Callback to be called when network packet received from either
136 * GattServer or GattClient.
138 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
141 * Callback to notify error from the BLE adapter.
143 static CAErrorHandleCallback g_errorHandler = NULL;
146 * Register network change notification callback.
148 * @param[in] netCallback CANetworkChangeCallback callback which will
149 * be set for the change in network.
151 * @return 0 on success otherwise a positive error value.
152 * @retval ::CA_STATUS_OK Successful.
153 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
154 * @retval ::CA_STATUS_FAILED Operation failed.
157 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
160 * Set the thread pool handle which is required for spawning new
163 * @param[in] handle Thread pool handle which is given by above layer
164 * for using thread creation task.
167 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
170 * Call the callback to the upper layer when the adapter state gets
173 * @param[in] adapter_state New state of the adapter to be notified to
176 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state);
179 * Call the callback to the upper layer when the device connection state gets
182 * @param[in] address LE address of the device to be notified to the upper layer.
183 * @param[in] isConnected whether connection state is connected or not.
185 static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
189 * Used to initialize all required mutex variable for LE Adapter
192 * @return 0 on success otherwise a positive error value.
193 * @retval ::CA_STATUS_OK Successful.
194 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
195 * @retval ::CA_STATUS_FAILED Operation failed.
198 static CAResult_t CAInitLEAdapterMutex();
201 * Terminate all required mutex variables for LE adapter
204 static void CATerminateLEAdapterMutex();
207 * Prepares and notify error through error callback.
209 static void CALEErrorHandler(const char *remoteAddress,
214 #ifndef SINGLE_THREAD
216 * Stop condition of recvhandler.
218 static bool g_dataBleReceiverHandlerState = false;
221 * Sender information.
223 static u_arraylist_t *g_bleServerSenderInfo = NULL;
225 static u_arraylist_t *g_bleClientSenderInfo = NULL;
228 * Queue to process the outgoing packets from GATTClient.
230 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
233 * Queue to process the incoming packets.
235 static CAQueueingThread_t *g_bleReceiverQueue = NULL;
238 * Queue to process the outgoing packets from GATTServer.
240 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
243 * This function will be associated with the sender queue for
246 * This function will fragment the data to the MTU of the transport
247 * and send the data in fragments to the adapters. The function will
248 * be blocked until all data is sent out from the adapter.
250 * @param[in] threadData Data pushed to the queue which contains the
251 * info about RemoteEndpoint and Data.
253 static void CALEServerSendDataThread(void *threadData);
256 * This function will be associated with the sender queue for
259 * This function will fragment the data to the MTU of the transport
260 * and send the data in fragments to the adapters. The function will
261 * be blocked until all data is sent out from the adapter.
263 * @param[in] threadData Data pushed to the queue which contains the
264 * info about RemoteEndpoint and Data.
266 static void CALEClientSendDataThread(void *threadData);
269 * This function will be associated with the receiver queue.
271 * This function will defragment the received data from each sender
272 * respectively and will send it up to CA layer. Respective sender's
273 * header will provide the length of the data sent.
275 * @param[in] threadData Data pushed to the queue which contains the
276 * info about RemoteEndpoint and Data.
278 static void CALEDataReceiverHandler(void *threadData);
281 * This function will stop all queues created for GattServer and
282 * GattClient. All four queues will be be stopped with this function
285 static void CAStopLEQueues();
288 * This function will terminate all queues created for GattServer and
289 * GattClient. All four queues will be be terminated with this
290 * function invocations.
292 static void CATerminateLEQueues();
295 * This function will initalize the Receiver and Sender queues for
296 * GattServer. This function will in turn call the functions
297 * CAInitBleServerReceiverQueue() and CAInitBleServerSenderQueue() to
298 * initialize the queues.
300 * @return ::CA_STATUS_OK or Appropriate error code.
301 * @retval ::CA_STATUS_OK Successful.
302 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
303 * @retval ::CA_STATUS_FAILED Operation failed.
305 static CAResult_t CAInitLEServerQueues();
308 * This function will initalize the Receiver and Sender queues for
309 * GattClient. This function will inturn call the functions
310 * CAInitBleClientReceiverQueue() and CAInitBleClientSenderQueue() to
311 * initialize the queues.
313 * @return ::CA_STATUS_OK or Appropriate error code.
314 * @retval ::CA_STATUS_OK Successful.
315 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
316 * @retval ::CA_STATUS_FAILED Operation failed.
319 static CAResult_t CAInitLEClientQueues();
322 * This function will initalize the Receiver queue for
323 * GattServer. This will initialize the queue to process the function
324 * CABLEServerSendDataThread() when ever the task is added to this
327 * @return ::CA_STATUS_OK or Appropriate error code.
328 * @retval ::CA_STATUS_OK Successful.
329 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
330 * @retval ::CA_STATUS_FAILED Operation failed.
332 static CAResult_t CAInitLEServerSenderQueue();
335 * This function will initalize the Receiver queue for
336 * GattClient. This will initialize the queue to process the function
337 * CABLEClientSendDataThread() when ever the task is added to this
340 * @return ::CA_STATUS_OK or Appropriate error code.
341 * @retval ::CA_STATUS_OK Successful.
342 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
343 * @retval ::CA_STATUS_FAILED Operation failed.
345 static CAResult_t CAInitLEClientSenderQueue();
348 * This function will initialize the Receiver queue for
349 * LEAdapter. This will initialize the queue to process the function
350 * CABLEDataReceiverHandler() when ever the task is added to this
353 * @return ::CA_STATUS_OK or Appropriate error code
354 * @retval ::CA_STATUS_OK Successful
355 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
356 * @retval ::CA_STATUS_FAILED Operation failed
359 static CAResult_t CAInitLEReceiverQueue();
362 * This function will create the Data required to send it in the
365 * @param[in] remoteEndpoint Remote endpoint information of the
367 * @param[in] data Data to be transmitted from LE.
368 * @param[in] dataLength Length of the Data being transmitted.
370 * @return ::CA_STATUS_OK or Appropriate error code.
371 * @retval ::CA_STATUS_OK Successful.
372 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
373 * @retval ::CA_STATUS_FAILED Operation failed.
375 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
378 u_arraylist_t *senderInfo);
381 * Used to free the BLE information stored in the sender/receiver
384 * @param[in] bleData Information for a particular data segment.
386 static void CAFreeLEData(CALEData_t *bleData);
391 static void CALEDataDestroyer(void *data, uint32_t size);
393 #ifndef SINGLE_THREAD
395 * remove request or response data of send queue.
397 * @param[in] queueHandle queue to process the outgoing packets.
398 * @param[in] mutex mutex related to sender for client / server.
399 * @param[in] address target address to remove data in queue.
401 static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle,
403 const char* address);
406 * remove all received data of data list from receive queue.
408 * @param[in] dataInfoList received data list to remove for client / server.
409 * @param[in] address target address to remove data in queue.
411 static void CALERemoveReceiveQueueData(u_arraylist_t *dataInfoList,
412 const char* address);
415 static CAResult_t CAInitLEServerQueues()
417 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
419 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
421 CAResult_t result = CAInitLEServerSenderQueue();
422 if (CA_STATUS_OK != result)
424 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
425 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
426 return CA_STATUS_FAILED;
429 g_bleServerSenderInfo = u_arraylist_create();
430 if (!g_bleServerSenderInfo)
432 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
433 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
434 return CA_MEMORY_ALLOC_FAILED;
437 result = CAInitLEReceiverQueue();
438 if (CA_STATUS_OK != result)
440 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
441 u_arraylist_free(&g_bleServerSenderInfo);
442 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
443 return CA_STATUS_FAILED;
446 g_dataBleReceiverHandlerState = true;
448 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
450 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
454 static CAResult_t CAInitLEClientQueues()
456 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
458 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
460 CAResult_t result = CAInitLEClientSenderQueue();
461 if (CA_STATUS_OK != result)
463 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
464 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
465 return CA_STATUS_FAILED;
468 g_bleClientSenderInfo = u_arraylist_create();
469 if (!g_bleClientSenderInfo)
471 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
472 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
473 return CA_MEMORY_ALLOC_FAILED;
476 result = CAInitLEReceiverQueue();
477 if (CA_STATUS_OK != result)
479 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
480 u_arraylist_free(&g_bleClientSenderInfo);
481 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
482 return CA_STATUS_FAILED;
485 g_dataBleReceiverHandlerState = true;
487 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
489 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
493 static CAResult_t CAInitLEReceiverQueue()
495 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEReceiverQueue");
496 // Check if the message queue is already initialized
497 if (g_bleReceiverQueue)
499 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
503 // Create recv message queue
504 g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
505 if (!g_bleReceiverQueue)
507 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
508 return CA_MEMORY_ALLOC_FAILED;
511 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue,
512 g_bleAdapterThreadPool,
513 CALEDataReceiverHandler,
516 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
517 OICFree(g_bleReceiverQueue);
518 g_bleReceiverQueue = NULL;
519 return CA_STATUS_FAILED;
522 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue))
524 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
525 OICFree(g_bleReceiverQueue);
526 g_bleReceiverQueue = NULL;
527 return CA_STATUS_FAILED;
530 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
534 static CAResult_t CAInitLEServerSenderQueue()
536 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEServerSenderQueue");
537 // Check if the message queue is already initialized
538 if (g_bleServerSendQueueHandle)
540 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
544 // Create send message queue
545 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
546 if (!g_bleServerSendQueueHandle)
548 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
549 return CA_MEMORY_ALLOC_FAILED;
552 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
553 g_bleAdapterThreadPool,
554 CALEServerSendDataThread,
557 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
558 OICFree(g_bleServerSendQueueHandle);
559 g_bleServerSendQueueHandle = NULL;
560 return CA_STATUS_FAILED;
563 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
567 static void CALEClearSenderInfoImpl(u_arraylist_t ** list)
569 const size_t length = u_arraylist_length(*list);
570 for (size_t i = 0; i < length; ++i)
572 CABLESenderInfo_t * const info =
573 (CABLESenderInfo_t *) u_arraylist_get(*list, i);
576 OICFree(info->defragData);
577 CAFreeEndpoint(info->remoteEndpoint);
581 u_arraylist_free(list);
584 static void CALEClearSenderInfo()
586 CALEClearSenderInfoImpl(&g_bleServerSenderInfo);
587 CALEClearSenderInfoImpl(&g_bleClientSenderInfo);
590 static CAResult_t CAInitLEClientSenderQueue()
592 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEClientSenderQueue");
594 if (g_bleClientSendQueueHandle)
596 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
600 // Create send message queue
601 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
602 if (!g_bleClientSendQueueHandle)
604 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
605 return CA_MEMORY_ALLOC_FAILED;
608 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
609 g_bleAdapterThreadPool,
610 CALEClientSendDataThread, CALEDataDestroyer))
612 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
613 OICFree(g_bleClientSendQueueHandle);
614 g_bleClientSendQueueHandle = NULL;
615 return CA_STATUS_FAILED;
618 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAInitLEClientSenderQueue");
622 static void CAStopLEQueues()
624 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStopLEQueues");
626 ca_mutex_lock(g_bleReceiveDataMutex);
627 if (NULL != g_bleReceiverQueue)
629 CAQueueingThreadStop(g_bleReceiverQueue);
631 ca_mutex_unlock(g_bleReceiveDataMutex);
633 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAStopLEQueues");
636 static void CATerminateLEQueues()
638 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
640 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
641 OICFree(g_bleClientSendQueueHandle);
642 g_bleClientSendQueueHandle = NULL;
644 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
645 OICFree(g_bleServerSendQueueHandle);
646 g_bleServerSendQueueHandle = NULL;
648 CAQueueingThreadDestroy(g_bleReceiverQueue);
649 OICFree(g_bleReceiverQueue);
650 g_bleReceiverQueue = NULL;
652 CALEClearSenderInfo();
654 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
657 static CAResult_t CALEGetSenderInfo(const char *leAddress,
658 u_arraylist_t *senderInfoList,
659 CABLESenderInfo_t **senderInfo,
660 uint32_t *senderIndex)
662 VERIFY_NON_NULL_RET(leAddress,
664 "NULL BLE address argument",
665 CA_STATUS_INVALID_PARAM);
666 VERIFY_NON_NULL_RET(senderIndex,
668 "NULL index argument",
669 CA_STATUS_INVALID_PARAM);
671 const uint32_t listLength = u_arraylist_length(senderInfoList);
672 const uint32_t addrLength = strlen(leAddress);
673 for (uint32_t index = 0; index < listLength; index++)
675 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(senderInfoList, index);
676 if(!info || !(info->remoteEndpoint))
681 if(!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
683 *senderIndex = index;
692 return CA_STATUS_FAILED;
695 static void CALEDataReceiverHandler(void *threadData)
697 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDataReceiverHandler");
699 ca_mutex_lock(g_bleReceiveDataMutex);
701 if (g_dataBleReceiverHandlerState)
703 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
705 CALEData_t *bleData = (CALEData_t *) threadData;
708 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
709 ca_mutex_unlock(g_bleReceiveDataMutex);
713 if (!(bleData->senderInfo))
715 OIC_LOG(ERROR, CALEADAPTER_TAG, "sender info is not available");
716 ca_mutex_unlock(g_bleReceiveDataMutex);
720 if (!(bleData->remoteEndpoint))
722 OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!");
723 ca_mutex_unlock(g_bleReceiveDataMutex);
727 CABLESenderInfo_t *senderInfo = NULL;
728 uint32_t senderIndex = 0;
730 if(CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
732 &senderInfo, &senderIndex))
734 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s]",
735 bleData->remoteEndpoint->addr);
740 CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
743 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
744 ca_mutex_unlock(g_bleReceiveDataMutex);
747 newSender->recvDataLen = 0;
748 newSender->totalDataLen = 0;
749 newSender->defragData = NULL;
750 newSender->remoteEndpoint = NULL;
752 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
754 newSender->totalDataLen = coap_get_total_message_length(bleData->data, bleData->dataLen);
756 if(!(newSender->totalDataLen))
758 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
760 ca_mutex_unlock(g_bleReceiveDataMutex);
764 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
765 newSender->totalDataLen);
766 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
769 newSender->defragData = OICCalloc(newSender->totalDataLen + 1,
770 sizeof(*newSender->defragData));
772 if (NULL == newSender->defragData)
774 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
776 ca_mutex_unlock(g_bleReceiveDataMutex);
780 const char *remoteAddress = bleData->remoteEndpoint->addr;
781 newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
782 CA_ADAPTER_GATT_BTLE,
785 if (NULL == newSender->remoteEndpoint)
787 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
788 OICFree(newSender->defragData);
790 ca_mutex_unlock(g_bleReceiveDataMutex);
794 if (newSender->recvDataLen + bleData->dataLen > newSender->totalDataLen)
796 OIC_LOG(ERROR, CALEADAPTER_TAG, "buffer is smaller than received data");
797 OICFree(newSender->defragData);
798 CAFreeEndpoint(newSender->remoteEndpoint);
800 ca_mutex_unlock(g_bleReceiveDataMutex);
803 memcpy(newSender->defragData, bleData->data, bleData->dataLen);
804 newSender->recvDataLen += bleData->dataLen;
806 u_arraylist_add(bleData->senderInfo,(void *)newSender);
808 //Getting newSender index position in bleSenderInfo array list
810 CALEGetSenderInfo(newSender->remoteEndpoint->addr, bleData->senderInfo,
813 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
814 OICFree(newSender->defragData);
815 CAFreeEndpoint(newSender->remoteEndpoint);
817 ca_mutex_unlock(g_bleReceiveDataMutex);
820 senderInfo = newSender;
824 if(senderInfo->recvDataLen + bleData->dataLen > senderInfo->totalDataLen)
826 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
827 "Data Length exceeding error!! Receiving [%d] total length [%d]",
828 senderInfo->recvDataLen + bleData->dataLen, senderInfo->totalDataLen);
829 u_arraylist_remove(bleData->senderInfo, senderIndex);
830 OICFree(senderInfo->defragData);
832 ca_mutex_unlock(g_bleReceiveDataMutex);
835 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]",
837 memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data,
839 senderInfo->recvDataLen += bleData->dataLen ;
840 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] received Datalen [%d]",
841 senderInfo->totalDataLen, senderInfo->recvDataLen);
844 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
846 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
847 if (NULL == g_networkPacketReceivedCallback)
849 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
851 u_arraylist_remove(bleData->senderInfo, senderIndex);
852 OICFree(senderInfo->defragData);
854 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
855 ca_mutex_unlock(g_bleReceiveDataMutex);
859 OIC_LOG(DEBUG, CALEADAPTER_TAG, "[CALEDataReceiverHandler] Sending data up !");
861 const CASecureEndpoint_t tmp =
863 .endpoint = *senderInfo->remoteEndpoint
866 g_networkPacketReceivedCallback(&tmp,
867 senderInfo->defragData,
868 senderInfo->recvDataLen);
869 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
870 u_arraylist_remove(bleData->senderInfo, senderIndex);
871 senderInfo->remoteEndpoint = NULL;
872 senderInfo->defragData = NULL;
876 ca_mutex_unlock(g_bleReceiveDataMutex);
877 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
880 static void CALEServerSendDataThread(void *threadData)
882 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEServerSendDataThread");
884 CALEData_t * const bleData = (CALEData_t *) threadData;
887 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
891 const uint32_t totalLength = bleData->dataLen;
895 "Server total Data length with header is [%u]",
898 uint8_t * const dataSegment = OICCalloc(totalLength, 1);
900 if (NULL == dataSegment)
902 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
907 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
909 length = totalLength;
910 memcpy(dataSegment, bleData->data, bleData->dataLen);
914 length = CA_SUPPORTED_BLE_MTU_SIZE;
915 memcpy(dataSegment, bleData->data, CA_SUPPORTED_BLE_MTU_SIZE);
918 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
920 CAResult_t result = CA_STATUS_FAILED;
922 // Send the first segment with the header.
923 if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
925 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
927 result = CAUpdateCharacteristicsToGattClient(
928 bleData->remoteEndpoint->addr, dataSegment, length);
930 if (CA_STATUS_OK != result)
934 "Update characteristics failed, result [%d]",
937 g_errorHandler(bleData->remoteEndpoint,
941 OICFree(dataSegment);
947 "Server Sent data length [%u]",
949 for (index = 1; index < iter; index++)
951 // Send the remaining header.
954 "Sending the chunk number [%u]",
958 CAUpdateCharacteristicsToGattClient(
959 bleData->remoteEndpoint->addr,
960 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE)),
961 CA_SUPPORTED_BLE_MTU_SIZE);
963 if (CA_STATUS_OK != result)
965 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
966 "Update characteristics failed, result [%d]", result);
967 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
968 OICFree(dataSegment);
971 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
972 CA_SUPPORTED_BLE_MTU_SIZE);
975 const uint32_t remainingLen =
976 totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
978 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
980 // send the last segment of the data (Ex: 22 bytes of 622
981 // bytes of data when MTU is 200)
982 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
984 result = CAUpdateCharacteristicsToGattClient(
985 bleData->remoteEndpoint->addr,
986 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
989 if (CA_STATUS_OK != result)
993 "Update characteristics failed, result [%d]",
995 g_errorHandler(bleData->remoteEndpoint,
999 OICFree(dataSegment);
1002 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1007 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
1008 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
1009 if (CA_STATUS_OK != result)
1011 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1013 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1014 OICFree(dataSegment);
1017 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1018 for (index = 1; index < iter; index++)
1020 // Send the remaining header.
1021 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1023 result = CAUpdateCharacteristicsToAllGattClients(
1024 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE)),
1025 CA_SUPPORTED_BLE_MTU_SIZE);
1027 if (CA_STATUS_OK != result)
1029 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1031 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1032 OICFree(dataSegment);
1035 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%u]",
1036 CA_SUPPORTED_BLE_MTU_SIZE);
1039 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1040 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1042 // send the last segment of the data
1043 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1045 result = CAUpdateCharacteristicsToAllGattClients(
1046 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1049 if (CA_STATUS_OK != result)
1051 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1053 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1054 OICFree(dataSegment);
1057 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1060 OICFree(dataSegment);
1062 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CALEServerSendDataThread");
1065 static void CALEClientSendDataThread(void *threadData)
1067 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEClientSendDataThread");
1069 CALEData_t *bleData = (CALEData_t *) threadData;
1072 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1076 const uint32_t totalLength = bleData->dataLen;
1078 uint8_t *dataSegment = OICCalloc(totalLength, 1);
1079 if (NULL == dataSegment)
1081 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1085 uint32_t length = 0;
1086 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1088 length = totalLength;
1095 length = CA_SUPPORTED_BLE_MTU_SIZE;
1098 CA_SUPPORTED_BLE_MTU_SIZE);
1101 CAResult_t result = CA_STATUS_FAILED;
1102 const uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1104 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1106 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1107 // Send the first segment with the header.
1109 CAUpdateCharacteristicsToGattServer(
1110 bleData->remoteEndpoint->addr,
1116 if (CA_STATUS_OK != result)
1120 "Update characteristics failed, result [%d]",
1122 g_errorHandler(bleData->remoteEndpoint,
1126 OICFree(dataSegment);
1132 "Client Sent Data length is [%u]",
1135 for (index = 1; index < iter; index++)
1137 // Send the remaining header.
1138 result = CAUpdateCharacteristicsToGattServer(
1139 bleData->remoteEndpoint->addr,
1140 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1141 CA_SUPPORTED_BLE_MTU_SIZE,
1144 if (CA_STATUS_OK != result)
1148 "Update characteristics failed, result [%d]",
1150 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1151 OICFree(dataSegment);
1154 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1155 CA_SUPPORTED_BLE_MTU_SIZE);
1158 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1159 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1161 // send the last segment of the data (Ex: 22 bytes of 622
1162 // bytes of data when MTU is 200)
1163 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1165 result = CAUpdateCharacteristicsToGattServer(
1166 bleData->remoteEndpoint->addr,
1167 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1171 if (CA_STATUS_OK != result)
1173 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1175 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1176 OICFree(dataSegment);
1179 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1184 //Sending Mulitcast Data
1185 // Send the first segment with the header.
1186 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1187 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1188 if (CA_STATUS_OK != result)
1190 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1191 "Update characteristics (all) failed, result [%d]", result);
1192 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1193 OICFree(dataSegment);
1196 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1197 // Send the remaining header.
1198 for (index = 1; index < iter; index++)
1200 result = CAUpdateCharacteristicsToAllGattServers(
1201 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1202 CA_SUPPORTED_BLE_MTU_SIZE);
1204 if (CA_STATUS_OK != result)
1206 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1208 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1209 OICFree(dataSegment);
1212 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1213 CA_SUPPORTED_BLE_MTU_SIZE);
1216 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1217 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1219 // send the last segment of the data (Ex: 22 bytes of 622
1220 // bytes of data when MTU is 200)
1221 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1223 CAUpdateCharacteristicsToAllGattServers(
1224 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1227 if (CA_STATUS_OK != result)
1229 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1230 "Update characteristics (all) failed, result [%d]", result);
1231 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1232 OICFree(dataSegment);
1235 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1240 OICFree(dataSegment);
1242 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1245 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
1246 const uint8_t *data,
1247 uint32_t dataLength,
1248 u_arraylist_t *senderInfo)
1250 CALEData_t * const bleData = OICMalloc(sizeof(CALEData_t));
1254 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1258 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1259 bleData->data = OICCalloc(dataLength + 1, 1);
1261 if (NULL == bleData->data)
1263 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1264 CAFreeLEData(bleData);
1268 memcpy(bleData->data, data, dataLength);
1269 bleData->dataLen = dataLength;
1272 bleData->senderInfo = senderInfo;
1278 static void CAFreeLEData(CALEData_t *bleData)
1280 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1282 CAFreeEndpoint(bleData->remoteEndpoint);
1283 OICFree(bleData->data);
1287 static void CALEDataDestroyer(void *data, uint32_t size)
1289 if ((size_t)size < sizeof(CALEData_t *))
1291 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1292 "Destroy data too small %p %d", data, size);
1294 CALEData_t *ledata = (CALEData_t *) data;
1296 CAFreeLEData(ledata);
1300 static CAResult_t CAInitLEAdapterMutex()
1302 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEAdapterMutex");
1304 if (NULL == g_bleIsServerMutex)
1306 g_bleIsServerMutex = ca_mutex_new();
1307 if (NULL == g_bleIsServerMutex)
1309 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1310 return CA_STATUS_FAILED;
1314 if (NULL == g_bleNetworkCbMutex)
1316 g_bleNetworkCbMutex = ca_mutex_new();
1317 if (NULL == g_bleNetworkCbMutex)
1319 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1320 CATerminateLEAdapterMutex();
1321 return CA_STATUS_FAILED;
1325 if (NULL == g_bleLocalAddressMutex)
1327 g_bleLocalAddressMutex = ca_mutex_new();
1328 if (NULL == g_bleLocalAddressMutex)
1330 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1331 CATerminateLEAdapterMutex();
1332 return CA_STATUS_FAILED;
1336 if (NULL == g_bleAdapterThreadPoolMutex)
1338 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1339 if (NULL == g_bleAdapterThreadPoolMutex)
1341 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1342 CATerminateLEAdapterMutex();
1343 return CA_STATUS_FAILED;
1347 if (NULL == g_bleClientSendDataMutex)
1349 g_bleClientSendDataMutex = ca_mutex_new();
1350 if (NULL == g_bleClientSendDataMutex)
1352 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1353 CATerminateLEAdapterMutex();
1354 return CA_STATUS_FAILED;
1358 if (NULL == g_bleServerSendDataMutex)
1360 g_bleServerSendDataMutex = ca_mutex_new();
1361 if (NULL == g_bleServerSendDataMutex)
1363 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1364 CATerminateLEAdapterMutex();
1365 return CA_STATUS_FAILED;
1369 if (NULL == g_bleAdapterReqRespCbMutex)
1371 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1372 if (NULL == g_bleAdapterReqRespCbMutex)
1374 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1375 CATerminateLEAdapterMutex();
1376 return CA_STATUS_FAILED;
1380 if (NULL == g_bleReceiveDataMutex)
1382 g_bleReceiveDataMutex = ca_mutex_new();
1383 if (NULL == g_bleReceiveDataMutex)
1385 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1386 return CA_STATUS_FAILED;
1390 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1391 return CA_STATUS_OK;
1394 static void CATerminateLEAdapterMutex()
1396 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CATerminateLEAdapterMutex");
1398 ca_mutex_free(g_bleIsServerMutex);
1399 g_bleIsServerMutex = NULL;
1401 ca_mutex_free(g_bleNetworkCbMutex);
1402 g_bleNetworkCbMutex = NULL;
1404 ca_mutex_free(g_bleLocalAddressMutex);
1405 g_bleLocalAddressMutex = NULL;
1407 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1408 g_bleAdapterThreadPoolMutex = NULL;
1410 ca_mutex_free(g_bleClientSendDataMutex);
1411 g_bleClientSendDataMutex = NULL;
1413 ca_mutex_free(g_bleServerSendDataMutex);
1414 g_bleServerSendDataMutex = NULL;
1416 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1417 g_bleAdapterReqRespCbMutex = NULL;
1419 ca_mutex_free(g_bleReceiveDataMutex);
1420 g_bleReceiveDataMutex = NULL;
1422 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1426 * Starting LE connectivity adapters.
1428 * As its peer to peer it does not require to start any servers.
1430 * @return ::CA_STATUS_OK or Appropriate error code.
1432 static CAResult_t CAStartLE();
1435 * Start listening server for receiving multicast search requests.
1437 * Transport Specific Behavior:
1438 * LE Starts GATT Server with prefixed UUID and Characteristics
1439 * per OIC Specification.
1440 * @return ::CA_STATUS_OK or Appropriate error code.
1442 static CAResult_t CAStartLEListeningServer();
1445 * Stops listening server from receiving multicast search requests.
1447 * Transport Specific Behavior:
1448 * LE Starts GATT Server with prefixed UUID and Characteristics
1449 * per OIC Specification.
1450 * @return ::CA_STATUS_OK or Appropriate error code.
1452 static CAResult_t CAStopLEListeningServer();
1455 * Sarting discovery of servers for receiving multicast
1458 * Transport Specific Behavior:
1459 * LE Starts GATT Server with prefixed UUID and Characteristics
1460 * per OIC Specification.
1462 * @return ::CA_STATUS_OK or Appropriate error code
1464 static CAResult_t CAStartLEDiscoveryServer();
1467 * Send data to the endpoint using the adapter connectivity.
1469 * @param[in] endpoint Remote Endpoint information (like MAC address,
1470 * reference URI and connectivity type) to which
1471 * the unicast data has to be sent.
1472 * @param[in] data Data which required to be sent.
1473 * @param[in] dataLen Size of data to be sent.
1475 * @note dataLen must be > 0.
1477 * @return The number of bytes sent on the network, or -1 on error.
1479 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1484 * Send multicast data to the endpoint using the LE connectivity.
1486 * @param[in] endpoint Remote Endpoint information to which the
1487 * multicast data has to be sent.
1488 * @param[in] data Data which required to be sent.
1489 * @param[in] dataLen Size of data to be sent.
1491 * @note dataLen must be > 0.
1493 * @return The number of bytes sent on the network, or -1 on error.
1495 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1500 * Get LE Connectivity network information.
1502 * @param[out] info Local connectivity information structures.
1503 * @param[out] size Number of local connectivity structures.
1505 * @return ::CA_STATUS_OK or Appropriate error code.
1507 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info,
1511 * Read Synchronous API callback.
1513 * @return ::CA_STATUS_OK or Appropriate error code.
1515 static CAResult_t CAReadLEData();
1518 * Stopping the adapters and close socket connections.
1520 * LE Stops all GATT servers and GATT Clients.
1522 * @return ::CA_STATUS_OK or Appropriate error code.
1524 static CAResult_t CAStopLE();
1527 * Terminate the LE connectivity adapter.
1529 * Configuration information will be deleted from further use.
1531 static void CATerminateLE();
1534 * This function will receive the data from the GattServer and add the
1535 * data to the Server receiver queue.
1537 * @param[in] remoteAddress Remote address of the device from where
1539 * @param[in] data Actual data received from the remote
1541 * @param[in] dataLength Length of the data received from the
1543 * @param[in] sentLength Length of the data sent from the remote
1546 * @return ::CA_STATUS_OK or Appropriate error code.
1547 * @retval ::CA_STATUS_OK Successful.
1548 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1549 * @retval ::CA_STATUS_FAILED Operation failed.
1552 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
1553 const uint8_t *data,
1554 uint32_t dataLength,
1555 uint32_t *sentLength);
1558 * This function will receive the data from the GattClient and add the
1559 * data into the Client receiver queue.
1561 * @param[in] remoteAddress Remote address of the device from where
1563 * @param[in] data Actual data recevied from the remote
1565 * @param[in] dataLength Length of the data received from the
1567 * @param[in] sentLength Length of the data sent from the remote
1570 * @return ::CA_STATUS_OK or Appropriate error code.
1571 * @retval ::CA_STATUS_OK Successful.
1572 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1573 * @retval ::CA_STATUS_FAILED Operation failed.
1575 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
1576 const uint8_t *data,
1577 uint32_t dataLength,
1578 uint32_t *sentLength);
1581 * Set the NetworkPacket received callback to CA layer from adapter
1584 * @param[in] callback Callback handle sent from the upper layer.
1586 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
1589 * Push the data from CA layer to the Sender processor queue.
1591 * @param[in] remoteEndpoint Remote endpoint information of the
1593 * @param[in] data Data to be transmitted from LE.
1594 * @param[in] dataLen Length of the Data being transmitted.
1596 * @return ::CA_STATUS_OK or Appropriate error code.
1597 * @retval ::CA_STATUS_OK Successful.
1598 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1599 * @retval ::CA_STATUS_FAILED Operation failed.
1601 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1602 const uint8_t *data,
1606 * Push the data from CA layer to the Sender processor queue.
1608 * @param[in] remoteEndpoint Remote endpoint information of the
1610 * @param[in] data Data to be transmitted from LE.
1611 * @param[in] dataLen Length of the Data being transmitted.
1613 * @return ::CA_STATUS_OK or Appropriate error code.
1614 * @retval ::CA_STATUS_OK Successful.
1615 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1616 * @retval ::CA_STATUS_FAILED Operation failed.
1618 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1619 const uint8_t *data,
1622 static CAResult_t CALEAdapterGattServerStart()
1624 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1626 CAResult_t result = CAStartLEGattServer();
1628 #ifndef SINGLE_THREAD
1630 Don't start the server side sending queue thread until the
1631 server itself has actually started.
1633 if (CA_STATUS_OK == result)
1635 ca_mutex_lock(g_bleServerSendDataMutex);
1636 result = CAQueueingThreadStart(g_bleServerSendQueueHandle);
1637 ca_mutex_unlock(g_bleServerSendDataMutex);
1639 if (CA_STATUS_OK != result)
1643 "Unable to start server queuing thread (%d)",
1652 static CAResult_t CALEAdapterGattServerStop()
1654 #ifndef SINGLE_THREAD
1655 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEAdapterGattServerStop");
1657 CAResult_t result = CAStopLEGattServer();
1658 ca_mutex_lock(g_bleServerSendDataMutex);
1659 if (CA_STATUS_OK == result)
1661 result = CAQueueingThreadStop(g_bleServerSendQueueHandle);
1663 ca_mutex_unlock(g_bleServerSendDataMutex);
1667 return CAStopLEGattServer();
1671 static CAResult_t CALEAdapterGattClientStart()
1673 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattClient");
1675 CAResult_t result = CAStartLEGattClient();
1677 #ifndef SINGLE_THREAD
1679 Don't start the client side sending queue thread until the
1680 client itself has actually started.
1682 if (CA_STATUS_OK == result)
1684 ca_mutex_lock(g_bleClientSendDataMutex);
1685 result = CAQueueingThreadStart(g_bleClientSendQueueHandle);
1686 ca_mutex_unlock(g_bleClientSendDataMutex);
1688 if (CA_STATUS_OK != result)
1692 "Unable to start client queuing thread");
1700 static CAResult_t CALEAdapterGattClientStop()
1702 #ifndef SINGLE_THREAD
1703 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEAdapterGattClientStop");
1704 CAStopLEGattClient();
1706 ca_mutex_lock(g_bleClientSendDataMutex);
1707 CAResult_t result = CAQueueingThreadStop(g_bleClientSendQueueHandle);
1708 ca_mutex_unlock(g_bleClientSendDataMutex);
1712 CAStopLEGattClient();
1714 return CA_STATUS_OK;
1718 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1719 CANetworkPacketReceivedCallback reqRespCallback,
1720 CANetworkChangeCallback netCallback,
1721 CAErrorHandleCallback errorCallback,
1722 ca_thread_pool_t handle)
1724 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1727 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1728 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1729 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1731 CAResult_t result = CA_STATUS_OK;
1732 result = CAInitLEAdapterMutex();
1733 if (CA_STATUS_OK != result)
1735 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1736 return CA_STATUS_FAILED;
1739 result = CAInitializeLENetworkMonitor();
1740 if (CA_STATUS_OK != result)
1742 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1743 return CA_STATUS_FAILED;
1745 CAInitializeLEAdapter(handle);
1747 CASetLEClientThreadPoolHandle(handle);
1749 result = CAInitializeLEGattClient();
1750 if (CA_STATUS_OK != result)
1752 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattClient() failed");
1753 return CA_STATUS_FAILED;
1756 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1757 CASetLEServerThreadPoolHandle(handle);
1758 result = CAInitializeLEGattServer();
1759 if (CA_STATUS_OK != result)
1761 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattServer() failed");
1762 return CA_STATUS_FAILED;
1765 CASetLEAdapterThreadPoolHandle(handle);
1766 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1767 CASetLEReqRespAdapterCallback(reqRespCallback);
1769 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1770 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1771 CALERegisterNetworkNotifications(netCallback);
1773 g_errorHandler = errorCallback;
1775 static const CAConnectivityHandler_t connHandler =
1777 .startAdapter = CAStartLE,
1778 .stopAdapter = CAStopLE,
1779 .startListenServer = CAStartLEListeningServer,
1780 .stopListenServer = CAStopLEListeningServer,
1781 .startDiscoveryServer = CAStartLEDiscoveryServer,
1782 .sendData = CASendLEUnicastData,
1783 .sendDataToAll = CASendLEMulticastData,
1784 .GetnetInfo = CAGetLEInterfaceInformation,
1785 .readData = CAReadLEData,
1786 .terminate = CATerminateLE,
1787 .cType = CA_ADAPTER_GATT_BTLE
1790 registerCallback(connHandler);
1792 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1794 return CA_STATUS_OK;
1797 static CAResult_t CAStartLE()
1799 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE");
1801 return CAStartLEAdapter();
1804 static CAResult_t CAStopLE()
1806 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1807 #ifndef SINGLE_THREAD
1811 ca_mutex_lock(g_bleIsServerMutex);
1812 switch (g_adapterType)
1814 case ADAPTER_SERVER:
1815 CALEAdapterGattServerStop();
1817 case ADAPTER_CLIENT:
1818 CALEAdapterGattClientStop();
1820 case ADAPTER_BOTH_CLIENT_SERVER:
1821 CALEAdapterGattServerStop();
1822 CALEAdapterGattClientStop();
1827 ca_mutex_unlock(g_bleIsServerMutex);
1829 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1831 return CAStopLEAdapter();
1834 static void CATerminateLE()
1836 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1838 CASetLEReqRespServerCallback(NULL);
1839 CASetLEReqRespClientCallback(NULL);
1840 CALERegisterNetworkNotifications(NULL);
1841 CASetLEReqRespAdapterCallback(NULL);
1842 CATerminateLENetworkMonitor();
1844 ca_mutex_lock(g_bleIsServerMutex);
1845 switch (g_adapterType)
1847 case ADAPTER_SERVER:
1848 CATerminateLEGattServer();
1850 case ADAPTER_CLIENT:
1851 CATerminateLEGattClient();
1853 case ADAPTER_BOTH_CLIENT_SERVER:
1854 CATerminateLEGattServer();
1855 CATerminateLEGattClient();
1860 g_adapterType = ADAPTER_EMPTY;
1861 ca_mutex_unlock(g_bleIsServerMutex);
1863 #ifndef SINGLE_THREAD
1864 CATerminateLEQueues();
1866 CATerminateLEAdapterMutex();
1868 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1871 static CAResult_t CAStartLEListeningServer()
1873 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEListeningServer");
1874 #ifndef ROUTING_GATEWAY
1875 CAResult_t result = CA_STATUS_OK;
1876 #ifndef SINGLE_THREAD
1877 result = CAInitLEServerQueues();
1878 if (CA_STATUS_OK != result)
1880 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1885 ca_mutex_lock(g_bleIsServerMutex);
1886 switch (g_adapterType)
1888 case ADAPTER_CLIENT:
1889 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1891 case ADAPTER_BOTH_CLIENT_SERVER:
1894 g_adapterType = ADAPTER_SERVER;
1896 ca_mutex_unlock(g_bleIsServerMutex);
1898 result = CAGetLEAdapterState();
1899 if (CA_STATUS_OK != result)
1901 if (CA_ADAPTER_NOT_ENABLED == result)
1905 "Listen Server will be started once BT Adapter is enabled");
1910 result = CALEAdapterGattServerStart();
1913 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1916 // Routing Gateway only supports BLE client mode.
1917 OIC_LOG(ERROR, CALEADAPTER_TAG, "LE server not supported in Routing Gateway");
1918 return CA_NOT_SUPPORTED;
1922 static CAResult_t CAStopLEListeningServer()
1924 OIC_LOG(ERROR, CALEADAPTER_TAG, "Listen server stop not supported.");
1925 return CA_NOT_SUPPORTED;
1928 static CAResult_t CAStartLEDiscoveryServer()
1930 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEDiscoveryServer");
1931 CAResult_t result = CA_STATUS_OK;
1932 #ifndef SINGLE_THREAD
1933 result = CAInitLEClientQueues();
1934 if (CA_STATUS_OK != result)
1936 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1941 ca_mutex_lock(g_bleIsServerMutex);
1942 switch (g_adapterType)
1944 case ADAPTER_SERVER:
1945 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1947 case ADAPTER_BOTH_CLIENT_SERVER:
1950 g_adapterType = ADAPTER_CLIENT;
1952 ca_mutex_unlock(g_bleIsServerMutex);
1954 result = CAGetLEAdapterState();
1955 if (CA_STATUS_OK != result)
1957 if (CA_ADAPTER_NOT_ENABLED == result)
1961 "Discovery Server will be started once BT Adapter is enabled");
1966 result = CALEAdapterGattClientStart();
1969 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1973 static CAResult_t CAReadLEData()
1975 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1976 #ifdef SINGLE_THREAD
1979 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1980 return CA_STATUS_OK;
1983 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1987 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEUnicastData");
1990 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1991 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1993 CAResult_t result = CA_STATUS_FAILED;
1995 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
1996 if (ADAPTER_EMPTY == g_adapterType)
1998 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2001 ca_mutex_lock(g_bleIsServerMutex);
2002 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2004 result = CALEAdapterServerSendData(endpoint, data, dataLen);
2005 if (CA_STATUS_OK != result)
2007 ca_mutex_unlock(g_bleIsServerMutex);
2008 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed");
2011 g_errorHandler(endpoint, data, dataLen, result);
2018 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2020 result = CALEAdapterClientSendData(endpoint, data, dataLen);
2021 if (CA_STATUS_OK != result)
2023 ca_mutex_unlock(g_bleIsServerMutex);
2024 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for client failed" );
2028 g_errorHandler(endpoint, data, dataLen, result);
2033 ca_mutex_unlock(g_bleIsServerMutex);
2035 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2039 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
2043 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEMulticastData");
2046 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
2050 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
2054 CAResult_t result = CA_STATUS_FAILED;
2056 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
2057 if (ADAPTER_EMPTY == g_adapterType)
2059 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2062 ca_mutex_lock(g_bleIsServerMutex);
2063 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2065 result = CALEAdapterServerSendData(NULL, data, dataLen);
2066 if (CA_STATUS_OK != result)
2068 ca_mutex_unlock(g_bleIsServerMutex);
2070 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data for server failed" );
2074 g_errorHandler(endpoint, data, dataLen, result);
2080 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2082 result = CALEAdapterClientSendData(NULL, data, dataLen);
2083 if (CA_STATUS_OK != result)
2085 ca_mutex_unlock(g_bleIsServerMutex);
2087 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data for client failed" );
2091 g_errorHandler(endpoint, data, dataLen, result);
2096 ca_mutex_unlock(g_bleIsServerMutex);
2098 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CASendLEMulticastData");
2102 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
2104 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2106 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
2108 char *local_address = NULL;
2110 CAResult_t res = CAGetLEAddress(&local_address);
2111 if (CA_STATUS_OK != res)
2113 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
2117 if (NULL == local_address)
2119 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
2120 return CA_STATUS_FAILED;
2124 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
2125 if (NULL == (*info))
2127 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
2128 OICFree(local_address);
2129 return CA_STATUS_FAILED;
2132 size_t local_address_len = strlen(local_address);
2134 if(local_address_len >= sizeof(g_localBLEAddress) ||
2135 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
2137 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
2139 OICFree(local_address);
2140 return CA_STATUS_FAILED;
2143 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
2144 ca_mutex_lock(g_bleLocalAddressMutex);
2145 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
2146 ca_mutex_unlock(g_bleLocalAddressMutex);
2148 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
2150 OICFree(local_address);
2152 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2153 return CA_STATUS_OK;
2156 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
2158 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2160 ca_mutex_lock(g_bleNetworkCbMutex);
2161 g_networkCallback = netCallback;
2162 ca_mutex_unlock(g_bleNetworkCbMutex);
2163 CAResult_t res = CA_STATUS_OK;
2166 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
2167 if (CA_STATUS_OK != res)
2169 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2172 res = CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCb);
2173 if (CA_STATUS_OK != res)
2175 OIC_LOG(ERROR, CALEADAPTER_TAG, "CALEConnectionStateChangedCb failed!");
2180 res = CAUnSetLEAdapterStateChangedCb();
2181 if (CA_STATUS_OK != res)
2183 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2187 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2191 static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
2194 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEConnectionStateChangedCb");
2196 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2200 ca_mutex_lock(g_bleIsServerMutex);
2201 switch (g_adapterType)
2203 case ADAPTER_SERVER:
2204 CALEGattServerConnectionStateChanged(isConnected, address);
2206 case ADAPTER_CLIENT:
2207 CALEGattConnectionStateChanged(isConnected, address);
2209 case ADAPTER_BOTH_CLIENT_SERVER:
2210 CALEGattConnectionStateChanged(isConnected, address);
2211 CALEGattServerConnectionStateChanged(isConnected, address);
2216 ca_mutex_unlock(g_bleIsServerMutex);
2221 #ifndef SINGLE_THREAD
2222 if(g_bleClientSenderInfo)
2224 CALERemoveReceiveQueueData(g_bleClientSenderInfo, address);
2227 if(g_bleServerSenderInfo)
2229 CALERemoveReceiveQueueData(g_bleServerSenderInfo, address);
2232 // remove data of send queue.
2233 if (g_bleClientSendQueueHandle)
2235 CALERemoveSendQueueData(g_bleClientSendQueueHandle,
2236 g_bleClientSendDataMutex,
2240 if (g_bleServerSendQueueHandle)
2242 CALERemoveSendQueueData(g_bleServerSendQueueHandle,
2243 g_bleServerSendDataMutex,
2249 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2252 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
2254 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDeviceStateChangedCb");
2256 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
2257 CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE };
2259 ca_mutex_lock(g_bleLocalAddressMutex);
2260 OICStrcpy(localEndpoint.addr,
2261 sizeof(localEndpoint.addr),
2263 ca_mutex_unlock(g_bleLocalAddressMutex);
2265 if (CA_ADAPTER_ENABLED == adapter_state)
2267 ca_mutex_lock(g_bleIsServerMutex);
2268 switch (g_adapterType)
2270 case ADAPTER_SERVER:
2271 CALEAdapterGattServerStart();
2273 case ADAPTER_CLIENT:
2274 CALEAdapterGattClientStart();
2276 case ADAPTER_BOTH_CLIENT_SERVER:
2277 CALEAdapterGattServerStart();
2278 CALEAdapterGattClientStart();
2283 ca_mutex_unlock(g_bleIsServerMutex);
2287 ca_mutex_lock(g_bleIsServerMutex);
2288 switch (g_adapterType)
2290 case ADAPTER_SERVER:
2291 CALEAdapterGattServerStop();
2293 case ADAPTER_CLIENT:
2294 CALEAdapterGattClientStop();
2296 case ADAPTER_BOTH_CLIENT_SERVER:
2297 CALEAdapterGattServerStop();
2298 CALEAdapterGattClientStop();
2303 ca_mutex_unlock(g_bleIsServerMutex);
2306 ca_mutex_lock(g_bleNetworkCbMutex);
2307 if (NULL != g_networkCallback)
2309 g_networkCallback(&localEndpoint, adapter_state);
2313 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
2315 ca_mutex_unlock(g_bleNetworkCbMutex);
2317 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2320 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
2321 const uint8_t *data,
2324 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2326 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2327 #ifndef SINGLE_THREAD
2328 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2329 "g_bleClientSendQueueHandle is NULL",
2331 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
2332 "g_bleClientSendDataMutex is NULL",
2335 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2336 "g_bleClientSendQueueHandle",
2339 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
2341 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2344 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2345 return CA_MEMORY_ALLOC_FAILED;
2347 // Add message to send queue
2348 ca_mutex_lock(g_bleClientSendDataMutex);
2349 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
2350 ca_mutex_unlock(g_bleClientSendDataMutex);
2352 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2353 return CA_STATUS_OK;
2356 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2357 const uint8_t *data,
2360 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2362 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2364 #ifdef SINGLE_THREAD
2365 if (!CAIsLEConnected())
2367 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2368 return CA_STATUS_FAILED;
2371 CAResult_t result = CA_STATUS_OK;
2372 const uint32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2373 for (uint32_t iter = 0; iter < dataLimit; iter++)
2376 CAUpdateCharacteristicsToAllGattClients(
2377 data + (iter * CA_SUPPORTED_BLE_MTU_SIZE),
2378 CA_SUPPORTED_BLE_MTU_SIZE);
2380 if (CA_STATUS_OK != result)
2382 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2383 return CA_STATUS_FAILED;
2389 const uint32_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2393 CAUpdateCharacteristicsToAllGattClients(
2394 data + (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE),
2396 if (CA_STATUS_OK != result)
2398 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2399 return CA_STATUS_FAILED;
2404 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2405 "BleClientReceiverQueue is NULL",
2407 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2408 "BleClientSendDataMutex is NULL",
2411 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2414 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2416 CALEData_t * const bleData =
2417 CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2421 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2422 return CA_MEMORY_ALLOC_FAILED;
2425 // Add message to send queue
2426 ca_mutex_lock(g_bleServerSendDataMutex);
2427 CAQueueingThreadAddData(g_bleServerSendQueueHandle,
2429 sizeof(CALEData_t));
2430 ca_mutex_unlock(g_bleServerSendDataMutex);
2432 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2433 return CA_STATUS_OK;
2436 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2437 const uint8_t *data,
2438 uint32_t dataLength,
2439 uint32_t *sentLength)
2441 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2444 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2445 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2447 #ifdef SINGLE_THREAD
2448 if(g_networkPacketReceivedCallback)
2450 // will be filled by upper layer
2451 const CASecureEndpoint_t endpoint =
2452 { .endpoint = { .adapter = CA_ADAPTER_GATT_BTLE } };
2455 g_networkPacketReceivedCallback(&endpoint, data, dataLength);
2458 VERIFY_NON_NULL_RET(g_bleReceiverQueue,
2460 "g_bleReceiverQueue",
2463 //Add message to data queue
2464 CAEndpoint_t * const remoteEndpoint =
2465 CACreateEndpointObject(CA_DEFAULT_FLAGS,
2466 CA_ADAPTER_GATT_BTLE,
2470 if (NULL == remoteEndpoint)
2472 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2473 return CA_STATUS_FAILED;
2476 // Create bleData to add to queue
2479 "Data received from LE layer [%d]",
2482 CALEData_t * const bleData =
2483 CACreateLEData(remoteEndpoint, data, dataLength, g_bleServerSenderInfo);
2487 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2488 CAFreeEndpoint(remoteEndpoint);
2489 return CA_MEMORY_ALLOC_FAILED;
2492 CAFreeEndpoint(remoteEndpoint);
2493 // Add message to receiver queue
2494 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2496 *sentLength = dataLength;
2498 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2499 return CA_STATUS_OK;
2502 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2503 const uint8_t *data,
2504 uint32_t dataLength,
2505 uint32_t *sentLength)
2507 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2510 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2511 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2512 #ifndef SINGLE_THREAD
2513 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG,
2514 "g_bleReceiverQueue",
2517 //Add message to data queue
2518 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2519 CA_ADAPTER_GATT_BTLE,
2521 if (NULL == remoteEndpoint)
2523 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2524 return CA_STATUS_FAILED;
2527 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%u]", dataLength);
2529 // Create bleData to add to queue
2530 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data,
2531 dataLength, g_bleClientSenderInfo);
2534 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2535 CAFreeEndpoint(remoteEndpoint);
2536 return CA_MEMORY_ALLOC_FAILED;
2539 CAFreeEndpoint(remoteEndpoint);
2540 // Add message to receiver queue
2541 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2543 *sentLength = dataLength;
2545 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2546 return CA_STATUS_OK;
2549 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2551 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2553 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2554 g_bleAdapterThreadPool = handle;
2555 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2557 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2560 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2562 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2564 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2566 g_networkPacketReceivedCallback = callback;
2568 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2570 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2573 static void CALEErrorHandler(const char *remoteAddress,
2574 const uint8_t *data,
2578 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2580 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2582 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2583 CA_ADAPTER_GATT_BTLE,
2587 // if required, will be used to build remote endpoint
2588 g_errorHandler(rep, data, dataLen, result);
2590 CAFreeEndpoint(rep);
2592 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");
2595 #ifndef SINGLE_THREAD
2596 static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle, ca_mutex mutex,
2597 const char* address)
2599 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALERemoveSendQueueData");
2601 VERIFY_NON_NULL_VOID(queueHandle, CALEADAPTER_TAG, "queueHandle");
2602 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2604 ca_mutex_lock(mutex);
2605 while (u_queue_get_size(queueHandle->dataQueue) > 0)
2607 OIC_LOG(DEBUG, CALEADAPTER_TAG, "get data from queue");
2608 u_queue_message_t *message = u_queue_get_element(queueHandle->dataQueue);
2609 if (NULL != message)
2611 CALEData_t *bleData = (CALEData_t *) message->msg;
2612 if (bleData && bleData->remoteEndpoint)
2614 if (!strcmp(bleData->remoteEndpoint->addr, address))
2616 OIC_LOG(DEBUG, CALEADAPTER_TAG, "found the message of disconnected device");
2617 if (NULL != queueHandle->destroy)
2619 queueHandle->destroy(message->msg, message->size);
2623 OICFree(message->msg);
2631 ca_mutex_unlock(mutex);
2634 static void CALERemoveReceiveQueueData(u_arraylist_t *dataInfoList, const char* address)
2636 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALERemoveReceiveQueueData");
2638 VERIFY_NON_NULL_VOID(dataInfoList, CALEADAPTER_TAG, "dataInfoList");
2639 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2641 CABLESenderInfo_t *senderInfo = NULL;
2642 uint32_t senderIndex = 0;
2644 if(CA_STATUS_OK == CALEGetSenderInfo(address, dataInfoList, &senderInfo,
2647 u_arraylist_remove(dataInfoList, senderIndex);
2648 OICFree(senderInfo->defragData);
2649 OICFree(senderInfo->remoteEndpoint);
2650 OICFree(senderInfo);
2652 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo is removed for disconnection");
2656 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo doesn't exist");