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"
39 * Logging tag for module name.
41 #define CALEADAPTER_TAG "OIC_LE_ADAP"
44 * Stores information of all the senders.
46 * This structure will be used to track and defragment all incoming
52 uint32_t totalDataLen;
54 CAEndpoint_t *remoteEndpoint;
60 ADAPTER_BOTH_CLIENT_SERVER,
66 * Callback to provide the status of the network change to CA layer.
68 static CANetworkChangeCallback g_networkCallback = NULL;
71 * bleAddress of the local adapter. Value will be initialized to zero,
72 * and will be updated later.
74 static char g_localBLEAddress[18] = { 0 };
77 * Variable to differentiate btw GattServer and GattClient.
79 static CABLEAdapter_t g_adapterType = ADAPTER_EMPTY;
82 * Mutex to synchronize the task to be executed on the GattServer
85 static ca_mutex g_bleIsServerMutex = NULL;
88 * Mutex to synchronize the callback to be called for the network
91 static ca_mutex g_bleNetworkCbMutex = NULL;
94 * Mutex to synchronize the updates of the local LE address of the
97 static ca_mutex g_bleLocalAddressMutex = NULL;
100 * Reference to thread pool.
102 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
105 * Mutex to synchronize the task to be pushed to thread pool.
107 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
110 * Mutex to synchronize the queing of the data from SenderQueue.
112 static ca_mutex g_bleClientSendDataMutex = NULL;
115 * Mutex to synchronize the queing of the data from ReceiverQueue.
117 static ca_mutex g_bleReceiveDataMutex = NULL;
120 * Mutex to synchronize the queing of the data from SenderQueue.
122 static ca_mutex g_bleServerSendDataMutex = NULL;
125 * Mutex to synchronize the callback to be called for the
126 * adapterReqResponse.
128 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
131 * Callback to be called when network packet received from either
132 * GattServer or GattClient.
134 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
137 * Callback to notify error from the BLE adapter.
139 static CAErrorHandleCallback g_errorHandler = NULL;
142 * Register network change notification callback.
144 * @param[in] netCallback CANetworkChangeCallback callback which will
145 * be set for the change in network.
147 * @return 0 on success otherwise a positive error value.
148 * @retval ::CA_STATUS_OK Successful.
149 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
150 * @retval ::CA_STATUS_FAILED Operation failed.
153 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
156 * Set the thread pool handle which is required for spawning new
159 * @param[in] handle Thread pool handle which is given by above layer
160 * for using thread creation task.
163 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
166 * Call the callback to the upper layer when the device state gets
169 * @param[in] adapter_state New state of the adapter to be notified to
172 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state);
175 * Used to initialize all required mutex variable for LE Adapter
178 * @return 0 on success otherwise a positive error value.
179 * @retval ::CA_STATUS_OK Successful.
180 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
181 * @retval ::CA_STATUS_FAILED Operation failed.
184 static CAResult_t CAInitLEAdapterMutex();
187 * Terminate all required mutex variables for LE adapter
190 static void CATerminateLEAdapterMutex();
193 * Prepares and notify error through error callback.
195 static void CALEErrorHandler(const char *remoteAddress,
200 #ifndef SINGLE_THREAD
202 * Stop condition of recvhandler.
204 static bool g_dataBleReceiverHandlerState = false;
207 * Sender information.
209 static u_arraylist_t *g_bleServerSenderInfo = NULL;
211 static u_arraylist_t *g_bleClientSenderInfo = NULL;
214 * Queue to process the outgoing packets from GATTClient.
216 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
219 * Queue to process the incoming packets.
221 static CAQueueingThread_t *g_bleReceiverQueue = NULL;
224 * Queue to process the outgoing packets from GATTServer.
226 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
229 * This function will be associated with the sender queue for
232 * This function will fragment the data to the MTU of the transport
233 * and send the data in fragments to the adapters. The function will
234 * be blocked until all data is sent out from the adapter.
236 * @param[in] threadData Data pushed to the queue which contains the
237 * info about RemoteEndpoint and Data.
239 static void CALEServerSendDataThread(void *threadData);
242 * This function will be associated with the sender queue for
245 * This function will fragment the data to the MTU of the transport
246 * and send the data in fragments to the adapters. The function will
247 * be blocked until all data is sent out from the adapter.
249 * @param[in] threadData Data pushed to the queue which contains the
250 * info about RemoteEndpoint and Data.
252 static void CALEClientSendDataThread(void *threadData);
255 * This function will be associated with the receiver queue.
257 * This function will defragment the received data from each sender
258 * respectively and will send it up to CA layer. Respective sender's
259 * header will provide the length of the data sent.
261 * @param[in] threadData Data pushed to the queue which contains the
262 * info about RemoteEndpoint and Data.
264 static void CALEDataReceiverHandler(void *threadData);
267 * This function will stop all queues created for GattServer and
268 * GattClient. All four queues will be be stopped with this function
271 static void CAStopLEQueues();
274 * This function will terminate all queues created for GattServer and
275 * GattClient. All four queues will be be terminated with this
276 * function invocations.
278 static void CATerminateLEQueues();
281 * This function will initalize the Receiver and Sender queues for
282 * GattServer. This function will in turn call the functions
283 * CAInitBleServerReceiverQueue() and CAInitBleServerSenderQueue() to
284 * initialize the queues.
286 * @return ::CA_STATUS_OK or Appropriate error code.
287 * @retval ::CA_STATUS_OK Successful.
288 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
289 * @retval ::CA_STATUS_FAILED Operation failed.
291 static CAResult_t CAInitLEServerQueues();
294 * This function will initalize the Receiver and Sender queues for
295 * GattClient. This function will inturn call the functions
296 * CAInitBleClientReceiverQueue() and CAInitBleClientSenderQueue() to
297 * initialize the queues.
299 * @return ::CA_STATUS_OK or Appropriate error code.
300 * @retval ::CA_STATUS_OK Successful.
301 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
302 * @retval ::CA_STATUS_FAILED Operation failed.
305 static CAResult_t CAInitLEClientQueues();
308 * This function will initalize the Receiver queue for
309 * GattServer. This will initialize the queue to process the function
310 * CABLEServerSendDataThread() when ever the task is added to this
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.
318 static CAResult_t CAInitLEServerSenderQueue();
321 * This function will initalize the Receiver queue for
322 * GattClient. This will initialize the queue to process the function
323 * CABLEClientSendDataThread() when ever the task is added to this
326 * @return ::CA_STATUS_OK or Appropriate error code.
327 * @retval ::CA_STATUS_OK Successful.
328 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
329 * @retval ::CA_STATUS_FAILED Operation failed.
331 static CAResult_t CAInitLEClientSenderQueue();
334 * This function will initialize the Receiver queue for
335 * LEAdapter. This will initialize the queue to process the function
336 * CABLEDataReceiverHandler() when ever the task is added to this
339 * @return ::CA_STATUS_OK or Appropriate error code
340 * @retval ::CA_STATUS_OK Successful
341 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
342 * @retval ::CA_STATUS_FAILED Operation failed
345 static CAResult_t CAInitLEReceiverQueue();
348 * This function will create the Data required to send it in the
351 * @param[in] remoteEndpoint Remote endpoint information of the
353 * @param[in] data Data to be transmitted from LE.
354 * @param[in] dataLength Length of the Data being transmitted.
356 * @return ::CA_STATUS_OK or Appropriate error code.
357 * @retval ::CA_STATUS_OK Successful.
358 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
359 * @retval ::CA_STATUS_FAILED Operation failed.
361 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
364 u_arraylist_t *senderInfo);
367 * Used to free the BLE information stored in the sender/receiver
370 * @param[in] bleData Information for a particular data segment.
372 static void CAFreeLEData(CALEData_t *bleData);
377 static void CALEDataDestroyer(void *data, uint32_t size);
379 static CAResult_t CAInitLEServerQueues()
381 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
383 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
385 CAResult_t result = CAInitLEServerSenderQueue();
386 if (CA_STATUS_OK != result)
388 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
389 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
390 return CA_STATUS_FAILED;
393 g_bleServerSenderInfo = u_arraylist_create();
394 if (!g_bleServerSenderInfo)
396 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
397 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
398 return CA_MEMORY_ALLOC_FAILED;
401 result = CAInitLEReceiverQueue();
402 if (CA_STATUS_OK != result)
404 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
405 u_arraylist_free(&g_bleServerSenderInfo);
406 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
407 return CA_STATUS_FAILED;
410 g_dataBleReceiverHandlerState = true;
412 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
414 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
418 static CAResult_t CAInitLEClientQueues()
420 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
422 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
424 CAResult_t result = CAInitLEClientSenderQueue();
425 if (CA_STATUS_OK != result)
427 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
428 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
429 return CA_STATUS_FAILED;
432 g_bleClientSenderInfo = u_arraylist_create();
433 if (!g_bleClientSenderInfo)
435 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
436 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
437 return CA_MEMORY_ALLOC_FAILED;
440 result = CAInitLEReceiverQueue();
441 if (CA_STATUS_OK != result)
443 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
444 u_arraylist_free(&g_bleClientSenderInfo);
445 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
446 return CA_STATUS_FAILED;
449 g_dataBleReceiverHandlerState = true;
451 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
453 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
457 static CAResult_t CAInitLEReceiverQueue()
459 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEReceiverQueue");
460 // Check if the message queue is already initialized
461 if (g_bleReceiverQueue)
463 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
467 // Create recv message queue
468 g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
469 if (!g_bleReceiverQueue)
471 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
472 return CA_MEMORY_ALLOC_FAILED;
475 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue,
476 g_bleAdapterThreadPool,
477 CALEDataReceiverHandler,
480 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
481 OICFree(g_bleReceiverQueue);
482 g_bleReceiverQueue = NULL;
483 return CA_STATUS_FAILED;
486 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue))
488 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
489 OICFree(g_bleReceiverQueue);
490 g_bleReceiverQueue = NULL;
491 return CA_STATUS_FAILED;
494 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
498 static CAResult_t CAInitLEServerSenderQueue()
500 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEServerSenderQueue");
501 // Check if the message queue is already initialized
502 if (g_bleServerSendQueueHandle)
504 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
508 // Create send message queue
509 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
510 if (!g_bleServerSendQueueHandle)
512 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
513 return CA_MEMORY_ALLOC_FAILED;
516 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
517 g_bleAdapterThreadPool,
518 CALEServerSendDataThread,
521 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
522 OICFree(g_bleServerSendQueueHandle);
523 g_bleServerSendQueueHandle = NULL;
524 return CA_STATUS_FAILED;
527 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
531 static void CALEClearSenderInfoImpl(u_arraylist_t ** list)
533 const size_t length = u_arraylist_length(*list);
534 for (size_t i = 0; i < length; ++i)
536 CABLESenderInfo_t * const info =
537 (CABLESenderInfo_t *) u_arraylist_get(*list, i);
540 OICFree(info->defragData);
541 CAFreeEndpoint(info->remoteEndpoint);
545 u_arraylist_free(list);
548 static void CALEClearSenderInfo()
550 CALEClearSenderInfoImpl(&g_bleServerSenderInfo);
551 CALEClearSenderInfoImpl(&g_bleClientSenderInfo);
554 static CAResult_t CAInitLEClientSenderQueue()
556 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEClientSenderQueue");
558 if (g_bleClientSendQueueHandle)
560 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
564 // Create send message queue
565 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
566 if (!g_bleClientSendQueueHandle)
568 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
569 return CA_MEMORY_ALLOC_FAILED;
572 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
573 g_bleAdapterThreadPool,
574 CALEClientSendDataThread, CALEDataDestroyer))
576 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
577 OICFree(g_bleClientSendQueueHandle);
578 g_bleClientSendQueueHandle = NULL;
579 return CA_STATUS_FAILED;
582 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAInitLEClientSenderQueue");
586 static void CAStopLEQueues()
588 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStopLEQueues");
590 ca_mutex_lock(g_bleReceiveDataMutex);
591 if (NULL != g_bleReceiverQueue)
593 CAQueueingThreadStop(g_bleReceiverQueue);
595 ca_mutex_unlock(g_bleReceiveDataMutex);
597 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAStopLEQueues");
600 static void CATerminateLEQueues()
602 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
604 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
605 OICFree(g_bleClientSendQueueHandle);
606 g_bleClientSendQueueHandle = NULL;
608 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
609 OICFree(g_bleServerSendQueueHandle);
610 g_bleServerSendQueueHandle = NULL;
612 CAQueueingThreadDestroy(g_bleReceiverQueue);
613 OICFree(g_bleReceiverQueue);
614 g_bleReceiverQueue = NULL;
616 CALEClearSenderInfo();
618 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
621 static CAResult_t CALEGetSenderInfo(const char *leAddress,
622 u_arraylist_t *senderInfoList,
623 CABLESenderInfo_t **senderInfo,
624 uint32_t *senderIndex)
626 VERIFY_NON_NULL_RET(leAddress,
628 "NULL BLE address argument",
629 CA_STATUS_INVALID_PARAM);
630 VERIFY_NON_NULL_RET(senderIndex,
632 "NULL index argument",
633 CA_STATUS_INVALID_PARAM);
635 const uint32_t listLength = u_arraylist_length(senderInfoList);
636 const uint32_t addrLength = strlen(leAddress);
637 for (uint32_t index = 0; index < listLength; index++)
639 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(senderInfoList, index);
640 if(!info || !(info->remoteEndpoint))
645 if(!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
647 *senderIndex = index;
656 return CA_STATUS_FAILED;
659 static void CALEDataReceiverHandler(void *threadData)
661 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDataReceiverHandler");
663 ca_mutex_lock(g_bleReceiveDataMutex);
665 if (g_dataBleReceiverHandlerState)
667 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
669 CALEData_t *bleData = (CALEData_t *) threadData;
672 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
673 ca_mutex_unlock(g_bleReceiveDataMutex);
677 if (!(bleData->senderInfo))
679 OIC_LOG(ERROR, CALEADAPTER_TAG, "sender info is not available");
680 ca_mutex_unlock(g_bleReceiveDataMutex);
684 if (!(bleData->remoteEndpoint))
686 OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!");
687 ca_mutex_unlock(g_bleReceiveDataMutex);
691 CABLESenderInfo_t *senderInfo = NULL;
692 uint32_t senderIndex = 0;
694 if(CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
696 &senderInfo, &senderIndex))
698 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s]",
699 bleData->remoteEndpoint->addr);
704 CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
707 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
708 ca_mutex_unlock(g_bleReceiveDataMutex);
711 newSender->recvDataLen = 0;
712 newSender->totalDataLen = 0;
713 newSender->defragData = NULL;
714 newSender->remoteEndpoint = NULL;
716 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
718 newSender->totalDataLen = coap_get_total_message_length(bleData->data, bleData->dataLen);
720 if(!(newSender->totalDataLen))
722 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
724 ca_mutex_unlock(g_bleReceiveDataMutex);
728 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
729 newSender->totalDataLen);
730 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
733 newSender->defragData = OICCalloc(newSender->totalDataLen + 1,
734 sizeof(*newSender->defragData));
736 if (NULL == newSender->defragData)
738 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
740 ca_mutex_unlock(g_bleReceiveDataMutex);
744 const char *remoteAddress = bleData->remoteEndpoint->addr;
745 newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
746 CA_ADAPTER_GATT_BTLE,
749 if (NULL == newSender->remoteEndpoint)
751 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
752 OICFree(newSender->defragData);
754 ca_mutex_unlock(g_bleReceiveDataMutex);
758 if (newSender->recvDataLen + bleData->dataLen > newSender->totalDataLen)
760 OIC_LOG(ERROR, CALEADAPTER_TAG, "buffer is smaller than received data");
761 OICFree(newSender->defragData);
762 CAFreeEndpoint(newSender->remoteEndpoint);
764 ca_mutex_unlock(g_bleReceiveDataMutex);
767 memcpy(newSender->defragData, bleData->data, bleData->dataLen);
768 newSender->recvDataLen += bleData->dataLen;
770 u_arraylist_add(bleData->senderInfo,(void *)newSender);
772 //Getting newSender index position in bleSenderInfo array list
774 CALEGetSenderInfo(newSender->remoteEndpoint->addr, bleData->senderInfo,
777 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
778 OICFree(newSender->defragData);
779 CAFreeEndpoint(newSender->remoteEndpoint);
781 ca_mutex_unlock(g_bleReceiveDataMutex);
784 senderInfo = newSender;
788 if(senderInfo->recvDataLen + bleData->dataLen > senderInfo->totalDataLen)
790 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
791 "Data Length exceeding error!! Receiving [%d] total length [%d]",
792 senderInfo->recvDataLen + bleData->dataLen, senderInfo->totalDataLen);
793 u_arraylist_remove(bleData->senderInfo, senderIndex);
794 OICFree(senderInfo->defragData);
796 ca_mutex_unlock(g_bleReceiveDataMutex);
799 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]",
801 memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data,
803 senderInfo->recvDataLen += bleData->dataLen ;
804 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] received Datalen [%d]",
805 senderInfo->totalDataLen, senderInfo->recvDataLen);
808 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
810 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
811 if (NULL == g_networkPacketReceivedCallback)
813 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
815 u_arraylist_remove(bleData->senderInfo, senderIndex);
816 OICFree(senderInfo->defragData);
818 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
819 ca_mutex_unlock(g_bleReceiveDataMutex);
823 OIC_LOG(DEBUG, CALEADAPTER_TAG, "[CALEDataReceiverHandler] Sending data up !");
825 const CASecureEndpoint_t tmp =
827 .endpoint = *senderInfo->remoteEndpoint
830 g_networkPacketReceivedCallback(&tmp,
831 senderInfo->defragData,
832 senderInfo->recvDataLen);
833 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
834 u_arraylist_remove(bleData->senderInfo, senderIndex);
835 senderInfo->remoteEndpoint = NULL;
836 senderInfo->defragData = NULL;
840 ca_mutex_unlock(g_bleReceiveDataMutex);
841 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
844 static void CALEServerSendDataThread(void *threadData)
846 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEServerSendDataThread");
848 CALEData_t * const bleData = (CALEData_t *) threadData;
851 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
855 const uint32_t totalLength = bleData->dataLen;
859 "Server total Data length with header is [%u]",
862 uint8_t * const dataSegment = OICCalloc(totalLength, 1);
864 if (NULL == dataSegment)
866 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
871 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
873 length = totalLength;
874 memcpy(dataSegment, bleData->data, bleData->dataLen);
878 length = CA_SUPPORTED_BLE_MTU_SIZE;
879 memcpy(dataSegment, bleData->data, CA_SUPPORTED_BLE_MTU_SIZE);
882 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
884 CAResult_t result = CA_STATUS_FAILED;
886 // Send the first segment with the header.
887 if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
889 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
891 result = CAUpdateCharacteristicsToGattClient(
892 bleData->remoteEndpoint->addr, dataSegment, length);
894 if (CA_STATUS_OK != result)
898 "Update characteristics failed, result [%d]",
901 g_errorHandler(bleData->remoteEndpoint,
905 OICFree(dataSegment);
911 "Server Sent data length [%u]",
913 for (index = 1; index < iter; index++)
915 // Send the remaining header.
918 "Sending the chunk number [%u]",
922 CAUpdateCharacteristicsToGattClient(
923 bleData->remoteEndpoint->addr,
924 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE)),
925 CA_SUPPORTED_BLE_MTU_SIZE);
927 if (CA_STATUS_OK != result)
929 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
930 "Update characteristics failed, result [%d]", result);
931 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
932 OICFree(dataSegment);
935 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
936 CA_SUPPORTED_BLE_MTU_SIZE);
939 const uint32_t remainingLen =
940 totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
942 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
944 // send the last segment of the data (Ex: 22 bytes of 622
945 // bytes of data when MTU is 200)
946 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
948 result = CAUpdateCharacteristicsToGattClient(
949 bleData->remoteEndpoint->addr,
950 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
953 if (CA_STATUS_OK != result)
957 "Update characteristics failed, result [%d]",
959 g_errorHandler(bleData->remoteEndpoint,
963 OICFree(dataSegment);
966 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
971 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
972 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
973 if (CA_STATUS_OK != result)
975 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
977 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
978 OICFree(dataSegment);
981 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
982 for (index = 1; index < iter; index++)
984 // Send the remaining header.
985 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
987 result = CAUpdateCharacteristicsToAllGattClients(
988 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE)),
989 CA_SUPPORTED_BLE_MTU_SIZE);
991 if (CA_STATUS_OK != result)
993 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
995 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
996 OICFree(dataSegment);
999 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%u]",
1000 CA_SUPPORTED_BLE_MTU_SIZE);
1003 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1004 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1006 // send the last segment of the data
1007 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1009 result = CAUpdateCharacteristicsToAllGattClients(
1010 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1013 if (CA_STATUS_OK != result)
1015 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1017 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1018 OICFree(dataSegment);
1021 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1024 OICFree(dataSegment);
1026 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CALEServerSendDataThread");
1029 static void CALEClientSendDataThread(void *threadData)
1031 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEClientSendDataThread");
1033 CALEData_t *bleData = (CALEData_t *) threadData;
1036 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1040 const uint32_t totalLength = bleData->dataLen;
1042 uint8_t *dataSegment = OICCalloc(totalLength, 1);
1043 if (NULL == dataSegment)
1045 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1049 uint32_t length = 0;
1050 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1052 length = totalLength;
1059 length = CA_SUPPORTED_BLE_MTU_SIZE;
1062 CA_SUPPORTED_BLE_MTU_SIZE);
1065 CAResult_t result = CA_STATUS_FAILED;
1066 const uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1068 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1070 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1071 // Send the first segment with the header.
1073 CAUpdateCharacteristicsToGattServer(
1074 bleData->remoteEndpoint->addr,
1080 if (CA_STATUS_OK != result)
1084 "Update characteristics failed, result [%d]",
1086 g_errorHandler(bleData->remoteEndpoint,
1090 OICFree(dataSegment);
1096 "Client Sent Data length is [%u]",
1099 for (index = 1; index < iter; index++)
1101 // Send the remaining header.
1102 result = CAUpdateCharacteristicsToGattServer(
1103 bleData->remoteEndpoint->addr,
1104 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1105 CA_SUPPORTED_BLE_MTU_SIZE,
1108 if (CA_STATUS_OK != result)
1112 "Update characteristics failed, result [%d]",
1114 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1115 OICFree(dataSegment);
1118 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1119 CA_SUPPORTED_BLE_MTU_SIZE);
1122 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1123 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1125 // send the last segment of the data (Ex: 22 bytes of 622
1126 // bytes of data when MTU is 200)
1127 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1129 result = CAUpdateCharacteristicsToGattServer(
1130 bleData->remoteEndpoint->addr,
1131 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1135 if (CA_STATUS_OK != result)
1137 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1139 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1140 OICFree(dataSegment);
1143 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1148 //Sending Mulitcast Data
1149 // Send the first segment with the header.
1150 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1151 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1152 if (CA_STATUS_OK != result)
1154 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1155 "Update characteristics (all) failed, result [%d]", result);
1156 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1157 OICFree(dataSegment);
1160 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1161 // Send the remaining header.
1162 for (index = 1; index < iter; index++)
1164 result = CAUpdateCharacteristicsToAllGattServers(
1165 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1166 CA_SUPPORTED_BLE_MTU_SIZE);
1168 if (CA_STATUS_OK != result)
1170 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1172 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1173 OICFree(dataSegment);
1176 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1177 CA_SUPPORTED_BLE_MTU_SIZE);
1180 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1181 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1183 // send the last segment of the data (Ex: 22 bytes of 622
1184 // bytes of data when MTU is 200)
1185 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1187 CAUpdateCharacteristicsToAllGattServers(
1188 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE),
1191 if (CA_STATUS_OK != result)
1193 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1194 "Update characteristics (all) failed, result [%d]", result);
1195 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1196 OICFree(dataSegment);
1199 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1204 OICFree(dataSegment);
1206 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1209 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
1210 const uint8_t *data,
1211 uint32_t dataLength,
1212 u_arraylist_t *senderInfo)
1214 CALEData_t * const bleData = OICMalloc(sizeof(CALEData_t));
1218 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1222 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1223 bleData->data = OICCalloc(dataLength + 1, 1);
1225 if (NULL == bleData->data)
1227 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1228 CAFreeLEData(bleData);
1232 memcpy(bleData->data, data, dataLength);
1233 bleData->dataLen = dataLength;
1236 bleData->senderInfo = senderInfo;
1242 static void CAFreeLEData(CALEData_t *bleData)
1244 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1246 CAFreeEndpoint(bleData->remoteEndpoint);
1247 OICFree(bleData->data);
1251 static void CALEDataDestroyer(void *data, uint32_t size)
1253 if ((size_t)size < sizeof(CALEData_t *))
1255 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1256 "Destroy data too small %p %d", data, size);
1258 CALEData_t *ledata = (CALEData_t *) data;
1260 CAFreeLEData(ledata);
1264 static CAResult_t CAInitLEAdapterMutex()
1266 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEAdapterMutex");
1268 if (NULL == g_bleIsServerMutex)
1270 g_bleIsServerMutex = ca_mutex_new();
1271 if (NULL == g_bleIsServerMutex)
1273 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1274 return CA_STATUS_FAILED;
1278 if (NULL == g_bleNetworkCbMutex)
1280 g_bleNetworkCbMutex = ca_mutex_new();
1281 if (NULL == g_bleNetworkCbMutex)
1283 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1284 CATerminateLEAdapterMutex();
1285 return CA_STATUS_FAILED;
1289 if (NULL == g_bleLocalAddressMutex)
1291 g_bleLocalAddressMutex = ca_mutex_new();
1292 if (NULL == g_bleLocalAddressMutex)
1294 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1295 CATerminateLEAdapterMutex();
1296 return CA_STATUS_FAILED;
1300 if (NULL == g_bleAdapterThreadPoolMutex)
1302 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1303 if (NULL == g_bleAdapterThreadPoolMutex)
1305 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1306 CATerminateLEAdapterMutex();
1307 return CA_STATUS_FAILED;
1311 if (NULL == g_bleClientSendDataMutex)
1313 g_bleClientSendDataMutex = ca_mutex_new();
1314 if (NULL == g_bleClientSendDataMutex)
1316 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1317 CATerminateLEAdapterMutex();
1318 return CA_STATUS_FAILED;
1322 if (NULL == g_bleServerSendDataMutex)
1324 g_bleServerSendDataMutex = ca_mutex_new();
1325 if (NULL == g_bleServerSendDataMutex)
1327 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1328 CATerminateLEAdapterMutex();
1329 return CA_STATUS_FAILED;
1333 if (NULL == g_bleAdapterReqRespCbMutex)
1335 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1336 if (NULL == g_bleAdapterReqRespCbMutex)
1338 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1339 CATerminateLEAdapterMutex();
1340 return CA_STATUS_FAILED;
1344 if (NULL == g_bleReceiveDataMutex)
1346 g_bleReceiveDataMutex = ca_mutex_new();
1347 if (NULL == g_bleReceiveDataMutex)
1349 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1350 return CA_STATUS_FAILED;
1354 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1355 return CA_STATUS_OK;
1358 static void CATerminateLEAdapterMutex()
1360 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CATerminateLEAdapterMutex");
1362 ca_mutex_free(g_bleIsServerMutex);
1363 g_bleIsServerMutex = NULL;
1365 ca_mutex_free(g_bleNetworkCbMutex);
1366 g_bleNetworkCbMutex = NULL;
1368 ca_mutex_free(g_bleLocalAddressMutex);
1369 g_bleLocalAddressMutex = NULL;
1371 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1372 g_bleAdapterThreadPoolMutex = NULL;
1374 ca_mutex_free(g_bleClientSendDataMutex);
1375 g_bleClientSendDataMutex = NULL;
1377 ca_mutex_free(g_bleServerSendDataMutex);
1378 g_bleServerSendDataMutex = NULL;
1380 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1381 g_bleAdapterReqRespCbMutex = NULL;
1383 ca_mutex_free(g_bleReceiveDataMutex);
1384 g_bleReceiveDataMutex = NULL;
1386 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1390 * Starting LE connectivity adapters.
1392 * As its peer to peer it does not require to start any servers.
1394 * @return ::CA_STATUS_OK or Appropriate error code.
1396 static CAResult_t CAStartLE();
1399 * Start listening server for receiving multicast search requests.
1401 * Transport Specific Behavior:
1402 * LE Starts GATT Server with prefixed UUID and Characteristics
1403 * per OIC Specification.
1404 * @return ::CA_STATUS_OK or Appropriate error code.
1406 static CAResult_t CAStartLEListeningServer();
1409 * Stops listening server from 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 CAStopLEListeningServer();
1419 * Sarting discovery of servers for receiving multicast
1422 * Transport Specific Behavior:
1423 * LE Starts GATT Server with prefixed UUID and Characteristics
1424 * per OIC Specification.
1426 * @return ::CA_STATUS_OK or Appropriate error code
1428 static CAResult_t CAStartLEDiscoveryServer();
1431 * Send data to the endpoint using the adapter connectivity.
1433 * @param[in] endpoint Remote Endpoint information (like MAC address,
1434 * reference URI and connectivity type) to which
1435 * the unicast data has to be sent.
1436 * @param[in] data Data which required to be sent.
1437 * @param[in] dataLen Size of data to be sent.
1439 * @note dataLen must be > 0.
1441 * @return The number of bytes sent on the network, or -1 on error.
1443 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1448 * Send multicast data to the endpoint using the LE connectivity.
1450 * @param[in] endpoint Remote Endpoint information to which the
1451 * multicast data has to be sent.
1452 * @param[in] data Data which required to be sent.
1453 * @param[in] dataLen Size of data to be sent.
1455 * @note dataLen must be > 0.
1457 * @return The number of bytes sent on the network, or -1 on error.
1459 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1464 * Get LE Connectivity network information.
1466 * @param[out] info Local connectivity information structures.
1467 * @param[out] size Number of local connectivity structures.
1469 * @return ::CA_STATUS_OK or Appropriate error code.
1471 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info,
1475 * Read Synchronous API callback.
1477 * @return ::CA_STATUS_OK or Appropriate error code.
1479 static CAResult_t CAReadLEData();
1482 * Stopping the adapters and close socket connections.
1484 * LE Stops all GATT servers and GATT Clients.
1486 * @return ::CA_STATUS_OK or Appropriate error code.
1488 static CAResult_t CAStopLE();
1491 * Terminate the LE connectivity adapter.
1493 * Configuration information will be deleted from further use.
1495 static void CATerminateLE();
1498 * This function will receive the data from the GattServer and add the
1499 * data to the Server receiver queue.
1501 * @param[in] remoteAddress Remote address of the device from where
1503 * @param[in] data Actual data recevied from the remote
1505 * @param[in] dataLength Length of the data received from the
1507 * @param[in] sentLength Length of the data sent from the remote
1510 * @return ::CA_STATUS_OK or Appropriate error code.
1511 * @retval ::CA_STATUS_OK Successful.
1512 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1513 * @retval ::CA_STATUS_FAILED Operation failed.
1516 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
1517 const uint8_t *data,
1518 uint32_t dataLength,
1519 uint32_t *sentLength);
1522 * This function will receive the data from the GattClient and add the
1523 * data into the Client receiver queue.
1525 * @param[in] remoteAddress Remote address of the device from where
1527 * @param[in] data Actual data recevied from the remote
1529 * @param[in] dataLength Length of the data received from the
1531 * @param[in] sentLength Length of the data sent from the remote
1534 * @return ::CA_STATUS_OK or Appropriate error code.
1535 * @retval ::CA_STATUS_OK Successful.
1536 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1537 * @retval ::CA_STATUS_FAILED Operation failed.
1539 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
1540 const uint8_t *data,
1541 uint32_t dataLength,
1542 uint32_t *sentLength);
1545 * Set the NetworkPacket received callback to CA layer from adapter
1548 * @param[in] callback Callback handle sent from the upper layer.
1550 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
1553 * Push the data from CA layer to the Sender processor queue.
1555 * @param[in] remoteEndpoint Remote endpoint information of the
1557 * @param[in] data Data to be transmitted from LE.
1558 * @param[in] dataLen Length of the Data being transmitted.
1560 * @return ::CA_STATUS_OK or Appropriate error code.
1561 * @retval ::CA_STATUS_OK Successful.
1562 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1563 * @retval ::CA_STATUS_FAILED Operation failed.
1565 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1566 const uint8_t *data,
1570 * Push the data from CA layer to the Sender processor queue.
1572 * @param[in] remoteEndpoint Remote endpoint information of the
1574 * @param[in] data Data to be transmitted from LE.
1575 * @param[in] dataLen Length of the Data being transmitted.
1577 * @return ::CA_STATUS_OK or Appropriate error code.
1578 * @retval ::CA_STATUS_OK Successful.
1579 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1580 * @retval ::CA_STATUS_FAILED Operation failed.
1582 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1583 const uint8_t *data,
1586 static CAResult_t CALEAdapterGattServerStart()
1588 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1590 CAResult_t result = CAStartLEGattServer();
1592 #ifndef SINGLE_THREAD
1594 Don't start the server side sending queue thread until the
1595 server itself has actually started.
1597 if (CA_STATUS_OK == result)
1599 ca_mutex_lock(g_bleServerSendDataMutex);
1600 result = CAQueueingThreadStart(g_bleServerSendQueueHandle);
1601 ca_mutex_unlock(g_bleServerSendDataMutex);
1603 if (CA_STATUS_OK != result)
1607 "Unable to start server queuing thread (%d)",
1616 static CAResult_t CALEAdapterGattServerStop()
1618 #ifndef SINGLE_THREAD
1619 ca_mutex_lock(g_bleServerSendDataMutex);
1620 CAResult_t result = CAQueueingThreadStop(g_bleServerSendQueueHandle);
1621 ca_mutex_unlock(g_bleServerSendDataMutex);
1622 if (CA_STATUS_OK == result)
1624 result = CAStopLEGattServer();
1629 return CAStopLEGattServer();
1633 static CAResult_t CALEAdapterGattClientStart()
1635 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattClient");
1637 CAResult_t result = CAStartLEGattClient();
1639 #ifndef SINGLE_THREAD
1641 Don't start the client side sending queue thread until the
1642 client itself has actually started.
1644 if (CA_STATUS_OK == result)
1646 ca_mutex_lock(g_bleClientSendDataMutex);
1647 result = CAQueueingThreadStart(g_bleClientSendQueueHandle);
1648 ca_mutex_unlock(g_bleClientSendDataMutex);
1650 if (CA_STATUS_OK != result)
1654 "Unable to start client queuing thread");
1662 static CAResult_t CALEAdapterGattClientStop()
1664 #ifndef SINGLE_THREAD
1665 ca_mutex_lock(g_bleClientSendDataMutex);
1666 CAResult_t result = CAQueueingThreadStop(g_bleClientSendQueueHandle);
1667 ca_mutex_unlock(g_bleClientSendDataMutex);
1668 if (CA_STATUS_OK == result)
1670 CAStopLEGattClient();
1675 CAStopLEGattClient();
1677 return CA_STATUS_OK;
1681 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1682 CANetworkPacketReceivedCallback reqRespCallback,
1683 CANetworkChangeCallback netCallback,
1684 CAErrorHandleCallback errorCallback,
1685 ca_thread_pool_t handle)
1687 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1690 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1691 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1692 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1694 CAResult_t result = CA_STATUS_OK;
1695 result = CAInitLEAdapterMutex();
1696 if (CA_STATUS_OK != result)
1698 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1699 return CA_STATUS_FAILED;
1702 result = CAInitializeLENetworkMonitor();
1703 if (CA_STATUS_OK != result)
1705 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1706 return CA_STATUS_FAILED;
1708 CAInitializeLEAdapter(handle);
1710 CASetLEClientThreadPoolHandle(handle);
1712 result = CAInitializeLEGattClient();
1713 if (CA_STATUS_OK != result)
1715 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattClient() failed");
1716 return CA_STATUS_FAILED;
1719 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1720 CASetLEServerThreadPoolHandle(handle);
1721 result = CAInitializeLEGattServer();
1722 if (CA_STATUS_OK != result)
1724 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattServer() failed");
1725 return CA_STATUS_FAILED;
1728 CASetLEAdapterThreadPoolHandle(handle);
1729 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1730 CASetLEReqRespAdapterCallback(reqRespCallback);
1732 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1733 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1734 CALERegisterNetworkNotifications(netCallback);
1736 g_errorHandler = errorCallback;
1738 static const CAConnectivityHandler_t connHandler =
1740 .startAdapter = CAStartLE,
1741 .stopAdapter = CAStopLE,
1742 .startListenServer = CAStartLEListeningServer,
1743 .stopListenServer = CAStopLEListeningServer,
1744 .startDiscoveryServer = CAStartLEDiscoveryServer,
1745 .sendData = CASendLEUnicastData,
1746 .sendDataToAll = CASendLEMulticastData,
1747 .GetnetInfo = CAGetLEInterfaceInformation,
1748 .readData = CAReadLEData,
1749 .terminate = CATerminateLE,
1750 .cType = CA_ADAPTER_GATT_BTLE
1753 registerCallback(connHandler);
1755 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1757 return CA_STATUS_OK;
1760 static CAResult_t CAStartLE()
1762 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE");
1764 return CAStartLEAdapter();
1767 static CAResult_t CAStopLE()
1769 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1770 #ifndef SINGLE_THREAD
1774 ca_mutex_lock(g_bleIsServerMutex);
1775 switch (g_adapterType)
1777 case ADAPTER_SERVER:
1778 CALEAdapterGattServerStop();
1780 case ADAPTER_CLIENT:
1781 CALEAdapterGattClientStop();
1783 case ADAPTER_BOTH_CLIENT_SERVER:
1784 CALEAdapterGattServerStop();
1785 CALEAdapterGattClientStop();
1790 ca_mutex_unlock(g_bleIsServerMutex);
1792 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1794 return CAStopLEAdapter();
1797 static void CATerminateLE()
1799 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1801 CASetLEReqRespServerCallback(NULL);
1802 CASetLEReqRespClientCallback(NULL);
1803 CALERegisterNetworkNotifications(NULL);
1804 CASetLEReqRespAdapterCallback(NULL);
1805 CATerminateLENetworkMonitor();
1807 ca_mutex_lock(g_bleIsServerMutex);
1808 switch (g_adapterType)
1810 case ADAPTER_SERVER:
1811 CATerminateLEGattServer();
1813 case ADAPTER_CLIENT:
1814 CATerminateLEGattClient();
1816 case ADAPTER_BOTH_CLIENT_SERVER:
1817 CATerminateLEGattServer();
1818 CATerminateLEGattClient();
1823 g_adapterType = ADAPTER_EMPTY;
1824 ca_mutex_unlock(g_bleIsServerMutex);
1826 #ifndef SINGLE_THREAD
1827 CATerminateLEQueues();
1829 CATerminateLEAdapterMutex();
1831 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1834 static CAResult_t CAStartLEListeningServer()
1836 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEListeningServer");
1837 #ifndef ROUTING_GATEWAY
1838 CAResult_t result = CA_STATUS_OK;
1839 #ifndef SINGLE_THREAD
1840 result = CAInitLEServerQueues();
1841 if (CA_STATUS_OK != result)
1843 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1848 ca_mutex_lock(g_bleIsServerMutex);
1849 switch (g_adapterType)
1851 case ADAPTER_CLIENT:
1852 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1854 case ADAPTER_BOTH_CLIENT_SERVER:
1857 g_adapterType = ADAPTER_SERVER;
1859 ca_mutex_unlock(g_bleIsServerMutex);
1861 result = CAGetLEAdapterState();
1862 if (CA_STATUS_OK != result)
1864 if (CA_ADAPTER_NOT_ENABLED == result)
1868 "Listen Server will be started once BT Adapter is enabled");
1873 result = CALEAdapterGattServerStart();
1876 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1879 // Routing Gateway only supports BLE client mode.
1880 OIC_LOG(ERROR, CALEADAPTER_TAG, "LE server not supported in Routing Gateway");
1881 return CA_NOT_SUPPORTED;
1885 static CAResult_t CAStopLEListeningServer()
1887 OIC_LOG(ERROR, CALEADAPTER_TAG, "Listen server stop not supported.");
1888 return CA_NOT_SUPPORTED;
1891 static CAResult_t CAStartLEDiscoveryServer()
1893 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEDiscoveryServer");
1894 CAResult_t result = CA_STATUS_OK;
1895 #ifndef SINGLE_THREAD
1896 result = CAInitLEClientQueues();
1897 if (CA_STATUS_OK != result)
1899 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1904 ca_mutex_lock(g_bleIsServerMutex);
1905 switch (g_adapterType)
1907 case ADAPTER_SERVER:
1908 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1910 case ADAPTER_BOTH_CLIENT_SERVER:
1913 g_adapterType = ADAPTER_CLIENT;
1915 ca_mutex_unlock(g_bleIsServerMutex);
1917 result = CAGetLEAdapterState();
1918 if (CA_STATUS_OK != result)
1920 if (CA_ADAPTER_NOT_ENABLED == result)
1924 "Discovery Server will be started once BT Adapter is enabled");
1929 result = CALEAdapterGattClientStart();
1932 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1936 static CAResult_t CAReadLEData()
1938 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1939 #ifdef SINGLE_THREAD
1942 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1943 return CA_STATUS_OK;
1946 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1950 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEUnicastData");
1953 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1954 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1956 CAResult_t result = CA_STATUS_FAILED;
1958 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
1959 if (ADAPTER_EMPTY == g_adapterType)
1961 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
1964 ca_mutex_lock(g_bleIsServerMutex);
1965 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
1967 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1968 if (CA_STATUS_OK != result)
1970 ca_mutex_unlock(g_bleIsServerMutex);
1971 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed");
1974 g_errorHandler(endpoint, data, dataLen, result);
1981 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
1983 result = CALEAdapterClientSendData(endpoint, data, dataLen);
1984 if (CA_STATUS_OK != result)
1986 ca_mutex_unlock(g_bleIsServerMutex);
1987 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for client failed" );
1991 g_errorHandler(endpoint, data, dataLen, result);
1996 ca_mutex_unlock(g_bleIsServerMutex);
1998 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2002 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
2006 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEMulticastData");
2009 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
2013 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
2017 CAResult_t result = CA_STATUS_FAILED;
2019 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
2020 if (ADAPTER_EMPTY == g_adapterType)
2022 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2025 ca_mutex_lock(g_bleIsServerMutex);
2026 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2028 result = CALEAdapterServerSendData(NULL, data, dataLen);
2029 if (CA_STATUS_OK != result)
2031 ca_mutex_unlock(g_bleIsServerMutex);
2033 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data for server failed" );
2037 g_errorHandler(endpoint, data, dataLen, result);
2043 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2045 result = CALEAdapterClientSendData(NULL, data, dataLen);
2046 if (CA_STATUS_OK != result)
2048 ca_mutex_unlock(g_bleIsServerMutex);
2050 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data for client failed" );
2054 g_errorHandler(endpoint, data, dataLen, result);
2059 ca_mutex_unlock(g_bleIsServerMutex);
2061 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CASendLEMulticastData");
2065 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
2067 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2069 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
2071 char *local_address = NULL;
2073 CAResult_t res = CAGetLEAddress(&local_address);
2074 if (CA_STATUS_OK != res)
2076 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
2080 if (NULL == local_address)
2082 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
2083 return CA_STATUS_FAILED;
2087 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
2088 if (NULL == (*info))
2090 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
2091 OICFree(local_address);
2092 return CA_STATUS_FAILED;
2095 size_t local_address_len = strlen(local_address);
2097 if(local_address_len >= sizeof(g_localBLEAddress) ||
2098 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
2100 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
2102 OICFree(local_address);
2103 return CA_STATUS_FAILED;
2106 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
2107 ca_mutex_lock(g_bleLocalAddressMutex);
2108 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
2109 ca_mutex_unlock(g_bleLocalAddressMutex);
2111 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
2113 OICFree(local_address);
2115 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2116 return CA_STATUS_OK;
2119 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
2121 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2123 ca_mutex_lock(g_bleNetworkCbMutex);
2124 g_networkCallback = netCallback;
2125 ca_mutex_unlock(g_bleNetworkCbMutex);
2126 CAResult_t res = CA_STATUS_OK;
2129 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
2130 if (CA_STATUS_OK != res)
2132 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2137 res = CAUnSetLEAdapterStateChangedCb();
2138 if (CA_STATUS_OK != res)
2140 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2144 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2148 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
2150 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDeviceStateChangedCb");
2152 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
2153 CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE };
2155 ca_mutex_lock(g_bleLocalAddressMutex);
2156 OICStrcpy(localEndpoint.addr,
2157 sizeof(localEndpoint.addr),
2159 ca_mutex_unlock(g_bleLocalAddressMutex);
2161 if (CA_ADAPTER_ENABLED == adapter_state)
2163 ca_mutex_lock(g_bleIsServerMutex);
2164 switch (g_adapterType)
2166 case ADAPTER_SERVER:
2167 CALEAdapterGattServerStart();
2169 case ADAPTER_CLIENT:
2170 CALEAdapterGattClientStart();
2172 case ADAPTER_BOTH_CLIENT_SERVER:
2173 CALEAdapterGattServerStart();
2174 CALEAdapterGattClientStart();
2179 ca_mutex_unlock(g_bleIsServerMutex);
2183 ca_mutex_lock(g_bleIsServerMutex);
2184 switch (g_adapterType)
2186 case ADAPTER_SERVER:
2187 CALEAdapterGattServerStop();
2189 case ADAPTER_CLIENT:
2190 CALEAdapterGattClientStop();
2192 case ADAPTER_BOTH_CLIENT_SERVER:
2193 CALEAdapterGattServerStop();
2194 CALEAdapterGattClientStop();
2199 ca_mutex_unlock(g_bleIsServerMutex);
2202 ca_mutex_lock(g_bleNetworkCbMutex);
2203 if (NULL != g_networkCallback)
2205 g_networkCallback(&localEndpoint, adapter_state);
2209 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
2211 ca_mutex_unlock(g_bleNetworkCbMutex);
2213 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2216 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
2217 const uint8_t *data,
2220 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2222 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2223 #ifndef SINGLE_THREAD
2224 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2225 "g_bleClientSendQueueHandle is NULL",
2227 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
2228 "g_bleClientSendDataMutex is NULL",
2231 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2232 "g_bleClientSendQueueHandle",
2235 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
2237 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2240 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2241 return CA_MEMORY_ALLOC_FAILED;
2243 // Add message to send queue
2244 ca_mutex_lock(g_bleClientSendDataMutex);
2245 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
2246 ca_mutex_unlock(g_bleClientSendDataMutex);
2248 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2249 return CA_STATUS_OK;
2252 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2253 const uint8_t *data,
2256 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2258 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2260 #ifdef SINGLE_THREAD
2261 if (!CAIsLEConnected())
2263 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2264 return CA_STATUS_FAILED;
2267 CAResult_t result = CA_STATUS_OK;
2268 const uint32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2269 for (uint32_t iter = 0; iter < dataLimit; iter++)
2272 CAUpdateCharacteristicsToAllGattClients(
2273 data + (iter * CA_SUPPORTED_BLE_MTU_SIZE),
2274 CA_SUPPORTED_BLE_MTU_SIZE);
2276 if (CA_STATUS_OK != result)
2278 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2279 return CA_STATUS_FAILED;
2285 const uint32_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2289 CAUpdateCharacteristicsToAllGattClients(
2290 data + (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE),
2292 if (CA_STATUS_OK != result)
2294 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2295 return CA_STATUS_FAILED;
2300 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2301 "BleClientReceiverQueue is NULL",
2303 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2304 "BleClientSendDataMutex is NULL",
2307 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2310 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2312 CALEData_t * const bleData =
2313 CACreateLEData(remoteEndpoint, data, dataLen, NULL);
2317 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2318 return CA_MEMORY_ALLOC_FAILED;
2321 // Add message to send queue
2322 ca_mutex_lock(g_bleServerSendDataMutex);
2323 CAQueueingThreadAddData(g_bleServerSendQueueHandle,
2325 sizeof(CALEData_t));
2326 ca_mutex_unlock(g_bleServerSendDataMutex);
2328 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2329 return CA_STATUS_OK;
2332 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2333 const uint8_t *data,
2334 uint32_t dataLength,
2335 uint32_t *sentLength)
2337 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2340 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2341 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2343 #ifdef SINGLE_THREAD
2344 if(g_networkPacketReceivedCallback)
2346 // will be filled by upper layer
2347 const CASecureEndpoint_t endpoint =
2348 { .endpoint = { .adapter = CA_ADAPTER_GATT_BTLE } };
2351 g_networkPacketReceivedCallback(&endpoint, data, dataLength);
2354 VERIFY_NON_NULL_RET(g_bleReceiverQueue,
2356 "g_bleReceiverQueue",
2359 //Add message to data queue
2360 CAEndpoint_t * const remoteEndpoint =
2361 CACreateEndpointObject(CA_DEFAULT_FLAGS,
2362 CA_ADAPTER_GATT_BTLE,
2366 if (NULL == remoteEndpoint)
2368 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2369 return CA_STATUS_FAILED;
2372 // Create bleData to add to queue
2375 "Data received from LE layer [%d]",
2378 CALEData_t * const bleData =
2379 CACreateLEData(remoteEndpoint, data, dataLength, g_bleServerSenderInfo);
2383 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2384 CAFreeEndpoint(remoteEndpoint);
2385 return CA_MEMORY_ALLOC_FAILED;
2388 CAFreeEndpoint(remoteEndpoint);
2389 // Add message to receiver queue
2390 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2392 *sentLength = dataLength;
2394 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2395 return CA_STATUS_OK;
2398 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2399 const uint8_t *data,
2400 uint32_t dataLength,
2401 uint32_t *sentLength)
2403 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2406 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2407 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2408 #ifndef SINGLE_THREAD
2409 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG,
2410 "g_bleReceiverQueue",
2413 //Add message to data queue
2414 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2415 CA_ADAPTER_GATT_BTLE,
2417 if (NULL == remoteEndpoint)
2419 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2420 return CA_STATUS_FAILED;
2423 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%u]", dataLength);
2425 // Create bleData to add to queue
2426 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data,
2427 dataLength, g_bleClientSenderInfo);
2430 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2431 CAFreeEndpoint(remoteEndpoint);
2432 return CA_MEMORY_ALLOC_FAILED;
2435 CAFreeEndpoint(remoteEndpoint);
2436 // Add message to receiver queue
2437 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2439 *sentLength = dataLength;
2441 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2442 return CA_STATUS_OK;
2445 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2447 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2449 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2450 g_bleAdapterThreadPool = handle;
2451 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2453 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2456 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2458 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2460 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2462 g_networkPacketReceivedCallback = callback;
2464 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2466 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2469 static void CALEErrorHandler(const char *remoteAddress,
2470 const uint8_t *data,
2474 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2476 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2478 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2479 CA_ADAPTER_GATT_BTLE,
2483 // if required, will be used to build remote endpoint
2484 g_errorHandler(rep, data, dataLen, result);
2486 CAFreeEndpoint(rep);
2488 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");