2020-04-27 09:36:39 -07:00
|
|
|
#include "SerialConsole.h"
|
2024-09-23 08:58:14 -05:00
|
|
|
#include "Default.h"
|
2021-03-10 15:21:30 +08:00
|
|
|
#include "NodeDB.h"
|
2020-06-08 16:35:26 -07:00
|
|
|
#include "PowerFSM.h"
|
2024-09-23 08:58:14 -05:00
|
|
|
#include "Throttle.h"
|
2022-05-07 20:31:21 +10:00
|
|
|
#include "configuration.h"
|
2024-05-03 22:49:22 +02:00
|
|
|
#include "time.h"
|
2020-04-27 09:36:39 -07:00
|
|
|
|
2024-03-14 16:29:28 +01:00
|
|
|
#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
|
2020-04-27 09:36:39 -07:00
|
|
|
#define Port Serial
|
2024-03-14 16:29:28 +01:00
|
|
|
#endif
|
2024-06-20 16:26:04 +02:00
|
|
|
#endif
|
2022-06-12 09:44:23 -05:00
|
|
|
// Defaulting to the formerly removed phone_timeout_secs value of 15 minutes
|
|
|
|
|
#define SERIAL_CONNECTION_TIMEOUT (15 * 60) * 1000UL
|
2020-04-27 09:36:39 -07:00
|
|
|
|
2021-03-25 06:15:15 +08:00
|
|
|
SerialConsole *console;
|
|
|
|
|
|
|
|
|
|
void consoleInit()
|
|
|
|
|
{
|
|
|
|
|
new SerialConsole(); // Must be dynamically allocated because we are now inheriting from thread
|
2023-08-31 20:40:01 -05:00
|
|
|
DEBUG_PORT.rpInit(); // Simply sets up semaphore
|
2021-03-25 06:15:15 +08:00
|
|
|
}
|
2020-04-27 09:36:39 -07:00
|
|
|
|
2021-03-10 15:21:30 +08:00
|
|
|
void consolePrintf(const char *format, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list arg;
|
|
|
|
|
va_start(arg, format);
|
2024-06-28 23:30:39 -05:00
|
|
|
console->vprintf(nullptr, format, arg);
|
2021-03-10 15:21:30 +08:00
|
|
|
va_end(arg);
|
2022-09-21 15:34:48 +02:00
|
|
|
console->flush();
|
2021-03-10 15:21:30 +08:00
|
|
|
}
|
|
|
|
|
|
2022-12-22 18:24:42 +01:00
|
|
|
SerialConsole::SerialConsole() : StreamAPI(&Port), RedirectablePrint(&Port), concurrency::OSThread("SerialConsole")
|
2020-04-27 09:36:39 -07:00
|
|
|
{
|
2021-03-25 06:15:15 +08:00
|
|
|
assert(!console);
|
|
|
|
|
console = this;
|
2020-04-27 09:36:39 -07:00
|
|
|
canWrite = false; // We don't send packets to our port until it has talked to us first
|
|
|
|
|
|
2024-03-14 16:29:28 +01:00
|
|
|
#ifdef RP2040_SLOW_CLOCK
|
|
|
|
|
Port.setTX(SERIAL2_TX);
|
|
|
|
|
Port.setRX(SERIAL2_RX);
|
|
|
|
|
#endif
|
2020-04-27 09:36:39 -07:00
|
|
|
Port.begin(SERIAL_BAUD);
|
2024-09-28 13:42:32 +02:00
|
|
|
#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)
|
2022-03-25 23:06:31 +01:00
|
|
|
time_t timeout = millis();
|
|
|
|
|
while (!Port) {
|
2024-09-23 08:58:14 -05:00
|
|
|
if (Throttle::isWithinTimespanMs(timeout, FIVE_SECONDS_MS)) {
|
2022-03-25 23:06:31 +01:00
|
|
|
delay(100);
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2024-05-06 20:56:59 -05:00
|
|
|
#if !ARCH_PORTDUINO
|
2020-04-28 17:43:16 -07:00
|
|
|
emitRebooted();
|
2024-05-06 20:56:59 -05:00
|
|
|
#endif
|
2020-04-27 09:36:39 -07:00
|
|
|
}
|
|
|
|
|
|
2022-12-22 18:24:42 +01:00
|
|
|
int32_t SerialConsole::runOnce()
|
|
|
|
|
{
|
2025-08-27 06:02:54 -05:00
|
|
|
#ifdef HELTEC_MESH_SOLAR
|
|
|
|
|
//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
|
2022-12-22 18:24:42 +01:00
|
|
|
return runOncePart();
|
|
|
|
|
}
|
2022-06-12 09:44:23 -05:00
|
|
|
|
2023-01-21 14:34:29 +01:00
|
|
|
void SerialConsole::flush()
|
|
|
|
|
{
|
2023-01-04 10:37:27 +01:00
|
|
|
Port.flush();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-03 14:46:30 +08: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()
|
|
|
|
|
{
|
2024-09-23 08:58:14 -05:00
|
|
|
return Throttle::isWithinTimespanMs(lastContactMsec, SERIAL_CONNECTION_TIMEOUT);
|
2021-05-03 14:46:30 +08:00
|
|
|
}
|
|
|
|
|
|
2020-04-27 09:36:39 -07:00
|
|
|
/**
|
2020-05-02 19:51:25 -07:00
|
|
|
* we override this to notice when we've received a protobuf over the serial
|
2024-03-15 18:40:48 -05:00
|
|
|
* stream. Then we shut off debug serial output.
|
2020-04-27 09:36:39 -07:00
|
|
|
*/
|
2021-03-25 06:15:15 +08:00
|
|
|
bool SerialConsole::handleToRadio(const uint8_t *buf, size_t len)
|
2020-04-27 09:36:39 -07:00
|
|
|
{
|
2022-10-05 10:33:39 +02:00
|
|
|
// only talk to the API once the configuration has been loaded and we're sure the serial port is not disabled.
|
2024-08-10 13:45:41 -05:00
|
|
|
if (config.has_lora && config.security.serial_enabled) {
|
2024-06-30 16:41:27 -07:00
|
|
|
// Switch to protobufs for log messages
|
|
|
|
|
usingProtobufs = true;
|
2022-10-05 10:33:39 +02:00
|
|
|
canWrite = true;
|
2020-04-27 09:36:39 -07:00
|
|
|
|
2022-10-05 10:33:39 +02:00
|
|
|
return StreamAPI::handleToRadio(buf, len);
|
2023-01-21 14:34:29 +01:00
|
|
|
} else {
|
2022-10-05 10:33:39 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2024-06-30 16:41:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SerialConsole::log_to_serial(const char *logLevel, const char *format, va_list arg)
|
|
|
|
|
{
|
2024-08-10 13:45:41 -05:00
|
|
|
if (usingProtobufs && config.security.debug_log_api_enabled) {
|
2024-10-26 20:06:50 +10:00
|
|
|
meshtastic_LogRecord_Level ll = RedirectablePrint::getLogLevel(logLevel);
|
2024-06-30 16:41:27 -07:00
|
|
|
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
|
|
|
}
|