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 oc_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 oc_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 oc_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 oc_mutex_unlock(g_edrDeviceListMutex);
194 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
195 oc_mutex_unlock(g_edrDeviceListMutex);
199 device->socketFD = connection->socket_fd;
200 oc_mutex_unlock(g_edrDeviceListMutex);
206 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
207 oc_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, device->remoteAddress,
215 edrData->data, 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 oc_mutex_unlock(g_edrDeviceListMutex);
233 case BT_SOCKET_DISCONNECTED:
235 oc_mutex_lock(g_edrDeviceListMutex);
236 CARemoveEDRDeviceFromList(&g_edrDeviceList, connection->remote_address);
237 oc_mutex_unlock(g_edrDeviceListMutex);
242 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Unknown Bluetooth Socket connection state");
245 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
249 void CAEDRDeviceDiscoveryCallback(int result, bt_adapter_device_discovery_state_e state,
250 bt_adapter_device_discovery_info_s *discoveryInfo, void *userData)
252 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
254 EDRDevice *device = NULL;
256 if (BT_ERROR_NONE != result)
258 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Received bad state!, error num [%x]",
265 case BT_ADAPTER_DEVICE_DISCOVERY_STARTED:
267 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Discovery started!");
271 case BT_ADAPTER_DEVICE_DISCOVERY_FINISHED:
273 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Discovery finished!");
274 oc_mutex_lock(g_multicastDataListMutex);
275 u_arraylist_destroy(g_multicastDataList);
276 g_multicastDataList = NULL;
277 oc_mutex_unlock(g_multicastDataListMutex);
281 case BT_ADAPTER_DEVICE_DISCOVERY_FOUND:
283 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Device discovered [%s]!",
284 discoveryInfo->remote_name);
285 if (true == CAEDRIsServiceSupported((const char **)discoveryInfo->service_uuid,
286 discoveryInfo->service_count,
289 // Check if the deivce is already in the list
290 oc_mutex_lock(g_edrDeviceListMutex);
291 if (CA_STATUS_OK == CAGetEDRDevice(g_edrDeviceList,
292 discoveryInfo->remote_address, &device))
296 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
297 oc_mutex_unlock(g_edrDeviceListMutex);
300 device->serviceSearched = true;
301 oc_mutex_unlock(g_edrDeviceListMutex);
305 // Create the deviceinfo and add to list
306 CAResult_t res = CACreateAndAddToDeviceList(&g_edrDeviceList,
307 discoveryInfo->remote_address, OIC_EDR_SERVICE_ID, &device);
308 if (CA_STATUS_OK != res)
310 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add device to list!");
311 oc_mutex_unlock(g_edrDeviceListMutex);
317 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
318 oc_mutex_unlock(g_edrDeviceListMutex);
322 int lengthData = u_arraylist_length(g_multicastDataList);
323 for(int len = 0; len < lengthData; len++)
325 // Adding to pending list
326 EDRData *multicastData =
327 (EDRData *)u_arraylist_get(g_multicastDataList, len);
328 if (NULL == multicastData)
330 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "multicastData is NULL");
333 result = CAAddEDRDataToList(&device->pendingDataList, multicastData->data,
334 multicastData->dataLength);
335 if (CA_STATUS_OK != result)
337 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
338 "Failed to add data to pending list[%d]", result);
344 result = CAEDRClientConnect(device->remoteAddress, device->serviceUUID);
345 if (CA_STATUS_OK != result)
347 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
348 "Failed to make RFCOMM connection[%d]", result);
350 //Remove the data which added to pending list
351 CARemoveEDRDataFromList(&device->pendingDataList);
354 device->serviceSearched = true;
355 oc_mutex_unlock(g_edrDeviceListMutex);
359 OIC_LOG(INFO, EDR_ADAPTER_TAG, "Device does not support OIC service!");
365 OIC_LOG(INFO, EDR_ADAPTER_TAG, "Unknown Bluetooth Adapter device discovery state");
368 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
371 void CAEDRServiceSearchedCallback(int32_t result,
372 bt_device_sdp_info_s *sdpInfo,void *userData)
374 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
378 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "SDP info is null!");
382 oc_mutex_lock(g_edrDeviceListMutex);
384 EDRDevice *device = NULL;
385 CAResult_t res = CAGetEDRDevice(g_edrDeviceList, sdpInfo->remote_address, &device);
386 if (CA_STATUS_OK == res && NULL != device)
388 if (device->serviceSearched)
390 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Service is already searched for this device!");
391 oc_mutex_unlock(g_edrDeviceListMutex);
395 if (true == CAEDRIsServiceSupported((const char **)sdpInfo->service_uuid,
396 sdpInfo->service_count, OIC_EDR_SERVICE_ID))
398 device->serviceSearched = true;
399 res = CAEDRClientConnect(sdpInfo->remote_address, OIC_EDR_SERVICE_ID);
400 if (CA_STATUS_OK != res)
402 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make rfcomm connection!");
404 // Remove the device from device list
405 CARemoveEDRDeviceFromList(&g_edrDeviceList, sdpInfo->remote_address);
410 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Device does not contain OIC service!");
412 // Remove device from list as it does not support OIC service
413 CARemoveEDRDeviceFromList(&g_edrDeviceList, sdpInfo->remote_address);
417 oc_mutex_unlock(g_edrDeviceListMutex);
419 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
422 CAResult_t CAEDRStartDeviceDiscovery(void)
424 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
426 bool isDiscoveryStarted = false;
428 // Check the device discovery state
429 bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
430 if (BT_ERROR_NONE != err)
432 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
434 return CA_STATUS_FAILED;
437 //Start device discovery if its not started
438 if (false == isDiscoveryStarted)
440 err = bt_adapter_start_device_discovery();
441 if (BT_ERROR_NONE != err)
443 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Device discovery failed!, error num [%x]",
445 return CA_STATUS_FAILED;
449 g_isDiscoveryServer = true;
451 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
455 CAResult_t CAEDRStopServiceSearch(void)
457 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
459 bt_error_e err = bt_device_cancel_service_search();
460 // Stop ongoing service search
461 if (BT_ERROR_NONE != err)
463 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
465 return CA_STATUS_FAILED;
468 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
472 CAResult_t CAEDRStopDeviceDiscovery(void)
474 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
476 bool isDiscoveryStarted = false;
477 bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
478 // Check the device discovery state
479 if (BT_ERROR_NONE != err)
481 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
483 return CA_STATUS_FAILED;
486 //stop the device discovery process
487 if (true == isDiscoveryStarted)
489 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Stopping the device search process");
490 if (BT_ERROR_NONE != (err = bt_adapter_stop_device_discovery()))
492 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to stop discovery!, error num [%x]",
497 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
501 CAResult_t CAEDRStartServiceSearch(const char *remoteAddress)
503 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
506 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
507 if (!remoteAddress[0])
509 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Remote address is empty!");
510 return CA_STATUS_INVALID_PARAM;
513 bt_error_e err = bt_device_start_service_search(remoteAddress);
514 // Start searching for OIC service
515 if (BT_ERROR_NONE != err)
517 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
519 return CA_STATUS_FAILED;
522 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
526 CAResult_t CAEDRClientSetCallbacks(void)
528 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
530 // Register for discovery and rfcomm socket connection callbacks
531 bt_adapter_set_device_discovery_state_changed_cb(CAEDRDeviceDiscoveryCallback, NULL);
532 bt_device_set_service_searched_cb(CAEDRServiceSearchedCallback, NULL);
533 bt_socket_set_connection_state_changed_cb(CAEDRSocketConnectionStateCallback, NULL);
534 bt_socket_set_data_received_cb(CAEDRDataRecvCallback, NULL);
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 = oc_mutex_new();
571 if (!g_multicastDataListMutex)
573 g_multicastDataListMutex = oc_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 oc_mutex_free(g_edrDeviceListMutex);
593 g_edrDeviceListMutex = NULL;
596 if (g_multicastDataListMutex)
598 oc_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 oc_mutex_lock(g_edrDeviceListMutex);
621 CADestroyEDRDeviceList(&g_edrDeviceList);
622 oc_mutex_unlock(g_edrDeviceListMutex);
625 if (g_multicastDataListMutex)
627 oc_mutex_lock(g_multicastDataListMutex);
628 u_arraylist_destroy(g_multicastDataList);
629 g_multicastDataList = NULL;
630 oc_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 oc_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 oc_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 oc_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 oc_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 oc_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 oc_mutex_unlock(g_edrDeviceListMutex);
724 return CA_STATUS_FAILED;
727 oc_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, device->remoteAddress, 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 oc_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, device->remoteAddress ,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 oc_mutex_unlock(g_edrDeviceListMutex);
840 if(g_isDiscoveryServer)
842 // Start the device Discovery.
843 result = CAEDRStartDeviceDiscovery();
844 if (CA_STATUS_OK == result)
846 OIC_LOG(INFO, EDR_ADAPTER_TAG, "Add the data to the multicast data list");
848 EDRData *multicastData = (EDRData *)OICCalloc(1, sizeof(EDRData));
849 if (NULL == multicastData)
851 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Malloc failed");
854 multicastData->data = OICCalloc(1, dataLength);
855 if (NULL == multicastData->data)
857 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Malloc failed");
860 memcpy(multicastData->data, data, dataLength);
861 multicastData->dataLength = dataLength;
863 // Add the data to pending multicast data list.
864 oc_mutex_lock(g_multicastDataListMutex);
865 if (NULL == g_multicastDataList)
867 g_multicastDataList = u_arraylist_create();
869 u_arraylist_add(g_multicastDataList, (void *)multicastData);
870 oc_mutex_unlock(g_multicastDataListMutex);
875 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
879 CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID)
881 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
883 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
884 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "Service UUID is null");
886 size_t addressLen = strlen(remoteAddress);
887 if (0 == addressLen || CA_MACADDR_SIZE - 1 != addressLen)
889 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Invalid remote address");
890 return CA_STATUS_INVALID_PARAM;
895 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Empty service uuid");
896 return CA_STATUS_INVALID_PARAM;
899 bt_error_e err = bt_socket_connect_rfcomm(remoteAddress, serviceUUID);
900 if (BT_ERROR_NONE != err)
902 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
903 "Failed to connect!, address [%s] error num [%x]",
905 return CA_STATUS_FAILED;
908 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
912 CAResult_t CAEDRClientDisconnect(const int32_t clientID)
914 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
919 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: negative client id");
920 return CA_STATUS_INVALID_PARAM;
923 bt_error_e err = bt_socket_disconnect_rfcomm(clientID);
924 if (BT_ERROR_NONE != err)
926 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed close rfcomm client socket!, error num [%x]",
928 return CA_STATUS_FAILED;
931 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
935 void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData)
937 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
939 EDRDevice *device = NULL;
941 if (NULL == data || 0 >= data->data_size)
943 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Data is null!");
947 // Get EDR device from list
948 oc_mutex_lock(g_edrDeviceListMutex);
949 CAResult_t result = CAGetEDRDeviceBySocketId(g_edrDeviceList, data->socket_fd, &device);
950 if (CA_STATUS_OK != result)
952 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Could not find the device!");
954 oc_mutex_unlock(g_edrDeviceListMutex);
957 oc_mutex_unlock(g_edrDeviceListMutex);
959 //: TODO Need to check if 'check required for socket still connected or not'
962 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
966 CAConnectedDeviceInfo_t *deviceInfo =
967 (CAConnectedDeviceInfo_t *) CAEDRGetDeviceInfoFromAddress(device->remoteAddress);
971 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Received Data from new device");
972 deviceInfo = (CAConnectedDeviceInfo_t *) OICCalloc(1, sizeof(*deviceInfo));
975 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Out of memory");
979 deviceInfo->state = STATE_CONNECTED;
980 deviceInfo->recvData = NULL;
981 deviceInfo->recvDataLen = 0;
982 deviceInfo->totalDataLen = 0;
983 result = CAEDRAddDeviceInfoToList(device->remoteAddress, deviceInfo);
984 if (CA_STATUS_OK != result)
986 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Could not add device info to list!");
992 if (!deviceInfo->recvData)
994 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Callocing deviceInfo->recvData");
995 deviceInfo->recvData = OICCalloc(data->data_size, sizeof(uint8_t));
996 if (!deviceInfo->recvData)
998 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "out of memory");
1003 memcpy(deviceInfo->recvData + deviceInfo->recvDataLen, (const char*)data->data,
1005 deviceInfo->recvDataLen += data->data_size;
1007 if (!deviceInfo->totalDataLen)
1009 coap_transport_t transport = coap_get_tcp_header_type_from_initbyte(
1010 ((unsigned char *)deviceInfo->recvData)[0] >> 4);
1011 size_t headerLen = coap_get_tcp_header_length_for_transport(transport);
1013 if (deviceInfo->recvDataLen >= headerLen)
1015 // get actual data length from coap over tcp header
1016 deviceInfo->totalDataLen = coap_get_total_message_length(deviceInfo->recvData,
1017 deviceInfo->recvDataLen);
1018 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "total data length [%d] bytes", deviceInfo->totalDataLen);
1020 uint8_t *newBuf = OICRealloc(deviceInfo->recvData, deviceInfo->totalDataLen);
1023 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "out of memory");
1027 deviceInfo->recvData = newBuf;
1031 if (deviceInfo->totalDataLen == deviceInfo->recvDataLen)
1033 if (g_edrPacketReceivedCallback)
1035 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG,"data will be sent to callback routine: %s, %d",
1036 deviceInfo->recvData, deviceInfo->recvDataLen);
1038 uint32_t sentLength = 0;
1039 g_edrPacketReceivedCallback(device->remoteAddress, (void*) deviceInfo->recvData,
1040 deviceInfo->recvDataLen, &sentLength);
1042 OICFree(deviceInfo->recvData);
1043 deviceInfo->recvData = NULL;
1044 deviceInfo->recvDataLen = 0;
1045 deviceInfo->totalDataLen = 0;
1049 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");