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