0

So I'm trying to serialize a struct containing a byte array and its size:

struct Msg {
    unsigned char* payload;
    size_t payload_size;
}; 

the following code uses an instance of this struct called msg.

    DiagResp resp  = DIAG_RESP__INIT;

    ProtobufCBinaryData payload;
    payload.data = msg->payload;
    payload.len = msg->payload_size;

    resp.payload = &payload;
    resp.n_payload = msg->payload_size;

    resp.has_payload_size = 1;
    resp.payload_size = msg->payload_size;

    size_t packed_size = diag_resp__get_packed_size(&resp);
    printf("packed_size=%lu, msg->received_bytes=%lu\n", 
           packed_size, msg->received_bytes);
    *ret_payload = malloc(packed_size);
    diag_resp__pack(&resp, *ret_payload);

this is from the .proto file:

message DiagResp {
  ...
  optional int32 payload_size = 10;
  repeated bytes payload = 11;
}

The printf call prints out this:

packed_size=4256922, msg->received_bytes=4

...but if I don't attempt to serialize the payload field, everything works as expected. So how do I serialize a repeated bytes field? It is not covered in the protobuf-c documentation...

embedded_crysis
  • 193
  • 2
  • 12
  • I may have it - for some reason, setting `resp.n_payload` makes protobuf-c glitch out completely. If I leave it unset, it seems to work ok... – embedded_crysis Mar 20 '17 at 15:05
  • I can't help with protobuf-c, but I wanted to comment that encoding `payload_size` as a second field is redundant here. Protobuf already keeps track of the size for a `bytes` field. – Kenton Varda Mar 27 '17 at 00:02

0 Answers0