Some quick code to use UDP-only (no TCP) with mBed. Echos received packets and sends packets when a button is pressed

Dependencies:   mbed

Committer:
pehrhovey
Date:
Sun Mar 14 00:54:12 2010 +0000
Revision:
0:a548a085de55

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pehrhovey 0:a548a085de55 1 #ifndef TCPCONNECTION_H
pehrhovey 0:a548a085de55 2 #define TCPCONNECTION_H
pehrhovey 0:a548a085de55 3
pehrhovey 0:a548a085de55 4 #include "arch/cc.h"
pehrhovey 0:a548a085de55 5 #include "lwip/err.h"
pehrhovey 0:a548a085de55 6 //#include "lwip/tcp.h"
pehrhovey 0:a548a085de55 7
pehrhovey 0:a548a085de55 8 #include "TCPItem.h"
pehrhovey 0:a548a085de55 9
pehrhovey 0:a548a085de55 10 namespace mbed {
pehrhovey 0:a548a085de55 11
pehrhovey 0:a548a085de55 12 class NetServer;
pehrhovey 0:a548a085de55 13 class TCPListener;
pehrhovey 0:a548a085de55 14
pehrhovey 0:a548a085de55 15 class TCPConnection : public TCPItem {
pehrhovey 0:a548a085de55 16 public:
pehrhovey 0:a548a085de55 17 TCPConnection(struct ip_addr, u16_t);
pehrhovey 0:a548a085de55 18 TCPConnection(TCPListener *, struct tcp_pcb *);
pehrhovey 0:a548a085de55 19
pehrhovey 0:a548a085de55 20 virtual ~TCPConnection();
pehrhovey 0:a548a085de55 21
pehrhovey 0:a548a085de55 22 void connect();
pehrhovey 0:a548a085de55 23
pehrhovey 0:a548a085de55 24 err_t write(void *, u16_t len, u8_t flags = TCP_WRITE_FLAG_COPY) const;
pehrhovey 0:a548a085de55 25
pehrhovey 0:a548a085de55 26 void recved(u32_t len) const;
pehrhovey 0:a548a085de55 27 u16_t sndbuf() const { return tcp_sndbuf(_pcb); }
pehrhovey 0:a548a085de55 28
pehrhovey 0:a548a085de55 29 void set_poll_timer(const u32_t &time) {
pehrhovey 0:a548a085de55 30 if(_pcb) {
pehrhovey 0:a548a085de55 31 _pcb->polltmr = time;
pehrhovey 0:a548a085de55 32 }
pehrhovey 0:a548a085de55 33 }
pehrhovey 0:a548a085de55 34
pehrhovey 0:a548a085de55 35 u32_t get_poll_interval() const {
pehrhovey 0:a548a085de55 36 return (_pcb)? _pcb->pollinterval: 0;
pehrhovey 0:a548a085de55 37 }
pehrhovey 0:a548a085de55 38
pehrhovey 0:a548a085de55 39 void set_poll_interval(const u32_t &value) {
pehrhovey 0:a548a085de55 40 if(_pcb) {
pehrhovey 0:a548a085de55 41 _pcb->pollinterval = value;
pehrhovey 0:a548a085de55 42 }
pehrhovey 0:a548a085de55 43 }
pehrhovey 0:a548a085de55 44
pehrhovey 0:a548a085de55 45 u32_t get_poll_timer() const {
pehrhovey 0:a548a085de55 46 return (_pcb)? _pcb->polltmr: 0;
pehrhovey 0:a548a085de55 47 }
pehrhovey 0:a548a085de55 48
pehrhovey 0:a548a085de55 49 protected:
pehrhovey 0:a548a085de55 50 TCPConnection();
pehrhovey 0:a548a085de55 51
pehrhovey 0:a548a085de55 52 /**
pehrhovey 0:a548a085de55 53 * Function to be called when more send buffer space is available.
pehrhovey 0:a548a085de55 54 * @param space the amount of bytes available
pehrhovey 0:a548a085de55 55 * @return ERR_OK: try to send some data by calling tcp_output
pehrhovey 0:a548a085de55 56 */
pehrhovey 0:a548a085de55 57 virtual err_t sent(u16_t space) = 0;
pehrhovey 0:a548a085de55 58
pehrhovey 0:a548a085de55 59 /**
pehrhovey 0:a548a085de55 60 * Function to be called when (in-sequence) data has arrived.
pehrhovey 0:a548a085de55 61 * @param p the packet buffer which arrived
pehrhovey 0:a548a085de55 62 * @param err an error argument (TODO: that is current always ERR_OK?)
pehrhovey 0:a548a085de55 63 * @return ERR_OK: try to send some data by calling tcp_output
pehrhovey 0:a548a085de55 64 */
pehrhovey 0:a548a085de55 65 virtual err_t recv(struct pbuf *p, err_t err) = 0;
pehrhovey 0:a548a085de55 66
pehrhovey 0:a548a085de55 67 /**
pehrhovey 0:a548a085de55 68 * Function to be called when a connection has been set up.
pehrhovey 0:a548a085de55 69 * @param pcb the tcp_pcb that now is connected
pehrhovey 0:a548a085de55 70 * @param err an error argument (TODO: that is current always ERR_OK?)
pehrhovey 0:a548a085de55 71 * @return value is currently ignored
pehrhovey 0:a548a085de55 72 */
pehrhovey 0:a548a085de55 73 virtual err_t connected(err_t err);
pehrhovey 0:a548a085de55 74
pehrhovey 0:a548a085de55 75 /**
pehrhovey 0:a548a085de55 76 * Function which is called periodically.
pehrhovey 0:a548a085de55 77 * The period can be adjusted in multiples of the TCP slow timer interval
pehrhovey 0:a548a085de55 78 * by changing tcp_pcb.polltmr.
pehrhovey 0:a548a085de55 79 * @return ERR_OK: try to send some data by calling tcp_output
pehrhovey 0:a548a085de55 80 */
pehrhovey 0:a548a085de55 81 virtual err_t poll() = 0;
pehrhovey 0:a548a085de55 82
pehrhovey 0:a548a085de55 83 /**
pehrhovey 0:a548a085de55 84 * Function to be called whenever a fatal error occurs.
pehrhovey 0:a548a085de55 85 * There is no pcb parameter since most of the times, the pcb is
pehrhovey 0:a548a085de55 86 * already deallocated (or there is no pcb) when this function is called.
pehrhovey 0:a548a085de55 87 * @param err an indication why the error callback is called:
pehrhovey 0:a548a085de55 88 * ERR_ABRT: aborted through tcp_abort or by a TCP timer
pehrhovey 0:a548a085de55 89 * ERR_RST: the connection was reset by the remote host
pehrhovey 0:a548a085de55 90 */
pehrhovey 0:a548a085de55 91 virtual void err(err_t err) = 0;
pehrhovey 0:a548a085de55 92
pehrhovey 0:a548a085de55 93 virtual void dnsreply(const char *hostname, struct ip_addr *addr) = 0;
pehrhovey 0:a548a085de55 94
pehrhovey 0:a548a085de55 95 err_t dnsrequest(const char *hostname, struct ip_addr *addr) const;
pehrhovey 0:a548a085de55 96
pehrhovey 0:a548a085de55 97 private:
pehrhovey 0:a548a085de55 98 static void dnsreply_callback(const char *name, struct ip_addr *ipaddr, void *arg);
pehrhovey 0:a548a085de55 99 static err_t connected_callback(void *arg, struct tcp_pcb *pcb, err_t err);
pehrhovey 0:a548a085de55 100 static err_t sent_callback(void *arg, struct tcp_pcb *pcb, u16_t space);
pehrhovey 0:a548a085de55 101 static err_t recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);
pehrhovey 0:a548a085de55 102 static err_t poll_callback(void *arg, struct tcp_pcb *pcb);
pehrhovey 0:a548a085de55 103 static void error_callback(void *arg, err_t erra);
pehrhovey 0:a548a085de55 104
pehrhovey 0:a548a085de55 105 protected:
pehrhovey 0:a548a085de55 106 TCPListener *_parent;
pehrhovey 0:a548a085de55 107 struct ip_addr _ipaddr;
pehrhovey 0:a548a085de55 108 u16_t _port;
pehrhovey 0:a548a085de55 109
pehrhovey 0:a548a085de55 110 friend class NetServer;
pehrhovey 0:a548a085de55 111 };
pehrhovey 0:a548a085de55 112
pehrhovey 0:a548a085de55 113 };
pehrhovey 0:a548a085de55 114
pehrhovey 0:a548a085de55 115 #endif /* TCPCONNECTION_H */