First version of decoding
[apps/agl-service-can-low-level.git] / pb_decode.h
1 #ifndef _PB_DECODE_H_
2 #define _PB_DECODE_H_
3
4 #include <stdbool.h>
5 #include "pb.h"
6
7 // Decode from stream to destination struct.
8 // The actual struct pointed to by dest must match the description in fields.
9 bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest);
10
11 /* --- Helper functions ---
12  * You may want to use these from your callbacks.
13  */
14
15 bool pb_decode_varint32(pb_istream_t *stream, uint32_t *dest);
16 bool pb_decode_varint64(pb_istream_t *stream, uint64_t *dest);
17
18 bool pb_skip_varint(pb_istream_t *stream);
19 bool pb_skip_string(pb_istream_t *stream);
20
21 /* --- Field decoders ---
22  * Each decoder takes stream and field description, and a pointer to the field
23  * in the destination struct (dest = struct_addr + field->offset).
24  */
25
26 // Integer types.
27 bool pb_dec_uint32(pb_istream_t *stream, const pb_field_t *field, void *dest);
28 bool pb_dec_sint32(pb_istream_t *stream, const pb_field_t *field, void *dest);
29 bool pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest);
30 bool pb_dec_uint64(pb_istream_t *stream, const pb_field_t *field, void *dest);
31 bool pb_dec_sint64(pb_istream_t *stream, const pb_field_t *field, void *dest);
32 bool pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest);
33 bool pb_dec_bool(pb_istream_t *stream, const pb_field_t *field, void *dest);
34
35 // Floating point types. Info is ignored.
36 bool pb_dec_float(pb_istream_t *stream, const pb_field_t *field, void *dest);
37 bool pb_dec_double(pb_istream_t *stream, const pb_field_t *field, void *dest);
38
39 // Byte array. Dest is pointer to 
40 bool pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest);
41
42 // Null-terminated string.
43 bool pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest);
44
45 // Use callback. Dest is pointer to pb_callback_t struct.
46 bool pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest);
47
48 typedef bool (*pb_decoder_t)(pb_istream_t *stream, const pb_field_t *field, void *dest);
49
50 /* --- Function pointers to field decoders ---
51  * Order in the array must match pb_action_t numbering.
52  */
53 const pb_decoder_t PB_DECODERS[PB_LAST_ACT];
54
55 #endif