|
|
@ -16,20 +16,20 @@ |
|
|
|
#define MAX_TXBUF 1024 |
|
|
|
#define MAX_TXBUF 1024 |
|
|
|
|
|
|
|
|
|
|
|
enum TcpState { |
|
|
|
enum TcpState { |
|
|
|
TCP_idle, // unused connection
|
|
|
|
TCP_idle, // unused connection
|
|
|
|
TCP_dns, // doing gethostbyname
|
|
|
|
TCP_dns, // doing gethostbyname
|
|
|
|
TCP_conn, // connecting to remote server
|
|
|
|
TCP_conn, // connecting to remote server
|
|
|
|
TCP_data, // connected
|
|
|
|
TCP_data, // connected
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// Connections
|
|
|
|
// Connections
|
|
|
|
typedef struct { |
|
|
|
typedef struct { |
|
|
|
struct espconn *conn; // esp connection structure
|
|
|
|
struct espconn *conn; // esp connection structure
|
|
|
|
esp_tcp *tcp; // esp TCP parameters
|
|
|
|
esp_tcp *tcp; // esp TCP parameters
|
|
|
|
char *txBuf; // buffer to accumulate into
|
|
|
|
char *txBuf; // buffer to accumulate into
|
|
|
|
char *txBufSent; // buffer held by espconn
|
|
|
|
char *txBufSent; // buffer held by espconn
|
|
|
|
uint8_t txBufLen; // number of chars in txbuf
|
|
|
|
uint8_t txBufLen; // number of chars in txbuf
|
|
|
|
enum TcpState state; |
|
|
|
enum TcpState state; |
|
|
|
} TcpConn; |
|
|
|
} TcpConn; |
|
|
|
|
|
|
|
|
|
|
|
static TcpConn tcpConn[MAX_CHAN]; |
|
|
|
static TcpConn tcpConn[MAX_CHAN]; |
|
|
@ -49,46 +49,46 @@ static void tcpRecvCb(void *arg, char *data, uint16_t len); |
|
|
|
// Allocate a new connection dynamically and return it. Returns NULL if buf alloc failed
|
|
|
|
// Allocate a new connection dynamically and return it. Returns NULL if buf alloc failed
|
|
|
|
static TcpConn* ICACHE_FLASH_ATTR |
|
|
|
static TcpConn* ICACHE_FLASH_ATTR |
|
|
|
tcpConnAlloc(uint8_t chan) { |
|
|
|
tcpConnAlloc(uint8_t chan) { |
|
|
|
TcpConn *tci = tcpConn+chan; |
|
|
|
TcpConn *tci = tcpConn+chan; |
|
|
|
if (tci->state != TCP_idle && tci->conn != NULL) return tci; |
|
|
|
if (tci->state != TCP_idle && tci->conn != NULL) return tci; |
|
|
|
|
|
|
|
|
|
|
|
// malloc and return espconn struct
|
|
|
|
// malloc and return espconn struct
|
|
|
|
tci->conn = os_malloc(sizeof(struct espconn)); |
|
|
|
tci->conn = os_malloc(sizeof(struct espconn)); |
|
|
|
if (tci->conn == NULL) goto fail; |
|
|
|
if (tci->conn == NULL) goto fail; |
|
|
|
memset(tci->conn, 0, sizeof(struct espconn)); |
|
|
|
memset(tci->conn, 0, sizeof(struct espconn)); |
|
|
|
// malloc esp_tcp struct
|
|
|
|
// malloc esp_tcp struct
|
|
|
|
tci->tcp = os_malloc(sizeof(esp_tcp)); |
|
|
|
tci->tcp = os_malloc(sizeof(esp_tcp)); |
|
|
|
if (tci->tcp == NULL) goto fail; |
|
|
|
if (tci->tcp == NULL) goto fail; |
|
|
|
memset(tci->tcp, 0, sizeof(esp_tcp)); |
|
|
|
memset(tci->tcp, 0, sizeof(esp_tcp)); |
|
|
|
|
|
|
|
|
|
|
|
// common init
|
|
|
|
// common init
|
|
|
|
tci->state = TCP_dns; |
|
|
|
tci->state = TCP_dns; |
|
|
|
tci->conn->type = ESPCONN_TCP; |
|
|
|
tci->conn->type = ESPCONN_TCP; |
|
|
|
tci->conn->state = ESPCONN_NONE; |
|
|
|
tci->conn->state = ESPCONN_NONE; |
|
|
|
tci->conn->proto.tcp = tci->tcp; |
|
|
|
tci->conn->proto.tcp = tci->tcp; |
|
|
|
tci->tcp->remote_port = 80; |
|
|
|
tci->tcp->remote_port = 80; |
|
|
|
espconn_regist_connectcb(tci->conn, tcpConnectCb); |
|
|
|
espconn_regist_connectcb(tci->conn, tcpConnectCb); |
|
|
|
espconn_regist_reconcb(tci->conn, tcpResetCb); |
|
|
|
espconn_regist_reconcb(tci->conn, tcpResetCb); |
|
|
|
espconn_regist_sentcb(tci->conn, tcpSentCb); |
|
|
|
espconn_regist_sentcb(tci->conn, tcpSentCb); |
|
|
|
espconn_regist_recvcb(tci->conn, tcpRecvCb); |
|
|
|
espconn_regist_recvcb(tci->conn, tcpRecvCb); |
|
|
|
espconn_regist_disconcb(tci->conn, tcpDisconCb); |
|
|
|
espconn_regist_disconcb(tci->conn, tcpDisconCb); |
|
|
|
tci->conn->reverse = tci; |
|
|
|
tci->conn->reverse = tci; |
|
|
|
|
|
|
|
|
|
|
|
return tci; |
|
|
|
return tci; |
|
|
|
|
|
|
|
|
|
|
|
fail: |
|
|
|
fail: |
|
|
|
tcpConnFree(tci); |
|
|
|
tcpConnFree(tci); |
|
|
|
return NULL; |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Free a connection dynamically.
|
|
|
|
// Free a connection dynamically.
|
|
|
|
static void ICACHE_FLASH_ATTR |
|
|
|
static void ICACHE_FLASH_ATTR |
|
|
|
tcpConnFree(TcpConn* tci) { |
|
|
|
tcpConnFree(TcpConn* tci) { |
|
|
|
if (tci->conn != NULL) os_free(tci->conn); |
|
|
|
if (tci->conn != NULL) os_free(tci->conn); |
|
|
|
if (tci->tcp != NULL) os_free(tci->tcp); |
|
|
|
if (tci->tcp != NULL) os_free(tci->tcp); |
|
|
|
if (tci->txBuf != NULL) os_free(tci->txBuf); |
|
|
|
if (tci->txBuf != NULL) os_free(tci->txBuf); |
|
|
|
if (tci->txBufSent != NULL) os_free(tci->txBufSent); |
|
|
|
if (tci->txBufSent != NULL) os_free(tci->txBufSent); |
|
|
|
memset(tci, 0, sizeof(TcpConn)); |
|
|
|
memset(tci, 0, sizeof(TcpConn)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//===== DNS
|
|
|
|
//===== DNS
|
|
|
@ -96,26 +96,26 @@ tcpConnFree(TcpConn* tci) { |
|
|
|
// DNS name resolution callback
|
|
|
|
// DNS name resolution callback
|
|
|
|
static void ICACHE_FLASH_ATTR |
|
|
|
static void ICACHE_FLASH_ATTR |
|
|
|
tcpClientHostnameCb(const char *name, ip_addr_t *ipaddr, void *arg) { |
|
|
|
tcpClientHostnameCb(const char *name, ip_addr_t *ipaddr, void *arg) { |
|
|
|
struct espconn *conn = arg; |
|
|
|
struct espconn *conn = arg; |
|
|
|
TcpConn *tci = conn->reverse; |
|
|
|
TcpConn *tci = conn->reverse; |
|
|
|
os_printf("TCP dns CB (%p %p)\n", arg, tci); |
|
|
|
os_printf("TCP dns CB (%p %p)\n", arg, tci); |
|
|
|
if (ipaddr == NULL) { |
|
|
|
if (ipaddr == NULL) { |
|
|
|
os_printf("TCP %s not found\n", name); |
|
|
|
os_printf("TCP %s not found\n", name); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
os_printf("TCP %s -> %d.%d.%d.%d\n", name, IP2STR(ipaddr)); |
|
|
|
os_printf("TCP %s -> %d.%d.%d.%d\n", name, IP2STR(ipaddr)); |
|
|
|
tci->tcp->remote_ip[0] = ip4_addr1(ipaddr); |
|
|
|
tci->tcp->remote_ip[0] = ip4_addr1(ipaddr); |
|
|
|
tci->tcp->remote_ip[1] = ip4_addr2(ipaddr); |
|
|
|
tci->tcp->remote_ip[1] = ip4_addr2(ipaddr); |
|
|
|
tci->tcp->remote_ip[2] = ip4_addr3(ipaddr); |
|
|
|
tci->tcp->remote_ip[2] = ip4_addr3(ipaddr); |
|
|
|
tci->tcp->remote_ip[3] = ip4_addr4(ipaddr); |
|
|
|
tci->tcp->remote_ip[3] = ip4_addr4(ipaddr); |
|
|
|
os_printf("TCP connect %d.%d.%d.%d (%p)\n", IP2STR(tci->tcp->remote_ip), tci); |
|
|
|
os_printf("TCP connect %d.%d.%d.%d (%p)\n", IP2STR(tci->tcp->remote_ip), tci); |
|
|
|
if (espconn_connect(tci->conn) == ESPCONN_OK) { |
|
|
|
if (espconn_connect(tci->conn) == ESPCONN_OK) { |
|
|
|
tci->state = TCP_conn; |
|
|
|
tci->state = TCP_conn; |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
os_printf("TCP connect failure\n"); |
|
|
|
os_printf("TCP connect failure\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
// oops
|
|
|
|
// oops
|
|
|
|
tcpConnFree(tci); |
|
|
|
tcpConnFree(tci); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//===== Connect / disconnect
|
|
|
|
//===== Connect / disconnect
|
|
|
@ -123,42 +123,42 @@ tcpClientHostnameCb(const char *name, ip_addr_t *ipaddr, void *arg) { |
|
|
|
// Connected callback
|
|
|
|
// Connected callback
|
|
|
|
static void ICACHE_FLASH_ATTR |
|
|
|
static void ICACHE_FLASH_ATTR |
|
|
|
tcpConnectCb(void *arg) { |
|
|
|
tcpConnectCb(void *arg) { |
|
|
|
struct espconn *conn = arg; |
|
|
|
struct espconn *conn = arg; |
|
|
|
TcpConn *tci = conn->reverse; |
|
|
|
TcpConn *tci = conn->reverse; |
|
|
|
os_printf("TCP connect CB (%p %p)\n", arg, tci); |
|
|
|
os_printf("TCP connect CB (%p %p)\n", arg, tci); |
|
|
|
tci->state = TCP_data; |
|
|
|
tci->state = TCP_data; |
|
|
|
// send any buffered data
|
|
|
|
// send any buffered data
|
|
|
|
if (tci->txBuf != NULL && tci->txBufLen > 0) tcpDoSend(tci); |
|
|
|
if (tci->txBuf != NULL && tci->txBufLen > 0) tcpDoSend(tci); |
|
|
|
// reply to serial
|
|
|
|
// reply to serial
|
|
|
|
char buf[6]; |
|
|
|
char buf[6]; |
|
|
|
short l = os_sprintf(buf, "\n~@%dC\n", tci-tcpConn); |
|
|
|
short l = os_sprintf(buf, "\n~@%dC\n", tci-tcpConn); |
|
|
|
uart0_tx_buffer(buf, l); |
|
|
|
uart0_tx_buffer(buf, l); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Disconnect callback
|
|
|
|
// Disconnect callback
|
|
|
|
static void ICACHE_FLASH_ATTR tcpDisconCb(void *arg) { |
|
|
|
static void ICACHE_FLASH_ATTR tcpDisconCb(void *arg) { |
|
|
|
struct espconn *conn = arg; |
|
|
|
struct espconn *conn = arg; |
|
|
|
TcpConn *tci = conn->reverse; |
|
|
|
TcpConn *tci = conn->reverse; |
|
|
|
os_printf("TCP disconnect CB (%p %p)\n", arg, tci); |
|
|
|
os_printf("TCP disconnect CB (%p %p)\n", arg, tci); |
|
|
|
// notify to serial
|
|
|
|
// notify to serial
|
|
|
|
char buf[6]; |
|
|
|
char buf[6]; |
|
|
|
short l = os_sprintf(buf, "\n~@%dZ\n", tci-tcpConn); |
|
|
|
short l = os_sprintf(buf, "\n~@%dZ\n", tci-tcpConn); |
|
|
|
uart0_tx_buffer(buf, l); |
|
|
|
uart0_tx_buffer(buf, l); |
|
|
|
// free
|
|
|
|
// free
|
|
|
|
tcpConnFree(tci); |
|
|
|
tcpConnFree(tci); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Connection reset callback
|
|
|
|
// Connection reset callback
|
|
|
|
static void ICACHE_FLASH_ATTR tcpResetCb(void *arg, sint8 err) { |
|
|
|
static void ICACHE_FLASH_ATTR tcpResetCb(void *arg, sint8 err) { |
|
|
|
struct espconn *conn = arg; |
|
|
|
struct espconn *conn = arg; |
|
|
|
TcpConn *tci = conn->reverse; |
|
|
|
TcpConn *tci = conn->reverse; |
|
|
|
os_printf("TCP reset CB (%p %p) err=%d\n", arg, tci, err); |
|
|
|
os_printf("TCP reset CB (%p %p) err=%d\n", arg, tci, err); |
|
|
|
// notify to serial
|
|
|
|
// notify to serial
|
|
|
|
char buf[6]; |
|
|
|
char buf[6]; |
|
|
|
short l = os_sprintf(buf, "\n~@%dZ\n", tci-tcpConn); |
|
|
|
short l = os_sprintf(buf, "\n~@%dZ\n", tci-tcpConn); |
|
|
|
uart0_tx_buffer(buf, l); |
|
|
|
uart0_tx_buffer(buf, l); |
|
|
|
// free
|
|
|
|
// free
|
|
|
|
tcpConnFree(tci); |
|
|
|
tcpConnFree(tci); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//===== Sending and receiving
|
|
|
|
//===== Sending and receiving
|
|
|
@ -166,89 +166,89 @@ static void ICACHE_FLASH_ATTR tcpResetCb(void *arg, sint8 err) { |
|
|
|
// Send the next buffer (assumes that the connection is in a state that allows it)
|
|
|
|
// Send the next buffer (assumes that the connection is in a state that allows it)
|
|
|
|
static void ICACHE_FLASH_ATTR |
|
|
|
static void ICACHE_FLASH_ATTR |
|
|
|
tcpDoSend(TcpConn *tci) { |
|
|
|
tcpDoSend(TcpConn *tci) { |
|
|
|
sint8 err = espconn_sent(tci->conn, (uint8*)tci->txBuf, tci->txBufLen); |
|
|
|
sint8 err = espconn_sent(tci->conn, (uint8*)tci->txBuf, tci->txBufLen); |
|
|
|
if (err == ESPCONN_OK) { |
|
|
|
if (err == ESPCONN_OK) { |
|
|
|
// send successful
|
|
|
|
// send successful
|
|
|
|
os_printf("TCP sent (%p %p)\n", tci->conn, tci); |
|
|
|
os_printf("TCP sent (%p %p)\n", tci->conn, tci); |
|
|
|
tci->txBuf[tci->txBufLen] = 0; os_printf("TCP data: %s\n", tci->txBuf); |
|
|
|
tci->txBuf[tci->txBufLen] = 0; os_printf("TCP data: %s\n", tci->txBuf); |
|
|
|
tci->txBufSent = tci->txBuf; |
|
|
|
tci->txBufSent = tci->txBuf; |
|
|
|
tci->txBuf = NULL; |
|
|
|
tci->txBuf = NULL; |
|
|
|
tci->txBufLen = 0; |
|
|
|
tci->txBufLen = 0; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// send error, leave as-is and try again later...
|
|
|
|
// send error, leave as-is and try again later...
|
|
|
|
os_printf("TCP send err (%p %p) %d\n", tci->conn, tci, err); |
|
|
|
os_printf("TCP send err (%p %p) %d\n", tci->conn, tci, err); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Sent callback
|
|
|
|
// Sent callback
|
|
|
|
static void ICACHE_FLASH_ATTR |
|
|
|
static void ICACHE_FLASH_ATTR |
|
|
|
tcpSentCb(void *arg) { |
|
|
|
tcpSentCb(void *arg) { |
|
|
|
struct espconn *conn = arg; |
|
|
|
struct espconn *conn = arg; |
|
|
|
TcpConn *tci = conn->reverse; |
|
|
|
TcpConn *tci = conn->reverse; |
|
|
|
os_printf("TCP sent CB (%p %p)\n", arg, tci); |
|
|
|
os_printf("TCP sent CB (%p %p)\n", arg, tci); |
|
|
|
if (tci->txBufSent != NULL) os_free(tci->txBufSent); |
|
|
|
if (tci->txBufSent != NULL) os_free(tci->txBufSent); |
|
|
|
tci->txBufSent = NULL; |
|
|
|
tci->txBufSent = NULL; |
|
|
|
|
|
|
|
|
|
|
|
if (tci->txBuf != NULL && tci->txBufLen == MAX_TXBUF) { |
|
|
|
if (tci->txBuf != NULL && tci->txBufLen == MAX_TXBUF) { |
|
|
|
// next buffer is full, send it now
|
|
|
|
// next buffer is full, send it now
|
|
|
|
tcpDoSend(tci); |
|
|
|
tcpDoSend(tci); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Recv callback
|
|
|
|
// Recv callback
|
|
|
|
static void ICACHE_FLASH_ATTR tcpRecvCb(void *arg, char *data, uint16_t len) { |
|
|
|
static void ICACHE_FLASH_ATTR tcpRecvCb(void *arg, char *data, uint16_t len) { |
|
|
|
struct espconn *conn = arg; |
|
|
|
struct espconn *conn = arg; |
|
|
|
TcpConn *tci = conn->reverse; |
|
|
|
TcpConn *tci = conn->reverse; |
|
|
|
os_printf("TCP recv CB (%p %p)\n", arg, tci); |
|
|
|
os_printf("TCP recv CB (%p %p)\n", arg, tci); |
|
|
|
if (tci->state == TCP_data) { |
|
|
|
if (tci->state == TCP_data) { |
|
|
|
uint8_t chan; |
|
|
|
uint8_t chan; |
|
|
|
for (chan=0; chan<MAX_CHAN && tcpConn+chan!=tci; chan++) |
|
|
|
for (chan=0; chan<MAX_CHAN && tcpConn+chan!=tci; chan++) |
|
|
|
if (chan >= MAX_CHAN) return; // oops!?
|
|
|
|
if (chan >= MAX_CHAN) return; // oops!?
|
|
|
|
char buf[6]; |
|
|
|
char buf[6]; |
|
|
|
short l = os_sprintf(buf, "\n~%d", chan); |
|
|
|
short l = os_sprintf(buf, "\n~%d", chan); |
|
|
|
uart0_tx_buffer(buf, l); |
|
|
|
uart0_tx_buffer(buf, l); |
|
|
|
uart0_tx_buffer(data, len); |
|
|
|
uart0_tx_buffer(data, len); |
|
|
|
uart0_tx_buffer("\0\n", 2); |
|
|
|
uart0_tx_buffer("\0\n", 2); |
|
|
|
} |
|
|
|
} |
|
|
|
serledFlash(50); // short blink on serial LED
|
|
|
|
serledFlash(50); // short blink on serial LED
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ICACHE_FLASH_ATTR |
|
|
|
void ICACHE_FLASH_ATTR |
|
|
|
tcpClientSendChar(uint8_t chan, char c) { |
|
|
|
tcpClientSendChar(uint8_t chan, char c) { |
|
|
|
TcpConn *tci = tcpConn+chan; |
|
|
|
TcpConn *tci = tcpConn+chan; |
|
|
|
if (tci->state == TCP_idle) return; |
|
|
|
if (tci->state == TCP_idle) return; |
|
|
|
|
|
|
|
|
|
|
|
if (tci->txBuf != NULL) { |
|
|
|
if (tci->txBuf != NULL) { |
|
|
|
// we have a buffer
|
|
|
|
// we have a buffer
|
|
|
|
if (tci->txBufLen < MAX_TXBUF) { |
|
|
|
if (tci->txBufLen < MAX_TXBUF) { |
|
|
|
// buffer has space, add char and return
|
|
|
|
// buffer has space, add char and return
|
|
|
|
tci->txBuf[tci->txBufLen++] = c; |
|
|
|
tci->txBuf[tci->txBufLen++] = c; |
|
|
|
return; |
|
|
|
return; |
|
|
|
} else if (tci->txBufSent == NULL) { |
|
|
|
} else if (tci->txBufSent == NULL) { |
|
|
|
// we don't have a send pending, send full buffer off
|
|
|
|
// we don't have a send pending, send full buffer off
|
|
|
|
if (tci->state == TCP_data) tcpDoSend(tci); |
|
|
|
if (tci->state == TCP_data) tcpDoSend(tci); |
|
|
|
if (tci->txBuf != NULL) return; // something went wrong
|
|
|
|
if (tci->txBuf != NULL) return; // something went wrong
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// buffers all backed-up, drop char
|
|
|
|
// buffers all backed-up, drop char
|
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// we do not have a buffer (either didn't have one or sent it off)
|
|
|
|
// we do not have a buffer (either didn't have one or sent it off)
|
|
|
|
// allocate one
|
|
|
|
// allocate one
|
|
|
|
tci->txBuf = os_malloc(MAX_TXBUF); |
|
|
|
tci->txBuf = os_malloc(MAX_TXBUF); |
|
|
|
tci->txBufLen = 0; |
|
|
|
tci->txBufLen = 0; |
|
|
|
if (tci->txBuf != NULL) { |
|
|
|
if (tci->txBuf != NULL) { |
|
|
|
tci->txBuf[tci->txBufLen++] = c; |
|
|
|
tci->txBuf[tci->txBufLen++] = c; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ICACHE_FLASH_ATTR |
|
|
|
void ICACHE_FLASH_ATTR |
|
|
|
tcpClientSendPush(uint8_t chan) { |
|
|
|
tcpClientSendPush(uint8_t chan) { |
|
|
|
TcpConn *tci = tcpConn+chan; |
|
|
|
TcpConn *tci = tcpConn+chan; |
|
|
|
if (tci->state != TCP_data) return; // no active connection on this channel
|
|
|
|
if (tci->state != TCP_data) return; // no active connection on this channel
|
|
|
|
if (tci->txBuf == NULL || tci->txBufLen == 0) return; // no chars accumulated to send
|
|
|
|
if (tci->txBuf == NULL || tci->txBufLen == 0) return; // no chars accumulated to send
|
|
|
|
if (tci->txBufSent != NULL) return; // already got a send in progress
|
|
|
|
if (tci->txBufSent != NULL) return; // already got a send in progress
|
|
|
|
tcpDoSend(tci); |
|
|
|
tcpDoSend(tci); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//===== Command parsing
|
|
|
|
//===== Command parsing
|
|
|
@ -257,59 +257,59 @@ tcpClientSendPush(uint8_t chan) { |
|
|
|
// Returns true on success.
|
|
|
|
// Returns true on success.
|
|
|
|
bool ICACHE_FLASH_ATTR |
|
|
|
bool ICACHE_FLASH_ATTR |
|
|
|
tcpClientCommand(uint8_t chan, char cmd, char *cmdBuf) { |
|
|
|
tcpClientCommand(uint8_t chan, char cmd, char *cmdBuf) { |
|
|
|
TcpConn *tci; |
|
|
|
TcpConn *tci; |
|
|
|
char *hostname; |
|
|
|
char *hostname; |
|
|
|
char *port; |
|
|
|
char *port; |
|
|
|
|
|
|
|
|
|
|
|
// copy the command so we can modify it
|
|
|
|
// copy the command so we can modify it
|
|
|
|
char buf[128]; |
|
|
|
char buf[128]; |
|
|
|
os_strncpy(buf, cmdBuf, 128); |
|
|
|
os_strncpy(buf, cmdBuf, 128); |
|
|
|
buf[127] = 0; |
|
|
|
buf[127] = 0; |
|
|
|
|
|
|
|
|
|
|
|
switch (cmd) { |
|
|
|
switch (cmd) { |
|
|
|
//== TCP Connect command
|
|
|
|
//== TCP Connect command
|
|
|
|
case 'T': |
|
|
|
case 'T': |
|
|
|
hostname = buf; |
|
|
|
hostname = buf; |
|
|
|
port = hostname; |
|
|
|
port = hostname; |
|
|
|
while (*port != 0 && *port != ':') port++; |
|
|
|
while (*port != 0 && *port != ':') port++; |
|
|
|
if (*port != ':') break; |
|
|
|
if (*port != ':') break; |
|
|
|
*port = 0; |
|
|
|
*port = 0; |
|
|
|
port++; |
|
|
|
port++; |
|
|
|
int portInt = atoi(port); |
|
|
|
int portInt = atoi(port); |
|
|
|
if (portInt < 1 || portInt > 65535) break; |
|
|
|
if (portInt < 1 || portInt > 65535) break; |
|
|
|
|
|
|
|
|
|
|
|
// allocate a connection
|
|
|
|
// allocate a connection
|
|
|
|
tci = tcpConnAlloc(chan); |
|
|
|
tci = tcpConnAlloc(chan); |
|
|
|
if (tci == NULL) break; |
|
|
|
if (tci == NULL) break; |
|
|
|
tci->state = TCP_dns; |
|
|
|
tci->state = TCP_dns; |
|
|
|
tci->tcp->remote_port = portInt; |
|
|
|
tci->tcp->remote_port = portInt; |
|
|
|
|
|
|
|
|
|
|
|
// start the DNS resolution
|
|
|
|
// start the DNS resolution
|
|
|
|
os_printf("TCP %p resolving %s for chan %d (conn=%p)\n", tci, hostname, chan ,tci->conn); |
|
|
|
os_printf("TCP %p resolving %s for chan %d (conn=%p)\n", tci, hostname, chan ,tci->conn); |
|
|
|
ip_addr_t ip; |
|
|
|
ip_addr_t ip; |
|
|
|
err_t err = espconn_gethostbyname(tci->conn, hostname, &ip, tcpClientHostnameCb); |
|
|
|
err_t err = espconn_gethostbyname(tci->conn, hostname, &ip, tcpClientHostnameCb); |
|
|
|
if (err == ESPCONN_OK) { |
|
|
|
if (err == ESPCONN_OK) { |
|
|
|
// dns cache hit, got the IP address, fake the callback (sigh)
|
|
|
|
// dns cache hit, got the IP address, fake the callback (sigh)
|
|
|
|
os_printf("TCP DNS hit\n"); |
|
|
|
os_printf("TCP DNS hit\n"); |
|
|
|
tcpClientHostnameCb(hostname, &ip, tci->conn); |
|
|
|
tcpClientHostnameCb(hostname, &ip, tci->conn); |
|
|
|
} else if (err != ESPCONN_INPROGRESS) { |
|
|
|
} else if (err != ESPCONN_INPROGRESS) { |
|
|
|
tcpConnFree(tci); |
|
|
|
tcpConnFree(tci); |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
//== TCP Close/disconnect command
|
|
|
|
//== TCP Close/disconnect command
|
|
|
|
case 'C': |
|
|
|
case 'C': |
|
|
|
os_printf("TCP closing chan %d\n", chan); |
|
|
|
os_printf("TCP closing chan %d\n", chan); |
|
|
|
tci = tcpConn+chan; |
|
|
|
tci = tcpConn+chan; |
|
|
|
if (tci->state > TCP_idle) { |
|
|
|
if (tci->state > TCP_idle) { |
|
|
|
tci->state = TCP_idle; // hackish...
|
|
|
|
tci->state = TCP_idle; // hackish...
|
|
|
|
espconn_disconnect(tci->conn); |
|
|
|
espconn_disconnect(tci->conn); |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|