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 * Maximum CoAP over TCP header length
44 * to know the total data length.
46 #define EDR_MAX_HEADER_LEN 6
49 * Mutex to synchronize the access to Bluetooth device information list.
51 static ca_mutex g_edrDeviceListMutex = NULL;
54 * Peer Bluetooth device information list.
56 static EDRDeviceList *g_edrDeviceList = NULL;
59 * Maintains the callback to be notified when data received from remote
62 static CAEDRDataReceivedCallback g_edrPacketReceivedCallback = NULL;
65 * Error callback to update error in EDR.
67 static CAEDRErrorHandleCallback g_edrErrorHandler = NULL;
70 * Pending multicast data list to be sent.
72 static u_arraylist_t *g_multicastDataList = NULL;
75 * Mutex to synchronize the access to Pending multicast data list.
77 static ca_mutex g_multicastDataListMutex = NULL;
80 * This function creates mutex.
82 static CAResult_t CAEDRManagerInitializeMutex(void);
85 * This function frees mutex.
87 static void CAEDRManagerTerminateMutex(void);
90 * This callback is registered to recieve data on any open RFCOMM connection.
92 static void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData);
95 * This function starts device discovery.
97 static CAResult_t CAEDRStartDeviceDiscovery(void);
100 * This function stops any ongoing service sevice search.
102 static CAResult_t CAEDRStopServiceSearch(void);
105 * This function stops device discovery.
107 static CAResult_t CAEDRStopDeviceDiscovery(void);
110 * This function searches for OIC service for remote Bluetooth device.
112 static CAResult_t CAEDRStartServiceSearch(const char *remoteAddress);
115 * This callback is registered to recieve all bluetooth nearby devices
116 * when device scan is initiated.
118 static void CAEDRDeviceDiscoveryCallback(int result,
119 bt_adapter_device_discovery_state_e state,
120 bt_adapter_device_discovery_info_s *discoveryInfo,
124 * This callback is registered to recieve all the services remote
125 * bluetooth device supports when service search initiated.
127 static void CAEDRServiceSearchedCallback(int result, bt_device_sdp_info_s *sdpInfo,
131 * This callback is registered to receive bluetooth RFCOMM connection
134 static void CAEDRSocketConnectionStateCallback(int result,
135 bt_socket_connection_state_e state,
136 bt_socket_connection_s *connection, void *userData);
139 * Establishes RFCOMM connection with remote bluetooth device.
141 static CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID);
144 * Disconnect RFCOMM client socket connection.
146 static CAResult_t CAEDRClientDisconnect(const int32_t clientID);
148 void CAEDRSetPacketReceivedCallback(CAEDRDataReceivedCallback packetReceivedCallback)
150 g_edrPacketReceivedCallback = packetReceivedCallback;
153 void CAEDRSetErrorHandler(CAEDRErrorHandleCallback errorHandleCallback)
155 g_edrErrorHandler = errorHandleCallback;
158 void CAEDRSocketConnectionStateCallback(int result, bt_socket_connection_state_e state,
159 bt_socket_connection_s *connection, void *userData)
161 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
163 EDRDevice *device = NULL;
165 if (BT_ERROR_NONE != result || NULL == connection)
167 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Invalid connection state!, error num [%x]",
174 case BT_SOCKET_CONNECTED:
176 ca_mutex_lock(g_edrDeviceListMutex);
177 CAResult_t res = CAGetEDRDevice(g_edrDeviceList, connection->remote_address,
179 if (CA_STATUS_OK != res)
181 // Create the deviceinfo and add to list
182 res = CACreateAndAddToDeviceList(&g_edrDeviceList,
183 connection->remote_address, OIC_EDR_SERVICE_ID, &device);
184 if (CA_STATUS_OK != res)
186 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed add device to list ret[%d]", res);
187 ca_mutex_unlock(g_edrDeviceListMutex);
193 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
194 ca_mutex_unlock(g_edrDeviceListMutex);
198 device->socketFD = connection->socket_fd;
199 ca_mutex_unlock(g_edrDeviceListMutex);
205 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
206 ca_mutex_unlock(g_edrDeviceListMutex);
209 device->socketFD = connection->socket_fd;
210 while (device->pendingDataList)
212 EDRData *edrData = device->pendingDataList->data;
213 res = CAEDRSendData(device->socketFD, edrData->data,
214 edrData->dataLength);
215 if (CA_STATUS_OK != res)
217 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send pending data [%s]",
218 device->remoteAddress);
220 // Remove all the data from pending list
221 CADestroyEDRDataList(&device->pendingDataList);
225 // Remove the data which send from pending list
226 CARemoveEDRDataFromList(&device->pendingDataList);
228 ca_mutex_unlock(g_edrDeviceListMutex);
232 case BT_SOCKET_DISCONNECTED:
234 ca_mutex_lock(g_edrDeviceListMutex);
235 CARemoveEDRDeviceFromList(&g_edrDeviceList, connection->remote_address);
236 ca_mutex_unlock(g_edrDeviceListMutex);
241 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
245 void CAEDRDeviceDiscoveryCallback(int result, bt_adapter_device_discovery_state_e state,
246 bt_adapter_device_discovery_info_s *discoveryInfo, void *userData)
248 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
250 EDRDevice *device = NULL;
252 if (BT_ERROR_NONE != result)
254 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Received bad state!, error num [%x]",
261 case BT_ADAPTER_DEVICE_DISCOVERY_STARTED:
263 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Discovery started!");
267 case BT_ADAPTER_DEVICE_DISCOVERY_FINISHED:
269 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Discovery finished!");
270 ca_mutex_lock(g_multicastDataListMutex);
271 u_arraylist_destroy(g_multicastDataList);
272 g_multicastDataList = NULL;
273 ca_mutex_unlock(g_multicastDataListMutex);
277 case BT_ADAPTER_DEVICE_DISCOVERY_FOUND:
279 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Device discovered [%s]!",
280 discoveryInfo->remote_name);
281 if (true == CAEDRIsServiceSupported((const char **)discoveryInfo->service_uuid,
282 discoveryInfo->service_count,
285 // Check if the deivce is already in the list
286 ca_mutex_lock(g_edrDeviceListMutex);
287 if (CA_STATUS_OK == CAGetEDRDevice(g_edrDeviceList,
288 discoveryInfo->remote_address, &device))
292 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
293 ca_mutex_unlock(g_edrDeviceListMutex);
296 device->serviceSearched = true;
297 ca_mutex_unlock(g_edrDeviceListMutex);
301 // Create the deviceinfo and add to list
302 CAResult_t res = CACreateAndAddToDeviceList(&g_edrDeviceList,
303 discoveryInfo->remote_address, OIC_EDR_SERVICE_ID, &device);
304 if (CA_STATUS_OK != res)
306 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add device to list!");
307 ca_mutex_unlock(g_edrDeviceListMutex);
313 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
314 ca_mutex_unlock(g_edrDeviceListMutex);
318 int lengthData = u_arraylist_length(g_multicastDataList);
319 for(int len = 0; len < lengthData; len++)
321 // Adding to pending list
322 EDRData *multicastData =
323 (EDRData *)u_arraylist_get(g_multicastDataList, len);
324 if (NULL == multicastData)
326 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "multicastData is NULL");
329 result = CAAddEDRDataToList(&device->pendingDataList, multicastData->data,
330 multicastData->dataLength);
331 if (CA_STATUS_OK != result)
333 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
334 "Failed to add data to pending list[%d]", result);
340 result = CAEDRClientConnect(device->remoteAddress, device->serviceUUID);
341 if (CA_STATUS_OK != result)
343 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
344 "Failed to make RFCOMM connection[%d]", result);
346 //Remove the data which added to pending list
347 CARemoveEDRDataFromList(&device->pendingDataList);
350 device->serviceSearched = true;
351 ca_mutex_unlock(g_edrDeviceListMutex);
355 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Device does not support OIC service!");
361 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
364 void CAEDRServiceSearchedCallback(int32_t result,
365 bt_device_sdp_info_s *sdpInfo,void *userData)
367 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
371 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "SDP info is null!");
375 ca_mutex_lock(g_edrDeviceListMutex);
377 EDRDevice *device = NULL;
378 CAResult_t res = CAGetEDRDevice(g_edrDeviceList, sdpInfo->remote_address, &device);
379 if (CA_STATUS_OK == res && NULL != device)
381 if (device->serviceSearched)
383 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Service is already searched for this device!");
384 ca_mutex_unlock(g_edrDeviceListMutex);
388 if (true == CAEDRIsServiceSupported((const char **)sdpInfo->service_uuid,
389 sdpInfo->service_count, OIC_EDR_SERVICE_ID))
391 device->serviceSearched = true;
392 res = CAEDRClientConnect(sdpInfo->remote_address, OIC_EDR_SERVICE_ID);
393 if (CA_STATUS_OK != res)
395 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make rfcomm connection!");
397 // Remove the device from device list
398 CARemoveEDRDeviceFromList(&g_edrDeviceList, sdpInfo->remote_address);
403 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Device does not contain OIC service!");
405 // Remove device from list as it does not support OIC service
406 CARemoveEDRDeviceFromList(&g_edrDeviceList, sdpInfo->remote_address);
410 ca_mutex_unlock(g_edrDeviceListMutex);
412 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
415 CAResult_t CAEDRStartDeviceDiscovery(void)
417 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 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
447 CAResult_t CAEDRStopServiceSearch(void)
449 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
451 bt_error_e err = bt_device_cancel_service_search();
452 // Stop ongoing service search
453 if (BT_ERROR_NONE != err)
455 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
457 return CA_STATUS_FAILED;
460 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
464 CAResult_t CAEDRStopDeviceDiscovery(void)
466 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
468 bool isDiscoveryStarted = false;
469 bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
470 // Check the device discovery state
471 if (BT_ERROR_NONE != err)
473 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
475 return CA_STATUS_FAILED;
478 //stop the device discovery process
479 if (true == isDiscoveryStarted)
481 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Stopping the device search process");
482 if (BT_ERROR_NONE != (err = bt_adapter_stop_device_discovery()))
484 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to stop discovery!, error num [%x]",
489 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
493 CAResult_t CAEDRStartServiceSearch(const char *remoteAddress)
495 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
498 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
499 if (!remoteAddress[0])
501 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Remote address is empty!");
502 return CA_STATUS_INVALID_PARAM;
505 bt_error_e err = bt_device_start_service_search(remoteAddress);
506 // Start searching for OIC service
507 if (BT_ERROR_NONE != err)
509 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
511 return CA_STATUS_FAILED;
514 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
518 CAResult_t CAEDRClientSetCallbacks(void)
520 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
522 // Register for discovery and rfcomm socket connection callbacks
523 bt_adapter_set_device_discovery_state_changed_cb(CAEDRDeviceDiscoveryCallback, NULL);
524 bt_device_set_service_searched_cb(CAEDRServiceSearchedCallback, NULL);
525 bt_socket_set_connection_state_changed_cb(CAEDRSocketConnectionStateCallback, NULL);
526 bt_socket_set_data_received_cb(CAEDRDataRecvCallback, NULL);
528 // Start device discovery
529 CAResult_t result = CAEDRStartDeviceDiscovery();
530 if(CA_STATUS_OK != result)
532 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Failed to Start Device discovery");
533 return CA_STATUS_FAILED;
536 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
541 void CAEDRClientUnsetCallbacks(void)
543 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
545 // Stop service search
546 CAEDRStopServiceSearch();
548 // Stop the device discovery process
549 CAEDRStopDeviceDiscovery();
551 // reset bluetooth adapter callbacks
552 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Resetting the callbacks");
553 bt_adapter_unset_device_discovery_state_changed_cb();
554 bt_device_unset_service_searched_cb();
555 bt_socket_unset_connection_state_changed_cb();
556 bt_socket_unset_data_received_cb();
558 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
561 CAResult_t CAEDRManagerInitializeMutex(void)
563 CAResult_t result = CA_STATUS_OK;
564 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
566 if (!g_edrDeviceListMutex)
568 g_edrDeviceListMutex = ca_mutex_new();
571 if (!g_multicastDataListMutex)
573 g_multicastDataListMutex = ca_mutex_new();
576 if (!g_edrDeviceListMutex || !g_multicastDataListMutex)
578 result = CA_STATUS_NOT_INITIALIZED;
581 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
586 void CAEDRManagerTerminateMutex(void)
588 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
590 if (g_edrDeviceListMutex)
592 ca_mutex_free(g_edrDeviceListMutex);
593 g_edrDeviceListMutex = NULL;
596 if (g_multicastDataListMutex)
598 ca_mutex_free(g_multicastDataListMutex);
599 g_multicastDataListMutex = NULL;
602 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
605 CAResult_t CAEDRClientInitialize()
607 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
608 CAResult_t result = CAEDRManagerInitializeMutex();
609 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
613 void CAEDRClientTerminate()
615 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
617 // Free EDRDevices list
618 if (g_edrDeviceListMutex)
620 ca_mutex_lock(g_edrDeviceListMutex);
621 CADestroyEDRDeviceList(&g_edrDeviceList);
622 ca_mutex_unlock(g_edrDeviceListMutex);
625 if (g_multicastDataListMutex)
627 ca_mutex_lock(g_multicastDataListMutex);
628 u_arraylist_destroy(g_multicastDataList);
629 g_multicastDataList = NULL;
630 ca_mutex_unlock(g_multicastDataListMutex);
634 CAEDRManagerTerminateMutex();
635 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
638 void CAEDRClientDisconnectAll(void)
640 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
642 ca_mutex_lock(g_edrDeviceListMutex);
644 EDRDeviceList *cur = g_edrDeviceList;
647 EDRDevice *device = cur->device;
650 if (device && 0 <= device->socketFD)
652 CAResult_t result = CAEDRClientDisconnect(device->socketFD);
653 if (CA_STATUS_OK != result)
655 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to disconnect with client :%s",
656 device->remoteAddress);
659 device->socketFD = -1;
663 ca_mutex_unlock(g_edrDeviceListMutex);
665 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
669 CAResult_t CAEDRClientSendUnicastData(const char *remoteAddress,
673 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
675 EDRDevice *device = NULL;
678 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
679 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
683 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
684 return CA_STATUS_INVALID_PARAM;
687 // Check the connection existence with remote device
688 ca_mutex_lock(g_edrDeviceListMutex);
689 CAResult_t result = CAGetEDRDevice(g_edrDeviceList, remoteAddress, &device);
690 if (CA_STATUS_OK != result)
692 // Create new device and add to list
693 result = CACreateAndAddToDeviceList(&g_edrDeviceList, remoteAddress,
694 OIC_EDR_SERVICE_ID, &device);
695 if (CA_STATUS_OK != result)
697 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed create device and add to list!");
699 ca_mutex_unlock(g_edrDeviceListMutex);
700 return CA_STATUS_FAILED;
703 // Start the OIC service search newly created device
704 result = CAEDRStartServiceSearch(remoteAddress);
705 if (CA_STATUS_OK != result)
707 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to initiate service search!");
709 // Remove device from list
710 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
712 ca_mutex_unlock(g_edrDeviceListMutex);
713 return CA_STATUS_FAILED;
719 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
720 // Remove device from list
721 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
723 ca_mutex_unlock(g_edrDeviceListMutex);
724 return CA_STATUS_FAILED;
727 ca_mutex_unlock(g_edrDeviceListMutex);
729 if (-1 == device->socketFD)
731 // Adding to pending list
732 result = CAAddEDRDataToList(&device->pendingDataList, data,
734 if (CA_STATUS_OK != result)
736 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list!");
738 //Remove device from list
739 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
740 return CA_STATUS_FAILED;
743 // Make a rfcomm connection with remote BT Device
744 if (device->serviceSearched &&
745 CA_STATUS_OK != CAEDRClientConnect(remoteAddress, OIC_EDR_SERVICE_ID))
747 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection!");
749 //Remove device from list
750 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
751 return CA_STATUS_FAILED;
756 result = CAEDRSendData(device->socketFD, data, dataLength);
757 if (CA_STATUS_OK != result)
759 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to send data!");
760 return CA_STATUS_FAILED;
764 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
768 CAResult_t CAEDRClientSendMulticastData(const uint8_t *data,
771 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
774 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
778 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
779 return CA_STATUS_INVALID_PARAM;
782 // Send the packet to all OIC devices
783 ca_mutex_lock(g_edrDeviceListMutex);
785 EDRDeviceList *curList = g_edrDeviceList;
786 CAResult_t result = CA_STATUS_FAILED;
787 while (curList != NULL)
789 EDRDevice *device = curList->device;
790 curList = curList->next;
794 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
798 if (-1 == device->socketFD)
800 // Check if the device service search is finished
801 if (false == device->serviceSearched)
803 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Device services are still unknown!");
807 // Adding to pendding list
808 result = CAAddEDRDataToList(&device->pendingDataList, data,
810 if (CA_STATUS_OK != result)
812 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list !");
816 // Make a rfcomm connection with remote BT Device
817 result = CAEDRClientConnect(device->remoteAddress, device->serviceUUID);
818 if (CA_STATUS_OK != result)
820 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection !");
822 //Remove the data which added to pending list
823 CARemoveEDRDataFromList(&device->pendingDataList);
829 result = CAEDRSendData(device->socketFD, data, dataLength);
830 if (CA_STATUS_OK != result)
832 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send data to [%s] !",
833 device->remoteAddress);
838 ca_mutex_unlock(g_edrDeviceListMutex);
840 // Start the device Discovery.
841 result = CAEDRStartDeviceDiscovery();
842 if (CA_STATUS_OK == result)
844 OIC_LOG(INFO, EDR_ADAPTER_TAG, "Add the data to the multicast data list");
846 EDRData *multicastData = (EDRData *)OICCalloc(1, sizeof(EDRData));
847 if (NULL == multicastData)
849 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Malloc failed");
852 multicastData->data = OICCalloc(1, dataLength);
853 if (NULL == multicastData->data)
855 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Malloc failed");
858 memcpy(multicastData->data, data, dataLength);
859 multicastData->dataLength = dataLength;
861 // Add the data to pending multicast data list.
862 ca_mutex_lock(g_multicastDataListMutex);
863 if (NULL == g_multicastDataList)
865 g_multicastDataList = u_arraylist_create();
867 u_arraylist_add(g_multicastDataList, (void *)multicastData);
868 ca_mutex_unlock(g_multicastDataListMutex);
872 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
876 CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID)
878 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
880 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
881 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "Service UUID is null");
883 size_t addressLen = strlen(remoteAddress);
884 if (0 == addressLen || CA_MACADDR_SIZE - 1 != addressLen)
886 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Invalid remote address");
887 return CA_STATUS_INVALID_PARAM;
892 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Empty service uuid");
893 return CA_STATUS_INVALID_PARAM;
896 bt_error_e err = bt_socket_connect_rfcomm(remoteAddress, serviceUUID);
897 if (BT_ERROR_NONE != err)
899 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
900 "Failed to connect!, address [%s] error num [%x]",
902 return CA_STATUS_FAILED;
905 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
909 CAResult_t CAEDRClientDisconnect(const int32_t clientID)
911 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
916 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: negative client id");
917 return CA_STATUS_INVALID_PARAM;
920 bt_error_e err = bt_socket_disconnect_rfcomm(clientID);
921 if (BT_ERROR_NONE != err)
923 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed close rfcomm client socket!, error num [%x]",
925 return CA_STATUS_FAILED;
928 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
932 void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData)
934 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
936 EDRDevice *device = NULL;
938 if (NULL == data || 0 >= data->data_size)
940 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Data is null!");
944 // Get EDR device from list
945 ca_mutex_lock(g_edrDeviceListMutex);
946 CAResult_t result = CAGetEDRDeviceBySocketId(g_edrDeviceList, data->socket_fd, &device);
947 if (CA_STATUS_OK != result)
949 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Could not find the device!");
951 ca_mutex_unlock(g_edrDeviceListMutex);
954 ca_mutex_unlock(g_edrDeviceListMutex);
956 //: TODO Need to check if 'check required for socket still connected or not'
959 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
963 CAConnectedDeviceInfo_t *deviceInfo =
964 (CAConnectedDeviceInfo_t *) CAEDRGetDeviceInfoFromAddress(device->remoteAddress);
968 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Received Data from new device");
969 deviceInfo = (CAConnectedDeviceInfo_t *) OICCalloc(1, sizeof(*deviceInfo));
972 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Out of memory");
976 deviceInfo->state = STATE_CONNECTED;
977 deviceInfo->recvData = NULL;
978 deviceInfo->recvDataLen = 0;
979 deviceInfo->totalDataLen = 0;
980 result = CAEDRAddDeviceInfoToList(device->remoteAddress, deviceInfo);
981 if (CA_STATUS_OK != result)
983 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Could not add device info to list!");
989 if (!deviceInfo->recvData)
991 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Callocing deviceInfo->recvData");
992 deviceInfo->recvData = OICCalloc(data->data_size, sizeof(uint8_t));
993 if (!deviceInfo->recvData)
995 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "out of memory");
1000 memcpy(deviceInfo->recvData + deviceInfo->recvDataLen, (const char*)data->data,
1002 deviceInfo->recvDataLen += data->data_size;
1004 if (!deviceInfo->totalDataLen)
1006 coap_transport_type transport = coap_get_tcp_header_type_from_initbyte(
1007 ((unsigned char *)deviceInfo->recvData)[0] >> 4);
1008 size_t headerLen = coap_get_tcp_header_length_for_transport(transport);
1010 if (deviceInfo->recvDataLen >= headerLen)
1012 // get actual data length from coap over tcp header
1013 deviceInfo->totalDataLen = coap_get_total_message_length(deviceInfo->recvData,
1014 deviceInfo->recvDataLen);
1015 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "total data length [%d] bytes", deviceInfo->totalDataLen);
1017 uint8_t *newBuf = OICRealloc(deviceInfo->recvData, deviceInfo->totalDataLen);
1020 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "out of memory");
1024 deviceInfo->recvData = newBuf;
1028 if (deviceInfo->totalDataLen == deviceInfo->recvDataLen)
1030 if (g_edrPacketReceivedCallback)
1032 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG,"data will be sent to callback routine: %s, %d",
1033 deviceInfo->recvData, deviceInfo->recvDataLen);
1035 uint32_t sentLength = 0;
1036 g_edrPacketReceivedCallback(device->remoteAddress, (void*) deviceInfo->recvData,
1037 deviceInfo->recvDataLen, &sentLength);
1039 OICFree(deviceInfo->recvData);
1040 deviceInfo->recvData = NULL;
1041 deviceInfo->recvDataLen = 0;
1042 deviceInfo->totalDataLen = 0;
1046 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");