Skip to content

Commit 74f136b

Browse files
authored
Merge pull request #1068 from fdlamotte/sensor_control_data
sensor: copy control data code from repeater
2 parents ab0721d + 0682503 commit 74f136b

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

examples/simple_sensor/SensorMesh.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,38 @@ bool SensorMesh::handleIncomingMsg(ClientInfo& from, uint32_t timestamp, uint8_t
617617
return false;
618618
}
619619

620+
#define CTL_TYPE_NODE_DISCOVER_REQ 0x80
621+
#define CTL_TYPE_NODE_DISCOVER_RESP 0x90
622+
623+
void SensorMesh::onControlDataRecv(mesh::Packet* packet) {
624+
uint8_t type = packet->payload[0] & 0xF0; // just test upper 4 bits
625+
if (type == CTL_TYPE_NODE_DISCOVER_REQ && packet->payload_len >= 6) {
626+
// TODO: apply rate limiting to these!
627+
int i = 1;
628+
uint8_t filter = packet->payload[i++];
629+
uint32_t tag;
630+
memcpy(&tag, &packet->payload[i], 4); i += 4;
631+
uint32_t since;
632+
if (packet->payload_len >= i+4) { // optional since field
633+
memcpy(&since, &packet->payload[i], 4); i += 4;
634+
} else {
635+
since = 0;
636+
}
637+
638+
if ((filter & (1 << ADV_TYPE_SENSOR)) != 0 && _prefs.discovery_mod_timestamp >= since) {
639+
uint8_t data[6 + PUB_KEY_SIZE];
640+
data[0] = CTL_TYPE_NODE_DISCOVER_RESP | ADV_TYPE_SENSOR; // low 4-bits for node type
641+
data[1] = packet->_snr; // let sender know the inbound SNR ( x 4)
642+
memcpy(&data[2], &tag, 4); // include tag from request, for client to match to
643+
memcpy(&data[6], self_id.pub_key, PUB_KEY_SIZE);
644+
auto resp = createControlData(data, sizeof(data));
645+
if (resp) {
646+
sendZeroHop(resp, getRetransmitDelay(resp)); // apply random delay, as multiple nodes can respond to this
647+
}
648+
}
649+
}
650+
}
651+
620652
bool SensorMesh::onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) {
621653
int i = matching_peer_indexes[sender_idx];
622654
if (i < 0 || i >= acl.getNumClients()) {

examples/simple_sensor/SensorMesh.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class SensorMesh : public mesh::Mesh, public CommonCLICallbacks {
125125
void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) override;
126126
void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) override;
127127
bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override;
128+
void onControlDataRecv(mesh::Packet* packet) override;
128129
void onAckRecv(mesh::Packet* packet, uint32_t ack_crc) override;
129130
virtual bool handleIncomingMsg(ClientInfo& from, uint32_t timestamp, uint8_t* data, uint flags, size_t len);
130131
void sendAckTo(const ClientInfo& dest, uint32_t ack_hash);

0 commit comments

Comments
 (0)