Some quick code to use UDP-only (no TCP) with mBed. Echos received packets and sends packets when a button is pressed
TCPConnection.h
00001 #ifndef TCPCONNECTION_H 00002 #define TCPCONNECTION_H 00003 00004 #include "arch/cc.h" 00005 #include "lwip/err.h" 00006 //#include "lwip/tcp.h" 00007 00008 #include "TCPItem.h" 00009 00010 namespace mbed { 00011 00012 class NetServer; 00013 class TCPListener; 00014 00015 class TCPConnection : public TCPItem { 00016 public: 00017 TCPConnection(struct ip_addr, u16_t); 00018 TCPConnection(TCPListener *, struct tcp_pcb *); 00019 00020 virtual ~TCPConnection(); 00021 00022 void connect(); 00023 00024 err_t write(void *, u16_t len, u8_t flags = TCP_WRITE_FLAG_COPY) const; 00025 00026 void recved(u32_t len) const; 00027 u16_t sndbuf() const { return tcp_sndbuf(_pcb); } 00028 00029 void set_poll_timer(const u32_t &time) { 00030 if(_pcb) { 00031 _pcb->polltmr = time; 00032 } 00033 } 00034 00035 u32_t get_poll_interval() const { 00036 return (_pcb)? _pcb->pollinterval: 0; 00037 } 00038 00039 void set_poll_interval(const u32_t &value) { 00040 if(_pcb) { 00041 _pcb->pollinterval = value; 00042 } 00043 } 00044 00045 u32_t get_poll_timer() const { 00046 return (_pcb)? _pcb->polltmr: 0; 00047 } 00048 00049 protected: 00050 TCPConnection(); 00051 00052 /** 00053 * Function to be called when more send buffer space is available. 00054 * @param space the amount of bytes available 00055 * @return ERR_OK: try to send some data by calling tcp_output 00056 */ 00057 virtual err_t sent(u16_t space) = 0; 00058 00059 /** 00060 * Function to be called when (in-sequence) data has arrived. 00061 * @param p the packet buffer which arrived 00062 * @param err an error argument (TODO: that is current always ERR_OK?) 00063 * @return ERR_OK: try to send some data by calling tcp_output 00064 */ 00065 virtual err_t recv(struct pbuf *p, err_t err) = 0; 00066 00067 /** 00068 * Function to be called when a connection has been set up. 00069 * @param pcb the tcp_pcb that now is connected 00070 * @param err an error argument (TODO: that is current always ERR_OK?) 00071 * @return value is currently ignored 00072 */ 00073 virtual err_t connected(err_t err); 00074 00075 /** 00076 * Function which is called periodically. 00077 * The period can be adjusted in multiples of the TCP slow timer interval 00078 * by changing tcp_pcb.polltmr. 00079 * @return ERR_OK: try to send some data by calling tcp_output 00080 */ 00081 virtual err_t poll() = 0; 00082 00083 /** 00084 * Function to be called whenever a fatal error occurs. 00085 * There is no pcb parameter since most of the times, the pcb is 00086 * already deallocated (or there is no pcb) when this function is called. 00087 * @param err an indication why the error callback is called: 00088 * ERR_ABRT: aborted through tcp_abort or by a TCP timer 00089 * ERR_RST: the connection was reset by the remote host 00090 */ 00091 virtual void err(err_t err) = 0; 00092 00093 virtual void dnsreply(const char *hostname, struct ip_addr *addr) = 0; 00094 00095 err_t dnsrequest(const char *hostname, struct ip_addr *addr) const; 00096 00097 private: 00098 static void dnsreply_callback(const char *name, struct ip_addr *ipaddr, void *arg); 00099 static err_t connected_callback(void *arg, struct tcp_pcb *pcb, err_t err); 00100 static err_t sent_callback(void *arg, struct tcp_pcb *pcb, u16_t space); 00101 static err_t recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err); 00102 static err_t poll_callback(void *arg, struct tcp_pcb *pcb); 00103 static void error_callback(void *arg, err_t erra); 00104 00105 protected: 00106 TCPListener *_parent; 00107 struct ip_addr _ipaddr; 00108 u16_t _port; 00109 00110 friend class NetServer; 00111 }; 00112 00113 }; 00114 00115 #endif /* TCPCONNECTION_H */
Generated on Tue Jul 12 2022 19:17:24 by 1.7.2