Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Channel callback + reading isFavourite #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Meshtastic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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));
Expand Down
42 changes: 36 additions & 6 deletions src/mt_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

Expand All @@ -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 '");
GUVWAF marked this conversation as resolved.
Show resolved Hide resolved
Serial.print(text);
Serial.print("' to ");
Serial.println(dest);
return _mt_send_toRadio(toRadio);
}

Expand All @@ -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<mt_channel_role>(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");
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions src/mt_serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ size_t mt_serial_check_radio(char * buf, size_t space_left) {
}
return bytes_read;
}