Callbacks Repeated Field

558 views
Skip to first unread message

geekco...@gmail.com

unread,
Jun 6, 2019, 11:22:37 AM6/6/19
to nanopb
Hi Everyone,

I have the following .proto file:

message Cluster {
required int32 id = 1;
repeated int32 xaxis = 2;
}
message ClusterArray {
repeated Cluster clusters = 1;
}

I have created a function in "function_callback.h" to handle and then encode values in encode_callbacks.c for the " int32 id "

I am really confused on how to implement the REPEATED INT32 XAXIS as If I use the same function in function_callbacks.h it gives error that I cannot assign an array to pb_callbacks_t type.

I cannot define the size of it in .proto file as I dont know the size so I have to use callbacks.

Can someone help me in this regard ?? Looking forward to responses !

callbacks.proto
encode_callbacks.c
function_callback.h

Petteri Aimonen

unread,
Jun 6, 2019, 2:57:33 PM6/6/19
to nan...@googlegroups.com
Hi,

> I am really confused on how to implement the REPEATED INT32 XAXIS as
> If I use the same function in function_callbacks.h it gives error that
> I cannot assign an array to pb_callbacks_t type.

So here you are encoding one of the submessages:

> if (!pb_encode_submessage(ostream, Cluster_fields, &(source->rep[i])))
> {
> const char * error = PB_GET_ERROR(ostream);
> printf("SimpleMessage_encode_numbers error: %s\n", error);
> return false;
> }

You could do before pb_encode_submessage() call something like:

Cluster *submsg = &(source->rep[i]);
submsg->xaxis.arg = &(source->xaxis_data[i]);
submsg->xaxis.func = xaxis_callback_func;

and xaxis_callback_func would write the array data similar to e.g. this
test function write_repeated_varint():
https://github.com/nanopb/nanopb/blob/master/tests/alltypes_callback/encode_alltypes_callback.c#L57

Basically think of pb_encode_submessage() as if it was just a normal
pb_encode(). Before calling it, setup the callbacks that the submessage
contains.

--

Notes about alternative methods if this gets too complex:

1) If your purpose is to encode only, you can also define
type:FT_POINTER for the field, and then you can provide a pointer to the
array you want to encode directly. For decoding FT_POINTER would require
malloc() support, but for encoding it works also without.

2) You can also use pb_encode_submessage() and pb_encode_varint() etc.
directly from the main program, without having to use callbacks. This
tends to be easier if most of your fields would be callback fields, with
few fields that benefit from the structs. But again, for decoding it's
not as useful, and callbacks are easier there.

--
Petteri

geekco...@gmail.com

unread,
Jun 11, 2019, 1:01:46 PM6/11/19
to nanopb
Hi Petteri,

Thanks for your response. I have implemented the repeated field and by giving values to it using an array it encodes and I can decode it too.

But, I want to pass the values to the repeated field using an array or array pointer by using the function that I have defined in the header file and I call it in main several times as below:

void messer_add_number(Messer * list, int32_t id, int32_t energy, float center_x, float center_y, int32_t xaxis[5])
{
if (list->numbers_count < MAX_NUMBERS)
{
(list->rep[list->numbers_count]).id = id;
(list->rep[list->numbers_count]).energy = energy;
(list->rep[list->numbers_count]).center_x = center_x;
(list->rep[list->numbers_count]).center_y = center_y;
(list->rep[list->numbers_count]).xaxis = xaxis[list->numbers_count];
list->numbers_count++;
}
}

messer_add_number(&actualData, 3, 60, 2.5, 5.4, {30,40,0});

but I get the following error:

build/callbacks/function_callback.h:23:52: error: incompatible types when assigning to type ‘pb_callback_t’ {aka ‘struct pb_callback_s’} from type ‘int32_t’ {aka ‘int’}
(list->rep[list->numbers_count]).xaxis = xaxis[list->numbers_count];


How do I update my FUNCTION ?? Looking forward to your response.
Attaching the new files.
callbacks.proto
encode_callbacks.c
function_callback.h
error_callback.png

Petteri Aimonen

unread,
Jun 11, 2019, 3:58:21 PM6/11/19
to nan...@googlegroups.com
Hi,

In your proto you have 'repeated' which can contain many items:
> repeated int32 xaxis = 2;

but in code you only assign a single item:
> (list->rep[list->numbers_count]).xaxis = xaxis[list->numbers_count];

So inside the xaxis repeated field, do you want one item, a few items or
unlimited items? If it's only a few, just set a maximum count for that
field. Otherwise you'll need nested callbacks.

--
Petteri
Message has been deleted

Grooo

unread,
Jun 12, 2019, 4:57:25 AM6/12/19
to nanopb
Hi Petteri, 

The data packet will look something like: 

id: 20
energy : 50
center_x : 2.2
center_y : 3.2 
xaxis : {20,50,40,60,12,14,25,19, ....}
yaxis : {20,50,40,60,12,14,25,19, ....}
zaxis : {20,50,40,60,12,14,25,19, ....}

and all of this will be repeated a number of times. 

On Wednesday, 12 June 2019 10:31:50 UTC+2, Grooo wrote:
Hi Petteri, 

I was trying to assign a single item just to check if it works. 

What I want is 3 repeated fields i.e.

repeated int32 xaxis; 
repeated int32 yaxis; 
repeated int32 zaxis;

and they all would have unlimited values or number of values that I cannot fix with a maximum count. So, how will the nested callbacks work, could you point me towards or give me an example ? 

Thank you !  

Petteri Aimonen

unread,
Jun 12, 2019, 4:57:50 AM6/12/19
to nan...@googlegroups.com
Hi,

> So, how will the nested callbacks work, could you point me towards or
> give me an example ?

Yeah, that's what I tried to show in my first reply.

But currently I don't have enough time to provide a more elaborate
example.

--
Petteri

Grooo

unread,
Jun 12, 2019, 5:05:15 AM6/12/19
to nanopb
Hi Petteri,

I could wait for a few days if you say so ? 

Thank you ! 

Petteri Aimonen

unread,
Jun 12, 2019, 5:12:54 AM6/12/19
to nan...@googlegroups.com
Hi,

> I could wait for a few days if you say so ?

Nah, that won't make a difference. I'm constantly overwhelmed by the
amount of free support requests on nanopb, so I just have to put a limit
on how much time I'll spend on one case.

--
Petteri
Reply all
Reply to author
Forward
0 new messages