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 static CAResult_t CAInitLEServerQueues()
408 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
410 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
412 CAResult_t result = CAInitLEServerSenderQueue();
413 if (CA_STATUS_OK != result)
415 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
416 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
417 return CA_STATUS_FAILED;
420 g_bleServerSenderInfo = u_arraylist_create();
421 if (!g_bleServerSenderInfo)
423 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
424 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
425 return CA_MEMORY_ALLOC_FAILED;
428 result = CAInitLEReceiverQueue();
429 if (CA_STATUS_OK != result)
431 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
432 u_arraylist_free(&g_bleServerSenderInfo);
433 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
434 return CA_STATUS_FAILED;
437 g_dataBleReceiverHandlerState = true;
439 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
441 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
445 static CAResult_t CAInitLEClientQueues()
447 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
449 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
451 CAResult_t result = CAInitLEClientSenderQueue();
452 if (CA_STATUS_OK != result)
454 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
455 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
456 return CA_STATUS_FAILED;
459 g_bleClientSenderInfo = u_arraylist_create();
460 if (!g_bleClientSenderInfo)
462 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
463 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
464 return CA_MEMORY_ALLOC_FAILED;
467 result = CAInitLEReceiverQueue();
468 if (CA_STATUS_OK != result)
470 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
471 u_arraylist_free(&g_bleClientSenderInfo);
472 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
473 return CA_STATUS_FAILED;
476 g_dataBleReceiverHandlerState = true;
478 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
480 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
484 static CAResult_t CAInitLEReceiverQueue()
486 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEReceiverQueue");
487 // Check if the message queue is already initialized
488 if (g_bleReceiverQueue)
490 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
494 // Create recv message queue
495 g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
496 if (!g_bleReceiverQueue)
498 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
499 return CA_MEMORY_ALLOC_FAILED;
502 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue,
503 g_bleAdapterThreadPool,
504 CALEDataReceiverHandler,
507 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
508 OICFree(g_bleReceiverQueue);
509 g_bleReceiverQueue = NULL;
510 return CA_STATUS_FAILED;
513 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue))
515 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
516 OICFree(g_bleReceiverQueue);
517 g_bleReceiverQueue = NULL;
518 return CA_STATUS_FAILED;
521 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
525 static CAResult_t CAInitLEServerSenderQueue()
527 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEServerSenderQueue");
528 // Check if the message queue is already initialized
529 if (g_bleServerSendQueueHandle)
531 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
535 // Create send message queue
536 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
537 if (!g_bleServerSendQueueHandle)
539 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
540 return CA_MEMORY_ALLOC_FAILED;
543 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
544 g_bleAdapterThreadPool,
545 CALEServerSendDataThread,
548 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
549 OICFree(g_bleServerSendQueueHandle);
550 g_bleServerSendQueueHandle = NULL;
551 return CA_STATUS_FAILED;
554 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
558 static void CALEClearSenderInfoImpl(u_arraylist_t ** list)
560 const size_t length = u_arraylist_length(*list);
561 for (size_t i = 0; i < length; ++i)
563 CABLESenderInfo_t * const info =
564 (CABLESenderInfo_t *) u_arraylist_get(*list, i);
567 OICFree(info->defragData);
568 CAFreeEndpoint(info->remoteEndpoint);
572 u_arraylist_free(list);
575 static void CALEClearSenderInfo()
577 CALEClearSenderInfoImpl(&g_bleServerSenderInfo);
578 CALEClearSenderInfoImpl(&g_bleClientSenderInfo);
581 static CAResult_t CAInitLEClientSenderQueue()
583 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEClientSenderQueue");
585 if (g_bleClientSendQueueHandle)
587 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
591 // Create send message queue
592 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
593 if (!g_bleClientSendQueueHandle)
595 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
596 return CA_MEMORY_ALLOC_FAILED;
599 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
600 g_bleAdapterThreadPool,
601 CALEClientSendDataThread, CALEDataDestroyer))
603 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
604 OICFree(g_bleClientSendQueueHandle);
605 g_bleClientSendQueueHandle = NULL;
606 return CA_STATUS_FAILED;
609 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAInitLEClientSenderQueue");
613 static void CAStopLEQueues()
615 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStopLEQueues");
617 ca_mutex_lock(g_bleReceiveDataMutex);
618 if (NULL != g_bleReceiverQueue)
620 CAQueueingThreadStop(g_bleReceiverQueue);
622 ca_mutex_unlock(g_bleReceiveDataMutex);
624 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAStopLEQueues");
627 static void CATerminateLEQueues()
629 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
631 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
632 OICFree(g_bleClientSendQueueHandle);
633 g_bleClientSendQueueHandle = NULL;
635 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
636 OICFree(g_bleServerSendQueueHandle);
637 g_bleServerSendQueueHandle = NULL;
639 CAQueueingThreadDestroy(g_bleReceiverQueue);
640 OICFree(g_bleReceiverQueue);
641 g_bleReceiverQueue = NULL;
643 CALEClearSenderInfo();
645 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
648 static CAResult_t CALEGetSenderInfo(const char *leAddress,
649 u_arraylist_t *senderInfoList,
650 CABLESenderInfo_t **senderInfo,
651 uint32_t *senderIndex)
653 VERIFY_NON_NULL_RET(leAddress,
655 "NULL BLE address argument",
656 CA_STATUS_INVALID_PARAM);
657 VERIFY_NON_NULL_RET(senderIndex,
659 "NULL index argument",
660 CA_STATUS_INVALID_PARAM);
662 const uint32_t listLength = u_arraylist_length(senderInfoList);
663 const uint32_t addrLength = strlen(leAddress);
664 for (uint32_t index = 0; index < listLength; index++)
666 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(senderInfoList, index);
667 if(!info || !(info->remoteEndpoint))
672 if(!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
674 *senderIndex = index;
683 return CA_STATUS_FAILED;
686 static void CALEDataReceiverHandler(void *threadData)
688 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDataReceiverHandler");
690 ca_mutex_lock(g_bleReceiveDataMutex);
692 if (g_dataBleReceiverHandlerState)
694 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
696 CALEData_t *bleData = (CALEData_t *) threadData;
699 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
700 ca_mutex_unlock(g_bleReceiveDataMutex);
704 if (!(bleData->senderInfo))
706 OIC_LOG(ERROR, CALEADAPTER_TAG, "sender info is not available");
707 ca_mutex_unlock(g_bleReceiveDataMutex);
711 if (!(bleData->remoteEndpoint))
713 OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!");
714 ca_mutex_unlock(g_bleReceiveDataMutex);
718 CABLESenderInfo_t *senderInfo = NULL;
719 uint32_t senderIndex = 0;
721 if(CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
723 &senderInfo, &senderIndex))
725 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s]",
726 bleData->remoteEndpoint->addr);
731 CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
734 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
735 ca_mutex_unlock(g_bleReceiveDataMutex);
738 newSender->recvDataLen = 0;
739 newSender->totalDataLen = 0;
740 newSender->defragData = NULL;
741 newSender->remoteEndpoint = NULL;
743 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
745 newSender->totalDataLen = coap_get_total_message_length(bleData->data, bleData->dataLen);
747 if(!(newSender->totalDataLen))
749 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
751 ca_mutex_unlock(g_bleReceiveDataMutex);
755 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
756 newSender->totalDataLen);
757 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
760 newSender->defragData = OICCalloc(newSender->totalDataLen + 1,
761 sizeof(*newSender->defragData));
763 if (NULL == newSender->defragData)
765 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
767 ca_mutex_unlock(g_bleReceiveDataMutex);
771 const char *remoteAddress = bleData->remoteEndpoint->addr;
772 newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
773 CA_ADAPTER_GATT_BTLE,
776 if (NULL == newSender->remoteEndpoint)
778 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
779 OICFree(newSender->defragData);
781 ca_mutex_unlock(g_bleReceiveDataMutex);
785 if (newSender->recvDataLen + bleData->dataLen > newSender->totalDataLen)
787 OIC_LOG(ERROR, CALEADAPTER_TAG, "buffer is smaller than received data");
788 OICFree(newSender->defragData);
789 CAFreeEndpoint(newSender->remoteEndpoint);
791 ca_mutex_unlock(g_bleReceiveDataMutex);
794 memcpy(newSender->defragData, bleData->data, bleData->dataLen);
795 newSender->recvDataLen += bleData->dataLen;
797 u_arraylist_add(bleData->senderInfo,(void *)newSender);
799 //Getting newSender index position in bleSenderInfo array list
801 CALEGetSenderInfo(newSender->remoteEndpoint->addr, bleData->senderInfo,
804 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
805 OICFree(newSender->defragData);
806 CAFreeEndpoint(newSender->remoteEndpoint);
808 ca_mutex_unlock(g_bleReceiveDataMutex);
811 senderInfo = newSender;
815 if(senderInfo->recvDataLen + bleData->dataLen > senderInfo->totalDataLen)
817 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
818 "Data Length exceeding error!! Receiving [%d] total length [%d]",
819 senderInfo->recvDataLen + bleData->dataLen, senderInfo->totalDataLen);
820 u_arraylist_remove(bleData->senderInfo, senderIndex);
821 OICFree(senderInfo->defragData);
823 ca_mutex_unlock(g_bleReceiveDataMutex);
826 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]",
828 memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data,
830 senderInfo->recvDataLen += bleData->dataLen ;
831 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] received Datalen [%d]",
832 senderInfo->totalDataLen, senderInfo->recvDataLen);
835 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
837 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
838 if (NULL == g_networkPacketReceivedCallback)
840 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
842 u_arraylist_remove(bleData->senderInfo, senderIndex);
843 OICFree(senderInfo->defragData);
845 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
846 ca_mutex_unlock(g_bleReceiveDataMutex);
850 OIC_LOG(DEBUG, CALEADAPTER_TAG, "[CALEDataReceiverHandler] Sending data up !");
852 const CASecureEndpoint_t tmp =
854 .endpoint = *senderInfo->remoteEndpoint
857 g_networkPacketReceivedCallback(&tmp,
858 senderInfo->defragData,
859 senderInfo->recvDataLen);
860 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
861 u_arraylist_remove(bleData->senderInfo, senderIndex);
862 senderInfo->remoteEndpoint = NULL;
863 senderInfo->defragData = NULL;
867 ca_mutex_unlock(g_bleReceiveDataMutex);
868 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
871 static void CALEServerSendDataThread(void *threadData)
873 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEServerSendDataThread");
875 CALEData_t * const bleData = (CALEData_t *) threadData;
878 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
882 const uint32_t totalLength = bleData->dataLen;
886 "Server total Data length with header is [%u]",
889 uint8_t * const dataSegment = OICCalloc(totalLength, 1);
891 if (NULL == dataSegment)
893 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
898 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
900 length = totalLength;
901 memcpy(dataSegment, bleData->data, bleData->dataLen);
905 length = CA_SUPPORTED_BLE_MTU_SIZE;
906 memcpy(dataSegment, bleData->data, CA_SUPPORTED_BLE_MTU_SIZE);
909 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
911 CAResult_t result = CA_STATUS_FAILED;
913 // Send the first segment with the header.
914 if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
916 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
918 result = CAUpdateCharacteristicsToGattClient(
919 bleData->remoteEndpoint->addr, dataSegment, length);
921 if (CA_STATUS_OK != result)
925 "Update characteristics failed, result [%d]",
928 g_errorHandler(bleData->remoteEndpoint,
932 OICFree(dataSegment);
938 "Server Sent data length [%u]",
940 for (index = 1; index < iter; index++)
942 // Send the remaining header.
945 "Sending the chunk number [%u]",
949 CAUpdateCharacteristicsToGattClient(
950 bleData->remoteEndpoint->addr,
951 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE)),
952 CA_SUPPORTED_BLE_MTU_SIZE);
954 if (CA_STATUS_OK != result)
956 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
957 "Update characteristics failed, result [%d]", result);
958 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
959 OICFree(dataSegment);
962 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
963 CA_SUPPORTED_BLE_MTU_SIZE);
966 const uint32_t remainingLen =
967 totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
969 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
971 // send the last segment of the data (Ex: 22 bytes of 622
972 // bytes of data when MTU is 200)
973 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
975 result = CAUpdateCharacteristicsToGattClient(
976 bleData->remoteEndpoint->addr,
977 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
980 if (CA_STATUS_OK != result)
984 "Update characteristics failed, result [%d]",
986 g_errorHandler(bleData->remoteEndpoint,
990 OICFree(dataSegment);
993 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
998 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
999 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
1000 if (CA_STATUS_OK != result)
1002 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1004 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1005 OICFree(dataSegment);
1008 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1009 for (index = 1; index < iter; index++)
1011 // Send the remaining header.
1012 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1014 result = CAUpdateCharacteristicsToAllGattClients(
1015 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE)),
1016 CA_SUPPORTED_BLE_MTU_SIZE);
1018 if (CA_STATUS_OK != result)
1020 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1022 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1023 OICFree(dataSegment);
1026 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%u]",
1027 CA_SUPPORTED_BLE_MTU_SIZE);
1030 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1031 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1033 // send the last segment of the data
1034 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1036 result = CAUpdateCharacteristicsToAllGattClients(
1037 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1040 if (CA_STATUS_OK != result)
1042 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1044 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1045 OICFree(dataSegment);
1048 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1051 OICFree(dataSegment);
1053 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CALEServerSendDataThread");
1056 static void CALEClientSendDataThread(void *threadData)
1058 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEClientSendDataThread");
1060 CALEData_t *bleData = (CALEData_t *) threadData;
1063 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1067 const uint32_t totalLength = bleData->dataLen;
1069 uint8_t *dataSegment = OICCalloc(totalLength, 1);
1070 if (NULL == dataSegment)
1072 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1076 uint32_t length = 0;
1077 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1079 length = totalLength;
1086 length = CA_SUPPORTED_BLE_MTU_SIZE;
1089 CA_SUPPORTED_BLE_MTU_SIZE);
1092 CAResult_t result = CA_STATUS_FAILED;
1093 const uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1095 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1097 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1098 // Send the first segment with the header.
1100 CAUpdateCharacteristicsToGattServer(
1101 bleData->remoteEndpoint->addr,
1107 if (CA_STATUS_OK != result)
1111 "Update characteristics failed, result [%d]",
1113 g_errorHandler(bleData->remoteEndpoint,
1117 OICFree(dataSegment);
1123 "Client Sent Data length is [%u]",
1126 for (index = 1; index < iter; index++)
1128 // Send the remaining header.
1129 result = CAUpdateCharacteristicsToGattServer(
1130 bleData->remoteEndpoint->addr,
1131 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1132 CA_SUPPORTED_BLE_MTU_SIZE,
1135 if (CA_STATUS_OK != result)
1139 "Update characteristics failed, result [%d]",
1141 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1142 OICFree(dataSegment);
1145 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1146 CA_SUPPORTED_BLE_MTU_SIZE);
1149 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1150 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1152 // send the last segment of the data (Ex: 22 bytes of 622
1153 // bytes of data when MTU is 200)
1154 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1156 result = CAUpdateCharacteristicsToGattServer(
1157 bleData->remoteEndpoint->addr,
1158 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1162 if (CA_STATUS_OK != result)
1164 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1166 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1167 OICFree(dataSegment);
1170 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1175 //Sending Mulitcast Data
1176 // Send the first segment with the header.
1177 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1178 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1179 if (CA_STATUS_OK != result)
1181 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1182 "Update characteristics (all) failed, result [%d]", result);
1183 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1184 OICFree(dataSegment);
1187 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1188 // Send the remaining header.
1189 for (index = 1; index < iter; index++)
1191 result = CAUpdateCharacteristicsToAllGattServers(
1192 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1193 CA_SUPPORTED_BLE_MTU_SIZE);
1195 if (CA_STATUS_OK != result)
1197 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1199 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1200 OICFree(dataSegment);
1203 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1204 CA_SUPPORTED_BLE_MTU_SIZE);
1207 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1208 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1210 // send the last segment of the data (Ex: 22 bytes of 622
1211 // bytes of data when MTU is 200)
1212 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1214 CAUpdateCharacteristicsToAllGattServers(
1215 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1218 if (CA_STATUS_OK != result)
1220 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1221 "Update characteristics (all) failed, result [%d]", result);
1222 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1223 OICFree(dataSegment);
1226 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1231 OICFree(dataSegment);
1233 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1236 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
1237 const uint8_t *data,
1238 uint32_t dataLength,
1239 u_arraylist_t *senderInfo)
1241 CALEData_t * const bleData = OICMalloc(sizeof(CALEData_t));
1245 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1249 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1250 bleData->data = OICCalloc(dataLength + 1, 1);
1252 if (NULL == bleData->data)
1254 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1255 CAFreeLEData(bleData);
1259 memcpy(bleData->data, data, dataLength);
1260 bleData->dataLen = dataLength;
1263 bleData->senderInfo = senderInfo;
1269 static void CAFreeLEData(CALEData_t *bleData)
1271 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1273 CAFreeEndpoint(bleData->remoteEndpoint);
1274 OICFree(bleData->data);
1278 static void CALEDataDestroyer(void *data, uint32_t size)
1280 if ((size_t)size < sizeof(CALEData_t *))
1282 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1283 "Destroy data too small %p %d", data, size);
1285 CALEData_t *ledata = (CALEData_t *) data;
1287 CAFreeLEData(ledata);
1291 static CAResult_t CAInitLEAdapterMutex()
1293 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEAdapterMutex");
1295 if (NULL == g_bleIsServerMutex)
1297 g_bleIsServerMutex = ca_mutex_new();
1298 if (NULL == g_bleIsServerMutex)
1300 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1301 return CA_STATUS_FAILED;
1305 if (NULL == g_bleNetworkCbMutex)
1307 g_bleNetworkCbMutex = ca_mutex_new();
1308 if (NULL == g_bleNetworkCbMutex)
1310 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1311 CATerminateLEAdapterMutex();
1312 return CA_STATUS_FAILED;
1316 if (NULL == g_bleLocalAddressMutex)
1318 g_bleLocalAddressMutex = ca_mutex_new();
1319 if (NULL == g_bleLocalAddressMutex)
1321 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1322 CATerminateLEAdapterMutex();
1323 return CA_STATUS_FAILED;
1327 if (NULL == g_bleAdapterThreadPoolMutex)
1329 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1330 if (NULL == g_bleAdapterThreadPoolMutex)
1332 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1333 CATerminateLEAdapterMutex();
1334 return CA_STATUS_FAILED;
1338 if (NULL == g_bleClientSendDataMutex)
1340 g_bleClientSendDataMutex = ca_mutex_new();
1341 if (NULL == g_bleClientSendDataMutex)
1343 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1344 CATerminateLEAdapterMutex();
1345 return CA_STATUS_FAILED;
1349 if (NULL == g_bleServerSendDataMutex)
1351 g_bleServerSendDataMutex = ca_mutex_new();
1352 if (NULL == g_bleServerSendDataMutex)
1354 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1355 CATerminateLEAdapterMutex();
1356 return CA_STATUS_FAILED;
1360 if (NULL == g_bleAdapterReqRespCbMutex)
1362 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1363 if (NULL == g_bleAdapterReqRespCbMutex)
1365 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1366 CATerminateLEAdapterMutex();
1367 return CA_STATUS_FAILED;
1371 if (NULL == g_bleReceiveDataMutex)
1373 g_bleReceiveDataMutex = ca_mutex_new();
1374 if (NULL == g_bleReceiveDataMutex)
1376 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1377 return CA_STATUS_FAILED;
1381 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1382 return CA_STATUS_OK;
1385 static void CATerminateLEAdapterMutex()
1387 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CATerminateLEAdapterMutex");
1389 ca_mutex_free(g_bleIsServerMutex);
1390 g_bleIsServerMutex = NULL;
1392 ca_mutex_free(g_bleNetworkCbMutex);
1393 g_bleNetworkCbMutex = NULL;
1395 ca_mutex_free(g_bleLocalAddressMutex);
1396 g_bleLocalAddressMutex = NULL;
1398 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1399 g_bleAdapterThreadPoolMutex = NULL;
1401 ca_mutex_free(g_bleClientSendDataMutex);
1402 g_bleClientSendDataMutex = NULL;
1404 ca_mutex_free(g_bleServerSendDataMutex);
1405 g_bleServerSendDataMutex = NULL;
1407 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1408 g_bleAdapterReqRespCbMutex = NULL;
1410 ca_mutex_free(g_bleReceiveDataMutex);
1411 g_bleReceiveDataMutex = NULL;
1413 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1417 * Starting LE connectivity adapters.
1419 * As its peer to peer it does not require to start any servers.
1421 * @return ::CA_STATUS_OK or Appropriate error code.
1423 static CAResult_t CAStartLE();
1426 * Start listening server for receiving multicast search requests.
1428 * Transport Specific Behavior:
1429 * LE Starts GATT Server with prefixed UUID and Characteristics
1430 * per OIC Specification.
1431 * @return ::CA_STATUS_OK or Appropriate error code.
1433 static CAResult_t CAStartLEListeningServer();
1436 * Stops listening server from receiving multicast search requests.
1438 * Transport Specific Behavior:
1439 * LE Starts GATT Server with prefixed UUID and Characteristics
1440 * per OIC Specification.
1441 * @return ::CA_STATUS_OK or Appropriate error code.
1443 static CAResult_t CAStopLEListeningServer();
1446 * Sarting discovery of servers for receiving multicast
1449 * Transport Specific Behavior:
1450 * LE Starts GATT Server with prefixed UUID and Characteristics
1451 * per OIC Specification.
1453 * @return ::CA_STATUS_OK or Appropriate error code
1455 static CAResult_t CAStartLEDiscoveryServer();
1458 * Send data to the endpoint using the adapter connectivity.
1460 * @param[in] endpoint Remote Endpoint information (like MAC address,
1461 * reference URI and connectivity type) to which
1462 * the unicast data has to be sent.
1463 * @param[in] data Data which required to be sent.
1464 * @param[in] dataLen Size of data to be sent.
1466 * @note dataLen must be > 0.
1468 * @return The number of bytes sent on the network, or -1 on error.
1470 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1475 * Send multicast data to the endpoint using the LE connectivity.
1477 * @param[in] endpoint Remote Endpoint information to which the
1478 * multicast data has to be sent.
1479 * @param[in] data Data which required to be sent.
1480 * @param[in] dataLen Size of data to be sent.
1482 * @note dataLen must be > 0.
1484 * @return The number of bytes sent on the network, or -1 on error.
1486 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1491 * Get LE Connectivity network information.
1493 * @param[out] info Local connectivity information structures.
1494 * @param[out] size Number of local connectivity structures.
1496 * @return ::CA_STATUS_OK or Appropriate error code.
1498 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info,
1502 * Read Synchronous API callback.
1504 * @return ::CA_STATUS_OK or Appropriate error code.
1506 static CAResult_t CAReadLEData();
1509 * Stopping the adapters and close socket connections.
1511 * LE Stops all GATT servers and GATT Clients.
1513 * @return ::CA_STATUS_OK or Appropriate error code.
1515 static CAResult_t CAStopLE();
1518 * Terminate the LE connectivity adapter.
1520 * Configuration information will be deleted from further use.
1522 static void CATerminateLE();
1525 * This function will receive the data from the GattServer and add the
1526 * data to the Server receiver queue.
1528 * @param[in] remoteAddress Remote address of the device from where
1530 * @param[in] data Actual data received from the remote
1532 * @param[in] dataLength Length of the data received from the
1534 * @param[in] sentLength Length of the data sent from the remote
1537 * @return ::CA_STATUS_OK or Appropriate error code.
1538 * @retval ::CA_STATUS_OK Successful.
1539 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1540 * @retval ::CA_STATUS_FAILED Operation failed.
1543 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
1544 const uint8_t *data,
1545 uint32_t dataLength,
1546 uint32_t *sentLength);
1549 * This function will receive the data from the GattClient and add the
1550 * data into the Client receiver queue.
1552 * @param[in] remoteAddress Remote address of the device from where
1554 * @param[in] data Actual data recevied from the remote
1556 * @param[in] dataLength Length of the data received from the
1558 * @param[in] sentLength Length of the data sent from the remote
1561 * @return ::CA_STATUS_OK or Appropriate error code.
1562 * @retval ::CA_STATUS_OK Successful.
1563 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1564 * @retval ::CA_STATUS_FAILED Operation failed.
1566 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
1567 const uint8_t *data,
1568 uint32_t dataLength,
1569 uint32_t *sentLength);
1572 * Set the NetworkPacket received callback to CA layer from adapter
1575 * @param[in] callback Callback handle sent from the upper layer.
1577 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
1580 * Push the data from CA layer to the Sender processor queue.
1582 * @param[in] remoteEndpoint Remote endpoint information of the
1584 * @param[in] data Data to be transmitted from LE.
1585 * @param[in] dataLen Length of the Data being transmitted.
1587 * @return ::CA_STATUS_OK or Appropriate error code.
1588 * @retval ::CA_STATUS_OK Successful.
1589 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1590 * @retval ::CA_STATUS_FAILED Operation failed.
1592 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1593 const uint8_t *data,
1597 * Push the data from CA layer to the Sender processor queue.
1599 * @param[in] remoteEndpoint Remote endpoint information of the
1601 * @param[in] data Data to be transmitted from LE.
1602 * @param[in] dataLen Length of the Data being transmitted.
1604 * @return ::CA_STATUS_OK or Appropriate error code.
1605 * @retval ::CA_STATUS_OK Successful.
1606 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1607 * @retval ::CA_STATUS_FAILED Operation failed.
1609 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1610 const uint8_t *data,
1613 static CAResult_t CALEAdapterGattServerStart()
1615 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1617 CAResult_t result = CAStartLEGattServer();
1619 #ifndef SINGLE_THREAD
1621 Don't start the server side sending queue thread until the
1622 server itself has actually started.
1624 if (CA_STATUS_OK == result)
1626 ca_mutex_lock(g_bleServerSendDataMutex);
1627 result = CAQueueingThreadStart(g_bleServerSendQueueHandle);
1628 ca_mutex_unlock(g_bleServerSendDataMutex);
1630 if (CA_STATUS_OK != result)
1634 "Unable to start server queuing thread (%d)",
1643 static CAResult_t CALEAdapterGattServerStop()
1645 #ifndef SINGLE_THREAD
1646 ca_mutex_lock(g_bleServerSendDataMutex);
1647 CAResult_t result = CAQueueingThreadStop(g_bleServerSendQueueHandle);
1648 ca_mutex_unlock(g_bleServerSendDataMutex);
1649 if (CA_STATUS_OK == result)
1651 result = CAStopLEGattServer();
1656 return CAStopLEGattServer();
1660 static CAResult_t CALEAdapterGattClientStart()
1662 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattClient");
1664 CAResult_t result = CAStartLEGattClient();
1666 #ifndef SINGLE_THREAD
1668 Don't start the client side sending queue thread until the
1669 client itself has actually started.
1671 if (CA_STATUS_OK == result)
1673 ca_mutex_lock(g_bleClientSendDataMutex);
1674 result = CAQueueingThreadStart(g_bleClientSendQueueHandle);
1675 ca_mutex_unlock(g_bleClientSendDataMutex);
1677 if (CA_STATUS_OK != result)
1681 "Unable to start client queuing thread");
1689 static CAResult_t CALEAdapterGattClientStop()
1691 #ifndef SINGLE_THREAD
1692 ca_mutex_lock(g_bleClientSendDataMutex);
1693 CAResult_t result = CAQueueingThreadStop(g_bleClientSendQueueHandle);
1694 ca_mutex_unlock(g_bleClientSendDataMutex);
1695 if (CA_STATUS_OK == result)
1697 CAStopLEGattClient();
1702 CAStopLEGattClient();
1704 return CA_STATUS_OK;
1708 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1709 CANetworkPacketReceivedCallback reqRespCallback,
1710 CANetworkChangeCallback netCallback,
1711 CAErrorHandleCallback errorCallback,
1712 ca_thread_pool_t handle)
1714 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1717 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1718 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1719 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1721 CAResult_t result = CA_STATUS_OK;
1722 result = CAInitLEAdapterMutex();
1723 if (CA_STATUS_OK != result)
1725 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1726 return CA_STATUS_FAILED;
1729 result = CAInitializeLENetworkMonitor();
1730 if (CA_STATUS_OK != result)
1732 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1733 return CA_STATUS_FAILED;
1735 CAInitializeLEAdapter(handle);
1737 CASetLEClientThreadPoolHandle(handle);
1739 result = CAInitializeLEGattClient();
1740 if (CA_STATUS_OK != result)
1742 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattClient() failed");
1743 return CA_STATUS_FAILED;
1746 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1747 CASetLEServerThreadPoolHandle(handle);
1748 result = CAInitializeLEGattServer();
1749 if (CA_STATUS_OK != result)
1751 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattServer() failed");
1752 return CA_STATUS_FAILED;
1755 CASetLEAdapterThreadPoolHandle(handle);
1756 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1757 CASetLEReqRespAdapterCallback(reqRespCallback);
1759 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1760 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1761 CALERegisterNetworkNotifications(netCallback);
1763 g_errorHandler = errorCallback;
1765 static const CAConnectivityHandler_t connHandler =
1767 .startAdapter = CAStartLE,
1768 .stopAdapter = CAStopLE,
1769 .startListenServer = CAStartLEListeningServer,
1770 .stopListenServer = CAStopLEListeningServer,
1771 .startDiscoveryServer = CAStartLEDiscoveryServer,
1772 .sendData = CASendLEUnicastData,
1773 .sendDataToAll = CASendLEMulticastData,
1774 .GetnetInfo = CAGetLEInterfaceInformation,
1775 .readData = CAReadLEData,
1776 .terminate = CATerminateLE,
1777 .cType = CA_ADAPTER_GATT_BTLE
1780 registerCallback(connHandler);
1782 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1784 return CA_STATUS_OK;
1787 static CAResult_t CAStartLE()
1789 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE");
1791 return CAStartLEAdapter();
1794 static CAResult_t CAStopLE()
1796 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1797 #ifndef SINGLE_THREAD
1801 ca_mutex_lock(g_bleIsServerMutex);
1802 switch (g_adapterType)
1804 case ADAPTER_SERVER:
1805 CALEAdapterGattServerStop();
1807 case ADAPTER_CLIENT:
1808 CALEAdapterGattClientStop();
1810 case ADAPTER_BOTH_CLIENT_SERVER:
1811 CALEAdapterGattServerStop();
1812 CALEAdapterGattClientStop();
1817 ca_mutex_unlock(g_bleIsServerMutex);
1819 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1821 return CAStopLEAdapter();
1824 static void CATerminateLE()
1826 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1828 CASetLEReqRespServerCallback(NULL);
1829 CASetLEReqRespClientCallback(NULL);
1830 CALERegisterNetworkNotifications(NULL);
1831 CASetLEReqRespAdapterCallback(NULL);
1832 CATerminateLENetworkMonitor();
1834 ca_mutex_lock(g_bleIsServerMutex);
1835 switch (g_adapterType)
1837 case ADAPTER_SERVER:
1838 CATerminateLEGattServer();
1840 case ADAPTER_CLIENT:
1841 CATerminateLEGattClient();
1843 case ADAPTER_BOTH_CLIENT_SERVER:
1844 CATerminateLEGattServer();
1845 CATerminateLEGattClient();
1850 g_adapterType = ADAPTER_EMPTY;
1851 ca_mutex_unlock(g_bleIsServerMutex);
1853 #ifndef SINGLE_THREAD
1854 CATerminateLEQueues();
1856 CATerminateLEAdapterMutex();
1858 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1861 static CAResult_t CAStartLEListeningServer()
1863 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEListeningServer");
1864 #ifndef ROUTING_GATEWAY
1865 CAResult_t result = CA_STATUS_OK;
1866 #ifndef SINGLE_THREAD
1867 result = CAInitLEServerQueues();
1868 if (CA_STATUS_OK != result)
1870 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1875 ca_mutex_lock(g_bleIsServerMutex);
1876 switch (g_adapterType)
1878 case ADAPTER_CLIENT:
1879 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1881 case ADAPTER_BOTH_CLIENT_SERVER:
1884 g_adapterType = ADAPTER_SERVER;
1886 ca_mutex_unlock(g_bleIsServerMutex);
1888 result = CAGetLEAdapterState();
1889 if (CA_STATUS_OK != result)
1891 if (CA_ADAPTER_NOT_ENABLED == result)
1895 "Listen Server will be started once BT Adapter is enabled");
1900 result = CALEAdapterGattServerStart();
1903 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1906 // Routing Gateway only supports BLE client mode.
1907 OIC_LOG(ERROR, CALEADAPTER_TAG, "LE server not supported in Routing Gateway");
1908 return CA_NOT_SUPPORTED;
1912 static CAResult_t CAStopLEListeningServer()
1914 OIC_LOG(ERROR, CALEADAPTER_TAG, "Listen server stop not supported.");
1915 return CA_NOT_SUPPORTED;
1918 static CAResult_t CAStartLEDiscoveryServer()
1920 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEDiscoveryServer");
1921 CAResult_t result = CA_STATUS_OK;
1922 #ifndef SINGLE_THREAD
1923 result = CAInitLEClientQueues();
1924 if (CA_STATUS_OK != result)
1926 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1931 ca_mutex_lock(g_bleIsServerMutex);
1932 switch (g_adapterType)
1934 case ADAPTER_SERVER:
1935 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1937 case ADAPTER_BOTH_CLIENT_SERVER:
1940 g_adapterType = ADAPTER_CLIENT;
1942 ca_mutex_unlock(g_bleIsServerMutex);
1944 result = CAGetLEAdapterState();
1945 if (CA_STATUS_OK != result)
1947 if (CA_ADAPTER_NOT_ENABLED == result)
1951 "Discovery Server will be started once BT Adapter is enabled");
1956 result = CALEAdapterGattClientStart();
1959 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1963 static CAResult_t CAReadLEData()
1965 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1966 #ifdef SINGLE_THREAD
1969 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1970 return CA_STATUS_OK;
1973 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1977 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEUnicastData");
1980 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1981 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1983 CAResult_t result = CA_STATUS_FAILED;
1985 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
1986 if (ADAPTER_EMPTY == g_adapterType)
1988 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
1991 ca_mutex_lock(g_bleIsServerMutex);
1992 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
1994 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1995 if (CA_STATUS_OK != result)
1997 ca_mutex_unlock(g_bleIsServerMutex);
1998 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed");
2001 g_errorHandler(endpoint, data, dataLen, result);
2008 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2010 result = CALEAdapterClientSendData(endpoint, data, dataLen);
2011 if (CA_STATUS_OK != result)
2013 ca_mutex_unlock(g_bleIsServerMutex);
2014 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for client failed" );
2018 g_errorHandler(endpoint, data, dataLen, result);
2023 ca_mutex_unlock(g_bleIsServerMutex);
2025 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2029 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
2033 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEMulticastData");
2036 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
2040 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
2044 CAResult_t result = CA_STATUS_FAILED;
2046 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
2047 if (ADAPTER_EMPTY == g_adapterType)
2049 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2052 ca_mutex_lock(g_bleIsServerMutex);
2053 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2055 result = CALEAdapterServerSendData(NULL, data, dataLen);
2056 if (CA_STATUS_OK != result)
2058 ca_mutex_unlock(g_bleIsServerMutex);
2060 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data for server failed" );
2064 g_errorHandler(endpoint, data, dataLen, result);
2070 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2072 result = CALEAdapterClientSendData(NULL, data, dataLen);
2073 if (CA_STATUS_OK != result)
2075 ca_mutex_unlock(g_bleIsServerMutex);
2077 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data for client failed" );
2081 g_errorHandler(endpoint, data, dataLen, result);
2086 ca_mutex_unlock(g_bleIsServerMutex);
2088 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CASendLEMulticastData");
2092 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
2094 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2096 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
2098 char *local_address = NULL;
2100 CAResult_t res = CAGetLEAddress(&local_address);
2101 if (CA_STATUS_OK != res)
2103 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
2107 if (NULL == local_address)
2109 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
2110 return CA_STATUS_FAILED;
2114 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
2115 if (NULL == (*info))
2117 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
2118 OICFree(local_address);
2119 return CA_STATUS_FAILED;
2122 size_t local_address_len = strlen(local_address);
2124 if(local_address_len >= sizeof(g_localBLEAddress) ||
2125 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
2127 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
2129 OICFree(local_address);
2130 return CA_STATUS_FAILED;
2133 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
2134 ca_mutex_lock(g_bleLocalAddressMutex);
2135 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
2136 ca_mutex_unlock(g_bleLocalAddressMutex);
2138 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
2140 OICFree(local_address);
2142 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2143 return CA_STATUS_OK;
2146 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
2148 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2150 ca_mutex_lock(g_bleNetworkCbMutex);
2151 g_networkCallback = netCallback;
2152 ca_mutex_unlock(g_bleNetworkCbMutex);
2153 CAResult_t res = CA_STATUS_OK;
2156 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
2157 if (CA_STATUS_OK != res)
2159 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2162 res = CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCb);
2163 if (CA_STATUS_OK != res)
2165 OIC_LOG(ERROR, CALEADAPTER_TAG, "CALEConnectionStateChangedCb failed!");
2170 res = CAUnSetLEAdapterStateChangedCb();
2171 if (CA_STATUS_OK != res)
2173 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2177 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2181 static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
2184 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEConnectionStateChangedCb");
2186 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2190 ca_mutex_lock(g_bleIsServerMutex);
2191 switch (g_adapterType)
2193 case ADAPTER_SERVER:
2194 CALEGattServerConnectionStateChanged(isConnected, address);
2196 case ADAPTER_CLIENT:
2197 CALEGattConnectionStateChanged(isConnected, address);
2199 case ADAPTER_BOTH_CLIENT_SERVER:
2200 CALEGattConnectionStateChanged(isConnected, address);
2201 CALEGattServerConnectionStateChanged(isConnected, address);
2206 ca_mutex_unlock(g_bleIsServerMutex);
2211 #ifndef SINGLE_THREAD
2212 if(g_bleClientSenderInfo)
2214 CABLESenderInfo_t *senderInfo = NULL;
2215 uint32_t senderIndex = 0;
2217 if(CA_STATUS_OK == CALEGetSenderInfo(address, g_bleClientSenderInfo, &senderInfo,
2220 u_arraylist_remove(g_bleClientSenderInfo, senderIndex);
2221 OICFree(senderInfo->defragData);
2222 OICFree(senderInfo->remoteEndpoint);
2223 OICFree(senderInfo);
2225 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo is removed for disconnection");
2229 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo doesn't exist");
2233 // remove data of send queue.
2234 if (g_bleClientSendQueueHandle)
2236 CALERemoveSendQueueData(g_bleClientSendQueueHandle,
2237 g_bleClientSendDataMutex,
2241 if (g_bleServerSendQueueHandle)
2243 CALERemoveSendQueueData(g_bleServerSendQueueHandle,
2244 g_bleServerSendDataMutex,
2250 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2253 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
2255 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDeviceStateChangedCb");
2257 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
2258 CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE };
2260 ca_mutex_lock(g_bleLocalAddressMutex);
2261 OICStrcpy(localEndpoint.addr,
2262 sizeof(localEndpoint.addr),
2264 ca_mutex_unlock(g_bleLocalAddressMutex);
2266 if (CA_ADAPTER_ENABLED == adapter_state)
2268 ca_mutex_lock(g_bleIsServerMutex);
2269 switch (g_adapterType)
2271 case ADAPTER_SERVER:
2272 CALEAdapterGattServerStart();
2274 case ADAPTER_CLIENT:
2275 CALEAdapterGattClientStart();
2277 case ADAPTER_BOTH_CLIENT_SERVER:
2278 CALEAdapterGattServerStart();
2279 CALEAdapterGattClientStart();
2284 ca_mutex_unlock(g_bleIsServerMutex);
2288 ca_mutex_lock(g_bleIsServerMutex);
2289 switch (g_adapterType)
2291 case ADAPTER_SERVER:
2292 CALEAdapterGattServerStop();
2294 case ADAPTER_CLIENT:
2295 CALEAdapterGattClientStop();
2297 case ADAPTER_BOTH_CLIENT_SERVER:
2298 CALEAdapterGattServerStop();
2299 CALEAdapterGattClientStop();
2304 ca_mutex_unlock(g_bleIsServerMutex);
2307 ca_mutex_lock(g_bleNetworkCbMutex);
2308 if (NULL != g_networkCallback)
2310 g_networkCallback(&localEndpoint, adapter_state);
2314 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
2316 ca_mutex_unlock(g_bleNetworkCbMutex);
2318 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2321 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
2322 const uint8_t *data,
2325 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2327 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2328 #ifndef SINGLE_THREAD
2329 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2330 "g_bleClientSendQueueHandle is NULL",
2332 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
2333 "g_bleClientSendDataMutex is NULL",
2336 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2337 "g_bleClientSendQueueHandle",
2340 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
2342 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2345 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2346 return CA_MEMORY_ALLOC_FAILED;
2348 // Add message to send queue
2349 ca_mutex_lock(g_bleClientSendDataMutex);
2350 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
2351 ca_mutex_unlock(g_bleClientSendDataMutex);
2353 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2354 return CA_STATUS_OK;
2357 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2358 const uint8_t *data,
2361 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2363 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2365 #ifdef SINGLE_THREAD
2366 if (!CAIsLEConnected())
2368 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2369 return CA_STATUS_FAILED;
2372 CAResult_t result = CA_STATUS_OK;
2373 const uint32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2374 for (uint32_t iter = 0; iter < dataLimit; iter++)
2377 CAUpdateCharacteristicsToAllGattClients(
2378 data + (iter * CA_SUPPORTED_BLE_MTU_SIZE),
2379 CA_SUPPORTED_BLE_MTU_SIZE);
2381 if (CA_STATUS_OK != result)
2383 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2384 return CA_STATUS_FAILED;
2390 const uint32_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2394 CAUpdateCharacteristicsToAllGattClients(
2395 data + (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE),
2397 if (CA_STATUS_OK != result)
2399 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2400 return CA_STATUS_FAILED;
2405 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2406 "BleClientReceiverQueue is NULL",
2408 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2409 "BleClientSendDataMutex is NULL",
2412 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2415 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2417 CALEData_t * const bleData =
2418 CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2422 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2423 return CA_MEMORY_ALLOC_FAILED;
2426 // Add message to send queue
2427 ca_mutex_lock(g_bleServerSendDataMutex);
2428 CAQueueingThreadAddData(g_bleServerSendQueueHandle,
2430 sizeof(CALEData_t));
2431 ca_mutex_unlock(g_bleServerSendDataMutex);
2433 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2434 return CA_STATUS_OK;
2437 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2438 const uint8_t *data,
2439 uint32_t dataLength,
2440 uint32_t *sentLength)
2442 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2445 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2446 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2448 #ifdef SINGLE_THREAD
2449 if(g_networkPacketReceivedCallback)
2451 // will be filled by upper layer
2452 const CASecureEndpoint_t endpoint =
2453 { .endpoint = { .adapter = CA_ADAPTER_GATT_BTLE } };
2456 g_networkPacketReceivedCallback(&endpoint, data, dataLength);
2459 VERIFY_NON_NULL_RET(g_bleReceiverQueue,
2461 "g_bleReceiverQueue",
2464 //Add message to data queue
2465 CAEndpoint_t * const remoteEndpoint =
2466 CACreateEndpointObject(CA_DEFAULT_FLAGS,
2467 CA_ADAPTER_GATT_BTLE,
2471 if (NULL == remoteEndpoint)
2473 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2474 return CA_STATUS_FAILED;
2477 // Create bleData to add to queue
2480 "Data received from LE layer [%d]",
2483 CALEData_t * const bleData =
2484 CACreateLEData(remoteEndpoint, data, dataLength, g_bleServerSenderInfo);
2488 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2489 CAFreeEndpoint(remoteEndpoint);
2490 return CA_MEMORY_ALLOC_FAILED;
2493 CAFreeEndpoint(remoteEndpoint);
2494 // Add message to receiver queue
2495 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2497 *sentLength = dataLength;
2499 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2500 return CA_STATUS_OK;
2503 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2504 const uint8_t *data,
2505 uint32_t dataLength,
2506 uint32_t *sentLength)
2508 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2511 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2512 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2513 #ifndef SINGLE_THREAD
2514 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG,
2515 "g_bleReceiverQueue",
2518 //Add message to data queue
2519 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2520 CA_ADAPTER_GATT_BTLE,
2522 if (NULL == remoteEndpoint)
2524 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2525 return CA_STATUS_FAILED;
2528 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%u]", dataLength);
2530 // Create bleData to add to queue
2531 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data,
2532 dataLength, g_bleClientSenderInfo);
2535 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2536 CAFreeEndpoint(remoteEndpoint);
2537 return CA_MEMORY_ALLOC_FAILED;
2540 CAFreeEndpoint(remoteEndpoint);
2541 // Add message to receiver queue
2542 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2544 *sentLength = dataLength;
2546 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2547 return CA_STATUS_OK;
2550 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2552 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2554 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2555 g_bleAdapterThreadPool = handle;
2556 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2558 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2561 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2563 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2565 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2567 g_networkPacketReceivedCallback = callback;
2569 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2571 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2574 static void CALEErrorHandler(const char *remoteAddress,
2575 const uint8_t *data,
2579 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2581 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2583 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2584 CA_ADAPTER_GATT_BTLE,
2588 // if required, will be used to build remote endpoint
2589 g_errorHandler(rep, data, dataLen, result);
2591 CAFreeEndpoint(rep);
2593 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");
2596 #ifndef SINGLE_THREAD
2597 static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle, ca_mutex mutex,
2598 const char* address)
2600 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALERemoveSendQueueData");
2602 VERIFY_NON_NULL_VOID(queueHandle, CALEADAPTER_TAG, "queueHandle");
2603 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2605 ca_mutex_lock(mutex);
2606 while (u_queue_get_size(queueHandle->dataQueue) > 0)
2608 OIC_LOG(DEBUG, CALEADAPTER_TAG, "get data from queue");
2609 u_queue_message_t *message = u_queue_get_element(queueHandle->dataQueue);
2610 if (NULL != message)
2612 CALEData_t *bleData = (CALEData_t *) message->msg;
2613 if (bleData && bleData->remoteEndpoint)
2615 if (!strcmp(bleData->remoteEndpoint->addr, address))
2617 OIC_LOG(DEBUG, CALEADAPTER_TAG, "found the message of disconnected device");
2618 if (NULL != queueHandle->destroy)
2620 queueHandle->destroy(message->msg, message->size);
2624 OICFree(message->msg);
2632 ca_mutex_unlock(mutex);