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 ******************************************************************/
23 * @brief This file provides the APIs to control Bluetooth transport
26 #include "cabtmanager.h"
27 #include "cabtclient.h"
28 #include "cabtserver.h"
29 #include "cabtendpoint.h"
30 #include "cabtdevicelist.h"
31 #include "cabtutils.h"
32 #include "caadapterutils.h"
33 #include "caqueueingthread.h"
34 #include "camsgparser.h"
37 * @struct CABTNetworkEvent
38 * @brief Structure to maintain the adapter information and its status.
42 CALocalConnectivity_t *info;
43 CANetworkStatus_t status;
48 * @brief Structure to maintain the information of data in message queue.
52 CARemoteEndpoint_t *remoteEndpoint;
57 * @var gNetworkPacketReceivedCallback
58 * @brief Maintains the callback to be notified on receival of network packets from other
61 static CANetworkPacketReceivedCallback gNetworkPacketReceivedCallback = NULL;
64 * @var gNetworkChangeCallback
65 * @brief Maintains the callback to be notified on local bluetooth adapter status change.
67 static CANetworkChangeCallback gNetworkChangeCallback = NULL;
70 * @var gBTDeviceListMutex
71 * @brief Mutex to synchronize the access to Bluetooth device information list.
73 static u_mutex gBTDeviceListMutex = NULL;
77 * @brief Peer Bluetooth device information list.
79 static BTDeviceList *gBTDeviceList = NULL;
82 * @var gLocalConnectivity
83 * @brief Information of local Bluetooth adapter.
85 static CALocalConnectivity_t *gLocalConnectivity = NULL;
89 * @brief Reference to threadpool.
91 static u_thread_pool_t gBTThreadPool = NULL;
94 * @var isHeaderAvailable
95 * @brief to differentiate btw header and data packet.
97 static CABool_t isHeaderAvailable = false;
100 * @var gSendQueueHandle
101 * @brief Queue handle for Send Data
103 static CAQueueingThread_t *gSendQueueHandle = NULL;
106 * @var gRecvQueueHandle
107 * @brief Queue handle for Receive Data
109 static CAQueueingThread_t *gRecvQueueHandle = NULL;
113 * @brief Storing Adapter state information
115 static CABool_t gAdapterState = TRUE;
119 * @brief Storing RfcommserverUUID
121 static int32_t gServerId = -1;
125 * @brief Storing Rfcommserver state information
127 static CABool_t gServerState = FALSE;
130 * @fn CABTAdapterStateChangeCallback
131 * @brief This callback is registered to receive bluetooth adapter state changes.
133 static void CABTAdapterStateChangeCallback(int result, bt_adapter_state_e adapterState,
137 * @fn CABTSocketConnectionStateCallback
138 * @brief This callback is registered to receive bluetooth RFCOMM connection state changes.
140 static void CABTSocketConnectionStateCallback(int result,
141 bt_socket_connection_state_e state,
142 bt_socket_connection_s *connection, void *userData);
145 * @fn CABTDataRecvCallback
146 * @brief This callback is registered to recieve data on any open RFCOMM connection.
148 static void CABTDataRecvCallback(bt_socket_received_data_s *data, void *userData);
151 * @fn CABTDeviceDiscoveryCallback
152 * @brief This callback is registered to recieve all bluetooth nearby devices when device
155 static void CABTDeviceDiscoveryCallback(int result,
156 bt_adapter_device_discovery_state_e state,
157 bt_adapter_device_discovery_info_s *discoveryInfo,
161 * @fn CABTServiceSearchedCallback
162 * @brief This callback is registered to recieve all the services remote bluetooth device supports
163 * when service search initiated.
165 static void CABTServiceSearchedCallback(int result, bt_device_sdp_info_s *sdpInfo,
170 * @fn CABTManagerInitializeQueues
171 * @brief This function creates and initialize message queues.
173 static CAResult_t CABTManagerInitializeQueues(void);
176 * @fn CABTManagerInitializeSendHandler
177 * @brief This function creates and initialize send message queue.
179 CAResult_t CABTManagerInitializeSendHandler(void);
182 * @fn CABTManagerInitializeQueues
183 * @brief This function creates and initialize receive message queue.
185 CAResult_t CABTManagerInitializeReceiveHandler(void);
187 * @fn CABTManagerTerminateQueues
188 * @brief This function terminates the send message queue.
190 static void CABTManagerTerminateQueues(void);
193 * @fn CABTManagerInitializeMutex
194 * @brief This function creates mutex.
196 static void CABTManagerInitializeMutex(void);
199 * @fn CABTManagerTerminateMutex
200 * @brief This function frees mutex.
202 static void CABTManagerTerminateMutex(void);
205 * @fn CABTManagerDataSendHandler
206 * @brief This function handles data from send message queue.
208 static void CABTManagerDataSendHandler(void *context);
211 * @fn CABTManagerDataReceiverHandler
212 * @brief This function handles data from recv message queue.
214 static void CABTManagerDataReceiverHandler(void *context);
217 * @fn CABTManagerSendUnicastData
218 * @brief This function sends data to specified remote bluetooth device.
220 static CAResult_t CABTManagerSendUnicastData(const char *remoteAddress, const char *serviceUUID,
221 void *data, uint32_t dataLength, uint32_t *sentLength);
224 * @fn CABTManagerSendMulticastData
225 * @brief This function sends data to all bluetooth devices running OIC service.
227 static CAResult_t CABTManagerSendMulticastData(const char *serviceUUID, void *data,
228 uint32_t dataLength, uint32_t *sentLength);
231 * @fn CABTGetAdapterEnableState
232 * @brief This function gets bluetooth adatper enable state.
234 static CAResult_t CABTGetAdapterEnableState(bool *state);
237 * @fn CABTStartDeviceDiscovery
238 * @brief This function starts device discovery.
240 static CAResult_t CABTStartDeviceDiscovery(void);
243 * @fn CABTStopDeviceDiscovery
244 * @brief This function stops device discovery.
246 static CAResult_t CABTStopDeviceDiscovery(void);
249 * @fn CABTStartServiceSearch
250 * @brief This function searches for OIC service for remote Bluetooth device.
252 static CAResult_t CABTStartServiceSearch(const char *remoteAddress);
255 * @fn CABTStopServiceSearch
256 * @brief This function stops any ongoing service sevice search.
258 static CAResult_t CABTStopServiceSearch(void);
261 * @fn CABTNotifyNetworkStauts
262 * @brief This function creates notification task for network adapter status and add it to thread pool.
264 static CAResult_t CABTNotifyNetworkStauts(CANetworkStatus_t status);
267 * @fn CABTOnNetworkStautsChanged
268 * @brief This is task callback function for notifying network adapter status to upper layer.
270 static void CABTOnNetworkStautsChanged(void *context);
273 * @fn CABTCreateNetworkEvent
274 * @brief Creates instance of CABTNetworkEvent.
276 static CABTNetworkEvent *CABTCreateNetworkEvent(CALocalConnectivity_t *connectivity,
277 CANetworkStatus_t status);
280 * @fn CABTFreeNetworkEvent
281 * @brief Free the memory associated with @event.
283 static void CABTFreeNetworkEvent(CABTNetworkEvent *event);
286 * @fn CABTManagerDisconnectAll
287 * @brief Closes all the client connection to peer bluetooth devices.
289 static void CABTManagerDisconnectAll(void);
293 * @brief creating CABTData
295 static CABTData *CACreateBTData(const CARemoteEndpoint_t *remoteEndpoint, void *data,
296 uint32_t dataLength);
299 * @fn CABTManagerDisconnectAll
300 * @brief freeing CABTData
302 static void CAFreeBTData(CABTData *btData);
305 CAResult_t CABTManagerInitialize(u_thread_pool_t threadPool)
307 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
309 int32_t err = BT_ERROR_NONE;
311 //Initialize Bluetooth service
312 if (BT_ERROR_NONE != (err = bt_initialize()))
314 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Bluetooth initialization failed!, error num [%x]",
316 return CA_STATUS_FAILED;
319 //Create and initialize the mutex
320 CABTManagerInitializeMutex();
322 if (NULL == gBTThreadPool)
324 gBTThreadPool = threadPool;
327 //Initialize Send/Receive data message queues
328 if (CA_STATUS_OK != CABTManagerInitializeQueues())
330 //Deinitialize the Bluetooth stack
332 return CA_STATUS_FAILED;
335 bt_adapter_set_state_changed_cb(CABTAdapterStateChangeCallback, NULL);
337 //Notity to upper layer
338 // CABTNotifyNetworkStauts(CA_INTERFACE_UP);
340 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
344 void CABTManagerTerminate(void)
346 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
348 gNetworkPacketReceivedCallback = NULL;
349 gNetworkChangeCallback = NULL;
354 //Unset bluetooth adapter callbacks
355 bt_adapter_unset_state_changed_cb();
357 //Terminate Bluetooth service
360 //Terminate thread pool
361 gBTThreadPool = NULL;
363 //Free LocalConnectivity information
364 CAAdapterFreeLocalEndpoint(gLocalConnectivity);
365 gLocalConnectivity = NULL;
367 //Free BTDevices list
368 if (gBTDeviceListMutex)
370 u_mutex_lock(gBTDeviceListMutex);
371 CADestroyBTDeviceList(&gBTDeviceList);
372 u_mutex_unlock(gBTDeviceListMutex);
376 CABTManagerTerminateMutex();
378 //Terminate Send/Receive data messages queues
379 CABTManagerTerminateQueues();
381 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
384 CAResult_t CABTManagerStart(void)
386 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
388 //Get Bluetooth adapter state
389 bool adapterState = false;
390 if (CA_STATUS_OK != CABTGetAdapterEnableState(&adapterState))
392 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to get adatper enable state");
393 return CA_STATUS_FAILED;
396 if (false == adapterState)
398 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Bluetooth adapter is disabled!");
399 gAdapterState = FALSE;
403 //Register for discovery and rfcomm socket connection callbacks
404 bt_adapter_set_device_discovery_state_changed_cb(CABTDeviceDiscoveryCallback, NULL);
405 bt_device_set_service_searched_cb(CABTServiceSearchedCallback, NULL);
406 bt_socket_set_connection_state_changed_cb(CABTSocketConnectionStateCallback, NULL);
407 bt_socket_set_data_received_cb(CABTDataRecvCallback, NULL);
409 //Start device discovery
410 CABTStartDeviceDiscovery();
412 // Start send queue thread
413 if (CA_STATUS_OK != CAQueueingThreadStart(gSendQueueHandle))
415 OIC_LOG(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to Start Send Data Thread");
417 return CA_STATUS_FAILED;
419 // Start receive queue thread
420 if (CA_STATUS_OK != CAQueueingThreadStart(gRecvQueueHandle))
422 OIC_LOG(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to Start Receive Data Thread");
424 return CA_STATUS_FAILED;
427 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
431 void CABTManagerStop(void)
433 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
435 //Stop service search
436 CABTStopServiceSearch();
438 //Stop the device discovery process
439 CABTStopDeviceDiscovery();
441 //Stop send queue thread
442 CAQueueingThreadStop(gSendQueueHandle);
444 //Stop receive queue thread
445 CAQueueingThreadStop(gRecvQueueHandle);
447 //reset bluetooth adapter callbacks
448 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "Resetting the callbacks");
449 bt_adapter_unset_device_discovery_state_changed_cb();
450 bt_device_unset_service_searched_cb();
451 bt_socket_unset_connection_state_changed_cb();
452 bt_socket_unset_data_received_cb();
454 //Disconnect all the client connections
455 CABTManagerDisconnectAll();
457 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
460 void CABTManagerSetPacketReceivedCallback(
461 CANetworkPacketReceivedCallback packetReceivedCallback)
463 gNetworkPacketReceivedCallback = packetReceivedCallback;
466 void CABTManagerSetNetworkChangeCallback(
467 CANetworkChangeCallback networkChangeCallback)
469 gNetworkChangeCallback = networkChangeCallback;
472 CAResult_t CABTManagerPushDataToReceiverQueue(const char *remoteAddress,
473 const char *serviceUUID, void *data, uint32_t dataLength, uint32_t *sentLength)
475 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
477 if (FALSE == gAdapterState)
479 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Bluetooth adapter is disabled!");
484 VERIFY_NON_NULL(serviceUUID, BLUETOOTH_ADAPTER_TAG, "service UUID is null");
485 VERIFY_NON_NULL(data, BLUETOOTH_ADAPTER_TAG, "Data is null");
486 VERIFY_NON_NULL(sentLength, BLUETOOTH_ADAPTER_TAG, "Sent data length holder is null");
488 //Add message to data queue
489 CARemoteEndpoint_t *remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_EDR, remoteAddress,
491 if (NULL == remoteEndpoint)
493 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to create remote endpoint !");
494 return CA_STATUS_FAILED;
497 CABTData *btData = CACreateBTData(remoteEndpoint, data, dataLength);
498 //Add message to data queue
500 CAQueueingThreadAddData(gRecvQueueHandle, btData, sizeof(btData));
502 *sentLength = dataLength;
504 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
507 CAResult_t CABTManagerSendData(const char *remoteAddress, const char *serviceUUID,
508 void *data, uint32_t dataLength, uint32_t *sentLength)
510 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
512 if (FALSE == gAdapterState)
514 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Bluetooth adapter is disabled!");
519 VERIFY_NON_NULL(serviceUUID, BLUETOOTH_ADAPTER_TAG, "service UUID is null");
520 VERIFY_NON_NULL(data, BLUETOOTH_ADAPTER_TAG, "Data is null");
521 VERIFY_NON_NULL(sentLength, BLUETOOTH_ADAPTER_TAG, "Sent data length holder is null");
523 CARemoteEndpoint_t *remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_EDR, remoteAddress,
525 if (NULL == remoteEndpoint)
527 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to create remote endpoint !");
528 return CA_STATUS_FAILED;
531 CABTData *btData = CACreateBTData(remoteEndpoint, data, dataLength);
532 //Add message to data queue
534 CAQueueingThreadAddData(gSendQueueHandle, btData, sizeof(btData));
536 *sentLength = dataLength;
538 //Signal message handler for processing data for sending
539 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
543 CAResult_t CABTManagerStartServer(void)
545 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
547 if (FALSE == gAdapterState)
549 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "Bluetooth adapter is disabled!");
550 //Setting gServerState for starting Rfcommserver when adapter starts
557 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "Server is already in running state.");
560 return CABTServerStart(OIC_BT_SERVICE_ID, &gServerId);
563 CAResult_t CABTManagerStopServer(void)
565 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
569 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "No Server is in running state.");
573 if (CA_STATUS_OK != CABTServerStop(gServerId))
575 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to close server socket!");
576 return CA_STATUS_FAILED;
580 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
584 CAResult_t CABTManagerGetInterface(CALocalConnectivity_t **info)
586 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
588 int32_t err = BT_ERROR_NONE;
589 char *localAddress = NULL;
592 VERIFY_NON_NULL(info, BLUETOOTH_ADAPTER_TAG, "LocalConnectivity info is null");
594 //Get the bluetooth adapter local address
595 if (BT_ERROR_NONE != (err = bt_adapter_get_address(&localAddress)))
597 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG,
598 "Getting local adapter address failed!, error num [%x]",
600 return CA_STATUS_FAILED;
603 //Create network info
604 *info = CAAdapterCreateLocalEndpoint(CA_EDR, localAddress);
607 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to create LocalConnectivity instance!");
609 OICFree(localAddress);
610 return CA_MEMORY_ALLOC_FAILED;
613 OICFree(localAddress);
615 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
619 CAResult_t CABTManagerReadData(void)
621 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
622 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
623 return CA_NOT_SUPPORTED;
626 CAResult_t CABTManagerInitializeQueues(void)
628 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
630 if (CA_STATUS_OK == CABTManagerInitializeSendHandler()
631 && CA_STATUS_OK == CABTManagerInitializeReceiveHandler())
633 OIC_LOG(DEBUG, BLUETOOTH_ADAPTER_TAG, "Queue is initialized!");
637 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
638 return CA_STATUS_FAILED;
641 CAResult_t CABTManagerInitializeSendHandler(void)
643 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
644 // Check if the message queue is already initialized
645 if (gSendQueueHandle)
647 OIC_LOG(DEBUG, BLUETOOTH_ADAPTER_TAG, "Already queue is initialized!");
651 gSendQueueHandle = OICMalloc(sizeof(CAQueueingThread_t));
652 if (!gSendQueueHandle)
654 OIC_LOG(ERROR, BLUETOOTH_ADAPTER_TAG, "Memory allocation failed!");
655 return CA_MEMORY_ALLOC_FAILED;
658 if (CA_STATUS_OK != CAQueueingThreadInitialize(gSendQueueHandle, gBTThreadPool,
659 CABTManagerDataSendHandler))
661 OIC_LOG(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to Initialize send queue thread");
662 return CA_STATUS_FAILED;
667 CAResult_t CABTManagerInitializeReceiveHandler(void)
669 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
670 // Check if the message queue is already initialized
671 if (gRecvQueueHandle)
673 OIC_LOG(DEBUG, BLUETOOTH_ADAPTER_TAG, "Already queue is initialized!");
677 gRecvQueueHandle = OICMalloc(sizeof(CAQueueingThread_t));
678 if (!gRecvQueueHandle)
680 OIC_LOG(ERROR, BLUETOOTH_ADAPTER_TAG, "Memory allocation failed!");
681 return CA_MEMORY_ALLOC_FAILED;
684 if (CA_STATUS_OK != CAQueueingThreadInitialize(gRecvQueueHandle, gBTThreadPool,
685 CABTManagerDataReceiverHandler))
687 OIC_LOG(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to Initialize send queue thread");
688 return CA_STATUS_FAILED;
691 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
695 void CABTManagerTerminateQueues(void)
697 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
699 if (gSendQueueHandle)
701 CAQueueingThreadDestroy(gSendQueueHandle);
702 gSendQueueHandle = NULL;
704 if (gRecvQueueHandle)
706 CAQueueingThreadDestroy(gRecvQueueHandle);
707 gRecvQueueHandle = NULL;
710 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
713 void CABTManagerInitializeMutex(void)
715 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
719 if (!gBTDeviceListMutex)
721 gBTDeviceListMutex = u_mutex_new();
724 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
727 void CABTManagerTerminateMutex(void)
729 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
731 if (gBTDeviceListMutex)
733 u_mutex_free(gBTDeviceListMutex);
734 gBTDeviceListMutex = NULL;
737 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
741 void CABTManagerDataReceiverHandler(void *context)
743 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
746 CABTData *message = (CABTData *) context;
749 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to get message!");
752 const char *remoteAddress = NULL;
753 const char *serviceUUID = NULL;
754 static uint32_t recvDataLen = 0;
755 static uint32_t totalDataLen = 0;
756 static char *defragData = NULL;
757 static CARemoteEndpoint_t *remoteEndpoint = NULL;
759 if (!isHeaderAvailable)
761 char *header = (char *) OICMalloc(sizeof(char) * CA_HEADER_LENGTH);
765 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Out of memory (header)!");
768 memcpy(header, (char *)message->data, CA_HEADER_LENGTH);
769 totalDataLen = CAParseHeader(header);
770 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "Total data to be accumulated [%d] bytes",
773 if (totalDataLen == 0)
775 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, " totalDataLen is zero");
779 defragData = (char *) OICMalloc(sizeof(char) * totalDataLen);
782 remoteAddress = message->remoteEndpoint->addressInfo.BT.btMacAddress;
783 serviceUUID = message->remoteEndpoint->resourceUri;
785 remoteEndpoint = CAAdapterCreateRemoteEndpoint(CA_EDR, remoteAddress,
788 memcpy(defragData + recvDataLen, message->data + CA_HEADER_LENGTH,
789 message->dataLen - CA_HEADER_LENGTH);
790 recvDataLen += message->dataLen - CA_HEADER_LENGTH;
791 isHeaderAvailable = true;
795 memcpy(defragData + recvDataLen, message->data, message->dataLen);
796 recvDataLen += message->dataLen ;
798 CAFreeBTData(message);
799 if (totalDataLen == recvDataLen)
801 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "Sending data up !");
802 gNetworkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
805 remoteEndpoint = NULL;
806 isHeaderAvailable = false;
809 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
813 void CABTManagerDataSendHandler(void *context)
815 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
817 CABTData *message = (CABTData *) context;
820 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to get message!");
823 const char *remoteAddress = NULL;
824 const char *serviceUUID = NULL;
825 uint32_t sentLength = 0;
826 int32_t headerAdded = 0;
828 remoteAddress = message->remoteEndpoint->addressInfo.BT.btMacAddress;
829 serviceUUID = message->remoteEndpoint->resourceUri;
831 char *dataSegment = NULL;
832 uint32_t offset = 0, ret = 1;
833 int32_t datalen = message->dataLen;
834 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "checking for fragmentation and the dataLen is %d",
838 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "fragmenting the data DataLen [%d] Offset [%d]",
840 ret = CAFragmentData((char *)message->data, &dataSegment, datalen, offset);
845 if (NULL == dataSegment)
847 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to allocated memory fo dataSegment.");
850 if (strlen(remoteAddress)) //Unicast data
852 if (CA_STATUS_OK != CABTManagerSendUnicastData(remoteAddress, serviceUUID,
853 dataSegment, ret, &sentLength))
855 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to send unicast data !");
860 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "sending multicast data");
861 if (CA_STATUS_OK != CABTManagerSendMulticastData(serviceUUID, dataSegment,
864 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to send multicast data !");
867 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "freeing dataSegment");
869 OICFree(dataSegment);
872 if (headerAdded == 0)
874 datalen -= offset - CA_HEADER_LENGTH;
875 offset = offset - CA_HEADER_LENGTH;
887 if (datalen == message->dataLen)
889 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "All data has been fragmented and sent");
894 CAFreeBTData(message);
896 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
899 CAResult_t CABTManagerSendUnicastData(const char *remoteAddress, const char *serviceUUID,
900 void *data, uint32_t dataLength, uint32_t *sentLength)
902 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
904 BTDevice *device = NULL;
907 VERIFY_NON_NULL(remoteAddress, BLUETOOTH_ADAPTER_TAG, "Remote address is null");
908 VERIFY_NON_NULL(serviceUUID, BLUETOOTH_ADAPTER_TAG, "service UUID is null");
909 VERIFY_NON_NULL(data, BLUETOOTH_ADAPTER_TAG, "Data is null");
910 VERIFY_NON_NULL(sentLength, BLUETOOTH_ADAPTER_TAG, "Sent data length holder is null");
914 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Invalid input: Negative data length!");
915 return CA_STATUS_INVALID_PARAM;
918 //Check the connection existence with remote device
919 u_mutex_lock(gBTDeviceListMutex);
920 if (CA_STATUS_OK != CAGetBTDevice(gBTDeviceList, remoteAddress, &device))
922 //Create new device and add to list
923 if (CA_STATUS_OK != CACreateAndAddToDeviceList(&gBTDeviceList, remoteAddress,
924 OIC_BT_SERVICE_ID, &device))
926 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed create device and add to list!");
928 u_mutex_unlock(gBTDeviceListMutex);
929 return CA_STATUS_FAILED;
932 //Start the OIC service search newly created device
933 if (CA_STATUS_OK != CABTStartServiceSearch(remoteAddress))
935 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to initiate service search!");
937 //Remove device from list
938 CARemoveBTDeviceFromList(&gBTDeviceList, remoteAddress);
940 u_mutex_unlock(gBTDeviceListMutex);
941 return CA_STATUS_FAILED;
944 u_mutex_unlock(gBTDeviceListMutex);
946 if (-1 == device->socketFD)
948 //Adding to pending list
949 if (CA_STATUS_OK != CAAddBTDataToList(&device->pendingDataList, data,
952 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to add data to pending list!");
954 //Remove device from list
955 CARemoveBTDeviceFromList(&gBTDeviceList, remoteAddress);
956 return CA_STATUS_FAILED;
959 //Make a rfcomm connection with remote BT Device
960 if (1 == device->serviceSearched &&
961 CA_STATUS_OK != CABTClientConnect(remoteAddress, serviceUUID))
963 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to make RFCOMM connection!");
965 //Remove device from list
966 CARemoveBTDeviceFromList(&gBTDeviceList, remoteAddress);
967 return CA_STATUS_FAILED;
969 *sentLength = dataLength;
973 if (CA_STATUS_OK != CABTSendData(device->socketFD, data, dataLength, sentLength))
975 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to send data!");
976 return CA_STATUS_FAILED;
980 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
984 CAResult_t CABTManagerSendMulticastData(const char *serviceUUID, void *data,
985 uint32_t dataLength, uint32_t *sentLength)
987 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
990 VERIFY_NON_NULL(serviceUUID, BLUETOOTH_ADAPTER_TAG, "service UUID is null");
991 VERIFY_NON_NULL(data, BLUETOOTH_ADAPTER_TAG, "Data is null");
992 VERIFY_NON_NULL(sentLength, BLUETOOTH_ADAPTER_TAG, "Sent data length holder is null");
996 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Invalid input: Negative data length!");
997 return CA_STATUS_INVALID_PARAM;
1000 *sentLength = dataLength;
1002 //Send the packet to all OIC devices
1003 u_mutex_lock(gBTDeviceListMutex);
1004 BTDeviceList *curList = gBTDeviceList;
1005 while (curList != NULL)
1007 BTDevice *device = curList->device;
1008 curList = curList->next;
1012 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "There is no device!");
1016 if (-1 == device->socketFD)
1018 //Check if the device service search is finished
1019 if (0 == device->serviceSearched)
1021 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Device services are still unknown!");
1025 //Adding to pendding list
1026 if (CA_STATUS_OK != CAAddBTDataToList(&device->pendingDataList, data,
1029 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to add data to pending list !");
1033 //Make a rfcomm connection with remote BT Device
1034 if (CA_STATUS_OK != CABTClientConnect(device->remoteAddress, device->serviceUUID))
1036 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to make RFCOMM connection !");
1038 //Remove the data which added to pending list
1039 CARemoveBTDataFromList(&device->pendingDataList);
1045 if (CA_STATUS_OK != CABTSendData(device->socketFD, data, dataLength, sentLength))
1047 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to send data to [%s] !",
1048 device->remoteAddress);
1052 u_mutex_unlock(gBTDeviceListMutex);
1054 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
1055 return CA_STATUS_OK;
1058 CAResult_t CABTGetAdapterEnableState(bool *state)
1060 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
1063 VERIFY_NON_NULL(state, BLUETOOTH_ADAPTER_TAG, "state holder is NULL!");
1065 bt_error_e err = BT_ERROR_NONE;
1066 bt_adapter_state_e adapterState;
1068 //Get Bluetooth adapter state
1069 if (BT_ERROR_NONE != (err = bt_adapter_get_state(&adapterState)))
1071 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Bluetooth get state failed!, error num [%x]",
1074 return CA_STATUS_FAILED;
1078 if (BT_ADAPTER_ENABLED == adapterState)
1083 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
1084 return CA_STATUS_OK;
1087 CAResult_t CABTStartDeviceDiscovery(void)
1089 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
1091 bt_error_e err = BT_ERROR_NONE;
1092 bool isDiscoveryStarted = false;
1094 //Check the device discovery state
1095 if (BT_ERROR_NONE != (err = bt_adapter_is_discovering(&isDiscoveryStarted)))
1097 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
1099 return CA_STATUS_FAILED;
1102 //Start device discovery if its not started
1103 if (false == isDiscoveryStarted)
1105 if (BT_ERROR_NONE != (err = bt_adapter_start_device_discovery()))
1107 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Device discovery failed!, error num [%x]",
1109 return CA_STATUS_FAILED;
1113 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
1114 return CA_STATUS_OK;
1117 CAResult_t CABTStopDeviceDiscovery(void)
1119 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
1121 bt_error_e err = BT_ERROR_NONE;
1122 bool isDiscoveryStarted = false;
1124 //Check the device discovery state
1125 if (BT_ERROR_NONE != (err = bt_adapter_is_discovering(&isDiscoveryStarted)))
1127 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
1129 return CA_STATUS_FAILED;
1132 //stop the device discovery process
1133 if (true == isDiscoveryStarted)
1135 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "Stopping the device search process");
1136 if (BT_ERROR_NONE != (err = bt_adapter_stop_device_discovery()))
1138 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to stop discovery!, error num [%x]",
1143 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
1144 return CA_STATUS_OK;
1147 CAResult_t CABTStartServiceSearch(const char *remoteAddress)
1149 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
1151 bt_error_e err = BT_ERROR_NONE;
1154 VERIFY_NON_NULL(remoteAddress, BLUETOOTH_ADAPTER_TAG, "Remote address is null");
1155 if (0 == strlen(remoteAddress))
1157 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Remote address is empty!");
1158 return CA_STATUS_INVALID_PARAM;
1161 //Start searching for OIC service
1162 if (BT_ERROR_NONE != (err = bt_device_start_service_search(remoteAddress)))
1164 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
1166 return CA_STATUS_FAILED;
1169 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
1170 return CA_STATUS_OK;
1173 CAResult_t CABTStopServiceSearch(void)
1175 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
1177 bt_error_e err = BT_ERROR_NONE;
1179 //Stop ongoing service search
1180 if (BT_ERROR_NONE != (err = bt_device_cancel_service_search()))
1182 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
1184 return CA_STATUS_FAILED;
1187 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
1188 return CA_STATUS_OK;
1191 void CABTAdapterStateChangeCallback(int result, bt_adapter_state_e adapterState,
1194 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
1196 if (BT_ADAPTER_ENABLED == adapterState)
1198 // checking for adapter state. If not started then start.
1199 if (FALSE == gAdapterState)
1202 gAdapterState = TRUE;
1203 //starting RFcommserver
1204 if (gServerState == TRUE)
1206 CABTManagerStartServer();
1207 gServerState = FALSE;
1210 //Notity to upper layer
1211 CABTNotifyNetworkStauts(CA_INTERFACE_UP);
1213 else if (BT_ADAPTER_DISABLED == adapterState)
1215 //Notity to upper layer
1216 CABTNotifyNetworkStauts(CA_INTERFACE_DOWN);
1217 gAdapterState = FALSE;
1220 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
1223 void CABTSocketConnectionStateCallback(int result, bt_socket_connection_state_e state,
1224 bt_socket_connection_s *connection, void *userData)
1226 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
1228 BTDevice *device = NULL;
1230 if (BT_ERROR_NONE != result || NULL == connection)
1232 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Invalid connection state!, error num [%x]",
1239 case BT_SOCKET_CONNECTED:
1241 u_mutex_lock(gBTDeviceListMutex);
1242 if (CA_STATUS_OK != CAGetBTDevice(gBTDeviceList, connection->remote_address,
1245 //Create the deviceinfo and add to list
1246 if (CA_STATUS_OK != CACreateAndAddToDeviceList(&gBTDeviceList,
1247 connection->remote_address, OIC_BT_SERVICE_ID, &device))
1249 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed add device to list!");
1250 u_mutex_unlock(gBTDeviceListMutex);
1254 device->socketFD = connection->socket_fd;
1255 u_mutex_unlock(gBTDeviceListMutex);
1259 device->socketFD = connection->socket_fd;
1260 while (device->pendingDataList)
1262 uint32_t sentData = 0;
1263 BTData *btData = device->pendingDataList->data;
1264 if (CA_STATUS_OK != CABTSendData(device->socketFD, btData->data,
1265 btData->dataLength, &sentData))
1267 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to send pending data [%s]",
1268 device->remoteAddress);
1270 //Remove all the data from pending list
1271 CADestroyBTDataList(&device->pendingDataList);
1275 //Remove the data which send from pending list
1276 CARemoveBTDataFromList(&device->pendingDataList);
1278 u_mutex_unlock(gBTDeviceListMutex);
1282 case BT_SOCKET_DISCONNECTED:
1284 u_mutex_lock(gBTDeviceListMutex);
1285 CARemoveBTDeviceFromList(&gBTDeviceList, connection->remote_address);
1286 u_mutex_unlock(gBTDeviceListMutex);
1291 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
1294 void CABTDataRecvCallback(bt_socket_received_data_s *data, void *userData)
1296 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
1298 BTDevice *device = NULL;
1300 if (NULL == data || 0 >= data->data_size)
1302 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Data is null!");
1306 if (NULL == gNetworkPacketReceivedCallback)
1308 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Callback is not registered!");
1312 //Get BT device from list
1313 u_mutex_lock(gBTDeviceListMutex);
1314 if (CA_STATUS_OK != CAGetBTDeviceBySocketId(gBTDeviceList, data->socket_fd, &device))
1316 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Could not find the device!");
1318 u_mutex_unlock(gBTDeviceListMutex);
1321 u_mutex_unlock(gBTDeviceListMutex);
1323 uint32_t sentLength = 0;
1325 CAResult_t res = CABTManagerPushDataToReceiverQueue(device->remoteAddress, OIC_BT_SERVICE_ID,
1326 data->data, (uint32_t)data->data_size, &sentLength);
1328 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "CABTManagerPushDataToReceiverQueue done [%d]", res);
1330 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
1333 void CABTDeviceDiscoveryCallback(int result, bt_adapter_device_discovery_state_e state,
1334 bt_adapter_device_discovery_info_s *discoveryInfo, void *userData)
1336 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
1338 BTDevice *device = NULL;
1340 if (BT_ERROR_NONE != result)
1342 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Received bad state!, error num [%x]",
1349 case BT_ADAPTER_DEVICE_DISCOVERY_STARTED:
1351 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "Discovery started!");
1355 case BT_ADAPTER_DEVICE_DISCOVERY_FINISHED:
1357 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "Discovery finished!");
1361 case BT_ADAPTER_DEVICE_DISCOVERY_FOUND:
1363 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "Device discovered [%s]!",
1364 discoveryInfo->remote_name);
1365 if (true == CABTIsServiceSupported((const char **)discoveryInfo->service_uuid,
1366 discoveryInfo->service_count,
1369 //Check if the deivce is already in the list
1370 u_mutex_lock(gBTDeviceListMutex);
1371 if (CA_STATUS_OK == CAGetBTDevice(gBTDeviceList, discoveryInfo->remote_address,
1374 device->serviceSearched = 1;
1375 u_mutex_unlock(gBTDeviceListMutex);
1379 //Create the deviceinfo and add to list
1380 if (CA_STATUS_OK != CACreateAndAddToDeviceList(&gBTDeviceList,
1381 discoveryInfo->remote_address, OIC_BT_SERVICE_ID, &device))
1383 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to add device to list!");
1384 u_mutex_unlock(gBTDeviceListMutex);
1388 device->serviceSearched = 1;
1389 u_mutex_unlock(gBTDeviceListMutex);
1393 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Device does not support OIC service!");
1399 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
1402 void CABTServiceSearchedCallback(int32_t result, bt_device_sdp_info_s *sdpInfo, void *userData)
1404 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
1406 if (NULL == sdpInfo)
1408 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "SDP info is null!");
1412 u_mutex_lock(gBTDeviceListMutex);
1414 BTDevice *device = NULL;
1415 if (CA_STATUS_OK == CAGetBTDevice(gBTDeviceList, sdpInfo->remote_address, &device)
1418 if (1 == device->serviceSearched)
1420 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "Service is already searched for this device!");
1421 u_mutex_unlock(gBTDeviceListMutex);
1425 if (true == CABTIsServiceSupported((const char **)sdpInfo->service_uuid,
1426 sdpInfo->service_count, OIC_BT_SERVICE_ID))
1428 device->serviceSearched = 1;
1429 if (CA_STATUS_OK != CABTClientConnect(sdpInfo->remote_address, OIC_BT_SERVICE_ID))
1431 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to make rfcomm connection!");
1433 //Remove the device from device list
1434 CARemoveBTDeviceFromList(&gBTDeviceList, sdpInfo->remote_address);
1439 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "Device does not contain OIC service!");
1441 //Remove device from list as it does not support OIC service
1442 CARemoveBTDeviceFromList(&gBTDeviceList, sdpInfo->remote_address);
1446 u_mutex_unlock(gBTDeviceListMutex);
1448 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
1451 CAResult_t CABTNotifyNetworkStauts(CANetworkStatus_t status)
1453 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
1455 //Create localconnectivity
1456 if (NULL == gLocalConnectivity)
1458 CABTManagerGetInterface(&gLocalConnectivity);
1461 //Notity to upper layer
1462 if (gNetworkChangeCallback && gLocalConnectivity && gBTThreadPool)
1464 //Add notification task to thread pool
1465 CABTNetworkEvent *event = CABTCreateNetworkEvent(gLocalConnectivity, status);
1468 if (CA_STATUS_OK != u_thread_pool_add_task(gBTThreadPool, CABTOnNetworkStautsChanged,
1471 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to create threadpool!");
1472 return CA_STATUS_FAILED;
1477 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
1478 return CA_STATUS_OK;
1481 void CABTOnNetworkStautsChanged(void *context)
1483 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
1485 if (NULL == context)
1487 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "context is NULL!");
1491 CABTNetworkEvent *networkEvent = (CABTNetworkEvent *) context;
1493 //Notity to upper layer
1494 if (gNetworkChangeCallback)
1496 gNetworkChangeCallback(networkEvent->info, networkEvent->status);
1499 //Free the created Network event
1500 CABTFreeNetworkEvent(networkEvent);
1502 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
1505 CABTNetworkEvent *CABTCreateNetworkEvent(CALocalConnectivity_t *connectivity,
1506 CANetworkStatus_t status)
1508 VERIFY_NON_NULL_RET(connectivity, BLUETOOTH_ADAPTER_TAG, "connectivity is NULL", NULL);
1510 //Create CABTNetworkEvent
1511 CABTNetworkEvent *event = (CABTNetworkEvent *) OICMalloc(sizeof(CABTNetworkEvent));
1514 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to allocate memory to network event!");
1518 //Create duplicate of Local connectivity
1519 event->info = CAAdapterCopyLocalEndpoint(connectivity);
1520 event->status = status;
1524 void CABTFreeNetworkEvent(CABTNetworkEvent *event)
1528 CAAdapterFreeLocalEndpoint(event->info);
1533 void CABTManagerDisconnectAll(void)
1535 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "IN");
1537 u_mutex_lock(gBTDeviceListMutex);
1539 BTDeviceList *cur = gBTDeviceList;
1542 BTDevice *device = cur->device;
1545 if (device && 0 <= device->socketFD)
1547 if (CA_STATUS_OK != CABTClientDisconnect(device->socketFD))
1549 OIC_LOG_V(ERROR, BLUETOOTH_ADAPTER_TAG, "Failed to disconnect with client :%s",
1550 device->remoteAddress);
1553 device->socketFD = -1;
1557 u_mutex_unlock(gBTDeviceListMutex);
1559 OIC_LOG_V(DEBUG, BLUETOOTH_ADAPTER_TAG, "OUT");
1561 CABTData *CACreateBTData(const CARemoteEndpoint_t *remoteEndpoint, void *data,
1562 uint32_t dataLength)
1564 CABTData *btData = (CABTData *) OICMalloc(sizeof(CABTData));
1567 OIC_LOG(ERROR, BLUETOOTH_ADAPTER_TAG, "Memory allocation failed!");
1571 btData->remoteEndpoint = CAAdapterCopyRemoteEndpoint(remoteEndpoint);
1572 btData->data = (void *)OICMalloc(dataLength);
1573 if (NULL == btData->data)
1575 OIC_LOG(ERROR, BLUETOOTH_ADAPTER_TAG, "Memory allocation failed!");
1576 CAFreeBTData(btData);
1579 memcpy(btData->data, data, dataLength);
1580 btData->dataLen = dataLength;
1585 void CAFreeBTData(CABTData *btData)
1590 CAAdapterFreeRemoteEndpoint(btData->remoteEndpoint);
1591 OICFree(btData->data);