Difference between revisions of "Protobuf notes"
m (Add link to swi-prolog.org.) |
m (Kitsune project source file proto_utils.c and pb_callback_t example.) |
||
Line 25: | Line 25: | ||
In kitsune project, looking at: | In kitsune project, looking at: | ||
− | (1) file kitsune/kitsune/audio_features_upload_task.c function setup_protbuf( . . . ) | + | (1) file kitsune/kitsune/audio_features_upload_task.c function setup_protbuf( . . . )<br /> |
− | (2) file audio_features_upload_task_helpers.c function encode_repeated_streaming_bytes_and_mark_done(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) | + | (2) file audio_features_upload_task_helpers.c function encode_repeated_streaming_bytes_and_mark_done(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)<br /> |
− | (3) in same file reviewing function write_streams(pb_ostream_t *stream, const pb_field_t *field,hlo_stream_t * hlo_stream) | + | (3) in same file reviewing function write_streams(pb_ostream_t *stream, const pb_field_t *field,hlo_stream_t * hlo_stream)<br /> |
+ | Here is an excerpt from proto_utils.c which appears to contain a pb_callback_t definition: | ||
+ | |||
+ | <pre> | ||
+ | 147 bool encode_device_id_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { | ||
+ | 148 //char are twice the size, extra 1 for null terminator | ||
+ | 149 char hex_device_id[2*DEVICE_ID_SZ+1] = {0}; | ||
+ | 150 if(!get_device_id(hex_device_id, sizeof(hex_device_id))) | ||
+ | 151 { | ||
+ | 152 return false; | ||
+ | 153 } | ||
+ | 154 | ||
+ | 155 return pb_encode_tag_for_field(stream, field) && pb_encode_string(stream, (uint8_t*)hex_device_id, strlen(hex_device_id)); | ||
+ | 156 } | ||
+ | |||
+ | </pre> | ||
== [[#top|^]] References To Sort == | == [[#top|^]] References To Sort == |
Revision as of 03:52, 8 October 2024
Introduction:
Also a good more detailed introduction:
Another reference with possible good detail:
When compiling nanopb Protobuf library as part of C language programs, nested Protobuf messages require use of nanopb defined function type `pb_callback_t` in order to encode and to decode those nested messages. Some examples of this on github:
In the first example an early on file instance of `pb_callback_t` occurs on line 56. Looking further this project has a few dozen protoc generated files . . . switching to a possible smaller project:
In kitsune project, looking at:
(1) file kitsune/kitsune/audio_features_upload_task.c function setup_protbuf( . . . )
(2) file audio_features_upload_task_helpers.c function encode_repeated_streaming_bytes_and_mark_done(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
(3) in same file reviewing function write_streams(pb_ostream_t *stream, const pb_field_t *field,hlo_stream_t * hlo_stream)
Here is an excerpt from proto_utils.c which appears to contain a pb_callback_t definition:
147 bool encode_device_id_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { 148 //char are twice the size, extra 1 for null terminator 149 char hex_device_id[2*DEVICE_ID_SZ+1] = {0}; 150 if(!get_device_id(hex_device_id, sizeof(hex_device_id))) 151 { 152 return false; 153 } 154 155 return pb_encode_tag_for_field(stream, field) && pb_encode_string(stream, (uint8_t*)hex_device_id, strlen(hex_device_id)); 156 }
^ References To Sort
Protobuf references, somewhat arbitrary starting point yet introduces some key topics of Protobuf standard and use cases:
- https://www.crankuptheamps.com/blog/posts/2017/10/12/protobuf-battle-of-the-syntaxes/
- https://www.educative.io/edpresso/what-is-the-difference-between-protocol-buffers-and-json
JSON supported data types:
First Protobuf .proto file, compiles using `protoc-c`, part of a package available with Ubuntu 20.04:
// syntax = "proto3"; syntax = "proto2"; // Notes: // $ protoc-c --c_out=. ./first.proto message sensorUpdates { required int32 message_id = 1; optional float vrms = 2; }
. . . It appears that the integer values which message elements are assigned as tantamount to key names in JSON.
^ Nanopb
2022-01-08 Saturday
- https://github.com/nanopb/nanopb/blob/master/generator/proto/nanopb.proto
- https://jpa.kapsi.fi/nanopb/docs/whats_new.html
- https://jpa.kapsi.fi/nanopb/docs/
Cmake script to locate Nanopb headers and sources: