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>
30 #include <bluetooth_internal.h>
32 #include "caedrinterface.h"
34 #include "caedrendpoint.h"
35 #include "caadapterutils.h"
36 #include "caedrutils.h"
39 #include "caedrdevicelist.h"
41 #define MICROSECS_PER_SEC 1000000
44 * Maximum CoAP over TCP header length
45 * to know the total data length.
47 #define EDR_MAX_HEADER_LEN 6
50 * Mutex to synchronize the access to Bluetooth device information list.
52 static ca_mutex g_edrDeviceListMutex = NULL;
55 * Peer Bluetooth device information list.
57 static EDRDeviceList *g_edrDeviceList = NULL;
60 * Maintains the callback to be notified when data received from remote
63 static CAEDRDataReceivedCallback g_edrPacketReceivedCallback = NULL;
66 * Error callback to update error in EDR.
68 static CAEDRErrorHandleCallback g_edrErrorHandler = NULL;
71 * Pending multicast data list to be sent.
73 static u_arraylist_t *g_multicastDataList = NULL;
76 * Mutex to synchronize the access to Pending multicast data list.
78 static ca_mutex g_multicastDataListMutex = NULL;
81 * To Store Adapter Mode information
83 static bool g_isDiscoveryServer = false;
86 * This function creates mutex.
88 static CAResult_t CAEDRManagerInitializeMutex(void);
91 * This function frees mutex.
93 static void CAEDRManagerTerminateMutex(void);
96 * This callback is registered to recieve data on any open RFCOMM connection.
98 static void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData);
101 * This function stops any ongoing service sevice search.
103 static CAResult_t CAEDRStopServiceSearch(void);
106 * This function stops device discovery.
108 static CAResult_t CAEDRStopDeviceDiscovery(void);
111 * This function searches for OIC service for remote Bluetooth device.
113 static CAResult_t CAEDRStartServiceSearch(const char *remoteAddress);
116 * This callback is registered to recieve all bluetooth nearby devices
117 * when device scan is initiated.
119 static void CAEDRDeviceDiscoveryCallback(int result,
120 bt_adapter_device_discovery_state_e state,
121 bt_adapter_device_discovery_info_s *discoveryInfo,
125 * This callback is registered to recieve all the services remote
126 * bluetooth device supports when service search initiated.
128 static void CAEDRServiceSearchedCallback(int result, bt_device_sdp_info_s *sdpInfo,
132 * This callback is registered to receive bluetooth RFCOMM connection
135 static void CAEDRSocketConnectionStateCallback(int result,
136 bt_socket_connection_state_e state,
137 bt_socket_connection_s *connection, void *userData);
140 * Establishes RFCOMM connection with remote bluetooth device.
142 static CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID);
145 * Disconnect RFCOMM client socket connection.
147 static CAResult_t CAEDRClientDisconnect(const int32_t clientID);
149 void CAEDRSetPacketReceivedCallback(CAEDRDataReceivedCallback packetReceivedCallback)
151 g_edrPacketReceivedCallback = packetReceivedCallback;
154 void CAEDRSetErrorHandler(CAEDRErrorHandleCallback errorHandleCallback)
156 g_edrErrorHandler = errorHandleCallback;
159 void CAEDRSocketConnectionStateCallback(int result, bt_socket_connection_state_e state,
160 bt_socket_connection_s *connection, void *userData)
162 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
164 EDRDevice *device = NULL;
166 if (BT_ERROR_NONE != result || NULL == connection)
168 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Invalid connection state!, error num [%x]",
175 case BT_SOCKET_CONNECTED:
177 ca_mutex_lock(g_edrDeviceListMutex);
178 CAResult_t res = CAGetEDRDevice(g_edrDeviceList, connection->remote_address,
180 if (CA_STATUS_OK != res)
182 // Create the deviceinfo and add to list
183 res = CACreateAndAddToDeviceList(&g_edrDeviceList,
184 connection->remote_address, OIC_EDR_SERVICE_ID, &device);
185 if (CA_STATUS_OK != res)
187 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed add device to list ret[%d]", res);
188 ca_mutex_unlock(g_edrDeviceListMutex);
194 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
195 ca_mutex_unlock(g_edrDeviceListMutex);
199 device->socketFD = connection->socket_fd;
200 ca_mutex_unlock(g_edrDeviceListMutex);
206 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
207 ca_mutex_unlock(g_edrDeviceListMutex);
210 device->socketFD = connection->socket_fd;
211 while (device->pendingDataList)
213 EDRData *edrData = device->pendingDataList->data;
214 res = CAEDRSendData(device->socketFD, edrData->data,
215 edrData->dataLength);
216 if (CA_STATUS_OK != res)
218 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send pending data [%s]",
219 device->remoteAddress);
221 // Remove all the data from pending list
222 CADestroyEDRDataList(&device->pendingDataList);
226 // Remove the data which send from pending list
227 CARemoveEDRDataFromList(&device->pendingDataList);
229 ca_mutex_unlock(g_edrDeviceListMutex);
233 case BT_SOCKET_DISCONNECTED:
235 ca_mutex_lock(g_edrDeviceListMutex);
236 CARemoveEDRDeviceFromList(&g_edrDeviceList, connection->remote_address);
237 ca_mutex_unlock(g_edrDeviceListMutex);
242 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
246 void CAEDRDeviceDiscoveryCallback(int result, bt_adapter_device_discovery_state_e state,
247 bt_adapter_device_discovery_info_s *discoveryInfo, void *userData)
249 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
251 EDRDevice *device = NULL;
253 if (BT_ERROR_NONE != result)
255 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Received bad state!, error num [%x]",
262 case BT_ADAPTER_DEVICE_DISCOVERY_STARTED:
264 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Discovery started!");
268 case BT_ADAPTER_DEVICE_DISCOVERY_FINISHED:
270 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Discovery finished!");
271 ca_mutex_lock(g_multicastDataListMutex);
272 u_arraylist_destroy(g_multicastDataList);
273 g_multicastDataList = NULL;
274 ca_mutex_unlock(g_multicastDataListMutex);
278 case BT_ADAPTER_DEVICE_DISCOVERY_FOUND:
280 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Device discovered [%s]!",
281 discoveryInfo->remote_name);
282 if (true == CAEDRIsServiceSupported((const char **)discoveryInfo->service_uuid,
283 discoveryInfo->service_count,
286 // Check if the deivce is already in the list
287 ca_mutex_lock(g_edrDeviceListMutex);
288 if (CA_STATUS_OK == CAGetEDRDevice(g_edrDeviceList,
289 discoveryInfo->remote_address, &device))
293 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
294 ca_mutex_unlock(g_edrDeviceListMutex);
297 device->serviceSearched = true;
298 ca_mutex_unlock(g_edrDeviceListMutex);
302 // Create the deviceinfo and add to list
303 CAResult_t res = CACreateAndAddToDeviceList(&g_edrDeviceList,
304 discoveryInfo->remote_address, OIC_EDR_SERVICE_ID, &device);
305 if (CA_STATUS_OK != res)
307 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add device to list!");
308 ca_mutex_unlock(g_edrDeviceListMutex);
314 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
315 ca_mutex_unlock(g_edrDeviceListMutex);
319 int lengthData = u_arraylist_length(g_multicastDataList);
320 for(int len = 0; len < lengthData; len++)
322 // Adding to pending list
323 EDRData *multicastData =
324 (EDRData *)u_arraylist_get(g_multicastDataList, len);
325 if (NULL == multicastData)
327 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "multicastData is NULL");
330 result = CAAddEDRDataToList(&device->pendingDataList, multicastData->data,
331 multicastData->dataLength);
332 if (CA_STATUS_OK != result)
334 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
335 "Failed to add data to pending list[%d]", result);
341 result = CAEDRClientConnect(device->remoteAddress, device->serviceUUID);
342 if (CA_STATUS_OK != result)
344 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
345 "Failed to make RFCOMM connection[%d]", result);
347 //Remove the data which added to pending list
348 CARemoveEDRDataFromList(&device->pendingDataList);
351 device->serviceSearched = true;
352 ca_mutex_unlock(g_edrDeviceListMutex);
356 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Device does not support OIC service!");
362 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
365 void CAEDRServiceSearchedCallback(int32_t result,
366 bt_device_sdp_info_s *sdpInfo,void *userData)
368 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
372 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "SDP info is null!");
376 ca_mutex_lock(g_edrDeviceListMutex);
378 EDRDevice *device = NULL;
379 CAResult_t res = CAGetEDRDevice(g_edrDeviceList, sdpInfo->remote_address, &device);
380 if (CA_STATUS_OK == res && NULL != device)
382 if (device->serviceSearched)
384 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Service is already searched for this device!");
385 ca_mutex_unlock(g_edrDeviceListMutex);
389 if (true == CAEDRIsServiceSupported((const char **)sdpInfo->service_uuid,
390 sdpInfo->service_count, OIC_EDR_SERVICE_ID))
392 device->serviceSearched = true;
393 res = CAEDRClientConnect(sdpInfo->remote_address, OIC_EDR_SERVICE_ID);
394 if (CA_STATUS_OK != res)
396 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make rfcomm connection!");
398 // Remove the device from device list
399 CARemoveEDRDeviceFromList(&g_edrDeviceList, sdpInfo->remote_address);
404 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Device does not contain OIC service!");
406 // Remove device from list as it does not support OIC service
407 CARemoveEDRDeviceFromList(&g_edrDeviceList, sdpInfo->remote_address);
411 ca_mutex_unlock(g_edrDeviceListMutex);
413 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
416 CAResult_t CAEDRStartDeviceDiscovery(void)
418 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
420 bool isDiscoveryStarted = false;
422 // Check the device discovery state
423 bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
424 if (BT_ERROR_NONE != err)
426 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
428 return CA_STATUS_FAILED;
431 //Start device discovery if its not started
432 if (false == isDiscoveryStarted)
434 err = bt_adapter_start_device_discovery();
435 if (BT_ERROR_NONE != err)
437 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Device discovery failed!, error num [%x]",
439 return CA_STATUS_FAILED;
443 g_isDiscoveryServer = true;
445 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
449 CAResult_t CAEDRStopServiceSearch(void)
451 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
453 bt_error_e err = bt_device_cancel_service_search();
454 // Stop ongoing service search
455 if (BT_ERROR_NONE != err)
457 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
459 return CA_STATUS_FAILED;
462 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
466 CAResult_t CAEDRStopDeviceDiscovery(void)
468 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
470 bool isDiscoveryStarted = false;
471 bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
472 // Check the device discovery state
473 if (BT_ERROR_NONE != err)
475 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
477 return CA_STATUS_FAILED;
480 //stop the device discovery process
481 if (true == isDiscoveryStarted)
483 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Stopping the device search process");
484 if (BT_ERROR_NONE != (err = bt_adapter_stop_device_discovery()))
486 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to stop discovery!, error num [%x]",
491 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
495 CAResult_t CAEDRStartServiceSearch(const char *remoteAddress)
497 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
500 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
501 if (!remoteAddress[0])
503 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Remote address is empty!");
504 return CA_STATUS_INVALID_PARAM;
507 bt_error_e err = bt_device_start_service_search(remoteAddress);
508 // Start searching for OIC service
509 if (BT_ERROR_NONE != err)
511 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
513 return CA_STATUS_FAILED;
516 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
520 CAResult_t CAEDRClientSetCallbacks(void)
522 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
524 // Register for discovery and rfcomm socket connection callbacks
525 bt_adapter_set_device_discovery_state_changed_cb(CAEDRDeviceDiscoveryCallback, NULL);
526 bt_device_set_service_searched_cb(CAEDRServiceSearchedCallback, NULL);
527 bt_socket_set_connection_state_changed_cb(CAEDRSocketConnectionStateCallback, NULL);
528 bt_socket_set_data_received_cb(CAEDRDataRecvCallback, NULL);
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 if(g_isDiscoveryServer)
836 // Start the device Discovery.
837 result = CAEDRStartDeviceDiscovery();
838 if (CA_STATUS_OK == result)
840 OIC_LOG(INFO, EDR_ADAPTER_TAG, "Add the data to the multicast data list");
842 EDRData *multicastData = (EDRData *)OICCalloc(1, sizeof(EDRData));
843 if (NULL == multicastData)
845 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Malloc failed");
848 multicastData->data = OICCalloc(1, dataLength);
849 if (NULL == multicastData->data)
851 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Malloc failed");
854 memcpy(multicastData->data, data, dataLength);
855 multicastData->dataLength = dataLength;
857 // Add the data to pending multicast data list.
858 ca_mutex_lock(g_multicastDataListMutex);
859 if (NULL == g_multicastDataList)
861 g_multicastDataList = u_arraylist_create();
863 u_arraylist_add(g_multicastDataList, (void *)multicastData);
864 ca_mutex_unlock(g_multicastDataListMutex);
869 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
873 CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID)
875 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
877 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
878 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "Service UUID is null");
880 size_t addressLen = strlen(remoteAddress);
881 if (0 == addressLen || CA_MACADDR_SIZE - 1 != addressLen)
883 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Invalid remote address");
884 return CA_STATUS_INVALID_PARAM;
889 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Empty service uuid");
890 return CA_STATUS_INVALID_PARAM;
893 bt_error_e err = bt_socket_connect_rfcomm(remoteAddress, serviceUUID);
894 if (BT_ERROR_NONE != err)
896 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
897 "Failed to connect!, address [%s] error num [%x]",
899 return CA_STATUS_FAILED;
902 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
906 CAResult_t CAEDRClientDisconnect(const int32_t clientID)
908 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
913 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: negative client id");
914 return CA_STATUS_INVALID_PARAM;
917 bt_error_e err = bt_socket_disconnect_rfcomm(clientID);
918 if (BT_ERROR_NONE != err)
920 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed close rfcomm client socket!, error num [%x]",
922 return CA_STATUS_FAILED;
925 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
929 void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData)
931 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
933 EDRDevice *device = NULL;
935 if (NULL == data || 0 >= data->data_size)
937 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Data is null!");
941 // Get EDR device from list
942 ca_mutex_lock(g_edrDeviceListMutex);
943 CAResult_t result = CAGetEDRDeviceBySocketId(g_edrDeviceList, data->socket_fd, &device);
944 if (CA_STATUS_OK != result)
946 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Could not find the device!");
948 ca_mutex_unlock(g_edrDeviceListMutex);
951 ca_mutex_unlock(g_edrDeviceListMutex);
953 //: TODO Need to check if 'check required for socket still connected or not'
956 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
960 CAConnectedDeviceInfo_t *deviceInfo =
961 (CAConnectedDeviceInfo_t *) CAEDRGetDeviceInfoFromAddress(device->remoteAddress);
965 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Received Data from new device");
966 deviceInfo = (CAConnectedDeviceInfo_t *) OICCalloc(1, sizeof(*deviceInfo));
969 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Out of memory");
973 deviceInfo->state = STATE_CONNECTED;
974 deviceInfo->recvData = NULL;
975 deviceInfo->recvDataLen = 0;
976 deviceInfo->totalDataLen = 0;
977 result = CAEDRAddDeviceInfoToList(device->remoteAddress, deviceInfo);
978 if (CA_STATUS_OK != result)
980 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Could not add device info to list!");
986 if (!deviceInfo->recvData)
988 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Callocing deviceInfo->recvData");
989 deviceInfo->recvData = OICCalloc(data->data_size, sizeof(uint8_t));
990 if (!deviceInfo->recvData)
992 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "out of memory");
997 memcpy(deviceInfo->recvData + deviceInfo->recvDataLen, (const char*)data->data,
999 deviceInfo->recvDataLen += data->data_size;
1001 if (!deviceInfo->totalDataLen)
1003 coap_transport_type transport = coap_get_tcp_header_type_from_initbyte(
1004 ((unsigned char *)deviceInfo->recvData)[0] >> 4);
1005 size_t headerLen = coap_get_tcp_header_length_for_transport(transport);
1007 if (deviceInfo->recvDataLen >= headerLen)
1009 // get actual data length from coap over tcp header
1010 deviceInfo->totalDataLen = coap_get_total_message_length(deviceInfo->recvData,
1011 deviceInfo->recvDataLen);
1012 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "total data length [%d] bytes", deviceInfo->totalDataLen);
1014 uint8_t *newBuf = OICRealloc(deviceInfo->recvData, deviceInfo->totalDataLen);
1017 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "out of memory");
1021 deviceInfo->recvData = newBuf;
1025 if (deviceInfo->totalDataLen == deviceInfo->recvDataLen)
1027 if (g_edrPacketReceivedCallback)
1029 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG,"data will be sent to callback routine: %s, %d",
1030 deviceInfo->recvData, deviceInfo->recvDataLen);
1032 uint32_t sentLength = 0;
1033 g_edrPacketReceivedCallback(device->remoteAddress, (void*) deviceInfo->recvData,
1034 deviceInfo->recvDataLen, &sentLength);
1036 OICFree(deviceInfo->recvData);
1037 deviceInfo->recvData = NULL;
1038 deviceInfo->recvDataLen = 0;
1039 deviceInfo->totalDataLen = 0;
1043 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");