1 #include "netio.h"
2
3 #include "lwip/opt.h"
4 #include "lwip/tcp.h"
5
6 /* See http://www.nwlab.net/art/netio/netio.html to get the netio tool */
7
8 #if LWIP_TCP && LWIP_CALLBACK_API
9 static err_t
netio_recv(void * arg,struct tcp_pcb * pcb,struct pbuf * p,err_t err)10 netio_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
11 {
12 LWIP_UNUSED_ARG(arg);
13
14 if (err == ERR_OK && p != NULL) {
15 tcp_recved(pcb, p->tot_len);
16 pbuf_free(p);
17 } else {
18 pbuf_free(p);
19 }
20
21 if (err == ERR_OK && p == NULL) {
22 tcp_arg(pcb, NULL);
23 tcp_sent(pcb, NULL);
24 tcp_recv(pcb, NULL);
25 tcp_close(pcb);
26 }
27
28 return ERR_OK;
29 }
30
31 static err_t
netio_accept(void * arg,struct tcp_pcb * pcb,err_t err)32 netio_accept(void *arg, struct tcp_pcb *pcb, err_t err)
33 {
34 LWIP_UNUSED_ARG(arg);
35 LWIP_UNUSED_ARG(err);
36
37 if (pcb != NULL) {
38 tcp_arg(pcb, NULL);
39 tcp_sent(pcb, NULL);
40 tcp_recv(pcb, netio_recv);
41 }
42 return ERR_OK;
43 }
44
45 void
netio_init(void)46 netio_init(void)
47 {
48 struct tcp_pcb *pcb;
49
50 pcb = tcp_new_ip_type(IPADDR_TYPE_ANY);
51 tcp_bind(pcb, IP_ANY_TYPE, 18767);
52 pcb = tcp_listen(pcb);
53 tcp_accept(pcb, netio_accept);
54 }
55 #endif /* LWIP_TCP && LWIP_CALLBACK_API */
56