diff --git a/src/meshtastic-pb.h b/src/meshtastic-pb.h new file mode 100644 index 0000000..9f9c6ca --- /dev/null +++ b/src/meshtastic-pb.h @@ -0,0 +1,26 @@ +#include +#include + +/// 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; + } +} \ No newline at end of file diff --git a/src/mt_protocol.cpp b/src/mt_protocol.cpp index 67adee1..c1b09c2 100644 --- a/src/mt_protocol.cpp +++ b/src/mt_protocol.cpp @@ -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 @@ -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;