Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions examples/simple_repeater/MyMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,6 @@ const char *MyMesh::getLogDateTime() {
return tmp;
}

void MyMesh::logRxRaw(float snr, float rssi, const uint8_t raw[], int len) {
#if MESH_PACKET_LOGGING
Serial.print(getLogDateTime());
Serial.print(" RAW: ");
mesh::Utils::printHex(Serial, raw, len);
Serial.println();
#endif
}

void MyMesh::logRx(mesh::Packet *pkt, int len, float score) {
#ifdef WITH_BRIDGE
Expand Down
2 changes: 0 additions & 2 deletions examples/simple_repeater/MyMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {

bool allowPacketForward(const mesh::Packet* packet) override;
const char* getLogDateTime() override;
void logRxRaw(float snr, float rssi, const uint8_t raw[], int len) override;

void logRx(mesh::Packet* pkt, int len, float score) override;
void logTx(mesh::Packet* pkt, int len) override;
void logTxFail(mesh::Packet* pkt, int len) override;
Expand Down
19 changes: 19 additions & 0 deletions examples/simple_repeater/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
#include <Mesh.h>

#include "MyMesh.h"
#if BLE_PACKET_LOGGING
#if defined(NRF52_PLATFORM) || defined(ESP32)
#include <helpers/BLELogInterface.h>
#else
#error "BLE_PACKET_LOGGING is not supported on this platform (only ESP32 and nRF52)"
#endif
#endif

#if MESH_PACKET_LOGGING && BLE_PACKET_LOGGING && (defined(NRF52_PLATFORM) || defined(ESP32))
static BLELogInterface ble_log;
#endif

#ifdef DISPLAY_CLASS
#include "UITask.h"
Expand Down Expand Up @@ -95,6 +106,11 @@ void setup() {

the_mesh.begin(fs);

#if MESH_PACKET_LOGGING && BLE_PACKET_LOGGING && (defined(NRF52_PLATFORM) || defined(ESP32))
ble_log.begin(the_mesh.getNodeName());
the_mesh.setPacketLogStream(&ble_log);
#endif

#ifdef DISPLAY_CLASS
ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
#endif
Expand Down Expand Up @@ -153,6 +169,9 @@ void loop() {
ui_task.loop();
#endif
rtc_clock.tick();
#if MESH_PACKET_LOGGING && BLE_PACKET_LOGGING && defined(ESP32)
ble_log.loop();
#endif

if (the_mesh.getNodePrefs()->powersaving_enabled && !the_mesh.hasPendingWork()) {
#if defined(NRF52_PLATFORM)
Expand Down
8 changes: 0 additions & 8 deletions examples/simple_room_server/MyMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,6 @@ int MyMesh::handleRequest(ClientInfo *sender, uint32_t sender_timestamp, uint8_t
return 0; // unknown command
}

void MyMesh::logRxRaw(float snr, float rssi, const uint8_t raw[], int len) {
#if MESH_PACKET_LOGGING
Serial.print(getLogDateTime());
Serial.print(" RAW: ");
mesh::Utils::printHex(Serial, raw, len);
Serial.println();
#endif
}

void MyMesh::logRx(mesh::Packet *pkt, int len, float score) {
if (_logging) {
Expand Down
1 change: 0 additions & 1 deletion examples/simple_room_server/MyMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
return _prefs.airtime_factor;
}

void logRxRaw(float snr, float rssi, const uint8_t raw[], int len) override;
void logRx(mesh::Packet* pkt, int len, float score) override;
void logTx(mesh::Packet* pkt, int len) override;
void logTxFail(mesh::Packet* pkt, int len) override;
Expand Down
19 changes: 19 additions & 0 deletions examples/simple_room_server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
#include <Mesh.h>

#include "MyMesh.h"
#if BLE_PACKET_LOGGING
#if defined(NRF52_PLATFORM) || defined(ESP32)
#include <helpers/BLELogInterface.h>
#else
#error "BLE_PACKET_LOGGING is not supported on this platform (only ESP32 and nRF52)"
#endif
#endif

#if MESH_PACKET_LOGGING && BLE_PACKET_LOGGING && (defined(NRF52_PLATFORM) || defined(ESP32))
static BLELogInterface ble_log;
#endif

#ifdef DISPLAY_CLASS
#include "UITask.h"
Expand Down Expand Up @@ -72,6 +83,11 @@ void setup() {

the_mesh.begin(fs);

#if MESH_PACKET_LOGGING && BLE_PACKET_LOGGING && (defined(NRF52_PLATFORM) || defined(ESP32))
ble_log.begin(the_mesh.getNodeName());
the_mesh.setPacketLogStream(&ble_log);
#endif

#ifdef DISPLAY_CLASS
ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
#endif
Expand Down Expand Up @@ -113,4 +129,7 @@ void loop() {
ui_task.loop();
#endif
rtc_clock.tick();
#if MESH_PACKET_LOGGING && BLE_PACKET_LOGGING && defined(ESP32)
ble_log.loop();
#endif
}
24 changes: 10 additions & 14 deletions src/Dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,21 +217,18 @@ void Dispatcher::checkRecv() {
}
if (pkt) {
#if MESH_PACKET_LOGGING
Serial.print(getLogDateTime());
Serial.printf(": RX, len=%d (type=%d, route=%s, payload_len=%d) SNR=%d RSSI=%d score=%d time=%d",
pkt->getRawLength(), pkt->getPayloadType(), pkt->isRouteDirect() ? "D" : "F", pkt->payload_len,
_packet_log.printf("%s: RX, len=%d (type=%d, route=%s, payload_len=%d) SNR=%d RSSI=%d score=%d time=%d",
getLogDateTime(), pkt->getRawLength(), pkt->getPayloadType(), pkt->isRouteDirect() ? "D" : "F", pkt->payload_len,
(int)pkt->getSNR(), (int)_radio->getLastRSSI(), (int)(score*1000), air_time);

static uint8_t packet_hash[MAX_HASH_SIZE];
pkt->calculatePacketHash(packet_hash);
Serial.print(" hash=");
mesh::Utils::printHex(Serial, packet_hash, MAX_HASH_SIZE);

_packet_log.print(" hash=");
mesh::Utils::printHex(_packet_log, packet_hash, MAX_HASH_SIZE);
if (pkt->getPayloadType() == PAYLOAD_TYPE_PATH || pkt->getPayloadType() == PAYLOAD_TYPE_REQ
|| pkt->getPayloadType() == PAYLOAD_TYPE_RESPONSE || pkt->getPayloadType() == PAYLOAD_TYPE_TXT_MSG) {
Serial.printf(" [%02X -> %02X]\n", (uint32_t)pkt->payload[1], (uint32_t)pkt->payload[0]);
_packet_log.printf(" [%02X -> %02X]\n", (uint32_t)pkt->payload[1], (uint32_t)pkt->payload[0]);
} else {
Serial.printf("\n");
_packet_log.print("\n");
}
#endif
logRx(pkt, pkt->getRawLength(), score); // hook for custom logging
Expand Down Expand Up @@ -338,14 +335,13 @@ void Dispatcher::checkSend() {
outbound_expiry = futureMillis(max_airtime);

#if MESH_PACKET_LOGGING
Serial.print(getLogDateTime());
Serial.printf(": TX, len=%d (type=%d, route=%s, payload_len=%d)",
len, outbound->getPayloadType(), outbound->isRouteDirect() ? "D" : "F", outbound->payload_len);
_packet_log.printf("%s: TX, len=%d (type=%d, route=%s, payload_len=%d)",
getLogDateTime(), len, outbound->getPayloadType(), outbound->isRouteDirect() ? "D" : "F", outbound->payload_len);
if (outbound->getPayloadType() == PAYLOAD_TYPE_PATH || outbound->getPayloadType() == PAYLOAD_TYPE_REQ
|| outbound->getPayloadType() == PAYLOAD_TYPE_RESPONSE || outbound->getPayloadType() == PAYLOAD_TYPE_TXT_MSG) {
Serial.printf(" [%02X -> %02X]\n", (uint32_t)outbound->payload[1], (uint32_t)outbound->payload[0]);
_packet_log.printf(" [%02X -> %02X]\n", (uint32_t)outbound->payload[1], (uint32_t)outbound->payload[0]);
} else {
Serial.printf("\n");
_packet_log.print("\n");
}
#endif
}
Expand Down
42 changes: 41 additions & 1 deletion src/Dispatcher.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
#pragma once

#include <MeshCore.h>
#if MESH_PACKET_LOGGING && ARDUINO
#include <Arduino.h>
#include <stdarg.h>

class LogPrint : public Print {
Print* _impl;
public:
LogPrint() : _impl(&Serial) {}
void setStream(Print* s) { if (s) _impl = s; }
size_t write(uint8_t c) override { return _impl->write(c); }
size_t write(const uint8_t* buf, size_t n) override { return _impl->write(buf, n); }
void printf(const char* fmt, ...) {
char buf[192]; // sized for longest log line: ~31 char datetime + ~90 char fields
va_list args;
va_start(args, fmt);
int n = vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
if (n >= (int)sizeof(buf)) {
// truncation occurred: mark it visibly rather than silently losing data
buf[sizeof(buf) - 4] = '.';
buf[sizeof(buf) - 3] = '.';
buf[sizeof(buf) - 2] = '\n';
buf[sizeof(buf) - 1] = '\0';
}
_impl->print(buf);
}
};
#endif
#include <Identity.h>
#include <Packet.h>
#include <Utils.h>
Expand Down Expand Up @@ -129,6 +157,9 @@ class Dispatcher {

void processRecvPacket(Packet* pkt);
void updateTxBudget();
#if MESH_PACKET_LOGGING
LogPrint _packet_log;
#endif

protected:
PacketManager* _mgr;
Expand All @@ -154,7 +185,13 @@ class Dispatcher {

virtual DispatcherAction onRecvPacket(Packet* pkt) = 0;

virtual void logRxRaw(float snr, float rssi, const uint8_t raw[], int len) { } // custom hook
virtual void logRxRaw(float snr, float rssi, const uint8_t raw[], int len) { // custom hook
#if MESH_PACKET_LOGGING
_packet_log.printf("%s RAW: ", getLogDateTime());
mesh::Utils::printHex(_packet_log, raw, len);
_packet_log.print("\n");
#endif
}

virtual void logRx(Packet* packet, int len, float score) { } // hooks for custom logging
virtual void logTx(Packet* packet, int len) { }
Expand All @@ -172,6 +209,9 @@ class Dispatcher {
public:
void begin();
void loop();
#if MESH_PACKET_LOGGING
void setPacketLogStream(Print* s) { _packet_log.setStream(s); }
#endif

Packet* obtainNewPacket();
void releasePacket(Packet* packet);
Expand Down
2 changes: 1 addition & 1 deletion src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void Utils::toHex(char* dest, const uint8_t* src, size_t len) {
*dest = 0;
}

void Utils::printHex(Stream& s, const uint8_t* src, size_t len) {
void Utils::printHex(Print& s, const uint8_t* src, size_t len) {
while (len > 0) {
uint8_t b = *src++;
s.print(hex_chars[b >> 4]);
Expand Down
4 changes: 2 additions & 2 deletions src/Utils.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <MeshCore.h>
#include <Stream.h>
#include <Print.h>
#include <string.h>

namespace mesh {
Expand Down Expand Up @@ -69,7 +69,7 @@ class Utils {
/**
* \brief Prints the hexadecimal representation of 'src' bytes of given length, to Stream 's'.
*/
static void printHex(Stream& s, const uint8_t* src, size_t len);
static void printHex(Print& s, const uint8_t* src, size_t len);

/**
* \brief parse 'text' into parts separated by 'separator' char.
Expand Down
17 changes: 17 additions & 0 deletions src/helpers/BLELogInterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

/**
* Platform-selecting shim for BLELogInterface.
* Include this header and use BLELogInterface directly; the correct
* platform implementation is pulled in automatically.
*
* Supported platforms: ESP32, nRF52.
* On unsupported platforms this header intentionally defines nothing —
* guard usage with #if defined(NRF52_PLATFORM) || defined(ESP32).
*/

#if defined(NRF52_PLATFORM)
#include "nrf52/BLELogInterface.h"
#elif defined(ESP32)
#include "esp32/BLELogInterface.h"
#endif
Loading
Loading