removed warning for Android EDR
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_edr_adapter / tizen / caedrclient.c
1 /* ****************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 /**
22  * @file
23  *
24  * This file provides the APIs to establish RFCOMM connection with remote
25  * bluetooth device.
26  */
27
28 #include <string.h>
29 #include <bluetooth.h>
30
31 #include "caedrinterface.h"
32 #include "camutex.h"
33 #include "caedrendpoint.h"
34 #include "caadapterutils.h"
35 #include "caedrutils.h"
36 #include "logger.h"
37 #include "cacommon.h"
38 #include "caedrdevicelist.h"
39
40 /**
41  * Mutex to synchronize the access to Bluetooth device information list.
42  */
43 static ca_mutex g_edrDeviceListMutex = NULL;
44
45 /**
46  * Peer Bluetooth device information list.
47  */
48 static EDRDeviceList *g_edrDeviceList = NULL;
49
50 /**
51  * Maintains the callback to be notified when data received from remote
52  * Bluetooth device.
53  */
54 static CAEDRDataReceivedCallback g_edrPacketReceivedCallback = NULL;
55
56 /**
57  * Error callback to update error in EDR.
58  */
59 static CAEDRErrorHandleCallback g_edrErrorHandler = NULL;
60
61 /**
62  * This function creates mutex.
63  */
64 static void CAEDRManagerInitializeMutex(void);
65
66 /**
67  * This function frees mutex.
68  */
69 static void CAEDRManagerTerminateMutex(void);
70
71 /**
72  * This callback is registered to recieve data on any open RFCOMM connection.
73  */
74 static void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData);
75
76 /**
77  * This function starts device discovery.
78  */
79 static CAResult_t CAEDRStartDeviceDiscovery(void);
80
81 /**
82  * This function stops any ongoing service sevice search.
83  */
84 static CAResult_t CAEDRStopServiceSearch(void);
85
86 /**
87  * This function stops device discovery.
88  */
89 static CAResult_t CAEDRStopDeviceDiscovery(void);
90
91 /**
92  * This function searches for OIC service for remote Bluetooth device.
93  */
94 static CAResult_t CAEDRStartServiceSearch(const char *remoteAddress);
95
96 /**
97  * This callback is registered to recieve all bluetooth nearby devices
98  * when device scan is initiated.
99  */
100 static void CAEDRDeviceDiscoveryCallback(int result,
101                                          bt_adapter_device_discovery_state_e state,
102                                          bt_adapter_device_discovery_info_s *discoveryInfo,
103                                          void *userData);
104
105 /**
106  * This callback is registered to recieve all the services remote
107  * bluetooth device supports when service search initiated.
108  */
109 static void CAEDRServiceSearchedCallback(int result, bt_device_sdp_info_s *sdpInfo,
110                                         void *userData);
111
112 /**
113  * This callback is registered to receive bluetooth RFCOMM connection
114  * state changes.
115  */
116 static void CAEDRSocketConnectionStateCallback(int result,
117                                     bt_socket_connection_state_e state,
118                                               bt_socket_connection_s *connection, void *userData);
119
120 /**
121  * Establishes RFCOMM connection with remote bluetooth device.
122  */
123 static CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID);
124
125 /**
126  * Disconnect RFCOMM client socket connection.
127  */
128 static CAResult_t CAEDRClientDisconnect(const int32_t clientID);
129
130 void CAEDRSetPacketReceivedCallback(CAEDRDataReceivedCallback packetReceivedCallback)
131 {
132     g_edrPacketReceivedCallback = packetReceivedCallback;
133 }
134
135 void CAEDRSetErrorHandler(CAEDRErrorHandleCallback errorHandleCallback)
136 {
137     g_edrErrorHandler = errorHandleCallback;
138 }
139
140 void CAEDRSocketConnectionStateCallback(int result, bt_socket_connection_state_e state,
141                                        bt_socket_connection_s *connection, void *userData)
142 {
143     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
144
145     EDRDevice *device = NULL;
146
147     if (BT_ERROR_NONE != result || NULL == connection)
148     {
149         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Invalid connection state!, error num [%x]",
150                   result);
151         return;
152     }
153
154     switch (state)
155     {
156         case BT_SOCKET_CONNECTED:
157             {
158                 ca_mutex_lock(g_edrDeviceListMutex);
159                 CAResult_t res = CAGetEDRDevice(g_edrDeviceList, connection->remote_address,
160                                                    &device);
161                 if (CA_STATUS_OK != res)
162                 {
163                     // Create the deviceinfo and add to list
164                     res = CACreateAndAddToDeviceList(&g_edrDeviceList,
165                             connection->remote_address, OIC_EDR_SERVICE_ID, &device);
166                     if (CA_STATUS_OK != res)
167                     {
168                         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed add device to list ret[%d]", res);
169                         ca_mutex_unlock(g_edrDeviceListMutex);
170                         return;
171                     }
172
173                     if(!device)
174                     {
175                         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
176                         ca_mutex_unlock(g_edrDeviceListMutex);
177                         return;
178                     }
179
180                     device->socketFD = connection->socket_fd;
181                     ca_mutex_unlock(g_edrDeviceListMutex);
182                     return;
183                 }
184
185                 if(!device)
186                 {
187                     OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
188                     ca_mutex_unlock(g_edrDeviceListMutex);
189                     return;
190                 }
191                 device->socketFD = connection->socket_fd;
192                 while (device->pendingDataList)
193                 {
194                     EDRData *edrData = device->pendingDataList->data;
195                     res = CAEDRSendData(device->socketFD, edrData->data,
196                                         edrData->dataLength);
197                     if (CA_STATUS_OK != res)
198                     {
199                         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send pending data [%s]",
200                                   device->remoteAddress);
201
202                         // Remove all the data from pending list
203                         CADestroyEDRDataList(&device->pendingDataList);
204                         break;
205                     }
206
207                     // Remove the data which send from pending list
208                     CARemoveEDRDataFromList(&device->pendingDataList);
209                 }
210                 ca_mutex_unlock(g_edrDeviceListMutex);
211             }
212             break;
213
214         case BT_SOCKET_DISCONNECTED:
215             {
216                 ca_mutex_lock(g_edrDeviceListMutex);
217                 CARemoveEDRDeviceFromList(&g_edrDeviceList, connection->remote_address);
218                 ca_mutex_unlock(g_edrDeviceListMutex);
219             }
220             break;
221     }
222
223     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
224 }
225
226
227 void CAEDRDeviceDiscoveryCallback(int result, bt_adapter_device_discovery_state_e state,
228                                  bt_adapter_device_discovery_info_s *discoveryInfo, void *userData)
229 {
230     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
231
232     EDRDevice *device = NULL;
233
234     if (BT_ERROR_NONE != result)
235     {
236         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Received bad state!, error num [%x]",
237                   result);
238         return;
239     }
240
241     switch (state)
242     {
243         case BT_ADAPTER_DEVICE_DISCOVERY_STARTED:
244             {
245                 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Discovery started!");
246             }
247             break;
248
249         case BT_ADAPTER_DEVICE_DISCOVERY_FINISHED:
250             {
251                 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Discovery finished!");
252             }
253             break;
254
255         case BT_ADAPTER_DEVICE_DISCOVERY_FOUND:
256             {
257                 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Device discovered [%s]!",
258                           discoveryInfo->remote_name);
259                 if (true == CAEDRIsServiceSupported((const char **)discoveryInfo->service_uuid,
260                                                         discoveryInfo->service_count,
261                                                         OIC_EDR_SERVICE_ID))
262                 {
263                     // Check if the deivce is already in the list
264                     ca_mutex_lock(g_edrDeviceListMutex);
265                     if (CA_STATUS_OK == CAGetEDRDevice(g_edrDeviceList,
266                                                 discoveryInfo->remote_address, &device))
267                     {
268                         if(!device)
269                         {
270                             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
271                             ca_mutex_unlock(g_edrDeviceListMutex);
272                             return;
273                         }
274                         device->serviceSearched = true;
275                         ca_mutex_unlock(g_edrDeviceListMutex);
276                         return;
277                     }
278
279                     // Create the deviceinfo and add to list
280                     CAResult_t res = CACreateAndAddToDeviceList(&g_edrDeviceList,
281                             discoveryInfo->remote_address, OIC_EDR_SERVICE_ID, &device);
282                     if (CA_STATUS_OK != res)
283                     {
284                         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add device to list!");
285                         ca_mutex_unlock(g_edrDeviceListMutex);
286                         return;
287                     }
288
289                     if(!device)
290                     {
291                         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
292                         ca_mutex_unlock(g_edrDeviceListMutex);
293                         return;
294                     }
295                     device->serviceSearched = true;
296                     ca_mutex_unlock(g_edrDeviceListMutex);
297                 }
298                 else
299                 {
300                     OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Device does not support OIC service!");
301                 }
302             }
303             break;
304     }
305
306     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
307 }
308
309 void CAEDRServiceSearchedCallback(int32_t result,
310                 bt_device_sdp_info_s *sdpInfo,void *userData)
311 {
312     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
313
314     if (NULL == sdpInfo)
315     {
316         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "SDP info is null!");
317         return;
318     }
319
320     ca_mutex_lock(g_edrDeviceListMutex);
321
322     EDRDevice *device = NULL;
323     CAResult_t res = CAGetEDRDevice(g_edrDeviceList, sdpInfo->remote_address, &device);
324     if (CA_STATUS_OK == res && NULL != device)
325     {
326         if (device->serviceSearched)
327         {
328             OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Service is already searched for this device!");
329             ca_mutex_unlock(g_edrDeviceListMutex);
330             return;
331         }
332
333         if (true == CAEDRIsServiceSupported((const char **)sdpInfo->service_uuid,
334                                            sdpInfo->service_count, OIC_EDR_SERVICE_ID))
335         {
336             device->serviceSearched = true;
337             res = CAEDRClientConnect(sdpInfo->remote_address, OIC_EDR_SERVICE_ID);
338             if (CA_STATUS_OK != res)
339             {
340                 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make rfcomm connection!");
341
342                 // Remove the device from device list
343                 CARemoveEDRDeviceFromList(&g_edrDeviceList, sdpInfo->remote_address);
344             }
345         }
346         else
347         {
348             OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Device does not contain OIC service!");
349
350             // Remove device from list as it does not support OIC service
351             CARemoveEDRDeviceFromList(&g_edrDeviceList, sdpInfo->remote_address);
352         }
353     }
354
355     ca_mutex_unlock(g_edrDeviceListMutex);
356
357     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
358 }
359
360 CAResult_t CAEDRStartDeviceDiscovery(void)
361 {
362     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
363
364
365     bool isDiscoveryStarted = false;
366
367     // Check the device discovery state
368     bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
369     if (BT_ERROR_NONE != err)
370     {
371         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
372                   err);
373         return CA_STATUS_FAILED;
374     }
375
376     //Start device discovery if its not started
377     if (false == isDiscoveryStarted)
378     {
379         err = bt_adapter_start_device_discovery();
380         if (BT_ERROR_NONE != err)
381         {
382             OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Device discovery failed!, error num [%x]",
383                       err);
384             return CA_STATUS_FAILED;
385         }
386     }
387
388     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
389     return CA_STATUS_OK;
390 }
391
392 CAResult_t CAEDRStopServiceSearch(void)
393 {
394     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
395
396     bt_error_e err = bt_device_cancel_service_search();
397     // Stop ongoing service search
398     if (BT_ERROR_NONE != err)
399     {
400         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
401                   err);
402         return CA_STATUS_FAILED;
403     }
404
405     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
406     return CA_STATUS_OK;
407 }
408
409 CAResult_t CAEDRStopDeviceDiscovery(void)
410 {
411     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
412
413     bool isDiscoveryStarted = false;
414     bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
415     // Check the device discovery state
416     if (BT_ERROR_NONE != err)
417     {
418         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
419                   err);
420         return CA_STATUS_FAILED;
421     }
422
423     //stop the device discovery process
424     if (true == isDiscoveryStarted)
425     {
426         OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Stopping the device search process");
427         if (BT_ERROR_NONE != (err = bt_adapter_stop_device_discovery()))
428         {
429             OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to stop discovery!, error num [%x]",
430                       err);
431         }
432     }
433
434     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
435     return CA_STATUS_OK;
436 }
437
438 CAResult_t CAEDRStartServiceSearch(const char *remoteAddress)
439 {
440     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
441
442     // Input validation
443     VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
444     if (!remoteAddress[0])
445     {
446         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Remote address is empty!");
447         return CA_STATUS_INVALID_PARAM;
448     }
449
450     bt_error_e err = bt_device_start_service_search(remoteAddress);
451     // Start searching for OIC service
452     if (BT_ERROR_NONE != err)
453     {
454         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
455                   err);
456         return CA_STATUS_FAILED;
457     }
458
459     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
460     return CA_STATUS_OK;
461 }
462
463 CAResult_t CAEDRClientSetCallbacks(void)
464 {
465     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
466
467     // Register for discovery and rfcomm socket connection callbacks
468     bt_adapter_set_device_discovery_state_changed_cb(CAEDRDeviceDiscoveryCallback, NULL);
469     bt_device_set_service_searched_cb(CAEDRServiceSearchedCallback, NULL);
470     bt_socket_set_connection_state_changed_cb(CAEDRSocketConnectionStateCallback, NULL);
471     bt_socket_set_data_received_cb(CAEDRDataRecvCallback, NULL);
472
473     // Start device discovery
474     CAResult_t result = CAEDRStartDeviceDiscovery();
475     if(CA_STATUS_OK != result)
476     {
477         OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Failed to Start Device discovery");
478         return CA_STATUS_FAILED;
479     }
480
481     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
482     return CA_STATUS_OK;
483 }
484
485
486 void CAEDRClientUnsetCallbacks(void)
487 {
488     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
489
490     // Stop service search
491     CAEDRStopServiceSearch();
492
493     // Stop the device discovery process
494     CAEDRStopDeviceDiscovery();
495
496     // reset bluetooth adapter callbacks
497     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Resetting the callbacks");
498     bt_adapter_unset_device_discovery_state_changed_cb();
499     bt_device_unset_service_searched_cb();
500     bt_socket_unset_connection_state_changed_cb();
501     bt_socket_unset_data_received_cb();
502
503     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
504 }
505
506 void CAEDRManagerInitializeMutex(void)
507 {
508     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
509
510     if (!g_edrDeviceListMutex)
511     {
512         g_edrDeviceListMutex = ca_mutex_new();
513     }
514
515     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
516 }
517
518 void CAEDRManagerTerminateMutex(void)
519 {
520     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
521
522     if (g_edrDeviceListMutex)
523     {
524         ca_mutex_free(g_edrDeviceListMutex);
525         g_edrDeviceListMutex = NULL;
526     }
527
528     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
529 }
530
531 void CAEDRInitializeClient(ca_thread_pool_t handle)
532 {
533     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
534     CAEDRManagerInitializeMutex();
535     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
536 }
537
538 void CAEDRClientTerminate()
539 {
540     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
541
542     // Free EDRDevices list
543     if (g_edrDeviceListMutex)
544     {
545         ca_mutex_lock(g_edrDeviceListMutex);
546         CADestroyEDRDeviceList(&g_edrDeviceList);
547         ca_mutex_unlock(g_edrDeviceListMutex);
548     }
549
550     // Free the mutex
551     CAEDRManagerTerminateMutex();
552     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
553 }
554
555 void CAEDRClientDisconnectAll(void)
556 {
557     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
558
559     ca_mutex_lock(g_edrDeviceListMutex);
560
561     EDRDeviceList *cur = g_edrDeviceList;
562     while (cur != NULL)
563     {
564         EDRDevice *device = cur->device;
565         cur = cur->next;
566
567         if (device && 0 <= device->socketFD)
568         {
569             CAResult_t result = CAEDRClientDisconnect(device->socketFD);
570             if (CA_STATUS_OK != result)
571             {
572                 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to disconnect with client :%s",
573                           device->remoteAddress);
574             }
575
576             device->socketFD = -1;
577         }
578     }
579
580     ca_mutex_unlock(g_edrDeviceListMutex);
581
582     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
583 }
584
585
586 CAResult_t CAEDRClientSendUnicastData(const char *remoteAddress, const void *data,
587                                       uint32_t dataLength)
588 {
589     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
590
591     EDRDevice *device = NULL;
592
593     // Input validation
594     VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
595     VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
596
597     if (0 >= dataLength)
598     {
599         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
600         return CA_STATUS_INVALID_PARAM;
601     }
602
603     // Check the connection existence with remote device
604     ca_mutex_lock(g_edrDeviceListMutex);
605     CAResult_t result = CAGetEDRDevice(g_edrDeviceList, remoteAddress, &device);
606     if (CA_STATUS_OK != result)
607     {
608         // Create new device and add to list
609         result = CACreateAndAddToDeviceList(&g_edrDeviceList, remoteAddress,
610                                             OIC_EDR_SERVICE_ID, &device);
611         if (CA_STATUS_OK != result)
612         {
613             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed create device and add to list!");
614
615             ca_mutex_unlock(g_edrDeviceListMutex);
616             return CA_STATUS_FAILED;
617         }
618
619         // Start the OIC service search newly created device
620         result = CAEDRStartServiceSearch(remoteAddress);
621         if (CA_STATUS_OK != result)
622         {
623             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to initiate service search!");
624
625             // Remove device from list
626             CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
627
628             ca_mutex_unlock(g_edrDeviceListMutex);
629             return CA_STATUS_FAILED;
630         }
631     }
632
633     if(!device)
634     {
635         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
636         // Remove device from list
637         CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
638
639         ca_mutex_unlock(g_edrDeviceListMutex);
640         return CA_STATUS_FAILED;
641     }
642
643     ca_mutex_unlock(g_edrDeviceListMutex);
644
645     if (-1 == device->socketFD)
646     {
647         // Adding to pending list
648         result = CAAddEDRDataToList(&device->pendingDataList, data,
649                                               dataLength);
650         if (CA_STATUS_OK != result)
651         {
652             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list!");
653
654             //Remove device from list
655             CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
656             return CA_STATUS_FAILED;
657         }
658
659         // Make a rfcomm connection with remote BT Device
660         if (device->serviceSearched &&
661             CA_STATUS_OK != CAEDRClientConnect(remoteAddress, OIC_EDR_SERVICE_ID))
662         {
663             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection!");
664
665             //Remove device from list
666             CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
667             return CA_STATUS_FAILED;
668         }
669     }
670     else
671     {
672         result = CAEDRSendData(device->socketFD, data, dataLength);
673         if (CA_STATUS_OK != result)
674         {
675             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to send data!");
676             return CA_STATUS_FAILED;
677         }
678     }
679
680     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
681     return CA_STATUS_OK;
682 }
683
684 CAResult_t CAEDRClientSendMulticastData(const void *data, uint32_t dataLength)
685 {
686     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
687
688     // Input validation
689     VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
690
691     if (0 >= dataLength)
692     {
693         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
694         return CA_STATUS_INVALID_PARAM;
695     }
696
697     // Send the packet to all OIC devices
698     ca_mutex_lock(g_edrDeviceListMutex);
699     EDRDeviceList *curList = g_edrDeviceList;
700     CAResult_t result = CA_STATUS_FAILED;
701     while (curList != NULL)
702     {
703         EDRDevice *device = curList->device;
704         curList = curList->next;
705
706         if (!device)
707         {
708             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
709             break;
710         }
711
712         if (-1 == device->socketFD)
713         {
714             OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN1");
715             // Check if the device service search is finished
716             if (false == device->serviceSearched)
717             {
718                 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Device services are still unknown!");
719                 continue;
720             }
721
722             // Adding to pendding list
723             result = CAAddEDRDataToList(&device->pendingDataList, data,
724                                                   dataLength);
725             if (CA_STATUS_OK != result)
726             {
727                 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list !");
728                 continue;
729             }
730
731             // Make a rfcomm connection with remote BT Device
732             result = CAEDRClientConnect(device->remoteAddress, device->serviceUUID);
733             if (CA_STATUS_OK != result)
734             {
735                 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection !");
736
737                 //Remove the data which added to pending list
738                 CARemoveEDRDataFromList(&device->pendingDataList);
739                 continue;
740             }
741             OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN2");
742         }
743         else
744         {
745             OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN3");
746             result = CAEDRSendData(device->socketFD, data, dataLength);
747             if (CA_STATUS_OK != result)
748             {
749                 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send data to [%s] !",
750                           device->remoteAddress);
751             }
752             OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN4");
753         }
754     }
755     ca_mutex_unlock(g_edrDeviceListMutex);
756
757     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
758     return CA_STATUS_OK;
759 }
760
761 CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID)
762 {
763     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
764
765     VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
766     VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "Service UUID is null");
767
768     size_t addressLen = strlen(remoteAddress);
769     if (0 == addressLen || CA_MACADDR_SIZE - 1 != addressLen)
770     {
771         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Invalid remote address");
772         return  CA_STATUS_INVALID_PARAM;
773     }
774
775     if (!serviceUUID[0])
776     {
777         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Empty service uuid");
778         return  CA_STATUS_INVALID_PARAM;
779     }
780
781     bt_error_e err = bt_socket_connect_rfcomm(remoteAddress, serviceUUID);
782     if (BT_ERROR_NONE != err)
783     {
784         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
785                   "Failed to connect!, address [%s] error num [%x]",
786                   remoteAddress, err);
787         return CA_STATUS_FAILED;
788     }
789
790     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
791     return CA_STATUS_OK;
792 }
793
794 CAResult_t CAEDRClientDisconnect(const int32_t clientID)
795 {
796     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
797
798     // Input validation
799     if (0 > clientID)
800     {
801         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: negative client id");
802         return CA_STATUS_INVALID_PARAM;
803     }
804
805     bt_error_e err = bt_socket_disconnect_rfcomm(clientID);
806     if (BT_ERROR_NONE != err)
807     {
808         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed close rfcomm client socket!, error num [%x]",
809                   err);
810         return CA_STATUS_FAILED;
811     }
812
813     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
814     return CA_STATUS_OK;
815 }
816
817 void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData)
818 {
819     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
820
821     EDRDevice *device = NULL;
822
823     if (NULL == data || 0 >= data->data_size)
824     {
825         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Data is null!");
826         return;
827     }
828
829     // Get EDR device from list
830     ca_mutex_lock(g_edrDeviceListMutex);
831     CAResult_t result = CAGetEDRDeviceBySocketId(g_edrDeviceList, data->socket_fd, &device);
832     if (CA_STATUS_OK != result)
833     {
834         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Could not find the device!");
835
836         ca_mutex_unlock(g_edrDeviceListMutex);
837         return;
838     }
839     ca_mutex_unlock(g_edrDeviceListMutex);
840
841     if (!device)
842     {
843         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
844         return;
845     }
846
847     uint32_t sentLength = 0;
848
849     g_edrPacketReceivedCallback(device->remoteAddress, data->data,
850                                 (uint32_t)data->data_size, &sentLength);
851
852     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
853 }