4

I need to implement rather simple network protocol: there is device with microcontroller (language is C) and Java application, they should communicate: I need to implement firmware update, and maybe some other things.

At least, I need to transmit some data structures as headers.

Only ugly way comes to mind:

I can declare packed structure on C side, and handle somehow the same data flow on Java side. So, if my structure is changed, then I need to make changes on both sides: C and Java. I strongly dislike this.

Is there some better way to do that? Maybe, something like this: I should write protocol structures in some special format, and then some utility can generate code for C and Java sides.

Or, maybe, something different.

I would be glad to see suggestions.

Dmitry Frank
  • 10,417
  • 10
  • 64
  • 114

3 Answers3

3

You might want to look at using a standardized notation for data transfer such as JSON. Here is some info on parsing JSON in c.

Parsing JSON using C

If it were my project I probably would go with just packed data structures. Hopefully once your project matures changes to the data structures are minimal and only occur during major releases. You can keep a version tag in the data structure to handle legacy data formats if needed.

Community
  • 1
  • 1
Brandon Yates
  • 2,022
  • 3
  • 22
  • 33
  • Thanks for the answer. I need to keep my C bootloader as lightweight as possible, so, JSON seems not to be good way. About version tag: yeah, surely I will keep it if I decide to use this way. – Dmitry Frank May 12 '13 at 18:26
3

One common solution to this problem would be to use Google's protobuf. However, as you specified that you need it to work in a microcontroller environment I think you could look into protobuf-c, which is a pure C-version of protobuf.

kpaleniu
  • 326
  • 1
  • 3
0

Could you describe details of protocol? Is if statefull or stateless? If your protocol is stateless, then take a look at web-services (especially, REST-WS). This is well known cross-platform communication practice.

uzvar
  • 71
  • 1
  • 5
  • Protocol is stateless, but my electronic device has no access to the World Wide Web, it just connects to the PC by USB. Or, did I misunderstood you? – Dmitry Frank May 12 '13 at 18:32
  • There is "Network" word in question title. Most frequently network protocol contains client & server roles. Server could be exposed just in the local network. If you communicate thru USB, than web-service is not right solution. – uzvar May 12 '13 at 18:41
  • Sorry for that, I thought I can call it "Network protocol". Just renamed it to "Data transfer protocol". – Dmitry Frank May 12 '13 at 19:16