Skip to content

Commit 73465d8

Browse files
authored
update to library and example to create robust processing and callbacks for both decoded and encrypted and generic PortNum (#42)
1 parent b1c62a1 commit 73465d8

File tree

7 files changed

+535
-48
lines changed

7 files changed

+535
-48
lines changed

examples/NodeInfoClient/NodeInfoClient.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
1717
*/
1818

19+
// Uncomment the line below to enable debugging
20+
// #define MT_DEBUGGING
21+
1922
#include <Meshtastic.h>
2023

2124
// Pins to use for WiFi; these defaults are for an Adafruit Feather M0 WiFi.
@@ -67,9 +70,6 @@ void setup() {
6770
#endif
6871
Serial.println(" mode");
6972

70-
// Set to true if you want debug messages
71-
mt_set_debug(false);
72-
7373
randomSeed(micros());
7474
}
7575

examples/SendReceiveClient/SendReceiveClient.ino

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
which prints the message to the serial console.
1010
*/
1111

12+
// Uncomment the line below to enable debugging
13+
// #define MT_DEBUGGING
14+
1215
#include <Meshtastic.h>
1316

1417
// Pins to use for WiFi; these defaults are for an Adafruit Feather M0 WiFi.
@@ -23,7 +26,7 @@
2326
#define SERIAL_RX_PIN 13
2427
#define SERIAL_TX_PIN 15
2528
// A different baud rate to communicate with the Meshtastic device can be specified here
26-
#define BAUD_RATE 9600
29+
#define BAUD_RATE 38400
2730

2831
// Send a text message every this many seconds
2932
#define SEND_PERIOD 300
@@ -38,6 +41,58 @@ void connected_callback(mt_node_t *node, mt_nr_progress_t progress) {
3841
not_yet_connected = false;
3942
}
4043

44+
45+
const char* meshtastic_portnum_to_string(meshtastic_PortNum port) {
46+
switch (port) {
47+
case meshtastic_PortNum_UNKNOWN_APP: return "UNKNOWN_APP";
48+
case meshtastic_PortNum_TEXT_MESSAGE_APP: return "TEXT_MESSAGE_APP";
49+
case meshtastic_PortNum_REMOTE_HARDWARE_APP: return "REMOTE_HARDWARE_APP";
50+
case meshtastic_PortNum_POSITION_APP: return "POSITION_APP";
51+
case meshtastic_PortNum_NODEINFO_APP: return "NODEINFO_APP";
52+
case meshtastic_PortNum_ROUTING_APP: return "ROUTING_APP";
53+
case meshtastic_PortNum_ADMIN_APP: return "ADMIN_APP";
54+
case meshtastic_PortNum_TEXT_MESSAGE_COMPRESSED_APP: return "TEXT_MESSAGE_COMPRESSED_APP";
55+
case meshtastic_PortNum_WAYPOINT_APP: return "WAYPOINT_APP";
56+
case meshtastic_PortNum_AUDIO_APP: return "AUDIO_APP";
57+
case meshtastic_PortNum_DETECTION_SENSOR_APP: return "DETECTION_SENSOR_APP";
58+
case meshtastic_PortNum_REPLY_APP: return "REPLY_APP";
59+
case meshtastic_PortNum_IP_TUNNEL_APP: return "IP_TUNNEL_APP";
60+
case meshtastic_PortNum_PAXCOUNTER_APP: return "PAXCOUNTER_APP";
61+
case meshtastic_PortNum_SERIAL_APP: return "SERIAL_APP";
62+
case meshtastic_PortNum_STORE_FORWARD_APP: return "STORE_FORWARD_APP";
63+
case meshtastic_PortNum_RANGE_TEST_APP: return "RANGE_TEST_APP";
64+
case meshtastic_PortNum_TELEMETRY_APP: return "TELEMETRY_APP";
65+
case meshtastic_PortNum_ZPS_APP: return "ZPS_APP";
66+
case meshtastic_PortNum_SIMULATOR_APP: return "SIMULATOR_APP";
67+
case meshtastic_PortNum_TRACEROUTE_APP: return "TRACEROUTE_APP";
68+
case meshtastic_PortNum_NEIGHBORINFO_APP: return "NEIGHBORINFO_APP";
69+
case meshtastic_PortNum_ATAK_PLUGIN: return "ATAK_PLUGIN";
70+
case meshtastic_PortNum_MAP_REPORT_APP: return "MAP_REPORT_APP";
71+
case meshtastic_PortNum_POWERSTRESS_APP: return "POWERSTRESS_APP";
72+
case meshtastic_PortNum_PRIVATE_APP: return "PRIVATE_APP";
73+
case meshtastic_PortNum_ATAK_FORWARDER: return "ATAK_FORWARDER";
74+
case meshtastic_PortNum_MAX: return "MAX";
75+
default: return "UNKNOWN_PORTNUM";
76+
}
77+
}
78+
79+
void displayPubKey(meshtastic_MeshPacket_public_key_t pubKey, char *hex_str) {
80+
for (int i = 0; i < 32; i++) {
81+
sprintf(&hex_str[i * 2], "%02x", (unsigned char)pubKey.bytes[i]);
82+
}
83+
84+
hex_str[64] = '\0'; // Null terminator
85+
}
86+
87+
88+
void encrypted_callback(uint32_t from, uint32_t to, uint8_t channel, meshtastic_MeshPacket_public_key_t pubKey, meshtastic_MeshPacket_encrypted_t *enc_payload) {
89+
Serial.printf("Received an ENCRYPTED callback from: %x to: %x\r\n", from, to);
90+
}
91+
92+
void portnum_callback(uint32_t from, uint32_t to, uint8_t channel, meshtastic_PortNum portNum, meshtastic_Data_payload_t *payload) {
93+
Serial.printf("Received a callback for PortNum %s\r\n", meshtastic_portnum_to_string(portNum));
94+
}
95+
4196
// This callback function will be called whenever the radio receives a text message
4297
void text_message_callback(uint32_t from, uint32_t to, uint8_t channel, const char* text) {
4398
// Do your own thing here. This example just prints the message to the serial console.
@@ -59,8 +114,8 @@ void text_message_callback(uint32_t from, uint32_t to, uint8_t channel, const c
59114
}
60115

61116
void setup() {
62-
// Try for up to five seconds to find a serial port; if not, the show must go on
63-
Serial.begin(9600);
117+
// Try for up to five seconds to find a serial port; if not, the show must gox on
118+
Serial.begin(115200);
64119
while(true) {
65120
if (Serial) break;
66121
if (millis() > 5000) {
@@ -82,16 +137,15 @@ void setup() {
82137
#endif
83138
Serial.println(" mode");
84139

85-
// Set to true if you want debug messages
86-
mt_set_debug(false);
87-
88140
randomSeed(micros());
89141

90142
// Initial connection to the Meshtastic device
91143
mt_request_node_report(connected_callback);
92144

93145
// Register a callback function to be called whenever a text message is received
94146
set_text_message_callback(text_message_callback);
147+
set_portnum_callback(portnum_callback);
148+
set_encrypted_callback(encrypted_callback);
95149
}
96150

97151
void loop() {

src/Meshtastic.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ bool mt_request_node_report(void (*callback)(mt_node_t *, mt_nr_progress_t));
7575
// Set the callback function that gets called when the node receives a text message.
7676
void set_text_message_callback(void (*callback)(uint32_t from, uint32_t to, uint8_t channel, const char * text));
7777

78+
// Set the callback function that gets called when the node receives any other portNum
79+
void set_portnum_callback(void (*callback)(uint32_t from, uint32_t to, uint8_t channel, meshtastic_PortNum port, meshtastic_Data_payload_t *payload));
80+
81+
// Set the callback function that gets called when the node receives an encrypted payload
82+
void set_encrypted_callback(void (*callback)(uint32_t from, uint32_t to, uint8_t channel, meshtastic_MeshPacket_public_key_t pubKey, meshtastic_MeshPacket_encrypted_t *payload));
83+
7884
// Send a text message with *text* as payload, to a destination node (optional), on a certain channel (optional).
7985
bool mt_send_text(const char * text, uint32_t dest = BROADCAST_ADDR, uint8_t channel_index = 0);
8086

src/mt_internals.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33

44
#include "Meshtastic.h"
55

6-
extern bool mt_debugging;
7-
void mt_debug_print(const char * s);
8-
#define d(s) mt_debug_print(s)
6+
#ifdef MT_DEBUGGING
7+
#define d(...) _d(__VA_ARGS__)
8+
#else
9+
#define d(...) do {} while (0)
10+
#endif
11+
12+
void _d(const char * fmt, ...);
913

1014
extern bool mt_wifi_mode;
1115
extern bool mt_serial_mode;

0 commit comments

Comments
 (0)