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 establish RFCOMM connection with remote bluetooth device
26 #include <bluetooth.h>
28 #include "caedrinterface.h"
30 #include "caedrendpoint.h"
31 #include "caadapterutils.h"
32 #include "caedrutils.h"
35 #include "caedrdevicelist.h"
38 * @var gEDRDeviceListMutex
39 * @brief Mutex to synchronize the access to Bluetooth device information list.
41 static u_mutex gEDRDeviceListMutex = NULL;
45 * @brief Peer Bluetooth device information list.
47 static EDRDeviceList *gEDRDeviceList = NULL;
50 * @var gEDRNetworkChangeCallback
51 * @brief Maintains the callback to be notified when data received from remote Bluetooth device
53 static CAEDRDataReceivedCallback gEDRPacketReceivedCallback = NULL;
56 * @fn CAEDRManagerInitializeMutex
57 * @brief This function creates mutex.
59 static void CAEDRManagerInitializeMutex(void);
62 * @fn CAEDRManagerTerminateMutex
63 * @brief This function frees mutex.
65 static void CAEDRManagerTerminateMutex(void);
68 * @fn CAEDRDataRecvCallback
69 * @brief This callback is registered to recieve data on any open RFCOMM connection.
71 static void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData);
74 * @brief This function starts device discovery.
77 static CAResult_t CAEDRStartDeviceDiscovery(void);
80 * @fn CAEDRStopServiceSearch
81 * @brief This function stops any ongoing service sevice search.
83 static CAResult_t CAEDRStopServiceSearch(void);
86 * @fn CAEDRStopDeviceDiscovery
87 * @brief This function stops device discovery.
89 static CAResult_t CAEDRStopDeviceDiscovery(void);
92 * @fn CAEDRStartServiceSearch
93 * @brief This function searches for OIC service for remote Bluetooth device.
95 static CAResult_t CAEDRStartServiceSearch(const char *remoteAddress);
98 * @fn CAEDRDeviceDiscoveryCallback
99 * @brief This callback is registered to recieve all bluetooth nearby devices when device
102 static void CAEDRDeviceDiscoveryCallback(int result,
103 bt_adapter_device_discovery_state_e state,
104 bt_adapter_device_discovery_info_s *discoveryInfo,
108 * @fn CAEDRServiceSearchedCallback
109 * @brief This callback is registered to recieve all the services remote bluetooth device supports
110 * when service search initiated.
112 static void CAEDRServiceSearchedCallback(int result, bt_device_sdp_info_s *sdpInfo,
116 * @fn CAEDRSocketConnectionStateCallback
117 * @brief This callback is registered to receive bluetooth RFCOMM connection state changes.
119 static void CAEDRSocketConnectionStateCallback(int result, bt_socket_connection_state_e state,
120 bt_socket_connection_s *connection, void *userData);
123 * @fn CAEDRClientConnect
124 * @brief Establishes RFCOMM connection with remote bluetooth device
126 static CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID);
129 * @fn CAEDRClientDisconnect
130 * @brief Disconnect RFCOMM client socket connection
132 static CAResult_t CAEDRClientDisconnect(const int32_t clientID);
134 void CAEDRSetPacketReceivedCallback(CAEDRDataReceivedCallback packetReceivedCallback)
136 gEDRPacketReceivedCallback = packetReceivedCallback;
139 void CAEDRSocketConnectionStateCallback(int result, bt_socket_connection_state_e state,
140 bt_socket_connection_s *connection, void *userData)
142 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN");
144 EDRDevice *device = NULL;
146 if (BT_ERROR_NONE != result || NULL == connection)
148 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Invalid connection state!, error num [%x]",
155 case BT_SOCKET_CONNECTED:
157 u_mutex_lock(gEDRDeviceListMutex);
158 if (CA_STATUS_OK != CAGetEDRDevice(gEDRDeviceList, connection->remote_address,
161 // Create the deviceinfo and add to list
162 if (CA_STATUS_OK != CACreateAndAddToDeviceList(&gEDRDeviceList,
163 connection->remote_address, OIC_EDR_SERVICE_ID, &device))
165 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed add device to list!");
166 u_mutex_unlock(gEDRDeviceListMutex);
170 device->socketFD = connection->socket_fd;
171 u_mutex_unlock(gEDRDeviceListMutex);
175 device->socketFD = connection->socket_fd;
176 while (device->pendingDataList)
178 uint32_t sentData = 0;
179 EDRData *edrData = device->pendingDataList->data;
180 if (CA_STATUS_OK != CAEDRSendData(device->socketFD, edrData->data,
181 edrData->dataLength, &sentData))
183 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send pending data [%s]",
184 device->remoteAddress);
186 // Remove all the data from pending list
187 CADestroyEDRDataList(&device->pendingDataList);
191 // Remove the data which send from pending list
192 CARemoveEDRDataFromList(&device->pendingDataList);
194 u_mutex_unlock(gEDRDeviceListMutex);
198 case BT_SOCKET_DISCONNECTED:
200 u_mutex_lock(gEDRDeviceListMutex);
201 CARemoveEDRDeviceFromList(&gEDRDeviceList, connection->remote_address);
202 u_mutex_unlock(gEDRDeviceListMutex);
207 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
211 void CAEDRDeviceDiscoveryCallback(int result, bt_adapter_device_discovery_state_e state,
212 bt_adapter_device_discovery_info_s *discoveryInfo, void *userData)
214 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN");
216 EDRDevice *device = NULL;
218 if (BT_ERROR_NONE != result)
220 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Received bad state!, error num [%x]",
227 case BT_ADAPTER_DEVICE_DISCOVERY_STARTED:
229 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Discovery started!");
233 case BT_ADAPTER_DEVICE_DISCOVERY_FINISHED:
235 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Discovery finished!");
239 case BT_ADAPTER_DEVICE_DISCOVERY_FOUND:
241 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Device discovered [%s]!",
242 discoveryInfo->remote_name);
243 if (CA_TRUE == CAEDRIsServiceSupported((const char **)discoveryInfo->service_uuid,
244 discoveryInfo->service_count,
247 // Check if the deivce is already in the list
248 u_mutex_lock(gEDRDeviceListMutex);
249 if (CA_STATUS_OK == CAGetEDRDevice(gEDRDeviceList, discoveryInfo->remote_address,
252 device->serviceSearched = 1;
253 u_mutex_unlock(gEDRDeviceListMutex);
257 // Create the deviceinfo and add to list
258 if (CA_STATUS_OK != CACreateAndAddToDeviceList(&gEDRDeviceList,
259 discoveryInfo->remote_address, OIC_EDR_SERVICE_ID, &device))
261 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to add device to list!");
262 u_mutex_unlock(gEDRDeviceListMutex);
266 device->serviceSearched = 1;
267 u_mutex_unlock(gEDRDeviceListMutex);
271 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Device does not support OIC service!");
277 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
280 void CAEDRServiceSearchedCallback(int32_t result, bt_device_sdp_info_s *sdpInfo, void *userData)
282 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN");
286 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "SDP info is null!");
290 u_mutex_lock(gEDRDeviceListMutex);
292 EDRDevice *device = NULL;
293 if (CA_STATUS_OK == CAGetEDRDevice(gEDRDeviceList, sdpInfo->remote_address, &device)
296 if (1 == device->serviceSearched)
298 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Service is already searched for this device!");
299 u_mutex_unlock(gEDRDeviceListMutex);
303 if (CA_TRUE == CAEDRIsServiceSupported((const char **)sdpInfo->service_uuid,
304 sdpInfo->service_count, OIC_EDR_SERVICE_ID))
306 device->serviceSearched = 1;
307 if (CA_STATUS_OK != CAEDRClientConnect(sdpInfo->remote_address, OIC_EDR_SERVICE_ID))
309 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to make rfcomm connection!");
311 // Remove the device from device list
312 CARemoveEDRDeviceFromList(&gEDRDeviceList, sdpInfo->remote_address);
317 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Device does not contain OIC service!");
319 // Remove device from list as it does not support OIC service
320 CARemoveEDRDeviceFromList(&gEDRDeviceList, sdpInfo->remote_address);
324 u_mutex_unlock(gEDRDeviceListMutex);
326 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
329 CAResult_t CAEDRStartDeviceDiscovery(void)
331 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN");
333 bt_error_e err = BT_ERROR_NONE;
334 bool isDiscoveryStarted = false;
336 // Check the device discovery state
337 if (BT_ERROR_NONE != (err = bt_adapter_is_discovering(&isDiscoveryStarted)))
339 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
341 return CA_STATUS_FAILED;
344 //Start device discovery if its not started
345 if (false == isDiscoveryStarted)
347 if (BT_ERROR_NONE != (err = bt_adapter_start_device_discovery()))
349 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Device discovery failed!, error num [%x]",
351 return CA_STATUS_FAILED;
355 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
359 CAResult_t CAEDRStopServiceSearch(void)
361 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN");
363 bt_error_e err = BT_ERROR_NONE;
365 // Stop ongoing service search
366 if (BT_ERROR_NONE != (err = bt_device_cancel_service_search()))
368 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
370 return CA_STATUS_FAILED;
373 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
377 CAResult_t CAEDRStopDeviceDiscovery(void)
379 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN");
381 bt_error_e err = BT_ERROR_NONE;
382 bool isDiscoveryStarted = false;
384 // Check the device discovery state
385 if (BT_ERROR_NONE != (err = bt_adapter_is_discovering(&isDiscoveryStarted)))
387 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
389 return CA_STATUS_FAILED;
392 //stop the device discovery process
393 if (true == isDiscoveryStarted)
395 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Stopping the device search process");
396 if (BT_ERROR_NONE != (err = bt_adapter_stop_device_discovery()))
398 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to stop discovery!, error num [%x]",
403 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
407 CAResult_t CAEDRStartServiceSearch(const char *remoteAddress)
409 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN");
411 bt_error_e err = BT_ERROR_NONE;
414 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
415 if (0 == strlen(remoteAddress))
417 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Remote address is empty!");
418 return CA_STATUS_INVALID_PARAM;
421 // Start searching for OIC service
422 if (BT_ERROR_NONE != (err = bt_device_start_service_search(remoteAddress)))
424 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
426 return CA_STATUS_FAILED;
429 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
433 CAResult_t CAEDRClientSetCallbacks(void)
435 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN");
437 // Register for discovery and rfcomm socket connection callbacks
438 bt_adapter_set_device_discovery_state_changed_cb(CAEDRDeviceDiscoveryCallback, NULL);
439 bt_device_set_service_searched_cb(CAEDRServiceSearchedCallback, NULL);
440 bt_socket_set_connection_state_changed_cb(CAEDRSocketConnectionStateCallback, NULL);
441 bt_socket_set_data_received_cb(CAEDRDataRecvCallback, NULL);
443 // Start device discovery
444 if( CA_STATUS_OK != CAEDRStartDeviceDiscovery())
446 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Failed to Start Device discovery");
447 return CA_STATUS_FAILED;
450 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
455 void CAEDRClientUnsetCallbacks(void)
457 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN");
459 // Stop service search
460 CAEDRStopServiceSearch();
462 // Stop the device discovery process
463 CAEDRStopDeviceDiscovery();
465 // reset bluetooth adapter callbacks
466 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Resetting the callbacks");
467 bt_adapter_unset_device_discovery_state_changed_cb();
468 bt_device_unset_service_searched_cb();
469 bt_socket_unset_connection_state_changed_cb();
470 bt_socket_unset_data_received_cb();
472 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
475 void CAEDRManagerInitializeMutex(void)
477 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN");
481 if (!gEDRDeviceListMutex)
483 gEDRDeviceListMutex = u_mutex_new();
486 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
489 void CAEDRManagerTerminateMutex(void)
491 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN");
493 if (gEDRDeviceListMutex)
495 u_mutex_free(gEDRDeviceListMutex);
496 gEDRDeviceListMutex = NULL;
499 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
502 void CAEDRInitializeClient(u_thread_pool_t handle)
504 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
505 CAEDRManagerInitializeMutex();
506 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
509 void CAEDRTerminateClient()
511 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
513 // Free EDRDevices list
514 if (gEDRDeviceListMutex)
516 u_mutex_lock(gEDRDeviceListMutex);
517 CADestroyEDRDeviceList(&gEDRDeviceList);
518 u_mutex_unlock(gEDRDeviceListMutex);
522 CAEDRManagerTerminateMutex();
523 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
526 void CAEDRClientDisconnectAll(void)
528 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN");
530 u_mutex_lock(gEDRDeviceListMutex);
532 EDRDeviceList *cur = gEDRDeviceList;
535 EDRDevice *device = cur->device;
538 if (device && 0 <= device->socketFD)
540 if (CA_STATUS_OK != CAEDRClientDisconnect(device->socketFD))
542 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to disconnect with client :%s",
543 device->remoteAddress);
546 device->socketFD = -1;
550 u_mutex_unlock(gEDRDeviceListMutex);
552 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
556 CAResult_t CAEDRClientSendUnicastData(const char *remoteAddress, const char *serviceUUID,
557 void *data, uint32_t dataLength, uint32_t *sentLength)
559 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN");
561 EDRDevice *device = NULL;
564 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
565 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "service UUID is null");
566 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
567 VERIFY_NON_NULL(sentLength, EDR_ADAPTER_TAG, "Sent data length holder is null");
571 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
572 return CA_STATUS_INVALID_PARAM;
575 // Check the connection existence with remote device
576 u_mutex_lock(gEDRDeviceListMutex);
577 if (CA_STATUS_OK != CAGetEDRDevice(gEDRDeviceList, remoteAddress, &device))
579 // Create new device and add to list
580 if (CA_STATUS_OK != CACreateAndAddToDeviceList(&gEDRDeviceList, remoteAddress,
581 OIC_EDR_SERVICE_ID, &device))
583 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed create device and add to list!");
585 u_mutex_unlock(gEDRDeviceListMutex);
586 return CA_STATUS_FAILED;
589 // Start the OIC service search newly created device
590 if (CA_STATUS_OK != CAEDRStartServiceSearch(remoteAddress))
592 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to initiate service search!");
594 // Remove device from list
595 CARemoveEDRDeviceFromList(&gEDRDeviceList, remoteAddress);
597 u_mutex_unlock(gEDRDeviceListMutex);
598 return CA_STATUS_FAILED;
601 u_mutex_unlock(gEDRDeviceListMutex);
603 if (-1 == device->socketFD)
605 // Adding to pending list
606 if (CA_STATUS_OK != CAAddEDRDataToList(&device->pendingDataList, data,
609 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list!");
611 //Remove device from list
612 CARemoveEDRDeviceFromList(&gEDRDeviceList, remoteAddress);
613 return CA_STATUS_FAILED;
616 // Make a rfcomm connection with remote BT Device
617 if (1 == device->serviceSearched &&
618 CA_STATUS_OK != CAEDRClientConnect(remoteAddress, serviceUUID))
620 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection!");
622 //Remove device from list
623 CARemoveEDRDeviceFromList(&gEDRDeviceList, remoteAddress);
624 return CA_STATUS_FAILED;
626 *sentLength = dataLength;
630 if (CA_STATUS_OK != CAEDRSendData(device->socketFD, data, dataLength, sentLength))
632 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send data!");
633 return CA_STATUS_FAILED;
637 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
641 CAResult_t CAEDRClientSendMulticastData(const char *serviceUUID, void *data,
642 uint32_t dataLength, uint32_t *sentLength)
644 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN");
647 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "service UUID is null");
648 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
649 VERIFY_NON_NULL(sentLength, EDR_ADAPTER_TAG, "Sent data length holder is null");
653 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
654 return CA_STATUS_INVALID_PARAM;
657 *sentLength = dataLength;
659 // Send the packet to all OIC devices
660 u_mutex_lock(gEDRDeviceListMutex);
661 EDRDeviceList *curList = gEDRDeviceList;
662 while (curList != NULL)
664 EDRDevice *device = curList->device;
665 curList = curList->next;
669 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "There is no device!");
673 if (-1 == device->socketFD)
675 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN1");
676 // Check if the device service search is finished
677 if (0 == device->serviceSearched)
679 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Device services are still unknown!");
683 // Adding to pendding list
684 if (CA_STATUS_OK != CAAddEDRDataToList(&device->pendingDataList, data,
687 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list !");
691 // Make a rfcomm connection with remote BT Device
692 if (CA_STATUS_OK != CAEDRClientConnect(device->remoteAddress, device->serviceUUID))
694 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection !");
696 //Remove the data which added to pending list
697 CARemoveEDRDataFromList(&device->pendingDataList);
700 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN2");
704 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN3");
705 if (CA_STATUS_OK != CAEDRSendData(device->socketFD, data, dataLength, sentLength))
707 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send data to [%s] !",
708 device->remoteAddress);
710 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN4");
713 u_mutex_unlock(gEDRDeviceListMutex);
715 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
719 CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID)
721 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN");
723 bt_error_e err = BT_ERROR_NONE;
724 int32_t addressLen = 0;
726 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
727 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "Service UUID is null");
729 addressLen = strlen(remoteAddress);
730 if (0 == addressLen || CA_MACADDR_SIZE - 1 != addressLen)
732 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Invalid input: Invalid remote address");
733 return CA_STATUS_INVALID_PARAM;
736 if (0 == strlen(serviceUUID))
738 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Invalid input: Empty service uuid");
739 return CA_STATUS_INVALID_PARAM;
742 if (BT_ERROR_NONE != (err = bt_socket_connect_rfcomm(remoteAddress, serviceUUID)))
744 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
745 "Failed to connect!, address [%s] error num [%x]",
747 return CA_STATUS_FAILED;
750 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
754 CAResult_t CAEDRClientDisconnect(const int32_t clientID)
756 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN");
761 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Invalid input: negative client id");
762 return CA_STATUS_INVALID_PARAM;
765 bt_error_e err = BT_ERROR_NONE;
766 if (BT_ERROR_NONE != (err = bt_socket_disconnect_rfcomm(clientID)))
768 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed close rfcomm client socket!, error num [%x]",
770 return CA_STATUS_FAILED;
773 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
777 void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData)
779 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN");
781 EDRDevice *device = NULL;
783 if (NULL == data || 0 >= data->data_size)
785 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Data is null!");
789 // Get EDR device from list
790 u_mutex_lock(gEDRDeviceListMutex);
791 if (CA_STATUS_OK != CAGetEDRDeviceBySocketId(gEDRDeviceList, data->socket_fd, &device))
793 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Could not find the device!");
795 u_mutex_unlock(gEDRDeviceListMutex);
798 u_mutex_unlock(gEDRDeviceListMutex);
800 uint32_t sentLength = 0;
802 gEDRPacketReceivedCallback(device->remoteAddress, data->data,
803 (uint32_t)data->data_size, &sentLength);
805 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");