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 "ca_adapter_net_ssl.h"
35 #include "caqueueingthread.h"
37 #if defined(__TIZEN__) || defined(__ANDROID__)
38 #include "caleserver.h"
39 #include "caleclient.h"
41 #include "oic_malloc.h"
42 #include "oic_string.h"
43 #include "caremotehandler.h"
47 * Logging tag for module name.
49 #define CALEADAPTER_TAG "OIC_CA_LE_ADAP"
52 * Stores information of all the senders.
54 * This structure will be used to track and defragment all incoming data packet.
59 uint32_t totalDataLen;
61 CAEndpoint_t *remoteEndpoint;
67 ADAPTER_BOTH_CLIENT_SERVER,
73 * mtu size to use in fragmentation logic.
74 * default value is 20 byte.
76 static uint16_t g_mtuSize = CA_DEFAULT_BLE_MTU_SIZE;
78 * Callback to provide the status of the network change to CA layer.
80 static CAAdapterChangeCallback g_networkCallback = NULL;
83 * Callback to provide the status of the connection change to CA layer.
85 static CAConnectionChangeCallback g_connectionCallback = NULL;
88 * Own port value to identify packet owner. Default port value is 1.
90 static uint8_t g_localBLESourcePort = 1;
93 * bleAddress of the local adapter. Value will be initialized to zero,
94 * and will be updated later.
96 static char g_localBLEAddress[18] = { 0 };
99 * Variable to differentiate btw GattServer and GattClient.
101 static CABLEAdapter_t g_adapterType = ADAPTER_EMPTY;
105 * Variable for storing data type during dtls encryption.
107 static CADataType_t g_dataType = CA_REQUEST_DATA;
111 * Mutex to synchronize the task to be executed on the GattServer
114 static oc_mutex g_bleIsServerMutex = NULL;
117 * Mutex to synchronize the callback to be called for the network
120 static oc_mutex g_bleNetworkCbMutex = NULL;
123 * Mutex to synchronize the updates of the local LE address of the
126 static oc_mutex g_bleLocalAddressMutex = NULL;
129 * Reference to thread pool.
131 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
134 * Mutex to synchronize the task to be pushed to thread pool.
136 static oc_mutex g_bleAdapterThreadPoolMutex = NULL;
139 * Mutex to synchronize the queing of the data from SenderQueue.
141 static oc_mutex g_bleClientSendDataMutex = NULL;
144 * Mutex to synchronize the queing of the data from ReceiverQueue.
146 static oc_mutex g_bleClientReceiveDataMutex = NULL;
149 * Mutex to synchronize the queing of the data from SenderQueue.
151 static oc_mutex g_bleServerSendDataMutex = NULL;
154 * Mutex to synchronize the queing of the data from ReceiverQueue.
156 static oc_mutex g_bleServerReceiveDataMutex = NULL;
159 * Mutex to synchronize the callback to be called for the
160 * adapterReqResponse.
162 static oc_mutex g_bleAdapterReqRespCbMutex = NULL;
165 * Callback to be called when network packet received from either
166 * GattServer or GattClient.
168 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
171 * Callback to notify error from the BLE adapter.
173 static CAErrorHandleCallback g_errorHandler = NULL;
177 * Callback to receive decrypted data from the ssl adapter.
179 static void CALESecureReceiveDataCB(const CASecureEndpoint_t *endpoint,
180 const void *data, uint32_t dataLength);
183 * Callback to receive encrypted data from the ssl adapter.
185 static void CALESecureSendDataCB(CAEndpoint_t *endpoint,
186 const void *data, uint32_t dataLength);
191 * Pointer to defragment received data from single threaded routine.
193 static CABLESenderInfo_t *g_singleThreadReceiveData = NULL;
196 * This function will be associated with the receive for single thread.
198 * This function will defragment the received data from sender
199 * respectively and will send it up to CA layer. Respective sender's
200 * header will provide the length of the data sent.
202 * @param[in] data Actual data received from the remote
204 * @param[in] dataLen Length of the data received from the
207 static void CALEDataReceiverHandlerSingleThread(const uint8_t *data,
211 * This function will be associated with the send for single threaded
214 * This function will fragment the data to the MTU of the transport
215 * and send the data in fragments to the adapters. The function will
216 * be blocked until all data is sent out from the adapter.
218 * @param[in] data Data to be transmitted from LE.
219 * @param[in] dataLen Length of the Data being transmitted.
221 static CAResult_t CALEServerSendDataSingleThread(const uint8_t *data,
226 * Register network change notification callback.
228 * @param[in] netCallback CAAdapterChangeCallback callback which will
229 * be set for the change in adapter.
230 * @param[in] connCallback CAConnectionChangeCallback callback which will
231 * be set for the change in connection.
233 * @return 0 on success otherwise a positive error value.
234 * @retval ::CA_STATUS_OK Successful.
235 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
236 * @retval ::CA_STATUS_FAILED Operation failed.
239 static CAResult_t CALERegisterNetworkNotifications(CAAdapterChangeCallback netCallback,
240 CAConnectionChangeCallback connCallback);
243 * Set the thread pool handle which is required for spawning new
246 * @param[in] handle Thread pool handle which is given by above layer
247 * for using thread creation task.
250 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
253 * Call the callback to the upper layer when the adapter state gets
256 * @param[in] adapter_state New state of the adapter to be notified to
259 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state);
262 * Call the callback to the upper layer when the device connection state gets
265 * @param[in] address LE address of the device to be notified to the upper layer.
266 * @param[in] isConnected whether connection state is connected or not.
268 static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
272 * Used to initialize all required mutex variable for LE Adapter
275 * @return 0 on success otherwise a positive error value.
276 * @retval ::CA_STATUS_OK Successful.
277 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
278 * @retval ::CA_STATUS_FAILED Operation failed.
281 static CAResult_t CAInitLEAdapterMutex();
284 * Terminate all required mutex variables for LE adapter
287 static void CATerminateLEAdapterMutex();
290 * Prepares and notify error through error callback.
292 static void CALEErrorHandler(const char *remoteAddress,
297 #ifndef SINGLE_THREAD
299 * Stop condition of Server recvhandler.
301 static bool g_dataBleServerReceiverHandlerState = false;
304 * Stop condition of Client recvhandler.
306 static bool g_dataBleClientReceiverHandlerState = false;
309 * Sender information of Server.
311 static u_arraylist_t *g_bleServerSenderInfo = NULL;
314 * Sender information of Client.
316 static u_arraylist_t *g_bleClientSenderInfo = NULL;
319 * Queue to process the outgoing packets from GATTServer.
321 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
324 * Queue to process the outgoing packets from GATTClient.
326 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
329 * Queue to process the incoming packets from GATTServer.
331 static CAQueueingThread_t *g_bleServerReceiverQueue = NULL;
334 * Queue to process the incoming packets from GATTClient.
336 static CAQueueingThread_t *g_bleClientReceiverQueue = NULL;
339 * This function will be associated with the sender queue for
342 * This function will fragment the data to the MTU of the transport
343 * and send the data in fragments to the adapters. The function will
344 * be blocked until all data is sent out from the adapter.
346 * @param[in] threadData Data pushed to the queue which contains the
347 * info about RemoteEndpoint and Data.
349 static void CALEServerSendDataThread(void *threadData);
352 * This function will be associated with the sender queue for
355 * This function will fragment the data to the MTU of the transport
356 * and send the data in fragments to the adapters. The function will
357 * be blocked until all data is sent out from the adapter.
359 * @param[in] threadData Data pushed to the queue which contains the
360 * info about RemoteEndpoint and Data.
362 static void CALEClientSendDataThread(void *threadData);
365 * This function will defragment the received data from each sender
366 * respectively and will send it up to CA layer. Respective sender's
367 * header will provide the length of the data sent.
369 * @param[in] threadData Data pushed to the queue which contains the
370 * info about RemoteEndpoint and Data.
371 * @param[in] receiverType Whether receiver is server or client.
373 static void CALEDataReceiverHandler(void *threadData, CABLEAdapter_t receiverType);
376 * This function will be associated with the receiver queue for
379 * This function will call the function CALEDataReceiverHandler()
380 * with server type to defragment the received data.
382 * @param[in] threadData Data pushed to the queue which contains the
383 * info about RemoteEndpoint and Data.
385 static void CALEServerDataReceiverHandler(void *threadData);
388 * This function will be associated with the receiver queue for
391 * This function will call the function CALEDataReceiverHandler()
392 * with client type to defragment the received data.
394 * @param[in] threadData Data pushed to the queue which contains the
395 * info about RemoteEndpoint and Data.
397 static void CALEClientDataReceiverHandler(void *threadData);
400 * This function will stop all queues created for GattServer and
401 * GattClient. All four queues will be be stopped with this function
404 static void CAStopLEQueues();
407 * This function will terminate all queues created for GattServer and
408 * GattClient. All four queues will be be terminated with this
409 * function invocations.
411 static void CATerminateLEQueues();
414 * This function will initalize the Receiver and Sender queues for
415 * GattServer. This function will in turn call the functions
416 * CAInitBleServerReceiverQueue() and CAInitBleServerSenderQueue() to
417 * initialize the queues.
419 * @return ::CA_STATUS_OK or Appropriate error code.
420 * @retval ::CA_STATUS_OK Successful.
421 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
422 * @retval ::CA_STATUS_FAILED Operation failed.
424 static CAResult_t CAInitLEServerQueues();
427 * This function will initalize the Receiver and Sender queues for
428 * GattClient. This function will inturn call the functions
429 * CAInitBleClientReceiverQueue() and CAInitBleClientSenderQueue() to
430 * initialize the queues.
432 * @return ::CA_STATUS_OK or Appropriate error code.
433 * @retval ::CA_STATUS_OK Successful.
434 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
435 * @retval ::CA_STATUS_FAILED Operation failed.
438 static CAResult_t CAInitLEClientQueues();
441 * This function will initalize the Receiver queue for
442 * GattServer. This will initialize the queue to process the function
443 * CABLEServerSendDataThread() when ever the task is added to this
446 * @return ::CA_STATUS_OK or Appropriate error code.
447 * @retval ::CA_STATUS_OK Successful.
448 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
449 * @retval ::CA_STATUS_FAILED Operation failed.
451 static CAResult_t CAInitLEServerSenderQueue();
454 * This function will initalize the Receiver queue for
455 * GattClient. This will initialize the queue to process the function
456 * CABLEClientSendDataThread() when ever the task is added to this
459 * @return ::CA_STATUS_OK or Appropriate error code.
460 * @retval ::CA_STATUS_OK Successful.
461 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
462 * @retval ::CA_STATUS_FAILED Operation failed.
464 static CAResult_t CAInitLEClientSenderQueue();
467 * This function will initialize the Receiver queue for
468 * GattServer. This will initialize the queue to process the function
469 * CALEServerDataReceiverHandler() when ever the task is added to this
472 * @return ::CA_STATUS_OK or Appropriate error code
473 * @retval ::CA_STATUS_OK Successful
474 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
475 * @retval ::CA_STATUS_FAILED Operation failed
478 static CAResult_t CAInitLEServerReceiverQueue();
481 * This function will initialize the Receiver queue for
482 * GattClient. This will initialize the queue to process the function
483 * CALEClientDataReceiverHandler() when ever the task is added to this
486 * @return ::CA_STATUS_OK or Appropriate error code
487 * @retval ::CA_STATUS_OK Successful
488 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
489 * @retval ::CA_STATUS_FAILED Operation failed
492 static CAResult_t CAInitLEClientReceiverQueue();
495 * This function will create the Data required to send it in the
498 * @param[in] remoteEndpoint Remote endpoint information of the
500 * @param[in] data Data to be transmitted from LE.
501 * @param[in] dataLength Length of the Data being transmitted.
503 * @return ::CA_STATUS_OK or Appropriate error code.
504 * @retval ::CA_STATUS_OK Successful.
505 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
506 * @retval ::CA_STATUS_FAILED Operation failed.
508 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
511 u_arraylist_t *senderInfo);
514 * Used to free the BLE information stored in the sender/receiver
517 * @param[in] bleData Information for a particular data segment.
519 static void CAFreeLEData(CALEData_t *bleData);
524 static void CALEDataDestroyer(void *data, uint32_t size);
526 #ifndef SINGLE_THREAD
528 * remove request or response data of send queue.
530 * @param[in] queueHandle queue to process the outgoing packets.
531 * @param[in] mutex mutex related to sender for client / server.
532 * @param[in] address target address to remove data in queue.
534 static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle,
536 const char* address);
539 * remove all received data of data list from receive queue.
541 * @param[in] dataInfoList received data list to remove for client / server.
542 * @param[in] address target address to remove data in queue.
544 static void CALERemoveReceiveQueueData(u_arraylist_t *dataInfoList,
545 const char* address);
548 * get received data info and positioned index from the received data list
549 * for client / server which is matched same leAddress and port.
551 * @param[in] leAddress target address to get serderInfo.
552 * @param[in] port target port to get serderInfo.
553 * @param[in] senderInfoList received data list for client / server.
554 * @param[out] senderInfo Pointer to contain matched(leAddress and port)
555 * received data info.
556 * @param[out] senderIndex Pointer to contain matched(leAddress and port)
557 * received data info index.
559 static CAResult_t CALEGetSenderInfo(const char *leAddress,
561 u_arraylist_t *senderInfoList,
562 CABLESenderInfo_t **senderInfo,
563 uint32_t *senderIndex);
566 * get ports related to remote address. It is need because multi application
567 * can have more than 2 senderInfo using same BLE address. So before remove
568 * receive queue data, should get port list from sender Info.
570 * @param[in] leAddress target address to get port in serderInfo.
571 * @param[in] senderInfoList received data list to remove for client / server.
572 * @param[out] portList target port list related to leAddress.
574 static CAResult_t CALEGetPortsFromSenderInfo(const char *leAddress,
575 u_arraylist_t *senderInfoList,
576 u_arraylist_t *portList);
579 static CAResult_t CAInitLEServerQueues()
581 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
583 oc_mutex_lock(g_bleAdapterThreadPoolMutex);
585 CAResult_t result = CAInitLEServerSenderQueue();
586 if (CA_STATUS_OK != result)
588 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
589 oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
590 return CA_STATUS_FAILED;
593 g_bleServerSenderInfo = u_arraylist_create();
594 if (!g_bleServerSenderInfo)
596 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
597 oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
598 return CA_MEMORY_ALLOC_FAILED;
601 result = CAInitLEServerReceiverQueue();
602 if (CA_STATUS_OK != result)
604 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerReceiverQueue failed");
605 u_arraylist_free(&g_bleServerSenderInfo);
606 oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
607 return CA_STATUS_FAILED;
610 g_dataBleServerReceiverHandlerState = true;
612 oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
614 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
618 static CAResult_t CAInitLEClientQueues()
620 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
622 oc_mutex_lock(g_bleAdapterThreadPoolMutex);
624 CAResult_t result = CAInitLEClientSenderQueue();
625 if (CA_STATUS_OK != result)
627 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
628 oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
629 return CA_STATUS_FAILED;
632 g_bleClientSenderInfo = u_arraylist_create();
633 if (!g_bleClientSenderInfo)
635 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
636 oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
637 return CA_MEMORY_ALLOC_FAILED;
640 result = CAInitLEClientReceiverQueue();
641 if (CA_STATUS_OK != result)
643 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientReceiverQueue failed");
644 u_arraylist_free(&g_bleClientSenderInfo);
645 oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
646 return CA_STATUS_FAILED;
649 g_dataBleClientReceiverHandlerState = true;
651 oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
653 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
657 static CAResult_t CAInitLEServerReceiverQueue()
659 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEServerReceiverQueue");
660 // Check if the message queue is already initialized
661 if (g_bleServerReceiverQueue)
663 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
667 // Create recv message queue
668 g_bleServerReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
669 if (!g_bleServerReceiverQueue)
671 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
672 return CA_MEMORY_ALLOC_FAILED;
675 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerReceiverQueue,
676 g_bleAdapterThreadPool,
677 CALEServerDataReceiverHandler,
680 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize server receiver queue thread");
681 OICFree(g_bleServerReceiverQueue);
682 g_bleServerReceiverQueue = NULL;
683 return CA_STATUS_FAILED;
686 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerReceiverQueue))
688 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
689 OICFree(g_bleServerReceiverQueue);
690 g_bleServerReceiverQueue = NULL;
691 return CA_STATUS_FAILED;
694 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
698 static CAResult_t CAInitLEClientReceiverQueue()
700 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEClientReceiverQueue");
701 // Check if the message queue is already initialized
702 if (g_bleClientReceiverQueue)
704 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
708 // Create recv message queue
709 g_bleClientReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
710 if (!g_bleClientReceiverQueue)
712 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
713 return CA_MEMORY_ALLOC_FAILED;
716 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientReceiverQueue,
717 g_bleAdapterThreadPool,
718 CALEClientDataReceiverHandler,
721 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize client receiver queue thread");
722 OICFree(g_bleClientReceiverQueue);
723 g_bleClientReceiverQueue = NULL;
724 return CA_STATUS_FAILED;
727 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientReceiverQueue))
729 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
730 OICFree(g_bleClientReceiverQueue);
731 g_bleClientReceiverQueue = NULL;
732 return CA_STATUS_FAILED;
735 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
739 static CAResult_t CAInitLEServerSenderQueue()
741 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEServerSenderQueue");
742 // Check if the message queue is already initialized
743 if (g_bleServerSendQueueHandle)
745 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
749 // Create send message queue
750 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
751 if (!g_bleServerSendQueueHandle)
753 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
754 return CA_MEMORY_ALLOC_FAILED;
757 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
758 g_bleAdapterThreadPool,
759 CALEServerSendDataThread,
762 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
763 OICFree(g_bleServerSendQueueHandle);
764 g_bleServerSendQueueHandle = NULL;
765 return CA_STATUS_FAILED;
768 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
772 static void CALEClearSenderInfoImpl(u_arraylist_t ** list)
774 const size_t length = u_arraylist_length(*list);
775 for (size_t i = 0; i < length; ++i)
777 CABLESenderInfo_t * const info =
778 (CABLESenderInfo_t *) u_arraylist_get(*list, i);
781 OICFree(info->defragData);
782 CAFreeEndpoint(info->remoteEndpoint);
786 u_arraylist_free(list);
789 static void CALEClearSenderInfo()
791 CALEClearSenderInfoImpl(&g_bleServerSenderInfo);
792 CALEClearSenderInfoImpl(&g_bleClientSenderInfo);
795 static CAResult_t CAInitLEClientSenderQueue()
797 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEClientSenderQueue");
799 if (g_bleClientSendQueueHandle)
801 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
805 // Create send message queue
806 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
807 if (!g_bleClientSendQueueHandle)
809 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
810 return CA_MEMORY_ALLOC_FAILED;
813 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
814 g_bleAdapterThreadPool,
815 CALEClientSendDataThread, CALEDataDestroyer))
817 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
818 OICFree(g_bleClientSendQueueHandle);
819 g_bleClientSendQueueHandle = NULL;
820 return CA_STATUS_FAILED;
823 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAInitLEClientSenderQueue");
827 static void CAStopLEQueues()
829 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStopLEQueues");
831 oc_mutex_lock(g_bleServerReceiveDataMutex);
832 if (NULL != g_bleServerReceiverQueue)
834 CAQueueingThreadStop(g_bleServerReceiverQueue);
836 oc_mutex_unlock(g_bleServerReceiveDataMutex);
838 oc_mutex_lock(g_bleClientReceiveDataMutex);
839 if (NULL != g_bleClientReceiverQueue)
841 CAQueueingThreadStop(g_bleClientReceiverQueue);
843 oc_mutex_unlock(g_bleClientReceiveDataMutex);
845 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAStopLEQueues");
848 static void CATerminateLEQueues()
850 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
852 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
853 OICFree(g_bleClientSendQueueHandle);
854 g_bleClientSendQueueHandle = NULL;
856 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
857 OICFree(g_bleServerSendQueueHandle);
858 g_bleServerSendQueueHandle = NULL;
860 CAQueueingThreadDestroy(g_bleServerReceiverQueue);
861 OICFree(g_bleServerReceiverQueue);
862 g_bleServerReceiverQueue = NULL;
864 CAQueueingThreadDestroy(g_bleClientReceiverQueue);
865 OICFree(g_bleClientReceiverQueue);
866 g_bleClientReceiverQueue = NULL;
868 CALEClearSenderInfo();
870 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
873 static CAResult_t CALEGetSenderInfo(const char *leAddress,
875 u_arraylist_t *senderInfoList,
876 CABLESenderInfo_t **senderInfo,
877 uint32_t *senderIndex)
879 VERIFY_NON_NULL_RET(leAddress,
881 "NULL BLE address argument",
882 CA_STATUS_INVALID_PARAM);
883 VERIFY_NON_NULL_RET(senderIndex,
885 "NULL index argument",
886 CA_STATUS_INVALID_PARAM);
888 const uint32_t listLength = u_arraylist_length(senderInfoList);
889 const uint32_t addrLength = strlen(leAddress);
890 for (uint32_t index = 0; index < listLength; index++)
892 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(senderInfoList, index);
893 if (!info || !(info->remoteEndpoint))
898 if (!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
900 if (info->remoteEndpoint->port == port)
902 *senderIndex = index;
912 return CA_STATUS_FAILED;
915 static void CALEDataReceiverHandler(void *threadData, CABLEAdapter_t receiverType)
917 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDataReceiverHandler");
919 oc_mutex bleReceiveDataMutex = NULL;
920 bool dataBleReceiverHandlerState = false;
922 switch (receiverType)
925 bleReceiveDataMutex = g_bleClientReceiveDataMutex;
926 dataBleReceiverHandlerState = g_dataBleClientReceiverHandlerState;
929 bleReceiveDataMutex = g_bleServerReceiveDataMutex;
930 dataBleReceiverHandlerState = g_dataBleServerReceiverHandlerState;
933 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Unsupported receiver type : %d", receiverType);
937 oc_mutex_lock(bleReceiveDataMutex);
939 if (dataBleReceiverHandlerState)
941 CALEData_t *bleData = (CALEData_t *) threadData;
944 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
945 oc_mutex_unlock(bleReceiveDataMutex);
949 if (!(bleData->senderInfo))
951 OIC_LOG(ERROR, CALEADAPTER_TAG, "sender info is not available");
952 oc_mutex_unlock(bleReceiveDataMutex);
956 if (!(bleData->remoteEndpoint))
958 OIC_LOG(ERROR, CALEADAPTER_TAG, "RemoteEndPoint NULL!!");
959 oc_mutex_unlock(bleReceiveDataMutex);
963 CABLESenderInfo_t *senderInfo = NULL;
964 uint32_t senderIndex = 0;
967 CABLEPacketStart_t startFlag = CA_BLE_PACKET_NOT_START;
968 CABLEPacketSecure_t secureFlag = CA_BLE_PACKET_NON_SECURE;
969 uint16_t sourcePort = 0;
970 uint16_t destPort = 0;
972 CAParseHeader(bleData->data, &startFlag, &sourcePort, &secureFlag, &destPort);
973 OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
974 "header info: startFlag[%X] sourcePort[%d] secureFlag[%X] destPort[%d]",
975 startFlag, sourcePort, secureFlag, destPort);
977 if (destPort != g_localBLESourcePort && destPort != CA_BLE_MULTICAST_PORT)
979 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
980 "this packet is not valid for this app(port mismatch[mine:%d, packet:%d])",
981 g_localBLESourcePort, destPort);
982 oc_mutex_unlock(bleReceiveDataMutex);
986 bleData->remoteEndpoint->port = sourcePort;
988 if (CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
989 bleData->remoteEndpoint->port,
991 &senderInfo, &senderIndex))
993 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s:%X]",
994 bleData->remoteEndpoint->addr, bleData->remoteEndpoint->port);
1000 OIC_LOG(ERROR, CALEADAPTER_TAG,
1001 "This packet is start packet but exist senderInfo. Remove senderInfo");
1002 u_arraylist_remove(bleData->senderInfo, senderIndex);
1003 OICFree(senderInfo->defragData);
1004 OICFree(senderInfo);
1012 uint32_t totalLength = 0;
1015 CAParseHeaderPayloadLength(bleData->data, CA_BLE_LENGTH_HEADER_SIZE, &totalLength);
1019 OIC_LOG(ERROR, CALEADAPTER_TAG, "This packet is wrong packet! ignore.");
1020 oc_mutex_unlock(bleReceiveDataMutex);
1024 CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
1027 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
1028 oc_mutex_unlock(bleReceiveDataMutex);
1031 newSender->recvDataLen = 0;
1032 newSender->totalDataLen = 0;
1033 newSender->defragData = NULL;
1034 newSender->remoteEndpoint = NULL;
1036 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
1038 newSender->totalDataLen = totalLength;
1040 if (!(newSender->totalDataLen))
1042 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
1044 oc_mutex_unlock(bleReceiveDataMutex);
1048 size_t dataOnlyLen =
1049 bleData->dataLen - (CA_BLE_HEADER_SIZE + CA_BLE_LENGTH_HEADER_SIZE);
1050 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
1051 newSender->totalDataLen);
1052 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%zu] bytes",
1055 newSender->defragData = OICCalloc(newSender->totalDataLen + 1,
1056 sizeof(*newSender->defragData));
1058 if (NULL == newSender->defragData)
1060 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
1062 oc_mutex_unlock(bleReceiveDataMutex);
1065 CATransportFlags_t flags = CA_DEFAULT_FLAGS;
1066 #ifdef __WITH_DTLS__
1067 if (CA_BLE_PACKET_SECURE == secureFlag)
1073 const char *remoteAddress = bleData->remoteEndpoint->addr;
1074 newSender->remoteEndpoint = CACreateEndpointObject(flags,
1075 CA_ADAPTER_GATT_BTLE,
1077 bleData->remoteEndpoint->port);
1079 if (NULL == newSender->remoteEndpoint)
1081 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
1082 OICFree(newSender->defragData);
1084 oc_mutex_unlock(bleReceiveDataMutex);
1088 if (newSender->recvDataLen + dataOnlyLen > newSender->totalDataLen)
1090 OIC_LOG(ERROR, CALEADAPTER_TAG, "buffer is smaller than received data");
1091 OICFree(newSender->defragData);
1092 CAFreeEndpoint(newSender->remoteEndpoint);
1094 oc_mutex_unlock(bleReceiveDataMutex);
1097 memcpy(newSender->defragData,
1098 bleData->data + (CA_BLE_HEADER_SIZE + CA_BLE_LENGTH_HEADER_SIZE),
1100 newSender->recvDataLen += dataOnlyLen;
1102 u_arraylist_add(bleData->senderInfo,(void *)newSender);
1104 //Getting newSender index position in bleSenderInfo array list
1106 CALEGetSenderInfo(newSender->remoteEndpoint->addr,
1107 newSender->remoteEndpoint->port,
1108 bleData->senderInfo,
1109 NULL, &senderIndex))
1111 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
1112 OICFree(newSender->defragData);
1113 CAFreeEndpoint(newSender->remoteEndpoint);
1115 oc_mutex_unlock(bleReceiveDataMutex);
1118 senderInfo = newSender;
1122 size_t dataOnlyLen = bleData->dataLen - CA_BLE_HEADER_SIZE;
1123 if (senderInfo->recvDataLen + dataOnlyLen > senderInfo->totalDataLen)
1125 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1126 "Data Length exceeding error!! Receiving [%zu] total length [%u]",
1127 senderInfo->recvDataLen + dataOnlyLen, senderInfo->totalDataLen);
1128 u_arraylist_remove(bleData->senderInfo, senderIndex);
1129 OICFree(senderInfo->defragData);
1130 OICFree(senderInfo);
1131 oc_mutex_unlock(bleReceiveDataMutex);
1134 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%zu]",
1136 memcpy(senderInfo->defragData + senderInfo->recvDataLen,
1137 bleData->data + CA_BLE_HEADER_SIZE,
1139 senderInfo->recvDataLen += dataOnlyLen;
1140 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] received Datalen [%d]",
1141 senderInfo->totalDataLen, senderInfo->recvDataLen);
1144 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
1146 oc_mutex_lock(g_bleAdapterReqRespCbMutex);
1147 if (NULL == g_networkPacketReceivedCallback)
1149 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1151 u_arraylist_remove(bleData->senderInfo, senderIndex);
1152 OICFree(senderInfo->defragData);
1153 OICFree(senderInfo);
1154 oc_mutex_unlock(g_bleAdapterReqRespCbMutex);
1155 oc_mutex_unlock(bleReceiveDataMutex);
1159 OIC_LOG(DEBUG, CALEADAPTER_TAG, "[CALEDataReceiverHandler] Received data up !");
1161 const CASecureEndpoint_t tmp =
1163 .endpoint = *senderInfo->remoteEndpoint
1166 #ifdef __WITH_DTLS__
1167 if (CA_SECURE & tmp.endpoint.flags)
1169 OIC_LOG(ERROR, CALEADAPTER_TAG, "Secure data received");
1170 if (CA_STATUS_FAILED == CAdecryptSsl(&tmp,
1171 senderInfo->defragData,
1172 senderInfo->recvDataLen))
1174 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAdecryptSsl failed");
1178 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAdecryptSsl successed");
1180 OICFree(senderInfo->defragData);
1185 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Non-Secure data received");
1186 g_networkPacketReceivedCallback(&tmp,
1187 senderInfo->defragData,
1188 senderInfo->recvDataLen);
1189 #ifdef __WITH_DTLS__
1193 oc_mutex_unlock(g_bleAdapterReqRespCbMutex);
1194 u_arraylist_remove(bleData->senderInfo, senderIndex);
1195 senderInfo->remoteEndpoint = NULL;
1196 senderInfo->defragData = NULL;
1197 OICFree(senderInfo);
1200 oc_mutex_unlock(bleReceiveDataMutex);
1201 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CALEDataReceiverHandler");
1205 static void CALEServerDataReceiverHandler(void *threadData)
1207 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEServerDataReceiverHandler");
1208 CALEDataReceiverHandler(threadData, ADAPTER_SERVER);
1209 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1212 static void CALEClientDataReceiverHandler(void *threadData)
1214 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEClientDataReceiverHandler");
1215 CALEDataReceiverHandler(threadData, ADAPTER_CLIENT);
1216 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1219 static void CALEServerSendDataThread(void *threadData)
1221 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEServerSendDataThread");
1223 CALEData_t * const bleData = (CALEData_t *) threadData;
1226 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1230 if (!bleData->remoteEndpoint)
1232 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid endpoint of bledata!");
1236 #if defined(__TIZEN__) || defined(__ANDROID__)
1238 g_mtuSize = CALEServerGetMtuSize(bleData->remoteEndpoint->addr);
1240 OIC_LOG_V(INFO, CALEADAPTER_TAG, "MTU size [%d]", g_mtuSize);
1242 uint32_t midPacketCount = 0;
1243 size_t remainingLen = 0;
1244 size_t totalLength = 0;
1245 CABLEPacketSecure_t secureFlag = CA_BLE_PACKET_NON_SECURE;
1247 CAResult_t result = CAGenerateVariableForFragmentation(bleData->dataLen,
1253 if (CA_STATUS_OK != result)
1255 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1256 "CAGenerateVariableForFragmentation failed, result [%d]", result);
1257 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1261 OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
1262 "Packet info: data size[%d] midPacketCount[%u] remainingLen[%zu] totalLength[%zu]",
1263 bleData->dataLen, midPacketCount, remainingLen, totalLength);
1267 "Server total Data length with header is [%zu]",
1270 uint8_t dataSegment[CA_SUPPORTED_BLE_MTU_SIZE] = {0};
1271 uint8_t dataHeader[CA_BLE_HEADER_SIZE] = {0};
1273 if (NULL != bleData->remoteEndpoint) //Unicast Data
1275 secureFlag = (bleData->remoteEndpoint->flags & CA_SECURE) ?
1276 CA_BLE_PACKET_SECURE : CA_BLE_PACKET_NON_SECURE;
1278 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This Packet is secure? %d", secureFlag);
1279 result = CAGenerateHeader(dataHeader,
1280 CA_BLE_PACKET_START,
1281 g_localBLESourcePort,
1283 bleData->remoteEndpoint->port);
1285 else //Multicast Data
1287 result = CAGenerateHeader(dataHeader,
1288 CA_BLE_PACKET_START,
1289 g_localBLESourcePort,
1291 CA_BLE_MULTICAST_PORT);
1294 if (CA_STATUS_OK != result)
1296 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1297 "CAGenerateHeader failed, result [%d]", result);
1298 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1302 uint8_t lengthHeader[CA_BLE_LENGTH_HEADER_SIZE] = {0};
1303 result = CAGenerateHeaderPayloadLength(lengthHeader,
1304 CA_BLE_LENGTH_HEADER_SIZE,
1307 if (CA_STATUS_OK != result)
1309 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1310 "CAGenerateHeaderPayloadLength failed, result [%d]", result);
1311 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1315 uint32_t length = 0;
1316 uint32_t dataLen = 0;
1317 if (g_mtuSize > totalLength)
1319 length = totalLength;
1320 dataLen = bleData->dataLen;
1325 dataLen = g_mtuSize - CA_BLE_HEADER_SIZE - CA_BLE_LENGTH_HEADER_SIZE;
1328 result = CAMakeFirstDataSegment(dataSegment,
1329 bleData->data, dataLen,
1330 dataHeader, lengthHeader);
1332 if (CA_STATUS_OK != result)
1334 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1335 "Making data segment failed, result [%d]", result);
1336 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1340 const uint32_t iter = midPacketCount;
1343 // Send the first segment with the header.
1344 if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
1346 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
1348 result = CAUpdateCharacteristicsToGattClient(
1349 bleData->remoteEndpoint->addr, dataSegment, length);
1351 if (CA_STATUS_OK != result)
1355 "Update characteristics failed, result [%d]",
1358 g_errorHandler(bleData->remoteEndpoint,
1367 "Server Sent Unicast First Data - data length [%zu]",
1370 result = CAGenerateHeader(dataHeader,
1371 CA_BLE_PACKET_NOT_START,
1372 g_localBLESourcePort,
1374 bleData->remoteEndpoint->port);
1376 if (CA_STATUS_OK != result)
1378 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1379 "CAGenerateHeader failed, result [%d]", result);
1380 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1384 for (index = 0; index < iter; index++)
1386 // Send the remaining header.
1387 result = CAMakeRemainDataSegment(dataSegment,
1388 g_mtuSize - CA_BLE_HEADER_SIZE,
1395 if (CA_STATUS_OK != result)
1397 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1398 "Making data segment failed, result [%d]", result);
1399 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1404 CAUpdateCharacteristicsToGattClient(
1405 bleData->remoteEndpoint->addr,
1409 if (CA_STATUS_OK != result)
1411 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1412 "Update characteristics failed, result [%d]", result);
1413 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1416 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
1420 if (remainingLen && (totalLength > g_mtuSize))
1422 // send the last segment of the data (Ex: 22 bytes of 622
1423 // bytes of data when MTU is 200)
1424 result = CAMakeRemainDataSegment(dataSegment,
1432 if (CA_STATUS_OK != result)
1434 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1435 "Making data segment failed, result [%d]", result);
1436 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1440 result = CAUpdateCharacteristicsToGattClient(
1441 bleData->remoteEndpoint->addr,
1443 remainingLen + CA_BLE_HEADER_SIZE);
1445 if (CA_STATUS_OK != result)
1449 "Update characteristics failed, result [%d]",
1451 g_errorHandler(bleData->remoteEndpoint,
1459 "Server Sent Unicast Last Data - data length [%zu]",
1460 remainingLen + CA_BLE_HEADER_SIZE);
1465 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
1466 #if defined(__TIZEN__)
1468 // tizen ble should also disabled when Tizen 3.0 is updated.
1469 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
1470 if (CA_STATUS_OK != result)
1472 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1474 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1478 result = CAGenerateHeader(dataHeader,
1479 CA_BLE_PACKET_NOT_START,
1480 g_localBLESourcePort,
1482 CA_BLE_MULTICAST_PORT);
1484 if (CA_STATUS_OK != result)
1486 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1487 "CAGenerateHeader failed, result [%d]", result);
1488 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1493 "Server Sent Multicast First Data - data length [%zu]",
1496 for (index = 0; index < iter; index++)
1498 // Send the remaining header.
1499 result = CAMakeRemainDataSegment(dataSegment,
1500 CA_BLE_NORMAL_SEGMENT_PAYLOAD_SIZE,
1506 if (CA_STATUS_OK != result)
1508 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1509 "Making data segment failed, result [%d]", result);
1510 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1514 result = CAUpdateCharacteristicsToAllGattClients(
1516 CA_SUPPORTED_BLE_MTU_SIZE);
1518 if (CA_STATUS_OK != result)
1520 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1522 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1527 "Server Sent Multicast %d Data - data length [%zu]",
1529 CA_SUPPORTED_BLE_MTU_SIZE);
1532 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1534 // send the last segment of the data (Ex: 22 bytes of 622
1535 // bytes of data when MTU is 200)
1536 result = CAMakeRemainDataSegment(dataSegment,
1543 if (CA_STATUS_OK != result)
1545 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1546 "Making data segment failed, result [%d]", result);
1547 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1551 result = CAUpdateCharacteristicsToAllGattClients(
1553 remainingLen + CA_BLE_HEADER_SIZE);
1555 if (CA_STATUS_OK != result)
1557 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1559 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1564 "Server Sent Multicast Last Data - data length [%zu]",
1565 remainingLen + CA_BLE_HEADER_SIZE);
1568 OIC_LOG(DEBUG, CALEADAPTER_TAG, "BLE Multicast is not supported");
1572 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CALEServerSendDataThread");
1575 static void CALEClientSendDataThread(void *threadData)
1577 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEClientSendDataThread");
1579 CALEData_t *bleData = (CALEData_t *) threadData;
1582 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1586 if (!bleData->remoteEndpoint)
1588 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid endpoint of bledata!");
1592 #if defined(__TIZEN__) || defined(__ANDROID__)
1594 if (false == CALEClientIsConnected(bleData->remoteEndpoint->addr))
1596 // triggering to gatt connect and MTU negotiation
1597 CAResult_t res = CALEClientSendNegotiationMessage(
1598 bleData->remoteEndpoint->addr);
1600 if (CA_STATUS_OK != res)
1604 "CALEClientSendNegotiationMessage has failed, result [%d]",
1607 g_errorHandler(bleData->remoteEndpoint,
1614 g_mtuSize = CALEClientGetMtuSize(bleData->remoteEndpoint->addr);
1616 OIC_LOG_V(INFO, CALEADAPTER_TAG, "MTU size [%d]", g_mtuSize);
1618 uint32_t midPacketCount = 0;
1619 size_t remainingLen = 0;
1620 size_t totalLength = 0;
1621 CABLEPacketSecure_t secureFlag = CA_BLE_PACKET_NON_SECURE;
1623 CAResult_t result = CAGenerateVariableForFragmentation(bleData->dataLen,
1629 if (CA_STATUS_OK != result)
1631 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1632 "CAGenerateVariableForFragmentation failed, result [%d]", result);
1633 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1637 OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
1638 "Packet info: data size[%d] midPacketCount[%u] remainingLen[%zu] totalLength[%zu]",
1639 bleData->dataLen, midPacketCount, remainingLen, totalLength);
1641 uint8_t dataSegment[CA_SUPPORTED_BLE_MTU_SIZE] = {0};
1642 uint8_t dataHeader[CA_BLE_HEADER_SIZE] = {0};
1644 if (NULL != bleData->remoteEndpoint) //Unicast Data
1646 secureFlag = (bleData->remoteEndpoint->flags & CA_SECURE) ?
1647 CA_BLE_PACKET_SECURE : CA_BLE_PACKET_NON_SECURE;
1649 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This Packet is secure? %d", secureFlag);
1650 result = CAGenerateHeader(dataHeader,
1651 CA_BLE_PACKET_START,
1652 g_localBLESourcePort,
1654 bleData->remoteEndpoint->port);
1656 else //Multicast Data
1658 result = CAGenerateHeader(dataHeader,
1659 CA_BLE_PACKET_START,
1660 g_localBLESourcePort,
1662 CA_BLE_MULTICAST_PORT);
1665 if (CA_STATUS_OK != result)
1667 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1668 "CAGenerateHeader failed, result [%d]", result);
1669 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1673 uint8_t lengthHeader[CA_BLE_LENGTH_HEADER_SIZE] = {0};
1674 result = CAGenerateHeaderPayloadLength(lengthHeader,
1675 CA_BLE_LENGTH_HEADER_SIZE,
1678 if (CA_STATUS_OK != result)
1680 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1681 "CAGenerateHeaderPayloadLength failed, result [%d]", result);
1682 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1686 uint32_t length = 0;
1687 uint32_t dataLen = 0;
1688 if (g_mtuSize > totalLength)
1690 length = totalLength;
1691 dataLen = bleData->dataLen;
1696 dataLen = g_mtuSize - CA_BLE_HEADER_SIZE - CA_BLE_LENGTH_HEADER_SIZE;
1699 result = CAMakeFirstDataSegment(dataSegment,
1700 bleData->data, dataLen,
1701 dataHeader, lengthHeader);
1703 if (CA_STATUS_OK != result)
1705 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1706 "Making data segment failed, result [%d]", result);
1707 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1711 const uint32_t iter = midPacketCount;
1713 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1715 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1716 // Send the first segment with the header.
1718 CAUpdateCharacteristicsToGattServer(
1719 bleData->remoteEndpoint->addr,
1725 if (CA_STATUS_OK != result)
1729 "Update characteristics failed, result [%d]",
1731 g_errorHandler(bleData->remoteEndpoint,
1739 "Client Sent Unicast First Data - data length [%zu]",
1742 result = CAGenerateHeader(dataHeader,
1743 CA_BLE_PACKET_NOT_START,
1744 g_localBLESourcePort,
1746 bleData->remoteEndpoint->port);
1748 if (CA_STATUS_OK != result)
1750 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1751 "CAGenerateHeader failed, result [%d]", result);
1752 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1756 for (index = 0; index < iter; index++)
1758 result = CAMakeRemainDataSegment(dataSegment,
1759 g_mtuSize - CA_BLE_HEADER_SIZE,
1766 if (CA_STATUS_OK != result)
1768 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1769 "Making data segment failed, result [%d]", result);
1770 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1774 // Send the remaining header.
1775 result = CAUpdateCharacteristicsToGattServer(
1776 bleData->remoteEndpoint->addr,
1781 if (CA_STATUS_OK != result)
1785 "Update characteristics failed, result [%d]",
1787 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1792 "Client Sent Unicast %d Data - data(mtu) length [%zu]",
1797 if (remainingLen && (totalLength > g_mtuSize))
1799 // send the last segment of the data (Ex: 22 bytes of 622
1800 // bytes of data when MTU is 200)
1801 result = CAMakeRemainDataSegment(dataSegment,
1809 if (CA_STATUS_OK != result)
1811 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1812 "Making data segment failed, result [%d]", result);
1813 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1817 result = CAUpdateCharacteristicsToGattServer(
1818 bleData->remoteEndpoint->addr,
1820 remainingLen + CA_BLE_HEADER_SIZE,
1823 if (CA_STATUS_OK != result)
1825 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1827 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1832 "Client Sent Unicast Last Data - data length [%zu]",
1833 remainingLen + CA_BLE_HEADER_SIZE);
1838 #if defined(__TIZEN__)
1840 // tizen ble should also disabled when Tizen 3.0 is updated.
1841 //Sending Mulitcast Data
1842 // Send the first segment with the header.
1843 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Client Sending Multicast Data");
1844 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1845 if (CA_STATUS_OK != result)
1847 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1848 "Update characteristics (all) failed, result [%d]", result);
1849 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1853 result = CAGenerateHeader(dataHeader,
1854 CA_BLE_PACKET_NOT_START,
1855 g_localBLESourcePort,
1859 if (CA_STATUS_OK != result)
1861 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1862 "CAGenerateHeader failed, result [%d]", result);
1863 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1868 "Client Sent Multicast First Data - data length [%zu]",
1871 for (index = 0; index < iter; index++)
1873 // Send the remaining header.
1874 result = CAMakeRemainDataSegment(dataSegment,
1875 CA_BLE_NORMAL_SEGMENT_PAYLOAD_SIZE,
1881 if (CA_STATUS_OK != result)
1883 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1884 "Making data segment failed, result [%d]", result);
1885 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1889 result = CAUpdateCharacteristicsToAllGattServers(
1891 CA_SUPPORTED_BLE_MTU_SIZE);
1893 if (CA_STATUS_OK != result)
1895 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1897 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1902 "Client Sent Multicast %d Data - data length [%zu]",
1904 CA_SUPPORTED_BLE_MTU_SIZE);
1907 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1909 // send the last segment of the data (Ex: 22 bytes of 622
1910 // bytes of data when MTU is 200)
1911 result = CAMakeRemainDataSegment(dataSegment,
1918 if (CA_STATUS_OK != result)
1920 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1921 "Making data segment failed, result [%d]", result);
1922 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1927 CAUpdateCharacteristicsToAllGattServers(
1929 remainingLen + CA_BLE_HEADER_SIZE);
1931 if (CA_STATUS_OK != result)
1933 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1934 "Update characteristics (all) failed, result [%d]", result);
1935 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1940 "Client Sent Multicast Last Data - data length [%zu]",
1941 remainingLen + CA_BLE_HEADER_SIZE);
1944 OIC_LOG(DEBUG, CALEADAPTER_TAG, "BLE Multicast is not supported");
1948 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1951 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
1952 const uint8_t *data,
1953 uint32_t dataLength,
1954 u_arraylist_t *senderInfo)
1956 CALEData_t * const bleData = OICMalloc(sizeof(CALEData_t));
1960 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1964 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1965 bleData->data = OICCalloc(dataLength + 1, 1);
1967 if (NULL == bleData->data)
1969 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1970 CAFreeLEData(bleData);
1974 memcpy(bleData->data, data, dataLength);
1975 bleData->dataLen = dataLength;
1978 bleData->senderInfo = senderInfo;
1984 static void CAFreeLEData(CALEData_t *bleData)
1986 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1988 CAFreeEndpoint(bleData->remoteEndpoint);
1989 OICFree(bleData->data);
1993 static void CALEDataDestroyer(void *data, uint32_t size)
1995 if ((size_t)size < sizeof(CALEData_t *))
1997 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1998 "Destroy data too small %p %d", data, size);
2000 CALEData_t *ledata = (CALEData_t *) data;
2002 CAFreeLEData(ledata);
2006 #ifdef SINGLE_THREAD
2007 static void CALEDataReceiverHandlerSingleThread(const uint8_t *data,
2010 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2012 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2015 CABLEPacketStart_t startFlag = CA_BLE_PACKET_NOT_START;
2016 CABLEPacketSecure_t secureFlag = CA_BLE_PACKET_NON_SECURE;
2017 uint16_t sourcePort = 0;
2018 uint16_t destPort = 0;
2020 CAParseHeader(data, &startFlag, &sourcePort, &secureFlag, &destPort);
2021 OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
2022 "header info: startFlag[%X] sourcePort[%d] secureFlag[%X] destPort[%d]",
2023 startFlag, sourcePort, secureFlag, destPort);
2025 if (destPort != g_localBLESourcePort && destPort != CA_BLE_MULTICAST_PORT)
2027 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
2028 "this packet is not valid for this app(port mismatch[mine:%d, packet:%d])",
2029 g_localBLESourcePort, destPort);
2035 if (g_singleThreadReceiveData)
2037 OIC_LOG(ERROR, CALEADAPTER_TAG,
2038 "This packet is start packet but exist senderInfo. Remove senderInfo");
2039 OICFree(g_singleThreadReceiveData->defragData);
2040 OICFree(g_singleThreadReceiveData);
2041 g_singleThreadReceiveData = NULL;
2044 uint32_t totalLength = 0;
2045 CAParseHeaderPayloadLength(data, CA_BLE_LENGTH_HEADER_SIZE, &totalLength);
2047 g_singleThreadReceiveData = OICMalloc(sizeof(CABLESenderInfo_t));
2049 if (!g_singleThreadReceiveData)
2051 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
2054 g_singleThreadReceiveData->recvDataLen = 0;
2055 g_singleThreadReceiveData->totalDataLen = 0;
2056 g_singleThreadReceiveData->defragData = NULL;
2057 g_singleThreadReceiveData->remoteEndpoint = NULL;
2059 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
2061 g_singleThreadReceiveData->totalDataLen = totalLength;
2063 if (!(g_singleThreadReceiveData->totalDataLen))
2065 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
2066 OICFree(g_singleThreadReceiveData);
2070 size_t dataOnlyLen = dataLen - (CA_BLE_HEADER_SIZE + CA_BLE_LENGTH_HEADER_SIZE);
2071 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
2072 g_singleThreadReceiveData->totalDataLen);
2073 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
2076 g_singleThreadReceiveData->defragData =
2077 OICCalloc(g_singleThreadReceiveData->totalDataLen + 1,
2078 sizeof(*g_singleThreadReceiveData->defragData));
2080 if (NULL == g_singleThreadReceiveData->defragData)
2082 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
2083 OICFree(g_singleThreadReceiveData);
2087 if (g_singleThreadReceiveData->recvDataLen + dataOnlyLen
2088 > g_singleThreadReceiveData->totalDataLen)
2090 OIC_LOG(ERROR, CALEADAPTER_TAG, "buffer is smaller than received data");
2091 OICFree(g_singleThreadReceiveData->defragData);
2092 OICFree(g_singleThreadReceiveData);
2095 memcpy(g_singleThreadReceiveData->defragData,
2096 data + (CA_BLE_HEADER_SIZE + CA_BLE_LENGTH_HEADER_SIZE),
2098 g_singleThreadReceiveData->recvDataLen += dataOnlyLen;
2102 size_t dataOnlyLen = dataLen - CA_BLE_HEADER_SIZE;
2103 if (g_singleThreadReceiveData->recvDataLen + dataOnlyLen
2104 > g_singleThreadReceiveData->totalDataLen)
2106 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
2107 "Data Length exceeding error!! Receiving [%d] total length [%d]",
2108 g_singleThreadReceiveData->recvDataLen + dataOnlyLen,
2109 g_singleThreadReceiveData->totalDataLen);
2110 OICFree(g_singleThreadReceiveData->defragData);
2111 OICFree(g_singleThreadReceiveData);
2114 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]",
2116 memcpy(g_singleThreadReceiveData->defragData + g_singleThreadReceiveData->recvDataLen,
2117 data + CA_BLE_HEADER_SIZE,
2119 g_singleThreadReceiveData->recvDataLen += dataOnlyLen;
2120 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] received Datalen [%d]",
2121 g_singleThreadReceiveData->totalDataLen, g_singleThreadReceiveData->recvDataLen);
2125 static CAResult_t CALEServerSendDataSingleThread(const uint8_t *data,
2128 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2130 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2132 uint32_t midPacketCount = 0;
2133 size_t remainingLen = 0;
2134 size_t totalLength = 0;
2135 CABLEPacketSecure_t secureFlag = CA_BLE_PACKET_NON_SECURE;
2137 CAResult_t result = CAGenerateVariableForFragmentation(dataLen,
2143 if (CA_STATUS_OK != result)
2145 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
2146 "CAGenerateVariableForFragmentation failed, result [%d]", result);
2150 OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
2151 "Packet info: data size[%d] midPacketCount[%d] remainingLen[%d] totalLength[%d]",
2152 dataLen, midPacketCount, remainingLen, totalLength);
2156 "Server total Data length with header is [%u]",
2159 uint8_t dataSegment[CA_SUPPORTED_BLE_MTU_SIZE] = {0};
2160 uint8_t dataHeader[CA_BLE_HEADER_SIZE] = {0};
2162 result = CAGenerateHeader(dataHeader,
2163 CA_BLE_PACKET_START,
2164 g_localBLESourcePort,
2165 CA_BLE_PACKET_NON_SECURE,
2166 CA_BLE_MULTICAST_PORT);
2168 if (CA_STATUS_OK != result)
2170 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
2171 "CAGenerateHeader failed, result [%d]", result);
2175 uint8_t lengthHeader[CA_BLE_LENGTH_HEADER_SIZE] = {0};
2176 result = CAGenerateHeaderPayloadLength(lengthHeader,
2177 CA_BLE_LENGTH_HEADER_SIZE,
2180 if (CA_STATUS_OK != result)
2182 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
2183 "CAGenerateHeaderPayloadLength failed, result [%d]", result);
2187 uint32_t length = 0;
2188 uint32_t dataOnlyLen = 0;
2189 if (g_mtuSize > totalLength)
2191 length = totalLength;
2192 dataOnlyLen = dataLen;
2197 dataOnlyLen = g_mtuSize - CA_BLE_HEADER_SIZE - CA_BLE_LENGTH_HEADER_SIZE;
2200 result = CAMakeFirstDataSegment(dataSegment,
2202 dataHeader, lengthHeader);
2204 if (CA_STATUS_OK != result)
2206 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
2207 "Making data segment failed, result [%d]", result);
2211 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
2212 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
2213 if (CA_STATUS_OK != result)
2215 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
2222 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
2224 result = CAGenerateHeader(dataHeader,
2225 CA_BLE_PACKET_NOT_START,
2226 g_localBLESourcePort,
2227 CA_BLE_PACKET_NON_SECURE,
2228 CA_BLE_MULTICAST_PORT);
2230 if (CA_STATUS_OK != result)
2232 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
2233 "CAGenerateHeader failed, result [%d]", result);
2237 const uint32_t dataLimit = midPacketCount;
2238 for (uint32_t iter = 0; iter < dataLimit; iter++)
2240 result = CAMakeRemainDataSegment(dataSegment,
2241 g_mtuSize - CA_BLE_HEADER_SIZE,
2248 if (CA_STATUS_OK != result)
2250 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
2251 "Making data segment failed, result [%d]", result);
2255 result = CAUpdateCharacteristicsToAllGattClients(
2259 if (CA_STATUS_OK != result)
2261 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2268 if (remainingLen && (totalLength > g_mtuSize))
2270 // send the last segment of the data
2271 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
2273 result = CAMakeRemainDataSegment(dataSegment,
2281 if (CA_STATUS_OK != result)
2283 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
2284 "Making data segment failed, result [%d]", result);
2288 result = CAUpdateCharacteristicsToAllGattClients(
2290 remainingLen + CA_BLE_HEADER_SIZE);
2292 if (CA_STATUS_OK != result)
2294 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2304 static CAResult_t CAInitLEAdapterMutex()
2306 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEAdapterMutex");
2308 if (NULL == g_bleIsServerMutex)
2310 g_bleIsServerMutex = oc_mutex_new();
2311 if (NULL == g_bleIsServerMutex)
2313 OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed");
2314 return CA_STATUS_FAILED;
2318 if (NULL == g_bleNetworkCbMutex)
2320 g_bleNetworkCbMutex = oc_mutex_new();
2321 if (NULL == g_bleNetworkCbMutex)
2323 OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed");
2324 CATerminateLEAdapterMutex();
2325 return CA_STATUS_FAILED;
2329 if (NULL == g_bleLocalAddressMutex)
2331 g_bleLocalAddressMutex = oc_mutex_new();
2332 if (NULL == g_bleLocalAddressMutex)
2334 OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed");
2335 CATerminateLEAdapterMutex();
2336 return CA_STATUS_FAILED;
2340 if (NULL == g_bleAdapterThreadPoolMutex)
2342 g_bleAdapterThreadPoolMutex = oc_mutex_new();
2343 if (NULL == g_bleAdapterThreadPoolMutex)
2345 OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed");
2346 CATerminateLEAdapterMutex();
2347 return CA_STATUS_FAILED;
2351 if (NULL == g_bleClientSendDataMutex)
2353 g_bleClientSendDataMutex = oc_mutex_new();
2354 if (NULL == g_bleClientSendDataMutex)
2356 OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed");
2357 CATerminateLEAdapterMutex();
2358 return CA_STATUS_FAILED;
2362 if (NULL == g_bleServerSendDataMutex)
2364 g_bleServerSendDataMutex = oc_mutex_new();
2365 if (NULL == g_bleServerSendDataMutex)
2367 OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed");
2368 CATerminateLEAdapterMutex();
2369 return CA_STATUS_FAILED;
2373 if (NULL == g_bleAdapterReqRespCbMutex)
2375 g_bleAdapterReqRespCbMutex = oc_mutex_new();
2376 if (NULL == g_bleAdapterReqRespCbMutex)
2378 OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed");
2379 CATerminateLEAdapterMutex();
2380 return CA_STATUS_FAILED;
2384 if (NULL == g_bleServerReceiveDataMutex)
2386 g_bleServerReceiveDataMutex = oc_mutex_new();
2387 if (NULL == g_bleServerReceiveDataMutex)
2389 OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed");
2390 return CA_STATUS_FAILED;
2394 if (NULL == g_bleClientReceiveDataMutex)
2396 g_bleClientReceiveDataMutex = oc_mutex_new();
2397 if (NULL == g_bleClientReceiveDataMutex)
2399 OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed");
2400 return CA_STATUS_FAILED;
2404 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2405 return CA_STATUS_OK;
2408 static void CATerminateLEAdapterMutex()
2410 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CATerminateLEAdapterMutex");
2412 oc_mutex_free(g_bleIsServerMutex);
2413 g_bleIsServerMutex = NULL;
2415 oc_mutex_free(g_bleNetworkCbMutex);
2416 g_bleNetworkCbMutex = NULL;
2418 oc_mutex_free(g_bleLocalAddressMutex);
2419 g_bleLocalAddressMutex = NULL;
2421 oc_mutex_free(g_bleAdapterThreadPoolMutex);
2422 g_bleAdapterThreadPoolMutex = NULL;
2424 oc_mutex_free(g_bleClientSendDataMutex);
2425 g_bleClientSendDataMutex = NULL;
2427 oc_mutex_free(g_bleServerSendDataMutex);
2428 g_bleServerSendDataMutex = NULL;
2430 oc_mutex_free(g_bleAdapterReqRespCbMutex);
2431 g_bleAdapterReqRespCbMutex = NULL;
2433 oc_mutex_free(g_bleServerReceiveDataMutex);
2434 g_bleServerReceiveDataMutex = NULL;
2436 oc_mutex_free(g_bleClientReceiveDataMutex);
2437 g_bleClientReceiveDataMutex = NULL;
2439 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2443 * Starting LE connectivity adapters.
2445 * As its peer to peer it does not require to start any servers.
2447 * @return ::CA_STATUS_OK or Appropriate error code.
2449 static CAResult_t CAStartLE();
2452 * Start listening server for receiving multicast search requests.
2454 * Transport Specific Behavior:
2455 * LE Starts GATT Server with prefixed UUID and Characteristics
2456 * per OIC Specification.
2457 * @return ::CA_STATUS_OK or Appropriate error code.
2459 static CAResult_t CAStartLEListeningServer();
2462 * Stops listening server from receiving multicast search requests.
2464 * Transport Specific Behavior:
2465 * LE Starts GATT Server with prefixed UUID and Characteristics
2466 * per OIC Specification.
2467 * @return ::CA_STATUS_OK or Appropriate error code.
2469 static CAResult_t CAStopLEListeningServer();
2472 * Sarting discovery of servers for receiving multicast
2475 * Transport Specific Behavior:
2476 * LE Starts GATT Server with prefixed UUID and Characteristics
2477 * per OIC Specification.
2479 * @return ::CA_STATUS_OK or Appropriate error code
2481 static CAResult_t CAStartLEDiscoveryServer();
2484 * Send data to the endpoint using the adapter connectivity.
2486 * @param[in] endpoint Remote Endpoint information (like MAC address,
2487 * reference URI and connectivity type) to which
2488 * the unicast data has to be sent.
2489 * @param[in] data Data which required to be sent.
2490 * @param[in] dataLen Size of data to be sent.
2492 * @note dataLen must be > 0.
2494 * @return The number of bytes sent on the network, or -1 on error.
2496 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
2499 CADataType_t dataType);
2502 * Send multicast data to the endpoint using the LE connectivity.
2504 * @param[in] endpoint Remote Endpoint information to which the
2505 * multicast data has to be sent.
2506 * @param[in] data Data which required to be sent.
2507 * @param[in] dataLen Size of data to be sent.
2509 * @note dataLen must be > 0.
2511 * @return The number of bytes sent on the network, or -1 on error.
2513 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
2516 CADataType_t dataType);
2519 * Get LE Connectivity network information.
2521 * @param[out] info Local connectivity information structures.
2522 * @param[out] size Number of local connectivity structures.
2524 * @return ::CA_STATUS_OK or Appropriate error code.
2526 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info,
2530 * Read Synchronous API callback.
2532 * @return ::CA_STATUS_OK or Appropriate error code.
2534 static CAResult_t CAReadLEData();
2537 * Stopping the adapters and close socket connections.
2539 * LE Stops all GATT servers and GATT Clients.
2541 * @return ::CA_STATUS_OK or Appropriate error code.
2543 static CAResult_t CAStopLE();
2546 * Terminate the LE connectivity adapter.
2548 * Configuration information will be deleted from further use.
2550 static void CATerminateLE();
2553 * This function will receive the data from the GattServer and add the
2554 * data to the Server receiver queue.
2556 * @param[in] remoteAddress Remote address of the device from where
2558 * @param[in] data Actual data received from the remote
2560 * @param[in] dataLength Length of the data received from the
2562 * @param[in] sentLength Length of the data sent from the remote
2565 * @return ::CA_STATUS_OK or Appropriate error code.
2566 * @retval ::CA_STATUS_OK Successful.
2567 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
2568 * @retval ::CA_STATUS_FAILED Operation failed.
2571 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2572 const uint8_t *data,
2573 uint32_t dataLength,
2574 uint32_t *sentLength);
2577 * This function will receive the data from the GattClient and add the
2578 * data into the Client receiver queue.
2580 * @param[in] remoteAddress Remote address of the device from where
2582 * @param[in] data Actual data recevied from the remote
2584 * @param[in] dataLength Length of the data received from the
2586 * @param[in] sentLength Length of the data sent from the remote
2589 * @return ::CA_STATUS_OK or Appropriate error code.
2590 * @retval ::CA_STATUS_OK Successful.
2591 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
2592 * @retval ::CA_STATUS_FAILED Operation failed.
2594 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2595 const uint8_t *data,
2596 uint32_t dataLength,
2597 uint32_t *sentLength);
2600 * Set the NetworkPacket received callback to CA layer from adapter
2603 * @param[in] callback Callback handle sent from the upper layer.
2605 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
2608 * Push the data from CA layer to the Sender processor queue.
2610 * @param[in] remoteEndpoint Remote endpoint information of the
2612 * @param[in] data Data to be transmitted from LE.
2613 * @param[in] dataLen Length of the Data being transmitted.
2615 * @return ::CA_STATUS_OK or Appropriate error code.
2616 * @retval ::CA_STATUS_OK Successful.
2617 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
2618 * @retval ::CA_STATUS_FAILED Operation failed.
2620 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2621 const uint8_t *data,
2625 * Push the data from CA layer to the Sender processor queue.
2627 * @param[in] remoteEndpoint Remote endpoint information of the
2629 * @param[in] data Data to be transmitted from LE.
2630 * @param[in] dataLen Length of the Data being transmitted.
2632 * @return ::CA_STATUS_OK or Appropriate error code.
2633 * @retval ::CA_STATUS_OK Successful.
2634 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
2635 * @retval ::CA_STATUS_FAILED Operation failed.
2637 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
2638 const uint8_t *data,
2641 static CAResult_t CALEAdapterGattServerStart()
2643 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
2645 if (caglobals.bleFlags & CA_LE_SERVER_DISABLE)
2647 OIC_LOG_V(INFO, CALEADAPTER_TAG, "server's flag is disabled in configuration [%d]",
2648 caglobals.bleFlags);
2649 return CA_STATUS_OK;
2652 CAResult_t result = CAStartLEGattServer();
2654 #ifndef SINGLE_THREAD
2656 Don't start the server side sending queue thread until the
2657 server itself has actually started.
2659 if (CA_STATUS_OK == result)
2661 oc_mutex_lock(g_bleServerSendDataMutex);
2662 result = CAQueueingThreadStart(g_bleServerSendQueueHandle);
2663 oc_mutex_unlock(g_bleServerSendDataMutex);
2665 if (CA_STATUS_OK != result)
2669 "Unable to start server queuing thread (%d)",
2678 static CAResult_t CALEAdapterGattServerStop()
2680 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Stop");
2682 if (caglobals.bleFlags & CA_LE_SERVER_DISABLE)
2684 OIC_LOG_V(INFO, CALEADAPTER_TAG, "server flag of configure is disable [%d]",
2685 caglobals.bleFlags);
2686 return CA_STATUS_OK;
2689 #ifndef SINGLE_THREAD
2691 CAResult_t res = CAStopLEGattServer();
2692 if (CA_STATUS_OK != res)
2694 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAStopLEGattServer has failed");
2697 oc_mutex_lock(g_bleServerSendDataMutex);
2698 res = CAQueueingThreadStop(g_bleServerSendQueueHandle);
2699 if (CA_STATUS_OK != res)
2701 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAQueueingThreadStop has failed");
2703 oc_mutex_unlock(g_bleServerSendDataMutex);
2707 return CAStopLEGattServer();
2711 static CAResult_t CALEAdapterGattClientStart()
2713 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattClient");
2715 CAResult_t result = CAStartLEGattClient();
2717 #ifndef SINGLE_THREAD
2719 Don't start the client side sending queue thread until the
2720 client itself has actually started.
2722 if (CA_STATUS_OK == result)
2724 oc_mutex_lock(g_bleClientSendDataMutex);
2725 result = CAQueueingThreadStart(g_bleClientSendQueueHandle);
2726 oc_mutex_unlock(g_bleClientSendDataMutex);
2728 if (CA_STATUS_OK != result)
2732 "Unable to start client queuing thread");
2740 static CAResult_t CALEAdapterGattClientStop()
2742 #ifndef SINGLE_THREAD
2743 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEAdapterGattClientStop");
2744 CAStopLEGattClient();
2746 oc_mutex_lock(g_bleClientSendDataMutex);
2747 CAResult_t result = CAQueueingThreadStop(g_bleClientSendQueueHandle);
2748 oc_mutex_unlock(g_bleClientSendDataMutex);
2752 CAStopLEGattClient();
2754 return CA_STATUS_OK;
2758 #ifdef __WITH_DTLS__
2759 static void CALESecureSendDataCB(CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
2761 VERIFY_NON_NULL_VOID(endpoint, CALEADAPTER_TAG, "endpoint is NULL");
2762 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "data is NULL");
2764 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Secure Data Send - encrypted datalen = %d", dataLen);
2767 CADataType_t dataType = g_dataType;
2769 if (ADAPTER_SERVER == g_adapterType ||
2770 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_DATA == dataType))
2772 result = CALEAdapterServerSendData(endpoint, data, dataLen);
2773 if (CA_STATUS_OK != result)
2775 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed" );
2779 g_errorHandler(endpoint, data, dataLen, result);
2783 else if (ADAPTER_CLIENT == g_adapterType ||
2784 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_REQUEST_DATA == dataType) ||
2785 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_FOR_RES == dataType))
2787 result = CALEAdapterClientSendData(endpoint, data, dataLen);
2788 if (CA_STATUS_OK != result)
2790 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for client failed" );
2794 g_errorHandler(endpoint, data, dataLen, result);
2800 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
2801 "Can't Send Message adapterType = %d, dataType = %d", g_adapterType, dataType);
2803 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2806 void CALESecureReceiveDataCB(const CASecureEndpoint_t *sep, const void *data,
2809 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2811 VERIFY_NON_NULL_VOID(sep, CALEADAPTER_TAG, "sep is NULL");
2812 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "data is NULL");
2814 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Secure Data Receive - decrypted datalen = %d", dataLen);
2816 if (g_networkPacketReceivedCallback)
2818 g_networkPacketReceivedCallback(sep, data, dataLen);
2823 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
2824 CANetworkPacketReceivedCallback reqRespCallback,
2825 CAAdapterChangeCallback netCallback,
2826 CAConnectionChangeCallback connCallback,
2827 CAErrorHandleCallback errorCallback,
2828 ca_thread_pool_t handle)
2830 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2833 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
2834 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
2835 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
2836 VERIFY_NON_NULL(connCallback, CALEADAPTER_TAG, "ConnectionChange Callback is null");
2838 CAResult_t result = CA_STATUS_OK;
2839 result = CAInitLEAdapterMutex();
2840 if (CA_STATUS_OK != result)
2842 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
2843 return CA_STATUS_FAILED;
2846 result = CAInitializeLENetworkMonitor();
2847 if (CA_STATUS_OK != result)
2849 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
2850 return CA_STATUS_FAILED;
2852 CAInitializeLEAdapter();
2854 CASetLEClientThreadPoolHandle(handle);
2856 result = CAInitializeLEGattClient();
2857 if (CA_STATUS_OK != result)
2859 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattClient() failed");
2860 return CA_STATUS_FAILED;
2863 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
2864 CASetLEServerThreadPoolHandle(handle);
2865 result = CAInitializeLEGattServer();
2866 if (CA_STATUS_OK != result)
2868 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLEGattServer() failed");
2869 return CA_STATUS_FAILED;
2872 CASetLEAdapterThreadPoolHandle(handle);
2873 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
2874 CASetLEReqRespAdapterCallback(reqRespCallback);
2876 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
2877 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
2878 CALERegisterNetworkNotifications(netCallback, connCallback);
2880 g_errorHandler = errorCallback;
2882 #ifdef __WITH_DTLS__
2883 if (CA_STATUS_OK != CAinitSslAdapter())
2885 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to init SSL adapter");
2889 CAsetSslAdapterCallbacks(CALESecureReceiveDataCB, CALESecureSendDataCB, CA_ADAPTER_GATT_BTLE);
2893 static const CAConnectivityHandler_t connHandler =
2895 .startAdapter = CAStartLE,
2896 .stopAdapter = CAStopLE,
2897 .startListenServer = CAStartLEListeningServer,
2898 .stopListenServer = CAStopLEListeningServer,
2899 .startDiscoveryServer = CAStartLEDiscoveryServer,
2900 .sendData = CASendLEUnicastData,
2901 .sendDataToAll = CASendLEMulticastData,
2902 .GetnetInfo = CAGetLEInterfaceInformation,
2903 .readData = CAReadLEData,
2904 .terminate = CATerminateLE,
2905 .cType = CA_ADAPTER_GATT_BTLE
2908 registerCallback(connHandler);
2910 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2912 return CA_STATUS_OK;
2915 static CAResult_t CAStartLE()
2917 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE");
2919 return CAStartLEAdapter();
2922 static CAResult_t CAStopLE()
2924 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2926 #ifdef __WITH_DTLS__
2927 CAdeinitSslAdapter();
2930 #ifndef SINGLE_THREAD
2934 oc_mutex_lock(g_bleIsServerMutex);
2935 switch (g_adapterType)
2937 case ADAPTER_SERVER:
2938 CALEAdapterGattServerStop();
2940 case ADAPTER_CLIENT:
2941 CALEAdapterGattClientStop();
2943 case ADAPTER_BOTH_CLIENT_SERVER:
2944 CALEAdapterGattServerStop();
2945 CALEAdapterGattClientStop();
2950 oc_mutex_unlock(g_bleIsServerMutex);
2952 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2954 return CAStopLEAdapter();
2957 static void CATerminateLE()
2959 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CATerminateLE");
2961 CASetLEReqRespServerCallback(NULL);
2962 CASetLEReqRespClientCallback(NULL);
2963 CALERegisterNetworkNotifications(NULL, NULL);
2964 CASetLEReqRespAdapterCallback(NULL);
2965 CATerminateLENetworkMonitor();
2967 oc_mutex_lock(g_bleIsServerMutex);
2968 switch (g_adapterType)
2970 case ADAPTER_SERVER:
2971 CATerminateLEGattServer();
2973 case ADAPTER_CLIENT:
2974 CATerminateLEGattClient();
2976 case ADAPTER_BOTH_CLIENT_SERVER:
2977 CATerminateLEGattServer();
2978 CATerminateLEGattClient();
2983 g_adapterType = ADAPTER_EMPTY;
2984 oc_mutex_unlock(g_bleIsServerMutex);
2986 #ifndef SINGLE_THREAD
2987 CATerminateLEQueues();
2990 #ifdef __WITH_DTLS__
2991 CAsetSslAdapterCallbacks(NULL, NULL, CA_ADAPTER_GATT_BTLE);
2994 CATerminateLEAdapterMutex();
2996 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CATerminateLE");
2999 static CAResult_t CAStartLEListeningServer()
3001 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEListeningServer");
3003 if (caglobals.bleFlags & CA_LE_SERVER_DISABLE)
3005 OIC_LOG_V(INFO, CALEADAPTER_TAG, "server flag of configure is disable [%d]",
3006 caglobals.bleFlags);
3007 return CA_STATUS_OK;
3010 #ifndef ROUTING_GATEWAY
3011 CAResult_t result = CA_STATUS_OK;
3012 #ifndef SINGLE_THREAD
3013 result = CAInitLEServerQueues();
3014 if (CA_STATUS_OK != result)
3016 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
3021 oc_mutex_lock(g_bleIsServerMutex);
3022 switch (g_adapterType)
3024 case ADAPTER_CLIENT:
3025 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
3027 case ADAPTER_BOTH_CLIENT_SERVER:
3030 g_adapterType = ADAPTER_SERVER;
3032 oc_mutex_unlock(g_bleIsServerMutex);
3034 result = CAGetLEAdapterState();
3035 if (CA_STATUS_OK != result)
3037 if (CA_ADAPTER_NOT_ENABLED == result)
3041 "Listen Server will be started once BT Adapter is enabled");
3042 result = CA_STATUS_OK;
3047 result = CALEAdapterGattServerStart();
3050 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3053 // Routing Gateway only supports BLE client mode.
3054 OIC_LOG(ERROR, CALEADAPTER_TAG, "LE server not supported in Routing Gateway");
3055 return CA_NOT_SUPPORTED;
3059 static CAResult_t CAStopLEListeningServer()
3061 OIC_LOG(ERROR, CALEADAPTER_TAG, "Listen server stop not supported.");
3062 return CA_NOT_SUPPORTED;
3065 static CAResult_t CAStartLEDiscoveryServer()
3067 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEDiscoveryServer");
3068 CAResult_t result = CA_STATUS_OK;
3069 #ifndef SINGLE_THREAD
3070 result = CAInitLEClientQueues();
3071 if (CA_STATUS_OK != result)
3073 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
3078 oc_mutex_lock(g_bleIsServerMutex);
3079 switch (g_adapterType)
3081 case ADAPTER_SERVER:
3082 g_adapterType = ADAPTER_BOTH_CLIENT_SERVER;
3084 case ADAPTER_BOTH_CLIENT_SERVER:
3087 g_adapterType = ADAPTER_CLIENT;
3089 oc_mutex_unlock(g_bleIsServerMutex);
3091 result = CAGetLEAdapterState();
3092 if (CA_STATUS_OK != result)
3094 if (CA_ADAPTER_NOT_ENABLED == result)
3098 "Discovery Server will be started once BT Adapter is enabled");
3099 result = CA_STATUS_OK;
3104 result = CALEAdapterGattClientStart();
3107 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3111 static CAResult_t CAReadLEData()
3113 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
3114 #ifdef SINGLE_THREAD
3117 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3118 return CA_STATUS_OK;
3121 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
3124 CADataType_t dataType)
3126 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN - CASendLEUnicastData : type(%d)", dataType);
3129 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
3130 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
3132 CAResult_t result = CA_STATUS_FAILED;
3134 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
3135 if (ADAPTER_EMPTY == g_adapterType)
3137 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
3140 oc_mutex_lock(g_bleIsServerMutex);
3141 if (ADAPTER_SERVER == g_adapterType ||
3142 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_DATA == dataType))
3144 #ifdef __WITH_DTLS__
3145 if (endpoint && endpoint->flags & CA_SECURE)
3147 OIC_LOG(DEBUG, CALEADAPTER_TAG,
3148 "Response Data or server - secured data send(caadapternetdtlsencrypt) call");
3149 g_dataType = dataType;
3150 oc_mutex_unlock(g_bleIsServerMutex);
3152 result = CAencryptSsl(endpoint, data, dataLen);
3153 if (CA_STATUS_OK != result)
3155 OIC_LOG(ERROR, CALEADAPTER_TAG, "caadapternetdtlsencrypt failed!");
3162 OIC_LOG(DEBUG, CALEADAPTER_TAG,
3163 "server or both - none secured data send(CALEAdapterServerSendData) call");
3164 result = CALEAdapterServerSendData(endpoint, data, dataLen);
3167 result = CALEAdapterServerSendData(endpoint, data, dataLen);
3169 if (CA_STATUS_OK != result)
3171 oc_mutex_unlock(g_bleIsServerMutex);
3172 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed");
3175 g_errorHandler(endpoint, data, dataLen, result);
3181 else if (ADAPTER_CLIENT == g_adapterType ||
3182 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_REQUEST_DATA == dataType) ||
3183 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_FOR_RES == dataType))
3185 #ifdef __WITH_DTLS__
3186 if (endpoint && endpoint->flags & CA_SECURE)
3188 OIC_LOG(DEBUG, CALEADAPTER_TAG,
3189 "Request Data or client - secured data send(caadapternetdtlsencrypt) call");
3190 g_dataType = dataType;
3191 oc_mutex_unlock(g_bleIsServerMutex);
3193 result = CAencryptSsl(endpoint, data, dataLen);
3194 if (CA_STATUS_OK != result)
3196 OIC_LOG(ERROR, CALEADAPTER_TAG, "caadapternetdtlsencrypt failed!");
3203 OIC_LOG(DEBUG, CALEADAPTER_TAG,
3204 "client or both - none secured data send(CALEAdapterClientSendData) call");
3205 result = CALEAdapterClientSendData(endpoint, data, dataLen);
3208 result = CALEAdapterClientSendData(endpoint, data, dataLen);
3210 if (CA_STATUS_OK != result)
3212 oc_mutex_unlock(g_bleIsServerMutex);
3213 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for client failed" );
3217 g_errorHandler(endpoint, data, dataLen, result);
3222 oc_mutex_unlock(g_bleIsServerMutex);
3224 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3228 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
3231 CADataType_t dataType)
3233 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEMulticastData");
3236 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
3240 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
3244 CAResult_t result = CA_STATUS_FAILED;
3246 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType);
3247 if (ADAPTER_EMPTY == g_adapterType)
3249 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty");
3252 oc_mutex_lock(g_bleIsServerMutex);
3253 if (ADAPTER_SERVER == g_adapterType ||
3254 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_DATA == dataType))
3256 result = CALEAdapterServerSendData(NULL, data, dataLen);
3257 if (CA_STATUS_OK != result)
3259 oc_mutex_unlock(g_bleIsServerMutex);
3261 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data for server failed" );
3265 g_errorHandler(endpoint, data, dataLen, result);
3271 if (ADAPTER_CLIENT == g_adapterType ||
3272 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_REQUEST_DATA == dataType) ||
3273 (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_FOR_RES == dataType))
3275 result = CALEAdapterClientSendData(NULL, data, dataLen);
3276 if (CA_STATUS_OK != result)
3278 oc_mutex_unlock(g_bleIsServerMutex);
3280 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data for client failed" );
3284 g_errorHandler(endpoint, data, dataLen, result);
3289 oc_mutex_unlock(g_bleIsServerMutex);
3291 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CASendLEMulticastData");
3295 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
3297 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
3299 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
3301 char *local_address = NULL;
3303 CAResult_t res = CAGetLEAddress(&local_address);
3304 if (CA_STATUS_OK != res)
3306 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
3310 if (NULL == local_address)
3312 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
3313 return CA_STATUS_FAILED;
3317 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
3318 if (NULL == (*info))
3320 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
3321 OICFree(local_address);
3322 return CA_STATUS_FAILED;
3325 size_t local_address_len = strlen(local_address);
3327 if(local_address_len >= sizeof(g_localBLEAddress) ||
3328 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
3330 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
3332 OICFree(local_address);
3333 return CA_STATUS_FAILED;
3336 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
3337 oc_mutex_lock(g_bleLocalAddressMutex);
3338 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
3339 oc_mutex_unlock(g_bleLocalAddressMutex);
3341 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
3343 OICFree(local_address);
3345 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3346 return CA_STATUS_OK;
3349 static CAResult_t CALERegisterNetworkNotifications(CAAdapterChangeCallback netCallback,
3350 CAConnectionChangeCallback connCallback)
3352 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
3354 oc_mutex_lock(g_bleNetworkCbMutex);
3355 g_networkCallback = netCallback;
3356 g_connectionCallback = connCallback;
3357 oc_mutex_unlock(g_bleNetworkCbMutex);
3358 CAResult_t res = CA_STATUS_OK;
3361 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
3362 if (CA_STATUS_OK != res)
3364 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
3369 res = CAUnSetLEAdapterStateChangedCb();
3370 if (CA_STATUS_OK != res)
3372 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
3376 if (g_connectionCallback)
3378 res = CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCb);
3379 if (CA_STATUS_OK != res)
3381 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLENWConnectionStateChangedCb failed!");
3385 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3389 static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
3392 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEConnectionStateChangedCb");
3394 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
3397 CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE };
3398 OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), address);
3401 oc_mutex_lock(g_bleIsServerMutex);
3402 switch (g_adapterType)
3404 case ADAPTER_SERVER:
3405 CALEGattServerConnectionStateChanged(isConnected, address);
3407 case ADAPTER_CLIENT:
3408 CALEGattConnectionStateChanged(isConnected, address);
3410 case ADAPTER_BOTH_CLIENT_SERVER:
3411 CALEGattConnectionStateChanged(isConnected, address);
3412 CALEGattServerConnectionStateChanged(isConnected, address);
3417 oc_mutex_unlock(g_bleIsServerMutex);
3422 #ifndef SINGLE_THREAD
3423 if(g_bleClientSenderInfo)
3425 CALERemoveReceiveQueueData(g_bleClientSenderInfo, address);
3428 if(g_bleServerSenderInfo)
3430 CALERemoveReceiveQueueData(g_bleServerSenderInfo, address);
3433 // remove data of send queue.
3434 if (g_bleClientSendQueueHandle)
3436 CALERemoveSendQueueData(g_bleClientSendQueueHandle,
3437 g_bleClientSendDataMutex,
3441 if (g_bleServerSendQueueHandle)
3443 CALERemoveSendQueueData(g_bleServerSendQueueHandle,
3444 g_bleServerSendDataMutex,
3449 #ifdef __WITH_DTLS__
3450 CAcloseSslConnection(&localEndpoint);
3454 oc_mutex_lock(g_bleNetworkCbMutex);
3455 if (g_connectionCallback)
3457 g_connectionCallback(&localEndpoint, isConnected);
3459 oc_mutex_unlock(g_bleNetworkCbMutex);
3461 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3464 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
3466 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDeviceStateChangedCb");
3468 if (CA_ADAPTER_ENABLED == adapter_state)
3470 oc_mutex_lock(g_bleIsServerMutex);
3471 switch (g_adapterType)
3473 case ADAPTER_SERVER:
3474 CALEAdapterGattServerStart();
3476 case ADAPTER_CLIENT:
3477 CALEAdapterGattClientStart();
3479 case ADAPTER_BOTH_CLIENT_SERVER:
3480 CALEAdapterGattServerStart();
3481 CALEAdapterGattClientStart();
3486 oc_mutex_unlock(g_bleIsServerMutex);
3490 oc_mutex_lock(g_bleIsServerMutex);
3491 switch (g_adapterType)
3493 case ADAPTER_SERVER:
3494 CALEAdapterGattServerStop();
3496 case ADAPTER_CLIENT:
3497 CALEAdapterGattClientStop();
3499 case ADAPTER_BOTH_CLIENT_SERVER:
3500 CALEAdapterGattServerStop();
3501 CALEAdapterGattClientStop();
3506 oc_mutex_unlock(g_bleIsServerMutex);
3509 oc_mutex_lock(g_bleNetworkCbMutex);
3510 if (NULL != g_networkCallback)
3512 g_networkCallback(CA_ADAPTER_GATT_BTLE, adapter_state);
3516 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
3518 oc_mutex_unlock(g_bleNetworkCbMutex);
3520 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3523 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
3524 const uint8_t *data,
3527 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
3529 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
3530 #ifndef SINGLE_THREAD
3531 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
3532 "g_bleClientSendQueueHandle is NULL",
3534 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
3535 "g_bleClientSendDataMutex is NULL",
3538 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
3539 "g_bleClientSendQueueHandle",
3542 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
3544 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen, NULL);
3547 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
3548 return CA_MEMORY_ALLOC_FAILED;
3550 // Add message to send queue
3551 oc_mutex_lock(g_bleClientSendDataMutex);
3552 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
3553 oc_mutex_unlock(g_bleClientSendDataMutex);
3555 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3556 return CA_STATUS_OK;
3559 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
3560 const uint8_t *data,
3563 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
3565 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
3567 #ifdef SINGLE_THREAD
3568 if (!CAIsLEConnected())
3570 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
3571 return CA_STATUS_FAILED;
3574 CAResult_t result = CALEServerSendDataSingleThread(data, dataLen);
3575 if (CA_STATUS_OK != result)
3577 OIC_LOG(ERROR, CALEADAPTER_TAG, "CALEServerSendDataSingleThread failed");
3578 return CA_STATUS_FAILED;
3581 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
3582 "BleClientReceiverQueue is NULL",
3584 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
3585 "BleClientSendDataMutex is NULL",
3588 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
3591 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
3593 CALEData_t * const bleData =
3594 CACreateLEData(remoteEndpoint, data, dataLen, NULL);
3598 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
3599 return CA_MEMORY_ALLOC_FAILED;
3602 // Add message to send queue
3603 oc_mutex_lock(g_bleServerSendDataMutex);
3604 CAQueueingThreadAddData(g_bleServerSendQueueHandle,
3606 sizeof(CALEData_t));
3607 oc_mutex_unlock(g_bleServerSendDataMutex);
3609 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3610 return CA_STATUS_OK;
3613 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
3614 const uint8_t *data,
3615 uint32_t dataLength,
3616 uint32_t *sentLength)
3618 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
3621 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
3622 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
3624 #ifdef SINGLE_THREAD
3625 CALEDataReceiverHandlerSingleThread(data, dataLength);
3627 if (g_singleThreadReceiveData->totalDataLen == g_singleThreadReceiveData->recvDataLen)
3629 if(g_networkPacketReceivedCallback)
3631 // will be filled by upper layer
3632 const CASecureEndpoint_t endpoint =
3633 { .endpoint = { .adapter = CA_ADAPTER_GATT_BTLE } };
3635 g_networkPacketReceivedCallback(&endpoint,
3636 g_singleThreadReceiveData->defragData,
3637 g_singleThreadReceiveData->recvDataLen);
3639 g_singleThreadReceiveData->remoteEndpoint = NULL;
3640 OICFree(g_singleThreadReceiveData->defragData);
3641 g_singleThreadReceiveData->defragData = NULL;
3642 OICFree(g_singleThreadReceiveData);
3643 g_singleThreadReceiveData = NULL;
3646 VERIFY_NON_NULL_RET(g_bleServerReceiverQueue,
3648 "g_bleServerReceiverQueue",
3651 //Add message to data queue
3652 CAEndpoint_t * const remoteEndpoint =
3653 CACreateEndpointObject(CA_DEFAULT_FLAGS,
3654 CA_ADAPTER_GATT_BTLE,
3658 if (NULL == remoteEndpoint)
3660 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
3661 return CA_STATUS_FAILED;
3664 // Create bleData to add to queue
3667 "Data received from LE Server layer [%d]",
3670 CALEData_t * const bleData =
3671 CACreateLEData(remoteEndpoint, data, dataLength, g_bleServerSenderInfo);
3675 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
3676 CAFreeEndpoint(remoteEndpoint);
3677 return CA_MEMORY_ALLOC_FAILED;
3680 CAFreeEndpoint(remoteEndpoint);
3681 // Add message to receiver queue
3682 CAQueueingThreadAddData(g_bleServerReceiverQueue, bleData, sizeof(CALEData_t));
3684 *sentLength = dataLength;
3686 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3687 return CA_STATUS_OK;
3690 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
3691 const uint8_t *data,
3692 uint32_t dataLength,
3693 uint32_t *sentLength)
3695 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
3698 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
3699 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
3700 #ifndef SINGLE_THREAD
3701 VERIFY_NON_NULL_RET(g_bleClientReceiverQueue, CALEADAPTER_TAG,
3702 "g_bleClientReceiverQueue",
3705 //Add message to data queue
3706 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
3707 CA_ADAPTER_GATT_BTLE,
3709 if (NULL == remoteEndpoint)
3711 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
3712 return CA_STATUS_FAILED;
3715 // Create bleData to add to queue
3718 "Data received from LE Client layer [%zu]",
3721 CALEData_t * const bleData =
3722 CACreateLEData(remoteEndpoint, data, dataLength, g_bleClientSenderInfo);
3726 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
3727 CAFreeEndpoint(remoteEndpoint);
3728 return CA_MEMORY_ALLOC_FAILED;
3731 CAFreeEndpoint(remoteEndpoint);
3732 // Add message to receiver queue
3733 CAQueueingThreadAddData(g_bleClientReceiverQueue, bleData, sizeof(CALEData_t));
3735 *sentLength = dataLength;
3737 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3738 return CA_STATUS_OK;
3741 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
3743 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
3745 oc_mutex_lock(g_bleAdapterThreadPoolMutex);
3746 g_bleAdapterThreadPool = handle;
3747 oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
3749 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3752 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
3754 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
3756 oc_mutex_lock(g_bleAdapterReqRespCbMutex);
3758 g_networkPacketReceivedCallback = callback;
3760 oc_mutex_unlock(g_bleAdapterReqRespCbMutex);
3762 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
3765 static void CALEErrorHandler(const char *remoteAddress,
3766 const uint8_t *data,
3770 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
3772 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
3774 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
3775 CA_ADAPTER_GATT_BTLE,
3779 // if required, will be used to build remote endpoint
3780 g_errorHandler(rep, data, dataLen, result);
3782 CAFreeEndpoint(rep);
3784 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");
3787 #ifndef SINGLE_THREAD
3788 static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle, oc_mutex mutex,
3789 const char* address)
3791 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALERemoveSendQueueData");
3793 VERIFY_NON_NULL_VOID(queueHandle, CALEADAPTER_TAG, "queueHandle");
3794 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
3796 oc_mutex_lock(mutex);
3797 while (u_queue_get_size(queueHandle->dataQueue) > 0)
3799 OIC_LOG(DEBUG, CALEADAPTER_TAG, "get data from queue");
3800 u_queue_message_t *message = u_queue_get_element(queueHandle->dataQueue);
3801 if (NULL != message)
3803 CALEData_t *bleData = (CALEData_t *) message->msg;
3804 if (bleData && bleData->remoteEndpoint)
3806 if (!strcmp(bleData->remoteEndpoint->addr, address))
3808 OIC_LOG(DEBUG, CALEADAPTER_TAG, "found the message of disconnected device");
3809 if (NULL != queueHandle->destroy)
3811 queueHandle->destroy(message->msg, message->size);
3815 OICFree(message->msg);
3823 oc_mutex_unlock(mutex);
3826 static void CALERemoveReceiveQueueData(u_arraylist_t *dataInfoList, const char* address)
3828 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALERemoveReceiveQueueData");
3830 VERIFY_NON_NULL_VOID(dataInfoList, CALEADAPTER_TAG, "dataInfoList");
3831 VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
3833 CABLESenderInfo_t *senderInfo = NULL;
3834 uint32_t senderIndex = 0;
3836 u_arraylist_t *portList = u_arraylist_create();
3837 if (CA_STATUS_OK == CALEGetPortsFromSenderInfo(address, dataInfoList, portList))
3839 uint32_t arrayLength = u_arraylist_length(portList);
3840 for (uint32_t i = 0; i < arrayLength; i++)
3842 uint16_t *port = (uint16_t *)u_arraylist_get(portList, i);
3845 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to get port from sender info !");
3846 u_arraylist_destroy(portList);
3850 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "port : %X", *port);
3852 if (CA_STATUS_OK == CALEGetSenderInfo(address, *port,
3853 dataInfoList, &senderInfo,
3856 u_arraylist_remove(dataInfoList, senderIndex);
3857 OICFree(senderInfo->defragData);
3858 OICFree(senderInfo->remoteEndpoint);
3859 OICFree(senderInfo);
3861 OIC_LOG(DEBUG, CALEADAPTER_TAG,
3862 "SenderInfo is removed for disconnection");
3866 OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo doesn't exist");
3870 u_arraylist_destroy(portList);
3873 static CAResult_t CALEGetPortsFromSenderInfo(const char *leAddress,
3874 u_arraylist_t *senderInfoList,
3875 u_arraylist_t *portList)
3877 VERIFY_NON_NULL(leAddress,
3879 "NULL BLE address argument");
3881 const uint32_t listLength = u_arraylist_length(senderInfoList);
3882 const uint32_t addrLength = strlen(leAddress);
3884 for (uint32_t index = 0; index < listLength; index++)
3886 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(senderInfoList, index);
3887 if (!info || !(info->remoteEndpoint))
3892 if (!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
3894 uint16_t *port = (uint16_t *)OICMalloc(sizeof(uint16_t));
3897 OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
3898 return CA_MEMORY_ALLOC_FAILED;
3900 *port = info->remoteEndpoint->port;
3901 u_arraylist_add(portList, (void *)port);
3905 if (u_arraylist_length(portList) != 0)
3907 return CA_STATUS_OK;
3911 return CA_STATUS_FAILED;