📄 aquaco_pas.c
字号:
#include "lwip/debug.h"
#include "lwip/stats.h"
#include "lwip/tcp.h"
#include "lwip/lcd.h"
#include "lwip/err.h"
#include "lwip/rbuf.h"
#include "lwip/aquaco_pas.h"
#pragma section FAR_CODE=RAMCODE /* located the program into RAM */
#pragma section CODE=RAMCODE
static char Exist_PAS=0;
static char num_pcb=0;
static const unsigned long ProtoID=0x31323334;
static const unsigned char SendIDSymb=0x35;
static const unsigned char SendExitSym=0xff;
static const unsigned long AquaStateID=0;
static const unsigned char SendStateSym=0x03;
static char IDF=0x30;
char pbuf_cnt=0;
static void
send_buf(struct tcp_pcb *pcb, struct pas_states *es);
/* Search function of active connection with the indicated port
and installation a flag of event */
void pas_event_set(int port) {
struct pas_states *es;
struct tcp_pcb *pcb;
for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
if(pcb->local_port == port) {
es=pcb->callback_arg;
es->Event++;
}
}
}
void pas_init() {
struct tcp_pcb *pcb;
pcb=tcp_new();
tcp_bind(pcb, IP_ADDR_ANY, PAS_PORT);
pcb=tcp_listen(pcb);
tcp_accept(pcb,pas_accept);
}
void pas_close(struct tcp_pcb *pcb, struct pas_states *es) {
if(es->pb==NULL) {
Put_String("Closing");
pbuf_free(es->pb);
mem_free(es->rbuf);
mem_free(es);
tcp_arg(pcb,NULL);
tcp_sent(pcb,NULL);
tcp_poll(pcb,NULL,6);
tcp_recv(pcb,NULL);
tcp_err(pcb,NULL);
tcp_close(pcb);
Exist_PAS--;
}
}
void pas_err (void *arg, err_t err) {
struct pas_states *es=arg;
Put_String("Error: ");
if(err==ERR_CLSD) Put_String("Error: Close ");
if(err==ERR_ABRT) Put_String("Error: Abort ");
if(err==ERR_RST) Put_String("Error: Reset ");
if(es!=NULL) {
//es->Error++;
pbuf_free(es->pb);
mem_free(es->rbuf);
mem_free(es);
if(Exist_PAS!=0) Exist_PAS--;
}
}
err_t pas_write(struct pas_states *es, const void *d, int len) {
struct pbuf *q;
q=pbuf_alloc(PBUF_RAW,len,PBUF_RAM);
if(q==NULL) {
Put_String("pas_write: ERR_MEM used ");
mprintf(stats.mem.used,5,0);
return ERR_MEM;
}
pbuf_cnt++;
bcopy(d,q->payload,len);
q->len=len;
if(es->pb!=NULL)
pbuf_chain(es->pb,q);
else es->pb=q;
// es->pb=q;
return ERR_OK;
}
static void
send_buf(struct tcp_pcb *pcb, struct pas_states *es)
/* [<][>][^][v][top][bottom][index][help] */
{
struct pbuf *q;
// Put_String("send_buf: ");
if(tcp_sndbuf(pcb) < es->pb->len) return;
q = es->pb;
es->pb = pbuf_dechain(q);
// mprintf(q->len,4,0);
if(tcp_write(pcb, q->payload, q->len, 1) == ERR_MEM) {
Put_String("send_buf: ERR_MEM ");
pbuf_chain(q, es->pb);
es->pb = q;
return;
}
pbuf_free(q);
pbuf_cnt--;
}
static err_t
pas_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
{
struct pas_states *es;
es=arg;
if(es != NULL && es->pb != NULL) {
Put_String("Send_buf:");
send_buf(pcb, es);
} else if(es->pb==NULL && es != NULL) es->Sent=1;
return ERR_OK;
}
err_t pas_poll(void *arg, struct tcp_pcb *pcb) {
struct pas_states *es;
es=arg;
// Put_String("Pool ");
/* Put_String("TCP state: ");
if(pcb->state==CLOSED) Put_String("Closed\n");
if(pcb->state==SYN_SENT) Put_String("SYN-SENT\n");
if(pcb->state==SYN_RCVD) Put_String("SYN-RCVD\n");
if(pcb->state==ESTABLISHED) Put_String("ESTABLISHED\n");
if(pcb->state==FIN_WAIT_1) Put_String("FIN_WAIT_1\n");
if(pcb->state==FIN_WAIT_2) Put_String("FIN_WAIT_2\n");
if(pcb->state==CLOSE_WAIT) Put_String("CLOSE_WAIT\n");
if(pcb->state==CLOSING) Put_String("CLOSING");
if(pcb->state==LAST_ACK) Put_String("LAST_ACK");
if(pcb->state==TIME_WAIT) Put_String("TIME_WAIT");*/
if(pcb->state==CLOSE_WAIT) pas_close(pcb,es);
if(es->Sent) {
es->Sent=0;
pas_sent(es,pcb,0);
}
return ERR_OK;
}
err_t pas_recv (void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) {
struct pas_states *es;
int ptr;
//Put_String("\nRecived:");
es=arg;
if(err==ERR_CLSD) Put_String("Error: Close ");
if(err==ERR_ABRT) Put_String("Error: Abort ");
if(err==ERR_RST) Put_String("Error: Reset ");
if(err==ERR_OK && es!=NULL) {
// Put_String("rbuf add ");
rbuf_add(es->rbuf,p);
tcp_recved(pcb,p->len);
pbuf_free(p);
}
if(err==ERR_OK && es==NULL) {
// Put_String("no data ");
pas_close(pcb,es);
}
//pas_write(es,"Asynhronus State Protocol for Aquaco ",37);
return ERR_OK;
}
/***************************************************************************/
/*********** Callback
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -