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 /// Remote endpoint contains the information of remote device.
52 CAEndpoint_t *remoteEndpoint;
54 /// Data to be transmitted over LE transport.
57 /// Length of the data being transmitted.
62 * Stores information of all the senders.
64 * This structure will be used to track and defragment all incoming
70 uint32_t totalDataLen;
72 CAEndpoint_t *remoteEndpoint;
76 * Callback to provide the status of the network change to CA layer.
78 static CANetworkChangeCallback g_networkCallback = NULL;
81 * bleAddress of the local adapter. Value will be initialized to zero,
82 * and will be updated later.
84 static char g_localBLEAddress[18] = { 0 };
87 * Variable to differentiate btw GattServer and GattClient.
89 static bool g_isServer = false;
92 * Mutex to synchronize the task to be executed on the GattServer
95 static ca_mutex g_bleIsServerMutex = NULL;
98 * Mutex to synchronize the callback to be called for the network
101 static ca_mutex g_bleNetworkCbMutex = NULL;
104 * Mutex to synchronize the updates of the local LE address of the
107 static ca_mutex g_bleLocalAddressMutex = NULL;
110 * Reference to thread pool.
112 static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
115 * Mutex to synchronize the task to be pushed to thread pool.
117 static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
120 * Mutex to synchronize the queing of the data from SenderQueue.
122 static ca_mutex g_bleClientSendDataMutex = NULL;
125 * Mutex to synchronize the queing of the data from ReceiverQueue.
127 static ca_mutex g_bleReceiveDataMutex = NULL;
131 * Mutex to synchronize the queing of the data from SenderQueue.
133 static ca_mutex g_bleServerSendDataMutex = NULL;
136 * Mutex to synchronize the callback to be called for the
137 * adapterReqResponse.
139 static ca_mutex g_bleAdapterReqRespCbMutex = NULL;
142 * Callback to be called when network packet received from either
143 * GattServer or GattClient.
145 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
148 * Callback to notify error from the BLE adapter.
150 static CAErrorHandleCallback g_errorHandler = NULL;
153 * Storing Adapter state information.
155 static CAAdapterState_t g_bleAdapterState = CA_ADAPTER_DISABLED;
160 * This enumeration provides information of LE Adapter Server status.
164 CA_SERVER_NOTSTARTED = 0,
170 * Structure to maintain the status of the server.
172 static CALeServerStatus gLeServerStatus = CA_SERVER_NOTSTARTED;
175 * Register network change notification callback.
177 * @param[in] netCallback CANetworkChangeCallback callback which will
178 * be set for the change in network.
180 * @return 0 on success otherwise a positive error value.
181 * @retval ::CA_STATUS_OK Successful.
182 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
183 * @retval ::CA_STATUS_FAILED Operation failed.
186 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback);
189 * Set the thread pool handle which is required for spawning new
192 * @param[in] handle Thread pool handle which is given by above layer
193 * for using thread creation task.
196 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
199 * Call the callback to the upper layer when the device state gets
202 * @param[in] adapter_state New state of the adapter to be notified to
205 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state);
208 * Used to initialize all required mutex variable for LE Adapter
211 * @return 0 on success otherwise a positive error value.
212 * @retval ::CA_STATUS_OK Successful.
213 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
214 * @retval ::CA_STATUS_FAILED Operation failed.
217 static CAResult_t CAInitLEAdapterMutex();
220 * Terminate all required mutex variables for LE adapter
223 static void CATerminateLEAdapterMutex();
226 * Prepares and notify error through error callback.
228 static void CALEErrorHandler(const char *remoteAddress,
233 #ifndef SINGLE_THREAD
235 * Stop condition of recvhandler.
237 static bool g_dataReceiverHandlerState = false;
240 * Sender information.
242 static u_arraylist_t *g_senderInfo = NULL;
245 * Queue to process the outgoing packets from GATTClient.
247 static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
250 * Queue to process the incoming packets to GATT Client.
252 static CAQueueingThread_t *g_bleReceiverQueue = NULL;
255 * Queue to process the outgoing packets from GATTServer.
257 static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
260 * Starting LE connectivity adapters.
262 * As its peer to peer it does not require to start any servers.
264 * @return ::CA_STATUS_OK or Appropriate error code.
266 static CAResult_t CAStartLE();
269 * Start listening server for receiving multicast search requests.
271 * Transport Specific Behavior:
272 * LE Starts GATT Server with prefixed UUID and Characteristics
273 * per OIC Specification.
274 * @return ::CA_STATUS_OK or Appropriate error code.
276 static CAResult_t CAStartLEListeningServer();
279 * Sarting discovery of servers for receiving multicast
282 * Transport Specific Behavior:
283 * LE Starts GATT Server with prefixed UUID and Characteristics
284 * per OIC Specification.
286 * @return ::CA_STATUS_OK or Appropriate error code
288 static CAResult_t CAStartLEDiscoveryServer();
291 * Send data to the endpoint using the adapter connectivity.
293 * @param[in] endpoint Remote Endpoint information (like MAC address,
294 * reference URI and connectivity type) to which
295 * the unicast data has to be sent.
296 * @param[in] data Data which required to be sent.
297 * @param[in] dataLen Size of data to be sent.
299 * @note dataLen must be > 0.
301 * @return The number of bytes sent on the network, or -1 on error.
303 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
308 * Send multicast data to the endpoint using the LE connectivity.
310 * @param[in] endpoint Remote Endpoint information to which the
311 * multicast data has to be sent.
312 * @param[in] data Data which required to be sent.
313 * @param[in] dataLen Size of data to be sent.
315 * @note dataLen must be > 0.
317 * @return The number of bytes sent on the network, or -1 on error.
319 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
324 * Get LE Connectivity network information.
326 * @param[out] info Local connectivity information structures.
327 * @param[out] size Number of local connectivity structures.
329 * @return ::CA_STATUS_OK or Appropriate error code.
331 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info,
335 * Read Synchronous API callback.
337 * @return ::CA_STATUS_OK or Appropriate error code.
339 static CAResult_t CAReadLEData();
342 * Stopping the adapters and close socket connections.
344 * LE Stops all GATT servers and GATT Clients.
346 * @return ::CA_STATUS_OK or Appropriate error code.
348 static CAResult_t CAStopLE();
351 * Terminate the LE connectivity adapter.
353 * Configuration information will be deleted from further use.
355 static void CATerminateLE();
358 * Set the NetworkPacket received callback to CA layer from adapter
361 * @param[in] callback Callback handle sent from the upper layer.
363 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
366 * Push the data from CA layer to the Sender processor queue.
368 * @param[in] remoteEndpoint Remote endpoint information of the
370 * @param[in] data Data to be transmitted from LE.
371 * @param[in] dataLen Length of the Data being transmitted.
373 * @return ::CA_STATUS_OK or Appropriate error code.
374 * @retval ::CA_STATUS_OK Successful.
375 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
376 * @retval ::CA_STATUS_FAILED Operation failed.
378 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
383 * Push the data from CA layer to the Sender processor queue.
385 * @param[in] remoteEndpoint Remote endpoint information of the
387 * @param[in] data Data to be transmitted from LE.
388 * @param[in] dataLen Length of the Data being transmitted.
390 * @return ::CA_STATUS_OK or Appropriate error code.
391 * @retval ::CA_STATUS_OK Successful.
392 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
393 * @retval ::CA_STATUS_FAILED Operation failed.
395 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
400 * This function will receive the data from the GattServer and add the
401 * data to the Server receiver queue.
403 * @param[in] remoteAddress Remote address of the device from where
405 * @param[in] data Actual data recevied from the remote
407 * @param[in] dataLength Length of the data received from the
409 * @param[in] sentLength Length of the data sent from the remote
412 * @return ::CA_STATUS_OK or Appropriate error code.
413 * @retval ::CA_STATUS_OK Successful.
414 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
415 * @retval ::CA_STATUS_FAILED Operation failed.
418 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
421 uint32_t *sentLength);
424 * This function will receive the data from the GattClient and add the
425 * data into the Client receiver queue.
427 * @param[in] remoteAddress Remote address of the device from where
429 * @param[in] data Actual data recevied from the remote
431 * @param[in] dataLength Length of the data received from the
433 * @param[in] sentLength Length of the data sent from the remote
436 * @return ::CA_STATUS_OK or Appropriate error code.
437 * @retval ::CA_STATUS_OK Successful.
438 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
439 * @retval ::CA_STATUS_FAILED Operation failed.
441 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
444 uint32_t *sentLength);
447 * This function will be associated with the sender queue for
450 * This function will fragment the data to the MTU of the transport
451 * and send the data in fragments to the adapters. The function will
452 * be blocked until all data is sent out from the adapter.
454 * @param[in] threadData Data pushed to the queue which contains the
455 * info about RemoteEndpoint and Data.
457 static void CALEServerSendDataThread(void *threadData);
460 * This function will be associated with the sender queue for
463 * This function will fragment the data to the MTU of the transport
464 * and send the data in fragments to the adapters. The function will
465 * be blocked until all data is sent out from the adapter.
467 * @param[in] threadData Data pushed to the queue which contains the
468 * info about RemoteEndpoint and Data.
470 static void CALEClientSendDataThread(void *threadData);
473 * This function will be associated with the receiver queue.
475 * This function will defragment the received data from each sender
476 * respectively and will send it up to CA layer. Respective sender's
477 * header will provide the length of the data sent.
479 * @param[in] threadData Data pushed to the queue which contains the
480 * info about RemoteEndpoint and Data.
482 static void CALEDataReceiverHandler(void *threadData);
485 * This function will stop all queues created for GattServer and
486 * GattClient. All four queues will be be stopped with this function
489 static void CAStopLEQueues();
492 * This function will terminate all queues created for GattServer and
493 * GattClient. All four queues will be be terminated with this
494 * function invocations.
496 static void CATerminateLEQueues();
499 * This function will initalize the Receiver and Sender queues for
500 * GattServer. This function will in turn call the functions
501 * CAInitBleServerReceiverQueue() and CAInitBleServerSenderQueue() to
502 * initialize the queues.
504 * @return ::CA_STATUS_OK or Appropriate error code.
505 * @retval ::CA_STATUS_OK Successful.
506 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
507 * @retval ::CA_STATUS_FAILED Operation failed.
509 static CAResult_t CAInitLEServerQueues();
512 * This function will initalize the Receiver and Sender queues for
513 * GattClient. This function will inturn call the functions
514 * CAInitBleClientReceiverQueue() and CAInitBleClientSenderQueue() to
515 * initialize the queues.
517 * @return ::CA_STATUS_OK or Appropriate error code.
518 * @retval ::CA_STATUS_OK Successful.
519 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
520 * @retval ::CA_STATUS_FAILED Operation failed.
523 static CAResult_t CAInitLEClientQueues();
526 * This function will initalize the Receiver queue for
527 * GattServer. This will initialize the queue to process the function
528 * CABLEServerSendDataThread() when ever the task is added to this
531 * @return ::CA_STATUS_OK or Appropriate error code.
532 * @retval ::CA_STATUS_OK Successful.
533 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
534 * @retval ::CA_STATUS_FAILED Operation failed.
536 static CAResult_t CAInitLEServerSenderQueue();
539 * This function will initalize the Receiver queue for
540 * GattClient. This will initialize the queue to process the function
541 * CABLEClientSendDataThread() when ever the task is added to this
544 * @return ::CA_STATUS_OK or Appropriate error code.
545 * @retval ::CA_STATUS_OK Successful.
546 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
547 * @retval ::CA_STATUS_FAILED Operation failed.
549 static CAResult_t CAInitLEClientSenderQueue();
552 * This function will initialize the Receiver queue for
553 * LEAdapter. This will initialize the queue to process the function
554 * CABLEDataReceiverHandler() when ever the task is added to this
557 * @return ::CA_STATUS_OK or Appropriate error code
558 * @retval ::CA_STATUS_OK Successful
559 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
560 * @retval ::CA_STATUS_FAILED Operation failed
563 static CAResult_t CAInitLEReceiverQueue();
566 * This function will create the Data required to send it in the
569 * @param[in] remoteEndpoint Remote endpoint information of the
571 * @param[in] data Data to be transmitted from LE.
572 * @param[in] dataLength Length of the Data being transmitted.
574 * @return ::CA_STATUS_OK or Appropriate error code.
575 * @retval ::CA_STATUS_OK Successful.
576 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments.
577 * @retval ::CA_STATUS_FAILED Operation failed.
579 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
581 uint32_t dataLength);
584 * Used to free the BLE information stored in the sender/receiver
587 * @param[in] bleData Information for a particular data segment.
589 static void CAFreeLEData(CALEData_t *bleData);
594 static void CALEDataDestroyer(void *data, uint32_t size);
596 static CAResult_t CAInitLEServerQueues()
598 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
600 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
602 CAResult_t result = CAInitLEServerSenderQueue();
603 if (CA_STATUS_OK != result)
605 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
606 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
607 return CA_STATUS_FAILED;
610 result = CAInitLEReceiverQueue();
611 if (CA_STATUS_OK != result)
613 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerReceiverQueue failed");
614 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
615 return CA_STATUS_FAILED;
618 g_dataReceiverHandlerState = true;
620 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
622 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
626 static CAResult_t CAInitLEClientQueues()
628 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
630 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
632 CAResult_t result = CAInitLEClientSenderQueue();
633 if (CA_STATUS_OK != result)
635 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
636 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
637 return CA_STATUS_FAILED;
640 result = CAInitLEReceiverQueue();
641 if (CA_STATUS_OK != result)
643 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientReceiverQueue failed");
644 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
645 return CA_STATUS_FAILED;
648 g_dataReceiverHandlerState = true;
650 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
652 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
656 static CAResult_t CAInitLEReceiverQueue()
658 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
659 // Check if the message queue is already initialized
660 if (g_bleReceiverQueue)
662 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
666 // Create recv message queue
667 g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
668 if (!g_bleReceiverQueue)
670 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
671 return CA_MEMORY_ALLOC_FAILED;
674 g_senderInfo = u_arraylist_create();
677 OIC_LOG(ERROR, CALEADAPTER_TAG, "ClientInfo memory allcation failed!");
678 OICFree(g_bleReceiverQueue);
679 g_bleReceiverQueue = NULL;
680 return CA_MEMORY_ALLOC_FAILED;
683 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue, g_bleAdapterThreadPool,
684 CALEDataReceiverHandler, CALEDataDestroyer))
686 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
687 OICFree(g_bleReceiverQueue);
688 g_bleReceiverQueue = NULL;
689 u_arraylist_free(&g_senderInfo);
690 return CA_STATUS_FAILED;
693 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue))
695 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
696 OICFree(g_bleReceiverQueue);
697 g_bleReceiverQueue = NULL;
698 u_arraylist_free(&g_senderInfo);
699 return CA_STATUS_FAILED;
702 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
706 static CAResult_t CAInitLEServerSenderQueue()
708 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
709 // Check if the message queue is already initialized
710 if (g_bleServerSendQueueHandle)
712 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
716 // Create send message queue
717 g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
718 if (!g_bleServerSendQueueHandle)
720 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
721 return CA_MEMORY_ALLOC_FAILED;
724 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
725 g_bleAdapterThreadPool,
726 CALEServerSendDataThread, CALEDataDestroyer))
728 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
729 OICFree(g_bleServerSendQueueHandle);
730 g_bleServerSendQueueHandle = NULL;
731 return CA_STATUS_FAILED;
734 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerSendQueueHandle))
736 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
737 OICFree(g_bleServerSendQueueHandle);
738 g_bleServerSendQueueHandle = NULL;
739 return CA_STATUS_FAILED;
742 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
746 static void CALEClearSenderInfo()
748 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
750 uint32_t listIndex = 0;
751 uint32_t listLength = u_arraylist_length(g_senderInfo);
752 for (listIndex = 0; listIndex < listLength; listIndex++)
754 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(g_senderInfo, listIndex);
760 OICFree(info->defragData);
761 CAFreeEndpoint(info->remoteEndpoint);
764 u_arraylist_free(&g_senderInfo);
765 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
768 static CAResult_t CAInitLEClientSenderQueue()
770 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
772 if (g_bleClientSendQueueHandle)
774 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
778 // Create send message queue
779 g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
780 if (!g_bleClientSendQueueHandle)
782 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
783 return CA_MEMORY_ALLOC_FAILED;
786 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
787 g_bleAdapterThreadPool,
788 CALEClientSendDataThread, CALEDataDestroyer))
790 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
791 OICFree(g_bleClientSendQueueHandle);
792 g_bleClientSendQueueHandle = NULL;
793 return CA_STATUS_FAILED;
796 if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientSendQueueHandle))
798 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
799 OICFree(g_bleClientSendQueueHandle);
800 g_bleClientSendQueueHandle = NULL;
801 return CA_STATUS_FAILED;
804 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
808 static void CAStopLEQueues()
810 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
812 ca_mutex_lock(g_bleClientSendDataMutex);
813 if (NULL != g_bleClientSendQueueHandle)
815 CAQueueingThreadStop(g_bleClientSendQueueHandle);
817 ca_mutex_unlock(g_bleClientSendDataMutex);
819 ca_mutex_lock(g_bleServerSendDataMutex);
820 if (NULL != g_bleServerSendQueueHandle)
822 CAQueueingThreadStop(g_bleServerSendQueueHandle);
824 ca_mutex_unlock(g_bleServerSendDataMutex);
826 ca_mutex_lock(g_bleReceiveDataMutex);
827 if (NULL != g_bleReceiverQueue)
829 CAQueueingThreadStop(g_bleReceiverQueue);
831 ca_mutex_unlock(g_bleReceiveDataMutex);
833 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
836 static void CATerminateLEQueues()
838 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
840 CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
841 OICFree(g_bleClientSendQueueHandle);
842 g_bleClientSendQueueHandle = NULL;
844 CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
845 OICFree(g_bleServerSendQueueHandle);
846 g_bleServerSendQueueHandle = NULL;
848 CAQueueingThreadDestroy(g_bleReceiverQueue);
849 OICFree(g_bleReceiverQueue);
850 g_bleReceiverQueue = NULL;
852 CALEClearSenderInfo();
854 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
857 static CAResult_t CALEGetSenderInfo(const char *leAddress,
858 CABLESenderInfo_t **senderInfo,
859 uint32_t *senderIndex)
861 VERIFY_NON_NULL_RET(leAddress,
863 "NULL BLE address argument",
864 CA_STATUS_INVALID_PARAM);
865 VERIFY_NON_NULL_RET(senderIndex,
867 "NULL index argument",
868 CA_STATUS_INVALID_PARAM);
870 const uint32_t listLength = u_arraylist_length(g_senderInfo);
871 const uint32_t addrLength = strlen(leAddress);
872 for (uint32_t index = 0; index < listLength; index++)
874 CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(g_senderInfo, index);
875 if(!info || !(info->remoteEndpoint))
880 if(!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
882 *senderIndex = index;
891 return CA_STATUS_FAILED;
894 static void CALEDataReceiverHandler(void *threadData)
896 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
898 ca_mutex_lock(g_bleReceiveDataMutex);
900 if (g_dataReceiverHandlerState)
902 OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
904 CALEData_t *bleData = (CALEData_t *) threadData;
907 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
908 ca_mutex_unlock(g_bleReceiveDataMutex);
912 if(!(bleData->remoteEndpoint))
914 OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!");
915 ca_mutex_unlock(g_bleReceiveDataMutex);
919 CABLESenderInfo_t *senderInfo = NULL;
920 uint32_t senderIndex = 0;
922 if(CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
923 &senderInfo, &senderIndex))
925 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s]",
926 bleData->remoteEndpoint->addr);
931 CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
934 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
935 ca_mutex_unlock(g_bleReceiveDataMutex);
938 newSender->recvDataLen = 0;
939 newSender->totalDataLen = 0;
940 newSender->defragData = NULL;
941 newSender->remoteEndpoint = NULL;
943 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
944 newSender->totalDataLen = CAParseHeader(bleData->data,
946 if(!(newSender->totalDataLen))
948 OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
950 ca_mutex_unlock(g_bleReceiveDataMutex);
954 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
955 newSender->totalDataLen);
956 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
959 newSender->defragData = OICCalloc(newSender->totalDataLen + 1,
960 sizeof(*newSender->defragData));
962 if (NULL == newSender->defragData)
964 OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
966 ca_mutex_unlock(g_bleReceiveDataMutex);
970 const char *remoteAddress = bleData->remoteEndpoint->addr;
971 newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
972 CA_ADAPTER_GATT_BTLE, remoteAddress, 0);
973 if (NULL == newSender->remoteEndpoint)
975 OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
976 OICFree(newSender->defragData);
978 ca_mutex_unlock(g_bleReceiveDataMutex);
981 memcpy(newSender->defragData, bleData->data + CA_HEADER_LENGTH,
982 bleData->dataLen - CA_HEADER_LENGTH);
983 newSender->recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
984 u_arraylist_add(g_senderInfo,(void *)newSender);
986 //Getting newSender index position in g_senderInfo array list
988 CALEGetSenderInfo(newSender->remoteEndpoint->addr, NULL, &senderIndex))
990 OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
991 OICFree(newSender->defragData);
992 CAFreeEndpoint(newSender->remoteEndpoint);
994 ca_mutex_unlock(g_bleReceiveDataMutex);
997 senderInfo = newSender;
1001 if(senderInfo->recvDataLen + bleData->dataLen > senderInfo->totalDataLen)
1003 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1004 "Data Length exceeding error!! Receiving [%d] total length [%d]",
1005 senderInfo->recvDataLen + bleData->dataLen, senderInfo->totalDataLen);
1006 u_arraylist_remove(g_senderInfo, senderIndex);
1007 OICFree(senderInfo->defragData);
1008 OICFree(senderInfo);
1009 ca_mutex_unlock(g_bleReceiveDataMutex);
1012 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
1013 memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data,
1015 senderInfo->recvDataLen += bleData->dataLen ;
1016 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
1017 senderInfo->totalDataLen, senderInfo->recvDataLen);
1020 if (senderInfo->totalDataLen == senderInfo->recvDataLen)
1022 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
1023 if (NULL == g_networkPacketReceivedCallback)
1025 OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
1027 u_arraylist_remove(g_senderInfo, senderIndex);
1028 OICFree(senderInfo->defragData);
1029 OICFree(senderInfo);
1030 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1031 ca_mutex_unlock(g_bleReceiveDataMutex);
1034 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
1035 g_networkPacketReceivedCallback(senderInfo->remoteEndpoint,
1036 senderInfo->defragData, senderInfo->recvDataLen);
1037 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
1038 u_arraylist_remove(g_senderInfo, senderIndex);
1039 senderInfo->remoteEndpoint = NULL;
1040 senderInfo->defragData = NULL;
1041 OICFree(senderInfo);
1044 ca_mutex_unlock(g_bleReceiveDataMutex);
1045 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1048 static void CALEServerSendDataThread(void *threadData)
1050 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1052 CALEData_t * const bleData = (CALEData_t *) threadData;
1055 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1059 uint8_t * const header = OICCalloc(CA_HEADER_LENGTH, 1);
1060 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1062 const uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1066 "Server total Data length with header is [%u]",
1069 uint8_t * const dataSegment = OICCalloc(totalLength, 1);
1071 if (NULL == dataSegment)
1073 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1078 CAResult_t result = CAGenerateHeader(header,
1081 if (CA_STATUS_OK != result )
1083 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1085 OICFree(dataSegment);
1089 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1092 uint32_t length = 0;
1093 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1095 length = totalLength;
1096 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
1100 length = CA_SUPPORTED_BLE_MTU_SIZE;
1101 memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
1102 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1105 uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1108 // Send the first segment with the header.
1109 if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
1111 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
1113 result = CAUpdateCharacteristicsToGattClient(
1114 bleData->remoteEndpoint->addr, dataSegment, length);
1116 if (CA_STATUS_OK != result)
1120 "Update characteristics failed, result [%d]",
1123 g_errorHandler(bleData->remoteEndpoint,
1127 OICFree(dataSegment);
1133 "Server Sent data length [%u]",
1135 for (index = 1; index < iter; index++)
1137 // Send the remaining header.
1140 "Sending the chunk number [%u]",
1144 CAUpdateCharacteristicsToGattClient(
1145 bleData->remoteEndpoint->addr,
1146 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1147 CA_SUPPORTED_BLE_MTU_SIZE);
1148 if (CA_STATUS_OK != result)
1150 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1151 "Update characteristics failed, result [%d]", result);
1152 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1153 OICFree(dataSegment);
1156 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
1157 CA_SUPPORTED_BLE_MTU_SIZE);
1160 const uint32_t remainingLen =
1161 totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1163 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1165 // send the last segment of the data (Ex: 22 bytes of 622
1166 // bytes of data when MTU is 200)
1167 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1168 result = CAUpdateCharacteristicsToGattClient(
1169 bleData->remoteEndpoint->addr,
1170 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1172 if (CA_STATUS_OK != result)
1176 "Update characteristics failed, result [%d]",
1178 g_errorHandler(bleData->remoteEndpoint,
1182 OICFree(dataSegment);
1185 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1190 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
1191 result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
1192 if (CA_STATUS_OK != result)
1194 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1196 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1197 OICFree(dataSegment);
1200 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
1201 for (index = 1; index < iter; index++)
1203 // Send the remaining header.
1204 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
1205 result = CAUpdateCharacteristicsToAllGattClients(
1206 bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
1207 CA_SUPPORTED_BLE_MTU_SIZE);
1208 if (CA_STATUS_OK != result)
1210 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1212 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1213 OICFree(dataSegment);
1216 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%u]", CA_SUPPORTED_BLE_MTU_SIZE);
1219 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1220 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1222 // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
1223 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1224 result = CAUpdateCharacteristicsToAllGattClients(
1225 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1227 if (CA_STATUS_OK != result)
1229 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1231 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1232 OICFree(dataSegment);
1235 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
1238 OICFree(dataSegment);
1240 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1243 static void CALEClientSendDataThread(void *threadData)
1245 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1247 CALEData_t *bleData = (CALEData_t *) threadData;
1250 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
1254 uint8_t * const header = OICCalloc(CA_HEADER_LENGTH, 1);
1255 VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
1257 const uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
1258 uint8_t *dataSegment = OICCalloc(totalLength, 1);
1259 if (NULL == dataSegment)
1261 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
1266 CAResult_t result = CAGenerateHeader(header,
1269 if (CA_STATUS_OK != result )
1271 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
1273 OICFree(dataSegment);
1276 memcpy(dataSegment, header, CA_HEADER_LENGTH);
1279 uint32_t length = 0;
1280 if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
1282 length = totalLength;
1283 memcpy(dataSegment + CA_HEADER_LENGTH,
1289 length = CA_SUPPORTED_BLE_MTU_SIZE;
1290 memcpy(dataSegment + CA_HEADER_LENGTH,
1292 CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
1295 const uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
1297 if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
1299 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
1300 // Send the first segment with the header.
1302 CAUpdateCharacteristicsToGattServer(
1303 bleData->remoteEndpoint->addr,
1309 if (CA_STATUS_OK != result)
1313 "Update characteristics failed, result [%d]",
1315 g_errorHandler(bleData->remoteEndpoint,
1319 OICFree(dataSegment);
1325 "Client Sent Data length is [%u]",
1328 for (index = 1; index < iter; index++)
1330 // Send the remaining header.
1331 result = CAUpdateCharacteristicsToGattServer(
1332 bleData->remoteEndpoint->addr,
1333 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1334 CA_SUPPORTED_BLE_MTU_SIZE,
1336 if (CA_STATUS_OK != result)
1340 "Update characteristics failed, result [%d]",
1342 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1343 OICFree(dataSegment);
1346 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1347 CA_SUPPORTED_BLE_MTU_SIZE);
1350 const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1351 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1353 // send the last segment of the data (Ex: 22 bytes of 622
1354 // bytes of data when MTU is 200)
1355 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1356 result = CAUpdateCharacteristicsToGattServer(
1357 bleData->remoteEndpoint->addr,
1358 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1362 if (CA_STATUS_OK != result)
1364 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
1366 g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
1367 OICFree(dataSegment);
1370 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1375 //Sending Mulitcast Data
1376 // Send the first segment with the header.
1377 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
1378 result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
1379 if (CA_STATUS_OK != result)
1381 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1382 "Update characteristics (all) failed, result [%d]", result);
1383 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1384 OICFree(dataSegment);
1387 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
1388 // Send the remaining header.
1389 for (index = 1; index < iter; index++)
1391 result = CAUpdateCharacteristicsToAllGattServers(
1392 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1393 CA_SUPPORTED_BLE_MTU_SIZE);
1394 if (CA_STATUS_OK != result)
1396 OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]",
1398 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1399 OICFree(dataSegment);
1402 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
1403 CA_SUPPORTED_BLE_MTU_SIZE);
1406 uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
1407 if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
1409 // send the last segment of the data (Ex: 22 bytes of 622
1410 // bytes of data when MTU is 200)
1411 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
1413 CAUpdateCharacteristicsToAllGattServers(
1414 bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
1416 if (CA_STATUS_OK != result)
1418 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1419 "Update characteristics (all) failed, result [%d]", result);
1420 CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
1421 OICFree(dataSegment);
1424 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
1429 OICFree(dataSegment);
1431 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
1434 static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint,
1435 const uint8_t *data,
1436 uint32_t dataLength)
1438 CALEData_t * const bleData = OICMalloc(sizeof(CALEData_t));
1442 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1446 bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
1447 bleData->data = OICCalloc(dataLength + 1, 1);
1449 if (NULL == bleData->data)
1451 OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
1452 CAFreeLEData(bleData);
1456 memcpy(bleData->data, data, dataLength);
1457 bleData->dataLen = dataLength;
1462 static void CAFreeLEData(CALEData_t *bleData)
1464 VERIFY_NON_NULL_VOID(bleData, CALEADAPTER_TAG, "Param bleData is NULL");
1466 CAFreeEndpoint(bleData->remoteEndpoint);
1467 OICFree(bleData->data);
1471 static void CALEDataDestroyer(void *data, uint32_t size)
1473 if ((size_t)size < sizeof(CALEData_t *))
1475 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
1476 "Destroy data too small %p %d", data, size);
1478 CALEData_t *ledata = (CALEData_t *) data;
1480 CAFreeLEData(ledata);
1484 static CAResult_t CAInitLEAdapterMutex()
1486 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1488 if (NULL == g_bleIsServerMutex)
1490 g_bleIsServerMutex = ca_mutex_new();
1491 if (NULL == g_bleIsServerMutex)
1493 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1494 return CA_STATUS_FAILED;
1498 if (NULL == g_bleNetworkCbMutex)
1500 g_bleNetworkCbMutex = ca_mutex_new();
1501 if (NULL == g_bleNetworkCbMutex)
1503 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1504 CATerminateLEAdapterMutex();
1505 return CA_STATUS_FAILED;
1509 if (NULL == g_bleLocalAddressMutex)
1511 g_bleLocalAddressMutex = ca_mutex_new();
1512 if (NULL == g_bleLocalAddressMutex)
1514 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1515 CATerminateLEAdapterMutex();
1516 return CA_STATUS_FAILED;
1520 if (NULL == g_bleAdapterThreadPoolMutex)
1522 g_bleAdapterThreadPoolMutex = ca_mutex_new();
1523 if (NULL == g_bleAdapterThreadPoolMutex)
1525 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1526 CATerminateLEAdapterMutex();
1527 return CA_STATUS_FAILED;
1531 if (NULL == g_bleClientSendDataMutex)
1533 g_bleClientSendDataMutex = ca_mutex_new();
1534 if (NULL == g_bleClientSendDataMutex)
1536 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1537 CATerminateLEAdapterMutex();
1538 return CA_STATUS_FAILED;
1542 if (NULL == g_bleServerSendDataMutex)
1544 g_bleServerSendDataMutex = ca_mutex_new();
1545 if (NULL == g_bleServerSendDataMutex)
1547 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1548 CATerminateLEAdapterMutex();
1549 return CA_STATUS_FAILED;
1553 if (NULL == g_bleAdapterReqRespCbMutex)
1555 g_bleAdapterReqRespCbMutex = ca_mutex_new();
1556 if (NULL == g_bleAdapterReqRespCbMutex)
1558 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1559 CATerminateLEAdapterMutex();
1560 return CA_STATUS_FAILED;
1564 if (NULL == g_bleReceiveDataMutex)
1566 g_bleReceiveDataMutex = ca_mutex_new();
1567 if (NULL == g_bleReceiveDataMutex)
1569 OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
1570 return CA_STATUS_FAILED;
1574 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1575 return CA_STATUS_OK;
1578 static void CATerminateLEAdapterMutex()
1580 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1582 ca_mutex_free(g_bleIsServerMutex);
1583 g_bleIsServerMutex = NULL;
1585 ca_mutex_free(g_bleNetworkCbMutex);
1586 g_bleNetworkCbMutex = NULL;
1588 ca_mutex_free(g_bleLocalAddressMutex);
1589 g_bleLocalAddressMutex = NULL;
1591 ca_mutex_free(g_bleAdapterThreadPoolMutex);
1592 g_bleAdapterThreadPoolMutex = NULL;
1594 ca_mutex_free(g_bleClientSendDataMutex);
1595 g_bleClientSendDataMutex = NULL;
1597 ca_mutex_free(g_bleServerSendDataMutex);
1598 g_bleServerSendDataMutex = NULL;
1600 ca_mutex_free(g_bleAdapterReqRespCbMutex);
1601 g_bleAdapterReqRespCbMutex = NULL;
1603 ca_mutex_free(g_bleReceiveDataMutex);
1604 g_bleReceiveDataMutex = NULL;
1606 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1609 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
1610 CANetworkPacketReceivedCallback reqRespCallback,
1611 CANetworkChangeCallback netCallback,
1612 CAErrorHandleCallback errorCallback,
1613 ca_thread_pool_t handle)
1615 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1618 VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null");
1619 VERIFY_NON_NULL(reqRespCallback, CALEADAPTER_TAG, "PacketReceived Callback is null");
1620 VERIFY_NON_NULL(netCallback, CALEADAPTER_TAG, "NetworkChange Callback is null");
1622 CAResult_t result = CA_STATUS_OK;
1623 result = CAInitLEAdapterMutex();
1624 if (CA_STATUS_OK != result)
1626 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
1627 return CA_STATUS_FAILED;
1629 result = CAInitializeLENetworkMonitor();
1630 if (CA_STATUS_OK != result)
1632 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
1633 return CA_STATUS_FAILED;
1636 CAInitializeLEAdapter();
1638 CASetLEClientThreadPoolHandle(handle);
1639 CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
1640 CASetLEServerThreadPoolHandle(handle);
1641 CASetLEAdapterThreadPoolHandle(handle);
1642 CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
1643 CASetLEReqRespAdapterCallback(reqRespCallback);
1645 CASetBLEClientErrorHandleCallback(CALEErrorHandler);
1646 CASetBLEServerErrorHandleCallback(CALEErrorHandler);
1647 CALERegisterNetworkNotifications(netCallback);
1649 g_errorHandler = errorCallback;
1651 static const CAConnectivityHandler_t connHandler =
1653 .startAdapter = CAStartLE,
1654 .stopAdapter = CAStopLE,
1655 .startListenServer = CAStartLEListeningServer,
1656 .startDiscoveryServer = CAStartLEDiscoveryServer,
1657 .sendData = CASendLEUnicastData,
1658 .sendDataToAll = CASendLEMulticastData,
1659 .GetnetInfo = CAGetLEInterfaceInformation,
1660 .readData = CAReadLEData,
1661 .terminate = CATerminateLE
1664 registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
1666 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1668 return CA_STATUS_OK;
1671 static CAResult_t CAStartLE()
1673 OIC_LOG(DEBUG, CALEADAPTER_TAG, __func__);
1675 return CAStartLEAdapter();
1678 static CAResult_t CAStopLE()
1680 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1681 #ifndef SINGLE_THREAD
1685 ca_mutex_lock(g_bleIsServerMutex);
1686 if (true == g_isServer)
1688 CAStopLEGattServer();
1692 CAStopLEGattClient();
1694 ca_mutex_unlock(g_bleIsServerMutex);
1696 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1698 return CA_STATUS_OK;
1701 static void CATerminateLE()
1703 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1705 CASetLEReqRespServerCallback(NULL);
1706 CASetLEReqRespClientCallback(NULL);
1707 CALERegisterNetworkNotifications(NULL);
1708 CASetLEReqRespAdapterCallback(NULL);
1709 CATerminateLENetworkMonitor();
1711 ca_mutex_lock(g_bleIsServerMutex);
1712 if (true == g_isServer)
1714 CATerminateLEGattServer();
1718 CATerminateLEGattClient();
1720 ca_mutex_unlock(g_bleIsServerMutex);
1722 #ifndef SINGLE_THREAD
1723 CATerminateLEQueues();
1725 CATerminateLEAdapterMutex();
1727 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1730 static CAResult_t CAStartLEListeningServer()
1732 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1733 CAResult_t result = CA_STATUS_OK;
1734 #ifndef SINGLE_THREAD
1735 result = CAInitLEServerQueues();
1736 if (CA_STATUS_OK != result)
1738 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
1739 return CA_STATUS_FAILED;
1743 result = CAGetLEAdapterState();
1744 if (CA_ADAPTER_NOT_ENABLED == result)
1746 gLeServerStatus = CA_LISTENING_SERVER;
1747 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1748 return CA_STATUS_OK;
1751 if (CA_STATUS_FAILED == result)
1753 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1754 return CA_STATUS_FAILED;
1757 CAStartLEGattServer();
1759 ca_mutex_lock(g_bleIsServerMutex);
1761 ca_mutex_unlock(g_bleIsServerMutex);
1763 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1764 return CA_STATUS_OK;
1767 static CAResult_t CAStartLEDiscoveryServer()
1769 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1770 CAResult_t result = CA_STATUS_OK;
1771 #ifndef SINGLE_THREAD
1772 result = CAInitLEClientQueues();
1773 if (CA_STATUS_OK != result)
1775 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
1776 return CA_STATUS_FAILED;
1779 result = CAGetLEAdapterState();
1780 if (CA_ADAPTER_NOT_ENABLED == result)
1782 gLeServerStatus = CA_DISCOVERY_SERVER;
1783 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
1784 return CA_STATUS_OK;
1787 if (CA_STATUS_FAILED == result)
1789 OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
1790 return CA_STATUS_FAILED;
1793 CAStartLEGattClient();
1795 ca_mutex_lock(g_bleIsServerMutex);
1797 ca_mutex_unlock(g_bleIsServerMutex);
1799 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1800 return CA_STATUS_OK;
1803 static CAResult_t CAReadLEData()
1805 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1806 #ifdef SINGLE_THREAD
1809 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1810 return CA_STATUS_OK;
1813 static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint,
1817 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1820 VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1);
1821 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1823 CAResult_t result = CA_STATUS_FAILED;
1825 ca_mutex_lock(g_bleIsServerMutex);
1826 if (true == g_isServer)
1828 result = CALEAdapterServerSendData(endpoint, data, dataLen);
1829 if (CA_STATUS_OK != result)
1831 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed\n");
1834 g_errorHandler(endpoint, data, dataLen, result);
1836 ca_mutex_unlock(g_bleIsServerMutex);
1842 result = CALEAdapterClientSendData(endpoint, data, dataLen);
1843 if (CA_STATUS_OK != result)
1845 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed \n");
1848 g_errorHandler(endpoint, data, dataLen, result);
1850 ca_mutex_unlock(g_bleIsServerMutex);
1854 ca_mutex_unlock(g_bleIsServerMutex);
1856 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1860 static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint,
1864 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1867 VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1);
1871 OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
1875 CAResult_t result = CA_STATUS_FAILED;
1877 ca_mutex_lock(g_bleIsServerMutex);
1878 if (true == g_isServer)
1880 result = CALEAdapterServerSendData(NULL, data, dataLen);
1881 if (CA_STATUS_OK != result)
1883 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data failed" );
1885 ca_mutex_unlock(g_bleIsServerMutex);
1888 g_errorHandler(endpoint, data, dataLen, result);
1895 result = CALEAdapterClientSendData(NULL, data, dataLen);
1896 if (CA_STATUS_OK != result)
1898 OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data failed" );
1901 g_errorHandler(endpoint, data, dataLen, result);
1903 ca_mutex_unlock(g_bleIsServerMutex);
1907 ca_mutex_unlock(g_bleIsServerMutex);
1909 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1913 static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
1915 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1917 VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null");
1919 char *local_address = NULL;
1921 CAResult_t res = CAGetLEAddress(&local_address);
1922 if (CA_STATUS_OK != res)
1924 OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
1928 if (NULL == local_address)
1930 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
1931 return CA_STATUS_FAILED;
1935 (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
1936 if (NULL == (*info))
1938 OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
1939 OICFree(local_address);
1940 return CA_STATUS_FAILED;
1943 size_t local_address_len = strlen(local_address);
1945 if(local_address_len >= sizeof(g_localBLEAddress) ||
1946 local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
1948 OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
1950 OICFree(local_address);
1951 return CA_STATUS_FAILED;
1954 OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
1955 ca_mutex_lock(g_bleLocalAddressMutex);
1956 OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
1957 ca_mutex_unlock(g_bleLocalAddressMutex);
1959 (*info)->adapter = CA_ADAPTER_GATT_BTLE;
1961 OICFree(local_address);
1963 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1964 return CA_STATUS_OK;
1967 static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
1969 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
1971 ca_mutex_lock(g_bleNetworkCbMutex);
1972 g_networkCallback = netCallback;
1973 ca_mutex_unlock(g_bleNetworkCbMutex);
1974 CAResult_t res = CA_STATUS_OK;
1977 res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
1978 if (CA_STATUS_OK != res)
1980 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1985 res = CAUnSetLEAdapterStateChangedCb();
1986 if (CA_STATUS_OK != res)
1988 OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
1992 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
1996 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
1998 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2000 VERIFY_NON_NULL_VOID(g_localBLEAddress, CALEADAPTER_TAG, "g_localBLEAddress is null");
2001 CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE };
2003 ca_mutex_lock(g_bleLocalAddressMutex);
2004 OICStrcpy(localEndpoint.addr,
2005 sizeof(localEndpoint.addr),
2007 ca_mutex_unlock(g_bleLocalAddressMutex);
2009 g_bleAdapterState = adapter_state;
2010 // Start a GattServer/Client if gLeServerStatus is SET
2011 if (CA_LISTENING_SERVER == gLeServerStatus)
2013 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
2014 CAStartLEGattServer();
2016 else if (CA_DISCOVERY_SERVER == gLeServerStatus)
2018 OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
2019 CAStartLEGattClient();
2021 gLeServerStatus = CA_SERVER_NOTSTARTED;
2023 ca_mutex_lock(g_bleNetworkCbMutex);
2024 if (NULL != g_networkCallback)
2026 g_networkCallback(&localEndpoint, adapter_state);
2030 OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
2032 ca_mutex_unlock(g_bleNetworkCbMutex);
2034 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2037 static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
2038 const uint8_t *data,
2041 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2043 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2044 #ifndef SINGLE_THREAD
2045 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
2046 "g_bleClientSendQueueHandle is NULL",
2048 VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
2049 "g_bleClientSendDataMutex is NULL",
2052 VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, "g_bleClientSendQueueHandle",
2055 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
2057 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
2060 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2061 return CA_MEMORY_ALLOC_FAILED;
2063 // Add message to send queue
2064 ca_mutex_lock(g_bleClientSendDataMutex);
2065 CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
2066 ca_mutex_unlock(g_bleClientSendDataMutex);
2068 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2069 return CA_STATUS_OK;
2072 static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
2073 const uint8_t *data,
2076 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2078 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
2080 #ifdef SINGLE_THREAD
2081 uint8_t header[CA_HEADER_LENGTH] = { 0 };
2084 CAGenerateHeader(header, CA_HEADER_LENGTH, dataLen);
2086 if (CA_STATUS_OK != result)
2088 OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
2089 return CA_STATUS_FAILED;
2092 if (!CAIsLEConnected())
2094 OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
2095 return CA_STATUS_FAILED;
2098 result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH);
2099 if (CA_STATUS_OK != result)
2101 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2102 return CA_STATUS_FAILED;
2105 const uint32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
2106 for (uint32_t iter = 0; iter < dataLimit; iter++)
2109 CAUpdateCharacteristicsToAllGattClients(
2110 data + (iter * CA_SUPPORTED_BLE_MTU_SIZE),
2111 CA_SUPPORTED_BLE_MTU_SIZE);
2113 if (CA_STATUS_OK != result)
2115 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2116 return CA_STATUS_FAILED;
2122 const uint32_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
2126 CAUpdateCharacteristicsToAllGattClients(
2127 data + (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE),
2129 if (CA_STATUS_OK != result)
2131 OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
2132 return CA_STATUS_FAILED;
2137 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
2138 "BleClientReceiverQueue is NULL",
2140 VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
2141 "BleClientSendDataMutex is NULL",
2144 VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
2147 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
2149 CALEData_t * const bleData =
2150 CACreateLEData(remoteEndpoint, data, dataLen);
2154 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2155 return CA_MEMORY_ALLOC_FAILED;
2158 // Add message to send queue
2159 ca_mutex_lock(g_bleServerSendDataMutex);
2160 CAQueueingThreadAddData(g_bleServerSendQueueHandle,
2162 sizeof(CALEData_t));
2163 ca_mutex_unlock(g_bleServerSendDataMutex);
2165 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2166 return CA_STATUS_OK;
2169 static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress,
2170 const uint8_t *data,
2171 uint32_t dataLength,
2172 uint32_t *sentLength)
2174 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2177 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2178 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2180 #ifdef SINGLE_THREAD
2181 if(g_networkPacketReceivedCallback)
2183 CAEndpoint_t endPoint =
2184 { .adapter = CA_ADAPTER_GATT_BTLE }; // will be filled by
2187 g_networkPacketReceivedCallback(&endPoint, data, dataLength);
2190 VERIFY_NON_NULL_RET(g_bleReceiverQueue,
2192 "g_bleReceiverQueue",
2195 //Add message to data queue
2196 CAEndpoint_t * const remoteEndpoint =
2197 CACreateEndpointObject(CA_DEFAULT_FLAGS,
2198 CA_ADAPTER_GATT_BTLE,
2202 if (NULL == remoteEndpoint)
2204 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2205 return CA_STATUS_FAILED;
2208 // Create bleData to add to queue
2211 "Data received from LE layer [%d]",
2214 CALEData_t * const bleData =
2215 CACreateLEData(remoteEndpoint, data, dataLength);
2219 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2220 CAFreeEndpoint(remoteEndpoint);
2221 return CA_MEMORY_ALLOC_FAILED;
2224 CAFreeEndpoint(remoteEndpoint);
2225 // Add message to receiver queue
2226 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2228 *sentLength = dataLength;
2230 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2231 return CA_STATUS_OK;
2234 static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress,
2235 const uint8_t *data,
2236 uint32_t dataLength,
2237 uint32_t *sentLength)
2239 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2242 VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
2243 VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
2244 #ifndef SINGLE_THREAD
2245 VERIFY_NON_NULL_RET(g_bleReceiverQueue, CALEADAPTER_TAG, "g_bleReceiverQueue",
2248 //Add message to data queue
2249 CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2250 CA_ADAPTER_GATT_BTLE,
2252 if (NULL == remoteEndpoint)
2254 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
2255 return CA_STATUS_FAILED;
2258 OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%u]", dataLength);
2260 // Create bleData to add to queue
2261 CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
2264 OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
2265 CAFreeEndpoint(remoteEndpoint);
2266 return CA_MEMORY_ALLOC_FAILED;
2269 CAFreeEndpoint(remoteEndpoint);
2270 // Add message to receiver queue
2271 CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t));
2273 *sentLength = dataLength;
2275 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2276 return CA_STATUS_OK;
2279 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
2281 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2283 ca_mutex_lock(g_bleAdapterThreadPoolMutex);
2284 g_bleAdapterThreadPool = handle;
2285 ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
2287 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2290 static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
2292 OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
2294 ca_mutex_lock(g_bleAdapterReqRespCbMutex);
2296 g_networkPacketReceivedCallback = callback;
2298 ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
2300 OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
2303 static void CALEErrorHandler(const char *remoteAddress,
2304 const uint8_t *data,
2308 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
2310 VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
2312 CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS,
2313 CA_ADAPTER_GATT_BTLE,
2317 // if required, will be used to build remote endpoint
2318 g_errorHandler(rep, data, dataLen, result);
2320 CAFreeEndpoint(rep);
2322 OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");