Last active
January 24, 2020 18:58
-
-
Save deviousasti/ced9041be0c54829a69e9cc18a39f7c4 to your computer and use it in GitHub Desktop.
Decoding a oneof case in nanopb
This file contains hidden or 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
typedef struct pb_union_s | |
{ | |
const uint32_t tag; | |
const pb_msgdesc_t* submsg_desc; | |
pb_istream_t stream; | |
} pb_union_t; | |
const pb_union_t getUnionType(uint8_t buffer[], size_t size) | |
{ | |
pb_istream_t stream = pb_istream_from_buffer(buffer, size); | |
pb_wire_type_t wire_type; | |
uint32_t tag; | |
bool eof; | |
while (pb_decode_tag(&stream, &wire_type, &tag, &eof)) | |
{ | |
if (wire_type == PB_WT_STRING) | |
{ | |
pb_field_iter_t iter; | |
if (pb_field_iter_begin(&iter, fields, NULL) && | |
pb_field_iter_find(&iter, tag)) | |
{ | |
//Found oneof | |
pb_union_t unionType = { tag, iter.submsg_desc, stream }; | |
return unionType; | |
} | |
} | |
//Next | |
pb_skip_field(&stream, wire_type); | |
} | |
return { 0, 0, 0 }; | |
} | |
bool decodeUnion(void* message, pb_union_t& unionType) | |
{ | |
pb_istream_t substream; | |
bool status; | |
if (!pb_make_string_substream(&unionType.stream, &substream)) | |
return false; | |
status = pb_decode(&substream, unionType.submsg_desc, message); | |
pb_close_string_substream(&unionType.stream, &substream); | |
return status; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment