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 "cafragmentation.h"
27 #include "caleinterface.h"
30 #include "caadapterutils.h"
32 #include "caqueueingthread.h"
34 #if defined(__TIZEN__) || defined(__ANDROID__)
35 #include "caleserver.h"
36 #include "caleclient.h"
38 #include "oic_malloc.h"
39 #include "oic_string.h"
40 #include "caremotehandler.h"
44 * Logging tag for module name.
46 #define CALEADAPTER_TAG "OIC_CA_LE_ADAP"
49 * Stores information of all the senders.
51 * This structure will be used to track and defragment all incoming
57 uint32_t totalDataLen;
59 CAEndpoint_t *remoteEndpoint;
65 ADAPTER_BOTH_CLIENT_SERVER,
71 * Callback to provide the status of the network change to CA layer.
73 static CAAdapterChangeCallback g_networkCallback = NULL;
76 * Callback to provide the status of the connection change to CA layer.
78 static CAConnectionChangeCallback g_connectionCallback = NULL;
81 * Own port value to identify packet owner. Default port value is 1.
83 static uint8_t g_localBLESourcePort = 1;
86 * bleAddress of the local adapter. Value will be initialized to zero,
87 * and will be updated later.
89 static char g_localBLEAddress[18] = { 0 };
92 * Variable to differentiate btw GattServer and GattClient.
94 static CABLEAdapter_t g_adapterType = ADAPTER_EMPTY;
97 * Mutex to synchronize the task to be executed on the GattServer
100 static ca_mutex g_bleIsServerMutex = NULL;
103 * Mutex to synchronize the callback to be called for the network
106 static ca_mutex g_bleNetworkCbMutex = NULL;
109 * Mutex to synchronize the updates of the local LE address of the
112 static ca_mutex g_bleLocalAddressMutex = NULL;
115 * Reference to thread pool.
117 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
120 * Mutex to synchronize the task to be pushed to thread pool.
122 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
125 * Mutex to synchronize the queing of the data from SenderQueue.
127 static ca_mutex g_bleClientSendDataMutex = NULL;
130 * Mutex to synchronize the queing of the data from ReceiverQueue.
132 static ca_mutex g_bleReceiveDataMutex = NULL;
135 * Mutex to synchronize the queing of the data from SenderQueue.
137 static ca_mutex g_bleServerSendDataMutex = NULL;
140 * Mutex to synchronize the callback to be called for the
141 * adapterReqResponse.
143 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
146 * Callback to be called when network packet received from either
147 * GattServer or GattClient.
149 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
152 * Callback to notify error from the BLE adapter.
154 static CAErrorHandleCallback g_errorHandler = NULL;
158 * Pointer to defragment received data from single threaded routine.
160 static CABLESenderInfo_t *g_singleThreadReceiveData = NULL;
163 * This function will be associated with the receive for single thread.
165 * This function will defragment the received data from sender
166 * respectively and will send it up to CA layer. Respective sender's
167 * header will provide the length of the data sent.
169 * @param[in] data Actual data received from the remote
171 * @param[in] dataLen Length of the data received from the
174 static void CALEDataReceiverHandlerSingleThread(const uint8_t *data,
178 * This function will be associated with the send for single threaded
181 * This function will fragment the data to the MTU of the transport
182 * and send the data in fragments to the adapters. The function will
183 * be blocked until all data is sent out from the adapter.
185 * @param[in] data Data to be transmitted from LE.
186 * @param[in] dataLen Length of the Data being transmitted.
188 static CAResult_t CALEServerSendDataSingleThread(const uint8_t *data,
193 * Register network change notification callback.
195 * @param[in] netCallback CAAdapterChangeCallback callback which will
196 * be set for the change in adapter.
197 * @param[in] connCallback CAConnectionChangeCallback callback which will
198 * be set for the change in connection.
200 * @return 0 on success otherwise a positive error value.
201 * @retval ::CA_STATUS_OK Successful.
202 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
203 * @retval ::CA_STATUS_FAILED Operation failed.
206 static CAResult_t CALERegisterNetworkNotifications(CAAdapterChangeCallback netCallback,
207 CAConnectionChangeCallback connCallback);
210 * Set the thread pool handle which is required for spawning new
213 * @param[in] handle Thread pool handle which is given by above layer
214 * for using thread creation task.
217 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
220 * Call the callback to the upper layer when the adapter state gets
223 * @param[in] adapter_state New state of the adapter to be notified to
226 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state);
229 * Call the callback to the upper layer when the device connection state gets
232 * @param[in] address LE address of the device to be notified to the upper layer.
233 * @param[in] isConnected whether connection state is connected or not.
235 static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
239 * Used to initialize all required mutex variable for LE Adapter
242 * @return 0 on success otherwise a positive error value.
243 * @retval ::CA_STATUS_OK Successful.
244 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
245 * @retval ::CA_STATUS_FAILED Operation failed.
248 static CAResult_t CAInitLEAdapterMutex();
251 * Terminate all required mutex variables for LE adapter
254 static void CATerminateLEAdapterMutex();
257 * Prepares and notify error through error callback.
259 static void CALEErrorHandler(const char *remoteAddress,
264 #ifndef SINGLE_THREAD
266 * Stop condition of recvhandler.
268 static bool g_dataBleReceiverHandlerState = false;
271 * Sender information.
273 static u_arraylist_t *g_bleServerSenderInfo = NULL;
275 static u_arraylist_t *g_bleClientSenderInfo = NULL;
278 * Queue to process the outgoing packets from GATTClient.
280 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
283 * Queue to process the incoming packets.
285 static CAQueueingThread_t *g_bleReceiverQueue = NULL;
288 * Queue to process the outgoing packets from GATTServer.
290 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
293 * This function will be associated with the sender queue for
296 * This function will fragment the data to the MTU of the transport
297 * and send the data in fragments to the adapters. The function will
298 * be blocked until all data is sent out from the adapter.
300 * @param[in] threadData Data pushed to the queue which contains the
301 * info about RemoteEndpoint and Data.
303 static void CALEServerSendDataThread(void *threadData);
306 * This function will be associated with the sender queue for
309 * This function will fragment the data to the MTU of the transport
310 * and send the data in fragments to the adapters. The function will
311 * be blocked until all data is sent out from the adapter.
313 * @param[in] threadData Data pushed to the queue which contains the
314 * info about RemoteEndpoint and Data.
316 static void CALEClientSendDataThread(void *threadData);
319 * This function will be associated with the receiver queue.
321 * This function will defragment the received data from each sender
322 * respectively and will send it up to CA layer. Respective sender's
323 * header will provide the length of the data sent.
325 * @param[in] threadData Data pushed to the queue which contains the
326 * info about RemoteEndpoint and Data.
328 static void CALEDataReceiverHandler(void *threadData);
331 * This function will stop all queues created for GattServer and
332 * GattClient. All four queues will be be stopped with this function
335 static void CAStopLEQueues();
338 * This function will terminate all queues created for GattServer and
339 * GattClient. All four queues will be be terminated with this
340 * function invocations.
342 static void CATerminateLEQueues();
345 * This function will initalize the Receiver and Sender queues for
346 * GattServer. This function will in turn call the functions
347 * CAInitBleServerReceiverQueue() and CAInitBleServerSenderQueue() to
348 * initialize the queues.
350 * @return ::CA_STATUS_OK or Appropriate error code.
351 * @retval ::CA_STATUS_OK Successful.
352 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
353 * @retval ::CA_STATUS_FAILED Operation failed.
355 static CAResult_t CAInitLEServerQueues();
358 * This function will initalize the Receiver and Sender queues for
359 * GattClient. This function will inturn call the functions
360 * CAInitBleClientReceiverQueue() and CAInitBleClientSenderQueue() to
361 * initialize the queues.
363 * @return ::CA_STATUS_OK or Appropriate error code.
364 * @retval ::CA_STATUS_OK Successful.
365 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
366 * @retval ::CA_STATUS_FAILED Operation failed.
369 static CAResult_t CAInitLEClientQueues();
372 * This function will initalize the Receiver queue for
373 * GattServer. This will initialize the queue to process the function
374 * CABLEServerSendDataThread() when ever the task is added to this
377 * @return ::CA_STATUS_OK or Appropriate error code.
378 * @retval ::CA_STATUS_OK Successful.
379 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
380 * @retval ::CA_STATUS_FAILED Operation failed.
382 static CAResult_t CAInitLEServerSenderQueue();
385 * This function will initalize the Receiver queue for
386 * GattClient. This will initialize the queue to process the function
387 * CABLEClientSendDataThread() when ever the task is added to this
390 * @return ::CA_STATUS_OK or Appropriate error code.
391 * @retval ::CA_STATUS_OK Successful.
392 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
393 * @retval ::CA_STATUS_FAILED Operation failed.
395 static CAResult_t CAInitLEClientSenderQueue();
398 * This function will initialize the Receiver queue for
399 * LEAdapter. This will initialize the queue to process the function
400 * CABLEDataReceiverHandler() when ever the task is added to this
403 * @return ::CA_STATUS_OK or Appropriate error code
404 * @retval ::CA_STATUS_OK Successful
405 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
406 * @retval ::CA_STATUS_FAILED Operation failed
409 static CAResult_t CAInitLEReceiverQueue();
412 * This function will create the Data required to send it in the
415 * @param[in] remoteEndpoint Remote endpoint information of the
417 * @param[in] data Data to be transmitted from LE.
418 * @param[in] dataLength Length of the Data being transmitted.
420 * @return ::CA_STATUS_OK or Appropriate error code.
421 * @retval ::CA_STATUS_OK Successful.
422 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
423 * @retval ::CA_STATUS_FAILED Operation failed.
425 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
428 u_arraylist_t *senderInfo);
431 * Used to free the BLE information stored in the sender/receiver
434 * @param[in] bleData Information for a particular data segment.
436 static void CAFreeLEData(CALEData_t *bleData);
441 static void CALEDataDestroyer(void *data, uint32_t size);
443 #ifndef SINGLE_THREAD
445 * remove request or response data of send queue.
447 * @param[in] queueHandle queue to process the outgoing packets.
448 * @param[in] mutex mutex related to sender for client / server.
449 * @param[in] address target address to remove data in queue.
451 static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle,
453 const char* address);
456 * remove all received data of data list from receive queue.
458 * @param[in] dataInfoList received data list to remove for client / server.
459 * @param[in] address target address to remove data in queue.
461 static void CALERemoveReceiveQueueData(u_arraylist_t *dataInfoList,
462 const char* address);
465 * get received data info and positioned index from the received data list
466 * for client / server which is matched same leAddress and port.
468 * @param[in] leAddress target address to get serderInfo.
469 * @param[in] port target port to get serderInfo.
470 * @param[in] senderInfoList received data list for client / server.
471 * @param[out] senderInfo Pointer to contain matched(leAddress and port)
472 * received data info.
473 * @param[out] senderIndex Pointer to contain matched(leAddress and port)
474 * received data info index.
476 static CAResult_t CALEGetSenderInfo(const char *leAddress,
478 u_arraylist_t *senderInfoList,
479 CABLESenderInfo_t **senderInfo,
480 uint32_t *senderIndex);
483 * get ports related to remote address. It is need because multi application
484 * can have more than 2 senderInfo using same BLE address. So before remove
485 * receive queue data, should get port list from sender Info.
487 * @param[in] leAddress target address to get port in serderInfo.
488 * @param[in] senderInfoList received data list to remove for client / server.
489 * @param[out] portList target port list related to leAddress.
491 static CAResult_t CALEGetPortsFromSenderInfo(const char *leAddress,
492 u_arraylist_t *senderInfoList,
493 u_arraylist_t *portList);
496 static CAResult_t CAInitLEServerQueues()
498 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
500 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
502 CAResult_t result = CAInitLEServerSenderQueue();
503 if (CA_STATUS_OK != result)
505 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
506 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
507 return CA_STATUS_FAILED;
510 g_bleServerSenderInfo = u_arraylist_create();
511 if (!g_bleServerSenderInfo)
513 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
514 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
515 return CA_MEMORY_ALLOC_FAILED;
518 result = CAInitLEReceiverQueue();
519 if (CA_STATUS_OK != result)
521 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
522 u_arraylist_free(&g_bleServerSenderInfo);
523 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
524 return CA_STATUS_FAILED;
527 g_dataBleReceiverHandlerState = true;
529 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
531 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
535 static CAResult_t CAInitLEClientQueues()
537 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
539 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
541 CAResult_t result = CAInitLEClientSenderQueue();
542 if (CA_STATUS_OK != result)
544 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
545 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
546 return CA_STATUS_FAILED;
549 g_bleClientSenderInfo = u_arraylist_create();
550 if (!g_bleClientSenderInfo)
552 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
553 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
554 return CA_MEMORY_ALLOC_FAILED;
557 result = CAInitLEReceiverQueue();
558 if (CA_STATUS_OK != result)
560 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed");
561 u_arraylist_free(&g_bleClientSenderInfo);
562 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
563 return CA_STATUS_FAILED;
566 g_dataBleReceiverHandlerState = true;
568 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
570 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
574 static CAResult_t CAInitLEReceiverQueue()
576 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEReceiverQueue");
577 // Check if the message queue is already initialized
578 if (g_bleReceiverQueue)
580 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
584 // Create recv message queue
585 g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
586 if (!g_bleReceiverQueue)
588 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
589 return CA_MEMORY_ALLOC_FAILED;
592 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue,
593 g_bleAdapterThreadPool,
594 CALEDataReceiverHandler,
597 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
598 OICFree(g_bleReceiverQueue);
599 g_bleReceiverQueue = NULL;
600 return CA_STATUS_FAILED;
603 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue))
605 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
606 OICFree(g_bleReceiverQueue);
607 g_bleReceiverQueue = NULL;
608 return CA_STATUS_FAILED;
611 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
615 static CAResult_t CAInitLEServerSenderQueue()
617 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEServerSenderQueue");
618 // Check if the message queue is already initialized
619 if (g_bleServerSendQueueHandle)
621 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
625 // Create send message queue
626 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
627 if (!g_bleServerSendQueueHandle)
629 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
630 return CA_MEMORY_ALLOC_FAILED;
633 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
634 g_bleAdapterThreadPool,
635 CALEServerSendDataThread,
638 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
639 OICFree(g_bleServerSendQueueHandle);
640 g_bleServerSendQueueHandle = NULL;
641 return CA_STATUS_FAILED;
644 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
648 static void CALEClearSenderInfoImpl(u_arraylist_t ** list)
650 const size_t length = u_arraylist_length(*list);
651 for (size_t i = 0; i < length; ++i)
653 CABLESenderInfo_t * const info =
654 (CABLESenderInfo_t *) u_arraylist_get(*list, i);
657 OICFree(info->defragData);
658 CAFreeEndpoint(info->remoteEndpoint);
662 u_arraylist_free(list);
665 static void CALEClearSenderInfo()
667 CALEClearSenderInfoImpl(&g_bleServerSenderInfo);
668 CALEClearSenderInfoImpl(&g_bleClientSenderInfo);
671 static CAResult_t CAInitLEClientSenderQueue()
673 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEClientSenderQueue");
675 if (g_bleClientSendQueueHandle)
677 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
681 // Create send message queue
682 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
683 if (!g_bleClientSendQueueHandle)
685 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
686 return CA_MEMORY_ALLOC_FAILED;
689 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
690 g_bleAdapterThreadPool,
691 CALEClientSendDataThread, CALEDataDestroyer))
693 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
694 OICFree(g_bleClientSendQueueHandle);
695 g_bleClientSendQueueHandle = NULL;
696 return CA_STATUS_FAILED;
699 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAInitLEClientSenderQueue");
703 static void CAStopLEQueues()
705 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStopLEQueues");
707 ca_mutex_lock(g_bleReceiveDataMutex);
708 if (NULL != g_bleReceiverQueue)
710 CAQueueingThreadStop(g_bleReceiverQueue);
712 ca_mutex_unlock(g_bleReceiveDataMutex);
714 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAStopLEQueues");
717 static void CATerminateLEQueues()
719 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
721 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
722 OICFree(g_bleClientSendQueueHandle);
723 g_bleClientSendQueueHandle = NULL;
725 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
726 OICFree(g_bleServerSendQueueHandle);
727 g_bleServerSendQueueHandle = NULL;
729 CAQueueingThreadDestroy(g_bleReceiverQueue);
730 OICFree(g_bleReceiverQueue);
731 g_bleReceiverQueue = NULL;
733 CALEClearSenderInfo();
735 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
738 static CAResult_t CALEGetSenderInfo(const char *leAddress,
740 u_arraylist_t *senderInfoList,
741 CABLESenderInfo_t **senderInfo,
742 uint32_t *senderIndex)
744 VERIFY_NON_NULL_RET(leAddress,
746 "NULL BLE address argument",
747 CA_STATUS_INVALID_PARAM);
748 VERIFY_NON_NULL_RET(senderIndex,
750 "NULL index argument",
751 CA_STATUS_INVALID_PARAM);
753 const uint32_t listLength = u_arraylist_length(senderInfoList);
754 const uint32_t addrLength = strlen(leAddress);
755 for (uint32_t index = 0; index < listLength; index++)
757 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(senderInfoList, index);
758 if (!info || !(info->remoteEndpoint))
763 if (!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
765 if (info->remoteEndpoint->port == port)
767 *senderIndex = index;
777 return CA_STATUS_FAILED;
780 static void CALEDataReceiverHandler(void *threadData)
782 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDataReceiverHandler");
784 ca_mutex_lock(g_bleReceiveDataMutex);
786 if (g_dataBleReceiverHandlerState)
788 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
790 CALEData_t *bleData = (CALEData_t *) threadData;
793 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
794 ca_mutex_unlock(g_bleReceiveDataMutex);
798 if (!(bleData->senderInfo))
800 OIC_LOG(ERROR, CALEADAPTER_TAG, "sender info is not available");
801 ca_mutex_unlock(g_bleReceiveDataMutex);
805 if (!(bleData->remoteEndpoint))
807 OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!");
808 ca_mutex_unlock(g_bleReceiveDataMutex);
812 CABLESenderInfo_t *senderInfo = NULL;
813 uint32_t senderIndex = 0;
816 CABLEPacketStart_t startFlag = CA_BLE_PACKET_NOT_START;
817 CABLEPacketSecure_t secureFlag = CA_BLE_PACKET_NON_SECURE;
818 uint16_t sourcePort = 0;
819 uint16_t destPort = 0;
821 CAParseHeader(bleData->data, &startFlag, &sourcePort, &secureFlag, &destPort);
822 OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
823 "header info: startFlag[%X] sourcePort[%d] secureFlag[%X] destPort[%d]",
824 startFlag, sourcePort, secureFlag, destPort);
826 if (destPort != g_localBLESourcePort && destPort != CA_BLE_MULTICAST_PORT)
828 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
829 "this packet is not valid for this app(port mismatch[mine:%d, packet:%d])",
830 g_localBLESourcePort, destPort);
831 ca_mutex_unlock(g_bleReceiveDataMutex);
835 bleData->remoteEndpoint->port = sourcePort;
837 if (CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
838 bleData->remoteEndpoint->port,
840 &senderInfo, &senderIndex))
842 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s:%X]",
843 bleData->remoteEndpoint->addr, bleData->remoteEndpoint->port);
849 OIC_LOG(ERROR, CALEADAPTER_TAG,
850 "This packet is start packet but exist senderInfo. Remove senderInfo");
851 u_arraylist_remove(bleData->senderInfo, senderIndex);
852 OICFree(senderInfo->defragData);
861 uint32_t totalLength = 0;
864 CAParseHeaderPayloadLength(bleData->data, CA_BLE_LENGTH_HEADER_SIZE, &totalLength);
868 OIC_LOG(ERROR, CALEADAPTER_TAG, "This packet is wrong packet! ignore.");
869 ca_mutex_unlock(g_bleReceiveDataMutex);
873 CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
876 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
877 ca_mutex_unlock(g_bleReceiveDataMutex);
880 newSender->recvDataLen = 0;
881 newSender->totalDataLen = 0;
882 newSender->defragData = NULL;
883 newSender->remoteEndpoint = NULL;
885 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
887 newSender->totalDataLen = totalLength;
889 if (!(newSender->totalDataLen))
891 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
893 ca_mutex_unlock(g_bleReceiveDataMutex);
898 bleData->dataLen - (CA_BLE_HEADER_SIZE + CA_BLE_LENGTH_HEADER_SIZE);
899 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
900 newSender->totalDataLen);
901 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
904 newSender->defragData = OICCalloc(newSender->totalDataLen + 1,
905 sizeof(*newSender->defragData));
907 if (NULL == newSender->defragData)
909 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
911 ca_mutex_unlock(g_bleReceiveDataMutex);
915 const char *remoteAddress = bleData->remoteEndpoint->addr;
916 newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
917 CA_ADAPTER_GATT_BTLE,
919 bleData->remoteEndpoint->port);
921 if (NULL == newSender->remoteEndpoint)
923 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
924 OICFree(newSender->defragData);
926 ca_mutex_unlock(g_bleReceiveDataMutex);
930 if (newSender->recvDataLen + dataOnlyLen > newSender->totalDataLen)
932 OIC_LOG(ERROR, CALEADAPTER_TAG, "buffer is smaller than received data");
933 OICFree(newSender->defragData);
934 CAFreeEndpoint(newSender->remoteEndpoint);
936 ca_mutex_unlock(g_bleReceiveDataMutex);
939 memcpy(newSender->defragData,
940 bleData->data + (CA_BLE_HEADER_SIZE + CA_BLE_LENGTH_HEADER_SIZE),
942 newSender->recvDataLen += dataOnlyLen;
944 u_arraylist_add(bleData->senderInfo,(void *)newSender);
946 //Getting newSender index position in bleSenderInfo array list
948 CALEGetSenderInfo(newSender->remoteEndpoint->addr,
949 newSender->remoteEndpoint->port,
953 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
954 OICFree(newSender->defragData);
955 CAFreeEndpoint(newSender->remoteEndpoint);
957 ca_mutex_unlock(g_bleReceiveDataMutex);
960 senderInfo = newSender;
964 size_t dataOnlyLen = bleData->dataLen - CA_BLE_HEADER_SIZE;
965 if (senderInfo->recvDataLen + dataOnlyLen > senderInfo->totalDataLen)
967 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
968 "Data Length exceeding error!! Receiving [%d] total length [%d]",
969 senderInfo->recvDataLen + dataOnlyLen, senderInfo->totalDataLen);
970 u_arraylist_remove(bleData->senderInfo, senderIndex);
971 OICFree(senderInfo->defragData);
973 ca_mutex_unlock(g_bleReceiveDataMutex);
976 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]",
978 memcpy(senderInfo->defragData + senderInfo->recvDataLen,
979 bleData->data + CA_BLE_HEADER_SIZE,
981 senderInfo->recvDataLen += dataOnlyLen;
982 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] received Datalen [%d]",
983 senderInfo->totalDataLen, senderInfo->recvDataLen);
986 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
988 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
989 if (NULL == g_networkPacketReceivedCallback)
991 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
993 u_arraylist_remove(bleData->senderInfo, senderIndex);
994 OICFree(senderInfo->defragData);
996 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
997 ca_mutex_unlock(g_bleReceiveDataMutex);
1001 OIC_LOG(DEBUG, CALEADAPTER_TAG, "[CALEDataReceiverHandler] Sending data up !");
1003 const CASecureEndpoint_t tmp =
1005 .endpoint = *senderInfo->remoteEndpoint
1008 g_networkPacketReceivedCallback(&tmp,
1009 senderInfo->defragData,
1010 senderInfo->recvDataLen);
1011 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1012 u_arraylist_remove(bleData->senderInfo, senderIndex);
1013 senderInfo->remoteEndpoint = NULL;
1014 senderInfo->defragData = NULL;
1015 OICFree(senderInfo);
1018 ca_mutex_unlock(g_bleReceiveDataMutex);
1019 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1022 static void CALEServerSendDataThread(void *threadData)
1024 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEServerSendDataThread");
1026 CALEData_t * const bleData = (CALEData_t *) threadData;
1029 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1033 uint32_t midPacketCount = 0;
1034 size_t remainingLen = 0;
1035 size_t totalLength = 0;
1036 CABLEPacketSecure_t secureFlag = CA_BLE_PACKET_NON_SECURE;
1038 CAResult_t result = CAGenerateVariableForFragmentation(bleData->dataLen,
1043 if (CA_STATUS_OK != result)
1045 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1046 "CAGenerateVariableForFragmentation failed, result [%d]", result);
1047 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1051 OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
1052 "Packet info: data size[%d] midPacketCount[%d] remainingLen[%d] totalLength[%d]",
1053 bleData->dataLen, midPacketCount, remainingLen, totalLength);
1057 "Server total Data length with header is [%u]",
1060 uint8_t dataSegment[CA_SUPPORTED_BLE_MTU_SIZE] = {0};
1061 uint8_t dataHeader[CA_BLE_HEADER_SIZE] = {0};
1063 if (NULL != bleData->remoteEndpoint) //Unicast Data
1065 secureFlag = bleData->remoteEndpoint->flags == CA_SECURE ?
1066 CA_BLE_PACKET_SECURE : CA_BLE_PACKET_NON_SECURE;
1068 result = CAGenerateHeader(dataHeader,
1069 CA_BLE_PACKET_START,
1070 g_localBLESourcePort,
1072 bleData->remoteEndpoint->port);
1074 else //Multicast Data
1076 result = CAGenerateHeader(dataHeader,
1077 CA_BLE_PACKET_START,
1078 g_localBLESourcePort,
1080 CA_BLE_MULTICAST_PORT);
1083 if (CA_STATUS_OK != result)
1085 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1086 "CAGenerateHeader failed, result [%d]", result);
1087 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1091 uint8_t lengthHeader[CA_BLE_LENGTH_HEADER_SIZE] = {0};
1092 result = CAGenerateHeaderPayloadLength(lengthHeader,
1093 CA_BLE_LENGTH_HEADER_SIZE,
1096 if (CA_STATUS_OK != result)
1098 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1099 "CAGenerateHeaderPayloadLength failed, result [%d]", result);
1100 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1104 uint32_t length = 0;
1105 uint32_t dataLen = 0;
1106 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1108 length = totalLength;
1109 dataLen = bleData->dataLen;
1113 length = CA_SUPPORTED_BLE_MTU_SIZE;
1114 dataLen = CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE - CA_BLE_LENGTH_HEADER_SIZE;
1117 result = CAMakeFirstDataSegment(dataSegment,
1118 bleData->data, dataLen,
1119 dataHeader, lengthHeader);
1121 if (CA_STATUS_OK != result)
1123 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1124 "Making data segment failed, result [%d]", result);
1125 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1129 const uint32_t iter = midPacketCount;
1132 // Send the first segment with the header.
1133 if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
1135 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
1137 result = CAUpdateCharacteristicsToGattClient(
1138 bleData->remoteEndpoint->addr, dataSegment, length);
1140 if (CA_STATUS_OK != result)
1144 "Update characteristics failed, result [%d]",
1147 g_errorHandler(bleData->remoteEndpoint,
1156 "Server Sent data length [%u]",
1159 result = CAGenerateHeader(dataHeader,
1160 CA_BLE_PACKET_NOT_START,
1161 g_localBLESourcePort,
1163 bleData->remoteEndpoint->port);
1165 if (CA_STATUS_OK != result)
1167 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1168 "CAGenerateHeader failed, result [%d]", result);
1169 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1173 for (index = 0; index < iter; index++)
1175 // Send the remaining header.
1178 "Sending the chunk number [%u]",
1181 result = CAMakeRemainDataSegment(dataSegment,
1183 CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE,
1187 if (CA_STATUS_OK != result)
1189 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1190 "Making data segment failed, result [%d]", result);
1191 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1196 CAUpdateCharacteristicsToGattClient(
1197 bleData->remoteEndpoint->addr,
1199 CA_SUPPORTED_BLE_MTU_SIZE);
1201 if (CA_STATUS_OK != result)
1203 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1204 "Update characteristics failed, result [%d]", result);
1205 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1208 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
1209 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 result = CAMakeRemainDataSegment(dataSegment,
1224 if (CA_STATUS_OK != result)
1226 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1227 "Making data segment failed, result [%d]", result);
1228 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1232 result = CAUpdateCharacteristicsToGattClient(
1233 bleData->remoteEndpoint->addr,
1235 remainingLen + CA_BLE_HEADER_SIZE);
1237 if (CA_STATUS_OK != result)
1241 "Update characteristics failed, result [%d]",
1243 g_errorHandler(bleData->remoteEndpoint,
1249 OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
1250 "Server Sent data length [%d]", remainingLen + CA_BLE_HEADER_SIZE);
1255 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
1256 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
1257 if (CA_STATUS_OK != result)
1259 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1261 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1264 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1266 result = CAGenerateHeader(dataHeader,
1267 CA_BLE_PACKET_NOT_START,
1268 g_localBLESourcePort,
1270 CA_BLE_MULTICAST_PORT);
1272 if (CA_STATUS_OK != result)
1274 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1275 "CAGenerateHeader failed, result [%d]", result);
1276 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1280 for (index = 0; index < iter; index++)
1282 // Send the remaining header.
1283 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1285 result = CAMakeRemainDataSegment(dataSegment,
1287 CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE,
1291 if (CA_STATUS_OK != result)
1293 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1294 "Making data segment failed, result [%d]", result);
1295 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1299 result = CAUpdateCharacteristicsToAllGattClients(
1301 CA_SUPPORTED_BLE_MTU_SIZE);
1303 if (CA_STATUS_OK != result)
1305 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1307 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1310 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%u]",
1311 CA_SUPPORTED_BLE_MTU_SIZE);
1314 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1316 // send the last segment of the data
1317 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1319 result = CAMakeRemainDataSegment(dataSegment,
1325 if (CA_STATUS_OK != result)
1327 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1328 "Making data segment failed, result [%d]", result);
1329 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1333 result = CAUpdateCharacteristicsToAllGattClients(
1335 remainingLen + CA_BLE_HEADER_SIZE);
1337 if (CA_STATUS_OK != result)
1339 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1341 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1344 OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
1345 "Server Sent data length [%d]", remainingLen + CA_BLE_HEADER_SIZE);
1349 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CALEServerSendDataThread");
1352 static void CALEClientSendDataThread(void *threadData)
1354 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEClientSendDataThread");
1356 CALEData_t *bleData = (CALEData_t *) threadData;
1359 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1363 uint32_t midPacketCount = 0;
1364 size_t remainingLen = 0;
1365 size_t totalLength = 0;
1366 CABLEPacketSecure_t secureFlag = CA_BLE_PACKET_NON_SECURE;
1368 CAResult_t result = CAGenerateVariableForFragmentation(bleData->dataLen,
1373 if (CA_STATUS_OK != result)
1375 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1376 "CAGenerateVariableForFragmentation failed, result [%d]", result);
1377 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1381 OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
1382 "Packet info: data size[%d] midPacketCount[%d] remainingLen[%d] totalLength[%d]",
1383 bleData->dataLen, midPacketCount, remainingLen, totalLength);
1385 uint8_t dataSegment[CA_SUPPORTED_BLE_MTU_SIZE] = {0};
1386 uint8_t dataHeader[CA_BLE_HEADER_SIZE] = {0};
1388 if (NULL != bleData->remoteEndpoint) //Unicast Data
1390 secureFlag = bleData->remoteEndpoint->flags == CA_SECURE ?
1391 CA_BLE_PACKET_SECURE : CA_BLE_PACKET_NON_SECURE;
1393 result = CAGenerateHeader(dataHeader,
1394 CA_BLE_PACKET_START,
1395 g_localBLESourcePort,
1397 bleData->remoteEndpoint->port);
1399 else //Multicast Data
1401 result = CAGenerateHeader(dataHeader,
1402 CA_BLE_PACKET_START,
1403 g_localBLESourcePort,
1405 CA_BLE_MULTICAST_PORT);
1408 if (CA_STATUS_OK != result)
1410 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1411 "CAGenerateHeader failed, result [%d]", result);
1412 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1416 uint8_t lengthHeader[CA_BLE_LENGTH_HEADER_SIZE] = {0};
1417 result = CAGenerateHeaderPayloadLength(lengthHeader,
1418 CA_BLE_LENGTH_HEADER_SIZE,
1421 if (CA_STATUS_OK != result)
1423 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1424 "CAGenerateHeaderPayloadLength failed, result [%d]", result);
1425 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1429 uint32_t length = 0;
1430 uint32_t dataLen = 0;
1431 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1433 length = totalLength;
1434 dataLen = bleData->dataLen;
1438 length = CA_SUPPORTED_BLE_MTU_SIZE;
1439 dataLen = CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE - CA_BLE_LENGTH_HEADER_SIZE;
1442 result = CAMakeFirstDataSegment(dataSegment,
1443 bleData->data, dataLen,
1444 dataHeader, lengthHeader);
1446 if (CA_STATUS_OK != result)
1448 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1449 "Making data segment failed, result [%d]", result);
1450 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1454 const uint32_t iter = midPacketCount;
1456 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1458 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1459 // Send the first segment with the header.
1461 CAUpdateCharacteristicsToGattServer(
1462 bleData->remoteEndpoint->addr,
1468 if (CA_STATUS_OK != result)
1472 "Update characteristics failed, result [%d]",
1474 g_errorHandler(bleData->remoteEndpoint,
1483 "Client Sent Data length is [%u]",
1486 result = CAGenerateHeader(dataHeader,
1487 CA_BLE_PACKET_NOT_START,
1488 g_localBLESourcePort,
1490 bleData->remoteEndpoint->port);
1492 if (CA_STATUS_OK != result)
1494 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1495 "CAGenerateHeader failed, result [%d]", result);
1496 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1500 for (index = 0; index < iter; index++)
1502 result = CAMakeRemainDataSegment(dataSegment,
1504 CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE,
1508 if (CA_STATUS_OK != result)
1510 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1511 "Making data segment failed, result [%d]", result);
1512 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1516 // Send the remaining header.
1517 result = CAUpdateCharacteristicsToGattServer(
1518 bleData->remoteEndpoint->addr,
1520 CA_SUPPORTED_BLE_MTU_SIZE,
1523 if (CA_STATUS_OK != result)
1527 "Update characteristics failed, result [%d]",
1529 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1532 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1533 CA_SUPPORTED_BLE_MTU_SIZE);
1536 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1538 // send the last segment of the data (Ex: 22 bytes of 622
1539 // bytes of data when MTU is 200)
1540 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1542 result = CAMakeRemainDataSegment(dataSegment,
1548 if (CA_STATUS_OK != result)
1550 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1551 "Making data segment failed, result [%d]", result);
1552 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1556 result = CAUpdateCharacteristicsToGattServer(
1557 bleData->remoteEndpoint->addr,
1559 remainingLen + CA_BLE_HEADER_SIZE,
1562 if (CA_STATUS_OK != result)
1564 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1566 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1569 OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
1570 "Client Sent Data length is [%d]", remainingLen + CA_BLE_HEADER_SIZE);
1575 //Sending Mulitcast Data
1576 // Send the first segment with the header.
1577 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1578 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1579 if (CA_STATUS_OK != result)
1581 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1582 "Update characteristics (all) failed, result [%d]", result);
1583 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1586 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1588 result = CAGenerateHeader(dataHeader,
1589 CA_BLE_PACKET_NOT_START,
1590 g_localBLESourcePort,
1594 if (CA_STATUS_OK != result)
1596 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1597 "CAGenerateHeader failed, result [%d]", result);
1598 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1602 // Send the remaining header.
1603 for (index = 0; index < iter; index++)
1605 result = CAMakeRemainDataSegment(dataSegment,
1607 CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE,
1611 if (CA_STATUS_OK != result)
1613 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1614 "Making data segment failed, result [%d]", result);
1615 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1619 result = CAUpdateCharacteristicsToAllGattServers(
1621 CA_SUPPORTED_BLE_MTU_SIZE);
1623 if (CA_STATUS_OK != result)
1625 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1627 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1630 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1631 CA_SUPPORTED_BLE_MTU_SIZE);
1634 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1636 // send the last segment of the data (Ex: 22 bytes of 622
1637 // bytes of data when MTU is 200)
1638 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1640 result = CAMakeRemainDataSegment(dataSegment,
1646 if (CA_STATUS_OK != result)
1648 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1649 "Making data segment failed, result [%d]", result);
1650 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1655 CAUpdateCharacteristicsToAllGattServers(
1657 remainingLen + CA_BLE_HEADER_SIZE);
1659 if (CA_STATUS_OK != result)
1661 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1662 "Update characteristics (all) failed, result [%d]", result);
1663 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1666 OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
1667 "Client Sent Data length is [%d]", remainingLen + CA_BLE_HEADER_SIZE);
1671 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1674 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
1675 const uint8_t *data,
1676 uint32_t dataLength,
1677 u_arraylist_t *senderInfo)
1679 CALEData_t * const bleData = OICMalloc(sizeof(CALEData_t));
1683 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1687 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1688 bleData->data = OICCalloc(dataLength + 1, 1);
1690 if (NULL == bleData->data)
1692 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1693 CAFreeLEData(bleData);
1697 memcpy(bleData->data, data, dataLength);
1698 bleData->dataLen = dataLength;
1701 bleData->senderInfo = senderInfo;
1707 static void CAFreeLEData(CALEData_t *bleData)
1709 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1711 CAFreeEndpoint(bleData->remoteEndpoint);
1712 OICFree(bleData->data);
1716 static void CALEDataDestroyer(void *data, uint32_t size)
1718 if ((size_t)size < sizeof(CALEData_t *))
1720 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1721 "Destroy data too small %p %d", data, size);
1723 CALEData_t *ledata = (CALEData_t *) data;
1725 CAFreeLEData(ledata);
1729 #ifdef SINGLE_THREAD
1730 static void CALEDataReceiverHandlerSingleThread(const uint8_t *data,
1733 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1735 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
1738 CABLEPacketStart_t startFlag = CA_BLE_PACKET_NOT_START;
1739 CABLEPacketSecure_t secureFlag = CA_BLE_PACKET_NON_SECURE;
1740 uint16_t sourcePort = 0;
1741 uint16_t destPort = 0;
1743 CAParseHeader(data, &startFlag, &sourcePort, &secureFlag, &destPort);
1744 OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
1745 "header info: startFlag[%X] sourcePort[%d] secureFlag[%X] destPort[%d]",
1746 startFlag, sourcePort, secureFlag, destPort);
1748 if (destPort != g_localBLESourcePort && destPort != CA_BLE_MULTICAST_PORT)
1750 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1751 "this packet is not valid for this app(port mismatch[mine:%d, packet:%d])",
1752 g_localBLESourcePort, destPort);
1758 if (g_singleThreadReceiveData)
1760 OIC_LOG(ERROR, CALEADAPTER_TAG,
1761 "This packet is start packet but exist senderInfo. Remove senderInfo");
1762 OICFree(g_singleThreadReceiveData->defragData);
1763 OICFree(g_singleThreadReceiveData);
1764 g_singleThreadReceiveData = NULL;
1767 uint32_t totalLength = 0;
1768 CAParseHeaderPayloadLength(data, CA_BLE_LENGTH_HEADER_SIZE, &totalLength);
1770 g_singleThreadReceiveData = OICMalloc(sizeof(CABLESenderInfo_t));
1772 if (!g_singleThreadReceiveData)
1774 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
1777 g_singleThreadReceiveData->recvDataLen = 0;
1778 g_singleThreadReceiveData->totalDataLen = 0;
1779 g_singleThreadReceiveData->defragData = NULL;
1780 g_singleThreadReceiveData->remoteEndpoint = NULL;
1782 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1784 g_singleThreadReceiveData->totalDataLen = totalLength;
1786 if (!(g_singleThreadReceiveData->totalDataLen))
1788 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
1789 OICFree(g_singleThreadReceiveData);
1793 size_t dataOnlyLen = dataLen - (CA_BLE_HEADER_SIZE + CA_BLE_LENGTH_HEADER_SIZE);
1794 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
1795 g_singleThreadReceiveData->totalDataLen);
1796 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
1799 g_singleThreadReceiveData->defragData =
1800 OICCalloc(g_singleThreadReceiveData->totalDataLen + 1,
1801 sizeof(*g_singleThreadReceiveData->defragData));
1803 if (NULL == g_singleThreadReceiveData->defragData)
1805 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1806 OICFree(g_singleThreadReceiveData);
1810 if (g_singleThreadReceiveData->recvDataLen + dataOnlyLen
1811 > g_singleThreadReceiveData->totalDataLen)
1813 OIC_LOG(ERROR, CALEADAPTER_TAG, "buffer is smaller than received data");
1814 OICFree(g_singleThreadReceiveData->defragData);
1815 OICFree(g_singleThreadReceiveData);
1818 memcpy(g_singleThreadReceiveData->defragData,
1819 data + (CA_BLE_HEADER_SIZE + CA_BLE_LENGTH_HEADER_SIZE),
1821 g_singleThreadReceiveData->recvDataLen += dataOnlyLen;
1825 size_t dataOnlyLen = dataLen - CA_BLE_HEADER_SIZE;
1826 if (g_singleThreadReceiveData->recvDataLen + dataOnlyLen
1827 > g_singleThreadReceiveData->totalDataLen)
1829 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1830 "Data Length exceeding error!! Receiving [%d] total length [%d]",
1831 g_singleThreadReceiveData->recvDataLen + dataOnlyLen,
1832 g_singleThreadReceiveData->totalDataLen);
1833 OICFree(g_singleThreadReceiveData->defragData);
1834 OICFree(g_singleThreadReceiveData);
1837 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]",
1839 memcpy(g_singleThreadReceiveData->defragData + g_singleThreadReceiveData->recvDataLen,
1840 data + CA_BLE_HEADER_SIZE,
1842 g_singleThreadReceiveData->recvDataLen += dataOnlyLen;
1843 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] received Datalen [%d]",
1844 g_singleThreadReceiveData->totalDataLen, g_singleThreadReceiveData->recvDataLen);
1848 static CAResult_t CALEServerSendDataSingleThread(const uint8_t *data,
1851 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1853 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
1855 uint32_t midPacketCount = 0;
1856 size_t remainingLen = 0;
1857 size_t totalLength = 0;
1858 CABLEPacketSecure_t secureFlag = CA_BLE_PACKET_NON_SECURE;
1860 CAResult_t result = CAGenerateVariableForFragmentation(dataLen,
1865 if (CA_STATUS_OK != result)
1867 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1868 "CAGenerateVariableForFragmentation failed, result [%d]", result);
1872 OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
1873 "Packet info: data size[%d] midPacketCount[%d] remainingLen[%d] totalLength[%d]",
1874 dataLen, midPacketCount, remainingLen, totalLength);
1878 "Server total Data length with header is [%u]",
1881 uint8_t dataSegment[CA_SUPPORTED_BLE_MTU_SIZE] = {0};
1882 uint8_t dataHeader[CA_BLE_HEADER_SIZE] = {0};
1884 result = CAGenerateHeader(dataHeader,
1885 CA_BLE_PACKET_START,
1886 g_localBLESourcePort,
1887 CA_BLE_PACKET_NON_SECURE,
1888 CA_BLE_MULTICAST_PORT);
1890 if (CA_STATUS_OK != result)
1892 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1893 "CAGenerateHeader failed, result [%d]", result);
1897 uint8_t lengthHeader[CA_BLE_LENGTH_HEADER_SIZE] = {0};
1898 result = CAGenerateHeaderPayloadLength(lengthHeader,
1899 CA_BLE_LENGTH_HEADER_SIZE,
1902 if (CA_STATUS_OK != result)
1904 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1905 "CAGenerateHeaderPayloadLength failed, result [%d]", result);
1909 uint32_t length = 0;
1910 uint32_t dataOnlyLen = 0;
1911 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1913 length = totalLength;
1914 dataOnlyLen = dataLen;
1918 length = CA_SUPPORTED_BLE_MTU_SIZE;
1919 dataOnlyLen = CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE - CA_BLE_LENGTH_HEADER_SIZE;
1922 result = CAMakeFirstDataSegment(dataSegment,
1924 dataHeader, lengthHeader);
1926 if (CA_STATUS_OK != result)
1928 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1929 "Making data segment failed, result [%d]", result);
1933 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
1934 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
1935 if (CA_STATUS_OK != result)
1937 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1944 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1946 result = CAGenerateHeader(dataHeader,
1947 CA_BLE_PACKET_NOT_START,
1948 g_localBLESourcePort,
1949 CA_BLE_PACKET_NON_SECURE,
1950 CA_BLE_MULTICAST_PORT);
1952 if (CA_STATUS_OK != result)
1954 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1955 "CAGenerateHeader failed, result [%d]", result);
1959 const uint32_t dataLimit = midPacketCount;
1960 for (uint32_t iter = 0; iter < dataLimit; iter++)
1962 result = CAMakeRemainDataSegment(dataSegment,
1964 CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE,
1968 if (CA_STATUS_OK != result)
1970 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1971 "Making data segment failed, result [%d]", result);
1975 result = CAUpdateCharacteristicsToAllGattClients(
1977 CA_SUPPORTED_BLE_MTU_SIZE);
1979 if (CA_STATUS_OK != result)
1981 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
1988 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1990 // send the last segment of the data
1991 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1993 result = CAMakeRemainDataSegment(dataSegment,
1999 if (CA_STATUS_OK != result)
2001 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
2002 "Making data segment failed, result [%d]", result);
2006 result = CAUpdateCharacteristicsToAllGattClients(
2008 remainingLen + CA_BLE_HEADER_SIZE);
2010 if (CA_STATUS_OK != result)
2012 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2022 static CAResult_t CAInitLEAdapterMutex()
2024 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEAdapterMutex");
2026 if (NULL == g_bleIsServerMutex)
2028 g_bleIsServerMutex = ca_mutex_new();
2029 if (NULL == g_bleIsServerMutex)
2031 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
2032 return CA_STATUS_FAILED;
2036 if (NULL == g_bleNetworkCbMutex)
2038 g_bleNetworkCbMutex = ca_mutex_new();
2039 if (NULL == g_bleNetworkCbMutex)
2041 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
2042 CATerminateLEAdapterMutex();
2043 return CA_STATUS_FAILED;
2047 if (NULL == g_bleLocalAddressMutex)
2049 g_bleLocalAddressMutex = ca_mutex_new();
2050 if (NULL == g_bleLocalAddressMutex)
2052 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
2053 CATerminateLEAdapterMutex();
2054 return CA_STATUS_FAILED;
2058 if (NULL == g_bleAdapterThreadPoolMutex)
2060 g_bleAdapterThreadPoolMutex = ca_mutex_new();
2061 if (NULL == g_bleAdapterThreadPoolMutex)
2063 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
2064 CATerminateLEAdapterMutex();
2065 return CA_STATUS_FAILED;
2069 if (NULL == g_bleClientSendDataMutex)
2071 g_bleClientSendDataMutex = ca_mutex_new();
2072 if (NULL == g_bleClientSendDataMutex)
2074 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
2075 CATerminateLEAdapterMutex();
2076 return CA_STATUS_FAILED;
2080 if (NULL == g_bleServerSendDataMutex)
2082 g_bleServerSendDataMutex = ca_mutex_new();
2083 if (NULL == g_bleServerSendDataMutex)
2085 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
2086 CATerminateLEAdapterMutex();
2087 return CA_STATUS_FAILED;
2091 if (NULL == g_bleAdapterReqRespCbMutex)
2093 g_bleAdapterReqRespCbMutex = ca_mutex_new();
2094 if (NULL == g_bleAdapterReqRespCbMutex)
2096 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
2097 CATerminateLEAdapterMutex();
2098 return CA_STATUS_FAILED;
2102 if (NULL == g_bleReceiveDataMutex)
2104 g_bleReceiveDataMutex = ca_mutex_new();
2105 if (NULL == g_bleReceiveDataMutex)
2107 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
2108 return CA_STATUS_FAILED;
2112 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2113 return CA_STATUS_OK;
2116 static void CATerminateLEAdapterMutex()
2118 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CATerminateLEAdapterMutex");
2120 ca_mutex_free(g_bleIsServerMutex);
2121 g_bleIsServerMutex = NULL;
2123 ca_mutex_free(g_bleNetworkCbMutex);
2124 g_bleNetworkCbMutex = NULL;
2126 ca_mutex_free(g_bleLocalAddressMutex);
2127 g_bleLocalAddressMutex = NULL;
2129 ca_mutex_free(g_bleAdapterThreadPoolMutex);
2130 g_bleAdapterThreadPoolMutex = NULL;
2132 ca_mutex_free(g_bleClientSendDataMutex);
2133 g_bleClientSendDataMutex = NULL;
2135 ca_mutex_free(g_bleServerSendDataMutex);
2136 g_bleServerSendDataMutex = NULL;
2138 ca_mutex_free(g_bleAdapterReqRespCbMutex);
2139 g_bleAdapterReqRespCbMutex = NULL;
2141 ca_mutex_free(g_bleReceiveDataMutex);
2142 g_bleReceiveDataMutex = NULL;
2144 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2148 * Starting LE connectivity adapters.
2150 * As its peer to peer it does not require to start any servers.
2152 * @return ::CA_STATUS_OK or Appropriate error code.
2154 static CAResult_t CAStartLE();
2157 * Start listening server for receiving multicast search requests.
2159 * Transport Specific Behavior:
2160 * LE Starts GATT Server with prefixed UUID and Characteristics
2161 * per OIC Specification.
2162 * @return ::CA_STATUS_OK or Appropriate error code.
2164 static CAResult_t CAStartLEListeningServer();
2167 * Stops listening server from receiving multicast search requests.
2169 * Transport Specific Behavior:
2170 * LE Starts GATT Server with prefixed UUID and Characteristics
2171 * per OIC Specification.
2172 * @return ::CA_STATUS_OK or Appropriate error code.
2174 static CAResult_t CAStopLEListeningServer();
2177 * Sarting discovery of servers for receiving multicast
2180 * Transport Specific Behavior:
2181 * LE Starts GATT Server with prefixed UUID and Characteristics
2182 * per OIC Specification.
2184 * @return ::CA_STATUS_OK or Appropriate error code
2186 static CAResult_t CAStartLEDiscoveryServer();
2189 * Send data to the endpoint using the adapter connectivity.
2191 * @param[in] endpoint Remote Endpoint information (like MAC address,
2192 * reference URI and connectivity type) to which
2193 * the unicast data has to be sent.
2194 * @param[in] data Data which required to be sent.
2195 * @param[in] dataLen Size of data to be sent.
2197 * @note dataLen must be > 0.
2199 * @return The number of bytes sent on the network, or -1 on error.
2201 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
2204 CADataType_t dataType);
2207 * Send multicast data to the endpoint using the LE connectivity.
2209 * @param[in] endpoint Remote Endpoint information to which the
2210 * multicast data has to be sent.
2211 * @param[in] data Data which required to be sent.
2212 * @param[in] dataLen Size of data to be sent.
2214 * @note dataLen must be > 0.
2216 * @return The number of bytes sent on the network, or -1 on error.
2218 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
2221 CADataType_t dataType);
2224 * Get LE Connectivity network information.
2226 * @param[out] info Local connectivity information structures.
2227 * @param[out] size Number of local connectivity structures.
2229 * @return ::CA_STATUS_OK or Appropriate error code.
2231 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info,
2235 * Read Synchronous API callback.
2237 * @return ::CA_STATUS_OK or Appropriate error code.
2239 static CAResult_t CAReadLEData();
2242 * Stopping the adapters and close socket connections.
2244 * LE Stops all GATT servers and GATT Clients.
2246 * @return ::CA_STATUS_OK or Appropriate error code.
2248 static CAResult_t CAStopLE();
2251 * Terminate the LE connectivity adapter.
2253 * Configuration information will be deleted from further use.
2255 static void CATerminateLE();
2258 * This function will receive the data from the GattServer and add the
2259 * data to the Server receiver queue.
2261 * @param[in] remoteAddress Remote address of the device from where
2263 * @param[in] data Actual data received from the remote
2265 * @param[in] dataLength Length of the data received from the
2267 * @param[in] sentLength Length of the data sent from the remote
2270 * @return ::CA_STATUS_OK or Appropriate error code.
2271 * @retval ::CA_STATUS_OK Successful.
2272 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
2273 * @retval ::CA_STATUS_FAILED Operation failed.
2276 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2277 const uint8_t *data,
2278 uint32_t dataLength,
2279 uint32_t *sentLength);
2282 * This function will receive the data from the GattClient and add the
2283 * data into the Client receiver queue.
2285 * @param[in] remoteAddress Remote address of the device from where
2287 * @param[in] data Actual data recevied from the remote
2289 * @param[in] dataLength Length of the data received from the
2291 * @param[in] sentLength Length of the data sent from the remote
2294 * @return ::CA_STATUS_OK or Appropriate error code.
2295 * @retval ::CA_STATUS_OK Successful.
2296 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
2297 * @retval ::CA_STATUS_FAILED Operation failed.
2299 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2300 const uint8_t *data,
2301 uint32_t dataLength,
2302 uint32_t *sentLength);
2305 * Set the NetworkPacket received callback to CA layer from adapter
2308 * @param[in] callback Callback handle sent from the upper layer.
2310 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
2313 * Push the data from CA layer to the Sender processor queue.
2315 * @param[in] remoteEndpoint Remote endpoint information of the
2317 * @param[in] data Data to be transmitted from LE.
2318 * @param[in] dataLen Length of the Data being transmitted.
2320 * @return ::CA_STATUS_OK or Appropriate error code.
2321 * @retval ::CA_STATUS_OK Successful.
2322 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
2323 * @retval ::CA_STATUS_FAILED Operation failed.
2325 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2326 const uint8_t *data,
2330 * Push the data from CA layer to the Sender processor queue.
2332 * @param[in] remoteEndpoint Remote endpoint information of the
2334 * @param[in] data Data to be transmitted from LE.
2335 * @param[in] dataLen Length of the Data being transmitted.
2337 * @return ::CA_STATUS_OK or Appropriate error code.
2338 * @retval ::CA_STATUS_OK Successful.
2339 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
2340 * @retval ::CA_STATUS_FAILED Operation failed.
2342 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
2343 const uint8_t *data,
2346 static CAResult_t CALEAdapterGattServerStart()
2348 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
2350 CAResult_t result = CAStartLEGattServer();
2352 #ifndef SINGLE_THREAD
2354 Don't start the server side sending queue thread until the
2355 server itself has actually started.
2357 if (CA_STATUS_OK == result)
2359 ca_mutex_lock(g_bleServerSendDataMutex);
2360 result = CAQueueingThreadStart(g_bleServerSendQueueHandle);
2361 ca_mutex_unlock(g_bleServerSendDataMutex);
2363 if (CA_STATUS_OK != result)
2367 "Unable to start server queuing thread (%d)",
2376 static CAResult_t CALEAdapterGattServerStop()
2378 #ifndef SINGLE_THREAD
2379 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEAdapterGattServerStop");
2381 CAResult_t result = CAStopLEGattServer();
2382 ca_mutex_lock(g_bleServerSendDataMutex);
2383 if (CA_STATUS_OK == result)
2385 result = CAQueueingThreadStop(g_bleServerSendQueueHandle);
2387 ca_mutex_unlock(g_bleServerSendDataMutex);
2391 return CAStopLEGattServer();
2395 static CAResult_t CALEAdapterGattClientStart()
2397 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattClient");
2399 CAResult_t result = CAStartLEGattClient();
2401 #ifndef SINGLE_THREAD
2403 Don't start the client side sending queue thread until the
2404 client itself has actually started.
2406 if (CA_STATUS_OK == result)
2408 ca_mutex_lock(g_bleClientSendDataMutex);
2409 result = CAQueueingThreadStart(g_bleClientSendQueueHandle);
2410 ca_mutex_unlock(g_bleClientSendDataMutex);
2412 if (CA_STATUS_OK != result)
2416 "Unable to start client queuing thread");
2424 static CAResult_t CALEAdapterGattClientStop()
2426 #ifndef SINGLE_THREAD
2427 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEAdapterGattClientStop");
2428 CAStopLEGattClient();
2430 ca_mutex_lock(g_bleClientSendDataMutex);
2431 CAResult_t result = CAQueueingThreadStop(g_bleClientSendQueueHandle);
2432 ca_mutex_unlock(g_bleClientSendDataMutex);
2436 CAStopLEGattClient();
2438 return CA_STATUS_OK;
2442 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
2443 CANetworkPacketReceivedCallback reqRespCallback,
2444 CAAdapterChangeCallback netCallback,
2445 CAConnectionChangeCallback connCallback,
2446 CAErrorHandleCallback errorCallback,
2447 ca_thread_pool_t handle)
2449 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2452 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
2453 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
2454 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
2455 VERIFY_NON_NULL(connCallback, CALEADAPTER_TAG, "ConnectionChange Callback is null");
2457 CAResult_t result = CA_STATUS_OK;
2458 result = CAInitLEAdapterMutex();
2459 if (CA_STATUS_OK != result)
2461 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
2462 return CA_STATUS_FAILED;
2465 result = CAInitializeLENetworkMonitor();
2466 if (CA_STATUS_OK != result)
2468 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
2469 return CA_STATUS_FAILED;
2471 CAInitializeLEAdapter();
2473 CASetLEClientThreadPoolHandle(handle);
2475 result = CAInitializeLEGattClient();
2476 if (CA_STATUS_OK != result)
2478 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattClient() failed");
2479 return CA_STATUS_FAILED;
2482 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
2483 CASetLEServerThreadPoolHandle(handle);
2484 result = CAInitializeLEGattServer();
2485 if (CA_STATUS_OK != result)
2487 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattServer() failed");
2488 return CA_STATUS_FAILED;
2491 CASetLEAdapterThreadPoolHandle(handle);
2492 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
2493 CASetLEReqRespAdapterCallback(reqRespCallback);
2495 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
2496 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
2497 CALERegisterNetworkNotifications(netCallback, connCallback);
2499 g_errorHandler = errorCallback;
2501 static const CAConnectivityHandler_t connHandler =
2503 .startAdapter = CAStartLE,
2504 .stopAdapter = CAStopLE,
2505 .startListenServer = CAStartLEListeningServer,
2506 .stopListenServer = CAStopLEListeningServer,
2507 .startDiscoveryServer = CAStartLEDiscoveryServer,
2508 .sendData = CASendLEUnicastData,
2509 .sendDataToAll = CASendLEMulticastData,
2510 .GetnetInfo = CAGetLEInterfaceInformation,
2511 .readData = CAReadLEData,
2512 .terminate = CATerminateLE,
2513 .cType = CA_ADAPTER_GATT_BTLE
2516 registerCallback(connHandler);
2518 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2520 return CA_STATUS_OK;
2523 static CAResult_t CAStartLE()
2525 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE");
2527 return CAStartLEAdapter();
2530 static CAResult_t CAStopLE()
2532 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2533 #ifndef SINGLE_THREAD
2537 ca_mutex_lock(g_bleIsServerMutex);
2538 switch (g_adapterType)
2540 case ADAPTER_SERVER:
2541 CALEAdapterGattServerStop();
2543 case ADAPTER_CLIENT:
2544 CALEAdapterGattClientStop();
2546 case ADAPTER_BOTH_CLIENT_SERVER:
2547 CALEAdapterGattServerStop();
2548 CALEAdapterGattClientStop();
2553 ca_mutex_unlock(g_bleIsServerMutex);
2555 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2557 return CAStopLEAdapter();
2560 static void CATerminateLE()
2562 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2564 CASetLEReqRespServerCallback(NULL);
2565 CASetLEReqRespClientCallback(NULL);
2566 CALERegisterNetworkNotifications(NULL, NULL);
2567 CASetLEReqRespAdapterCallback(NULL);
2568 CATerminateLENetworkMonitor();
2570 ca_mutex_lock(g_bleIsServerMutex);
2571 switch (g_adapterType)
2573 case ADAPTER_SERVER:
2574 CATerminateLEGattServer();
2576 case ADAPTER_CLIENT:
2577 CATerminateLEGattClient();
2579 case ADAPTER_BOTH_CLIENT_SERVER:
2580 CATerminateLEGattServer();
2581 CATerminateLEGattClient();
2586 g_adapterType = ADAPTER_EMPTY;
2587 ca_mutex_unlock(g_bleIsServerMutex);
2589 #ifndef SINGLE_THREAD
2590 CATerminateLEQueues();
2592 CATerminateLEAdapterMutex();
2594 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2597 static CAResult_t CAStartLEListeningServer()
2599 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEListeningServer");
2600 #ifndef ROUTING_GATEWAY
2601 CAResult_t result = CA_STATUS_OK;
2602 #ifndef SINGLE_THREAD
2603 result = CAInitLEServerQueues();
2604 if (CA_STATUS_OK != result)
2606 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
2611 ca_mutex_lock(g_bleIsServerMutex);
2612 switch (g_adapterType)
2614 case ADAPTER_CLIENT:
2615 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
2617 case ADAPTER_BOTH_CLIENT_SERVER:
2620 g_adapterType = ADAPTER_SERVER;
2622 ca_mutex_unlock(g_bleIsServerMutex);
2624 result = CAGetLEAdapterState();
2625 if (CA_STATUS_OK != result)
2627 if (CA_ADAPTER_NOT_ENABLED == result)
2631 "Listen Server will be started once BT Adapter is enabled");
2632 result = CA_STATUS_OK;
2637 result = CALEAdapterGattServerStart();
2640 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2643 // Routing Gateway only supports BLE client mode.
2644 OIC_LOG(ERROR, CALEADAPTER_TAG, "LE server not supported in Routing Gateway");
2645 return CA_NOT_SUPPORTED;
2649 static CAResult_t CAStopLEListeningServer()
2651 OIC_LOG(ERROR, CALEADAPTER_TAG, "Listen server stop not supported.");
2652 return CA_NOT_SUPPORTED;
2655 static CAResult_t CAStartLEDiscoveryServer()
2657 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEDiscoveryServer");
2658 CAResult_t result = CA_STATUS_OK;
2659 #ifndef SINGLE_THREAD
2660 result = CAInitLEClientQueues();
2661 if (CA_STATUS_OK != result)
2663 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
2668 ca_mutex_lock(g_bleIsServerMutex);
2669 switch (g_adapterType)
2671 case ADAPTER_SERVER:
2672 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
2674 case ADAPTER_BOTH_CLIENT_SERVER:
2677 g_adapterType = ADAPTER_CLIENT;
2679 ca_mutex_unlock(g_bleIsServerMutex);
2681 result = CAGetLEAdapterState();
2682 if (CA_STATUS_OK != result)
2684 if (CA_ADAPTER_NOT_ENABLED == result)
2688 "Discovery Server will be started once BT Adapter is enabled");
2689 result = CA_STATUS_OK;
2694 result = CALEAdapterGattClientStart();
2697 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2701 static CAResult_t CAReadLEData()
2703 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2704 #ifdef SINGLE_THREAD
2707 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2708 return CA_STATUS_OK;
2711 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
2714 CADataType_t dataType)
2716 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN - CASendLEUnicastData : type(%d)", dataType);
2719 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
2720 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
2722 CAResult_t result = CA_STATUS_FAILED;
2724 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
2725 if (ADAPTER_EMPTY == g_adapterType)
2727 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2730 ca_mutex_lock(g_bleIsServerMutex);
2731 if (ADAPTER_SERVER == g_adapterType ||
2732 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_DATA == dataType))
2734 result = CALEAdapterServerSendData(endpoint, data, dataLen);
2735 if (CA_STATUS_OK != result)
2737 ca_mutex_unlock(g_bleIsServerMutex);
2738 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed");
2741 g_errorHandler(endpoint, data, dataLen, result);
2748 if (ADAPTER_CLIENT == g_adapterType ||
2749 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_REQUEST_DATA == dataType) ||
2750 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_FOR_RES == dataType))
2752 result = CALEAdapterClientSendData(endpoint, data, dataLen);
2753 if (CA_STATUS_OK != result)
2755 ca_mutex_unlock(g_bleIsServerMutex);
2756 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for client failed" );
2760 g_errorHandler(endpoint, data, dataLen, result);
2765 ca_mutex_unlock(g_bleIsServerMutex);
2767 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2771 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
2774 CADataType_t dataType)
2776 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEMulticastData");
2779 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
2783 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
2787 CAResult_t result = CA_STATUS_FAILED;
2789 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
2790 if (ADAPTER_EMPTY == g_adapterType)
2792 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
2795 ca_mutex_lock(g_bleIsServerMutex);
2796 if (ADAPTER_SERVER == g_adapterType ||
2797 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_DATA == dataType))
2799 result = CALEAdapterServerSendData(NULL, data, dataLen);
2800 if (CA_STATUS_OK != result)
2802 ca_mutex_unlock(g_bleIsServerMutex);
2804 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data for server failed" );
2808 g_errorHandler(endpoint, data, dataLen, result);
2814 if (ADAPTER_CLIENT == g_adapterType ||
2815 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_REQUEST_DATA == dataType) ||
2816 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_FOR_RES == dataType))
2818 result = CALEAdapterClientSendData(NULL, data, dataLen);
2819 if (CA_STATUS_OK != result)
2821 ca_mutex_unlock(g_bleIsServerMutex);
2823 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data for client failed" );
2827 g_errorHandler(endpoint, data, dataLen, result);
2832 ca_mutex_unlock(g_bleIsServerMutex);
2834 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CASendLEMulticastData");
2838 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
2840 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2842 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
2844 char *local_address = NULL;
2846 CAResult_t res = CAGetLEAddress(&local_address);
2847 if (CA_STATUS_OK != res)
2849 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
2853 if (NULL == local_address)
2855 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
2856 return CA_STATUS_FAILED;
2860 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
2861 if (NULL == (*info))
2863 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
2864 OICFree(local_address);
2865 return CA_STATUS_FAILED;
2868 size_t local_address_len = strlen(local_address);
2870 if(local_address_len >= sizeof(g_localBLEAddress) ||
2871 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
2873 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
2875 OICFree(local_address);
2876 return CA_STATUS_FAILED;
2879 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
2880 ca_mutex_lock(g_bleLocalAddressMutex);
2881 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
2882 ca_mutex_unlock(g_bleLocalAddressMutex);
2884 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
2886 OICFree(local_address);
2888 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2889 return CA_STATUS_OK;
2892 static CAResult_t CALERegisterNetworkNotifications(CAAdapterChangeCallback netCallback,
2893 CAConnectionChangeCallback connCallback)
2895 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2897 ca_mutex_lock(g_bleNetworkCbMutex);
2898 g_networkCallback = netCallback;
2899 g_connectionCallback = connCallback;
2900 ca_mutex_unlock(g_bleNetworkCbMutex);
2901 CAResult_t res = CA_STATUS_OK;
2904 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
2905 if (CA_STATUS_OK != res)
2907 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2912 res = CAUnSetLEAdapterStateChangedCb();
2913 if (CA_STATUS_OK != res)
2915 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
2919 if (g_connectionCallback)
2921 res = CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCb);
2922 if (CA_STATUS_OK != res)
2924 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLENWConnectionStateChangedCb failed!");
2928 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2932 static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
2935 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEConnectionStateChangedCb");
2937 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
2941 ca_mutex_lock(g_bleIsServerMutex);
2942 switch (g_adapterType)
2944 case ADAPTER_SERVER:
2945 CALEGattServerConnectionStateChanged(isConnected, address);
2947 case ADAPTER_CLIENT:
2948 CALEGattConnectionStateChanged(isConnected, address);
2950 case ADAPTER_BOTH_CLIENT_SERVER:
2951 CALEGattConnectionStateChanged(isConnected, address);
2952 CALEGattServerConnectionStateChanged(isConnected, address);
2957 ca_mutex_unlock(g_bleIsServerMutex);
2962 #ifndef SINGLE_THREAD
2963 if(g_bleClientSenderInfo)
2965 CALERemoveReceiveQueueData(g_bleClientSenderInfo, address);
2968 if(g_bleServerSenderInfo)
2970 CALERemoveReceiveQueueData(g_bleServerSenderInfo, address);
2973 // remove data of send queue.
2974 if (g_bleClientSendQueueHandle)
2976 CALERemoveSendQueueData(g_bleClientSendQueueHandle,
2977 g_bleClientSendDataMutex,
2981 if (g_bleServerSendQueueHandle)
2983 CALERemoveSendQueueData(g_bleServerSendQueueHandle,
2984 g_bleServerSendDataMutex,
2990 CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE };
2991 OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), address);
2993 ca_mutex_lock(g_bleNetworkCbMutex);
2994 if (g_connectionCallback)
2996 g_connectionCallback(&localEndpoint, isConnected);
2998 ca_mutex_unlock(g_bleNetworkCbMutex);
3000 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3003 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
3005 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDeviceStateChangedCb");
3007 if (CA_ADAPTER_ENABLED == adapter_state)
3009 ca_mutex_lock(g_bleIsServerMutex);
3010 switch (g_adapterType)
3012 case ADAPTER_SERVER:
3013 CALEAdapterGattServerStart();
3015 case ADAPTER_CLIENT:
3016 CALEAdapterGattClientStart();
3018 case ADAPTER_BOTH_CLIENT_SERVER:
3019 CALEAdapterGattServerStart();
3020 CALEAdapterGattClientStart();
3025 ca_mutex_unlock(g_bleIsServerMutex);
3029 ca_mutex_lock(g_bleIsServerMutex);
3030 switch (g_adapterType)
3032 case ADAPTER_SERVER:
3033 CALEAdapterGattServerStop();
3035 case ADAPTER_CLIENT:
3036 CALEAdapterGattClientStop();
3038 case ADAPTER_BOTH_CLIENT_SERVER:
3039 CALEAdapterGattServerStop();
3040 CALEAdapterGattClientStop();
3045 ca_mutex_unlock(g_bleIsServerMutex);
3048 ca_mutex_lock(g_bleNetworkCbMutex);
3049 if (NULL != g_networkCallback)
3051 g_networkCallback(CA_ADAPTER_GATT_BTLE, adapter_state);
3055 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
3057 ca_mutex_unlock(g_bleNetworkCbMutex);
3059 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3062 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
3063 const uint8_t *data,
3066 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
3068 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
3069 #ifndef SINGLE_THREAD
3070 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
3071 "g_bleClientSendQueueHandle is NULL",
3073 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
3074 "g_bleClientSendDataMutex is NULL",
3077 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
3078 "g_bleClientSendQueueHandle",
3081 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
3083 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen, NULL);
3086 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
3087 return CA_MEMORY_ALLOC_FAILED;
3089 // Add message to send queue
3090 ca_mutex_lock(g_bleClientSendDataMutex);
3091 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
3092 ca_mutex_unlock(g_bleClientSendDataMutex);
3094 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3095 return CA_STATUS_OK;
3098 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
3099 const uint8_t *data,
3102 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
3104 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
3106 #ifdef SINGLE_THREAD
3107 if (!CAIsLEConnected())
3109 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
3110 return CA_STATUS_FAILED;
3113 CAResult_t result = CALEServerSendDataSingleThread(data, dataLen);
3114 if (CA_STATUS_OK != result)
3116 OIC_LOG(ERROR, CALEADAPTER_TAG, "CALEServerSendDataSingleThread failed");
3117 return CA_STATUS_FAILED;
3120 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
3121 "BleClientReceiverQueue is NULL",
3123 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
3124 "BleClientSendDataMutex is NULL",
3127 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
3130 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
3132 CALEData_t * const bleData =
3133 CACreateLEData(remoteEndpoint, data, dataLen, NULL);
3137 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
3138 return CA_MEMORY_ALLOC_FAILED;
3141 // Add message to send queue
3142 ca_mutex_lock(g_bleServerSendDataMutex);
3143 CAQueueingThreadAddData(g_bleServerSendQueueHandle,
3145 sizeof(CALEData_t));
3146 ca_mutex_unlock(g_bleServerSendDataMutex);
3148 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3149 return CA_STATUS_OK;
3152 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
3153 const uint8_t *data,
3154 uint32_t dataLength,
3155 uint32_t *sentLength)
3157 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
3160 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
3161 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
3163 #ifdef SINGLE_THREAD
3164 CALEDataReceiverHandlerSingleThread(data, dataLength);
3166 if (g_singleThreadReceiveData->totalDataLen == g_singleThreadReceiveData->recvDataLen)
3168 if(g_networkPacketReceivedCallback)
3170 // will be filled by upper layer
3171 const CASecureEndpoint_t endpoint =
3172 { .endpoint = { .adapter = CA_ADAPTER_GATT_BTLE } };
3174 g_networkPacketReceivedCallback(&endpoint,
3175 g_singleThreadReceiveData->defragData,
3176 g_singleThreadReceiveData->recvDataLen);
3178 g_singleThreadReceiveData->remoteEndpoint = NULL;
3179 OICFree(g_singleThreadReceiveData->defragData);
3180 g_singleThreadReceiveData->defragData = NULL;
3181 OICFree(g_singleThreadReceiveData);
3182 g_singleThreadReceiveData = NULL;
3185 VERIFY_NON_NULL_RET(g_bleReceiverQueue,
3187 "g_bleReceiverQueue",
3190 //Add message to data queue
3191 CAEndpoint_t * const remoteEndpoint =
3192 CACreateEndpointObject(CA_DEFAULT_FLAGS,
3193 CA_ADAPTER_GATT_BTLE,
3197 if (NULL == remoteEndpoint)
3199 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
3200 return CA_STATUS_FAILED;
3203 // Create bleData to add to queue
3206 "Data received from LE layer [%d]",
3209 CALEData_t * const bleData =
3210 CACreateLEData(remoteEndpoint, data, dataLength, g_bleServerSenderInfo);
3214 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
3215 CAFreeEndpoint(remoteEndpoint);
3216 return CA_MEMORY_ALLOC_FAILED;
3219 CAFreeEndpoint(remoteEndpoint);
3220 // Add message to receiver queue
3221 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
3223 *sentLength = dataLength;
3225 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3226 return CA_STATUS_OK;
3229 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
3230 const uint8_t *data,
3231 uint32_t dataLength,
3232 uint32_t *sentLength)
3234 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
3237 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
3238 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
3239 #ifndef SINGLE_THREAD
3240 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG,
3241 "g_bleReceiverQueue",
3244 //Add message to data queue
3245 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
3246 CA_ADAPTER_GATT_BTLE,
3248 if (NULL == remoteEndpoint)
3250 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
3251 return CA_STATUS_FAILED;
3254 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%u]", dataLength);
3256 // Create bleData to add to queue
3257 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data,
3258 dataLength, g_bleClientSenderInfo);
3261 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
3262 CAFreeEndpoint(remoteEndpoint);
3263 return CA_MEMORY_ALLOC_FAILED;
3266 CAFreeEndpoint(remoteEndpoint);
3267 // Add message to receiver queue
3268 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
3270 *sentLength = dataLength;
3272 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3273 return CA_STATUS_OK;
3276 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
3278 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
3280 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
3281 g_bleAdapterThreadPool = handle;
3282 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
3284 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3287 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
3289 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
3291 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
3293 g_networkPacketReceivedCallback = callback;
3295 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
3297 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3300 static void CALEErrorHandler(const char *remoteAddress,
3301 const uint8_t *data,
3305 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
3307 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
3309 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
3310 CA_ADAPTER_GATT_BTLE,
3314 // if required, will be used to build remote endpoint
3315 g_errorHandler(rep, data, dataLen, result);
3317 CAFreeEndpoint(rep);
3319 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");
3322 #ifndef SINGLE_THREAD
3323 static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle, ca_mutex mutex,
3324 const char* address)
3326 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALERemoveSendQueueData");
3328 VERIFY_NON_NULL_VOID(queueHandle, CALEADAPTER_TAG, "queueHandle");
3329 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
3331 ca_mutex_lock(mutex);
3332 while (u_queue_get_size(queueHandle->dataQueue) > 0)
3334 OIC_LOG(DEBUG, CALEADAPTER_TAG, "get data from queue");
3335 u_queue_message_t *message = u_queue_get_element(queueHandle->dataQueue);
3336 if (NULL != message)
3338 CALEData_t *bleData = (CALEData_t *) message->msg;
3339 if (bleData && bleData->remoteEndpoint)
3341 if (!strcmp(bleData->remoteEndpoint->addr, address))
3343 OIC_LOG(DEBUG, CALEADAPTER_TAG, "found the message of disconnected device");
3344 if (NULL != queueHandle->destroy)
3346 queueHandle->destroy(message->msg, message->size);
3350 OICFree(message->msg);
3358 ca_mutex_unlock(mutex);
3361 static void CALERemoveReceiveQueueData(u_arraylist_t *dataInfoList, const char* address)
3363 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALERemoveReceiveQueueData");
3365 VERIFY_NON_NULL_VOID(dataInfoList, CALEADAPTER_TAG, "dataInfoList");
3366 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
3368 CABLESenderInfo_t *senderInfo = NULL;
3369 uint32_t senderIndex = 0;
3371 u_arraylist_t *portList = u_arraylist_create();
3372 if (CA_STATUS_OK == CALEGetPortsFromSenderInfo(address, dataInfoList, portList))
3374 uint32_t arrayLength = u_arraylist_length(portList);
3375 for (uint32_t i = 0; i < arrayLength; i++)
3377 uint16_t port = u_arraylist_get(portList, i);
3378 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "port : %X", port);
3380 if (CA_STATUS_OK == CALEGetSenderInfo(address, port,
3381 dataInfoList, &senderInfo,
3384 u_arraylist_remove(dataInfoList, senderIndex);
3385 OICFree(senderInfo->defragData);
3386 OICFree(senderInfo->remoteEndpoint);
3387 OICFree(senderInfo);
3389 OIC_LOG(DEBUG, CALEADAPTER_TAG,
3390 "SenderInfo is removed for disconnection");
3394 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo doesn't exist");
3398 u_arraylist_destroy(portList);
3401 static CAResult_t CALEGetPortsFromSenderInfo(const char *leAddress,
3402 u_arraylist_t *senderInfoList,
3403 u_arraylist_t *portList)
3405 VERIFY_NON_NULL(leAddress,
3407 "NULL BLE address argument");
3409 const uint32_t listLength = u_arraylist_length(senderInfoList);
3410 const uint32_t addrLength = strlen(leAddress);
3412 for (uint32_t index = 0; index < listLength; index++)
3414 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(senderInfoList, index);
3415 if (!info || !(info->remoteEndpoint))
3420 if (!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
3422 u_arraylist_add(portList, (void *)info->remoteEndpoint->port);
3426 if (u_arraylist_length(portList) != 0)
3428 return CA_STATUS_OK;
3432 return CA_STATUS_FAILED;