|
|
@ -22,65 +22,79 @@ sint8 ICACHE_FLASH_ATTR espbuffsend(serbridgeConnData *conn, const char *data, |
|
|
|
|
|
|
|
|
|
|
|
// Connection pool
|
|
|
|
// Connection pool
|
|
|
|
serbridgeConnData connData[MAX_CONN]; |
|
|
|
serbridgeConnData connData[MAX_CONN]; |
|
|
|
// Transmit buffers for the connection pool
|
|
|
|
|
|
|
|
static char txbuffer[MAX_CONN][MAX_TXBUFFER]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Given a pointer to an espconn struct find the connection that correcponds to it
|
|
|
|
|
|
|
|
static serbridgeConnData ICACHE_FLASH_ATTR *serbridgeFindConnData(void *arg) { |
|
|
|
|
|
|
|
for (int i=0; i<MAX_CONN; i++) { |
|
|
|
|
|
|
|
if (connData[i].conn == (struct espconn *)arg) { |
|
|
|
|
|
|
|
return &connData[i]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//os_printf("FindConnData: Huh? Couldn't find connection for %p\n", arg);
|
|
|
|
|
|
|
|
return NULL; // not found, may be closed already...
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Send all data in conn->txbuffer
|
|
|
|
// Send all data in conn->txbuffer
|
|
|
|
// returns result from espconn_sent if data in buffer or ESPCONN_OK (0)
|
|
|
|
// returns result from espconn_sent if data in buffer or ESPCONN_OK (0)
|
|
|
|
// Use only internally from espbuffsend and serbridgeSentCb
|
|
|
|
// Use only internally from espbuffsend and serbridgeSentCb
|
|
|
|
static sint8 ICACHE_FLASH_ATTR sendtxbuffer(serbridgeConnData *conn) { |
|
|
|
static sint8 ICACHE_FLASH_ATTR sendtxbuffer(serbridgeConnData *conn) { |
|
|
|
sint8 result = ESPCONN_OK; |
|
|
|
sint8 result = ESPCONN_OK; |
|
|
|
if (conn->txbufferlen != 0) { |
|
|
|
if (conn->txbufferlen != 0) { |
|
|
|
//os_printf("%d TX %d\n", system_get_time(), conn->txbufferlen);
|
|
|
|
//os_printf("%d TX %d\n", system_get_time(), conn->txbufferlen);
|
|
|
|
conn->readytosend = false; |
|
|
|
conn->readytosend = false; |
|
|
|
result = espconn_sent(conn->conn, (uint8_t*)conn->txbuffer, conn->txbufferlen); |
|
|
|
result = espconn_sent(conn->conn, (uint8_t*)conn->txbuffer, conn->txbufferlen); |
|
|
|
conn->txbufferlen = 0; |
|
|
|
if (result != ESPCONN_OK) { |
|
|
|
if (result != ESPCONN_OK) { |
|
|
|
os_printf("sendtxbuffer: espconn_sent error %d on conn %p\n", result, conn); |
|
|
|
os_printf("sendtxbuffer: espconn_sent error %d on conn %p\n", result, conn); |
|
|
|
conn->txbufferlen = 0; |
|
|
|
} |
|
|
|
} else { |
|
|
|
} |
|
|
|
conn->sentbuffer = conn->txbuffer; |
|
|
|
return result; |
|
|
|
conn->txbuffer = NULL; |
|
|
|
|
|
|
|
conn->txbufferlen = 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *tx_full_msg = "espbuffsend: txbuffer full on conn %p\n"; |
|
|
|
|
|
|
|
|
|
|
|
// espbuffsend adds data to the send buffer. If the previous send was completed it calls
|
|
|
|
// espbuffsend adds data to the send buffer. If the previous send was completed it calls
|
|
|
|
// sendtxbuffer and espconn_sent.
|
|
|
|
// sendtxbuffer and espconn_sent.
|
|
|
|
// Returns ESPCONN_OK (0) for success, -128 if buffer is full or error from espconn_sent
|
|
|
|
// Returns ESPCONN_OK (0) for success, -128 if buffer is full or error from espconn_sent
|
|
|
|
// Use espbuffsend instead of espconn_sent as it solves the problem that espconn_sent must
|
|
|
|
// Use espbuffsend instead of espconn_sent as it solves the problem that espconn_sent must
|
|
|
|
// only be called *after* receiving an espconn_sent_callback for the previous packet.
|
|
|
|
// only be called *after* receiving an espconn_sent_callback for the previous packet.
|
|
|
|
sint8 ICACHE_FLASH_ATTR espbuffsend(serbridgeConnData *conn, const char *data, uint16 len) { |
|
|
|
sint8 ICACHE_FLASH_ATTR espbuffsend(serbridgeConnData *conn, const char *data, uint16 len) { |
|
|
|
if (conn->txbufferlen + len > MAX_TXBUFFER) { |
|
|
|
if (conn->txbufferlen >= MAX_TXBUFFER) { |
|
|
|
os_printf("espbuffsend: txbuffer full on conn %p\n", conn); |
|
|
|
os_printf(tx_full_msg, conn); |
|
|
|
return -128; |
|
|
|
return -128; |
|
|
|
} |
|
|
|
} |
|
|
|
os_memcpy(conn->txbuffer + conn->txbufferlen, data, len); |
|
|
|
|
|
|
|
conn->txbufferlen += len; |
|
|
|
// make sure we indeed have a buffer
|
|
|
|
if (conn->readytosend) { |
|
|
|
if (conn->txbuffer == NULL) conn->txbuffer = os_zalloc(MAX_TXBUFFER); |
|
|
|
return sendtxbuffer(conn); |
|
|
|
if (conn->txbuffer == NULL) { |
|
|
|
} else { |
|
|
|
os_printf("espbuffsend: cannot alloc tx buffer\n"); |
|
|
|
//os_printf("%d QU %d\n", system_get_time(), conn->txbufferlen);
|
|
|
|
return -128; |
|
|
|
} |
|
|
|
} |
|
|
|
return ESPCONN_OK; |
|
|
|
|
|
|
|
|
|
|
|
// add to send buffer
|
|
|
|
|
|
|
|
uint16_t avail = conn->txbufferlen+len > MAX_TXBUFFER ? MAX_TXBUFFER-conn->txbufferlen : len; |
|
|
|
|
|
|
|
os_memcpy(conn->txbuffer + conn->txbufferlen, data, avail); |
|
|
|
|
|
|
|
conn->txbufferlen += len; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// try to send
|
|
|
|
|
|
|
|
sint8 result = ESPCONN_OK; |
|
|
|
|
|
|
|
if (conn->readytosend) result = sendtxbuffer(conn); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (avail < len) { |
|
|
|
|
|
|
|
// some data didn't fit into the buffer
|
|
|
|
|
|
|
|
if (conn->txbufferlen == 0) { |
|
|
|
|
|
|
|
// we sent the prior buffer, so try again
|
|
|
|
|
|
|
|
return espbuffsend(conn, data+avail, len-avail); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
os_printf(tx_full_msg, conn); |
|
|
|
|
|
|
|
return -128; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//callback after the data are sent
|
|
|
|
//callback after the data are sent
|
|
|
|
static void ICACHE_FLASH_ATTR serbridgeSentCb(void *arg) { |
|
|
|
static void ICACHE_FLASH_ATTR serbridgeSentCb(void *arg) { |
|
|
|
serbridgeConnData *conn = serbridgeFindConnData(arg); |
|
|
|
serbridgeConnData *conn = ((struct espconn*)arg)->reverse; |
|
|
|
//os_printf("Sent callback on conn %p\n", conn);
|
|
|
|
//os_printf("Sent callback on conn %p\n", conn);
|
|
|
|
if (conn == NULL) return; |
|
|
|
if (conn == NULL) return; |
|
|
|
//os_printf("%d ST\n", system_get_time());
|
|
|
|
//os_printf("%d ST\n", system_get_time());
|
|
|
|
conn->readytosend = true; |
|
|
|
if (conn->sentbuffer != NULL) os_free(conn->sentbuffer); |
|
|
|
sendtxbuffer(conn); // send possible new data in txbuffer
|
|
|
|
conn->sentbuffer = NULL; |
|
|
|
|
|
|
|
conn->readytosend = true; |
|
|
|
|
|
|
|
sendtxbuffer(conn); // send possible new data in txbuffer
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Telnet protocol characters
|
|
|
|
// Telnet protocol characters
|
|
|
@ -102,265 +116,257 @@ enum { TN_normal, TN_iac, TN_will, TN_start, TN_end, TN_comPort, TN_setControl } |
|
|
|
static uint8_t ICACHE_FLASH_ATTR |
|
|
|
static uint8_t ICACHE_FLASH_ATTR |
|
|
|
telnetUnwrap(uint8_t *inBuf, int len, uint8_t state) |
|
|
|
telnetUnwrap(uint8_t *inBuf, int len, uint8_t state) |
|
|
|
{ |
|
|
|
{ |
|
|
|
for (int i=0; i<len; i++) { |
|
|
|
for (int i=0; i<len; i++) { |
|
|
|
uint8_t c = inBuf[i]; |
|
|
|
uint8_t c = inBuf[i]; |
|
|
|
switch (state) { |
|
|
|
switch (state) { |
|
|
|
default: |
|
|
|
default: |
|
|
|
case TN_normal: |
|
|
|
case TN_normal: |
|
|
|
if (c == IAC) state = TN_iac; // escape char: see what's next
|
|
|
|
if (c == IAC) state = TN_iac; // escape char: see what's next
|
|
|
|
else uart0_write_char(c); // regular char
|
|
|
|
else uart0_write_char(c); // regular char
|
|
|
|
break; |
|
|
|
break; |
|
|
|
case TN_iac: |
|
|
|
case TN_iac: |
|
|
|
switch (c) { |
|
|
|
switch (c) { |
|
|
|
case IAC: // second escape -> write one to outbuf and go normal again
|
|
|
|
case IAC: // second escape -> write one to outbuf and go normal again
|
|
|
|
state = TN_normal; |
|
|
|
state = TN_normal; |
|
|
|
uart0_write_char(c); |
|
|
|
uart0_write_char(c); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case WILL: // negotiation
|
|
|
|
case WILL: // negotiation
|
|
|
|
state = TN_will; |
|
|
|
state = TN_will; |
|
|
|
break; |
|
|
|
break; |
|
|
|
case SB: // command sequence begin
|
|
|
|
case SB: // command sequence begin
|
|
|
|
state = TN_start; |
|
|
|
state = TN_start; |
|
|
|
break; |
|
|
|
break; |
|
|
|
case SE: // command sequence end
|
|
|
|
case SE: // command sequence end
|
|
|
|
state = TN_normal; |
|
|
|
state = TN_normal; |
|
|
|
break; |
|
|
|
break; |
|
|
|
default: // not sure... let's ignore
|
|
|
|
default: // not sure... let's ignore
|
|
|
|
uart0_write_char(IAC); |
|
|
|
uart0_write_char(IAC); |
|
|
|
uart0_write_char(c); |
|
|
|
uart0_write_char(c); |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
case TN_will: |
|
|
|
case TN_will: |
|
|
|
state = TN_normal; // yes, we do COM port options, let's go back to normal
|
|
|
|
state = TN_normal; // yes, we do COM port options, let's go back to normal
|
|
|
|
break; |
|
|
|
break; |
|
|
|
case TN_start: // in command seq, now comes the type of cmd
|
|
|
|
case TN_start: // in command seq, now comes the type of cmd
|
|
|
|
if (c == ComPortOpt) state = TN_comPort; |
|
|
|
if (c == ComPortOpt) state = TN_comPort; |
|
|
|
else state = TN_end; // an option we don't know, skip 'til the end seq
|
|
|
|
else state = TN_end; // an option we don't know, skip 'til the end seq
|
|
|
|
break; |
|
|
|
break; |
|
|
|
case TN_end: // wait for end seq
|
|
|
|
case TN_end: // wait for end seq
|
|
|
|
if (c == IAC) state = TN_iac; // simple wait to accept end or next escape seq
|
|
|
|
if (c == IAC) state = TN_iac; // simple wait to accept end or next escape seq
|
|
|
|
break; |
|
|
|
break; |
|
|
|
case TN_comPort: |
|
|
|
case TN_comPort: |
|
|
|
if (c == SetControl) state = TN_setControl; |
|
|
|
if (c == SetControl) state = TN_setControl; |
|
|
|
else state = TN_end; |
|
|
|
else state = TN_end; |
|
|
|
break; |
|
|
|
break; |
|
|
|
case TN_setControl: // switch control line and delay a tad
|
|
|
|
case TN_setControl: // switch control line and delay a tad
|
|
|
|
switch (c) { |
|
|
|
switch (c) { |
|
|
|
case DTR_ON: |
|
|
|
case DTR_ON: |
|
|
|
if (mcu_reset_pin >= 0) { |
|
|
|
if (mcu_reset_pin >= 0) { |
|
|
|
os_printf("MCU reset gpio%d\n", mcu_reset_pin); |
|
|
|
os_printf("MCU reset gpio%d\n", mcu_reset_pin); |
|
|
|
GPIO_OUTPUT_SET(mcu_reset_pin, 0); |
|
|
|
GPIO_OUTPUT_SET(mcu_reset_pin, 0); |
|
|
|
os_delay_us(100L); |
|
|
|
os_delay_us(100L); |
|
|
|
} else os_printf("MCU reset: no pin\n"); |
|
|
|
} else os_printf("MCU reset: no pin\n"); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case DTR_OFF: |
|
|
|
case DTR_OFF: |
|
|
|
if (mcu_reset_pin >= 0) { |
|
|
|
if (mcu_reset_pin >= 0) { |
|
|
|
GPIO_OUTPUT_SET(mcu_reset_pin, 1); |
|
|
|
GPIO_OUTPUT_SET(mcu_reset_pin, 1); |
|
|
|
os_delay_us(100L); |
|
|
|
os_delay_us(100L); |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
case RTS_ON: |
|
|
|
case RTS_ON: |
|
|
|
if (mcu_isp_pin >= 0) { |
|
|
|
if (mcu_isp_pin >= 0) { |
|
|
|
os_printf("MCU ISP gpio%d\n", mcu_isp_pin); |
|
|
|
os_printf("MCU ISP gpio%d\n", mcu_isp_pin); |
|
|
|
GPIO_OUTPUT_SET(mcu_isp_pin, 0); |
|
|
|
GPIO_OUTPUT_SET(mcu_isp_pin, 0); |
|
|
|
os_delay_us(100L); |
|
|
|
os_delay_us(100L); |
|
|
|
} else os_printf("MCU isp: no pin\n"); |
|
|
|
} else os_printf("MCU isp: no pin\n"); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case RTS_OFF: |
|
|
|
case RTS_OFF: |
|
|
|
if (mcu_isp_pin >= 0) { |
|
|
|
if (mcu_isp_pin >= 0) { |
|
|
|
GPIO_OUTPUT_SET(mcu_isp_pin, 1); |
|
|
|
GPIO_OUTPUT_SET(mcu_isp_pin, 1); |
|
|
|
os_delay_us(100L); |
|
|
|
os_delay_us(100L); |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
state = TN_end; |
|
|
|
state = TN_end; |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return state; |
|
|
|
return state; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ICACHE_FLASH_ATTR serbridgeReset() { |
|
|
|
void ICACHE_FLASH_ATTR serbridgeReset() { |
|
|
|
if (mcu_reset_pin >= 0) { |
|
|
|
if (mcu_reset_pin >= 0) { |
|
|
|
os_printf("MCU reset gpio%d\n", mcu_reset_pin); |
|
|
|
os_printf("MCU reset gpio%d\n", mcu_reset_pin); |
|
|
|
GPIO_OUTPUT_SET(mcu_reset_pin, 0); |
|
|
|
GPIO_OUTPUT_SET(mcu_reset_pin, 0); |
|
|
|
os_delay_us(100L); |
|
|
|
os_delay_us(100L); |
|
|
|
GPIO_OUTPUT_SET(mcu_reset_pin, 1); |
|
|
|
GPIO_OUTPUT_SET(mcu_reset_pin, 1); |
|
|
|
} else os_printf("MCU reset: no pin\n"); |
|
|
|
} else os_printf("MCU reset: no pin\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Receive callback
|
|
|
|
// Receive callback
|
|
|
|
static void ICACHE_FLASH_ATTR serbridgeRecvCb(void *arg, char *data, unsigned short len) { |
|
|
|
static void ICACHE_FLASH_ATTR serbridgeRecvCb(void *arg, char *data, unsigned short len) { |
|
|
|
serbridgeConnData *conn = serbridgeFindConnData(arg); |
|
|
|
serbridgeConnData *conn = ((struct espconn*)arg)->reverse; |
|
|
|
//os_printf("Receive callback on conn %p\n", conn);
|
|
|
|
//os_printf("Receive callback on conn %p\n", conn);
|
|
|
|
if (conn == NULL) return; |
|
|
|
if (conn == NULL) return; |
|
|
|
|
|
|
|
|
|
|
|
// at the start of a connection we're in cmInit mode and we wait for the first few characters
|
|
|
|
// at the start of a connection we're in cmInit mode and we wait for the first few characters
|
|
|
|
// to arrive in order to decide what type of connection this is.. The following if statements
|
|
|
|
// to arrive in order to decide what type of connection this is.. The following if statements
|
|
|
|
// do this dispatch. An issue here is that we assume that the first few characters all arrive
|
|
|
|
// do this dispatch. An issue here is that we assume that the first few characters all arrive
|
|
|
|
// in the same TCP packet, which is true if the sender is a program, but not necessarily
|
|
|
|
// in the same TCP packet, which is true if the sender is a program, but not necessarily
|
|
|
|
// if the sender is a person typing (although in that case the line-oriented TTY input seems
|
|
|
|
// if the sender is a person typing (although in that case the line-oriented TTY input seems
|
|
|
|
// to make it work too). If this becomes a problem we need to buffer the first few chars...
|
|
|
|
// to make it work too). If this becomes a problem we need to buffer the first few chars...
|
|
|
|
if (conn->conn_mode == cmInit) { |
|
|
|
if (conn->conn_mode == cmInit) { |
|
|
|
|
|
|
|
|
|
|
|
// If the connection starts with the Arduino or ARM reset sequence we perform a RESET
|
|
|
|
// If the connection starts with the Arduino or ARM reset sequence we perform a RESET
|
|
|
|
if ((len == 2 && strncmp(data, "0 ", 2) == 0) || |
|
|
|
if ((len == 2 && strncmp(data, "0 ", 2) == 0) || |
|
|
|
(len == 2 && strncmp(data, "?\n", 2) == 0) || |
|
|
|
(len == 2 && strncmp(data, "?\n", 2) == 0) || |
|
|
|
(len == 3 && strncmp(data, "?\r\n", 3) == 0)) { |
|
|
|
(len == 3 && strncmp(data, "?\r\n", 3) == 0)) { |
|
|
|
os_printf("MCU Reset=%d ISP=%d\n", mcu_reset_pin, mcu_isp_pin); |
|
|
|
os_printf("MCU Reset=%d ISP=%d\n", mcu_reset_pin, mcu_isp_pin); |
|
|
|
os_delay_us(2*1000L); // time for os_printf to happen
|
|
|
|
os_delay_us(2*1000L); // time for os_printf to happen
|
|
|
|
// send reset to arduino/ARM
|
|
|
|
// send reset to arduino/ARM
|
|
|
|
if (mcu_reset_pin >= 0) GPIO_OUTPUT_SET(mcu_reset_pin, 0); |
|
|
|
if (mcu_reset_pin >= 0) GPIO_OUTPUT_SET(mcu_reset_pin, 0); |
|
|
|
os_delay_us(100L); |
|
|
|
os_delay_us(100L); |
|
|
|
if (mcu_isp_pin >= 0) GPIO_OUTPUT_SET(mcu_isp_pin, 0); |
|
|
|
if (mcu_isp_pin >= 0) GPIO_OUTPUT_SET(mcu_isp_pin, 0); |
|
|
|
os_delay_us(100L); |
|
|
|
os_delay_us(100L); |
|
|
|
if (mcu_reset_pin >= 0) GPIO_OUTPUT_SET(mcu_reset_pin, 1); |
|
|
|
if (mcu_reset_pin >= 0) GPIO_OUTPUT_SET(mcu_reset_pin, 1); |
|
|
|
os_delay_us(100L); |
|
|
|
os_delay_us(100L); |
|
|
|
if (mcu_isp_pin >= 0) GPIO_OUTPUT_SET(mcu_isp_pin, 1); |
|
|
|
if (mcu_isp_pin >= 0) GPIO_OUTPUT_SET(mcu_isp_pin, 1); |
|
|
|
os_delay_us(1000L); |
|
|
|
os_delay_us(1000L); |
|
|
|
conn->conn_mode = cmAVR; |
|
|
|
conn->conn_mode = cmAVR; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If the connection starts with a telnet negotiation we will do telnet
|
|
|
|
// If the connection starts with a telnet negotiation we will do telnet
|
|
|
|
} else if (len >= 3 && strncmp(data, (char[]){IAC, WILL, ComPortOpt}, 3) == 0) { |
|
|
|
} else if (len >= 3 && strncmp(data, (char[]){IAC, WILL, ComPortOpt}, 3) == 0) { |
|
|
|
conn->conn_mode = cmTelnet; |
|
|
|
conn->conn_mode = cmTelnet; |
|
|
|
conn->telnet_state = TN_normal; |
|
|
|
conn->telnet_state = TN_normal; |
|
|
|
// note that the three negotiation chars will be gobbled-up by telnetUnwrap
|
|
|
|
// note that the three negotiation chars will be gobbled-up by telnetUnwrap
|
|
|
|
os_printf("telnet mode\n"); |
|
|
|
os_printf("telnet mode\n"); |
|
|
|
|
|
|
|
|
|
|
|
// looks like a plain-vanilla connection!
|
|
|
|
// looks like a plain-vanilla connection!
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
conn->conn_mode = cmTransparent; |
|
|
|
conn->conn_mode = cmTransparent; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// write the buffer to the uart
|
|
|
|
// write the buffer to the uart
|
|
|
|
if (conn->conn_mode == cmTelnet) { |
|
|
|
if (conn->conn_mode == cmTelnet) { |
|
|
|
conn->telnet_state = telnetUnwrap((uint8_t *)data, len, conn->telnet_state); |
|
|
|
conn->telnet_state = telnetUnwrap((uint8_t *)data, len, conn->telnet_state); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
uart0_tx_buffer(data, len); |
|
|
|
uart0_tx_buffer(data, len); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
serledFlash(50); // short blink on serial LED
|
|
|
|
serledFlash(50); // short blink on serial LED
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Error callback (it's really poorly named, it's not a "connection reconnected" callback,
|
|
|
|
|
|
|
|
// it's really a "connection broken, please reconnect" callback)
|
|
|
|
|
|
|
|
static void ICACHE_FLASH_ATTR serbridgeReconCb(void *arg, sint8 err) { |
|
|
|
|
|
|
|
serbridgeConnData *conn=serbridgeFindConnData(arg); |
|
|
|
|
|
|
|
if (conn == NULL) return; |
|
|
|
|
|
|
|
// Close the connection
|
|
|
|
|
|
|
|
espconn_disconnect(conn->conn); |
|
|
|
|
|
|
|
conn->conn = NULL; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Disconnection callback
|
|
|
|
// Disconnection callback
|
|
|
|
static void ICACHE_FLASH_ATTR serbridgeDisconCb(void *arg) { |
|
|
|
static void ICACHE_FLASH_ATTR serbridgeDisconCb(void *arg) { |
|
|
|
// Iterate through all the connections and deallocate the ones that are in a state that
|
|
|
|
serbridgeConnData *conn = ((struct espconn*)arg)->reverse; |
|
|
|
// indicates that they're closed
|
|
|
|
if (conn == NULL) return; |
|
|
|
for (int i=0; i<MAX_CONN; i++) { |
|
|
|
// Free buffers
|
|
|
|
if (connData[i].conn != NULL && |
|
|
|
if (conn->sentbuffer != NULL) os_free(conn->sentbuffer); |
|
|
|
(connData[i].conn->state == ESPCONN_NONE || connData[i].conn->state == ESPCONN_CLOSE)) |
|
|
|
conn->sentbuffer = NULL; |
|
|
|
{ |
|
|
|
if (conn->txbuffer != NULL) os_free(conn->txbuffer); |
|
|
|
if (connData[i].conn_mode == cmAVR) { |
|
|
|
conn->txbuffer = NULL; |
|
|
|
// send reset to arduino/ARM
|
|
|
|
conn->txbufferlen = 0; |
|
|
|
if (mcu_reset_pin >= 0) { |
|
|
|
// Send reset to attached uC if it was in programming mode
|
|
|
|
GPIO_OUTPUT_SET(mcu_reset_pin, 0); |
|
|
|
if (conn->conn_mode == cmAVR && mcu_reset_pin >= 0) { |
|
|
|
os_delay_us(100L); |
|
|
|
GPIO_OUTPUT_SET(mcu_reset_pin, 0); |
|
|
|
GPIO_OUTPUT_SET(mcu_reset_pin, 1); |
|
|
|
os_delay_us(100L); |
|
|
|
} |
|
|
|
GPIO_OUTPUT_SET(mcu_reset_pin, 1); |
|
|
|
} |
|
|
|
} |
|
|
|
connData[i].conn = NULL; |
|
|
|
conn->conn = NULL; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Connection reset callback (note that there will be no DisconCb)
|
|
|
|
|
|
|
|
static void ICACHE_FLASH_ATTR serbridgeResetCb(void *arg, sint8 err) { |
|
|
|
|
|
|
|
os_printf("serbridge: connection reset err=%d\n", err); |
|
|
|
|
|
|
|
serbridgeDisconCb(arg); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// New connection callback, use one of the connection descriptors, if we have one left.
|
|
|
|
// New connection callback, use one of the connection descriptors, if we have one left.
|
|
|
|
static void ICACHE_FLASH_ATTR serbridgeConnectCb(void *arg) { |
|
|
|
static void ICACHE_FLASH_ATTR serbridgeConnectCb(void *arg) { |
|
|
|
struct espconn *conn = arg; |
|
|
|
struct espconn *conn = arg; |
|
|
|
//Find empty conndata in pool
|
|
|
|
//Find empty conndata in pool
|
|
|
|
int i; |
|
|
|
int i; |
|
|
|
for (i=0; i<MAX_CONN; i++) if (connData[i].conn==NULL) break; |
|
|
|
for (i=0; i<MAX_CONN; i++) if (connData[i].conn==NULL) break; |
|
|
|
os_printf("Accept port 23, conn=%p, pool slot %d\n", conn, i); |
|
|
|
os_printf("Accept port 23, conn=%p, pool slot %d\n", conn, i); |
|
|
|
|
|
|
|
|
|
|
|
if (i==MAX_CONN) { |
|
|
|
if (i==MAX_CONN) { |
|
|
|
os_printf("Aiee, conn pool overflow!\n"); |
|
|
|
os_printf("Aiee, conn pool overflow!\n"); |
|
|
|
espconn_disconnect(conn); |
|
|
|
espconn_disconnect(conn); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
connData[i].conn=conn; |
|
|
|
os_memset(connData+i, 0, sizeof(struct serbridgeConnData)); |
|
|
|
connData[i].txbufferlen = 0; |
|
|
|
connData[i].conn = conn; |
|
|
|
connData[i].readytosend = true; |
|
|
|
conn->reverse = connData+i; |
|
|
|
connData[i].telnet_state = 0; |
|
|
|
connData[i].readytosend = true; |
|
|
|
connData[i].conn_mode = cmInit; |
|
|
|
connData[i].conn_mode = cmInit; |
|
|
|
|
|
|
|
|
|
|
|
espconn_regist_recvcb(conn, serbridgeRecvCb); |
|
|
|
espconn_regist_recvcb(conn, serbridgeRecvCb); |
|
|
|
espconn_regist_reconcb(conn, serbridgeReconCb); |
|
|
|
espconn_regist_disconcb(conn, serbridgeDisconCb); |
|
|
|
espconn_regist_disconcb(conn, serbridgeDisconCb); |
|
|
|
espconn_regist_reconcb(conn, serbridgeResetCb); |
|
|
|
espconn_regist_sentcb(conn, serbridgeSentCb); |
|
|
|
espconn_regist_sentcb(conn, serbridgeSentCb); |
|
|
|
|
|
|
|
|
|
|
|
espconn_set_opt(conn, ESPCONN_REUSEADDR|ESPCONN_NODELAY); |
|
|
|
espconn_set_opt(conn, ESPCONN_REUSEADDR|ESPCONN_NODELAY); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// callback with a buffer of characters that have arrived on the uart
|
|
|
|
// callback with a buffer of characters that have arrived on the uart
|
|
|
|
void ICACHE_FLASH_ATTR |
|
|
|
void ICACHE_FLASH_ATTR |
|
|
|
serbridgeUartCb(char *buf, int length) { |
|
|
|
serbridgeUartCb(char *buf, int length) { |
|
|
|
// push the buffer into the microcontroller console
|
|
|
|
// push the buffer into the microcontroller console
|
|
|
|
for (int i=0; i<length; i++) |
|
|
|
for (int i=0; i<length; i++) |
|
|
|
console_write_char(buf[i]); |
|
|
|
console_write_char(buf[i]); |
|
|
|
// push the buffer into each open connection
|
|
|
|
// push the buffer into each open connection
|
|
|
|
for (int i = 0; i < MAX_CONN; ++i) { |
|
|
|
for (int i = 0; i < MAX_CONN; ++i) { |
|
|
|
if (connData[i].conn) { |
|
|
|
if (connData[i].conn) { |
|
|
|
espbuffsend(&connData[i], buf, length); |
|
|
|
espbuffsend(&connData[i], buf, length); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
serledFlash(50); // short blink on serial LED
|
|
|
|
serledFlash(50); // short blink on serial LED
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ICACHE_FLASH_ATTR serbridgeInitPins() { |
|
|
|
void ICACHE_FLASH_ATTR serbridgeInitPins() { |
|
|
|
mcu_reset_pin = flashConfig.reset_pin; |
|
|
|
mcu_reset_pin = flashConfig.reset_pin; |
|
|
|
mcu_isp_pin = flashConfig.isp_pin; |
|
|
|
mcu_isp_pin = flashConfig.isp_pin; |
|
|
|
os_printf("Serbridge pins: reset=%d isp=%d swap=%d\n", |
|
|
|
os_printf("Serbridge pins: reset=%d isp=%d swap=%d\n", |
|
|
|
mcu_reset_pin, mcu_isp_pin, flashConfig.swap_uart); |
|
|
|
mcu_reset_pin, mcu_isp_pin, flashConfig.swap_uart); |
|
|
|
|
|
|
|
|
|
|
|
if (flashConfig.swap_uart) { |
|
|
|
if (flashConfig.swap_uart) { |
|
|
|
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 4); |
|
|
|
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 4); |
|
|
|
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 4); |
|
|
|
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 4); |
|
|
|
PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTCK_U); |
|
|
|
PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTCK_U); |
|
|
|
PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTDO_U); |
|
|
|
PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTDO_U); |
|
|
|
system_uart_swap(); |
|
|
|
system_uart_swap(); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, 0); |
|
|
|
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, 0); |
|
|
|
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, 0); |
|
|
|
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, 0); |
|
|
|
system_uart_de_swap(); |
|
|
|
system_uart_de_swap(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// set both pins to 1 before turning them on so we don't cause a reset
|
|
|
|
// set both pins to 1 before turning them on so we don't cause a reset
|
|
|
|
if (mcu_isp_pin >= 0) GPIO_OUTPUT_SET(mcu_isp_pin, 1); |
|
|
|
if (mcu_isp_pin >= 0) GPIO_OUTPUT_SET(mcu_isp_pin, 1); |
|
|
|
if (mcu_reset_pin >= 0) GPIO_OUTPUT_SET(mcu_reset_pin, 1); |
|
|
|
if (mcu_reset_pin >= 0) GPIO_OUTPUT_SET(mcu_reset_pin, 1); |
|
|
|
// switch pin mux to make these pins GPIO pins
|
|
|
|
// switch pin mux to make these pins GPIO pins
|
|
|
|
if (mcu_reset_pin >= 0) makeGpio(mcu_reset_pin); |
|
|
|
if (mcu_reset_pin >= 0) makeGpio(mcu_reset_pin); |
|
|
|
if (mcu_isp_pin >= 0) makeGpio(mcu_isp_pin); |
|
|
|
if (mcu_isp_pin >= 0) makeGpio(mcu_isp_pin); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Start transparent serial bridge TCP server on specified port (typ. 23)
|
|
|
|
// Start transparent serial bridge TCP server on specified port (typ. 23)
|
|
|
|
void ICACHE_FLASH_ATTR serbridgeInit(int port) { |
|
|
|
void ICACHE_FLASH_ATTR serbridgeInit(int port) { |
|
|
|
serbridgeInitPins(); |
|
|
|
serbridgeInitPins(); |
|
|
|
|
|
|
|
|
|
|
|
int i; |
|
|
|
for (int i = 0; i < MAX_CONN; i++) { |
|
|
|
for (i = 0; i < MAX_CONN; i++) { |
|
|
|
connData[i].conn = NULL; |
|
|
|
connData[i].conn = NULL; |
|
|
|
} |
|
|
|
connData[i].txbuffer = txbuffer[i]; |
|
|
|
serbridgeConn.type = ESPCONN_TCP; |
|
|
|
} |
|
|
|
serbridgeConn.state = ESPCONN_NONE; |
|
|
|
serbridgeConn.type = ESPCONN_TCP; |
|
|
|
serbridgeTcp.local_port = port; |
|
|
|
serbridgeConn.state = ESPCONN_NONE; |
|
|
|
serbridgeConn.proto.tcp = &serbridgeTcp; |
|
|
|
serbridgeTcp.local_port = port; |
|
|
|
|
|
|
|
serbridgeConn.proto.tcp = &serbridgeTcp; |
|
|
|
espconn_regist_connectcb(&serbridgeConn, serbridgeConnectCb); |
|
|
|
|
|
|
|
espconn_accept(&serbridgeConn); |
|
|
|
espconn_regist_connectcb(&serbridgeConn, serbridgeConnectCb); |
|
|
|
espconn_tcp_set_max_con_allow(&serbridgeConn, MAX_CONN); |
|
|
|
espconn_accept(&serbridgeConn); |
|
|
|
espconn_regist_time(&serbridgeConn, SER_BRIDGE_TIMEOUT, 0); |
|
|
|
espconn_tcp_set_max_con_allow(&serbridgeConn, MAX_CONN); |
|
|
|
|
|
|
|
espconn_regist_time(&serbridgeConn, SER_BRIDGE_TIMEOUT, 0); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|