From 54fb250ab278324d60f404b23d87867bbd3e54d7 Mon Sep 17 00:00:00 2001 From: Marcelo Aquino Date: Tue, 13 Feb 2018 10:22:27 -0200 Subject: [PATCH] Linux: Serial minor update (#1053) - The symlink name for the PTY device is set using --my-serial-port making --my-serial-pty deprecated - Replace MY_LINUX_IS_SERIAL_PTY with MY_LINUX_SERIAL_IS_PTY - MY_DEBUGDEVICE can be used to print debug messages from MySensors (as it is currently in the other architectures) - Rename SerialSimulator class to StdInOutStream - Remove old deprecated option --my-radio --- MyConfig.h | 35 +++++++++++++------ configure | 14 ++++---- ...SerialSimulator.cpp => StdInOutStream.cpp} | 16 ++++----- .../{SerialSimulator.h => StdInOutStream.h} | 8 ++--- examples_linux/mysgw.cpp | 7 ++-- hal/architecture/Linux/MyHwLinuxGeneric.cpp | 29 +++++++++++++++ hal/architecture/Linux/MyHwLinuxGeneric.h | 12 +++---- 7 files changed, 80 insertions(+), 41 deletions(-) rename drivers/Linux/{SerialSimulator.cpp => StdInOutStream.cpp} (80%) rename drivers/Linux/{SerialSimulator.h => StdInOutStream.h} (91%) diff --git a/MyConfig.h b/MyConfig.h index 865f44dc..f3d2a593 100644 --- a/MyConfig.h +++ b/MyConfig.h @@ -1805,29 +1805,39 @@ * @brief These options control Linux specific configurations. * @{ */ + /** * @def MY_LINUX_SERIAL_PORT * @brief Serial device port */ -#ifndef MY_LINUX_SERIAL_PORT -#define MY_LINUX_SERIAL_PORT "/dev/ttyACM0" +//#define MY_LINUX_SERIAL_PORT "/dev/ttyUSB0" + +/** + * @def MY_LINUX_SERIAL_PTY + * @brief deprecated option + */ +#ifdef MY_LINUX_SERIAL_PTY +#warning MY_LINUX_SERIAL_PTY is deprecated, please use MY_LINUX_SERIAL_PORT +#define MY_LINUX_SERIAL_PORT MY_LINUX_SERIAL_PTY #endif /** * @def MY_LINUX_IS_SERIAL_PTY + * @brief deprecated option + */ +#ifdef MY_LINUX_IS_SERIAL_PTY +#warning MY_LINUX_IS_SERIAL_PTY is deprecated, please use MY_LINUX_SERIAL_IS_PTY +#define MY_LINUX_SERIAL_IS_PTY +#endif + +/** + * @def MY_LINUX_SERIAL_IS_PTY * @brief Set serial as a pseudo terminal. * * Enable this if you need to connect to a controller running on the same device. + * You also need to define MY_LINUX_SERIAL_PORT with the symlink name for the PTY device. */ -//#define MY_LINUX_IS_SERIAL_PTY - -/** - * @def MY_LINUX_SERIAL_PTY - * @brief Symlink name for the PTY device. - */ -#ifndef MY_LINUX_SERIAL_PTY -#define MY_LINUX_SERIAL_PTY "/dev/ttyMySensorsGateway" -#endif +//#define MY_LINUX_SERIAL_IS_PTY /** * @def MY_LINUX_SERIAL_GROUPNAME @@ -1983,7 +1993,10 @@ #define MY_DISABLED_SERIAL #define MY_SPLASH_SCREEN_DISABLED // linux +#define MY_LINUX_SERIAL_PORT +#define MY_LINUX_SERIAL_IS_PTY #define MY_LINUX_SERIAL_GROUPNAME +#define MY_LINUX_SERIAL_PTY #define MY_LINUX_IS_SERIAL_PTY // inclusion mode #define MY_INCLUSION_MODE_FEATURE diff --git a/configure b/configure index 81c6efdd..77df1433 100755 --- a/configure +++ b/configure @@ -47,11 +47,12 @@ MySensors options: Controller or MQTT broker ip. --my-port= The port to keep open on gateway mode. If gateway is set to mqtt, it sets the broker port. - --my-serial-port= Serial port. [/dev/ttyACM0] + --my-serial-port= Serial port. --my-serial-baudrate= Serial baud rate. [115200] --my-serial-is-pty Set the serial port to be a pseudo terminal. Use this if you want to connect to a controller running on the same device. - --my-serial-pty= Symlink name for the PTY device. [/dev/ttyMySensorsGateway] + You also need to set the symlink name for the PTY device with + the --my-serial-port option. --my-serial-groupname= Grant access to the specified system group for the serial device. --my-mqtt-client-id= MQTT client id. @@ -348,10 +349,6 @@ for opt do --my-transport=*) transport_type=${optarg} ;; - --my-radio=*) - echo "Warning: --my-radio is deprecated, please use --my-transport" - transport_type=${optarg} - ;; --my-serial-port=*) CPPFLAGS="-DMY_LINUX_SERIAL_PORT=\\\"${optarg}\\\" $CPPFLAGS" ;; @@ -359,10 +356,11 @@ for opt do CPPFLAGS="-DMY_BAUD_RATE=${optarg} $CPPFLAGS" ;; --my-serial-is-pty*) - CPPFLAGS="-DMY_LINUX_IS_SERIAL_PTY $CPPFLAGS" + CPPFLAGS="-DMY_LINUX_SERIAL_IS_PTY $CPPFLAGS" ;; --my-serial-pty=*) - CPPFLAGS="-DMY_LINUX_SERIAL_PTY=\\\"${optarg}\\\" $CPPFLAGS" + echo "Warning: --my-serial-pty is deprecated, please use --my-serial-port" + CPPFLAGS="-DMY_LINUX_SERIAL_PORT=\\\"${optarg}\\\" $CPPFLAGS" ;; --my-serial-groupname=*) CPPFLAGS="-DMY_LINUX_SERIAL_GROUPNAME=\\\"${optarg}\\\" $CPPFLAGS" diff --git a/drivers/Linux/SerialSimulator.cpp b/drivers/Linux/StdInOutStream.cpp similarity index 80% rename from drivers/Linux/SerialSimulator.cpp rename to drivers/Linux/StdInOutStream.cpp index b14cb657..d9867d15 100644 --- a/drivers/Linux/SerialSimulator.cpp +++ b/drivers/Linux/StdInOutStream.cpp @@ -18,39 +18,39 @@ */ #include -#include "SerialSimulator.h" +#include "StdInOutStream.h" -void SerialSimulator::begin(int baud) +void StdInOutStream::begin(int baud) { (void)baud; } -int SerialSimulator::available() +int StdInOutStream::available() { return 1; } -int SerialSimulator::read() +int StdInOutStream::read() { return getchar(); } -size_t SerialSimulator::write(uint8_t b) +size_t StdInOutStream::write(uint8_t b) { return (size_t)::printf("%c", b); } -int SerialSimulator::peek() +int StdInOutStream::peek() { return -1; } -void SerialSimulator::flush() +void StdInOutStream::flush() { fflush(stdout); } -void SerialSimulator::end() +void StdInOutStream::end() { flush(); } diff --git a/drivers/Linux/SerialSimulator.h b/drivers/Linux/StdInOutStream.h similarity index 91% rename from drivers/Linux/SerialSimulator.h rename to drivers/Linux/StdInOutStream.h index 2007d218..2f0356ee 100644 --- a/drivers/Linux/SerialSimulator.h +++ b/drivers/Linux/StdInOutStream.h @@ -17,8 +17,8 @@ * version 2 as published by the Free Software Foundation. */ -#ifndef SerialSimulator_h -#define SerialSimulator_h +#ifndef StdInOutStream_h +#define StdInOutStream_h #include #include @@ -26,9 +26,9 @@ #include "Stream.h" /** - * @brief A class equivalent to Serial in Arduino but outputs to stdout + * @brief A class that prints to stdout and reads from stdin */ -class SerialSimulator : public Stream +class StdInOutStream : public Stream { public: diff --git a/examples_linux/mysgw.cpp b/examples_linux/mysgw.cpp index da5ba055..28024f15 100644 --- a/examples_linux/mysgw.cpp +++ b/examples_linux/mysgw.cpp @@ -31,11 +31,10 @@ // Serial config // Enable this if you are using an Arduino connected to the USB -//#define MY_LINUX_SERIAL_PORT "/dev/ttyACM0" +//#define MY_LINUX_SERIAL_PORT "/dev/ttyUSB0" // Enable this if you need to connect to a controller running on the same device -//#define MY_LINUX_IS_SERIAL_PTY -// Choose a symlink name for the PTY device -//#define MY_LINUX_SERIAL_PTY "/dev/ttyMySensorsGateway" +// You also need to define MY_LINUX_SERIAL_PORT above with the symlink name for the PTY device +//#define MY_LINUX_SERIAL_IS_PTY // Grant access to the specified system group for the serial device //#define MY_LINUX_SERIAL_GROUPNAME "tty" diff --git a/hal/architecture/Linux/MyHwLinuxGeneric.cpp b/hal/architecture/Linux/MyHwLinuxGeneric.cpp index 6256e661..18d09060 100644 --- a/hal/architecture/Linux/MyHwLinuxGeneric.cpp +++ b/hal/architecture/Linux/MyHwLinuxGeneric.cpp @@ -165,8 +165,37 @@ void hwPinMode(uint8_t pin, uint8_t mode) void hwDebugPrint(const char *fmt, ...) { +#ifndef MY_DISABLED_SERIAL +#ifdef MY_DEBUGDEVICE + char fmtBuffer[MY_SERIAL_OUTPUT_SIZE]; +#ifdef MY_GATEWAY_SERIAL + // prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE) + snprintf_P(fmtBuffer, sizeof(fmtBuffer), PSTR("0;255;%" PRIu8 ";0;%" PRIu8 ";%" PRIu32 " "), + C_INTERNAL, I_LOG_MESSAGE, hwMillis()); + MY_DEBUGDEVICE.print(fmtBuffer); +#else + // prepend timestamp + MY_DEBUGDEVICE.print(hwMillis()); + MY_DEBUGDEVICE.print(" "); +#endif + va_list args; + va_start (args, fmt ); + vsnprintf_P(fmtBuffer, sizeof(fmtBuffer), fmt, args); +#ifdef MY_GATEWAY_SERIAL + // Truncate message if this is gateway node + fmtBuffer[sizeof(fmtBuffer) - 2] = '\n'; + fmtBuffer[sizeof(fmtBuffer) - 1] = '\0'; +#endif + va_end (args); + MY_DEBUGDEVICE.print(fmtBuffer); + MY_DEBUGDEVICE.flush(); +#else va_list args; va_start(args, fmt); vlogDebug(fmt, args); va_end(args); +#endif +#else + (void)fmt; +#endif } diff --git a/hal/architecture/Linux/MyHwLinuxGeneric.h b/hal/architecture/Linux/MyHwLinuxGeneric.h index a933632c..40d7950b 100644 --- a/hal/architecture/Linux/MyHwLinuxGeneric.h +++ b/hal/architecture/Linux/MyHwLinuxGeneric.h @@ -23,16 +23,16 @@ #include #include #include "SerialPort.h" -#include "SerialSimulator.h" +#include "StdInOutStream.h" -#ifdef MY_GATEWAY_SERIAL -#ifdef MY_LINUX_IS_SERIAL_PTY -SerialPort Serial = SerialPort(MY_LINUX_SERIAL_PTY, true); +#ifdef MY_LINUX_SERIAL_PORT +#ifdef MY_LINUX_SERIAL_IS_PTY +SerialPort Serial = SerialPort(MY_LINUX_SERIAL_PORT, true); #else -SerialPort Serial = SerialPort(MY_LINUX_SERIAL_PORT); +SerialPort Serial = SerialPort(MY_LINUX_SERIAL_PORT, false); #endif #else -SerialSimulator Serial = SerialSimulator(); +StdInOutStream Serial = StdInOutStream(); #endif #ifndef MY_SERIALDEVICE