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 * Mutex to synchronize the access to Bluetooth device information list.
45 static ca_mutex g_edrDeviceListMutex = NULL;
48 * Peer Bluetooth device information list.
50 static EDRDeviceList *g_edrDeviceList = NULL;
53 * Maintains the callback to be notified when data received from remote
56 static CAEDRDataReceivedCallback g_edrPacketReceivedCallback = NULL;
59 * Error callback to update error in EDR.
61 static CAEDRErrorHandleCallback g_edrErrorHandler = NULL;
64 * Pending multicast data list to be sent.
66 static u_arraylist_t *g_multicastDataList = NULL;
69 * Mutex to synchronize the access to Pending multicast data list.
71 static ca_mutex g_multicastDataListMutex = NULL;
74 * This function creates mutex.
76 static CAResult_t 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!");
264 ca_mutex_lock(g_multicastDataListMutex);
265 u_arraylist_destroy(g_multicastDataList);
266 g_multicastDataList = NULL;
267 ca_mutex_unlock(g_multicastDataListMutex);
271 case BT_ADAPTER_DEVICE_DISCOVERY_FOUND:
273 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Device discovered [%s]!",
274 discoveryInfo->remote_name);
275 if (true == CAEDRIsServiceSupported((const char **)discoveryInfo->service_uuid,
276 discoveryInfo->service_count,
279 // Check if the deivce is already in the list
280 ca_mutex_lock(g_edrDeviceListMutex);
281 if (CA_STATUS_OK == CAGetEDRDevice(g_edrDeviceList,
282 discoveryInfo->remote_address, &device))
286 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
287 ca_mutex_unlock(g_edrDeviceListMutex);
290 device->serviceSearched = true;
291 ca_mutex_unlock(g_edrDeviceListMutex);
295 // Create the deviceinfo and add to list
296 CAResult_t res = CACreateAndAddToDeviceList(&g_edrDeviceList,
297 discoveryInfo->remote_address, OIC_EDR_SERVICE_ID, &device);
298 if (CA_STATUS_OK != res)
300 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add device to list!");
301 ca_mutex_unlock(g_edrDeviceListMutex);
307 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
308 ca_mutex_unlock(g_edrDeviceListMutex);
312 int lengthData = u_arraylist_length(g_multicastDataList);
313 for(int len = 0; len < lengthData; len++)
315 // Adding to pending list
316 EDRData *multicastData =
317 (EDRData *)u_arraylist_get(g_multicastDataList, len);
318 if (NULL == multicastData)
320 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "multicastData is NULL");
323 result = CAAddEDRDataToList(&device->pendingDataList, multicastData->data,
324 multicastData->dataLength);
325 if (CA_STATUS_OK != result)
327 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
328 "Failed to add data to pending list[%d]", result);
334 result = CAEDRClientConnect(device->remoteAddress, device->serviceUUID);
335 if (CA_STATUS_OK != result)
337 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
338 "Failed to make RFCOMM connection[%d]", result);
340 //Remove the data which added to pending list
341 CARemoveEDRDataFromList(&device->pendingDataList);
344 device->serviceSearched = true;
345 ca_mutex_unlock(g_edrDeviceListMutex);
349 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Device does not support OIC service!");
355 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
358 void CAEDRServiceSearchedCallback(int32_t result,
359 bt_device_sdp_info_s *sdpInfo,void *userData)
361 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
365 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "SDP info is null!");
369 ca_mutex_lock(g_edrDeviceListMutex);
371 EDRDevice *device = NULL;
372 CAResult_t res = CAGetEDRDevice(g_edrDeviceList, sdpInfo->remote_address, &device);
373 if (CA_STATUS_OK == res && NULL != device)
375 if (device->serviceSearched)
377 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Service is already searched for this device!");
378 ca_mutex_unlock(g_edrDeviceListMutex);
382 if (true == CAEDRIsServiceSupported((const char **)sdpInfo->service_uuid,
383 sdpInfo->service_count, OIC_EDR_SERVICE_ID))
385 device->serviceSearched = true;
386 res = CAEDRClientConnect(sdpInfo->remote_address, OIC_EDR_SERVICE_ID);
387 if (CA_STATUS_OK != res)
389 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make rfcomm connection!");
391 // Remove the device from device list
392 CARemoveEDRDeviceFromList(&g_edrDeviceList, sdpInfo->remote_address);
397 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Device does not contain OIC service!");
399 // Remove device from list as it does not support OIC service
400 CARemoveEDRDeviceFromList(&g_edrDeviceList, sdpInfo->remote_address);
404 ca_mutex_unlock(g_edrDeviceListMutex);
406 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
409 CAResult_t CAEDRStartDeviceDiscovery(void)
411 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
414 bool isDiscoveryStarted = false;
416 // Check the device discovery state
417 bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
418 if (BT_ERROR_NONE != err)
420 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
422 return CA_STATUS_FAILED;
425 //Start device discovery if its not started
426 if (false == isDiscoveryStarted)
428 err = bt_adapter_start_device_discovery();
429 if (BT_ERROR_NONE != err)
431 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Device discovery failed!, error num [%x]",
433 return CA_STATUS_FAILED;
437 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
441 CAResult_t CAEDRStopServiceSearch(void)
443 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
445 bt_error_e err = bt_device_cancel_service_search();
446 // Stop ongoing service search
447 if (BT_ERROR_NONE != err)
449 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
451 return CA_STATUS_FAILED;
454 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
458 CAResult_t CAEDRStopDeviceDiscovery(void)
460 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
462 bool isDiscoveryStarted = false;
463 bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
464 // Check the device discovery state
465 if (BT_ERROR_NONE != err)
467 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
469 return CA_STATUS_FAILED;
472 //stop the device discovery process
473 if (true == isDiscoveryStarted)
475 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Stopping the device search process");
476 if (BT_ERROR_NONE != (err = bt_adapter_stop_device_discovery()))
478 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to stop discovery!, error num [%x]",
483 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
487 CAResult_t CAEDRStartServiceSearch(const char *remoteAddress)
489 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
492 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
493 if (!remoteAddress[0])
495 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Remote address is empty!");
496 return CA_STATUS_INVALID_PARAM;
499 bt_error_e err = bt_device_start_service_search(remoteAddress);
500 // Start searching for OIC service
501 if (BT_ERROR_NONE != err)
503 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
505 return CA_STATUS_FAILED;
508 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
512 CAResult_t CAEDRClientSetCallbacks(void)
514 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
516 // Register for discovery and rfcomm socket connection callbacks
517 bt_adapter_set_device_discovery_state_changed_cb(CAEDRDeviceDiscoveryCallback, NULL);
518 bt_device_set_service_searched_cb(CAEDRServiceSearchedCallback, NULL);
519 bt_socket_set_connection_state_changed_cb(CAEDRSocketConnectionStateCallback, NULL);
520 bt_socket_set_data_received_cb(CAEDRDataRecvCallback, NULL);
522 // Start device discovery
523 CAResult_t result = CAEDRStartDeviceDiscovery();
524 if(CA_STATUS_OK != result)
526 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Failed to Start Device discovery");
527 return CA_STATUS_FAILED;
530 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
535 void CAEDRClientUnsetCallbacks(void)
537 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
539 // Stop service search
540 CAEDRStopServiceSearch();
542 // Stop the device discovery process
543 CAEDRStopDeviceDiscovery();
545 // reset bluetooth adapter callbacks
546 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Resetting the callbacks");
547 bt_adapter_unset_device_discovery_state_changed_cb();
548 bt_device_unset_service_searched_cb();
549 bt_socket_unset_connection_state_changed_cb();
550 bt_socket_unset_data_received_cb();
552 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
555 CAResult_t CAEDRManagerInitializeMutex(void)
557 CAResult_t result = CA_STATUS_OK;
558 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
560 if (!g_edrDeviceListMutex)
562 g_edrDeviceListMutex = ca_mutex_new();
565 if (!g_multicastDataListMutex)
567 g_multicastDataListMutex = ca_mutex_new();
570 if (!g_edrDeviceListMutex || !g_multicastDataListMutex)
572 result = CA_STATUS_NOT_INITIALIZED;
575 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
580 void CAEDRManagerTerminateMutex(void)
582 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
584 if (g_edrDeviceListMutex)
586 ca_mutex_free(g_edrDeviceListMutex);
587 g_edrDeviceListMutex = NULL;
590 if (g_multicastDataListMutex)
592 ca_mutex_free(g_multicastDataListMutex);
593 g_multicastDataListMutex = NULL;
596 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
599 CAResult_t CAEDRClientInitialize()
601 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
602 CAResult_t result = CAEDRManagerInitializeMutex();
603 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
607 void CAEDRClientTerminate()
609 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
611 // Free EDRDevices list
612 if (g_edrDeviceListMutex)
614 ca_mutex_lock(g_edrDeviceListMutex);
615 CADestroyEDRDeviceList(&g_edrDeviceList);
616 ca_mutex_unlock(g_edrDeviceListMutex);
619 if (g_multicastDataListMutex)
621 ca_mutex_lock(g_multicastDataListMutex);
622 u_arraylist_destroy(g_multicastDataList);
623 g_multicastDataList = NULL;
624 ca_mutex_unlock(g_multicastDataListMutex);
628 CAEDRManagerTerminateMutex();
629 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
632 void CAEDRClientDisconnectAll(void)
634 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
636 ca_mutex_lock(g_edrDeviceListMutex);
638 EDRDeviceList *cur = g_edrDeviceList;
641 EDRDevice *device = cur->device;
644 if (device && 0 <= device->socketFD)
646 CAResult_t result = CAEDRClientDisconnect(device->socketFD);
647 if (CA_STATUS_OK != result)
649 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to disconnect with client :%s",
650 device->remoteAddress);
653 device->socketFD = -1;
657 ca_mutex_unlock(g_edrDeviceListMutex);
659 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
663 CAResult_t CAEDRClientSendUnicastData(const char *remoteAddress,
667 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
669 EDRDevice *device = NULL;
672 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
673 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
677 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
678 return CA_STATUS_INVALID_PARAM;
681 // Check the connection existence with remote device
682 ca_mutex_lock(g_edrDeviceListMutex);
683 CAResult_t result = CAGetEDRDevice(g_edrDeviceList, remoteAddress, &device);
684 if (CA_STATUS_OK != result)
686 // Create new device and add to list
687 result = CACreateAndAddToDeviceList(&g_edrDeviceList, remoteAddress,
688 OIC_EDR_SERVICE_ID, &device);
689 if (CA_STATUS_OK != result)
691 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed create device and add to list!");
693 ca_mutex_unlock(g_edrDeviceListMutex);
694 return CA_STATUS_FAILED;
697 // Start the OIC service search newly created device
698 result = CAEDRStartServiceSearch(remoteAddress);
699 if (CA_STATUS_OK != result)
701 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to initiate service search!");
703 // Remove device from list
704 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
706 ca_mutex_unlock(g_edrDeviceListMutex);
707 return CA_STATUS_FAILED;
713 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
714 // Remove device from list
715 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
717 ca_mutex_unlock(g_edrDeviceListMutex);
718 return CA_STATUS_FAILED;
721 ca_mutex_unlock(g_edrDeviceListMutex);
723 if (-1 == device->socketFD)
725 // Adding to pending list
726 result = CAAddEDRDataToList(&device->pendingDataList, data,
728 if (CA_STATUS_OK != result)
730 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list!");
732 //Remove device from list
733 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
734 return CA_STATUS_FAILED;
737 // Make a rfcomm connection with remote BT Device
738 if (device->serviceSearched &&
739 CA_STATUS_OK != CAEDRClientConnect(remoteAddress, OIC_EDR_SERVICE_ID))
741 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection!");
743 //Remove device from list
744 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
745 return CA_STATUS_FAILED;
750 result = CAEDRSendData(device->socketFD, data, dataLength);
751 if (CA_STATUS_OK != result)
753 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to send data!");
754 return CA_STATUS_FAILED;
758 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
762 CAResult_t CAEDRClientSendMulticastData(const uint8_t *data,
765 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
768 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
772 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
773 return CA_STATUS_INVALID_PARAM;
776 // Send the packet to all OIC devices
777 ca_mutex_lock(g_edrDeviceListMutex);
779 EDRDeviceList *curList = g_edrDeviceList;
780 CAResult_t result = CA_STATUS_FAILED;
781 while (curList != NULL)
783 EDRDevice *device = curList->device;
784 curList = curList->next;
788 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
792 if (-1 == device->socketFD)
794 // Check if the device service search is finished
795 if (false == device->serviceSearched)
797 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Device services are still unknown!");
801 // Adding to pendding list
802 result = CAAddEDRDataToList(&device->pendingDataList, data,
804 if (CA_STATUS_OK != result)
806 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list !");
810 // Make a rfcomm connection with remote BT Device
811 result = CAEDRClientConnect(device->remoteAddress, device->serviceUUID);
812 if (CA_STATUS_OK != result)
814 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection !");
816 //Remove the data which added to pending list
817 CARemoveEDRDataFromList(&device->pendingDataList);
823 result = CAEDRSendData(device->socketFD, data, dataLength);
824 if (CA_STATUS_OK != result)
826 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send data to [%s] !",
827 device->remoteAddress);
832 ca_mutex_unlock(g_edrDeviceListMutex);
834 // Start the device Discovery.
835 result = CAEDRStartDeviceDiscovery();
836 if (CA_STATUS_OK == result)
838 OIC_LOG(INFO, EDR_ADAPTER_TAG, "Add the data to the multicast data list");
840 EDRData *multicastData = (EDRData *)OICCalloc(1, sizeof(EDRData));
841 if (NULL == multicastData)
843 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Malloc failed");
846 multicastData->data = OICCalloc(1, dataLength);
847 if (NULL == multicastData->data)
849 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Malloc failed");
852 memcpy(multicastData->data, data, dataLength);
853 multicastData->dataLength = dataLength;
855 // Add the data to pending multicast data list.
856 ca_mutex_lock(g_multicastDataListMutex);
857 if (NULL == g_multicastDataList)
859 g_multicastDataList = u_arraylist_create();
861 u_arraylist_add(g_multicastDataList, (void *)multicastData);
862 ca_mutex_unlock(g_multicastDataListMutex);
866 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
870 CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID)
872 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
874 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
875 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "Service UUID is null");
877 size_t addressLen = strlen(remoteAddress);
878 if (0 == addressLen || CA_MACADDR_SIZE - 1 != addressLen)
880 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Invalid remote address");
881 return CA_STATUS_INVALID_PARAM;
886 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Empty service uuid");
887 return CA_STATUS_INVALID_PARAM;
890 bt_error_e err = bt_socket_connect_rfcomm(remoteAddress, serviceUUID);
891 if (BT_ERROR_NONE != err)
893 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
894 "Failed to connect!, address [%s] error num [%x]",
896 return CA_STATUS_FAILED;
899 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
903 CAResult_t CAEDRClientDisconnect(const int32_t clientID)
905 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
910 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: negative client id");
911 return CA_STATUS_INVALID_PARAM;
914 bt_error_e err = bt_socket_disconnect_rfcomm(clientID);
915 if (BT_ERROR_NONE != err)
917 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed close rfcomm client socket!, error num [%x]",
919 return CA_STATUS_FAILED;
922 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
926 void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData)
928 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
930 EDRDevice *device = NULL;
932 if (NULL == data || 0 >= data->data_size)
934 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Data is null!");
938 // Get EDR device from list
939 ca_mutex_lock(g_edrDeviceListMutex);
940 CAResult_t result = CAGetEDRDeviceBySocketId(g_edrDeviceList, data->socket_fd, &device);
941 if (CA_STATUS_OK != result)
943 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Could not find the device!");
945 ca_mutex_unlock(g_edrDeviceListMutex);
948 ca_mutex_unlock(g_edrDeviceListMutex);
952 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
956 uint32_t sentLength = 0;
958 g_edrPacketReceivedCallback(device->remoteAddress,
959 (uint8_t *) data->data,
960 (uint32_t) data->data_size,
963 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");