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 device 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 * Used to initialize all required mutex variable for LE Adapter
178 * @return 0 on success otherwise a positive error value.
179 * @retval ::CA_STATUS_OK Successful.
180 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
181 * @retval ::CA_STATUS_FAILED Operation failed.
184 static CAResult_t CAInitLEAdapterMutex();
187 * Terminate all required mutex variables for LE adapter
190 static void CATerminateLEAdapterMutex();
193 * Prepares and notify error through error callback.
195 static void CALEErrorHandler(const char *remoteAddress,
200 #ifndef SINGLE_THREAD
202 * Stop condition of recvhandler.
204 static bool g_dataBleReceiverHandlerState = false;
207 * Sender information.
209 static u_arraylist_t *g_bleServerSenderInfo = NULL;
211 static u_arraylist_t *g_bleClientSenderInfo = NULL;
214 * Queue to process the outgoing packets from GATTClient.
216 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
219 * Queue to process the incoming packets.
221 static CAQueueingThread_t *g_bleReceiverQueue = NULL;
224 * Queue to process the outgoing packets from GATTServer.
226 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
229 * This function will be associated with the sender queue for
232 * This function will fragment the data to the MTU of the transport
233 * and send the data in fragments to the adapters. The function will
234 * be blocked until all data is sent out from the adapter.
236 * @param[in] threadData Data pushed to the queue which contains the
237 * info about RemoteEndpoint and Data.
239 static void CALEServerSendDataThread(void *threadData);
242 * This function will be associated with the sender queue for
245 * This function will fragment the data to the MTU of the transport
246 * and send the data in fragments to the adapters. The function will
247 * be blocked until all data is sent out from the adapter.
249 * @param[in] threadData Data pushed to the queue which contains the
250 * info about RemoteEndpoint and Data.
252 static void CALEClientSendDataThread(void *threadData);
255 * This function will be associated with the receiver queue.
257 * This function will defragment the received data from each sender
258 * respectively and will send it up to CA layer. Respective sender's
259 * header will provide the length of the data sent.
261 * @param[in] threadData Data pushed to the queue which contains the
262 * info about RemoteEndpoint and Data.
264 static void CALEDataReceiverHandler(void *threadData);
267 * This function will stop all queues created for GattServer and
268 * GattClient. All four queues will be be stopped with this function
271 static void CAStopLEQueues();
274 * This function will terminate all queues created for GattServer and
275 * GattClient. All four queues will be be terminated with this
276 * function invocations.
278 static void CATerminateLEQueues();
281 * This function will initalize the Receiver and Sender queues for
282 * GattServer. This function will in turn call the functions
283 * CAInitBleServerReceiverQueue() and CAInitBleServerSenderQueue() to
284 * initialize the queues.
286 * @return ::CA_STATUS_OK or Appropriate error code.
287 * @retval ::CA_STATUS_OK Successful.
288 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
289 * @retval ::CA_STATUS_FAILED Operation failed.
291 static CAResult_t CAInitLEServerQueues();
294 * This function will initalize the Receiver and Sender queues for
295 * GattClient. This function will inturn call the functions
296 * CAInitBleClientReceiverQueue() and CAInitBleClientSenderQueue() to
297 * initialize the queues.
299 * @return ::CA_STATUS_OK or Appropriate error code.
300 * @retval ::CA_STATUS_OK Successful.
301 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
302 * @retval ::CA_STATUS_FAILED Operation failed.
305 static CAResult_t CAInitLEClientQueues();
308 * This function will initalize the Receiver queue for
309 * GattServer. This will initialize the queue to process the function
310 * CABLEServerSendDataThread() when ever the task is added to this
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.
318 static CAResult_t CAInitLEServerSenderQueue();
321 * This function will initalize the Receiver queue for
322 * GattClient. This will initialize the queue to process the function
323 * CABLEClientSendDataThread() when ever the task is added to this
326 * @return ::CA_STATUS_OK or Appropriate error code.
327 * @retval ::CA_STATUS_OK Successful.
328 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
329 * @retval ::CA_STATUS_FAILED Operation failed.
331 static CAResult_t CAInitLEClientSenderQueue();
334 * This function will initialize the Receiver queue for
335 * LEAdapter. This will initialize the queue to process the function
336 * CABLEDataReceiverHandler() when ever the task is added to this
339 * @return ::CA_STATUS_OK or Appropriate error code
340 * @retval ::CA_STATUS_OK Successful
341 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
342 * @retval ::CA_STATUS_FAILED Operation failed
345 static CAResult_t CAInitLEReceiverQueue();
348 * This function will create the Data required to send it in the
351 * @param[in] remoteEndpoint Remote endpoint information of the
353 * @param[in] data Data to be transmitted from LE.
354 * @param[in] dataLength Length of the Data being transmitted.
356 * @return ::CA_STATUS_OK or Appropriate error code.
357 * @retval ::CA_STATUS_OK Successful.
358 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
359 * @retval ::CA_STATUS_FAILED Operation failed.
361 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
364 u_arraylist_t *senderInfo);
367 * Used to free the BLE information stored in the sender/receiver
370 * @param[in] bleData Information for a particular data segment.
372 static void CAFreeLEData(CALEData_t *bleData);
377 static void CALEDataDestroyer(void *data, uint32_t size);
379 static CAResult_t CAInitLEServerQueues()
381 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
383 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
385 CAResult_t result = CAInitLEServerSenderQueue();
386 if (CA_STATUS_OK != result)
388 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
389 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
390 return CA_STATUS_FAILED;
393 g_bleServerSenderInfo = u_arraylist_create();
394 if (!g_bleServerSenderInfo)
396 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
397 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
398 return CA_MEMORY_ALLOC_FAILED;
401 result = CAInitLEReceiverQueue();
402 if (CA_STATUS_OK != result)
404 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
405 u_arraylist_free(&g_bleServerSenderInfo);
406 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
407 return CA_STATUS_FAILED;
410 g_dataBleReceiverHandlerState = true;
412 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
414 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
418 static CAResult_t CAInitLEClientQueues()
420 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
422 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
424 CAResult_t result = CAInitLEClientSenderQueue();
425 if (CA_STATUS_OK != result)
427 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
428 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
429 return CA_STATUS_FAILED;
432 g_bleClientSenderInfo = u_arraylist_create();
433 if (!g_bleClientSenderInfo)
435 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
436 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
437 return CA_MEMORY_ALLOC_FAILED;
440 result = CAInitLEReceiverQueue();
441 if (CA_STATUS_OK != result)
443 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
444 u_arraylist_free(&g_bleClientSenderInfo);
445 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
446 return CA_STATUS_FAILED;
449 g_dataBleReceiverHandlerState = true;
451 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
453 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
457 static CAResult_t CAInitLEReceiverQueue()
459 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEReceiverQueue");
460 // Check if the message queue is already initialized
461 if (g_bleReceiverQueue)
463 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
467 // Create recv message queue
468 g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
469 if (!g_bleReceiverQueue)
471 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
472 return CA_MEMORY_ALLOC_FAILED;
475 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue,
476 g_bleAdapterThreadPool,
477 CALEDataReceiverHandler,
480 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
481 OICFree(g_bleReceiverQueue);
482 g_bleReceiverQueue = NULL;
483 return CA_STATUS_FAILED;
486 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue))
488 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
489 OICFree(g_bleReceiverQueue);
490 g_bleReceiverQueue = NULL;
491 return CA_STATUS_FAILED;
494 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
498 static CAResult_t CAInitLEServerSenderQueue()
500 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEServerSenderQueue");
501 // Check if the message queue is already initialized
502 if (g_bleServerSendQueueHandle)
504 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
508 // Create send message queue
509 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
510 if (!g_bleServerSendQueueHandle)
512 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
513 return CA_MEMORY_ALLOC_FAILED;
516 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
517 g_bleAdapterThreadPool,
518 CALEServerSendDataThread,
521 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
522 OICFree(g_bleServerSendQueueHandle);
523 g_bleServerSendQueueHandle = NULL;
524 return CA_STATUS_FAILED;
527 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
531 static void CALEClearSenderInfoImpl(u_arraylist_t ** list)
533 const size_t length = u_arraylist_length(*list);
534 for (size_t i = 0; i < length; ++i)
536 CABLESenderInfo_t * const info =
537 (CABLESenderInfo_t *) u_arraylist_get(*list, i);
540 OICFree(info->defragData);
541 CAFreeEndpoint(info->remoteEndpoint);
545 u_arraylist_free(list);
548 static void CALEClearSenderInfo()
550 CALEClearSenderInfoImpl(&g_bleServerSenderInfo);
551 CALEClearSenderInfoImpl(&g_bleClientSenderInfo);
554 static size_t CAGetMessageLengthFromData(const unsigned char *recvBuffer)
556 coap_transport_type transport = coap_get_tcp_header_type_from_initbyte(
557 ((unsigned char *)recvBuffer)[0] >> 4);
558 size_t optPaylaodLen = coap_get_length_from_header((unsigned char *)recvBuffer,
560 size_t headerLen = coap_get_tcp_header_length((unsigned char *)recvBuffer);
562 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "option/paylaod length [%d]", optPaylaodLen);
563 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "header length [%d]", headerLen);
564 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "total data length [%d]", headerLen + optPaylaodLen);
566 return headerLen + optPaylaodLen;
569 static CAResult_t CAInitLEClientSenderQueue()
571 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEClientSenderQueue");
573 if (g_bleClientSendQueueHandle)
575 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
579 // Create send message queue
580 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
581 if (!g_bleClientSendQueueHandle)
583 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
584 return CA_MEMORY_ALLOC_FAILED;
587 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
588 g_bleAdapterThreadPool,
589 CALEClientSendDataThread, CALEDataDestroyer))
591 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
592 OICFree(g_bleClientSendQueueHandle);
593 g_bleClientSendQueueHandle = NULL;
594 return CA_STATUS_FAILED;
597 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAInitLEClientSenderQueue");
601 static void CAStopLEQueues()
603 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStopLEQueues");
605 ca_mutex_lock(g_bleReceiveDataMutex);
606 if (NULL != g_bleReceiverQueue)
608 CAQueueingThreadStop(g_bleReceiverQueue);
610 ca_mutex_unlock(g_bleReceiveDataMutex);
612 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAStopLEQueues");
615 static void CATerminateLEQueues()
617 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
619 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
620 OICFree(g_bleClientSendQueueHandle);
621 g_bleClientSendQueueHandle = NULL;
623 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
624 OICFree(g_bleServerSendQueueHandle);
625 g_bleServerSendQueueHandle = NULL;
627 CAQueueingThreadDestroy(g_bleReceiverQueue);
628 OICFree(g_bleReceiverQueue);
629 g_bleReceiverQueue = NULL;
631 CALEClearSenderInfo();
633 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
636 static CAResult_t CALEGetSenderInfo(const char *leAddress,
637 u_arraylist_t *senderInfoList,
638 CABLESenderInfo_t **senderInfo,
639 uint32_t *senderIndex)
641 VERIFY_NON_NULL_RET(leAddress,
643 "NULL BLE address argument",
644 CA_STATUS_INVALID_PARAM);
645 VERIFY_NON_NULL_RET(senderIndex,
647 "NULL index argument",
648 CA_STATUS_INVALID_PARAM);
650 const uint32_t listLength = u_arraylist_length(senderInfoList);
651 const uint32_t addrLength = strlen(leAddress);
652 for (uint32_t index = 0; index < listLength; index++)
654 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(senderInfoList, index);
655 if(!info || !(info->remoteEndpoint))
660 if(!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
662 *senderIndex = index;
671 return CA_STATUS_FAILED;
674 static void CALEDataReceiverHandler(void *threadData)
676 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDataReceiverHandler");
678 ca_mutex_lock(g_bleReceiveDataMutex);
680 if (g_dataBleReceiverHandlerState)
682 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
684 CALEData_t *bleData = (CALEData_t *) threadData;
687 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
688 ca_mutex_unlock(g_bleReceiveDataMutex);
692 if (!(bleData->senderInfo))
694 OIC_LOG(ERROR, CALEADAPTER_TAG, "sender info is not available");
695 ca_mutex_unlock(g_bleReceiveDataMutex);
699 if (!(bleData->remoteEndpoint))
701 OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!");
702 ca_mutex_unlock(g_bleReceiveDataMutex);
706 CABLESenderInfo_t *senderInfo = NULL;
707 uint32_t senderIndex = 0;
709 if(CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
711 &senderInfo, &senderIndex))
713 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s]",
714 bleData->remoteEndpoint->addr);
719 CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
722 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
723 ca_mutex_unlock(g_bleReceiveDataMutex);
726 newSender->recvDataLen = 0;
727 newSender->totalDataLen = 0;
728 newSender->defragData = NULL;
729 newSender->remoteEndpoint = NULL;
731 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
733 newSender->totalDataLen = CAGetMessageLengthFromData(bleData->data);
735 if(!(newSender->totalDataLen))
737 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
739 ca_mutex_unlock(g_bleReceiveDataMutex);
743 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
744 newSender->totalDataLen);
745 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
748 newSender->defragData = OICCalloc(newSender->totalDataLen + 1,
749 sizeof(*newSender->defragData));
751 if (NULL == newSender->defragData)
753 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
755 ca_mutex_unlock(g_bleReceiveDataMutex);
759 const char *remoteAddress = bleData->remoteEndpoint->addr;
760 newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
761 CA_ADAPTER_GATT_BTLE,
764 if (NULL == newSender->remoteEndpoint)
766 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
767 OICFree(newSender->defragData);
769 ca_mutex_unlock(g_bleReceiveDataMutex);
773 if (newSender->recvDataLen + bleData->dataLen > newSender->totalDataLen)
775 OIC_LOG(ERROR, CALEADAPTER_TAG, "buffer is smaller than received data");
776 OICFree(newSender->defragData);
777 CAFreeEndpoint(newSender->remoteEndpoint);
779 ca_mutex_unlock(g_bleReceiveDataMutex);
782 memcpy(newSender->defragData, bleData->data, bleData->dataLen);
783 newSender->recvDataLen += bleData->dataLen;
785 u_arraylist_add(bleData->senderInfo,(void *)newSender);
787 //Getting newSender index position in bleSenderInfo array list
789 CALEGetSenderInfo(newSender->remoteEndpoint->addr, bleData->senderInfo,
792 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
793 OICFree(newSender->defragData);
794 CAFreeEndpoint(newSender->remoteEndpoint);
796 ca_mutex_unlock(g_bleReceiveDataMutex);
799 senderInfo = newSender;
803 if(senderInfo->recvDataLen + bleData->dataLen > senderInfo->totalDataLen)
805 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
806 "Data Length exceeding error!! Receiving [%d] total length [%d]",
807 senderInfo->recvDataLen + bleData->dataLen, senderInfo->totalDataLen);
808 u_arraylist_remove(bleData->senderInfo, senderIndex);
809 OICFree(senderInfo->defragData);
811 ca_mutex_unlock(g_bleReceiveDataMutex);
814 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]",
816 memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data,
818 senderInfo->recvDataLen += bleData->dataLen ;
819 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] received Datalen [%d]",
820 senderInfo->totalDataLen, senderInfo->recvDataLen);
823 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
825 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
826 if (NULL == g_networkPacketReceivedCallback)
828 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
830 u_arraylist_remove(bleData->senderInfo, senderIndex);
831 OICFree(senderInfo->defragData);
833 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
834 ca_mutex_unlock(g_bleReceiveDataMutex);
838 OIC_LOG(DEBUG, CALEADAPTER_TAG, "[CALEDataReceiverHandler] Sending data up !");
840 const CASecureEndpoint_t tmp =
842 .endpoint = *senderInfo->remoteEndpoint
845 g_networkPacketReceivedCallback(&tmp,
846 senderInfo->defragData,
847 senderInfo->recvDataLen);
848 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
849 u_arraylist_remove(bleData->senderInfo, senderIndex);
850 senderInfo->remoteEndpoint = NULL;
851 senderInfo->defragData = NULL;
855 ca_mutex_unlock(g_bleReceiveDataMutex);
856 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
859 static void CALEServerSendDataThread(void *threadData)
861 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEServerSendDataThread");
863 CALEData_t * const bleData = (CALEData_t *) threadData;
866 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
870 const uint32_t totalLength = bleData->dataLen;
874 "Server total Data length with header is [%u]",
877 uint8_t * const dataSegment = OICCalloc(totalLength, 1);
879 if (NULL == dataSegment)
881 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
886 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
888 length = totalLength;
889 memcpy(dataSegment, bleData->data, bleData->dataLen);
893 length = CA_SUPPORTED_BLE_MTU_SIZE;
894 memcpy(dataSegment, bleData->data, CA_SUPPORTED_BLE_MTU_SIZE);
897 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
899 CAResult_t result = CA_STATUS_FAILED;
901 // Send the first segment with the header.
902 if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
904 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
906 result = CAUpdateCharacteristicsToGattClient(
907 bleData->remoteEndpoint->addr, dataSegment, length);
909 if (CA_STATUS_OK != result)
913 "Update characteristics failed, result [%d]",
916 g_errorHandler(bleData->remoteEndpoint,
920 OICFree(dataSegment);
926 "Server Sent data length [%u]",
928 for (index = 1; index < iter; index++)
930 // Send the remaining header.
933 "Sending the chunk number [%u]",
937 CAUpdateCharacteristicsToGattClient(
938 bleData->remoteEndpoint->addr,
939 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE)),
940 CA_SUPPORTED_BLE_MTU_SIZE);
942 if (CA_STATUS_OK != result)
944 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
945 "Update characteristics failed, result [%d]", result);
946 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
947 OICFree(dataSegment);
950 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
951 CA_SUPPORTED_BLE_MTU_SIZE);
954 const uint32_t remainingLen =
955 totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
957 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
959 // send the last segment of the data (Ex: 22 bytes of 622
960 // bytes of data when MTU is 200)
961 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
963 result = CAUpdateCharacteristicsToGattClient(
964 bleData->remoteEndpoint->addr,
965 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
968 if (CA_STATUS_OK != result)
972 "Update characteristics failed, result [%d]",
974 g_errorHandler(bleData->remoteEndpoint,
978 OICFree(dataSegment);
981 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
986 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
987 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
988 if (CA_STATUS_OK != result)
990 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
992 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
993 OICFree(dataSegment);
996 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
997 for (index = 1; index < iter; index++)
999 // Send the remaining header.
1000 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1002 result = CAUpdateCharacteristicsToAllGattClients(
1003 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE)),
1004 CA_SUPPORTED_BLE_MTU_SIZE);
1006 if (CA_STATUS_OK != result)
1008 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1010 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1011 OICFree(dataSegment);
1014 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%u]",
1015 CA_SUPPORTED_BLE_MTU_SIZE);
1018 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1019 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1021 // send the last segment of the data
1022 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1024 result = CAUpdateCharacteristicsToAllGattClients(
1025 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1028 if (CA_STATUS_OK != result)
1030 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1032 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1033 OICFree(dataSegment);
1036 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1039 OICFree(dataSegment);
1041 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CALEServerSendDataThread");
1044 static void CALEClientSendDataThread(void *threadData)
1046 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEClientSendDataThread");
1048 CALEData_t *bleData = (CALEData_t *) threadData;
1051 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1055 const uint32_t totalLength = bleData->dataLen;
1057 uint8_t *dataSegment = OICCalloc(totalLength, 1);
1058 if (NULL == dataSegment)
1060 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1064 uint32_t length = 0;
1065 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1067 length = totalLength;
1074 length = CA_SUPPORTED_BLE_MTU_SIZE;
1077 CA_SUPPORTED_BLE_MTU_SIZE);
1080 CAResult_t result = CA_STATUS_FAILED;
1081 const uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1083 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1085 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1086 // Send the first segment with the header.
1088 CAUpdateCharacteristicsToGattServer(
1089 bleData->remoteEndpoint->addr,
1095 if (CA_STATUS_OK != result)
1099 "Update characteristics failed, result [%d]",
1101 g_errorHandler(bleData->remoteEndpoint,
1105 OICFree(dataSegment);
1111 "Client Sent Data length is [%u]",
1114 for (index = 1; index < iter; index++)
1116 // Send the remaining header.
1117 result = CAUpdateCharacteristicsToGattServer(
1118 bleData->remoteEndpoint->addr,
1119 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1120 CA_SUPPORTED_BLE_MTU_SIZE,
1123 if (CA_STATUS_OK != result)
1127 "Update characteristics failed, result [%d]",
1129 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1130 OICFree(dataSegment);
1133 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1134 CA_SUPPORTED_BLE_MTU_SIZE);
1137 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1138 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1140 // send the last segment of the data (Ex: 22 bytes of 622
1141 // bytes of data when MTU is 200)
1142 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1144 result = CAUpdateCharacteristicsToGattServer(
1145 bleData->remoteEndpoint->addr,
1146 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1150 if (CA_STATUS_OK != result)
1152 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1154 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1155 OICFree(dataSegment);
1158 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1163 //Sending Mulitcast Data
1164 // Send the first segment with the header.
1165 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1166 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1167 if (CA_STATUS_OK != result)
1169 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1170 "Update characteristics (all) failed, result [%d]", result);
1171 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1172 OICFree(dataSegment);
1175 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1176 // Send the remaining header.
1177 for (index = 1; index < iter; index++)
1179 result = CAUpdateCharacteristicsToAllGattServers(
1180 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1181 CA_SUPPORTED_BLE_MTU_SIZE);
1183 if (CA_STATUS_OK != result)
1185 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1187 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1188 OICFree(dataSegment);
1191 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1192 CA_SUPPORTED_BLE_MTU_SIZE);
1195 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1196 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1198 // send the last segment of the data (Ex: 22 bytes of 622
1199 // bytes of data when MTU is 200)
1200 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1202 CAUpdateCharacteristicsToAllGattServers(
1203 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1206 if (CA_STATUS_OK != result)
1208 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1209 "Update characteristics (all) failed, result [%d]", result);
1210 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1211 OICFree(dataSegment);
1214 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1219 OICFree(dataSegment);
1221 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1224 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
1225 const uint8_t *data,
1226 uint32_t dataLength,
1227 u_arraylist_t *senderInfo)
1229 CALEData_t * const bleData = OICMalloc(sizeof(CALEData_t));
1233 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1237 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1238 bleData->data = OICCalloc(dataLength + 1, 1);
1240 if (NULL == bleData->data)
1242 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1243 CAFreeLEData(bleData);
1247 memcpy(bleData->data, data, dataLength);
1248 bleData->dataLen = dataLength;
1251 bleData->senderInfo = senderInfo;
1257 static void CAFreeLEData(CALEData_t *bleData)
1259 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1261 CAFreeEndpoint(bleData->remoteEndpoint);
1262 OICFree(bleData->data);
1266 static void CALEDataDestroyer(void *data, uint32_t size)
1268 if ((size_t)size < sizeof(CALEData_t *))
1270 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1271 "Destroy data too small %p %d", data, size);
1273 CALEData_t *ledata = (CALEData_t *) data;
1275 CAFreeLEData(ledata);
1279 static CAResult_t CAInitLEAdapterMutex()
1281 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEAdapterMutex");
1283 if (NULL == g_bleIsServerMutex)
1285 g_bleIsServerMutex = ca_mutex_new();
1286 if (NULL == g_bleIsServerMutex)
1288 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1289 return CA_STATUS_FAILED;
1293 if (NULL == g_bleNetworkCbMutex)
1295 g_bleNetworkCbMutex = ca_mutex_new();
1296 if (NULL == g_bleNetworkCbMutex)
1298 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1299 CATerminateLEAdapterMutex();
1300 return CA_STATUS_FAILED;
1304 if (NULL == g_bleLocalAddressMutex)
1306 g_bleLocalAddressMutex = ca_mutex_new();
1307 if (NULL == g_bleLocalAddressMutex)
1309 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1310 CATerminateLEAdapterMutex();
1311 return CA_STATUS_FAILED;
1315 if (NULL == g_bleAdapterThreadPoolMutex)
1317 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1318 if (NULL == g_bleAdapterThreadPoolMutex)
1320 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1321 CATerminateLEAdapterMutex();
1322 return CA_STATUS_FAILED;
1326 if (NULL == g_bleClientSendDataMutex)
1328 g_bleClientSendDataMutex = ca_mutex_new();
1329 if (NULL == g_bleClientSendDataMutex)
1331 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1332 CATerminateLEAdapterMutex();
1333 return CA_STATUS_FAILED;
1337 if (NULL == g_bleServerSendDataMutex)
1339 g_bleServerSendDataMutex = ca_mutex_new();
1340 if (NULL == g_bleServerSendDataMutex)
1342 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1343 CATerminateLEAdapterMutex();
1344 return CA_STATUS_FAILED;
1348 if (NULL == g_bleAdapterReqRespCbMutex)
1350 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1351 if (NULL == g_bleAdapterReqRespCbMutex)
1353 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1354 CATerminateLEAdapterMutex();
1355 return CA_STATUS_FAILED;
1359 if (NULL == g_bleReceiveDataMutex)
1361 g_bleReceiveDataMutex = ca_mutex_new();
1362 if (NULL == g_bleReceiveDataMutex)
1364 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1365 return CA_STATUS_FAILED;
1369 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1370 return CA_STATUS_OK;
1373 static void CATerminateLEAdapterMutex()
1375 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CATerminateLEAdapterMutex");
1377 ca_mutex_free(g_bleIsServerMutex);
1378 g_bleIsServerMutex = NULL;
1380 ca_mutex_free(g_bleNetworkCbMutex);
1381 g_bleNetworkCbMutex = NULL;
1383 ca_mutex_free(g_bleLocalAddressMutex);
1384 g_bleLocalAddressMutex = NULL;
1386 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1387 g_bleAdapterThreadPoolMutex = NULL;
1389 ca_mutex_free(g_bleClientSendDataMutex);
1390 g_bleClientSendDataMutex = NULL;
1392 ca_mutex_free(g_bleServerSendDataMutex);
1393 g_bleServerSendDataMutex = NULL;
1395 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1396 g_bleAdapterReqRespCbMutex = NULL;
1398 ca_mutex_free(g_bleReceiveDataMutex);
1399 g_bleReceiveDataMutex = NULL;
1401 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1405 * Starting LE connectivity adapters.
1407 * As its peer to peer it does not require to start any servers.
1409 * @return ::CA_STATUS_OK or Appropriate error code.
1411 static CAResult_t CAStartLE();
1414 * Start listening server for receiving multicast search requests.
1416 * Transport Specific Behavior:
1417 * LE Starts GATT Server with prefixed UUID and Characteristics
1418 * per OIC Specification.
1419 * @return ::CA_STATUS_OK or Appropriate error code.
1421 static CAResult_t CAStartLEListeningServer();
1424 * Stops listening server from receiving multicast search requests.
1426 * Transport Specific Behavior:
1427 * LE Starts GATT Server with prefixed UUID and Characteristics
1428 * per OIC Specification.
1429 * @return ::CA_STATUS_OK or Appropriate error code.
1431 static CAResult_t CAStopLEListeningServer();
1434 * Sarting discovery of servers for receiving multicast
1437 * Transport Specific Behavior:
1438 * LE Starts GATT Server with prefixed UUID and Characteristics
1439 * per OIC Specification.
1441 * @return ::CA_STATUS_OK or Appropriate error code
1443 static CAResult_t CAStartLEDiscoveryServer();
1446 * Send data to the endpoint using the adapter connectivity.
1448 * @param[in] endpoint Remote Endpoint information (like MAC address,
1449 * reference URI and connectivity type) to which
1450 * the unicast data has to be sent.
1451 * @param[in] data Data which required to be sent.
1452 * @param[in] dataLen Size of data to be sent.
1454 * @note dataLen must be > 0.
1456 * @return The number of bytes sent on the network, or -1 on error.
1458 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1463 * Send multicast data to the endpoint using the LE connectivity.
1465 * @param[in] endpoint Remote Endpoint information to which the
1466 * multicast data has to be sent.
1467 * @param[in] data Data which required to be sent.
1468 * @param[in] dataLen Size of data to be sent.
1470 * @note dataLen must be > 0.
1472 * @return The number of bytes sent on the network, or -1 on error.
1474 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1479 * Get LE Connectivity network information.
1481 * @param[out] info Local connectivity information structures.
1482 * @param[out] size Number of local connectivity structures.
1484 * @return ::CA_STATUS_OK or Appropriate error code.
1486 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info,
1490 * Read Synchronous API callback.
1492 * @return ::CA_STATUS_OK or Appropriate error code.
1494 static CAResult_t CAReadLEData();
1497 * Stopping the adapters and close socket connections.
1499 * LE Stops all GATT servers and GATT Clients.
1501 * @return ::CA_STATUS_OK or Appropriate error code.
1503 static CAResult_t CAStopLE();
1506 * Terminate the LE connectivity adapter.
1508 * Configuration information will be deleted from further use.
1510 static void CATerminateLE();
1513 * This function will receive the data from the GattServer and add the
1514 * data to the Server receiver queue.
1516 * @param[in] remoteAddress Remote address of the device from where
1518 * @param[in] data Actual data recevied from the remote
1520 * @param[in] dataLength Length of the data received from the
1522 * @param[in] sentLength Length of the data sent from the remote
1525 * @return ::CA_STATUS_OK or Appropriate error code.
1526 * @retval ::CA_STATUS_OK Successful.
1527 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1528 * @retval ::CA_STATUS_FAILED Operation failed.
1531 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
1532 const uint8_t *data,
1533 uint32_t dataLength,
1534 uint32_t *sentLength);
1537 * This function will receive the data from the GattClient and add the
1538 * data into the Client receiver queue.
1540 * @param[in] remoteAddress Remote address of the device from where
1542 * @param[in] data Actual data recevied from the remote
1544 * @param[in] dataLength Length of the data received from the
1546 * @param[in] sentLength Length of the data sent from the remote
1549 * @return ::CA_STATUS_OK or Appropriate error code.
1550 * @retval ::CA_STATUS_OK Successful.
1551 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1552 * @retval ::CA_STATUS_FAILED Operation failed.
1554 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
1555 const uint8_t *data,
1556 uint32_t dataLength,
1557 uint32_t *sentLength);
1560 * Set the NetworkPacket received callback to CA layer from adapter
1563 * @param[in] callback Callback handle sent from the upper layer.
1565 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
1568 * Push the data from CA layer to the Sender processor queue.
1570 * @param[in] remoteEndpoint Remote endpoint information of the
1572 * @param[in] data Data to be transmitted from LE.
1573 * @param[in] dataLen Length of the Data being transmitted.
1575 * @return ::CA_STATUS_OK or Appropriate error code.
1576 * @retval ::CA_STATUS_OK Successful.
1577 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1578 * @retval ::CA_STATUS_FAILED Operation failed.
1580 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1581 const uint8_t *data,
1585 * Push the data from CA layer to the Sender processor queue.
1587 * @param[in] remoteEndpoint Remote endpoint information of the
1589 * @param[in] data Data to be transmitted from LE.
1590 * @param[in] dataLen Length of the Data being transmitted.
1592 * @return ::CA_STATUS_OK or Appropriate error code.
1593 * @retval ::CA_STATUS_OK Successful.
1594 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1595 * @retval ::CA_STATUS_FAILED Operation failed.
1597 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1598 const uint8_t *data,
1601 static CAResult_t CALEAdapterGattServerStart()
1603 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1605 CAResult_t result = CAStartLEGattServer();
1607 #ifndef SINGLE_THREAD
1609 Don't start the server side sending queue thread until the
1610 server itself has actually started.
1612 if (CA_STATUS_OK == result)
1614 ca_mutex_lock(g_bleServerSendDataMutex);
1615 result = CAQueueingThreadStart(g_bleServerSendQueueHandle);
1616 ca_mutex_unlock(g_bleServerSendDataMutex);
1618 if (CA_STATUS_OK != result)
1622 "Unable to start server queuing thread (%d)",
1631 static CAResult_t CALEAdapterGattServerStop()
1633 #ifndef SINGLE_THREAD
1634 ca_mutex_lock(g_bleServerSendDataMutex);
1635 CAResult_t result = CAQueueingThreadStop(g_bleServerSendQueueHandle);
1636 ca_mutex_unlock(g_bleServerSendDataMutex);
1637 if (CA_STATUS_OK == result)
1639 result = CAStopLEGattServer();
1644 return CAStopLEGattServer();
1648 static CAResult_t CALEAdapterGattClientStart()
1650 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattClient");
1652 CAResult_t result = CAStartLEGattClient();
1654 #ifndef SINGLE_THREAD
1656 Don't start the client side sending queue thread until the
1657 client itself has actually started.
1659 if (CA_STATUS_OK == result)
1661 ca_mutex_lock(g_bleClientSendDataMutex);
1662 result = CAQueueingThreadStart(g_bleClientSendQueueHandle);
1663 ca_mutex_unlock(g_bleClientSendDataMutex);
1665 if (CA_STATUS_OK != result)
1669 "Unable to start client queuing thread");
1677 static CAResult_t CALEAdapterGattClientStop()
1679 #ifndef SINGLE_THREAD
1680 ca_mutex_lock(g_bleClientSendDataMutex);
1681 CAResult_t result = CAQueueingThreadStop(g_bleClientSendQueueHandle);
1682 ca_mutex_unlock(g_bleClientSendDataMutex);
1683 if (CA_STATUS_OK == result)
1685 CAStopLEGattClient();
1690 CAStopLEGattClient();
1692 return CA_STATUS_OK;
1696 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1697 CANetworkPacketReceivedCallback reqRespCallback,
1698 CANetworkChangeCallback netCallback,
1699 CAErrorHandleCallback errorCallback,
1700 ca_thread_pool_t handle)
1702 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1705 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1706 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1707 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1709 CAResult_t result = CA_STATUS_OK;
1710 result = CAInitLEAdapterMutex();
1711 if (CA_STATUS_OK != result)
1713 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1714 return CA_STATUS_FAILED;
1717 result = CAInitializeLENetworkMonitor();
1718 if (CA_STATUS_OK != result)
1720 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1721 return CA_STATUS_FAILED;
1723 CAInitializeLEAdapter(handle);
1725 CASetLEClientThreadPoolHandle(handle);
1727 result = CAInitializeLEGattClient();
1728 if (CA_STATUS_OK != result)
1730 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattClient() failed");
1731 return CA_STATUS_FAILED;
1734 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1735 CASetLEServerThreadPoolHandle(handle);
1736 result = CAInitializeLEGattServer();
1737 if (CA_STATUS_OK != result)
1739 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattServer() failed");
1740 return CA_STATUS_FAILED;
1743 CASetLEAdapterThreadPoolHandle(handle);
1744 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1745 CASetLEReqRespAdapterCallback(reqRespCallback);
1747 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1748 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1749 CALERegisterNetworkNotifications(netCallback);
1751 g_errorHandler = errorCallback;
1753 static const CAConnectivityHandler_t connHandler =
1755 .startAdapter = CAStartLE,
1756 .stopAdapter = CAStopLE,
1757 .startListenServer = CAStartLEListeningServer,
1758 .stopListenServer = CAStopLEListeningServer,
1759 .startDiscoveryServer = CAStartLEDiscoveryServer,
1760 .sendData = CASendLEUnicastData,
1761 .sendDataToAll = CASendLEMulticastData,
1762 .GetnetInfo = CAGetLEInterfaceInformation,
1763 .readData = CAReadLEData,
1764 .terminate = CATerminateLE,
1765 .cType = CA_ADAPTER_GATT_BTLE
1768 registerCallback(connHandler);
1770 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1772 return CA_STATUS_OK;
1775 static CAResult_t CAStartLE()
1777 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE");
1779 return CAStartLEAdapter();
1782 static CAResult_t CAStopLE()
1784 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1785 #ifndef SINGLE_THREAD
1789 ca_mutex_lock(g_bleIsServerMutex);
1790 switch (g_adapterType)
1792 case ADAPTER_SERVER:
1793 CALEAdapterGattServerStop();
1795 case ADAPTER_CLIENT:
1796 CALEAdapterGattClientStop();
1798 case ADAPTER_BOTH_CLIENT_SERVER:
1799 CALEAdapterGattServerStop();
1800 CALEAdapterGattClientStop();
1805 ca_mutex_unlock(g_bleIsServerMutex);
1807 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1809 return CAStopLEAdapter();
1812 static void CATerminateLE()
1814 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1816 CASetLEReqRespServerCallback(NULL);
1817 CASetLEReqRespClientCallback(NULL);
1818 CALERegisterNetworkNotifications(NULL);
1819 CASetLEReqRespAdapterCallback(NULL);
1820 CATerminateLENetworkMonitor();
1822 ca_mutex_lock(g_bleIsServerMutex);
1823 switch (g_adapterType)
1825 case ADAPTER_SERVER:
1826 CATerminateLEGattServer();
1828 case ADAPTER_CLIENT:
1829 CATerminateLEGattClient();
1831 case ADAPTER_BOTH_CLIENT_SERVER:
1832 CATerminateLEGattServer();
1833 CATerminateLEGattClient();
1838 g_adapterType = ADAPTER_EMPTY;
1839 ca_mutex_unlock(g_bleIsServerMutex);
1841 #ifndef SINGLE_THREAD
1842 CATerminateLEQueues();
1844 CATerminateLEAdapterMutex();
1846 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1849 static CAResult_t CAStartLEListeningServer()
1851 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEListeningServer");
1852 #ifndef ROUTING_GATEWAY
1853 CAResult_t result = CA_STATUS_OK;
1854 #ifndef SINGLE_THREAD
1855 result = CAInitLEServerQueues();
1856 if (CA_STATUS_OK != result)
1858 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1863 ca_mutex_lock(g_bleIsServerMutex);
1864 switch (g_adapterType)
1866 case ADAPTER_CLIENT:
1867 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1869 case ADAPTER_BOTH_CLIENT_SERVER:
1872 g_adapterType = ADAPTER_SERVER;
1874 ca_mutex_unlock(g_bleIsServerMutex);
1876 result = CAGetLEAdapterState();
1877 if (CA_STATUS_OK != result)
1879 if (CA_ADAPTER_NOT_ENABLED == result)
1883 "Listen Server will be started once BT Adapter is enabled");
1888 result = CALEAdapterGattServerStart();
1891 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1894 // Routing Gateway only supports BLE client mode.
1895 OIC_LOG(ERROR, CALEADAPTER_TAG, "LE server not supported in Routing Gateway");
1896 return CA_NOT_SUPPORTED;
1900 static CAResult_t CAStopLEListeningServer()
1902 OIC_LOG(ERROR, CALEADAPTER_TAG, "Listen server stop not supported.");
1903 return CA_NOT_SUPPORTED;
1906 static CAResult_t CAStartLEDiscoveryServer()
1908 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEDiscoveryServer");
1909 CAResult_t result = CA_STATUS_OK;
1910 #ifndef SINGLE_THREAD
1911 result = CAInitLEClientQueues();
1912 if (CA_STATUS_OK != result)
1914 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1919 ca_mutex_lock(g_bleIsServerMutex);
1920 switch (g_adapterType)
1922 case ADAPTER_SERVER:
1923 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1925 case ADAPTER_BOTH_CLIENT_SERVER:
1928 g_adapterType = ADAPTER_CLIENT;
1930 ca_mutex_unlock(g_bleIsServerMutex);
1932 result = CAGetLEAdapterState();
1933 if (CA_STATUS_OK != result)
1935 if (CA_ADAPTER_NOT_ENABLED == result)
1939 "Discovery Server will be started once BT Adapter is enabled");
1944 result = CALEAdapterGattClientStart();
1947 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1951 static CAResult_t CAReadLEData()
1953 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1954 #ifdef SINGLE_THREAD
1957 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1958 return CA_STATUS_OK;
1961 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1965 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEUnicastData");
1968 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1969 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1971 CAResult_t result = CA_STATUS_FAILED;
1973 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
1974 if (ADAPTER_EMPTY == g_adapterType)
1976 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
1979 ca_mutex_lock(g_bleIsServerMutex);
1980 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
1982 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1983 if (CA_STATUS_OK != result)
1985 ca_mutex_unlock(g_bleIsServerMutex);
1986 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed");
1989 g_errorHandler(endpoint, data, dataLen, result);
1996 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
1998 result = CALEAdapterClientSendData(endpoint, data, dataLen);
1999 if (CA_STATUS_OK != result)
2001 ca_mutex_unlock(g_bleIsServerMutex);
2002 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for client failed" );
2006 g_errorHandler(endpoint, data, dataLen, result);
2011 ca_mutex_unlock(g_bleIsServerMutex);
2013 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2017 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
2021 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEMulticastData");
2024 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
2028 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
2032 CAResult_t result = CA_STATUS_FAILED;
2034 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
2035 if (ADAPTER_EMPTY == g_adapterType)
2037 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2040 ca_mutex_lock(g_bleIsServerMutex);
2041 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2043 result = CALEAdapterServerSendData(NULL, data, dataLen);
2044 if (CA_STATUS_OK != result)
2046 ca_mutex_unlock(g_bleIsServerMutex);
2048 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data for server failed" );
2052 g_errorHandler(endpoint, data, dataLen, result);
2058 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2060 result = CALEAdapterClientSendData(NULL, data, dataLen);
2061 if (CA_STATUS_OK != result)
2063 ca_mutex_unlock(g_bleIsServerMutex);
2065 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data for client failed" );
2069 g_errorHandler(endpoint, data, dataLen, result);
2074 ca_mutex_unlock(g_bleIsServerMutex);
2076 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CASendLEMulticastData");
2080 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
2082 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2084 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
2086 char *local_address = NULL;
2088 CAResult_t res = CAGetLEAddress(&local_address);
2089 if (CA_STATUS_OK != res)
2091 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
2095 if (NULL == local_address)
2097 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
2098 return CA_STATUS_FAILED;
2102 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
2103 if (NULL == (*info))
2105 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
2106 OICFree(local_address);
2107 return CA_STATUS_FAILED;
2110 size_t local_address_len = strlen(local_address);
2112 if(local_address_len >= sizeof(g_localBLEAddress) ||
2113 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
2115 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
2117 OICFree(local_address);
2118 return CA_STATUS_FAILED;
2121 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
2122 ca_mutex_lock(g_bleLocalAddressMutex);
2123 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
2124 ca_mutex_unlock(g_bleLocalAddressMutex);
2126 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
2128 OICFree(local_address);
2130 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2131 return CA_STATUS_OK;
2134 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
2136 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2138 ca_mutex_lock(g_bleNetworkCbMutex);
2139 g_networkCallback = netCallback;
2140 ca_mutex_unlock(g_bleNetworkCbMutex);
2141 CAResult_t res = CA_STATUS_OK;
2144 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
2145 if (CA_STATUS_OK != res)
2147 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2152 res = CAUnSetLEAdapterStateChangedCb();
2153 if (CA_STATUS_OK != res)
2155 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2159 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2163 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
2165 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDeviceStateChangedCb");
2167 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
2168 CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE };
2170 ca_mutex_lock(g_bleLocalAddressMutex);
2171 OICStrcpy(localEndpoint.addr,
2172 sizeof(localEndpoint.addr),
2174 ca_mutex_unlock(g_bleLocalAddressMutex);
2176 if (CA_ADAPTER_ENABLED == adapter_state)
2178 ca_mutex_lock(g_bleIsServerMutex);
2179 switch (g_adapterType)
2181 case ADAPTER_SERVER:
2182 CALEAdapterGattServerStart();
2184 case ADAPTER_CLIENT:
2185 CALEAdapterGattClientStart();
2187 case ADAPTER_BOTH_CLIENT_SERVER:
2188 CALEAdapterGattServerStart();
2189 CALEAdapterGattClientStart();
2194 ca_mutex_unlock(g_bleIsServerMutex);
2198 ca_mutex_lock(g_bleIsServerMutex);
2199 switch (g_adapterType)
2201 case ADAPTER_SERVER:
2202 CALEAdapterGattServerStop();
2204 case ADAPTER_CLIENT:
2205 CALEAdapterGattClientStop();
2207 case ADAPTER_BOTH_CLIENT_SERVER:
2208 CALEAdapterGattServerStop();
2209 CALEAdapterGattClientStop();
2214 ca_mutex_unlock(g_bleIsServerMutex);
2217 ca_mutex_lock(g_bleNetworkCbMutex);
2218 if (NULL != g_networkCallback)
2220 g_networkCallback(&localEndpoint, adapter_state);
2224 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
2226 ca_mutex_unlock(g_bleNetworkCbMutex);
2228 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2231 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
2232 const uint8_t *data,
2235 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2237 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2238 #ifndef SINGLE_THREAD
2239 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2240 "g_bleClientSendQueueHandle is NULL",
2242 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
2243 "g_bleClientSendDataMutex is NULL",
2246 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2247 "g_bleClientSendQueueHandle",
2250 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
2252 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2255 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2256 return CA_MEMORY_ALLOC_FAILED;
2258 // Add message to send queue
2259 ca_mutex_lock(g_bleClientSendDataMutex);
2260 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
2261 ca_mutex_unlock(g_bleClientSendDataMutex);
2263 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2264 return CA_STATUS_OK;
2267 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2268 const uint8_t *data,
2271 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2273 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2275 #ifdef SINGLE_THREAD
2276 uint8_t header[CA_HEADER_LENGTH] = { 0 };
2279 CAGenerateHeader(header, CA_HEADER_LENGTH, dataLen);
2281 if (CA_STATUS_OK != result)
2283 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
2284 return CA_STATUS_FAILED;
2287 if (!CAIsLEConnected())
2289 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2290 return CA_STATUS_FAILED;
2293 result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH);
2294 if (CA_STATUS_OK != result)
2296 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2297 return CA_STATUS_FAILED;
2300 const uint32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2301 for (uint32_t iter = 0; iter < dataLimit; iter++)
2304 CAUpdateCharacteristicsToAllGattClients(
2305 data + (iter * CA_SUPPORTED_BLE_MTU_SIZE),
2306 CA_SUPPORTED_BLE_MTU_SIZE);
2308 if (CA_STATUS_OK != result)
2310 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2311 return CA_STATUS_FAILED;
2317 const uint32_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2321 CAUpdateCharacteristicsToAllGattClients(
2322 data + (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE),
2324 if (CA_STATUS_OK != result)
2326 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2327 return CA_STATUS_FAILED;
2332 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2333 "BleClientReceiverQueue is NULL",
2335 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2336 "BleClientSendDataMutex is NULL",
2339 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2342 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2344 CALEData_t * const bleData =
2345 CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2349 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2350 return CA_MEMORY_ALLOC_FAILED;
2353 // Add message to send queue
2354 ca_mutex_lock(g_bleServerSendDataMutex);
2355 CAQueueingThreadAddData(g_bleServerSendQueueHandle,
2357 sizeof(CALEData_t));
2358 ca_mutex_unlock(g_bleServerSendDataMutex);
2360 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2361 return CA_STATUS_OK;
2364 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2365 const uint8_t *data,
2366 uint32_t dataLength,
2367 uint32_t *sentLength)
2369 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2372 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2373 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2375 #ifdef SINGLE_THREAD
2376 if(g_networkPacketReceivedCallback)
2378 // will be filled by upper layer
2379 const CASecureEndpoint_t endpoint =
2380 { .endpoint = { .adapter = CA_ADAPTER_GATT_BTLE } };
2383 g_networkPacketReceivedCallback(&endpoint, data, dataLength);
2386 VERIFY_NON_NULL_RET(g_bleReceiverQueue,
2388 "g_bleReceiverQueue",
2391 //Add message to data queue
2392 CAEndpoint_t * const remoteEndpoint =
2393 CACreateEndpointObject(CA_DEFAULT_FLAGS,
2394 CA_ADAPTER_GATT_BTLE,
2398 if (NULL == remoteEndpoint)
2400 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2401 return CA_STATUS_FAILED;
2404 // Create bleData to add to queue
2407 "Data received from LE layer [%d]",
2410 CALEData_t * const bleData =
2411 CACreateLEData(remoteEndpoint, data, dataLength, g_bleServerSenderInfo);
2415 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2416 CAFreeEndpoint(remoteEndpoint);
2417 return CA_MEMORY_ALLOC_FAILED;
2420 CAFreeEndpoint(remoteEndpoint);
2421 // Add message to receiver queue
2422 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2424 *sentLength = dataLength;
2426 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2427 return CA_STATUS_OK;
2430 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2431 const uint8_t *data,
2432 uint32_t dataLength,
2433 uint32_t *sentLength)
2435 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2438 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2439 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2440 #ifndef SINGLE_THREAD
2441 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG,
2442 "g_bleReceiverQueue",
2445 //Add message to data queue
2446 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2447 CA_ADAPTER_GATT_BTLE,
2449 if (NULL == remoteEndpoint)
2451 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2452 return CA_STATUS_FAILED;
2455 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%u]", dataLength);
2457 // Create bleData to add to queue
2458 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data,
2459 dataLength, g_bleClientSenderInfo);
2462 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2463 CAFreeEndpoint(remoteEndpoint);
2464 return CA_MEMORY_ALLOC_FAILED;
2467 CAFreeEndpoint(remoteEndpoint);
2468 // Add message to receiver queue
2469 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2471 *sentLength = dataLength;
2473 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2474 return CA_STATUS_OK;
2477 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2479 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2481 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2482 g_bleAdapterThreadPool = handle;
2483 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2485 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2488 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2490 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2492 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2494 g_networkPacketReceivedCallback = callback;
2496 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2498 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2501 static void CALEErrorHandler(const char *remoteAddress,
2502 const uint8_t *data,
2506 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2508 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2510 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2511 CA_ADAPTER_GATT_BTLE,
2515 // if required, will be used to build remote endpoint
2516 g_errorHandler(rep, data, dataLen, result);
2518 CAFreeEndpoint(rep);
2520 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");