1 /* ****************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
20 #include "caleadapter.h"
25 #include "caleinterface.h"
28 #include "caadapterutils.h"
30 #include "caqueueingthread.h"
32 #include "cafragmentation.h"
33 #include "oic_malloc.h"
34 #include "oic_string.h"
35 #include "caremotehandler.h"
38 * Logging tag for module name.
40 #define CALEADAPTER_TAG "OIC_LE_ADAP"
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;
78 ADAPTER_BOTH_CLIENT_SERVER,
84 * Callback to provide the status of the network change to CA layer.
86 static CANetworkChangeCallback g_networkCallback = NULL;
89 * bleAddress of the local adapter. Value will be initialized to zero,
90 * and will be updated later.
92 static char g_localBLEAddress[18] = { 0 };
95 * Variable to differentiate btw GattServer and GattClient.
97 static CABLEAdapter_t g_adapterType = ADAPTER_EMPTY;
100 * Mutex to synchronize the task to be executed on the GattServer
103 static ca_mutex g_bleIsServerMutex = NULL;
106 * Mutex to synchronize the callback to be called for the network
109 static ca_mutex g_bleNetworkCbMutex = NULL;
112 * Mutex to synchronize the updates of the local LE address of the
115 static ca_mutex g_bleLocalAddressMutex = NULL;
118 * Reference to thread pool.
120 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
123 * Mutex to synchronize the task to be pushed to thread pool.
125 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
128 * Mutex to synchronize the queing of the data from SenderQueue.
130 static ca_mutex g_bleClientSendDataMutex = NULL;
133 * Mutex to synchronize the queing of the data from ReceiverQueue.
135 static ca_mutex g_bleReceiveDataMutex = NULL;
139 * Mutex to synchronize the queing of the data from SenderQueue.
141 static ca_mutex g_bleServerSendDataMutex = NULL;
144 * Mutex to synchronize the callback to be called for the
145 * adapterReqResponse.
147 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
150 * Callback to be called when network packet received from either
151 * GattServer or GattClient.
153 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
156 * Callback to notify error from the BLE adapter.
158 static CAErrorHandleCallback g_errorHandler = NULL;
161 * Register network change notification callback.
163 * @param[in] netCallback CANetworkChangeCallback callback which will
164 * be set for the change in network.
166 * @return 0 on success otherwise a positive error value.
167 * @retval ::CA_STATUS_OK Successful.
168 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
169 * @retval ::CA_STATUS_FAILED Operation failed.
172 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
175 * Set the thread pool handle which is required for spawning new
178 * @param[in] handle Thread pool handle which is given by above layer
179 * for using thread creation task.
182 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
185 * Call the callback to the upper layer when the device state gets
188 * @param[in] adapter_state New state of the adapter to be notified to
191 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state);
194 * Used to initialize all required mutex variable for LE Adapter
197 * @return 0 on success otherwise a positive error value.
198 * @retval ::CA_STATUS_OK Successful.
199 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
200 * @retval ::CA_STATUS_FAILED Operation failed.
203 static CAResult_t CAInitLEAdapterMutex();
206 * Terminate all required mutex variables for LE adapter
209 static void CATerminateLEAdapterMutex();
212 * Prepares and notify error through error callback.
214 static void CALEErrorHandler(const char *remoteAddress,
219 #ifndef SINGLE_THREAD
221 * Stop condition of recvhandler.
223 static bool g_dataReceiverHandlerState = false;
226 * Sender information.
228 static u_arraylist_t *g_senderInfo = NULL;
231 * Queue to process the outgoing packets from GATTClient.
233 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
236 * Queue to process the incoming packets to GATT Client.
238 static CAQueueingThread_t *g_bleReceiverQueue = NULL;
241 * Queue to process the outgoing packets from GATTServer.
243 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
246 * This function will be associated with the sender queue for
249 * This function will fragment the data to the MTU of the transport
250 * and send the data in fragments to the adapters. The function will
251 * be blocked until all data is sent out from the adapter.
253 * @param[in] threadData Data pushed to the queue which contains the
254 * info about RemoteEndpoint and Data.
256 static void CALEServerSendDataThread(void *threadData);
259 * This function will be associated with the sender queue for
262 * This function will fragment the data to the MTU of the transport
263 * and send the data in fragments to the adapters. The function will
264 * be blocked until all data is sent out from the adapter.
266 * @param[in] threadData Data pushed to the queue which contains the
267 * info about RemoteEndpoint and Data.
269 static void CALEClientSendDataThread(void *threadData);
272 * This function will be associated with the receiver queue.
274 * This function will defragment the received data from each sender
275 * respectively and will send it up to CA layer. Respective sender's
276 * header will provide the length of the data sent.
278 * @param[in] threadData Data pushed to the queue which contains the
279 * info about RemoteEndpoint and Data.
281 static void CALEDataReceiverHandler(void *threadData);
284 * This function will stop all queues created for GattServer and
285 * GattClient. All four queues will be be stopped with this function
288 static void CAStopLEQueues();
291 * This function will terminate all queues created for GattServer and
292 * GattClient. All four queues will be be terminated with this
293 * function invocations.
295 static void CATerminateLEQueues();
298 * This function will initalize the Receiver and Sender queues for
299 * GattServer. This function will in turn call the functions
300 * CAInitBleServerReceiverQueue() and CAInitBleServerSenderQueue() to
301 * initialize the queues.
303 * @return ::CA_STATUS_OK or Appropriate error code.
304 * @retval ::CA_STATUS_OK Successful.
305 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
306 * @retval ::CA_STATUS_FAILED Operation failed.
308 static CAResult_t CAInitLEServerQueues();
311 * This function will initalize the Receiver and Sender queues for
312 * GattClient. This function will inturn call the functions
313 * CAInitBleClientReceiverQueue() and CAInitBleClientSenderQueue() to
314 * initialize the queues.
316 * @return ::CA_STATUS_OK or Appropriate error code.
317 * @retval ::CA_STATUS_OK Successful.
318 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
319 * @retval ::CA_STATUS_FAILED Operation failed.
322 static CAResult_t CAInitLEClientQueues();
325 * This function will initalize the Receiver queue for
326 * GattServer. This will initialize the queue to process the function
327 * CABLEServerSendDataThread() when ever the task is added to this
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.
335 static CAResult_t CAInitLEServerSenderQueue();
338 * This function will initalize the Receiver queue for
339 * GattClient. This will initialize the queue to process the function
340 * CABLEClientSendDataThread() when ever the task is added to this
343 * @return ::CA_STATUS_OK or Appropriate error code.
344 * @retval ::CA_STATUS_OK Successful.
345 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
346 * @retval ::CA_STATUS_FAILED Operation failed.
348 static CAResult_t CAInitLEClientSenderQueue();
351 * This function will initialize the Receiver queue for
352 * LEAdapter. This will initialize the queue to process the function
353 * CABLEDataReceiverHandler() when ever the task is added to this
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
362 static CAResult_t CAInitLEReceiverQueue();
365 * This function will create the Data required to send it in the
368 * @param[in] remoteEndpoint Remote endpoint information of the
370 * @param[in] data Data to be transmitted from LE.
371 * @param[in] dataLength Length of the Data being transmitted.
373 * @return ::CA_STATUS_OK or Appropriate error code.
374 * @retval ::CA_STATUS_OK Successful.
375 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
376 * @retval ::CA_STATUS_FAILED Operation failed.
378 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
380 uint32_t dataLength);
383 * Used to free the BLE information stored in the sender/receiver
386 * @param[in] bleData Information for a particular data segment.
388 static void CAFreeLEData(CALEData_t *bleData);
393 static void CALEDataDestroyer(void *data, uint32_t size);
395 static CAResult_t CAInitLEServerQueues()
397 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
399 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
401 CAResult_t result = CAInitLEServerSenderQueue();
402 if (CA_STATUS_OK != result)
404 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
405 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
406 return CA_STATUS_FAILED;
409 result = CAInitLEReceiverQueue();
410 if (CA_STATUS_OK != result)
412 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerReceiverQueue failed");
413 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
414 return CA_STATUS_FAILED;
417 g_dataReceiverHandlerState = true;
419 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
421 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
425 static CAResult_t CAInitLEClientQueues()
427 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
429 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
431 CAResult_t result = CAInitLEClientSenderQueue();
432 if (CA_STATUS_OK != result)
434 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
435 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
436 return CA_STATUS_FAILED;
439 result = CAInitLEReceiverQueue();
440 if (CA_STATUS_OK != result)
442 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientReceiverQueue failed");
443 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
444 return CA_STATUS_FAILED;
447 g_dataReceiverHandlerState = true;
449 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
451 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
455 static CAResult_t CAInitLEReceiverQueue()
457 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
458 // Check if the message queue is already initialized
459 if (g_bleReceiverQueue)
461 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
465 // Create recv message queue
466 g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
467 if (!g_bleReceiverQueue)
469 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
470 return CA_MEMORY_ALLOC_FAILED;
473 g_senderInfo = u_arraylist_create();
476 OIC_LOG(ERROR, CALEADAPTER_TAG, "ClientInfo memory allcation failed!");
477 OICFree(g_bleReceiverQueue);
478 g_bleReceiverQueue = NULL;
479 return CA_MEMORY_ALLOC_FAILED;
482 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue, g_bleAdapterThreadPool,
483 CALEDataReceiverHandler, CALEDataDestroyer))
485 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
486 OICFree(g_bleReceiverQueue);
487 g_bleReceiverQueue = NULL;
488 u_arraylist_free(&g_senderInfo);
489 return CA_STATUS_FAILED;
492 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue))
494 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
495 OICFree(g_bleReceiverQueue);
496 g_bleReceiverQueue = NULL;
497 u_arraylist_free(&g_senderInfo);
498 return CA_STATUS_FAILED;
501 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
505 static CAResult_t CAInitLEServerSenderQueue()
507 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
508 // Check if the message queue is already initialized
509 if (g_bleServerSendQueueHandle)
511 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
515 // Create send message queue
516 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
517 if (!g_bleServerSendQueueHandle)
519 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
520 return CA_MEMORY_ALLOC_FAILED;
523 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
524 g_bleAdapterThreadPool,
525 CALEServerSendDataThread, CALEDataDestroyer))
527 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
528 OICFree(g_bleServerSendQueueHandle);
529 g_bleServerSendQueueHandle = NULL;
530 return CA_STATUS_FAILED;
533 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
537 static void CALEClearSenderInfo()
539 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
541 uint32_t listIndex = 0;
542 uint32_t listLength = u_arraylist_length(g_senderInfo);
543 for (listIndex = 0; listIndex < listLength; listIndex++)
545 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(g_senderInfo, listIndex);
551 OICFree(info->defragData);
552 CAFreeEndpoint(info->remoteEndpoint);
555 u_arraylist_free(&g_senderInfo);
556 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
559 static CAResult_t CAInitLEClientSenderQueue()
561 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
563 if (g_bleClientSendQueueHandle)
565 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
569 // Create send message queue
570 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
571 if (!g_bleClientSendQueueHandle)
573 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
574 return CA_MEMORY_ALLOC_FAILED;
577 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
578 g_bleAdapterThreadPool,
579 CALEClientSendDataThread, CALEDataDestroyer))
581 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
582 OICFree(g_bleClientSendQueueHandle);
583 g_bleClientSendQueueHandle = NULL;
584 return CA_STATUS_FAILED;
587 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
591 static void CAStopLEQueues()
593 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
595 ca_mutex_lock(g_bleReceiveDataMutex);
596 if (NULL != g_bleReceiverQueue)
598 CAQueueingThreadStop(g_bleReceiverQueue);
600 ca_mutex_unlock(g_bleReceiveDataMutex);
602 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
605 static void CATerminateLEQueues()
607 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
609 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
610 OICFree(g_bleClientSendQueueHandle);
611 g_bleClientSendQueueHandle = NULL;
613 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
614 OICFree(g_bleServerSendQueueHandle);
615 g_bleServerSendQueueHandle = NULL;
617 CAQueueingThreadDestroy(g_bleReceiverQueue);
618 OICFree(g_bleReceiverQueue);
619 g_bleReceiverQueue = NULL;
621 CALEClearSenderInfo();
623 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
626 static CAResult_t CALEGetSenderInfo(const char *leAddress,
627 CABLESenderInfo_t **senderInfo,
628 uint32_t *senderIndex)
630 VERIFY_NON_NULL_RET(leAddress,
632 "NULL BLE address argument",
633 CA_STATUS_INVALID_PARAM);
634 VERIFY_NON_NULL_RET(senderIndex,
636 "NULL index argument",
637 CA_STATUS_INVALID_PARAM);
639 const uint32_t listLength = u_arraylist_length(g_senderInfo);
640 const uint32_t addrLength = strlen(leAddress);
641 for (uint32_t index = 0; index < listLength; index++)
643 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(g_senderInfo, index);
644 if(!info || !(info->remoteEndpoint))
649 if(!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
651 *senderIndex = index;
660 return CA_STATUS_FAILED;
663 static void CALEDataReceiverHandler(void *threadData)
665 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
667 ca_mutex_lock(g_bleReceiveDataMutex);
669 if (g_dataReceiverHandlerState)
671 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
673 CALEData_t *bleData = (CALEData_t *) threadData;
676 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
677 ca_mutex_unlock(g_bleReceiveDataMutex);
681 if(!(bleData->remoteEndpoint))
683 OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!");
684 ca_mutex_unlock(g_bleReceiveDataMutex);
688 CABLESenderInfo_t *senderInfo = NULL;
689 uint32_t senderIndex = 0;
691 if(CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
692 &senderInfo, &senderIndex))
694 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s]",
695 bleData->remoteEndpoint->addr);
700 CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
703 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
704 ca_mutex_unlock(g_bleReceiveDataMutex);
707 newSender->recvDataLen = 0;
708 newSender->totalDataLen = 0;
709 newSender->defragData = NULL;
710 newSender->remoteEndpoint = NULL;
712 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
713 newSender->totalDataLen = CAParseHeader(bleData->data,
715 if(!(newSender->totalDataLen))
717 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
719 ca_mutex_unlock(g_bleReceiveDataMutex);
723 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
724 newSender->totalDataLen);
725 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
728 newSender->defragData = OICCalloc(newSender->totalDataLen + 1,
729 sizeof(*newSender->defragData));
731 if (NULL == newSender->defragData)
733 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
735 ca_mutex_unlock(g_bleReceiveDataMutex);
739 const char *remoteAddress = bleData->remoteEndpoint->addr;
740 newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
741 CA_ADAPTER_GATT_BTLE, remoteAddress, 0);
742 if (NULL == newSender->remoteEndpoint)
744 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
745 OICFree(newSender->defragData);
747 ca_mutex_unlock(g_bleReceiveDataMutex);
750 memcpy(newSender->defragData, bleData->data + CA_HEADER_LENGTH,
751 bleData->dataLen - CA_HEADER_LENGTH);
752 newSender->recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
753 u_arraylist_add(g_senderInfo,(void *)newSender);
755 //Getting newSender index position in g_senderInfo array list
757 CALEGetSenderInfo(newSender->remoteEndpoint->addr, NULL, &senderIndex))
759 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
760 OICFree(newSender->defragData);
761 CAFreeEndpoint(newSender->remoteEndpoint);
763 ca_mutex_unlock(g_bleReceiveDataMutex);
766 senderInfo = newSender;
770 if(senderInfo->recvDataLen + bleData->dataLen > senderInfo->totalDataLen)
772 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
773 "Data Length exceeding error!! Receiving [%d] total length [%d]",
774 senderInfo->recvDataLen + bleData->dataLen, senderInfo->totalDataLen);
775 u_arraylist_remove(g_senderInfo, senderIndex);
776 OICFree(senderInfo->defragData);
778 ca_mutex_unlock(g_bleReceiveDataMutex);
781 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
782 memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data,
784 senderInfo->recvDataLen += bleData->dataLen ;
785 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
786 senderInfo->totalDataLen, senderInfo->recvDataLen);
789 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
791 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
792 if (NULL == g_networkPacketReceivedCallback)
794 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
796 u_arraylist_remove(g_senderInfo, senderIndex);
797 OICFree(senderInfo->defragData);
799 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
800 ca_mutex_unlock(g_bleReceiveDataMutex);
804 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
806 const CASecureEndpoint_t tmp =
808 .endpoint = *senderInfo->remoteEndpoint
811 g_networkPacketReceivedCallback(&tmp,
812 senderInfo->defragData,
813 senderInfo->recvDataLen);
814 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
815 u_arraylist_remove(g_senderInfo, senderIndex);
816 senderInfo->remoteEndpoint = NULL;
817 senderInfo->defragData = NULL;
821 ca_mutex_unlock(g_bleReceiveDataMutex);
822 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
825 static void CALEServerSendDataThread(void *threadData)
827 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
829 CALEData_t * const bleData = (CALEData_t *) threadData;
832 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
836 uint8_t * const header = OICCalloc(CA_HEADER_LENGTH, 1);
837 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
839 const uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
843 "Server total Data length with header is [%u]",
846 uint8_t * const dataSegment = OICCalloc(totalLength, 1);
848 if (NULL == dataSegment)
850 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
855 CAResult_t result = CAGenerateHeader(header,
858 if (CA_STATUS_OK != result )
860 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
862 OICFree(dataSegment);
866 memcpy(dataSegment, header, CA_HEADER_LENGTH);
870 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
872 length = totalLength;
873 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
877 length = CA_SUPPORTED_BLE_MTU_SIZE;
878 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
879 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
882 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
885 // Send the first segment with the header.
886 if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
888 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
890 result = CAUpdateCharacteristicsToGattClient(
891 bleData->remoteEndpoint->addr, dataSegment, length);
893 if (CA_STATUS_OK != result)
897 "Update characteristics failed, result [%d]",
900 g_errorHandler(bleData->remoteEndpoint,
904 OICFree(dataSegment);
910 "Server Sent data length [%u]",
912 for (index = 1; index < iter; index++)
914 // Send the remaining header.
917 "Sending the chunk number [%u]",
921 CAUpdateCharacteristicsToGattClient(
922 bleData->remoteEndpoint->addr,
923 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
924 CA_SUPPORTED_BLE_MTU_SIZE);
925 if (CA_STATUS_OK != result)
927 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
928 "Update characteristics failed, result [%d]", result);
929 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
930 OICFree(dataSegment);
933 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
934 CA_SUPPORTED_BLE_MTU_SIZE);
937 const uint32_t remainingLen =
938 totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
940 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
942 // send the last segment of the data (Ex: 22 bytes of 622
943 // bytes of data when MTU is 200)
944 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
945 result = CAUpdateCharacteristicsToGattClient(
946 bleData->remoteEndpoint->addr,
947 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
949 if (CA_STATUS_OK != result)
953 "Update characteristics failed, result [%d]",
955 g_errorHandler(bleData->remoteEndpoint,
959 OICFree(dataSegment);
962 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
967 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
968 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
969 if (CA_STATUS_OK != result)
971 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
973 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
974 OICFree(dataSegment);
977 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
978 for (index = 1; index < iter; index++)
980 // Send the remaining header.
981 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
982 result = CAUpdateCharacteristicsToAllGattClients(
983 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
984 CA_SUPPORTED_BLE_MTU_SIZE);
985 if (CA_STATUS_OK != result)
987 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
989 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
990 OICFree(dataSegment);
993 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%u]", CA_SUPPORTED_BLE_MTU_SIZE);
996 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
997 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
999 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1000 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1001 result = CAUpdateCharacteristicsToAllGattClients(
1002 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1004 if (CA_STATUS_OK != result)
1006 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1008 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1009 OICFree(dataSegment);
1012 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1015 OICFree(dataSegment);
1017 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1020 static void CALEClientSendDataThread(void *threadData)
1022 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1024 CALEData_t *bleData = (CALEData_t *) threadData;
1027 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1031 uint8_t * const header = OICCalloc(CA_HEADER_LENGTH, 1);
1032 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1034 const uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1035 uint8_t *dataSegment = OICCalloc(totalLength, 1);
1036 if (NULL == dataSegment)
1038 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1043 CAResult_t result = CAGenerateHeader(header,
1046 if (CA_STATUS_OK != result )
1048 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1050 OICFree(dataSegment);
1053 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1056 uint32_t length = 0;
1057 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1059 length = totalLength;
1060 memcpy(dataSegment + CA_HEADER_LENGTH,
1066 length = CA_SUPPORTED_BLE_MTU_SIZE;
1067 memcpy(dataSegment + CA_HEADER_LENGTH,
1069 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1072 const uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1074 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1076 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1077 // Send the first segment with the header.
1079 CAUpdateCharacteristicsToGattServer(
1080 bleData->remoteEndpoint->addr,
1086 if (CA_STATUS_OK != result)
1090 "Update characteristics failed, result [%d]",
1092 g_errorHandler(bleData->remoteEndpoint,
1096 OICFree(dataSegment);
1102 "Client Sent Data length is [%u]",
1105 for (index = 1; index < iter; index++)
1107 // Send the remaining header.
1108 result = CAUpdateCharacteristicsToGattServer(
1109 bleData->remoteEndpoint->addr,
1110 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1111 CA_SUPPORTED_BLE_MTU_SIZE,
1113 if (CA_STATUS_OK != result)
1117 "Update characteristics failed, result [%d]",
1119 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1120 OICFree(dataSegment);
1123 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1124 CA_SUPPORTED_BLE_MTU_SIZE);
1127 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1128 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1130 // send the last segment of the data (Ex: 22 bytes of 622
1131 // bytes of data when MTU is 200)
1132 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1133 result = CAUpdateCharacteristicsToGattServer(
1134 bleData->remoteEndpoint->addr,
1135 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1139 if (CA_STATUS_OK != result)
1141 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1143 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1144 OICFree(dataSegment);
1147 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1152 //Sending Mulitcast Data
1153 // Send the first segment with the header.
1154 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1155 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1156 if (CA_STATUS_OK != result)
1158 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1159 "Update characteristics (all) failed, result [%d]", result);
1160 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1161 OICFree(dataSegment);
1164 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1165 // Send the remaining header.
1166 for (index = 1; index < iter; index++)
1168 result = CAUpdateCharacteristicsToAllGattServers(
1169 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1170 CA_SUPPORTED_BLE_MTU_SIZE);
1171 if (CA_STATUS_OK != result)
1173 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]",
1175 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1176 OICFree(dataSegment);
1179 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1180 CA_SUPPORTED_BLE_MTU_SIZE);
1183 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1184 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1186 // send the last segment of the data (Ex: 22 bytes of 622
1187 // bytes of data when MTU is 200)
1188 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1190 CAUpdateCharacteristicsToAllGattServers(
1191 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1193 if (CA_STATUS_OK != result)
1195 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1196 "Update characteristics (all) failed, result [%d]", result);
1197 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1198 OICFree(dataSegment);
1201 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1206 OICFree(dataSegment);
1208 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1211 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
1212 const uint8_t *data,
1213 uint32_t dataLength)
1215 CALEData_t * const bleData = OICMalloc(sizeof(CALEData_t));
1219 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1223 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1224 bleData->data = OICCalloc(dataLength + 1, 1);
1226 if (NULL == bleData->data)
1228 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1229 CAFreeLEData(bleData);
1233 memcpy(bleData->data, data, dataLength);
1234 bleData->dataLen = dataLength;
1239 static void CAFreeLEData(CALEData_t *bleData)
1241 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1243 CAFreeEndpoint(bleData->remoteEndpoint);
1244 OICFree(bleData->data);
1248 static void CALEDataDestroyer(void *data, uint32_t size)
1250 if ((size_t)size < sizeof(CALEData_t *))
1252 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1253 "Destroy data too small %p %d", data, size);
1255 CALEData_t *ledata = (CALEData_t *) data;
1257 CAFreeLEData(ledata);
1261 static CAResult_t CAInitLEAdapterMutex()
1263 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1265 if (NULL == g_bleIsServerMutex)
1267 g_bleIsServerMutex = ca_mutex_new();
1268 if (NULL == g_bleIsServerMutex)
1270 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1271 return CA_STATUS_FAILED;
1275 if (NULL == g_bleNetworkCbMutex)
1277 g_bleNetworkCbMutex = ca_mutex_new();
1278 if (NULL == g_bleNetworkCbMutex)
1280 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1281 CATerminateLEAdapterMutex();
1282 return CA_STATUS_FAILED;
1286 if (NULL == g_bleLocalAddressMutex)
1288 g_bleLocalAddressMutex = ca_mutex_new();
1289 if (NULL == g_bleLocalAddressMutex)
1291 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1292 CATerminateLEAdapterMutex();
1293 return CA_STATUS_FAILED;
1297 if (NULL == g_bleAdapterThreadPoolMutex)
1299 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1300 if (NULL == g_bleAdapterThreadPoolMutex)
1302 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1303 CATerminateLEAdapterMutex();
1304 return CA_STATUS_FAILED;
1308 if (NULL == g_bleClientSendDataMutex)
1310 g_bleClientSendDataMutex = ca_mutex_new();
1311 if (NULL == g_bleClientSendDataMutex)
1313 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1314 CATerminateLEAdapterMutex();
1315 return CA_STATUS_FAILED;
1319 if (NULL == g_bleServerSendDataMutex)
1321 g_bleServerSendDataMutex = ca_mutex_new();
1322 if (NULL == g_bleServerSendDataMutex)
1324 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1325 CATerminateLEAdapterMutex();
1326 return CA_STATUS_FAILED;
1330 if (NULL == g_bleAdapterReqRespCbMutex)
1332 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1333 if (NULL == g_bleAdapterReqRespCbMutex)
1335 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1336 CATerminateLEAdapterMutex();
1337 return CA_STATUS_FAILED;
1341 if (NULL == g_bleReceiveDataMutex)
1343 g_bleReceiveDataMutex = ca_mutex_new();
1344 if (NULL == g_bleReceiveDataMutex)
1346 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1347 return CA_STATUS_FAILED;
1351 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1352 return CA_STATUS_OK;
1355 static void CATerminateLEAdapterMutex()
1357 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1359 ca_mutex_free(g_bleIsServerMutex);
1360 g_bleIsServerMutex = NULL;
1362 ca_mutex_free(g_bleNetworkCbMutex);
1363 g_bleNetworkCbMutex = NULL;
1365 ca_mutex_free(g_bleLocalAddressMutex);
1366 g_bleLocalAddressMutex = NULL;
1368 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1369 g_bleAdapterThreadPoolMutex = NULL;
1371 ca_mutex_free(g_bleClientSendDataMutex);
1372 g_bleClientSendDataMutex = NULL;
1374 ca_mutex_free(g_bleServerSendDataMutex);
1375 g_bleServerSendDataMutex = NULL;
1377 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1378 g_bleAdapterReqRespCbMutex = NULL;
1380 ca_mutex_free(g_bleReceiveDataMutex);
1381 g_bleReceiveDataMutex = NULL;
1383 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1387 * Starting LE connectivity adapters.
1389 * As its peer to peer it does not require to start any servers.
1391 * @return ::CA_STATUS_OK or Appropriate error code.
1393 static CAResult_t CAStartLE();
1396 * Start listening server for receiving multicast search requests.
1398 * Transport Specific Behavior:
1399 * LE Starts GATT Server with prefixed UUID and Characteristics
1400 * per OIC Specification.
1401 * @return ::CA_STATUS_OK or Appropriate error code.
1403 static CAResult_t CAStartLEListeningServer();
1406 * Stops listening server from receiving multicast search requests.
1408 * Transport Specific Behavior:
1409 * LE Starts GATT Server with prefixed UUID and Characteristics
1410 * per OIC Specification.
1411 * @return ::CA_STATUS_OK or Appropriate error code.
1413 static CAResult_t CAStopLEListeningServer();
1416 * Sarting discovery of servers for receiving multicast
1419 * Transport Specific Behavior:
1420 * LE Starts GATT Server with prefixed UUID and Characteristics
1421 * per OIC Specification.
1423 * @return ::CA_STATUS_OK or Appropriate error code
1425 static CAResult_t CAStartLEDiscoveryServer();
1428 * Send data to the endpoint using the adapter connectivity.
1430 * @param[in] endpoint Remote Endpoint information (like MAC address,
1431 * reference URI and connectivity type) to which
1432 * the unicast data has to be sent.
1433 * @param[in] data Data which required to be sent.
1434 * @param[in] dataLen Size of data to be sent.
1436 * @note dataLen must be > 0.
1438 * @return The number of bytes sent on the network, or -1 on error.
1440 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1445 * Send multicast data to the endpoint using the LE connectivity.
1447 * @param[in] endpoint Remote Endpoint information to which the
1448 * multicast data has to be sent.
1449 * @param[in] data Data which required to be sent.
1450 * @param[in] dataLen Size of data to be sent.
1452 * @note dataLen must be > 0.
1454 * @return The number of bytes sent on the network, or -1 on error.
1456 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1461 * Get LE Connectivity network information.
1463 * @param[out] info Local connectivity information structures.
1464 * @param[out] size Number of local connectivity structures.
1466 * @return ::CA_STATUS_OK or Appropriate error code.
1468 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info,
1472 * Read Synchronous API callback.
1474 * @return ::CA_STATUS_OK or Appropriate error code.
1476 static CAResult_t CAReadLEData();
1479 * Stopping the adapters and close socket connections.
1481 * LE Stops all GATT servers and GATT Clients.
1483 * @return ::CA_STATUS_OK or Appropriate error code.
1485 static CAResult_t CAStopLE();
1488 * Terminate the LE connectivity adapter.
1490 * Configuration information will be deleted from further use.
1492 static void CATerminateLE();
1495 * This function will receive the data from the GattServer and add the
1496 * data to the Server receiver queue.
1498 * @param[in] remoteAddress Remote address of the device from where
1500 * @param[in] data Actual data recevied from the remote
1502 * @param[in] dataLength Length of the data received from the
1504 * @param[in] sentLength Length of the data sent from the remote
1507 * @return ::CA_STATUS_OK or Appropriate error code.
1508 * @retval ::CA_STATUS_OK Successful.
1509 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1510 * @retval ::CA_STATUS_FAILED Operation failed.
1513 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
1514 const uint8_t *data,
1515 uint32_t dataLength,
1516 uint32_t *sentLength);
1519 * This function will receive the data from the GattClient and add the
1520 * data into the Client receiver queue.
1522 * @param[in] remoteAddress Remote address of the device from where
1524 * @param[in] data Actual data recevied from the remote
1526 * @param[in] dataLength Length of the data received from the
1528 * @param[in] sentLength Length of the data sent from the remote
1531 * @return ::CA_STATUS_OK or Appropriate error code.
1532 * @retval ::CA_STATUS_OK Successful.
1533 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1534 * @retval ::CA_STATUS_FAILED Operation failed.
1536 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
1537 const uint8_t *data,
1538 uint32_t dataLength,
1539 uint32_t *sentLength);
1542 * Set the NetworkPacket received callback to CA layer from adapter
1545 * @param[in] callback Callback handle sent from the upper layer.
1547 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
1550 * Push the data from CA layer to the Sender processor queue.
1552 * @param[in] remoteEndpoint Remote endpoint information of the
1554 * @param[in] data Data to be transmitted from LE.
1555 * @param[in] dataLen Length of the Data being transmitted.
1557 * @return ::CA_STATUS_OK or Appropriate error code.
1558 * @retval ::CA_STATUS_OK Successful.
1559 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1560 * @retval ::CA_STATUS_FAILED Operation failed.
1562 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
1563 const uint8_t *data,
1567 * Push the data from CA layer to the Sender processor queue.
1569 * @param[in] remoteEndpoint Remote endpoint information of the
1571 * @param[in] data Data to be transmitted from LE.
1572 * @param[in] dataLen Length of the Data being transmitted.
1574 * @return ::CA_STATUS_OK or Appropriate error code.
1575 * @retval ::CA_STATUS_OK Successful.
1576 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
1577 * @retval ::CA_STATUS_FAILED Operation failed.
1579 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1580 const uint8_t *data,
1583 static CAResult_t CALEAdapterGattServerStart()
1585 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1587 CAResult_t result = CAStartLEGattServer();
1589 #ifndef SINGLE_THREAD
1591 Don't start the server side sending queue thread until the
1592 server itself has actually started.
1594 if (CA_STATUS_OK == result)
1596 ca_mutex_lock(g_bleServerSendDataMutex);
1597 result = CAQueueingThreadStart(g_bleServerSendQueueHandle);
1598 ca_mutex_unlock(g_bleServerSendDataMutex);
1600 if (CA_STATUS_OK != result)
1604 "Unable to start server queuing thread (%d)",
1612 "GATT server failed to start (%d)",
1620 static CAResult_t CALEAdapterGattServerStop()
1622 #ifndef SINGLE_THREAD
1623 ca_mutex_lock(g_bleServerSendDataMutex);
1624 CAResult_t result = CAQueueingThreadStop(g_bleServerSendQueueHandle);
1625 ca_mutex_unlock(g_bleServerSendDataMutex);
1627 if (CA_STATUS_OK == result)
1629 result = CAStopLEGattServer();
1634 return CAStopLEGattServer();
1638 static CAResult_t CALEAdapterGattClientStart()
1640 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattClient");
1642 CAResult_t result = CAStartLEGattClient();
1644 #ifndef SINGLE_THREAD
1646 Don't start the client side sending queue thread until the
1647 client itself has actually started.
1649 if (CA_STATUS_OK == result)
1651 ca_mutex_lock(g_bleClientSendDataMutex);
1652 result = CAQueueingThreadStart(g_bleClientSendQueueHandle);
1653 ca_mutex_unlock(g_bleClientSendDataMutex);
1655 if (CA_STATUS_OK != result)
1659 "Unable to start client queuing thread");
1666 "GATT client failed to start (%d)",
1674 static CAResult_t CALEAdapterGattClientStop()
1676 #ifndef SINGLE_THREAD
1677 ca_mutex_lock(g_bleClientSendDataMutex);
1678 CAResult_t result = CAQueueingThreadStop(g_bleClientSendQueueHandle);
1679 ca_mutex_unlock(g_bleClientSendDataMutex);
1681 if (CA_STATUS_OK == result)
1683 CAStopLEGattClient();
1688 CAStopLEGattClient();
1690 return CA_STATUS_OK;
1694 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1695 CANetworkPacketReceivedCallback reqRespCallback,
1696 CANetworkChangeCallback netCallback,
1697 CAErrorHandleCallback errorCallback,
1698 ca_thread_pool_t handle)
1700 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1703 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1704 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1705 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1707 CAResult_t result = CA_STATUS_OK;
1708 result = CAInitLEAdapterMutex();
1709 if (CA_STATUS_OK != result)
1711 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1712 return CA_STATUS_FAILED;
1715 result = CAInitializeLENetworkMonitor();
1716 if (CA_STATUS_OK != result)
1718 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1719 return CA_STATUS_FAILED;
1722 CAInitializeLEAdapter();
1724 CASetLEClientThreadPoolHandle(handle);
1726 result = CAInitializeLEGattClient();
1727 if (CA_STATUS_OK != result)
1729 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattClient() failed");
1730 return CA_STATUS_FAILED;
1733 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1734 CASetLEServerThreadPoolHandle(handle);
1735 result = CAInitializeLEGattServer();
1736 if (CA_STATUS_OK != result)
1738 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattServer() failed");
1739 return CA_STATUS_FAILED;
1742 CASetLEAdapterThreadPoolHandle(handle);
1743 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1744 CASetLEReqRespAdapterCallback(reqRespCallback);
1746 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1747 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1748 CALERegisterNetworkNotifications(netCallback);
1750 g_errorHandler = errorCallback;
1752 static const CAConnectivityHandler_t connHandler =
1754 .startAdapter = CAStartLE,
1755 .stopAdapter = CAStopLE,
1756 .startListenServer = CAStartLEListeningServer,
1757 .stopListenServer = CAStopLEListeningServer,
1758 .startDiscoveryServer = CAStartLEDiscoveryServer,
1759 .sendData = CASendLEUnicastData,
1760 .sendDataToAll = CASendLEMulticastData,
1761 .GetnetInfo = CAGetLEInterfaceInformation,
1762 .readData = CAReadLEData,
1763 .terminate = CATerminateLE
1766 registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
1768 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1770 return CA_STATUS_OK;
1773 static CAResult_t CAStartLE()
1775 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE");
1777 return CAStartLEAdapter();
1780 static CAResult_t CAStopLE()
1782 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1783 #ifndef SINGLE_THREAD
1787 ca_mutex_lock(g_bleIsServerMutex);
1788 switch (g_adapterType)
1790 case ADAPTER_SERVER:
1791 CALEAdapterGattServerStop();
1793 case ADAPTER_CLIENT:
1794 CALEAdapterGattClientStop();
1796 case ADAPTER_BOTH_CLIENT_SERVER:
1797 CALEAdapterGattServerStop();
1798 CALEAdapterGattClientStop();
1803 ca_mutex_unlock(g_bleIsServerMutex);
1805 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1807 return CAStopLEAdapter();
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 switch (g_adapterType)
1823 case ADAPTER_SERVER:
1824 CATerminateLEGattServer();
1826 case ADAPTER_CLIENT:
1827 CATerminateLEGattClient();
1829 case ADAPTER_BOTH_CLIENT_SERVER:
1830 CATerminateLEGattServer();
1831 CATerminateLEGattClient();
1836 g_adapterType = ADAPTER_EMPTY;
1837 ca_mutex_unlock(g_bleIsServerMutex);
1839 #ifndef SINGLE_THREAD
1840 CATerminateLEQueues();
1842 CATerminateLEAdapterMutex();
1844 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1847 static CAResult_t CAStartLEListeningServer()
1849 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1850 #ifndef ROUTING_GATEWAY
1851 CAResult_t result = CA_STATUS_OK;
1852 #ifndef SINGLE_THREAD
1853 result = CAInitLEServerQueues();
1854 if (CA_STATUS_OK != result)
1856 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1861 result = CAGetLEAdapterState();
1863 if (CA_STATUS_FAILED == result)
1865 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1869 ca_mutex_lock(g_bleIsServerMutex);
1870 switch (g_adapterType)
1872 case ADAPTER_CLIENT:
1873 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1875 case ADAPTER_BOTH_CLIENT_SERVER:
1878 g_adapterType = ADAPTER_SERVER;
1880 ca_mutex_unlock(g_bleIsServerMutex);
1882 if (CA_ADAPTER_NOT_ENABLED == result)
1886 "Listen Server will be started once BT Adapter is enabled");
1887 result = CA_STATUS_OK;
1891 result = CALEAdapterGattServerStart();
1894 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1897 // Routing Gateway only supports BLE client mode.
1898 OIC_LOG(ERROR, CALEADAPTER_TAG, "LE server not supported in Routing Gateway");
1899 return CA_NOT_SUPPORTED;
1903 static CAResult_t CAStopLEListeningServer()
1905 OIC_LOG(ERROR, CALEADAPTER_TAG, "Listen server stop not supported.");
1906 return CA_NOT_SUPPORTED;
1909 static CAResult_t CAStartLEDiscoveryServer()
1911 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1912 CAResult_t result = CA_STATUS_OK;
1913 #ifndef SINGLE_THREAD
1914 result = CAInitLEClientQueues();
1915 if (CA_STATUS_OK != result)
1917 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1921 result = CAGetLEAdapterState();
1923 if (CA_STATUS_FAILED == result)
1925 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1929 ca_mutex_lock(g_bleIsServerMutex);
1930 switch (g_adapterType)
1932 case ADAPTER_SERVER:
1933 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
1935 case ADAPTER_BOTH_CLIENT_SERVER:
1938 g_adapterType = ADAPTER_CLIENT;
1940 ca_mutex_unlock(g_bleIsServerMutex);
1942 if (CA_ADAPTER_NOT_ENABLED == result)
1944 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Discovery server will be started once BT Adapter is enabled");
1945 result = CA_STATUS_OK;
1949 result = CALEAdapterGattClientStart();
1952 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1956 static CAResult_t CAReadLEData()
1958 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1959 #ifdef SINGLE_THREAD
1962 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1963 return CA_STATUS_OK;
1966 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1970 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1973 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1974 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1976 CAResult_t result = CA_STATUS_FAILED;
1978 ca_mutex_lock(g_bleIsServerMutex);
1979 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
1981 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1982 if (CA_STATUS_OK != result)
1984 ca_mutex_unlock(g_bleIsServerMutex);
1985 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed");
1988 g_errorHandler(endpoint, data, dataLen, result);
1995 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
1997 result = CALEAdapterClientSendData(endpoint, data, dataLen);
1998 if (CA_STATUS_OK != result)
2000 ca_mutex_unlock(g_bleIsServerMutex);
2001 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for client failed" );
2005 g_errorHandler(endpoint, data, dataLen, result);
2010 ca_mutex_unlock(g_bleIsServerMutex);
2012 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2016 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
2020 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2023 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
2027 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
2031 CAResult_t result = CA_STATUS_FAILED;
2033 ca_mutex_lock(g_bleIsServerMutex);
2034 if (ADAPTER_SERVER == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2036 result = CALEAdapterServerSendData(NULL, data, dataLen);
2037 if (CA_STATUS_OK != result)
2039 ca_mutex_unlock(g_bleIsServerMutex);
2041 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data for server failed" );
2045 g_errorHandler(endpoint, data, dataLen, result);
2051 if (ADAPTER_CLIENT == g_adapterType || ADAPTER_BOTH_CLIENT_SERVER == g_adapterType)
2053 result = CALEAdapterClientSendData(NULL, data, dataLen);
2054 if (CA_STATUS_OK != result)
2056 ca_mutex_unlock(g_bleIsServerMutex);
2058 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data for client failed" );
2062 g_errorHandler(endpoint, data, dataLen, result);
2067 ca_mutex_unlock(g_bleIsServerMutex);
2069 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2073 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
2075 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2077 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
2079 char *local_address = NULL;
2081 CAResult_t res = CAGetLEAddress(&local_address);
2082 if (CA_STATUS_OK != res)
2084 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
2088 if (NULL == local_address)
2090 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
2091 return CA_STATUS_FAILED;
2095 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
2096 if (NULL == (*info))
2098 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
2099 OICFree(local_address);
2100 return CA_STATUS_FAILED;
2103 size_t local_address_len = strlen(local_address);
2105 if(local_address_len >= sizeof(g_localBLEAddress) ||
2106 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
2108 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
2110 OICFree(local_address);
2111 return CA_STATUS_FAILED;
2114 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
2115 ca_mutex_lock(g_bleLocalAddressMutex);
2116 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
2117 ca_mutex_unlock(g_bleLocalAddressMutex);
2119 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
2121 OICFree(local_address);
2123 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2124 return CA_STATUS_OK;
2127 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
2129 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2131 ca_mutex_lock(g_bleNetworkCbMutex);
2132 g_networkCallback = netCallback;
2133 ca_mutex_unlock(g_bleNetworkCbMutex);
2134 CAResult_t res = CA_STATUS_OK;
2137 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
2138 if (CA_STATUS_OK != res)
2140 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2145 res = CAUnSetLEAdapterStateChangedCb();
2146 if (CA_STATUS_OK != res)
2148 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2152 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2156 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
2158 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2160 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
2161 CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE };
2163 ca_mutex_lock(g_bleLocalAddressMutex);
2164 OICStrcpy(localEndpoint.addr,
2165 sizeof(localEndpoint.addr),
2167 ca_mutex_unlock(g_bleLocalAddressMutex);
2169 if (CA_ADAPTER_ENABLED == adapter_state)
2171 ca_mutex_lock(g_bleIsServerMutex);
2172 switch (g_adapterType)
2174 case ADAPTER_SERVER:
2175 CALEAdapterGattServerStart();
2177 case ADAPTER_CLIENT:
2178 CALEAdapterGattClientStart();
2180 case ADAPTER_BOTH_CLIENT_SERVER:
2181 CALEAdapterGattServerStart();
2182 CALEAdapterGattClientStart();
2187 ca_mutex_unlock(g_bleIsServerMutex);
2191 ca_mutex_lock(g_bleIsServerMutex);
2192 switch (g_adapterType)
2194 case ADAPTER_SERVER:
2195 CALEAdapterGattServerStop();
2197 case ADAPTER_CLIENT:
2198 CALEAdapterGattClientStop();
2200 case ADAPTER_BOTH_CLIENT_SERVER:
2201 CALEAdapterGattServerStop();
2202 CALEAdapterGattClientStop();
2207 ca_mutex_unlock(g_bleIsServerMutex);
2210 ca_mutex_lock(g_bleNetworkCbMutex);
2211 if (NULL != g_networkCallback)
2213 g_networkCallback(&localEndpoint, adapter_state);
2217 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
2219 ca_mutex_unlock(g_bleNetworkCbMutex);
2221 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2224 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
2225 const uint8_t *data,
2228 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2230 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2231 #ifndef SINGLE_THREAD
2232 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2233 "g_bleClientSendQueueHandle is NULL",
2235 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
2236 "g_bleClientSendDataMutex is NULL",
2239 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
2242 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
2244 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
2247 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2248 return CA_MEMORY_ALLOC_FAILED;
2250 // Add message to send queue
2251 ca_mutex_lock(g_bleClientSendDataMutex);
2252 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
2253 ca_mutex_unlock(g_bleClientSendDataMutex);
2255 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2256 return CA_STATUS_OK;
2259 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2260 const uint8_t *data,
2263 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2265 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2267 #ifdef SINGLE_THREAD
2268 uint8_t header[CA_HEADER_LENGTH] = { 0 };
2271 CAGenerateHeader(header, CA_HEADER_LENGTH, dataLen);
2273 if (CA_STATUS_OK != result)
2275 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
2276 return CA_STATUS_FAILED;
2279 if (!CAIsLEConnected())
2281 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2282 return CA_STATUS_FAILED;
2285 result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH);
2286 if (CA_STATUS_OK != result)
2288 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2289 return CA_STATUS_FAILED;
2292 const uint32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2293 for (uint32_t iter = 0; iter < dataLimit; iter++)
2296 CAUpdateCharacteristicsToAllGattClients(
2297 data + (iter * CA_SUPPORTED_BLE_MTU_SIZE),
2298 CA_SUPPORTED_BLE_MTU_SIZE);
2300 if (CA_STATUS_OK != result)
2302 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2303 return CA_STATUS_FAILED;
2309 const uint32_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2313 CAUpdateCharacteristicsToAllGattClients(
2314 data + (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE),
2316 if (CA_STATUS_OK != result)
2318 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2319 return CA_STATUS_FAILED;
2324 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2325 "BleClientReceiverQueue is NULL",
2327 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2328 "BleClientSendDataMutex is NULL",
2331 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2334 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2336 CALEData_t * const bleData =
2337 CACreateLEData(remoteEndpoint, data, dataLen);
2341 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2342 return CA_MEMORY_ALLOC_FAILED;
2345 // Add message to send queue
2346 ca_mutex_lock(g_bleServerSendDataMutex);
2347 CAQueueingThreadAddData(g_bleServerSendQueueHandle,
2349 sizeof(CALEData_t));
2350 ca_mutex_unlock(g_bleServerSendDataMutex);
2352 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2353 return CA_STATUS_OK;
2356 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2357 const uint8_t *data,
2358 uint32_t dataLength,
2359 uint32_t *sentLength)
2361 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2364 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2365 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2367 #ifdef SINGLE_THREAD
2368 if(g_networkPacketReceivedCallback)
2370 // will be filled by upper layer
2371 const CASecureEndpoint_t endpoint =
2372 { .endpoint = { .adapter = CA_ADAPTER_GATT_BTLE } };
2375 g_networkPacketReceivedCallback(&endpoint, data, dataLength);
2378 VERIFY_NON_NULL_RET(g_bleReceiverQueue,
2380 "g_bleReceiverQueue",
2383 //Add message to data queue
2384 CAEndpoint_t * const remoteEndpoint =
2385 CACreateEndpointObject(CA_DEFAULT_FLAGS,
2386 CA_ADAPTER_GATT_BTLE,
2390 if (NULL == remoteEndpoint)
2392 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2393 return CA_STATUS_FAILED;
2396 // Create bleData to add to queue
2399 "Data received from LE layer [%d]",
2402 CALEData_t * const bleData =
2403 CACreateLEData(remoteEndpoint, data, dataLength);
2407 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2408 CAFreeEndpoint(remoteEndpoint);
2409 return CA_MEMORY_ALLOC_FAILED;
2412 CAFreeEndpoint(remoteEndpoint);
2413 // Add message to receiver queue
2414 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2416 *sentLength = dataLength;
2418 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2419 return CA_STATUS_OK;
2422 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2423 const uint8_t *data,
2424 uint32_t dataLength,
2425 uint32_t *sentLength)
2427 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2430 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2431 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2432 #ifndef SINGLE_THREAD
2433 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG, "g_bleReceiverQueue",
2436 //Add message to data queue
2437 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2438 CA_ADAPTER_GATT_BTLE,
2440 if (NULL == remoteEndpoint)
2442 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2443 return CA_STATUS_FAILED;
2446 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%u]", dataLength);
2448 // Create bleData to add to queue
2449 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
2452 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2453 CAFreeEndpoint(remoteEndpoint);
2454 return CA_MEMORY_ALLOC_FAILED;
2457 CAFreeEndpoint(remoteEndpoint);
2458 // Add message to receiver queue
2459 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2461 *sentLength = dataLength;
2463 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2464 return CA_STATUS_OK;
2467 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2469 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2471 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2472 g_bleAdapterThreadPool = handle;
2473 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2475 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2478 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2480 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2482 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2484 g_networkPacketReceivedCallback = callback;
2486 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2488 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2491 static void CALEErrorHandler(const char *remoteAddress,
2492 const uint8_t *data,
2496 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2498 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2500 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2501 CA_ADAPTER_GATT_BTLE,
2505 // if required, will be used to build remote endpoint
2506 g_errorHandler(rep, data, dataLen, result);
2508 CAFreeEndpoint(rep);
2510 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");