WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
pb_encode.h
Go to the documentation of this file.
1/* pb_encode.h: Functions to encode protocol buffers. Depends on pb_encode.c.
2 * The main function is pb_encode. You also need an output stream, and the
3 * field descriptions created by nanopb_generator.py.
4 *
5 * Modified for WPILib Use
6 */
7
8#ifndef PB_ENCODE_H_INCLUDED
9#define PB_ENCODE_H_INCLUDED
10
11#include "pb.h"
12
13/* Structure for defining custom output streams. You will need to provide
14 * a callback function to write the bytes to your storage, which can be
15 * for example a file or a network socket.
16 *
17 * The callback must conform to these rules:
18 *
19 * 1) Return false on IO errors. This will cause encoding to abort.
20 * 2) You can use state to store your own data (e.g. buffer pointer).
21 * 3) pb_write will update bytes_written after your callback runs.
22 * 4) Substreams will modify max_size and bytes_written. Don't use them
23 * to calculate any pointers.
24 */
26{
27#ifdef PB_BUFFER_ONLY
28 /* Callback pointer is not used in buffer-only configuration.
29 * Having an int pointer here allows binary compatibility but
30 * gives an error if someone tries to assign callback function.
31 * Also, NULL pointer marks a 'sizing stream' that does not
32 * write anything.
33 */
34 const int *callback;
35#else
36 bool (*callback)(pb_ostream_t *stream, const pb_byte_t *buf, size_t count);
37#endif
38
39 /* state is a free field for use of the callback function defined above.
40 * Note that when pb_ostream_from_buffer() is used, it reserves this field
41 * for its own use.
42 */
43 void *state;
44
45 /* Limit number of output bytes written. Can be set to SIZE_MAX. */
46 size_t max_size;
47
48 /* Number of bytes written so far. */
50
51#ifndef PB_NO_ERRMSG
52 /* Pointer to constant (ROM) string when decoding function returns error */
53 const char *errmsg;
54#endif
55};
56
57/***************************
58 * Main encoding functions *
59 ***************************/
60
61/* Encode a single protocol buffers message from C structure into a stream.
62 * Returns true on success, false on any failure.
63 * The actual struct pointed to by src_struct must match the description in fields.
64 * All required fields in the struct are assumed to have been filled in.
65 *
66 * Example usage:
67 * MyMessage msg = {};
68 * uint8_t buffer[64];
69 * pb_ostream_t stream;
70 *
71 * msg.field1 = 42;
72 * stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
73 * pb_encode(&stream, MyMessage_fields, &msg);
74 */
75bool pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct);
76
77/* Extended version of pb_encode, with several options to control the
78 * encoding process:
79 *
80 * PB_ENCODE_DELIMITED: Prepend the length of message as a varint.
81 * Corresponds to writeDelimitedTo() in Google's
82 * protobuf API.
83 *
84 * PB_ENCODE_NULLTERMINATED: Append a null byte to the message for termination.
85 * NOTE: This behaviour is not supported in most other
86 * protobuf implementations, so PB_ENCODE_DELIMITED
87 * is a better option for compatibility.
88 */
89#define PB_ENCODE_DELIMITED 0x02U
90#define PB_ENCODE_NULLTERMINATED 0x04U
91bool pb_encode_ex(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct, unsigned int flags);
92
93/* Defines for backwards compatibility with code written before nanopb-0.4.0 */
94#define pb_encode_delimited(s,f,d) pb_encode_ex(s,f,d, PB_ENCODE_DELIMITED)
95#define pb_encode_nullterminated(s,f,d) pb_encode_ex(s,f,d, PB_ENCODE_NULLTERMINATED)
96
97/* Encode the message to get the size of the encoded data, but do not store
98 * the data. */
99bool pb_get_encoded_size(size_t *size, const pb_msgdesc_t *fields, const void *src_struct);
100
101/**************************************
102 * Functions for manipulating streams *
103 **************************************/
104
105/* Create an output stream for writing into a memory buffer.
106 * The number of bytes written can be found in stream.bytes_written after
107 * encoding the message.
108 *
109 * Alternatively, you can use a custom stream that writes directly to e.g.
110 * a file or a network socket.
111 */
113
114/* Pseudo-stream for measuring the size of a message without actually storing
115 * the encoded data.
116 *
117 * Example usage:
118 * MyMessage msg = {};
119 * pb_ostream_t stream = PB_OSTREAM_SIZING;
120 * pb_encode(&stream, MyMessage_fields, &msg);
121 * printf("Message size is %d\n", stream.bytes_written);
122 */
123#ifndef PB_NO_ERRMSG
124#define PB_OSTREAM_SIZING {0,0,0,0,0}
125#else
126#define PB_OSTREAM_SIZING {0,0,0,0}
127#endif
128
129/* Function to write into a pb_ostream_t stream. You can use this if you need
130 * to append or prepend some custom headers to the message.
131 */
132bool pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count);
133
134
135/************************************************
136 * Helper functions for writing field callbacks *
137 ************************************************/
138
139/* Encode field header based on type and field number defined in the field
140 * structure. Call this from the callback before writing out field contents. */
142
143/* Encode field header by manually specifying wire type. You need to use this
144 * if you want to write out packed arrays from a callback field. */
145bool pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number);
146
147/* Encode an integer in the varint format.
148 * This works for bool, enum, int32, int64, uint32 and uint64 field types. */
149#ifndef PB_WITHOUT_64BIT
150bool pb_encode_varint(pb_ostream_t *stream, uint64_t value);
151#else
152bool pb_encode_varint(pb_ostream_t *stream, uint32_t value);
153#endif
154
155/* Encode an integer in the zig-zagged svarint format.
156 * This works for sint32 and sint64. */
157#ifndef PB_WITHOUT_64BIT
158bool pb_encode_svarint(pb_ostream_t *stream, int64_t value);
159#else
160bool pb_encode_svarint(pb_ostream_t *stream, int32_t value);
161#endif
162
163/* Encode a string or bytes type field. For strings, pass strlen(s) as size. */
164bool pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size);
165
166/* Encode a fixed32, sfixed32 or float value.
167 * You need to pass a pointer to a 4-byte wide C variable. */
168bool pb_encode_fixed32(pb_ostream_t *stream, const void *value);
169
170#ifndef PB_WITHOUT_64BIT
171/* Encode a fixed64, sfixed64 or double value.
172 * You need to pass a pointer to a 8-byte wide C variable. */
173bool pb_encode_fixed64(pb_ostream_t *stream, const void *value);
174#endif
175
176#ifdef PB_CONVERT_DOUBLE_FLOAT
177/* Encode a float value so that it appears like a double in the encoded
178 * message. */
179bool pb_encode_float_as_double(pb_ostream_t *stream, float value);
180#endif
181
182/* Encode a submessage field.
183 * You need to pass the pb_field_t array and pointer to struct, just like
184 * with pb_encode(). This internally encodes the submessage twice, first to
185 * calculate message size and then to actually write it out.
186 */
187bool pb_encode_submessage(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct);
188
189#endif
uint_least8_t pb_byte_t
Definition pb.h:228
pb_wire_type_t
Definition pb.h:430
bool pb_encode_ex(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct, unsigned int flags)
bool pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_iter_t *field)
bool pb_encode_varint(pb_ostream_t *stream, uint64_t value)
bool pb_encode_submessage(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
bool pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
bool pb_get_encoded_size(size_t *size, const pb_msgdesc_t *fields, const void *src_struct)
pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
bool pb_encode_fixed64(pb_ostream_t *stream, const void *value)
bool pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number)
bool pb_encode_fixed32(pb_ostream_t *stream, const void *value)
bool pb_encode_svarint(pb_ostream_t *stream, int64_t value)
bool pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
bool pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size)
Definition pb.h:347
Definition pb.h:331
Definition pb_encode.h:26
size_t bytes_written
Definition pb_encode.h:49
bool(* callback)(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
Definition pb_encode.h:36
const char * errmsg
Definition pb_encode.h:53
void * state
Definition pb_encode.h:43
size_t max_size
Definition pb_encode.h:46