2020-04-27 09:36:39 -07:00
|
|
|
#include "SerialConsole.h"
|
2021-03-10 15:21:30 +08:00
|
|
|
#include "NodeDB.h"
|
2020-06-08 16:35:26 -07:00
|
|
|
#include "PowerFSM.h"
|
2020-04-27 09:36:39 -07:00
|
|
|
#include "configuration.h"
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
|
|
|
|
|
#define Port Serial
|
|
|
|
|
|
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
|
|
|
|
|
}
|
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);
|
2021-03-25 06:15:15 +08:00
|
|
|
console->vprintf(format, arg);
|
2021-03-10 15:21:30 +08:00
|
|
|
va_end(arg);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-27 09:36:39 -07:00
|
|
|
SerialConsole::SerialConsole() : StreamAPI(&Port), RedirectablePrint(&Port)
|
|
|
|
|
{
|
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
|
2021-03-25 06:15:15 +08:00
|
|
|
// setDestination(&noopPrint); for testing, try turning off 'all' debug output and see what leaks
|
2020-04-27 09:36:39 -07:00
|
|
|
|
|
|
|
|
Port.begin(SERIAL_BAUD);
|
2020-04-28 17:43:16 -07:00
|
|
|
emitRebooted();
|
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
|
|
|
|
|
* stream. Then we shunt 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
|
|
|
{
|
2020-06-09 10:35:06 -07:00
|
|
|
// Turn off debug serial printing once the API is activated, because other threads could print and corrupt packets
|
2021-03-10 15:21:30 +08:00
|
|
|
if (!radioConfig.preferences.debug_log_enabled)
|
2020-12-09 11:56:41 +08:00
|
|
|
setDestination(&noopPrint);
|
2020-04-27 09:36:39 -07:00
|
|
|
canWrite = true;
|
|
|
|
|
|
2021-03-25 06:15:15 +08:00
|
|
|
return StreamAPI::handleToRadio(buf, len);
|
2020-06-08 16:06:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Hookable to find out when connection changes
|
|
|
|
|
void SerialConsole::onConnectionChanged(bool connected)
|
|
|
|
|
{
|
2020-06-08 16:35:26 -07:00
|
|
|
if (connected) { // To prevent user confusion, turn off bluetooth while using the serial port api
|
|
|
|
|
powerFSM.trigger(EVENT_SERIAL_CONNECTED);
|
|
|
|
|
} else {
|
2020-09-21 12:41:39 -07:00
|
|
|
// FIXME, we get no notice of serial going away, we should instead automatically generate this event if we haven't
|
|
|
|
|
// received a packet in a while
|
2020-06-08 16:35:26 -07:00
|
|
|
powerFSM.trigger(EVENT_SERIAL_DISCONNECTED);
|
|
|
|
|
}
|
2020-04-27 09:36:39 -07:00
|
|
|
}
|