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"
38 * Logging tag for module name.
40 #define CALEADAPTER_TAG "LAD"
44 * Stores the information of the Data to be sent from the queues.
46 * This structure will be pushed to the sender/receiver queue for
51 /// Remote endpoint contains the information of remote device.
52 CAEndpoint_t *remoteEndpoint;
54 /// Data to be transmitted over LE transport.
57 /// Length of the data being transmitted.
62 * Stores information of all the senders.
64 * This structure will be used to track and defragment all incoming
70 uint32_t totalDataLen;
72 CAEndpoint_t *remoteEndpoint;
76 * Callback to provide the status of the network change to CA layer.
78 static CANetworkChangeCallback g_networkCallback = NULL;
81 * bleAddress of the local adapter. Value will be initialized to zero,
82 * and will be updated later.
84 static char g_localBLEAddress[18] = { 0 };
87 * Variable to differentiate btw GattServer and GattClient.
89 static bool g_isServer = false;
92 * Mutex to synchronize the task to be executed on the GattServer
95 static ca_mutex g_bleIsServerMutex = NULL;
98 * Mutex to synchronize the callback to be called for the network
101 static ca_mutex g_bleNetworkCbMutex = NULL;
104 * Mutex to synchronize the updates of the local LE address of the
107 static ca_mutex g_bleLocalAddressMutex = NULL;
110 * Reference to thread pool.
112 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
115 * Mutex to synchronize the task to be pushed to thread pool.
117 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
120 * Mutex to synchronize the queing of the data from SenderQueue.
122 static ca_mutex g_bleClientSendDataMutex = NULL;
125 * Mutex to synchronize the queing of the data from ReceiverQueue.
127 static ca_mutex g_bleReceiveDataMutex = NULL;
131 * Mutex to synchronize the queing of the data from SenderQueue.
133 static ca_mutex g_bleServerSendDataMutex = NULL;
136 * Mutex to synchronize the callback to be called for the
137 * adapterReqResponse.
139 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
142 * Callback to be called when network packet received from either
143 * GattServer or GattClient.
145 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
148 * Callback to notify error from the BLE adapter.
150 static CAErrorHandleCallback g_errorHandler = NULL;
153 * Storing Adapter state information.
155 static CAAdapterState_t g_bleAdapterState = CA_ADAPTER_DISABLED;
160 * This enumeration provides information of LE Adapter Server status.
164 CA_SERVER_NOTSTARTED = 0,
170 * Structure to maintain the status of the server.
172 static CALeServerStatus gLeServerStatus = CA_SERVER_NOTSTARTED;
175 * Register network change notification callback.
177 * @param[in] netCallback CANetworkChangeCallback callback which will
178 * be set for the change in network.
180 * @return 0 on success otherwise a positive error value.
181 * @retval ::CA_STATUS_OK Successful.
182 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
183 * @retval ::CA_STATUS_FAILED Operation failed.
186 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
189 * Set the thread pool handle which is required for spawning new
192 * @param[in] handle Thread pool handle which is given by above layer
193 * for using thread creation task.
196 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
199 * Call the callback to the upper layer when the device state gets
202 * @param[in] adapter_state New state of the adapter to be notified to
205 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state);
208 * Used to initialize all required mutex variable for LE Adapter
211 * @return 0 on success otherwise a positive error value.
212 * @retval ::CA_STATUS_OK Successful.
213 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
214 * @retval ::CA_STATUS_FAILED Operation failed.
217 static CAResult_t CAInitLEAdapterMutex();
220 * Terminate all required mutex variables for LE adapter
223 static void CATerminateLEAdapterMutex();
226 * Prepares and notify error through error callback.
228 static void CALEErrorHandler(const char *remoteAddress,
233 #ifndef SINGLE_THREAD
235 * Stop condition of recvhandler.
237 static bool g_dataReceiverHandlerState = false;
240 * Sender information.
242 static u_arraylist_t *g_senderInfo = NULL;
245 * Queue to process the outgoing packets from GATTClient.
247 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
250 * Queue to process the incoming packets to GATT Client.
252 static CAQueueingThread_t *g_bleReceiverQueue = NULL;
255 * Queue to process the outgoing packets from GATTServer.
257 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
260 * This function will be associated with the sender queue for
263 * This function will fragment the data to the MTU of the transport
264 * and send the data in fragments to the adapters. The function will
265 * be blocked until all data is sent out from the adapter.
267 * @param[in] threadData Data pushed to the queue which contains the
268 * info about RemoteEndpoint and Data.
270 static void CALEServerSendDataThread(void *threadData);
273 * This function will be associated with the sender queue for
276 * This function will fragment the data to the MTU of the transport
277 * and send the data in fragments to the adapters. The function will
278 * be blocked until all data is sent out from the adapter.
280 * @param[in] threadData Data pushed to the queue which contains the
281 * info about RemoteEndpoint and Data.
283 static void CALEClientSendDataThread(void *threadData);
286 * This function will be associated with the receiver queue.
288 * This function will defragment the received data from each sender
289 * respectively and will send it up to CA layer. Respective sender's
290 * header will provide the length of the data sent.
292 * @param[in] threadData Data pushed to the queue which contains the
293 * info about RemoteEndpoint and Data.
295 static void CALEDataReceiverHandler(void *threadData);
298 * This function will stop all queues created for GattServer and
299 * GattClient. All four queues will be be stopped with this function
302 static void CAStopLEQueues();
305 * This function will terminate all queues created for GattServer and
306 * GattClient. All four queues will be be terminated with this
307 * function invocations.
309 static void CATerminateLEQueues();
312 * This function will initalize the Receiver and Sender queues for
313 * GattServer. This function will in turn call the functions
314 * CAInitBleServerReceiverQueue() and CAInitBleServerSenderQueue() to
315 * initialize the queues.
317 * @return ::CA_STATUS_OK or Appropriate error code.
318 * @retval ::CA_STATUS_OK Successful.
319 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
320 * @retval ::CA_STATUS_FAILED Operation failed.
322 static CAResult_t CAInitLEServerQueues();
325 * This function will initalize the Receiver and Sender queues for
326 * GattClient. This function will inturn call the functions
327 * CAInitBleClientReceiverQueue() and CAInitBleClientSenderQueue() to
328 * initialize the queues.
330 * @return ::CA_STATUS_OK or Appropriate error code.
331 * @retval ::CA_STATUS_OK Successful.
332 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
333 * @retval ::CA_STATUS_FAILED Operation failed.
336 static CAResult_t CAInitLEClientQueues();
339 * This function will initalize the Receiver queue for
340 * GattServer. This will initialize the queue to process the function
341 * CABLEServerSendDataThread() when ever the task is added to this
344 * @return ::CA_STATUS_OK or Appropriate error code.
345 * @retval ::CA_STATUS_OK Successful.
346 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
347 * @retval ::CA_STATUS_FAILED Operation failed.
349 static CAResult_t CAInitLEServerSenderQueue();
352 * This function will initalize the Receiver queue for
353 * GattClient. This will initialize the queue to process the function
354 * CABLEClientSendDataThread() when ever the task is added to this
357 * @return ::CA_STATUS_OK or Appropriate error code.
358 * @retval ::CA_STATUS_OK Successful.
359 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
360 * @retval ::CA_STATUS_FAILED Operation failed.
362 static CAResult_t CAInitLEClientSenderQueue();
365 * This function will initialize the Receiver queue for
366 * LEAdapter. This will initialize the queue to process the function
367 * CABLEDataReceiverHandler() when ever the task is added to this
370 * @return ::CA_STATUS_OK or Appropriate error code
371 * @retval ::CA_STATUS_OK Successful
372 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
373 * @retval ::CA_STATUS_FAILED Operation failed
376 static CAResult_t CAInitLEReceiverQueue();
379 * This function will create the Data required to send it in the
382 * @param[in] remoteEndpoint Remote endpoint information of the
384 * @param[in] data Data to be transmitted from LE.
385 * @param[in] dataLength Length of the Data being transmitted.
387 * @return ::CA_STATUS_OK or Appropriate error code.
388 * @retval ::CA_STATUS_OK Successful.
389 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
390 * @retval ::CA_STATUS_FAILED Operation failed.
392 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
394 uint32_t dataLength);
397 * Used to free the BLE information stored in the sender/receiver
400 * @param[in] bleData Information for a particular data segment.
402 static void CAFreeLEData(CALEData_t *bleData);
407 static void CALEDataDestroyer(void *data, uint32_t size);
409 static CAResult_t CAInitLEServerQueues()
411 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
413 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
415 CAResult_t result = CAInitLEServerSenderQueue();
416 if (CA_STATUS_OK != result)
418 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
419 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
420 return CA_STATUS_FAILED;
423 result = CAInitLEReceiverQueue();
424 if (CA_STATUS_OK != result)
426 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerReceiverQueue failed");
427 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
428 return CA_STATUS_FAILED;
431 g_dataReceiverHandlerState = true;
433 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
435 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
439 static CAResult_t CAInitLEClientQueues()
441 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
443 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
445 CAResult_t result = CAInitLEClientSenderQueue();
446 if (CA_STATUS_OK != result)
448 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
449 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
450 return CA_STATUS_FAILED;
453 result = CAInitLEReceiverQueue();
454 if (CA_STATUS_OK != result)
456 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientReceiverQueue failed");
457 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
458 return CA_STATUS_FAILED;
461 g_dataReceiverHandlerState = true;
463 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
465 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
469 static CAResult_t CAInitLEReceiverQueue()
471 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
472 // Check if the message queue is already initialized
473 if (g_bleReceiverQueue)
475 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
479 // Create recv message queue
480 g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
481 if (!g_bleReceiverQueue)
483 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
484 return CA_MEMORY_ALLOC_FAILED;
487 g_senderInfo = u_arraylist_create();
490 OIC_LOG(ERROR, CALEADAPTER_TAG, "ClientInfo memory allcation failed!");
491 OICFree(g_bleReceiverQueue);
492 g_bleReceiverQueue = NULL;
493 return CA_MEMORY_ALLOC_FAILED;
496 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue, g_bleAdapterThreadPool,
497 CALEDataReceiverHandler, CALEDataDestroyer))
499 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
500 OICFree(g_bleReceiverQueue);
501 g_bleReceiverQueue = NULL;
502 u_arraylist_free(&g_senderInfo);
503 return CA_STATUS_FAILED;
506 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue))
508 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
509 OICFree(g_bleReceiverQueue);
510 g_bleReceiverQueue = NULL;
511 u_arraylist_free(&g_senderInfo);
512 return CA_STATUS_FAILED;
515 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
519 static CAResult_t CAInitLEServerSenderQueue()
521 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
522 // Check if the message queue is already initialized
523 if (g_bleServerSendQueueHandle)
525 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
529 // Create send message queue
530 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
531 if (!g_bleServerSendQueueHandle)
533 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
534 return CA_MEMORY_ALLOC_FAILED;
537 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
538 g_bleAdapterThreadPool,
539 CALEServerSendDataThread, CALEDataDestroyer))
541 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
542 OICFree(g_bleServerSendQueueHandle);
543 g_bleServerSendQueueHandle = NULL;
544 return CA_STATUS_FAILED;
547 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerSendQueueHandle))
549 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
550 OICFree(g_bleServerSendQueueHandle);
551 g_bleServerSendQueueHandle = NULL;
552 return CA_STATUS_FAILED;
555 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
559 static void CALEClearSenderInfo()
561 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
563 uint32_t listIndex = 0;
564 uint32_t listLength = u_arraylist_length(g_senderInfo);
565 for (listIndex = 0; listIndex < listLength; listIndex++)
567 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(g_senderInfo, listIndex);
573 OICFree(info->defragData);
574 CAFreeEndpoint(info->remoteEndpoint);
577 u_arraylist_free(&g_senderInfo);
578 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
581 static CAResult_t CAInitLEClientSenderQueue()
583 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
585 if (g_bleClientSendQueueHandle)
587 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
591 // Create send message queue
592 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
593 if (!g_bleClientSendQueueHandle)
595 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
596 return CA_MEMORY_ALLOC_FAILED;
599 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
600 g_bleAdapterThreadPool,
601 CALEClientSendDataThread, CALEDataDestroyer))
603 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
604 OICFree(g_bleClientSendQueueHandle);
605 g_bleClientSendQueueHandle = NULL;
606 return CA_STATUS_FAILED;
609 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientSendQueueHandle))
611 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
612 OICFree(g_bleClientSendQueueHandle);
613 g_bleClientSendQueueHandle = NULL;
614 return CA_STATUS_FAILED;
617 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
621 static void CAStopLEQueues()
623 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
625 ca_mutex_lock(g_bleClientSendDataMutex);
626 if (NULL != g_bleClientSendQueueHandle)
628 CAQueueingThreadStop(g_bleClientSendQueueHandle);
630 ca_mutex_unlock(g_bleClientSendDataMutex);
632 ca_mutex_lock(g_bleServerSendDataMutex);
633 if (NULL != g_bleServerSendQueueHandle)
635 CAQueueingThreadStop(g_bleServerSendQueueHandle);
637 ca_mutex_unlock(g_bleServerSendDataMutex);
639 ca_mutex_lock(g_bleReceiveDataMutex);
640 if (NULL != g_bleReceiverQueue)
642 CAQueueingThreadStop(g_bleReceiverQueue);
644 ca_mutex_unlock(g_bleReceiveDataMutex);
646 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
649 static void CATerminateLEQueues()
651 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
653 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
654 OICFree(g_bleClientSendQueueHandle);
655 g_bleClientSendQueueHandle = NULL;
657 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
658 OICFree(g_bleServerSendQueueHandle);
659 g_bleServerSendQueueHandle = NULL;
661 CAQueueingThreadDestroy(g_bleReceiverQueue);
662 OICFree(g_bleReceiverQueue);
663 g_bleReceiverQueue = NULL;
665 CALEClearSenderInfo();
667 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
670 static CAResult_t CALEGetSenderInfo(const char *leAddress,
671 CABLESenderInfo_t **senderInfo,
672 uint32_t *senderIndex)
674 VERIFY_NON_NULL_RET(leAddress,
676 "NULL BLE address argument",
677 CA_STATUS_INVALID_PARAM);
678 VERIFY_NON_NULL_RET(senderIndex,
680 "NULL index argument",
681 CA_STATUS_INVALID_PARAM);
683 const uint32_t listLength = u_arraylist_length(g_senderInfo);
684 const uint32_t addrLength = strlen(leAddress);
685 for (uint32_t index = 0; index < listLength; index++)
687 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(g_senderInfo, index);
688 if(!info || !(info->remoteEndpoint))
693 if(!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
695 *senderIndex = index;
704 return CA_STATUS_FAILED;
707 static void CALEDataReceiverHandler(void *threadData)
709 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
711 ca_mutex_lock(g_bleReceiveDataMutex);
713 if (g_dataReceiverHandlerState)
715 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
717 CALEData_t *bleData = (CALEData_t *) threadData;
720 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
721 ca_mutex_unlock(g_bleReceiveDataMutex);
725 if(!(bleData->remoteEndpoint))
727 OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!");
728 ca_mutex_unlock(g_bleReceiveDataMutex);
732 CABLESenderInfo_t *senderInfo = NULL;
733 uint32_t senderIndex = 0;
735 if(CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
736 &senderInfo, &senderIndex))
738 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s]",
739 bleData->remoteEndpoint->addr);
744 CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
747 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
748 ca_mutex_unlock(g_bleReceiveDataMutex);
751 newSender->recvDataLen = 0;
752 newSender->totalDataLen = 0;
753 newSender->defragData = NULL;
754 newSender->remoteEndpoint = NULL;
756 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
757 newSender->totalDataLen = CAParseHeader(bleData->data,
759 if(!(newSender->totalDataLen))
761 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
763 ca_mutex_unlock(g_bleReceiveDataMutex);
767 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
768 newSender->totalDataLen);
769 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
772 newSender->defragData = OICCalloc(newSender->totalDataLen + 1,
773 sizeof(*newSender->defragData));
775 if (NULL == newSender->defragData)
777 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
779 ca_mutex_unlock(g_bleReceiveDataMutex);
783 const char *remoteAddress = bleData->remoteEndpoint->addr;
784 newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
785 CA_ADAPTER_GATT_BTLE, remoteAddress, 0);
786 if (NULL == newSender->remoteEndpoint)
788 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
789 OICFree(newSender->defragData);
791 ca_mutex_unlock(g_bleReceiveDataMutex);
794 memcpy(newSender->defragData, bleData->data + CA_HEADER_LENGTH,
795 bleData->dataLen - CA_HEADER_LENGTH);
796 newSender->recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
797 u_arraylist_add(g_senderInfo,(void *)newSender);
799 //Getting newSender index position in g_senderInfo array list
801 CALEGetSenderInfo(newSender->remoteEndpoint->addr, NULL, &senderIndex))
803 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
804 OICFree(newSender->defragData);
805 CAFreeEndpoint(newSender->remoteEndpoint);
807 ca_mutex_unlock(g_bleReceiveDataMutex);
810 senderInfo = newSender;
814 if(senderInfo->recvDataLen + bleData->dataLen > senderInfo->totalDataLen)
816 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
817 "Data Length exceeding error!! Receiving [%d] total length [%d]",
818 senderInfo->recvDataLen + bleData->dataLen, senderInfo->totalDataLen);
819 u_arraylist_remove(g_senderInfo, senderIndex);
820 OICFree(senderInfo->defragData);
822 ca_mutex_unlock(g_bleReceiveDataMutex);
825 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
826 memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data,
828 senderInfo->recvDataLen += bleData->dataLen ;
829 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
830 senderInfo->totalDataLen, senderInfo->recvDataLen);
833 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
835 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
836 if (NULL == g_networkPacketReceivedCallback)
838 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
840 u_arraylist_remove(g_senderInfo, senderIndex);
841 OICFree(senderInfo->defragData);
843 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
844 ca_mutex_unlock(g_bleReceiveDataMutex);
848 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
850 const CASecureEndpoint_t tmp =
852 .endpoint = *senderInfo->remoteEndpoint
855 g_networkPacketReceivedCallback(&tmp,
856 senderInfo->defragData,
857 senderInfo->recvDataLen);
858 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
859 u_arraylist_remove(g_senderInfo, senderIndex);
860 senderInfo->remoteEndpoint = NULL;
861 senderInfo->defragData = NULL;
865 ca_mutex_unlock(g_bleReceiveDataMutex);
866 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
869 static void CALEServerSendDataThread(void *threadData)
871 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
873 CALEData_t * const bleData = (CALEData_t *) threadData;
876 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
880 uint8_t * const header = OICCalloc(CA_HEADER_LENGTH, 1);
881 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
883 const uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
887 "Server total Data length with header is [%u]",
890 uint8_t * const dataSegment = OICCalloc(totalLength, 1);
892 if (NULL == dataSegment)
894 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
899 CAResult_t result = CAGenerateHeader(header,
902 if (CA_STATUS_OK != result )
904 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
906 OICFree(dataSegment);
910 memcpy(dataSegment, header, CA_HEADER_LENGTH);
914 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
916 length = totalLength;
917 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
921 length = CA_SUPPORTED_BLE_MTU_SIZE;
922 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
923 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
926 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
929 // Send the first segment with the header.
930 if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
932 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
934 result = CAUpdateCharacteristicsToGattClient(
935 bleData->remoteEndpoint->addr, dataSegment, length);
937 if (CA_STATUS_OK != result)
941 "Update characteristics failed, result [%d]",
944 g_errorHandler(bleData->remoteEndpoint,
948 OICFree(dataSegment);
954 "Server Sent data length [%u]",
956 for (index = 1; index < iter; index++)
958 // Send the remaining header.
961 "Sending the chunk number [%u]",
965 CAUpdateCharacteristicsToGattClient(
966 bleData->remoteEndpoint->addr,
967 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
968 CA_SUPPORTED_BLE_MTU_SIZE);
969 if (CA_STATUS_OK != result)
971 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
972 "Update characteristics failed, result [%d]", result);
973 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
974 OICFree(dataSegment);
977 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
978 CA_SUPPORTED_BLE_MTU_SIZE);
981 const uint32_t remainingLen =
982 totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
984 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
986 // send the last segment of the data (Ex: 22 bytes of 622
987 // bytes of data when MTU is 200)
988 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
989 result = CAUpdateCharacteristicsToGattClient(
990 bleData->remoteEndpoint->addr,
991 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
993 if (CA_STATUS_OK != result)
997 "Update characteristics failed, result [%d]",
999 g_errorHandler(bleData->remoteEndpoint,
1003 OICFree(dataSegment);
1006 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1011 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
1012 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
1013 if (CA_STATUS_OK != result)
1015 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1017 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1018 OICFree(dataSegment);
1021 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1022 for (index = 1; index < iter; index++)
1024 // Send the remaining header.
1025 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1026 result = CAUpdateCharacteristicsToAllGattClients(
1027 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1028 CA_SUPPORTED_BLE_MTU_SIZE);
1029 if (CA_STATUS_OK != result)
1031 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1033 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1034 OICFree(dataSegment);
1037 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%u]", CA_SUPPORTED_BLE_MTU_SIZE);
1040 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1041 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1043 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1044 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1045 result = CAUpdateCharacteristicsToAllGattClients(
1046 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1048 if (CA_STATUS_OK != result)
1050 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1052 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1053 OICFree(dataSegment);
1056 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1059 OICFree(dataSegment);
1061 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1064 static void CALEClientSendDataThread(void *threadData)
1066 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1068 CALEData_t *bleData = (CALEData_t *) threadData;
1071 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1075 uint8_t * const header = OICCalloc(CA_HEADER_LENGTH, 1);
1076 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1078 const uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1079 uint8_t *dataSegment = OICCalloc(totalLength, 1);
1080 if (NULL == dataSegment)
1082 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1087 CAResult_t result = CAGenerateHeader(header,
1090 if (CA_STATUS_OK != result )
1092 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1094 OICFree(dataSegment);
1097 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1100 uint32_t length = 0;
1101 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1103 length = totalLength;
1104 memcpy(dataSegment + CA_HEADER_LENGTH,
1110 length = CA_SUPPORTED_BLE_MTU_SIZE;
1111 memcpy(dataSegment + CA_HEADER_LENGTH,
1113 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1116 const uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1118 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1120 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1121 // Send the first segment with the header.
1123 CAUpdateCharacteristicsToGattServer(
1124 bleData->remoteEndpoint->addr,
1130 if (CA_STATUS_OK != result)
1134 "Update characteristics failed, result [%d]",
1136 g_errorHandler(bleData->remoteEndpoint,
1140 OICFree(dataSegment);
1146 "Client Sent Data length is [%u]",
1149 for (index = 1; index < iter; index++)
1151 // Send the remaining header.
1152 result = CAUpdateCharacteristicsToGattServer(
1153 bleData->remoteEndpoint->addr,
1154 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1155 CA_SUPPORTED_BLE_MTU_SIZE,
1157 if (CA_STATUS_OK != result)
1161 "Update characteristics failed, result [%d]",
1163 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1164 OICFree(dataSegment);
1167 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1168 CA_SUPPORTED_BLE_MTU_SIZE);
1171 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1172 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1174 // send the last segment of the data (Ex: 22 bytes of 622
1175 // bytes of data when MTU is 200)
1176 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1177 result = CAUpdateCharacteristicsToGattServer(
1178 bleData->remoteEndpoint->addr,
1179 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1183 if (CA_STATUS_OK != result)
1185 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1187 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1188 OICFree(dataSegment);
1191 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1196 //Sending Mulitcast Data
1197 // Send the first segment with the header.
1198 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1199 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1200 if (CA_STATUS_OK != result)
1202 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1203 "Update characteristics (all) failed, result [%d]", result);
1204 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1205 OICFree(dataSegment);
1208 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1209 // Send the remaining header.
1210 for (index = 1; index < iter; index++)
1212 result = CAUpdateCharacteristicsToAllGattServers(
1213 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1214 CA_SUPPORTED_BLE_MTU_SIZE);
1215 if (CA_STATUS_OK != result)
1217 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]",
1219 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1220 OICFree(dataSegment);
1223 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1224 CA_SUPPORTED_BLE_MTU_SIZE);
1227 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1228 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1230 // send the last segment of the data (Ex: 22 bytes of 622
1231 // bytes of data when MTU is 200)
1232 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1234 CAUpdateCharacteristicsToAllGattServers(
1235 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1237 if (CA_STATUS_OK != result)
1239 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1240 "Update characteristics (all) failed, result [%d]", result);
1241 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1242 OICFree(dataSegment);
1245 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1250 OICFree(dataSegment);
1252 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1255 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
1256 const uint8_t *data,
1257 uint32_t dataLength)
1259 CALEData_t * const bleData = OICMalloc(sizeof(CALEData_t));
1263 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1267 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1268 bleData->data = OICCalloc(dataLength + 1, 1);
1270 if (NULL == bleData->data)
1272 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1273 CAFreeLEData(bleData);
1277 memcpy(bleData->data, data, dataLength);
1278 bleData->dataLen = dataLength;
1283 static void CAFreeLEData(CALEData_t *bleData)
1285 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1287 CAFreeEndpoint(bleData->remoteEndpoint);
1288 OICFree(bleData->data);
1292 static void CALEDataDestroyer(void *data, uint32_t size)
1294 if ((size_t)size < sizeof(CALEData_t *))
1296 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1297 "Destroy data too small %p %d", data, size);
1299 CALEData_t *ledata = (CALEData_t *) data;
1301 CAFreeLEData(ledata);
1305 static CAResult_t CAInitLEAdapterMutex()
1307 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1309 if (NULL == g_bleIsServerMutex)
1311 g_bleIsServerMutex = ca_mutex_new();
1312 if (NULL == g_bleIsServerMutex)
1314 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1315 return CA_STATUS_FAILED;
1319 if (NULL == g_bleNetworkCbMutex)
1321 g_bleNetworkCbMutex = ca_mutex_new();
1322 if (NULL == g_bleNetworkCbMutex)
1324 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1325 CATerminateLEAdapterMutex();
1326 return CA_STATUS_FAILED;
1330 if (NULL == g_bleLocalAddressMutex)
1332 g_bleLocalAddressMutex = ca_mutex_new();
1333 if (NULL == g_bleLocalAddressMutex)
1335 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1336 CATerminateLEAdapterMutex();
1337 return CA_STATUS_FAILED;
1341 if (NULL == g_bleAdapterThreadPoolMutex)
1343 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1344 if (NULL == g_bleAdapterThreadPoolMutex)
1346 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1347 CATerminateLEAdapterMutex();
1348 return CA_STATUS_FAILED;
1352 if (NULL == g_bleClientSendDataMutex)
1354 g_bleClientSendDataMutex = ca_mutex_new();
1355 if (NULL == g_bleClientSendDataMutex)
1357 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1358 CATerminateLEAdapterMutex();
1359 return CA_STATUS_FAILED;
1363 if (NULL == g_bleServerSendDataMutex)
1365 g_bleServerSendDataMutex = ca_mutex_new();
1366 if (NULL == g_bleServerSendDataMutex)
1368 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1369 CATerminateLEAdapterMutex();
1370 return CA_STATUS_FAILED;
1374 if (NULL == g_bleAdapterReqRespCbMutex)
1376 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1377 if (NULL == g_bleAdapterReqRespCbMutex)
1379 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1380 CATerminateLEAdapterMutex();
1381 return CA_STATUS_FAILED;
1385 if (NULL == g_bleReceiveDataMutex)
1387 g_bleReceiveDataMutex = ca_mutex_new();
1388 if (NULL == g_bleReceiveDataMutex)
1390 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1391 return CA_STATUS_FAILED;
1395 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1396 return CA_STATUS_OK;
1399 static void CATerminateLEAdapterMutex()
1401 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1403 ca_mutex_free(g_bleIsServerMutex);
1404 g_bleIsServerMutex = NULL;
1406 ca_mutex_free(g_bleNetworkCbMutex);
1407 g_bleNetworkCbMutex = NULL;
1409 ca_mutex_free(g_bleLocalAddressMutex);
1410 g_bleLocalAddressMutex = NULL;
1412 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1413 g_bleAdapterThreadPoolMutex = NULL;
1415 ca_mutex_free(g_bleClientSendDataMutex);
1416 g_bleClientSendDataMutex = NULL;
1418 ca_mutex_free(g_bleServerSendDataMutex);
1419 g_bleServerSendDataMutex = NULL;
1421 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1422 g_bleAdapterReqRespCbMutex = NULL;
1424 ca_mutex_free(g_bleReceiveDataMutex);
1425 g_bleReceiveDataMutex = NULL;
1427 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1431 * Starting LE connectivity adapters.
1433 * As its peer to peer it does not require to start any servers.
1435 * @return ::CA_STATUS_OK or Appropriate error code.
1437 static CAResult_t CAStartLE();
1440 * Start listening server for receiving multicast search requests.
1442 * Transport Specific Behavior:
1443 * LE Starts GATT Server with prefixed UUID and Characteristics
1444 * per OIC Specification.
1445 * @return ::CA_STATUS_OK or Appropriate error code.
1447 static CAResult_t CAStartLEListeningServer();
1450 * Sarting discovery of servers for receiving multicast
1453 * Transport Specific Behavior:
1454 * LE Starts GATT Server with prefixed UUID and Characteristics
1455 * per OIC Specification.
1457 * @return ::CA_STATUS_OK or Appropriate error code
1459 static CAResult_t CAStartLEDiscoveryServer();
1462 * Send data to the endpoint using the adapter connectivity.
1464 * @param[in] endpoint Remote Endpoint information (like MAC address,
1465 * reference URI and connectivity type) to which
1466 * the unicast 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 CASendLEUnicastData(const CAEndpoint_t *endpoint,
1479 * Send multicast data to the endpoint using the LE connectivity.
1481 * @param[in] endpoint Remote Endpoint information to which the
1482 * multicast data has to be sent.
1483 * @param[in] data Data which required to be sent.
1484 * @param[in] dataLen Size of data to be sent.
1486 * @note dataLen must be > 0.
1488 * @return The number of bytes sent on the network, or -1 on error.
1490 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1495 * Get LE Connectivity network information.
1497 * @param[out] info Local connectivity information structures.
1498 * @param[out] size Number of local connectivity structures.
1500 * @return ::CA_STATUS_OK or Appropriate error code.
1502 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info,
1506 * Read Synchronous API callback.
1508 * @return ::CA_STATUS_OK or Appropriate error code.
1510 static CAResult_t CAReadLEData();
1513 * Stopping the adapters and close socket connections.
1515 * LE Stops all GATT servers and GATT Clients.
1517 * @return ::CA_STATUS_OK or Appropriate error code.
1519 static CAResult_t CAStopLE();
1522 * Terminate the LE connectivity adapter.
1524 * Configuration information will be deleted from further use.
1526 static void CATerminateLE();
1529 * This function will receive the data from the GattServer and add the
1530 * data to the Server receiver queue.
1532 * @param[in] remoteAddress Remote address of the device from where
1534 * @param[in] data Actual data recevied from the remote
1536 * @param[in] dataLength Length of the data received from the
1538 * @param[in] sentLength Length of the data sent from the remote
1541 * @return ::CA_STATUS_OK or Appropriate error code.
1542 * @retval ::CA_STATUS_OK Successful.
1543 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1544 * @retval ::CA_STATUS_FAILED Operation failed.
1547 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
1548 const uint8_t *data,
1549 uint32_t dataLength,
1550 uint32_t *sentLength);
1553 * This function will receive the data from the GattClient and add the
1554 * data into the Client receiver queue.
1556 * @param[in] remoteAddress Remote address of the device from where
1558 * @param[in] data Actual data recevied from the remote
1560 * @param[in] dataLength Length of the data received from the
1562 * @param[in] sentLength Length of the data sent from the remote
1565 * @return ::CA_STATUS_OK or Appropriate error code.
1566 * @retval ::CA_STATUS_OK Successful.
1567 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1568 * @retval ::CA_STATUS_FAILED Operation failed.
1570 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
1571 const uint8_t *data,
1572 uint32_t dataLength,
1573 uint32_t *sentLength);
1576 * Set the NetworkPacket received callback to CA layer from adapter
1579 * @param[in] callback Callback handle sent from the upper layer.
1581 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
1584 * Push the data from CA layer to the Sender processor queue.
1586 * @param[in] remoteEndpoint Remote endpoint information of the
1588 * @param[in] data Data to be transmitted from LE.
1589 * @param[in] dataLen Length of the Data being transmitted.
1591 * @return ::CA_STATUS_OK or Appropriate error code.
1592 * @retval ::CA_STATUS_OK Successful.
1593 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1594 * @retval ::CA_STATUS_FAILED Operation failed.
1596 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1597 const uint8_t *data,
1601 * Push the data from CA layer to the Sender processor queue.
1603 * @param[in] remoteEndpoint Remote endpoint information of the
1605 * @param[in] data Data to be transmitted from LE.
1606 * @param[in] dataLen Length of the Data being transmitted.
1608 * @return ::CA_STATUS_OK or Appropriate error code.
1609 * @retval ::CA_STATUS_OK Successful.
1610 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1611 * @retval ::CA_STATUS_FAILED Operation failed.
1613 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1614 const uint8_t *data,
1617 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1618 CANetworkPacketReceivedCallback reqRespCallback,
1619 CANetworkChangeCallback netCallback,
1620 CAErrorHandleCallback errorCallback,
1621 ca_thread_pool_t handle)
1623 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1626 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1627 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1628 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1630 CAResult_t result = CA_STATUS_OK;
1631 result = CAInitLEAdapterMutex();
1632 if (CA_STATUS_OK != result)
1634 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1635 return CA_STATUS_FAILED;
1637 result = CAInitializeLENetworkMonitor();
1638 if (CA_STATUS_OK != result)
1640 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1641 return CA_STATUS_FAILED;
1644 CAInitializeLEAdapter();
1646 CASetLEClientThreadPoolHandle(handle);
1647 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1648 CASetLEServerThreadPoolHandle(handle);
1649 CASetLEAdapterThreadPoolHandle(handle);
1650 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1651 CASetLEReqRespAdapterCallback(reqRespCallback);
1653 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1654 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1655 CALERegisterNetworkNotifications(netCallback);
1657 g_errorHandler = errorCallback;
1659 static const CAConnectivityHandler_t connHandler =
1661 .startAdapter = CAStartLE,
1662 .stopAdapter = CAStopLE,
1663 .startListenServer = CAStartLEListeningServer,
1664 .startDiscoveryServer = CAStartLEDiscoveryServer,
1665 .sendData = CASendLEUnicastData,
1666 .sendDataToAll = CASendLEMulticastData,
1667 .GetnetInfo = CAGetLEInterfaceInformation,
1668 .readData = CAReadLEData,
1669 .terminate = CATerminateLE
1672 registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
1674 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1676 return CA_STATUS_OK;
1679 static CAResult_t CAStartLE()
1681 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE");
1683 return CAStartLEAdapter();
1686 static CAResult_t CAStopLE()
1688 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1689 #ifndef SINGLE_THREAD
1693 ca_mutex_lock(g_bleIsServerMutex);
1694 if (true == g_isServer)
1696 CAStopLEGattServer();
1700 CAStopLEGattClient();
1702 ca_mutex_unlock(g_bleIsServerMutex);
1704 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1706 return CA_STATUS_OK;
1709 static void CATerminateLE()
1711 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1713 CASetLEReqRespServerCallback(NULL);
1714 CASetLEReqRespClientCallback(NULL);
1715 CALERegisterNetworkNotifications(NULL);
1716 CASetLEReqRespAdapterCallback(NULL);
1717 CATerminateLENetworkMonitor();
1719 ca_mutex_lock(g_bleIsServerMutex);
1720 if (true == g_isServer)
1722 CATerminateLEGattServer();
1726 CATerminateLEGattClient();
1728 ca_mutex_unlock(g_bleIsServerMutex);
1730 #ifndef SINGLE_THREAD
1731 CATerminateLEQueues();
1733 CATerminateLEAdapterMutex();
1735 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1738 static CAResult_t CAStartLEListeningServer()
1740 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1741 #ifndef ROUTING_GATEWAY
1742 CAResult_t result = CA_STATUS_OK;
1743 #ifndef SINGLE_THREAD
1744 result = CAInitLEServerQueues();
1745 if (CA_STATUS_OK != result)
1747 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1748 return CA_STATUS_FAILED;
1752 result = CAGetLEAdapterState();
1753 if (CA_ADAPTER_NOT_ENABLED == result)
1755 gLeServerStatus = CA_LISTENING_SERVER;
1756 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1757 return CA_STATUS_OK;
1760 if (CA_STATUS_FAILED == result)
1762 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1763 return CA_STATUS_FAILED;
1766 CAStartLEGattServer();
1768 ca_mutex_lock(g_bleIsServerMutex);
1770 ca_mutex_unlock(g_bleIsServerMutex);
1772 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1773 return CA_STATUS_OK;
1775 // Routing Gateway only supports BLE client mode.
1776 OIC_LOG(ERROR, CALEADAPTER_TAG, "LE server not supported in Routing Gateway");
1777 return CA_NOT_SUPPORTED;
1781 static CAResult_t CAStartLEDiscoveryServer()
1783 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1784 CAResult_t result = CA_STATUS_OK;
1785 #ifndef SINGLE_THREAD
1786 result = CAInitLEClientQueues();
1787 if (CA_STATUS_OK != result)
1789 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1790 return CA_STATUS_FAILED;
1793 result = CAGetLEAdapterState();
1794 if (CA_ADAPTER_NOT_ENABLED == result)
1796 gLeServerStatus = CA_DISCOVERY_SERVER;
1797 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1798 return CA_STATUS_OK;
1801 if (CA_STATUS_FAILED == result)
1803 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1804 return CA_STATUS_FAILED;
1807 CAStartLEGattClient();
1809 ca_mutex_lock(g_bleIsServerMutex);
1811 ca_mutex_unlock(g_bleIsServerMutex);
1813 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1814 return CA_STATUS_OK;
1817 static CAResult_t CAReadLEData()
1819 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1820 #ifdef SINGLE_THREAD
1823 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1824 return CA_STATUS_OK;
1827 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1831 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1834 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1835 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1837 CAResult_t result = CA_STATUS_FAILED;
1839 ca_mutex_lock(g_bleIsServerMutex);
1840 if (true == g_isServer)
1842 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1843 if (CA_STATUS_OK != result)
1845 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed\n");
1848 g_errorHandler(endpoint, data, dataLen, result);
1850 ca_mutex_unlock(g_bleIsServerMutex);
1856 result = CALEAdapterClientSendData(endpoint, data, dataLen);
1857 if (CA_STATUS_OK != result)
1859 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed \n");
1862 g_errorHandler(endpoint, data, dataLen, result);
1864 ca_mutex_unlock(g_bleIsServerMutex);
1868 ca_mutex_unlock(g_bleIsServerMutex);
1870 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1874 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1878 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1881 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1885 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
1889 CAResult_t result = CA_STATUS_FAILED;
1891 ca_mutex_lock(g_bleIsServerMutex);
1892 if (true == g_isServer)
1894 result = CALEAdapterServerSendData(NULL, data, dataLen);
1895 if (CA_STATUS_OK != result)
1897 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data failed" );
1899 ca_mutex_unlock(g_bleIsServerMutex);
1902 g_errorHandler(endpoint, data, dataLen, result);
1909 result = CALEAdapterClientSendData(NULL, data, dataLen);
1910 if (CA_STATUS_OK != result)
1912 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data failed" );
1915 g_errorHandler(endpoint, data, dataLen, result);
1917 ca_mutex_unlock(g_bleIsServerMutex);
1921 ca_mutex_unlock(g_bleIsServerMutex);
1923 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1927 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
1929 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1931 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
1933 char *local_address = NULL;
1935 CAResult_t res = CAGetLEAddress(&local_address);
1936 if (CA_STATUS_OK != res)
1938 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
1942 if (NULL == local_address)
1944 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
1945 return CA_STATUS_FAILED;
1949 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
1950 if (NULL == (*info))
1952 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
1953 OICFree(local_address);
1954 return CA_STATUS_FAILED;
1957 size_t local_address_len = strlen(local_address);
1959 if(local_address_len >= sizeof(g_localBLEAddress) ||
1960 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
1962 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
1964 OICFree(local_address);
1965 return CA_STATUS_FAILED;
1968 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
1969 ca_mutex_lock(g_bleLocalAddressMutex);
1970 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
1971 ca_mutex_unlock(g_bleLocalAddressMutex);
1973 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
1975 OICFree(local_address);
1977 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1978 return CA_STATUS_OK;
1981 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
1983 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1985 ca_mutex_lock(g_bleNetworkCbMutex);
1986 g_networkCallback = netCallback;
1987 ca_mutex_unlock(g_bleNetworkCbMutex);
1988 CAResult_t res = CA_STATUS_OK;
1991 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
1992 if (CA_STATUS_OK != res)
1994 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1999 res = CAUnSetLEAdapterStateChangedCb();
2000 if (CA_STATUS_OK != res)
2002 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2006 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2010 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
2012 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2014 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
2015 CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE };
2017 ca_mutex_lock(g_bleLocalAddressMutex);
2018 OICStrcpy(localEndpoint.addr,
2019 sizeof(localEndpoint.addr),
2021 ca_mutex_unlock(g_bleLocalAddressMutex);
2023 g_bleAdapterState = adapter_state;
2024 // Start a GattServer/Client if gLeServerStatus is SET
2025 if (CA_LISTENING_SERVER == gLeServerStatus)
2027 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
2028 CAStartLEGattServer();
2030 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
2032 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
2033 CAStartLEGattClient();
2035 gLeServerStatus = CA_SERVER_NOTSTARTED;
2037 ca_mutex_lock(g_bleNetworkCbMutex);
2038 if (NULL != g_networkCallback)
2040 g_networkCallback(&localEndpoint, adapter_state);
2044 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
2046 ca_mutex_unlock(g_bleNetworkCbMutex);
2048 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2051 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
2052 const uint8_t *data,
2055 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2057 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2058 #ifndef SINGLE_THREAD
2059 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2060 "g_bleClientSendQueueHandle is NULL",
2062 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
2063 "g_bleClientSendDataMutex is NULL",
2066 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
2069 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
2071 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
2074 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2075 return CA_MEMORY_ALLOC_FAILED;
2077 // Add message to send queue
2078 ca_mutex_lock(g_bleClientSendDataMutex);
2079 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
2080 ca_mutex_unlock(g_bleClientSendDataMutex);
2082 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2083 return CA_STATUS_OK;
2086 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2087 const uint8_t *data,
2090 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2092 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2094 #ifdef SINGLE_THREAD
2095 uint8_t header[CA_HEADER_LENGTH] = { 0 };
2098 CAGenerateHeader(header, CA_HEADER_LENGTH, dataLen);
2100 if (CA_STATUS_OK != result)
2102 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
2103 return CA_STATUS_FAILED;
2106 if (!CAIsLEConnected())
2108 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2109 return CA_STATUS_FAILED;
2112 result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH);
2113 if (CA_STATUS_OK != result)
2115 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2116 return CA_STATUS_FAILED;
2119 const uint32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2120 for (uint32_t iter = 0; iter < dataLimit; iter++)
2123 CAUpdateCharacteristicsToAllGattClients(
2124 data + (iter * CA_SUPPORTED_BLE_MTU_SIZE),
2125 CA_SUPPORTED_BLE_MTU_SIZE);
2127 if (CA_STATUS_OK != result)
2129 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2130 return CA_STATUS_FAILED;
2136 const uint32_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2140 CAUpdateCharacteristicsToAllGattClients(
2141 data + (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE),
2143 if (CA_STATUS_OK != result)
2145 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2146 return CA_STATUS_FAILED;
2151 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2152 "BleClientReceiverQueue is NULL",
2154 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2155 "BleClientSendDataMutex is NULL",
2158 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2161 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2163 CALEData_t * const bleData =
2164 CACreateLEData(remoteEndpoint, data, dataLen);
2168 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2169 return CA_MEMORY_ALLOC_FAILED;
2172 // Add message to send queue
2173 ca_mutex_lock(g_bleServerSendDataMutex);
2174 CAQueueingThreadAddData(g_bleServerSendQueueHandle,
2176 sizeof(CALEData_t));
2177 ca_mutex_unlock(g_bleServerSendDataMutex);
2179 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2180 return CA_STATUS_OK;
2183 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2184 const uint8_t *data,
2185 uint32_t dataLength,
2186 uint32_t *sentLength)
2188 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2191 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2192 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2194 #ifdef SINGLE_THREAD
2195 if(g_networkPacketReceivedCallback)
2197 // will be filled by upper layer
2198 const CASecureEndpoint_t endpoint =
2199 { .endpoint = { .adapter = CA_ADAPTER_GATT_BTLE } };
2202 g_networkPacketReceivedCallback(&endpoint, data, dataLength);
2205 VERIFY_NON_NULL_RET(g_bleReceiverQueue,
2207 "g_bleReceiverQueue",
2210 //Add message to data queue
2211 CAEndpoint_t * const remoteEndpoint =
2212 CACreateEndpointObject(CA_DEFAULT_FLAGS,
2213 CA_ADAPTER_GATT_BTLE,
2217 if (NULL == remoteEndpoint)
2219 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2220 return CA_STATUS_FAILED;
2223 // Create bleData to add to queue
2226 "Data received from LE layer [%d]",
2229 CALEData_t * const bleData =
2230 CACreateLEData(remoteEndpoint, data, dataLength);
2234 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2235 CAFreeEndpoint(remoteEndpoint);
2236 return CA_MEMORY_ALLOC_FAILED;
2239 CAFreeEndpoint(remoteEndpoint);
2240 // Add message to receiver queue
2241 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2243 *sentLength = dataLength;
2245 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2246 return CA_STATUS_OK;
2249 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2250 const uint8_t *data,
2251 uint32_t dataLength,
2252 uint32_t *sentLength)
2254 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2257 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2258 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2259 #ifndef SINGLE_THREAD
2260 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG, "g_bleReceiverQueue",
2263 //Add message to data queue
2264 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2265 CA_ADAPTER_GATT_BTLE,
2267 if (NULL == remoteEndpoint)
2269 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2270 return CA_STATUS_FAILED;
2273 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%u]", dataLength);
2275 // Create bleData to add to queue
2276 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
2279 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2280 CAFreeEndpoint(remoteEndpoint);
2281 return CA_MEMORY_ALLOC_FAILED;
2284 CAFreeEndpoint(remoteEndpoint);
2285 // Add message to receiver queue
2286 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2288 *sentLength = dataLength;
2290 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2291 return CA_STATUS_OK;
2294 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2296 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2298 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2299 g_bleAdapterThreadPool = handle;
2300 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2302 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2305 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2307 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2309 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2311 g_networkPacketReceivedCallback = callback;
2313 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2315 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2318 static void CALEErrorHandler(const char *remoteAddress,
2319 const uint8_t *data,
2323 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2325 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2327 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2328 CA_ADAPTER_GATT_BTLE,
2332 // if required, will be used to build remote endpoint
2333 g_errorHandler(rep, data, dataLen, result);
2335 CAFreeEndpoint(rep);
2337 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");