-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8c9baf
commit 66460e9
Showing
2 changed files
with
33 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters