Fix remaining NodeDB review issues

Agent-Logs-Url: https://github.com/meshtastic/firmware/sessions/c76b9a5a-7244-4fbc-9ef0-98091d8caaea

Co-authored-by: thebentern <9000580+thebentern@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-05-09 12:45:51 +00:00
committed by GitHub
co-authored by thebentern
parent 589634c18d
commit a0daaf6b5c
16 changed files with 158 additions and 175 deletions
+6 -6
View File
@@ -53,8 +53,8 @@ class GPSStatus : public Status
int32_t getLatitude() const
{
if (config.position.fixed_position) {
const meshtastic_PositionLite *pos = nodeDB->getNodePosition(nodeDB->getNodeNum());
return pos ? pos->latitude_i : 0;
meshtastic_PositionLite pos;
return nodeDB->copyNodePosition(nodeDB->getNodeNum(), pos) ? pos.latitude_i : 0;
} else {
return p.latitude_i;
}
@@ -63,8 +63,8 @@ class GPSStatus : public Status
int32_t getLongitude() const
{
if (config.position.fixed_position) {
const meshtastic_PositionLite *pos = nodeDB->getNodePosition(nodeDB->getNodeNum());
return pos ? pos->longitude_i : 0;
meshtastic_PositionLite pos;
return nodeDB->copyNodePosition(nodeDB->getNodeNum(), pos) ? pos.longitude_i : 0;
} else {
return p.longitude_i;
}
@@ -73,8 +73,8 @@ class GPSStatus : public Status
int32_t getAltitude() const
{
if (config.position.fixed_position) {
const meshtastic_PositionLite *pos = nodeDB->getNodePosition(nodeDB->getNodeNum());
return pos ? pos->altitude : 0;
meshtastic_PositionLite pos;
return nodeDB->copyNodePosition(nodeDB->getNodeNum(), pos) ? pos.altitude : 0;
} else {
return p.altitude;
}
+21 -19
View File
@@ -104,17 +104,17 @@ std::string getSafeNodeName(OLEDDisplay *display, meshtastic_NodeInfoLite *node,
// of NodeDB's per-NodeNum cache instead of scanning a FIFO.
std::string composedFromStatus;
if (config.display.use_long_node_name && nodeInfoLiteHasUser(node) && nodeDB) {
const meshtastic_StatusMessage *cachedStatus = nodeDB->getNodeStatus(node->num);
if (cachedStatus && cachedStatus->status[0]) {
meshtastic_StatusMessage cachedStatus;
if (nodeDB->copyNodeStatus(node->num, cachedStatus) && cachedStatus.status[0]) {
const char *shortName = node->short_name;
const size_t statusLen = std::strlen(cachedStatus->status);
const size_t statusLen = std::strlen(cachedStatus.status);
composedFromStatus.reserve(4 + (shortName ? std::strlen(shortName) : 0) + 1 + statusLen);
composedFromStatus += "(";
if (shortName && *shortName) {
composedFromStatus += shortName;
}
composedFromStatus += ") ";
composedFromStatus += cachedStatus->status;
composedFromStatus += cachedStatus.status;
raw = composedFromStatus.c_str(); // safe for now; we'll sanitize immediately into std::string
}
@@ -399,13 +399,15 @@ void drawNodeDistance(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int16
char distStr[10] = "";
meshtastic_NodeInfoLite *ourNode = nodeDB->getMeshNode(nodeDB->getNodeNum());
const meshtastic_PositionLite *ourPos = ourNode ? nodeDB->getNodePosition(ourNode->num) : nullptr;
const meshtastic_PositionLite *theirPos = nodeDB->getNodePosition(node->num);
if (nodeDB->hasValidPosition(ourNode) && nodeDB->hasValidPosition(node) && ourPos && theirPos) {
double lat1 = ourPos->latitude_i * 1e-7;
double lon1 = ourPos->longitude_i * 1e-7;
double lat2 = theirPos->latitude_i * 1e-7;
double lon2 = theirPos->longitude_i * 1e-7;
meshtastic_PositionLite ourPos;
meshtastic_PositionLite theirPos;
const bool haveOurPos = ourNode && nodeDB->copyNodePosition(ourNode->num, ourPos);
const bool haveTheirPos = nodeDB->copyNodePosition(node->num, theirPos);
if (nodeDB->hasValidPosition(ourNode) && nodeDB->hasValidPosition(node) && haveOurPos && haveTheirPos) {
double lat1 = ourPos.latitude_i * 1e-7;
double lon1 = ourPos.longitude_i * 1e-7;
double lat2 = theirPos.latitude_i * 1e-7;
double lon2 = theirPos.longitude_i * 1e-7;
double earthRadiusKm = 6371.0;
double dLat = (lat2 - lat1) * DEG_TO_RAD;
@@ -538,11 +540,11 @@ void drawCompassArrow(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int16
int centerX = x + columnWidth - arrowXOffset;
int centerY = y + FONT_HEIGHT_SMALL / 2;
const meshtastic_PositionLite *nodePos = nodeDB->getNodePosition(node->num);
if (!nodePos)
meshtastic_PositionLite nodePos;
if (!nodeDB->copyNodePosition(node->num, nodePos))
return;
double nodeLat = nodePos->latitude_i * 1e-7;
double nodeLon = nodePos->longitude_i * 1e-7;
double nodeLat = nodePos.latitude_i * 1e-7;
double nodeLon = nodePos.longitude_i * 1e-7;
float bearing = GeoCoord::bearing(userLat, userLon, nodeLat, nodeLon);
float relativeBearing = CompassRenderer::adjustBearingForCompassMode(bearing, myHeadingRadian);
float relativeBearingDeg = CompassRenderer::radiansToDegrees360(relativeBearing);
@@ -883,14 +885,14 @@ void drawNodeListWithCompasses(OLEDDisplay *display, OLEDDisplayUiState *state,
{
float headingRadian = 0.0f;
auto ourNode = nodeDB->getMeshNode(nodeDB->getNodeNum());
const meshtastic_PositionLite *ourSelfPos = ourNode ? nodeDB->getNodePosition(ourNode->num) : nullptr;
if (!ourNode || !nodeDB->hasValidPosition(ourNode) || !ourSelfPos) {
meshtastic_PositionLite ourSelfPos;
if (!ourNode || !nodeDB->hasValidPosition(ourNode) || !nodeDB->copyNodePosition(ourNode->num, ourSelfPos)) {
drawNodeListScreen(display, state, x, y, "Bearings", drawEntryCompass, drawCompassUnknown, headingRadian, 0.0, 0.0);
return;
}
double lat = DegD(ourSelfPos->latitude_i);
double lon = DegD(ourSelfPos->longitude_i);
double lat = DegD(ourSelfPos.latitude_i);
double lon = DegD(ourSelfPos.longitude_i);
#if defined(M5STACK_UNITC6L)
display->clear();
+18 -15
View File
@@ -801,9 +801,9 @@ void UIRenderer::drawFavoriteNode(OLEDDisplay *display, OLEDDisplayUiState *stat
// Display it directly under the username line (if we have one). The cache
// lives on NodeDB now, keyed by NodeNum, so this is an O(1) lookup.
if (nodeDB) {
const meshtastic_StatusMessage *cachedStatus = nodeDB->getNodeStatus(node->num);
if (cachedStatus && cachedStatus->status[0]) {
drawTruncatedStatusLine(display, x, getTextPositions(display)[line++], cachedStatus->status);
meshtastic_StatusMessage cachedStatus;
if (nodeDB->copyNodeStatus(node->num, cachedStatus) && cachedStatus.status[0]) {
drawTruncatedStatusLine(display, x, getTextPositions(display)[line++], cachedStatus.status);
}
}
#endif
@@ -962,11 +962,11 @@ void UIRenderer::drawFavoriteNode(OLEDDisplay *display, OLEDDisplayUiState *stat
#if !defined(M5STACK_UNITC6L)
// === 4. Uptime (only show if metric is present) ===
char uptimeStr[32] = "";
const meshtastic_DeviceMetrics *nodeMetrics = nodeDB->getNodeTelemetry(node->num);
if (nodeMetrics && nodeMetrics->has_uptime_seconds) {
meshtastic_DeviceMetrics nodeMetrics;
if (nodeDB->copyNodeTelemetry(node->num, nodeMetrics) && nodeMetrics.has_uptime_seconds) {
char upPrefix[12]; // enough for leftSideSpacing + "Up:"
snprintf(upPrefix, sizeof(upPrefix), "%sUp:", leftSideSpacing);
getUptimeStr(nodeMetrics->uptime_seconds * 1000, upPrefix, uptimeStr, sizeof(uptimeStr));
getUptimeStr(nodeMetrics.uptime_seconds * 1000, upPrefix, uptimeStr, sizeof(uptimeStr));
}
if (uptimeStr[0]) {
display->drawString(x, getTextPositions(display)[line++], uptimeStr);
@@ -977,12 +977,15 @@ void UIRenderer::drawFavoriteNode(OLEDDisplay *display, OLEDDisplayUiState *stat
char distStr[24] = ""; // Make buffer big enough for any string
bool haveDistance = false;
const meshtastic_PositionLite *nodePos = nodeDB->getNodePosition(node->num);
const meshtastic_PositionLite *ourPos = ourNode ? nodeDB->getNodePosition(ourNode->num) : nullptr;
if (nodeDB->hasValidPosition(ourNode) && nodeDB->hasValidPosition(node) && nodePos && ourPos) {
meshtastic_PositionLite nodePos;
meshtastic_PositionLite ourPos;
const bool haveNodePos = nodeDB->copyNodePosition(node->num, nodePos);
const bool haveOurPos = ourNode && nodeDB->copyNodePosition(ourNode->num, ourPos);
if (nodeDB->hasValidPosition(ourNode) && nodeDB->hasValidPosition(node) && haveNodePos && haveOurPos) {
// Use shared meter conversion, then format display units with lightweight integer rounding.
const float distanceMeters = GeoCoord::latLongToMeter(DegD(nodePos->latitude_i), DegD(nodePos->longitude_i),
DegD(ourPos->latitude_i), DegD(ourPos->longitude_i));
const float distanceMeters =
GeoCoord::latLongToMeter(DegD(nodePos.latitude_i), DegD(nodePos.longitude_i), DegD(ourPos.latitude_i),
DegD(ourPos.longitude_i));
if (config.display.units == meshtastic_Config_DisplayConfig_DisplayUnits_IMPERIAL) {
const int feet = static_cast<int>((distanceMeters * METERS_TO_FEET) + 0.5f);
if (feet > 0 && feet < 1000) {
@@ -1606,10 +1609,10 @@ void UIRenderer::drawCompassAndLocationScreen(OLEDDisplay *display, OLEDDisplayU
headingLat = DegD(gpsStatus->getLatitude());
headingLon = DegD(gpsStatus->getLongitude());
} else if (hasOwnPositionFix) {
const meshtastic_PositionLite *ownPos = nodeDB->getNodePosition(ourNode->num);
if (ownPos) {
headingLat = DegD(ownPos->latitude_i);
headingLon = DegD(ownPos->longitude_i);
meshtastic_PositionLite ownPos;
if (nodeDB->copyNodePosition(ourNode->num, ownPos)) {
headingLat = DegD(ownPos.latitude_i);
headingLon = DegD(ownPos.longitude_i);
}
}
validHeading = CompassRenderer::getHeadingRadians(headingLat, headingLon, heading);
@@ -137,9 +137,9 @@ void InkHUD::MapApplet::onRender(bool full)
// Draw our node LAST with full white fill + outline
meshtastic_NodeInfoLite *ourNode = nodeDB->getMeshNode(nodeDB->getNodeNum());
const meshtastic_PositionLite *ourSelfPos = ourNode ? nodeDB->getNodePosition(ourNode->num) : nullptr;
if (ourNode && nodeDB->hasValidPosition(ourNode) && ourSelfPos) {
Marker self = calculateMarker(ourSelfPos->latitude_i * 1e-7, ourSelfPos->longitude_i * 1e-7, false, 0);
meshtastic_PositionLite ourSelfPos;
if (ourNode && nodeDB->hasValidPosition(ourNode) && nodeDB->copyNodePosition(ourNode->num, ourSelfPos)) {
Marker self = calculateMarker(ourSelfPos.latitude_i * 1e-7, ourSelfPos.longitude_i * 1e-7, false, 0);
int16_t centerX = X(0.5) + (self.eastMeters * metersToPx);
int16_t centerY = Y(0.5) - (self.northMeters * metersToPx);
@@ -170,10 +170,10 @@ void InkHUD::MapApplet::getMapCenter(float *lat, float *lng)
{
// If we have a valid position for our own node, use that as the anchor
meshtastic_NodeInfoLite *ourNode = nodeDB->getMeshNode(nodeDB->getNodeNum());
const meshtastic_PositionLite *ourSelfPos = ourNode ? nodeDB->getNodePosition(ourNode->num) : nullptr;
if (ourNode && nodeDB->hasValidPosition(ourNode) && ourSelfPos) {
*lat = ourSelfPos->latitude_i * 1e-7;
*lng = ourSelfPos->longitude_i * 1e-7;
meshtastic_PositionLite ourSelfPos;
if (ourNode && nodeDB->hasValidPosition(ourNode) && nodeDB->copyNodePosition(ourNode->num, ourSelfPos)) {
*lat = ourSelfPos.latitude_i * 1e-7;
*lng = ourSelfPos.longitude_i * 1e-7;
} else {
// Find mean lat long coords
// ============================
@@ -203,13 +203,13 @@ void InkHUD::MapApplet::getMapCenter(float *lat, float *lng)
if (!shouldDrawNode(node))
continue;
const meshtastic_PositionLite *pos = nodeDB->getNodePosition(node->num);
if (!pos)
meshtastic_PositionLite pos;
if (!nodeDB->copyNodePosition(node->num, pos))
continue;
// Latitude and Longitude of node, in radians
float latRad = pos->latitude_i * (1e-7) * DEG_TO_RAD;
float lngRad = pos->longitude_i * (1e-7) * DEG_TO_RAD;
float latRad = pos.latitude_i * (1e-7) * DEG_TO_RAD;
float lngRad = pos.longitude_i * (1e-7) * DEG_TO_RAD;
// Convert to cartesian points, with center of earth at 0, 0, 0
// Exact distance from center is irrelevant, as we're only interested in the vector
@@ -306,12 +306,12 @@ void InkHUD::MapApplet::getMapCenter(float *lat, float *lng)
if (!shouldDrawNode(node))
continue;
const meshtastic_PositionLite *pos = nodeDB->getNodePosition(node->num);
if (!pos)
meshtastic_PositionLite pos;
if (!nodeDB->copyNodePosition(node->num, pos))
continue;
// Check for a new top or bottom latitude
float latNode = pos->latitude_i * 1e-7;
float latNode = pos.latitude_i * 1e-7;
northernmost = max(northernmost, latNode);
southernmost = min(southernmost, latNode);
@@ -382,10 +382,11 @@ void InkHUD::MapApplet::drawLabeledMarker(meshtastic_NodeInfoLite *node)
{
// Find x and y position based on node's position in nodeDB
assert(nodeDB->hasValidPosition(node));
const meshtastic_PositionLite *pos = nodeDB->getNodePosition(node->num);
assert(pos);
Marker m = calculateMarker(pos->latitude_i * 1e-7, // Lat, converted from Meshtastic's internal int32 style
pos->longitude_i * 1e-7, // Long, converted from Meshtastic's internal int32 style
meshtastic_PositionLite pos;
const bool hasPos = nodeDB->copyNodePosition(node->num, pos);
assert(hasPos);
Marker m = calculateMarker(pos.latitude_i * 1e-7, // Lat, converted from Meshtastic's internal int32 style
pos.longitude_i * 1e-7, // Long, converted from Meshtastic's internal int32 style
node->has_hops_away, // Is the hopsAway number valid
node->hops_away // Hops away
);
@@ -528,13 +529,13 @@ void InkHUD::MapApplet::calculateAllMarkers()
if (node->num == nodeDB->getNodeNum())
continue;
const meshtastic_PositionLite *pos = nodeDB->getNodePosition(node->num);
if (!pos)
meshtastic_PositionLite pos;
if (!nodeDB->copyNodePosition(node->num, pos))
continue;
// Calculate marker and store it
markers.push_back(calculateMarker(pos->latitude_i * 1e-7, // Lat, converted from Meshtastic's internal int32 style
pos->longitude_i * 1e-7, // Long, converted from Meshtastic's internal int32 style
markers.push_back(calculateMarker(pos.latitude_i * 1e-7, // Lat, converted from Meshtastic's internal int32 style
pos.longitude_i * 1e-7, // Long, converted from Meshtastic's internal int32 style
node->has_hops_away, // Is the hopsAway number valid
node->hops_away // Hops away
));
@@ -57,13 +57,13 @@ ProcessMessage InkHUD::NodeListApplet::handleReceived(const meshtastic_MeshPacke
c.hopsAway = node->hops_away;
if (nodeDB->hasValidPosition(node) && nodeDB->hasValidPosition(ourNode)) {
const meshtastic_PositionLite *ourPos = nodeDB->getNodePosition(ourNode->num);
const meshtastic_PositionLite *theirPos = nodeDB->getNodePosition(node->num);
if (ourPos && theirPos) {
float ourLat = ourPos->latitude_i * 1e-7;
float ourLong = ourPos->longitude_i * 1e-7;
float theirLat = theirPos->latitude_i * 1e-7;
float theirLong = theirPos->longitude_i * 1e-7;
meshtastic_PositionLite ourPos;
meshtastic_PositionLite theirPos;
if (nodeDB->copyNodePosition(ourNode->num, ourPos) && nodeDB->copyNodePosition(node->num, theirPos)) {
float ourLat = ourPos.latitude_i * 1e-7;
float ourLong = ourPos.longitude_i * 1e-7;
float theirLat = theirPos.latitude_i * 1e-7;
float theirLong = theirPos.longitude_i * 1e-7;
c.distanceMeters = (int32_t)GeoCoord::latLongToMeter(theirLat, theirLong, ourLat, ourLong);
}
@@ -89,13 +89,13 @@ void InkHUD::HeardApplet::populateFromNodeDB()
c.hopsAway = node->hops_away;
if (nodeDB->hasValidPosition(node) && nodeDB->hasValidPosition(ourNode)) {
const meshtastic_PositionLite *ourPos = nodeDB->getNodePosition(ourNode->num);
const meshtastic_PositionLite *theirPos = nodeDB->getNodePosition(node->num);
if (ourPos && theirPos) {
float ourLat = ourPos->latitude_i * 1e-7;
float ourLong = ourPos->longitude_i * 1e-7;
float theirLat = theirPos->latitude_i * 1e-7;
float theirLong = theirPos->longitude_i * 1e-7;
meshtastic_PositionLite ourPos;
meshtastic_PositionLite theirPos;
if (nodeDB->copyNodePosition(ourNode->num, ourPos) && nodeDB->copyNodePosition(node->num, theirPos)) {
float ourLat = ourPos.latitude_i * 1e-7;
float ourLong = ourPos.longitude_i * 1e-7;
float theirLat = theirPos.latitude_i * 1e-7;
float theirLong = theirPos.longitude_i * 1e-7;
c.distanceMeters = (int32_t)GeoCoord::latLongToMeter(theirLat, theirLong, ourLat, ourLong);
}
@@ -124,4 +124,4 @@ std::string InkHUD::HeardApplet::getHeaderText()
return text;
}
#endif
#endif
+3 -3
View File
@@ -430,9 +430,9 @@ int MeshService::onGPSChanged(const meshtastic::GPSStatus *newStatus)
// Used fixed position if configured regardless of GPS lock
if (config.position.fixed_position) {
LOG_WARN("Use fixed position");
const meshtastic_PositionLite *fixedSlot = nodeDB->getNodePosition(node->num);
if (fixedSlot)
pos = TypeConversions::ConvertToPosition(*fixedSlot);
meshtastic_PositionLite fixedSlot;
if (nodeDB->copyNodePosition(node->num, fixedSlot))
pos = TypeConversions::ConvertToPosition(fixedSlot);
}
// Add a fresh timestamp
+5 -46
View File
@@ -1196,17 +1196,6 @@ void NodeDB::clearLocalPosition()
localPositionUpdatedSinceBoot = false;
}
const meshtastic_PositionLite *NodeDB::getNodePosition(NodeNum n) const
{
#if MESHTASTIC_EXCLUDE_POSITIONDB
(void)n;
return nullptr;
#else
auto it = nodePositions.find(n);
return (it == nodePositions.end()) ? nullptr : &it->second;
#endif
}
bool NodeDB::copyNodePosition(NodeNum n, meshtastic_PositionLite &out) const
{
#if MESHTASTIC_EXCLUDE_POSITIONDB
@@ -1219,17 +1208,6 @@ bool NodeDB::copyNodePosition(NodeNum n, meshtastic_PositionLite &out) const
#endif
}
const meshtastic_DeviceMetrics *NodeDB::getNodeTelemetry(NodeNum n) const
{
#if MESHTASTIC_EXCLUDE_TELEMETRYDB
(void)n;
return nullptr;
#else
auto it = nodeTelemetry.find(n);
return (it == nodeTelemetry.end()) ? nullptr : &it->second;
#endif
}
bool NodeDB::copyNodeTelemetry(NodeNum n, meshtastic_DeviceMetrics &out) const
{
#if MESHTASTIC_EXCLUDE_TELEMETRYDB
@@ -1242,17 +1220,6 @@ bool NodeDB::copyNodeTelemetry(NodeNum n, meshtastic_DeviceMetrics &out) const
#endif
}
const meshtastic_EnvironmentMetrics *NodeDB::getNodeEnvironment(NodeNum n) const
{
#if MESHTASTIC_EXCLUDE_ENVIRONMENTDB
(void)n;
return nullptr;
#else
auto it = nodeEnvironment.find(n);
return (it == nodeEnvironment.end()) ? nullptr : &it->second;
#endif
}
bool NodeDB::copyNodeEnvironment(NodeNum n, meshtastic_EnvironmentMetrics &out) const
{
#if MESHTASTIC_EXCLUDE_ENVIRONMENTDB
@@ -1265,17 +1232,6 @@ bool NodeDB::copyNodeEnvironment(NodeNum n, meshtastic_EnvironmentMetrics &out)
#endif
}
const meshtastic_StatusMessage *NodeDB::getNodeStatus(NodeNum n) const
{
#if MESHTASTIC_EXCLUDE_STATUSDB
(void)n;
return nullptr;
#else
auto it = nodeStatus.find(n);
return (it == nodeStatus.end()) ? nullptr : &it->second;
#endif
}
bool NodeDB::copyNodeStatus(NodeNum n, meshtastic_StatusMessage &out) const
{
#if MESHTASTIC_EXCLUDE_STATUSDB
@@ -2623,8 +2579,11 @@ bool NodeDB::hasValidPosition(const meshtastic_NodeInfoLite *n)
{
if (!n)
return false;
const meshtastic_PositionLite *pos = getNodePosition(n->num);
return pos && (pos->latitude_i != 0 || pos->longitude_i != 0);
if (n->num == getNodeNum()) {
return localPosition.latitude_i != 0 || localPosition.longitude_i != 0;
}
meshtastic_PositionLite pos;
return copyNodePosition(n->num, pos) && (pos.latitude_i != 0 || pos.longitude_i != 0);
}
/// If we have a node / user and they report is_licensed = true
+22 -9
View File
@@ -296,11 +296,8 @@ class NodeDB
virtual meshtastic_NodeInfoLite *getMeshNode(NodeNum n);
size_t getNumMeshNodes() { return numMeshNodes; }
// nullptr if absent or the corresponding DB is compiled out.
const meshtastic_PositionLite *getNodePosition(NodeNum n) const;
const meshtastic_DeviceMetrics *getNodeTelemetry(NodeNum n) const;
const meshtastic_EnvironmentMetrics *getNodeEnvironment(NodeNum n) const;
const meshtastic_StatusMessage *getNodeStatus(NodeNum n) const;
// Thread-safe satellite-map accessors. Return false if absent or the
// corresponding DB is compiled out.
bool copyNodePosition(NodeNum n, meshtastic_PositionLite &out) const;
bool copyNodeTelemetry(NodeNum n, meshtastic_DeviceMetrics &out) const;
bool copyNodeEnvironment(NodeNum n, meshtastic_EnvironmentMetrics &out) const;
@@ -313,10 +310,26 @@ class NodeDB
void setNodeStatus(NodeNum n, const meshtastic_StatusMessage &status);
void touchNodePositionTime(NodeNum n, uint32_t time);
bool hasNodePosition(NodeNum n) const { return getNodePosition(n) != nullptr; }
bool hasNodeTelemetry(NodeNum n) const { return getNodeTelemetry(n) != nullptr; }
bool hasNodeEnvironment(NodeNum n) const { return getNodeEnvironment(n) != nullptr; }
bool hasNodeStatus(NodeNum n) const { return getNodeStatus(n) != nullptr; }
bool hasNodePosition(NodeNum n) const
{
meshtastic_PositionLite scratch;
return copyNodePosition(n, scratch);
}
bool hasNodeTelemetry(NodeNum n) const
{
meshtastic_DeviceMetrics scratch;
return copyNodeTelemetry(n, scratch);
}
bool hasNodeEnvironment(NodeNum n) const
{
meshtastic_EnvironmentMetrics scratch;
return copyNodeEnvironment(n, scratch);
}
bool hasNodeStatus(NodeNum n) const
{
meshtastic_StatusMessage scratch;
return copyNodeStatus(n, scratch);
}
void eraseNodeSatellites(NodeNum n);
+7 -3
View File
@@ -51,8 +51,12 @@ meshtastic_NodeInfo TypeConversions::ConvertToNodeInfo(const meshtastic_NodeInfo
meshtastic_NodeInfo TypeConversions::ConvertToNodeInfo(const meshtastic_NodeInfoLite *lite)
{
const meshtastic_PositionLite *pos = nodeDB ? nodeDB->getNodePosition(lite->num) : nullptr;
const meshtastic_DeviceMetrics *dm = nodeDB ? nodeDB->getNodeTelemetry(lite->num) : nullptr;
meshtastic_PositionLite posScratch;
meshtastic_DeviceMetrics dmScratch;
const meshtastic_PositionLite *pos =
(nodeDB && nodeDB->copyNodePosition(lite->num, posScratch)) ? &posScratch : nullptr;
const meshtastic_DeviceMetrics *dm =
(nodeDB && nodeDB->copyNodeTelemetry(lite->num, dmScratch)) ? &dmScratch : nullptr;
return ConvertToNodeInfo(lite, pos, dm);
}
@@ -138,4 +142,4 @@ meshtastic_User TypeConversions::ConvertToUser(const meshtastic_NodeInfoLite *li
memset(user.macaddr, 0, sizeof(user.macaddr));
return user;
}
}
+6 -6
View File
@@ -742,12 +742,12 @@ void handleNodes(HTTPRequest *req, HTTPResponse *res)
node["position"] = new JSONValue();
if (nodeDB->hasValidPosition(tempNodeInfo)) {
const meshtastic_PositionLite *posLite = nodeDB->getNodePosition(tempNodeInfo->num);
if (posLite) {
meshtastic_PositionLite posLite;
if (nodeDB->copyNodePosition(tempNodeInfo->num, posLite)) {
JSONObject position;
position["latitude"] = new JSONValue((float)posLite->latitude_i * 1e-7);
position["longitude"] = new JSONValue((float)posLite->longitude_i * 1e-7);
position["altitude"] = new JSONValue((int)posLite->altitude);
position["latitude"] = new JSONValue((float)posLite.latitude_i * 1e-7);
position["longitude"] = new JSONValue((float)posLite.longitude_i * 1e-7);
position["altitude"] = new JSONValue((int)posLite.altitude);
node["position"] = new JSONValue(position);
}
}
@@ -930,4 +930,4 @@ void handleScanNetworks(HTTPRequest *req, HTTPResponse *res)
res->print(jsonString.c_str());
delete value;
}
#endif
#endif
+13 -13
View File
@@ -193,9 +193,9 @@ meshtastic_MeshPacket *PositionModule::allocPositionPacket()
meshtastic_Position p = meshtastic_Position_init_default; // Start with an empty structure
// if localPosition is totally empty, put our last saved position (lite) in there
if (localPosition.latitude_i == 0 && localPosition.longitude_i == 0) {
const meshtastic_PositionLite *cachedSelf = nodeDB->getNodePosition(node->num);
if (cachedSelf)
nodeDB->setLocalPosition(TypeConversions::ConvertToPosition(*cachedSelf));
meshtastic_PositionLite cachedSelf;
if (nodeDB->copyNodePosition(node->num, cachedSelf))
nodeDB->setLocalPosition(TypeConversions::ConvertToPosition(cachedSelf));
}
localPosition.seq_number++;
@@ -446,10 +446,10 @@ int32_t PositionModule::runOnce()
} else if (nodeDB->hasValidPosition(node)) {
lastGpsSend = now;
const meshtastic_PositionLite *selfPos = nodeDB->getNodePosition(node->num);
if (selfPos) {
lastGpsLatitude = selfPos->latitude_i;
lastGpsLongitude = selfPos->longitude_i;
meshtastic_PositionLite selfPos;
if (nodeDB->copyNodePosition(node->num, selfPos)) {
lastGpsLatitude = selfPos.latitude_i;
lastGpsLongitude = selfPos.longitude_i;
}
if (transmitHistory)
@@ -465,10 +465,10 @@ int32_t PositionModule::runOnce()
if (nodeDB->hasValidPosition(node2)) {
// The minimum time (in seconds) that would pass before we are able to send a new position packet.
const meshtastic_PositionLite *selfPos = nodeDB->getNodePosition(node->num);
if (!selfPos)
meshtastic_PositionLite selfPos;
if (!nodeDB->copyNodePosition(node->num, selfPos))
return RUNONCE_INTERVAL; // Defensive: hasValidPosition should imply this is non-null
auto smartPosition = getDistanceTraveledSinceLastSend(*selfPos);
auto smartPosition = getDistanceTraveledSinceLastSend(selfPos);
msSinceLastSend = now - lastGpsSend;
if (smartPosition.hasTraveledOverThreshold &&
@@ -574,10 +574,10 @@ void PositionModule::handleNewPosition()
const meshtastic_NodeInfoLite *node2 = service->refreshLocalMeshNode(); // should guarantee there is now a position
// We limit our GPS broadcasts to a max rate
if (nodeDB->hasValidPosition(node2)) {
const meshtastic_PositionLite *selfPos = nodeDB->getNodePosition(node->num);
if (!selfPos)
meshtastic_PositionLite selfPos;
if (!nodeDB->copyNodePosition(node->num, selfPos))
return;
auto smartPosition = getDistanceTraveledSinceLastSend(*selfPos);
auto smartPosition = getDistanceTraveledSinceLastSend(selfPos);
uint32_t msSinceLastSend = millis() - lastGpsSend;
if (smartPosition.hasTraveledOverThreshold &&
Throttle::execute(
+9 -9
View File
@@ -270,10 +270,10 @@ bool RangeTestModuleRadio::appendFile(const meshtastic_MeshPacket &mp)
fileToAppend.printf("%d,", getFrom(&mp)); // From
fileToAppend.printf("%s,", n ? n->long_name : ""); // Long Name
const meshtastic_PositionLite *senderPos = nodeDB->getNodePosition(getFrom(&mp));
if (senderPos) {
fileToAppend.printf("%f,", senderPos->latitude_i * 1e-7); // Sender Lat
fileToAppend.printf("%f,", senderPos->longitude_i * 1e-7); // Sender Long
meshtastic_PositionLite senderPos;
if (nodeDB->copyNodePosition(getFrom(&mp), senderPos)) {
fileToAppend.printf("%f,", senderPos.latitude_i * 1e-7); // Sender Lat
fileToAppend.printf("%f,", senderPos.longitude_i * 1e-7); // Sender Long
} else {
fileToAppend.printf("0.0,0.0,");
}
@@ -283,11 +283,11 @@ bool RangeTestModuleRadio::appendFile(const meshtastic_MeshPacket &mp)
fileToAppend.printf("%d,", gpsStatus->getAltitude()); // RX Altitude
} else {
// When the phone API is in use, the node info will be updated with position
const meshtastic_PositionLite *usPos = nodeDB->getNodePosition(nodeDB->getNodeNum());
if (usPos) {
fileToAppend.printf("%f,", usPos->latitude_i * 1e-7); // RX Lat
fileToAppend.printf("%f,", usPos->longitude_i * 1e-7); // RX Long
fileToAppend.printf("%d,", usPos->altitude); // RX Altitude
meshtastic_PositionLite usPos;
if (nodeDB->copyNodePosition(nodeDB->getNodeNum(), usPos)) {
fileToAppend.printf("%f,", usPos.latitude_i * 1e-7); // RX Lat
fileToAppend.printf("%f,", usPos.longitude_i * 1e-7); // RX Long
fileToAppend.printf("%d,", usPos.altitude); // RX Altitude
} else {
fileToAppend.printf("0.0,0.0,0,");
}
+4 -4
View File
@@ -251,9 +251,9 @@ int32_t SerialModule::runOnce()
const meshtastic_NodeInfoLite *tempNodeInfo = nodeDB->readNextMeshNode(readIndex);
while (tempNodeInfo != NULL) {
if (nodeInfoLiteHasUser(tempNodeInfo) && nodeDB->hasValidPosition(tempNodeInfo)) {
const meshtastic_PositionLite *pos = nodeDB->getNodePosition(tempNodeInfo->num);
if (pos) {
printWPL(outbuf, sizeof(outbuf), *pos, tempNodeInfo->long_name, true);
meshtastic_PositionLite pos;
if (nodeDB->copyNodePosition(tempNodeInfo->num, pos)) {
printWPL(outbuf, sizeof(outbuf), pos, tempNodeInfo->long_name, true);
serialPrint->printf("%s", outbuf);
}
}
@@ -721,4 +721,4 @@ void SerialModule::processWXSerial()
#endif
return;
}
#endif
#endif
+2 -2
View File
@@ -24,10 +24,10 @@ class StatusMessageModule : public SinglePortModule, private concurrency::OSThre
protected:
/** Called to handle a particular incoming message. The cached most-recent
* status for each node lives on NodeDB; use nodeDB->getNodeStatus(num).
* status for each node lives on NodeDB; use nodeDB->copyNodeStatus(num, ...).
*/
virtual ProcessMessage handleReceived(const meshtastic_MeshPacket &mp) override;
};
extern StatusMessageModule *statusMessageModule;
#endif
#endif
+4 -3
View File
@@ -147,9 +147,10 @@ void WaypointModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state,
const char *statusLine2 = nullptr;
// Distance only needs our own position fix; compass/bearing additionally needs heading.
const meshtastic_PositionLite *ownPos = ourNode ? nodeDB->getNodePosition(ourNode->num) : nullptr;
if (hasOwnPositionFix && ownPos) {
const meshtastic_PositionLite &op = *ownPos;
meshtastic_PositionLite ownPos;
const bool haveOwnPos = ourNode && nodeDB->copyNodePosition(ourNode->num, ownPos);
if (hasOwnPositionFix && haveOwnPos) {
const meshtastic_PositionLite &op = ownPos;
const float d =
GeoCoord::latLongToMeter(DegD(wp.latitude_i), DegD(wp.longitude_i), DegD(op.latitude_i), DegD(op.longitude_i));