diff --git a/src/mesh/Default.h b/src/mesh/Default.h index bafa60898..5641b5d25 100644 --- a/src/mesh/Default.h +++ b/src/mesh/Default.h @@ -3,6 +3,8 @@ #include #define ONE_DAY 24 * 60 * 60 #define ONE_MINUTE_MS 60 * 1000 +#define THIRTY_SECONDS_MS 30 * 1000 +#define FIVE_SECONDS_MS 5 * 1000 #define default_gps_update_interval IF_ROUTER(ONE_DAY, 2 * 60) #define default_telemetry_broadcast_interval_secs IF_ROUTER(ONE_DAY / 2, 30 * 60) diff --git a/src/mesh/PhoneAPI.cpp b/src/mesh/PhoneAPI.cpp index 742bdbf34..121687c49 100644 --- a/src/mesh/PhoneAPI.cpp +++ b/src/mesh/PhoneAPI.cpp @@ -25,6 +25,7 @@ #if !MESHTASTIC_EXCLUDE_MQTT #include "mqtt/MQTT.h" #endif +#include PhoneAPI::PhoneAPI() { @@ -541,14 +542,37 @@ bool PhoneAPI::available() return false; } +void PhoneAPI::sendNotification(meshtastic_LogRecord_Level level, uint32_t replyId, const char *message) +{ + meshtastic_ClientNotification *cn = clientNotificationPool.allocZeroed(); + cn->has_reply_id = true; + cn->reply_id = replyId; + cn->level = meshtastic_LogRecord_Level_WARNING; + cn->time = getValidTime(RTCQualityFromNet); + strncpy(cn->message, message, sizeof(cn->message)); + service->sendClientNotification(cn); + delete cn; +} + /** * 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) { printPacket("PACKET FROM PHONE", &p); + if (p.decoded.portnum == meshtastic_PortNum_TRACEROUTE_APP && lastPortNumToRadio[p.decoded.portnum] && + (millis() - lastPortNumToRadio[p.decoded.portnum]) < (THIRTY_SECONDS_MS)) { + LOG_WARN("Rate limiting portnum %d\n", p.decoded.portnum); + sendNotification(meshtastic_LogRecord_Level_WARNING, p.id, "TraceRoute can only be sent once every 30 seconds"); + return false; + } else if (p.decoded.portnum == meshtastic_PortNum_POSITION_APP && lastPortNumToRadio[p.decoded.portnum] && + (millis() - lastPortNumToRadio[p.decoded.portnum]) < (FIVE_SECONDS_MS)) { + 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"); + return false; + } + lastPortNumToRadio[p.decoded.portnum] = millis(); service->handleToRadio(p); - return true; } diff --git a/src/mesh/PhoneAPI.h b/src/mesh/PhoneAPI.h index 5feb1c4bf..1e09b9535 100644 --- a/src/mesh/PhoneAPI.h +++ b/src/mesh/PhoneAPI.h @@ -2,8 +2,10 @@ #include "Observer.h" #include "mesh-pb-constants.h" +#include "meshtastic/portnums.pb.h" #include #include +#include #include // Make sure that we never let our packets grow too large for one BLE packet @@ -48,6 +50,9 @@ class PhoneAPI uint8_t config_state = 0; + // Hashmap of timestamps for last time we received a packet on the API per portnum + std::unordered_map lastPortNumToRadio; + /** * Each packet sent to the phone has an incrementing count */ @@ -99,6 +104,11 @@ class PhoneAPI */ virtual bool handleToRadio(const uint8_t *buf, size_t len); + /** + * Send a (client)notification to the phone + */ + virtual void sendNotification(meshtastic_LogRecord_Level level, uint32_t replyId, const char *message); + /** * Get the next packet we want to send to the phone *