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(senderInfo->defragData);
982 ca_mutex_unlock(g_bleReceiveDataMutex);
985 senderInfo = newSender;
989 if(senderInfo->recvDataLen + bleData->dataLen > senderInfo->totalDataLen)
991 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
992 "Data Length exceeding error!! Receiving [%d] total length [%d]",
993 senderInfo->recvDataLen + bleData->dataLen, senderInfo->totalDataLen);
994 u_arraylist_remove(g_senderInfo, senderIndex);
995 OICFree(senderInfo->defragData);
997 ca_mutex_unlock(g_bleReceiveDataMutex);
1000 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1001 memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data,
1003 senderInfo->recvDataLen += bleData->dataLen ;
1004 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1005 senderInfo->totalDataLen, senderInfo->recvDataLen);
1008 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
1010 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1011 if (NULL == g_networkPacketReceivedCallback)
1013 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1015 u_arraylist_remove(g_senderInfo, senderIndex);
1016 OICFree(senderInfo->defragData);
1017 OICFree(senderInfo);
1018 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1019 ca_mutex_unlock(g_bleReceiveDataMutex);
1022 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1023 g_networkPacketReceivedCallback(senderInfo->remoteEndpoint,
1024 senderInfo->defragData, senderInfo->recvDataLen);
1025 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1026 u_arraylist_remove(g_senderInfo, senderIndex);
1027 senderInfo->remoteEndpoint = NULL;
1028 senderInfo->defragData = NULL;
1029 OICFree(senderInfo);
1032 ca_mutex_unlock(g_bleReceiveDataMutex);
1033 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1036 static void CALEServerSendDataThread(void *threadData)
1038 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1040 CALEData_t *bleData = (CALEData_t *) threadData;
1043 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1047 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1048 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1050 int32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1052 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server total Data length with header is [%d]", totalLength);
1053 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
1054 if (NULL == dataSegment)
1056 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1061 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1062 if (CA_STATUS_OK != result )
1064 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1066 OICFree(dataSegment);
1070 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1074 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1076 length = totalLength;
1077 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1081 length = CA_SUPPORTED_BLE_MTU_SIZE;
1082 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1083 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1086 int32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1088 // Send the first segment with the header.
1089 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1091 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
1092 result = CAUpdateCharacteristicsToGattClient(
1093 bleData->remoteEndpoint->addr, dataSegment, length);
1094 if (CA_STATUS_OK != result)
1096 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1097 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1098 OICFree(dataSegment);
1102 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1103 for (index = 1; index < iter; index++)
1105 // Send the remaining header.
1106 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1107 result = CAUpdateCharacteristicsToGattClient(
1108 bleData->remoteEndpoint->addr,
1109 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1110 CA_SUPPORTED_BLE_MTU_SIZE);
1111 if (CA_STATUS_OK != result)
1113 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1114 "Update characteristics failed, result [%d]", result);
1115 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1116 OICFree(dataSegment);
1119 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
1120 CA_SUPPORTED_BLE_MTU_SIZE);
1123 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1124 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1126 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1127 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1128 result = CAUpdateCharacteristicsToGattClient(
1129 bleData->remoteEndpoint->addr,
1130 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1132 if (CA_STATUS_OK != result)
1134 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1136 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1137 OICFree(dataSegment);
1140 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1145 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
1146 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
1147 if (CA_STATUS_OK != result)
1149 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1151 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1152 OICFree(dataSegment);
1155 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1156 for (index = 1; index < iter; index++)
1158 // Send the remaining header.
1159 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1160 result = CAUpdateCharacteristicsToAllGattClients(
1161 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1162 CA_SUPPORTED_BLE_MTU_SIZE);
1163 if (CA_STATUS_OK != result)
1165 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1167 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1168 OICFree(dataSegment);
1171 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
1174 int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1175 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1177 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1178 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1179 result = CAUpdateCharacteristicsToAllGattClients(
1180 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1182 if (CA_STATUS_OK != result)
1184 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1186 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1187 OICFree(dataSegment);
1190 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1193 OICFree(dataSegment);
1195 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1198 static void CALEClientSendDataThread(void *threadData)
1200 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1202 CALEData_t *bleData = (CALEData_t *) threadData;
1205 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1209 char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
1210 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1212 uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1213 char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
1214 if (NULL == dataSegment)
1216 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1221 CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
1222 if (CA_STATUS_OK != result )
1224 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1226 OICFree(dataSegment);
1229 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1232 uint32_t length = 0;
1233 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1235 length = totalLength;
1236 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1237 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1241 length = CA_SUPPORTED_BLE_MTU_SIZE;
1242 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
1243 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1244 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1247 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1249 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1251 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1252 // Send the first segment with the header.
1253 result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addr,
1258 if (CA_STATUS_OK != result)
1260 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
1261 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1262 OICFree(dataSegment);
1266 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1267 for (index = 1; index < iter; index++)
1269 // Send the remaining header.
1270 result = CAUpdateCharacteristicsToGattServer(
1271 bleData->remoteEndpoint->addr,
1272 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1273 CA_SUPPORTED_BLE_MTU_SIZE,
1275 if (CA_STATUS_OK != result)
1277 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1279 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1280 OICFree(dataSegment);
1283 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1284 CA_SUPPORTED_BLE_MTU_SIZE);
1287 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1288 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1290 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1291 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1292 result = CAUpdateCharacteristicsToGattServer(
1293 bleData->remoteEndpoint->addr,
1294 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1298 if (CA_STATUS_OK != result)
1300 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1302 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1303 OICFree(dataSegment);
1306 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1311 //Sending Mulitcast Data
1312 // Send the first segment with the header.
1313 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1314 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1315 if (CA_STATUS_OK != result)
1317 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1318 "Update characteristics (all) failed, result [%d]", result);
1319 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1320 OICFree(dataSegment);
1323 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1324 // Send the remaining header.
1325 for (index = 1; index < iter; index++)
1327 result = CAUpdateCharacteristicsToAllGattServers(
1328 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1329 CA_SUPPORTED_BLE_MTU_SIZE);
1330 if (CA_STATUS_OK != result)
1332 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]",
1334 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1335 OICFree(dataSegment);
1338 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1339 CA_SUPPORTED_BLE_MTU_SIZE);
1342 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1343 if ( remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1345 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1346 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1347 result = CAUpdateCharacteristicsToAllGattServers(
1348 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1350 if (CA_STATUS_OK != result)
1352 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1353 "Update characteristics (all) failed, result [%d]", result);
1354 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1355 OICFree(dataSegment);
1358 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1363 OICFree(dataSegment);
1365 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1368 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
1370 uint32_t dataLength)
1372 CALEData_t *bleData = (CALEData_t *) OICMalloc(sizeof(CALEData_t));
1375 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1379 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1380 bleData->data = (void *)OICCalloc(dataLength + 1, 1);
1381 if (NULL == bleData->data)
1383 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1384 CAFreeLEData(bleData);
1387 memcpy(bleData->data, data, dataLength);
1388 bleData->dataLen = dataLength;
1393 static void CAFreeLEData(CALEData_t *bleData)
1395 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1397 CAFreeEndpoint(bleData->remoteEndpoint);
1398 OICFree(bleData->data);
1402 static void CALEDataDestroyer(void *data, uint32_t size)
1404 CALEData_t *ledata = (CALEData_t *) data;
1406 CAFreeLEData(ledata);
1410 static CAResult_t CAInitLEAdapterMutex()
1412 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1414 if (NULL == g_bleIsServerMutex)
1416 g_bleIsServerMutex = ca_mutex_new();
1417 if (NULL == g_bleIsServerMutex)
1419 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1420 return CA_STATUS_FAILED;
1424 if (NULL == g_bleNetworkCbMutex)
1426 g_bleNetworkCbMutex = ca_mutex_new();
1427 if (NULL == g_bleNetworkCbMutex)
1429 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1430 CATerminateLEAdapterMutex();
1431 return CA_STATUS_FAILED;
1435 if (NULL == g_bleLocalAddressMutex)
1437 g_bleLocalAddressMutex = ca_mutex_new();
1438 if (NULL == g_bleLocalAddressMutex)
1440 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1441 CATerminateLEAdapterMutex();
1442 return CA_STATUS_FAILED;
1446 if (NULL == g_bleAdapterThreadPoolMutex)
1448 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1449 if (NULL == g_bleAdapterThreadPoolMutex)
1451 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1452 CATerminateLEAdapterMutex();
1453 return CA_STATUS_FAILED;
1457 if (NULL == g_bleClientSendDataMutex)
1459 g_bleClientSendDataMutex = ca_mutex_new();
1460 if (NULL == g_bleClientSendDataMutex)
1462 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1463 CATerminateLEAdapterMutex();
1464 return CA_STATUS_FAILED;
1468 if (NULL == g_bleServerSendDataMutex)
1470 g_bleServerSendDataMutex = ca_mutex_new();
1471 if (NULL == g_bleServerSendDataMutex)
1473 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1474 CATerminateLEAdapterMutex();
1475 return CA_STATUS_FAILED;
1479 if (NULL == g_bleAdapterReqRespCbMutex)
1481 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1482 if (NULL == g_bleAdapterReqRespCbMutex)
1484 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1485 CATerminateLEAdapterMutex();
1486 return CA_STATUS_FAILED;
1490 if (NULL == g_bleReceiveDataMutex)
1492 g_bleReceiveDataMutex = ca_mutex_new();
1493 if (NULL == g_bleReceiveDataMutex)
1495 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1496 return CA_STATUS_FAILED;
1500 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1501 return CA_STATUS_OK;
1504 static void CATerminateLEAdapterMutex()
1506 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1508 ca_mutex_free(g_bleIsServerMutex);
1509 g_bleIsServerMutex = NULL;
1511 ca_mutex_free(g_bleNetworkCbMutex);
1512 g_bleNetworkCbMutex = NULL;
1514 ca_mutex_free(g_bleLocalAddressMutex);
1515 g_bleLocalAddressMutex = NULL;
1517 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1518 g_bleAdapterThreadPoolMutex = NULL;
1520 ca_mutex_free(g_bleClientSendDataMutex);
1521 g_bleClientSendDataMutex = NULL;
1523 ca_mutex_free(g_bleServerSendDataMutex);
1524 g_bleServerSendDataMutex = NULL;
1526 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1527 g_bleAdapterReqRespCbMutex = NULL;
1529 ca_mutex_free(g_bleReceiveDataMutex);
1530 g_bleReceiveDataMutex = NULL;
1532 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1535 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1536 CANetworkPacketReceivedCallback reqRespCallback,
1537 CANetworkChangeCallback netCallback,
1538 CAErrorHandleCallback errorCallback,
1539 ca_thread_pool_t handle)
1541 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1544 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1545 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1546 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1548 CAResult_t result = CA_STATUS_OK;
1549 result = CAInitLEAdapterMutex();
1550 if (CA_STATUS_OK != result)
1552 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1553 return CA_STATUS_FAILED;
1555 result = CAInitializeLENetworkMonitor();
1556 if (CA_STATUS_OK != result)
1558 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1559 return CA_STATUS_FAILED;
1562 CAInitializeLEAdapter();
1564 CASetLEClientThreadPoolHandle(handle);
1565 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1566 CASetLEServerThreadPoolHandle(handle);
1567 CASetLEAdapterThreadPoolHandle(handle);
1568 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1569 CASetLEReqRespAdapterCallback(reqRespCallback);
1571 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1572 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1573 CALERegisterNetworkNotifications(netCallback);
1575 g_errorHandler = errorCallback;
1577 static const CAConnectivityHandler_t connHandler =
1579 .startAdapter = CAStartLE,
1580 .stopAdapter = CAStopLE,
1581 .startListenServer = CAStartLEListeningServer,
1582 .startDiscoveryServer = CAStartLEDiscoveryServer,
1583 .sendData = CASendLEUnicastData,
1584 .sendDataToAll = CASendLEMulticastData,
1585 .GetnetInfo = CAGetLEInterfaceInformation,
1586 .readData = CAReadLEData,
1587 .terminate = CATerminateLE
1590 registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
1592 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1594 return CA_STATUS_OK;
1597 static CAResult_t CAStartLE()
1599 OIC_LOG(DEBUG, CALEADAPTER_TAG, __func__);
1601 return CAStartLEAdapter();
1604 static CAResult_t CAStopLE()
1606 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1607 #ifndef SINGLE_THREAD
1611 ca_mutex_lock(g_bleIsServerMutex);
1612 if (true == g_isServer)
1614 CAStopLEGattServer();
1618 CAStopLEGattClient();
1620 ca_mutex_unlock(g_bleIsServerMutex);
1622 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1624 return CA_STATUS_OK;
1627 static void CATerminateLE()
1629 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1631 CASetLEReqRespServerCallback(NULL);
1632 CASetLEReqRespClientCallback(NULL);
1633 CALERegisterNetworkNotifications(NULL);
1634 CASetLEReqRespAdapterCallback(NULL);
1635 CATerminateLENetworkMonitor();
1637 ca_mutex_lock(g_bleIsServerMutex);
1638 if (true == g_isServer)
1640 CATerminateLEGattServer();
1644 CATerminateLEGattClient();
1646 ca_mutex_unlock(g_bleIsServerMutex);
1648 #ifndef SINGLE_THREAD
1649 CATerminateLEQueues();
1651 CATerminateLEAdapterMutex();
1653 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1656 static CAResult_t CAStartLEListeningServer()
1658 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1659 CAResult_t result = CA_STATUS_OK;
1660 #ifndef SINGLE_THREAD
1661 result = CAInitLEServerQueues();
1662 if (CA_STATUS_OK != result)
1664 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1665 return CA_STATUS_FAILED;
1669 result = CAGetLEAdapterState();
1670 if (CA_ADAPTER_NOT_ENABLED == result)
1672 gLeServerStatus = CA_LISTENING_SERVER;
1673 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1674 return CA_STATUS_OK;
1677 if (CA_STATUS_FAILED == result)
1679 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1680 return CA_STATUS_FAILED;
1683 CAStartLEGattServer();
1685 ca_mutex_lock(g_bleIsServerMutex);
1687 ca_mutex_unlock(g_bleIsServerMutex);
1689 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1690 return CA_STATUS_OK;
1693 static CAResult_t CAStartLEDiscoveryServer()
1695 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1696 CAResult_t result = CA_STATUS_OK;
1697 #ifndef SINGLE_THREAD
1698 result = CAInitLEClientQueues();
1699 if (CA_STATUS_OK != result)
1701 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1702 return CA_STATUS_FAILED;
1705 result = CAGetLEAdapterState();
1706 if (CA_ADAPTER_NOT_ENABLED == result)
1708 gLeServerStatus = CA_DISCOVERY_SERVER;
1709 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1710 return CA_STATUS_OK;
1713 if (CA_STATUS_FAILED == result)
1715 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1716 return CA_STATUS_FAILED;
1719 CAStartLEGattClient();
1721 ca_mutex_lock(g_bleIsServerMutex);
1723 ca_mutex_unlock(g_bleIsServerMutex);
1725 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1726 return CA_STATUS_OK;
1729 static CAResult_t CAReadLEData()
1731 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1732 #ifdef SINGLE_THREAD
1735 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1736 return CA_STATUS_OK;
1739 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1743 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1746 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1747 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1749 CAResult_t result = CA_STATUS_FAILED;
1751 ca_mutex_lock(g_bleIsServerMutex);
1752 if (true == g_isServer)
1754 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1755 if (CA_STATUS_OK != result)
1757 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed\n");
1760 g_errorHandler((void *) endpoint, (void *) data, dataLen, result);
1762 ca_mutex_unlock(g_bleIsServerMutex);
1768 result = CALEAdapterClientSendData(endpoint, data, dataLen);
1769 if (CA_STATUS_OK != result)
1771 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed \n");
1774 g_errorHandler(endpoint, data, dataLen, result);
1776 ca_mutex_unlock(g_bleIsServerMutex);
1780 ca_mutex_unlock(g_bleIsServerMutex);
1782 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1786 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1790 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1793 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1797 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
1801 CAResult_t result = CA_STATUS_FAILED;
1803 ca_mutex_lock(g_bleIsServerMutex);
1804 if (true == g_isServer)
1806 result = CALEAdapterServerSendData(NULL, data, dataLen);
1807 if (CA_STATUS_OK != result)
1809 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data failed" );
1811 ca_mutex_unlock(g_bleIsServerMutex);
1814 g_errorHandler(endpoint, data, dataLen, result);
1821 result = CALEAdapterClientSendData(NULL, data, dataLen);
1822 if (CA_STATUS_OK != result)
1824 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data failed" );
1827 g_errorHandler(endpoint, data, dataLen, result);
1829 ca_mutex_unlock(g_bleIsServerMutex);
1833 ca_mutex_unlock(g_bleIsServerMutex);
1835 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1839 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
1841 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1843 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
1845 char *local_address = NULL;
1847 CAResult_t res = CAGetLEAddress(&local_address);
1848 if (CA_STATUS_OK != res)
1850 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
1854 if (NULL == local_address)
1856 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
1857 return CA_STATUS_FAILED;
1861 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
1862 if (NULL == (*info))
1864 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
1865 OICFree(local_address);
1866 return CA_STATUS_FAILED;
1869 size_t local_address_len = strlen(local_address);
1871 if(local_address_len >= sizeof(g_localBLEAddress) ||
1872 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
1874 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
1876 OICFree(local_address);
1877 return CA_STATUS_FAILED;
1880 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
1881 ca_mutex_lock(g_bleLocalAddressMutex);
1882 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
1883 ca_mutex_unlock(g_bleLocalAddressMutex);
1885 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
1887 OICFree(local_address);
1889 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1890 return CA_STATUS_OK;
1893 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
1895 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1897 ca_mutex_lock(g_bleNetworkCbMutex);
1898 g_networkCallback = netCallback;
1899 ca_mutex_unlock(g_bleNetworkCbMutex);
1900 CAResult_t res = CA_STATUS_OK;
1903 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
1904 if (CA_STATUS_OK != res)
1906 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1911 res = CAUnSetLEAdapterStateChangedCb();
1912 if (CA_STATUS_OK != res)
1914 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1918 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1922 static void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
1924 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1926 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
1927 CAEndpoint_t localEndpoint = {};
1929 ca_mutex_lock(g_bleLocalAddressMutex);
1930 OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), g_localBLEAddress);
1931 ca_mutex_unlock(g_bleLocalAddressMutex);
1933 g_bleAdapterState = adapter_state;
1934 // Start a GattServer/Client if gLeServerStatus is SET
1935 if (CA_LISTENING_SERVER == gLeServerStatus)
1937 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1938 CAStartLEGattServer();
1940 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
1942 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
1943 CAStartLEGattClient();
1945 gLeServerStatus = CA_SERVER_NOTSTARTED;
1947 ca_mutex_lock(g_bleNetworkCbMutex);
1948 if (NULL != g_networkCallback)
1950 g_networkCallback(&localEndpoint, adapter_state);
1954 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
1956 ca_mutex_unlock(g_bleNetworkCbMutex);
1958 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1961 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1965 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1967 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
1968 #ifndef SINGLE_THREAD
1969 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
1970 "g_bleClientSendQueueHandle is NULL",
1972 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
1973 "g_bleClientSendDataMutex is NULL",
1976 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
1979 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1981 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
1984 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1985 return CA_MEMORY_ALLOC_FAILED;
1987 // Add message to send queue
1988 ca_mutex_lock(g_bleClientSendDataMutex);
1989 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
1990 ca_mutex_unlock(g_bleClientSendDataMutex);
1992 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1993 return CA_STATUS_OK;
1996 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2000 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2002 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2004 #ifdef SINGLE_THREAD
2005 char header[CA_HEADER_LENGTH] = {0};
2007 CAResult_t result = CAGenerateHeader(header, dataLen);
2009 if (CA_STATUS_OK != result)
2011 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
2015 if (!CAIsLEConnected())
2017 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2021 result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH);
2022 if (CA_STATUS_OK != result)
2024 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2028 int32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2029 for (int32_t iter = 0; iter < dataLimit; iter++)
2031 result = CAUpdateCharacteristicsToAllGattClients((data +
2032 (iter * CA_SUPPORTED_BLE_MTU_SIZE)),
2033 CA_SUPPORTED_BLE_MTU_SIZE);
2034 if (CA_STATUS_OK != result)
2036 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2042 uint8_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2045 result = CAUpdateCharacteristicsToAllGattClients((data +
2046 (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE)),
2048 if (CA_STATUS_OK != result)
2050 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2056 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2057 "BleClientReceiverQueue is NULL",
2059 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2060 "BleClientSendDataMutex is NULL",
2063 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2066 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2068 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
2071 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2072 return CA_MEMORY_ALLOC_FAILED;
2074 // Add message to send queue
2075 ca_mutex_lock(g_bleServerSendDataMutex);
2076 CAQueueingThreadAddData(g_bleServerSendQueueHandle, bleData, sizeof(CALEData_t));
2077 ca_mutex_unlock(g_bleServerSendDataMutex);
2079 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2080 return CA_STATUS_OK;
2083 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2085 uint32_t dataLength,
2086 uint32_t *sentLength)
2088 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2091 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2092 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2094 #ifdef SINGLE_THREAD
2095 if(g_networkPacketReceivedCallback)
2097 CAEndpoint_t endPoint = { 0 }; // will be filled by upper layer
2098 endPoint.adapter = CA_ADAPTER_GATT_BTLE;
2099 g_networkPacketReceivedCallback(&endPoint, data, dataLength);
2102 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG, "g_bleReceiverQueue",
2105 //Add message to data queue
2106 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2107 CA_ADAPTER_GATT_BTLE,
2109 if (NULL == remoteEndpoint)
2111 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2112 return CA_STATUS_FAILED;
2115 // Create bleData to add to queue
2116 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
2118 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
2121 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2122 CAFreeEndpoint(remoteEndpoint);
2123 return CA_MEMORY_ALLOC_FAILED;
2126 CAFreeEndpoint(remoteEndpoint);
2127 // Add message to receiver queue
2128 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2130 *sentLength = dataLength;
2132 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2133 return CA_STATUS_OK;
2136 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2138 uint32_t dataLength,
2139 uint32_t *sentLength)
2141 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2144 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2145 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2146 #ifndef SINGLE_THREAD
2147 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG, "g_bleReceiverQueue",
2150 //Add message to data queue
2151 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2152 CA_ADAPTER_GATT_BTLE,
2154 if (NULL == remoteEndpoint)
2156 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2157 return CA_STATUS_FAILED;
2160 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
2162 // Create bleData to add to queue
2163 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
2166 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2167 CAFreeEndpoint(remoteEndpoint);
2168 return CA_MEMORY_ALLOC_FAILED;
2171 CAFreeEndpoint(remoteEndpoint);
2172 // Add message to receiver queue
2173 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2175 *sentLength = dataLength;
2177 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2178 return CA_STATUS_OK;
2181 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2183 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2185 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2186 g_bleAdapterThreadPool = handle;
2187 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2189 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2192 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2194 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2196 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2198 g_networkPacketReceivedCallback = callback;
2200 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2202 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2205 static void CALEErrorHandler(const char *remoteAddress,
2210 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2212 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2213 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2214 CA_ADAPTER_GATT_BTLE,
2217 //if required, will be used to build remote end point
2218 g_errorHandler(rep, data, dataLen, result);
2220 CAFreeEndpoint(rep);
2221 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");