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 #if defined(__TIZEN__) || defined(__ANDROID__)
33 #include "caleserver.h"
34 #include "caleclient.h"
36 #include "oic_malloc.h"
37 #include "oic_string.h"
38 #include "caremotehandler.h"
42 * Logging tag for module name.
44 #define CALEADAPTER_TAG "OIC_CA_LE_ADAP"
47 * The MTU supported for BLE adapter
49 #define CA_SUPPORTED_BLE_MTU_SIZE 20
52 * Stores information of all the senders.
54 * This structure will be used to track and defragment all incoming
60 uint32_t totalDataLen;
62 CAEndpoint_t *remoteEndpoint;
68 ADAPTER_BOTH_CLIENT_SERVER,
74 * Callback to provide the status of the network change to CA layer.
76 static CAAdapterChangeCallback g_networkCallback = NULL;
79 * Callback to provide the status of the connection change to CA layer.
81 static CAConnectionChangeCallback g_connectionCallback = NULL;
84 * bleAddress of the local adapter. Value will be initialized to zero,
85 * and will be updated later.
87 static char g_localBLEAddress[18] = { 0 };
90 * Variable to differentiate btw GattServer and GattClient.
92 static CABLEAdapter_t g_adapterType = ADAPTER_EMPTY;
95 * Mutex to synchronize the task to be executed on the GattServer
98 static ca_mutex g_bleIsServerMutex = NULL;
101 * Mutex to synchronize the callback to be called for the network
104 static ca_mutex g_bleNetworkCbMutex = NULL;
107 * Mutex to synchronize the updates of the local LE address of the
110 static ca_mutex g_bleLocalAddressMutex = NULL;
113 * Reference to thread pool.
115 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
118 * Mutex to synchronize the task to be pushed to thread pool.
120 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
123 * Mutex to synchronize the queing of the data from SenderQueue.
125 static ca_mutex g_bleClientSendDataMutex = NULL;
128 * Mutex to synchronize the queing of the data from ReceiverQueue.
130 static ca_mutex g_bleReceiveDataMutex = NULL;
133 * Mutex to synchronize the queing of the data from SenderQueue.
135 static ca_mutex g_bleServerSendDataMutex = NULL;
138 * Mutex to synchronize the callback to be called for the
139 * adapterReqResponse.
141 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
144 * Callback to be called when network packet received from either
145 * GattServer or GattClient.
147 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
150 * Callback to notify error from the BLE adapter.
152 static CAErrorHandleCallback g_errorHandler = NULL;
155 * Register network change notification callback.
157 * @param[in] netCallback CAAdapterChangeCallback callback which will
158 * be set for the change in adapter.
159 * @param[in] connCallback CAConnectionChangeCallback callback which will
160 * be set for the change in connection.
162 * @return 0 on success otherwise a positive error value.
163 * @retval ::CA_STATUS_OK Successful.
164 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
165 * @retval ::CA_STATUS_FAILED Operation failed.
168 static CAResult_t CALERegisterNetworkNotifications(CAAdapterChangeCallback netCallback,
169 CAConnectionChangeCallback connCallback);
172 * Set the thread pool handle which is required for spawning new
175 * @param[in] handle Thread pool handle which is given by above layer
176 * for using thread creation task.
179 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
182 * Call the callback to the upper layer when the adapter state gets
185 * @param[in] adapter_state New state of the adapter to be notified to
188 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state);
191 * Call the callback to the upper layer when the device connection state gets
194 * @param[in] address LE address of the device to be notified to the upper layer.
195 * @param[in] isConnected whether connection state is connected or not.
197 static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
201 * Used to initialize all required mutex variable for LE Adapter
204 * @return 0 on success otherwise a positive error value.
205 * @retval ::CA_STATUS_OK Successful.
206 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
207 * @retval ::CA_STATUS_FAILED Operation failed.
210 static CAResult_t CAInitLEAdapterMutex();
213 * Terminate all required mutex variables for LE adapter
216 static void CATerminateLEAdapterMutex();
219 * Prepares and notify error through error callback.
221 static void CALEErrorHandler(const char *remoteAddress,
226 #ifndef SINGLE_THREAD
228 * Stop condition of recvhandler.
230 static bool g_dataBleReceiverHandlerState = false;
233 * Sender information.
235 static u_arraylist_t *g_bleServerSenderInfo = NULL;
237 static u_arraylist_t *g_bleClientSenderInfo = NULL;
240 * Queue to process the outgoing packets from GATTClient.
242 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
245 * Queue to process the incoming packets.
247 static CAQueueingThread_t *g_bleReceiverQueue = NULL;
250 * Queue to process the outgoing packets from GATTServer.
252 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
255 * This function will be associated with the sender queue for
258 * This function will fragment the data to the MTU of the transport
259 * and send the data in fragments to the adapters. The function will
260 * be blocked until all data is sent out from the adapter.
262 * @param[in] threadData Data pushed to the queue which contains the
263 * info about RemoteEndpoint and Data.
265 static void CALEServerSendDataThread(void *threadData);
268 * This function will be associated with the sender queue for
271 * This function will fragment the data to the MTU of the transport
272 * and send the data in fragments to the adapters. The function will
273 * be blocked until all data is sent out from the adapter.
275 * @param[in] threadData Data pushed to the queue which contains the
276 * info about RemoteEndpoint and Data.
278 static void CALEClientSendDataThread(void *threadData);
281 * This function will be associated with the receiver queue.
283 * This function will defragment the received data from each sender
284 * respectively and will send it up to CA layer. Respective sender's
285 * header will provide the length of the data sent.
287 * @param[in] threadData Data pushed to the queue which contains the
288 * info about RemoteEndpoint and Data.
290 static void CALEDataReceiverHandler(void *threadData);
293 * This function will stop all queues created for GattServer and
294 * GattClient. All four queues will be be stopped with this function
297 static void CAStopLEQueues();
300 * This function will terminate all queues created for GattServer and
301 * GattClient. All four queues will be be terminated with this
302 * function invocations.
304 static void CATerminateLEQueues();
307 * This function will initalize the Receiver and Sender queues for
308 * GattServer. This function will in turn call the functions
309 * CAInitBleServerReceiverQueue() and CAInitBleServerSenderQueue() to
310 * initialize the queues.
312 * @return ::CA_STATUS_OK or Appropriate error code.
313 * @retval ::CA_STATUS_OK Successful.
314 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
315 * @retval ::CA_STATUS_FAILED Operation failed.
317 static CAResult_t CAInitLEServerQueues();
320 * This function will initalize the Receiver and Sender queues for
321 * GattClient. This function will inturn call the functions
322 * CAInitBleClientReceiverQueue() and CAInitBleClientSenderQueue() to
323 * initialize the queues.
325 * @return ::CA_STATUS_OK or Appropriate error code.
326 * @retval ::CA_STATUS_OK Successful.
327 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
328 * @retval ::CA_STATUS_FAILED Operation failed.
331 static CAResult_t CAInitLEClientQueues();
334 * This function will initalize the Receiver queue for
335 * GattServer. This will initialize the queue to process the function
336 * CABLEServerSendDataThread() 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.
344 static CAResult_t CAInitLEServerSenderQueue();
347 * This function will initalize the Receiver queue for
348 * GattClient. This will initialize the queue to process the function
349 * CABLEClientSendDataThread() when ever the task is added to this
352 * @return ::CA_STATUS_OK or Appropriate error code.
353 * @retval ::CA_STATUS_OK Successful.
354 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
355 * @retval ::CA_STATUS_FAILED Operation failed.
357 static CAResult_t CAInitLEClientSenderQueue();
360 * This function will initialize the Receiver queue for
361 * LEAdapter. This will initialize the queue to process the function
362 * CABLEDataReceiverHandler() when ever the task is added to this
365 * @return ::CA_STATUS_OK or Appropriate error code
366 * @retval ::CA_STATUS_OK Successful
367 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
368 * @retval ::CA_STATUS_FAILED Operation failed
371 static CAResult_t CAInitLEReceiverQueue();
374 * This function will create the Data required to send it in the
377 * @param[in] remoteEndpoint Remote endpoint information of the
379 * @param[in] data Data to be transmitted from LE.
380 * @param[in] dataLength Length of the Data being transmitted.
382 * @return ::CA_STATUS_OK or Appropriate error code.
383 * @retval ::CA_STATUS_OK Successful.
384 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
385 * @retval ::CA_STATUS_FAILED Operation failed.
387 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
390 u_arraylist_t *senderInfo);
393 * Used to free the BLE information stored in the sender/receiver
396 * @param[in] bleData Information for a particular data segment.
398 static void CAFreeLEData(CALEData_t *bleData);
403 static void CALEDataDestroyer(void *data, uint32_t size);
405 #ifndef SINGLE_THREAD
407 * remove request or response data of send queue.
409 * @param[in] queueHandle queue to process the outgoing packets.
410 * @param[in] mutex mutex related to sender for client / server.
411 * @param[in] address target address to remove data in queue.
413 static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle,
415 const char* address);
418 * remove all received data of data list from receive queue.
420 * @param[in] dataInfoList received data list to remove for client / server.
421 * @param[in] address target address to remove data in queue.
423 static void CALERemoveReceiveQueueData(u_arraylist_t *dataInfoList,
424 const char* address);
427 static CAResult_t CAInitLEServerQueues()
429 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
431 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
433 CAResult_t result = CAInitLEServerSenderQueue();
434 if (CA_STATUS_OK != result)
436 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
437 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
438 return CA_STATUS_FAILED;
441 g_bleServerSenderInfo = u_arraylist_create();
442 if (!g_bleServerSenderInfo)
444 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
445 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
446 return CA_MEMORY_ALLOC_FAILED;
449 result = CAInitLEReceiverQueue();
450 if (CA_STATUS_OK != result)
452 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
453 u_arraylist_free(&g_bleServerSenderInfo);
454 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
455 return CA_STATUS_FAILED;
458 g_dataBleReceiverHandlerState = true;
460 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
462 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
466 static CAResult_t CAInitLEClientQueues()
468 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
470 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
472 CAResult_t result = CAInitLEClientSenderQueue();
473 if (CA_STATUS_OK != result)
475 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
476 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
477 return CA_STATUS_FAILED;
480 g_bleClientSenderInfo = u_arraylist_create();
481 if (!g_bleClientSenderInfo)
483 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
484 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
485 return CA_MEMORY_ALLOC_FAILED;
488 result = CAInitLEReceiverQueue();
489 if (CA_STATUS_OK != result)
491 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
492 u_arraylist_free(&g_bleClientSenderInfo);
493 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
494 return CA_STATUS_FAILED;
497 g_dataBleReceiverHandlerState = true;
499 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
501 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
505 static CAResult_t CAInitLEReceiverQueue()
507 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEReceiverQueue");
508 // Check if the message queue is already initialized
509 if (g_bleReceiverQueue)
511 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
515 // Create recv message queue
516 g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
517 if (!g_bleReceiverQueue)
519 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
520 return CA_MEMORY_ALLOC_FAILED;
523 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue,
524 g_bleAdapterThreadPool,
525 CALEDataReceiverHandler,
528 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
529 OICFree(g_bleReceiverQueue);
530 g_bleReceiverQueue = NULL;
531 return CA_STATUS_FAILED;
534 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue))
536 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
537 OICFree(g_bleReceiverQueue);
538 g_bleReceiverQueue = NULL;
539 return CA_STATUS_FAILED;
542 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
546 static CAResult_t CAInitLEServerSenderQueue()
548 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEServerSenderQueue");
549 // Check if the message queue is already initialized
550 if (g_bleServerSendQueueHandle)
552 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
556 // Create send message queue
557 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
558 if (!g_bleServerSendQueueHandle)
560 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
561 return CA_MEMORY_ALLOC_FAILED;
564 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
565 g_bleAdapterThreadPool,
566 CALEServerSendDataThread,
569 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
570 OICFree(g_bleServerSendQueueHandle);
571 g_bleServerSendQueueHandle = NULL;
572 return CA_STATUS_FAILED;
575 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
579 static void CALEClearSenderInfoImpl(u_arraylist_t ** list)
581 const size_t length = u_arraylist_length(*list);
582 for (size_t i = 0; i < length; ++i)
584 CABLESenderInfo_t * const info =
585 (CABLESenderInfo_t *) u_arraylist_get(*list, i);
588 OICFree(info->defragData);
589 CAFreeEndpoint(info->remoteEndpoint);
593 u_arraylist_free(list);
596 static void CALEClearSenderInfo()
598 CALEClearSenderInfoImpl(&g_bleServerSenderInfo);
599 CALEClearSenderInfoImpl(&g_bleClientSenderInfo);
602 static CAResult_t CAInitLEClientSenderQueue()
604 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEClientSenderQueue");
606 if (g_bleClientSendQueueHandle)
608 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
612 // Create send message queue
613 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
614 if (!g_bleClientSendQueueHandle)
616 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
617 return CA_MEMORY_ALLOC_FAILED;
620 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
621 g_bleAdapterThreadPool,
622 CALEClientSendDataThread, CALEDataDestroyer))
624 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
625 OICFree(g_bleClientSendQueueHandle);
626 g_bleClientSendQueueHandle = NULL;
627 return CA_STATUS_FAILED;
630 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAInitLEClientSenderQueue");
634 static void CAStopLEQueues()
636 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStopLEQueues");
638 ca_mutex_lock(g_bleReceiveDataMutex);
639 if (NULL != g_bleReceiverQueue)
641 CAQueueingThreadStop(g_bleReceiverQueue);
643 ca_mutex_unlock(g_bleReceiveDataMutex);
645 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAStopLEQueues");
648 static void CATerminateLEQueues()
650 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
652 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
653 OICFree(g_bleClientSendQueueHandle);
654 g_bleClientSendQueueHandle = NULL;
656 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
657 OICFree(g_bleServerSendQueueHandle);
658 g_bleServerSendQueueHandle = NULL;
660 CAQueueingThreadDestroy(g_bleReceiverQueue);
661 OICFree(g_bleReceiverQueue);
662 g_bleReceiverQueue = NULL;
664 CALEClearSenderInfo();
666 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
669 static CAResult_t CALEGetSenderInfo(const char *leAddress,
670 u_arraylist_t *senderInfoList,
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(senderInfoList);
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(senderInfoList, 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 - CALEDataReceiverHandler");
711 ca_mutex_lock(g_bleReceiveDataMutex);
713 if (g_dataBleReceiverHandlerState)
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->senderInfo))
727 OIC_LOG(ERROR, CALEADAPTER_TAG, "sender info is not available");
728 ca_mutex_unlock(g_bleReceiveDataMutex);
732 if (!(bleData->remoteEndpoint))
734 OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!");
735 ca_mutex_unlock(g_bleReceiveDataMutex);
739 CABLESenderInfo_t *senderInfo = NULL;
740 uint32_t senderIndex = 0;
742 if(CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
744 &senderInfo, &senderIndex))
746 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s]",
747 bleData->remoteEndpoint->addr);
752 CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
755 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
756 ca_mutex_unlock(g_bleReceiveDataMutex);
759 newSender->recvDataLen = 0;
760 newSender->totalDataLen = 0;
761 newSender->defragData = NULL;
762 newSender->remoteEndpoint = NULL;
764 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
766 newSender->totalDataLen = coap_get_total_message_length(bleData->data, bleData->dataLen);
768 if(!(newSender->totalDataLen))
770 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
772 ca_mutex_unlock(g_bleReceiveDataMutex);
776 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
777 newSender->totalDataLen);
778 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
781 newSender->defragData = OICCalloc(newSender->totalDataLen + 1,
782 sizeof(*newSender->defragData));
784 if (NULL == newSender->defragData)
786 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
788 ca_mutex_unlock(g_bleReceiveDataMutex);
792 const char *remoteAddress = bleData->remoteEndpoint->addr;
793 newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
794 CA_ADAPTER_GATT_BTLE,
797 if (NULL == newSender->remoteEndpoint)
799 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
800 OICFree(newSender->defragData);
802 ca_mutex_unlock(g_bleReceiveDataMutex);
806 if (newSender->recvDataLen + bleData->dataLen > newSender->totalDataLen)
808 OIC_LOG(ERROR, CALEADAPTER_TAG, "buffer is smaller than received data");
809 OICFree(newSender->defragData);
810 CAFreeEndpoint(newSender->remoteEndpoint);
812 ca_mutex_unlock(g_bleReceiveDataMutex);
815 memcpy(newSender->defragData, bleData->data, bleData->dataLen);
816 newSender->recvDataLen += bleData->dataLen;
818 u_arraylist_add(bleData->senderInfo,(void *)newSender);
820 //Getting newSender index position in bleSenderInfo array list
822 CALEGetSenderInfo(newSender->remoteEndpoint->addr, bleData->senderInfo,
825 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
826 OICFree(newSender->defragData);
827 CAFreeEndpoint(newSender->remoteEndpoint);
829 ca_mutex_unlock(g_bleReceiveDataMutex);
832 senderInfo = newSender;
836 if(senderInfo->recvDataLen + bleData->dataLen > senderInfo->totalDataLen)
838 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
839 "Data Length exceeding error!! Receiving [%d] total length [%d]",
840 senderInfo->recvDataLen + bleData->dataLen, senderInfo->totalDataLen);
841 u_arraylist_remove(bleData->senderInfo, senderIndex);
842 OICFree(senderInfo->defragData);
844 ca_mutex_unlock(g_bleReceiveDataMutex);
847 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]",
849 memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data,
851 senderInfo->recvDataLen += bleData->dataLen ;
852 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] received Datalen [%d]",
853 senderInfo->totalDataLen, senderInfo->recvDataLen);
856 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
858 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
859 if (NULL == g_networkPacketReceivedCallback)
861 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
863 u_arraylist_remove(bleData->senderInfo, senderIndex);
864 OICFree(senderInfo->defragData);
866 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
867 ca_mutex_unlock(g_bleReceiveDataMutex);
871 OIC_LOG(DEBUG, CALEADAPTER_TAG, "[CALEDataReceiverHandler] Sending data up !");
873 const CASecureEndpoint_t tmp =
875 .endpoint = *senderInfo->remoteEndpoint
878 g_networkPacketReceivedCallback(&tmp,
879 senderInfo->defragData,
880 senderInfo->recvDataLen);
881 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
882 u_arraylist_remove(bleData->senderInfo, senderIndex);
883 senderInfo->remoteEndpoint = NULL;
884 senderInfo->defragData = NULL;
888 ca_mutex_unlock(g_bleReceiveDataMutex);
889 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
892 static void CALEServerSendDataThread(void *threadData)
894 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEServerSendDataThread");
896 CALEData_t * const bleData = (CALEData_t *) threadData;
899 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
903 const uint32_t totalLength = bleData->dataLen;
907 "Server total Data length with header is [%u]",
910 uint8_t * const dataSegment = OICCalloc(totalLength, 1);
912 if (NULL == dataSegment)
914 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
919 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
921 length = totalLength;
922 memcpy(dataSegment, bleData->data, bleData->dataLen);
926 length = CA_SUPPORTED_BLE_MTU_SIZE;
927 memcpy(dataSegment, bleData->data, CA_SUPPORTED_BLE_MTU_SIZE);
930 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
932 CAResult_t result = CA_STATUS_FAILED;
934 // Send the first segment with the header.
935 if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
937 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
939 result = CAUpdateCharacteristicsToGattClient(
940 bleData->remoteEndpoint->addr, dataSegment, length);
942 if (CA_STATUS_OK != result)
946 "Update characteristics failed, result [%d]",
949 g_errorHandler(bleData->remoteEndpoint,
953 OICFree(dataSegment);
959 "Server Sent data length [%u]",
961 for (index = 1; index < iter; index++)
963 // Send the remaining header.
966 "Sending the chunk number [%u]",
970 CAUpdateCharacteristicsToGattClient(
971 bleData->remoteEndpoint->addr,
972 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE)),
973 CA_SUPPORTED_BLE_MTU_SIZE);
975 if (CA_STATUS_OK != result)
977 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
978 "Update characteristics failed, result [%d]", result);
979 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
980 OICFree(dataSegment);
983 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
984 CA_SUPPORTED_BLE_MTU_SIZE);
987 const uint32_t remainingLen =
988 totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
990 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
992 // send the last segment of the data (Ex: 22 bytes of 622
993 // bytes of data when MTU is 200)
994 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
996 result = CAUpdateCharacteristicsToGattClient(
997 bleData->remoteEndpoint->addr,
998 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1001 if (CA_STATUS_OK != result)
1005 "Update characteristics failed, result [%d]",
1007 g_errorHandler(bleData->remoteEndpoint,
1011 OICFree(dataSegment);
1014 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1019 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
1020 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
1021 if (CA_STATUS_OK != result)
1023 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1025 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1026 OICFree(dataSegment);
1029 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1030 for (index = 1; index < iter; index++)
1032 // Send the remaining header.
1033 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1035 result = CAUpdateCharacteristicsToAllGattClients(
1036 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE)),
1037 CA_SUPPORTED_BLE_MTU_SIZE);
1039 if (CA_STATUS_OK != result)
1041 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1043 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1044 OICFree(dataSegment);
1047 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%u]",
1048 CA_SUPPORTED_BLE_MTU_SIZE);
1051 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1052 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1054 // send the last segment of the data
1055 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1057 result = CAUpdateCharacteristicsToAllGattClients(
1058 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1061 if (CA_STATUS_OK != result)
1063 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1065 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1066 OICFree(dataSegment);
1069 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1072 OICFree(dataSegment);
1074 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CALEServerSendDataThread");
1077 static void CALEClientSendDataThread(void *threadData)
1079 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEClientSendDataThread");
1081 CALEData_t *bleData = (CALEData_t *) threadData;
1084 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1088 const uint32_t totalLength = bleData->dataLen;
1090 uint8_t *dataSegment = OICCalloc(totalLength, 1);
1091 if (NULL == dataSegment)
1093 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1097 uint32_t length = 0;
1098 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1100 length = totalLength;
1107 length = CA_SUPPORTED_BLE_MTU_SIZE;
1110 CA_SUPPORTED_BLE_MTU_SIZE);
1113 CAResult_t result = CA_STATUS_FAILED;
1114 const uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1116 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1118 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1119 // Send the first segment with the header.
1121 CAUpdateCharacteristicsToGattServer(
1122 bleData->remoteEndpoint->addr,
1128 if (CA_STATUS_OK != result)
1132 "Update characteristics failed, result [%d]",
1134 g_errorHandler(bleData->remoteEndpoint,
1138 OICFree(dataSegment);
1144 "Client Sent Data length is [%u]",
1147 for (index = 1; index < iter; index++)
1149 // Send the remaining header.
1150 result = CAUpdateCharacteristicsToGattServer(
1151 bleData->remoteEndpoint->addr,
1152 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1153 CA_SUPPORTED_BLE_MTU_SIZE,
1156 if (CA_STATUS_OK != result)
1160 "Update characteristics failed, result [%d]",
1162 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1163 OICFree(dataSegment);
1166 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1167 CA_SUPPORTED_BLE_MTU_SIZE);
1170 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1171 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1173 // send the last segment of the data (Ex: 22 bytes of 622
1174 // bytes of data when MTU is 200)
1175 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),
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),
1214 CA_SUPPORTED_BLE_MTU_SIZE);
1216 if (CA_STATUS_OK != result)
1218 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1220 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1221 OICFree(dataSegment);
1224 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1225 CA_SUPPORTED_BLE_MTU_SIZE);
1228 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1229 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1231 // send the last segment of the data (Ex: 22 bytes of 622
1232 // bytes of data when MTU is 200)
1233 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1235 CAUpdateCharacteristicsToAllGattServers(
1236 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1239 if (CA_STATUS_OK != result)
1241 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1242 "Update characteristics (all) failed, result [%d]", result);
1243 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1244 OICFree(dataSegment);
1247 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1252 OICFree(dataSegment);
1254 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1257 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
1258 const uint8_t *data,
1259 uint32_t dataLength,
1260 u_arraylist_t *senderInfo)
1262 CALEData_t * const bleData = OICMalloc(sizeof(CALEData_t));
1266 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1270 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1271 bleData->data = OICCalloc(dataLength + 1, 1);
1273 if (NULL == bleData->data)
1275 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1276 CAFreeLEData(bleData);
1280 memcpy(bleData->data, data, dataLength);
1281 bleData->dataLen = dataLength;
1284 bleData->senderInfo = senderInfo;
1290 static void CAFreeLEData(CALEData_t *bleData)
1292 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1294 CAFreeEndpoint(bleData->remoteEndpoint);
1295 OICFree(bleData->data);
1299 static void CALEDataDestroyer(void *data, uint32_t size)
1301 if ((size_t)size < sizeof(CALEData_t *))
1303 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1304 "Destroy data too small %p %d", data, size);
1306 CALEData_t *ledata = (CALEData_t *) data;
1308 CAFreeLEData(ledata);
1312 static CAResult_t CAInitLEAdapterMutex()
1314 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEAdapterMutex");
1316 if (NULL == g_bleIsServerMutex)
1318 g_bleIsServerMutex = ca_mutex_new();
1319 if (NULL == g_bleIsServerMutex)
1321 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1322 return CA_STATUS_FAILED;
1326 if (NULL == g_bleNetworkCbMutex)
1328 g_bleNetworkCbMutex = ca_mutex_new();
1329 if (NULL == g_bleNetworkCbMutex)
1331 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1332 CATerminateLEAdapterMutex();
1333 return CA_STATUS_FAILED;
1337 if (NULL == g_bleLocalAddressMutex)
1339 g_bleLocalAddressMutex = ca_mutex_new();
1340 if (NULL == g_bleLocalAddressMutex)
1342 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1343 CATerminateLEAdapterMutex();
1344 return CA_STATUS_FAILED;
1348 if (NULL == g_bleAdapterThreadPoolMutex)
1350 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1351 if (NULL == g_bleAdapterThreadPoolMutex)
1353 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1354 CATerminateLEAdapterMutex();
1355 return CA_STATUS_FAILED;
1359 if (NULL == g_bleClientSendDataMutex)
1361 g_bleClientSendDataMutex = ca_mutex_new();
1362 if (NULL == g_bleClientSendDataMutex)
1364 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1365 CATerminateLEAdapterMutex();
1366 return CA_STATUS_FAILED;
1370 if (NULL == g_bleServerSendDataMutex)
1372 g_bleServerSendDataMutex = ca_mutex_new();
1373 if (NULL == g_bleServerSendDataMutex)
1375 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1376 CATerminateLEAdapterMutex();
1377 return CA_STATUS_FAILED;
1381 if (NULL == g_bleAdapterReqRespCbMutex)
1383 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1384 if (NULL == g_bleAdapterReqRespCbMutex)
1386 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1387 CATerminateLEAdapterMutex();
1388 return CA_STATUS_FAILED;
1392 if (NULL == g_bleReceiveDataMutex)
1394 g_bleReceiveDataMutex = ca_mutex_new();
1395 if (NULL == g_bleReceiveDataMutex)
1397 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1398 return CA_STATUS_FAILED;
1402 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1403 return CA_STATUS_OK;
1406 static void CATerminateLEAdapterMutex()
1408 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CATerminateLEAdapterMutex");
1410 ca_mutex_free(g_bleIsServerMutex);
1411 g_bleIsServerMutex = NULL;
1413 ca_mutex_free(g_bleNetworkCbMutex);
1414 g_bleNetworkCbMutex = NULL;
1416 ca_mutex_free(g_bleLocalAddressMutex);
1417 g_bleLocalAddressMutex = NULL;
1419 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1420 g_bleAdapterThreadPoolMutex = NULL;
1422 ca_mutex_free(g_bleClientSendDataMutex);
1423 g_bleClientSendDataMutex = NULL;
1425 ca_mutex_free(g_bleServerSendDataMutex);
1426 g_bleServerSendDataMutex = NULL;
1428 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1429 g_bleAdapterReqRespCbMutex = NULL;
1431 ca_mutex_free(g_bleReceiveDataMutex);
1432 g_bleReceiveDataMutex = NULL;
1434 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1438 * Starting LE connectivity adapters.
1440 * As its peer to peer it does not require to start any servers.
1442 * @return ::CA_STATUS_OK or Appropriate error code.
1444 static CAResult_t CAStartLE();
1447 * Start listening server for receiving multicast search requests.
1449 * Transport Specific Behavior:
1450 * LE Starts GATT Server with prefixed UUID and Characteristics
1451 * per OIC Specification.
1452 * @return ::CA_STATUS_OK or Appropriate error code.
1454 static CAResult_t CAStartLEListeningServer();
1457 * Stops listening server from receiving multicast search requests.
1459 * Transport Specific Behavior:
1460 * LE Starts GATT Server with prefixed UUID and Characteristics
1461 * per OIC Specification.
1462 * @return ::CA_STATUS_OK or Appropriate error code.
1464 static CAResult_t CAStopLEListeningServer();
1467 * Sarting discovery of servers for receiving multicast
1470 * Transport Specific Behavior:
1471 * LE Starts GATT Server with prefixed UUID and Characteristics
1472 * per OIC Specification.
1474 * @return ::CA_STATUS_OK or Appropriate error code
1476 static CAResult_t CAStartLEDiscoveryServer();
1479 * Send data to the endpoint using the adapter connectivity.
1481 * @param[in] endpoint Remote Endpoint information (like MAC address,
1482 * reference URI and connectivity type) to which
1483 * the unicast data has to be sent.
1484 * @param[in] data Data which required to be sent.
1485 * @param[in] dataLen Size of data to be sent.
1487 * @note dataLen must be > 0.
1489 * @return The number of bytes sent on the network, or -1 on error.
1491 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1496 * Send multicast data to the endpoint using the LE connectivity.
1498 * @param[in] endpoint Remote Endpoint information to which the
1499 * multicast data has to be sent.
1500 * @param[in] data Data which required to be sent.
1501 * @param[in] dataLen Size of data to be sent.
1503 * @note dataLen must be > 0.
1505 * @return The number of bytes sent on the network, or -1 on error.
1507 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1512 * Get LE Connectivity network information.
1514 * @param[out] info Local connectivity information structures.
1515 * @param[out] size Number of local connectivity structures.
1517 * @return ::CA_STATUS_OK or Appropriate error code.
1519 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info,
1523 * Read Synchronous API callback.
1525 * @return ::CA_STATUS_OK or Appropriate error code.
1527 static CAResult_t CAReadLEData();
1530 * Stopping the adapters and close socket connections.
1532 * LE Stops all GATT servers and GATT Clients.
1534 * @return ::CA_STATUS_OK or Appropriate error code.
1536 static CAResult_t CAStopLE();
1539 * Terminate the LE connectivity adapter.
1541 * Configuration information will be deleted from further use.
1543 static void CATerminateLE();
1546 * This function will receive the data from the GattServer and add the
1547 * data to the Server receiver queue.
1549 * @param[in] remoteAddress Remote address of the device from where
1551 * @param[in] data Actual data received from the remote
1553 * @param[in] dataLength Length of the data received from the
1555 * @param[in] sentLength Length of the data sent from the remote
1558 * @return ::CA_STATUS_OK or Appropriate error code.
1559 * @retval ::CA_STATUS_OK Successful.
1560 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1561 * @retval ::CA_STATUS_FAILED Operation failed.
1564 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
1565 const uint8_t *data,
1566 uint32_t dataLength,
1567 uint32_t *sentLength);
1570 * This function will receive the data from the GattClient and add the
1571 * data into the Client receiver queue.
1573 * @param[in] remoteAddress Remote address of the device from where
1575 * @param[in] data Actual data recevied from the remote
1577 * @param[in] dataLength Length of the data received from the
1579 * @param[in] sentLength Length of the data sent from the remote
1582 * @return ::CA_STATUS_OK or Appropriate error code.
1583 * @retval ::CA_STATUS_OK Successful.
1584 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1585 * @retval ::CA_STATUS_FAILED Operation failed.
1587 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
1588 const uint8_t *data,
1589 uint32_t dataLength,
1590 uint32_t *sentLength);
1593 * Set the NetworkPacket received callback to CA layer from adapter
1596 * @param[in] callback Callback handle sent from the upper layer.
1598 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
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 CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1614 const uint8_t *data,
1618 * Push the data from CA layer to the Sender processor queue.
1620 * @param[in] remoteEndpoint Remote endpoint information of the
1622 * @param[in] data Data to be transmitted from LE.
1623 * @param[in] dataLen Length of the Data being transmitted.
1625 * @return ::CA_STATUS_OK or Appropriate error code.
1626 * @retval ::CA_STATUS_OK Successful.
1627 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1628 * @retval ::CA_STATUS_FAILED Operation failed.
1630 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1631 const uint8_t *data,
1634 static CAResult_t CALEAdapterGattServerStart()
1636 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1638 CAResult_t result = CAStartLEGattServer();
1640 #ifndef SINGLE_THREAD
1642 Don't start the server side sending queue thread until the
1643 server itself has actually started.
1645 if (CA_STATUS_OK == result)
1647 ca_mutex_lock(g_bleServerSendDataMutex);
1648 result = CAQueueingThreadStart(g_bleServerSendQueueHandle);
1649 ca_mutex_unlock(g_bleServerSendDataMutex);
1651 if (CA_STATUS_OK != result)
1655 "Unable to start server queuing thread (%d)",
1664 static CAResult_t CALEAdapterGattServerStop()
1666 #ifndef SINGLE_THREAD
1667 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEAdapterGattServerStop");
1669 CAResult_t result = CAStopLEGattServer();
1670 ca_mutex_lock(g_bleServerSendDataMutex);
1671 if (CA_STATUS_OK == result)
1673 result = CAQueueingThreadStop(g_bleServerSendQueueHandle);
1675 ca_mutex_unlock(g_bleServerSendDataMutex);
1679 return CAStopLEGattServer();
1683 static CAResult_t CALEAdapterGattClientStart()
1685 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattClient");
1687 CAResult_t result = CAStartLEGattClient();
1689 #ifndef SINGLE_THREAD
1691 Don't start the client side sending queue thread until the
1692 client itself has actually started.
1694 if (CA_STATUS_OK == result)
1696 ca_mutex_lock(g_bleClientSendDataMutex);
1697 result = CAQueueingThreadStart(g_bleClientSendQueueHandle);
1698 ca_mutex_unlock(g_bleClientSendDataMutex);
1700 if (CA_STATUS_OK != result)
1704 "Unable to start client queuing thread");
1712 static CAResult_t CALEAdapterGattClientStop()
1714 #ifndef SINGLE_THREAD
1715 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEAdapterGattClientStop");
1716 CAStopLEGattClient();
1718 ca_mutex_lock(g_bleClientSendDataMutex);
1719 CAResult_t result = CAQueueingThreadStop(g_bleClientSendQueueHandle);
1720 ca_mutex_unlock(g_bleClientSendDataMutex);
1724 CAStopLEGattClient();
1726 return CA_STATUS_OK;
1730 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1731 CANetworkPacketReceivedCallback reqRespCallback,
1732 CAAdapterChangeCallback netCallback,
1733 CAConnectionChangeCallback connCallback,
1734 CAErrorHandleCallback errorCallback,
1735 ca_thread_pool_t handle)
1737 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1740 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1741 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1742 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1743 VERIFY_NON_NULL(connCallback, CALEADAPTER_TAG, "ConnectionChange Callback is null");
1745 CAResult_t result = CA_STATUS_OK;
1746 result = CAInitLEAdapterMutex();
1747 if (CA_STATUS_OK != result)
1749 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1750 return CA_STATUS_FAILED;
1753 result = CAInitializeLENetworkMonitor();
1754 if (CA_STATUS_OK != result)
1756 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1757 return CA_STATUS_FAILED;
1759 CAInitializeLEAdapter(handle);
1761 CASetLEClientThreadPoolHandle(handle);
1763 result = CAInitializeLEGattClient();
1764 if (CA_STATUS_OK != result)
1766 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattClient() failed");
1767 return CA_STATUS_FAILED;
1770 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1771 CASetLEServerThreadPoolHandle(handle);
1772 result = CAInitializeLEGattServer();
1773 if (CA_STATUS_OK != result)
1775 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattServer() failed");
1776 return CA_STATUS_FAILED;
1779 CASetLEAdapterThreadPoolHandle(handle);
1780 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1781 CASetLEReqRespAdapterCallback(reqRespCallback);
1783 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1784 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1785 CALERegisterNetworkNotifications(netCallback, connCallback);
1787 g_errorHandler = errorCallback;
1789 static const CAConnectivityHandler_t connHandler =
1791 .startAdapter = CAStartLE,
1792 .stopAdapter = CAStopLE,
1793 .startListenServer = CAStartLEListeningServer,
1794 .stopListenServer = CAStopLEListeningServer,
1795 .startDiscoveryServer = CAStartLEDiscoveryServer,
1796 .sendData = CASendLEUnicastData,
1797 .sendDataToAll = CASendLEMulticastData,
1798 .GetnetInfo = CAGetLEInterfaceInformation,
1799 .readData = CAReadLEData,
1800 .terminate = CATerminateLE,
1801 .cType = CA_ADAPTER_GATT_BTLE
1804 registerCallback(connHandler);
1806 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1808 return CA_STATUS_OK;
1811 static CAResult_t CAStartLE()
1813 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE");
1815 return CAStartLEAdapter();
1818 static CAResult_t CAStopLE()
1820 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1821 #ifndef SINGLE_THREAD
1825 ca_mutex_lock(g_bleIsServerMutex);
1826 switch (g_adapterType)
1828 case ADAPTER_SERVER:
1829 CALEAdapterGattServerStop();
1831 case ADAPTER_CLIENT:
1832 CALEAdapterGattClientStop();
1834 case ADAPTER_BOTH_CLIENT_SERVER:
1835 CALEAdapterGattServerStop();
1836 CALEAdapterGattClientStop();
1841 ca_mutex_unlock(g_bleIsServerMutex);
1843 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1845 return CAStopLEAdapter();
1848 static void CATerminateLE()
1850 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1852 CASetLEReqRespServerCallback(NULL);
1853 CASetLEReqRespClientCallback(NULL);
1854 CALERegisterNetworkNotifications(NULL, NULL);
1855 CASetLEReqRespAdapterCallback(NULL);
1856 CATerminateLENetworkMonitor();
1858 ca_mutex_lock(g_bleIsServerMutex);
1859 switch (g_adapterType)
1861 case ADAPTER_SERVER:
1862 CATerminateLEGattServer();
1864 case ADAPTER_CLIENT:
1865 CATerminateLEGattClient();
1867 case ADAPTER_BOTH_CLIENT_SERVER:
1868 CATerminateLEGattServer();
1869 CATerminateLEGattClient();
1874 g_adapterType = ADAPTER_EMPTY;
1875 ca_mutex_unlock(g_bleIsServerMutex);
1877 #ifndef SINGLE_THREAD
1878 CATerminateLEQueues();
1880 CATerminateLEAdapterMutex();
1882 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1885 static CAResult_t CAStartLEListeningServer()
1887 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEListeningServer");
1888 #ifndef ROUTING_GATEWAY
1889 CAResult_t result = CA_STATUS_OK;
1890 #ifndef SINGLE_THREAD
1891 result = CAInitLEServerQueues();
1892 if (CA_STATUS_OK != result)
1894 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1899 ca_mutex_lock(g_bleIsServerMutex);
1900 switch (g_adapterType)
1902 case ADAPTER_CLIENT:
1903 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1905 case ADAPTER_BOTH_CLIENT_SERVER:
1908 g_adapterType = ADAPTER_SERVER;
1910 ca_mutex_unlock(g_bleIsServerMutex);
1912 result = CAGetLEAdapterState();
1913 if (CA_STATUS_OK != result)
1915 if (CA_ADAPTER_NOT_ENABLED == result)
1919 "Listen Server will be started once BT Adapter is enabled");
1920 result = CA_STATUS_OK;
1925 result = CALEAdapterGattServerStart();
1928 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1931 // Routing Gateway only supports BLE client mode.
1932 OIC_LOG(ERROR, CALEADAPTER_TAG, "LE server not supported in Routing Gateway");
1933 return CA_NOT_SUPPORTED;
1937 static CAResult_t CAStopLEListeningServer()
1939 OIC_LOG(ERROR, CALEADAPTER_TAG, "Listen server stop not supported.");
1940 return CA_NOT_SUPPORTED;
1943 static CAResult_t CAStartLEDiscoveryServer()
1945 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEDiscoveryServer");
1946 CAResult_t result = CA_STATUS_OK;
1947 #ifndef SINGLE_THREAD
1948 result = CAInitLEClientQueues();
1949 if (CA_STATUS_OK != result)
1951 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1956 ca_mutex_lock(g_bleIsServerMutex);
1957 switch (g_adapterType)
1959 case ADAPTER_SERVER:
1960 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1962 case ADAPTER_BOTH_CLIENT_SERVER:
1965 g_adapterType = ADAPTER_CLIENT;
1967 ca_mutex_unlock(g_bleIsServerMutex);
1969 result = CAGetLEAdapterState();
1970 if (CA_STATUS_OK != result)
1972 if (CA_ADAPTER_NOT_ENABLED == result)
1976 "Discovery Server will be started once BT Adapter is enabled");
1977 result = CA_STATUS_OK;
1982 result = CALEAdapterGattClientStart();
1985 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1989 static CAResult_t CAReadLEData()
1991 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1992 #ifdef SINGLE_THREAD
1995 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1996 return CA_STATUS_OK;
1999 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
2003 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEUnicastData");
2006 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
2007 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
2009 CAResult_t result = CA_STATUS_FAILED;
2011 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
2012 if (ADAPTER_EMPTY == g_adapterType)
2014 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2017 ca_mutex_lock(g_bleIsServerMutex);
2018 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2020 result = CALEAdapterServerSendData(endpoint, data, dataLen);
2021 if (CA_STATUS_OK != result)
2023 ca_mutex_unlock(g_bleIsServerMutex);
2024 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed");
2027 g_errorHandler(endpoint, data, dataLen, result);
2034 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2036 result = CALEAdapterClientSendData(endpoint, data, dataLen);
2037 if (CA_STATUS_OK != result)
2039 ca_mutex_unlock(g_bleIsServerMutex);
2040 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for client failed" );
2044 g_errorHandler(endpoint, data, dataLen, result);
2049 ca_mutex_unlock(g_bleIsServerMutex);
2051 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2055 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
2059 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEMulticastData");
2062 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
2066 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
2070 CAResult_t result = CA_STATUS_FAILED;
2072 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
2073 if (ADAPTER_EMPTY == g_adapterType)
2075 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2078 ca_mutex_lock(g_bleIsServerMutex);
2079 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2081 result = CALEAdapterServerSendData(NULL, data, dataLen);
2082 if (CA_STATUS_OK != result)
2084 ca_mutex_unlock(g_bleIsServerMutex);
2086 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data for server failed" );
2090 g_errorHandler(endpoint, data, dataLen, result);
2096 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2098 result = CALEAdapterClientSendData(NULL, data, dataLen);
2099 if (CA_STATUS_OK != result)
2101 ca_mutex_unlock(g_bleIsServerMutex);
2103 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data for client failed" );
2107 g_errorHandler(endpoint, data, dataLen, result);
2112 ca_mutex_unlock(g_bleIsServerMutex);
2114 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CASendLEMulticastData");
2118 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
2120 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2122 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
2124 char *local_address = NULL;
2126 CAResult_t res = CAGetLEAddress(&local_address);
2127 if (CA_STATUS_OK != res)
2129 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
2133 if (NULL == local_address)
2135 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
2136 return CA_STATUS_FAILED;
2140 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
2141 if (NULL == (*info))
2143 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
2144 OICFree(local_address);
2145 return CA_STATUS_FAILED;
2148 size_t local_address_len = strlen(local_address);
2150 if(local_address_len >= sizeof(g_localBLEAddress) ||
2151 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
2153 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
2155 OICFree(local_address);
2156 return CA_STATUS_FAILED;
2159 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
2160 ca_mutex_lock(g_bleLocalAddressMutex);
2161 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
2162 ca_mutex_unlock(g_bleLocalAddressMutex);
2164 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
2166 OICFree(local_address);
2168 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2169 return CA_STATUS_OK;
2172 static CAResult_t CALERegisterNetworkNotifications(CAAdapterChangeCallback netCallback,
2173 CAConnectionChangeCallback connCallback)
2175 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2177 ca_mutex_lock(g_bleNetworkCbMutex);
2178 g_networkCallback = netCallback;
2179 g_connectionCallback = connCallback;
2180 ca_mutex_unlock(g_bleNetworkCbMutex);
2181 CAResult_t res = CA_STATUS_OK;
2184 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
2185 if (CA_STATUS_OK != res)
2187 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2192 res = CAUnSetLEAdapterStateChangedCb();
2193 if (CA_STATUS_OK != res)
2195 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2199 if (g_connectionCallback)
2201 res = CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCb);
2202 if (CA_STATUS_OK != res)
2204 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLENWConnectionStateChangedCb failed!");
2208 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2212 static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
2215 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEConnectionStateChangedCb");
2217 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2221 ca_mutex_lock(g_bleIsServerMutex);
2222 switch (g_adapterType)
2224 case ADAPTER_SERVER:
2225 CALEGattServerConnectionStateChanged(isConnected, address);
2227 case ADAPTER_CLIENT:
2228 CALEGattConnectionStateChanged(isConnected, address);
2230 case ADAPTER_BOTH_CLIENT_SERVER:
2231 CALEGattConnectionStateChanged(isConnected, address);
2232 CALEGattServerConnectionStateChanged(isConnected, address);
2237 ca_mutex_unlock(g_bleIsServerMutex);
2242 #ifndef SINGLE_THREAD
2243 if(g_bleClientSenderInfo)
2245 CALERemoveReceiveQueueData(g_bleClientSenderInfo, address);
2248 if(g_bleServerSenderInfo)
2250 CALERemoveReceiveQueueData(g_bleServerSenderInfo, address);
2253 // remove data of send queue.
2254 if (g_bleClientSendQueueHandle)
2256 CALERemoveSendQueueData(g_bleClientSendQueueHandle,
2257 g_bleClientSendDataMutex,
2261 if (g_bleServerSendQueueHandle)
2263 CALERemoveSendQueueData(g_bleServerSendQueueHandle,
2264 g_bleServerSendDataMutex,
2270 CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE };
2271 OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), address);
2273 ca_mutex_lock(g_bleNetworkCbMutex);
2274 if (g_connectionCallback)
2276 g_connectionCallback(&localEndpoint, isConnected);
2278 ca_mutex_unlock(g_bleNetworkCbMutex);
2280 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2283 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
2285 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDeviceStateChangedCb");
2287 if (CA_ADAPTER_ENABLED == adapter_state)
2289 ca_mutex_lock(g_bleIsServerMutex);
2290 switch (g_adapterType)
2292 case ADAPTER_SERVER:
2293 CALEAdapterGattServerStart();
2295 case ADAPTER_CLIENT:
2296 CALEAdapterGattClientStart();
2298 case ADAPTER_BOTH_CLIENT_SERVER:
2299 CALEAdapterGattServerStart();
2300 CALEAdapterGattClientStart();
2305 ca_mutex_unlock(g_bleIsServerMutex);
2309 ca_mutex_lock(g_bleIsServerMutex);
2310 switch (g_adapterType)
2312 case ADAPTER_SERVER:
2313 CALEAdapterGattServerStop();
2315 case ADAPTER_CLIENT:
2316 CALEAdapterGattClientStop();
2318 case ADAPTER_BOTH_CLIENT_SERVER:
2319 CALEAdapterGattServerStop();
2320 CALEAdapterGattClientStop();
2325 ca_mutex_unlock(g_bleIsServerMutex);
2328 ca_mutex_lock(g_bleNetworkCbMutex);
2329 if (NULL != g_networkCallback)
2331 g_networkCallback(CA_ADAPTER_GATT_BTLE, adapter_state);
2335 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
2337 ca_mutex_unlock(g_bleNetworkCbMutex);
2339 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2342 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
2343 const uint8_t *data,
2346 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2348 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2349 #ifndef SINGLE_THREAD
2350 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2351 "g_bleClientSendQueueHandle is NULL",
2353 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
2354 "g_bleClientSendDataMutex is NULL",
2357 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2358 "g_bleClientSendQueueHandle",
2361 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
2363 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2366 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2367 return CA_MEMORY_ALLOC_FAILED;
2369 // Add message to send queue
2370 ca_mutex_lock(g_bleClientSendDataMutex);
2371 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
2372 ca_mutex_unlock(g_bleClientSendDataMutex);
2374 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2375 return CA_STATUS_OK;
2378 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2379 const uint8_t *data,
2382 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2384 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2386 #ifdef SINGLE_THREAD
2387 if (!CAIsLEConnected())
2389 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2390 return CA_STATUS_FAILED;
2393 CAResult_t result = CA_STATUS_OK;
2394 const uint32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2395 for (uint32_t iter = 0; iter < dataLimit; iter++)
2398 CAUpdateCharacteristicsToAllGattClients(
2399 data + (iter * CA_SUPPORTED_BLE_MTU_SIZE),
2400 CA_SUPPORTED_BLE_MTU_SIZE);
2402 if (CA_STATUS_OK != result)
2404 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2405 return CA_STATUS_FAILED;
2411 const uint32_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2415 CAUpdateCharacteristicsToAllGattClients(
2416 data + (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE),
2418 if (CA_STATUS_OK != result)
2420 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2421 return CA_STATUS_FAILED;
2426 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2427 "BleClientReceiverQueue is NULL",
2429 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2430 "BleClientSendDataMutex is NULL",
2433 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2436 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2438 CALEData_t * const bleData =
2439 CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2443 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2444 return CA_MEMORY_ALLOC_FAILED;
2447 // Add message to send queue
2448 ca_mutex_lock(g_bleServerSendDataMutex);
2449 CAQueueingThreadAddData(g_bleServerSendQueueHandle,
2451 sizeof(CALEData_t));
2452 ca_mutex_unlock(g_bleServerSendDataMutex);
2454 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2455 return CA_STATUS_OK;
2458 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2459 const uint8_t *data,
2460 uint32_t dataLength,
2461 uint32_t *sentLength)
2463 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2466 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2467 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2469 #ifdef SINGLE_THREAD
2470 if(g_networkPacketReceivedCallback)
2472 // will be filled by upper layer
2473 const CASecureEndpoint_t endpoint =
2474 { .endpoint = { .adapter = CA_ADAPTER_GATT_BTLE } };
2477 g_networkPacketReceivedCallback(&endpoint, data, dataLength);
2480 VERIFY_NON_NULL_RET(g_bleReceiverQueue,
2482 "g_bleReceiverQueue",
2485 //Add message to data queue
2486 CAEndpoint_t * const remoteEndpoint =
2487 CACreateEndpointObject(CA_DEFAULT_FLAGS,
2488 CA_ADAPTER_GATT_BTLE,
2492 if (NULL == remoteEndpoint)
2494 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2495 return CA_STATUS_FAILED;
2498 // Create bleData to add to queue
2501 "Data received from LE layer [%d]",
2504 CALEData_t * const bleData =
2505 CACreateLEData(remoteEndpoint, data, dataLength, g_bleServerSenderInfo);
2509 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2510 CAFreeEndpoint(remoteEndpoint);
2511 return CA_MEMORY_ALLOC_FAILED;
2514 CAFreeEndpoint(remoteEndpoint);
2515 // Add message to receiver queue
2516 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2518 *sentLength = dataLength;
2520 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2521 return CA_STATUS_OK;
2524 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2525 const uint8_t *data,
2526 uint32_t dataLength,
2527 uint32_t *sentLength)
2529 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2532 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2533 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2534 #ifndef SINGLE_THREAD
2535 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG,
2536 "g_bleReceiverQueue",
2539 //Add message to data queue
2540 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2541 CA_ADAPTER_GATT_BTLE,
2543 if (NULL == remoteEndpoint)
2545 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2546 return CA_STATUS_FAILED;
2549 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%u]", dataLength);
2551 // Create bleData to add to queue
2552 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data,
2553 dataLength, g_bleClientSenderInfo);
2556 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2557 CAFreeEndpoint(remoteEndpoint);
2558 return CA_MEMORY_ALLOC_FAILED;
2561 CAFreeEndpoint(remoteEndpoint);
2562 // Add message to receiver queue
2563 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2565 *sentLength = dataLength;
2567 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2568 return CA_STATUS_OK;
2571 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2573 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2575 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2576 g_bleAdapterThreadPool = handle;
2577 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2579 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2582 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2584 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2586 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2588 g_networkPacketReceivedCallback = callback;
2590 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2592 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2595 static void CALEErrorHandler(const char *remoteAddress,
2596 const uint8_t *data,
2600 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2602 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2604 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2605 CA_ADAPTER_GATT_BTLE,
2609 // if required, will be used to build remote endpoint
2610 g_errorHandler(rep, data, dataLen, result);
2612 CAFreeEndpoint(rep);
2614 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");
2617 #ifndef SINGLE_THREAD
2618 static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle, ca_mutex mutex,
2619 const char* address)
2621 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALERemoveSendQueueData");
2623 VERIFY_NON_NULL_VOID(queueHandle, CALEADAPTER_TAG, "queueHandle");
2624 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2626 ca_mutex_lock(mutex);
2627 while (u_queue_get_size(queueHandle->dataQueue) > 0)
2629 OIC_LOG(DEBUG, CALEADAPTER_TAG, "get data from queue");
2630 u_queue_message_t *message = u_queue_get_element(queueHandle->dataQueue);
2631 if (NULL != message)
2633 CALEData_t *bleData = (CALEData_t *) message->msg;
2634 if (bleData && bleData->remoteEndpoint)
2636 if (!strcmp(bleData->remoteEndpoint->addr, address))
2638 OIC_LOG(DEBUG, CALEADAPTER_TAG, "found the message of disconnected device");
2639 if (NULL != queueHandle->destroy)
2641 queueHandle->destroy(message->msg, message->size);
2645 OICFree(message->msg);
2653 ca_mutex_unlock(mutex);
2656 static void CALERemoveReceiveQueueData(u_arraylist_t *dataInfoList, const char* address)
2658 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALERemoveReceiveQueueData");
2660 VERIFY_NON_NULL_VOID(dataInfoList, CALEADAPTER_TAG, "dataInfoList");
2661 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2663 CABLESenderInfo_t *senderInfo = NULL;
2664 uint32_t senderIndex = 0;
2666 if(CA_STATUS_OK == CALEGetSenderInfo(address, dataInfoList, &senderInfo,
2669 u_arraylist_remove(dataInfoList, senderIndex);
2670 OICFree(senderInfo->defragData);
2671 OICFree(senderInfo->remoteEndpoint);
2672 OICFree(senderInfo);
2674 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo is removed for disconnection");
2678 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo doesn't exist");