2020-04-27 16:36:39 +00:00
|
|
|
#include "SerialConsole.h"
|
2021-03-10 07:21:30 +00:00
|
|
|
#include "NodeDB.h"
|
2020-06-08 23:35:26 +00:00
|
|
|
#include "PowerFSM.h"
|
2022-05-07 10:31:21 +00:00
|
|
|
#include "configuration.h"
|
2024-05-03 20:49:22 +00:00
|
|
|
#include "time.h"
|
2020-04-27 16:36:39 +00:00
|
|
|
|
2024-03-14 15:29:28 +00:00
|
|
|
#ifdef RP2040_SLOW_CLOCK
|
|
|
|
#define Port Serial2
|
|
|
|
#else
|
2024-06-20 14:26:04 +00:00
|
|
|
#ifdef USER_DEBUG_PORT // change by WayenWeng
|
|
|
|
#define Port USER_DEBUG_PORT
|
|
|
|
#else
|
2020-04-27 16:36:39 +00:00
|
|
|
#define Port Serial
|
2024-03-14 15:29:28 +00:00
|
|
|
#endif
|
2024-06-20 14:26:04 +00:00
|
|
|
#endif
|
2022-06-12 14:44:23 +00:00
|
|
|
// Defaulting to the formerly removed phone_timeout_secs value of 15 minutes
|
|
|
|
#define SERIAL_CONNECTION_TIMEOUT (15 * 60) * 1000UL
|
2020-04-27 16:36:39 +00:00
|
|
|
|
2021-03-24 22:15:15 +00:00
|
|
|
SerialConsole *console;
|
|
|
|
|
|
|
|
void consoleInit()
|
|
|
|
{
|
|
|
|
new SerialConsole(); // Must be dynamically allocated because we are now inheriting from thread
|
2023-09-01 01:40:01 +00:00
|
|
|
DEBUG_PORT.rpInit(); // Simply sets up semaphore
|
2021-03-24 22:15:15 +00:00
|
|
|
}
|
2020-04-27 16:36:39 +00:00
|
|
|
|
2021-03-10 07:21:30 +00:00
|
|
|
void consolePrintf(const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list arg;
|
|
|
|
va_start(arg, format);
|
2024-06-29 04:30:39 +00:00
|
|
|
console->vprintf(nullptr, format, arg);
|
2021-03-10 07:21:30 +00:00
|
|
|
va_end(arg);
|
2022-09-21 13:34:48 +00:00
|
|
|
console->flush();
|
2021-03-10 07:21:30 +00:00
|
|
|
}
|
|
|
|
|
2022-12-22 17:24:42 +00:00
|
|
|
SerialConsole::SerialConsole() : StreamAPI(&Port), RedirectablePrint(&Port), concurrency::OSThread("SerialConsole")
|
2020-04-27 16:36:39 +00:00
|
|
|
{
|
2021-03-24 22:15:15 +00:00
|
|
|
assert(!console);
|
|
|
|
console = this;
|
2020-04-27 16:36:39 +00:00
|
|
|
canWrite = false; // We don't send packets to our port until it has talked to us first
|
|
|
|
|
2024-03-14 15:29:28 +00:00
|
|
|
#ifdef RP2040_SLOW_CLOCK
|
|
|
|
Port.setTX(SERIAL2_TX);
|
|
|
|
Port.setRX(SERIAL2_RX);
|
|
|
|
#endif
|
2020-04-27 16:36:39 +00:00
|
|
|
Port.begin(SERIAL_BAUD);
|
2023-05-23 21:19:36 +00:00
|
|
|
#if defined(ARCH_NRF52) || defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3) || defined(ARCH_RP2040)
|
2022-03-25 22:06:31 +00:00
|
|
|
time_t timeout = millis();
|
|
|
|
while (!Port) {
|
2022-03-26 10:05:36 +00:00
|
|
|
if ((millis() - timeout) < 5000) {
|
2022-03-25 22:06:31 +00:00
|
|
|
delay(100);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2024-05-07 01:56:59 +00:00
|
|
|
#if !ARCH_PORTDUINO
|
2020-04-29 00:43:16 +00:00
|
|
|
emitRebooted();
|
2024-05-07 01:56:59 +00:00
|
|
|
#endif
|
2020-04-27 16:36:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-22 17:24:42 +00:00
|
|
|
int32_t SerialConsole::runOnce()
|
|
|
|
{
|
|
|
|
return runOncePart();
|
|
|
|
}
|
2022-06-12 14:44:23 +00:00
|
|
|
|
2023-01-21 13:34:29 +00:00
|
|
|
void SerialConsole::flush()
|
|
|
|
{
|
2023-01-04 09:37:27 +00:00
|
|
|
Port.flush();
|
|
|
|
}
|
|
|
|
|
2021-05-03 06:46:30 +00: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()
|
|
|
|
{
|
|
|
|
uint32_t now = millis();
|
2022-06-12 14:44:23 +00:00
|
|
|
return (now - lastContactMsec) < SERIAL_CONNECTION_TIMEOUT;
|
2021-05-03 06:46:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 16:36:39 +00:00
|
|
|
/**
|
2020-05-03 02:51:25 +00:00
|
|
|
* we override this to notice when we've received a protobuf over the serial
|
2024-03-15 23:40:48 +00:00
|
|
|
* stream. Then we shut off debug serial output.
|
2020-04-27 16:36:39 +00:00
|
|
|
*/
|
2021-03-24 22:15:15 +00:00
|
|
|
bool SerialConsole::handleToRadio(const uint8_t *buf, size_t len)
|
2020-04-27 16:36:39 +00:00
|
|
|
{
|
2022-10-05 08:33:39 +00:00
|
|
|
// 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.device.serial_enabled) {
|
2024-06-30 23:41:27 +00:00
|
|
|
// Switch to protobufs for log messages
|
|
|
|
usingProtobufs = true;
|
2022-10-05 08:33:39 +00:00
|
|
|
canWrite = true;
|
2020-04-27 16:36:39 +00:00
|
|
|
|
2022-10-05 08:33:39 +00:00
|
|
|
return StreamAPI::handleToRadio(buf, len);
|
2023-01-21 13:34:29 +00:00
|
|
|
} else {
|
2022-10-05 08:33:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
2024-06-30 23:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SerialConsole::log_to_serial(const char *logLevel, const char *format, va_list arg)
|
|
|
|
{
|
2024-07-30 20:05:33 +00:00
|
|
|
if (usingProtobufs && config.device.debug_log_enabled) {
|
2024-06-30 23:41:27 +00:00
|
|
|
meshtastic_LogRecord_Level ll = meshtastic_LogRecord_Level_UNSET; // default to unset
|
|
|
|
switch (logLevel[0]) {
|
|
|
|
case 'D':
|
|
|
|
ll = meshtastic_LogRecord_Level_DEBUG;
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
ll = meshtastic_LogRecord_Level_INFO;
|
|
|
|
break;
|
|
|
|
case 'W':
|
|
|
|
ll = meshtastic_LogRecord_Level_WARNING;
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
ll = meshtastic_LogRecord_Level_ERROR;
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
ll = meshtastic_LogRecord_Level_CRITICAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto thread = concurrency::OSThread::currentThread;
|
|
|
|
emitLogRecord(ll, thread ? thread->ThreadName.c_str() : "", format, arg);
|
|
|
|
} else
|
|
|
|
RedirectablePrint::log_to_serial(logLevel, format, arg);
|
2024-07-30 20:05:33 +00:00
|
|
|
}
|