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 "LAD"
44 * Stores the information of the Data to be sent from the queues.
46 * This structure will be pushed to the sender/receiver queue for
51 /// Remote endpoint contains the information of remote device.
52 CAEndpoint_t *remoteEndpoint;
54 /// Data to be transmitted over LE transport.
57 /// Length of the data being transmitted.
62 * Stores information of all the senders.
64 * This structure will be used to track and defragment all incoming
70 uint32_t totalDataLen;
72 CAEndpoint_t *remoteEndpoint;
76 * Callback to provide the status of the network change to CA layer.
78 static CANetworkChangeCallback g_networkCallback = NULL;
81 * bleAddress of the local adapter. Value will be initialized to zero,
82 * and will be updated later.
84 static char g_localBLEAddress[18] = { 0 };
87 * Variable to differentiate btw GattServer and GattClient.
89 static bool g_isServer = false;
92 * Mutex to synchronize the task to be executed on the GattServer
95 static ca_mutex g_bleIsServerMutex = NULL;
98 * Mutex to synchronize the callback to be called for the network
101 static ca_mutex g_bleNetworkCbMutex = NULL;
104 * Mutex to synchronize the updates of the local LE address of the
107 static ca_mutex g_bleLocalAddressMutex = NULL;
110 * Reference to thread pool.
112 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
115 * Mutex to synchronize the task to be pushed to thread pool.
117 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
120 * Mutex to synchronize the queing of the data from SenderQueue.
122 static ca_mutex g_bleClientSendDataMutex = NULL;
125 * Mutex to synchronize the queing of the data from ReceiverQueue.
127 static ca_mutex g_bleReceiveDataMutex = NULL;
131 * Mutex to synchronize the queing of the data from SenderQueue.
133 static ca_mutex g_bleServerSendDataMutex = NULL;
136 * Mutex to synchronize the callback to be called for the
137 * adapterReqResponse.
139 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
142 * Callback to be called when network packet received from either
143 * GattServer or GattClient.
145 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
148 * Callback to notify error from the BLE adapter.
150 static CAErrorHandleCallback g_errorHandler = NULL;
153 * Storing Adapter state information.
155 static CAAdapterState_t g_bleAdapterState = CA_ADAPTER_DISABLED;
160 * This enumeration provides information of LE Adapter Server status.
164 CA_SERVER_NOTSTARTED = 0,
170 * Structure to maintain the status of the server.
172 static CALeServerStatus gLeServerStatus = CA_SERVER_NOTSTARTED;
175 * Register network change notification callback.
177 * @param[in] netCallback CANetworkChangeCallback callback which will
178 * be set for the change in network.
180 * @return 0 on success otherwise a positive error value.
181 * @retval ::CA_STATUS_OK Successful.
182 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
183 * @retval ::CA_STATUS_FAILED Operation failed.
186 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
189 * Set the thread pool handle which is required for spawning new
192 * @param[in] handle Thread pool handle which is given by above layer
193 * for using thread creation task.
196 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
199 * Call the callback to the upper layer when the device state gets
202 * @param[in] adapter_state New state of the adapter to be notified to
205 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state);
208 * Used to initialize all required mutex variable for LE Adapter
211 * @return 0 on success otherwise a positive error value.
212 * @retval ::CA_STATUS_OK Successful.
213 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
214 * @retval ::CA_STATUS_FAILED Operation failed.
217 static CAResult_t CAInitLEAdapterMutex();
220 * Terminate all required mutex variables for LE adapter
223 static void CATerminateLEAdapterMutex();
226 * Prepares and notify error through error callback.
228 static void CALEErrorHandler(const char *remoteAddress,
233 #ifndef SINGLE_THREAD
235 * Stop condition of recvhandler.
237 static bool g_dataReceiverHandlerState = false;
240 * Sender information.
242 static u_arraylist_t *g_senderInfo = NULL;
245 * Queue to process the outgoing packets from GATTClient.
247 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
250 * Queue to process the incoming packets to GATT Client.
252 static CAQueueingThread_t *g_bleReceiverQueue = NULL;
255 * Queue to process the outgoing packets from GATTServer.
257 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
260 * This function will be associated with the sender queue for
263 * This function will fragment the data to the MTU of the transport
264 * and send the data in fragments to the adapters. The function will
265 * be blocked until all data is sent out from the adapter.
267 * @param[in] threadData Data pushed to the queue which contains the
268 * info about RemoteEndpoint and Data.
270 static void CALEServerSendDataThread(void *threadData);
273 * This function will be associated with the sender queue for
276 * This function will fragment the data to the MTU of the transport
277 * and send the data in fragments to the adapters. The function will
278 * be blocked until all data is sent out from the adapter.
280 * @param[in] threadData Data pushed to the queue which contains the
281 * info about RemoteEndpoint and Data.
283 static void CALEClientSendDataThread(void *threadData);
286 * This function will be associated with the receiver queue.
288 * This function will defragment the received data from each sender
289 * respectively and will send it up to CA layer. Respective sender's
290 * header will provide the length of the data sent.
292 * @param[in] threadData Data pushed to the queue which contains the
293 * info about RemoteEndpoint and Data.
295 static void CALEDataReceiverHandler(void *threadData);
298 * This function will stop all queues created for GattServer and
299 * GattClient. All four queues will be be stopped with this function
302 static void CAStopLEQueues();
305 * This function will terminate all queues created for GattServer and
306 * GattClient. All four queues will be be terminated with this
307 * function invocations.
309 static void CATerminateLEQueues();
312 * This function will initalize the Receiver and Sender queues for
313 * GattServer. This function will in turn call the functions
314 * CAInitBleServerReceiverQueue() and CAInitBleServerSenderQueue() to
315 * initialize the queues.
317 * @return ::CA_STATUS_OK or Appropriate error code.
318 * @retval ::CA_STATUS_OK Successful.
319 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
320 * @retval ::CA_STATUS_FAILED Operation failed.
322 static CAResult_t CAInitLEServerQueues();
325 * This function will initalize the Receiver and Sender queues for
326 * GattClient. This function will inturn call the functions
327 * CAInitBleClientReceiverQueue() and CAInitBleClientSenderQueue() to
328 * initialize the queues.
330 * @return ::CA_STATUS_OK or Appropriate error code.
331 * @retval ::CA_STATUS_OK Successful.
332 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
333 * @retval ::CA_STATUS_FAILED Operation failed.
336 static CAResult_t CAInitLEClientQueues();
339 * This function will initalize the Receiver queue for
340 * GattServer. This will initialize the queue to process the function
341 * CABLEServerSendDataThread() when ever the task is added to this
344 * @return ::CA_STATUS_OK or Appropriate error code.
345 * @retval ::CA_STATUS_OK Successful.
346 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
347 * @retval ::CA_STATUS_FAILED Operation failed.
349 static CAResult_t CAInitLEServerSenderQueue();
352 * This function will initalize the Receiver queue for
353 * GattClient. This will initialize the queue to process the function
354 * CABLEClientSendDataThread() when ever the task is added to this
357 * @return ::CA_STATUS_OK or Appropriate error code.
358 * @retval ::CA_STATUS_OK Successful.
359 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
360 * @retval ::CA_STATUS_FAILED Operation failed.
362 static CAResult_t CAInitLEClientSenderQueue();
365 * This function will initialize the Receiver queue for
366 * LEAdapter. This will initialize the queue to process the function
367 * CABLEDataReceiverHandler() when ever the task is added to this
370 * @return ::CA_STATUS_OK or Appropriate error code
371 * @retval ::CA_STATUS_OK Successful
372 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
373 * @retval ::CA_STATUS_FAILED Operation failed
376 static CAResult_t CAInitLEReceiverQueue();
379 * This function will create the Data required to send it in the
382 * @param[in] remoteEndpoint Remote endpoint information of the
384 * @param[in] data Data to be transmitted from LE.
385 * @param[in] dataLength Length of the Data being transmitted.
387 * @return ::CA_STATUS_OK or Appropriate error code.
388 * @retval ::CA_STATUS_OK Successful.
389 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
390 * @retval ::CA_STATUS_FAILED Operation failed.
392 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
394 uint32_t dataLength);
397 * Used to free the BLE information stored in the sender/receiver
400 * @param[in] bleData Information for a particular data segment.
402 static void CAFreeLEData(CALEData_t *bleData);
407 static void CALEDataDestroyer(void *data, uint32_t size);
409 static CAResult_t CAInitLEServerQueues()
411 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
413 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
415 CAResult_t result = CAInitLEServerSenderQueue();
416 if (CA_STATUS_OK != result)
418 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
419 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
420 return CA_STATUS_FAILED;
423 result = CAInitLEReceiverQueue();
424 if (CA_STATUS_OK != result)
426 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerReceiverQueue failed");
427 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
428 return CA_STATUS_FAILED;
431 g_dataReceiverHandlerState = true;
433 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
435 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
439 static CAResult_t CAInitLEClientQueues()
441 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
443 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
445 CAResult_t result = CAInitLEClientSenderQueue();
446 if (CA_STATUS_OK != result)
448 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
449 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
450 return CA_STATUS_FAILED;
453 result = CAInitLEReceiverQueue();
454 if (CA_STATUS_OK != result)
456 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientReceiverQueue failed");
457 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
458 return CA_STATUS_FAILED;
461 g_dataReceiverHandlerState = true;
463 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
465 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
469 static CAResult_t CAInitLEReceiverQueue()
471 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
472 // Check if the message queue is already initialized
473 if (g_bleReceiverQueue)
475 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
479 // Create recv message queue
480 g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
481 if (!g_bleReceiverQueue)
483 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
484 return CA_MEMORY_ALLOC_FAILED;
487 g_senderInfo = u_arraylist_create();
490 OIC_LOG(ERROR, CALEADAPTER_TAG, "ClientInfo memory allcation failed!");
491 OICFree(g_bleReceiverQueue);
492 g_bleReceiverQueue = NULL;
493 return CA_MEMORY_ALLOC_FAILED;
496 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue, g_bleAdapterThreadPool,
497 CALEDataReceiverHandler, CALEDataDestroyer))
499 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
500 OICFree(g_bleReceiverQueue);
501 g_bleReceiverQueue = NULL;
502 u_arraylist_free(&g_senderInfo);
503 return CA_STATUS_FAILED;
506 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue))
508 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
509 OICFree(g_bleReceiverQueue);
510 g_bleReceiverQueue = NULL;
511 u_arraylist_free(&g_senderInfo);
512 return CA_STATUS_FAILED;
515 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
519 static CAResult_t CAInitLEServerSenderQueue()
521 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
522 // Check if the message queue is already initialized
523 if (g_bleServerSendQueueHandle)
525 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
529 // Create send message queue
530 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
531 if (!g_bleServerSendQueueHandle)
533 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
534 return CA_MEMORY_ALLOC_FAILED;
537 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
538 g_bleAdapterThreadPool,
539 CALEServerSendDataThread, CALEDataDestroyer))
541 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
542 OICFree(g_bleServerSendQueueHandle);
543 g_bleServerSendQueueHandle = NULL;
544 return CA_STATUS_FAILED;
547 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
551 static void CALEClearSenderInfo()
553 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
555 uint32_t listIndex = 0;
556 uint32_t listLength = u_arraylist_length(g_senderInfo);
557 for (listIndex = 0; listIndex < listLength; listIndex++)
559 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(g_senderInfo, listIndex);
565 OICFree(info->defragData);
566 CAFreeEndpoint(info->remoteEndpoint);
569 u_arraylist_free(&g_senderInfo);
570 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
573 static CAResult_t CAInitLEClientSenderQueue()
575 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
577 if (g_bleClientSendQueueHandle)
579 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
583 // Create send message queue
584 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
585 if (!g_bleClientSendQueueHandle)
587 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
588 return CA_MEMORY_ALLOC_FAILED;
591 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
592 g_bleAdapterThreadPool,
593 CALEClientSendDataThread, CALEDataDestroyer))
595 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
596 OICFree(g_bleClientSendQueueHandle);
597 g_bleClientSendQueueHandle = NULL;
598 return CA_STATUS_FAILED;
601 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
605 static void CAStopLEQueues()
607 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
609 ca_mutex_lock(g_bleClientSendDataMutex);
610 if (NULL != g_bleClientSendQueueHandle)
612 CAQueueingThreadStop(g_bleClientSendQueueHandle);
614 ca_mutex_unlock(g_bleClientSendDataMutex);
616 ca_mutex_lock(g_bleServerSendDataMutex);
617 if (NULL != g_bleServerSendQueueHandle)
619 CAQueueingThreadStop(g_bleServerSendQueueHandle);
621 ca_mutex_unlock(g_bleServerSendDataMutex);
623 ca_mutex_lock(g_bleReceiveDataMutex);
624 if (NULL != g_bleReceiverQueue)
626 CAQueueingThreadStop(g_bleReceiverQueue);
628 ca_mutex_unlock(g_bleReceiveDataMutex);
630 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
633 static void CATerminateLEQueues()
635 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
637 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
638 OICFree(g_bleClientSendQueueHandle);
639 g_bleClientSendQueueHandle = NULL;
641 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
642 OICFree(g_bleServerSendQueueHandle);
643 g_bleServerSendQueueHandle = NULL;
645 CAQueueingThreadDestroy(g_bleReceiverQueue);
646 OICFree(g_bleReceiverQueue);
647 g_bleReceiverQueue = NULL;
649 CALEClearSenderInfo();
651 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
654 static CAResult_t CALEGetSenderInfo(const char *leAddress,
655 CABLESenderInfo_t **senderInfo,
656 uint32_t *senderIndex)
658 VERIFY_NON_NULL_RET(leAddress,
660 "NULL BLE address argument",
661 CA_STATUS_INVALID_PARAM);
662 VERIFY_NON_NULL_RET(senderIndex,
664 "NULL index argument",
665 CA_STATUS_INVALID_PARAM);
667 const uint32_t listLength = u_arraylist_length(g_senderInfo);
668 const uint32_t addrLength = strlen(leAddress);
669 for (uint32_t index = 0; index < listLength; index++)
671 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(g_senderInfo, index);
672 if(!info || !(info->remoteEndpoint))
677 if(!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
679 *senderIndex = index;
688 return CA_STATUS_FAILED;
691 static void CALEDataReceiverHandler(void *threadData)
693 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
695 ca_mutex_lock(g_bleReceiveDataMutex);
697 if (g_dataReceiverHandlerState)
699 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
701 CALEData_t *bleData = (CALEData_t *) threadData;
704 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
705 ca_mutex_unlock(g_bleReceiveDataMutex);
709 if(!(bleData->remoteEndpoint))
711 OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!");
712 ca_mutex_unlock(g_bleReceiveDataMutex);
716 CABLESenderInfo_t *senderInfo = NULL;
717 uint32_t senderIndex = 0;
719 if(CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
720 &senderInfo, &senderIndex))
722 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s]",
723 bleData->remoteEndpoint->addr);
728 CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
731 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
732 ca_mutex_unlock(g_bleReceiveDataMutex);
735 newSender->recvDataLen = 0;
736 newSender->totalDataLen = 0;
737 newSender->defragData = NULL;
738 newSender->remoteEndpoint = NULL;
740 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
741 newSender->totalDataLen = CAParseHeader(bleData->data,
743 if(!(newSender->totalDataLen))
745 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
747 ca_mutex_unlock(g_bleReceiveDataMutex);
751 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
752 newSender->totalDataLen);
753 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
756 newSender->defragData = OICCalloc(newSender->totalDataLen + 1,
757 sizeof(*newSender->defragData));
759 if (NULL == newSender->defragData)
761 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
763 ca_mutex_unlock(g_bleReceiveDataMutex);
767 const char *remoteAddress = bleData->remoteEndpoint->addr;
768 newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
769 CA_ADAPTER_GATT_BTLE, remoteAddress, 0);
770 if (NULL == newSender->remoteEndpoint)
772 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
773 OICFree(newSender->defragData);
775 ca_mutex_unlock(g_bleReceiveDataMutex);
778 memcpy(newSender->defragData, bleData->data + CA_HEADER_LENGTH,
779 bleData->dataLen - CA_HEADER_LENGTH);
780 newSender->recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
781 u_arraylist_add(g_senderInfo,(void *)newSender);
783 //Getting newSender index position in g_senderInfo array list
785 CALEGetSenderInfo(newSender->remoteEndpoint->addr, NULL, &senderIndex))
787 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
788 OICFree(newSender->defragData);
789 CAFreeEndpoint(newSender->remoteEndpoint);
791 ca_mutex_unlock(g_bleReceiveDataMutex);
794 senderInfo = newSender;
798 if(senderInfo->recvDataLen + bleData->dataLen > senderInfo->totalDataLen)
800 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
801 "Data Length exceeding error!! Receiving [%d] total length [%d]",
802 senderInfo->recvDataLen + bleData->dataLen, senderInfo->totalDataLen);
803 u_arraylist_remove(g_senderInfo, senderIndex);
804 OICFree(senderInfo->defragData);
806 ca_mutex_unlock(g_bleReceiveDataMutex);
809 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
810 memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data,
812 senderInfo->recvDataLen += bleData->dataLen ;
813 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
814 senderInfo->totalDataLen, senderInfo->recvDataLen);
817 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
819 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
820 if (NULL == g_networkPacketReceivedCallback)
822 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
824 u_arraylist_remove(g_senderInfo, senderIndex);
825 OICFree(senderInfo->defragData);
827 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
828 ca_mutex_unlock(g_bleReceiveDataMutex);
832 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
834 const CASecureEndpoint_t tmp =
836 .endpoint = *senderInfo->remoteEndpoint
839 g_networkPacketReceivedCallback(&tmp,
840 senderInfo->defragData,
841 senderInfo->recvDataLen);
842 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
843 u_arraylist_remove(g_senderInfo, senderIndex);
844 senderInfo->remoteEndpoint = NULL;
845 senderInfo->defragData = NULL;
849 ca_mutex_unlock(g_bleReceiveDataMutex);
850 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
853 static void CALEServerSendDataThread(void *threadData)
855 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
857 CALEData_t * const bleData = (CALEData_t *) threadData;
860 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
864 uint8_t * const header = OICCalloc(CA_HEADER_LENGTH, 1);
865 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
867 const uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
871 "Server total Data length with header is [%u]",
874 uint8_t * const dataSegment = OICCalloc(totalLength, 1);
876 if (NULL == dataSegment)
878 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
883 CAResult_t result = CAGenerateHeader(header,
886 if (CA_STATUS_OK != result )
888 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
890 OICFree(dataSegment);
894 memcpy(dataSegment, header, CA_HEADER_LENGTH);
898 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
900 length = totalLength;
901 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
905 length = CA_SUPPORTED_BLE_MTU_SIZE;
906 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
907 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
910 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
913 // Send the first segment with the header.
914 if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
916 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
918 result = CAUpdateCharacteristicsToGattClient(
919 bleData->remoteEndpoint->addr, dataSegment, length);
921 if (CA_STATUS_OK != result)
925 "Update characteristics failed, result [%d]",
928 g_errorHandler(bleData->remoteEndpoint,
932 OICFree(dataSegment);
938 "Server Sent data length [%u]",
940 for (index = 1; index < iter; index++)
942 // Send the remaining header.
945 "Sending the chunk number [%u]",
949 CAUpdateCharacteristicsToGattClient(
950 bleData->remoteEndpoint->addr,
951 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
952 CA_SUPPORTED_BLE_MTU_SIZE);
953 if (CA_STATUS_OK != result)
955 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
956 "Update characteristics failed, result [%d]", result);
957 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
958 OICFree(dataSegment);
961 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
962 CA_SUPPORTED_BLE_MTU_SIZE);
965 const uint32_t remainingLen =
966 totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
968 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
970 // send the last segment of the data (Ex: 22 bytes of 622
971 // bytes of data when MTU is 200)
972 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
973 result = CAUpdateCharacteristicsToGattClient(
974 bleData->remoteEndpoint->addr,
975 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
977 if (CA_STATUS_OK != result)
981 "Update characteristics failed, result [%d]",
983 g_errorHandler(bleData->remoteEndpoint,
987 OICFree(dataSegment);
990 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
995 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
996 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
997 if (CA_STATUS_OK != result)
999 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1001 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1002 OICFree(dataSegment);
1005 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1006 for (index = 1; index < iter; index++)
1008 // Send the remaining header.
1009 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1010 result = CAUpdateCharacteristicsToAllGattClients(
1011 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1012 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 [%u]", CA_SUPPORTED_BLE_MTU_SIZE);
1024 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1025 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1027 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1028 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1029 result = CAUpdateCharacteristicsToAllGattClients(
1030 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1032 if (CA_STATUS_OK != result)
1034 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1036 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1037 OICFree(dataSegment);
1040 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1043 OICFree(dataSegment);
1045 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1048 static void CALEClientSendDataThread(void *threadData)
1050 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1052 CALEData_t *bleData = (CALEData_t *) threadData;
1055 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1059 uint8_t * const header = OICCalloc(CA_HEADER_LENGTH, 1);
1060 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1062 const uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1063 uint8_t *dataSegment = OICCalloc(totalLength, 1);
1064 if (NULL == dataSegment)
1066 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1071 CAResult_t result = CAGenerateHeader(header,
1074 if (CA_STATUS_OK != result )
1076 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1078 OICFree(dataSegment);
1081 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1084 uint32_t length = 0;
1085 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1087 length = totalLength;
1088 memcpy(dataSegment + CA_HEADER_LENGTH,
1094 length = CA_SUPPORTED_BLE_MTU_SIZE;
1095 memcpy(dataSegment + CA_HEADER_LENGTH,
1097 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1100 const uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1102 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1104 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1105 // Send the first segment with the header.
1107 CAUpdateCharacteristicsToGattServer(
1108 bleData->remoteEndpoint->addr,
1114 if (CA_STATUS_OK != result)
1118 "Update characteristics failed, result [%d]",
1120 g_errorHandler(bleData->remoteEndpoint,
1124 OICFree(dataSegment);
1130 "Client Sent Data length is [%u]",
1133 for (index = 1; index < iter; index++)
1135 // Send the remaining header.
1136 result = CAUpdateCharacteristicsToGattServer(
1137 bleData->remoteEndpoint->addr,
1138 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1139 CA_SUPPORTED_BLE_MTU_SIZE,
1141 if (CA_STATUS_OK != result)
1145 "Update characteristics failed, result [%d]",
1147 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1148 OICFree(dataSegment);
1151 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1152 CA_SUPPORTED_BLE_MTU_SIZE);
1155 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1156 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1158 // send the last segment of the data (Ex: 22 bytes of 622
1159 // bytes of data when MTU is 200)
1160 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1161 result = CAUpdateCharacteristicsToGattServer(
1162 bleData->remoteEndpoint->addr,
1163 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1167 if (CA_STATUS_OK != result)
1169 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1171 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1172 OICFree(dataSegment);
1175 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1180 //Sending Mulitcast Data
1181 // Send the first segment with the header.
1182 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1183 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1184 if (CA_STATUS_OK != result)
1186 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1187 "Update characteristics (all) failed, result [%d]", result);
1188 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1189 OICFree(dataSegment);
1192 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1193 // Send the remaining header.
1194 for (index = 1; index < iter; index++)
1196 result = CAUpdateCharacteristicsToAllGattServers(
1197 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1198 CA_SUPPORTED_BLE_MTU_SIZE);
1199 if (CA_STATUS_OK != result)
1201 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]",
1203 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1204 OICFree(dataSegment);
1207 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1208 CA_SUPPORTED_BLE_MTU_SIZE);
1211 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1212 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1214 // send the last segment of the data (Ex: 22 bytes of 622
1215 // bytes of data when MTU is 200)
1216 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1218 CAUpdateCharacteristicsToAllGattServers(
1219 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1221 if (CA_STATUS_OK != result)
1223 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1224 "Update characteristics (all) failed, result [%d]", result);
1225 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1226 OICFree(dataSegment);
1229 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1234 OICFree(dataSegment);
1236 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1239 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
1240 const uint8_t *data,
1241 uint32_t dataLength)
1243 CALEData_t * const bleData = OICMalloc(sizeof(CALEData_t));
1247 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1251 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1252 bleData->data = OICCalloc(dataLength + 1, 1);
1254 if (NULL == bleData->data)
1256 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1257 CAFreeLEData(bleData);
1261 memcpy(bleData->data, data, dataLength);
1262 bleData->dataLen = dataLength;
1267 static void CAFreeLEData(CALEData_t *bleData)
1269 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1271 CAFreeEndpoint(bleData->remoteEndpoint);
1272 OICFree(bleData->data);
1276 static void CALEDataDestroyer(void *data, uint32_t size)
1278 if ((size_t)size < sizeof(CALEData_t *))
1280 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1281 "Destroy data too small %p %d", data, size);
1283 CALEData_t *ledata = (CALEData_t *) data;
1285 CAFreeLEData(ledata);
1289 static CAResult_t CAInitLEAdapterMutex()
1291 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1293 if (NULL == g_bleIsServerMutex)
1295 g_bleIsServerMutex = ca_mutex_new();
1296 if (NULL == g_bleIsServerMutex)
1298 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1299 return CA_STATUS_FAILED;
1303 if (NULL == g_bleNetworkCbMutex)
1305 g_bleNetworkCbMutex = ca_mutex_new();
1306 if (NULL == g_bleNetworkCbMutex)
1308 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1309 CATerminateLEAdapterMutex();
1310 return CA_STATUS_FAILED;
1314 if (NULL == g_bleLocalAddressMutex)
1316 g_bleLocalAddressMutex = ca_mutex_new();
1317 if (NULL == g_bleLocalAddressMutex)
1319 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1320 CATerminateLEAdapterMutex();
1321 return CA_STATUS_FAILED;
1325 if (NULL == g_bleAdapterThreadPoolMutex)
1327 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1328 if (NULL == g_bleAdapterThreadPoolMutex)
1330 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1331 CATerminateLEAdapterMutex();
1332 return CA_STATUS_FAILED;
1336 if (NULL == g_bleClientSendDataMutex)
1338 g_bleClientSendDataMutex = ca_mutex_new();
1339 if (NULL == g_bleClientSendDataMutex)
1341 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1342 CATerminateLEAdapterMutex();
1343 return CA_STATUS_FAILED;
1347 if (NULL == g_bleServerSendDataMutex)
1349 g_bleServerSendDataMutex = ca_mutex_new();
1350 if (NULL == g_bleServerSendDataMutex)
1352 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1353 CATerminateLEAdapterMutex();
1354 return CA_STATUS_FAILED;
1358 if (NULL == g_bleAdapterReqRespCbMutex)
1360 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1361 if (NULL == g_bleAdapterReqRespCbMutex)
1363 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1364 CATerminateLEAdapterMutex();
1365 return CA_STATUS_FAILED;
1369 if (NULL == g_bleReceiveDataMutex)
1371 g_bleReceiveDataMutex = ca_mutex_new();
1372 if (NULL == g_bleReceiveDataMutex)
1374 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1375 return CA_STATUS_FAILED;
1379 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1380 return CA_STATUS_OK;
1383 static void CATerminateLEAdapterMutex()
1385 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1387 ca_mutex_free(g_bleIsServerMutex);
1388 g_bleIsServerMutex = NULL;
1390 ca_mutex_free(g_bleNetworkCbMutex);
1391 g_bleNetworkCbMutex = NULL;
1393 ca_mutex_free(g_bleLocalAddressMutex);
1394 g_bleLocalAddressMutex = NULL;
1396 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1397 g_bleAdapterThreadPoolMutex = NULL;
1399 ca_mutex_free(g_bleClientSendDataMutex);
1400 g_bleClientSendDataMutex = NULL;
1402 ca_mutex_free(g_bleServerSendDataMutex);
1403 g_bleServerSendDataMutex = NULL;
1405 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1406 g_bleAdapterReqRespCbMutex = NULL;
1408 ca_mutex_free(g_bleReceiveDataMutex);
1409 g_bleReceiveDataMutex = NULL;
1411 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1415 * Starting LE connectivity adapters.
1417 * As its peer to peer it does not require to start any servers.
1419 * @return ::CA_STATUS_OK or Appropriate error code.
1421 static CAResult_t CAStartLE();
1424 * Start listening server for receiving multicast search requests.
1426 * Transport Specific Behavior:
1427 * LE Starts GATT Server with prefixed UUID and Characteristics
1428 * per OIC Specification.
1429 * @return ::CA_STATUS_OK or Appropriate error code.
1431 static CAResult_t CAStartLEListeningServer();
1434 * Stops listening server from receiving multicast search requests.
1436 * Transport Specific Behavior:
1437 * LE Starts GATT Server with prefixed UUID and Characteristics
1438 * per OIC Specification.
1439 * @return ::CA_STATUS_OK or Appropriate error code.
1441 static CAResult_t CAStopLEListeningServer();
1444 * Sarting discovery of servers for receiving multicast
1447 * Transport Specific Behavior:
1448 * LE Starts GATT Server with prefixed UUID and Characteristics
1449 * per OIC Specification.
1451 * @return ::CA_STATUS_OK or Appropriate error code
1453 static CAResult_t CAStartLEDiscoveryServer();
1456 * Send data to the endpoint using the adapter connectivity.
1458 * @param[in] endpoint Remote Endpoint information (like MAC address,
1459 * reference URI and connectivity type) to which
1460 * the unicast data has to be sent.
1461 * @param[in] data Data which required to be sent.
1462 * @param[in] dataLen Size of data to be sent.
1464 * @note dataLen must be > 0.
1466 * @return The number of bytes sent on the network, or -1 on error.
1468 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1473 * Send multicast data to the endpoint using the LE connectivity.
1475 * @param[in] endpoint Remote Endpoint information to which the
1476 * multicast data has to be sent.
1477 * @param[in] data Data which required to be sent.
1478 * @param[in] dataLen Size of data to be sent.
1480 * @note dataLen must be > 0.
1482 * @return The number of bytes sent on the network, or -1 on error.
1484 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1489 * Get LE Connectivity network information.
1491 * @param[out] info Local connectivity information structures.
1492 * @param[out] size Number of local connectivity structures.
1494 * @return ::CA_STATUS_OK or Appropriate error code.
1496 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info,
1500 * Read Synchronous API callback.
1502 * @return ::CA_STATUS_OK or Appropriate error code.
1504 static CAResult_t CAReadLEData();
1507 * Stopping the adapters and close socket connections.
1509 * LE Stops all GATT servers and GATT Clients.
1511 * @return ::CA_STATUS_OK or Appropriate error code.
1513 static CAResult_t CAStopLE();
1516 * Terminate the LE connectivity adapter.
1518 * Configuration information will be deleted from further use.
1520 static void CATerminateLE();
1523 * This function will receive the data from the GattServer and add the
1524 * data to the Server receiver queue.
1526 * @param[in] remoteAddress Remote address of the device from where
1528 * @param[in] data Actual data recevied from the remote
1530 * @param[in] dataLength Length of the data received from the
1532 * @param[in] sentLength Length of the data sent from the remote
1535 * @return ::CA_STATUS_OK or Appropriate error code.
1536 * @retval ::CA_STATUS_OK Successful.
1537 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1538 * @retval ::CA_STATUS_FAILED Operation failed.
1541 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
1542 const uint8_t *data,
1543 uint32_t dataLength,
1544 uint32_t *sentLength);
1547 * This function will receive the data from the GattClient and add the
1548 * data into the Client receiver queue.
1550 * @param[in] remoteAddress Remote address of the device from where
1552 * @param[in] data Actual data recevied from the remote
1554 * @param[in] dataLength Length of the data received from the
1556 * @param[in] sentLength Length of the data sent from the remote
1559 * @return ::CA_STATUS_OK or Appropriate error code.
1560 * @retval ::CA_STATUS_OK Successful.
1561 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1562 * @retval ::CA_STATUS_FAILED Operation failed.
1564 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
1565 const uint8_t *data,
1566 uint32_t dataLength,
1567 uint32_t *sentLength);
1570 * Set the NetworkPacket received callback to CA layer from adapter
1573 * @param[in] callback Callback handle sent from the upper layer.
1575 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
1578 * Push the data from CA layer to the Sender processor queue.
1580 * @param[in] remoteEndpoint Remote endpoint information of the
1582 * @param[in] data Data to be transmitted from LE.
1583 * @param[in] dataLen Length of the Data being transmitted.
1585 * @return ::CA_STATUS_OK or Appropriate error code.
1586 * @retval ::CA_STATUS_OK Successful.
1587 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1588 * @retval ::CA_STATUS_FAILED Operation failed.
1590 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1591 const uint8_t *data,
1595 * Push the data from CA layer to the Sender processor queue.
1597 * @param[in] remoteEndpoint Remote endpoint information of the
1599 * @param[in] data Data to be transmitted from LE.
1600 * @param[in] dataLen Length of the Data being transmitted.
1602 * @return ::CA_STATUS_OK or Appropriate error code.
1603 * @retval ::CA_STATUS_OK Successful.
1604 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1605 * @retval ::CA_STATUS_FAILED Operation failed.
1607 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1608 const uint8_t *data,
1611 static CAResult_t CALEAdapterGattServerStart()
1613 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1615 CAResult_t result = CAStartLEGattServer();
1617 #ifndef SINGLE_THREAD
1619 Don't start the server side sending queue thread until the
1620 server itself has actually started.
1622 if (CA_STATUS_OK == result)
1624 result = CAQueueingThreadStart(g_bleServerSendQueueHandle);
1626 if (CA_STATUS_OK != result)
1630 "Unable to start server queuing thread");
1638 static CAResult_t CALEAdapterGattServerStop()
1640 #ifndef SINGLE_THREAD
1641 CAResult_t result = CAQueueingThreadStop(g_bleServerSendQueueHandle);
1643 if (CA_STATUS_OK == result)
1645 result = CAStopLEGattServer();
1650 return CAStopLEGattServer();
1654 static CAResult_t CALEAdapterGattClientStart()
1656 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattClient");
1658 CAResult_t result = CAStartLEGattClient();
1660 #ifndef SINGLE_THREAD
1662 Don't start the client side sending queue thread until the
1663 client itself has actually started.
1665 if (CA_STATUS_OK == result)
1667 result = CAQueueingThreadStart(g_bleClientSendQueueHandle);
1669 if (CA_STATUS_OK != result)
1673 "Unable to start client queuing thread");
1681 static CAResult_t CALEAdapterGattClientStop()
1683 #ifndef SINGLE_THREAD
1684 CAResult_t result = CAQueueingThreadStop(g_bleClientSendQueueHandle);
1686 if (CA_STATUS_OK == result)
1688 CAStopLEGattClient();
1693 CAStopLEGattClient();
1695 return CA_STATUS_OK;
1699 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1700 CANetworkPacketReceivedCallback reqRespCallback,
1701 CANetworkChangeCallback netCallback,
1702 CAErrorHandleCallback errorCallback,
1703 ca_thread_pool_t handle)
1705 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1708 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1709 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1710 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1712 CAResult_t result = CA_STATUS_OK;
1713 result = CAInitLEAdapterMutex();
1714 if (CA_STATUS_OK != result)
1716 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1717 return CA_STATUS_FAILED;
1720 result = CAInitializeLENetworkMonitor();
1721 if (CA_STATUS_OK != result)
1723 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1724 return CA_STATUS_FAILED;
1727 CAInitializeLEAdapter();
1729 CASetLEClientThreadPoolHandle(handle);
1731 result = CAInitializeLEGattClient();
1732 if (CA_STATUS_OK != result)
1734 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattClient() failed");
1735 return CA_STATUS_FAILED;
1738 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1739 CASetLEServerThreadPoolHandle(handle);
1740 result = CAInitializeLEGattServer();
1741 if (CA_STATUS_OK != result)
1743 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattServer() failed");
1744 return CA_STATUS_FAILED;
1747 CASetLEAdapterThreadPoolHandle(handle);
1748 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1749 CASetLEReqRespAdapterCallback(reqRespCallback);
1751 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1752 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1753 CALERegisterNetworkNotifications(netCallback);
1755 g_errorHandler = errorCallback;
1757 static const CAConnectivityHandler_t connHandler =
1759 .startAdapter = CAStartLE,
1760 .stopAdapter = CAStopLE,
1761 .startListenServer = CAStartLEListeningServer,
1762 .stopListenServer = CAStopLEListeningServer,
1763 .startDiscoveryServer = CAStartLEDiscoveryServer,
1764 .sendData = CASendLEUnicastData,
1765 .sendDataToAll = CASendLEMulticastData,
1766 .GetnetInfo = CAGetLEInterfaceInformation,
1767 .readData = CAReadLEData,
1768 .terminate = CATerminateLE
1771 registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
1773 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1775 return CA_STATUS_OK;
1778 static CAResult_t CAStartLE()
1780 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE");
1782 return CAStartLEAdapter();
1785 static CAResult_t CAStopLE()
1787 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1788 #ifndef SINGLE_THREAD
1792 ca_mutex_lock(g_bleIsServerMutex);
1793 if (true == g_isServer)
1795 CALEAdapterGattServerStop();
1799 CALEAdapterGattClientStop();
1801 ca_mutex_unlock(g_bleIsServerMutex);
1803 gLeServerStatus = CA_SERVER_NOTSTARTED;
1805 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1807 return CA_STATUS_OK;
1810 static void CATerminateLE()
1812 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1814 CASetLEReqRespServerCallback(NULL);
1815 CASetLEReqRespClientCallback(NULL);
1816 CALERegisterNetworkNotifications(NULL);
1817 CASetLEReqRespAdapterCallback(NULL);
1818 CATerminateLENetworkMonitor();
1820 ca_mutex_lock(g_bleIsServerMutex);
1821 if (true == g_isServer)
1823 CATerminateLEGattServer();
1827 CATerminateLEGattClient();
1829 ca_mutex_unlock(g_bleIsServerMutex);
1831 #ifndef SINGLE_THREAD
1832 CATerminateLEQueues();
1834 CATerminateLEAdapterMutex();
1836 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1839 static CAResult_t CAStartLEListeningServer()
1841 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1842 #ifndef ROUTING_GATEWAY
1843 CAResult_t result = CA_STATUS_OK;
1844 #ifndef SINGLE_THREAD
1845 result = CAInitLEServerQueues();
1846 if (CA_STATUS_OK != result)
1848 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1849 return CA_STATUS_FAILED;
1853 result = CAGetLEAdapterState();
1854 if (CA_ADAPTER_NOT_ENABLED == result)
1856 gLeServerStatus = CA_LISTENING_SERVER;
1857 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1858 return CA_STATUS_OK;
1861 if (CA_STATUS_FAILED == result)
1863 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1864 return CA_STATUS_FAILED;
1867 result = CALEAdapterGattServerStart();
1869 ca_mutex_lock(g_bleIsServerMutex);
1871 ca_mutex_unlock(g_bleIsServerMutex);
1873 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1876 // Routing Gateway only supports BLE client mode.
1877 OIC_LOG(ERROR, CALEADAPTER_TAG, "LE server not supported in Routing Gateway");
1878 return CA_NOT_SUPPORTED;
1882 static CAResult_t CAStopLEListeningServer()
1884 OIC_LOG(ERROR, CALEADAPTER_TAG, "Listen server stop not supported.");
1885 return CA_NOT_SUPPORTED;
1888 static CAResult_t CAStartLEDiscoveryServer()
1890 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1891 CAResult_t result = CA_STATUS_OK;
1892 #ifndef SINGLE_THREAD
1893 result = CAInitLEClientQueues();
1894 if (CA_STATUS_OK != result)
1896 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1897 return CA_STATUS_FAILED;
1900 result = CAGetLEAdapterState();
1901 if (CA_ADAPTER_NOT_ENABLED == result)
1903 gLeServerStatus = CA_DISCOVERY_SERVER;
1904 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1905 return CA_STATUS_OK;
1908 if (CA_STATUS_FAILED == result)
1910 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1911 return CA_STATUS_FAILED;
1914 result = CALEAdapterGattClientStart();
1916 ca_mutex_lock(g_bleIsServerMutex);
1918 ca_mutex_unlock(g_bleIsServerMutex);
1920 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1924 static CAResult_t CAReadLEData()
1926 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1927 #ifdef SINGLE_THREAD
1930 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1931 return CA_STATUS_OK;
1934 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1938 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1941 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1942 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1944 CAResult_t result = CA_STATUS_FAILED;
1946 ca_mutex_lock(g_bleIsServerMutex);
1947 if (true == g_isServer)
1949 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1950 if (CA_STATUS_OK != result)
1952 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed\n");
1955 g_errorHandler(endpoint, data, dataLen, result);
1957 ca_mutex_unlock(g_bleIsServerMutex);
1963 result = CALEAdapterClientSendData(endpoint, data, dataLen);
1964 if (CA_STATUS_OK != result)
1966 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed \n");
1969 g_errorHandler(endpoint, data, dataLen, result);
1971 ca_mutex_unlock(g_bleIsServerMutex);
1975 ca_mutex_unlock(g_bleIsServerMutex);
1977 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1981 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1985 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1988 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1992 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
1996 CAResult_t result = CA_STATUS_FAILED;
1998 ca_mutex_lock(g_bleIsServerMutex);
1999 if (true == g_isServer)
2001 result = CALEAdapterServerSendData(NULL, data, dataLen);
2002 if (CA_STATUS_OK != result)
2004 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data failed" );
2006 ca_mutex_unlock(g_bleIsServerMutex);
2009 g_errorHandler(endpoint, data, dataLen, result);
2016 result = CALEAdapterClientSendData(NULL, data, dataLen);
2017 if (CA_STATUS_OK != result)
2019 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data failed" );
2022 g_errorHandler(endpoint, data, dataLen, result);
2024 ca_mutex_unlock(g_bleIsServerMutex);
2028 ca_mutex_unlock(g_bleIsServerMutex);
2030 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2034 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
2036 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2038 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
2040 char *local_address = NULL;
2042 CAResult_t res = CAGetLEAddress(&local_address);
2043 if (CA_STATUS_OK != res)
2045 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
2049 if (NULL == local_address)
2051 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
2052 return CA_STATUS_FAILED;
2056 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
2057 if (NULL == (*info))
2059 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
2060 OICFree(local_address);
2061 return CA_STATUS_FAILED;
2064 size_t local_address_len = strlen(local_address);
2066 if(local_address_len >= sizeof(g_localBLEAddress) ||
2067 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
2069 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
2071 OICFree(local_address);
2072 return CA_STATUS_FAILED;
2075 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
2076 ca_mutex_lock(g_bleLocalAddressMutex);
2077 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
2078 ca_mutex_unlock(g_bleLocalAddressMutex);
2080 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
2082 OICFree(local_address);
2084 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2085 return CA_STATUS_OK;
2088 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
2090 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2092 ca_mutex_lock(g_bleNetworkCbMutex);
2093 g_networkCallback = netCallback;
2094 ca_mutex_unlock(g_bleNetworkCbMutex);
2095 CAResult_t res = CA_STATUS_OK;
2098 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
2099 if (CA_STATUS_OK != res)
2101 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2106 res = CAUnSetLEAdapterStateChangedCb();
2107 if (CA_STATUS_OK != res)
2109 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2113 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2117 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
2119 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2121 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
2122 CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE };
2124 ca_mutex_lock(g_bleLocalAddressMutex);
2125 OICStrcpy(localEndpoint.addr,
2126 sizeof(localEndpoint.addr),
2128 ca_mutex_unlock(g_bleLocalAddressMutex);
2130 g_bleAdapterState = adapter_state;
2131 // Start a GattServer/Client if gLeServerStatus is SET
2132 if (CA_LISTENING_SERVER == gLeServerStatus)
2134 const CAResult_t result =
2135 (CA_ADAPTER_ENABLED == adapter_state
2136 ? CALEAdapterGattServerStart()
2137 : CALEAdapterGattServerStop());
2139 if (CA_STATUS_OK != result)
2143 "GATT server failed to %s (%d)",
2144 (CA_ADAPTER_ENABLED == adapter_state
2145 ? "start" : "stop"),
2149 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
2151 const CAResult_t result =
2152 (CA_ADAPTER_ENABLED == adapter_state
2153 ? CALEAdapterGattClientStart()
2154 : CALEAdapterGattClientStop());
2156 if (CA_STATUS_OK != result)
2160 "GATT client failed to %s (%d)",
2161 (CA_ADAPTER_ENABLED == adapter_state
2162 ? "start" : "stop"),
2167 if (CA_ADAPTER_DISABLED == adapter_state)
2169 gLeServerStatus = CA_SERVER_NOTSTARTED;
2172 ca_mutex_lock(g_bleNetworkCbMutex);
2173 if (NULL != g_networkCallback)
2175 g_networkCallback(&localEndpoint, adapter_state);
2179 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
2181 ca_mutex_unlock(g_bleNetworkCbMutex);
2183 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2186 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
2187 const uint8_t *data,
2190 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2192 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2193 #ifndef SINGLE_THREAD
2194 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2195 "g_bleClientSendQueueHandle is NULL",
2197 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
2198 "g_bleClientSendDataMutex is NULL",
2201 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
2204 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
2206 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
2209 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2210 return CA_MEMORY_ALLOC_FAILED;
2212 // Add message to send queue
2213 ca_mutex_lock(g_bleClientSendDataMutex);
2214 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
2215 ca_mutex_unlock(g_bleClientSendDataMutex);
2217 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2218 return CA_STATUS_OK;
2221 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2222 const uint8_t *data,
2225 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2227 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2229 #ifdef SINGLE_THREAD
2230 uint8_t header[CA_HEADER_LENGTH] = { 0 };
2233 CAGenerateHeader(header, CA_HEADER_LENGTH, dataLen);
2235 if (CA_STATUS_OK != result)
2237 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
2238 return CA_STATUS_FAILED;
2241 if (!CAIsLEConnected())
2243 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2244 return CA_STATUS_FAILED;
2247 result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH);
2248 if (CA_STATUS_OK != result)
2250 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2251 return CA_STATUS_FAILED;
2254 const uint32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2255 for (uint32_t iter = 0; iter < dataLimit; iter++)
2258 CAUpdateCharacteristicsToAllGattClients(
2259 data + (iter * CA_SUPPORTED_BLE_MTU_SIZE),
2260 CA_SUPPORTED_BLE_MTU_SIZE);
2262 if (CA_STATUS_OK != result)
2264 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2265 return CA_STATUS_FAILED;
2271 const uint32_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2275 CAUpdateCharacteristicsToAllGattClients(
2276 data + (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE),
2278 if (CA_STATUS_OK != result)
2280 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2281 return CA_STATUS_FAILED;
2286 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2287 "BleClientReceiverQueue is NULL",
2289 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2290 "BleClientSendDataMutex is NULL",
2293 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2296 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2298 CALEData_t * const bleData =
2299 CACreateLEData(remoteEndpoint, data, dataLen);
2303 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2304 return CA_MEMORY_ALLOC_FAILED;
2307 // Add message to send queue
2308 ca_mutex_lock(g_bleServerSendDataMutex);
2309 CAQueueingThreadAddData(g_bleServerSendQueueHandle,
2311 sizeof(CALEData_t));
2312 ca_mutex_unlock(g_bleServerSendDataMutex);
2314 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2315 return CA_STATUS_OK;
2318 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2319 const uint8_t *data,
2320 uint32_t dataLength,
2321 uint32_t *sentLength)
2323 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2326 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2327 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2329 #ifdef SINGLE_THREAD
2330 if(g_networkPacketReceivedCallback)
2332 // will be filled by upper layer
2333 const CASecureEndpoint_t endpoint =
2334 { .endpoint = { .adapter = CA_ADAPTER_GATT_BTLE } };
2337 g_networkPacketReceivedCallback(&endpoint, data, dataLength);
2340 VERIFY_NON_NULL_RET(g_bleReceiverQueue,
2342 "g_bleReceiverQueue",
2345 //Add message to data queue
2346 CAEndpoint_t * const remoteEndpoint =
2347 CACreateEndpointObject(CA_DEFAULT_FLAGS,
2348 CA_ADAPTER_GATT_BTLE,
2352 if (NULL == remoteEndpoint)
2354 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2355 return CA_STATUS_FAILED;
2358 // Create bleData to add to queue
2361 "Data received from LE layer [%d]",
2364 CALEData_t * const bleData =
2365 CACreateLEData(remoteEndpoint, data, dataLength);
2369 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2370 CAFreeEndpoint(remoteEndpoint);
2371 return CA_MEMORY_ALLOC_FAILED;
2374 CAFreeEndpoint(remoteEndpoint);
2375 // Add message to receiver queue
2376 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2378 *sentLength = dataLength;
2380 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2381 return CA_STATUS_OK;
2384 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2385 const uint8_t *data,
2386 uint32_t dataLength,
2387 uint32_t *sentLength)
2389 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2392 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2393 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2394 #ifndef SINGLE_THREAD
2395 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG, "g_bleReceiverQueue",
2398 //Add message to data queue
2399 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2400 CA_ADAPTER_GATT_BTLE,
2402 if (NULL == remoteEndpoint)
2404 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2405 return CA_STATUS_FAILED;
2408 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%u]", dataLength);
2410 // Create bleData to add to queue
2411 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
2414 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2415 CAFreeEndpoint(remoteEndpoint);
2416 return CA_MEMORY_ALLOC_FAILED;
2419 CAFreeEndpoint(remoteEndpoint);
2420 // Add message to receiver queue
2421 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2423 *sentLength = dataLength;
2425 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2426 return CA_STATUS_OK;
2429 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2431 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2433 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2434 g_bleAdapterThreadPool = handle;
2435 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2437 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2440 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2442 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2444 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2446 g_networkPacketReceivedCallback = callback;
2448 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2450 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2453 static void CALEErrorHandler(const char *remoteAddress,
2454 const uint8_t *data,
2458 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2460 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2462 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2463 CA_ADAPTER_GATT_BTLE,
2467 // if required, will be used to build remote endpoint
2468 g_errorHandler(rep, data, dataLen, result);
2470 CAFreeEndpoint(rep);
2472 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");