Skip to content

Commit 6431cd2

Browse files
authored
Merge pull request #1900 from wbijen/feature/contact-filter-by-hops
Add configurable max hops filter for auto-add contacts
2 parents 06ab9f7 + 1d190ad commit 6431cd2

6 files changed

Lines changed: 23 additions & 1 deletion

File tree

examples/companion_radio/DataStore.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ void DataStore::loadPrefsInt(const char *filename, NodePrefs& _prefs, double& no
229229
file.read((uint8_t *)&_prefs.gps_enabled, sizeof(_prefs.gps_enabled)); // 85
230230
file.read((uint8_t *)&_prefs.gps_interval, sizeof(_prefs.gps_interval)); // 86
231231
file.read((uint8_t *)&_prefs.autoadd_config, sizeof(_prefs.autoadd_config)); // 87
232+
file.read((uint8_t *)&_prefs.autoadd_max_hops, sizeof(_prefs.autoadd_max_hops)); // 88
232233

233234
file.close();
234235
}
@@ -265,6 +266,7 @@ void DataStore::savePrefs(const NodePrefs& _prefs, double node_lat, double node_
265266
file.write((uint8_t *)&_prefs.gps_enabled, sizeof(_prefs.gps_enabled)); // 85
266267
file.write((uint8_t *)&_prefs.gps_interval, sizeof(_prefs.gps_interval)); // 86
267268
file.write((uint8_t *)&_prefs.autoadd_config, sizeof(_prefs.autoadd_config)); // 87
269+
file.write((uint8_t *)&_prefs.autoadd_max_hops, sizeof(_prefs.autoadd_max_hops)); // 88
268270

269271
file.close();
270272
}

examples/companion_radio/MyMesh.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ bool MyMesh::shouldOverwriteWhenFull() const {
318318
return (_prefs.autoadd_config & AUTO_ADD_OVERWRITE_OLDEST) != 0;
319319
}
320320

321+
uint8_t MyMesh::getAutoAddMaxHops() const {
322+
return _prefs.autoadd_max_hops;
323+
}
324+
321325
void MyMesh::onContactOverwrite(const uint8_t* pub_key) {
322326
_store->deleteBlobByKey(pub_key, PUB_KEY_SIZE); // delete from storage
323327
if (_serial->isConnected()) {
@@ -1785,12 +1789,16 @@ void MyMesh::handleCmdFrame(size_t len) {
17851789
}
17861790
} else if (cmd_frame[0] == CMD_SET_AUTOADD_CONFIG) {
17871791
_prefs.autoadd_config = cmd_frame[1];
1792+
if (len >= 3) {
1793+
_prefs.autoadd_max_hops = min(cmd_frame[2], (uint8_t)64);
1794+
}
17881795
savePrefs();
1789-
writeOKFrame();
1796+
writeOKFrame();
17901797
} else if (cmd_frame[0] == CMD_GET_AUTOADD_CONFIG) {
17911798
int i = 0;
17921799
out_frame[i++] = RESP_CODE_AUTOADD_CONFIG;
17931800
out_frame[i++] = _prefs.autoadd_config;
1801+
out_frame[i++] = _prefs.autoadd_max_hops;
17941802
_serial->writeFrame(out_frame, i);
17951803
} else if (cmd_frame[0] == CMD_GET_ALLOWED_REPEAT_FREQ) {
17961804
int i = 0;

examples/companion_radio/MyMesh.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class MyMesh : public BaseChatMesh, public DataStoreHost {
119119
bool isAutoAddEnabled() const override;
120120
bool shouldAutoAddContactType(uint8_t type) const override;
121121
bool shouldOverwriteWhenFull() const override;
122+
uint8_t getAutoAddMaxHops() const override;
122123
void onContactsFull() override;
123124
void onContactOverwrite(const uint8_t* pub_key) override;
124125
bool onContactPathRecv(ContactInfo& from, uint8_t* in_path, uint8_t in_path_len, uint8_t* out_path, uint8_t out_path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override;

examples/companion_radio/NodePrefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ struct NodePrefs { // persisted to file
3030
uint8_t autoadd_config; // bitmask for auto-add contacts config
3131
uint8_t client_repeat;
3232
uint8_t path_hash_mode; // which path mode to use when sending
33+
uint8_t autoadd_max_hops; // 0 = no limit, 1 = direct (0 hops), N = up to N-1 hops (max 64)
3334
};

src/helpers/BaseChatMesh.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ void BaseChatMesh::onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id,
141141
return;
142142
}
143143

144+
// check hop limit for new contacts (0 = no limit, 1 = direct (0 hops), N = up to N-1 hops)
145+
uint8_t max_hops = getAutoAddMaxHops();
146+
if (max_hops > 0 && packet->getPathHashCount() >= max_hops) {
147+
ContactInfo ci;
148+
populateContactFromAdvert(ci, id, parser, timestamp);
149+
onDiscoveredContact(ci, true, packet->path_len, packet->path); // let UI know
150+
return;
151+
}
152+
144153
from = allocateContactSlot();
145154
if (from == NULL) {
146155
ContactInfo ci;

src/helpers/BaseChatMesh.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class BaseChatMesh : public mesh::Mesh {
9898
virtual bool shouldAutoAddContactType(uint8_t type) const { return true; }
9999
virtual void onContactsFull() {};
100100
virtual bool shouldOverwriteWhenFull() const { return false; }
101+
virtual uint8_t getAutoAddMaxHops() const { return 0; } // 0 = no limit, 1 = direct (0 hops), N = up to N-1 hops
101102
virtual void onContactOverwrite(const uint8_t* pub_key) {};
102103
virtual void onDiscoveredContact(ContactInfo& contact, bool is_new, uint8_t path_len, const uint8_t* path) = 0;
103104
virtual ContactInfo* processAck(const uint8_t *data) = 0;

0 commit comments

Comments
 (0)