BluetoothSocket.h

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 #ifndef _OASYS_BT_SOCKET_H_
00018 #define _OASYS_BT_SOCKET_H_
00019 
00020 #include <config.h> 
00021 #ifdef OASYS_BLUETOOTH_ENABLED
00022 
00023 #include <stdlib.h>
00024 #include <sys/types.h>
00025 #include <sys/socket.h>
00026 
00027 #include <bluetooth/bluetooth.h>
00028 #include <bluetooth/hci.h>
00029 #include <bluetooth/rfcomm.h>
00030 
00031 #include "../io/IO.h"
00032 #include "Bluetooth.h"
00033 #include "../debug/Log.h"
00034 
00035 #include "../thread/SpinLock.h"
00036 
00037 namespace oasys {
00038 
00039 #ifndef BDADDR_ANY
00040 #define BDADDR_ANY   (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
00041 #endif
00042 
00047 class BluetoothSocket : public Logger,
00048                         virtual public IOHandlerBase {
00049 public:
00061     enum proto_t {
00062         L2CAP=0,
00063         HCI,
00064         SCO,
00065         RFCOMM,
00066         BNEP,
00067         CMTP,
00068         HIDP,
00069         AVDTP
00070     };
00071 
00072     BluetoothSocket(int socktype, proto_t proto, const char* logbase);
00073     BluetoothSocket(int socktype, proto_t proto, int fd, bdaddr_t remote_addr,
00074                     u_int8_t channel, const char* logbase);
00075     virtual ~BluetoothSocket();
00076 
00078     void configure();
00079 
00081 
00082     virtual int bind(bdaddr_t local_addr, u_int8_t channel);
00083     virtual int bind();
00084     virtual int connect(bdaddr_t remote_addr, u_int8_t channel);
00085     virtual int connect();
00086     virtual int close();
00087     virtual int shutdown(int how);
00088 
00089     virtual int send(const char* bp, size_t len, int flags);
00090     virtual int recv(char* bp, size_t len, int flags);
00091 
00093 
00097     int async_connect_result();
00098 
00100     virtual int poll_sockfd(int events, int* revents, int timeout_ms);
00101 
00102 
00104     enum state_t {
00105         INIT,           
00106         LISTENING,      
00107         CONNECTING,     
00108         ESTABLISHED,    
00109         RDCLOSED,       
00110         WRCLOSED,       
00111         CLOSED,         
00112         FINI            
00113     };
00114 
00115     enum sockaddr_t {
00116         ZERO,
00117         LOCAL,
00118         REMOTE
00119     };
00120 
00121     // cf <bits/socket.h>
00122     static const char* socktypetoa(int socktype) {
00123         switch(socktype) {
00124             case SOCK_STREAM:    return "SOCK_STREAM";
00125             case SOCK_DGRAM:     return "SOCK_DGRAM";
00126             case SOCK_RAW:       return "SOCK_RAW";
00127             case SOCK_RDM:       return "SOCK_RDM";
00128             case SOCK_SEQPACKET: return "SOCK_SEQPACKET";
00129             case SOCK_PACKET:    return "SOCK_PACKET";
00130             default:             return "UNKNOWN SOCKET TYPE";
00131         };
00132     }
00133 
00137     state_t state() { return state_; }
00138 
00143     struct bluetooth_socket_params {
00144         bluetooth_socket_params() :
00145             reuseaddr_      (true),
00146             silent_connect_ (false),
00147             recv_bufsize_   (0),
00148             send_bufsize_   (0) {}
00149         bool reuseaddr_;      // default: on
00150         bool silent_connect_; // default: off
00151         int recv_bufsize_;    // default: system setting
00152         int send_bufsize_;    // default: system setting
00153     } params_;
00154 
00156     int fd();
00157 
00159     void local_addr(bdaddr_t& addr);
00160 
00162     u_int8_t channel();
00163 
00165     void remote_addr(bdaddr_t& addr);
00166 
00168     void set_local_addr(bdaddr_t& addr);
00169 
00171     void set_remote_addr(bdaddr_t& addr);
00172 
00174     void set_channel(u_int8_t channel);
00175 
00176     // logfd can be set to false to disable the appending of the
00178     void set_logfd(bool logfd) { logfd_ = logfd; }
00179 
00180     void init_socket(); 
00181 protected:
00182     void set_state(state_t state);
00183     const char* statetoa(state_t state); 
00184     void set_proto(proto_t proto);
00185     const char* prototoa(proto_t proto); 
00186     void get_local();
00187     void get_remote(); 
00188 
00189     static int abort_on_error_; 
00190     int fd_;
00191     int socktype_;
00192     state_t state_;
00193     int proto_;
00194     bool logfd_; 
00195     bdaddr_t local_addr_;
00196     bdaddr_t remote_addr_;
00197     u_int8_t channel_; 
00198     struct sockaddr_rc* rc_;  /* BTPROTO_RFCOMM */
00199 };
00200 
00201 } // namespace oasys
00202 
00203 #endif // OASYS_BLUETOOTH_ENABLED
00204 #endif // _OASYS_BT_SOCKET_H_

Generated on Sat Sep 8 08:43:24 2007 for DTN Reference Implementation by  doxygen 1.5.3