mirror of
https://github.com/meshtastic/firmware.git
synced 2025-06-09 06:32:06 +00:00
Initial PhoneAPI rate-limiting of messages on certain ports (#4756)
This commit is contained in:
parent
35e1c401e2
commit
777bcf691a
@ -3,6 +3,8 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#define ONE_DAY 24 * 60 * 60
|
#define ONE_DAY 24 * 60 * 60
|
||||||
#define ONE_MINUTE_MS 60 * 1000
|
#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_gps_update_interval IF_ROUTER(ONE_DAY, 2 * 60)
|
||||||
#define default_telemetry_broadcast_interval_secs IF_ROUTER(ONE_DAY / 2, 30 * 60)
|
#define default_telemetry_broadcast_interval_secs IF_ROUTER(ONE_DAY / 2, 30 * 60)
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
#if !MESHTASTIC_EXCLUDE_MQTT
|
#if !MESHTASTIC_EXCLUDE_MQTT
|
||||||
#include "mqtt/MQTT.h"
|
#include "mqtt/MQTT.h"
|
||||||
#endif
|
#endif
|
||||||
|
#include <RTC.h>
|
||||||
|
|
||||||
PhoneAPI::PhoneAPI()
|
PhoneAPI::PhoneAPI()
|
||||||
{
|
{
|
||||||
@ -541,14 +542,37 @@ bool PhoneAPI::available()
|
|||||||
return false;
|
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
|
* 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.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);
|
service->handleToRadio(p);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,8 +2,10 @@
|
|||||||
|
|
||||||
#include "Observer.h"
|
#include "Observer.h"
|
||||||
#include "mesh-pb-constants.h"
|
#include "mesh-pb-constants.h"
|
||||||
|
#include "meshtastic/portnums.pb.h"
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
// Make sure that we never let our packets grow too large for one BLE packet
|
// 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;
|
uint8_t config_state = 0;
|
||||||
|
|
||||||
|
// Hashmap of timestamps for last time we received a packet on the API per portnum
|
||||||
|
std::unordered_map<meshtastic_PortNum, uint32_t> lastPortNumToRadio;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Each packet sent to the phone has an incrementing count
|
* 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);
|
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
|
* Get the next packet we want to send to the phone
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user