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 * @var g_edrDeviceListMutex
42 * @brief Mutex to synchronize the access to Bluetooth device information list.
44 static ca_mutex g_edrDeviceListMutex = NULL;
47 * @var g_edrDeviceList
48 * @brief Peer Bluetooth device information list.
50 static EDRDeviceList *g_edrDeviceList = NULL;
53 * @var gEDRNetworkChangeCallback
54 * @brief Maintains the callback to be notified when data received from remote Bluetooth device
56 static CAEDRDataReceivedCallback g_edrPacketReceivedCallback = NULL;
59 * @var g_edrErrorHandler
60 * @brief Error callback to update error in EDR
62 static CAEDRErrorHandleCallback g_edrErrorHandler = NULL;
64 * @fn CAEDRManagerInitializeMutex
65 * @brief This function creates mutex.
67 static void CAEDRManagerInitializeMutex(void);
70 * @fn CAEDRManagerTerminateMutex
71 * @brief This function frees mutex.
73 static void CAEDRManagerTerminateMutex(void);
76 * @fn CAEDRDataRecvCallback
77 * @brief This callback is registered to recieve data on any open RFCOMM connection.
79 static void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData);
82 * @brief This function starts device discovery.
85 static CAResult_t CAEDRStartDeviceDiscovery(void);
88 * @fn CAEDRStopServiceSearch
89 * @brief This function stops any ongoing service sevice search.
91 static CAResult_t CAEDRStopServiceSearch(void);
94 * @fn CAEDRStopDeviceDiscovery
95 * @brief This function stops device discovery.
97 static CAResult_t CAEDRStopDeviceDiscovery(void);
100 * @fn CAEDRStartServiceSearch
101 * @brief This function searches for OIC service for remote Bluetooth device.
103 static CAResult_t CAEDRStartServiceSearch(const char *remoteAddress);
106 * @fn CAEDRDeviceDiscoveryCallback
107 * @brief This callback is registered to recieve all bluetooth nearby devices when device
110 static void CAEDRDeviceDiscoveryCallback(int result,
111 bt_adapter_device_discovery_state_e state,
112 bt_adapter_device_discovery_info_s *discoveryInfo,
116 * @fn CAEDRServiceSearchedCallback
117 * @brief This callback is registered to recieve all the services remote bluetooth device supports
118 * when service search initiated.
120 static void CAEDRServiceSearchedCallback(int result, bt_device_sdp_info_s *sdpInfo,
124 * @fn CAEDRSocketConnectionStateCallback
125 * @brief This callback is registered to receive bluetooth RFCOMM connection state changes.
127 static void CAEDRSocketConnectionStateCallback(int result,
128 bt_socket_connection_state_e state,
129 bt_socket_connection_s *connection, void *userData);
132 * @fn CAEDRClientConnect
133 * @brief Establishes RFCOMM connection with remote bluetooth device
135 static CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID);
138 * @fn CAEDRClientDisconnect
139 * @brief Disconnect RFCOMM client socket connection
141 static CAResult_t CAEDRClientDisconnect(const int32_t clientID);
143 void CAEDRSetPacketReceivedCallback(CAEDRDataReceivedCallback packetReceivedCallback)
145 g_edrPacketReceivedCallback = packetReceivedCallback;
148 void CAEDRSetErrorHandler(CAEDRErrorHandleCallback errorHandleCallback)
150 g_edrErrorHandler = errorHandleCallback;
153 void CAEDRSocketConnectionStateCallback(int result, bt_socket_connection_state_e state,
154 bt_socket_connection_s *connection, void *userData)
156 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
158 EDRDevice *device = NULL;
160 if (BT_ERROR_NONE != result || NULL == connection)
162 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Invalid connection state!, error num [%x]",
169 case BT_SOCKET_CONNECTED:
171 ca_mutex_lock(g_edrDeviceListMutex);
172 CAResult_t res = CAGetEDRDevice(g_edrDeviceList, connection->remote_address,
174 if (CA_STATUS_OK != res)
176 // Create the deviceinfo and add to list
177 res = CACreateAndAddToDeviceList(&g_edrDeviceList,
178 connection->remote_address, OIC_EDR_SERVICE_ID, &device);
179 if (CA_STATUS_OK != res)
181 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed add device to list ret[%d]", res);
182 ca_mutex_unlock(g_edrDeviceListMutex);
188 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
189 ca_mutex_unlock(g_edrDeviceListMutex);
193 device->socketFD = connection->socket_fd;
194 ca_mutex_unlock(g_edrDeviceListMutex);
200 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
201 ca_mutex_unlock(g_edrDeviceListMutex);
204 device->socketFD = connection->socket_fd;
205 while (device->pendingDataList)
207 uint32_t sentData = 0;
208 EDRData *edrData = device->pendingDataList->data;
209 res = CAEDRSendData(device->socketFD, edrData->data,
210 edrData->dataLength, &sentData);
211 if (CA_STATUS_OK != res)
213 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send pending data [%s]",
214 device->remoteAddress);
216 // Remove all the data from pending list
217 CADestroyEDRDataList(&device->pendingDataList);
221 // Remove the data which send from pending list
222 CARemoveEDRDataFromList(&device->pendingDataList);
224 ca_mutex_unlock(g_edrDeviceListMutex);
228 case BT_SOCKET_DISCONNECTED:
230 ca_mutex_lock(g_edrDeviceListMutex);
231 CARemoveEDRDeviceFromList(&g_edrDeviceList, connection->remote_address);
232 ca_mutex_unlock(g_edrDeviceListMutex);
237 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
241 void CAEDRDeviceDiscoveryCallback(int result, bt_adapter_device_discovery_state_e state,
242 bt_adapter_device_discovery_info_s *discoveryInfo, void *userData)
244 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
246 EDRDevice *device = NULL;
248 if (BT_ERROR_NONE != result)
250 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Received bad state!, error num [%x]",
257 case BT_ADAPTER_DEVICE_DISCOVERY_STARTED:
259 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Discovery started!");
263 case BT_ADAPTER_DEVICE_DISCOVERY_FINISHED:
265 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Discovery finished!");
269 case BT_ADAPTER_DEVICE_DISCOVERY_FOUND:
271 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Device discovered [%s]!",
272 discoveryInfo->remote_name);
273 if (true == CAEDRIsServiceSupported((const char **)discoveryInfo->service_uuid,
274 discoveryInfo->service_count,
277 // Check if the deivce is already in the list
278 ca_mutex_lock(g_edrDeviceListMutex);
279 if (CA_STATUS_OK == CAGetEDRDevice(g_edrDeviceList,
280 discoveryInfo->remote_address, &device))
284 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
285 ca_mutex_unlock(g_edrDeviceListMutex);
288 device->serviceSearched = true;
289 ca_mutex_unlock(g_edrDeviceListMutex);
293 // Create the deviceinfo and add to list
294 CAResult_t res = CACreateAndAddToDeviceList(&g_edrDeviceList,
295 discoveryInfo->remote_address, OIC_EDR_SERVICE_ID, &device);
296 if (CA_STATUS_OK != res)
298 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add device to list!");
299 ca_mutex_unlock(g_edrDeviceListMutex);
305 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
306 ca_mutex_unlock(g_edrDeviceListMutex);
309 device->serviceSearched = true;
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 // Register for discovery and rfcomm socket connection callbacks
482 bt_adapter_set_device_discovery_state_changed_cb(CAEDRDeviceDiscoveryCallback, NULL);
483 bt_device_set_service_searched_cb(CAEDRServiceSearchedCallback, NULL);
484 bt_socket_set_connection_state_changed_cb(CAEDRSocketConnectionStateCallback, NULL);
485 bt_socket_set_data_received_cb(CAEDRDataRecvCallback, NULL);
487 // Start device discovery
488 CAResult_t result = CAEDRStartDeviceDiscovery();
489 if(CA_STATUS_OK != result)
491 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Failed to Start Device discovery");
492 return CA_STATUS_FAILED;
495 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
500 void CAEDRClientUnsetCallbacks(void)
502 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
504 // Stop service search
505 CAEDRStopServiceSearch();
507 // Stop the device discovery process
508 CAEDRStopDeviceDiscovery();
510 // reset bluetooth adapter callbacks
511 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Resetting the callbacks");
512 bt_adapter_unset_device_discovery_state_changed_cb();
513 bt_device_unset_service_searched_cb();
514 bt_socket_unset_connection_state_changed_cb();
515 bt_socket_unset_data_received_cb();
517 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
520 void CAEDRManagerInitializeMutex(void)
522 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
524 if (!g_edrDeviceListMutex)
526 g_edrDeviceListMutex = ca_mutex_new();
529 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
532 void CAEDRManagerTerminateMutex(void)
534 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
536 if (g_edrDeviceListMutex)
538 ca_mutex_free(g_edrDeviceListMutex);
539 g_edrDeviceListMutex = NULL;
542 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
545 void CAEDRInitializeClient(ca_thread_pool_t handle)
547 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
548 CAEDRManagerInitializeMutex();
549 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
552 void CAEDRClientTerminate()
554 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
556 // Free EDRDevices list
557 if (g_edrDeviceListMutex)
559 ca_mutex_lock(g_edrDeviceListMutex);
560 CADestroyEDRDeviceList(&g_edrDeviceList);
561 ca_mutex_unlock(g_edrDeviceListMutex);
565 CAEDRManagerTerminateMutex();
566 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
569 void CAEDRClientDisconnectAll(void)
571 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
573 ca_mutex_lock(g_edrDeviceListMutex);
575 EDRDeviceList *cur = g_edrDeviceList;
578 EDRDevice *device = cur->device;
581 if (device && 0 <= device->socketFD)
583 CAResult_t result = CAEDRClientDisconnect(device->socketFD);
584 if (CA_STATUS_OK != result)
586 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to disconnect with client :%s",
587 device->remoteAddress);
590 device->socketFD = -1;
594 ca_mutex_unlock(g_edrDeviceListMutex);
596 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
600 CAResult_t CAEDRClientSendUnicastData(const char *remoteAddress, const char *serviceUUID,
601 const void *data, uint32_t dataLength, uint32_t *sentLength)
603 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
605 EDRDevice *device = NULL;
608 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
609 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "service UUID is null");
610 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
611 VERIFY_NON_NULL(sentLength, EDR_ADAPTER_TAG, "Sent data length holder is null");
615 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
616 return CA_STATUS_INVALID_PARAM;
619 // Check the connection existence with remote device
620 ca_mutex_lock(g_edrDeviceListMutex);
621 CAResult_t result = CAGetEDRDevice(g_edrDeviceList, remoteAddress, &device);
622 if (CA_STATUS_OK != result)
624 // Create new device and add to list
625 result = CACreateAndAddToDeviceList(&g_edrDeviceList, remoteAddress,
626 OIC_EDR_SERVICE_ID, &device);
627 if (CA_STATUS_OK != result)
629 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed create device and add to list!");
631 ca_mutex_unlock(g_edrDeviceListMutex);
632 return CA_STATUS_FAILED;
635 // Start the OIC service search newly created device
636 result = CAEDRStartServiceSearch(remoteAddress);
637 if (CA_STATUS_OK != result)
639 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to initiate service search!");
641 // Remove device from list
642 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
644 ca_mutex_unlock(g_edrDeviceListMutex);
645 return CA_STATUS_FAILED;
651 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
652 // Remove device from list
653 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
655 ca_mutex_unlock(g_edrDeviceListMutex);
656 return CA_STATUS_FAILED;
659 ca_mutex_unlock(g_edrDeviceListMutex);
661 if (-1 == device->socketFD)
663 // Adding to pending list
664 result = CAAddEDRDataToList(&device->pendingDataList, data,
666 if (CA_STATUS_OK != result)
668 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list!");
670 //Remove device from list
671 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
672 return CA_STATUS_FAILED;
675 // Make a rfcomm connection with remote BT Device
676 if (device->serviceSearched &&
677 CA_STATUS_OK != CAEDRClientConnect(remoteAddress, serviceUUID))
679 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection!");
681 //Remove device from list
682 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
683 return CA_STATUS_FAILED;
685 *sentLength = dataLength;
689 result = CAEDRSendData(device->socketFD, data, dataLength, sentLength);
690 if (CA_STATUS_OK != result)
692 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to send data!");
693 return CA_STATUS_FAILED;
697 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
701 CAResult_t CAEDRClientSendMulticastData(const char *serviceUUID, const void *data,
702 uint32_t dataLength, uint32_t *sentLength)
704 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
707 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "service UUID is null");
708 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
709 VERIFY_NON_NULL(sentLength, EDR_ADAPTER_TAG, "Sent data length holder is null");
713 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
714 return CA_STATUS_INVALID_PARAM;
717 *sentLength = dataLength;
719 // Send the packet to all OIC devices
720 ca_mutex_lock(g_edrDeviceListMutex);
721 EDRDeviceList *curList = g_edrDeviceList;
722 CAResult_t result = CA_STATUS_FAILED;
723 while (curList != NULL)
725 EDRDevice *device = curList->device;
726 curList = curList->next;
730 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
734 if (-1 == device->socketFD)
736 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN1");
737 // Check if the device service search is finished
738 if (false == device->serviceSearched)
740 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Device services are still unknown!");
744 // Adding to pendding list
745 result = CAAddEDRDataToList(&device->pendingDataList, data,
747 if (CA_STATUS_OK != result)
749 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list !");
753 // Make a rfcomm connection with remote BT Device
754 result = CAEDRClientConnect(device->remoteAddress, device->serviceUUID);
755 if (CA_STATUS_OK != result)
757 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection !");
759 //Remove the data which added to pending list
760 CARemoveEDRDataFromList(&device->pendingDataList);
763 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN2");
767 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN3");
768 result = CAEDRSendData(device->socketFD, data, dataLength, sentLength);
769 if (CA_STATUS_OK != result)
771 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send data to [%s] !",
772 device->remoteAddress);
774 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN4");
777 ca_mutex_unlock(g_edrDeviceListMutex);
779 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
783 CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID)
785 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
787 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
788 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "Service UUID is null");
790 size_t addressLen = strlen(remoteAddress);
791 if (0 == addressLen || CA_MACADDR_SIZE - 1 != addressLen)
793 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Invalid remote address");
794 return CA_STATUS_INVALID_PARAM;
799 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Empty service uuid");
800 return CA_STATUS_INVALID_PARAM;
803 bt_error_e err = bt_socket_connect_rfcomm(remoteAddress, serviceUUID);
804 if (BT_ERROR_NONE != err)
806 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
807 "Failed to connect!, address [%s] error num [%x]",
809 return CA_STATUS_FAILED;
812 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
816 CAResult_t CAEDRClientDisconnect(const int32_t clientID)
818 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
823 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: negative client id");
824 return CA_STATUS_INVALID_PARAM;
827 bt_error_e err = bt_socket_disconnect_rfcomm(clientID);
828 if (BT_ERROR_NONE != err)
830 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed close rfcomm client socket!, error num [%x]",
832 return CA_STATUS_FAILED;
835 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
839 void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData)
841 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
843 EDRDevice *device = NULL;
845 if (NULL == data || 0 >= data->data_size)
847 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Data is null!");
851 // Get EDR device from list
852 ca_mutex_lock(g_edrDeviceListMutex);
853 CAResult_t result = CAGetEDRDeviceBySocketId(g_edrDeviceList, data->socket_fd, &device);
854 if (CA_STATUS_OK != result)
856 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Could not find the device!");
858 ca_mutex_unlock(g_edrDeviceListMutex);
861 ca_mutex_unlock(g_edrDeviceListMutex);
865 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
869 uint32_t sentLength = 0;
871 g_edrPacketReceivedCallback(device->remoteAddress, data->data,
872 (uint32_t)data->data_size, &sentLength);
874 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");