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 * This function creates mutex.
83 static CAResult_t CAEDRManagerInitializeMutex(void);
86 * This function frees mutex.
88 static void CAEDRManagerTerminateMutex(void);
91 * This callback is registered to recieve data on any open RFCOMM connection.
93 static void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData);
96 * This function starts device discovery.
98 static CAResult_t CAEDRStartDeviceDiscovery(void);
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");
421 bool isDiscoveryStarted = false;
423 // Check the device discovery state
424 bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
425 if (BT_ERROR_NONE != err)
427 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
429 return CA_STATUS_FAILED;
432 //Start device discovery if its not started
433 if (false == isDiscoveryStarted)
435 err = bt_adapter_start_device_discovery();
436 if (BT_ERROR_NONE != err)
438 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Device discovery failed!, error num [%x]",
440 return CA_STATUS_FAILED;
444 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
448 CAResult_t CAEDRStopServiceSearch(void)
450 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
452 bt_error_e err = bt_device_cancel_service_search();
453 // Stop ongoing service search
454 if (BT_ERROR_NONE != err)
456 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
458 return CA_STATUS_FAILED;
461 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
465 CAResult_t CAEDRStopDeviceDiscovery(void)
467 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
469 bool isDiscoveryStarted = false;
470 bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
471 // Check the device discovery state
472 if (BT_ERROR_NONE != err)
474 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
476 return CA_STATUS_FAILED;
479 //stop the device discovery process
480 if (true == isDiscoveryStarted)
482 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Stopping the device search process");
483 if (BT_ERROR_NONE != (err = bt_adapter_stop_device_discovery()))
485 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to stop discovery!, error num [%x]",
490 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
494 CAResult_t CAEDRStartServiceSearch(const char *remoteAddress)
496 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
499 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
500 if (!remoteAddress[0])
502 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Remote address is empty!");
503 return CA_STATUS_INVALID_PARAM;
506 bt_error_e err = bt_device_start_service_search(remoteAddress);
507 // Start searching for OIC service
508 if (BT_ERROR_NONE != err)
510 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
512 return CA_STATUS_FAILED;
515 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
519 CAResult_t CAEDRClientSetCallbacks(void)
521 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
523 // Register for discovery and rfcomm socket connection callbacks
524 bt_adapter_set_device_discovery_state_changed_cb(CAEDRDeviceDiscoveryCallback, NULL);
525 bt_device_set_service_searched_cb(CAEDRServiceSearchedCallback, NULL);
526 bt_socket_set_connection_state_changed_cb(CAEDRSocketConnectionStateCallback, NULL);
527 bt_socket_set_data_received_cb(CAEDRDataRecvCallback, NULL);
529 // Start device discovery
530 CAResult_t result = CAEDRStartDeviceDiscovery();
531 if(CA_STATUS_OK != result)
533 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Failed to Start Device discovery");
534 return CA_STATUS_FAILED;
537 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
542 void CAEDRClientUnsetCallbacks(void)
544 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
546 // Stop service search
547 CAEDRStopServiceSearch();
549 // Stop the device discovery process
550 CAEDRStopDeviceDiscovery();
552 // reset bluetooth adapter callbacks
553 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Resetting the callbacks");
554 bt_adapter_unset_device_discovery_state_changed_cb();
555 bt_device_unset_service_searched_cb();
556 bt_socket_unset_connection_state_changed_cb();
557 bt_socket_unset_data_received_cb();
559 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
562 CAResult_t CAEDRManagerInitializeMutex(void)
564 CAResult_t result = CA_STATUS_OK;
565 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
567 if (!g_edrDeviceListMutex)
569 g_edrDeviceListMutex = ca_mutex_new();
572 if (!g_multicastDataListMutex)
574 g_multicastDataListMutex = ca_mutex_new();
577 if (!g_edrDeviceListMutex || !g_multicastDataListMutex)
579 result = CA_STATUS_NOT_INITIALIZED;
582 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
587 void CAEDRManagerTerminateMutex(void)
589 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
591 if (g_edrDeviceListMutex)
593 ca_mutex_free(g_edrDeviceListMutex);
594 g_edrDeviceListMutex = NULL;
597 if (g_multicastDataListMutex)
599 ca_mutex_free(g_multicastDataListMutex);
600 g_multicastDataListMutex = NULL;
603 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
606 CAResult_t CAEDRClientInitialize()
608 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
609 CAResult_t result = CAEDRManagerInitializeMutex();
610 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
614 void CAEDRClientTerminate()
616 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
618 // Free EDRDevices list
619 if (g_edrDeviceListMutex)
621 ca_mutex_lock(g_edrDeviceListMutex);
622 CADestroyEDRDeviceList(&g_edrDeviceList);
623 ca_mutex_unlock(g_edrDeviceListMutex);
626 if (g_multicastDataListMutex)
628 ca_mutex_lock(g_multicastDataListMutex);
629 u_arraylist_destroy(g_multicastDataList);
630 g_multicastDataList = NULL;
631 ca_mutex_unlock(g_multicastDataListMutex);
635 CAEDRManagerTerminateMutex();
636 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
639 void CAEDRClientDisconnectAll(void)
641 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
643 ca_mutex_lock(g_edrDeviceListMutex);
645 EDRDeviceList *cur = g_edrDeviceList;
648 EDRDevice *device = cur->device;
651 if (device && 0 <= device->socketFD)
653 CAResult_t result = CAEDRClientDisconnect(device->socketFD);
654 if (CA_STATUS_OK != result)
656 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to disconnect with client :%s",
657 device->remoteAddress);
660 device->socketFD = -1;
664 ca_mutex_unlock(g_edrDeviceListMutex);
666 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
670 CAResult_t CAEDRClientSendUnicastData(const char *remoteAddress,
674 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
676 EDRDevice *device = NULL;
679 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
680 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
684 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
685 return CA_STATUS_INVALID_PARAM;
688 // Check the connection existence with remote device
689 ca_mutex_lock(g_edrDeviceListMutex);
690 CAResult_t result = CAGetEDRDevice(g_edrDeviceList, remoteAddress, &device);
691 if (CA_STATUS_OK != result)
693 // Create new device and add to list
694 result = CACreateAndAddToDeviceList(&g_edrDeviceList, remoteAddress,
695 OIC_EDR_SERVICE_ID, &device);
696 if (CA_STATUS_OK != result)
698 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed create device and add to list!");
700 ca_mutex_unlock(g_edrDeviceListMutex);
701 return CA_STATUS_FAILED;
704 // Start the OIC service search newly created device
705 result = CAEDRStartServiceSearch(remoteAddress);
706 if (CA_STATUS_OK != result)
708 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to initiate service search!");
710 // Remove device from list
711 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
713 ca_mutex_unlock(g_edrDeviceListMutex);
714 return CA_STATUS_FAILED;
720 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
721 // Remove device from list
722 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
724 ca_mutex_unlock(g_edrDeviceListMutex);
725 return CA_STATUS_FAILED;
728 ca_mutex_unlock(g_edrDeviceListMutex);
730 if (-1 == device->socketFD)
732 // Adding to pending list
733 result = CAAddEDRDataToList(&device->pendingDataList, data,
735 if (CA_STATUS_OK != result)
737 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list!");
739 //Remove device from list
740 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
741 return CA_STATUS_FAILED;
744 // Make a rfcomm connection with remote BT Device
745 if (device->serviceSearched &&
746 CA_STATUS_OK != CAEDRClientConnect(remoteAddress, OIC_EDR_SERVICE_ID))
748 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection!");
750 //Remove device from list
751 CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
752 return CA_STATUS_FAILED;
757 result = CAEDRSendData(device->socketFD, data, dataLength);
758 if (CA_STATUS_OK != result)
760 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to send data!");
761 return CA_STATUS_FAILED;
765 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
769 CAResult_t CAEDRClientSendMulticastData(const uint8_t *data,
772 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
775 VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
779 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
780 return CA_STATUS_INVALID_PARAM;
783 // Send the packet to all OIC devices
784 ca_mutex_lock(g_edrDeviceListMutex);
786 EDRDeviceList *curList = g_edrDeviceList;
787 CAResult_t result = CA_STATUS_FAILED;
788 while (curList != NULL)
790 EDRDevice *device = curList->device;
791 curList = curList->next;
795 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
799 if (-1 == device->socketFD)
801 // Check if the device service search is finished
802 if (false == device->serviceSearched)
804 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Device services are still unknown!");
808 // Adding to pendding list
809 result = CAAddEDRDataToList(&device->pendingDataList, data,
811 if (CA_STATUS_OK != result)
813 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list !");
817 // Make a rfcomm connection with remote BT Device
818 result = CAEDRClientConnect(device->remoteAddress, device->serviceUUID);
819 if (CA_STATUS_OK != result)
821 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection !");
823 //Remove the data which added to pending list
824 CARemoveEDRDataFromList(&device->pendingDataList);
830 result = CAEDRSendData(device->socketFD, data, dataLength);
831 if (CA_STATUS_OK != result)
833 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send data to [%s] !",
834 device->remoteAddress);
839 ca_mutex_unlock(g_edrDeviceListMutex);
841 // Start the device Discovery.
842 result = CAEDRStartDeviceDiscovery();
843 if (CA_STATUS_OK == result)
845 OIC_LOG(INFO, EDR_ADAPTER_TAG, "Add the data to the multicast data list");
847 EDRData *multicastData = (EDRData *)OICCalloc(1, sizeof(EDRData));
848 if (NULL == multicastData)
850 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Malloc failed");
853 multicastData->data = OICCalloc(1, dataLength);
854 if (NULL == multicastData->data)
856 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Malloc failed");
859 memcpy(multicastData->data, data, dataLength);
860 multicastData->dataLength = dataLength;
862 // Add the data to pending multicast data list.
863 ca_mutex_lock(g_multicastDataListMutex);
864 if (NULL == g_multicastDataList)
866 g_multicastDataList = u_arraylist_create();
868 u_arraylist_add(g_multicastDataList, (void *)multicastData);
869 ca_mutex_unlock(g_multicastDataListMutex);
873 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
877 CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID)
879 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
881 VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
882 VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "Service UUID is null");
884 size_t addressLen = strlen(remoteAddress);
885 if (0 == addressLen || CA_MACADDR_SIZE - 1 != addressLen)
887 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Invalid remote address");
888 return CA_STATUS_INVALID_PARAM;
893 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Empty service uuid");
894 return CA_STATUS_INVALID_PARAM;
897 bt_error_e err = bt_socket_connect_rfcomm(remoteAddress, serviceUUID);
898 if (BT_ERROR_NONE != err)
900 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
901 "Failed to connect!, address [%s] error num [%x]",
903 return CA_STATUS_FAILED;
906 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
910 CAResult_t CAEDRClientDisconnect(const int32_t clientID)
912 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
917 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: negative client id");
918 return CA_STATUS_INVALID_PARAM;
921 bt_error_e err = bt_socket_disconnect_rfcomm(clientID);
922 if (BT_ERROR_NONE != err)
924 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed close rfcomm client socket!, error num [%x]",
926 return CA_STATUS_FAILED;
929 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
933 void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData)
935 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
937 EDRDevice *device = NULL;
939 if (NULL == data || 0 >= data->data_size)
941 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Data is null!");
945 // Get EDR device from list
946 ca_mutex_lock(g_edrDeviceListMutex);
947 CAResult_t result = CAGetEDRDeviceBySocketId(g_edrDeviceList, data->socket_fd, &device);
948 if (CA_STATUS_OK != result)
950 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Could not find the device!");
952 ca_mutex_unlock(g_edrDeviceListMutex);
955 ca_mutex_unlock(g_edrDeviceListMutex);
957 //: TODO Need to check if 'check required for socket still connected or not'
960 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
964 CAConnectedDeviceInfo_t *deviceInfo =
965 (CAConnectedDeviceInfo_t *) CAEDRGetDeviceInfoFromAddress(device->remoteAddress);
969 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Received Data from new device");
970 deviceInfo = (CAConnectedDeviceInfo_t *) OICCalloc(1, sizeof(*deviceInfo));
973 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Out of memory");
977 deviceInfo->state = STATE_CONNECTED;
978 deviceInfo->recvData = NULL;
979 deviceInfo->recvDataLen = 0;
980 deviceInfo->totalDataLen = 0;
981 result = CAEDRAddDeviceInfoToList(device->remoteAddress, deviceInfo);
982 if (CA_STATUS_OK != result)
984 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Could not add device info to list!");
990 if (!deviceInfo->recvData)
992 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Callocing deviceInfo->recvData");
993 deviceInfo->recvData = OICCalloc(data->data_size, sizeof(uint8_t));
994 if (!deviceInfo->recvData)
996 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "out of memory");
1001 memcpy(deviceInfo->recvData + deviceInfo->recvDataLen, (const char*)data->data,
1003 deviceInfo->recvDataLen += data->data_size;
1005 if (!deviceInfo->totalDataLen)
1007 coap_transport_type transport = coap_get_tcp_header_type_from_initbyte(
1008 ((unsigned char *)deviceInfo->recvData)[0] >> 4);
1009 size_t headerLen = coap_get_tcp_header_length_for_transport(transport);
1011 if (deviceInfo->recvDataLen >= headerLen)
1013 // get actual data length from coap over tcp header
1014 deviceInfo->totalDataLen = coap_get_total_message_length(deviceInfo->recvData,
1015 deviceInfo->recvDataLen);
1016 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "total data length [%d] bytes", deviceInfo->totalDataLen);
1018 uint8_t *newBuf = OICRealloc(deviceInfo->recvData, deviceInfo->totalDataLen);
1021 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "out of memory");
1025 deviceInfo->recvData = newBuf;
1029 if (deviceInfo->totalDataLen == deviceInfo->recvDataLen)
1031 if (g_edrPacketReceivedCallback)
1033 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG,"data will be sent to callback routine: %s, %d",
1034 deviceInfo->recvData, deviceInfo->recvDataLen);
1036 uint32_t sentLength = 0;
1037 g_edrPacketReceivedCallback(device->remoteAddress, (void*) deviceInfo->recvData,
1038 deviceInfo->recvDataLen, &sentLength);
1040 OICFree(deviceInfo->recvData);
1041 deviceInfo->recvData = NULL;
1042 deviceInfo->recvDataLen = 0;
1043 deviceInfo->totalDataLen = 0;
1047 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");