2022-02-27 00:18:35 -08:00
|
|
|
#include "NodeInfoModule.h"
|
2024-03-17 08:18:30 -05:00
|
|
|
#include "Default.h"
|
2020-12-05 10:00:46 +08:00
|
|
|
#include "MeshService.h"
|
|
|
|
|
#include "NodeDB.h"
|
|
|
|
|
#include "RTC.h"
|
|
|
|
|
#include "Router.h"
|
2022-05-07 20:31:21 +10:00
|
|
|
#include "configuration.h"
|
2020-12-05 10:00:46 +08:00
|
|
|
#include "main.h"
|
2024-09-23 08:58:14 -05:00
|
|
|
#include <Throttle.h>
|
2020-12-05 10:00:46 +08:00
|
|
|
|
2022-02-27 02:21:02 -08:00
|
|
|
NodeInfoModule *nodeInfoModule;
|
2020-12-05 10:00:46 +08:00
|
|
|
|
2023-01-21 18:22:19 +01:00
|
|
|
bool NodeInfoModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshtastic_User *pptr)
|
2020-12-05 10:00:46 +08:00
|
|
|
{
|
2021-02-17 19:04:41 +08:00
|
|
|
auto p = *pptr;
|
2020-12-05 10:00:46 +08:00
|
|
|
|
2025-07-16 16:05:34 -05:00
|
|
|
if (p.is_licensed != owner.is_licensed) {
|
|
|
|
|
LOG_WARN("Invalid nodeInfo detected, is_licensed mismatch!");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-28 11:30:59 -05:00
|
|
|
// Coerce user.id to be derived from the node number
|
|
|
|
|
snprintf(p.id, sizeof(p.id), "!%08x", getFrom(&mp));
|
|
|
|
|
|
2024-03-21 09:06:37 -05:00
|
|
|
bool hasChanged = nodeDB->updateUser(getFrom(&mp), p, mp.channel);
|
2020-12-05 10:00:46 +08:00
|
|
|
|
2024-10-19 12:48:00 -05:00
|
|
|
bool wasBroadcast = isBroadcast(mp.to);
|
2020-12-05 10:00:46 +08:00
|
|
|
|
2023-04-11 13:16:19 +02:00
|
|
|
// if user has changed while packet was not for us, inform phone
|
2024-10-04 13:28:51 +02:00
|
|
|
if (hasChanged && !wasBroadcast && !isToUs(&mp))
|
2024-08-01 19:29:49 -05:00
|
|
|
service->sendToPhone(packetPool.allocCopy(mp));
|
2023-04-11 13:16:19 +02:00
|
|
|
|
2024-10-14 06:11:43 +02:00
|
|
|
// LOG_DEBUG("did handleReceived");
|
2020-12-05 10:00:46 +08:00
|
|
|
return false; // Let others look at this message also if they want
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 17:17:53 -05:00
|
|
|
void NodeInfoModule::sendOurNodeInfo(NodeNum dest, bool wantReplies, uint8_t channel, bool _shorterTimeout)
|
2020-12-05 10:00:46 +08:00
|
|
|
{
|
2021-02-11 17:39:53 +08:00
|
|
|
// cancel any not yet sent (now stale) position packets
|
2021-02-14 12:27:10 +08:00
|
|
|
if (prevPacketId) // if we wrap around to zero, we'll simply fail to cancel in that rare case (no big deal)
|
2024-08-01 19:29:49 -05:00
|
|
|
service->cancelSending(prevPacketId);
|
2024-08-14 17:17:53 -05:00
|
|
|
shorterTimeout = _shorterTimeout;
|
2023-01-21 18:22:19 +01:00
|
|
|
meshtastic_MeshPacket *p = allocReply();
|
2023-02-15 19:31:09 +01:00
|
|
|
if (p) { // Check whether we didn't ignore it
|
|
|
|
|
p->to = dest;
|
2023-10-01 12:48:12 -05:00
|
|
|
p->decoded.want_response = (config.device.role != meshtastic_Config_DeviceConfig_Role_TRACKER &&
|
|
|
|
|
config.device.role != meshtastic_Config_DeviceConfig_Role_SENSOR) &&
|
|
|
|
|
wantReplies;
|
2024-08-14 17:17:53 -05:00
|
|
|
if (_shorterTimeout)
|
|
|
|
|
p->priority = meshtastic_MeshPacket_Priority_DEFAULT;
|
|
|
|
|
else
|
|
|
|
|
p->priority = meshtastic_MeshPacket_Priority_BACKGROUND;
|
2023-03-29 13:51:22 +02:00
|
|
|
if (channel > 0) {
|
2024-11-04 12:16:25 -06:00
|
|
|
LOG_DEBUG("Send ourNodeInfo to channel %d", channel);
|
2023-03-29 13:51:22 +02:00
|
|
|
p->channel = channel;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 19:31:09 +01:00
|
|
|
prevPacketId = p->id;
|
2021-02-11 19:00:17 +08:00
|
|
|
|
2024-08-01 19:29:49 -05:00
|
|
|
service->sendToMesh(p);
|
2024-08-14 17:17:53 -05:00
|
|
|
shorterTimeout = false;
|
2023-02-15 19:31:09 +01:00
|
|
|
}
|
2020-12-05 10:00:46 +08:00
|
|
|
}
|
2020-12-05 11:15:06 +08:00
|
|
|
|
2023-01-21 18:22:19 +01:00
|
|
|
meshtastic_MeshPacket *NodeInfoModule::allocReply()
|
2020-12-07 10:18:11 +08:00
|
|
|
{
|
2024-05-30 08:49:01 -05:00
|
|
|
if (!airTime->isTxAllowedChannelUtil(false)) {
|
|
|
|
|
ignoreRequest = true; // Mark it as ignored for MeshModule
|
2024-11-04 19:15:59 -06:00
|
|
|
LOG_DEBUG("Skip send NodeInfo > 40%% ch. util");
|
2024-05-30 08:49:01 -05:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2024-03-16 19:56:42 -05:00
|
|
|
// If we sent our NodeInfo less than 5 min. ago, don't send it again as it may be still underway.
|
2024-09-23 08:58:14 -05:00
|
|
|
if (!shorterTimeout && lastSentToMesh && Throttle::isWithinTimespanMs(lastSentToMesh, 5 * 60 * 1000)) {
|
2024-11-06 07:03:25 -06:00
|
|
|
LOG_DEBUG("Skip send NodeInfo since we sent it <5min ago");
|
2023-02-15 19:31:09 +01:00
|
|
|
ignoreRequest = true; // Mark it as ignored for MeshModule
|
|
|
|
|
return NULL;
|
2024-09-23 08:58:14 -05:00
|
|
|
} else if (shorterTimeout && lastSentToMesh && Throttle::isWithinTimespanMs(lastSentToMesh, 60 * 1000)) {
|
2024-11-06 07:03:25 -06:00
|
|
|
LOG_DEBUG("Skip send NodeInfo since we sent it <60s ago");
|
2024-08-14 17:17:53 -05:00
|
|
|
ignoreRequest = true; // Mark it as ignored for MeshModule
|
|
|
|
|
return NULL;
|
2023-02-15 19:31:09 +01:00
|
|
|
} else {
|
|
|
|
|
ignoreRequest = false; // Don't ignore requests anymore
|
|
|
|
|
meshtastic_User &u = owner;
|
|
|
|
|
|
2024-10-30 14:05:09 -03:00
|
|
|
// Strip the public key if the user is licensed
|
|
|
|
|
if (u.is_licensed && u.public_key.size > 0) {
|
|
|
|
|
u.public_key.bytes[0] = 0;
|
|
|
|
|
u.public_key.size = 0;
|
|
|
|
|
}
|
2025-05-14 15:28:09 -05:00
|
|
|
// Coerce unmessagable for Repeater role
|
|
|
|
|
if (u.role == meshtastic_Config_DeviceConfig_Role_REPEATER) {
|
|
|
|
|
u.has_is_unmessagable = true;
|
|
|
|
|
u.is_unmessagable = true;
|
|
|
|
|
}
|
2024-10-30 14:05:09 -03:00
|
|
|
|
2024-11-04 12:16:25 -06:00
|
|
|
LOG_INFO("Send owner %s/%s/%s", u.id, u.long_name, u.short_name);
|
2024-09-23 08:58:14 -05:00
|
|
|
lastSentToMesh = millis();
|
2023-02-15 19:31:09 +01:00
|
|
|
return allocDataProtobuf(u);
|
|
|
|
|
}
|
2020-12-07 10:18:11 +08:00
|
|
|
}
|
2021-02-14 12:27:10 +08:00
|
|
|
|
2022-02-27 02:21:02 -08:00
|
|
|
NodeInfoModule::NodeInfoModule()
|
2024-11-04 12:16:25 -06:00
|
|
|
: ProtobufModule("nodeinfo", meshtastic_PortNum_NODEINFO_APP, &meshtastic_User_msg), concurrency::OSThread("NodeInfo")
|
2021-02-14 12:27:10 +08:00
|
|
|
{
|
2021-02-17 19:04:41 +08:00
|
|
|
isPromiscuous = true; // We always want to update our nodedb, even if we are sniffing on others
|
2025-01-26 20:59:59 +01:00
|
|
|
|
|
|
|
|
setIntervalFromNow(setStartDelay()); // Send our initial owner announcement 30 seconds
|
|
|
|
|
// after we start (to give network time to setup)
|
2021-02-14 12:27:10 +08:00
|
|
|
}
|
|
|
|
|
|
2022-02-27 02:21:02 -08:00
|
|
|
int32_t NodeInfoModule::runOnce()
|
2021-02-14 12:27:10 +08:00
|
|
|
{
|
|
|
|
|
// If we changed channels, ask everyone else for their latest info
|
|
|
|
|
bool requestReplies = currentGeneration != radioGeneration;
|
|
|
|
|
currentGeneration = radioGeneration;
|
|
|
|
|
|
2023-12-06 14:04:09 -06:00
|
|
|
if (airTime->isTxAllowedAirUtil() && config.device.role != meshtastic_Config_DeviceConfig_Role_CLIENT_HIDDEN) {
|
2024-11-04 12:16:25 -06:00
|
|
|
LOG_INFO("Send our nodeinfo to mesh (wantReplies=%d)", requestReplies);
|
2023-01-11 21:52:19 +01:00
|
|
|
sendOurNodeInfo(NODENUM_BROADCAST, requestReplies); // Send our info (don't request replies)
|
|
|
|
|
}
|
2024-03-17 08:18:30 -05:00
|
|
|
return Default::getConfiguredOrDefaultMs(config.device.node_info_broadcast_secs, default_node_info_broadcast_secs);
|
2025-01-26 20:59:59 +01:00
|
|
|
}
|