Skip to content

Instantly share code, notes, and snippets.

@deviousasti
Last active January 24, 2020 18:58
Show Gist options
  • Save deviousasti/ced9041be0c54829a69e9cc18a39f7c4 to your computer and use it in GitHub Desktop.
Save deviousasti/ced9041be0c54829a69e9cc18a39f7c4 to your computer and use it in GitHub Desktop.
Decoding a oneof case in nanopb
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