2022-02-27 00:18:35 -08:00
|
|
|
#include "NodeInfoModule.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"
|
|
|
|
|
|
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
|
|
|
|
2023-04-11 13:16:19 +02:00
|
|
|
bool hasChanged = nodeDB.updateUser(getFrom(&mp), p);
|
2020-12-05 10:00:46 +08:00
|
|
|
|
|
|
|
|
bool wasBroadcast = mp.to == NODENUM_BROADCAST;
|
|
|
|
|
|
|
|
|
|
// Show new nodes on LCD screen
|
|
|
|
|
if (wasBroadcast) {
|
|
|
|
|
String lcd = String("Joined: ") + p.long_name + "\n";
|
2022-05-07 20:31:21 +10:00
|
|
|
if (screen)
|
2021-03-09 15:07:02 +08:00
|
|
|
screen->print(lcd.c_str());
|
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
|
|
|
|
|
if (hasChanged && !wasBroadcast && mp.to != nodeDB.getNodeNum())
|
|
|
|
|
service.sendToPhone(packetPool.allocCopy(mp));
|
|
|
|
|
|
2022-12-29 20:41:37 -06:00
|
|
|
// LOG_DEBUG("did handleReceived\n");
|
2020-12-05 10:00:46 +08:00
|
|
|
return false; // Let others look at this message also if they want
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 13:51:22 +02:00
|
|
|
void NodeInfoModule::sendOurNodeInfo(NodeNum dest, bool wantReplies, uint8_t channel)
|
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)
|
2021-02-11 17:39:53 +08:00
|
|
|
service.cancelSending(prevPacketId);
|
|
|
|
|
|
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;
|
2023-02-15 19:31:09 +01:00
|
|
|
p->priority = meshtastic_MeshPacket_Priority_BACKGROUND;
|
2023-03-29 13:51:22 +02:00
|
|
|
if (channel > 0) {
|
|
|
|
|
LOG_DEBUG("sending ourNodeInfo to channel %d\n", channel);
|
|
|
|
|
p->channel = channel;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 19:31:09 +01:00
|
|
|
prevPacketId = p->id;
|
2021-02-11 19:00:17 +08:00
|
|
|
|
2023-02-15 19:31:09 +01:00
|
|
|
service.sendToMesh(p);
|
|
|
|
|
}
|
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
|
|
|
{
|
2023-02-15 19:31:09 +01:00
|
|
|
uint32_t now = millis();
|
|
|
|
|
// If we sent our NodeInfo less than 1 min. ago, don't send it again as it may be still underway.
|
|
|
|
|
if (lastSentToMesh && (now - lastSentToMesh) < 60 * 1000) {
|
|
|
|
|
LOG_DEBUG("Sending NodeInfo will be ignored since we just sent it.\n");
|
|
|
|
|
ignoreRequest = true; // Mark it as ignored for MeshModule
|
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
|
|
|
|
ignoreRequest = false; // Don't ignore requests anymore
|
|
|
|
|
meshtastic_User &u = owner;
|
|
|
|
|
|
|
|
|
|
LOG_INFO("sending owner %s/%s/%s\n", u.id, u.long_name, u.short_name);
|
|
|
|
|
lastSentToMesh = now;
|
|
|
|
|
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()
|
2023-01-21 18:22:19 +01:00
|
|
|
: ProtobufModule("nodeinfo", meshtastic_PortNum_NODEINFO_APP, &meshtastic_User_msg), concurrency::OSThread("NodeInfoModule")
|
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
|
2023-01-21 14:34:29 +01:00
|
|
|
setIntervalFromNow(30 *
|
|
|
|
|
1000); // 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
|
|
|
{
|
|
|
|
|
static uint32_t currentGeneration;
|
|
|
|
|
|
|
|
|
|
// If we changed channels, ask everyone else for their latest info
|
|
|
|
|
bool requestReplies = currentGeneration != radioGeneration;
|
|
|
|
|
currentGeneration = radioGeneration;
|
|
|
|
|
|
2023-01-11 21:52:19 +01:00
|
|
|
if (airTime->isTxAllowedAirUtil()) {
|
|
|
|
|
LOG_INFO("Sending our nodeinfo to mesh (wantReplies=%d)\n", requestReplies);
|
|
|
|
|
sendOurNodeInfo(NODENUM_BROADCAST, requestReplies); // Send our info (don't request replies)
|
|
|
|
|
}
|
2021-02-14 12:27:10 +08:00
|
|
|
|
2023-02-08 18:04:21 -06:00
|
|
|
return getConfiguredOrDefaultMs(config.device.node_info_broadcast_secs, default_broadcast_interval_secs);
|
2023-01-11 21:52:19 +01:00
|
|
|
}
|