Skip to content

Commit

Permalink
Added decode
Browse files Browse the repository at this point in the history
  • Loading branch information
thebentern committed Jan 25, 2024
1 parent e8c9baf commit 66460e9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/meshtastic-pb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <pb_decode.h>
#include <pb_encode.h>

/// helper function for encoding a record as a protobuf, any failures to encode are fatal and we will panic
/// returns the encoded packet size
size_t pb_encode_to_bytes(uint8_t *destbuf, size_t destbufsize, const pb_msgdesc_t *fields, const void *src_struct)
{
pb_ostream_t stream = pb_ostream_from_buffer(destbuf, destbufsize);
if (!pb_encode(&stream, fields, src_struct)) {
Serial.println("Panic: can't encode protobuf reason='%s'\n", PB_GET_ERROR(&stream));
} else {
return stream.bytes_written;
}
}

/// helper function for decoding a record as a protobuf, we will return false if the decoding failed
bool pb_decode_from_bytes(const uint8_t *srcbuf, size_t srcbufsize, const pb_msgdesc_t *fields, void *dest_struct)
{
pb_istream_t stream = pb_istream_from_buffer(srcbuf, srcbufsize);
if (!pb_decode(&stream, fields, dest_struct)) {
Serial.println("Can't decode protobuf reason='%s', pb_msgdesc %p\n", PB_GET_ERROR(&stream), fields);
return false;
} else {
return true;
}
}
9 changes: 7 additions & 2 deletions src/mt_protocol.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "mt_internals.h"
#include "meshtastic-pb.h"

// Magic number at the start of all MT packets
#define MT_MAGIC_0 0x94
Expand Down Expand Up @@ -191,14 +192,18 @@ bool handle_config_complete_id(uint32_t now, uint32_t config_complete_id) {
return true;
}

if (mp.which_payload_variant == meshtastic_MeshPacket_decoded_tag && mp.decoded.portnum == ourPortNum) {


bool handle_mesh_packet(meshtastic_MeshPacket *meshPacket) {
if (meshPacket->which_payload_variant == meshtastic_MeshPacket_decoded_tag) {
if (meshPacket->decoded.portnum == meshtastic_PortNum_TEXT_MESSAGE_APP) {
if (text_message_callback != NULL)
text_message_callback(meshPacket->from, (const char*)meshPacket->decoded.payload.bytes);
} else if (meshPacket->decoded.portnum == meshtastic_PortNum_TELEMETRY_APP) {
if (telemetry_callback != NULL)
telemetry_callback(meshPacket->from, );
meshtastic_Telemetry *telemetry = {0};
if (telemetry_callback != NULL && pb_decode_from_bytes(meshPacket->decoded.payload.bytes, meshPacket->decoded.payload.bytes, &meshtastic_Telemetry_msg, &telemetry))
telemetry_callback(meshPacket->from, telemetry);
} else {
// TODO handle other portnums
return false;
Expand Down

0 comments on commit 66460e9

Please sign in to comment.