1 /* ****************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
20 #include "caleadapter.h"
25 #include "caleinterface.h"
28 #include "caadapterutils.h"
30 #include "caqueueingthread.h"
32 #include "oic_malloc.h"
33 #include "oic_string.h"
34 #include "caremotehandler.h"
38 * Logging tag for module name.
40 #define CALEADAPTER_TAG "OIC_LE_ADAP"
43 * The MTU supported for BLE adapter
45 #define CA_SUPPORTED_BLE_MTU_SIZE 20
48 * Stores information of all the senders.
50 * This structure will be used to track and defragment all incoming
56 uint32_t totalDataLen;
58 CAEndpoint_t *remoteEndpoint;
64 ADAPTER_BOTH_CLIENT_SERVER,
70 * Callback to provide the status of the network change to CA layer.
72 static CANetworkChangeCallback g_networkCallback = NULL;
75 * bleAddress of the local adapter. Value will be initialized to zero,
76 * and will be updated later.
78 static char g_localBLEAddress[18] = { 0 };
81 * Variable to differentiate btw GattServer and GattClient.
83 static CABLEAdapter_t g_adapterType = ADAPTER_EMPTY;
86 * Mutex to synchronize the task to be executed on the GattServer
89 static ca_mutex g_bleIsServerMutex = NULL;
92 * Mutex to synchronize the callback to be called for the network
95 static ca_mutex g_bleNetworkCbMutex = NULL;
98 * Mutex to synchronize the updates of the local LE address of the
101 static ca_mutex g_bleLocalAddressMutex = NULL;
104 * Reference to thread pool.
106 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
109 * Mutex to synchronize the task to be pushed to thread pool.
111 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
114 * Mutex to synchronize the queing of the data from SenderQueue.
116 static ca_mutex g_bleClientSendDataMutex = NULL;
119 * Mutex to synchronize the queing of the data from ReceiverQueue.
121 static ca_mutex g_bleReceiveDataMutex = NULL;
124 * Mutex to synchronize the queing of the data from SenderQueue.
126 static ca_mutex g_bleServerSendDataMutex = NULL;
129 * Mutex to synchronize the callback to be called for the
130 * adapterReqResponse.
132 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
135 * Callback to be called when network packet received from either
136 * GattServer or GattClient.
138 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
141 * Callback to notify error from the BLE adapter.
143 static CAErrorHandleCallback g_errorHandler = NULL;
146 * Register network change notification callback.
148 * @param[in] netCallback CANetworkChangeCallback callback which will
149 * be set for the change in network.
151 * @return 0 on success otherwise a positive error value.
152 * @retval ::CA_STATUS_OK Successful.
153 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
154 * @retval ::CA_STATUS_FAILED Operation failed.
157 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
160 * Set the thread pool handle which is required for spawning new
163 * @param[in] handle Thread pool handle which is given by above layer
164 * for using thread creation task.
167 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
170 * Call the callback to the upper layer when the adapter state gets
173 * @param[in] adapter_state New state of the adapter to be notified to
176 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state);
179 * Call the callback to the upper layer when the device connection state gets
182 * @param[in] address LE address of the device to be notified to the upper layer.
183 * @param[in] isConnected whether connection state is connected or not.
185 static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
189 * Used to initialize all required mutex variable for LE Adapter
192 * @return 0 on success otherwise a positive error value.
193 * @retval ::CA_STATUS_OK Successful.
194 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
195 * @retval ::CA_STATUS_FAILED Operation failed.
198 static CAResult_t CAInitLEAdapterMutex();
201 * Terminate all required mutex variables for LE adapter
204 static void CATerminateLEAdapterMutex();
207 * Prepares and notify error through error callback.
209 static void CALEErrorHandler(const char *remoteAddress,
214 #ifndef SINGLE_THREAD
216 * Stop condition of recvhandler.
218 static bool g_dataBleReceiverHandlerState = false;
221 * Sender information.
223 static u_arraylist_t *g_bleServerSenderInfo = NULL;
225 static u_arraylist_t *g_bleClientSenderInfo = NULL;
228 * Queue to process the outgoing packets from GATTClient.
230 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
233 * Queue to process the incoming packets.
235 static CAQueueingThread_t *g_bleReceiverQueue = NULL;
238 * Queue to process the outgoing packets from GATTServer.
240 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
243 * This function will be associated with the sender queue for
246 * This function will fragment the data to the MTU of the transport
247 * and send the data in fragments to the adapters. The function will
248 * be blocked until all data is sent out from the adapter.
250 * @param[in] threadData Data pushed to the queue which contains the
251 * info about RemoteEndpoint and Data.
253 static void CALEServerSendDataThread(void *threadData);
256 * This function will be associated with the sender queue for
259 * This function will fragment the data to the MTU of the transport
260 * and send the data in fragments to the adapters. The function will
261 * be blocked until all data is sent out from the adapter.
263 * @param[in] threadData Data pushed to the queue which contains the
264 * info about RemoteEndpoint and Data.
266 static void CALEClientSendDataThread(void *threadData);
269 * This function will be associated with the receiver queue.
271 * This function will defragment the received data from each sender
272 * respectively and will send it up to CA layer. Respective sender's
273 * header will provide the length of the data sent.
275 * @param[in] threadData Data pushed to the queue which contains the
276 * info about RemoteEndpoint and Data.
278 static void CALEDataReceiverHandler(void *threadData);
281 * This function will stop all queues created for GattServer and
282 * GattClient. All four queues will be be stopped with this function
285 static void CAStopLEQueues();
288 * This function will terminate all queues created for GattServer and
289 * GattClient. All four queues will be be terminated with this
290 * function invocations.
292 static void CATerminateLEQueues();
295 * This function will initalize the Receiver and Sender queues for
296 * GattServer. This function will in turn call the functions
297 * CAInitBleServerReceiverQueue() and CAInitBleServerSenderQueue() to
298 * initialize the queues.
300 * @return ::CA_STATUS_OK or Appropriate error code.
301 * @retval ::CA_STATUS_OK Successful.
302 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
303 * @retval ::CA_STATUS_FAILED Operation failed.
305 static CAResult_t CAInitLEServerQueues();
308 * This function will initalize the Receiver and Sender queues for
309 * GattClient. This function will inturn call the functions
310 * CAInitBleClientReceiverQueue() and CAInitBleClientSenderQueue() to
311 * initialize the queues.
313 * @return ::CA_STATUS_OK or Appropriate error code.
314 * @retval ::CA_STATUS_OK Successful.
315 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
316 * @retval ::CA_STATUS_FAILED Operation failed.
319 static CAResult_t CAInitLEClientQueues();
322 * This function will initalize the Receiver queue for
323 * GattServer. This will initialize the queue to process the function
324 * CABLEServerSendDataThread() when ever the task is added to this
327 * @return ::CA_STATUS_OK or Appropriate error code.
328 * @retval ::CA_STATUS_OK Successful.
329 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
330 * @retval ::CA_STATUS_FAILED Operation failed.
332 static CAResult_t CAInitLEServerSenderQueue();
335 * This function will initalize the Receiver queue for
336 * GattClient. This will initialize the queue to process the function
337 * CABLEClientSendDataThread() when ever the task is added to this
340 * @return ::CA_STATUS_OK or Appropriate error code.
341 * @retval ::CA_STATUS_OK Successful.
342 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
343 * @retval ::CA_STATUS_FAILED Operation failed.
345 static CAResult_t CAInitLEClientSenderQueue();
348 * This function will initialize the Receiver queue for
349 * LEAdapter. This will initialize the queue to process the function
350 * CABLEDataReceiverHandler() when ever the task is added to this
353 * @return ::CA_STATUS_OK or Appropriate error code
354 * @retval ::CA_STATUS_OK Successful
355 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
356 * @retval ::CA_STATUS_FAILED Operation failed
359 static CAResult_t CAInitLEReceiverQueue();
362 * This function will create the Data required to send it in the
365 * @param[in] remoteEndpoint Remote endpoint information of the
367 * @param[in] data Data to be transmitted from LE.
368 * @param[in] dataLength Length of the Data being transmitted.
370 * @return ::CA_STATUS_OK or Appropriate error code.
371 * @retval ::CA_STATUS_OK Successful.
372 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
373 * @retval ::CA_STATUS_FAILED Operation failed.
375 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
378 u_arraylist_t *senderInfo);
381 * Used to free the BLE information stored in the sender/receiver
384 * @param[in] bleData Information for a particular data segment.
386 static void CAFreeLEData(CALEData_t *bleData);
391 static void CALEDataDestroyer(void *data, uint32_t size);
393 static CAResult_t CAInitLEServerQueues()
395 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
397 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
399 CAResult_t result = CAInitLEServerSenderQueue();
400 if (CA_STATUS_OK != result)
402 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
403 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
404 return CA_STATUS_FAILED;
407 g_bleServerSenderInfo = u_arraylist_create();
408 if (!g_bleServerSenderInfo)
410 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
411 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
412 return CA_MEMORY_ALLOC_FAILED;
415 result = CAInitLEReceiverQueue();
416 if (CA_STATUS_OK != result)
418 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
419 u_arraylist_free(&g_bleServerSenderInfo);
420 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
421 return CA_STATUS_FAILED;
424 g_dataBleReceiverHandlerState = true;
426 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
428 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
432 static CAResult_t CAInitLEClientQueues()
434 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
436 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
438 CAResult_t result = CAInitLEClientSenderQueue();
439 if (CA_STATUS_OK != result)
441 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
442 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
443 return CA_STATUS_FAILED;
446 g_bleClientSenderInfo = u_arraylist_create();
447 if (!g_bleClientSenderInfo)
449 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
450 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
451 return CA_MEMORY_ALLOC_FAILED;
454 result = CAInitLEReceiverQueue();
455 if (CA_STATUS_OK != result)
457 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
458 u_arraylist_free(&g_bleClientSenderInfo);
459 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
460 return CA_STATUS_FAILED;
463 g_dataBleReceiverHandlerState = true;
465 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
467 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
471 static CAResult_t CAInitLEReceiverQueue()
473 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEReceiverQueue");
474 // Check if the message queue is already initialized
475 if (g_bleReceiverQueue)
477 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
481 // Create recv message queue
482 g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
483 if (!g_bleReceiverQueue)
485 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
486 return CA_MEMORY_ALLOC_FAILED;
489 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue,
490 g_bleAdapterThreadPool,
491 CALEDataReceiverHandler,
494 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
495 OICFree(g_bleReceiverQueue);
496 g_bleReceiverQueue = NULL;
497 return CA_STATUS_FAILED;
500 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue))
502 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
503 OICFree(g_bleReceiverQueue);
504 g_bleReceiverQueue = NULL;
505 return CA_STATUS_FAILED;
508 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
512 static CAResult_t CAInitLEServerSenderQueue()
514 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEServerSenderQueue");
515 // Check if the message queue is already initialized
516 if (g_bleServerSendQueueHandle)
518 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
522 // Create send message queue
523 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
524 if (!g_bleServerSendQueueHandle)
526 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
527 return CA_MEMORY_ALLOC_FAILED;
530 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
531 g_bleAdapterThreadPool,
532 CALEServerSendDataThread,
535 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
536 OICFree(g_bleServerSendQueueHandle);
537 g_bleServerSendQueueHandle = NULL;
538 return CA_STATUS_FAILED;
541 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
545 static void CALEClearSenderInfoImpl(u_arraylist_t ** list)
547 const size_t length = u_arraylist_length(*list);
548 for (size_t i = 0; i < length; ++i)
550 CABLESenderInfo_t * const info =
551 (CABLESenderInfo_t *) u_arraylist_get(*list, i);
554 OICFree(info->defragData);
555 CAFreeEndpoint(info->remoteEndpoint);
559 u_arraylist_free(list);
562 static void CALEClearSenderInfo()
564 CALEClearSenderInfoImpl(&g_bleServerSenderInfo);
565 CALEClearSenderInfoImpl(&g_bleClientSenderInfo);
568 static CAResult_t CAInitLEClientSenderQueue()
570 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEClientSenderQueue");
572 if (g_bleClientSendQueueHandle)
574 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
578 // Create send message queue
579 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
580 if (!g_bleClientSendQueueHandle)
582 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
583 return CA_MEMORY_ALLOC_FAILED;
586 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
587 g_bleAdapterThreadPool,
588 CALEClientSendDataThread, CALEDataDestroyer))
590 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
591 OICFree(g_bleClientSendQueueHandle);
592 g_bleClientSendQueueHandle = NULL;
593 return CA_STATUS_FAILED;
596 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAInitLEClientSenderQueue");
600 static void CAStopLEQueues()
602 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStopLEQueues");
604 ca_mutex_lock(g_bleReceiveDataMutex);
605 if (NULL != g_bleReceiverQueue)
607 CAQueueingThreadStop(g_bleReceiverQueue);
609 ca_mutex_unlock(g_bleReceiveDataMutex);
611 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAStopLEQueues");
614 static void CATerminateLEQueues()
616 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
618 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
619 OICFree(g_bleClientSendQueueHandle);
620 g_bleClientSendQueueHandle = NULL;
622 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
623 OICFree(g_bleServerSendQueueHandle);
624 g_bleServerSendQueueHandle = NULL;
626 CAQueueingThreadDestroy(g_bleReceiverQueue);
627 OICFree(g_bleReceiverQueue);
628 g_bleReceiverQueue = NULL;
630 CALEClearSenderInfo();
632 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
635 static CAResult_t CALEGetSenderInfo(const char *leAddress,
636 u_arraylist_t *senderInfoList,
637 CABLESenderInfo_t **senderInfo,
638 uint32_t *senderIndex)
640 VERIFY_NON_NULL_RET(leAddress,
642 "NULL BLE address argument",
643 CA_STATUS_INVALID_PARAM);
644 VERIFY_NON_NULL_RET(senderIndex,
646 "NULL index argument",
647 CA_STATUS_INVALID_PARAM);
649 const uint32_t listLength = u_arraylist_length(senderInfoList);
650 const uint32_t addrLength = strlen(leAddress);
651 for (uint32_t index = 0; index < listLength; index++)
653 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(senderInfoList, index);
654 if(!info || !(info->remoteEndpoint))
659 if(!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
661 *senderIndex = index;
670 return CA_STATUS_FAILED;
673 static void CALEDataReceiverHandler(void *threadData)
675 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDataReceiverHandler");
677 ca_mutex_lock(g_bleReceiveDataMutex);
679 if (g_dataBleReceiverHandlerState)
681 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
683 CALEData_t *bleData = (CALEData_t *) threadData;
686 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
687 ca_mutex_unlock(g_bleReceiveDataMutex);
691 if (!(bleData->senderInfo))
693 OIC_LOG(ERROR, CALEADAPTER_TAG, "sender info is not available");
694 ca_mutex_unlock(g_bleReceiveDataMutex);
698 if (!(bleData->remoteEndpoint))
700 OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!");
701 ca_mutex_unlock(g_bleReceiveDataMutex);
705 CABLESenderInfo_t *senderInfo = NULL;
706 uint32_t senderIndex = 0;
708 if(CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
710 &senderInfo, &senderIndex))
712 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s]",
713 bleData->remoteEndpoint->addr);
718 CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
721 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
722 ca_mutex_unlock(g_bleReceiveDataMutex);
725 newSender->recvDataLen = 0;
726 newSender->totalDataLen = 0;
727 newSender->defragData = NULL;
728 newSender->remoteEndpoint = NULL;
730 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
732 newSender->totalDataLen = coap_get_total_message_length(bleData->data, bleData->dataLen);
734 if(!(newSender->totalDataLen))
736 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
738 ca_mutex_unlock(g_bleReceiveDataMutex);
742 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
743 newSender->totalDataLen);
744 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
747 newSender->defragData = OICCalloc(newSender->totalDataLen + 1,
748 sizeof(*newSender->defragData));
750 if (NULL == newSender->defragData)
752 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
754 ca_mutex_unlock(g_bleReceiveDataMutex);
758 const char *remoteAddress = bleData->remoteEndpoint->addr;
759 newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
760 CA_ADAPTER_GATT_BTLE,
763 if (NULL == newSender->remoteEndpoint)
765 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
766 OICFree(newSender->defragData);
768 ca_mutex_unlock(g_bleReceiveDataMutex);
772 if (newSender->recvDataLen + bleData->dataLen > newSender->totalDataLen)
774 OIC_LOG(ERROR, CALEADAPTER_TAG, "buffer is smaller than received data");
775 OICFree(newSender->defragData);
776 CAFreeEndpoint(newSender->remoteEndpoint);
778 ca_mutex_unlock(g_bleReceiveDataMutex);
781 memcpy(newSender->defragData, bleData->data, bleData->dataLen);
782 newSender->recvDataLen += bleData->dataLen;
784 u_arraylist_add(bleData->senderInfo,(void *)newSender);
786 //Getting newSender index position in bleSenderInfo array list
788 CALEGetSenderInfo(newSender->remoteEndpoint->addr, bleData->senderInfo,
791 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
792 OICFree(newSender->defragData);
793 CAFreeEndpoint(newSender->remoteEndpoint);
795 ca_mutex_unlock(g_bleReceiveDataMutex);
798 senderInfo = newSender;
802 if(senderInfo->recvDataLen + bleData->dataLen > senderInfo->totalDataLen)
804 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
805 "Data Length exceeding error!! Receiving [%d] total length [%d]",
806 senderInfo->recvDataLen + bleData->dataLen, senderInfo->totalDataLen);
807 u_arraylist_remove(bleData->senderInfo, senderIndex);
808 OICFree(senderInfo->defragData);
810 ca_mutex_unlock(g_bleReceiveDataMutex);
813 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]",
815 memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data,
817 senderInfo->recvDataLen += bleData->dataLen ;
818 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] received Datalen [%d]",
819 senderInfo->totalDataLen, senderInfo->recvDataLen);
822 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
824 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
825 if (NULL == g_networkPacketReceivedCallback)
827 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
829 u_arraylist_remove(bleData->senderInfo, senderIndex);
830 OICFree(senderInfo->defragData);
832 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
833 ca_mutex_unlock(g_bleReceiveDataMutex);
837 OIC_LOG(DEBUG, CALEADAPTER_TAG, "[CALEDataReceiverHandler] Sending data up !");
839 const CASecureEndpoint_t tmp =
841 .endpoint = *senderInfo->remoteEndpoint
844 g_networkPacketReceivedCallback(&tmp,
845 senderInfo->defragData,
846 senderInfo->recvDataLen);
847 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
848 u_arraylist_remove(bleData->senderInfo, senderIndex);
849 senderInfo->remoteEndpoint = NULL;
850 senderInfo->defragData = NULL;
854 ca_mutex_unlock(g_bleReceiveDataMutex);
855 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
858 static void CALEServerSendDataThread(void *threadData)
860 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEServerSendDataThread");
862 CALEData_t * const bleData = (CALEData_t *) threadData;
865 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
869 const uint32_t totalLength = bleData->dataLen;
873 "Server total Data length with header is [%u]",
876 uint8_t * const dataSegment = OICCalloc(totalLength, 1);
878 if (NULL == dataSegment)
880 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
885 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
887 length = totalLength;
888 memcpy(dataSegment, bleData->data, bleData->dataLen);
892 length = CA_SUPPORTED_BLE_MTU_SIZE;
893 memcpy(dataSegment, bleData->data, CA_SUPPORTED_BLE_MTU_SIZE);
896 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
898 CAResult_t result = CA_STATUS_FAILED;
900 // Send the first segment with the header.
901 if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
903 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
905 result = CAUpdateCharacteristicsToGattClient(
906 bleData->remoteEndpoint->addr, dataSegment, length);
908 if (CA_STATUS_OK != result)
912 "Update characteristics failed, result [%d]",
915 g_errorHandler(bleData->remoteEndpoint,
919 OICFree(dataSegment);
925 "Server Sent data length [%u]",
927 for (index = 1; index < iter; index++)
929 // Send the remaining header.
932 "Sending the chunk number [%u]",
936 CAUpdateCharacteristicsToGattClient(
937 bleData->remoteEndpoint->addr,
938 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE)),
939 CA_SUPPORTED_BLE_MTU_SIZE);
941 if (CA_STATUS_OK != result)
943 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
944 "Update characteristics failed, result [%d]", result);
945 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
946 OICFree(dataSegment);
949 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
950 CA_SUPPORTED_BLE_MTU_SIZE);
953 const uint32_t remainingLen =
954 totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
956 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
958 // send the last segment of the data (Ex: 22 bytes of 622
959 // bytes of data when MTU is 200)
960 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
962 result = CAUpdateCharacteristicsToGattClient(
963 bleData->remoteEndpoint->addr,
964 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
967 if (CA_STATUS_OK != result)
971 "Update characteristics failed, result [%d]",
973 g_errorHandler(bleData->remoteEndpoint,
977 OICFree(dataSegment);
980 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
985 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
986 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
987 if (CA_STATUS_OK != result)
989 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
991 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
992 OICFree(dataSegment);
995 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
996 for (index = 1; index < iter; index++)
998 // Send the remaining header.
999 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1001 result = CAUpdateCharacteristicsToAllGattClients(
1002 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE)),
1003 CA_SUPPORTED_BLE_MTU_SIZE);
1005 if (CA_STATUS_OK != result)
1007 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1009 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1010 OICFree(dataSegment);
1013 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%u]",
1014 CA_SUPPORTED_BLE_MTU_SIZE);
1017 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1018 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1020 // send the last segment of the data
1021 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1023 result = CAUpdateCharacteristicsToAllGattClients(
1024 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1027 if (CA_STATUS_OK != result)
1029 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1031 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1032 OICFree(dataSegment);
1035 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1038 OICFree(dataSegment);
1040 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CALEServerSendDataThread");
1043 static void CALEClientSendDataThread(void *threadData)
1045 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEClientSendDataThread");
1047 CALEData_t *bleData = (CALEData_t *) threadData;
1050 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1054 const uint32_t totalLength = bleData->dataLen;
1056 uint8_t *dataSegment = OICCalloc(totalLength, 1);
1057 if (NULL == dataSegment)
1059 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1063 uint32_t length = 0;
1064 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1066 length = totalLength;
1073 length = CA_SUPPORTED_BLE_MTU_SIZE;
1076 CA_SUPPORTED_BLE_MTU_SIZE);
1079 CAResult_t result = CA_STATUS_FAILED;
1080 const uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1082 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1084 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1085 // Send the first segment with the header.
1087 CAUpdateCharacteristicsToGattServer(
1088 bleData->remoteEndpoint->addr,
1094 if (CA_STATUS_OK != result)
1098 "Update characteristics failed, result [%d]",
1100 g_errorHandler(bleData->remoteEndpoint,
1104 OICFree(dataSegment);
1110 "Client Sent Data length is [%u]",
1113 for (index = 1; index < iter; index++)
1115 // Send the remaining header.
1116 result = CAUpdateCharacteristicsToGattServer(
1117 bleData->remoteEndpoint->addr,
1118 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1119 CA_SUPPORTED_BLE_MTU_SIZE,
1122 if (CA_STATUS_OK != result)
1126 "Update characteristics failed, result [%d]",
1128 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1129 OICFree(dataSegment);
1132 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1133 CA_SUPPORTED_BLE_MTU_SIZE);
1136 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1137 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1139 // send the last segment of the data (Ex: 22 bytes of 622
1140 // bytes of data when MTU is 200)
1141 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1143 result = CAUpdateCharacteristicsToGattServer(
1144 bleData->remoteEndpoint->addr,
1145 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1149 if (CA_STATUS_OK != result)
1151 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1153 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1154 OICFree(dataSegment);
1157 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1162 //Sending Mulitcast Data
1163 // Send the first segment with the header.
1164 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1165 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1166 if (CA_STATUS_OK != result)
1168 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1169 "Update characteristics (all) failed, result [%d]", result);
1170 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1171 OICFree(dataSegment);
1174 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1175 // Send the remaining header.
1176 for (index = 1; index < iter; index++)
1178 result = CAUpdateCharacteristicsToAllGattServers(
1179 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1180 CA_SUPPORTED_BLE_MTU_SIZE);
1182 if (CA_STATUS_OK != result)
1184 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1186 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1187 OICFree(dataSegment);
1190 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1191 CA_SUPPORTED_BLE_MTU_SIZE);
1194 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1195 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1197 // send the last segment of the data (Ex: 22 bytes of 622
1198 // bytes of data when MTU is 200)
1199 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1201 CAUpdateCharacteristicsToAllGattServers(
1202 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1205 if (CA_STATUS_OK != result)
1207 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1208 "Update characteristics (all) failed, result [%d]", result);
1209 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1210 OICFree(dataSegment);
1213 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1218 OICFree(dataSegment);
1220 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1223 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
1224 const uint8_t *data,
1225 uint32_t dataLength,
1226 u_arraylist_t *senderInfo)
1228 CALEData_t * const bleData = OICMalloc(sizeof(CALEData_t));
1232 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1236 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1237 bleData->data = OICCalloc(dataLength + 1, 1);
1239 if (NULL == bleData->data)
1241 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1242 CAFreeLEData(bleData);
1246 memcpy(bleData->data, data, dataLength);
1247 bleData->dataLen = dataLength;
1250 bleData->senderInfo = senderInfo;
1256 static void CAFreeLEData(CALEData_t *bleData)
1258 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1260 CAFreeEndpoint(bleData->remoteEndpoint);
1261 OICFree(bleData->data);
1265 static void CALEDataDestroyer(void *data, uint32_t size)
1267 if ((size_t)size < sizeof(CALEData_t *))
1269 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1270 "Destroy data too small %p %d", data, size);
1272 CALEData_t *ledata = (CALEData_t *) data;
1274 CAFreeLEData(ledata);
1278 static CAResult_t CAInitLEAdapterMutex()
1280 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEAdapterMutex");
1282 if (NULL == g_bleIsServerMutex)
1284 g_bleIsServerMutex = ca_mutex_new();
1285 if (NULL == g_bleIsServerMutex)
1287 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1288 return CA_STATUS_FAILED;
1292 if (NULL == g_bleNetworkCbMutex)
1294 g_bleNetworkCbMutex = ca_mutex_new();
1295 if (NULL == g_bleNetworkCbMutex)
1297 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1298 CATerminateLEAdapterMutex();
1299 return CA_STATUS_FAILED;
1303 if (NULL == g_bleLocalAddressMutex)
1305 g_bleLocalAddressMutex = ca_mutex_new();
1306 if (NULL == g_bleLocalAddressMutex)
1308 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1309 CATerminateLEAdapterMutex();
1310 return CA_STATUS_FAILED;
1314 if (NULL == g_bleAdapterThreadPoolMutex)
1316 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1317 if (NULL == g_bleAdapterThreadPoolMutex)
1319 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1320 CATerminateLEAdapterMutex();
1321 return CA_STATUS_FAILED;
1325 if (NULL == g_bleClientSendDataMutex)
1327 g_bleClientSendDataMutex = ca_mutex_new();
1328 if (NULL == g_bleClientSendDataMutex)
1330 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1331 CATerminateLEAdapterMutex();
1332 return CA_STATUS_FAILED;
1336 if (NULL == g_bleServerSendDataMutex)
1338 g_bleServerSendDataMutex = ca_mutex_new();
1339 if (NULL == g_bleServerSendDataMutex)
1341 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1342 CATerminateLEAdapterMutex();
1343 return CA_STATUS_FAILED;
1347 if (NULL == g_bleAdapterReqRespCbMutex)
1349 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1350 if (NULL == g_bleAdapterReqRespCbMutex)
1352 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1353 CATerminateLEAdapterMutex();
1354 return CA_STATUS_FAILED;
1358 if (NULL == g_bleReceiveDataMutex)
1360 g_bleReceiveDataMutex = ca_mutex_new();
1361 if (NULL == g_bleReceiveDataMutex)
1363 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1364 return CA_STATUS_FAILED;
1368 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1369 return CA_STATUS_OK;
1372 static void CATerminateLEAdapterMutex()
1374 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CATerminateLEAdapterMutex");
1376 ca_mutex_free(g_bleIsServerMutex);
1377 g_bleIsServerMutex = NULL;
1379 ca_mutex_free(g_bleNetworkCbMutex);
1380 g_bleNetworkCbMutex = NULL;
1382 ca_mutex_free(g_bleLocalAddressMutex);
1383 g_bleLocalAddressMutex = NULL;
1385 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1386 g_bleAdapterThreadPoolMutex = NULL;
1388 ca_mutex_free(g_bleClientSendDataMutex);
1389 g_bleClientSendDataMutex = NULL;
1391 ca_mutex_free(g_bleServerSendDataMutex);
1392 g_bleServerSendDataMutex = NULL;
1394 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1395 g_bleAdapterReqRespCbMutex = NULL;
1397 ca_mutex_free(g_bleReceiveDataMutex);
1398 g_bleReceiveDataMutex = NULL;
1400 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1404 * Starting LE connectivity adapters.
1406 * As its peer to peer it does not require to start any servers.
1408 * @return ::CA_STATUS_OK or Appropriate error code.
1410 static CAResult_t CAStartLE();
1413 * Start listening server for receiving multicast search requests.
1415 * Transport Specific Behavior:
1416 * LE Starts GATT Server with prefixed UUID and Characteristics
1417 * per OIC Specification.
1418 * @return ::CA_STATUS_OK or Appropriate error code.
1420 static CAResult_t CAStartLEListeningServer();
1423 * Stops listening server from receiving multicast search requests.
1425 * Transport Specific Behavior:
1426 * LE Starts GATT Server with prefixed UUID and Characteristics
1427 * per OIC Specification.
1428 * @return ::CA_STATUS_OK or Appropriate error code.
1430 static CAResult_t CAStopLEListeningServer();
1433 * Sarting discovery of servers for receiving multicast
1436 * Transport Specific Behavior:
1437 * LE Starts GATT Server with prefixed UUID and Characteristics
1438 * per OIC Specification.
1440 * @return ::CA_STATUS_OK or Appropriate error code
1442 static CAResult_t CAStartLEDiscoveryServer();
1445 * Send data to the endpoint using the adapter connectivity.
1447 * @param[in] endpoint Remote Endpoint information (like MAC address,
1448 * reference URI and connectivity type) to which
1449 * the unicast data has to be sent.
1450 * @param[in] data Data which required to be sent.
1451 * @param[in] dataLen Size of data to be sent.
1453 * @note dataLen must be > 0.
1455 * @return The number of bytes sent on the network, or -1 on error.
1457 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1462 * Send multicast data to the endpoint using the LE connectivity.
1464 * @param[in] endpoint Remote Endpoint information to which the
1465 * multicast data has to be sent.
1466 * @param[in] data Data which required to be sent.
1467 * @param[in] dataLen Size of data to be sent.
1469 * @note dataLen must be > 0.
1471 * @return The number of bytes sent on the network, or -1 on error.
1473 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1478 * Get LE Connectivity network information.
1480 * @param[out] info Local connectivity information structures.
1481 * @param[out] size Number of local connectivity structures.
1483 * @return ::CA_STATUS_OK or Appropriate error code.
1485 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info,
1489 * Read Synchronous API callback.
1491 * @return ::CA_STATUS_OK or Appropriate error code.
1493 static CAResult_t CAReadLEData();
1496 * Stopping the adapters and close socket connections.
1498 * LE Stops all GATT servers and GATT Clients.
1500 * @return ::CA_STATUS_OK or Appropriate error code.
1502 static CAResult_t CAStopLE();
1505 * Terminate the LE connectivity adapter.
1507 * Configuration information will be deleted from further use.
1509 static void CATerminateLE();
1512 * This function will receive the data from the GattServer and add the
1513 * data to the Server receiver queue.
1515 * @param[in] remoteAddress Remote address of the device from where
1517 * @param[in] data Actual data received from the remote
1519 * @param[in] dataLength Length of the data received from the
1521 * @param[in] sentLength Length of the data sent from the remote
1524 * @return ::CA_STATUS_OK or Appropriate error code.
1525 * @retval ::CA_STATUS_OK Successful.
1526 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1527 * @retval ::CA_STATUS_FAILED Operation failed.
1530 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
1531 const uint8_t *data,
1532 uint32_t dataLength,
1533 uint32_t *sentLength);
1536 * This function will receive the data from the GattClient and add the
1537 * data into the Client receiver queue.
1539 * @param[in] remoteAddress Remote address of the device from where
1541 * @param[in] data Actual data recevied from the remote
1543 * @param[in] dataLength Length of the data received from the
1545 * @param[in] sentLength Length of the data sent from the remote
1548 * @return ::CA_STATUS_OK or Appropriate error code.
1549 * @retval ::CA_STATUS_OK Successful.
1550 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1551 * @retval ::CA_STATUS_FAILED Operation failed.
1553 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
1554 const uint8_t *data,
1555 uint32_t dataLength,
1556 uint32_t *sentLength);
1559 * Set the NetworkPacket received callback to CA layer from adapter
1562 * @param[in] callback Callback handle sent from the upper layer.
1564 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
1567 * Push the data from CA layer to the Sender processor queue.
1569 * @param[in] remoteEndpoint Remote endpoint information of the
1571 * @param[in] data Data to be transmitted from LE.
1572 * @param[in] dataLen Length of the Data being transmitted.
1574 * @return ::CA_STATUS_OK or Appropriate error code.
1575 * @retval ::CA_STATUS_OK Successful.
1576 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1577 * @retval ::CA_STATUS_FAILED Operation failed.
1579 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1580 const uint8_t *data,
1584 * Push the data from CA layer to the Sender processor queue.
1586 * @param[in] remoteEndpoint Remote endpoint information of the
1588 * @param[in] data Data to be transmitted from LE.
1589 * @param[in] dataLen Length of the Data being transmitted.
1591 * @return ::CA_STATUS_OK or Appropriate error code.
1592 * @retval ::CA_STATUS_OK Successful.
1593 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1594 * @retval ::CA_STATUS_FAILED Operation failed.
1596 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1597 const uint8_t *data,
1600 static CAResult_t CALEAdapterGattServerStart()
1602 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1604 CAResult_t result = CAStartLEGattServer();
1606 #ifndef SINGLE_THREAD
1608 Don't start the server side sending queue thread until the
1609 server itself has actually started.
1611 if (CA_STATUS_OK == result)
1613 ca_mutex_lock(g_bleServerSendDataMutex);
1614 result = CAQueueingThreadStart(g_bleServerSendQueueHandle);
1615 ca_mutex_unlock(g_bleServerSendDataMutex);
1617 if (CA_STATUS_OK != result)
1621 "Unable to start server queuing thread (%d)",
1630 static CAResult_t CALEAdapterGattServerStop()
1632 #ifndef SINGLE_THREAD
1633 ca_mutex_lock(g_bleServerSendDataMutex);
1634 CAResult_t result = CAQueueingThreadStop(g_bleServerSendQueueHandle);
1635 ca_mutex_unlock(g_bleServerSendDataMutex);
1636 if (CA_STATUS_OK == result)
1638 result = CAStopLEGattServer();
1643 return CAStopLEGattServer();
1647 static CAResult_t CALEAdapterGattClientStart()
1649 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattClient");
1651 CAResult_t result = CAStartLEGattClient();
1653 #ifndef SINGLE_THREAD
1655 Don't start the client side sending queue thread until the
1656 client itself has actually started.
1658 if (CA_STATUS_OK == result)
1660 ca_mutex_lock(g_bleClientSendDataMutex);
1661 result = CAQueueingThreadStart(g_bleClientSendQueueHandle);
1662 ca_mutex_unlock(g_bleClientSendDataMutex);
1664 if (CA_STATUS_OK != result)
1668 "Unable to start client queuing thread");
1676 static CAResult_t CALEAdapterGattClientStop()
1678 #ifndef SINGLE_THREAD
1679 ca_mutex_lock(g_bleClientSendDataMutex);
1680 CAResult_t result = CAQueueingThreadStop(g_bleClientSendQueueHandle);
1681 ca_mutex_unlock(g_bleClientSendDataMutex);
1682 if (CA_STATUS_OK == result)
1684 CAStopLEGattClient();
1689 CAStopLEGattClient();
1691 return CA_STATUS_OK;
1695 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1696 CANetworkPacketReceivedCallback reqRespCallback,
1697 CANetworkChangeCallback netCallback,
1698 CAErrorHandleCallback errorCallback,
1699 ca_thread_pool_t handle)
1701 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1704 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1705 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1706 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1708 CAResult_t result = CA_STATUS_OK;
1709 result = CAInitLEAdapterMutex();
1710 if (CA_STATUS_OK != result)
1712 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1713 return CA_STATUS_FAILED;
1716 result = CAInitializeLENetworkMonitor();
1717 if (CA_STATUS_OK != result)
1719 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1720 return CA_STATUS_FAILED;
1722 CAInitializeLEAdapter(handle);
1724 CASetLEClientThreadPoolHandle(handle);
1726 result = CAInitializeLEGattClient();
1727 if (CA_STATUS_OK != result)
1729 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattClient() failed");
1730 return CA_STATUS_FAILED;
1733 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1734 CASetLEServerThreadPoolHandle(handle);
1735 result = CAInitializeLEGattServer();
1736 if (CA_STATUS_OK != result)
1738 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattServer() failed");
1739 return CA_STATUS_FAILED;
1742 CASetLEAdapterThreadPoolHandle(handle);
1743 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1744 CASetLEReqRespAdapterCallback(reqRespCallback);
1746 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1747 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1748 CALERegisterNetworkNotifications(netCallback);
1750 g_errorHandler = errorCallback;
1752 static const CAConnectivityHandler_t connHandler =
1754 .startAdapter = CAStartLE,
1755 .stopAdapter = CAStopLE,
1756 .startListenServer = CAStartLEListeningServer,
1757 .stopListenServer = CAStopLEListeningServer,
1758 .startDiscoveryServer = CAStartLEDiscoveryServer,
1759 .sendData = CASendLEUnicastData,
1760 .sendDataToAll = CASendLEMulticastData,
1761 .GetnetInfo = CAGetLEInterfaceInformation,
1762 .readData = CAReadLEData,
1763 .terminate = CATerminateLE,
1764 .cType = CA_ADAPTER_GATT_BTLE
1767 registerCallback(connHandler);
1769 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1771 return CA_STATUS_OK;
1774 static CAResult_t CAStartLE()
1776 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE");
1778 return CAStartLEAdapter();
1781 static CAResult_t CAStopLE()
1783 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1784 #ifndef SINGLE_THREAD
1788 ca_mutex_lock(g_bleIsServerMutex);
1789 switch (g_adapterType)
1791 case ADAPTER_SERVER:
1792 CALEAdapterGattServerStop();
1794 case ADAPTER_CLIENT:
1795 CALEAdapterGattClientStop();
1797 case ADAPTER_BOTH_CLIENT_SERVER:
1798 CALEAdapterGattServerStop();
1799 CALEAdapterGattClientStop();
1804 ca_mutex_unlock(g_bleIsServerMutex);
1806 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1808 return CAStopLEAdapter();
1811 static void CATerminateLE()
1813 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1815 CASetLEReqRespServerCallback(NULL);
1816 CASetLEReqRespClientCallback(NULL);
1817 CALERegisterNetworkNotifications(NULL);
1818 CASetLEReqRespAdapterCallback(NULL);
1819 CATerminateLENetworkMonitor();
1821 ca_mutex_lock(g_bleIsServerMutex);
1822 switch (g_adapterType)
1824 case ADAPTER_SERVER:
1825 CATerminateLEGattServer();
1827 case ADAPTER_CLIENT:
1828 CATerminateLEGattClient();
1830 case ADAPTER_BOTH_CLIENT_SERVER:
1831 CATerminateLEGattServer();
1832 CATerminateLEGattClient();
1837 g_adapterType = ADAPTER_EMPTY;
1838 ca_mutex_unlock(g_bleIsServerMutex);
1840 #ifndef SINGLE_THREAD
1841 CATerminateLEQueues();
1843 CATerminateLEAdapterMutex();
1845 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1848 static CAResult_t CAStartLEListeningServer()
1850 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEListeningServer");
1851 #ifndef ROUTING_GATEWAY
1852 CAResult_t result = CA_STATUS_OK;
1853 #ifndef SINGLE_THREAD
1854 result = CAInitLEServerQueues();
1855 if (CA_STATUS_OK != result)
1857 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1862 ca_mutex_lock(g_bleIsServerMutex);
1863 switch (g_adapterType)
1865 case ADAPTER_CLIENT:
1866 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1868 case ADAPTER_BOTH_CLIENT_SERVER:
1871 g_adapterType = ADAPTER_SERVER;
1873 ca_mutex_unlock(g_bleIsServerMutex);
1875 result = CAGetLEAdapterState();
1876 if (CA_STATUS_OK != result)
1878 if (CA_ADAPTER_NOT_ENABLED == result)
1882 "Listen Server will be started once BT Adapter is enabled");
1887 result = CALEAdapterGattServerStart();
1890 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1893 // Routing Gateway only supports BLE client mode.
1894 OIC_LOG(ERROR, CALEADAPTER_TAG, "LE server not supported in Routing Gateway");
1895 return CA_NOT_SUPPORTED;
1899 static CAResult_t CAStopLEListeningServer()
1901 OIC_LOG(ERROR, CALEADAPTER_TAG, "Listen server stop not supported.");
1902 return CA_NOT_SUPPORTED;
1905 static CAResult_t CAStartLEDiscoveryServer()
1907 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEDiscoveryServer");
1908 CAResult_t result = CA_STATUS_OK;
1909 #ifndef SINGLE_THREAD
1910 result = CAInitLEClientQueues();
1911 if (CA_STATUS_OK != result)
1913 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1918 ca_mutex_lock(g_bleIsServerMutex);
1919 switch (g_adapterType)
1921 case ADAPTER_SERVER:
1922 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1924 case ADAPTER_BOTH_CLIENT_SERVER:
1927 g_adapterType = ADAPTER_CLIENT;
1929 ca_mutex_unlock(g_bleIsServerMutex);
1931 result = CAGetLEAdapterState();
1932 if (CA_STATUS_OK != result)
1934 if (CA_ADAPTER_NOT_ENABLED == result)
1938 "Discovery Server will be started once BT Adapter is enabled");
1943 result = CALEAdapterGattClientStart();
1946 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1950 static CAResult_t CAReadLEData()
1952 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1953 #ifdef SINGLE_THREAD
1956 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1957 return CA_STATUS_OK;
1960 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1964 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEUnicastData");
1967 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1968 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1970 CAResult_t result = CA_STATUS_FAILED;
1972 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
1973 if (ADAPTER_EMPTY == g_adapterType)
1975 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
1978 ca_mutex_lock(g_bleIsServerMutex);
1979 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
1981 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1982 if (CA_STATUS_OK != result)
1984 ca_mutex_unlock(g_bleIsServerMutex);
1985 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed");
1988 g_errorHandler(endpoint, data, dataLen, result);
1995 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
1997 result = CALEAdapterClientSendData(endpoint, data, dataLen);
1998 if (CA_STATUS_OK != result)
2000 ca_mutex_unlock(g_bleIsServerMutex);
2001 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for client failed" );
2005 g_errorHandler(endpoint, data, dataLen, result);
2010 ca_mutex_unlock(g_bleIsServerMutex);
2012 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2016 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
2020 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEMulticastData");
2023 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
2027 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
2031 CAResult_t result = CA_STATUS_FAILED;
2033 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
2034 if (ADAPTER_EMPTY == g_adapterType)
2036 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2039 ca_mutex_lock(g_bleIsServerMutex);
2040 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2042 result = CALEAdapterServerSendData(NULL, data, dataLen);
2043 if (CA_STATUS_OK != result)
2045 ca_mutex_unlock(g_bleIsServerMutex);
2047 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data for server failed" );
2051 g_errorHandler(endpoint, data, dataLen, result);
2057 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2059 result = CALEAdapterClientSendData(NULL, data, dataLen);
2060 if (CA_STATUS_OK != result)
2062 ca_mutex_unlock(g_bleIsServerMutex);
2064 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data for client failed" );
2068 g_errorHandler(endpoint, data, dataLen, result);
2073 ca_mutex_unlock(g_bleIsServerMutex);
2075 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CASendLEMulticastData");
2079 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
2081 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2083 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
2085 char *local_address = NULL;
2087 CAResult_t res = CAGetLEAddress(&local_address);
2088 if (CA_STATUS_OK != res)
2090 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
2094 if (NULL == local_address)
2096 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
2097 return CA_STATUS_FAILED;
2101 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
2102 if (NULL == (*info))
2104 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
2105 OICFree(local_address);
2106 return CA_STATUS_FAILED;
2109 size_t local_address_len = strlen(local_address);
2111 if(local_address_len >= sizeof(g_localBLEAddress) ||
2112 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
2114 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
2116 OICFree(local_address);
2117 return CA_STATUS_FAILED;
2120 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
2121 ca_mutex_lock(g_bleLocalAddressMutex);
2122 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
2123 ca_mutex_unlock(g_bleLocalAddressMutex);
2125 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
2127 OICFree(local_address);
2129 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2130 return CA_STATUS_OK;
2133 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
2135 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2137 ca_mutex_lock(g_bleNetworkCbMutex);
2138 g_networkCallback = netCallback;
2139 ca_mutex_unlock(g_bleNetworkCbMutex);
2140 CAResult_t res = CA_STATUS_OK;
2143 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
2144 if (CA_STATUS_OK != res)
2146 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2149 res = CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCb);
2150 if (CA_STATUS_OK != res)
2152 OIC_LOG(ERROR, CALEADAPTER_TAG, "CALEConnectionStateChangedCb failed!");
2157 res = CAUnSetLEAdapterStateChangedCb();
2158 if (CA_STATUS_OK != res)
2160 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2164 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2168 static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
2171 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEConnectionStateChangedCb");
2173 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2177 ca_mutex_lock(g_bleIsServerMutex);
2178 switch (g_adapterType)
2180 case ADAPTER_SERVER:
2181 CALEGattServerConnectionStateChanged(isConnected, address);
2183 case ADAPTER_CLIENT:
2184 CALEGattConnectionStateChanged(isConnected, address);
2186 case ADAPTER_BOTH_CLIENT_SERVER:
2187 CALEGattConnectionStateChanged(isConnected, address);
2188 CALEGattServerConnectionStateChanged(isConnected, address);
2193 ca_mutex_unlock(g_bleIsServerMutex);
2198 #ifndef SINGLE_THREAD
2199 if(g_bleClientSenderInfo)
2201 CABLESenderInfo_t *senderInfo = NULL;
2202 uint32_t senderIndex = 0;
2204 if(CA_STATUS_OK == CALEGetSenderInfo(address, g_bleClientSenderInfo, &senderInfo,
2207 u_arraylist_remove(g_bleClientSenderInfo, senderIndex);
2208 OICFree(senderInfo->defragData);
2209 OICFree(senderInfo->remoteEndpoint);
2210 OICFree(senderInfo);
2212 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo is removed for disconnection");
2216 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo doesn't exist");
2220 if(g_bleServerSenderInfo)
2222 CABLESenderInfo_t *senderInfo = NULL;
2223 uint32_t senderIndex = 0;
2225 if(CA_STATUS_OK == CALEGetSenderInfo(address, g_bleServerSenderInfo, &senderInfo,
2228 u_arraylist_remove(g_bleServerSenderInfo, senderIndex);
2229 OICFree(senderInfo->defragData);
2230 OICFree(senderInfo->remoteEndpoint);
2231 OICFree(senderInfo);
2233 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo is removed for disconnection");
2237 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo doesn't exist");
2243 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2246 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
2248 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDeviceStateChangedCb");
2250 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
2251 CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE };
2253 ca_mutex_lock(g_bleLocalAddressMutex);
2254 OICStrcpy(localEndpoint.addr,
2255 sizeof(localEndpoint.addr),
2257 ca_mutex_unlock(g_bleLocalAddressMutex);
2259 if (CA_ADAPTER_ENABLED == adapter_state)
2261 ca_mutex_lock(g_bleIsServerMutex);
2262 switch (g_adapterType)
2264 case ADAPTER_SERVER:
2265 CALEAdapterGattServerStart();
2267 case ADAPTER_CLIENT:
2268 CALEAdapterGattClientStart();
2270 case ADAPTER_BOTH_CLIENT_SERVER:
2271 CALEAdapterGattServerStart();
2272 CALEAdapterGattClientStart();
2277 ca_mutex_unlock(g_bleIsServerMutex);
2281 ca_mutex_lock(g_bleIsServerMutex);
2282 switch (g_adapterType)
2284 case ADAPTER_SERVER:
2285 CALEAdapterGattServerStop();
2287 case ADAPTER_CLIENT:
2288 CALEAdapterGattClientStop();
2290 case ADAPTER_BOTH_CLIENT_SERVER:
2291 CALEAdapterGattServerStop();
2292 CALEAdapterGattClientStop();
2297 ca_mutex_unlock(g_bleIsServerMutex);
2300 ca_mutex_lock(g_bleNetworkCbMutex);
2301 if (NULL != g_networkCallback)
2303 g_networkCallback(&localEndpoint, adapter_state);
2307 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
2309 ca_mutex_unlock(g_bleNetworkCbMutex);
2311 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2314 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
2315 const uint8_t *data,
2318 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2320 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2321 #ifndef SINGLE_THREAD
2322 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2323 "g_bleClientSendQueueHandle is NULL",
2325 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
2326 "g_bleClientSendDataMutex is NULL",
2329 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2330 "g_bleClientSendQueueHandle",
2333 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
2335 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2338 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2339 return CA_MEMORY_ALLOC_FAILED;
2341 // Add message to send queue
2342 ca_mutex_lock(g_bleClientSendDataMutex);
2343 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
2344 ca_mutex_unlock(g_bleClientSendDataMutex);
2346 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2347 return CA_STATUS_OK;
2350 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2351 const uint8_t *data,
2354 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2356 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2358 #ifdef SINGLE_THREAD
2359 if (!CAIsLEConnected())
2361 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2362 return CA_STATUS_FAILED;
2365 CAResult_t result = CA_STATUS_OK;
2366 const uint32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2367 for (uint32_t iter = 0; iter < dataLimit; iter++)
2370 CAUpdateCharacteristicsToAllGattClients(
2371 data + (iter * CA_SUPPORTED_BLE_MTU_SIZE),
2372 CA_SUPPORTED_BLE_MTU_SIZE);
2374 if (CA_STATUS_OK != result)
2376 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2377 return CA_STATUS_FAILED;
2383 const uint32_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2387 CAUpdateCharacteristicsToAllGattClients(
2388 data + (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE),
2390 if (CA_STATUS_OK != result)
2392 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2393 return CA_STATUS_FAILED;
2398 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2399 "BleClientReceiverQueue is NULL",
2401 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2402 "BleClientSendDataMutex is NULL",
2405 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2408 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2410 CALEData_t * const bleData =
2411 CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2415 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2416 return CA_MEMORY_ALLOC_FAILED;
2419 // Add message to send queue
2420 ca_mutex_lock(g_bleServerSendDataMutex);
2421 CAQueueingThreadAddData(g_bleServerSendQueueHandle,
2423 sizeof(CALEData_t));
2424 ca_mutex_unlock(g_bleServerSendDataMutex);
2426 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2427 return CA_STATUS_OK;
2430 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2431 const uint8_t *data,
2432 uint32_t dataLength,
2433 uint32_t *sentLength)
2435 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2438 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2439 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2441 #ifdef SINGLE_THREAD
2442 if(g_networkPacketReceivedCallback)
2444 // will be filled by upper layer
2445 const CASecureEndpoint_t endpoint =
2446 { .endpoint = { .adapter = CA_ADAPTER_GATT_BTLE } };
2449 g_networkPacketReceivedCallback(&endpoint, data, dataLength);
2452 VERIFY_NON_NULL_RET(g_bleReceiverQueue,
2454 "g_bleReceiverQueue",
2457 //Add message to data queue
2458 CAEndpoint_t * const remoteEndpoint =
2459 CACreateEndpointObject(CA_DEFAULT_FLAGS,
2460 CA_ADAPTER_GATT_BTLE,
2464 if (NULL == remoteEndpoint)
2466 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2467 return CA_STATUS_FAILED;
2470 // Create bleData to add to queue
2473 "Data received from LE layer [%d]",
2476 CALEData_t * const bleData =
2477 CACreateLEData(remoteEndpoint, data, dataLength, g_bleServerSenderInfo);
2481 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2482 CAFreeEndpoint(remoteEndpoint);
2483 return CA_MEMORY_ALLOC_FAILED;
2486 CAFreeEndpoint(remoteEndpoint);
2487 // Add message to receiver queue
2488 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2490 *sentLength = dataLength;
2492 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2493 return CA_STATUS_OK;
2496 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2497 const uint8_t *data,
2498 uint32_t dataLength,
2499 uint32_t *sentLength)
2501 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2504 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2505 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2506 #ifndef SINGLE_THREAD
2507 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG,
2508 "g_bleReceiverQueue",
2511 //Add message to data queue
2512 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2513 CA_ADAPTER_GATT_BTLE,
2515 if (NULL == remoteEndpoint)
2517 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2518 return CA_STATUS_FAILED;
2521 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%u]", dataLength);
2523 // Create bleData to add to queue
2524 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data,
2525 dataLength, g_bleClientSenderInfo);
2528 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2529 CAFreeEndpoint(remoteEndpoint);
2530 return CA_MEMORY_ALLOC_FAILED;
2533 CAFreeEndpoint(remoteEndpoint);
2534 // Add message to receiver queue
2535 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2537 *sentLength = dataLength;
2539 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2540 return CA_STATUS_OK;
2543 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2545 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2547 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2548 g_bleAdapterThreadPool = handle;
2549 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2551 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2554 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2556 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2558 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2560 g_networkPacketReceivedCallback = callback;
2562 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2564 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2567 static void CALEErrorHandler(const char *remoteAddress,
2568 const uint8_t *data,
2572 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2574 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2576 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2577 CA_ADAPTER_GATT_BTLE,
2581 // if required, will be used to build remote endpoint
2582 g_errorHandler(rep, data, dataLen, result);
2584 CAFreeEndpoint(rep);
2586 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");