fix indentation to use spaces

pull/37/head
Thorsten von Eicken 9 years ago
parent 6867989b47
commit 7490e45aab
  1. 160
      serial/console.c
  2. 20
      serial/serbridge.h
  3. 422
      serial/tcpclient.c

@ -25,112 +25,112 @@ static int console_pos; // offset since reset of buffer
static void ICACHE_FLASH_ATTR static void ICACHE_FLASH_ATTR
console_write(char c) { console_write(char c) {
console_buf[console_wr] = c; console_buf[console_wr] = c;
console_wr = (console_wr+1) % BUF_MAX; console_wr = (console_wr+1) % BUF_MAX;
if (console_wr == console_rd) { if (console_wr == console_rd) {
// full, we write anyway and loose the oldest char // full, we write anyway and loose the oldest char
console_rd = (console_rd+1) % BUF_MAX; // full, eat first char console_rd = (console_rd+1) % BUF_MAX; // full, eat first char
console_pos++; console_pos++;
} }
} }
#if 0 #if 0
// return previous character in console, 0 if at start // return previous character in console, 0 if at start
static char ICACHE_FLASH_ATTR static char ICACHE_FLASH_ATTR
console_prev(void) { console_prev(void) {
if (console_wr == console_rd) return 0; if (console_wr == console_rd) return 0;
return console_buf[(console_wr-1+BUF_MAX)%BUF_MAX]; return console_buf[(console_wr-1+BUF_MAX)%BUF_MAX];
} }
#endif #endif
void ICACHE_FLASH_ATTR void ICACHE_FLASH_ATTR
console_write_char(char c) { console_write_char(char c) {
//if (c == '\n' && console_prev() != '\r') console_write('\r'); // does more harm than good //if (c == '\n' && console_prev() != '\r') console_write('\r'); // does more harm than good
console_write(c); console_write(c);
} }
int ICACHE_FLASH_ATTR int ICACHE_FLASH_ATTR
ajaxConsoleReset(HttpdConnData *connData) { ajaxConsoleReset(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
jsonHeader(connData, 200); jsonHeader(connData, 200);
console_rd = console_wr = console_pos = 0; console_rd = console_wr = console_pos = 0;
serbridgeReset(); serbridgeReset();
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
} }
int ICACHE_FLASH_ATTR int ICACHE_FLASH_ATTR
ajaxConsoleBaud(HttpdConnData *connData) { ajaxConsoleBaud(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
char buff[512]; char buff[512];
int len, status = 400; int len, status = 400;
len = httpdFindArg(connData->getArgs, "rate", buff, sizeof(buff)); len = httpdFindArg(connData->getArgs, "rate", buff, sizeof(buff));
if (len > 0) { if (len > 0) {
int rate = atoi(buff); int rate = atoi(buff);
if (rate >= 9600 && rate <= 1000000) { if (rate >= 9600 && rate <= 1000000) {
uart0_baud(rate); uart0_baud(rate);
flashConfig.baud_rate = rate; flashConfig.baud_rate = rate;
status = configSave() ? 200 : 400; status = configSave() ? 200 : 400;
} }
} else if (connData->requestType == HTTPD_METHOD_GET) { } else if (connData->requestType == HTTPD_METHOD_GET) {
status = 200; status = 200;
} }
jsonHeader(connData, status); jsonHeader(connData, status);
os_sprintf(buff, "{\"rate\": %ld}", flashConfig.baud_rate); os_sprintf(buff, "{\"rate\": %ld}", flashConfig.baud_rate);
httpdSend(connData, buff, -1); httpdSend(connData, buff, -1);
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
} }
int ICACHE_FLASH_ATTR int ICACHE_FLASH_ATTR
ajaxConsole(HttpdConnData *connData) { ajaxConsole(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
char buff[2048]; char buff[2048];
int len; // length of text in buff int len; // length of text in buff
int console_len = (console_wr+BUF_MAX-console_rd) % BUF_MAX; // num chars in console_buf int console_len = (console_wr+BUF_MAX-console_rd) % BUF_MAX; // num chars in console_buf
int start = 0; // offset onto console_wr to start sending out chars int start = 0; // offset onto console_wr to start sending out chars
jsonHeader(connData, 200); jsonHeader(connData, 200);
// figure out where to start in buffer based on URI param // figure out where to start in buffer based on URI param
len = httpdFindArg(connData->getArgs, "start", buff, sizeof(buff)); len = httpdFindArg(connData->getArgs, "start", buff, sizeof(buff));
if (len > 0) { if (len > 0) {
start = atoi(buff); start = atoi(buff);
if (start < console_pos) { if (start < console_pos) {
start = 0; start = 0;
} else if (start >= console_pos+console_len) { } else if (start >= console_pos+console_len) {
start = console_len; start = console_len;
} else { } else {
start = start - console_pos; start = start - console_pos;
} }
} }
// start outputting // start outputting
len = os_sprintf(buff, "{\"len\":%d, \"start\":%d, \"text\": \"", len = os_sprintf(buff, "{\"len\":%d, \"start\":%d, \"text\": \"",
console_len-start, console_pos+start); console_len-start, console_pos+start);
int rd = (console_rd+start) % BUF_MAX; int rd = (console_rd+start) % BUF_MAX;
while (len < 2040 && rd != console_wr) { while (len < 2040 && rd != console_wr) {
uint8_t c = console_buf[rd]; uint8_t c = console_buf[rd];
if (c == '\\' || c == '"') { if (c == '\\' || c == '"') {
buff[len++] = '\\'; buff[len++] = '\\';
buff[len++] = c; buff[len++] = c;
} else if (c == '\r') { } else if (c == '\r') {
// this is crummy, but browsers display a newline for \r\n sequences // this is crummy, but browsers display a newline for \r\n sequences
} else if (c < ' ') { } else if (c < ' ') {
len += os_sprintf(buff+len, "\\u%04x", c); len += os_sprintf(buff+len, "\\u%04x", c);
} else { } else {
buff[len++] = c; buff[len++] = c;
} }
rd = (rd + 1) % BUF_MAX; rd = (rd + 1) % BUF_MAX;
} }
os_strcpy(buff+len, "\"}"); len+=2; os_strcpy(buff+len, "\"}"); len+=2;
httpdSend(connData, buff, len); httpdSend(connData, buff, len);
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
} }
void ICACHE_FLASH_ATTR consoleInit() { void ICACHE_FLASH_ATTR consoleInit() {
console_wr = 0; console_wr = 0;
console_rd = 0; console_rd = 0;
} }

