Corrected files with doxygen comments that were generating warnings.
[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                     uint32_t sentData = 0;
195                     EDRData *edrData = device->pendingDataList->data;
196                     res = CAEDRSendData(device->socketFD, edrData->data,
197                                                      edrData->dataLength, &sentData);
198                     if (CA_STATUS_OK != res)
199                     {
200                         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send pending data [%s]",
201                                   device->remoteAddress);
202
203                         // Remove all the data from pending list
204                         CADestroyEDRDataList(&device->pendingDataList);
205                         break;
206                     }
207
208                     // Remove the data which send from pending list
209                     CARemoveEDRDataFromList(&device->pendingDataList);
210                 }
211                 ca_mutex_unlock(g_edrDeviceListMutex);
212             }
213             break;
214
215         case BT_SOCKET_DISCONNECTED:
216             {
217                 ca_mutex_lock(g_edrDeviceListMutex);
218                 CARemoveEDRDeviceFromList(&g_edrDeviceList, connection->remote_address);
219                 ca_mutex_unlock(g_edrDeviceListMutex);
220             }
221             break;
222     }
223
224     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
225 }
226
227
228 void CAEDRDeviceDiscoveryCallback(int result, bt_adapter_device_discovery_state_e state,
229                                  bt_adapter_device_discovery_info_s *discoveryInfo, void *userData)
230 {
231     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
232
233     EDRDevice *device = NULL;
234
235     if (BT_ERROR_NONE != result)
236     {
237         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Received bad state!, error num [%x]",
238                   result);
239         return;
240     }
241
242     switch (state)
243     {
244         case BT_ADAPTER_DEVICE_DISCOVERY_STARTED:
245             {
246                 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Discovery started!");
247             }
248             break;
249
250         case BT_ADAPTER_DEVICE_DISCOVERY_FINISHED:
251             {
252                 OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Discovery finished!");
253             }
254             break;
255
256         case BT_ADAPTER_DEVICE_DISCOVERY_FOUND:
257             {
258                 OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Device discovered [%s]!",
259                           discoveryInfo->remote_name);
260                 if (true == CAEDRIsServiceSupported((const char **)discoveryInfo->service_uuid,
261                                                         discoveryInfo->service_count,
262                                                         OIC_EDR_SERVICE_ID))
263                 {
264                     // Check if the deivce is already in the list
265                     ca_mutex_lock(g_edrDeviceListMutex);
266                     if (CA_STATUS_OK == CAGetEDRDevice(g_edrDeviceList,
267                                                 discoveryInfo->remote_address, &device))
268                     {
269                         if(!device)
270                         {
271                             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
272                             ca_mutex_unlock(g_edrDeviceListMutex);
273                             return;
274                         }
275                         device->serviceSearched = true;
276                         ca_mutex_unlock(g_edrDeviceListMutex);
277                         return;
278                     }
279
280                     // Create the deviceinfo and add to list
281                     CAResult_t res = CACreateAndAddToDeviceList(&g_edrDeviceList,
282                             discoveryInfo->remote_address, OIC_EDR_SERVICE_ID, &device);
283                     if (CA_STATUS_OK != res)
284                     {
285                         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add device to list!");
286                         ca_mutex_unlock(g_edrDeviceListMutex);
287                         return;
288                     }
289
290                     if(!device)
291                     {
292                         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
293                         ca_mutex_unlock(g_edrDeviceListMutex);
294                         return;
295                     }
296                     device->serviceSearched = true;
297                     ca_mutex_unlock(g_edrDeviceListMutex);
298                 }
299                 else
300                 {
301                     OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Device does not support OIC service!");
302                 }
303             }
304             break;
305     }
306
307     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
308 }
309
310 void CAEDRServiceSearchedCallback(int32_t result,
311                 bt_device_sdp_info_s *sdpInfo,void *userData)
312 {
313     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
314
315     if (NULL == sdpInfo)
316     {
317         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "SDP info is null!");
318         return;
319     }
320
321     ca_mutex_lock(g_edrDeviceListMutex);
322
323     EDRDevice *device = NULL;
324     CAResult_t res = CAGetEDRDevice(g_edrDeviceList, sdpInfo->remote_address, &device);
325     if (CA_STATUS_OK == res && NULL != device)
326     {
327         if (device->serviceSearched)
328         {
329             OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Service is already searched for this device!");
330             ca_mutex_unlock(g_edrDeviceListMutex);
331             return;
332         }
333
334         if (true == CAEDRIsServiceSupported((const char **)sdpInfo->service_uuid,
335                                            sdpInfo->service_count, OIC_EDR_SERVICE_ID))
336         {
337             device->serviceSearched = true;
338             res = CAEDRClientConnect(sdpInfo->remote_address, OIC_EDR_SERVICE_ID);
339             if (CA_STATUS_OK != res)
340             {
341                 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make rfcomm connection!");
342
343                 // Remove the device from device list
344                 CARemoveEDRDeviceFromList(&g_edrDeviceList, sdpInfo->remote_address);
345             }
346         }
347         else
348         {
349             OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Device does not contain OIC service!");
350
351             // Remove device from list as it does not support OIC service
352             CARemoveEDRDeviceFromList(&g_edrDeviceList, sdpInfo->remote_address);
353         }
354     }
355
356     ca_mutex_unlock(g_edrDeviceListMutex);
357
358     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
359 }
360
361 CAResult_t CAEDRStartDeviceDiscovery(void)
362 {
363     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
364
365
366     bool isDiscoveryStarted = false;
367
368     // Check the device discovery state
369     bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
370     if (BT_ERROR_NONE != err)
371     {
372         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
373                   err);
374         return CA_STATUS_FAILED;
375     }
376
377     //Start device discovery if its not started
378     if (false == isDiscoveryStarted)
379     {
380         err = bt_adapter_start_device_discovery();
381         if (BT_ERROR_NONE != err)
382         {
383             OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Device discovery failed!, error num [%x]",
384                       err);
385             return CA_STATUS_FAILED;
386         }
387     }
388
389     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
390     return CA_STATUS_OK;
391 }
392
393 CAResult_t CAEDRStopServiceSearch(void)
394 {
395     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
396
397     bt_error_e err = bt_device_cancel_service_search();
398     // Stop ongoing service search
399     if (BT_ERROR_NONE != err)
400     {
401         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
402                   err);
403         return CA_STATUS_FAILED;
404     }
405
406     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
407     return CA_STATUS_OK;
408 }
409
410 CAResult_t CAEDRStopDeviceDiscovery(void)
411 {
412     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
413
414     bool isDiscoveryStarted = false;
415     bt_error_e err = bt_adapter_is_discovering(&isDiscoveryStarted);
416     // Check the device discovery state
417     if (BT_ERROR_NONE != err)
418     {
419         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to get discovery state!, error num [%x]",
420                   err);
421         return CA_STATUS_FAILED;
422     }
423
424     //stop the device discovery process
425     if (true == isDiscoveryStarted)
426     {
427         OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Stopping the device search process");
428         if (BT_ERROR_NONE != (err = bt_adapter_stop_device_discovery()))
429         {
430             OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to stop discovery!, error num [%x]",
431                       err);
432         }
433     }
434
435     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
436     return CA_STATUS_OK;
437 }
438
439 CAResult_t CAEDRStartServiceSearch(const char *remoteAddress)
440 {
441     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
442
443     // Input validation
444     VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
445     if (!remoteAddress[0])
446     {
447         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Remote address is empty!");
448         return CA_STATUS_INVALID_PARAM;
449     }
450
451     bt_error_e err = bt_device_start_service_search(remoteAddress);
452     // Start searching for OIC service
453     if (BT_ERROR_NONE != err)
454     {
455         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Get bonded device failed!, error num [%x]",
456                   err);
457         return CA_STATUS_FAILED;
458     }
459
460     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
461     return CA_STATUS_OK;
462 }
463
464 CAResult_t CAEDRClientSetCallbacks(void)
465 {
466     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
467
468     // Register for discovery and rfcomm socket connection callbacks
469     bt_adapter_set_device_discovery_state_changed_cb(CAEDRDeviceDiscoveryCallback, NULL);
470     bt_device_set_service_searched_cb(CAEDRServiceSearchedCallback, NULL);
471     bt_socket_set_connection_state_changed_cb(CAEDRSocketConnectionStateCallback, NULL);
472     bt_socket_set_data_received_cb(CAEDRDataRecvCallback, NULL);
473
474     // Start device discovery
475     CAResult_t result = CAEDRStartDeviceDiscovery();
476     if(CA_STATUS_OK != result)
477     {
478         OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Failed to Start Device discovery");
479         return CA_STATUS_FAILED;
480     }
481
482     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
483     return CA_STATUS_OK;
484 }
485
486
487 void CAEDRClientUnsetCallbacks(void)
488 {
489     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
490
491     // Stop service search
492     CAEDRStopServiceSearch();
493
494     // Stop the device discovery process
495     CAEDRStopDeviceDiscovery();
496
497     // reset bluetooth adapter callbacks
498     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Resetting the callbacks");
499     bt_adapter_unset_device_discovery_state_changed_cb();
500     bt_device_unset_service_searched_cb();
501     bt_socket_unset_connection_state_changed_cb();
502     bt_socket_unset_data_received_cb();
503
504     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
505 }
506
507 void CAEDRManagerInitializeMutex(void)
508 {
509     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
510
511     if (!g_edrDeviceListMutex)
512     {
513         g_edrDeviceListMutex = ca_mutex_new();
514     }
515
516     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
517 }
518
519 void CAEDRManagerTerminateMutex(void)
520 {
521     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
522
523     if (g_edrDeviceListMutex)
524     {
525         ca_mutex_free(g_edrDeviceListMutex);
526         g_edrDeviceListMutex = NULL;
527     }
528
529     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
530 }
531
532 void CAEDRInitializeClient(ca_thread_pool_t handle)
533 {
534     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
535     CAEDRManagerInitializeMutex();
536     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
537 }
538
539 void CAEDRClientTerminate()
540 {
541     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
542
543     // Free EDRDevices list
544     if (g_edrDeviceListMutex)
545     {
546         ca_mutex_lock(g_edrDeviceListMutex);
547         CADestroyEDRDeviceList(&g_edrDeviceList);
548         ca_mutex_unlock(g_edrDeviceListMutex);
549     }
550
551     // Free the mutex
552     CAEDRManagerTerminateMutex();
553     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
554 }
555
556 void CAEDRClientDisconnectAll(void)
557 {
558     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
559
560     ca_mutex_lock(g_edrDeviceListMutex);
561
562     EDRDeviceList *cur = g_edrDeviceList;
563     while (cur != NULL)
564     {
565         EDRDevice *device = cur->device;
566         cur = cur->next;
567
568         if (device && 0 <= device->socketFD)
569         {
570             CAResult_t result = CAEDRClientDisconnect(device->socketFD);
571             if (CA_STATUS_OK != result)
572             {
573                 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to disconnect with client :%s",
574                           device->remoteAddress);
575             }
576
577             device->socketFD = -1;
578         }
579     }
580
581     ca_mutex_unlock(g_edrDeviceListMutex);
582
583     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
584 }
585
586
587 CAResult_t CAEDRClientSendUnicastData(const char *remoteAddress, const char *serviceUUID,
588                                       const void *data, uint32_t dataLength, uint32_t *sentLength)
589 {
590     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
591
592     EDRDevice *device = NULL;
593
594     // Input validation
595     VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
596     VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "service UUID is null");
597     VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
598     VERIFY_NON_NULL(sentLength, EDR_ADAPTER_TAG, "Sent data length holder is null");
599
600     if (0 >= dataLength)
601     {
602         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
603         return CA_STATUS_INVALID_PARAM;
604     }
605
606     // Check the connection existence with remote device
607     ca_mutex_lock(g_edrDeviceListMutex);
608     CAResult_t result = CAGetEDRDevice(g_edrDeviceList, remoteAddress, &device);
609     if (CA_STATUS_OK != result)
610     {
611         // Create new device and add to list
612         result = CACreateAndAddToDeviceList(&g_edrDeviceList, remoteAddress,
613                                             OIC_EDR_SERVICE_ID, &device);
614         if (CA_STATUS_OK != result)
615         {
616             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed create device and add to list!");
617
618             ca_mutex_unlock(g_edrDeviceListMutex);
619             return CA_STATUS_FAILED;
620         }
621
622         // Start the OIC service search newly created device
623         result = CAEDRStartServiceSearch(remoteAddress);
624         if (CA_STATUS_OK != result)
625         {
626             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to initiate service search!");
627
628             // Remove device from list
629             CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
630
631             ca_mutex_unlock(g_edrDeviceListMutex);
632             return CA_STATUS_FAILED;
633         }
634     }
635
636     if(!device)
637     {
638         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
639         // Remove device from list
640         CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
641
642         ca_mutex_unlock(g_edrDeviceListMutex);
643         return CA_STATUS_FAILED;
644     }
645
646     ca_mutex_unlock(g_edrDeviceListMutex);
647
648     if (-1 == device->socketFD)
649     {
650         // Adding to pending list
651         result = CAAddEDRDataToList(&device->pendingDataList, data,
652                                               dataLength);
653         if (CA_STATUS_OK != result)
654         {
655             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list!");
656
657             //Remove device from list
658             CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
659             return CA_STATUS_FAILED;
660         }
661
662         // Make a rfcomm connection with remote BT Device
663         if (device->serviceSearched &&
664             CA_STATUS_OK != CAEDRClientConnect(remoteAddress, serviceUUID))
665         {
666             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection!");
667
668             //Remove device from list
669             CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
670             return CA_STATUS_FAILED;
671         }
672         *sentLength = dataLength;
673     }
674     else
675     {
676         result = CAEDRSendData(device->socketFD, data, dataLength, sentLength);
677         if (CA_STATUS_OK != result)
678         {
679             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to send data!");
680             return CA_STATUS_FAILED;
681         }
682     }
683
684     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
685     return CA_STATUS_OK;
686 }
687
688 CAResult_t CAEDRClientSendMulticastData(const char *serviceUUID, const void *data,
689                                         uint32_t dataLength, uint32_t *sentLength)
690 {
691     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
692
693     // Input validation
694     VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "service UUID is null");
695     VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
696     VERIFY_NON_NULL(sentLength, EDR_ADAPTER_TAG, "Sent data length holder is null");
697
698     if (0 >= dataLength)
699     {
700         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
701         return CA_STATUS_INVALID_PARAM;
702     }
703
704     *sentLength = dataLength;
705
706     // Send the packet to all OIC devices
707     ca_mutex_lock(g_edrDeviceListMutex);
708     EDRDeviceList *curList = g_edrDeviceList;
709     CAResult_t result = CA_STATUS_FAILED;
710     while (curList != NULL)
711     {
712         EDRDevice *device = curList->device;
713         curList = curList->next;
714
715         if (!device)
716         {
717             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
718             break;
719         }
720
721         if (-1 == device->socketFD)
722         {
723             OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN1");
724             // Check if the device service search is finished
725             if (false == device->serviceSearched)
726             {
727                 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Device services are still unknown!");
728                 continue;
729             }
730
731             // Adding to pendding list
732             result = CAAddEDRDataToList(&device->pendingDataList, data,
733                                                   dataLength);
734             if (CA_STATUS_OK != result)
735             {
736                 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list !");
737                 continue;
738             }
739
740             // Make a rfcomm connection with remote BT Device
741             result = CAEDRClientConnect(device->remoteAddress, device->serviceUUID);
742             if (CA_STATUS_OK != result)
743             {
744                 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection !");
745
746                 //Remove the data which added to pending list
747                 CARemoveEDRDataFromList(&device->pendingDataList);
748                 continue;
749             }
750             OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN2");
751         }
752         else
753         {
754             OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN3");
755             result = CAEDRSendData(device->socketFD, data, dataLength, sentLength);
756             if (CA_STATUS_OK != result)
757             {
758                 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send data to [%s] !",
759                           device->remoteAddress);
760             }
761             OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN4");
762         }
763     }
764     ca_mutex_unlock(g_edrDeviceListMutex);
765
766     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
767     return CA_STATUS_OK;
768 }
769
770 CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID)
771 {
772     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
773
774     VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
775     VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "Service UUID is null");
776
777     size_t addressLen = strlen(remoteAddress);
778     if (0 == addressLen || CA_MACADDR_SIZE - 1 != addressLen)
779     {
780         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Invalid remote address");
781         return  CA_STATUS_INVALID_PARAM;
782     }
783
784     if (!serviceUUID[0])
785     {
786         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Empty service uuid");
787         return  CA_STATUS_INVALID_PARAM;
788     }
789
790     bt_error_e err = bt_socket_connect_rfcomm(remoteAddress, serviceUUID);
791     if (BT_ERROR_NONE != err)
792     {
793         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
794                   "Failed to connect!, address [%s] error num [%x]",
795                   remoteAddress, err);
796         return CA_STATUS_FAILED;
797     }
798
799     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
800     return CA_STATUS_OK;
801 }
802
803 CAResult_t CAEDRClientDisconnect(const int32_t clientID)
804 {
805     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
806
807     // Input validation
808     if (0 > clientID)
809     {
810         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: negative client id");
811         return CA_STATUS_INVALID_PARAM;
812     }
813
814     bt_error_e err = bt_socket_disconnect_rfcomm(clientID);
815     if (BT_ERROR_NONE != err)
816     {
817         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed close rfcomm client socket!, error num [%x]",
818                   err);
819         return CA_STATUS_FAILED;
820     }
821
822     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
823     return CA_STATUS_OK;
824 }
825
826 void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData)
827 {
828     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
829
830     EDRDevice *device = NULL;
831
832     if (NULL == data || 0 >= data->data_size)
833     {
834         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Data is null!");
835         return;
836     }
837
838     // Get EDR device from list
839     ca_mutex_lock(g_edrDeviceListMutex);
840     CAResult_t result = CAGetEDRDeviceBySocketId(g_edrDeviceList, data->socket_fd, &device);
841     if (CA_STATUS_OK != result)
842     {
843         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Could not find the device!");
844
845         ca_mutex_unlock(g_edrDeviceListMutex);
846         return;
847     }
848     ca_mutex_unlock(g_edrDeviceListMutex);
849
850     if (!device)
851     {
852         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
853         return;
854     }
855
856     uint32_t sentLength = 0;
857
858     g_edrPacketReceivedCallback(device->remoteAddress, data->data,
859                                 (uint32_t)data->data_size, &sentLength);
860
861     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
862 }