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 "cafragmentation.h"
33 #include "oic_malloc.h"
34 #include "oic_string.h"
35 #include "caremotehandler.h"
39 * Logging tag for module name.
41 #define CALEADAPTER_TAG "OIC_LE_ADAP"
44 * Stores information of all the senders.
46 * This structure will be used to track and defragment all incoming
52 uint32_t totalDataLen;
54 CAEndpoint_t *remoteEndpoint;
60 ADAPTER_BOTH_CLIENT_SERVER,
66 * Callback to provide the status of the network change to CA layer.
68 static CANetworkChangeCallback g_networkCallback = NULL;
71 * bleAddress of the local adapter. Value will be initialized to zero,
72 * and will be updated later.
74 static char g_localBLEAddress[18] = { 0 };
77 * Variable to differentiate btw GattServer and GattClient.
79 static CABLEAdapter_t g_adapterType = ADAPTER_EMPTY;
82 * Mutex to synchronize the task to be executed on the GattServer
85 static ca_mutex g_bleIsServerMutex = NULL;
88 * Mutex to synchronize the callback to be called for the network
91 static ca_mutex g_bleNetworkCbMutex = NULL;
94 * Mutex to synchronize the updates of the local LE address of the
97 static ca_mutex g_bleLocalAddressMutex = NULL;
100 * Reference to thread pool.
102 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
105 * Mutex to synchronize the task to be pushed to thread pool.
107 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
110 * Mutex to synchronize the queing of the data from SenderQueue.
112 static ca_mutex g_bleClientSendDataMutex = NULL;
115 * Mutex to synchronize the queing of the data from ReceiverQueue.
117 static ca_mutex g_bleReceiveDataMutex = NULL;
120 * Mutex to synchronize the queing of the data from SenderQueue.
122 static ca_mutex g_bleServerSendDataMutex = NULL;
125 * Mutex to synchronize the callback to be called for the
126 * adapterReqResponse.
128 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
131 * Callback to be called when network packet received from either
132 * GattServer or GattClient.
134 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
137 * Callback to notify error from the BLE adapter.
139 static CAErrorHandleCallback g_errorHandler = NULL;
142 * Register network change notification callback.
144 * @param[in] netCallback CANetworkChangeCallback callback which will
145 * be set for the change in network.
147 * @return 0 on success otherwise a positive error value.
148 * @retval ::CA_STATUS_OK Successful.
149 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
150 * @retval ::CA_STATUS_FAILED Operation failed.
153 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
156 * Set the thread pool handle which is required for spawning new
159 * @param[in] handle Thread pool handle which is given by above layer
160 * for using thread creation task.
163 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
166 * Call the callback to the upper layer when the adapter state gets
169 * @param[in] adapter_state New state of the adapter to be notified to
172 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state);
175 * Call the callback to the upper layer when the device connection state gets
178 * @param[in] address LE address of the device to be notified to the upper layer.
179 * @param[in] isConnected whether connection state is connected or not.
181 static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
185 * Used to initialize all required mutex variable for LE Adapter
188 * @return 0 on success otherwise a positive error value.
189 * @retval ::CA_STATUS_OK Successful.
190 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
191 * @retval ::CA_STATUS_FAILED Operation failed.
194 static CAResult_t CAInitLEAdapterMutex();
197 * Terminate all required mutex variables for LE adapter
200 static void CATerminateLEAdapterMutex();
203 * Prepares and notify error through error callback.
205 static void CALEErrorHandler(const char *remoteAddress,
210 #ifndef SINGLE_THREAD
212 * Stop condition of recvhandler.
214 static bool g_dataBleReceiverHandlerState = false;
217 * Sender information.
219 static u_arraylist_t *g_bleServerSenderInfo = NULL;
221 static u_arraylist_t *g_bleClientSenderInfo = NULL;
224 * Queue to process the outgoing packets from GATTClient.
226 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
229 * Queue to process the incoming packets.
231 static CAQueueingThread_t *g_bleReceiverQueue = NULL;
234 * Queue to process the outgoing packets from GATTServer.
236 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
239 * This function will be associated with the sender queue for
242 * This function will fragment the data to the MTU of the transport
243 * and send the data in fragments to the adapters. The function will
244 * be blocked until all data is sent out from the adapter.
246 * @param[in] threadData Data pushed to the queue which contains the
247 * info about RemoteEndpoint and Data.
249 static void CALEServerSendDataThread(void *threadData);
252 * This function will be associated with the sender queue for
255 * This function will fragment the data to the MTU of the transport
256 * and send the data in fragments to the adapters. The function will
257 * be blocked until all data is sent out from the adapter.
259 * @param[in] threadData Data pushed to the queue which contains the
260 * info about RemoteEndpoint and Data.
262 static void CALEClientSendDataThread(void *threadData);
265 * This function will be associated with the receiver queue.
267 * This function will defragment the received data from each sender
268 * respectively and will send it up to CA layer. Respective sender's
269 * header will provide the length of the data sent.
271 * @param[in] threadData Data pushed to the queue which contains the
272 * info about RemoteEndpoint and Data.
274 static void CALEDataReceiverHandler(void *threadData);
277 * This function will stop all queues created for GattServer and
278 * GattClient. All four queues will be be stopped with this function
281 static void CAStopLEQueues();
284 * This function will terminate all queues created for GattServer and
285 * GattClient. All four queues will be be terminated with this
286 * function invocations.
288 static void CATerminateLEQueues();
291 * This function will initalize the Receiver and Sender queues for
292 * GattServer. This function will in turn call the functions
293 * CAInitBleServerReceiverQueue() and CAInitBleServerSenderQueue() to
294 * initialize the queues.
296 * @return ::CA_STATUS_OK or Appropriate error code.
297 * @retval ::CA_STATUS_OK Successful.
298 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
299 * @retval ::CA_STATUS_FAILED Operation failed.
301 static CAResult_t CAInitLEServerQueues();
304 * This function will initalize the Receiver and Sender queues for
305 * GattClient. This function will inturn call the functions
306 * CAInitBleClientReceiverQueue() and CAInitBleClientSenderQueue() to
307 * initialize the queues.
309 * @return ::CA_STATUS_OK or Appropriate error code.
310 * @retval ::CA_STATUS_OK Successful.
311 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
312 * @retval ::CA_STATUS_FAILED Operation failed.
315 static CAResult_t CAInitLEClientQueues();
318 * This function will initalize the Receiver queue for
319 * GattServer. This will initialize the queue to process the function
320 * CABLEServerSendDataThread() when ever the task is added to this
323 * @return ::CA_STATUS_OK or Appropriate error code.
324 * @retval ::CA_STATUS_OK Successful.
325 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
326 * @retval ::CA_STATUS_FAILED Operation failed.
328 static CAResult_t CAInitLEServerSenderQueue();
331 * This function will initalize the Receiver queue for
332 * GattClient. This will initialize the queue to process the function
333 * CABLEClientSendDataThread() when ever the task is added to this
336 * @return ::CA_STATUS_OK or Appropriate error code.
337 * @retval ::CA_STATUS_OK Successful.
338 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
339 * @retval ::CA_STATUS_FAILED Operation failed.
341 static CAResult_t CAInitLEClientSenderQueue();
344 * This function will initialize the Receiver queue for
345 * LEAdapter. This will initialize the queue to process the function
346 * CABLEDataReceiverHandler() when ever the task is added to this
349 * @return ::CA_STATUS_OK or Appropriate error code
350 * @retval ::CA_STATUS_OK Successful
351 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
352 * @retval ::CA_STATUS_FAILED Operation failed
355 static CAResult_t CAInitLEReceiverQueue();
358 * This function will create the Data required to send it in the
361 * @param[in] remoteEndpoint Remote endpoint information of the
363 * @param[in] data Data to be transmitted from LE.
364 * @param[in] dataLength Length of the Data being transmitted.
366 * @return ::CA_STATUS_OK or Appropriate error code.
367 * @retval ::CA_STATUS_OK Successful.
368 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
369 * @retval ::CA_STATUS_FAILED Operation failed.
371 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
374 u_arraylist_t *senderInfo);
377 * Used to free the BLE information stored in the sender/receiver
380 * @param[in] bleData Information for a particular data segment.
382 static void CAFreeLEData(CALEData_t *bleData);
387 static void CALEDataDestroyer(void *data, uint32_t size);
389 static CAResult_t CAInitLEServerQueues()
391 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
393 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
395 CAResult_t result = CAInitLEServerSenderQueue();
396 if (CA_STATUS_OK != result)
398 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
399 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
400 return CA_STATUS_FAILED;
403 g_bleServerSenderInfo = u_arraylist_create();
404 if (!g_bleServerSenderInfo)
406 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
407 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
408 return CA_MEMORY_ALLOC_FAILED;
411 result = CAInitLEReceiverQueue();
412 if (CA_STATUS_OK != result)
414 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
415 u_arraylist_free(&g_bleServerSenderInfo);
416 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
417 return CA_STATUS_FAILED;
420 g_dataBleReceiverHandlerState = true;
422 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
424 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
428 static CAResult_t CAInitLEClientQueues()
430 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
432 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
434 CAResult_t result = CAInitLEClientSenderQueue();
435 if (CA_STATUS_OK != result)
437 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
438 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
439 return CA_STATUS_FAILED;
442 g_bleClientSenderInfo = u_arraylist_create();
443 if (!g_bleClientSenderInfo)
445 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
446 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
447 return CA_MEMORY_ALLOC_FAILED;
450 result = CAInitLEReceiverQueue();
451 if (CA_STATUS_OK != result)
453 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
454 u_arraylist_free(&g_bleClientSenderInfo);
455 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
456 return CA_STATUS_FAILED;
459 g_dataBleReceiverHandlerState = true;
461 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
463 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
467 static CAResult_t CAInitLEReceiverQueue()
469 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEReceiverQueue");
470 // Check if the message queue is already initialized
471 if (g_bleReceiverQueue)
473 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
477 // Create recv message queue
478 g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
479 if (!g_bleReceiverQueue)
481 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
482 return CA_MEMORY_ALLOC_FAILED;
485 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue,
486 g_bleAdapterThreadPool,
487 CALEDataReceiverHandler,
490 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
491 OICFree(g_bleReceiverQueue);
492 g_bleReceiverQueue = NULL;
493 return CA_STATUS_FAILED;
496 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue))
498 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
499 OICFree(g_bleReceiverQueue);
500 g_bleReceiverQueue = NULL;
501 return CA_STATUS_FAILED;
504 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
508 static CAResult_t CAInitLEServerSenderQueue()
510 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEServerSenderQueue");
511 // Check if the message queue is already initialized
512 if (g_bleServerSendQueueHandle)
514 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
518 // Create send message queue
519 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
520 if (!g_bleServerSendQueueHandle)
522 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
523 return CA_MEMORY_ALLOC_FAILED;
526 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
527 g_bleAdapterThreadPool,
528 CALEServerSendDataThread,
531 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
532 OICFree(g_bleServerSendQueueHandle);
533 g_bleServerSendQueueHandle = NULL;
534 return CA_STATUS_FAILED;
537 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
541 static void CALEClearSenderInfoImpl(u_arraylist_t ** list)
543 const size_t length = u_arraylist_length(*list);
544 for (size_t i = 0; i < length; ++i)
546 CABLESenderInfo_t * const info =
547 (CABLESenderInfo_t *) u_arraylist_get(*list, i);
550 OICFree(info->defragData);
551 CAFreeEndpoint(info->remoteEndpoint);
555 u_arraylist_free(list);
558 static void CALEClearSenderInfo()
560 CALEClearSenderInfoImpl(&g_bleServerSenderInfo);
561 CALEClearSenderInfoImpl(&g_bleClientSenderInfo);
564 static CAResult_t CAInitLEClientSenderQueue()
566 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEClientSenderQueue");
568 if (g_bleClientSendQueueHandle)
570 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
574 // Create send message queue
575 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
576 if (!g_bleClientSendQueueHandle)
578 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
579 return CA_MEMORY_ALLOC_FAILED;
582 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
583 g_bleAdapterThreadPool,
584 CALEClientSendDataThread, CALEDataDestroyer))
586 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
587 OICFree(g_bleClientSendQueueHandle);
588 g_bleClientSendQueueHandle = NULL;
589 return CA_STATUS_FAILED;
592 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAInitLEClientSenderQueue");
596 static void CAStopLEQueues()
598 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStopLEQueues");
600 ca_mutex_lock(g_bleReceiveDataMutex);
601 if (NULL != g_bleReceiverQueue)
603 CAQueueingThreadStop(g_bleReceiverQueue);
605 ca_mutex_unlock(g_bleReceiveDataMutex);
607 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAStopLEQueues");
610 static void CATerminateLEQueues()
612 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
614 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
615 OICFree(g_bleClientSendQueueHandle);
616 g_bleClientSendQueueHandle = NULL;
618 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
619 OICFree(g_bleServerSendQueueHandle);
620 g_bleServerSendQueueHandle = NULL;
622 CAQueueingThreadDestroy(g_bleReceiverQueue);
623 OICFree(g_bleReceiverQueue);
624 g_bleReceiverQueue = NULL;
626 CALEClearSenderInfo();
628 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
631 static CAResult_t CALEGetSenderInfo(const char *leAddress,
632 u_arraylist_t *senderInfoList,
633 CABLESenderInfo_t **senderInfo,
634 uint32_t *senderIndex)
636 VERIFY_NON_NULL_RET(leAddress,
638 "NULL BLE address argument",
639 CA_STATUS_INVALID_PARAM);
640 VERIFY_NON_NULL_RET(senderIndex,
642 "NULL index argument",
643 CA_STATUS_INVALID_PARAM);
645 const uint32_t listLength = u_arraylist_length(senderInfoList);
646 const uint32_t addrLength = strlen(leAddress);
647 for (uint32_t index = 0; index < listLength; index++)
649 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(senderInfoList, index);
650 if(!info || !(info->remoteEndpoint))
655 if(!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
657 *senderIndex = index;
666 return CA_STATUS_FAILED;
669 static void CALEDataReceiverHandler(void *threadData)
671 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDataReceiverHandler");
673 ca_mutex_lock(g_bleReceiveDataMutex);
675 if (g_dataBleReceiverHandlerState)
677 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
679 CALEData_t *bleData = (CALEData_t *) threadData;
682 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
683 ca_mutex_unlock(g_bleReceiveDataMutex);
687 if (!(bleData->senderInfo))
689 OIC_LOG(ERROR, CALEADAPTER_TAG, "sender info is not available");
690 ca_mutex_unlock(g_bleReceiveDataMutex);
694 if (!(bleData->remoteEndpoint))
696 OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!");
697 ca_mutex_unlock(g_bleReceiveDataMutex);
701 CABLESenderInfo_t *senderInfo = NULL;
702 uint32_t senderIndex = 0;
704 if(CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
706 &senderInfo, &senderIndex))
708 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s]",
709 bleData->remoteEndpoint->addr);
714 CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
717 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
718 ca_mutex_unlock(g_bleReceiveDataMutex);
721 newSender->recvDataLen = 0;
722 newSender->totalDataLen = 0;
723 newSender->defragData = NULL;
724 newSender->remoteEndpoint = NULL;
726 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
728 newSender->totalDataLen = coap_get_total_message_length(bleData->data, bleData->dataLen);
730 if(!(newSender->totalDataLen))
732 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
734 ca_mutex_unlock(g_bleReceiveDataMutex);
738 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
739 newSender->totalDataLen);
740 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
743 newSender->defragData = OICCalloc(newSender->totalDataLen + 1,
744 sizeof(*newSender->defragData));
746 if (NULL == newSender->defragData)
748 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
750 ca_mutex_unlock(g_bleReceiveDataMutex);
754 const char *remoteAddress = bleData->remoteEndpoint->addr;
755 newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
756 CA_ADAPTER_GATT_BTLE,
759 if (NULL == newSender->remoteEndpoint)
761 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
762 OICFree(newSender->defragData);
764 ca_mutex_unlock(g_bleReceiveDataMutex);
768 if (newSender->recvDataLen + bleData->dataLen > newSender->totalDataLen)
770 OIC_LOG(ERROR, CALEADAPTER_TAG, "buffer is smaller than received data");
771 OICFree(newSender->defragData);
772 CAFreeEndpoint(newSender->remoteEndpoint);
774 ca_mutex_unlock(g_bleReceiveDataMutex);
777 memcpy(newSender->defragData, bleData->data, bleData->dataLen);
778 newSender->recvDataLen += bleData->dataLen;
780 u_arraylist_add(bleData->senderInfo,(void *)newSender);
782 //Getting newSender index position in bleSenderInfo array list
784 CALEGetSenderInfo(newSender->remoteEndpoint->addr, bleData->senderInfo,
787 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
788 OICFree(newSender->defragData);
789 CAFreeEndpoint(newSender->remoteEndpoint);
791 ca_mutex_unlock(g_bleReceiveDataMutex);
794 senderInfo = newSender;
798 if(senderInfo->recvDataLen + bleData->dataLen > senderInfo->totalDataLen)
800 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
801 "Data Length exceeding error!! Receiving [%d] total length [%d]",
802 senderInfo->recvDataLen + bleData->dataLen, senderInfo->totalDataLen);
803 u_arraylist_remove(bleData->senderInfo, senderIndex);
804 OICFree(senderInfo->defragData);
806 ca_mutex_unlock(g_bleReceiveDataMutex);
809 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]",
811 memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data,
813 senderInfo->recvDataLen += bleData->dataLen ;
814 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] received Datalen [%d]",
815 senderInfo->totalDataLen, senderInfo->recvDataLen);
818 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
820 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
821 if (NULL == g_networkPacketReceivedCallback)
823 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
825 u_arraylist_remove(bleData->senderInfo, senderIndex);
826 OICFree(senderInfo->defragData);
828 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
829 ca_mutex_unlock(g_bleReceiveDataMutex);
833 OIC_LOG(DEBUG, CALEADAPTER_TAG, "[CALEDataReceiverHandler] Sending data up !");
835 const CASecureEndpoint_t tmp =
837 .endpoint = *senderInfo->remoteEndpoint
840 g_networkPacketReceivedCallback(&tmp,
841 senderInfo->defragData,
842 senderInfo->recvDataLen);
843 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
844 u_arraylist_remove(bleData->senderInfo, senderIndex);
845 senderInfo->remoteEndpoint = NULL;
846 senderInfo->defragData = NULL;
850 ca_mutex_unlock(g_bleReceiveDataMutex);
851 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
854 static void CALEServerSendDataThread(void *threadData)
856 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEServerSendDataThread");
858 CALEData_t * const bleData = (CALEData_t *) threadData;
861 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
865 const uint32_t totalLength = bleData->dataLen;
869 "Server total Data length with header is [%u]",
872 uint8_t * const dataSegment = OICCalloc(totalLength, 1);
874 if (NULL == dataSegment)
876 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
881 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
883 length = totalLength;
884 memcpy(dataSegment, bleData->data, bleData->dataLen);
888 length = CA_SUPPORTED_BLE_MTU_SIZE;
889 memcpy(dataSegment, bleData->data, CA_SUPPORTED_BLE_MTU_SIZE);
892 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
894 CAResult_t result = CA_STATUS_FAILED;
896 // Send the first segment with the header.
897 if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
899 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
901 result = CAUpdateCharacteristicsToGattClient(
902 bleData->remoteEndpoint->addr, dataSegment, length);
904 if (CA_STATUS_OK != result)
908 "Update characteristics failed, result [%d]",
911 g_errorHandler(bleData->remoteEndpoint,
915 OICFree(dataSegment);
921 "Server Sent data length [%u]",
923 for (index = 1; index < iter; index++)
925 // Send the remaining header.
928 "Sending the chunk number [%u]",
932 CAUpdateCharacteristicsToGattClient(
933 bleData->remoteEndpoint->addr,
934 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE)),
935 CA_SUPPORTED_BLE_MTU_SIZE);
937 if (CA_STATUS_OK != result)
939 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
940 "Update characteristics failed, result [%d]", result);
941 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
942 OICFree(dataSegment);
945 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
946 CA_SUPPORTED_BLE_MTU_SIZE);
949 const uint32_t remainingLen =
950 totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
952 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
954 // send the last segment of the data (Ex: 22 bytes of 622
955 // bytes of data when MTU is 200)
956 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
958 result = CAUpdateCharacteristicsToGattClient(
959 bleData->remoteEndpoint->addr,
960 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
963 if (CA_STATUS_OK != result)
967 "Update characteristics failed, result [%d]",
969 g_errorHandler(bleData->remoteEndpoint,
973 OICFree(dataSegment);
976 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
981 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
982 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
983 if (CA_STATUS_OK != result)
985 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
987 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
988 OICFree(dataSegment);
991 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
992 for (index = 1; index < iter; index++)
994 // Send the remaining header.
995 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
997 result = CAUpdateCharacteristicsToAllGattClients(
998 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE)),
999 CA_SUPPORTED_BLE_MTU_SIZE);
1001 if (CA_STATUS_OK != result)
1003 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1005 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1006 OICFree(dataSegment);
1009 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%u]",
1010 CA_SUPPORTED_BLE_MTU_SIZE);
1013 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1014 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1016 // send the last segment of the data
1017 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1019 result = CAUpdateCharacteristicsToAllGattClients(
1020 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1023 if (CA_STATUS_OK != result)
1025 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1027 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1028 OICFree(dataSegment);
1031 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1034 OICFree(dataSegment);
1036 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CALEServerSendDataThread");
1039 static void CALEClientSendDataThread(void *threadData)
1041 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEClientSendDataThread");
1043 CALEData_t *bleData = (CALEData_t *) threadData;
1046 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1050 const uint32_t totalLength = bleData->dataLen;
1052 uint8_t *dataSegment = OICCalloc(totalLength, 1);
1053 if (NULL == dataSegment)
1055 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1059 uint32_t length = 0;
1060 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1062 length = totalLength;
1069 length = CA_SUPPORTED_BLE_MTU_SIZE;
1072 CA_SUPPORTED_BLE_MTU_SIZE);
1075 CAResult_t result = CA_STATUS_FAILED;
1076 const uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1078 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1080 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1081 // Send the first segment with the header.
1083 CAUpdateCharacteristicsToGattServer(
1084 bleData->remoteEndpoint->addr,
1090 if (CA_STATUS_OK != result)
1094 "Update characteristics failed, result [%d]",
1096 g_errorHandler(bleData->remoteEndpoint,
1100 OICFree(dataSegment);
1106 "Client Sent Data length is [%u]",
1109 for (index = 1; index < iter; index++)
1111 // Send the remaining header.
1112 result = CAUpdateCharacteristicsToGattServer(
1113 bleData->remoteEndpoint->addr,
1114 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1115 CA_SUPPORTED_BLE_MTU_SIZE,
1118 if (CA_STATUS_OK != result)
1122 "Update characteristics failed, result [%d]",
1124 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1125 OICFree(dataSegment);
1128 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1129 CA_SUPPORTED_BLE_MTU_SIZE);
1132 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1133 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1135 // send the last segment of the data (Ex: 22 bytes of 622
1136 // bytes of data when MTU is 200)
1137 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1139 result = CAUpdateCharacteristicsToGattServer(
1140 bleData->remoteEndpoint->addr,
1141 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1145 if (CA_STATUS_OK != result)
1147 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1149 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1150 OICFree(dataSegment);
1153 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1158 //Sending Mulitcast Data
1159 // Send the first segment with the header.
1160 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1161 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1162 if (CA_STATUS_OK != result)
1164 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1165 "Update characteristics (all) failed, result [%d]", result);
1166 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1167 OICFree(dataSegment);
1170 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1171 // Send the remaining header.
1172 for (index = 1; index < iter; index++)
1174 result = CAUpdateCharacteristicsToAllGattServers(
1175 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1176 CA_SUPPORTED_BLE_MTU_SIZE);
1178 if (CA_STATUS_OK != result)
1180 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1182 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1183 OICFree(dataSegment);
1186 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1187 CA_SUPPORTED_BLE_MTU_SIZE);
1190 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1191 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1193 // send the last segment of the data (Ex: 22 bytes of 622
1194 // bytes of data when MTU is 200)
1195 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1197 CAUpdateCharacteristicsToAllGattServers(
1198 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1201 if (CA_STATUS_OK != result)
1203 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1204 "Update characteristics (all) failed, result [%d]", result);
1205 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1206 OICFree(dataSegment);
1209 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1214 OICFree(dataSegment);
1216 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1219 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
1220 const uint8_t *data,
1221 uint32_t dataLength,
1222 u_arraylist_t *senderInfo)
1224 CALEData_t * const bleData = OICMalloc(sizeof(CALEData_t));
1228 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1232 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1233 bleData->data = OICCalloc(dataLength + 1, 1);
1235 if (NULL == bleData->data)
1237 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1238 CAFreeLEData(bleData);
1242 memcpy(bleData->data, data, dataLength);
1243 bleData->dataLen = dataLength;
1246 bleData->senderInfo = senderInfo;
1252 static void CAFreeLEData(CALEData_t *bleData)
1254 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1256 CAFreeEndpoint(bleData->remoteEndpoint);
1257 OICFree(bleData->data);
1261 static void CALEDataDestroyer(void *data, uint32_t size)
1263 if ((size_t)size < sizeof(CALEData_t *))
1265 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1266 "Destroy data too small %p %d", data, size);
1268 CALEData_t *ledata = (CALEData_t *) data;
1270 CAFreeLEData(ledata);
1274 static CAResult_t CAInitLEAdapterMutex()
1276 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEAdapterMutex");
1278 if (NULL == g_bleIsServerMutex)
1280 g_bleIsServerMutex = ca_mutex_new();
1281 if (NULL == g_bleIsServerMutex)
1283 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1284 return CA_STATUS_FAILED;
1288 if (NULL == g_bleNetworkCbMutex)
1290 g_bleNetworkCbMutex = ca_mutex_new();
1291 if (NULL == g_bleNetworkCbMutex)
1293 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1294 CATerminateLEAdapterMutex();
1295 return CA_STATUS_FAILED;
1299 if (NULL == g_bleLocalAddressMutex)
1301 g_bleLocalAddressMutex = ca_mutex_new();
1302 if (NULL == g_bleLocalAddressMutex)
1304 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1305 CATerminateLEAdapterMutex();
1306 return CA_STATUS_FAILED;
1310 if (NULL == g_bleAdapterThreadPoolMutex)
1312 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1313 if (NULL == g_bleAdapterThreadPoolMutex)
1315 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1316 CATerminateLEAdapterMutex();
1317 return CA_STATUS_FAILED;
1321 if (NULL == g_bleClientSendDataMutex)
1323 g_bleClientSendDataMutex = ca_mutex_new();
1324 if (NULL == g_bleClientSendDataMutex)
1326 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1327 CATerminateLEAdapterMutex();
1328 return CA_STATUS_FAILED;
1332 if (NULL == g_bleServerSendDataMutex)
1334 g_bleServerSendDataMutex = ca_mutex_new();
1335 if (NULL == g_bleServerSendDataMutex)
1337 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1338 CATerminateLEAdapterMutex();
1339 return CA_STATUS_FAILED;
1343 if (NULL == g_bleAdapterReqRespCbMutex)
1345 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1346 if (NULL == g_bleAdapterReqRespCbMutex)
1348 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1349 CATerminateLEAdapterMutex();
1350 return CA_STATUS_FAILED;
1354 if (NULL == g_bleReceiveDataMutex)
1356 g_bleReceiveDataMutex = ca_mutex_new();
1357 if (NULL == g_bleReceiveDataMutex)
1359 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1360 return CA_STATUS_FAILED;
1364 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1365 return CA_STATUS_OK;
1368 static void CATerminateLEAdapterMutex()
1370 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CATerminateLEAdapterMutex");
1372 ca_mutex_free(g_bleIsServerMutex);
1373 g_bleIsServerMutex = NULL;
1375 ca_mutex_free(g_bleNetworkCbMutex);
1376 g_bleNetworkCbMutex = NULL;
1378 ca_mutex_free(g_bleLocalAddressMutex);
1379 g_bleLocalAddressMutex = NULL;
1381 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1382 g_bleAdapterThreadPoolMutex = NULL;
1384 ca_mutex_free(g_bleClientSendDataMutex);
1385 g_bleClientSendDataMutex = NULL;
1387 ca_mutex_free(g_bleServerSendDataMutex);
1388 g_bleServerSendDataMutex = NULL;
1390 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1391 g_bleAdapterReqRespCbMutex = NULL;
1393 ca_mutex_free(g_bleReceiveDataMutex);
1394 g_bleReceiveDataMutex = NULL;
1396 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1400 * Starting LE connectivity adapters.
1402 * As its peer to peer it does not require to start any servers.
1404 * @return ::CA_STATUS_OK or Appropriate error code.
1406 static CAResult_t CAStartLE();
1409 * Start listening server for receiving multicast search requests.
1411 * Transport Specific Behavior:
1412 * LE Starts GATT Server with prefixed UUID and Characteristics
1413 * per OIC Specification.
1414 * @return ::CA_STATUS_OK or Appropriate error code.
1416 static CAResult_t CAStartLEListeningServer();
1419 * Stops listening server from receiving multicast search requests.
1421 * Transport Specific Behavior:
1422 * LE Starts GATT Server with prefixed UUID and Characteristics
1423 * per OIC Specification.
1424 * @return ::CA_STATUS_OK or Appropriate error code.
1426 static CAResult_t CAStopLEListeningServer();
1429 * Sarting discovery of servers for receiving multicast
1432 * Transport Specific Behavior:
1433 * LE Starts GATT Server with prefixed UUID and Characteristics
1434 * per OIC Specification.
1436 * @return ::CA_STATUS_OK or Appropriate error code
1438 static CAResult_t CAStartLEDiscoveryServer();
1441 * Send data to the endpoint using the adapter connectivity.
1443 * @param[in] endpoint Remote Endpoint information (like MAC address,
1444 * reference URI and connectivity type) to which
1445 * the unicast data has to be sent.
1446 * @param[in] data Data which required to be sent.
1447 * @param[in] dataLen Size of data to be sent.
1449 * @note dataLen must be > 0.
1451 * @return The number of bytes sent on the network, or -1 on error.
1453 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1458 * Send multicast data to the endpoint using the LE connectivity.
1460 * @param[in] endpoint Remote Endpoint information to which the
1461 * multicast data has to be sent.
1462 * @param[in] data Data which required to be sent.
1463 * @param[in] dataLen Size of data to be sent.
1465 * @note dataLen must be > 0.
1467 * @return The number of bytes sent on the network, or -1 on error.
1469 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1474 * Get LE Connectivity network information.
1476 * @param[out] info Local connectivity information structures.
1477 * @param[out] size Number of local connectivity structures.
1479 * @return ::CA_STATUS_OK or Appropriate error code.
1481 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info,
1485 * Read Synchronous API callback.
1487 * @return ::CA_STATUS_OK or Appropriate error code.
1489 static CAResult_t CAReadLEData();
1492 * Stopping the adapters and close socket connections.
1494 * LE Stops all GATT servers and GATT Clients.
1496 * @return ::CA_STATUS_OK or Appropriate error code.
1498 static CAResult_t CAStopLE();
1501 * Terminate the LE connectivity adapter.
1503 * Configuration information will be deleted from further use.
1505 static void CATerminateLE();
1508 * This function will receive the data from the GattServer and add the
1509 * data to the Server receiver queue.
1511 * @param[in] remoteAddress Remote address of the device from where
1513 * @param[in] data Actual data received from the remote
1515 * @param[in] dataLength Length of the data received from the
1517 * @param[in] sentLength Length of the data sent from the remote
1520 * @return ::CA_STATUS_OK or Appropriate error code.
1521 * @retval ::CA_STATUS_OK Successful.
1522 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1523 * @retval ::CA_STATUS_FAILED Operation failed.
1526 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
1527 const uint8_t *data,
1528 uint32_t dataLength,
1529 uint32_t *sentLength);
1532 * This function will receive the data from the GattClient and add the
1533 * data into the Client receiver queue.
1535 * @param[in] remoteAddress Remote address of the device from where
1537 * @param[in] data Actual data recevied from the remote
1539 * @param[in] dataLength Length of the data received from the
1541 * @param[in] sentLength Length of the data sent from the remote
1544 * @return ::CA_STATUS_OK or Appropriate error code.
1545 * @retval ::CA_STATUS_OK Successful.
1546 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1547 * @retval ::CA_STATUS_FAILED Operation failed.
1549 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
1550 const uint8_t *data,
1551 uint32_t dataLength,
1552 uint32_t *sentLength);
1555 * Set the NetworkPacket received callback to CA layer from adapter
1558 * @param[in] callback Callback handle sent from the upper layer.
1560 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
1563 * Push the data from CA layer to the Sender processor queue.
1565 * @param[in] remoteEndpoint Remote endpoint information of the
1567 * @param[in] data Data to be transmitted from LE.
1568 * @param[in] dataLen Length of the Data being transmitted.
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 CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1576 const uint8_t *data,
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 CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1593 const uint8_t *data,
1596 static CAResult_t CALEAdapterGattServerStart()
1598 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1600 CAResult_t result = CAStartLEGattServer();
1602 #ifndef SINGLE_THREAD
1604 Don't start the server side sending queue thread until the
1605 server itself has actually started.
1607 if (CA_STATUS_OK == result)
1609 ca_mutex_lock(g_bleServerSendDataMutex);
1610 result = CAQueueingThreadStart(g_bleServerSendQueueHandle);
1611 ca_mutex_unlock(g_bleServerSendDataMutex);
1613 if (CA_STATUS_OK != result)
1617 "Unable to start server queuing thread (%d)",
1626 static CAResult_t CALEAdapterGattServerStop()
1628 #ifndef SINGLE_THREAD
1629 ca_mutex_lock(g_bleServerSendDataMutex);
1630 CAResult_t result = CAQueueingThreadStop(g_bleServerSendQueueHandle);
1631 ca_mutex_unlock(g_bleServerSendDataMutex);
1632 if (CA_STATUS_OK == result)
1634 result = CAStopLEGattServer();
1639 return CAStopLEGattServer();
1643 static CAResult_t CALEAdapterGattClientStart()
1645 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattClient");
1647 CAResult_t result = CAStartLEGattClient();
1649 #ifndef SINGLE_THREAD
1651 Don't start the client side sending queue thread until the
1652 client itself has actually started.
1654 if (CA_STATUS_OK == result)
1656 ca_mutex_lock(g_bleClientSendDataMutex);
1657 result = CAQueueingThreadStart(g_bleClientSendQueueHandle);
1658 ca_mutex_unlock(g_bleClientSendDataMutex);
1660 if (CA_STATUS_OK != result)
1664 "Unable to start client queuing thread");
1672 static CAResult_t CALEAdapterGattClientStop()
1674 #ifndef SINGLE_THREAD
1675 ca_mutex_lock(g_bleClientSendDataMutex);
1676 CAResult_t result = CAQueueingThreadStop(g_bleClientSendQueueHandle);
1677 ca_mutex_unlock(g_bleClientSendDataMutex);
1678 if (CA_STATUS_OK == result)
1680 CAStopLEGattClient();
1685 CAStopLEGattClient();
1687 return CA_STATUS_OK;
1691 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1692 CANetworkPacketReceivedCallback reqRespCallback,
1693 CANetworkChangeCallback netCallback,
1694 CAErrorHandleCallback errorCallback,
1695 ca_thread_pool_t handle)
1697 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1700 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1701 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1702 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1704 CAResult_t result = CA_STATUS_OK;
1705 result = CAInitLEAdapterMutex();
1706 if (CA_STATUS_OK != result)
1708 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1709 return CA_STATUS_FAILED;
1712 result = CAInitializeLENetworkMonitor();
1713 if (CA_STATUS_OK != result)
1715 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1716 return CA_STATUS_FAILED;
1718 CAInitializeLEAdapter(handle);
1720 CASetLEClientThreadPoolHandle(handle);
1722 result = CAInitializeLEGattClient();
1723 if (CA_STATUS_OK != result)
1725 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattClient() failed");
1726 return CA_STATUS_FAILED;
1729 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1730 CASetLEServerThreadPoolHandle(handle);
1731 result = CAInitializeLEGattServer();
1732 if (CA_STATUS_OK != result)
1734 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattServer() failed");
1735 return CA_STATUS_FAILED;
1738 CASetLEAdapterThreadPoolHandle(handle);
1739 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1740 CASetLEReqRespAdapterCallback(reqRespCallback);
1742 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1743 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1744 CALERegisterNetworkNotifications(netCallback);
1746 g_errorHandler = errorCallback;
1748 static const CAConnectivityHandler_t connHandler =
1750 .startAdapter = CAStartLE,
1751 .stopAdapter = CAStopLE,
1752 .startListenServer = CAStartLEListeningServer,
1753 .stopListenServer = CAStopLEListeningServer,
1754 .startDiscoveryServer = CAStartLEDiscoveryServer,
1755 .sendData = CASendLEUnicastData,
1756 .sendDataToAll = CASendLEMulticastData,
1757 .GetnetInfo = CAGetLEInterfaceInformation,
1758 .readData = CAReadLEData,
1759 .terminate = CATerminateLE,
1760 .cType = CA_ADAPTER_GATT_BTLE
1763 registerCallback(connHandler);
1765 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1767 return CA_STATUS_OK;
1770 static CAResult_t CAStartLE()
1772 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE");
1774 return CAStartLEAdapter();
1777 static CAResult_t CAStopLE()
1779 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1780 #ifndef SINGLE_THREAD
1784 ca_mutex_lock(g_bleIsServerMutex);
1785 switch (g_adapterType)
1787 case ADAPTER_SERVER:
1788 CALEAdapterGattServerStop();
1790 case ADAPTER_CLIENT:
1791 CALEAdapterGattClientStop();
1793 case ADAPTER_BOTH_CLIENT_SERVER:
1794 CALEAdapterGattServerStop();
1795 CALEAdapterGattClientStop();
1800 ca_mutex_unlock(g_bleIsServerMutex);
1802 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1804 return CAStopLEAdapter();
1807 static void CATerminateLE()
1809 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1811 CASetLEReqRespServerCallback(NULL);
1812 CASetLEReqRespClientCallback(NULL);
1813 CALERegisterNetworkNotifications(NULL);
1814 CASetLEReqRespAdapterCallback(NULL);
1815 CATerminateLENetworkMonitor();
1817 ca_mutex_lock(g_bleIsServerMutex);
1818 switch (g_adapterType)
1820 case ADAPTER_SERVER:
1821 CATerminateLEGattServer();
1823 case ADAPTER_CLIENT:
1824 CATerminateLEGattClient();
1826 case ADAPTER_BOTH_CLIENT_SERVER:
1827 CATerminateLEGattServer();
1828 CATerminateLEGattClient();
1833 g_adapterType = ADAPTER_EMPTY;
1834 ca_mutex_unlock(g_bleIsServerMutex);
1836 #ifndef SINGLE_THREAD
1837 CATerminateLEQueues();
1839 CATerminateLEAdapterMutex();
1841 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1844 static CAResult_t CAStartLEListeningServer()
1846 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEListeningServer");
1847 #ifndef ROUTING_GATEWAY
1848 CAResult_t result = CA_STATUS_OK;
1849 #ifndef SINGLE_THREAD
1850 result = CAInitLEServerQueues();
1851 if (CA_STATUS_OK != result)
1853 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1858 ca_mutex_lock(g_bleIsServerMutex);
1859 switch (g_adapterType)
1861 case ADAPTER_CLIENT:
1862 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1864 case ADAPTER_BOTH_CLIENT_SERVER:
1867 g_adapterType = ADAPTER_SERVER;
1869 ca_mutex_unlock(g_bleIsServerMutex);
1871 result = CAGetLEAdapterState();
1872 if (CA_STATUS_OK != result)
1874 if (CA_ADAPTER_NOT_ENABLED == result)
1878 "Listen Server will be started once BT Adapter is enabled");
1883 result = CALEAdapterGattServerStart();
1886 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1889 // Routing Gateway only supports BLE client mode.
1890 OIC_LOG(ERROR, CALEADAPTER_TAG, "LE server not supported in Routing Gateway");
1891 return CA_NOT_SUPPORTED;
1895 static CAResult_t CAStopLEListeningServer()
1897 OIC_LOG(ERROR, CALEADAPTER_TAG, "Listen server stop not supported.");
1898 return CA_NOT_SUPPORTED;
1901 static CAResult_t CAStartLEDiscoveryServer()
1903 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEDiscoveryServer");
1904 CAResult_t result = CA_STATUS_OK;
1905 #ifndef SINGLE_THREAD
1906 result = CAInitLEClientQueues();
1907 if (CA_STATUS_OK != result)
1909 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1914 ca_mutex_lock(g_bleIsServerMutex);
1915 switch (g_adapterType)
1917 case ADAPTER_SERVER:
1918 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1920 case ADAPTER_BOTH_CLIENT_SERVER:
1923 g_adapterType = ADAPTER_CLIENT;
1925 ca_mutex_unlock(g_bleIsServerMutex);
1927 result = CAGetLEAdapterState();
1928 if (CA_STATUS_OK != result)
1930 if (CA_ADAPTER_NOT_ENABLED == result)
1934 "Discovery Server will be started once BT Adapter is enabled");
1939 result = CALEAdapterGattClientStart();
1942 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1946 static CAResult_t CAReadLEData()
1948 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1949 #ifdef SINGLE_THREAD
1952 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1953 return CA_STATUS_OK;
1956 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1960 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEUnicastData");
1963 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1964 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1966 CAResult_t result = CA_STATUS_FAILED;
1968 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
1969 if (ADAPTER_EMPTY == g_adapterType)
1971 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
1974 ca_mutex_lock(g_bleIsServerMutex);
1975 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
1977 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1978 if (CA_STATUS_OK != result)
1980 ca_mutex_unlock(g_bleIsServerMutex);
1981 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed");
1984 g_errorHandler(endpoint, data, dataLen, result);
1991 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
1993 result = CALEAdapterClientSendData(endpoint, data, dataLen);
1994 if (CA_STATUS_OK != result)
1996 ca_mutex_unlock(g_bleIsServerMutex);
1997 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for client failed" );
2001 g_errorHandler(endpoint, data, dataLen, result);
2006 ca_mutex_unlock(g_bleIsServerMutex);
2008 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2012 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
2016 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEMulticastData");
2019 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
2023 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
2027 CAResult_t result = CA_STATUS_FAILED;
2029 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
2030 if (ADAPTER_EMPTY == g_adapterType)
2032 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2035 ca_mutex_lock(g_bleIsServerMutex);
2036 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2038 result = CALEAdapterServerSendData(NULL, data, dataLen);
2039 if (CA_STATUS_OK != result)
2041 ca_mutex_unlock(g_bleIsServerMutex);
2043 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data for server failed" );
2047 g_errorHandler(endpoint, data, dataLen, result);
2053 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2055 result = CALEAdapterClientSendData(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 client failed" );
2064 g_errorHandler(endpoint, data, dataLen, result);
2069 ca_mutex_unlock(g_bleIsServerMutex);
2071 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CASendLEMulticastData");
2075 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
2077 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2079 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
2081 char *local_address = NULL;
2083 CAResult_t res = CAGetLEAddress(&local_address);
2084 if (CA_STATUS_OK != res)
2086 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
2090 if (NULL == local_address)
2092 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
2093 return CA_STATUS_FAILED;
2097 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
2098 if (NULL == (*info))
2100 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
2101 OICFree(local_address);
2102 return CA_STATUS_FAILED;
2105 size_t local_address_len = strlen(local_address);
2107 if(local_address_len >= sizeof(g_localBLEAddress) ||
2108 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
2110 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
2112 OICFree(local_address);
2113 return CA_STATUS_FAILED;
2116 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
2117 ca_mutex_lock(g_bleLocalAddressMutex);
2118 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
2119 ca_mutex_unlock(g_bleLocalAddressMutex);
2121 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
2123 OICFree(local_address);
2125 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2126 return CA_STATUS_OK;
2129 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
2131 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2133 ca_mutex_lock(g_bleNetworkCbMutex);
2134 g_networkCallback = netCallback;
2135 ca_mutex_unlock(g_bleNetworkCbMutex);
2136 CAResult_t res = CA_STATUS_OK;
2139 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
2140 if (CA_STATUS_OK != res)
2142 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2145 res = CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCb);
2146 if (CA_STATUS_OK != res)
2148 OIC_LOG(ERROR, CALEADAPTER_TAG, "CALEConnectionStateChangedCb failed!");
2153 res = CAUnSetLEAdapterStateChangedCb();
2154 if (CA_STATUS_OK != res)
2156 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2160 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2164 static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
2167 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEConnectionStateChangedCb");
2169 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2173 ca_mutex_lock(g_bleIsServerMutex);
2174 switch (g_adapterType)
2176 case ADAPTER_SERVER:
2177 CALEGattServerConnectionStateChanged(isConnected, address);
2179 case ADAPTER_CLIENT:
2180 CALEGattConnectionStateChanged(isConnected, address);
2182 case ADAPTER_BOTH_CLIENT_SERVER:
2183 CALEGattConnectionStateChanged(isConnected, address);
2184 CALEGattServerConnectionStateChanged(isConnected, address);
2189 ca_mutex_unlock(g_bleIsServerMutex);
2194 #ifndef SINGLE_THREAD
2195 if(g_bleClientSenderInfo)
2197 CABLESenderInfo_t *senderInfo = NULL;
2198 uint32_t senderIndex = 0;
2200 if(CA_STATUS_OK == CALEGetSenderInfo(address, g_bleClientSenderInfo, &senderInfo,
2203 u_arraylist_remove(g_bleClientSenderInfo, senderIndex);
2204 OICFree(senderInfo->defragData);
2205 OICFree(senderInfo->remoteEndpoint);
2206 OICFree(senderInfo);
2208 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo is removed for disconnection");
2212 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo doesn't exist");
2216 if(g_bleServerSenderInfo)
2218 CABLESenderInfo_t *senderInfo = NULL;
2219 uint32_t senderIndex = 0;
2221 if(CA_STATUS_OK == CALEGetSenderInfo(address, g_bleServerSenderInfo, &senderInfo,
2224 u_arraylist_remove(g_bleServerSenderInfo, senderIndex);
2225 OICFree(senderInfo->defragData);
2226 OICFree(senderInfo->remoteEndpoint);
2227 OICFree(senderInfo);
2229 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo is removed for disconnection");
2233 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo doesn't exist");
2239 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2242 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
2244 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDeviceStateChangedCb");
2246 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
2247 CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE };
2249 ca_mutex_lock(g_bleLocalAddressMutex);
2250 OICStrcpy(localEndpoint.addr,
2251 sizeof(localEndpoint.addr),
2253 ca_mutex_unlock(g_bleLocalAddressMutex);
2255 if (CA_ADAPTER_ENABLED == adapter_state)
2257 ca_mutex_lock(g_bleIsServerMutex);
2258 switch (g_adapterType)
2260 case ADAPTER_SERVER:
2261 CALEAdapterGattServerStart();
2263 case ADAPTER_CLIENT:
2264 CALEAdapterGattClientStart();
2266 case ADAPTER_BOTH_CLIENT_SERVER:
2267 CALEAdapterGattServerStart();
2268 CALEAdapterGattClientStart();
2273 ca_mutex_unlock(g_bleIsServerMutex);
2277 ca_mutex_lock(g_bleIsServerMutex);
2278 switch (g_adapterType)
2280 case ADAPTER_SERVER:
2281 CALEAdapterGattServerStop();
2283 case ADAPTER_CLIENT:
2284 CALEAdapterGattClientStop();
2286 case ADAPTER_BOTH_CLIENT_SERVER:
2287 CALEAdapterGattServerStop();
2288 CALEAdapterGattClientStop();
2293 ca_mutex_unlock(g_bleIsServerMutex);
2296 ca_mutex_lock(g_bleNetworkCbMutex);
2297 if (NULL != g_networkCallback)
2299 g_networkCallback(&localEndpoint, adapter_state);
2303 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
2305 ca_mutex_unlock(g_bleNetworkCbMutex);
2307 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2310 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
2311 const uint8_t *data,
2314 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2316 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2317 #ifndef SINGLE_THREAD
2318 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2319 "g_bleClientSendQueueHandle is NULL",
2321 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
2322 "g_bleClientSendDataMutex is NULL",
2325 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2326 "g_bleClientSendQueueHandle",
2329 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
2331 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2334 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2335 return CA_MEMORY_ALLOC_FAILED;
2337 // Add message to send queue
2338 ca_mutex_lock(g_bleClientSendDataMutex);
2339 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
2340 ca_mutex_unlock(g_bleClientSendDataMutex);
2342 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2343 return CA_STATUS_OK;
2346 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2347 const uint8_t *data,
2350 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2352 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2354 #ifdef SINGLE_THREAD
2355 if (!CAIsLEConnected())
2357 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2358 return CA_STATUS_FAILED;
2361 CAResult_t result = CA_STATUS_OK;
2362 const uint32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2363 for (uint32_t iter = 0; iter < dataLimit; iter++)
2366 CAUpdateCharacteristicsToAllGattClients(
2367 data + (iter * CA_SUPPORTED_BLE_MTU_SIZE),
2368 CA_SUPPORTED_BLE_MTU_SIZE);
2370 if (CA_STATUS_OK != result)
2372 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2373 return CA_STATUS_FAILED;
2379 const uint32_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2383 CAUpdateCharacteristicsToAllGattClients(
2384 data + (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE),
2386 if (CA_STATUS_OK != result)
2388 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2389 return CA_STATUS_FAILED;
2394 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2395 "BleClientReceiverQueue is NULL",
2397 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2398 "BleClientSendDataMutex is NULL",
2401 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2404 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2406 CALEData_t * const bleData =
2407 CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2411 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2412 return CA_MEMORY_ALLOC_FAILED;
2415 // Add message to send queue
2416 ca_mutex_lock(g_bleServerSendDataMutex);
2417 CAQueueingThreadAddData(g_bleServerSendQueueHandle,
2419 sizeof(CALEData_t));
2420 ca_mutex_unlock(g_bleServerSendDataMutex);
2422 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2423 return CA_STATUS_OK;
2426 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2427 const uint8_t *data,
2428 uint32_t dataLength,
2429 uint32_t *sentLength)
2431 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2434 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2435 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2437 #ifdef SINGLE_THREAD
2438 if(g_networkPacketReceivedCallback)
2440 // will be filled by upper layer
2441 const CASecureEndpoint_t endpoint =
2442 { .endpoint = { .adapter = CA_ADAPTER_GATT_BTLE } };
2445 g_networkPacketReceivedCallback(&endpoint, data, dataLength);
2448 VERIFY_NON_NULL_RET(g_bleReceiverQueue,
2450 "g_bleReceiverQueue",
2453 //Add message to data queue
2454 CAEndpoint_t * const remoteEndpoint =
2455 CACreateEndpointObject(CA_DEFAULT_FLAGS,
2456 CA_ADAPTER_GATT_BTLE,
2460 if (NULL == remoteEndpoint)
2462 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2463 return CA_STATUS_FAILED;
2466 // Create bleData to add to queue
2469 "Data received from LE layer [%d]",
2472 CALEData_t * const bleData =
2473 CACreateLEData(remoteEndpoint, data, dataLength, g_bleServerSenderInfo);
2477 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2478 CAFreeEndpoint(remoteEndpoint);
2479 return CA_MEMORY_ALLOC_FAILED;
2482 CAFreeEndpoint(remoteEndpoint);
2483 // Add message to receiver queue
2484 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2486 *sentLength = dataLength;
2488 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2489 return CA_STATUS_OK;
2492 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2493 const uint8_t *data,
2494 uint32_t dataLength,
2495 uint32_t *sentLength)
2497 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2500 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2501 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2502 #ifndef SINGLE_THREAD
2503 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG,
2504 "g_bleReceiverQueue",
2507 //Add message to data queue
2508 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2509 CA_ADAPTER_GATT_BTLE,
2511 if (NULL == remoteEndpoint)
2513 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2514 return CA_STATUS_FAILED;
2517 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%u]", dataLength);
2519 // Create bleData to add to queue
2520 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data,
2521 dataLength, g_bleClientSenderInfo);
2524 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2525 CAFreeEndpoint(remoteEndpoint);
2526 return CA_MEMORY_ALLOC_FAILED;
2529 CAFreeEndpoint(remoteEndpoint);
2530 // Add message to receiver queue
2531 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2533 *sentLength = dataLength;
2535 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2536 return CA_STATUS_OK;
2539 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2541 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2543 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2544 g_bleAdapterThreadPool = handle;
2545 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2547 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2550 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2552 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2554 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2556 g_networkPacketReceivedCallback = callback;
2558 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2560 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2563 static void CALEErrorHandler(const char *remoteAddress,
2564 const uint8_t *data,
2568 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2570 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2572 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2573 CA_ADAPTER_GATT_BTLE,
2577 // if required, will be used to build remote endpoint
2578 g_errorHandler(rep, data, dataLen, result);
2580 CAFreeEndpoint(rep);
2582 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");