BluetoothSocket.h

Go to the documentation of this file.
00001 #ifndef _OASYS_BT_SOCKET_H_
00002 #define _OASYS_BT_SOCKET_H_
00003 
00004 #include <config.h> 
00005 #ifdef OASYS_BLUETOOTH_ENABLED
00006 
00007 #include <stdlib.h>
00008 #include <sys/types.h>
00009 #include <sys/socket.h>
00010 
00011 #include <bluetooth/bluetooth.h>
00012 #include <bluetooth/hci.h>
00013 #include <bluetooth/rfcomm.h>
00014 
00015 #include "../io/IO.h"
00016 #include "Bluetooth.h"
00017 #include "../debug/Log.h"
00018 
00019 #include "../thread/SpinLock.h"
00020 
00021 namespace oasys {
00022 
00023 #ifndef BDADDR_ANY
00024 #define BDADDR_ANY   (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
00025 #endif
00026 
00031 class BluetoothSocket : public Logger,
00032                         virtual public IOHandlerBase {
00033 public:
00045     enum proto_t {
00046         L2CAP=0,
00047         HCI,
00048         SCO,
00049         RFCOMM,
00050         BNEP,
00051         CMTP,
00052         HIDP,
00053         AVDTP
00054     };
00055 
00056     BluetoothSocket(int socktype, proto_t proto, const char* logbase);
00057     BluetoothSocket(int socktype, proto_t proto, int fd, bdaddr_t remote_addr,
00058                     u_int8_t channel, const char* logbase);
00059     virtual ~BluetoothSocket();
00060 
00062     void configure();
00063 
00065 
00066     virtual int bind(bdaddr_t local_addr, u_int8_t channel);
00067     virtual int bind();
00068     virtual int connect(bdaddr_t remote_addr, u_int8_t channel);
00069     virtual int connect();
00070     virtual int close();
00071     virtual int shutdown(int how);
00072 
00073     virtual int send(const char* bp, size_t len, int flags);
00074     virtual int recv(char* bp, size_t len, int flags);
00075 
00077 
00081     int async_connect_result();
00082 
00084     virtual int poll_sockfd(int events, int* revents, int timeout_ms);
00085 
00086 
00088     enum state_t {
00089         INIT,           
00090         LISTENING,      
00091         CONNECTING,     
00092         ESTABLISHED,    
00093         RDCLOSED,       
00094         WRCLOSED,       
00095         CLOSED,         
00096         FINI            
00097     };
00098 
00099     enum sockaddr_t {
00100         ZERO,
00101         LOCAL,
00102         REMOTE
00103     };
00104 
00105     // cf <bits/socket.h>
00106     static const char* socktypetoa(int socktype) {
00107         switch(socktype) {
00108             case SOCK_STREAM:    return "SOCK_STREAM";
00109             case SOCK_DGRAM:     return "SOCK_DGRAM";
00110             case SOCK_RAW:       return "SOCK_RAW";
00111             case SOCK_RDM:       return "SOCK_RDM";
00112             case SOCK_SEQPACKET: return "SOCK_SEQPACKET";
00113             case SOCK_PACKET:    return "SOCK_PACKET";
00114             default:             return "UNKNOWN SOCKET TYPE";
00115         };
00116     }
00117 
00121     state_t state() { return state_; }
00122 
00127     struct bluetooth_socket_params {
00128         bluetooth_socket_params() :
00129             reuseaddr_    (true),
00130             recv_bufsize_ (0),
00131             send_bufsize_ (0) {}
00132         bool reuseaddr_;   // default: on
00133         int recv_bufsize_; // default: system setting
00134         int send_bufsize_; // default: system setting
00135     } params_;
00136 
00138     int fd();
00139 
00141     void local_addr(bdaddr_t& addr);
00142     bdaddr_t local_addr();
00143 
00145     u_int8_t channel();
00146 
00148     void remote_addr(bdaddr_t& addr);
00149     bdaddr_t remote_addr();
00150 
00152     void set_local_addr(bdaddr_t& addr);
00153 
00155     void set_remote_addr(bdaddr_t& addr);
00156 
00158     void set_channel(u_int8_t channel);
00159 
00160     // logfd can be set to false to disable the appending of the
00162     void set_logfd(bool logfd) { logfd_ = logfd; }
00163 
00164     bool reuse_addr() { return reuse_addr_; }
00165     void reuse_addr(bool b) { reuse_addr_ = b; }
00166     void init_socket(); 
00167 protected:
00168     void init_sa(int zero=(int)ZERO); 
00169     void set_state(state_t state);
00170     const char* statetoa(state_t state); 
00171     void set_proto(proto_t proto);
00172     const char* prototoa(proto_t proto); 
00173     void get_local();
00174     void get_remote(); 
00175     bdaddr_t* sa_baddr();
00176     u_int8_t sa_channel();
00177 
00178     static int abort_on_error_; 
00179     int fd_;
00180     int socktype_;
00181     state_t state_;
00182     int proto_;
00183     bool logfd_; 
00184     bdaddr_t local_addr_;
00185     bdaddr_t remote_addr_;
00186     u_int8_t channel_; 
00187     struct sockaddr* sa_;
00188     int slen_;
00189     struct sockaddr_rc* rc_;  /* BTPROTO_RFCOMM */
00190     bool reuse_addr_;
00191     bool silent_connect_;
00192 };
00193 
00194 class RFCOMMChannel {
00195 public:
00196     static int next();
00197 private:
00198     static int rc_channel_;
00199     static SpinLock lock_;
00200 }; // RFCOMMChannel
00201 
00202 } // namespace oasys
00203 
00204 #endif // OASYS_BLUETOOTH_ENABLED
00205 #endif // _OASYS_BT_SOCKET_H_

Generated on Fri Dec 22 14:47:57 2006 for DTN Reference Implementation by  doxygen 1.5.1