tahu icon indicating copy to clipboard operation
tahu copied to clipboard

Finding size of actual payload while encoding using encode_payload()

Open shaila123456789 opened this issue 4 years ago • 7 comments

Hello,

I am trying to find a way to get the size of a payload to encode (using encode_payload API). As per the API usage, I need to allocate space big enough to hold the entire payload in encode format. But I need to hard-code this value as of now. If I use sizeof() on the payload variable, it calculates the size of struct org_eclipse_tahu_protobuf_Payload (which is datatype of payload).

How can I find the size of the actual payload with values at runtime ?

I am using code from https://github.com/jbrzozoski/tahu/tree/c_client_rework.

e.g. in test program, buffer_length = 128 has been hard-coded. I need to have this value calculated at runtime, as each time the payload will have different sizes. org_eclipse_tahu_protobuf_Payload ddata_payload; // The binary_buffer must be large enough to hold the contents of the binary payload size_t buffer_length = 128; uint8_t *binary_buffer = (uint8_t *)malloc(buffer_length * sizeof(uint8_t)); size_t message_length = encode_payload(binary_buffer, buffer_length, &ddata_payload);

-Thanks, S

shaila123456789 avatar Jul 17 '20 13:07 shaila123456789

There is a separate NanoPB function called pb_get_encoded_size to do this, but it isn't directly exposed through any helper functions yet. This should be a workable example:

// The binary_buffer must be large enough to hold the contents of the binary payload
size_t buffer_length;
bool encode_passed = pb_get_encoded_size(&buffer_length, org_eclipse_tahu_protobuf_Payload_fields, &ddata_payload);
uint8_t *binary_buffer = (uint8_t *)malloc(buffer_length * sizeof(uint8_t));
size_t message_length = encode_payload(binary_buffer, buffer_length, &ddata_payload);

jbrzozoski avatar Jul 17 '20 14:07 jbrzozoski

I'm trying to get my branch updated and into a pull request soon. I'll add a feature to find the size more intuitively.

jbrzozoski avatar Jul 17 '20 15:07 jbrzozoski

Thanks for the reply jbrzozoski !

This worked as expected. Apart from this, there is one more thing. I am getting lot of console logs from tahu library. Is there any way in which I can turn off these console logs ?

-Thanks, S

shaila123456789 avatar Jul 20 '20 05:07 shaila123456789

Near the top of tahu.h, change this define from 1 to 0:

// Enable/disable debug messages
#define SPARKPLUG_DEBUG 1

jbrzozoski avatar Jul 20 '20 13:07 jbrzozoski

Hello, I took the code from develop branch. I see you have merged earlier branch there. I set the SPARKPLUG_DEBUG to 0 and compiled the library. But still got the messages on console. (I had commented out all the fprintf's from test.c file). Commenting out the complete line #define SPARKPLUG_DEBUG 1 worked, as obviously compiler will not find this pre-processor. But apart from this, I am looking for a command line parameter while compiling the library itself. I don't want to make changes in the code and then compile the library. Instead if I can add a parameter in make command that will be much helpful.

-thanks, S

shaila123456789 avatar Jul 24 '20 09:07 shaila123456789

Hello jbrzozoski,

I wanted to stop tahu from showing console logs and I was looking for a command line parameter while compiling the library itself. Is any such compile time parameter available now to stop the console logs ?

-thanks, S

shaila123456789 avatar Sep 15 '20 07:09 shaila123456789

There is currently no compile time option.

jbrzozoski avatar Sep 28 '20 17:09 jbrzozoski