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
1767 registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
1769 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1771 return CA_STATUS_OK;
1774 static CAResult_t CAStartLE()
1776 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE");
1778 return CAStartLEAdapter();
1781 static CAResult_t CAStopLE()
1783 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1784 #ifndef SINGLE_THREAD
1788 ca_mutex_lock(g_bleIsServerMutex);
1789 switch (g_adapterType)
1791 case ADAPTER_SERVER:
1792 CALEAdapterGattServerStop();
1794 case ADAPTER_CLIENT:
1795 CALEAdapterGattClientStop();
1797 case ADAPTER_BOTH_CLIENT_SERVER:
1798 CALEAdapterGattServerStop();
1799 CALEAdapterGattClientStop();
1804 ca_mutex_unlock(g_bleIsServerMutex);
1806 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1808 return CAStopLEAdapter();
1811 static void CATerminateLE()
1813 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1815 CASetLEReqRespServerCallback(NULL);
1816 CASetLEReqRespClientCallback(NULL);
1817 CALERegisterNetworkNotifications(NULL);
1818 CASetLEReqRespAdapterCallback(NULL);
1819 CATerminateLENetworkMonitor();
1821 ca_mutex_lock(g_bleIsServerMutex);
1822 switch (g_adapterType)
1824 case ADAPTER_SERVER:
1825 CATerminateLEGattServer();
1827 case ADAPTER_CLIENT:
1828 CATerminateLEGattClient();
1830 case ADAPTER_BOTH_CLIENT_SERVER:
1831 CATerminateLEGattServer();
1832 CATerminateLEGattClient();
1837 g_adapterType = ADAPTER_EMPTY;
1838 ca_mutex_unlock(g_bleIsServerMutex);
1840 #ifndef SINGLE_THREAD
1841 CATerminateLEQueues();
1843 CATerminateLEAdapterMutex();
1845 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1848 static CAResult_t CAStartLEListeningServer()
1850 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEListeningServer");
1851 #ifndef ROUTING_GATEWAY
1852 CAResult_t result = CA_STATUS_OK;
1853 #ifndef SINGLE_THREAD
1854 result = CAInitLEServerQueues();
1855 if (CA_STATUS_OK != result)
1857 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1862 ca_mutex_lock(g_bleIsServerMutex);
1863 switch (g_adapterType)
1865 case ADAPTER_CLIENT:
1866 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1868 case ADAPTER_BOTH_CLIENT_SERVER:
1871 g_adapterType = ADAPTER_SERVER;
1873 ca_mutex_unlock(g_bleIsServerMutex);
1875 result = CAGetLEAdapterState();
1876 if (CA_STATUS_OK != result)
1878 if (CA_ADAPTER_NOT_ENABLED == result)
1882 "Listen Server will be started once BT Adapter is enabled");
1887 result = CALEAdapterGattServerStart();
1890 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1893 // Routing Gateway only supports BLE client mode.
1894 OIC_LOG(ERROR, CALEADAPTER_TAG, "LE server not supported in Routing Gateway");
1895 return CA_NOT_SUPPORTED;
1899 static CAResult_t CAStopLEListeningServer()
1901 OIC_LOG(ERROR, CALEADAPTER_TAG, "Listen server stop not supported.");
1902 return CA_NOT_SUPPORTED;
1905 static CAResult_t CAStartLEDiscoveryServer()
1907 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEDiscoveryServer");
1908 CAResult_t result = CA_STATUS_OK;
1909 #ifndef SINGLE_THREAD
1910 result = CAInitLEClientQueues();
1911 if (CA_STATUS_OK != result)
1913 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1918 ca_mutex_lock(g_bleIsServerMutex);
1919 switch (g_adapterType)
1921 case ADAPTER_SERVER:
1922 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1924 case ADAPTER_BOTH_CLIENT_SERVER:
1927 g_adapterType = ADAPTER_CLIENT;
1929 ca_mutex_unlock(g_bleIsServerMutex);
1931 result = CAGetLEAdapterState();
1932 if (CA_STATUS_OK != result)
1934 if (CA_ADAPTER_NOT_ENABLED == result)
1938 "Discovery Server will be started once BT Adapter is enabled");
1943 result = CALEAdapterGattClientStart();
1946 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1950 static CAResult_t CAReadLEData()
1952 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1953 #ifdef SINGLE_THREAD
1956 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1957 return CA_STATUS_OK;
1960 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1964 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEUnicastData");
1967 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1968 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1970 CAResult_t result = CA_STATUS_FAILED;
1972 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
1973 if (ADAPTER_EMPTY == g_adapterType)
1975 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
1978 ca_mutex_lock(g_bleIsServerMutex);
1979 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
1981 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1982 if (CA_STATUS_OK != result)
1984 ca_mutex_unlock(g_bleIsServerMutex);
1985 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed");
1988 g_errorHandler(endpoint, data, dataLen, result);
1995 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
1997 result = CALEAdapterClientSendData(endpoint, data, dataLen);
1998 if (CA_STATUS_OK != result)
2000 ca_mutex_unlock(g_bleIsServerMutex);
2001 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for client failed" );
2005 g_errorHandler(endpoint, data, dataLen, result);
2010 ca_mutex_unlock(g_bleIsServerMutex);
2012 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2016 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
2020 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEMulticastData");
2023 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
2027 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
2031 CAResult_t result = CA_STATUS_FAILED;
2033 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
2034 if (ADAPTER_EMPTY == g_adapterType)
2036 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2039 ca_mutex_lock(g_bleIsServerMutex);
2040 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2042 result = CALEAdapterServerSendData(NULL, data, dataLen);
2043 if (CA_STATUS_OK != result)
2045 ca_mutex_unlock(g_bleIsServerMutex);
2047 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data for server failed" );
2051 g_errorHandler(endpoint, data, dataLen, result);
2057 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2059 result = CALEAdapterClientSendData(NULL, data, dataLen);
2060 if (CA_STATUS_OK != result)
2062 ca_mutex_unlock(g_bleIsServerMutex);
2064 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data for client failed" );
2068 g_errorHandler(endpoint, data, dataLen, result);
2073 ca_mutex_unlock(g_bleIsServerMutex);
2075 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CASendLEMulticastData");
2079 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
2081 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2083 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
2085 char *local_address = NULL;
2087 CAResult_t res = CAGetLEAddress(&local_address);
2088 if (CA_STATUS_OK != res)
2090 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
2094 if (NULL == local_address)
2096 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
2097 return CA_STATUS_FAILED;
2101 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
2102 if (NULL == (*info))
2104 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
2105 OICFree(local_address);
2106 return CA_STATUS_FAILED;
2109 size_t local_address_len = strlen(local_address);
2111 if(local_address_len >= sizeof(g_localBLEAddress) ||
2112 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
2114 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
2116 OICFree(local_address);
2117 return CA_STATUS_FAILED;
2120 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
2121 ca_mutex_lock(g_bleLocalAddressMutex);
2122 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
2123 ca_mutex_unlock(g_bleLocalAddressMutex);
2125 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
2127 OICFree(local_address);
2129 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2130 return CA_STATUS_OK;
2133 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
2135 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2137 ca_mutex_lock(g_bleNetworkCbMutex);
2138 g_networkCallback = netCallback;
2139 ca_mutex_unlock(g_bleNetworkCbMutex);
2140 CAResult_t res = CA_STATUS_OK;
2143 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
2144 if (CA_STATUS_OK != res)
2146 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2151 res = CAUnSetLEAdapterStateChangedCb();
2152 if (CA_STATUS_OK != res)
2154 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2158 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2162 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
2164 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDeviceStateChangedCb");
2166 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
2167 CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE };
2169 ca_mutex_lock(g_bleLocalAddressMutex);
2170 OICStrcpy(localEndpoint.addr,
2171 sizeof(localEndpoint.addr),
2173 ca_mutex_unlock(g_bleLocalAddressMutex);
2175 if (CA_ADAPTER_ENABLED == adapter_state)
2177 ca_mutex_lock(g_bleIsServerMutex);
2178 switch (g_adapterType)
2180 case ADAPTER_SERVER:
2181 CALEAdapterGattServerStart();
2183 case ADAPTER_CLIENT:
2184 CALEAdapterGattClientStart();
2186 case ADAPTER_BOTH_CLIENT_SERVER:
2187 CALEAdapterGattServerStart();
2188 CALEAdapterGattClientStart();
2193 ca_mutex_unlock(g_bleIsServerMutex);
2197 ca_mutex_lock(g_bleIsServerMutex);
2198 switch (g_adapterType)
2200 case ADAPTER_SERVER:
2201 CALEAdapterGattServerStop();
2203 case ADAPTER_CLIENT:
2204 CALEAdapterGattClientStop();
2206 case ADAPTER_BOTH_CLIENT_SERVER:
2207 CALEAdapterGattServerStop();
2208 CALEAdapterGattClientStop();
2213 ca_mutex_unlock(g_bleIsServerMutex);
2216 ca_mutex_lock(g_bleNetworkCbMutex);
2217 if (NULL != g_networkCallback)
2219 g_networkCallback(&localEndpoint, adapter_state);
2223 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
2225 ca_mutex_unlock(g_bleNetworkCbMutex);
2227 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2230 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
2231 const uint8_t *data,
2234 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2236 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2237 #ifndef SINGLE_THREAD
2238 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2239 "g_bleClientSendQueueHandle is NULL",
2241 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
2242 "g_bleClientSendDataMutex is NULL",
2245 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2246 "g_bleClientSendQueueHandle",
2249 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
2251 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2254 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2255 return CA_MEMORY_ALLOC_FAILED;
2257 // Add message to send queue
2258 ca_mutex_lock(g_bleClientSendDataMutex);
2259 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
2260 ca_mutex_unlock(g_bleClientSendDataMutex);
2262 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2263 return CA_STATUS_OK;
2266 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2267 const uint8_t *data,
2270 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2272 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2274 #ifdef SINGLE_THREAD
2275 uint8_t header[CA_HEADER_LENGTH] = { 0 };
2278 CAGenerateHeader(header, CA_HEADER_LENGTH, dataLen);
2280 if (CA_STATUS_OK != result)
2282 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
2283 return CA_STATUS_FAILED;
2286 if (!CAIsLEConnected())
2288 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2289 return CA_STATUS_FAILED;
2292 result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH);
2293 if (CA_STATUS_OK != result)
2295 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2296 return CA_STATUS_FAILED;
2299 const uint32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2300 for (uint32_t iter = 0; iter < dataLimit; iter++)
2303 CAUpdateCharacteristicsToAllGattClients(
2304 data + (iter * CA_SUPPORTED_BLE_MTU_SIZE),
2305 CA_SUPPORTED_BLE_MTU_SIZE);
2307 if (CA_STATUS_OK != result)
2309 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2310 return CA_STATUS_FAILED;
2316 const uint32_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2320 CAUpdateCharacteristicsToAllGattClients(
2321 data + (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE),
2323 if (CA_STATUS_OK != result)
2325 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2326 return CA_STATUS_FAILED;
2331 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2332 "BleClientReceiverQueue is NULL",
2334 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2335 "BleClientSendDataMutex is NULL",
2338 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2341 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2343 CALEData_t * const bleData =
2344 CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2348 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2349 return CA_MEMORY_ALLOC_FAILED;
2352 // Add message to send queue
2353 ca_mutex_lock(g_bleServerSendDataMutex);
2354 CAQueueingThreadAddData(g_bleServerSendQueueHandle,
2356 sizeof(CALEData_t));
2357 ca_mutex_unlock(g_bleServerSendDataMutex);
2359 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2360 return CA_STATUS_OK;
2363 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2364 const uint8_t *data,
2365 uint32_t dataLength,
2366 uint32_t *sentLength)
2368 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2371 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2372 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2374 #ifdef SINGLE_THREAD
2375 if(g_networkPacketReceivedCallback)
2377 // will be filled by upper layer
2378 const CASecureEndpoint_t endpoint =
2379 { .endpoint = { .adapter = CA_ADAPTER_GATT_BTLE } };
2382 g_networkPacketReceivedCallback(&endpoint, data, dataLength);
2385 VERIFY_NON_NULL_RET(g_bleReceiverQueue,
2387 "g_bleReceiverQueue",
2390 //Add message to data queue
2391 CAEndpoint_t * const remoteEndpoint =
2392 CACreateEndpointObject(CA_DEFAULT_FLAGS,
2393 CA_ADAPTER_GATT_BTLE,
2397 if (NULL == remoteEndpoint)
2399 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2400 return CA_STATUS_FAILED;
2403 // Create bleData to add to queue
2406 "Data received from LE layer [%d]",
2409 CALEData_t * const bleData =
2410 CACreateLEData(remoteEndpoint, data, dataLength, g_bleServerSenderInfo);
2414 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2415 CAFreeEndpoint(remoteEndpoint);
2416 return CA_MEMORY_ALLOC_FAILED;
2419 CAFreeEndpoint(remoteEndpoint);
2420 // Add message to receiver queue
2421 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2423 *sentLength = dataLength;
2425 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2426 return CA_STATUS_OK;
2429 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2430 const uint8_t *data,
2431 uint32_t dataLength,
2432 uint32_t *sentLength)
2434 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2437 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2438 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2439 #ifndef SINGLE_THREAD
2440 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG,
2441 "g_bleReceiverQueue",
2444 //Add message to data queue
2445 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2446 CA_ADAPTER_GATT_BTLE,
2448 if (NULL == remoteEndpoint)
2450 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2451 return CA_STATUS_FAILED;
2454 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%u]", dataLength);
2456 // Create bleData to add to queue
2457 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data,
2458 dataLength, g_bleClientSenderInfo);
2461 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2462 CAFreeEndpoint(remoteEndpoint);
2463 return CA_MEMORY_ALLOC_FAILED;
2466 CAFreeEndpoint(remoteEndpoint);
2467 // Add message to receiver queue
2468 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2470 *sentLength = dataLength;
2472 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2473 return CA_STATUS_OK;
2476 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2478 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2480 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2481 g_bleAdapterThreadPool = handle;
2482 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2484 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2487 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2489 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2491 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2493 g_networkPacketReceivedCallback = callback;
2495 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2497 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2500 static void CALEErrorHandler(const char *remoteAddress,
2501 const uint8_t *data,
2505 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2507 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2509 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2510 CA_ADAPTER_GATT_BTLE,
2514 // if required, will be used to build remote endpoint
2515 g_errorHandler(rep, data, dataLen, result);
2517 CAFreeEndpoint(rep);
2519 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");