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,
1494 CADataType_t dataType);
1497 * Send multicast data to the endpoint using the LE connectivity.
1499 * @param[in] endpoint Remote Endpoint information to which the
1500 * multicast data has to be sent.
1501 * @param[in] data Data which required to be sent.
1502 * @param[in] dataLen Size of data to be sent.
1504 * @note dataLen must be > 0.
1506 * @return The number of bytes sent on the network, or -1 on error.
1508 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1511 CADataType_t dataType);
1514 * Get LE Connectivity network information.
1516 * @param[out] info Local connectivity information structures.
1517 * @param[out] size Number of local connectivity structures.
1519 * @return ::CA_STATUS_OK or Appropriate error code.
1521 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info,
1525 * Read Synchronous API callback.
1527 * @return ::CA_STATUS_OK or Appropriate error code.
1529 static CAResult_t CAReadLEData();
1532 * Stopping the adapters and close socket connections.
1534 * LE Stops all GATT servers and GATT Clients.
1536 * @return ::CA_STATUS_OK or Appropriate error code.
1538 static CAResult_t CAStopLE();
1541 * Terminate the LE connectivity adapter.
1543 * Configuration information will be deleted from further use.
1545 static void CATerminateLE();
1548 * This function will receive the data from the GattServer and add the
1549 * data to the Server receiver queue.
1551 * @param[in] remoteAddress Remote address of the device from where
1553 * @param[in] data Actual data received from the remote
1555 * @param[in] dataLength Length of the data received from the
1557 * @param[in] sentLength Length of the data sent from the remote
1560 * @return ::CA_STATUS_OK or Appropriate error code.
1561 * @retval ::CA_STATUS_OK Successful.
1562 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1563 * @retval ::CA_STATUS_FAILED Operation failed.
1566 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
1567 const uint8_t *data,
1568 uint32_t dataLength,
1569 uint32_t *sentLength);
1572 * This function will receive the data from the GattClient and add the
1573 * data into the Client receiver queue.
1575 * @param[in] remoteAddress Remote address of the device from where
1577 * @param[in] data Actual data recevied from the remote
1579 * @param[in] dataLength Length of the data received from the
1581 * @param[in] sentLength Length of the data sent from the remote
1584 * @return ::CA_STATUS_OK or Appropriate error code.
1585 * @retval ::CA_STATUS_OK Successful.
1586 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1587 * @retval ::CA_STATUS_FAILED Operation failed.
1589 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
1590 const uint8_t *data,
1591 uint32_t dataLength,
1592 uint32_t *sentLength);
1595 * Set the NetworkPacket received callback to CA layer from adapter
1598 * @param[in] callback Callback handle sent from the upper layer.
1600 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
1603 * Push the data from CA layer to the Sender processor queue.
1605 * @param[in] remoteEndpoint Remote endpoint information of the
1607 * @param[in] data Data to be transmitted from LE.
1608 * @param[in] dataLen Length of the Data being transmitted.
1610 * @return ::CA_STATUS_OK or Appropriate error code.
1611 * @retval ::CA_STATUS_OK Successful.
1612 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1613 * @retval ::CA_STATUS_FAILED Operation failed.
1615 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1616 const uint8_t *data,
1620 * Push the data from CA layer to the Sender processor queue.
1622 * @param[in] remoteEndpoint Remote endpoint information of the
1624 * @param[in] data Data to be transmitted from LE.
1625 * @param[in] dataLen Length of the Data being transmitted.
1627 * @return ::CA_STATUS_OK or Appropriate error code.
1628 * @retval ::CA_STATUS_OK Successful.
1629 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1630 * @retval ::CA_STATUS_FAILED Operation failed.
1632 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1633 const uint8_t *data,
1636 static CAResult_t CALEAdapterGattServerStart()
1638 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1640 CAResult_t result = CAStartLEGattServer();
1642 #ifndef SINGLE_THREAD
1644 Don't start the server side sending queue thread until the
1645 server itself has actually started.
1647 if (CA_STATUS_OK == result)
1649 ca_mutex_lock(g_bleServerSendDataMutex);
1650 result = CAQueueingThreadStart(g_bleServerSendQueueHandle);
1651 ca_mutex_unlock(g_bleServerSendDataMutex);
1653 if (CA_STATUS_OK != result)
1657 "Unable to start server queuing thread (%d)",
1666 static CAResult_t CALEAdapterGattServerStop()
1668 #ifndef SINGLE_THREAD
1669 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEAdapterGattServerStop");
1671 CAResult_t result = CAStopLEGattServer();
1672 ca_mutex_lock(g_bleServerSendDataMutex);
1673 if (CA_STATUS_OK == result)
1675 result = CAQueueingThreadStop(g_bleServerSendQueueHandle);
1677 ca_mutex_unlock(g_bleServerSendDataMutex);
1681 return CAStopLEGattServer();
1685 static CAResult_t CALEAdapterGattClientStart()
1687 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattClient");
1689 CAResult_t result = CAStartLEGattClient();
1691 #ifndef SINGLE_THREAD
1693 Don't start the client side sending queue thread until the
1694 client itself has actually started.
1696 if (CA_STATUS_OK == result)
1698 ca_mutex_lock(g_bleClientSendDataMutex);
1699 result = CAQueueingThreadStart(g_bleClientSendQueueHandle);
1700 ca_mutex_unlock(g_bleClientSendDataMutex);
1702 if (CA_STATUS_OK != result)
1706 "Unable to start client queuing thread");
1714 static CAResult_t CALEAdapterGattClientStop()
1716 #ifndef SINGLE_THREAD
1717 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEAdapterGattClientStop");
1718 CAStopLEGattClient();
1720 ca_mutex_lock(g_bleClientSendDataMutex);
1721 CAResult_t result = CAQueueingThreadStop(g_bleClientSendQueueHandle);
1722 ca_mutex_unlock(g_bleClientSendDataMutex);
1726 CAStopLEGattClient();
1728 return CA_STATUS_OK;
1732 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1733 CANetworkPacketReceivedCallback reqRespCallback,
1734 CAAdapterChangeCallback netCallback,
1735 CAConnectionChangeCallback connCallback,
1736 CAErrorHandleCallback errorCallback,
1737 ca_thread_pool_t handle)
1739 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1742 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1743 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1744 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1745 VERIFY_NON_NULL(connCallback, CALEADAPTER_TAG, "ConnectionChange Callback is null");
1747 CAResult_t result = CA_STATUS_OK;
1748 result = CAInitLEAdapterMutex();
1749 if (CA_STATUS_OK != result)
1751 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1752 return CA_STATUS_FAILED;
1755 result = CAInitializeLENetworkMonitor();
1756 if (CA_STATUS_OK != result)
1758 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1759 return CA_STATUS_FAILED;
1761 CAInitializeLEAdapter();
1763 CASetLEClientThreadPoolHandle(handle);
1765 result = CAInitializeLEGattClient();
1766 if (CA_STATUS_OK != result)
1768 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattClient() failed");
1769 return CA_STATUS_FAILED;
1772 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1773 CASetLEServerThreadPoolHandle(handle);
1774 result = CAInitializeLEGattServer();
1775 if (CA_STATUS_OK != result)
1777 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattServer() failed");
1778 return CA_STATUS_FAILED;
1781 CASetLEAdapterThreadPoolHandle(handle);
1782 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1783 CASetLEReqRespAdapterCallback(reqRespCallback);
1785 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1786 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1787 CALERegisterNetworkNotifications(netCallback, connCallback);
1789 g_errorHandler = errorCallback;
1791 static const CAConnectivityHandler_t connHandler =
1793 .startAdapter = CAStartLE,
1794 .stopAdapter = CAStopLE,
1795 .startListenServer = CAStartLEListeningServer,
1796 .stopListenServer = CAStopLEListeningServer,
1797 .startDiscoveryServer = CAStartLEDiscoveryServer,
1798 .sendData = CASendLEUnicastData,
1799 .sendDataToAll = CASendLEMulticastData,
1800 .GetnetInfo = CAGetLEInterfaceInformation,
1801 .readData = CAReadLEData,
1802 .terminate = CATerminateLE,
1803 .cType = CA_ADAPTER_GATT_BTLE
1806 registerCallback(connHandler);
1808 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1810 return CA_STATUS_OK;
1813 static CAResult_t CAStartLE()
1815 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE");
1817 return CAStartLEAdapter();
1820 static CAResult_t CAStopLE()
1822 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1823 #ifndef SINGLE_THREAD
1827 ca_mutex_lock(g_bleIsServerMutex);
1828 switch (g_adapterType)
1830 case ADAPTER_SERVER:
1831 CALEAdapterGattServerStop();
1833 case ADAPTER_CLIENT:
1834 CALEAdapterGattClientStop();
1836 case ADAPTER_BOTH_CLIENT_SERVER:
1837 CALEAdapterGattServerStop();
1838 CALEAdapterGattClientStop();
1843 ca_mutex_unlock(g_bleIsServerMutex);
1845 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1847 return CAStopLEAdapter();
1850 static void CATerminateLE()
1852 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1854 CASetLEReqRespServerCallback(NULL);
1855 CASetLEReqRespClientCallback(NULL);
1856 CALERegisterNetworkNotifications(NULL, NULL);
1857 CASetLEReqRespAdapterCallback(NULL);
1858 CATerminateLENetworkMonitor();
1860 ca_mutex_lock(g_bleIsServerMutex);
1861 switch (g_adapterType)
1863 case ADAPTER_SERVER:
1864 CATerminateLEGattServer();
1866 case ADAPTER_CLIENT:
1867 CATerminateLEGattClient();
1869 case ADAPTER_BOTH_CLIENT_SERVER:
1870 CATerminateLEGattServer();
1871 CATerminateLEGattClient();
1876 g_adapterType = ADAPTER_EMPTY;
1877 ca_mutex_unlock(g_bleIsServerMutex);
1879 #ifndef SINGLE_THREAD
1880 CATerminateLEQueues();
1882 CATerminateLEAdapterMutex();
1884 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1887 static CAResult_t CAStartLEListeningServer()
1889 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEListeningServer");
1890 #ifndef ROUTING_GATEWAY
1891 CAResult_t result = CA_STATUS_OK;
1892 #ifndef SINGLE_THREAD
1893 result = CAInitLEServerQueues();
1894 if (CA_STATUS_OK != result)
1896 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1901 ca_mutex_lock(g_bleIsServerMutex);
1902 switch (g_adapterType)
1904 case ADAPTER_CLIENT:
1905 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1907 case ADAPTER_BOTH_CLIENT_SERVER:
1910 g_adapterType = ADAPTER_SERVER;
1912 ca_mutex_unlock(g_bleIsServerMutex);
1914 result = CAGetLEAdapterState();
1915 if (CA_STATUS_OK != result)
1917 if (CA_ADAPTER_NOT_ENABLED == result)
1921 "Listen Server will be started once BT Adapter is enabled");
1922 result = CA_STATUS_OK;
1927 result = CALEAdapterGattServerStart();
1930 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1933 // Routing Gateway only supports BLE client mode.
1934 OIC_LOG(ERROR, CALEADAPTER_TAG, "LE server not supported in Routing Gateway");
1935 return CA_NOT_SUPPORTED;
1939 static CAResult_t CAStopLEListeningServer()
1941 OIC_LOG(ERROR, CALEADAPTER_TAG, "Listen server stop not supported.");
1942 return CA_NOT_SUPPORTED;
1945 static CAResult_t CAStartLEDiscoveryServer()
1947 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEDiscoveryServer");
1948 CAResult_t result = CA_STATUS_OK;
1949 #ifndef SINGLE_THREAD
1950 result = CAInitLEClientQueues();
1951 if (CA_STATUS_OK != result)
1953 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1958 ca_mutex_lock(g_bleIsServerMutex);
1959 switch (g_adapterType)
1961 case ADAPTER_SERVER:
1962 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1964 case ADAPTER_BOTH_CLIENT_SERVER:
1967 g_adapterType = ADAPTER_CLIENT;
1969 ca_mutex_unlock(g_bleIsServerMutex);
1971 result = CAGetLEAdapterState();
1972 if (CA_STATUS_OK != result)
1974 if (CA_ADAPTER_NOT_ENABLED == result)
1978 "Discovery Server will be started once BT Adapter is enabled");
1979 result = CA_STATUS_OK;
1984 result = CALEAdapterGattClientStart();
1987 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1991 static CAResult_t CAReadLEData()
1993 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1994 #ifdef SINGLE_THREAD
1997 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1998 return CA_STATUS_OK;
2001 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
2004 CADataType_t dataType)
2006 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN - CASendLEUnicastData : type(%d)", dataType);
2009 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
2010 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
2012 CAResult_t result = CA_STATUS_FAILED;
2014 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
2015 if (ADAPTER_EMPTY == g_adapterType)
2017 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2020 ca_mutex_lock(g_bleIsServerMutex);
2021 if (ADAPTER_SERVER == g_adapterType ||
2022 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_DATA == dataType))
2024 result = CALEAdapterServerSendData(endpoint, data, dataLen);
2025 if (CA_STATUS_OK != result)
2027 ca_mutex_unlock(g_bleIsServerMutex);
2028 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed");
2031 g_errorHandler(endpoint, data, dataLen, result);
2038 if (ADAPTER_CLIENT == g_adapterType ||
2039 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_REQUEST_DATA == dataType) ||
2040 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_FOR_RES == dataType))
2042 result = CALEAdapterClientSendData(endpoint, data, dataLen);
2043 if (CA_STATUS_OK != result)
2045 ca_mutex_unlock(g_bleIsServerMutex);
2046 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for client failed" );
2050 g_errorHandler(endpoint, data, dataLen, result);
2055 ca_mutex_unlock(g_bleIsServerMutex);
2057 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2061 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
2064 CADataType_t dataType)
2066 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEMulticastData");
2069 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
2073 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
2077 CAResult_t result = CA_STATUS_FAILED;
2079 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
2080 if (ADAPTER_EMPTY == g_adapterType)
2082 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2085 ca_mutex_lock(g_bleIsServerMutex);
2086 if (ADAPTER_SERVER == g_adapterType ||
2087 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_DATA == dataType))
2089 result = CALEAdapterServerSendData(NULL, data, dataLen);
2090 if (CA_STATUS_OK != result)
2092 ca_mutex_unlock(g_bleIsServerMutex);
2094 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data for server failed" );
2098 g_errorHandler(endpoint, data, dataLen, result);
2104 if (ADAPTER_CLIENT == g_adapterType ||
2105 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_REQUEST_DATA == dataType) ||
2106 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_FOR_RES == dataType))
2108 result = CALEAdapterClientSendData(NULL, data, dataLen);
2109 if (CA_STATUS_OK != result)
2111 ca_mutex_unlock(g_bleIsServerMutex);
2113 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data for client failed" );
2117 g_errorHandler(endpoint, data, dataLen, result);
2122 ca_mutex_unlock(g_bleIsServerMutex);
2124 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CASendLEMulticastData");
2128 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
2130 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2132 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
2134 char *local_address = NULL;
2136 CAResult_t res = CAGetLEAddress(&local_address);
2137 if (CA_STATUS_OK != res)
2139 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
2143 if (NULL == local_address)
2145 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
2146 return CA_STATUS_FAILED;
2150 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
2151 if (NULL == (*info))
2153 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
2154 OICFree(local_address);
2155 return CA_STATUS_FAILED;
2158 size_t local_address_len = strlen(local_address);
2160 if(local_address_len >= sizeof(g_localBLEAddress) ||
2161 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
2163 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
2165 OICFree(local_address);
2166 return CA_STATUS_FAILED;
2169 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
2170 ca_mutex_lock(g_bleLocalAddressMutex);
2171 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
2172 ca_mutex_unlock(g_bleLocalAddressMutex);
2174 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
2176 OICFree(local_address);
2178 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2179 return CA_STATUS_OK;
2182 static CAResult_t CALERegisterNetworkNotifications(CAAdapterChangeCallback netCallback,
2183 CAConnectionChangeCallback connCallback)
2185 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2187 ca_mutex_lock(g_bleNetworkCbMutex);
2188 g_networkCallback = netCallback;
2189 g_connectionCallback = connCallback;
2190 ca_mutex_unlock(g_bleNetworkCbMutex);
2191 CAResult_t res = CA_STATUS_OK;
2194 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
2195 if (CA_STATUS_OK != res)
2197 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2202 res = CAUnSetLEAdapterStateChangedCb();
2203 if (CA_STATUS_OK != res)
2205 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2209 if (g_connectionCallback)
2211 res = CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCb);
2212 if (CA_STATUS_OK != res)
2214 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLENWConnectionStateChangedCb failed!");
2218 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2222 static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
2225 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEConnectionStateChangedCb");
2227 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2231 ca_mutex_lock(g_bleIsServerMutex);
2232 switch (g_adapterType)
2234 case ADAPTER_SERVER:
2235 CALEGattServerConnectionStateChanged(isConnected, address);
2237 case ADAPTER_CLIENT:
2238 CALEGattConnectionStateChanged(isConnected, address);
2240 case ADAPTER_BOTH_CLIENT_SERVER:
2241 CALEGattConnectionStateChanged(isConnected, address);
2242 CALEGattServerConnectionStateChanged(isConnected, address);
2247 ca_mutex_unlock(g_bleIsServerMutex);
2252 #ifndef SINGLE_THREAD
2253 if(g_bleClientSenderInfo)
2255 CALERemoveReceiveQueueData(g_bleClientSenderInfo, address);
2258 if(g_bleServerSenderInfo)
2260 CALERemoveReceiveQueueData(g_bleServerSenderInfo, address);
2263 // remove data of send queue.
2264 if (g_bleClientSendQueueHandle)
2266 CALERemoveSendQueueData(g_bleClientSendQueueHandle,
2267 g_bleClientSendDataMutex,
2271 if (g_bleServerSendQueueHandle)
2273 CALERemoveSendQueueData(g_bleServerSendQueueHandle,
2274 g_bleServerSendDataMutex,
2280 CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE };
2281 OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), address);
2283 ca_mutex_lock(g_bleNetworkCbMutex);
2284 if (g_connectionCallback)
2286 g_connectionCallback(&localEndpoint, isConnected);
2288 ca_mutex_unlock(g_bleNetworkCbMutex);
2290 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2293 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
2295 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDeviceStateChangedCb");
2297 if (CA_ADAPTER_ENABLED == adapter_state)
2299 ca_mutex_lock(g_bleIsServerMutex);
2300 switch (g_adapterType)
2302 case ADAPTER_SERVER:
2303 CALEAdapterGattServerStart();
2305 case ADAPTER_CLIENT:
2306 CALEAdapterGattClientStart();
2308 case ADAPTER_BOTH_CLIENT_SERVER:
2309 CALEAdapterGattServerStart();
2310 CALEAdapterGattClientStart();
2315 ca_mutex_unlock(g_bleIsServerMutex);
2319 ca_mutex_lock(g_bleIsServerMutex);
2320 switch (g_adapterType)
2322 case ADAPTER_SERVER:
2323 CALEAdapterGattServerStop();
2325 case ADAPTER_CLIENT:
2326 CALEAdapterGattClientStop();
2328 case ADAPTER_BOTH_CLIENT_SERVER:
2329 CALEAdapterGattServerStop();
2330 CALEAdapterGattClientStop();
2335 ca_mutex_unlock(g_bleIsServerMutex);
2338 ca_mutex_lock(g_bleNetworkCbMutex);
2339 if (NULL != g_networkCallback)
2341 g_networkCallback(CA_ADAPTER_GATT_BTLE, adapter_state);
2345 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
2347 ca_mutex_unlock(g_bleNetworkCbMutex);
2349 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2352 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
2353 const uint8_t *data,
2356 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2358 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2359 #ifndef SINGLE_THREAD
2360 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2361 "g_bleClientSendQueueHandle is NULL",
2363 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
2364 "g_bleClientSendDataMutex is NULL",
2367 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2368 "g_bleClientSendQueueHandle",
2371 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
2373 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2376 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2377 return CA_MEMORY_ALLOC_FAILED;
2379 // Add message to send queue
2380 ca_mutex_lock(g_bleClientSendDataMutex);
2381 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
2382 ca_mutex_unlock(g_bleClientSendDataMutex);
2384 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2385 return CA_STATUS_OK;
2388 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2389 const uint8_t *data,
2392 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2394 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2396 #ifdef SINGLE_THREAD
2397 if (!CAIsLEConnected())
2399 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2400 return CA_STATUS_FAILED;
2403 CAResult_t result = CA_STATUS_OK;
2404 const uint32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2405 for (uint32_t iter = 0; iter < dataLimit; iter++)
2408 CAUpdateCharacteristicsToAllGattClients(
2409 data + (iter * CA_SUPPORTED_BLE_MTU_SIZE),
2410 CA_SUPPORTED_BLE_MTU_SIZE);
2412 if (CA_STATUS_OK != result)
2414 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2415 return CA_STATUS_FAILED;
2421 const uint32_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2425 CAUpdateCharacteristicsToAllGattClients(
2426 data + (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE),
2428 if (CA_STATUS_OK != result)
2430 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2431 return CA_STATUS_FAILED;
2436 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2437 "BleClientReceiverQueue is NULL",
2439 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2440 "BleClientSendDataMutex is NULL",
2443 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2446 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2448 CALEData_t * const bleData =
2449 CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2453 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2454 return CA_MEMORY_ALLOC_FAILED;
2457 // Add message to send queue
2458 ca_mutex_lock(g_bleServerSendDataMutex);
2459 CAQueueingThreadAddData(g_bleServerSendQueueHandle,
2461 sizeof(CALEData_t));
2462 ca_mutex_unlock(g_bleServerSendDataMutex);
2464 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2465 return CA_STATUS_OK;
2468 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2469 const uint8_t *data,
2470 uint32_t dataLength,
2471 uint32_t *sentLength)
2473 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2476 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2477 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2479 #ifdef SINGLE_THREAD
2480 if(g_networkPacketReceivedCallback)
2482 // will be filled by upper layer
2483 const CASecureEndpoint_t endpoint =
2484 { .endpoint = { .adapter = CA_ADAPTER_GATT_BTLE } };
2487 g_networkPacketReceivedCallback(&endpoint, data, dataLength);
2490 VERIFY_NON_NULL_RET(g_bleReceiverQueue,
2492 "g_bleReceiverQueue",
2495 //Add message to data queue
2496 CAEndpoint_t * const remoteEndpoint =
2497 CACreateEndpointObject(CA_DEFAULT_FLAGS,
2498 CA_ADAPTER_GATT_BTLE,
2502 if (NULL == remoteEndpoint)
2504 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2505 return CA_STATUS_FAILED;
2508 // Create bleData to add to queue
2511 "Data received from LE layer [%d]",
2514 CALEData_t * const bleData =
2515 CACreateLEData(remoteEndpoint, data, dataLength, g_bleServerSenderInfo);
2519 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2520 CAFreeEndpoint(remoteEndpoint);
2521 return CA_MEMORY_ALLOC_FAILED;
2524 CAFreeEndpoint(remoteEndpoint);
2525 // Add message to receiver queue
2526 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2528 *sentLength = dataLength;
2530 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2531 return CA_STATUS_OK;
2534 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2535 const uint8_t *data,
2536 uint32_t dataLength,
2537 uint32_t *sentLength)
2539 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2542 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2543 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2544 #ifndef SINGLE_THREAD
2545 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG,
2546 "g_bleReceiverQueue",
2549 //Add message to data queue
2550 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2551 CA_ADAPTER_GATT_BTLE,
2553 if (NULL == remoteEndpoint)
2555 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2556 return CA_STATUS_FAILED;
2559 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%u]", dataLength);
2561 // Create bleData to add to queue
2562 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data,
2563 dataLength, g_bleClientSenderInfo);
2566 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2567 CAFreeEndpoint(remoteEndpoint);
2568 return CA_MEMORY_ALLOC_FAILED;
2571 CAFreeEndpoint(remoteEndpoint);
2572 // Add message to receiver queue
2573 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2575 *sentLength = dataLength;
2577 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2578 return CA_STATUS_OK;
2581 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2583 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2585 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2586 g_bleAdapterThreadPool = handle;
2587 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2589 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2592 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2594 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2596 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2598 g_networkPacketReceivedCallback = callback;
2600 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2602 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2605 static void CALEErrorHandler(const char *remoteAddress,
2606 const uint8_t *data,
2610 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2612 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2614 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2615 CA_ADAPTER_GATT_BTLE,
2619 // if required, will be used to build remote endpoint
2620 g_errorHandler(rep, data, dataLen, result);
2622 CAFreeEndpoint(rep);
2624 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");
2627 #ifndef SINGLE_THREAD
2628 static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle, ca_mutex mutex,
2629 const char* address)
2631 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALERemoveSendQueueData");
2633 VERIFY_NON_NULL_VOID(queueHandle, CALEADAPTER_TAG, "queueHandle");
2634 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2636 ca_mutex_lock(mutex);
2637 while (u_queue_get_size(queueHandle->dataQueue) > 0)
2639 OIC_LOG(DEBUG, CALEADAPTER_TAG, "get data from queue");
2640 u_queue_message_t *message = u_queue_get_element(queueHandle->dataQueue);
2641 if (NULL != message)
2643 CALEData_t *bleData = (CALEData_t *) message->msg;
2644 if (bleData && bleData->remoteEndpoint)
2646 if (!strcmp(bleData->remoteEndpoint->addr, address))
2648 OIC_LOG(DEBUG, CALEADAPTER_TAG, "found the message of disconnected device");
2649 if (NULL != queueHandle->destroy)
2651 queueHandle->destroy(message->msg, message->size);
2655 OICFree(message->msg);
2663 ca_mutex_unlock(mutex);
2666 static void CALERemoveReceiveQueueData(u_arraylist_t *dataInfoList, const char* address)
2668 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALERemoveReceiveQueueData");
2670 VERIFY_NON_NULL_VOID(dataInfoList, CALEADAPTER_TAG, "dataInfoList");
2671 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2673 CABLESenderInfo_t *senderInfo = NULL;
2674 uint32_t senderIndex = 0;
2676 if(CA_STATUS_OK == CALEGetSenderInfo(address, dataInfoList, &senderInfo,
2679 u_arraylist_remove(dataInfoList, senderIndex);
2680 OICFree(senderInfo->defragData);
2681 OICFree(senderInfo->remoteEndpoint);
2682 OICFree(senderInfo);
2684 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo is removed for disconnection");
2688 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo doesn't exist");