Ignore seen phoneapi packets (#4888)

* Ignore PhoneAPI packet if it's been seen

* ignoramus

* Also keep track of the last 20 packet IDs

* Fill

* Make this match the nimble one

* Add the log too

* Ignore zero ID packets

* Remove message entirely

* TRunkt
This commit is contained in:
Ben Meadors 2024-09-27 20:09:53 -05:00 committed by GitHub
parent 0e0811eccd
commit 8efc15f4d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 52 additions and 6 deletions

View File

@ -8,7 +8,7 @@ plugins:
uri: https://github.com/trunk-io/plugins uri: https://github.com/trunk-io/plugins
lint: lint:
enabled: enabled:
- trufflehog@3.82.4 - trufflehog@3.82.5
- yamllint@1.35.1 - yamllint@1.35.1
- bandit@1.7.10 - bandit@1.7.10
- checkov@3.2.255 - checkov@3.2.255

View File

@ -8,6 +8,7 @@
#include "MeshTypes.h" #include "MeshTypes.h"
#include "NodeStatus.h" #include "NodeStatus.h"
#include "configuration.h"
#include "mesh-pb-constants.h" #include "mesh-pb-constants.h"
#include "mesh/generated/meshtastic/mesh.pb.h" // For CriticalErrorCode #include "mesh/generated/meshtastic/mesh.pb.h" // For CriticalErrorCode

View File

@ -8,6 +8,7 @@
#include "FSCommon.h" #include "FSCommon.h"
#include "MeshService.h" #include "MeshService.h"
#include "NodeDB.h" #include "NodeDB.h"
#include "PacketHistory.h"
#include "PhoneAPI.h" #include "PhoneAPI.h"
#include "PowerFSM.h" #include "PowerFSM.h"
#include "RadioInterface.h" #include "RadioInterface.h"
@ -31,6 +32,7 @@
PhoneAPI::PhoneAPI() PhoneAPI::PhoneAPI()
{ {
lastContactMsec = millis(); lastContactMsec = millis();
std::fill(std::begin(recentToRadioPacketIds), std::end(recentToRadioPacketIds), 0);
} }
PhoneAPI::~PhoneAPI() PhoneAPI::~PhoneAPI()
@ -109,8 +111,6 @@ bool PhoneAPI::handleToRadio(const uint8_t *buf, size_t bufLength)
powerFSM.trigger(EVENT_CONTACT_FROM_PHONE); // As long as the phone keeps talking to us, don't let the radio go to sleep powerFSM.trigger(EVENT_CONTACT_FROM_PHONE); // As long as the phone keeps talking to us, don't let the radio go to sleep
lastContactMsec = millis(); lastContactMsec = millis();
// return (lastContactMsec != 0) &&
memset(&toRadioScratch, 0, sizeof(toRadioScratch)); memset(&toRadioScratch, 0, sizeof(toRadioScratch));
if (pb_decode_from_bytes(buf, bufLength, &meshtastic_ToRadio_msg, &toRadioScratch)) { if (pb_decode_from_bytes(buf, bufLength, &meshtastic_ToRadio_msg, &toRadioScratch)) {
switch (toRadioScratch.which_payload_variant) { switch (toRadioScratch.which_payload_variant) {
@ -572,12 +572,35 @@ void PhoneAPI::sendNotification(meshtastic_LogRecord_Level level, uint32_t reply
service->sendClientNotification(cn); service->sendClientNotification(cn);
} }
bool PhoneAPI::wasSeenRecently(uint32_t id)
{
for (int i = 0; i < 20; i++) {
if (recentToRadioPacketIds[i] == id) {
return true;
}
if (recentToRadioPacketIds[i] == 0) {
recentToRadioPacketIds[i] = id;
return false;
}
}
// If the array is full, shift all elements to the left and add the new id at the end
memmove(recentToRadioPacketIds, recentToRadioPacketIds + 1, (19) * sizeof(uint32_t));
recentToRadioPacketIds[19] = id;
return false;
}
/** /**
* Handle a packet that the phone wants us to send. It is our responsibility to free the packet to the pool * Handle a packet that the phone wants us to send. It is our responsibility to free the packet to the pool
*/ */
bool PhoneAPI::handleToRadioPacket(meshtastic_MeshPacket &p) bool PhoneAPI::handleToRadioPacket(meshtastic_MeshPacket &p)
{ {
printPacket("PACKET FROM PHONE", &p); printPacket("PACKET FROM PHONE", &p);
if (p.id > 0 && wasSeenRecently(p.id)) {
LOG_DEBUG("Ignoring packet from phone, already seen recently\n");
return false;
}
if (p.decoded.portnum == meshtastic_PortNum_TRACEROUTE_APP && lastPortNumToRadio[p.decoded.portnum] && if (p.decoded.portnum == meshtastic_PortNum_TRACEROUTE_APP && lastPortNumToRadio[p.decoded.portnum] &&
Throttle::isWithinTimespanMs(lastPortNumToRadio[p.decoded.portnum], THIRTY_SECONDS_MS)) { Throttle::isWithinTimespanMs(lastPortNumToRadio[p.decoded.portnum], THIRTY_SECONDS_MS)) {
LOG_WARN("Rate limiting portnum %d\n", p.decoded.portnum); LOG_WARN("Rate limiting portnum %d\n", p.decoded.portnum);
@ -586,7 +609,8 @@ bool PhoneAPI::handleToRadioPacket(meshtastic_MeshPacket &p)
} else if (p.decoded.portnum == meshtastic_PortNum_POSITION_APP && lastPortNumToRadio[p.decoded.portnum] && } else if (p.decoded.portnum == meshtastic_PortNum_POSITION_APP && lastPortNumToRadio[p.decoded.portnum] &&
Throttle::isWithinTimespanMs(lastPortNumToRadio[p.decoded.portnum], FIVE_SECONDS_MS)) { Throttle::isWithinTimespanMs(lastPortNumToRadio[p.decoded.portnum], FIVE_SECONDS_MS)) {
LOG_WARN("Rate limiting portnum %d\n", p.decoded.portnum); LOG_WARN("Rate limiting portnum %d\n", p.decoded.portnum);
sendNotification(meshtastic_LogRecord_Level_WARNING, p.id, "Position can only be sent once every 5 seconds"); // FIXME: Figure out why this continues to happen
// sendNotification(meshtastic_LogRecord_Level_WARNING, p.id, "Position can only be sent once every 5 seconds");
return false; return false;
} }
lastPortNumToRadio[p.decoded.portnum] = millis(); lastPortNumToRadio[p.decoded.portnum] = millis();

View File

@ -52,6 +52,7 @@ class PhoneAPI
// Hashmap of timestamps for last time we received a packet on the API per portnum // Hashmap of timestamps for last time we received a packet on the API per portnum
std::unordered_map<meshtastic_PortNum, uint32_t> lastPortNumToRadio; std::unordered_map<meshtastic_PortNum, uint32_t> lastPortNumToRadio;
uint32_t recentToRadioPacketIds[20]; // Last 20 ToRadio MeshPacket IDs we have seen
/** /**
* Each packet sent to the phone has an incrementing count * Each packet sent to the phone has an incrementing count
@ -159,6 +160,8 @@ class PhoneAPI
/// begin a new connection /// begin a new connection
void handleStartConfig(); void handleStartConfig();
bool wasSeenRecently(uint32_t packetId);
/** /**
* Handle a packet that the phone wants us to send. We can write to it but can not keep a reference to it * Handle a packet that the phone wants us to send. We can write to it but can not keep a reference to it
* @return true true if a packet was queued for sending * @return true true if a packet was queued for sending

View File

@ -44,6 +44,9 @@ static BluetoothPhoneAPI *bluetoothPhoneAPI;
* Subclasses can use this as a hook to provide custom notifications for their transport (i.e. bluetooth notifies) * Subclasses can use this as a hook to provide custom notifications for their transport (i.e. bluetooth notifies)
*/ */
// Last ToRadio value received from the phone
static uint8_t lastToRadio[MAX_TO_FROM_RADIO_SIZE];
class NimbleBluetoothToRadioCallback : public NimBLECharacteristicCallbacks class NimbleBluetoothToRadioCallback : public NimBLECharacteristicCallbacks
{ {
virtual void onWrite(NimBLECharacteristic *pCharacteristic) virtual void onWrite(NimBLECharacteristic *pCharacteristic)
@ -51,7 +54,13 @@ class NimbleBluetoothToRadioCallback : public NimBLECharacteristicCallbacks
LOG_INFO("To Radio onwrite\n"); LOG_INFO("To Radio onwrite\n");
auto val = pCharacteristic->getValue(); auto val = pCharacteristic->getValue();
bluetoothPhoneAPI->handleToRadio(val.data(), val.length()); if (memcmp(lastToRadio, val.data(), val.length()) != 0) {
LOG_DEBUG("New ToRadio packet\n");
memcpy(lastToRadio, val.data(), val.length());
bluetoothPhoneAPI->handleToRadio(val.data(), val.length());
} else {
LOG_DEBUG("Dropping duplicate ToRadio packet we just saw\n");
}
} }
}; };

View File

@ -141,10 +141,19 @@ void onFromRadioAuthorize(uint16_t conn_hdl, BLECharacteristic *chr, ble_gatts_e
} }
authorizeRead(conn_hdl); authorizeRead(conn_hdl);
} }
// Last ToRadio value received from the phone
static uint8_t lastToRadio[MAX_TO_FROM_RADIO_SIZE];
void onToRadioWrite(uint16_t conn_hdl, BLECharacteristic *chr, uint8_t *data, uint16_t len) void onToRadioWrite(uint16_t conn_hdl, BLECharacteristic *chr, uint8_t *data, uint16_t len)
{ {
LOG_INFO("toRadioWriteCb data %p, len %u\n", data, len); LOG_INFO("toRadioWriteCb data %p, len %u\n", data, len);
bluetoothPhoneAPI->handleToRadio(data, len); if (memcmp(lastToRadio, data, len) != 0) {
LOG_DEBUG("New ToRadio packet\n");
memcpy(lastToRadio, data, len);
bluetoothPhoneAPI->handleToRadio(data, len);
} else {
LOG_DEBUG("Dropping duplicate ToRadio packet we just saw\n");
}
} }
void setupMeshService(void) void setupMeshService(void)