00001
00002 #ifndef Wireless_h_DEFINED
00003 #define Wireless_h_DEFINED
00004
00005 #ifdef PLATFORM_APERIOS
00006 # include <OPENR/OObject.h>
00007 # include <OPENR/OSubject.h>
00008 # include <OPENR/OObserver.h>
00009 # include <ant.h>
00010 #else
00011 namespace ThreadNS {
00012 class Lock;
00013 }
00014 typedef unsigned int uint32;
00015 #endif
00016 #include "Socket.h"
00017 #include "DummySocket.h"
00018 #include <list>
00019
00020 using namespace SocketNS;
00021 using namespace __gnu_cxx;
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 class Wireless {
00037 public:
00038
00039 static const int WIRELESS_MAX_SOCKETS=100;
00040
00041
00042 static const int WIRELESS_DEF_RECV_SIZE=1024;
00043
00044
00045 static const int WIRELESS_DEF_SEND_SIZE=1024;
00046
00047
00048
00049
00050
00051
00052 Wireless();
00053 ~Wireless();
00054
00055
00056
00057
00058
00059
00060 Socket* socket(TransportType_t ttype);
00061
00062
00063
00064
00065 Socket* socket(TransportType_t ttype, int recvsize, int sendsize);
00066
00067
00068
00069
00070
00071
00072
00073 int listen(int sock, int port);
00074
00075
00076 int connect(int sock, const char* ipaddr, int port);
00077
00078 void setReceiver(int sock, int (*rcvcbckfn) (char*, int) );
00079
00080 void setDaemon(int sock, bool val=true) { sockets[sock]->daemon=val; }
00081
00082 bool getDaemon(int sock) { return sockets[sock]->daemon; }
00083
00084 void close(int sock);
00085
00086
00087
00088
00089
00090
00091 bool isConnected(int sock) {
00092 return sockets[sock]==NULL ? false : sockets[sock]->state==CONNECTION_CONNECTED;
00093 }
00094 bool isReady(int sock) { return !sockets[sock]->tx; }
00095 bool hasData(int sock) { return !sockets[sock]->rx; }
00096
00097
00098
00099
00100 void setReceiver(Socket &sobj, int (*rcvcbckfn) (char*, int) )
00101 { setReceiver(sobj.sock, rcvcbckfn); }
00102 void setReceiver(Socket *sobj, int (*rcvcbckfn) (char*, int) )
00103 { setReceiver(sobj->sock, rcvcbckfn); }
00104 void setDaemon(Socket &sobj, bool val=true) { setDaemon(sobj.sock, val); }
00105 void setDaemon(Socket *sobj, bool val=true) { setDaemon(sobj->sock, val); }
00106 bool getDaemon(Socket &sobj) { return getDaemon(sobj.sock); }
00107 bool getDaemon(Socket *sobj) { return getDaemon(sobj->sock); }
00108 int listen(Socket &sobj, int port) { return listen(sobj.sock, port); }
00109 int listen(Socket *sobj, int port) { return listen(sobj->sock, port); }
00110 int connect(Socket &sobj, const char* ipaddr, int port)
00111 { return connect (sobj.sock, ipaddr, port); }
00112 int connect(Socket *sobj, const char* ipaddr, int port)
00113 { return connect (sobj->sock, ipaddr, port); }
00114 void close(Socket &sobj) { close(sobj.sock); }
00115 void close(Socket *sobj) { close(sobj->sock); }
00116 unsigned int getNumInterfaces() { return 1; }
00117 uint32 getIPAddress(unsigned int idx=0);
00118
00119
00120
00121
00122 void receive(int sock, int (*rcvcbckfn) (char*, int) );
00123 void receive(int sock);
00124
00125
00126
00127
00128
00129 void send(int sock);
00130 void blockingSend(int sock);
00131
00132
00133 #ifdef PLATFORM_APERIOS
00134
00135
00136
00137 void ListenCont (void* msg);
00138 void BindCont (void* msg);
00139 void ConnectCont(void* msg);
00140 void SendCont (void* msg);
00141 void ReceiveCont(void* msg);
00142 void CloseCont (void* msg);
00143
00144
00145 #else
00146 void pollSetup();
00147 bool pollTest(struct timeval* tv);
00148 void pollProcess();
00149 void wakeup(Socket * del=NULL);
00150
00151 #endif
00152
00153 protected:
00154 friend class Socket;
00155 static const int MAXCONNECTIONS = 5;
00156
00157
00158
00159 #ifdef PLATFORM_APERIOS
00160 antStackRef ipstackRef;
00161 OID myOID;
00162 #else
00163 static ThreadNS::Lock& getLock();
00164 int interruptChk;
00165 int interruptCtl;
00166 fd_set rfds;
00167 fd_set wfds;
00168 fd_set efds;
00169 int fdsMax;
00170 #endif
00171 Socket* sockets[WIRELESS_MAX_SOCKETS];
00172 std::list<int> freeSockets;
00173 std::list<int> usedSockets;
00174
00175
00176 private:
00177 Wireless(const Wireless&);
00178 Wireless& operator= (const Wireless&);
00179 };
00180
00181
00182 extern Wireless* wireless;
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197 #endif // Wireless_h_DEFINED