Protocol buffers

I went looking for a library to parse network messages in my robot, so that I could control it using JSON RPC from a browser. I wanted something that would translate directly to and from C data structures. Nothing like this exists.

I briefly contemplated writing something, and started looking for a starting point. What I found was protobuf-c. Then I thought: forget it, lets use protocol buffers, it'll be more efficient anyway. I already have a bridge between the browser and the device, perhaps it can do translation. As it turns out, I won't have to write that either: protobuf-json. So far so promising.

My newline based framing won't work for protocol buffers, so I'll replace it with SLIP framing which is almost as simple.

There are some limited instructions for cross compiling protobuf-c. They lead me to compile from source, to make sure I get a consistent version. I've downloaded and extracted 0.15.

On Debian/Ubuntu you will need to install the developer interface for the protocol buffer compiler:

$ sudo apt-get install libprotoc-dev

Then build and install the protobuf-c compiler:

$ ./configure
$ make
$ sudo make install

Next in the instructions, it seems to be expected this will work:

$ ./configure --disable-protoc arm-none-eabi

Of course it won't: arm-none-eabi-gcc can't link executables: there's no target platform. Stack overflow to the rescue. So I tried this:

$ CFLAGS="-nostdlib -nostartfiles -nodefaultlibs -ffreestanding" ./configure --disable-protoc --enable-shared=false -with-gnu-ld --host=arm-none-eabi

Which got me past configure, but now the build fails, wanting pollfd. I think this is because of some extraneous include path entry, but for Chibios, this is the wrong way anyway: I just need to figure out what to add to the include path, and the list of required files, and add them to the Chibios build.

So in the end, I just added 

       $(PROTOBUFC)/src/google/protobuf-c/protobuf-c.c \
       $(PROTOBUFC)/src/google/protobuf-c/protobuf-c-data-buffer.c \

to CSRC, and

       $(PROTOBUFC)/src/google/protobuf

to INCDIR in the project Makefile

The remaining files: protobuf-c-dispatch.c and protobuf-c-rpc.c are POSIX only, but might be useful starting points for my own dispatcher.

I went on to replace the telemetry and configuration data structures with protocol buffer definitions.

Then things got messy: even though protocol buffers don't support references, message types aren't in-line structs: they are pointers. This means they can't be initialised declaratively: I have to write functions to do it. Yuk! Not to mention this requires loads of ugly code changes.

I don't think I'm going to like the result of this in the C code, and I still need to add a converter to bridge code, so I think I might pause this for a while, and see if I can do better with JSON.

Popular posts from this blog

3D Printer ramblings

Balance Bot V2

FLSUN Q5