Corrected pointer arithmetic on void* variables.
[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,
587                                       const uint8_t *data,
588                                       uint32_t dataLength)
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(data, EDR_ADAPTER_TAG, "Data is null");
597
598     if (0 >= dataLength)
599     {
600         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
601         return CA_STATUS_INVALID_PARAM;
602     }
603
604     // Check the connection existence with remote device
605     ca_mutex_lock(g_edrDeviceListMutex);
606     CAResult_t result = CAGetEDRDevice(g_edrDeviceList, remoteAddress, &device);
607     if (CA_STATUS_OK != result)
608     {
609         // Create new device and add to list
610         result = CACreateAndAddToDeviceList(&g_edrDeviceList, remoteAddress,
611                                             OIC_EDR_SERVICE_ID, &device);
612         if (CA_STATUS_OK != result)
613         {
614             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed create device and add to list!");
615
616             ca_mutex_unlock(g_edrDeviceListMutex);
617             return CA_STATUS_FAILED;
618         }
619
620         // Start the OIC service search newly created device
621         result = CAEDRStartServiceSearch(remoteAddress);
622         if (CA_STATUS_OK != result)
623         {
624             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to initiate service search!");
625
626             // Remove device from list
627             CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
628
629             ca_mutex_unlock(g_edrDeviceListMutex);
630             return CA_STATUS_FAILED;
631         }
632     }
633
634     if(!device)
635     {
636         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "EDRDevice is null!");
637         // Remove device from list
638         CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
639
640         ca_mutex_unlock(g_edrDeviceListMutex);
641         return CA_STATUS_FAILED;
642     }
643
644     ca_mutex_unlock(g_edrDeviceListMutex);
645
646     if (-1 == device->socketFD)
647     {
648         // Adding to pending list
649         result = CAAddEDRDataToList(&device->pendingDataList, data,
650                                               dataLength);
651         if (CA_STATUS_OK != result)
652         {
653             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list!");
654
655             //Remove device from list
656             CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
657             return CA_STATUS_FAILED;
658         }
659
660         // Make a rfcomm connection with remote BT Device
661         if (device->serviceSearched &&
662             CA_STATUS_OK != CAEDRClientConnect(remoteAddress, OIC_EDR_SERVICE_ID))
663         {
664             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection!");
665
666             //Remove device from list
667             CARemoveEDRDeviceFromList(&g_edrDeviceList, remoteAddress);
668             return CA_STATUS_FAILED;
669         }
670     }
671     else
672     {
673         result = CAEDRSendData(device->socketFD, data, dataLength);
674         if (CA_STATUS_OK != result)
675         {
676             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to send data!");
677             return CA_STATUS_FAILED;
678         }
679     }
680
681     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
682     return CA_STATUS_OK;
683 }
684
685 CAResult_t CAEDRClientSendMulticastData(const uint8_t *data,
686                                         uint32_t dataLength)
687 {
688     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
689
690     // Input validation
691     VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null");
692
693     if (0 >= dataLength)
694     {
695         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Negative data length!");
696         return CA_STATUS_INVALID_PARAM;
697     }
698
699     // Send the packet to all OIC devices
700     ca_mutex_lock(g_edrDeviceListMutex);
701     EDRDeviceList *curList = g_edrDeviceList;
702     CAResult_t result = CA_STATUS_FAILED;
703     while (curList != NULL)
704     {
705         EDRDevice *device = curList->device;
706         curList = curList->next;
707
708         if (!device)
709         {
710             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
711             break;
712         }
713
714         if (-1 == device->socketFD)
715         {
716             OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN1");
717             // Check if the device service search is finished
718             if (false == device->serviceSearched)
719             {
720                 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Device services are still unknown!");
721                 continue;
722             }
723
724             // Adding to pendding list
725             result = CAAddEDRDataToList(&device->pendingDataList, data,
726                                                   dataLength);
727             if (CA_STATUS_OK != result)
728             {
729                 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to add data to pending list !");
730                 continue;
731             }
732
733             // Make a rfcomm connection with remote BT Device
734             result = CAEDRClientConnect(device->remoteAddress, device->serviceUUID);
735             if (CA_STATUS_OK != result)
736             {
737                 OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to make RFCOMM connection !");
738
739                 //Remove the data which added to pending list
740                 CARemoveEDRDataFromList(&device->pendingDataList);
741                 continue;
742             }
743             OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN2");
744         }
745         else
746         {
747             OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN3");
748             result = CAEDRSendData(device->socketFD, data, dataLength);
749             if (CA_STATUS_OK != result)
750             {
751                 OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to send data to [%s] !",
752                           device->remoteAddress);
753             }
754             OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN4");
755         }
756     }
757     ca_mutex_unlock(g_edrDeviceListMutex);
758
759     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
760     return CA_STATUS_OK;
761 }
762
763 CAResult_t CAEDRClientConnect(const char *remoteAddress, const char *serviceUUID)
764 {
765     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
766
767     VERIFY_NON_NULL(remoteAddress, EDR_ADAPTER_TAG, "Remote address is null");
768     VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "Service UUID is null");
769
770     size_t addressLen = strlen(remoteAddress);
771     if (0 == addressLen || CA_MACADDR_SIZE - 1 != addressLen)
772     {
773         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Invalid remote address");
774         return  CA_STATUS_INVALID_PARAM;
775     }
776
777     if (!serviceUUID[0])
778     {
779         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Empty service uuid");
780         return  CA_STATUS_INVALID_PARAM;
781     }
782
783     bt_error_e err = bt_socket_connect_rfcomm(remoteAddress, serviceUUID);
784     if (BT_ERROR_NONE != err)
785     {
786         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
787                   "Failed to connect!, address [%s] error num [%x]",
788                   remoteAddress, err);
789         return CA_STATUS_FAILED;
790     }
791
792     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
793     return CA_STATUS_OK;
794 }
795
796 CAResult_t CAEDRClientDisconnect(const int32_t clientID)
797 {
798     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
799
800     // Input validation
801     if (0 > clientID)
802     {
803         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: negative client id");
804         return CA_STATUS_INVALID_PARAM;
805     }
806
807     bt_error_e err = bt_socket_disconnect_rfcomm(clientID);
808     if (BT_ERROR_NONE != err)
809     {
810         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed close rfcomm client socket!, error num [%x]",
811                   err);
812         return CA_STATUS_FAILED;
813     }
814
815     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
816     return CA_STATUS_OK;
817 }
818
819 void CAEDRDataRecvCallback(bt_socket_received_data_s *data, void *userData)
820 {
821     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
822
823     EDRDevice *device = NULL;
824
825     if (NULL == data || 0 >= data->data_size)
826     {
827         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Data is null!");
828         return;
829     }
830
831     // Get EDR device from list
832     ca_mutex_lock(g_edrDeviceListMutex);
833     CAResult_t result = CAGetEDRDeviceBySocketId(g_edrDeviceList, data->socket_fd, &device);
834     if (CA_STATUS_OK != result)
835     {
836         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Could not find the device!");
837
838         ca_mutex_unlock(g_edrDeviceListMutex);
839         return;
840     }
841     ca_mutex_unlock(g_edrDeviceListMutex);
842
843     if (!device)
844     {
845         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "There is no device!");
846         return;
847     }
848
849     uint32_t sentLength = 0;
850
851     g_edrPacketReceivedCallback(device->remoteAddress,
852                                 (uint8_t *) data->data,
853                                 (uint32_t) data->data_size,
854                                 &sentLength);
855
856     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
857 }