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 ******************************************************************/
24 * This file provides the APIs to establish RFCOMM connection with remote
29 #include <bluetooth.h>
31 #include "caedrinterface.h"
33 #include "caedrendpoint.h"
34 #include "caadapterutils.h"
35 #include "caedrutils.h"
38 #include "caedrdevicelist.h"
41 * Mutex to synchronize the access to Bluetooth device information list.
43 static ca_mutex g_edrDeviceListMutex = NULL;
46 * Peer Bluetooth device information list.
48 static EDRDeviceList *g_edrDeviceList = NULL;
51 * Maintains the callback to be notified when data received from remote
54 static CAEDRDataReceivedCallback g_edrPacketReceivedCallback = NULL;
57 * Error callback to update error in EDR.
59 static CAEDRErrorHandleCallback g_edrErrorHandler = NULL;
62 * This function creates mutex.
64 static void CAEDRManagerInitializeMutex(void);
67 * This function frees mutex.
69 static void CAEDRManagerTerminateMutex(void);
72 * This callback is registered to recieve data on any open RFCOMM connection.
74 static void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData);
77 * This function starts device discovery.
79 static CAResult_t CAEDRStartDeviceDiscovery(void);
82 * This function stops any ongoing service sevice search.
84 static CAResult_t CAEDRStopServiceSearch(void);
87 * This function stops device discovery.
89 static CAResult_t CAEDRStopDeviceDiscovery(void);
92 * This function searches for OIC service for remote Bluetooth device.
94 static CAResult_t CAEDRStartServiceSearch(const char *remoteAddress);
97 * This callback is registered to recieve all bluetooth nearby devices
98 * when device scan is initiated.
100 static void CAEDRDeviceDiscoveryCallback(int result,
101 bt_adapter_device_discovery_state_e state,
102 bt_adapter_device_discovery_info_s *discoveryInfo,
106 * This callback is registered to recieve all the services remote
107 * bluetooth device supports when service search initiated.
109 static void CAEDRServiceSearchedCallback(int result, bt_device_sdp_info_s *sdpInfo,
113 * This callback is registered to receive bluetooth RFCOMM connection
116 static void CAEDRSocketConnectionStateCallback(int result,
117 bt_socket_connection_state_e state,
118 bt_socket_connection_s *connection, void *userData);
121 * Establishes RFCOMM connection with remote bluetooth device.
123 static CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID);
126 * Disconnect RFCOMM client socket connection.
128 static CAResult_t CAEDRClientDisconnect(const int32_t clientID);
130 void CAEDRSetPacketReceivedCallback(CAEDRDataReceivedCallback packetReceivedCallback)
132 g_edrPacketReceivedCallback = packetReceivedCallback;
135 void CAEDRSetErrorHandler(CAEDRErrorHandleCallback errorHandleCallback)
137 g_edrErrorHandler = errorHandleCallback;
140 void CAEDRSocketConnectionStateCallback(int result, bt_socket_connection_state_e state,
141 bt_socket_connection_s *connection, void *userData)
143 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
145 EDRDevice *device = NULL;
147 if (BT_ERROR_NONE != result || NULL == connection)
149 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Invalid connection state!, error num [%x]",
156 case BT_SOCKET_CONNECTED:
158 ca_mutex_lock(g_edrDeviceListMutex);
159 CAResult_t res = CAGetEDRDevice(g_edrDeviceList, connection->remote_address,
161 if (CA_STATUS_OK != res)
163 // Create the deviceinfo and add to list
164 res = CACreateAndAddToDeviceList(&g_edrDeviceList,
165 connection->remote_address, OIC_EDR_SERVICE_ID, &device);
166 if (CA_STATUS_OK != res)
168 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed add device to list ret[%d]", res);
169 ca_mutex_unlock(g_edrDeviceListMutex);
175 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
176 ca_mutex_unlock(g_edrDeviceListMutex);
180 device->socketFD = connection->socket_fd;
181 ca_mutex_unlock(g_edrDeviceListMutex);
187 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
188 ca_mutex_unlock(g_edrDeviceListMutex);
191 device->socketFD = connection->socket_fd;
192 while (device->pendingDataList)
194 EDRData *edrData = device->pendingDataList->data;
195 res = CAEDRSendData(device->socketFD, edrData->data,
196 edrData->dataLength);
197 if (CA_STATUS_OK != res)
199 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send pending data [%s]",
200 device->remoteAddress);
202 // Remove all the data from pending list
203 CADestroyEDRDataList(&device->pendingDataList);
207 // Remove the data which send from pending list
208 CARemoveEDRDataFromList(&device->pendingDataList);
210 ca_mutex_unlock(g_edrDeviceListMutex);
214 case BT_SOCKET_DISCONNECTED:
216 ca_mutex_lock(g_edrDeviceListMutex);
217 CARemoveEDRDeviceFromList(&g_edrDeviceList, connection->remote_address);
218 ca_mutex_unlock(g_edrDeviceListMutex);
223 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
227 void CAEDRDeviceDiscoveryCallback(int result, bt_adapter_device_discovery_state_e state,
228 bt_adapter_device_discovery_info_s *discoveryInfo, void *userData)
230 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
232 EDRDevice *device = NULL;
234 if (BT_ERROR_NONE != result)
236 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Received bad state!, error num [%x]",
243 case BT_ADAPTER_DEVICE_DISCOVERY_STARTED:
245 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Discovery started!");
249 case BT_ADAPTER_DEVICE_DISCOVERY_FINISHED:
251 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Discovery finished!");
255 case BT_ADAPTER_DEVICE_DISCOVERY_FOUND:
257 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Device discovered [%s]!",
258 discoveryInfo->remote_name);
259 if (true == CAEDRIsServiceSupported((const char **)discoveryInfo->service_uuid,
260 discoveryInfo->service_count,
263 // Check if the deivce is already in the list
264 ca_mutex_lock(g_edrDeviceListMutex);
265 if (CA_STATUS_OK == CAGetEDRDevice(g_edrDeviceList,
266 discoveryInfo->remote_address, &device))
270 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
271 ca_mutex_unlock(g_edrDeviceListMutex);
274 device->serviceSearched = true;
275 ca_mutex_unlock(g_edrDeviceListMutex);
279 // Create the deviceinfo and add to list
280 CAResult_t res = CACreateAndAddToDeviceList(&g_edrDeviceList,
281 discoveryInfo->remote_address, OIC_EDR_SERVICE_ID, &device);
282 if (CA_STATUS_OK != res)
284 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add device to list!");
285 ca_mutex_unlock(g_edrDeviceListMutex);
291 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
292 ca_mutex_unlock(g_edrDeviceListMutex);
295 device->serviceSearched = true;
296 ca_mutex_unlock(g_edrDeviceListMutex);
300 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Device does not support OIC service!");
306 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
309 void CAEDRServiceSearchedCallback(int32_t result,
310 bt_device_sdp_info_s *sdpInfo,void *userData)
312 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
316 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "SDP info is null!");
320 ca_mutex_lock(g_edrDeviceListMutex);
322 EDRDevice *device = NULL;
323 CAResult_t res = CAGetEDRDevice(g_edrDeviceList, sdpInfo->remote_address, &device);
324 if (CA_STATUS_OK == res && NULL != device)
326 if (device->serviceSearched)
328 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Service is already searched for this device!");
329 ca_mutex_unlock(g_edrDeviceListMutex);
333 if (true == CAEDRIsServiceSupported((const char **)sdpInfo->service_uuid,
334 sdpInfo->service_count, OIC_EDR_SERVICE_ID))
336 device->serviceSearched = true;
337 res = CAEDRClientConnect(sdpInfo->remote_address, OIC_EDR_SERVICE_ID);
338 if (CA_STATUS_OK != res)
340 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make rfcomm connection!");
342 // Remove the device from device list
343 CARemoveEDRDeviceFromList(&g_edrDeviceList, sdpInfo->remote_address);
348 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Device does not contain OIC service!");
350 // Remove device from list as it does not support OIC service
351 CARemoveEDRDeviceFromList(&g_edrDeviceList, sdpInfo->remote_address);
355 ca_mutex_unlock(g_edrDeviceListMutex);
357 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
360 CAResult_t CAEDRStartDeviceDiscovery(void)
362 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
365 bool isDiscoveryStarted = false;
367 // Check the device discovery state
368 bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
369 if (BT_ERROR_NONE != err)
371 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
373 return CA_STATUS_FAILED;
376 //Start device discovery if its not started
377 if (false == isDiscoveryStarted)
379 err = bt_adapter_start_device_discovery();
380 if (BT_ERROR_NONE != err)
382 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Device discovery failed!, error num [%x]",
384 return CA_STATUS_FAILED;
388 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
392 CAResult_t CAEDRStopServiceSearch(void)
394 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
396 bt_error_e err = bt_device_cancel_service_search();
397 // Stop ongoing service search
398 if (BT_ERROR_NONE != err)
400 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
402 return CA_STATUS_FAILED;
405 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
409 CAResult_t CAEDRStopDeviceDiscovery(void)
411 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
413 bool isDiscoveryStarted = false;
414 bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
415 // Check the device discovery state
416 if (BT_ERROR_NONE != err)
418 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
420 return CA_STATUS_FAILED;
423 //stop the device discovery process
424 if (true == isDiscoveryStarted)
426 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Stopping the device search process");
427 if (BT_ERROR_NONE != (err = bt_adapter_stop_device_discovery()))
429 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to stop discovery!, error num [%x]",
434 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
438 CAResult_t CAEDRStartServiceSearch(const char *remoteAddress)
440 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
443 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
444 if (!remoteAddress[0])
446 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Remote address is empty!");
447 return CA_STATUS_INVALID_PARAM;
450 bt_error_e err = bt_device_start_service_search(remoteAddress);
451 // Start searching for OIC service
452 if (BT_ERROR_NONE != err)
454 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
456 return CA_STATUS_FAILED;
459 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
463 CAResult_t CAEDRClientSetCallbacks(void)
465 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
467 // Register for discovery and rfcomm socket connection callbacks
468 bt_adapter_set_device_discovery_state_changed_cb(CAEDRDeviceDiscoveryCallback, NULL);
469 bt_device_set_service_searched_cb(CAEDRServiceSearchedCallback, NULL);
470 bt_socket_set_connection_state_changed_cb(CAEDRSocketConnectionStateCallback, NULL);
471 bt_socket_set_data_received_cb(CAEDRDataRecvCallback, NULL);
473 // Start device discovery
474 CAResult_t result = CAEDRStartDeviceDiscovery();
475 if(CA_STATUS_OK != result)
477 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Failed to Start Device discovery");
478 return CA_STATUS_FAILED;
481 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
486 void CAEDRClientUnsetCallbacks(void)
488 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
490 // Stop service search
491 CAEDRStopServiceSearch();
493 // Stop the device discovery process
494 CAEDRStopDeviceDiscovery();
496 // reset bluetooth adapter callbacks
497 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Resetting the callbacks");
498 bt_adapter_unset_device_discovery_state_changed_cb();
499 bt_device_unset_service_searched_cb();
500 bt_socket_unset_connection_state_changed_cb();
501 bt_socket_unset_data_received_cb();
503 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
506 void CAEDRManagerInitializeMutex(void)
508 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
510 if (!g_edrDeviceListMutex)
512 g_edrDeviceListMutex = ca_mutex_new();
515 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
518 void CAEDRManagerTerminateMutex(void)
520 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
522 if (g_edrDeviceListMutex)
524 ca_mutex_free(g_edrDeviceListMutex);
525 g_edrDeviceListMutex = NULL;
528 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
531 void CAEDRInitializeClient(ca_thread_pool_t handle)
533 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
534 CAEDRManagerInitializeMutex();
535 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
538 void CAEDRClientTerminate()
540 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
542 // Free EDRDevices list
543 if (g_edrDeviceListMutex)
545 ca_mutex_lock(g_edrDeviceListMutex);
546 CADestroyEDRDeviceList(&g_edrDeviceList);
547 ca_mutex_unlock(g_edrDeviceListMutex);
551 CAEDRManagerTerminateMutex();
552 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
555 void CAEDRClientDisconnectAll(void)
557 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
559 ca_mutex_lock(g_edrDeviceListMutex);
561 EDRDeviceList *cur = g_edrDeviceList;
564 EDRDevice *device = cur->device;
567 if (device && 0 <= device->socketFD)
569 CAResult_t result = CAEDRClientDisconnect(device->socketFD);
570 if (CA_STATUS_OK != result)
572 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to disconnect with client :%s",
573 device->remoteAddress);
576 device->socketFD = -1;
580 ca_mutex_unlock(g_edrDeviceListMutex);
582 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
586 CAResult_t CAEDRClientSendUnicastData(const char *remoteAddress, const void *data,
589 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
591 EDRDevice *device = NULL;
594 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
595 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
599 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
600 return CA_STATUS_INVALID_PARAM;
603 // Check the connection existence with remote device
604 ca_mutex_lock(g_edrDeviceListMutex);
605 CAResult_t result = CAGetEDRDevice(g_edrDeviceList, remoteAddress, &device);
606 if (CA_STATUS_OK != result)
608 // Create new device and add to list
609 result = CACreateAndAddToDeviceList(&g_edrDeviceList, remoteAddress,
610 OIC_EDR_SERVICE_ID, &device);
611 if (CA_STATUS_OK != result)
613 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed create device and add to list!");
615 ca_mutex_unlock(g_edrDeviceListMutex);
616 return CA_STATUS_FAILED;
619 // Start the OIC service search newly created device
620 result = CAEDRStartServiceSearch(remoteAddress);
621 if (CA_STATUS_OK != result)
623 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to initiate service search!");
625 // Remove device from list
626 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
628 ca_mutex_unlock(g_edrDeviceListMutex);
629 return CA_STATUS_FAILED;
635 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
636 // Remove device from list
637 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
639 ca_mutex_unlock(g_edrDeviceListMutex);
640 return CA_STATUS_FAILED;
643 ca_mutex_unlock(g_edrDeviceListMutex);
645 if (-1 == device->socketFD)
647 // Adding to pending list
648 result = CAAddEDRDataToList(&device->pendingDataList, data,
650 if (CA_STATUS_OK != result)
652 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list!");
654 //Remove device from list
655 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
656 return CA_STATUS_FAILED;
659 // Make a rfcomm connection with remote BT Device
660 if (device->serviceSearched &&
661 CA_STATUS_OK != CAEDRClientConnect(remoteAddress, OIC_EDR_SERVICE_ID))
663 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection!");
665 //Remove device from list
666 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
667 return CA_STATUS_FAILED;
672 result = CAEDRSendData(device->socketFD, data, dataLength);
673 if (CA_STATUS_OK != result)
675 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to send data!");
676 return CA_STATUS_FAILED;
680 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
684 CAResult_t CAEDRClientSendMulticastData(const void *data, uint32_t dataLength)
686 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
689 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
693 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
694 return CA_STATUS_INVALID_PARAM;
697 // Send the packet to all OIC devices
698 ca_mutex_lock(g_edrDeviceListMutex);
699 EDRDeviceList *curList = g_edrDeviceList;
700 CAResult_t result = CA_STATUS_FAILED;
701 while (curList != NULL)
703 EDRDevice *device = curList->device;
704 curList = curList->next;
708 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
712 if (-1 == device->socketFD)
714 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN1");
715 // Check if the device service search is finished
716 if (false == device->serviceSearched)
718 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Device services are still unknown!");
722 // Adding to pendding list
723 result = CAAddEDRDataToList(&device->pendingDataList, data,
725 if (CA_STATUS_OK != result)
727 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list !");
731 // Make a rfcomm connection with remote BT Device
732 result = CAEDRClientConnect(device->remoteAddress, device->serviceUUID);
733 if (CA_STATUS_OK != result)
735 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection !");
737 //Remove the data which added to pending list
738 CARemoveEDRDataFromList(&device->pendingDataList);
741 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN2");
745 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN3");
746 result = CAEDRSendData(device->socketFD, data, dataLength);
747 if (CA_STATUS_OK != result)
749 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send data to [%s] !",
750 device->remoteAddress);
752 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN4");
755 ca_mutex_unlock(g_edrDeviceListMutex);
757 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
761 CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID)
763 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
765 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
766 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "Service UUID is null");
768 size_t addressLen = strlen(remoteAddress);
769 if (0 == addressLen || CA_MACADDR_SIZE - 1 != addressLen)
771 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Invalid remote address");
772 return CA_STATUS_INVALID_PARAM;
777 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Empty service uuid");
778 return CA_STATUS_INVALID_PARAM;
781 bt_error_e err = bt_socket_connect_rfcomm(remoteAddress, serviceUUID);
782 if (BT_ERROR_NONE != err)
784 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
785 "Failed to connect!, address [%s] error num [%x]",
787 return CA_STATUS_FAILED;
790 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
794 CAResult_t CAEDRClientDisconnect(const int32_t clientID)
796 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
801 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: negative client id");
802 return CA_STATUS_INVALID_PARAM;
805 bt_error_e err = bt_socket_disconnect_rfcomm(clientID);
806 if (BT_ERROR_NONE != err)
808 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed close rfcomm client socket!, error num [%x]",
810 return CA_STATUS_FAILED;
813 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
817 void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData)
819 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
821 EDRDevice *device = NULL;
823 if (NULL == data || 0 >= data->data_size)
825 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Data is null!");
829 // Get EDR device from list
830 ca_mutex_lock(g_edrDeviceListMutex);
831 CAResult_t result = CAGetEDRDeviceBySocketId(g_edrDeviceList, data->socket_fd, &device);
832 if (CA_STATUS_OK != result)
834 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Could not find the device!");
836 ca_mutex_unlock(g_edrDeviceListMutex);
839 ca_mutex_unlock(g_edrDeviceListMutex);
843 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
847 uint32_t sentLength = 0;
849 g_edrPacketReceivedCallback(device->remoteAddress, data->data,
850 (uint32_t)data->data_size, &sentLength);
852 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");