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 * @fn CAEDRManagerInitializeMutex
60 * @brief This function creates mutex.
62 static void CAEDRManagerInitializeMutex(void);
65 * @fn CAEDRManagerTerminateMutex
66 * @brief This function frees mutex.
68 static void CAEDRManagerTerminateMutex(void);
71 * @fn CAEDRDataRecvCallback
72 * @brief 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 * @brief This function starts device discovery.
80 static CAResult_t CAEDRStartDeviceDiscovery(void);
83 * @fn CAEDRStopServiceSearch
84 * @brief This function stops any ongoing service sevice search.
86 static CAResult_t CAEDRStopServiceSearch(void);
89 * @fn CAEDRStopDeviceDiscovery
90 * @brief This function stops device discovery.
92 static CAResult_t CAEDRStopDeviceDiscovery(void);
95 * @fn CAEDRStartServiceSearch
96 * @brief This function searches for OIC service for remote Bluetooth device.
98 static CAResult_t CAEDRStartServiceSearch(const char *remoteAddress);
101 * @fn CAEDRDeviceDiscoveryCallback
102 * @brief This callback is registered to recieve all bluetooth nearby devices when device
105 static void CAEDRDeviceDiscoveryCallback(int result,
106 bt_adapter_device_discovery_state_e state,
107 bt_adapter_device_discovery_info_s *discoveryInfo,
111 * @fn CAEDRServiceSearchedCallback
112 * @brief This callback is registered to recieve all the services remote bluetooth device supports
113 * when service search initiated.
115 static void CAEDRServiceSearchedCallback(int result, bt_device_sdp_info_s *sdpInfo,
119 * @fn CAEDRSocketConnectionStateCallback
120 * @brief This callback is registered to receive bluetooth RFCOMM connection state changes.
122 static void CAEDRSocketConnectionStateCallback(int result,
123 bt_socket_connection_state_e state,
124 bt_socket_connection_s *connection, void *userData);
127 * @fn CAEDRClientConnect
128 * @brief Establishes RFCOMM connection with remote bluetooth device
130 static CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID);
133 * @fn CAEDRClientDisconnect
134 * @brief Disconnect RFCOMM client socket connection
136 static CAResult_t CAEDRClientDisconnect(const int32_t clientID);
138 void CAEDRSetPacketReceivedCallback(CAEDRDataReceivedCallback packetReceivedCallback)
140 g_edrPacketReceivedCallback = packetReceivedCallback;
143 void CAEDRSocketConnectionStateCallback(int result, bt_socket_connection_state_e state,
144 bt_socket_connection_s *connection, void *userData)
146 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
148 EDRDevice *device = NULL;
150 if (BT_ERROR_NONE != result || NULL == connection)
152 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Invalid connection state!, error num [%x]",
159 case BT_SOCKET_CONNECTED:
161 ca_mutex_lock(g_edrDeviceListMutex);
162 CAResult_t res = CAGetEDRDevice(g_edrDeviceList, connection->remote_address,
164 if (CA_STATUS_OK != res)
166 // Create the deviceinfo and add to list
167 res = CACreateAndAddToDeviceList(&g_edrDeviceList,
168 connection->remote_address, OIC_EDR_SERVICE_ID, &device);
169 if (CA_STATUS_OK != res)
171 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed add device to list ret[%d]", res);
172 ca_mutex_unlock(g_edrDeviceListMutex);
178 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
179 ca_mutex_unlock(g_edrDeviceListMutex);
183 device->socketFD = connection->socket_fd;
184 ca_mutex_unlock(g_edrDeviceListMutex);
190 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
191 ca_mutex_unlock(g_edrDeviceListMutex);
194 device->socketFD = connection->socket_fd;
195 while (device->pendingDataList)
197 uint32_t sentData = 0;
198 EDRData *edrData = device->pendingDataList->data;
199 res = CAEDRSendData(device->socketFD, edrData->data,
200 edrData->dataLength, &sentData);
201 if (CA_STATUS_OK != res)
203 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send pending data [%s]",
204 device->remoteAddress);
206 // Remove all the data from pending list
207 CADestroyEDRDataList(&device->pendingDataList);
211 // Remove the data which send from pending list
212 CARemoveEDRDataFromList(&device->pendingDataList);
214 ca_mutex_unlock(g_edrDeviceListMutex);
218 case BT_SOCKET_DISCONNECTED:
220 ca_mutex_lock(g_edrDeviceListMutex);
221 CARemoveEDRDeviceFromList(&g_edrDeviceList, connection->remote_address);
222 ca_mutex_unlock(g_edrDeviceListMutex);
227 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
231 void CAEDRDeviceDiscoveryCallback(int result, bt_adapter_device_discovery_state_e state,
232 bt_adapter_device_discovery_info_s *discoveryInfo, void *userData)
234 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
236 EDRDevice *device = NULL;
238 if (BT_ERROR_NONE != result)
240 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Received bad state!, error num [%x]",
247 case BT_ADAPTER_DEVICE_DISCOVERY_STARTED:
249 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Discovery started!");
253 case BT_ADAPTER_DEVICE_DISCOVERY_FINISHED:
255 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Discovery finished!");
259 case BT_ADAPTER_DEVICE_DISCOVERY_FOUND:
261 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Device discovered [%s]!",
262 discoveryInfo->remote_name);
263 if (true == CAEDRIsServiceSupported((const char **)discoveryInfo->service_uuid,
264 discoveryInfo->service_count,
267 // Check if the deivce is already in the list
268 ca_mutex_lock(g_edrDeviceListMutex);
269 if (CA_STATUS_OK == CAGetEDRDevice(g_edrDeviceList,
270 discoveryInfo->remote_address, &device))
274 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
275 ca_mutex_unlock(g_edrDeviceListMutex);
278 device->serviceSearched = true;
279 ca_mutex_unlock(g_edrDeviceListMutex);
283 // Create the deviceinfo and add to list
284 CAResult_t res = CACreateAndAddToDeviceList(&g_edrDeviceList,
285 discoveryInfo->remote_address, OIC_EDR_SERVICE_ID, &device);
286 if (CA_STATUS_OK != res)
288 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add device to list!");
289 ca_mutex_unlock(g_edrDeviceListMutex);
295 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
296 ca_mutex_unlock(g_edrDeviceListMutex);
299 device->serviceSearched = true;
300 ca_mutex_unlock(g_edrDeviceListMutex);
304 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Device does not support OIC service!");
310 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
313 void CAEDRServiceSearchedCallback(int32_t result,
314 bt_device_sdp_info_s *sdpInfo,void *userData)
316 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
320 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "SDP info is null!");
324 ca_mutex_lock(g_edrDeviceListMutex);
326 EDRDevice *device = NULL;
327 CAResult_t res = CAGetEDRDevice(g_edrDeviceList, sdpInfo->remote_address, &device);
328 if (CA_STATUS_OK == res && NULL != device)
330 if (device->serviceSearched)
332 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Service is already searched for this device!");
333 ca_mutex_unlock(g_edrDeviceListMutex);
337 if (true == CAEDRIsServiceSupported((const char **)sdpInfo->service_uuid,
338 sdpInfo->service_count, OIC_EDR_SERVICE_ID))
340 device->serviceSearched = true;
341 res = CAEDRClientConnect(sdpInfo->remote_address, OIC_EDR_SERVICE_ID);
342 if (CA_STATUS_OK != res)
344 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make rfcomm connection!");
346 // Remove the device from device list
347 CARemoveEDRDeviceFromList(&g_edrDeviceList, sdpInfo->remote_address);
352 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Device does not contain OIC service!");
354 // Remove device from list as it does not support OIC service
355 CARemoveEDRDeviceFromList(&g_edrDeviceList, sdpInfo->remote_address);
359 ca_mutex_unlock(g_edrDeviceListMutex);
361 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
364 CAResult_t CAEDRStartDeviceDiscovery(void)
366 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
369 bool isDiscoveryStarted = false;
371 // Check the device discovery state
372 bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
373 if (BT_ERROR_NONE != err)
375 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
377 return CA_STATUS_FAILED;
380 //Start device discovery if its not started
381 if (false == isDiscoveryStarted)
383 err = bt_adapter_start_device_discovery();
384 if (BT_ERROR_NONE != err)
386 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Device discovery failed!, error num [%x]",
388 return CA_STATUS_FAILED;
392 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
396 CAResult_t CAEDRStopServiceSearch(void)
398 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
400 bt_error_e err = bt_device_cancel_service_search();
401 // Stop ongoing service search
402 if (BT_ERROR_NONE != err)
404 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
406 return CA_STATUS_FAILED;
409 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
413 CAResult_t CAEDRStopDeviceDiscovery(void)
415 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
417 bool isDiscoveryStarted = false;
418 bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
419 // Check the device discovery state
420 if (BT_ERROR_NONE != err)
422 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
424 return CA_STATUS_FAILED;
427 //stop the device discovery process
428 if (true == isDiscoveryStarted)
430 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Stopping the device search process");
431 if (BT_ERROR_NONE != (err = bt_adapter_stop_device_discovery()))
433 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to stop discovery!, error num [%x]",
438 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
442 CAResult_t CAEDRStartServiceSearch(const char *remoteAddress)
444 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
447 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
448 if (!remoteAddress[0])
450 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Remote address is empty!");
451 return CA_STATUS_INVALID_PARAM;
454 bt_error_e err = bt_device_start_service_search(remoteAddress);
455 // Start searching for OIC service
456 if (BT_ERROR_NONE != err)
458 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
460 return CA_STATUS_FAILED;
463 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
467 CAResult_t CAEDRClientSetCallbacks(void)
469 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
471 // Register for discovery and rfcomm socket connection callbacks
472 bt_adapter_set_device_discovery_state_changed_cb(CAEDRDeviceDiscoveryCallback, NULL);
473 bt_device_set_service_searched_cb(CAEDRServiceSearchedCallback, NULL);
474 bt_socket_set_connection_state_changed_cb(CAEDRSocketConnectionStateCallback, NULL);
475 bt_socket_set_data_received_cb(CAEDRDataRecvCallback, NULL);
477 // Start device discovery
478 CAResult_t result = CAEDRStartDeviceDiscovery();
479 if(CA_STATUS_OK != result)
481 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Failed to Start Device discovery");
482 return CA_STATUS_FAILED;
485 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
490 void CAEDRClientUnsetCallbacks(void)
492 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
494 // Stop service search
495 CAEDRStopServiceSearch();
497 // Stop the device discovery process
498 CAEDRStopDeviceDiscovery();
500 // reset bluetooth adapter callbacks
501 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Resetting the callbacks");
502 bt_adapter_unset_device_discovery_state_changed_cb();
503 bt_device_unset_service_searched_cb();
504 bt_socket_unset_connection_state_changed_cb();
505 bt_socket_unset_data_received_cb();
507 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
510 void CAEDRManagerInitializeMutex(void)
512 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
514 if (!g_edrDeviceListMutex)
516 g_edrDeviceListMutex = ca_mutex_new();
519 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
522 void CAEDRManagerTerminateMutex(void)
524 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
526 if (g_edrDeviceListMutex)
528 ca_mutex_free(g_edrDeviceListMutex);
529 g_edrDeviceListMutex = NULL;
532 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
535 void CAEDRInitializeClient(ca_thread_pool_t handle)
537 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
538 CAEDRManagerInitializeMutex();
539 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
542 void CAEDRClientTerminate()
544 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
546 // Free EDRDevices list
547 if (g_edrDeviceListMutex)
549 ca_mutex_lock(g_edrDeviceListMutex);
550 CADestroyEDRDeviceList(&g_edrDeviceList);
551 ca_mutex_unlock(g_edrDeviceListMutex);
555 CAEDRManagerTerminateMutex();
556 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
559 void CAEDRClientDisconnectAll(void)
561 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
563 ca_mutex_lock(g_edrDeviceListMutex);
565 EDRDeviceList *cur = g_edrDeviceList;
568 EDRDevice *device = cur->device;
571 if (device && 0 <= device->socketFD)
573 CAResult_t result = CAEDRClientDisconnect(device->socketFD);
574 if (CA_STATUS_OK != result)
576 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to disconnect with client :%s",
577 device->remoteAddress);
580 device->socketFD = -1;
584 ca_mutex_unlock(g_edrDeviceListMutex);
586 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
590 CAResult_t CAEDRClientSendUnicastData(const char *remoteAddress, const char *serviceUUID,
591 const void *data, uint32_t dataLength, uint32_t *sentLength)
593 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
595 EDRDevice *device = NULL;
598 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
599 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "service UUID is null");
600 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
601 VERIFY_NON_NULL(sentLength, EDR_ADAPTER_TAG, "Sent data length holder is null");
605 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
606 return CA_STATUS_INVALID_PARAM;
609 // Check the connection existence with remote device
610 ca_mutex_lock(g_edrDeviceListMutex);
611 CAResult_t result = CAGetEDRDevice(g_edrDeviceList, remoteAddress, &device);
612 if (CA_STATUS_OK != result)
614 // Create new device and add to list
615 result = CACreateAndAddToDeviceList(&g_edrDeviceList, remoteAddress,
616 OIC_EDR_SERVICE_ID, &device);
617 if (CA_STATUS_OK != result)
619 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed create device and add to list!");
621 ca_mutex_unlock(g_edrDeviceListMutex);
622 return CA_STATUS_FAILED;
625 // Start the OIC service search newly created device
626 result = CAEDRStartServiceSearch(remoteAddress);
627 if (CA_STATUS_OK != result)
629 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to initiate service search!");
631 // Remove device from list
632 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
634 ca_mutex_unlock(g_edrDeviceListMutex);
635 return CA_STATUS_FAILED;
641 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
642 // Remove device from list
643 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
645 ca_mutex_unlock(g_edrDeviceListMutex);
646 return CA_STATUS_FAILED;
649 ca_mutex_unlock(g_edrDeviceListMutex);
651 if (-1 == device->socketFD)
653 // Adding to pending list
654 result = CAAddEDRDataToList(&device->pendingDataList, data,
656 if (CA_STATUS_OK != result)
658 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list!");
660 //Remove device from list
661 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
662 return CA_STATUS_FAILED;
665 // Make a rfcomm connection with remote BT Device
666 if (device->serviceSearched &&
667 CA_STATUS_OK != CAEDRClientConnect(remoteAddress, serviceUUID))
669 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection!");
671 //Remove device from list
672 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
673 return CA_STATUS_FAILED;
675 *sentLength = dataLength;
679 result = CAEDRSendData(device->socketFD, data, dataLength, sentLength);
680 if (CA_STATUS_OK != result)
682 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to send data!");
683 return CA_STATUS_FAILED;
687 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
691 CAResult_t CAEDRClientSendMulticastData(const char *serviceUUID, const void *data,
692 uint32_t dataLength, uint32_t *sentLength)
694 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
697 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "service UUID is null");
698 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
699 VERIFY_NON_NULL(sentLength, EDR_ADAPTER_TAG, "Sent data length holder is null");
703 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
704 return CA_STATUS_INVALID_PARAM;
707 *sentLength = dataLength;
709 // Send the packet to all OIC devices
710 ca_mutex_lock(g_edrDeviceListMutex);
711 EDRDeviceList *curList = g_edrDeviceList;
712 CAResult_t result = CA_STATUS_FAILED;
713 while (curList != NULL)
715 EDRDevice *device = curList->device;
716 curList = curList->next;
720 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
724 if (-1 == device->socketFD)
726 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN1");
727 // Check if the device service search is finished
728 if (false == device->serviceSearched)
730 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Device services are still unknown!");
734 // Adding to pendding list
735 result = CAAddEDRDataToList(&device->pendingDataList, data,
737 if (CA_STATUS_OK != result)
739 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list !");
743 // Make a rfcomm connection with remote BT Device
744 result = CAEDRClientConnect(device->remoteAddress, device->serviceUUID);
745 if (CA_STATUS_OK != result)
747 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection !");
749 //Remove the data which added to pending list
750 CARemoveEDRDataFromList(&device->pendingDataList);
753 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN2");
757 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN3");
758 result = CAEDRSendData(device->socketFD, data, dataLength, sentLength);
759 if (CA_STATUS_OK != result)
761 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send data to [%s] !",
762 device->remoteAddress);
764 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN4");
767 ca_mutex_unlock(g_edrDeviceListMutex);
769 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
773 CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID)
775 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
777 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
778 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "Service UUID is null");
780 size_t addressLen = strlen(remoteAddress);
781 if (0 == addressLen || CA_MACADDR_SIZE - 1 != addressLen)
783 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Invalid remote address");
784 return CA_STATUS_INVALID_PARAM;
789 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Empty service uuid");
790 return CA_STATUS_INVALID_PARAM;
793 bt_error_e err = bt_socket_connect_rfcomm(remoteAddress, serviceUUID);
794 if (BT_ERROR_NONE != err)
796 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
797 "Failed to connect!, address [%s] error num [%x]",
799 return CA_STATUS_FAILED;
802 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
806 CAResult_t CAEDRClientDisconnect(const int32_t clientID)
808 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
813 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: negative client id");
814 return CA_STATUS_INVALID_PARAM;
817 bt_error_e err = bt_socket_disconnect_rfcomm(clientID);
818 if (BT_ERROR_NONE != err)
820 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed close rfcomm client socket!, error num [%x]",
822 return CA_STATUS_FAILED;
825 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
829 void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData)
831 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
833 EDRDevice *device = NULL;
835 if (NULL == data || 0 >= data->data_size)
837 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Data is null!");
841 // Get EDR device from list
842 ca_mutex_lock(g_edrDeviceListMutex);
843 CAResult_t result = CAGetEDRDeviceBySocketId(g_edrDeviceList, data->socket_fd, &device);
844 if (CA_STATUS_OK != result)
846 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Could not find the device!");
848 ca_mutex_unlock(g_edrDeviceListMutex);
851 ca_mutex_unlock(g_edrDeviceListMutex);
855 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
859 uint32_t sentLength = 0;
861 g_edrPacketReceivedCallback(device->remoteAddress, data->data,
862 (uint32_t)data->data_size, &sentLength);
864 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");