@ -12,21 +12,21 @@
#define MAX_TXBUFFER 1024 #define MAX_TXBUFFER 1024
enum connModes { enum connModes {
cmInit = 0, // initialization mode: nothing received yet cmInit = 0, // initialization mode: nothing received yet
cmTransparent, // transparent mode cmTransparent, // transparent mode
cmAVR, // Arduino/AVR programming mode cmAVR, // Arduino/AVR programming mode
cmARM, // ARM (LPC8xx) programming cmARM, // ARM (LPC8xx) programming
cmEcho, // simply echo characters (used for debugging latency) cmEcho, // simply echo characters (used for debugging latency)
cmTelnet, // use telnet escape sequences for programming mode cmTelnet, // use telnet escape sequences for programming mode
cmTcpClient, // client connection (initiated via serial) cmTcpClient, // client connection (initiated via serial)
}; };
typedef struct serbridgeConnData { typedef struct serbridgeConnData {
struct espconn *conn; struct espconn *conn;
enum connModes conn_mode; // connection mode enum connModes conn_mode; // connection mode
char *txbuffer; // buffer for the data to send char *txbuffer; // buffer for the data to send
uint16 txbufferlen; // length of data in txbuffer uint16 txbufferlen; // length of data in txbuffer
bool readytosend; // true, if txbuffer can send by espconn_sent bool readytosend; // true, if txbuffer can send by espconn_sent
uint8_t telnet_state; uint8_t telnet_state;
} serbridgeConnData; } serbridgeConnData;

@ -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;
} }

Loading…
Cancel
Save