| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| #include "lwip/opt.h" |
| #include "lwip/debug.h" |
| #include "lwip/stats.h" |
| #include "lwip/tcp.h" |
|
|
| #if LWIP_TCP |
|
|
| static struct tcp_pcb *echo_pcb; |
|
|
| enum echo_states |
| { |
| ES_NONE = 0, |
| ES_ACCEPTED, |
| ES_RECEIVED, |
| ES_CLOSING |
| }; |
|
|
| struct echo_state |
| { |
| u8_t state; |
| u8_t retries; |
| struct tcp_pcb *pcb; |
| |
| struct pbuf *p; |
| }; |
|
|
| err_t echo_accept(void *arg, struct tcp_pcb *newpcb, err_t err); |
| err_t echo_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err); |
| void echo_error(void *arg, err_t err); |
| err_t echo_poll(void *arg, struct tcp_pcb *tpcb); |
| err_t echo_sent(void *arg, struct tcp_pcb *tpcb, u16_t len); |
| void echo_send(struct tcp_pcb *tpcb, struct echo_state *es); |
| void echo_close(struct tcp_pcb *tpcb, struct echo_state *es); |
|
|
| void |
| echo_init(void) |
| { |
| echo_pcb = tcp_new(); |
| if (echo_pcb != NULL) |
| { |
| err_t err; |
|
|
| err = tcp_bind(echo_pcb, IP_ADDR_ANY, 7); |
| if (err == ERR_OK) |
| { |
| echo_pcb = tcp_listen(echo_pcb); |
| tcp_accept(echo_pcb, echo_accept); |
| } |
| else |
| { |
| |
| } |
| } |
| else |
| { |
| |
| } |
| } |
|
|
|
|
| err_t |
| echo_accept(void *arg, struct tcp_pcb *newpcb, err_t err) |
| { |
| err_t ret_err; |
| struct echo_state *es; |
|
|
| LWIP_UNUSED_ARG(arg); |
| LWIP_UNUSED_ARG(err); |
|
|
| |
| |
| |
| tcp_setprio(newpcb, TCP_PRIO_MIN); |
|
|
| es = (struct echo_state *)mem_malloc(sizeof(struct echo_state)); |
| if (es != NULL) |
| { |
| es->state = ES_ACCEPTED; |
| es->pcb = newpcb; |
| es->retries = 0; |
| es->p = NULL; |
| |
| tcp_arg(newpcb, es); |
| tcp_recv(newpcb, echo_recv); |
| tcp_err(newpcb, echo_error); |
| tcp_poll(newpcb, echo_poll, 0); |
| ret_err = ERR_OK; |
| } |
| else |
| { |
| ret_err = ERR_MEM; |
| } |
| return ret_err; |
| } |
|
|
| err_t |
| echo_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) |
| { |
| struct echo_state *es; |
| err_t ret_err; |
|
|
| LWIP_ASSERT("arg != NULL",arg != NULL); |
| es = (struct echo_state *)arg; |
| if (p == NULL) |
| { |
| |
| es->state = ES_CLOSING; |
| if(es->p == NULL) |
| { |
| |
| echo_close(tpcb, es); |
| } |
| else |
| { |
| |
| tcp_sent(tpcb, echo_sent); |
| echo_send(tpcb, es); |
| } |
| ret_err = ERR_OK; |
| } |
| else if(err != ERR_OK) |
| { |
| |
| if (p != NULL) |
| { |
| es->p = NULL; |
| pbuf_free(p); |
| } |
| ret_err = err; |
| } |
| else if(es->state == ES_ACCEPTED) |
| { |
| |
| es->state = ES_RECEIVED; |
| |
| es->p = p; |
| |
| tcp_sent(tpcb, echo_sent); |
| echo_send(tpcb, es); |
| ret_err = ERR_OK; |
| } |
| else if (es->state == ES_RECEIVED) |
| { |
| |
| if(es->p == NULL) |
| { |
| es->p = p; |
| tcp_sent(tpcb, echo_sent); |
| echo_send(tpcb, es); |
| } |
| else |
| { |
| struct pbuf *ptr; |
|
|
| |
| ptr = es->p; |
| pbuf_chain(ptr,p); |
| } |
| ret_err = ERR_OK; |
| } |
| else if(es->state == ES_CLOSING) |
| { |
| |
| tcp_recved(tpcb, p->tot_len); |
| es->p = NULL; |
| pbuf_free(p); |
| ret_err = ERR_OK; |
| } |
| else |
| { |
| |
| tcp_recved(tpcb, p->tot_len); |
| es->p = NULL; |
| pbuf_free(p); |
| ret_err = ERR_OK; |
| } |
| return ret_err; |
| } |
|
|
| void |
| echo_error(void *arg, err_t err) |
| { |
| struct echo_state *es; |
|
|
| LWIP_UNUSED_ARG(err); |
|
|
| es = (struct echo_state *)arg; |
| if (es != NULL) |
| { |
| mem_free(es); |
| } |
| } |
|
|
| err_t |
| echo_poll(void *arg, struct tcp_pcb *tpcb) |
| { |
| err_t ret_err; |
| struct echo_state *es; |
|
|
| es = (struct echo_state *)arg; |
| if (es != NULL) |
| { |
| if (es->p != NULL) |
| { |
| |
| tcp_sent(tpcb, echo_sent); |
| echo_send(tpcb, es); |
| } |
| else |
| { |
| |
| if(es->state == ES_CLOSING) |
| { |
| echo_close(tpcb, es); |
| } |
| } |
| ret_err = ERR_OK; |
| } |
| else |
| { |
| |
| tcp_abort(tpcb); |
| ret_err = ERR_ABRT; |
| } |
| return ret_err; |
| } |
|
|
| err_t |
| echo_sent(void *arg, struct tcp_pcb *tpcb, u16_t len) |
| { |
| struct echo_state *es; |
|
|
| LWIP_UNUSED_ARG(len); |
|
|
| es = (struct echo_state *)arg; |
| es->retries = 0; |
| |
| if(es->p != NULL) |
| { |
| |
| tcp_sent(tpcb, echo_sent); |
| echo_send(tpcb, es); |
| } |
| else |
| { |
| |
| if(es->state == ES_CLOSING) |
| { |
| echo_close(tpcb, es); |
| } |
| } |
| return ERR_OK; |
| } |
|
|
| void |
| echo_send(struct tcp_pcb *tpcb, struct echo_state *es) |
| { |
| struct pbuf *ptr; |
| err_t wr_err = ERR_OK; |
| |
| while ((wr_err == ERR_OK) && |
| (es->p != NULL) && |
| (es->p->len <= tcp_sndbuf(tpcb))) |
| { |
| ptr = es->p; |
|
|
| |
| wr_err = tcp_write(tpcb, ptr->payload, ptr->len, 1); |
| if (wr_err == ERR_OK) |
| { |
| u16_t plen; |
| u8_t freed; |
|
|
| plen = ptr->len; |
| |
| es->p = ptr->next; |
| if(es->p != NULL) |
| { |
| |
| pbuf_ref(es->p); |
| } |
| |
| do |
| { |
| |
| freed = pbuf_free(ptr); |
| } |
| while(freed == 0); |
| |
| tcp_recved(tpcb, plen); |
| } |
| else if(wr_err == ERR_MEM) |
| { |
| |
| es->p = ptr; |
| } |
| else |
| { |
| |
| } |
| } |
| } |
|
|
| void |
| echo_close(struct tcp_pcb *tpcb, struct echo_state *es) |
| { |
| tcp_arg(tpcb, NULL); |
| tcp_sent(tpcb, NULL); |
| tcp_recv(tpcb, NULL); |
| tcp_err(tpcb, NULL); |
| tcp_poll(tpcb, NULL, 0); |
| |
| if (es != NULL) |
| { |
| mem_free(es); |
| } |
| tcp_close(tpcb); |
| } |
|
|
| #endif |
|
|