diff --git a/esp-link/cgitelnet.c b/esp-link/cgitelnet.c index 1ffe3f9..c3a8c00 100644 --- a/esp-link/cgitelnet.c +++ b/esp-link/cgitelnet.c @@ -28,12 +28,16 @@ int ICACHE_FLASH_ATTR cgiTelnetSet(HttpdConnData *connData) { return HTTPD_CGI_DONE; // Connection aborted } - char *coll; int8_t ok = 0; uint16_t port1, port2; ok |= getUInt16Arg(connData, "port1", &port1); ok |= getUInt16Arg(connData, "port2", &port2); - if (ok < 0) { coll = "Failed to fetch ports. Are they valid?"; goto collision; } + if (ok <= 0) { //If we get at least one good value, this should be >= 1 + os_printf("Unable to fetch telnet ports.\n Received: port1=%d port2=%d\n", + flashConfig.telnet_port1, flashConfig.telnet_port2); + errorResponse(connData, 400, buff); + return HTTPD_CGI_DONE; + } if (ok > 0) { // fill both port variables from flash or ajax provided value @@ -41,13 +45,18 @@ int ICACHE_FLASH_ATTR cgiTelnetSet(HttpdConnData *connData) { if (!port2) port2 = flashConfig.telnet_port2; // check whether ports are different - if (port1 == port2) { coll = "Ports cannot be the same!"; goto collision; } + if (port1 == port2) { + os_printf("Ports cannot be the same.\n Tried to set: port1=%d port2=%d\n", + flashConfig.telnet_port1, flashConfig.telnet_port2); + errorResponse(connData, 400, buff); + return HTTPD_CGI_DONE; + } // we're good, set flashconfig flashConfig.telnet_port1 = port1; flashConfig.telnet_port2 = port2; os_printf("Telnet ports changed: port1=%d port2=%d\n", - flashConfig.telnet_port1, flashConfig.telnet_port2); + flashConfig.telnet_port1, flashConfig.telnet_port2); // save to flash if (configSave()) { @@ -60,16 +69,13 @@ int ICACHE_FLASH_ATTR cgiTelnetSet(HttpdConnData *connData) { } // apply the changes - serbridgeInit(flashConfig.telnet_port1, flashConfig.telnet_port2); + serbridgeInit(); + serbridgeStart(1, flashConfig.telnet_port1, flashDefault.telnet_port1mode); + serbridgeStart(2, flashConfig.telnet_port2, flashDefault.telnet_port2mode); + } return HTTPD_CGI_DONE; - collision: { - char buff[128]; - os_sprintf(buff, "Ports assignment for %s collides with another assignment", coll); - errorResponse(connData, 400, buff); - return HTTPD_CGI_DONE; - } } int ICACHE_FLASH_ATTR cgiTelnet(HttpdConnData *connData) { diff --git a/esp-link/config.c b/esp-link/config.c index 8afedbc..bb31af5 100644 --- a/esp-link/config.c +++ b/esp-link/config.c @@ -37,6 +37,8 @@ FlashConfig flashDefault = { .stop_bits = ONE_STOP_BIT, .telnet_port1 = 23, .telnet_port2 = 2323, + .telnet_port1Mode = 0, + .telnet_port2Mode = 0, }; typedef union { @@ -163,6 +165,9 @@ bool ICACHE_FLASH_ATTR configRestore(void) { flashConfig.parity = flashDefault.parity; flashConfig.stop_bits = flashDefault.stop_bits; } + + if (flashConfig.telnet_port1 == 0) { flashConfig.telnet_port1 = flashDefault.telnet_port1; } + if (flashConfig.telnet_port2 == 0) { flashConfig.telnet_port2 = flashDefault.telnet_port2; } return true; } diff --git a/esp-link/config.h b/esp-link/config.h index 2f5aede..c1c1f68 100644 --- a/esp-link/config.h +++ b/esp-link/config.h @@ -41,8 +41,10 @@ typedef struct { int8_t data_bits; int8_t parity; int8_t stop_bits; - uint16_t telnet_port1, // Telnet port settings + uint16_t telnet_port1, // Telnet port settings telnet_port2; + int8_t telnet_port1mode, + telnet_port2mode; } FlashConfig; extern FlashConfig flashConfig; diff --git a/esp-link/main.c b/esp-link/main.c index d70e86a..54fa075 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -181,7 +181,10 @@ user_init(void) { WEB_Init(); // init the wifi-serial transparent bridge (port 23) - serbridgeInit(flashConfig.telnet_port1, flashConfig.telnet_port1); + serbridgeInit(); + serbridgeStart(1, flashConfig.telnet_port1, flashDefault.telnet_port1mode); + serbridgeStart(2, flashConfig.telnet_port2, flashDefault.telnet_port2mode); + uart_add_recv_cb(&serbridgeUartCb); #ifdef SHOW_HEAP_USE os_timer_disarm(&prHeapTimer); diff --git a/html/home.html b/html/home.html index d2c9791..a3d2b87 100644 --- a/html/home.html +++ b/html/home.html @@ -50,7 +50,7 @@ the online README for up-to-date help.

-
+

Telnet Serial-Bridge

There are two ports available for telnet to use by default: 23 & 2323
Note - this time, only port1 may be changed & used sucessfully.

diff --git a/serial/serbridge.c b/serial/serbridge.c index cebc561..e82e3b4 100644 --- a/serial/serbridge.c +++ b/serial/serbridge.c @@ -16,8 +16,9 @@ #define syslog(X1...) #endif -static struct espconn serbridgeConn1; // plain bridging port -static struct espconn serbridgeConn2; // programming port +static struct espconn serbridgeConn[1]; // plain bridging port +static struct espconn serbridgeConn[2]; // programming port +static esp_tcp serbridgeTcp[1], serbridgeTcp[2]; static esp_tcp serbridgeTcp1, serbridgeTcp2; static int8_t mcu_reset_pin, mcu_isp_pin; @@ -421,7 +422,7 @@ serbridgeConnectCb(void *arg) connData[i].readytosend = true; connData[i].conn_mode = cmInit; // if it's the second port we start out in programming mode - if (conn->proto.tcp->local_port == serbridgeConn2.proto.tcp->local_port) + if (conn->proto.tcp->local_port == serbridgeConn[2].proto.tcp->local_port) connData[i].conn_mode = cmPGMInit; espconn_regist_recvcb(conn, serbridgeRecvCb); @@ -470,35 +471,44 @@ serbridgeInitPins() // Start transparent serial bridge TCP server on specified port (typ. 23) void ICACHE_FLASH_ATTR -serbridgeInit(int port1, int port2) +serbridgeInit() { serbridgeInitPins(); os_memset(connData, 0, sizeof(connData)); - os_memset(&serbridgeTcp1, 0, sizeof(serbridgeTcp1)); - os_memset(&serbridgeTcp2, 0, sizeof(serbridgeTcp2)); - - // set-up the primary port for plain bridging - serbridgeConn1.type = ESPCONN_TCP; - serbridgeConn1.state = ESPCONN_NONE; - serbridgeTcp1.local_port = port1; - serbridgeConn1.proto.tcp = &serbridgeTcp1; - - espconn_regist_connectcb(&serbridgeConn1, serbridgeConnectCb); - espconn_accept(&serbridgeConn1); - espconn_tcp_set_max_con_allow(&serbridgeConn1, MAX_CONN); - espconn_regist_time(&serbridgeConn1, SER_BRIDGE_TIMEOUT, 0); - - // set-up the secondary port for programming - serbridgeConn2.type = ESPCONN_TCP; - serbridgeConn2.state = ESPCONN_NONE; - serbridgeTcp2.local_port = port2; - serbridgeConn2.proto.tcp = &serbridgeTcp2; - - espconn_regist_connectcb(&serbridgeConn2, serbridgeConnectCb); - espconn_accept(&serbridgeConn2); - espconn_tcp_set_max_con_allow(&serbridgeConn2, MAX_CONN); - espconn_regist_time(&serbridgeConn2, SER_BRIDGE_TIMEOUT, 0); + os_memset(&serbridgeTcp[1], 0, sizeof(serbridgeTcp[1])); + os_memset(&serbridgeTcp[2], 0, sizeof(serbridgeTcp[2])); +} + +// Start transparent serial bridge TCP server on specified port (typ. 23) +void ICACHE_FLASH_ATTR +serbridgeStart(int ix, int port, int mode) +{ + + if (ix < 0 || ix > 2) // FIXME hardcoded limit + return; + if (serbridgeConn[ix] != NULL) { serbridgeCleanup(serbridgeConn[ix]); } //If we are already initialized, let's clean it up. + if (0 < port && port < 65536 && port != 80) { + serbridgeConn[ix].type = ESPCONN_TCP; + serbridgeConn[ix].state = ESPCONN_NONE; + serbridgeTcp[ix].local_port = port; + serbridgeConn[ix].proto.tcp = &serbridgeTcp[ix]; + + espconn_regist_connectcb(&serbridgeConn[ix], serbridgeConnectCb); + espconn_accept(&serbridgeConn[ix]); + espconn_tcp_set_max_con_allow(&serbridgeConn[ix], MAX_CONN); + espconn_regist_time(&serbridgeConn[ix], SER_BRIDGE_TIMEOUT, 0); + } +} + +static void ICACHE_FLASH_ATTR +serbridgeCleanup(void *arg) +{ + serbridgeConnData *conn = ((struct espconn*)arg)->reverse; + if (conn == NULL) return; + // Free memory & set to NULL + os_free(conn); + conn = NULL; } int ICACHE_FLASH_ATTR serbridgeInMCUFlashing() diff --git a/serial/serbridge.h b/serial/serbridge.h index ed661e1..a1122ef 100644 --- a/serial/serbridge.h +++ b/serial/serbridge.h @@ -31,11 +31,11 @@ typedef struct serbridgeConnData { } serbridgeConnData; // port1 is transparent&programming, second port is programming only -void ICACHE_FLASH_ATTR serbridgeInit(int port1, int port2); +void ICACHE_FLASH_ATTR serbridgeInit(); +void ICACHE_FLASH_ATTR serbridgeStart(int ix, int port, int mode); void ICACHE_FLASH_ATTR serbridgeInitPins(void); void ICACHE_FLASH_ATTR serbridgeUartCb(char *buf, short len); void ICACHE_FLASH_ATTR serbridgeReset(); - int ICACHE_FLASH_ATTR serbridgeInMCUFlashing(); // callback when receiving UART chars when in programming mode