From a485e8e13a6fc4a83491bc74e74e6aeabb438a71 Mon Sep 17 00:00:00 2001 From: Clemens Kirchgatterer Date: Tue, 19 Feb 2019 15:55:50 +0100 Subject: [PATCH 1/8] emulation on host: Add full UART driver emulation. This PR replaces the high level Serial mock with a more complete UART driver. This way the HardwareSerial works without any modifications. Additionally the driver supports UART0 and UART1 at the same time (UART0 is directed to stdout and UART1 writes to stderr). RX is implemented by switching the terminal into raw non-blocking mode and injecting each key-press directly into the FIFO of UART0. A new command line switch -c was added to ignore CTRL-C and send it via serial as well. The decumentation was updated accordingly. Reading and setting of GPIOs does only write to stderr, when compiled with D=1. But this is subject to be replaced with a proper GPIO emu anyway. Reading from GPIO0 now returns 1 instead of 0 because this is most likely a low active input. --- tests/host/Makefile | 3 +- tests/host/README.txt | 12 +- tests/host/common/ArduinoMain.cpp | 63 +++- tests/host/common/HostWiring.cpp | 14 +- tests/host/common/MockSerial.cpp | 137 --------- tests/host/common/MockUART.cpp | 465 ++++++++++++++++++++++++++++++ tests/host/common/mock.h | 9 + 7 files changed, 556 insertions(+), 147 deletions(-) delete mode 100644 tests/host/common/MockSerial.cpp create mode 100644 tests/host/common/MockUART.cpp diff --git a/tests/host/Makefile b/tests/host/Makefile index b0749392e2..0205368f73 100644 --- a/tests/host/Makefile +++ b/tests/host/Makefile @@ -65,6 +65,7 @@ CORE_CPP_FILES := $(addprefix $(CORE_PATH)/,\ libb64/cencode.cpp \ libb64/cdecode.cpp \ Schedule.cpp \ + HardwareSerial.cpp \ ) CORE_C_FILES := $(addprefix $(CORE_PATH)/,\ @@ -74,7 +75,7 @@ MOCK_CPP_FILES_COMMON := $(addprefix common/,\ Arduino.cpp \ spiffs_mock.cpp \ WMath.cpp \ - MockSerial.cpp \ + MockUART.cpp \ MockTools.cpp \ MocklwIP.cpp \ ) diff --git a/tests/host/README.txt b/tests/host/README.txt index 8bc0dac85c..4e947a954e 100644 --- a/tests/host/README.txt +++ b/tests/host/README.txt @@ -19,6 +19,13 @@ WiFiClient+WiFiServer/ClientContext and WifiUdp/UdpContext using socket posix API. Further work will optionally propose native lwIP library instead. +Serial UART0 and UART1 are emulated via stdin/stdout/stderr. Therefor +stdin is connected to UART0(RX) and stdout is connected to UART0(TX). +UART1(TX) writes to stderr. Reading from stdin happens in non-blocking +raw mode, that means each character is directly injected into the UART +FIFO without any buffering in the console. The command line switch -c +can be used to stop the emulation from intersepting CTRL-C (SIGINT). + How to compile and run a sketch ------------------------------- @@ -73,7 +80,10 @@ Options are available: -h -i eth0 bind servers to this interface (WIP) -l bind Multicast to the above interface (WIP) - -f no throttle (possibly 100%CPU) + -c ignore CTRL-C (send it over serial device) + -f no throttle (possibly 100%CPU) + -S spiffs size in KBytes (default: 1024) + (negative value will force mismatched size) TODO ---- diff --git a/tests/host/common/ArduinoMain.cpp b/tests/host/common/ArduinoMain.cpp index 54420d9e2a..202a5a8c50 100644 --- a/tests/host/common/ArduinoMain.cpp +++ b/tests/host/common/ArduinoMain.cpp @@ -33,12 +33,49 @@ #include // wifi_get_ip_info() #include -#include // usleep +#include #include +#include +#include +#include bool user_exit = false; const char* host_interface = nullptr; size_t spiffs_kb = 1024; +bool ignore_sigint = false; + +#define STDIN 0 + +static struct termios initial_settings; + +static int mock_start_uart(void) +{ + struct termios settings; + + if (tcgetattr(STDIN, &initial_settings) < 0) return (-1); + settings = initial_settings; + if (ignore_sigint) settings.c_lflag &= ~(ISIG); + settings.c_oflag = 0; + settings.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN); + settings.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | + PARMRK | INPCK | ISTRIP | IXON ); + settings.c_cc[VMIN] = 0; + settings.c_cc[VTIME] = 0; + if (tcsetattr(STDIN, TCSANOW, &settings) < 0) return (-2); + return (0); +} + +static int mock_stop_uart(void) +{ + if (tcsetattr(STDIN, TCSANOW, &initial_settings) < 0) return (-1); + return (0); +} + +static uint8_t mock_read_uart(void) +{ + uint8_t ch = 0; + return (read(STDIN, &ch, 1) == 1) ? ch : 0; +} void help (const char* argv0, int exitcode) { @@ -48,6 +85,7 @@ void help (const char* argv0, int exitcode) " -h\n" " -i - use this interface for IP address\n" " -l - bind tcp/udp servers to interface only (not 0.0.0.0)\n" + " -c - ignore CTRL-C (send it via Serial)\n" " -f - no throttle (possibly 100%%CPU)\n" " -S - spiffs size in KBytes (default: %zd)\n" " (negative value will force mismatched size)\n" @@ -60,13 +98,15 @@ static struct option options[] = { "help", no_argument, NULL, 'h' }, { "fast", no_argument, NULL, 'f' }, { "local", no_argument, NULL, 'l' }, + { "sigint", no_argument, NULL, 'c' }, { "interface", required_argument, NULL, 'i' }, { "spiffskb", required_argument, NULL, 'S' }, }; -void save () +void cleanup () { mock_stop_spiffs(); + mock_stop_uart(); } void control_c (int sig) @@ -76,7 +116,7 @@ void control_c (int sig) if (user_exit) { fprintf(stderr, MOCK "stuck, killing\n"); - save(); + cleanup(); exit(1); } user_exit = true; @@ -90,7 +130,7 @@ int main (int argc, char* const argv []) for (;;) { - int n = getopt_long(argc, argv, "hlfi:S:", options, NULL); + int n = getopt_long(argc, argv, "hlcfi:S:", options, NULL); if (n < 0) break; switch (n) @@ -104,6 +144,9 @@ int main (int argc, char* const argv []) case 'l': global_ipv4_netfmt = NO_GLOBAL_BINDING; break; + case 'c': + ignore_sigint = true; + break; case 'f': fast = true; break; @@ -128,16 +171,22 @@ int main (int argc, char* const argv []) // setup global global_ipv4_netfmt wifi_get_ip_info(0, nullptr); + // set stdin to non blocking mode + mock_start_uart(); + setup(); while (!user_exit) { + uint8_t data = mock_read_uart(); + + if (data) + uart_new_data(UART0, data); if (!fast) - usleep(10000); // not 100% cpu + usleep(1000); // not 100% cpu, ~1000 loops per second loop(); check_incoming_udp(); } - - save(); + cleanup(); return 0; } diff --git a/tests/host/common/HostWiring.cpp b/tests/host/common/HostWiring.cpp index b2887c0323..d08733a527 100644 --- a/tests/host/common/HostWiring.cpp +++ b/tests/host/common/HostWiring.cpp @@ -46,17 +46,23 @@ void pinMode (uint8_t pin, uint8_t mode) case WAKEUP_PULLDOWN: m="WAKEUP_PULLDOWN"; break; default: m="(special)"; } +#ifdef DEBUG_ESP_CORE fprintf(stderr, MOCK "gpio%d: mode='%s'\n", pin, m); +#endif } void digitalWrite(uint8_t pin, uint8_t val) { +#ifdef DEBUG_ESP_CORE fprintf(stderr, MOCK "digitalWrite(pin=%d val=%d)\n", pin, val); +#endif } void analogWrite(uint8_t pin, int val) { +#ifdef DEBUG_ESP_CORE fprintf(stderr, MOCK "analogWrite(pin=%d, val=%d\n", pin, val); +#endif } int analogRead(uint8_t pin) @@ -67,11 +73,17 @@ int analogRead(uint8_t pin) void analogWriteRange(uint32_t range) { +#ifdef DEBUG_ESP_CORE fprintf(stderr, MOCK "analogWriteRange(range=%d)\n", range); +#endif } int digitalRead(uint8_t pin) { +#ifdef DEBUG_ESP_CORE fprintf(stderr, MOCK "digitalRead(%d)\n", pin); - return 0; +#endif + + // pin 0 is most likely a low active input + return pin ? 0 : 1; } diff --git a/tests/host/common/MockSerial.cpp b/tests/host/common/MockSerial.cpp deleted file mode 100644 index eedcaed115..0000000000 --- a/tests/host/common/MockSerial.cpp +++ /dev/null @@ -1,137 +0,0 @@ -/* - Arduino Hardware Serial emulation - Copyright (c) 2018 david gauchard. All rights reserved. - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal with the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - - Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimers. - - - Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimers in the - documentation and/or other materials provided with the distribution. - - - The names of its contributors may not be used to endorse or promote - products derived from this Software without specific prior written - permission. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR - OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS WITH THE SOFTWARE. -*/ - -#include -#include - -#include // write - -HardwareSerial Serial(UART0); - -HardwareSerial::HardwareSerial (int uart_nr) -{ - if (uart_nr != 0) - fprintf(stderr, MOCK "FIXME HardwareSerial::HardwareSerial(%d)\n", uart_nr); - _uart = (decltype(_uart))1; // not used, for 'while (!Serial);' to pass -} - -void HardwareSerial::begin (unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin) -{ - if (config != SERIAL_8N1 || mode != SERIAL_FULL || tx_pin != 1) - fprintf(stderr, MOCK "FIXME HardwareSerial::begin(baud=%ld config=0x%x mode=0x%x)\n", baud, (int)config, (int)mode); -} - -void HardwareSerial::setDebugOutput (bool on) -{ - (void)on; -} - -int HardwareSerial::available (void) -{ - printf(MOCK "TODO HardwareSerial::available\n"); - return 0; -} - -void HardwareSerial::flush () -{ - //XXXTODO - fflush(stdout); -} - -size_t HardwareSerial::readBytes(char* buffer, size_t size) -{ - size_t got = 0; - - while (got < size) - { - esp8266::polledTimeout::oneShot timeOut(_timeout); - size_t avail; - while ((avail = available()) == 0 && !timeOut); - if (avail == 0) - break; - got += read(buffer + got, std::min(size - got, avail)); - } - return got; -} - -// uart.c - -extern "C" -{ - -size_t uart_write_char (uart_t* uart, char c) -{ - //XXXTODO - (void)uart; - return write(1, &c, 1); -} - -int uart_peek_char (uart_t* uart) -{ - ///XXXTODO - static bool notimpl = false; - if (!notimpl) - { - notimpl = true; - fprintf(stderr, MOCK "FIXME uart_peek_char\n"); - } - (void)uart; - return -1; -} - -int uart_read_char (uart_t* uart) -{ - ///XXXTODO - static bool notimpl = false; - if (!notimpl) - { - notimpl = true; - fprintf(stderr, MOCK "FIXME uart_read_char\n"); - } - (void)uart; - return -1; -} - -size_t uart_write (uart_t* uart, const char* buf, size_t size) -{ - ///XXXTODO - (void)uart; - return write(1, buf, size); -} - -size_t uart_read(uart_t* uart, char* userbuffer, size_t usersize) -{ - ///XXXTODO - (void)uart; - return read(0, userbuffer, usersize); -} - -} // extern "C" diff --git a/tests/host/common/MockUART.cpp b/tests/host/common/MockUART.cpp new file mode 100644 index 0000000000..5562e17825 --- /dev/null +++ b/tests/host/common/MockUART.cpp @@ -0,0 +1,465 @@ +/* + MockUART.cpp - esp8266 UART HAL EMULATION + + Copyright (c) 2019 Clemens Kirchgatterer. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* + This UART driver is directly derived from the ESP8266 UART HAL driver + Copyright (c) 2014 Ivan Grokhotkov. It provides the same API as the + original driver and was striped from all HW dependent interfaces. + + UART0 writes got to stdout, while UART1 writes got to stderr. The user + is responsible for feeding the RX FIFO new data by calling uart_new_data(). + */ + +#include // write + +#include "Arduino.h" +#include "uart.h" + +//#define UART_DISCARD_NEWEST + +extern "C" { + +static int s_uart_debug_nr = UART1; + +static uart_t *UART[2] = { NULL, NULL }; + +struct uart_rx_buffer_ +{ + size_t size; + size_t rpos; + size_t wpos; + uint8_t * buffer; +}; + +struct uart_ +{ + int uart_nr; + int baud_rate; + bool rx_enabled; + bool tx_enabled; + bool rx_overrun; + struct uart_rx_buffer_ * rx_buffer; +}; + +// write one byte to the emulated UART +static void +uart_do_write_char(const int uart_nr, char c) +{ + if (uart_nr >= UART0 && uart_nr <= UART1) + write(uart_nr + 1, &c, 1); +} + +// write a new byte into the RX FIFO buffer +static void +uart_handle_data(uart_t* uart, uint8_t data) +{ + struct uart_rx_buffer_ *rx_buffer = uart->rx_buffer; + + size_t nextPos = (rx_buffer->wpos + 1) % rx_buffer->size; + if(nextPos == rx_buffer->rpos) + { + uart->rx_overrun = true; +#ifdef UART_DISCARD_NEWEST + return; +#else + if (++rx_buffer->rpos == rx_buffer->size) + rx_buffer->rpos = 0; +#endif + } + rx_buffer->buffer[rx_buffer->wpos] = data; + rx_buffer->wpos = nextPos; +} + +// insert a new byte into the RX FIFO nuffer +void +uart_new_data(const int uart_nr, uint8_t data) +{ + uart_t* uart = UART[uart_nr]; + + if(uart == NULL || !uart->rx_enabled) { + return; + } + + uart_handle_data(uart, data); +} + +static size_t +uart_rx_available_unsafe(const struct uart_rx_buffer_ * rx_buffer) +{ + size_t ret = rx_buffer->wpos - rx_buffer->rpos; + + if(rx_buffer->wpos < rx_buffer->rpos) + ret = (rx_buffer->wpos + rx_buffer->size) - rx_buffer->rpos; + + return ret; +} + +// taking data straight from fifo, only needed in uart_resize_rx_buffer() +static int +uart_read_char_unsafe(uart_t* uart) +{ + if (uart_rx_available_unsafe(uart->rx_buffer)) + { + // take oldest sw data + int ret = uart->rx_buffer->buffer[uart->rx_buffer->rpos]; + uart->rx_buffer->rpos = (uart->rx_buffer->rpos + 1) % uart->rx_buffer->size; + return ret; + } + // unavailable + return -1; +} + +/**********************************************************/ +/************ UART API FUNCTIONS **************************/ +/**********************************************************/ + +size_t +uart_rx_available(uart_t* uart) +{ + if(uart == NULL || !uart->rx_enabled) + return 0; + + return uart_rx_available_unsafe(uart->rx_buffer); +} + +int +uart_peek_char(uart_t* uart) +{ + if(uart == NULL || !uart->rx_enabled) + return -1; + + if (!uart_rx_available_unsafe(uart->rx_buffer)) + return -1; + + return uart->rx_buffer->buffer[uart->rx_buffer->rpos]; +} + +int +uart_read_char(uart_t* uart) +{ + uint8_t ret; + return uart_read(uart, (char*)&ret, 1) ? ret : -1; +} + +size_t +uart_read(uart_t* uart, char* userbuffer, size_t usersize) +{ + if(uart == NULL || !uart->rx_enabled) + return 0; + + size_t ret = 0; + while (ret < usersize && uart_rx_available_unsafe(uart->rx_buffer)) + { + // pour sw buffer to user's buffer + // get largest linear length from sw buffer + size_t chunk = uart->rx_buffer->rpos < uart->rx_buffer->wpos ? + uart->rx_buffer->wpos - uart->rx_buffer->rpos : + uart->rx_buffer->size - uart->rx_buffer->rpos; + if (ret + chunk > usersize) + chunk = usersize - ret; + memcpy(userbuffer + ret, uart->rx_buffer->buffer + uart->rx_buffer->rpos, chunk); + uart->rx_buffer->rpos = (uart->rx_buffer->rpos + chunk) % uart->rx_buffer->size; + ret += chunk; + } + return ret; +} + +size_t +uart_resize_rx_buffer(uart_t* uart, size_t new_size) +{ + if(uart == NULL || !uart->rx_enabled) + return 0; + + if(uart->rx_buffer->size == new_size) + return uart->rx_buffer->size; + + uint8_t * new_buf = (uint8_t*)malloc(new_size); + if(!new_buf) + return uart->rx_buffer->size; + + size_t new_wpos = 0; + // if uart_rx_available_unsafe() returns non-0, uart_read_char_unsafe() can't return -1 + while(uart_rx_available_unsafe(uart->rx_buffer) && new_wpos < new_size) + new_buf[new_wpos++] = uart_read_char_unsafe(uart); + if (new_wpos == new_size) + new_wpos = 0; + + uint8_t * old_buf = uart->rx_buffer->buffer; + uart->rx_buffer->rpos = 0; + uart->rx_buffer->wpos = new_wpos; + uart->rx_buffer->size = new_size; + uart->rx_buffer->buffer = new_buf; + free(old_buf); + return uart->rx_buffer->size; +} + +size_t +uart_get_rx_buffer_size(uart_t* uart) +{ + return uart && uart->rx_enabled ? uart->rx_buffer->size : 0; +} + +size_t +uart_write_char(uart_t* uart, char c) +{ + if(uart == NULL || !uart->tx_enabled) + return 0; + + uart_do_write_char(uart->uart_nr, c); + + return 1; +} + +size_t +uart_write(uart_t* uart, const char* buf, size_t size) +{ + if(uart == NULL || !uart->tx_enabled) + return 0; + + size_t ret = size; + const int uart_nr = uart->uart_nr; + while (size--) + uart_do_write_char(uart_nr, *buf++); + + return ret; +} + +size_t +uart_tx_free(uart_t* uart) +{ + if(uart == NULL || !uart->tx_enabled) + return 0; + + return UART_TX_FIFO_SIZE; +} + +void +uart_wait_tx_empty(uart_t* uart) +{ +} + +void +uart_flush(uart_t* uart) +{ + if(uart == NULL) + return; + + if(uart->rx_enabled) + { + uart->rx_buffer->rpos = 0; + uart->rx_buffer->wpos = 0; + } +} + +void +uart_set_baudrate(uart_t* uart, int baud_rate) +{ + if(uart == NULL) + return; + + uart->baud_rate = baud_rate; +} + +int +uart_get_baudrate(uart_t* uart) +{ + if(uart == NULL) + return 0; + + return uart->baud_rate; +} + +uart_t* +uart_init(int uart_nr, int baudrate, int config, int mode, int tx_pin, size_t rx_size) +{ + uart_t* uart = (uart_t*) malloc(sizeof(uart_t)); + if(uart == NULL) + return NULL; + + uart->uart_nr = uart_nr; + uart->rx_overrun = false; + + switch(uart->uart_nr) + { + case UART0: + uart->rx_enabled = (mode != UART_TX_ONLY); + uart->tx_enabled = (mode != UART_RX_ONLY); + if(uart->rx_enabled) + { + struct uart_rx_buffer_ * rx_buffer = (struct uart_rx_buffer_ *)malloc(sizeof(struct uart_rx_buffer_)); + if(rx_buffer == NULL) + { + free(uart); + return NULL; + } + rx_buffer->size = rx_size;//var this + rx_buffer->rpos = 0; + rx_buffer->wpos = 0; + rx_buffer->buffer = (uint8_t *)malloc(rx_buffer->size); + if(rx_buffer->buffer == NULL) + { + free(rx_buffer); + free(uart); + return NULL; + } + uart->rx_buffer = rx_buffer; + } + break; + + case UART1: + // Note: uart_interrupt_handler does not support RX on UART 1. + uart->rx_enabled = false; + uart->tx_enabled = (mode != UART_RX_ONLY); + break; + + case UART_NO: + default: + // big fail! + free(uart); + return NULL; + } + + uart_set_baudrate(uart, baudrate); + + UART[uart_nr] = uart; + + return uart; +} + +void +uart_uninit(uart_t* uart) +{ + if(uart == NULL) + return; + + if(uart->rx_enabled) { + free(uart->rx_buffer->buffer); + free(uart->rx_buffer); + } + free(uart); +} + +void +uart_swap(uart_t* uart, int tx_pin) +{ +} + +void +uart_set_tx(uart_t* uart, int tx_pin) +{ +} + +void +uart_set_pins(uart_t* uart, int tx, int rx) +{ +} + +bool +uart_tx_enabled(uart_t* uart) +{ + if(uart == NULL) + return false; + + return uart->tx_enabled; +} + +bool +uart_rx_enabled(uart_t* uart) +{ + if(uart == NULL) + return false; + + return uart->rx_enabled; +} + +bool +uart_has_overrun(uart_t* uart) +{ + if(uart == NULL || !uart->rx_overrun) + return false; + + // clear flag + uart->rx_overrun = false; + return true; +} + +bool +uart_has_rx_error(uart_t* uart) +{ + return false; +} + +static void +uart_ignore_char(char c) +{ + (void) c; +} + +static void +uart0_write_char(char c) +{ + uart_do_write_char(UART0, c); +} + +static void +uart1_write_char(char c) +{ + uart_do_write_char(UART1, c); +} + +void +uart_set_debug(int uart_nr) +{ + s_uart_debug_nr = uart_nr; + void (*func)(char) = NULL; + switch(s_uart_debug_nr) + { + case UART0: + func = &uart0_write_char; + break; + case UART1: + func = &uart1_write_char; + break; + case UART_NO: + default: + func = &uart_ignore_char; + break; + } +} + +int +uart_get_debug() +{ + return s_uart_debug_nr; +} + +void +uart_start_detect_baudrate(int uart_nr) +{ +} + +int +uart_detect_baudrate(int uart_nr) +{ + return 115200; +} + +}; diff --git a/tests/host/common/mock.h b/tests/host/common/mock.h index 23b5a675d7..6cfafc5ff7 100644 --- a/tests/host/common/mock.h +++ b/tests/host/common/mock.h @@ -94,6 +94,15 @@ extern uint32_t global_ipv4_netfmt; // selected interface addresse to bind to #define CCBUFSIZE 65536 #endif +// uart +#ifdef __cplusplus +extern "C" { +#endif +void uart_new_data(const int uart_nr, uint8_t data); +#ifdef __cplusplus +} +#endif + // tcp int mockSockSetup (int sock); int mockConnect (uint32_t addr, int& sock, int port); From 7efe49e67428e28f9c70d94466848ce2f541e569 Mon Sep 17 00:00:00 2001 From: Clemens Kirchgatterer Date: Tue, 19 Feb 2019 17:03:25 +0100 Subject: [PATCH 2/8] Fixed unused variable. --- tests/host/common/MockUART.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/host/common/MockUART.cpp b/tests/host/common/MockUART.cpp index 5562e17825..bd275eb787 100644 --- a/tests/host/common/MockUART.cpp +++ b/tests/host/common/MockUART.cpp @@ -428,6 +428,10 @@ uart1_write_char(char c) void uart_set_debug(int uart_nr) { + (void)uart_nr; +/* + TODO after there are debug log functions in the host emu + s_uart_debug_nr = uart_nr; void (*func)(char) = NULL; switch(s_uart_debug_nr) @@ -443,6 +447,14 @@ uart_set_debug(int uart_nr) func = &uart_ignore_char; break; } + + if (uart_nr == UART0 || uart_nr == UART1) { + system_set_os_print(1); + } else { + system_set_os_print(0); + } + ets_install_putc1((void *) func); +*/ } int From 136179b65338cc11a7d09107030276c6baac00e8 Mon Sep 17 00:00:00 2001 From: Clemens Kirchgatterer Date: Tue, 19 Feb 2019 17:43:36 +0100 Subject: [PATCH 3/8] Remove unused functions, as long as there are no debug macros using them. --- tests/host/common/MockUART.cpp | 44 ---------------------------------- 1 file changed, 44 deletions(-) diff --git a/tests/host/common/MockUART.cpp b/tests/host/common/MockUART.cpp index bd275eb787..0e6ec76153 100644 --- a/tests/host/common/MockUART.cpp +++ b/tests/host/common/MockUART.cpp @@ -407,54 +407,10 @@ uart_has_rx_error(uart_t* uart) return false; } -static void -uart_ignore_char(char c) -{ - (void) c; -} - -static void -uart0_write_char(char c) -{ - uart_do_write_char(UART0, c); -} - -static void -uart1_write_char(char c) -{ - uart_do_write_char(UART1, c); -} - void uart_set_debug(int uart_nr) { (void)uart_nr; -/* - TODO after there are debug log functions in the host emu - - s_uart_debug_nr = uart_nr; - void (*func)(char) = NULL; - switch(s_uart_debug_nr) - { - case UART0: - func = &uart0_write_char; - break; - case UART1: - func = &uart1_write_char; - break; - case UART_NO: - default: - func = &uart_ignore_char; - break; - } - - if (uart_nr == UART0 || uart_nr == UART1) { - system_set_os_print(1); - } else { - system_set_os_print(0); - } - ets_install_putc1((void *) func); -*/ } int From e671ec7d5c1f045813442f722cb78523e77b25ba Mon Sep 17 00:00:00 2001 From: Clemens Kirchgatterer Date: Tue, 19 Feb 2019 20:06:11 +0100 Subject: [PATCH 4/8] Move user_interface.cc from MOCK_CPP_FILES_EMU to MOCK_CPP_FILES. --- tests/host/Makefile | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/host/Makefile b/tests/host/Makefile index 0205368f73..eafe7c9a6e 100644 --- a/tests/host/Makefile +++ b/tests/host/Makefile @@ -82,13 +82,13 @@ MOCK_CPP_FILES_COMMON := $(addprefix common/,\ MOCK_CPP_FILES := $(MOCK_CPP_FILES_COMMON) $(addprefix common/,\ ArduinoCatch.cpp \ + user_interface.cpp \ ) MOCK_CPP_FILES_EMU := $(MOCK_CPP_FILES_COMMON) $(addprefix common/,\ ArduinoMain.cpp \ ArduinoMainUdp.cpp \ ArduinoMainSpiffs.cpp \ - user_interface.cpp \ ) MOCK_C_FILES := $(addprefix common/,\ @@ -96,7 +96,6 @@ MOCK_C_FILES := $(addprefix common/,\ noniso.c \ ) - INC_PATHS += $(addprefix -I, \ . \ common \ @@ -232,7 +231,7 @@ ARDUINO_LIBS := \ WiFiServerSecureBearSSL.cpp \ BearSSLHelpers.cpp \ CertStoreBearSSL.cpp \ - ) \ + ) OPT_ARDUINO_LIBS ?= $(addprefix ../../libraries/,\ $(addprefix ESP8266WebServer/src/,\ @@ -252,7 +251,7 @@ OPT_ARDUINO_LIBS ?= $(addprefix ../../libraries/,\ DNSServer/src/DNSServer.cpp \ ESP8266AVRISP/src/ESP8266AVRISP.cpp \ ESP8266HTTPClient/src/ESP8266HTTPClient.cpp \ -) \ +) MOCK_ARDUINO_LIBS := $(addprefix common/,\ ClientContextSocket.cpp \ @@ -264,7 +263,7 @@ MOCK_ARDUINO_LIBS := $(addprefix common/,\ MockEsp.cpp \ MockEEPROM.cpp \ MockSPI.cpp \ -) \ +) CPP_SOURCES_CORE_EMU = \ $(MOCK_CPP_FILES_EMU) \ From 5003ad61bb39bded98a07b07f245dfdd87e28514 Mon Sep 17 00:00:00 2001 From: Clemens Kirchgatterer Date: Tue, 19 Feb 2019 20:23:41 +0100 Subject: [PATCH 5/8] Move optimistic_yield() from user_interface.cpp to Arduino.cpp --- tests/host/Makefile | 2 +- tests/host/common/Arduino.cpp | 5 +++++ tests/host/common/user_interface.cpp | 5 ----- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/host/Makefile b/tests/host/Makefile index eafe7c9a6e..ae082baa3d 100644 --- a/tests/host/Makefile +++ b/tests/host/Makefile @@ -82,13 +82,13 @@ MOCK_CPP_FILES_COMMON := $(addprefix common/,\ MOCK_CPP_FILES := $(MOCK_CPP_FILES_COMMON) $(addprefix common/,\ ArduinoCatch.cpp \ - user_interface.cpp \ ) MOCK_CPP_FILES_EMU := $(MOCK_CPP_FILES_COMMON) $(addprefix common/,\ ArduinoMain.cpp \ ArduinoMainUdp.cpp \ ArduinoMainSpiffs.cpp \ + user_interface.cpp \ ) MOCK_C_FILES := $(addprefix common/,\ diff --git a/tests/host/common/Arduino.cpp b/tests/host/common/Arduino.cpp index 5f173646a2..830ab3480a 100644 --- a/tests/host/common/Arduino.cpp +++ b/tests/host/common/Arduino.cpp @@ -37,6 +37,11 @@ extern "C" void yield() { } +extern "C" void optimistic_yield (uint32_t interval_us) +{ + usleep(interval_us); +} + extern "C" void esp_yield() { } diff --git a/tests/host/common/user_interface.cpp b/tests/host/common/user_interface.cpp index c15e3759b9..011269888b 100644 --- a/tests/host/common/user_interface.cpp +++ b/tests/host/common/user_interface.cpp @@ -424,11 +424,6 @@ void esp_schedule (void) { } -void optimistic_yield (uint32_t interval_us) -{ - usleep(interval_us); -} - void dns_setserver (u8_t numdns, ip_addr_t *dnsserver) { (void)numdns; From 329f3090f3ec457d93179412041d3c7b74e31a34 Mon Sep 17 00:00:00 2001 From: Clemens Kirchgatterer Date: Thu, 21 Feb 2019 18:08:06 +0100 Subject: [PATCH 6/8] Remove atexit() weak declaration (fixes segfault when calling atexit). --- tests/host/common/Arduino.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/host/common/Arduino.h b/tests/host/common/Arduino.h index a1aba082c8..9b75fc114c 100644 --- a/tests/host/common/Arduino.h +++ b/tests/host/common/Arduino.h @@ -191,8 +191,6 @@ extern "C" { void init(void); void initVariant(void); - int atexit(void (*func)()) __attribute__((weak)); - void pinMode(uint8_t pin, uint8_t mode); void digitalWrite(uint8_t pin, uint8_t val); int digitalRead(uint8_t pin); From 682ae9a9a40d207b86b74a45c11b3270606668b0 Mon Sep 17 00:00:00 2001 From: Clemens Kirchgatterer Date: Thu, 21 Feb 2019 18:13:55 +0100 Subject: [PATCH 7/8] Improve resetting of terminal after program exit, convert \r to \r\n. --- tests/host/common/ArduinoMain.cpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/tests/host/common/ArduinoMain.cpp b/tests/host/common/ArduinoMain.cpp index 202a5a8c50..95c4e0b0c2 100644 --- a/tests/host/common/ArduinoMain.cpp +++ b/tests/host/common/ArduinoMain.cpp @@ -44,7 +44,7 @@ const char* host_interface = nullptr; size_t spiffs_kb = 1024; bool ignore_sigint = false; -#define STDIN 0 +#define STDIN STDIN_FILENO static struct termios initial_settings; @@ -52,22 +52,24 @@ static int mock_start_uart(void) { struct termios settings; - if (tcgetattr(STDIN, &initial_settings) < 0) return (-1); + if (!isatty(STDIN)) return 0; + if (tcgetattr(STDIN, &initial_settings) < 0) return -1; settings = initial_settings; - if (ignore_sigint) settings.c_lflag &= ~(ISIG); - settings.c_oflag = 0; - settings.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN); - settings.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | - PARMRK | INPCK | ISTRIP | IXON ); - settings.c_cc[VMIN] = 0; + settings.c_lflag &= ~(ignore_sigint ? ISIG : 0); + settings.c_lflag &= ~(ECHO | ICANON); + settings.c_iflag &= ~(ICRNL | INLCR | ISTRIP | IXON ); + settings.c_oflag |= (ONLCR); + settings.c_cc[VMIN] = 0; settings.c_cc[VTIME] = 0; - if (tcsetattr(STDIN, TCSANOW, &settings) < 0) return (-2); - return (0); + if (tcsetattr(STDIN, TCSANOW, &settings) < 0) return -2; + return 0; } static int mock_stop_uart(void) { - if (tcsetattr(STDIN, TCSANOW, &initial_settings) < 0) return (-1); + if (!isatty(STDIN)) return 0; + if (tcsetattr(STDIN, TCSANOW, &initial_settings) < 0) return -1; + printf("\e[?25h"); // show cursor return (0); } @@ -88,7 +90,7 @@ void help (const char* argv0, int exitcode) " -c - ignore CTRL-C (send it via Serial)\n" " -f - no throttle (possibly 100%%CPU)\n" " -S - spiffs size in KBytes (default: %zd)\n" - " (negative value will force mismatched size)\n" + " (negative value will force mismatched size)\n" , argv0, spiffs_kb); exit(exitcode); } @@ -174,6 +176,9 @@ int main (int argc, char* const argv []) // set stdin to non blocking mode mock_start_uart(); + // install exit handler in case Esp.restart() is called + atexit(cleanup); + setup(); while (!user_exit) { From 538ee51768b9e028bcf3d14e8acb1da4d348838b Mon Sep 17 00:00:00 2001 From: Clemens Kirchgatterer Date: Fri, 22 Feb 2019 15:21:20 +0100 Subject: [PATCH 8/8] Show error, when STDOUT is not a TTY, minor code cleanung. --- tests/host/common/ArduinoMain.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/host/common/ArduinoMain.cpp b/tests/host/common/ArduinoMain.cpp index 95c4e0b0c2..c7b71aac01 100644 --- a/tests/host/common/ArduinoMain.cpp +++ b/tests/host/common/ArduinoMain.cpp @@ -43,6 +43,7 @@ bool user_exit = false; const char* host_interface = nullptr; size_t spiffs_kb = 1024; bool ignore_sigint = false; +bool restore_tty = false; #define STDIN STDIN_FILENO @@ -57,17 +58,23 @@ static int mock_start_uart(void) settings = initial_settings; settings.c_lflag &= ~(ignore_sigint ? ISIG : 0); settings.c_lflag &= ~(ECHO | ICANON); - settings.c_iflag &= ~(ICRNL | INLCR | ISTRIP | IXON ); + settings.c_iflag &= ~(ICRNL | INLCR | ISTRIP | IXON); settings.c_oflag |= (ONLCR); settings.c_cc[VMIN] = 0; settings.c_cc[VTIME] = 0; if (tcsetattr(STDIN, TCSANOW, &settings) < 0) return -2; + tty_restore = true; return 0; } static int mock_stop_uart(void) { - if (!isatty(STDIN)) return 0; + if (!restore_tty) return 0; + if (!isatty(STDIN)) { + perror("isatty(STDIN)"); + //system("stty sane"); <- same error message "Inappropriate ioctl for device" + return 0; + } if (tcsetattr(STDIN, TCSANOW, &initial_settings) < 0) return -1; printf("\e[?25h"); // show cursor return (0);