nanopb provides encoder and decoder implementation for files generated with nanopb 0.3.8 https://github.com/nanopb/nanopb/releases

Dependents:   ProtocBufTest Borsch

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pb_encode.h Source File

pb_encode.h

00001 /* pb_encode.h: Functions to encode protocol buffers. Depends on pb_encode.c.
00002  * The main function is pb_encode. You also need an output stream, and the
00003  * field descriptions created by nanopb_generator.py.
00004  */
00005 
00006 #ifndef PB_ENCODE_H_INCLUDED
00007 #define PB_ENCODE_H_INCLUDED
00008 
00009 #include "pb.h"
00010 
00011 #ifdef __cplusplus
00012 extern "C" {
00013 #endif
00014 
00015 /* Structure for defining custom output streams. You will need to provide
00016  * a callback function to write the bytes to your storage, which can be
00017  * for example a file or a network socket.
00018  *
00019  * The callback must conform to these rules:
00020  *
00021  * 1) Return false on IO errors. This will cause encoding to abort.
00022  * 2) You can use state to store your own data (e.g. buffer pointer).
00023  * 3) pb_write will update bytes_written after your callback runs.
00024  * 4) Substreams will modify max_size and bytes_written. Don't use them
00025  *    to calculate any pointers.
00026  */
00027 struct pb_ostream_s
00028 {
00029 #ifdef PB_BUFFER_ONLY
00030     /* Callback pointer is not used in buffer-only configuration.
00031      * Having an int pointer here allows binary compatibility but
00032      * gives an error if someone tries to assign callback function.
00033      * Also, NULL pointer marks a 'sizing stream' that does not
00034      * write anything.
00035      */
00036     int *callback;
00037 #else
00038     bool (*callback)(pb_ostream_t *stream, const pb_byte_t *buf, size_t count);
00039 #endif
00040     void *state;          /* Free field for use by callback implementation. */
00041     size_t max_size;      /* Limit number of output bytes written (or use SIZE_MAX). */
00042     size_t bytes_written; /* Number of bytes written so far. */
00043     
00044 #ifndef PB_NO_ERRMSG
00045     const char *errmsg;
00046 #endif
00047 };
00048 
00049 /***************************
00050  * Main encoding functions *
00051  ***************************/
00052 
00053 /* Encode a single protocol buffers message from C structure into a stream.
00054  * Returns true on success, false on any failure.
00055  * The actual struct pointed to by src_struct must match the description in fields.
00056  * All required fields in the struct are assumed to have been filled in.
00057  *
00058  * Example usage:
00059  *    MyMessage msg = {};
00060  *    uint8_t buffer[64];
00061  *    pb_ostream_t stream;
00062  *
00063  *    msg.field1 = 42;
00064  *    stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
00065  *    pb_encode(&stream, MyMessage_fields, &msg);
00066  */
00067 bool pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct);
00068 
00069 /* Same as pb_encode, but prepends the length of the message as a varint.
00070  * Corresponds to writeDelimitedTo() in Google's protobuf API.
00071  */
00072 bool pb_encode_delimited(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct);
00073 
00074 /* Encode the message to get the size of the encoded data, but do not store
00075  * the data. */
00076 bool pb_get_encoded_size(size_t *size, const pb_field_t fields[], const void *src_struct);
00077 
00078 /**************************************
00079  * Functions for manipulating streams *
00080  **************************************/
00081 
00082 /* Create an output stream for writing into a memory buffer.
00083  * The number of bytes written can be found in stream.bytes_written after
00084  * encoding the message.
00085  *
00086  * Alternatively, you can use a custom stream that writes directly to e.g.
00087  * a file or a network socket.
00088  */
00089 pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize);
00090 
00091 /* Pseudo-stream for measuring the size of a message without actually storing
00092  * the encoded data.
00093  * 
00094  * Example usage:
00095  *    MyMessage msg = {};
00096  *    pb_ostream_t stream = PB_OSTREAM_SIZING;
00097  *    pb_encode(&stream, MyMessage_fields, &msg);
00098  *    printf("Message size is %d\n", stream.bytes_written);
00099  */
00100 #ifndef PB_NO_ERRMSG
00101 #define PB_OSTREAM_SIZING {0,0,0,0,0}
00102 #else
00103 #define PB_OSTREAM_SIZING {0,0,0,0}
00104 #endif
00105 
00106 /* Function to write into a pb_ostream_t stream. You can use this if you need
00107  * to append or prepend some custom headers to the message.
00108  */
00109 bool pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count);
00110 
00111 
00112 /************************************************
00113  * Helper functions for writing field callbacks *
00114  ************************************************/
00115 
00116 /* Encode field header based on type and field number defined in the field
00117  * structure. Call this from the callback before writing out field contents. */
00118 bool pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t *field);
00119 
00120 /* Encode field header by manually specifing wire type. You need to use this
00121  * if you want to write out packed arrays from a callback field. */
00122 bool pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number);
00123 
00124 /* Encode an integer in the varint format.
00125  * This works for bool, enum, int32, int64, uint32 and uint64 field types. */
00126 bool pb_encode_varint(pb_ostream_t *stream, uint64_t value);
00127 
00128 /* Encode an integer in the zig-zagged svarint format.
00129  * This works for sint32 and sint64. */
00130 bool pb_encode_svarint(pb_ostream_t *stream, int64_t value);
00131 
00132 /* Encode a string or bytes type field. For strings, pass strlen(s) as size. */
00133 bool pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size);
00134 
00135 /* Encode a fixed32, sfixed32 or float value.
00136  * You need to pass a pointer to a 4-byte wide C variable. */
00137 bool pb_encode_fixed32(pb_ostream_t *stream, const void *value);
00138 
00139 /* Encode a fixed64, sfixed64 or double value.
00140  * You need to pass a pointer to a 8-byte wide C variable. */
00141 bool pb_encode_fixed64(pb_ostream_t *stream, const void *value);
00142 
00143 /* Encode a submessage field.
00144  * You need to pass the pb_field_t array and pointer to struct, just like
00145  * with pb_encode(). This internally encodes the submessage twice, first to
00146  * calculate message size and then to actually write it out.
00147  */
00148 bool pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct);
00149 
00150 #ifdef __cplusplus
00151 } /* extern "C" */
00152 #endif
00153 
00154 #endif