Problem with extensions

136 views
Skip to first unread message

vasilyi...@gmail.com

unread,
May 14, 2014, 9:29:19 AM5/14/14
to nan...@googlegroups.com
Hi. I've just started working with nanopb. I have the extended message structure, next one:

message Success {
required int32 int1st = 1;
optional int32 int2nd = 2;
optional float float1st = 3;
optional float float2nd = 4;
extensions 10 to 100;
}

extend Success {
optional int32 newValue = 10 [default=1];
}

The question is - how to set up or read the newValue field in C ? Just can't find it in help

Petteri Aimonen

unread,
May 14, 2014, 2:27:07 PM5/14/14
to nan...@googlegroups.com
Hi,

> The question is - how to set up or read the newValue field in C ? Just can't find it in help

This test case could act as an example:
https://code.google.com/p/nanopb/source/browse/tests/extensions/

Ask if there is still something unclear. I admit that the documentation
on the extensions is quite lacking at the moment.

--
Petteri

vasilyi...@gmail.com

unread,
May 15, 2014, 2:22:12 AM5/15/14
to nan...@googlegroups.com
Thank you, you really helped :)

One more question - in callbacks example user's callback functions defined for each field. But it's really necessary? I mean if i have one repeated field in my message, can i define just one custom encode function? And as i see callback function must call two encoding functions - for tag and for value. So, if i want to encode in my repeated field for example 5 values then that field will looks like stream with structure: field tag, value 1, field tag, value 2... ?

Petteri Aimonen

unread,
May 15, 2014, 2:33:04 AM5/15/14
to nan...@googlegroups.com
Hi,

> One more question - in callbacks example user's callback functions
> defined for each field. But it's really necessary? I mean if i have
> one repeated field in my message, can i define just one custom encode
> function?

Yeah, you can have some fields as callbacks and other fields as static.
The callback test case just has every field as callback because it has
to test all the types.

> And as i see callback function must call two encoding
> functions - for tag and for value. So, if i want to encode in my
> repeated field for example 5 values then that field will looks like
> stream with structure: field tag, value 1, field tag, value 2... ?

Typically yeah.

The callback function requires some amount of understanding of the
protobuf wire format, which is fortunately quite simple:
https://developers.google.com/protocol-buffers/docs/encoding

Normal repeated fields are like you said:
tag, value 1, tag, value 2.

There also exists a packed format which is:
tag, length, value 1, value 2, value 3, value 4, ...

Either can be used in the callbacks. The packed format is somewhat
smaller.

--
Petteri

vasilyi...@gmail.com

unread,
May 15, 2014, 2:50:47 AM5/15/14
to nan...@googlegroups.com
And as i understood if i want to go with packed field i need use pb_encode_tag instead pb_encode_tag_for_field ? And the functional scheme of encoding will be something like this:

pb_encode_tag(...);
pb_encode_varint(stream, sizeof(fieldtype)*n);
for(i = 0; i<n; i++) {
pb_encode_varint(stream, value[i]);
}

vasilyi...@gmail.com

unread,
May 15, 2014, 3:21:54 AM5/15/14
to nan...@googlegroups.com, vasilyi...@gmail.com
BTW, it's not so obvious how to decode repeated field when we have more than one value. I could imagine that when we've using packed format values in stream are going sequentially is possible to use low-level stream reading, but how it must work with usual repeated field?
Message has been deleted

Petteri Aimonen

unread,
May 15, 2014, 3:54:16 AM5/15/14
to nan...@googlegroups.com
Hi,
Nanopb handles the difference between packed/unpacked fields in the
decoder. Per the protobuf specification, decoders must be able to handle
either type of data, no matter whether [packed=true] is specified or
not.

The simplest thing you can do in a decoder callback is to read just one
value. The callback will be called once for each value, and everything
works the same for both packed and unpacked fields.

If you want to speed-optimize for packed fields, you can also read until
the stream has bytes_left=0. This avoids the overhead of multiple
callback calls.

--
Petteri


Petteri Aimonen

unread,
May 15, 2014, 3:57:32 AM5/15/14
to nan...@googlegroups.com
Hi,

> And as i understood if i want to go with packed field i need use
> pb_encode_tag instead pb_encode_tag_for_field ?

Yes. And specify PB_WT_STRING.

> And the functional scheme of encoding will be something like this:
> pb_encode_varint(stream, sizeof(fieldtype)*n);

This line is wrong, because sizeof(fieldtype) is not the same as the
size of the encoded varints. Currently the only way to know the size of
the varints is to encode them into a dummy stream first. You can check
pb_encode.c around line 130 in encode_array() to see how it is done.

--
Petteri

vasilyi...@gmail.com

unread,
May 15, 2014, 4:03:47 AM5/15/14
to nan...@googlegroups.com
Thanks :) Keep developing nanopb, it's really useful

adity...@gmail.com

unread,
Mar 20, 2015, 11:21:49 AM3/20/15
to nan...@googlegroups.com, vasilyi...@gmail.com
Hi Vasil,

I am doing something similar, Can you post the encoded and decoded pb code for the message extended message you listed and got it working. It will be great help for me.
Reply all
Reply to author
Forward
0 new messages