From f861d6f12d22601c17c621de3c573e1dda7c590e Mon Sep 17 00:00:00 2001 From: Jan Muller Date: Tue, 17 Dec 2024 13:43:16 +0100 Subject: [PATCH 1/2] Add Channel callback --- src/Meshtastic.h | 16 +++++++++++++++- src/mt_protocol.cpp | 42 ++++++++++++++++++++++++++++++++++++------ src/mt_serial.cpp | 1 + 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/Meshtastic.h b/src/Meshtastic.h index 3cc9fbe..b4e5b61 100644 --- a/src/Meshtastic.h +++ b/src/Meshtastic.h @@ -10,6 +10,7 @@ #define MAX_USER_ID_LEN (sizeof(meshtastic_User().id) - 1) #define MAX_LONG_NAME_LEN (sizeof(meshtastic_User().long_name) - 1) #define MAX_SHORT_NAME_LEN (sizeof(meshtastic_User().short_name) - 1) +#define MAX_ACCOUNT_NAME (sizeof(meshtastic_Channel().settings.name) - 1) #define BAUD_DEFAULT 9600 #define BROADCAST_ADDR 0xFFFFFFFF @@ -36,8 +37,21 @@ typedef struct { float voltage; float channel_utilization; float air_util_tx; + bool is_favorite; } mt_node_t; +typedef enum mt_channel_role { + DISABLED = 0, + PRIMARY = 1, + SECONDARY = 2 +} mt_channel_role; + +typedef struct { + int8_t index; + char name[MAX_ACCOUNT_NAME]; + mt_channel_role role; +} mt_channel_t; + // Initialize, using wifi to connect to the MT radio void mt_wifi_init(int8_t cs_pin, int8_t irq_pin, int8_t reset_pin, int8_t enable_pin, const char * ssid, const char * password); @@ -70,7 +84,7 @@ typedef enum { // // Returns true if we were able to request the report, false if we couldn't // even do that. -bool mt_request_node_report(void (*callback)(mt_node_t *, mt_nr_progress_t)); +bool mt_request_node_report(void (*callback)(mt_node_t *, mt_nr_progress_t), void (*callbackChannels)(mt_channel_t *, mt_nr_progress_t) = NULL); // Set the callback function that gets called when the node receives a text message. void set_text_message_callback(void (*callback)(uint32_t from, uint32_t to, uint8_t channel, const char * text)); diff --git a/src/mt_protocol.cpp b/src/mt_protocol.cpp index 4633e9d..eca7a6a 100644 --- a/src/mt_protocol.cpp +++ b/src/mt_protocol.cpp @@ -32,10 +32,13 @@ uint32_t want_config_id = 0; // Node number of the MT node hosting our WiFi uint32_t my_node_num = 0; + bool mt_debugging = false; void (*text_message_callback)(uint32_t from, uint32_t to, uint8_t channel, const char* text) = NULL; void (*node_report_callback)(mt_node_t *, mt_nr_progress_t) = NULL; +void (*channel_callback)(mt_channel_t *, mt_nr_progress_t) = NULL; mt_node_t node; +mt_channel_t channel; bool mt_wifi_mode = false; bool mt_serial_mode = false; @@ -87,7 +90,7 @@ bool _mt_send_toRadio(meshtastic_ToRadio toRadio) { } // Request a node report from our MT -bool mt_request_node_report(void (*callback)(mt_node_t *, mt_nr_progress_t)) { +bool mt_request_node_report(void (*callback)(mt_node_t *, mt_nr_progress_t), void (*callbackChannels)(mt_channel_t *, mt_nr_progress_t)) { meshtastic_ToRadio toRadio = meshtastic_ToRadio_init_default; toRadio.which_payload_variant = meshtastic_ToRadio_want_config_id_tag; want_config_id = random(0x7FffFFff); // random() can't handle anything bigger @@ -100,7 +103,10 @@ bool mt_request_node_report(void (*callback)(mt_node_t *, mt_nr_progress_t)) { bool rv = _mt_send_toRadio(toRadio); - if (rv) node_report_callback = callback; + if (rv) { + node_report_callback = callback; + channel_callback = callbackChannels; + } return rv; } @@ -119,10 +125,6 @@ bool mt_send_text(const char * text, uint32_t dest, uint8_t channel_index) { toRadio.which_payload_variant = meshtastic_ToRadio_packet_tag; toRadio.packet = meshPacket; - Serial.print("Sending text message '"); - Serial.print(text); - Serial.print("' to "); - Serial.println(dest); return _mt_send_toRadio(toRadio); } @@ -147,6 +149,22 @@ bool handle_my_info(meshtastic_MyNodeInfo *myNodeInfo) { return true; } +bool handle_channels(meshtastic_Channel *tChannel) { + if (channel_callback == NULL) { + d("Got a channel, but we don't have a callback"); + return false; + } + if (tChannel->has_settings) { + memcpy(channel.name, tChannel->settings.name, MAX_ACCOUNT_NAME); + } + + channel.index = tChannel->index; + channel.role = static_cast(tChannel->role); + + channel_callback(&channel, MT_NR_IN_PROGRESS); + return true; +} + bool handle_node_info(meshtastic_NodeInfo *nodeInfo) { if (node_report_callback == NULL) { d("Got a node report, but we don't have a callback"); @@ -189,6 +207,7 @@ bool handle_node_info(meshtastic_NodeInfo *nodeInfo) { node.channel_utilization = NAN; node.air_util_tx = NAN; } + node.is_favorite = nodeInfo->is_favorite; node_report_callback(&node, MT_NR_IN_PROGRESS); return true; @@ -202,8 +221,16 @@ bool handle_config_complete_id(uint32_t now, uint32_t config_complete_id) { want_config_id = 0; node_report_callback(NULL, MT_NR_DONE); node_report_callback = NULL; + + if (channel_callback != NULL) { + channel_callback(NULL, MT_NR_DONE); + } + channel_callback = NULL; } else { node_report_callback(NULL, MT_NR_INVALID); // but return true, since it was still a valid packet + if (channel_callback != NULL) { + channel_callback(NULL, MT_NR_INVALID); + } } return true; } @@ -245,6 +272,9 @@ bool handle_packet(uint32_t now, size_t payload_len) { } switch (fromRadio.which_payload_variant) { + + case meshtastic_FromRadio_channel_tag: + return handle_channels(&fromRadio.channel); case meshtastic_FromRadio_my_info_tag: return handle_my_info(&fromRadio.my_info); case meshtastic_FromRadio_node_info_tag: diff --git a/src/mt_serial.cpp b/src/mt_serial.cpp index 903f2eb..c3ac002 100644 --- a/src/mt_serial.cpp +++ b/src/mt_serial.cpp @@ -58,3 +58,4 @@ size_t mt_serial_check_radio(char * buf, size_t space_left) { } return bytes_read; } + From 3c5583845124f3a567d8273151045c1a0e957f38 Mon Sep 17 00:00:00 2001 From: Jan Muller Date: Thu, 26 Dec 2024 09:53:23 +0100 Subject: [PATCH 2/2] Return back Serial.prtint in mt_send_text function. --- src/Meshtastic.h | 6 +++--- src/mt_protocol.cpp | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Meshtastic.h b/src/Meshtastic.h index b4e5b61..6564c7b 100644 --- a/src/Meshtastic.h +++ b/src/Meshtastic.h @@ -41,9 +41,9 @@ typedef struct { } mt_node_t; typedef enum mt_channel_role { - DISABLED = 0, - PRIMARY = 1, - SECONDARY = 2 + CR_DISABLED = 0, + CR_PRIMARY = 1, + CR_SECONDARY = 2 } mt_channel_role; typedef struct { diff --git a/src/mt_protocol.cpp b/src/mt_protocol.cpp index eca7a6a..c20ce72 100644 --- a/src/mt_protocol.cpp +++ b/src/mt_protocol.cpp @@ -125,6 +125,13 @@ bool mt_send_text(const char * text, uint32_t dest, uint8_t channel_index) { toRadio.which_payload_variant = meshtastic_ToRadio_packet_tag; toRadio.packet = meshPacket; + if (mt_debugging) { + Serial.print("Sending text message '"); + Serial.print(text); + Serial.print("' to "); + Serial.println(dest); + } + return _mt_send_toRadio(toRadio); }