1 /* ****************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
20 #include "caleadapter.h"
25 #include "caleinterface.h"
28 #include "caadapterutils.h"
30 #include "caqueueingthread.h"
32 #include "cafragmentation.h"
33 #include "oic_malloc.h"
34 #include "oic_string.h"
35 #include "caremotehandler.h"
38 * Logging tag for module name.
40 #define CALEADAPTER_TAG "OIC_LE_ADAP"
43 * Stores information of all the senders.
45 * This structure will be used to track and defragment all incoming
51 uint32_t totalDataLen;
53 CAEndpoint_t *remoteEndpoint;
59 ADAPTER_BOTH_CLIENT_SERVER,
65 * Callback to provide the status of the network change to CA layer.
67 static CANetworkChangeCallback g_networkCallback = NULL;
70 * bleAddress of the local adapter. Value will be initialized to zero,
71 * and will be updated later.
73 static char g_localBLEAddress[18] = { 0 };
76 * Variable to differentiate btw GattServer and GattClient.
78 static CABLEAdapter_t g_adapterType = ADAPTER_EMPTY;
81 * Mutex to synchronize the task to be executed on the GattServer
84 static ca_mutex g_bleIsServerMutex = NULL;
87 * Mutex to synchronize the callback to be called for the network
90 static ca_mutex g_bleNetworkCbMutex = NULL;
93 * Mutex to synchronize the updates of the local LE address of the
96 static ca_mutex g_bleLocalAddressMutex = NULL;
99 * Reference to thread pool.
101 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
104 * Mutex to synchronize the task to be pushed to thread pool.
106 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
109 * Mutex to synchronize the queing of the data from SenderQueue.
111 static ca_mutex g_bleClientSendDataMutex = NULL;
114 * Mutex to synchronize the queing of the data from ReceiverQueue.
116 static ca_mutex g_bleReceiveDataMutex = NULL;
119 * Mutex to synchronize the queing of the data from SenderQueue.
121 static ca_mutex g_bleServerSendDataMutex = NULL;
124 * Mutex to synchronize the callback to be called for the
125 * adapterReqResponse.
127 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
130 * Callback to be called when network packet received from either
131 * GattServer or GattClient.
133 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
136 * Callback to notify error from the BLE adapter.
138 static CAErrorHandleCallback g_errorHandler = NULL;
141 * Register network change notification callback.
143 * @param[in] netCallback CANetworkChangeCallback callback which will
144 * be set for the change in network.
146 * @return 0 on success otherwise a positive error value.
147 * @retval ::CA_STATUS_OK Successful.
148 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
149 * @retval ::CA_STATUS_FAILED Operation failed.
152 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
155 * Set the thread pool handle which is required for spawning new
158 * @param[in] handle Thread pool handle which is given by above layer
159 * for using thread creation task.
162 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
165 * Call the callback to the upper layer when the device state gets
168 * @param[in] adapter_state New state of the adapter to be notified to
171 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state);
174 * Used to initialize all required mutex variable for LE Adapter
177 * @return 0 on success otherwise a positive error value.
178 * @retval ::CA_STATUS_OK Successful.
179 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
180 * @retval ::CA_STATUS_FAILED Operation failed.
183 static CAResult_t CAInitLEAdapterMutex();
186 * Terminate all required mutex variables for LE adapter
189 static void CATerminateLEAdapterMutex();
192 * Prepares and notify error through error callback.
194 static void CALEErrorHandler(const char *remoteAddress,
199 #ifndef SINGLE_THREAD
201 * Stop condition of recvhandler.
203 static bool g_dataBleReceiverHandlerState = false;
206 * Sender information.
208 static u_arraylist_t *g_bleServerSenderInfo = NULL;
210 static u_arraylist_t *g_bleClientSenderInfo = NULL;
213 * Queue to process the outgoing packets from GATTClient.
215 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
218 * Queue to process the incoming packets.
220 static CAQueueingThread_t *g_bleReceiverQueue = NULL;
223 * Queue to process the outgoing packets from GATTServer.
225 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
228 * This function will be associated with the sender queue for
231 * This function will fragment the data to the MTU of the transport
232 * and send the data in fragments to the adapters. The function will
233 * be blocked until all data is sent out from the adapter.
235 * @param[in] threadData Data pushed to the queue which contains the
236 * info about RemoteEndpoint and Data.
238 static void CALEServerSendDataThread(void *threadData);
241 * This function will be associated with the sender queue for
244 * This function will fragment the data to the MTU of the transport
245 * and send the data in fragments to the adapters. The function will
246 * be blocked until all data is sent out from the adapter.
248 * @param[in] threadData Data pushed to the queue which contains the
249 * info about RemoteEndpoint and Data.
251 static void CALEClientSendDataThread(void *threadData);
254 * This function will be associated with the receiver queue.
256 * This function will defragment the received data from each sender
257 * respectively and will send it up to CA layer. Respective sender's
258 * header will provide the length of the data sent.
260 * @param[in] threadData Data pushed to the queue which contains the
261 * info about RemoteEndpoint and Data.
263 static void CALEDataReceiverHandler(void *threadData);
266 * This function will stop all queues created for GattServer and
267 * GattClient. All four queues will be be stopped with this function
270 static void CAStopLEQueues();
273 * This function will terminate all queues created for GattServer and
274 * GattClient. All four queues will be be terminated with this
275 * function invocations.
277 static void CATerminateLEQueues();
280 * This function will initalize the Receiver and Sender queues for
281 * GattServer. This function will in turn call the functions
282 * CAInitBleServerReceiverQueue() and CAInitBleServerSenderQueue() to
283 * initialize the queues.
285 * @return ::CA_STATUS_OK or Appropriate error code.
286 * @retval ::CA_STATUS_OK Successful.
287 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
288 * @retval ::CA_STATUS_FAILED Operation failed.
290 static CAResult_t CAInitLEServerQueues();
293 * This function will initalize the Receiver and Sender queues for
294 * GattClient. This function will inturn call the functions
295 * CAInitBleClientReceiverQueue() and CAInitBleClientSenderQueue() to
296 * initialize the queues.
298 * @return ::CA_STATUS_OK or Appropriate error code.
299 * @retval ::CA_STATUS_OK Successful.
300 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
301 * @retval ::CA_STATUS_FAILED Operation failed.
304 static CAResult_t CAInitLEClientQueues();
307 * This function will initalize the Receiver queue for
308 * GattServer. This will initialize the queue to process the function
309 * CABLEServerSendDataThread() when ever the task is added to this
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 CAInitLEServerSenderQueue();
320 * This function will initalize the Receiver queue for
321 * GattClient. This will initialize the queue to process the function
322 * CABLEClientSendDataThread() when ever the task is added to this
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.
330 static CAResult_t CAInitLEClientSenderQueue();
333 * This function will initialize the Receiver queue for
334 * LEAdapter. This will initialize the queue to process the function
335 * CABLEDataReceiverHandler() when ever the task is added to this
338 * @return ::CA_STATUS_OK or Appropriate error code
339 * @retval ::CA_STATUS_OK Successful
340 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
341 * @retval ::CA_STATUS_FAILED Operation failed
344 static CAResult_t CAInitLEReceiverQueue();
347 * This function will create the Data required to send it in the
350 * @param[in] remoteEndpoint Remote endpoint information of the
352 * @param[in] data Data to be transmitted from LE.
353 * @param[in] dataLength Length of the Data being transmitted.
355 * @return ::CA_STATUS_OK or Appropriate error code.
356 * @retval ::CA_STATUS_OK Successful.
357 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
358 * @retval ::CA_STATUS_FAILED Operation failed.
360 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
363 u_arraylist_t *senderInfo);
366 * Used to free the BLE information stored in the sender/receiver
369 * @param[in] bleData Information for a particular data segment.
371 static void CAFreeLEData(CALEData_t *bleData);
376 static void CALEDataDestroyer(void *data, uint32_t size);
378 static CAResult_t CAInitLEServerQueues()
380 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
382 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
384 CAResult_t result = CAInitLEServerSenderQueue();
385 if (CA_STATUS_OK != result)
387 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
388 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
389 return CA_STATUS_FAILED;
392 g_bleServerSenderInfo = u_arraylist_create();
393 if (!g_bleServerSenderInfo)
395 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
396 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
397 return CA_MEMORY_ALLOC_FAILED;
400 result = CAInitLEReceiverQueue();
401 if (CA_STATUS_OK != result)
403 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
404 u_arraylist_free(&g_bleServerSenderInfo);
405 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
406 return CA_STATUS_FAILED;
409 g_dataBleReceiverHandlerState = true;
411 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
413 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
417 static CAResult_t CAInitLEClientQueues()
419 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
421 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
423 CAResult_t result = CAInitLEClientSenderQueue();
424 if (CA_STATUS_OK != result)
426 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
427 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
428 return CA_STATUS_FAILED;
431 g_bleClientSenderInfo = u_arraylist_create();
432 if (!g_bleClientSenderInfo)
434 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
435 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
436 return CA_MEMORY_ALLOC_FAILED;
439 result = CAInitLEReceiverQueue();
440 if (CA_STATUS_OK != result)
442 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
443 u_arraylist_free(&g_bleClientSenderInfo);
444 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
445 return CA_STATUS_FAILED;
448 g_dataBleReceiverHandlerState = true;
450 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
452 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
456 static CAResult_t CAInitLEReceiverQueue()
458 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEReceiverQueue");
459 // Check if the message queue is already initialized
460 if (g_bleReceiverQueue)
462 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
466 // Create recv message queue
467 g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
468 if (!g_bleReceiverQueue)
470 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
471 return CA_MEMORY_ALLOC_FAILED;
474 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue,
475 g_bleAdapterThreadPool,
476 CALEDataReceiverHandler,
479 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
480 OICFree(g_bleReceiverQueue);
481 g_bleReceiverQueue = NULL;
482 return CA_STATUS_FAILED;
485 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue))
487 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
488 OICFree(g_bleReceiverQueue);
489 g_bleReceiverQueue = NULL;
490 return CA_STATUS_FAILED;
493 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
497 static CAResult_t CAInitLEServerSenderQueue()
499 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEServerSenderQueue");
500 // Check if the message queue is already initialized
501 if (g_bleServerSendQueueHandle)
503 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
507 // Create send message queue
508 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
509 if (!g_bleServerSendQueueHandle)
511 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
512 return CA_MEMORY_ALLOC_FAILED;
515 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
516 g_bleAdapterThreadPool,
517 CALEServerSendDataThread,
520 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
521 OICFree(g_bleServerSendQueueHandle);
522 g_bleServerSendQueueHandle = NULL;
523 return CA_STATUS_FAILED;
526 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
530 static void CALEClearSenderInfoImpl(u_arraylist_t ** list)
532 const size_t length = u_arraylist_length(*list);
533 for (size_t i = 0; i < length; ++i)
535 CABLESenderInfo_t * const info =
536 (CABLESenderInfo_t *) u_arraylist_get(*list, i);
539 OICFree(info->defragData);
540 CAFreeEndpoint(info->remoteEndpoint);
544 u_arraylist_free(list);
547 static void CALEClearSenderInfo()
549 CALEClearSenderInfoImpl(&g_bleServerSenderInfo);
550 CALEClearSenderInfoImpl(&g_bleClientSenderInfo);
553 static CAResult_t CAInitLEClientSenderQueue()
555 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEClientSenderQueue");
557 if (g_bleClientSendQueueHandle)
559 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
563 // Create send message queue
564 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
565 if (!g_bleClientSendQueueHandle)
567 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
568 return CA_MEMORY_ALLOC_FAILED;
571 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
572 g_bleAdapterThreadPool,
573 CALEClientSendDataThread, CALEDataDestroyer))
575 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
576 OICFree(g_bleClientSendQueueHandle);
577 g_bleClientSendQueueHandle = NULL;
578 return CA_STATUS_FAILED;
581 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAInitLEClientSenderQueue");
585 static void CAStopLEQueues()
587 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStopLEQueues");
589 ca_mutex_lock(g_bleReceiveDataMutex);
590 if (NULL != g_bleReceiverQueue)
592 CAQueueingThreadStop(g_bleReceiverQueue);
594 ca_mutex_unlock(g_bleReceiveDataMutex);
596 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAStopLEQueues");
599 static void CATerminateLEQueues()
601 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
603 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
604 OICFree(g_bleClientSendQueueHandle);
605 g_bleClientSendQueueHandle = NULL;
607 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
608 OICFree(g_bleServerSendQueueHandle);
609 g_bleServerSendQueueHandle = NULL;
611 CAQueueingThreadDestroy(g_bleReceiverQueue);
612 OICFree(g_bleReceiverQueue);
613 g_bleReceiverQueue = NULL;
615 CALEClearSenderInfo();
617 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
620 static CAResult_t CALEGetSenderInfo(const char *leAddress,
621 u_arraylist_t *senderInfoList,
622 CABLESenderInfo_t **senderInfo,
623 uint32_t *senderIndex)
625 VERIFY_NON_NULL_RET(leAddress,
627 "NULL BLE address argument",
628 CA_STATUS_INVALID_PARAM);
629 VERIFY_NON_NULL_RET(senderIndex,
631 "NULL index argument",
632 CA_STATUS_INVALID_PARAM);
634 const uint32_t listLength = u_arraylist_length(senderInfoList);
635 const uint32_t addrLength = strlen(leAddress);
636 for (uint32_t index = 0; index < listLength; index++)
638 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(senderInfoList, index);
639 if(!info || !(info->remoteEndpoint))
644 if(!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
646 *senderIndex = index;
655 return CA_STATUS_FAILED;
658 static void CALEDataReceiverHandler(void *threadData)
660 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDataReceiverHandler");
662 ca_mutex_lock(g_bleReceiveDataMutex);
664 if (g_dataBleReceiverHandlerState)
666 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
668 CALEData_t *bleData = (CALEData_t *) threadData;
671 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
672 ca_mutex_unlock(g_bleReceiveDataMutex);
676 if (!(bleData->senderInfo))
678 OIC_LOG(ERROR, CALEADAPTER_TAG, "sender info is not available");
679 ca_mutex_unlock(g_bleReceiveDataMutex);
683 if (!(bleData->remoteEndpoint))
685 OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!");
686 ca_mutex_unlock(g_bleReceiveDataMutex);
690 CABLESenderInfo_t *senderInfo = NULL;
691 uint32_t senderIndex = 0;
693 if(CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
695 &senderInfo, &senderIndex))
697 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s]",
698 bleData->remoteEndpoint->addr);
703 CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
706 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
707 ca_mutex_unlock(g_bleReceiveDataMutex);
710 newSender->recvDataLen = 0;
711 newSender->totalDataLen = 0;
712 newSender->defragData = NULL;
713 newSender->remoteEndpoint = NULL;
715 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
716 newSender->totalDataLen = CAParseHeader(bleData->data,
718 if(!(newSender->totalDataLen))
720 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
722 ca_mutex_unlock(g_bleReceiveDataMutex);
726 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
727 newSender->totalDataLen);
728 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
731 newSender->defragData = OICCalloc(newSender->totalDataLen + 1,
732 sizeof(*newSender->defragData));
734 if (NULL == newSender->defragData)
736 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
738 ca_mutex_unlock(g_bleReceiveDataMutex);
742 const char *remoteAddress = bleData->remoteEndpoint->addr;
743 newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
744 CA_ADAPTER_GATT_BTLE,
747 if (NULL == newSender->remoteEndpoint)
749 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
750 OICFree(newSender->defragData);
752 ca_mutex_unlock(g_bleReceiveDataMutex);
755 memcpy(newSender->defragData, bleData->data + CA_HEADER_LENGTH,
756 bleData->dataLen - CA_HEADER_LENGTH);
757 newSender->recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
758 u_arraylist_add(bleData->senderInfo, (void *)newSender);
760 //Getting newSender index position in bleSenderInfo array list
762 CALEGetSenderInfo(newSender->remoteEndpoint->addr, bleData->senderInfo,
765 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
766 OICFree(newSender->defragData);
767 CAFreeEndpoint(newSender->remoteEndpoint);
769 ca_mutex_unlock(g_bleReceiveDataMutex);
772 senderInfo = newSender;
776 if(senderInfo->recvDataLen + bleData->dataLen > senderInfo->totalDataLen)
778 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
779 "Data Length exceeding error!! Receiving [%d] total length [%d]",
780 senderInfo->recvDataLen + bleData->dataLen, senderInfo->totalDataLen);
781 u_arraylist_remove(bleData->senderInfo, senderIndex);
782 OICFree(senderInfo->defragData);
784 ca_mutex_unlock(g_bleReceiveDataMutex);
787 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]",
789 memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data,
791 senderInfo->recvDataLen += bleData->dataLen ;
792 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] received Datalen [%d]",
793 senderInfo->totalDataLen, senderInfo->recvDataLen);
796 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
798 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
799 if (NULL == g_networkPacketReceivedCallback)
801 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
803 u_arraylist_remove(bleData->senderInfo, senderIndex);
804 OICFree(senderInfo->defragData);
806 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
807 ca_mutex_unlock(g_bleReceiveDataMutex);
811 OIC_LOG(DEBUG, CALEADAPTER_TAG, "[CALEDataReceiverHandler] Sending data up !");
813 const CASecureEndpoint_t tmp =
815 .endpoint = *senderInfo->remoteEndpoint
818 g_networkPacketReceivedCallback(&tmp,
819 senderInfo->defragData,
820 senderInfo->recvDataLen);
821 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
822 u_arraylist_remove(bleData->senderInfo, senderIndex);
823 senderInfo->remoteEndpoint = NULL;
824 senderInfo->defragData = NULL;
828 ca_mutex_unlock(g_bleReceiveDataMutex);
829 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
832 static void CALEServerSendDataThread(void *threadData)
834 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEServerSendDataThread");
836 CALEData_t * const bleData = (CALEData_t *) threadData;
839 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
843 uint8_t * const header = OICCalloc(CA_HEADER_LENGTH, 1);
844 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
846 const uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
850 "Server total Data length with header is [%u]",
853 uint8_t * const dataSegment = OICCalloc(totalLength, 1);
855 if (NULL == dataSegment)
857 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
862 CAResult_t result = CAGenerateHeader(header,
865 if (CA_STATUS_OK != result )
867 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
869 OICFree(dataSegment);
873 memcpy(dataSegment, header, CA_HEADER_LENGTH);
877 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
879 length = totalLength;
880 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
884 length = CA_SUPPORTED_BLE_MTU_SIZE;
885 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
886 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
889 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
892 // Send the first segment with the header.
893 if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
895 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
897 result = CAUpdateCharacteristicsToGattClient(
898 bleData->remoteEndpoint->addr, dataSegment, length);
900 if (CA_STATUS_OK != result)
904 "Update characteristics failed, result [%d]",
907 g_errorHandler(bleData->remoteEndpoint,
911 OICFree(dataSegment);
917 "Server Sent data length [%u]",
919 for (index = 1; index < iter; index++)
921 // Send the remaining header.
924 "Sending the chunk number [%u]",
928 CAUpdateCharacteristicsToGattClient(
929 bleData->remoteEndpoint->addr,
930 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
931 CA_SUPPORTED_BLE_MTU_SIZE);
932 if (CA_STATUS_OK != result)
934 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
935 "Update characteristics failed, result [%d]", result);
936 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
937 OICFree(dataSegment);
940 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
941 CA_SUPPORTED_BLE_MTU_SIZE);
944 const uint32_t remainingLen =
945 totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
947 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
949 // send the last segment of the data (Ex: 22 bytes of 622
950 // bytes of data when MTU is 200)
951 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
952 result = CAUpdateCharacteristicsToGattClient(
953 bleData->remoteEndpoint->addr,
954 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
956 if (CA_STATUS_OK != result)
960 "Update characteristics failed, result [%d]",
962 g_errorHandler(bleData->remoteEndpoint,
966 OICFree(dataSegment);
969 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
974 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
975 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
976 if (CA_STATUS_OK != result)
978 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
980 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
981 OICFree(dataSegment);
984 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
985 for (index = 1; index < iter; index++)
987 // Send the remaining header.
988 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
989 result = CAUpdateCharacteristicsToAllGattClients(
990 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
991 CA_SUPPORTED_BLE_MTU_SIZE);
992 if (CA_STATUS_OK != result)
994 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
996 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
997 OICFree(dataSegment);
1000 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%u]",
1001 CA_SUPPORTED_BLE_MTU_SIZE);
1004 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1005 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1007 // send the last segment of the data
1008 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1009 result = CAUpdateCharacteristicsToAllGattClients(
1010 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1012 if (CA_STATUS_OK != result)
1014 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1016 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1017 OICFree(dataSegment);
1020 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1023 OICFree(dataSegment);
1025 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CALEServerSendDataThread");
1028 static void CALEClientSendDataThread(void *threadData)
1030 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEClientSendDataThread");
1032 CALEData_t *bleData = (CALEData_t *) threadData;
1035 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1039 uint8_t * const header = OICCalloc(CA_HEADER_LENGTH, 1);
1040 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1042 const uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1043 uint8_t *dataSegment = OICCalloc(totalLength, 1);
1044 if (NULL == dataSegment)
1046 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1051 CAResult_t result = CAGenerateHeader(header,
1054 if (CA_STATUS_OK != result )
1056 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1058 OICFree(dataSegment);
1061 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1064 uint32_t length = 0;
1065 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1067 length = totalLength;
1068 memcpy(dataSegment + CA_HEADER_LENGTH,
1074 length = CA_SUPPORTED_BLE_MTU_SIZE;
1075 memcpy(dataSegment + CA_HEADER_LENGTH,
1077 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
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) - CA_HEADER_LENGTH,
1119 CA_SUPPORTED_BLE_MTU_SIZE,
1121 if (CA_STATUS_OK != result)
1125 "Update characteristics failed, result [%d]",
1127 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1128 OICFree(dataSegment);
1131 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1132 CA_SUPPORTED_BLE_MTU_SIZE);
1135 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1136 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1138 // send the last segment of the data (Ex: 22 bytes of 622
1139 // bytes of data when MTU is 200)
1140 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1141 result = CAUpdateCharacteristicsToGattServer(
1142 bleData->remoteEndpoint->addr,
1143 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1147 if (CA_STATUS_OK != result)
1149 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1151 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1152 OICFree(dataSegment);
1155 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1160 //Sending Mulitcast Data
1161 // Send the first segment with the header.
1162 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1163 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1164 if (CA_STATUS_OK != result)
1166 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1167 "Update characteristics (all) failed, result [%d]", result);
1168 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1169 OICFree(dataSegment);
1172 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1173 // Send the remaining header.
1174 for (index = 1; index < iter; index++)
1176 result = CAUpdateCharacteristicsToAllGattServers(
1177 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1178 CA_SUPPORTED_BLE_MTU_SIZE);
1179 if (CA_STATUS_OK != result)
1181 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1183 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1184 OICFree(dataSegment);
1187 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1188 CA_SUPPORTED_BLE_MTU_SIZE);
1191 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1192 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1194 // send the last segment of the data (Ex: 22 bytes of 622
1195 // bytes of data when MTU is 200)
1196 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1198 CAUpdateCharacteristicsToAllGattServers(
1199 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1201 if (CA_STATUS_OK != result)
1203 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1204 "Update characteristics (all) failed, result [%d]", result);
1205 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1206 OICFree(dataSegment);
1209 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1214 OICFree(dataSegment);
1216 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1219 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
1220 const uint8_t *data,
1221 uint32_t dataLength,
1222 u_arraylist_t *senderInfo)
1224 CALEData_t * const bleData = OICMalloc(sizeof(CALEData_t));
1228 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1232 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1233 bleData->data = OICCalloc(dataLength + 1, 1);
1235 if (NULL == bleData->data)
1237 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1238 CAFreeLEData(bleData);
1242 memcpy(bleData->data, data, dataLength);
1243 bleData->dataLen = dataLength;
1246 bleData->senderInfo = senderInfo;
1252 static void CAFreeLEData(CALEData_t *bleData)
1254 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1256 CAFreeEndpoint(bleData->remoteEndpoint);
1257 OICFree(bleData->data);
1261 static void CALEDataDestroyer(void *data, uint32_t size)
1263 if ((size_t)size < sizeof(CALEData_t *))
1265 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1266 "Destroy data too small %p %d", data, size);
1268 CALEData_t *ledata = (CALEData_t *) data;
1270 CAFreeLEData(ledata);
1274 static CAResult_t CAInitLEAdapterMutex()
1276 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEAdapterMutex");
1278 if (NULL == g_bleIsServerMutex)
1280 g_bleIsServerMutex = ca_mutex_new();
1281 if (NULL == g_bleIsServerMutex)
1283 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1284 return CA_STATUS_FAILED;
1288 if (NULL == g_bleNetworkCbMutex)
1290 g_bleNetworkCbMutex = ca_mutex_new();
1291 if (NULL == g_bleNetworkCbMutex)
1293 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1294 CATerminateLEAdapterMutex();
1295 return CA_STATUS_FAILED;
1299 if (NULL == g_bleLocalAddressMutex)
1301 g_bleLocalAddressMutex = ca_mutex_new();
1302 if (NULL == g_bleLocalAddressMutex)
1304 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1305 CATerminateLEAdapterMutex();
1306 return CA_STATUS_FAILED;
1310 if (NULL == g_bleAdapterThreadPoolMutex)
1312 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1313 if (NULL == g_bleAdapterThreadPoolMutex)
1315 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1316 CATerminateLEAdapterMutex();
1317 return CA_STATUS_FAILED;
1321 if (NULL == g_bleClientSendDataMutex)
1323 g_bleClientSendDataMutex = ca_mutex_new();
1324 if (NULL == g_bleClientSendDataMutex)
1326 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1327 CATerminateLEAdapterMutex();
1328 return CA_STATUS_FAILED;
1332 if (NULL == g_bleServerSendDataMutex)
1334 g_bleServerSendDataMutex = ca_mutex_new();
1335 if (NULL == g_bleServerSendDataMutex)
1337 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1338 CATerminateLEAdapterMutex();
1339 return CA_STATUS_FAILED;
1343 if (NULL == g_bleAdapterReqRespCbMutex)
1345 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1346 if (NULL == g_bleAdapterReqRespCbMutex)
1348 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1349 CATerminateLEAdapterMutex();
1350 return CA_STATUS_FAILED;
1354 if (NULL == g_bleReceiveDataMutex)
1356 g_bleReceiveDataMutex = ca_mutex_new();
1357 if (NULL == g_bleReceiveDataMutex)
1359 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1360 return CA_STATUS_FAILED;
1364 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1365 return CA_STATUS_OK;
1368 static void CATerminateLEAdapterMutex()
1370 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CATerminateLEAdapterMutex");
1372 ca_mutex_free(g_bleIsServerMutex);
1373 g_bleIsServerMutex = NULL;
1375 ca_mutex_free(g_bleNetworkCbMutex);
1376 g_bleNetworkCbMutex = NULL;
1378 ca_mutex_free(g_bleLocalAddressMutex);
1379 g_bleLocalAddressMutex = NULL;
1381 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1382 g_bleAdapterThreadPoolMutex = NULL;
1384 ca_mutex_free(g_bleClientSendDataMutex);
1385 g_bleClientSendDataMutex = NULL;
1387 ca_mutex_free(g_bleServerSendDataMutex);
1388 g_bleServerSendDataMutex = NULL;
1390 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1391 g_bleAdapterReqRespCbMutex = NULL;
1393 ca_mutex_free(g_bleReceiveDataMutex);
1394 g_bleReceiveDataMutex = NULL;
1396 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1400 * Starting LE connectivity adapters.
1402 * As its peer to peer it does not require to start any servers.
1404 * @return ::CA_STATUS_OK or Appropriate error code.
1406 static CAResult_t CAStartLE();
1409 * Start listening server for receiving multicast search requests.
1411 * Transport Specific Behavior:
1412 * LE Starts GATT Server with prefixed UUID and Characteristics
1413 * per OIC Specification.
1414 * @return ::CA_STATUS_OK or Appropriate error code.
1416 static CAResult_t CAStartLEListeningServer();
1419 * Stops listening server from receiving multicast search requests.
1421 * Transport Specific Behavior:
1422 * LE Starts GATT Server with prefixed UUID and Characteristics
1423 * per OIC Specification.
1424 * @return ::CA_STATUS_OK or Appropriate error code.
1426 static CAResult_t CAStopLEListeningServer();
1429 * Sarting discovery of servers for receiving multicast
1432 * Transport Specific Behavior:
1433 * LE Starts GATT Server with prefixed UUID and Characteristics
1434 * per OIC Specification.
1436 * @return ::CA_STATUS_OK or Appropriate error code
1438 static CAResult_t CAStartLEDiscoveryServer();
1441 * Send data to the endpoint using the adapter connectivity.
1443 * @param[in] endpoint Remote Endpoint information (like MAC address,
1444 * reference URI and connectivity type) to which
1445 * the unicast data has to be sent.
1446 * @param[in] data Data which required to be sent.
1447 * @param[in] dataLen Size of data to be sent.
1449 * @note dataLen must be > 0.
1451 * @return The number of bytes sent on the network, or -1 on error.
1453 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1458 * Send multicast data to the endpoint using the LE connectivity.
1460 * @param[in] endpoint Remote Endpoint information to which the
1461 * multicast data has to be sent.
1462 * @param[in] data Data which required to be sent.
1463 * @param[in] dataLen Size of data to be sent.
1465 * @note dataLen must be > 0.
1467 * @return The number of bytes sent on the network, or -1 on error.
1469 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1474 * Get LE Connectivity network information.
1476 * @param[out] info Local connectivity information structures.
1477 * @param[out] size Number of local connectivity structures.
1479 * @return ::CA_STATUS_OK or Appropriate error code.
1481 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info,
1485 * Read Synchronous API callback.
1487 * @return ::CA_STATUS_OK or Appropriate error code.
1489 static CAResult_t CAReadLEData();
1492 * Stopping the adapters and close socket connections.
1494 * LE Stops all GATT servers and GATT Clients.
1496 * @return ::CA_STATUS_OK or Appropriate error code.
1498 static CAResult_t CAStopLE();
1501 * Terminate the LE connectivity adapter.
1503 * Configuration information will be deleted from further use.
1505 static void CATerminateLE();
1508 * This function will receive the data from the GattServer and add the
1509 * data to the Server receiver queue.
1511 * @param[in] remoteAddress Remote address of the device from where
1513 * @param[in] data Actual data recevied from the remote
1515 * @param[in] dataLength Length of the data received from the
1517 * @param[in] sentLength Length of the data sent from the remote
1520 * @return ::CA_STATUS_OK or Appropriate error code.
1521 * @retval ::CA_STATUS_OK Successful.
1522 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1523 * @retval ::CA_STATUS_FAILED Operation failed.
1526 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
1527 const uint8_t *data,
1528 uint32_t dataLength,
1529 uint32_t *sentLength);
1532 * This function will receive the data from the GattClient and add the
1533 * data into the Client receiver queue.
1535 * @param[in] remoteAddress Remote address of the device from where
1537 * @param[in] data Actual data recevied from the remote
1539 * @param[in] dataLength Length of the data received from the
1541 * @param[in] sentLength Length of the data sent from the remote
1544 * @return ::CA_STATUS_OK or Appropriate error code.
1545 * @retval ::CA_STATUS_OK Successful.
1546 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1547 * @retval ::CA_STATUS_FAILED Operation failed.
1549 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
1550 const uint8_t *data,
1551 uint32_t dataLength,
1552 uint32_t *sentLength);
1555 * Set the NetworkPacket received callback to CA layer from adapter
1558 * @param[in] callback Callback handle sent from the upper layer.
1560 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
1563 * Push the data from CA layer to the Sender processor queue.
1565 * @param[in] remoteEndpoint Remote endpoint information of the
1567 * @param[in] data Data to be transmitted from LE.
1568 * @param[in] dataLen Length of the Data being transmitted.
1570 * @return ::CA_STATUS_OK or Appropriate error code.
1571 * @retval ::CA_STATUS_OK Successful.
1572 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1573 * @retval ::CA_STATUS_FAILED Operation failed.
1575 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1576 const uint8_t *data,
1580 * Push the data from CA layer to the Sender processor queue.
1582 * @param[in] remoteEndpoint Remote endpoint information of the
1584 * @param[in] data Data to be transmitted from LE.
1585 * @param[in] dataLen Length of the Data being transmitted.
1587 * @return ::CA_STATUS_OK or Appropriate error code.
1588 * @retval ::CA_STATUS_OK Successful.
1589 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1590 * @retval ::CA_STATUS_FAILED Operation failed.
1592 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1593 const uint8_t *data,
1596 static CAResult_t CALEAdapterGattServerStart()
1598 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1600 CAResult_t result = CAStartLEGattServer();
1602 #ifndef SINGLE_THREAD
1604 Don't start the server side sending queue thread until the
1605 server itself has actually started.
1607 if (CA_STATUS_OK == result)
1609 ca_mutex_lock(g_bleServerSendDataMutex);
1610 result = CAQueueingThreadStart(g_bleServerSendQueueHandle);
1611 ca_mutex_unlock(g_bleServerSendDataMutex);
1613 if (CA_STATUS_OK != result)
1617 "Unable to start server queuing thread (%d)",
1626 static CAResult_t CALEAdapterGattServerStop()
1628 #ifndef SINGLE_THREAD
1629 ca_mutex_lock(g_bleServerSendDataMutex);
1630 CAResult_t result = CAQueueingThreadStop(g_bleServerSendQueueHandle);
1631 ca_mutex_unlock(g_bleServerSendDataMutex);
1632 if (CA_STATUS_OK == result)
1634 result = CAStopLEGattServer();
1639 return CAStopLEGattServer();
1643 static CAResult_t CALEAdapterGattClientStart()
1645 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattClient");
1647 CAResult_t result = CAStartLEGattClient();
1649 #ifndef SINGLE_THREAD
1651 Don't start the client side sending queue thread until the
1652 client itself has actually started.
1654 if (CA_STATUS_OK == result)
1656 ca_mutex_lock(g_bleClientSendDataMutex);
1657 result = CAQueueingThreadStart(g_bleClientSendQueueHandle);
1658 ca_mutex_unlock(g_bleClientSendDataMutex);
1660 if (CA_STATUS_OK != result)
1664 "Unable to start client queuing thread");
1672 static CAResult_t CALEAdapterGattClientStop()
1674 #ifndef SINGLE_THREAD
1675 ca_mutex_lock(g_bleClientSendDataMutex);
1676 CAResult_t result = CAQueueingThreadStop(g_bleClientSendQueueHandle);
1677 ca_mutex_unlock(g_bleClientSendDataMutex);
1678 if (CA_STATUS_OK == result)
1680 CAStopLEGattClient();
1685 CAStopLEGattClient();
1687 return CA_STATUS_OK;
1691 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1692 CANetworkPacketReceivedCallback reqRespCallback,
1693 CANetworkChangeCallback netCallback,
1694 CAErrorHandleCallback errorCallback,
1695 ca_thread_pool_t handle)
1697 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1700 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1701 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1702 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1704 CAResult_t result = CA_STATUS_OK;
1705 result = CAInitLEAdapterMutex();
1706 if (CA_STATUS_OK != result)
1708 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1709 return CA_STATUS_FAILED;
1712 result = CAInitializeLENetworkMonitor();
1713 if (CA_STATUS_OK != result)
1715 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1716 return CA_STATUS_FAILED;
1718 CAInitializeLEAdapter(handle);
1720 CASetLEClientThreadPoolHandle(handle);
1722 result = CAInitializeLEGattClient();
1723 if (CA_STATUS_OK != result)
1725 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattClient() failed");
1726 return CA_STATUS_FAILED;
1729 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1730 CASetLEServerThreadPoolHandle(handle);
1731 result = CAInitializeLEGattServer();
1732 if (CA_STATUS_OK != result)
1734 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattServer() failed");
1735 return CA_STATUS_FAILED;
1738 CASetLEAdapterThreadPoolHandle(handle);
1739 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1740 CASetLEReqRespAdapterCallback(reqRespCallback);
1742 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1743 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1744 CALERegisterNetworkNotifications(netCallback);
1746 g_errorHandler = errorCallback;
1748 static const CAConnectivityHandler_t connHandler =
1750 .startAdapter = CAStartLE,
1751 .stopAdapter = CAStopLE,
1752 .startListenServer = CAStartLEListeningServer,
1753 .stopListenServer = CAStopLEListeningServer,
1754 .startDiscoveryServer = CAStartLEDiscoveryServer,
1755 .sendData = CASendLEUnicastData,
1756 .sendDataToAll = CASendLEMulticastData,
1757 .GetnetInfo = CAGetLEInterfaceInformation,
1758 .readData = CAReadLEData,
1759 .terminate = CATerminateLE
1762 registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
1764 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1766 return CA_STATUS_OK;
1769 static CAResult_t CAStartLE()
1771 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE");
1773 return CAStartLEAdapter();
1776 static CAResult_t CAStopLE()
1778 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1779 #ifndef SINGLE_THREAD
1783 ca_mutex_lock(g_bleIsServerMutex);
1784 switch (g_adapterType)
1786 case ADAPTER_SERVER:
1787 CALEAdapterGattServerStop();
1789 case ADAPTER_CLIENT:
1790 CALEAdapterGattClientStop();
1792 case ADAPTER_BOTH_CLIENT_SERVER:
1793 CALEAdapterGattServerStop();
1794 CALEAdapterGattClientStop();
1799 ca_mutex_unlock(g_bleIsServerMutex);
1801 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1803 return CAStopLEAdapter();
1806 static void CATerminateLE()
1808 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1810 CASetLEReqRespServerCallback(NULL);
1811 CASetLEReqRespClientCallback(NULL);
1812 CALERegisterNetworkNotifications(NULL);
1813 CASetLEReqRespAdapterCallback(NULL);
1814 CATerminateLENetworkMonitor();
1816 ca_mutex_lock(g_bleIsServerMutex);
1817 switch (g_adapterType)
1819 case ADAPTER_SERVER:
1820 CATerminateLEGattServer();
1822 case ADAPTER_CLIENT:
1823 CATerminateLEGattClient();
1825 case ADAPTER_BOTH_CLIENT_SERVER:
1826 CATerminateLEGattServer();
1827 CATerminateLEGattClient();
1832 g_adapterType = ADAPTER_EMPTY;
1833 ca_mutex_unlock(g_bleIsServerMutex);
1835 #ifndef SINGLE_THREAD
1836 CATerminateLEQueues();
1838 CATerminateLEAdapterMutex();
1840 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1843 static CAResult_t CAStartLEListeningServer()
1845 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEListeningServer");
1846 #ifndef ROUTING_GATEWAY
1847 CAResult_t result = CA_STATUS_OK;
1848 #ifndef SINGLE_THREAD
1849 result = CAInitLEServerQueues();
1850 if (CA_STATUS_OK != result)
1852 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1857 ca_mutex_lock(g_bleIsServerMutex);
1858 switch (g_adapterType)
1860 case ADAPTER_CLIENT:
1861 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1863 case ADAPTER_BOTH_CLIENT_SERVER:
1866 g_adapterType = ADAPTER_SERVER;
1868 ca_mutex_unlock(g_bleIsServerMutex);
1870 result = CAGetLEAdapterState();
1871 if (CA_STATUS_OK != result)
1873 if (CA_ADAPTER_NOT_ENABLED == result)
1877 "Listen Server will be started once BT Adapter is enabled");
1882 result = CALEAdapterGattServerStart();
1885 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1888 // Routing Gateway only supports BLE client mode.
1889 OIC_LOG(ERROR, CALEADAPTER_TAG, "LE server not supported in Routing Gateway");
1890 return CA_NOT_SUPPORTED;
1894 static CAResult_t CAStopLEListeningServer()
1896 OIC_LOG(ERROR, CALEADAPTER_TAG, "Listen server stop not supported.");
1897 return CA_NOT_SUPPORTED;
1900 static CAResult_t CAStartLEDiscoveryServer()
1902 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEDiscoveryServer");
1903 CAResult_t result = CA_STATUS_OK;
1904 #ifndef SINGLE_THREAD
1905 result = CAInitLEClientQueues();
1906 if (CA_STATUS_OK != result)
1908 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1913 ca_mutex_lock(g_bleIsServerMutex);
1914 switch (g_adapterType)
1916 case ADAPTER_SERVER:
1917 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1919 case ADAPTER_BOTH_CLIENT_SERVER:
1922 g_adapterType = ADAPTER_CLIENT;
1924 ca_mutex_unlock(g_bleIsServerMutex);
1926 result = CAGetLEAdapterState();
1927 if (CA_STATUS_OK != result)
1929 if (CA_ADAPTER_NOT_ENABLED == result)
1933 "Discovery Server will be started once BT Adapter is enabled");
1938 result = CALEAdapterGattClientStart();
1941 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1945 static CAResult_t CAReadLEData()
1947 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1948 #ifdef SINGLE_THREAD
1951 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1952 return CA_STATUS_OK;
1955 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1959 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEUnicastData");
1962 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1963 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1965 CAResult_t result = CA_STATUS_FAILED;
1967 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
1968 if (ADAPTER_EMPTY == g_adapterType)
1970 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
1973 ca_mutex_lock(g_bleIsServerMutex);
1974 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
1976 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1977 if (CA_STATUS_OK != result)
1979 ca_mutex_unlock(g_bleIsServerMutex);
1980 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed");
1983 g_errorHandler(endpoint, data, dataLen, result);
1990 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
1992 result = CALEAdapterClientSendData(endpoint, data, dataLen);
1993 if (CA_STATUS_OK != result)
1995 ca_mutex_unlock(g_bleIsServerMutex);
1996 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for client failed" );
2000 g_errorHandler(endpoint, data, dataLen, result);
2005 ca_mutex_unlock(g_bleIsServerMutex);
2007 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2011 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
2015 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEMulticastData");
2018 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
2022 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
2026 CAResult_t result = CA_STATUS_FAILED;
2028 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
2029 if (ADAPTER_EMPTY == g_adapterType)
2031 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2034 ca_mutex_lock(g_bleIsServerMutex);
2035 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2037 result = CALEAdapterServerSendData(NULL, data, dataLen);
2038 if (CA_STATUS_OK != result)
2040 ca_mutex_unlock(g_bleIsServerMutex);
2042 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data for server failed" );
2046 g_errorHandler(endpoint, data, dataLen, result);
2052 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2054 result = CALEAdapterClientSendData(NULL, data, dataLen);
2055 if (CA_STATUS_OK != result)
2057 ca_mutex_unlock(g_bleIsServerMutex);
2059 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data for client failed" );
2063 g_errorHandler(endpoint, data, dataLen, result);
2068 ca_mutex_unlock(g_bleIsServerMutex);
2070 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CASendLEMulticastData");
2074 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
2076 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2078 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
2080 char *local_address = NULL;
2082 CAResult_t res = CAGetLEAddress(&local_address);
2083 if (CA_STATUS_OK != res)
2085 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
2089 if (NULL == local_address)
2091 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
2092 return CA_STATUS_FAILED;
2096 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
2097 if (NULL == (*info))
2099 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
2100 OICFree(local_address);
2101 return CA_STATUS_FAILED;
2104 size_t local_address_len = strlen(local_address);
2106 if(local_address_len >= sizeof(g_localBLEAddress) ||
2107 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
2109 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
2111 OICFree(local_address);
2112 return CA_STATUS_FAILED;
2115 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
2116 ca_mutex_lock(g_bleLocalAddressMutex);
2117 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
2118 ca_mutex_unlock(g_bleLocalAddressMutex);
2120 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
2122 OICFree(local_address);
2124 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2125 return CA_STATUS_OK;
2128 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
2130 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2132 ca_mutex_lock(g_bleNetworkCbMutex);
2133 g_networkCallback = netCallback;
2134 ca_mutex_unlock(g_bleNetworkCbMutex);
2135 CAResult_t res = CA_STATUS_OK;
2138 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
2139 if (CA_STATUS_OK != res)
2141 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2146 res = CAUnSetLEAdapterStateChangedCb();
2147 if (CA_STATUS_OK != res)
2149 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2153 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2157 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
2159 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDeviceStateChangedCb");
2161 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
2162 CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE };
2164 ca_mutex_lock(g_bleLocalAddressMutex);
2165 OICStrcpy(localEndpoint.addr,
2166 sizeof(localEndpoint.addr),
2168 ca_mutex_unlock(g_bleLocalAddressMutex);
2170 if (CA_ADAPTER_ENABLED == adapter_state)
2172 ca_mutex_lock(g_bleIsServerMutex);
2173 switch (g_adapterType)
2175 case ADAPTER_SERVER:
2176 CALEAdapterGattServerStart();
2178 case ADAPTER_CLIENT:
2179 CALEAdapterGattClientStart();
2181 case ADAPTER_BOTH_CLIENT_SERVER:
2182 CALEAdapterGattServerStart();
2183 CALEAdapterGattClientStart();
2188 ca_mutex_unlock(g_bleIsServerMutex);
2192 ca_mutex_lock(g_bleIsServerMutex);
2193 switch (g_adapterType)
2195 case ADAPTER_SERVER:
2196 CALEAdapterGattServerStop();
2198 case ADAPTER_CLIENT:
2199 CALEAdapterGattClientStop();
2201 case ADAPTER_BOTH_CLIENT_SERVER:
2202 CALEAdapterGattServerStop();
2203 CALEAdapterGattClientStop();
2208 ca_mutex_unlock(g_bleIsServerMutex);
2211 ca_mutex_lock(g_bleNetworkCbMutex);
2212 if (NULL != g_networkCallback)
2214 g_networkCallback(&localEndpoint, adapter_state);
2218 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
2220 ca_mutex_unlock(g_bleNetworkCbMutex);
2222 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2225 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
2226 const uint8_t *data,
2229 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2231 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2232 #ifndef SINGLE_THREAD
2233 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2234 "g_bleClientSendQueueHandle is NULL",
2236 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
2237 "g_bleClientSendDataMutex is NULL",
2240 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2241 "g_bleClientSendQueueHandle",
2244 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
2246 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2249 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2250 return CA_MEMORY_ALLOC_FAILED;
2252 // Add message to send queue
2253 ca_mutex_lock(g_bleClientSendDataMutex);
2254 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
2255 ca_mutex_unlock(g_bleClientSendDataMutex);
2257 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2258 return CA_STATUS_OK;
2261 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2262 const uint8_t *data,
2265 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2267 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2269 #ifdef SINGLE_THREAD
2270 uint8_t header[CA_HEADER_LENGTH] = { 0 };
2273 CAGenerateHeader(header, CA_HEADER_LENGTH, dataLen);
2275 if (CA_STATUS_OK != result)
2277 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
2278 return CA_STATUS_FAILED;
2281 if (!CAIsLEConnected())
2283 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2284 return CA_STATUS_FAILED;
2287 result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH);
2288 if (CA_STATUS_OK != result)
2290 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2291 return CA_STATUS_FAILED;
2294 const uint32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2295 for (uint32_t iter = 0; iter < dataLimit; iter++)
2298 CAUpdateCharacteristicsToAllGattClients(
2299 data + (iter * CA_SUPPORTED_BLE_MTU_SIZE),
2300 CA_SUPPORTED_BLE_MTU_SIZE);
2302 if (CA_STATUS_OK != result)
2304 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2305 return CA_STATUS_FAILED;
2311 const uint32_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2315 CAUpdateCharacteristicsToAllGattClients(
2316 data + (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE),
2318 if (CA_STATUS_OK != result)
2320 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2321 return CA_STATUS_FAILED;
2326 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2327 "BleClientReceiverQueue is NULL",
2329 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2330 "BleClientSendDataMutex is NULL",
2333 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2336 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2338 CALEData_t * const bleData =
2339 CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2343 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2344 return CA_MEMORY_ALLOC_FAILED;
2347 // Add message to send queue
2348 ca_mutex_lock(g_bleServerSendDataMutex);
2349 CAQueueingThreadAddData(g_bleServerSendQueueHandle,
2351 sizeof(CALEData_t));
2352 ca_mutex_unlock(g_bleServerSendDataMutex);
2354 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2355 return CA_STATUS_OK;
2358 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2359 const uint8_t *data,
2360 uint32_t dataLength,
2361 uint32_t *sentLength)
2363 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2366 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2367 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2369 #ifdef SINGLE_THREAD
2370 if(g_networkPacketReceivedCallback)
2372 // will be filled by upper layer
2373 const CASecureEndpoint_t endpoint =
2374 { .endpoint = { .adapter = CA_ADAPTER_GATT_BTLE } };
2377 g_networkPacketReceivedCallback(&endpoint, data, dataLength);
2380 VERIFY_NON_NULL_RET(g_bleReceiverQueue,
2382 "g_bleReceiverQueue",
2385 //Add message to data queue
2386 CAEndpoint_t * const remoteEndpoint =
2387 CACreateEndpointObject(CA_DEFAULT_FLAGS,
2388 CA_ADAPTER_GATT_BTLE,
2392 if (NULL == remoteEndpoint)
2394 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2395 return CA_STATUS_FAILED;
2398 // Create bleData to add to queue
2401 "Data received from LE layer [%d]",
2404 CALEData_t * const bleData =
2405 CACreateLEData(remoteEndpoint, data, dataLength, g_bleServerSenderInfo);
2409 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2410 CAFreeEndpoint(remoteEndpoint);
2411 return CA_MEMORY_ALLOC_FAILED;
2414 CAFreeEndpoint(remoteEndpoint);
2415 // Add message to receiver queue
2416 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2418 *sentLength = dataLength;
2420 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2421 return CA_STATUS_OK;
2424 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2425 const uint8_t *data,
2426 uint32_t dataLength,
2427 uint32_t *sentLength)
2429 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2432 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2433 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2434 #ifndef SINGLE_THREAD
2435 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG,
2436 "g_bleReceiverQueue",
2439 //Add message to data queue
2440 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2441 CA_ADAPTER_GATT_BTLE,
2443 if (NULL == remoteEndpoint)
2445 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2446 return CA_STATUS_FAILED;
2449 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%u]", dataLength);
2451 // Create bleData to add to queue
2452 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data,
2453 dataLength, g_bleClientSenderInfo);
2456 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2457 CAFreeEndpoint(remoteEndpoint);
2458 return CA_MEMORY_ALLOC_FAILED;
2461 CAFreeEndpoint(remoteEndpoint);
2462 // Add message to receiver queue
2463 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2465 *sentLength = dataLength;
2467 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2468 return CA_STATUS_OK;
2471 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2473 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2475 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2476 g_bleAdapterThreadPool = handle;
2477 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2479 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2482 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2484 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2486 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2488 g_networkPacketReceivedCallback = callback;
2490 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2492 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2495 static void CALEErrorHandler(const char *remoteAddress,
2496 const uint8_t *data,
2500 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2502 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2504 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2505 CA_ADAPTER_GATT_BTLE,
2509 // if required, will be used to build remote endpoint
2510 g_errorHandler(rep, data, dataLen, result);
2512 CAFreeEndpoint(rep);
2514 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");