First version of decoding
[apps/low-level-can-service.git] / pb.h
1 #ifndef _PB_H_
2 #define _PB_H_
3
4 #include <stdint.h>
5 #include <stddef.h> // size_t
6
7 /* Lightweight input stream.
8  * If buf is NULL, read but don't store bytes. */
9 typedef struct _pb_istream_t pb_istream_t;
10 struct _pb_istream_t
11 {
12     bool (*callback)(pb_istream_t *stream, char *buf, size_t count);
13     void *state; // Free field for use by callback implementation
14     size_t bytes_left;
15 };
16
17 static inline bool pb_read(pb_istream_t *stream, char *buf, size_t count)
18 {
19     bool status = stream->callback(stream, buf, count);
20     stream->bytes_left -= count;
21     return status;
22 }
23
24 /* Lightweight output stream. */
25 typedef struct _pb_ostream_t pb_ostream_t;
26 struct _pb_ostream_t
27 {
28     bool (*callback)(pb_ostream_t *stream, const char *buf, size_t count);
29     void *state; // Free field for use by callback implementation
30     size_t bytes_written;
31 };
32
33 static inline bool pb_write(pb_ostream_t *stream, const char *buf, size_t count)
34 {
35     bool status = stream->callback(stream, buf, count);
36     stream->bytes_written += count;
37     return status;
38 }
39
40 /* List of possible decode/encode action types */
41
42 typedef enum {
43     // Special case. Sets boolean field to true, continues parsing the data.
44     PB_ACT_HAS,
45     
46     // Standard integer types
47     PB_ACT_UINT32,
48     PB_ACT_SINT32,
49     PB_ACT_INT32,
50     PB_ACT_FIXED32,
51     PB_ACT_SFIXED32,
52     PB_ACT_UINT64,
53     PB_ACT_SINT64,
54     PB_ACT_INT64,
55     PB_ACT_FIXED64,
56     PB_ACT_SFIXED64,
57     PB_ACT_BOOL,
58     
59     // Standard float types
60     PB_ACT_FLOAT,
61     PB_ACT_DOUBLE,
62     
63     // Constant-sized array
64     PB_ACT_BYTES,
65     
66     // Constant-sized array, with null termination
67     PB_ACT_STRING,
68     
69     // Callback function pointer in field value
70     PB_ACT_SUBMESSAGE,
71     
72     PB_LAST_ACT
73 } pb_action_t;
74
75 // This structure is used in constants to specify struct fields.
76 typedef struct {
77     int field_number;
78     uint16_t offset;
79     pb_action_t action;
80     uint8_t fieldsize;
81 } pb_field_t;
82
83 #define PB_LAST_FIELD {0,0,0,0}
84
85 /* --- Types to use inside generated structures. --- */
86
87 // Byte array and size word.
88 // Note: because of variable length array, this type cannot be directly used.
89 // Autogenerated code declares the same type of fields but with explicit length.
90 typedef struct {
91     size_t size;
92     char bytes[];
93 } pb_bytearray_t;
94
95 // This structure is used for giving the callback function.
96 typedef struct _pb_callback_t pb_callback_t;
97 struct _pb_callback_t {
98     union {
99         bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg);
100         bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void *arg);
101     } funcs;
102     
103     // Free arg for use by callback
104     void *arg;
105 };
106
107 #endif