1 /* ****************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
20 #include "caleadapter.h"
25 #include "caleinterface.h"
28 #include "caadapterutils.h"
30 #include "caqueueingthread.h"
32 #include "cafragmentation.h"
33 #include "oic_malloc.h"
34 #include "oic_string.h"
35 #include "caremotehandler.h"
38 * Logging tag for module name.
40 #define CALEADAPTER_TAG "LAD"
44 * Stores the information of the Data to be sent from the queues.
46 * This structure will be pushed to the sender/receiver queue for
51 CAEndpoint_t *remoteEndpoint; /**< Remote endpoint contains the
52 information of remote device. */
53 void *data; /**< Data to be transmitted over LE transport. */
54 uint32_t dataLen; /**< Length of the data being transmitted. */
58 * Stores information of all the senders.
60 * This structure will be used to track and defragment all incoming
66 uint32_t totalDataLen;
68 CAEndpoint_t *remoteEndpoint;
72 * Callback to provide the status of the network change to CA layer.
74 static CANetworkChangeCallback g_networkCallback = NULL;
77 * bleAddress of the local adapter. Value will be initialized to zero,
78 * and will be updated later.
80 static char g_localBLEAddress[18] = { 0 };
83 * Variable to differentiate btw GattServer and GattClient.
85 static bool g_isServer = false;
88 * Mutex to synchronize the task to be executed on the GattServer
91 static ca_mutex g_bleIsServerMutex = NULL;
94 * Mutex to synchronize the callback to be called for the network
97 static ca_mutex g_bleNetworkCbMutex = NULL;
100 * Mutex to synchronize the updates of the local LE address of the
103 static ca_mutex g_bleLocalAddressMutex = NULL;
106 * Reference to thread pool.
108 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
111 * Mutex to synchronize the task to be pushed to thread pool.
113 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
116 * Mutex to synchronize the queing of the data from SenderQueue.
118 static ca_mutex g_bleClientSendDataMutex = NULL;
121 * Mutex to synchronize the queing of the data from ReceiverQueue.
123 static ca_mutex g_bleReceiveDataMutex = NULL;
127 * Mutex to synchronize the queing of the data from SenderQueue.
129 static ca_mutex g_bleServerSendDataMutex = NULL;
132 * Mutex to synchronize the callback to be called for the
133 * adapterReqResponse.
135 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
138 * Callback to be called when network packet received from either
139 * GattServer or GattClient.
141 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
144 * Callback to notify error from the BLE adapter.
146 static CAErrorHandleCallback g_errorHandler = NULL;
149 * Storing Adapter state information.
151 static CAAdapterState_t g_bleAdapterState = CA_ADAPTER_DISABLED;
156 * This enumeration provides information of LE Adapter Server status.
160 CA_SERVER_NOTSTARTED = 0,
166 * Structure to maintain the status of the server.
168 static CALeServerStatus gLeServerStatus = CA_SERVER_NOTSTARTED;
171 * Register network change notification callback.
173 * @param[in] netCallback CANetworkChangeCallback callback which will
174 * be set for the change in network.
176 * @return 0 on success otherwise a positive error value.
177 * @retval ::CA_STATUS_OK Successful.
178 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
179 * @retval ::CA_STATUS_FAILED Operation failed.
182 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
185 * Set the thread pool handle which is required for spawning new
188 * @param[in] handle Thread pool handle which is given by above layer
189 * for using thread creation task.
192 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
195 * Call the callback to the upper layer when the device state gets
198 * @param[in] adapter_state New state of the adapter to be notified to
201 static void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state);
204 * Used to initialize all required mutex variable for LE Adapter
207 * @return 0 on success otherwise a positive error value.
208 * @retval ::CA_STATUS_OK Successful.
209 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
210 * @retval ::CA_STATUS_FAILED Operation failed.
213 static CAResult_t CAInitLEAdapterMutex();
216 * Terminate all required mutex variables for LE adapter
219 static void CATerminateLEAdapterMutex();
222 * Prepares and notify error through error callback.
225 static void CALEErrorHandler(const char *remoteAddress,
230 #ifndef SINGLE_THREAD
232 * Stop condition of recvhandler.
234 static bool g_dataReceiverHandlerState = false;
237 * Sender information.
239 static u_arraylist_t *g_senderInfo = NULL;
242 * Queue to process the outgoing packets from GATTClient.
244 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
247 * Queue to process the incoming packets to GATT Client.
249 static CAQueueingThread_t *g_bleReceiverQueue = NULL;
252 * Queue to process the outgoing packets from GATTServer.
254 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
257 * Starting LE connectivity adapters.
259 * As its peer to peer it does not require to start any servers.
261 * @return ::CA_STATUS_OK or Appropriate error code.
263 static CAResult_t CAStartLE();
266 * Start listening server for receiving multicast search requests.
268 * Transport Specific Behavior:
269 * LE Starts GATT Server with prefixed UUID and Characteristics
270 * per OIC Specification.
271 * @return ::CA_STATUS_OK or Appropriate error code.
273 static CAResult_t CAStartLEListeningServer();
276 * Sarting discovery of servers for receiving multicast
279 * Transport Specific Behavior:
280 * LE Starts GATT Server with prefixed UUID and Characteristics
281 * per OIC Specification.
283 * @return ::CA_STATUS_OK or Appropriate error code
285 static CAResult_t CAStartLEDiscoveryServer();
288 * Send data to the endpoint using the adapter connectivity.
290 * @param[in] endpoint Remote Endpoint information (like MAC address,
291 * reference URI and connectivity type) to which
292 * the unicast data has to be sent.
293 * @param[in] data Data which required to be sent.
294 * @param[in] dataLen Size of data to be sent.
296 * @note dataLen must be > 0.
298 * @return The number of bytes sent on the network, or -1 on error.
300 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
305 * Send multicast data to the endpoint using the LE connectivity.
307 * @param[in] endpoint Remote Endpoint information to which the
308 * multicast data has to be sent.
309 * @param[in] data Data which required to be sent.
310 * @param[in] dataLen Size of data to be sent.
312 * @note dataLen must be > 0.
314 * @return The number of bytes sent on the network, or -1 on error.
316 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
321 * Get LE Connectivity network information.
323 * @param[out] info Local connectivity information structures.
324 * @param[out] size Number of local connectivity structures.
326 * @return ::CA_STATUS_OK or Appropriate error code.
328 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info,
332 * Read Synchronous API callback.
334 * @return ::CA_STATUS_OK or Appropriate error code.
336 static CAResult_t CAReadLEData();
339 * Stopping the adapters and close socket connections.
341 * LE Stops all GATT servers and GATT Clients.
343 * @return ::CA_STATUS_OK or Appropriate error code.
345 static CAResult_t CAStopLE();
348 * Terminate the LE connectivity adapter.
350 * Configuration information will be deleted from further use.
352 static void CATerminateLE();
355 * This function will receive the data from the GattServer and add the
356 * data to the Server receiver queue.
358 * @param[in] remoteAddress Remote address of the device from where
360 * @param[in] data Actual data recevied from the remote
362 * @param[in] dataLength Length of the data received from the
364 * @param[in] sentLength Length of the data sent from the remote
367 * @return ::CA_STATUS_OK or Appropriate error code.
368 * @retval ::CA_STATUS_OK Successful.
369 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
370 * @retval ::CA_STATUS_FAILED Operation failed.
373 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
376 uint32_t *sentLength);
379 * This function will receive the data from the GattClient and add the
380 * data into the Client receiver queue.
382 * @param[in] remoteAddress Remote address of the device from where
384 * @param[in] data Actual data recevied from the remote
386 * @param[in] dataLength Length of the data received from the
388 * @param[in] sentLength Length of the data sent from the remote
391 * @return ::CA_STATUS_OK or Appropriate error code.
392 * @retval ::CA_STATUS_OK Successful.
393 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
394 * @retval ::CA_STATUS_FAILED Operation failed.
396 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
399 uint32_t *sentLength);
402 * Set the NetworkPacket received callback to CA layer from adapter
405 * @param[in] callback Callback handle sent from the upper layer.
407 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
410 * Push the data from CA layer to the Sender processor queue.
412 * @param[in] remoteEndpoint Remote endpoint information of the
414 * @param[in] data Data to be transmitted from LE.
415 * @param[in] dataLen Length of the Data being transmitted.
417 * @return ::CA_STATUS_OK or Appropriate error code.
418 * @retval ::CA_STATUS_OK Successful.
419 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
420 * @retval ::CA_STATUS_FAILED Operation failed.
422 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
427 * Push the data from CA layer to the Sender processor queue.
429 * @param[in] remoteEndpoint Remote endpoint information of the
431 * @param[in] data Data to be transmitted from LE.
432 * @param[in] dataLen Length of the Data being transmitted.
434 * @return ::CA_STATUS_OK or Appropriate error code.
435 * @retval ::CA_STATUS_OK Successful.
436 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
437 * @retval ::CA_STATUS_FAILED Operation failed.
439 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
444 * This function will be associated with the sender queue for
447 * This function will fragment the data to the MTU of the transport
448 * and send the data in fragments to the adapters. The function will
449 * be blocked until all data is sent out from the adapter.
451 * @param[in] threadData Data pushed to the queue which contains the
452 * info about RemoteEndpoint and Data.
454 static void CALEServerSendDataThread(void *threadData);
457 * This function will be associated with the sender queue for
460 * This function will fragment the data to the MTU of the transport
461 * and send the data in fragments to the adapters. The function will
462 * be blocked until all data is sent out from the adapter.
464 * @param[in] threadData Data pushed to the queue which contains the
465 * info about RemoteEndpoint and Data.
467 static void CALEClientSendDataThread(void *threadData);
470 * This function will be associated with the receiver queue.
472 * This function will defragment the received data from each sender
473 * respectively and will send it up to CA layer. Respective sender's
474 * header will provide the length of the data sent.
476 * @param[in] threadData Data pushed to the queue which contains the
477 * info about RemoteEndpoint and Data.
479 static void CALEDataReceiverHandler(void *threadData);
482 * This function will stop all queues created for GattServer and
483 * GattClient. All four queues will be be stopped with this function
486 static void CAStopLEQueues();
489 * This function will terminate all queues created for GattServer and
490 * GattClient. All four queues will be be terminated with this
491 * function invocations.
493 static void CATerminateLEQueues();
496 * This function will initalize the Receiver and Sender queues for
497 * GattServer. This function will in turn call the functions
498 * CAInitBleServerReceiverQueue() and CAInitBleServerSenderQueue() to
499 * initialize the queues.
501 * @return ::CA_STATUS_OK or Appropriate error code.
502 * @retval ::CA_STATUS_OK Successful.
503 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
504 * @retval ::CA_STATUS_FAILED Operation failed.
506 static CAResult_t CAInitLEServerQueues();
509 * This function will initalize the Receiver and Sender queues for
510 * GattClient. This function will inturn call the functions
511 * CAInitBleClientReceiverQueue() and CAInitBleClientSenderQueue() to
512 * initialize the queues.
514 * @return ::CA_STATUS_OK or Appropriate error code.
515 * @retval ::CA_STATUS_OK Successful.
516 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
517 * @retval ::CA_STATUS_FAILED Operation failed.
520 static CAResult_t CAInitLEClientQueues();
523 * This function will initalize the Receiver queue for
524 * GattServer. This will initialize the queue to process the function
525 * CABLEServerSendDataThread() when ever the task is added to this
528 * @return ::CA_STATUS_OK or Appropriate error code.
529 * @retval ::CA_STATUS_OK Successful.
530 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
531 * @retval ::CA_STATUS_FAILED Operation failed.
533 static CAResult_t CAInitLEServerSenderQueue();
536 * This function will initalize the Receiver queue for
537 * GattClient. This will initialize the queue to process the function
538 * CABLEClientSendDataThread() when ever the task is added to this
541 * @return ::CA_STATUS_OK or Appropriate error code.
542 * @retval ::CA_STATUS_OK Successful.
543 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
544 * @retval ::CA_STATUS_FAILED Operation failed.
546 static CAResult_t CAInitLEClientSenderQueue();
549 * This function will initialize the Receiver queue for
550 * LEAdapter. This will initialize the queue to process the function
551 * CABLEDataReceiverHandler() when ever the task is added to this
554 * @return ::CA_STATUS_OK or Appropriate error code
555 * @retval ::CA_STATUS_OK Successful
556 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
557 * @retval ::CA_STATUS_FAILED Operation failed
560 static CAResult_t CAInitLEReceiverQueue();
563 * This function will create the Data required to send it in the
566 * @param[in] remoteEndpoint Remote endpoint information of the
568 * @param[in] data Data to be transmitted from LE.
569 * @param[in] dataLength Length of the Data being transmitted.
571 * @return ::CA_STATUS_OK or Appropriate error code.
572 * @retval ::CA_STATUS_OK Successful.
573 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
574 * @retval ::CA_STATUS_FAILED Operation failed.
576 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
578 uint32_t dataLength);
581 * Used to free the BLE information stored in the sender/receiver
584 * @param[in] bleData Information for a particular data segment.
586 static void CAFreeLEData(CALEData_t *bleData);
591 static void CALEDataDestroyer(void *data, uint32_t size);
593 static CAResult_t CAInitLEServerQueues()
595 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
597 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
599 CAResult_t result = CAInitLEServerSenderQueue();
600 if (CA_STATUS_OK != result)
602 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
603 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
604 return CA_STATUS_FAILED;
607 result = CAInitLEReceiverQueue();
608 if (CA_STATUS_OK != result)
610 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerReceiverQueue failed");
611 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
612 return CA_STATUS_FAILED;
615 g_dataReceiverHandlerState = true;
617 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
619 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
623 static CAResult_t CAInitLEClientQueues()
625 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
627 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
629 CAResult_t result = CAInitLEClientSenderQueue();
630 if (CA_STATUS_OK != result)
632 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
633 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
634 return CA_STATUS_FAILED;
637 result = CAInitLEReceiverQueue();
638 if (CA_STATUS_OK != result)
640 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientReceiverQueue failed");
641 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
642 return CA_STATUS_FAILED;
645 g_dataReceiverHandlerState = true;
647 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
649 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
653 static CAResult_t CAInitLEReceiverQueue()
655 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
656 // Check if the message queue is already initialized
657 if (g_bleReceiverQueue)
659 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
663 // Create recv message queue
664 g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
665 if (!g_bleReceiverQueue)
667 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
668 return CA_MEMORY_ALLOC_FAILED;
671 g_senderInfo = u_arraylist_create();
674 OIC_LOG(ERROR, CALEADAPTER_TAG, "ClientInfo memory allcation failed!");
675 OICFree(g_bleReceiverQueue);
676 g_bleReceiverQueue = NULL;
677 return CA_MEMORY_ALLOC_FAILED;
680 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue, g_bleAdapterThreadPool,
681 CALEDataReceiverHandler, CALEDataDestroyer))
683 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
684 OICFree(g_bleReceiverQueue);
685 g_bleReceiverQueue = NULL;
686 u_arraylist_free(&g_senderInfo);
687 return CA_STATUS_FAILED;
690 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue))
692 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
693 OICFree(g_bleReceiverQueue);
694 g_bleReceiverQueue = NULL;
695 u_arraylist_free(&g_senderInfo);
696 return CA_STATUS_FAILED;
699 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
703 static CAResult_t CAInitLEServerSenderQueue()
705 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
706 // Check if the message queue is already initialized
707 if (g_bleServerSendQueueHandle)
709 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
713 // Create send message queue
714 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
715 if (!g_bleServerSendQueueHandle)
717 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
718 return CA_MEMORY_ALLOC_FAILED;
721 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
722 g_bleAdapterThreadPool,
723 CALEServerSendDataThread, CALEDataDestroyer))
725 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
726 OICFree(g_bleServerSendQueueHandle);
727 g_bleServerSendQueueHandle = NULL;
728 return CA_STATUS_FAILED;
731 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerSendQueueHandle))
733 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
734 OICFree(g_bleServerSendQueueHandle);
735 g_bleServerSendQueueHandle = NULL;
736 return CA_STATUS_FAILED;
739 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
743 static void CALEClearSenderInfo()
745 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
747 uint32_t listIndex = 0;
748 uint32_t listLength = u_arraylist_length(g_senderInfo);
749 for (listIndex = 0; listIndex < listLength; listIndex++)
751 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(g_senderInfo, listIndex);
757 OICFree(info->defragData);
758 CAFreeEndpoint(info->remoteEndpoint);
761 u_arraylist_free(&g_senderInfo);
762 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
765 static CAResult_t CAInitLEClientSenderQueue()
767 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
769 if (g_bleClientSendQueueHandle)
771 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
775 // Create send message queue
776 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
777 if (!g_bleClientSendQueueHandle)
779 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
780 return CA_MEMORY_ALLOC_FAILED;
783 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
784 g_bleAdapterThreadPool,
785 CALEClientSendDataThread, CALEDataDestroyer))
787 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
788 OICFree(g_bleClientSendQueueHandle);
789 g_bleClientSendQueueHandle = NULL;
790 return CA_STATUS_FAILED;
793 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientSendQueueHandle))
795 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
796 OICFree(g_bleClientSendQueueHandle);
797 g_bleClientSendQueueHandle = NULL;
798 return CA_STATUS_FAILED;
801 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
805 static void CAStopLEQueues()
807 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
809 ca_mutex_lock(g_bleClientSendDataMutex);
810 if (NULL != g_bleClientSendQueueHandle)
812 CAQueueingThreadStop(g_bleClientSendQueueHandle);
814 ca_mutex_unlock(g_bleClientSendDataMutex);
816 ca_mutex_lock(g_bleServerSendDataMutex);
817 if (NULL != g_bleServerSendQueueHandle)
819 CAQueueingThreadStop(g_bleServerSendQueueHandle);
821 ca_mutex_unlock(g_bleServerSendDataMutex);
823 ca_mutex_lock(g_bleReceiveDataMutex);
824 if (NULL != g_bleReceiverQueue)
826 CAQueueingThreadStop(g_bleReceiverQueue);
828 ca_mutex_unlock(g_bleReceiveDataMutex);
830 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
833 static void CATerminateLEQueues()
835 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
837 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
838 OICFree(g_bleClientSendQueueHandle);
839 g_bleClientSendQueueHandle = NULL;
841 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
842 OICFree(g_bleServerSendQueueHandle);
843 g_bleServerSendQueueHandle = NULL;
845 CAQueueingThreadDestroy(g_bleReceiverQueue);
846 OICFree(g_bleReceiverQueue);
847 g_bleReceiverQueue = NULL;
849 CALEClearSenderInfo();
851 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
854 static CAResult_t CALEGetSenderInfo(char *leAddress,
855 CABLESenderInfo_t **senderInfo,
856 uint32_t *senderIndex)
858 VERIFY_NON_NULL_RET(leAddress, CALEADAPTER_TAG, "Ble-Address in-param NULL", CA_STATUS_FAILED);
859 VERIFY_NON_NULL_RET(senderIndex, CALEADAPTER_TAG, "Index in-param NULL", CA_STATUS_FAILED);
861 uint32_t listLength = u_arraylist_length(g_senderInfo);
862 uint32_t addrLength = strlen(leAddress);
863 for (uint32_t index = 0; index < listLength; index++)
865 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(g_senderInfo, index);
866 if(!info || !(info->remoteEndpoint))
871 if(!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
873 *senderIndex = index;
882 return CA_STATUS_FAILED;
885 static void CALEDataReceiverHandler(void *threadData)
887 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
889 ca_mutex_lock(g_bleReceiveDataMutex);
891 if (g_dataReceiverHandlerState)
893 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
895 CALEData_t *bleData = (CALEData_t *) threadData;
898 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
899 ca_mutex_unlock(g_bleReceiveDataMutex);
903 if(!(bleData->remoteEndpoint))
905 OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!");
906 ca_mutex_unlock(g_bleReceiveDataMutex);
910 CABLESenderInfo_t *senderInfo = NULL;
911 uint32_t senderIndex = 0;
913 if(CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
914 &senderInfo, &senderIndex))
916 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s]",
917 bleData->remoteEndpoint->addr);
922 CABLESenderInfo_t *newSender = (CABLESenderInfo_t*)OICMalloc(sizeof(CABLESenderInfo_t));
925 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
926 ca_mutex_unlock(g_bleReceiveDataMutex);
929 newSender->recvDataLen = 0;
930 newSender->totalDataLen = 0;
931 newSender->defragData = NULL;
932 newSender->remoteEndpoint = NULL;
934 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
935 newSender->totalDataLen = CAParseHeader((char*)bleData->data);
936 if(!(newSender->totalDataLen))
938 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
940 ca_mutex_unlock(g_bleReceiveDataMutex);
944 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
945 newSender->totalDataLen);
946 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
949 newSender->defragData = (char *) OICCalloc(newSender->totalDataLen + 1, sizeof(char));
951 if (NULL == newSender->defragData)
953 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
955 ca_mutex_unlock(g_bleReceiveDataMutex);
959 const char *remoteAddress = bleData->remoteEndpoint->addr;
960 newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
961 CA_ADAPTER_GATT_BTLE, remoteAddress, 0);
962 if (NULL == newSender->remoteEndpoint)
964 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
965 OICFree(newSender->defragData);
967 ca_mutex_unlock(g_bleReceiveDataMutex);
970 memcpy(newSender->defragData, bleData->data + CA_HEADER_LENGTH,
971 bleData->dataLen - CA_HEADER_LENGTH);
972 newSender->recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
973 u_arraylist_add(g_senderInfo,(void *)newSender);
975 //Getting newSender index position in g_senderInfo array list
977 CALEGetSenderInfo(newSender->remoteEndpoint->addr, NULL, &senderIndex))
979 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
980 OICFree(newSender->defragData);
981 CAFreeEndpoint(newSender->remoteEndpoint);
983 ca_mutex_unlock(g_bleReceiveDataMutex);
986 senderInfo = newSender;
990 if(senderInfo->recvDataLen + bleData->dataLen > senderInfo->totalDataLen)
992 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
993 "Data Length exceeding error!! Receiving [%d] total length [%d]",
994 senderInfo->recvDataLen + bleData->dataLen, senderInfo->totalDataLen);
995 u_arraylist_remove(g_senderInfo, senderIndex);
996 OICFree(senderInfo->defragData);
998 ca_mutex_unlock(g_bleReceiveDataMutex);
1001 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1002 memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data,
1004 senderInfo->recvDataLen += bleData->dataLen ;
1005 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1006 senderInfo->totalDataLen, senderInfo->recvDataLen);
1009 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
1011 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1012 if (NULL == g_networkPacketReceivedCallback)
1014 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1016 u_arraylist_remove(g_senderInfo, senderIndex);
1017 OICFree(senderInfo->defragData);
1018 OICFree(senderInfo);
1019 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1020 ca_mutex_unlock(g_bleReceiveDataMutex);
1023 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1024 g_networkPacketReceivedCallback(senderInfo->remoteEndpoint,
1025 senderInfo->defragData, senderInfo->recvDataLen);
1026 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1027 u_arraylist_remove(g_senderInfo, senderIndex);
1028 senderInfo->remoteEndpoint = NULL;
1029 senderInfo->defragData = NULL;
1030 OICFree(senderInfo);
1033 ca_mutex_unlock(g_bleReceiveDataMutex);
1034 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1037 static void CALEServerSendDataThread(void *threadData)
1039 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1041 CALEData_t *bleData = (CALEData_t *) threadData;
1044 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1048 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1049 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1051 int32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1053 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server total Data length with header is [%d]", totalLength);
1054 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
1055 if (NULL == dataSegment)
1057 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1062 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1063 if (CA_STATUS_OK != result )
1065 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1067 OICFree(dataSegment);
1071 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1075 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1077 length = totalLength;
1078 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1082 length = CA_SUPPORTED_BLE_MTU_SIZE;
1083 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1084 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1087 int32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1089 // Send the first segment with the header.
1090 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1092 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
1093 result = CAUpdateCharacteristicsToGattClient(
1094 bleData->remoteEndpoint->addr, dataSegment, length);
1095 if (CA_STATUS_OK != result)
1097 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1098 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1099 OICFree(dataSegment);
1103 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1104 for (index = 1; index < iter; index++)
1106 // Send the remaining header.
1107 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1108 result = CAUpdateCharacteristicsToGattClient(
1109 bleData->remoteEndpoint->addr,
1110 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1111 CA_SUPPORTED_BLE_MTU_SIZE);
1112 if (CA_STATUS_OK != result)
1114 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1115 "Update characteristics failed, result [%d]", result);
1116 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1117 OICFree(dataSegment);
1120 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
1121 CA_SUPPORTED_BLE_MTU_SIZE);
1124 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1125 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1127 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1128 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1129 result = CAUpdateCharacteristicsToGattClient(
1130 bleData->remoteEndpoint->addr,
1131 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1133 if (CA_STATUS_OK != result)
1135 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1137 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1138 OICFree(dataSegment);
1141 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1146 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
1147 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
1148 if (CA_STATUS_OK != result)
1150 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1152 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1153 OICFree(dataSegment);
1156 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1157 for (index = 1; index < iter; index++)
1159 // Send the remaining header.
1160 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1161 result = CAUpdateCharacteristicsToAllGattClients(
1162 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1163 CA_SUPPORTED_BLE_MTU_SIZE);
1164 if (CA_STATUS_OK != result)
1166 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1168 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1169 OICFree(dataSegment);
1172 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
1175 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1176 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1178 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1179 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1180 result = CAUpdateCharacteristicsToAllGattClients(
1181 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1183 if (CA_STATUS_OK != result)
1185 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1187 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1188 OICFree(dataSegment);
1191 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1194 OICFree(dataSegment);
1196 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1199 static void CALEClientSendDataThread(void *threadData)
1201 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1203 CALEData_t *bleData = (CALEData_t *) threadData;
1206 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1210 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1211 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1213 uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1214 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
1215 if (NULL == dataSegment)
1217 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1222 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1223 if (CA_STATUS_OK != result )
1225 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1227 OICFree(dataSegment);
1230 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1233 uint32_t length = 0;
1234 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1236 length = totalLength;
1237 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1238 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1242 length = CA_SUPPORTED_BLE_MTU_SIZE;
1243 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1244 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1245 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1248 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1250 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1252 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1253 // Send the first segment with the header.
1254 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addr,
1259 if (CA_STATUS_OK != result)
1261 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1262 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1263 OICFree(dataSegment);
1267 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1268 for (index = 1; index < iter; index++)
1270 // Send the remaining header.
1271 result = CAUpdateCharacteristicsToGattServer(
1272 bleData->remoteEndpoint->addr,
1273 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1274 CA_SUPPORTED_BLE_MTU_SIZE,
1276 if (CA_STATUS_OK != result)
1278 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1280 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1281 OICFree(dataSegment);
1284 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1285 CA_SUPPORTED_BLE_MTU_SIZE);
1288 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1289 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1291 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1292 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1293 result = CAUpdateCharacteristicsToGattServer(
1294 bleData->remoteEndpoint->addr,
1295 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1299 if (CA_STATUS_OK != result)
1301 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1303 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1304 OICFree(dataSegment);
1307 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1312 //Sending Mulitcast Data
1313 // Send the first segment with the header.
1314 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1315 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1316 if (CA_STATUS_OK != result)
1318 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1319 "Update characteristics (all) failed, result [%d]", result);
1320 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1321 OICFree(dataSegment);
1324 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1325 // Send the remaining header.
1326 for (index = 1; index < iter; index++)
1328 result = CAUpdateCharacteristicsToAllGattServers(
1329 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1330 CA_SUPPORTED_BLE_MTU_SIZE);
1331 if (CA_STATUS_OK != result)
1333 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]",
1335 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1336 OICFree(dataSegment);
1339 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1340 CA_SUPPORTED_BLE_MTU_SIZE);
1343 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1344 if ( remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1346 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1347 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1348 result = CAUpdateCharacteristicsToAllGattServers(
1349 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1351 if (CA_STATUS_OK != result)
1353 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1354 "Update characteristics (all) failed, result [%d]", result);
1355 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1356 OICFree(dataSegment);
1359 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1364 OICFree(dataSegment);
1366 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1369 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
1371 uint32_t dataLength)
1373 CALEData_t *bleData = (CALEData_t *) OICMalloc(sizeof(CALEData_t));
1376 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1380 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1381 bleData->data = (void *)OICCalloc(dataLength + 1, 1);
1382 if (NULL == bleData->data)
1384 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1385 CAFreeLEData(bleData);
1388 memcpy(bleData->data, data, dataLength);
1389 bleData->dataLen = dataLength;
1394 static void CAFreeLEData(CALEData_t *bleData)
1396 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1398 CAFreeEndpoint(bleData->remoteEndpoint);
1399 OICFree(bleData->data);
1403 static void CALEDataDestroyer(void *data, uint32_t size)
1405 if ((size_t)size < sizeof(CALEData_t *))
1407 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1408 "Destroy data too small %p %d", data, size);
1410 CALEData_t *ledata = (CALEData_t *) data;
1412 CAFreeLEData(ledata);
1416 static CAResult_t CAInitLEAdapterMutex()
1418 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1420 if (NULL == g_bleIsServerMutex)
1422 g_bleIsServerMutex = ca_mutex_new();
1423 if (NULL == g_bleIsServerMutex)
1425 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1426 return CA_STATUS_FAILED;
1430 if (NULL == g_bleNetworkCbMutex)
1432 g_bleNetworkCbMutex = ca_mutex_new();
1433 if (NULL == g_bleNetworkCbMutex)
1435 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1436 CATerminateLEAdapterMutex();
1437 return CA_STATUS_FAILED;
1441 if (NULL == g_bleLocalAddressMutex)
1443 g_bleLocalAddressMutex = ca_mutex_new();
1444 if (NULL == g_bleLocalAddressMutex)
1446 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1447 CATerminateLEAdapterMutex();
1448 return CA_STATUS_FAILED;
1452 if (NULL == g_bleAdapterThreadPoolMutex)
1454 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1455 if (NULL == g_bleAdapterThreadPoolMutex)
1457 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1458 CATerminateLEAdapterMutex();
1459 return CA_STATUS_FAILED;
1463 if (NULL == g_bleClientSendDataMutex)
1465 g_bleClientSendDataMutex = ca_mutex_new();
1466 if (NULL == g_bleClientSendDataMutex)
1468 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1469 CATerminateLEAdapterMutex();
1470 return CA_STATUS_FAILED;
1474 if (NULL == g_bleServerSendDataMutex)
1476 g_bleServerSendDataMutex = ca_mutex_new();
1477 if (NULL == g_bleServerSendDataMutex)
1479 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1480 CATerminateLEAdapterMutex();
1481 return CA_STATUS_FAILED;
1485 if (NULL == g_bleAdapterReqRespCbMutex)
1487 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1488 if (NULL == g_bleAdapterReqRespCbMutex)
1490 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1491 CATerminateLEAdapterMutex();
1492 return CA_STATUS_FAILED;
1496 if (NULL == g_bleReceiveDataMutex)
1498 g_bleReceiveDataMutex = ca_mutex_new();
1499 if (NULL == g_bleReceiveDataMutex)
1501 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1502 return CA_STATUS_FAILED;
1506 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1507 return CA_STATUS_OK;
1510 static void CATerminateLEAdapterMutex()
1512 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1514 ca_mutex_free(g_bleIsServerMutex);
1515 g_bleIsServerMutex = NULL;
1517 ca_mutex_free(g_bleNetworkCbMutex);
1518 g_bleNetworkCbMutex = NULL;
1520 ca_mutex_free(g_bleLocalAddressMutex);
1521 g_bleLocalAddressMutex = NULL;
1523 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1524 g_bleAdapterThreadPoolMutex = NULL;
1526 ca_mutex_free(g_bleClientSendDataMutex);
1527 g_bleClientSendDataMutex = NULL;
1529 ca_mutex_free(g_bleServerSendDataMutex);
1530 g_bleServerSendDataMutex = NULL;
1532 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1533 g_bleAdapterReqRespCbMutex = NULL;
1535 ca_mutex_free(g_bleReceiveDataMutex);
1536 g_bleReceiveDataMutex = NULL;
1538 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1541 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1542 CANetworkPacketReceivedCallback reqRespCallback,
1543 CANetworkChangeCallback netCallback,
1544 CAErrorHandleCallback errorCallback,
1545 ca_thread_pool_t handle)
1547 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1550 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1551 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1552 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1554 CAResult_t result = CA_STATUS_OK;
1555 result = CAInitLEAdapterMutex();
1556 if (CA_STATUS_OK != result)
1558 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1559 return CA_STATUS_FAILED;
1561 result = CAInitializeLENetworkMonitor();
1562 if (CA_STATUS_OK != result)
1564 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1565 return CA_STATUS_FAILED;
1568 CAInitializeLEAdapter();
1570 CASetLEClientThreadPoolHandle(handle);
1571 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1572 CASetLEServerThreadPoolHandle(handle);
1573 CASetLEAdapterThreadPoolHandle(handle);
1574 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1575 CASetLEReqRespAdapterCallback(reqRespCallback);
1577 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1578 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1579 CALERegisterNetworkNotifications(netCallback);
1581 g_errorHandler = errorCallback;
1583 static const CAConnectivityHandler_t connHandler =
1585 .startAdapter = CAStartLE,
1586 .stopAdapter = CAStopLE,
1587 .startListenServer = CAStartLEListeningServer,
1588 .startDiscoveryServer = CAStartLEDiscoveryServer,
1589 .sendData = CASendLEUnicastData,
1590 .sendDataToAll = CASendLEMulticastData,
1591 .GetnetInfo = CAGetLEInterfaceInformation,
1592 .readData = CAReadLEData,
1593 .terminate = CATerminateLE
1596 registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
1598 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1600 return CA_STATUS_OK;
1603 static CAResult_t CAStartLE()
1605 OIC_LOG(DEBUG, CALEADAPTER_TAG, __func__);
1607 return CAStartLEAdapter();
1610 static CAResult_t CAStopLE()
1612 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1613 #ifndef SINGLE_THREAD
1617 ca_mutex_lock(g_bleIsServerMutex);
1618 if (true == g_isServer)
1620 CAStopLEGattServer();
1624 CAStopLEGattClient();
1626 ca_mutex_unlock(g_bleIsServerMutex);
1628 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1630 return CA_STATUS_OK;
1633 static void CATerminateLE()
1635 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1637 CASetLEReqRespServerCallback(NULL);
1638 CASetLEReqRespClientCallback(NULL);
1639 CALERegisterNetworkNotifications(NULL);
1640 CASetLEReqRespAdapterCallback(NULL);
1641 CATerminateLENetworkMonitor();
1643 ca_mutex_lock(g_bleIsServerMutex);
1644 if (true == g_isServer)
1646 CATerminateLEGattServer();
1650 CATerminateLEGattClient();
1652 ca_mutex_unlock(g_bleIsServerMutex);
1654 #ifndef SINGLE_THREAD
1655 CATerminateLEQueues();
1657 CATerminateLEAdapterMutex();
1659 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1662 static CAResult_t CAStartLEListeningServer()
1664 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1665 CAResult_t result = CA_STATUS_OK;
1666 #ifndef SINGLE_THREAD
1667 result = CAInitLEServerQueues();
1668 if (CA_STATUS_OK != result)
1670 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1671 return CA_STATUS_FAILED;
1675 result = CAGetLEAdapterState();
1676 if (CA_ADAPTER_NOT_ENABLED == result)
1678 gLeServerStatus = CA_LISTENING_SERVER;
1679 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1680 return CA_STATUS_OK;
1683 if (CA_STATUS_FAILED == result)
1685 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1686 return CA_STATUS_FAILED;
1689 CAStartLEGattServer();
1691 ca_mutex_lock(g_bleIsServerMutex);
1693 ca_mutex_unlock(g_bleIsServerMutex);
1695 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1696 return CA_STATUS_OK;
1699 static CAResult_t CAStartLEDiscoveryServer()
1701 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1702 CAResult_t result = CA_STATUS_OK;
1703 #ifndef SINGLE_THREAD
1704 result = CAInitLEClientQueues();
1705 if (CA_STATUS_OK != result)
1707 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1708 return CA_STATUS_FAILED;
1711 result = CAGetLEAdapterState();
1712 if (CA_ADAPTER_NOT_ENABLED == result)
1714 gLeServerStatus = CA_DISCOVERY_SERVER;
1715 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1716 return CA_STATUS_OK;
1719 if (CA_STATUS_FAILED == result)
1721 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1722 return CA_STATUS_FAILED;
1725 CAStartLEGattClient();
1727 ca_mutex_lock(g_bleIsServerMutex);
1729 ca_mutex_unlock(g_bleIsServerMutex);
1731 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1732 return CA_STATUS_OK;
1735 static CAResult_t CAReadLEData()
1737 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1738 #ifdef SINGLE_THREAD
1741 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1742 return CA_STATUS_OK;
1745 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1749 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1752 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1753 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1755 CAResult_t result = CA_STATUS_FAILED;
1757 ca_mutex_lock(g_bleIsServerMutex);
1758 if (true == g_isServer)
1760 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1761 if (CA_STATUS_OK != result)
1763 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed\n");
1766 g_errorHandler((void *) endpoint, (void *) data, dataLen, result);
1768 ca_mutex_unlock(g_bleIsServerMutex);
1774 result = CALEAdapterClientSendData(endpoint, data, dataLen);
1775 if (CA_STATUS_OK != result)
1777 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed \n");
1780 g_errorHandler(endpoint, data, dataLen, result);
1782 ca_mutex_unlock(g_bleIsServerMutex);
1786 ca_mutex_unlock(g_bleIsServerMutex);
1788 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1792 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1796 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1799 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1803 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
1807 CAResult_t result = CA_STATUS_FAILED;
1809 ca_mutex_lock(g_bleIsServerMutex);
1810 if (true == g_isServer)
1812 result = CALEAdapterServerSendData(NULL, data, dataLen);
1813 if (CA_STATUS_OK != result)
1815 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data failed" );
1817 ca_mutex_unlock(g_bleIsServerMutex);
1820 g_errorHandler(endpoint, data, dataLen, result);
1827 result = CALEAdapterClientSendData(NULL, data, dataLen);
1828 if (CA_STATUS_OK != result)
1830 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data failed" );
1833 g_errorHandler(endpoint, data, dataLen, result);
1835 ca_mutex_unlock(g_bleIsServerMutex);
1839 ca_mutex_unlock(g_bleIsServerMutex);
1841 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1845 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
1847 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1849 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
1851 char *local_address = NULL;
1853 CAResult_t res = CAGetLEAddress(&local_address);
1854 if (CA_STATUS_OK != res)
1856 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
1860 if (NULL == local_address)
1862 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
1863 return CA_STATUS_FAILED;
1867 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
1868 if (NULL == (*info))
1870 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
1871 OICFree(local_address);
1872 return CA_STATUS_FAILED;
1875 size_t local_address_len = strlen(local_address);
1877 if(local_address_len >= sizeof(g_localBLEAddress) ||
1878 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
1880 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
1882 OICFree(local_address);
1883 return CA_STATUS_FAILED;
1886 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
1887 ca_mutex_lock(g_bleLocalAddressMutex);
1888 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
1889 ca_mutex_unlock(g_bleLocalAddressMutex);
1891 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
1893 OICFree(local_address);
1895 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1896 return CA_STATUS_OK;
1899 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
1901 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1903 ca_mutex_lock(g_bleNetworkCbMutex);
1904 g_networkCallback = netCallback;
1905 ca_mutex_unlock(g_bleNetworkCbMutex);
1906 CAResult_t res = CA_STATUS_OK;
1909 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
1910 if (CA_STATUS_OK != res)
1912 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1917 res = CAUnSetLEAdapterStateChangedCb();
1918 if (CA_STATUS_OK != res)
1920 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1924 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1928 static void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
1930 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1932 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
1933 CAEndpoint_t localEndpoint = {.adapter = CA_DEFAULT_ADAPTER};
1935 ca_mutex_lock(g_bleLocalAddressMutex);
1936 OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), g_localBLEAddress);
1937 ca_mutex_unlock(g_bleLocalAddressMutex);
1939 g_bleAdapterState = adapter_state;
1940 // Start a GattServer/Client if gLeServerStatus is SET
1941 if (CA_LISTENING_SERVER == gLeServerStatus)
1943 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1944 CAStartLEGattServer();
1946 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
1948 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
1949 CAStartLEGattClient();
1951 gLeServerStatus = CA_SERVER_NOTSTARTED;
1953 ca_mutex_lock(g_bleNetworkCbMutex);
1954 if (NULL != g_networkCallback)
1956 g_networkCallback(&localEndpoint, adapter_state);
1960 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
1962 ca_mutex_unlock(g_bleNetworkCbMutex);
1964 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1967 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1971 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1973 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
1974 #ifndef SINGLE_THREAD
1975 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
1976 "g_bleClientSendQueueHandle is NULL",
1978 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
1979 "g_bleClientSendDataMutex is NULL",
1982 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
1985 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1987 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
1990 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1991 return CA_MEMORY_ALLOC_FAILED;
1993 // Add message to send queue
1994 ca_mutex_lock(g_bleClientSendDataMutex);
1995 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
1996 ca_mutex_unlock(g_bleClientSendDataMutex);
1998 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1999 return CA_STATUS_OK;
2002 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2006 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2008 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2010 #ifdef SINGLE_THREAD
2011 char header[CA_HEADER_LENGTH] = {0};
2013 CAResult_t result = CAGenerateHeader(header, dataLen);
2015 if (CA_STATUS_OK != result)
2017 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
2021 if (!CAIsLEConnected())
2023 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2027 result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH);
2028 if (CA_STATUS_OK != result)
2030 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2034 int32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2035 for (int32_t iter = 0; iter < dataLimit; iter++)
2037 result = CAUpdateCharacteristicsToAllGattClients((data +
2038 (iter * CA_SUPPORTED_BLE_MTU_SIZE)),
2039 CA_SUPPORTED_BLE_MTU_SIZE);
2040 if (CA_STATUS_OK != result)
2042 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2048 uint8_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2051 result = CAUpdateCharacteristicsToAllGattClients((data +
2052 (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE)),
2054 if (CA_STATUS_OK != result)
2056 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2062 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2063 "BleClientReceiverQueue is NULL",
2065 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2066 "BleClientSendDataMutex is NULL",
2069 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2072 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2074 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
2077 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2078 return CA_MEMORY_ALLOC_FAILED;
2080 // Add message to send queue
2081 ca_mutex_lock(g_bleServerSendDataMutex);
2082 CAQueueingThreadAddData(g_bleServerSendQueueHandle, bleData, sizeof(CALEData_t));
2083 ca_mutex_unlock(g_bleServerSendDataMutex);
2085 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2086 return CA_STATUS_OK;
2089 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2091 uint32_t dataLength,
2092 uint32_t *sentLength)
2094 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2097 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2098 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2100 #ifdef SINGLE_THREAD
2101 if(g_networkPacketReceivedCallback)
2103 CAEndpoint_t endPoint = { 0 }; // will be filled by upper layer
2104 endPoint.adapter = CA_ADAPTER_GATT_BTLE;
2105 g_networkPacketReceivedCallback(&endPoint, data, dataLength);
2108 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG, "g_bleReceiverQueue",
2111 //Add message to data queue
2112 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2113 CA_ADAPTER_GATT_BTLE,
2115 if (NULL == remoteEndpoint)
2117 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2118 return CA_STATUS_FAILED;
2121 // Create bleData to add to queue
2122 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
2124 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
2127 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2128 CAFreeEndpoint(remoteEndpoint);
2129 return CA_MEMORY_ALLOC_FAILED;
2132 CAFreeEndpoint(remoteEndpoint);
2133 // Add message to receiver queue
2134 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2136 *sentLength = dataLength;
2138 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2139 return CA_STATUS_OK;
2142 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2144 uint32_t dataLength,
2145 uint32_t *sentLength)
2147 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2150 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2151 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2152 #ifndef SINGLE_THREAD
2153 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG, "g_bleReceiverQueue",
2156 //Add message to data queue
2157 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2158 CA_ADAPTER_GATT_BTLE,
2160 if (NULL == remoteEndpoint)
2162 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2163 return CA_STATUS_FAILED;
2166 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
2168 // Create bleData to add to queue
2169 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
2172 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2173 CAFreeEndpoint(remoteEndpoint);
2174 return CA_MEMORY_ALLOC_FAILED;
2177 CAFreeEndpoint(remoteEndpoint);
2178 // Add message to receiver queue
2179 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2181 *sentLength = dataLength;
2183 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2184 return CA_STATUS_OK;
2187 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2189 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2191 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2192 g_bleAdapterThreadPool = handle;
2193 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2195 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2198 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2200 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2202 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2204 g_networkPacketReceivedCallback = callback;
2206 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2208 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2211 static void CALEErrorHandler(const char *remoteAddress,
2216 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2218 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2219 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2220 CA_ADAPTER_GATT_BTLE,
2223 //if required, will be used to build remote end point
2224 g_errorHandler(rep, data, dataLen, result);
2226 CAFreeEndpoint(rep);
2227 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");