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 uint32_t sentData = 0;
195 EDRData *edrData = device->pendingDataList->data;
196 res = CAEDRSendData(device->socketFD, edrData->data,
197 edrData->dataLength, &sentData);
198 if (CA_STATUS_OK != res)
200 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send pending data [%s]",
201 device->remoteAddress);
203 // Remove all the data from pending list
204 CADestroyEDRDataList(&device->pendingDataList);
208 // Remove the data which send from pending list
209 CARemoveEDRDataFromList(&device->pendingDataList);
211 ca_mutex_unlock(g_edrDeviceListMutex);
215 case BT_SOCKET_DISCONNECTED:
217 ca_mutex_lock(g_edrDeviceListMutex);
218 CARemoveEDRDeviceFromList(&g_edrDeviceList, connection->remote_address);
219 ca_mutex_unlock(g_edrDeviceListMutex);
224 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
228 void CAEDRDeviceDiscoveryCallback(int result, bt_adapter_device_discovery_state_e state,
229 bt_adapter_device_discovery_info_s *discoveryInfo, void *userData)
231 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
233 EDRDevice *device = NULL;
235 if (BT_ERROR_NONE != result)
237 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Received bad state!, error num [%x]",
244 case BT_ADAPTER_DEVICE_DISCOVERY_STARTED:
246 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Discovery started!");
250 case BT_ADAPTER_DEVICE_DISCOVERY_FINISHED:
252 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Discovery finished!");
256 case BT_ADAPTER_DEVICE_DISCOVERY_FOUND:
258 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Device discovered [%s]!",
259 discoveryInfo->remote_name);
260 if (true == CAEDRIsServiceSupported((const char **)discoveryInfo->service_uuid,
261 discoveryInfo->service_count,
264 // Check if the deivce is already in the list
265 ca_mutex_lock(g_edrDeviceListMutex);
266 if (CA_STATUS_OK == CAGetEDRDevice(g_edrDeviceList,
267 discoveryInfo->remote_address, &device))
271 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
272 ca_mutex_unlock(g_edrDeviceListMutex);
275 device->serviceSearched = true;
276 ca_mutex_unlock(g_edrDeviceListMutex);
280 // Create the deviceinfo and add to list
281 CAResult_t res = CACreateAndAddToDeviceList(&g_edrDeviceList,
282 discoveryInfo->remote_address, OIC_EDR_SERVICE_ID, &device);
283 if (CA_STATUS_OK != res)
285 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add device to list!");
286 ca_mutex_unlock(g_edrDeviceListMutex);
292 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
293 ca_mutex_unlock(g_edrDeviceListMutex);
296 device->serviceSearched = true;
297 ca_mutex_unlock(g_edrDeviceListMutex);
301 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Device does not support OIC service!");
307 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
310 void CAEDRServiceSearchedCallback(int32_t result,
311 bt_device_sdp_info_s *sdpInfo,void *userData)
313 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
317 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "SDP info is null!");
321 ca_mutex_lock(g_edrDeviceListMutex);
323 EDRDevice *device = NULL;
324 CAResult_t res = CAGetEDRDevice(g_edrDeviceList, sdpInfo->remote_address, &device);
325 if (CA_STATUS_OK == res && NULL != device)
327 if (device->serviceSearched)
329 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Service is already searched for this device!");
330 ca_mutex_unlock(g_edrDeviceListMutex);
334 if (true == CAEDRIsServiceSupported((const char **)sdpInfo->service_uuid,
335 sdpInfo->service_count, OIC_EDR_SERVICE_ID))
337 device->serviceSearched = true;
338 res = CAEDRClientConnect(sdpInfo->remote_address, OIC_EDR_SERVICE_ID);
339 if (CA_STATUS_OK != res)
341 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make rfcomm connection!");
343 // Remove the device from device list
344 CARemoveEDRDeviceFromList(&g_edrDeviceList, sdpInfo->remote_address);
349 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Device does not contain OIC service!");
351 // Remove device from list as it does not support OIC service
352 CARemoveEDRDeviceFromList(&g_edrDeviceList, sdpInfo->remote_address);
356 ca_mutex_unlock(g_edrDeviceListMutex);
358 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
361 CAResult_t CAEDRStartDeviceDiscovery(void)
363 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
366 bool isDiscoveryStarted = false;
368 // Check the device discovery state
369 bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
370 if (BT_ERROR_NONE != err)
372 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
374 return CA_STATUS_FAILED;
377 //Start device discovery if its not started
378 if (false == isDiscoveryStarted)
380 err = bt_adapter_start_device_discovery();
381 if (BT_ERROR_NONE != err)
383 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Device discovery failed!, error num [%x]",
385 return CA_STATUS_FAILED;
389 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
393 CAResult_t CAEDRStopServiceSearch(void)
395 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
397 bt_error_e err = bt_device_cancel_service_search();
398 // Stop ongoing service search
399 if (BT_ERROR_NONE != err)
401 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
403 return CA_STATUS_FAILED;
406 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
410 CAResult_t CAEDRStopDeviceDiscovery(void)
412 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
414 bool isDiscoveryStarted = false;
415 bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
416 // Check the device discovery state
417 if (BT_ERROR_NONE != err)
419 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
421 return CA_STATUS_FAILED;
424 //stop the device discovery process
425 if (true == isDiscoveryStarted)
427 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Stopping the device search process");
428 if (BT_ERROR_NONE != (err = bt_adapter_stop_device_discovery()))
430 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to stop discovery!, error num [%x]",
435 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
439 CAResult_t CAEDRStartServiceSearch(const char *remoteAddress)
441 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
444 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
445 if (!remoteAddress[0])
447 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Remote address is empty!");
448 return CA_STATUS_INVALID_PARAM;
451 bt_error_e err = bt_device_start_service_search(remoteAddress);
452 // Start searching for OIC service
453 if (BT_ERROR_NONE != err)
455 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
457 return CA_STATUS_FAILED;
460 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
464 CAResult_t CAEDRClientSetCallbacks(void)
466 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
468 // Register for discovery and rfcomm socket connection callbacks
469 bt_adapter_set_device_discovery_state_changed_cb(CAEDRDeviceDiscoveryCallback, NULL);
470 bt_device_set_service_searched_cb(CAEDRServiceSearchedCallback, NULL);
471 bt_socket_set_connection_state_changed_cb(CAEDRSocketConnectionStateCallback, NULL);
472 bt_socket_set_data_received_cb(CAEDRDataRecvCallback, NULL);
474 // Start device discovery
475 CAResult_t result = CAEDRStartDeviceDiscovery();
476 if(CA_STATUS_OK != result)
478 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Failed to Start Device discovery");
479 return CA_STATUS_FAILED;
482 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
487 void CAEDRClientUnsetCallbacks(void)
489 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
491 // Stop service search
492 CAEDRStopServiceSearch();
494 // Stop the device discovery process
495 CAEDRStopDeviceDiscovery();
497 // reset bluetooth adapter callbacks
498 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Resetting the callbacks");
499 bt_adapter_unset_device_discovery_state_changed_cb();
500 bt_device_unset_service_searched_cb();
501 bt_socket_unset_connection_state_changed_cb();
502 bt_socket_unset_data_received_cb();
504 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
507 void CAEDRManagerInitializeMutex(void)
509 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
511 if (!g_edrDeviceListMutex)
513 g_edrDeviceListMutex = ca_mutex_new();
516 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
519 void CAEDRManagerTerminateMutex(void)
521 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
523 if (g_edrDeviceListMutex)
525 ca_mutex_free(g_edrDeviceListMutex);
526 g_edrDeviceListMutex = NULL;
529 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
532 void CAEDRInitializeClient(ca_thread_pool_t handle)
534 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
535 CAEDRManagerInitializeMutex();
536 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
539 void CAEDRClientTerminate()
541 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
543 // Free EDRDevices list
544 if (g_edrDeviceListMutex)
546 ca_mutex_lock(g_edrDeviceListMutex);
547 CADestroyEDRDeviceList(&g_edrDeviceList);
548 ca_mutex_unlock(g_edrDeviceListMutex);
552 CAEDRManagerTerminateMutex();
553 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
556 void CAEDRClientDisconnectAll(void)
558 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
560 ca_mutex_lock(g_edrDeviceListMutex);
562 EDRDeviceList *cur = g_edrDeviceList;
565 EDRDevice *device = cur->device;
568 if (device && 0 <= device->socketFD)
570 CAResult_t result = CAEDRClientDisconnect(device->socketFD);
571 if (CA_STATUS_OK != result)
573 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to disconnect with client :%s",
574 device->remoteAddress);
577 device->socketFD = -1;
581 ca_mutex_unlock(g_edrDeviceListMutex);
583 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
587 CAResult_t CAEDRClientSendUnicastData(const char *remoteAddress, const char *serviceUUID,
588 const void *data, uint32_t dataLength, uint32_t *sentLength)
590 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
592 EDRDevice *device = NULL;
595 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
596 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "service UUID is null");
597 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
598 VERIFY_NON_NULL(sentLength, EDR_ADAPTER_TAG, "Sent data length holder is null");
602 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
603 return CA_STATUS_INVALID_PARAM;
606 // Check the connection existence with remote device
607 ca_mutex_lock(g_edrDeviceListMutex);
608 CAResult_t result = CAGetEDRDevice(g_edrDeviceList, remoteAddress, &device);
609 if (CA_STATUS_OK != result)
611 // Create new device and add to list
612 result = CACreateAndAddToDeviceList(&g_edrDeviceList, remoteAddress,
613 OIC_EDR_SERVICE_ID, &device);
614 if (CA_STATUS_OK != result)
616 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed create device and add to list!");
618 ca_mutex_unlock(g_edrDeviceListMutex);
619 return CA_STATUS_FAILED;
622 // Start the OIC service search newly created device
623 result = CAEDRStartServiceSearch(remoteAddress);
624 if (CA_STATUS_OK != result)
626 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to initiate service search!");
628 // Remove device from list
629 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
631 ca_mutex_unlock(g_edrDeviceListMutex);
632 return CA_STATUS_FAILED;
638 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
639 // Remove device from list
640 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
642 ca_mutex_unlock(g_edrDeviceListMutex);
643 return CA_STATUS_FAILED;
646 ca_mutex_unlock(g_edrDeviceListMutex);
648 if (-1 == device->socketFD)
650 // Adding to pending list
651 result = CAAddEDRDataToList(&device->pendingDataList, data,
653 if (CA_STATUS_OK != result)
655 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list!");
657 //Remove device from list
658 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
659 return CA_STATUS_FAILED;
662 // Make a rfcomm connection with remote BT Device
663 if (device->serviceSearched &&
664 CA_STATUS_OK != CAEDRClientConnect(remoteAddress, serviceUUID))
666 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection!");
668 //Remove device from list
669 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
670 return CA_STATUS_FAILED;
672 *sentLength = dataLength;
676 result = CAEDRSendData(device->socketFD, data, dataLength, sentLength);
677 if (CA_STATUS_OK != result)
679 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to send data!");
680 return CA_STATUS_FAILED;
684 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
688 CAResult_t CAEDRClientSendMulticastData(const char *serviceUUID, const void *data,
689 uint32_t dataLength, uint32_t *sentLength)
691 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
694 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "service UUID is null");
695 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
696 VERIFY_NON_NULL(sentLength, EDR_ADAPTER_TAG, "Sent data length holder is null");
700 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
701 return CA_STATUS_INVALID_PARAM;
704 *sentLength = dataLength;
706 // Send the packet to all OIC devices
707 ca_mutex_lock(g_edrDeviceListMutex);
708 EDRDeviceList *curList = g_edrDeviceList;
709 CAResult_t result = CA_STATUS_FAILED;
710 while (curList != NULL)
712 EDRDevice *device = curList->device;
713 curList = curList->next;
717 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
721 if (-1 == device->socketFD)
723 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN1");
724 // Check if the device service search is finished
725 if (false == device->serviceSearched)
727 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Device services are still unknown!");
731 // Adding to pendding list
732 result = CAAddEDRDataToList(&device->pendingDataList, data,
734 if (CA_STATUS_OK != result)
736 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list !");
740 // Make a rfcomm connection with remote BT Device
741 result = CAEDRClientConnect(device->remoteAddress, device->serviceUUID);
742 if (CA_STATUS_OK != result)
744 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection !");
746 //Remove the data which added to pending list
747 CARemoveEDRDataFromList(&device->pendingDataList);
750 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN2");
754 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN3");
755 result = CAEDRSendData(device->socketFD, data, dataLength, sentLength);
756 if (CA_STATUS_OK != result)
758 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send data to [%s] !",
759 device->remoteAddress);
761 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN4");
764 ca_mutex_unlock(g_edrDeviceListMutex);
766 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
770 CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID)
772 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
774 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
775 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "Service UUID is null");
777 size_t addressLen = strlen(remoteAddress);
778 if (0 == addressLen || CA_MACADDR_SIZE - 1 != addressLen)
780 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Invalid remote address");
781 return CA_STATUS_INVALID_PARAM;
786 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Empty service uuid");
787 return CA_STATUS_INVALID_PARAM;
790 bt_error_e err = bt_socket_connect_rfcomm(remoteAddress, serviceUUID);
791 if (BT_ERROR_NONE != err)
793 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
794 "Failed to connect!, address [%s] error num [%x]",
796 return CA_STATUS_FAILED;
799 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
803 CAResult_t CAEDRClientDisconnect(const int32_t clientID)
805 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
810 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: negative client id");
811 return CA_STATUS_INVALID_PARAM;
814 bt_error_e err = bt_socket_disconnect_rfcomm(clientID);
815 if (BT_ERROR_NONE != err)
817 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed close rfcomm client socket!, error num [%x]",
819 return CA_STATUS_FAILED;
822 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
826 void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData)
828 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
830 EDRDevice *device = NULL;
832 if (NULL == data || 0 >= data->data_size)
834 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Data is null!");
838 // Get EDR device from list
839 ca_mutex_lock(g_edrDeviceListMutex);
840 CAResult_t result = CAGetEDRDeviceBySocketId(g_edrDeviceList, data->socket_fd, &device);
841 if (CA_STATUS_OK != result)
843 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Could not find the device!");
845 ca_mutex_unlock(g_edrDeviceListMutex);
848 ca_mutex_unlock(g_edrDeviceListMutex);
852 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
856 uint32_t sentLength = 0;
858 g_edrPacketReceivedCallback(device->remoteAddress, data->data,
859 (uint32_t)data->data_size, &sentLength);
861 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");