00001 #include "config.h"
00002
00003 #ifdef OASYS_BLUETOOTH_ENABLED
00004
00005 #include "Bluetooth.h"
00006 #include "RFCOMMClient.h"
00007 #include <errno.h>
00008
00009 extern int errno;
00010
00011 namespace oasys {
00012
00013 int
00014 RFCOMMClient::rc_connect(bdaddr_t remote_addr)
00015 {
00016 int res = -1;
00017 char buff[18];
00018
00019 set_remote_addr(remote_addr);
00020
00021
00022 channel_ = RFCOMMChannel::next();
00023
00024 for (int k = 0; k < 30; k++) {
00025
00026 if ((res = bind()) != 0) {
00027
00028 close();
00029
00030 if (errno != EADDRINUSE) {
00031
00032 if ( silent_connect_ == false )
00033 log_err("error binding to %s:%d: %s",
00034 Bluetooth::batostr(&local_addr_,buff),
00035 channel_,
00036 strerror(errno));
00037
00038
00039 if (errno == EBADFD) {
00040
00041 }
00042
00043 break;
00044 }
00045
00046 if ( silent_connect_ == false )
00047 log_debug("can't bind to %s:%d: %s",
00048 Bluetooth::batostr(&local_addr_,buff),
00049 channel_,
00050 strerror(errno));
00051
00052 } else {
00053
00054
00055
00056 if ((res = connect()) == 0) {
00057
00058
00059 if ( silent_connect_ == false )
00060 log_debug("connected to %s:%d",
00061 Bluetooth::batostr(&remote_addr_,buff),
00062 channel_);
00063
00064 return res;
00065
00066 } else {
00067
00068 close();
00069
00070
00071 if ( silent_connect_ == false )
00072 log_debug("can't connect to %s:%d: %s",
00073 Bluetooth::batostr(&remote_addr_,buff),
00074 channel_,
00075 strerror(errno));
00076
00077
00078 if (errno == EBADFD) {
00079
00080 }
00081
00082 }
00083 }
00084
00085
00086 channel_ = RFCOMMChannel::next();
00087 }
00088
00089 log_err("Scanned all RFCOMM channels but unable to connect to %s",
00090 Bluetooth::batostr(&remote_addr_,buff));
00091 return -1;
00092 }
00093
00094 int
00095 RFCOMMClient::rc_connect()
00096 {
00097 ASSERT(bacmp(&remote_addr_,BDADDR_ANY)!=0);
00098 return rc_connect(remote_addr_);
00099 }
00100
00101
00102 }
00103
00104 #endif