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 CALEData_t *ledata = (CALEData_t *) data;
1407 CAFreeLEData(ledata);
1411 static CAResult_t CAInitLEAdapterMutex()
1413 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1415 if (NULL == g_bleIsServerMutex)
1417 g_bleIsServerMutex = ca_mutex_new();
1418 if (NULL == g_bleIsServerMutex)
1420 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1421 return CA_STATUS_FAILED;
1425 if (NULL == g_bleNetworkCbMutex)
1427 g_bleNetworkCbMutex = ca_mutex_new();
1428 if (NULL == g_bleNetworkCbMutex)
1430 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1431 CATerminateLEAdapterMutex();
1432 return CA_STATUS_FAILED;
1436 if (NULL == g_bleLocalAddressMutex)
1438 g_bleLocalAddressMutex = ca_mutex_new();
1439 if (NULL == g_bleLocalAddressMutex)
1441 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1442 CATerminateLEAdapterMutex();
1443 return CA_STATUS_FAILED;
1447 if (NULL == g_bleAdapterThreadPoolMutex)
1449 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1450 if (NULL == g_bleAdapterThreadPoolMutex)
1452 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1453 CATerminateLEAdapterMutex();
1454 return CA_STATUS_FAILED;
1458 if (NULL == g_bleClientSendDataMutex)
1460 g_bleClientSendDataMutex = ca_mutex_new();
1461 if (NULL == g_bleClientSendDataMutex)
1463 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1464 CATerminateLEAdapterMutex();
1465 return CA_STATUS_FAILED;
1469 if (NULL == g_bleServerSendDataMutex)
1471 g_bleServerSendDataMutex = ca_mutex_new();
1472 if (NULL == g_bleServerSendDataMutex)
1474 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1475 CATerminateLEAdapterMutex();
1476 return CA_STATUS_FAILED;
1480 if (NULL == g_bleAdapterReqRespCbMutex)
1482 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1483 if (NULL == g_bleAdapterReqRespCbMutex)
1485 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1486 CATerminateLEAdapterMutex();
1487 return CA_STATUS_FAILED;
1491 if (NULL == g_bleReceiveDataMutex)
1493 g_bleReceiveDataMutex = ca_mutex_new();
1494 if (NULL == g_bleReceiveDataMutex)
1496 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1497 return CA_STATUS_FAILED;
1501 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1502 return CA_STATUS_OK;
1505 static void CATerminateLEAdapterMutex()
1507 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1509 ca_mutex_free(g_bleIsServerMutex);
1510 g_bleIsServerMutex = NULL;
1512 ca_mutex_free(g_bleNetworkCbMutex);
1513 g_bleNetworkCbMutex = NULL;
1515 ca_mutex_free(g_bleLocalAddressMutex);
1516 g_bleLocalAddressMutex = NULL;
1518 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1519 g_bleAdapterThreadPoolMutex = NULL;
1521 ca_mutex_free(g_bleClientSendDataMutex);
1522 g_bleClientSendDataMutex = NULL;
1524 ca_mutex_free(g_bleServerSendDataMutex);
1525 g_bleServerSendDataMutex = NULL;
1527 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1528 g_bleAdapterReqRespCbMutex = NULL;
1530 ca_mutex_free(g_bleReceiveDataMutex);
1531 g_bleReceiveDataMutex = NULL;
1533 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1536 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1537 CANetworkPacketReceivedCallback reqRespCallback,
1538 CANetworkChangeCallback netCallback,
1539 CAErrorHandleCallback errorCallback,
1540 ca_thread_pool_t handle)
1542 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1545 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1546 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1547 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1549 CAResult_t result = CA_STATUS_OK;
1550 result = CAInitLEAdapterMutex();
1551 if (CA_STATUS_OK != result)
1553 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1554 return CA_STATUS_FAILED;
1556 result = CAInitializeLENetworkMonitor();
1557 if (CA_STATUS_OK != result)
1559 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1560 return CA_STATUS_FAILED;
1563 CAInitializeLEAdapter();
1565 CASetLEClientThreadPoolHandle(handle);
1566 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1567 CASetLEServerThreadPoolHandle(handle);
1568 CASetLEAdapterThreadPoolHandle(handle);
1569 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1570 CASetLEReqRespAdapterCallback(reqRespCallback);
1572 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1573 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1574 CALERegisterNetworkNotifications(netCallback);
1576 g_errorHandler = errorCallback;
1578 static const CAConnectivityHandler_t connHandler =
1580 .startAdapter = CAStartLE,
1581 .stopAdapter = CAStopLE,
1582 .startListenServer = CAStartLEListeningServer,
1583 .startDiscoveryServer = CAStartLEDiscoveryServer,
1584 .sendData = CASendLEUnicastData,
1585 .sendDataToAll = CASendLEMulticastData,
1586 .GetnetInfo = CAGetLEInterfaceInformation,
1587 .readData = CAReadLEData,
1588 .terminate = CATerminateLE
1591 registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
1593 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1595 return CA_STATUS_OK;
1598 static CAResult_t CAStartLE()
1600 OIC_LOG(DEBUG, CALEADAPTER_TAG, __func__);
1602 return CAStartLEAdapter();
1605 static CAResult_t CAStopLE()
1607 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1608 #ifndef SINGLE_THREAD
1612 ca_mutex_lock(g_bleIsServerMutex);
1613 if (true == g_isServer)
1615 CAStopLEGattServer();
1619 CAStopLEGattClient();
1621 ca_mutex_unlock(g_bleIsServerMutex);
1623 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1625 return CA_STATUS_OK;
1628 static void CATerminateLE()
1630 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1632 CASetLEReqRespServerCallback(NULL);
1633 CASetLEReqRespClientCallback(NULL);
1634 CALERegisterNetworkNotifications(NULL);
1635 CASetLEReqRespAdapterCallback(NULL);
1636 CATerminateLENetworkMonitor();
1638 ca_mutex_lock(g_bleIsServerMutex);
1639 if (true == g_isServer)
1641 CATerminateLEGattServer();
1645 CATerminateLEGattClient();
1647 ca_mutex_unlock(g_bleIsServerMutex);
1649 #ifndef SINGLE_THREAD
1650 CATerminateLEQueues();
1652 CATerminateLEAdapterMutex();
1654 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1657 static CAResult_t CAStartLEListeningServer()
1659 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1660 CAResult_t result = CA_STATUS_OK;
1661 #ifndef SINGLE_THREAD
1662 result = CAInitLEServerQueues();
1663 if (CA_STATUS_OK != result)
1665 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1666 return CA_STATUS_FAILED;
1670 result = CAGetLEAdapterState();
1671 if (CA_ADAPTER_NOT_ENABLED == result)
1673 gLeServerStatus = CA_LISTENING_SERVER;
1674 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1675 return CA_STATUS_OK;
1678 if (CA_STATUS_FAILED == result)
1680 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1681 return CA_STATUS_FAILED;
1684 CAStartLEGattServer();
1686 ca_mutex_lock(g_bleIsServerMutex);
1688 ca_mutex_unlock(g_bleIsServerMutex);
1690 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1691 return CA_STATUS_OK;
1694 static CAResult_t CAStartLEDiscoveryServer()
1696 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1697 CAResult_t result = CA_STATUS_OK;
1698 #ifndef SINGLE_THREAD
1699 result = CAInitLEClientQueues();
1700 if (CA_STATUS_OK != result)
1702 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1703 return CA_STATUS_FAILED;
1706 result = CAGetLEAdapterState();
1707 if (CA_ADAPTER_NOT_ENABLED == result)
1709 gLeServerStatus = CA_DISCOVERY_SERVER;
1710 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1711 return CA_STATUS_OK;
1714 if (CA_STATUS_FAILED == result)
1716 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1717 return CA_STATUS_FAILED;
1720 CAStartLEGattClient();
1722 ca_mutex_lock(g_bleIsServerMutex);
1724 ca_mutex_unlock(g_bleIsServerMutex);
1726 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1727 return CA_STATUS_OK;
1730 static CAResult_t CAReadLEData()
1732 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1733 #ifdef SINGLE_THREAD
1736 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1737 return CA_STATUS_OK;
1740 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1744 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1747 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1748 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1750 CAResult_t result = CA_STATUS_FAILED;
1752 ca_mutex_lock(g_bleIsServerMutex);
1753 if (true == g_isServer)
1755 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1756 if (CA_STATUS_OK != result)
1758 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed\n");
1761 g_errorHandler((void *) endpoint, (void *) data, dataLen, result);
1763 ca_mutex_unlock(g_bleIsServerMutex);
1769 result = CALEAdapterClientSendData(endpoint, data, dataLen);
1770 if (CA_STATUS_OK != result)
1772 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed \n");
1775 g_errorHandler(endpoint, data, dataLen, result);
1777 ca_mutex_unlock(g_bleIsServerMutex);
1781 ca_mutex_unlock(g_bleIsServerMutex);
1783 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1787 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1791 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1794 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1798 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
1802 CAResult_t result = CA_STATUS_FAILED;
1804 ca_mutex_lock(g_bleIsServerMutex);
1805 if (true == g_isServer)
1807 result = CALEAdapterServerSendData(NULL, data, dataLen);
1808 if (CA_STATUS_OK != result)
1810 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data failed" );
1812 ca_mutex_unlock(g_bleIsServerMutex);
1815 g_errorHandler(endpoint, data, dataLen, result);
1822 result = CALEAdapterClientSendData(NULL, data, dataLen);
1823 if (CA_STATUS_OK != result)
1825 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data failed" );
1828 g_errorHandler(endpoint, data, dataLen, result);
1830 ca_mutex_unlock(g_bleIsServerMutex);
1834 ca_mutex_unlock(g_bleIsServerMutex);
1836 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1840 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
1842 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1844 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
1846 char *local_address = NULL;
1848 CAResult_t res = CAGetLEAddress(&local_address);
1849 if (CA_STATUS_OK != res)
1851 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
1855 if (NULL == local_address)
1857 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
1858 return CA_STATUS_FAILED;
1862 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
1863 if (NULL == (*info))
1865 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
1866 OICFree(local_address);
1867 return CA_STATUS_FAILED;
1870 size_t local_address_len = strlen(local_address);
1872 if(local_address_len >= sizeof(g_localBLEAddress) ||
1873 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
1875 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
1877 OICFree(local_address);
1878 return CA_STATUS_FAILED;
1881 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
1882 ca_mutex_lock(g_bleLocalAddressMutex);
1883 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
1884 ca_mutex_unlock(g_bleLocalAddressMutex);
1886 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
1888 OICFree(local_address);
1890 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1891 return CA_STATUS_OK;
1894 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
1896 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1898 ca_mutex_lock(g_bleNetworkCbMutex);
1899 g_networkCallback = netCallback;
1900 ca_mutex_unlock(g_bleNetworkCbMutex);
1901 CAResult_t res = CA_STATUS_OK;
1904 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
1905 if (CA_STATUS_OK != res)
1907 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1912 res = CAUnSetLEAdapterStateChangedCb();
1913 if (CA_STATUS_OK != res)
1915 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1919 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1923 static void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
1925 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1927 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
1928 CAEndpoint_t localEndpoint = {};
1930 ca_mutex_lock(g_bleLocalAddressMutex);
1931 OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), g_localBLEAddress);
1932 ca_mutex_unlock(g_bleLocalAddressMutex);
1934 g_bleAdapterState = adapter_state;
1935 // Start a GattServer/Client if gLeServerStatus is SET
1936 if (CA_LISTENING_SERVER == gLeServerStatus)
1938 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
1939 CAStartLEGattServer();
1941 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
1943 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
1944 CAStartLEGattClient();
1946 gLeServerStatus = CA_SERVER_NOTSTARTED;
1948 ca_mutex_lock(g_bleNetworkCbMutex);
1949 if (NULL != g_networkCallback)
1951 g_networkCallback(&localEndpoint, adapter_state);
1955 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
1957 ca_mutex_unlock(g_bleNetworkCbMutex);
1959 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1962 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
1966 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1968 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
1969 #ifndef SINGLE_THREAD
1970 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
1971 "g_bleClientSendQueueHandle is NULL",
1973 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
1974 "g_bleClientSendDataMutex is NULL",
1977 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
1980 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
1982 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
1985 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
1986 return CA_MEMORY_ALLOC_FAILED;
1988 // Add message to send queue
1989 ca_mutex_lock(g_bleClientSendDataMutex);
1990 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
1991 ca_mutex_unlock(g_bleClientSendDataMutex);
1993 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1994 return CA_STATUS_OK;
1997 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2001 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2003 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2005 #ifdef SINGLE_THREAD
2006 char header[CA_HEADER_LENGTH] = {0};
2008 CAResult_t result = CAGenerateHeader(header, dataLen);
2010 if (CA_STATUS_OK != result)
2012 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
2016 if (!CAIsLEConnected())
2018 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2022 result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH);
2023 if (CA_STATUS_OK != result)
2025 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2029 int32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2030 for (int32_t iter = 0; iter < dataLimit; iter++)
2032 result = CAUpdateCharacteristicsToAllGattClients((data +
2033 (iter * CA_SUPPORTED_BLE_MTU_SIZE)),
2034 CA_SUPPORTED_BLE_MTU_SIZE);
2035 if (CA_STATUS_OK != result)
2037 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2043 uint8_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2046 result = CAUpdateCharacteristicsToAllGattClients((data +
2047 (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE)),
2049 if (CA_STATUS_OK != result)
2051 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2057 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2058 "BleClientReceiverQueue is NULL",
2060 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2061 "BleClientSendDataMutex is NULL",
2064 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2067 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2069 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
2072 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2073 return CA_MEMORY_ALLOC_FAILED;
2075 // Add message to send queue
2076 ca_mutex_lock(g_bleServerSendDataMutex);
2077 CAQueueingThreadAddData(g_bleServerSendQueueHandle, bleData, sizeof(CALEData_t));
2078 ca_mutex_unlock(g_bleServerSendDataMutex);
2080 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2081 return CA_STATUS_OK;
2084 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2086 uint32_t dataLength,
2087 uint32_t *sentLength)
2089 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2092 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2093 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2095 #ifdef SINGLE_THREAD
2096 if(g_networkPacketReceivedCallback)
2098 CAEndpoint_t endPoint = { 0 }; // will be filled by upper layer
2099 endPoint.adapter = CA_ADAPTER_GATT_BTLE;
2100 g_networkPacketReceivedCallback(&endPoint, data, dataLength);
2103 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG, "g_bleReceiverQueue",
2106 //Add message to data queue
2107 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2108 CA_ADAPTER_GATT_BTLE,
2110 if (NULL == remoteEndpoint)
2112 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2113 return CA_STATUS_FAILED;
2116 // Create bleData to add to queue
2117 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
2119 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
2122 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2123 CAFreeEndpoint(remoteEndpoint);
2124 return CA_MEMORY_ALLOC_FAILED;
2127 CAFreeEndpoint(remoteEndpoint);
2128 // Add message to receiver queue
2129 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2131 *sentLength = dataLength;
2133 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2134 return CA_STATUS_OK;
2137 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2139 uint32_t dataLength,
2140 uint32_t *sentLength)
2142 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2145 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2146 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2147 #ifndef SINGLE_THREAD
2148 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG, "g_bleReceiverQueue",
2151 //Add message to data queue
2152 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2153 CA_ADAPTER_GATT_BTLE,
2155 if (NULL == remoteEndpoint)
2157 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2158 return CA_STATUS_FAILED;
2161 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
2163 // Create bleData to add to queue
2164 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
2167 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2168 CAFreeEndpoint(remoteEndpoint);
2169 return CA_MEMORY_ALLOC_FAILED;
2172 CAFreeEndpoint(remoteEndpoint);
2173 // Add message to receiver queue
2174 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2176 *sentLength = dataLength;
2178 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2179 return CA_STATUS_OK;
2182 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2184 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2186 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2187 g_bleAdapterThreadPool = handle;
2188 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2190 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2193 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2195 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2197 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2199 g_networkPacketReceivedCallback = callback;
2201 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2203 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2206 static void CALEErrorHandler(const char *remoteAddress,
2211 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2213 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2214 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2215 CA_ADAPTER_GATT_BTLE,
2218 //if required, will be used to build remote end point
2219 g_errorHandler(rep, data, dataLen, result);
2221 CAFreeEndpoint(rep);
2222 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");