Files
firmware/src/SerialConsole.cpp

132 lines
4.1 KiB
C++
Raw Normal View History

#include "SerialConsole.h"
#include "Default.h"
2021-03-10 15:21:30 +08:00
#include "NodeDB.h"
#include "PowerFSM.h"
#include "Throttle.h"
2022-05-07 20:31:21 +10:00
#include "configuration.h"
#include "time.h"
#if defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT
#define IS_USB_SERIAL
#ifdef SERIAL_HAS_ON_RECEIVE
#undef SERIAL_HAS_ON_RECEIVE
#endif
#include "HWCDC.h"
#endif
#ifdef RP2040_SLOW_CLOCK
#define Port Serial2
#else
2024-06-20 16:26:04 +02:00
#ifdef USER_DEBUG_PORT // change by WayenWeng
#define Port USER_DEBUG_PORT
#else
#define Port Serial
#endif
2024-06-20 16:26:04 +02:00
#endif
// Defaulting to the formerly removed phone_timeout_secs value of 15 minutes
#define SERIAL_CONNECTION_TIMEOUT (15 * 60) * 1000UL
SerialConsole *console;
2026-01-03 21:19:24 +01:00
void consoleInit() {
auto sc = new SerialConsole(); // Must be dynamically allocated because we are now inheriting from thread
#if defined(SERIAL_HAS_ON_RECEIVE)
2026-01-03 21:19:24 +01:00
// onReceive does only exist for HardwareSerial not for USB CDC serial
Port.onReceive([sc]() { sc->rxInt(); });
#endif
2026-01-03 21:19:24 +01:00
DEBUG_PORT.rpInit(); // Simply sets up semaphore
}
2026-01-03 21:19:24 +01:00
void consolePrintf(const char *format, ...) {
va_list arg;
va_start(arg, format);
console->vprintf(nullptr, format, arg);
va_end(arg);
console->flush();
2021-03-10 15:21:30 +08:00
}
2026-01-03 21:19:24 +01:00
SerialConsole::SerialConsole() : StreamAPI(&Port), RedirectablePrint(&Port), concurrency::OSThread("SerialConsole") {
api_type = TYPE_SERIAL;
assert(!console);
console = this;
canWrite = false; // We don't send packets to our port until it has talked to us first
#ifdef RP2040_SLOW_CLOCK
2026-01-03 21:19:24 +01:00
Port.setTX(SERIAL2_TX);
Port.setRX(SERIAL2_RX);
#endif
2026-01-03 21:19:24 +01:00
Port.begin(SERIAL_BAUD);
#if defined(ARCH_NRF52) || defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3) || defined(ARCH_RP2040) || \
defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32C6)
2026-01-03 21:19:24 +01:00
time_t timeout = millis();
while (!Port) {
if (Throttle::isWithinTimespanMs(timeout, FIVE_SECONDS_MS)) {
delay(100);
} else {
break;
}
2026-01-03 21:19:24 +01:00
}
#endif
2024-05-06 20:56:59 -05:00
#if !ARCH_PORTDUINO
2026-01-03 21:19:24 +01:00
emitRebooted();
2024-05-06 20:56:59 -05:00
#endif
}
2026-01-03 21:19:24 +01:00
int32_t SerialConsole::runOnce() {
#ifdef HELTEC_MESH_SOLAR
2026-01-03 21:19:24 +01:00
// After enabling the mesh solar serial port module configuration, command processing is handled by the serial port
// module.
if (moduleConfig.serial.enabled && moduleConfig.serial.override_console_serial_port &&
moduleConfig.serial.mode == meshtastic_ModuleConfig_SerialConfig_Serial_Mode_MS_CONFIG) {
return 250;
}
#endif
2026-01-03 21:19:24 +01:00
int32_t delay = runOncePart();
#if defined(SERIAL_HAS_ON_RECEIVE) || defined(CONFIG_IDF_TARGET_ESP32S2)
2026-01-03 21:19:24 +01:00
return Port.available() ? delay : INT32_MAX;
#elif defined(IS_USB_SERIAL)
2026-01-03 21:19:24 +01:00
return HWCDC::isPlugged() ? delay : (1000 * 20);
#else
2026-01-03 21:19:24 +01:00
return delay;
#endif
}
2026-01-03 21:19:24 +01:00
void SerialConsole::flush() { Port.flush(); }
// trigger tx of serial data
2026-01-03 21:19:24 +01:00
void SerialConsole::onNowHasData(uint32_t fromRadioNum) { setIntervalFromNow(0); }
// trigger rx of serial data
2026-01-03 21:19:24 +01:00
void SerialConsole::rxInt() { setIntervalFromNow(0); }
2026-01-03 21:19:24 +01:00
// For the serial port we can't really detect if any client is on the other side, so instead just look for recent
// messages
bool SerialConsole::checkIsConnected() { return Throttle::isWithinTimespanMs(lastContactMsec, SERIAL_CONNECTION_TIMEOUT); }
/**
2020-05-02 19:51:25 -07:00
* we override this to notice when we've received a protobuf over the serial
* stream. Then we shut off debug serial output.
*/
2026-01-03 21:19:24 +01:00
bool SerialConsole::handleToRadio(const uint8_t *buf, size_t len) {
// only talk to the API once the configuration has been loaded and we're sure the serial port is not disabled.
if (config.has_lora && config.security.serial_enabled) {
// Switch to protobufs for log messages
usingProtobufs = true;
canWrite = true;
return StreamAPI::handleToRadio(buf, len);
} else {
return false;
}
}
2026-01-03 21:19:24 +01:00
void SerialConsole::log_to_serial(const char *logLevel, const char *format, va_list arg) {
if (usingProtobufs && config.security.debug_log_api_enabled) {
meshtastic_LogRecord_Level ll = RedirectablePrint::getLogLevel(logLevel);
auto thread = concurrency::OSThread::currentThread;
emitLogRecord(ll, thread ? thread->ThreadName.c_str() : "", format, arg);
} else
RedirectablePrint::log_to_serial(logLevel, format, arg);
2024-09-23 15:53:42 -05:00
}