firmware/src/SerialConsole.cpp

67 lines
1.8 KiB
C++
Raw Normal View History

#include "SerialConsole.h"
2021-03-10 07:21:30 +00:00
#include "NodeDB.h"
#include "PowerFSM.h"
2022-05-07 10:31:21 +00:00
#include "configuration.h"
#define Port Serial
// Defaulting to the formerly removed phone_timeout_secs value of 15 minutes
#define SERIAL_CONNECTION_TIMEOUT (15 * 60) * 1000UL
SerialConsole *console;
void consoleInit()
{
new SerialConsole(); // Must be dynamically allocated because we are now inheriting from thread
}
2021-03-10 07:21:30 +00:00
void consolePrintf(const char *format, ...)
{
va_list arg;
va_start(arg, format);
console->vprintf(format, arg);
2021-03-10 07:21:30 +00:00
va_end(arg);
}
SerialConsole::SerialConsole() : StreamAPI(&Port), RedirectablePrint(&Port)
{
assert(!console);
console = this;
canWrite = false; // We don't send packets to our port until it has talked to us first
// setDestination(&noopPrint); for testing, try turning off 'all' debug output and see what leaks
Port.begin(SERIAL_BAUD);
#ifdef ARCH_NRF52
time_t timeout = millis();
while (!Port) {
if ((millis() - timeout) < 5000) {
delay(100);
} else {
break;
}
}
#endif
emitRebooted();
}
// 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();
return (now - lastContactMsec) < SERIAL_CONNECTION_TIMEOUT;
}
/**
2020-05-03 02:51:25 +00:00
* we override this to notice when we've received a protobuf over the serial
* stream. Then we shunt off debug serial output.
*/
bool SerialConsole::handleToRadio(const uint8_t *buf, size_t len)
{
// Turn off debug serial printing once the API is activated, because other threads could print and corrupt packets
if (!config.device.debug_log_enabled)
2020-12-09 03:56:41 +00:00
setDestination(&noopPrint);
canWrite = true;
return StreamAPI::handleToRadio(buf, len);
}