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"
40 #define MICROSECS_PER_SEC 1000000
43 * Condition to check if OIC supported device is found.
45 static ca_cond g_deviceDescCond = NULL;
48 * Flag that will be set when EDR adapter is stopped.
50 static bool g_isStopping = false;
53 * Mutex to synchronize the access to Bluetooth device information list.
55 static ca_mutex g_edrDeviceListMutex = NULL;
58 * Peer Bluetooth device information list.
60 static EDRDeviceList *g_edrDeviceList = NULL;
63 * Maintains the callback to be notified when data received from remote
66 static CAEDRDataReceivedCallback g_edrPacketReceivedCallback = NULL;
69 * Error callback to update error in EDR.
71 static CAEDRErrorHandleCallback g_edrErrorHandler = NULL;
74 * This function creates mutex.
76 static void CAEDRManagerInitializeMutex(void);
79 * This function frees mutex.
81 static void CAEDRManagerTerminateMutex(void);
84 * This callback is registered to recieve data on any open RFCOMM connection.
86 static void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData);
89 * This function starts device discovery.
91 static CAResult_t CAEDRStartDeviceDiscovery(void);
94 * This function stops any ongoing service sevice search.
96 static CAResult_t CAEDRStopServiceSearch(void);
99 * This function stops device discovery.
101 static CAResult_t CAEDRStopDeviceDiscovery(void);
104 * This function searches for OIC service for remote Bluetooth device.
106 static CAResult_t CAEDRStartServiceSearch(const char *remoteAddress);
109 * This callback is registered to recieve all bluetooth nearby devices
110 * when device scan is initiated.
112 static void CAEDRDeviceDiscoveryCallback(int result,
113 bt_adapter_device_discovery_state_e state,
114 bt_adapter_device_discovery_info_s *discoveryInfo,
118 * This callback is registered to recieve all the services remote
119 * bluetooth device supports when service search initiated.
121 static void CAEDRServiceSearchedCallback(int result, bt_device_sdp_info_s *sdpInfo,
125 * This callback is registered to receive bluetooth RFCOMM connection
128 static void CAEDRSocketConnectionStateCallback(int result,
129 bt_socket_connection_state_e state,
130 bt_socket_connection_s *connection, void *userData);
133 * Establishes RFCOMM connection with remote bluetooth device.
135 static CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID);
138 * Disconnect RFCOMM client socket connection.
140 static CAResult_t CAEDRClientDisconnect(const int32_t clientID);
142 void CAEDRSetPacketReceivedCallback(CAEDRDataReceivedCallback packetReceivedCallback)
144 g_edrPacketReceivedCallback = packetReceivedCallback;
147 void CAEDRSetErrorHandler(CAEDRErrorHandleCallback errorHandleCallback)
149 g_edrErrorHandler = errorHandleCallback;
152 void CAEDRSocketConnectionStateCallback(int result, bt_socket_connection_state_e state,
153 bt_socket_connection_s *connection, void *userData)
155 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
157 EDRDevice *device = NULL;
159 if (BT_ERROR_NONE != result || NULL == connection)
161 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Invalid connection state!, error num [%x]",
168 case BT_SOCKET_CONNECTED:
170 ca_mutex_lock(g_edrDeviceListMutex);
171 CAResult_t res = CAGetEDRDevice(g_edrDeviceList, connection->remote_address,
173 if (CA_STATUS_OK != res)
175 // Create the deviceinfo and add to list
176 res = CACreateAndAddToDeviceList(&g_edrDeviceList,
177 connection->remote_address, OIC_EDR_SERVICE_ID, &device);
178 if (CA_STATUS_OK != res)
180 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed add device to list ret[%d]", res);
181 ca_mutex_unlock(g_edrDeviceListMutex);
187 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
188 ca_mutex_unlock(g_edrDeviceListMutex);
192 device->socketFD = connection->socket_fd;
193 ca_mutex_unlock(g_edrDeviceListMutex);
199 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
200 ca_mutex_unlock(g_edrDeviceListMutex);
203 device->socketFD = connection->socket_fd;
204 while (device->pendingDataList)
206 EDRData *edrData = device->pendingDataList->data;
207 res = CAEDRSendData(device->socketFD, edrData->data,
208 edrData->dataLength);
209 if (CA_STATUS_OK != res)
211 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send pending data [%s]",
212 device->remoteAddress);
214 // Remove all the data from pending list
215 CADestroyEDRDataList(&device->pendingDataList);
219 // Remove the data which send from pending list
220 CARemoveEDRDataFromList(&device->pendingDataList);
222 ca_mutex_unlock(g_edrDeviceListMutex);
226 case BT_SOCKET_DISCONNECTED:
228 ca_mutex_lock(g_edrDeviceListMutex);
229 CARemoveEDRDeviceFromList(&g_edrDeviceList, connection->remote_address);
230 ca_mutex_unlock(g_edrDeviceListMutex);
235 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
239 void CAEDRDeviceDiscoveryCallback(int result, bt_adapter_device_discovery_state_e state,
240 bt_adapter_device_discovery_info_s *discoveryInfo, void *userData)
242 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
244 EDRDevice *device = NULL;
246 if (BT_ERROR_NONE != result)
248 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Received bad state!, error num [%x]",
255 case BT_ADAPTER_DEVICE_DISCOVERY_STARTED:
257 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Discovery started!");
261 case BT_ADAPTER_DEVICE_DISCOVERY_FINISHED:
263 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Discovery finished!");
267 case BT_ADAPTER_DEVICE_DISCOVERY_FOUND:
269 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Device discovered [%s]!",
270 discoveryInfo->remote_name);
271 if (true == CAEDRIsServiceSupported((const char **)discoveryInfo->service_uuid,
272 discoveryInfo->service_count,
275 // Check if the deivce is already in the list
276 ca_mutex_lock(g_edrDeviceListMutex);
277 if (CA_STATUS_OK == CAGetEDRDevice(g_edrDeviceList,
278 discoveryInfo->remote_address, &device))
282 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
283 ca_mutex_unlock(g_edrDeviceListMutex);
286 device->serviceSearched = true;
287 ca_mutex_unlock(g_edrDeviceListMutex);
291 // Create the deviceinfo and add to list
292 CAResult_t res = CACreateAndAddToDeviceList(&g_edrDeviceList,
293 discoveryInfo->remote_address, OIC_EDR_SERVICE_ID, &device);
294 if (CA_STATUS_OK != res)
296 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add device to list!");
297 ca_mutex_unlock(g_edrDeviceListMutex);
303 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
304 ca_mutex_unlock(g_edrDeviceListMutex);
307 device->serviceSearched = true;
308 // Signal the wait to send the data.
309 ca_cond_signal(g_deviceDescCond);
310 ca_mutex_unlock(g_edrDeviceListMutex);
314 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Device does not support OIC service!");
320 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
323 void CAEDRServiceSearchedCallback(int32_t result,
324 bt_device_sdp_info_s *sdpInfo,void *userData)
326 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
330 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "SDP info is null!");
334 ca_mutex_lock(g_edrDeviceListMutex);
336 EDRDevice *device = NULL;
337 CAResult_t res = CAGetEDRDevice(g_edrDeviceList, sdpInfo->remote_address, &device);
338 if (CA_STATUS_OK == res && NULL != device)
340 if (device->serviceSearched)
342 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Service is already searched for this device!");
343 ca_mutex_unlock(g_edrDeviceListMutex);
347 if (true == CAEDRIsServiceSupported((const char **)sdpInfo->service_uuid,
348 sdpInfo->service_count, OIC_EDR_SERVICE_ID))
350 device->serviceSearched = true;
351 res = CAEDRClientConnect(sdpInfo->remote_address, OIC_EDR_SERVICE_ID);
352 if (CA_STATUS_OK != res)
354 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make rfcomm connection!");
356 // Remove the device from device list
357 CARemoveEDRDeviceFromList(&g_edrDeviceList, sdpInfo->remote_address);
362 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Device does not contain OIC service!");
364 // Remove device from list as it does not support OIC service
365 CARemoveEDRDeviceFromList(&g_edrDeviceList, sdpInfo->remote_address);
369 ca_mutex_unlock(g_edrDeviceListMutex);
371 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
374 CAResult_t CAEDRStartDeviceDiscovery(void)
376 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
379 bool isDiscoveryStarted = false;
381 // Check the device discovery state
382 bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
383 if (BT_ERROR_NONE != err)
385 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
387 return CA_STATUS_FAILED;
390 //Start device discovery if its not started
391 if (false == isDiscoveryStarted)
393 err = bt_adapter_start_device_discovery();
394 if (BT_ERROR_NONE != err)
396 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Device discovery failed!, error num [%x]",
398 return CA_STATUS_FAILED;
402 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
406 CAResult_t CAEDRStopServiceSearch(void)
408 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
410 bt_error_e err = bt_device_cancel_service_search();
411 // Stop ongoing service search
412 if (BT_ERROR_NONE != err)
414 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
416 return CA_STATUS_FAILED;
419 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
423 CAResult_t CAEDRStopDeviceDiscovery(void)
425 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
427 bool isDiscoveryStarted = false;
428 bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
429 // Check the device discovery state
430 if (BT_ERROR_NONE != err)
432 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
434 return CA_STATUS_FAILED;
437 //stop the device discovery process
438 if (true == isDiscoveryStarted)
440 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Stopping the device search process");
441 if (BT_ERROR_NONE != (err = bt_adapter_stop_device_discovery()))
443 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to stop discovery!, error num [%x]",
448 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
452 CAResult_t CAEDRStartServiceSearch(const char *remoteAddress)
454 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
457 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
458 if (!remoteAddress[0])
460 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Remote address is empty!");
461 return CA_STATUS_INVALID_PARAM;
464 bt_error_e err = bt_device_start_service_search(remoteAddress);
465 // Start searching for OIC service
466 if (BT_ERROR_NONE != err)
468 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
470 return CA_STATUS_FAILED;
473 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
477 CAResult_t CAEDRClientSetCallbacks(void)
479 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
481 g_isStopping = false;
482 // Register for discovery and rfcomm socket connection callbacks
483 bt_adapter_set_device_discovery_state_changed_cb(CAEDRDeviceDiscoveryCallback, NULL);
484 bt_device_set_service_searched_cb(CAEDRServiceSearchedCallback, NULL);
485 bt_socket_set_connection_state_changed_cb(CAEDRSocketConnectionStateCallback, NULL);
486 bt_socket_set_data_received_cb(CAEDRDataRecvCallback, NULL);
488 // Start device discovery
489 CAResult_t result = CAEDRStartDeviceDiscovery();
490 if(CA_STATUS_OK != result)
492 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Failed to Start Device discovery");
493 return CA_STATUS_FAILED;
496 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
501 void CAEDRClientUnsetCallbacks(void)
503 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
505 // Stop service search
506 CAEDRStopServiceSearch();
508 // Stop the device discovery process
509 CAEDRStopDeviceDiscovery();
511 // Signal the conditional wait for discovery of devices.
513 ca_cond_signal(g_deviceDescCond);
515 // reset bluetooth adapter callbacks
516 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Resetting the callbacks");
517 bt_adapter_unset_device_discovery_state_changed_cb();
518 bt_device_unset_service_searched_cb();
519 bt_socket_unset_connection_state_changed_cb();
520 bt_socket_unset_data_received_cb();
522 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
525 void CAEDRManagerInitializeMutex(void)
527 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
529 if (!g_edrDeviceListMutex)
531 g_edrDeviceListMutex = ca_mutex_new();
534 if (!g_deviceDescCond)
536 g_deviceDescCond = ca_cond_new();
539 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
542 void CAEDRManagerTerminateMutex(void)
544 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
546 if (g_edrDeviceListMutex)
548 ca_mutex_free(g_edrDeviceListMutex);
549 g_edrDeviceListMutex = NULL;
552 if (g_deviceDescCond)
554 ca_cond_free(g_deviceDescCond);
555 g_deviceDescCond = NULL;
557 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
560 void CAEDRInitializeClient(ca_thread_pool_t handle)
562 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
563 CAEDRManagerInitializeMutex();
564 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
567 void CAEDRClientTerminate()
569 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
571 // Free EDRDevices list
572 if (g_edrDeviceListMutex)
574 ca_mutex_lock(g_edrDeviceListMutex);
575 CADestroyEDRDeviceList(&g_edrDeviceList);
576 ca_mutex_unlock(g_edrDeviceListMutex);
580 CAEDRManagerTerminateMutex();
581 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
584 void CAEDRClientDisconnectAll(void)
586 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
588 ca_mutex_lock(g_edrDeviceListMutex);
590 EDRDeviceList *cur = g_edrDeviceList;
593 EDRDevice *device = cur->device;
596 if (device && 0 <= device->socketFD)
598 CAResult_t result = CAEDRClientDisconnect(device->socketFD);
599 if (CA_STATUS_OK != result)
601 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to disconnect with client :%s",
602 device->remoteAddress);
605 device->socketFD = -1;
609 ca_mutex_unlock(g_edrDeviceListMutex);
611 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
615 CAResult_t CAEDRClientSendUnicastData(const char *remoteAddress,
619 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
621 EDRDevice *device = NULL;
624 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
625 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
629 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
630 return CA_STATUS_INVALID_PARAM;
633 // Check the connection existence with remote device
634 ca_mutex_lock(g_edrDeviceListMutex);
635 CAResult_t result = CAGetEDRDevice(g_edrDeviceList, remoteAddress, &device);
636 if (CA_STATUS_OK != result)
638 // Create new device and add to list
639 result = CACreateAndAddToDeviceList(&g_edrDeviceList, remoteAddress,
640 OIC_EDR_SERVICE_ID, &device);
641 if (CA_STATUS_OK != result)
643 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed create device and add to list!");
645 ca_mutex_unlock(g_edrDeviceListMutex);
646 return CA_STATUS_FAILED;
649 // Start the OIC service search newly created device
650 result = CAEDRStartServiceSearch(remoteAddress);
651 if (CA_STATUS_OK != result)
653 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to initiate service search!");
655 // Remove device from list
656 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
658 ca_mutex_unlock(g_edrDeviceListMutex);
659 return CA_STATUS_FAILED;
665 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
666 // Remove device from list
667 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
669 ca_mutex_unlock(g_edrDeviceListMutex);
670 return CA_STATUS_FAILED;
673 ca_mutex_unlock(g_edrDeviceListMutex);
675 if (-1 == device->socketFD)
677 // Adding to pending list
678 result = CAAddEDRDataToList(&device->pendingDataList, data,
680 if (CA_STATUS_OK != result)
682 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list!");
684 //Remove device from list
685 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
686 return CA_STATUS_FAILED;
689 // Make a rfcomm connection with remote BT Device
690 if (device->serviceSearched &&
691 CA_STATUS_OK != CAEDRClientConnect(remoteAddress, OIC_EDR_SERVICE_ID))
693 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection!");
695 //Remove device from list
696 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
697 return CA_STATUS_FAILED;
702 result = CAEDRSendData(device->socketFD, data, dataLength);
703 if (CA_STATUS_OK != result)
705 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to send data!");
706 return CA_STATUS_FAILED;
710 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
714 CAResult_t CAEDRClientSendMulticastData(const uint8_t *data,
717 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
720 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
724 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
725 return CA_STATUS_INVALID_PARAM;
728 // Send the packet to all OIC devices
729 ca_mutex_lock(g_edrDeviceListMutex);
731 // Check if any device is discovered.
732 if (NULL == g_edrDeviceList)
734 // Wait for BT devices to be discovered.
736 // Number of times to wait for discovery to complete.
737 int const RETRIES = 5;
739 uint64_t const TIMEOUT = 2 * MICROSECS_PER_SEC; // Microseconds
741 bool devicesDiscovered = false;
742 for (size_t i = 0; NULL == g_edrDeviceList && i < RETRIES && !g_isStopping;
745 if (ca_cond_wait_for(g_deviceDescCond, g_edrDeviceListMutex,
748 devicesDiscovered = true;
751 if (!devicesDiscovered || g_isStopping)
757 EDRDeviceList *curList = g_edrDeviceList;
758 CAResult_t result = CA_STATUS_FAILED;
759 while (curList != NULL)
761 EDRDevice *device = curList->device;
762 curList = curList->next;
766 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
770 if (-1 == device->socketFD)
772 // Check if the device service search is finished
773 if (false == device->serviceSearched)
775 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Device services are still unknown!");
779 // Adding to pendding list
780 result = CAAddEDRDataToList(&device->pendingDataList, data,
782 if (CA_STATUS_OK != result)
784 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list !");
788 // Make a rfcomm connection with remote BT Device
789 result = CAEDRClientConnect(device->remoteAddress, device->serviceUUID);
790 if (CA_STATUS_OK != result)
792 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection !");
794 //Remove the data which added to pending list
795 CARemoveEDRDataFromList(&device->pendingDataList);
801 result = CAEDRSendData(device->socketFD, data, dataLength);
802 if (CA_STATUS_OK != result)
804 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send data to [%s] !",
805 device->remoteAddress);
810 ca_mutex_unlock(g_edrDeviceListMutex);
812 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
816 CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID)
818 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
820 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
821 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "Service UUID is null");
823 size_t addressLen = strlen(remoteAddress);
824 if (0 == addressLen || CA_MACADDR_SIZE - 1 != addressLen)
826 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Invalid remote address");
827 return CA_STATUS_INVALID_PARAM;
832 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Empty service uuid");
833 return CA_STATUS_INVALID_PARAM;
836 bt_error_e err = bt_socket_connect_rfcomm(remoteAddress, serviceUUID);
837 if (BT_ERROR_NONE != err)
839 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
840 "Failed to connect!, address [%s] error num [%x]",
842 return CA_STATUS_FAILED;
845 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
849 CAResult_t CAEDRClientDisconnect(const int32_t clientID)
851 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
856 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: negative client id");
857 return CA_STATUS_INVALID_PARAM;
860 bt_error_e err = bt_socket_disconnect_rfcomm(clientID);
861 if (BT_ERROR_NONE != err)
863 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed close rfcomm client socket!, error num [%x]",
865 return CA_STATUS_FAILED;
868 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
872 void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData)
874 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
876 EDRDevice *device = NULL;
878 if (NULL == data || 0 >= data->data_size)
880 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Data is null!");
884 // Get EDR device from list
885 ca_mutex_lock(g_edrDeviceListMutex);
886 CAResult_t result = CAGetEDRDeviceBySocketId(g_edrDeviceList, data->socket_fd, &device);
887 if (CA_STATUS_OK != result)
889 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Could not find the device!");
891 ca_mutex_unlock(g_edrDeviceListMutex);
894 ca_mutex_unlock(g_edrDeviceListMutex);
898 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
902 uint32_t sentLength = 0;
904 g_edrPacketReceivedCallback(device->remoteAddress,
905 (uint8_t *) data->data,
906 (uint32_t) data->data_size,
909 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");