Skip to content

Commit

Permalink
Merge branch 'master' into ublox-m10-deepsleep-power-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
thebentern authored Jun 9, 2024
2 parents f9133b8 + 24458a7 commit e677ada
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 13 deletions.
16 changes: 10 additions & 6 deletions src/mesh/MeshModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ meshtastic_MeshPacket *MeshModule::allocAckNak(meshtastic_Routing_Error err, Nod

meshtastic_MeshPacket *MeshModule::allocErrorResponse(meshtastic_Routing_Error err, const meshtastic_MeshPacket *p)
{
auto r = allocAckNak(err, getFrom(p), p->id, p->channel);
// If the original packet couldn't be decoded, use the primary channel
uint8_t channelIndex =
p->which_payload_variant == meshtastic_MeshPacket_decoded_tag ? p->channel : channels.getPrimaryIndex();
auto r = allocAckNak(err, getFrom(p), p->id, channelIndex);

setReplyTo(r, *p);

Expand Down Expand Up @@ -114,13 +117,13 @@ void MeshModule::callModules(meshtastic_MeshPacket &mp, RxSource src)
/// Also: if a packet comes in on the local PC interface, we don't check for bound channels, because it is TRUSTED and
/// it needs to to be able to fetch the initial admin packets without yet knowing any channels.

bool rxChannelOk = !pi.boundChannel || (mp.from == 0) || (strcasecmp(ch->settings.name, pi.boundChannel) == 0);
bool rxChannelOk = !pi.boundChannel || (mp.from == 0) || (ch && strcasecmp(ch->settings.name, pi.boundChannel) == 0);

if (!rxChannelOk) {
// no one should have already replied!
assert(!currentReply);

if (mp.decoded.want_response) {
if (isDecoded && mp.decoded.want_response) {
printPacket("packet on wrong channel, returning error", &mp);
currentReply = pi.allocErrorResponse(meshtastic_Routing_Error_NOT_AUTHORIZED, &mp);
} else
Expand All @@ -138,7 +141,8 @@ void MeshModule::callModules(meshtastic_MeshPacket &mp, RxSource src)
// because currently when the phone sends things, it sends things using the local node ID as the from address. A
// better solution (FIXME) would be to let phones have their own distinct addresses and we 'route' to them like
// any other node.
if (mp.decoded.want_response && toUs && (getFrom(&mp) != ourNodeNum || mp.to == ourNodeNum) && !currentReply) {
if (isDecoded && mp.decoded.want_response && toUs && (getFrom(&mp) != ourNodeNum || mp.to == ourNodeNum) &&
!currentReply) {
pi.sendResponse(mp);
ignoreRequest = ignoreRequest || pi.ignoreRequest; // If at least one module asks it, we may ignore a request
LOG_INFO("Asked module '%s' to send a response\n", pi.name);
Expand All @@ -163,7 +167,7 @@ void MeshModule::callModules(meshtastic_MeshPacket &mp, RxSource src)
pi.currentRequest = NULL;
}

if (mp.decoded.want_response && toUs) {
if (isDecoded && mp.decoded.want_response && toUs) {
if (currentReply) {
printPacket("Sending response", currentReply);
service.sendToMesh(currentReply);
Expand All @@ -183,7 +187,7 @@ void MeshModule::callModules(meshtastic_MeshPacket &mp, RxSource src)
}
}

if (!moduleFound) {
if (!moduleFound && isDecoded) {
LOG_DEBUG("No modules interested in portnum=%d, src=%s\n", mp.decoded.portnum,
(src == RX_SRC_LOCAL) ? "LOCAL" : "REMOTE");
}
Expand Down
64 changes: 61 additions & 3 deletions src/mesh/RF95Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,48 @@
// if you set power to something higher than 17 or 20 you might fry your board.

#define POWER_DEFAULT 17 // How much power to use if the user hasn't set a power level
#ifdef RADIOMASTER_900_BANDIT_NANO
// Structure to hold DAC and DB values
typedef struct {
uint8_t dac;
uint8_t db;
} DACDB;

// Interpolation function
DACDB interpolate(uint8_t dbm, uint8_t dbm1, uint8_t dbm2, DACDB val1, DACDB val2) {
DACDB result;
double fraction = (double)(dbm - dbm1) / (dbm2 - dbm1);
result.dac = (uint8_t)(val1.dac + fraction * (val2.dac - val1.dac));
result.db = (uint8_t)(val1.db + fraction * (val2.db - val1.db));
return result;
}

// Function to find the correct DAC and DB values based on dBm using interpolation
DACDB getDACandDB(uint8_t dbm) {
// Predefined values
static const struct {
uint8_t dbm;
DACDB values;
} dbmToDACDB[] = {
{20, {168, 2}}, // 100mW
{24, {148, 6}}, // 250mW
{27, {128, 9}}, // 500mW
{30, {90, 12}} // 1000mW
};
const int numValues = sizeof(dbmToDACDB) / sizeof(dbmToDACDB[0]);

// Find the interval dbm falls within and interpolate
for (int i = 0; i < numValues - 1; i++) {
if (dbm >= dbmToDACDB[i].dbm && dbm <= dbmToDACDB[i + 1].dbm) {
return interpolate(dbm, dbmToDACDB[i].dbm, dbmToDACDB[i + 1].dbm, dbmToDACDB[i].values, dbmToDACDB[i + 1].values);
}
}

// Return a default value if no match is found and default to 100mW
DACDB defaultValue = {168, 2};
return defaultValue;
}
#endif

RF95Interface::RF95Interface(LockingArduinoHal *hal, RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst,
RADIOLIB_PIN_TYPE busy)
Expand Down Expand Up @@ -52,9 +94,16 @@ bool RF95Interface::init()
{
RadioLibInterface::init();

#ifdef RADIOMASTER_900_BANDIT_NANO
// DAC and DB values based on dBm using interpolation
DACDB dacDbValues = getDACandDB(power);
int8_t powerDAC = dacDbValues.dac;
power = dacDbValues.db;
#endif

if (power > RF95_MAX_POWER) // This chip has lower power limits than some
power = RF95_MAX_POWER;

limitPower();

iface = lora = new RadioLibRF95(&module);
Expand All @@ -67,7 +116,13 @@ bool RF95Interface::init()
// enable PA
#ifdef RF95_PA_EN
#if defined(RF95_PA_DAC_EN)
dacWrite(RF95_PA_EN, RF95_PA_LEVEL);
#ifdef RADIOMASTER_900_BANDIT_NANO
// Use calculated DAC value
dacWrite(RF95_PA_EN, powerDAC);
#else
// Use Value set in /*/variant.h
dacWrite(RF95_PA_EN, RF95_PA_LEVEL);
#endif
#endif
#endif

Expand Down Expand Up @@ -107,6 +162,9 @@ bool RF95Interface::init()
LOG_INFO("Frequency set to %f\n", getFreq());
LOG_INFO("Bandwidth set to %f\n", bw);
LOG_INFO("Power output set to %d\n", power);
#ifdef RADIOMASTER_900_BANDIT_NANO
LOG_INFO("DAC output set to %d\n", powerDAC);
#endif

if (res == RADIOLIB_ERR_NONE)
res = lora->setCRC(RADIOLIB_SX126X_LORA_CRC_ON);
Expand Down Expand Up @@ -259,4 +317,4 @@ bool RF95Interface::sleep()
#endif

return true;
}
}
28 changes: 24 additions & 4 deletions src/modules/TraceRouteModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,37 @@ bool TraceRouteModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, m
void TraceRouteModule::alterReceivedProtobuf(meshtastic_MeshPacket &p, meshtastic_RouteDiscovery *r)
{
auto &incoming = p.decoded;
// Only append an ID for the request (one way) and if we are not the destination (the reply will have our NodeNum already)
if (!incoming.request_id && p.to != nodeDB->getNodeNum()) {
appendMyID(r);
printRoute(r, p.from, NODENUM_BROADCAST);
// Only append IDs for the request (one way)
if (!incoming.request_id) {
// Insert unknown hops if necessary
insertUnknownHops(p, r);

// Don't add ourselves if we are the destination (the reply will have our NodeNum already)
if (p.to != nodeDB->getNodeNum()) {
appendMyID(r);
printRoute(r, p.from, NODENUM_BROADCAST);
}
// Set updated route to the payload of the to be flooded packet
p.decoded.payload.size =
pb_encode_to_bytes(p.decoded.payload.bytes, sizeof(p.decoded.payload.bytes), &meshtastic_RouteDiscovery_msg, r);
}
}

void TraceRouteModule::insertUnknownHops(meshtastic_MeshPacket &p, meshtastic_RouteDiscovery *r)
{
// Only insert unknown hops if hop_start is valid
if (p.hop_start != 0 && p.hop_limit <= p.hop_start) {
uint8_t hopsTaken = p.hop_start - p.hop_limit;
int8_t diff = hopsTaken - r->route_count;
for (uint8_t i = 0; i < diff; i++) {
if (r->route_count < sizeof(r->route) / sizeof(r->route[0])) {
r->route[r->route_count] = NODENUM_BROADCAST; // This will represent an unknown hop
r->route_count += 1;
}
}
}
}

void TraceRouteModule::appendMyID(meshtastic_RouteDiscovery *updated)
{
// Length of route array can normally not be exceeded due to the max. hop_limit of 7
Expand Down
3 changes: 3 additions & 0 deletions src/modules/TraceRouteModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class TraceRouteModule : public ProtobufModule<meshtastic_RouteDiscovery>
void alterReceivedProtobuf(meshtastic_MeshPacket &p, meshtastic_RouteDiscovery *r) override;

private:
// Call to add unknown hops (e.g. when a node couldn't decrypt it) to the route based on hopStart and current hopLimit
void insertUnknownHops(meshtastic_MeshPacket &p, meshtastic_RouteDiscovery *r);

// Call to add your ID to the route array of a RouteDiscovery message
void appendMyID(meshtastic_RouteDiscovery *r);

Expand Down

0 comments on commit e677ada

Please sign in to comment.