Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transfer data from protobuf-embedded-c to Google protobuf C++/Java library?

I am using protobuf-embedded-c on a small system to transfer different data from the it to a PC. The problem is, that if I use the embedded library on both ends everything works. If i use the google C++ on the PC it doesn't work anymore. I think i traced the problem to the embedded library having length prefixes in every message, but i cannot seem to do this in a good way on the C++ library. Here is the test application which i used to debug this:

person.proto:

enum PhoneType {
  MOBILE = 0;
  HOME = 1;
  WORK = 2;
}

message PhoneNumber {
  required float number = 1;
  required PhoneType type = 2;
}

main.cpp:

#include <stdint.h>
#include <stdio.h>

#define __PROTOBUF_CPP_IMPL

#ifdef __PROTOBUF_CPP_IMPL
#include "person.pb.h"
#else
    extern "C"{
        #include "person.h"
    }
#endif

int main(int argc, char *argv[])
{
    static const uint32_t outputbuflen = 1024;
    uint8_t outputbuffer[outputbuflen];
    uint32_t writtenlenght = 0;

#ifdef __PROTOBUF_CPP_IMPL

    // C++ implementation.
    printf("Google C++ implementation;\n");
    PhoneNumber number;
    number.set_number(0800123123.0);
    number.set_type(MOBILE);

    writtenlenght = number.ByteSize();
    numberSerializeToArray(outputbuffer, writtenlenght);

#else

    // Embedded c implementation.
    printf("embedded-c implementation:\n");
    PhoneNumber number;
    number._number = 0800123123.0;
    number._type = _MOBILE;

    writtenlenght = PhoneNumber_write_delimited_to(&number, outputbuffer, 0);

#endif

    for(uint32_t i = 0; i < writtenlenght; i++){
        printf("%.2X ", outputbuffer[i]);
    }
    printf("\n");
    return 0;
}

The real question: What methods should be used in the Google protobuf C++/Java library to communicate succesfully with the embedded library? The simple answer would probably be to add (read when parsing) the prefix to all messages, but this breaks the code. Should I just look for a better embedded library?

Update: I tried this great little library called nanopb and it works great for now. Bottom line: protobuf-embedded-c in NOT compatible with the google protobuf implementation!

Protobuf comparision.

like image 401
rotator Avatar asked May 27 '12 14:05

rotator


1 Answers

Give offset as -1 instead of 0. For example:

writtenlenght = PhoneNumber_write_delimited_to(&number, outputbuffer, -1);

or you can just use PhoneNumber_write(&number, outputbuffer, 0);

like image 156
Nick Avatar answered Sep 27 '22 22:09

Nick