RFCOMMClient.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2006 Baylor University
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 #include "config.h"
00018 
00019 #ifdef OASYS_BLUETOOTH_ENABLED
00020 
00021 #include "Bluetooth.h"
00022 #include "RFCOMMClient.h"
00023 #include <errno.h>
00024 
00025 extern int errno;
00026 
00027 namespace oasys {
00028 
00029 int
00030 RFCOMMClient::rc_connect(bdaddr_t remote_addr)
00031 {
00032     int res = -1;
00033 
00034     set_remote_addr(remote_addr);
00035 
00036     for (channel_ = 1; channel_ <= 30; channel_++) {
00037 
00038         if ((res = bind()) != 0) { 
00039 
00040             close();
00041 
00042             if (errno != EADDRINUSE) {
00043                 // something is borked
00044                 if ( params_.silent_connect_ == false )
00045                     log_err("error binding to %s:%d: %s",
00046                             bd2str(local_addr_), channel_, strerror(errno));
00047 
00048                 // unrecoverable
00049                 if (errno == EBADFD) {
00050                     //return -1;
00051                 }
00052 
00053                 break;
00054             }
00055 
00056             if ( params_.silent_connect_ == false )
00057                 log_debug("can't bind to %s:%d: %s",
00058                           bd2str(local_addr_), channel_, strerror(errno));
00059 
00060         } else {
00061 
00062             // local bind succeeded, now try remote connect
00063 
00064             if ((res = connect()) == 0) {
00065 
00066                 // success!
00067                 if ( params_.silent_connect_ == false )
00068                     log_debug("connected to %s:%d",
00069                               bd2str(remote_addr_), channel_);
00070 
00071                 return res;
00072 
00073             } else {
00074 
00075                 close();
00076 
00077                 // failed to connect; report it and move on
00078                 if ( params_.silent_connect_ == false )
00079                     log_debug("can't connect to %s:%d: %s",
00080                               bd2str(remote_addr_), channel_, strerror(errno)); 
00081 
00082                 // unrecoverable
00083                 if (errno == EBADFD) {
00084                     //return -1;
00085                 }
00086 
00087             }
00088         }
00089     }
00090 
00091     log_err("Scanned all RFCOMM channels but unable to connect to %s",
00092             bd2str(remote_addr_));
00093     return -1;
00094 }
00095 
00096 int
00097 RFCOMMClient::rc_connect()
00098 {
00099     ASSERT(bacmp(&remote_addr_,BDADDR_ANY)!=0);
00100     return rc_connect(remote_addr_);
00101 }
00102 
00103 
00104 }  // namespace oasys
00105 
00106 #endif /* OASYS_BLUETOOTH_ENABLED */

Generated on Sat Sep 8 08:36:18 2007 for DTN Reference Implementation by  doxygen 1.5.3