00001 #ifndef _OASYS_RFCOMM_SERVER_H_
00002 #define _OASYS_RFCOMM_SERVER_H_
00003
00004 #include "config.h"
00005
00006 #ifdef OASYS_BLUETOOTH_ENABLED
00007
00008 #include "BluetoothServer.h"
00009 #include "../thread/Thread.h"
00010
00011 namespace oasys {
00012
00013 class RFCOMMServer : public BluetoothServer {
00014 public:
00015 RFCOMMServer(char* logbase = "/rfcommserver")
00016 : BluetoothServer(SOCK_STREAM,BluetoothSocket::RFCOMM,logbase)
00017 {
00018 }
00019 };
00020
00021 class RFCOMMServerThread : public BluetoothServerThread {
00022 public:
00023 RFCOMMServerThread(char * logbase = "/rfcommserver", int flags = 0)
00024 : BluetoothServerThread(SOCK_STREAM,
00025 BluetoothSocket::RFCOMM,
00026 "oasys::RFCOMMServerThread",
00027 logbase,
00028 flags)
00029 {
00030 }
00031
00032 void set_accept_timeout(int ms) {
00033 accept_timeout_ = ms;
00034 }
00035
00036
00037 int rc_bind() {
00038 int res = -1;
00039 char buff[18];
00040
00041
00042 channel_ = RFCOMMChannel::next();
00043 for (int k = 0; k < 30; k++) {
00044 if ((res = bind()) != 0) {
00045
00046
00047 if (errno != EADDRINUSE) {
00048 log_err("error binding to %s:%d: %s",
00049 Bluetooth::batostr(&local_addr_,buff),
00050 channel_,
00051 strerror(errno));
00052 if (errno == EBADFD) close();
00053 return res;
00054 }
00055 } else {
00056
00057
00058 return res;
00059 }
00060
00061 channel_ = RFCOMMChannel::next();
00062 }
00063
00064 log_err("Scanned all local RFCOMM channels but unable to bind to %s",
00065 Bluetooth::batostr(&local_addr_,buff));
00066 return -1;
00067 }
00068 };
00069
00070 }
00071
00072 #endif
00073 #endif