773b7178c7a91d29e25ffc8b39754b1be871e9c9
[apps/agl-service-can-low-level.git] / pb_encode.h
1 #ifndef _PB_ENCODE_H_
2 #define _PB_ENCODE_H_
3
4 /* pb_encode.h: Functions to encode protocol buffers. Depends on pb_encode.c.
5  * The main function is pb_encode. You also need an output stream, structures
6  * and their field descriptions (just like with pb_decode).
7  */
8
9 #include "pb.h"
10
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14
15 /* Lightweight output stream.
16  * You can provide callback for writing or use pb_ostream_from_buffer.
17  * 
18  * Alternatively, callback can be NULL in which case the stream will just
19  * count the number of bytes that would have been written. In this case
20  * max_size is not checked.
21  *
22  * Rules for callback:
23  * 1) Return false on IO errors. This will cause encoding to abort.
24  * 
25  * 2) You can use state to store your own data (e.g. buffer pointer).
26  * 
27  * 3) pb_write will update bytes_written after your callback runs.
28  * 
29  * 4) Substreams will modify max_size and bytes_written. Don't use them to
30  * calculate any pointers.
31  */
32 struct _pb_ostream_t
33 {
34 #ifdef PB_BUFFER_ONLY
35     /* Callback pointer is not used in buffer-only configuration.
36      * Having an int pointer here allows binary compatibility but
37      * gives an error if someone tries to assign callback function.
38      * Also, NULL pointer marks a 'sizing stream' that does not
39      * write anything.
40      */
41     int *callback;
42 #else
43     bool (*callback)(pb_ostream_t *stream, const uint8_t *buf, size_t count);
44 #endif
45     void *state; /* Free field for use by callback implementation */
46     size_t max_size; /* Limit number of output bytes written (or use SIZE_MAX). */
47     size_t bytes_written;
48     
49 #ifndef PB_NO_ERRMSG
50     const char *errmsg;
51 #endif
52 };
53
54 pb_ostream_t pb_ostream_from_buffer(uint8_t *buf, size_t bufsize);
55 bool pb_write(pb_ostream_t *stream, const uint8_t *buf, size_t count);
56
57 /* Stream type for use in computing message sizes */
58 #ifndef PB_NO_ERRMSG
59 #define PB_OSTREAM_SIZING {0,0,0,0,0}
60 #else
61 #define PB_OSTREAM_SIZING {0,0,0,0}
62 #endif
63
64 /* Encode struct to given output stream.
65  * Returns true on success, false on any failure.
66  * The actual struct pointed to by src_struct must match the description in fields.
67  * All required fields in the struct are assumed to have been filled in.
68  */
69 bool pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct);
70
71 /* --- Helper functions ---
72  * You may want to use these from your caller or callbacks.
73  */
74
75 /* Encode field header based on LTYPE and field number defined in the field structure.
76  * Call this from the callback before writing out field contents. */
77 bool pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t *field);
78
79 /* Encode field header by manually specifing wire type. You need to use this if
80  * you want to write out packed arrays from a callback field. */
81 bool pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number);
82
83 /* Encode an integer in the varint format.
84  * This works for bool, enum, int32, int64, uint32 and uint64 field types. */
85 bool pb_encode_varint(pb_ostream_t *stream, uint64_t value);
86
87 /* Encode an integer in the zig-zagged svarint format.
88  * This works for sint32 and sint64. */
89 bool pb_encode_svarint(pb_ostream_t *stream, int64_t value);
90
91 /* Encode a string or bytes type field. For strings, pass strlen(s) as size. */
92 bool pb_encode_string(pb_ostream_t *stream, const uint8_t *buffer, size_t size);
93
94 /* Encode a fixed32, sfixed32 or float value.
95  * You need to pass a pointer to a 4-byte wide C variable. */
96 bool pb_encode_fixed32(pb_ostream_t *stream, const void *value);
97
98 /* Encode a fixed64, sfixed64 or double value.
99  * You need to pass a pointer to a 8-byte wide C variable. */
100 bool pb_encode_fixed64(pb_ostream_t *stream, const void *value);
101
102 /* Encode a submessage field.
103  * You need to pass the pb_field_t array and pointer to struct, just like with pb_encode().
104  * This internally encodes the submessage twice, first to calculate message size and then to actually write it out.
105  */
106 bool pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct);
107
108 /* --- Internal functions ---
109  * These functions are not terribly useful for the average library user, but
110  * are exported to make the unit testing and extending nanopb easier.
111  */
112
113 #ifdef NANOPB_INTERNALS
114 bool pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, const void *src);
115 bool pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src);
116 bool pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *src);
117 bool pb_enc_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *src);
118 bool pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src);
119 bool pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src);
120 #endif
121
122 /* This function is not recommended for new programs. Use pb_encode_submessage()
123  * instead, it has the same functionality with a less confusing interface. */
124 bool pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src);
125
126 #ifdef __cplusplus
127 } /* extern "C" */
128 #endif
129
130 #endif