![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
test
Dependencies: Nanopb iSerial mbed BaseJpegDecode FatFileSystem SDFileSystem RingBuffer Camera_LS_Y201
Nanopb/pb_decode.h@0:d69efd0ee139, 2014-09-18 (annotated)
- Committer:
- cgraham
- Date:
- Thu Sep 18 15:21:47 2014 +0000
- Revision:
- 0:d69efd0ee139
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cgraham | 0:d69efd0ee139 | 1 | /* pb_decode.h: Functions to decode protocol buffers. Depends on pb_decode.c. |
cgraham | 0:d69efd0ee139 | 2 | * The main function is pb_decode. You also need an input stream, and the |
cgraham | 0:d69efd0ee139 | 3 | * field descriptions created by nanopb_generator.py. |
cgraham | 0:d69efd0ee139 | 4 | */ |
cgraham | 0:d69efd0ee139 | 5 | |
cgraham | 0:d69efd0ee139 | 6 | #ifndef _PB_DECODE_H_ |
cgraham | 0:d69efd0ee139 | 7 | #define _PB_DECODE_H_ |
cgraham | 0:d69efd0ee139 | 8 | |
cgraham | 0:d69efd0ee139 | 9 | #include "pb.h" |
cgraham | 0:d69efd0ee139 | 10 | |
cgraham | 0:d69efd0ee139 | 11 | #ifdef __cplusplus |
cgraham | 0:d69efd0ee139 | 12 | extern "C" { |
cgraham | 0:d69efd0ee139 | 13 | #endif |
cgraham | 0:d69efd0ee139 | 14 | |
cgraham | 0:d69efd0ee139 | 15 | /* Structure for defining custom input streams. You will need to provide |
cgraham | 0:d69efd0ee139 | 16 | * a callback function to read the bytes from your storage, which can be |
cgraham | 0:d69efd0ee139 | 17 | * for example a file or a network socket. |
cgraham | 0:d69efd0ee139 | 18 | * |
cgraham | 0:d69efd0ee139 | 19 | * The callback must conform to these rules: |
cgraham | 0:d69efd0ee139 | 20 | * |
cgraham | 0:d69efd0ee139 | 21 | * 1) Return false on IO errors. This will cause decoding to abort. |
cgraham | 0:d69efd0ee139 | 22 | * 2) You can use state to store your own data (e.g. buffer pointer), |
cgraham | 0:d69efd0ee139 | 23 | * and rely on pb_read to verify that no-body reads past bytes_left. |
cgraham | 0:d69efd0ee139 | 24 | * 3) Your callback may be used with substreams, in which case bytes_left |
cgraham | 0:d69efd0ee139 | 25 | * is different than from the main stream. Don't use bytes_left to compute |
cgraham | 0:d69efd0ee139 | 26 | * any pointers. |
cgraham | 0:d69efd0ee139 | 27 | */ |
cgraham | 0:d69efd0ee139 | 28 | struct _pb_istream_t |
cgraham | 0:d69efd0ee139 | 29 | { |
cgraham | 0:d69efd0ee139 | 30 | #ifdef PB_BUFFER_ONLY |
cgraham | 0:d69efd0ee139 | 31 | /* Callback pointer is not used in buffer-only configuration. |
cgraham | 0:d69efd0ee139 | 32 | * Having an int pointer here allows binary compatibility but |
cgraham | 0:d69efd0ee139 | 33 | * gives an error if someone tries to assign callback function. |
cgraham | 0:d69efd0ee139 | 34 | */ |
cgraham | 0:d69efd0ee139 | 35 | int *callback; |
cgraham | 0:d69efd0ee139 | 36 | #else |
cgraham | 0:d69efd0ee139 | 37 | bool (*callback)(pb_istream_t *stream, uint8_t *buf, size_t count); |
cgraham | 0:d69efd0ee139 | 38 | #endif |
cgraham | 0:d69efd0ee139 | 39 | |
cgraham | 0:d69efd0ee139 | 40 | void *state; /* Free field for use by callback implementation */ |
cgraham | 0:d69efd0ee139 | 41 | size_t bytes_left; |
cgraham | 0:d69efd0ee139 | 42 | |
cgraham | 0:d69efd0ee139 | 43 | #ifndef PB_NO_ERRMSG |
cgraham | 0:d69efd0ee139 | 44 | const char *errmsg; |
cgraham | 0:d69efd0ee139 | 45 | #endif |
cgraham | 0:d69efd0ee139 | 46 | }; |
cgraham | 0:d69efd0ee139 | 47 | |
cgraham | 0:d69efd0ee139 | 48 | /*************************** |
cgraham | 0:d69efd0ee139 | 49 | * Main decoding functions * |
cgraham | 0:d69efd0ee139 | 50 | ***************************/ |
cgraham | 0:d69efd0ee139 | 51 | |
cgraham | 0:d69efd0ee139 | 52 | /* Decode a single protocol buffers message from input stream into a C structure. |
cgraham | 0:d69efd0ee139 | 53 | * Returns true on success, false on any failure. |
cgraham | 0:d69efd0ee139 | 54 | * The actual struct pointed to by dest must match the description in fields. |
cgraham | 0:d69efd0ee139 | 55 | * Callback fields of the destination structure must be initialized by caller. |
cgraham | 0:d69efd0ee139 | 56 | * All other fields will be initialized by this function. |
cgraham | 0:d69efd0ee139 | 57 | * |
cgraham | 0:d69efd0ee139 | 58 | * Example usage: |
cgraham | 0:d69efd0ee139 | 59 | * MyMessage msg = {}; |
cgraham | 0:d69efd0ee139 | 60 | * uint8_t buffer[64]; |
cgraham | 0:d69efd0ee139 | 61 | * pb_istream_t stream; |
cgraham | 0:d69efd0ee139 | 62 | * |
cgraham | 0:d69efd0ee139 | 63 | * // ... read some data into buffer ... |
cgraham | 0:d69efd0ee139 | 64 | * |
cgraham | 0:d69efd0ee139 | 65 | * stream = pb_istream_from_buffer(buffer, count); |
cgraham | 0:d69efd0ee139 | 66 | * pb_decode(&stream, MyMessage_fields, &msg); |
cgraham | 0:d69efd0ee139 | 67 | */ |
cgraham | 0:d69efd0ee139 | 68 | bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); |
cgraham | 0:d69efd0ee139 | 69 | |
cgraham | 0:d69efd0ee139 | 70 | /* Same as pb_decode, except does not initialize the destination structure |
cgraham | 0:d69efd0ee139 | 71 | * to default values. This is slightly faster if you need no default values |
cgraham | 0:d69efd0ee139 | 72 | * and just do memset(struct, 0, sizeof(struct)) yourself. |
cgraham | 0:d69efd0ee139 | 73 | * |
cgraham | 0:d69efd0ee139 | 74 | * This can also be used for 'merging' two messages, i.e. update only the |
cgraham | 0:d69efd0ee139 | 75 | * fields that exist in the new message. |
cgraham | 0:d69efd0ee139 | 76 | */ |
cgraham | 0:d69efd0ee139 | 77 | bool pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); |
cgraham | 0:d69efd0ee139 | 78 | |
cgraham | 0:d69efd0ee139 | 79 | /* Same as pb_decode, except expects the stream to start with the message size |
cgraham | 0:d69efd0ee139 | 80 | * encoded as varint. Corresponds to parseDelimitedFrom() in Google's |
cgraham | 0:d69efd0ee139 | 81 | * protobuf API. |
cgraham | 0:d69efd0ee139 | 82 | */ |
cgraham | 0:d69efd0ee139 | 83 | bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); |
cgraham | 0:d69efd0ee139 | 84 | |
cgraham | 0:d69efd0ee139 | 85 | |
cgraham | 0:d69efd0ee139 | 86 | /************************************** |
cgraham | 0:d69efd0ee139 | 87 | * Functions for manipulating streams * |
cgraham | 0:d69efd0ee139 | 88 | **************************************/ |
cgraham | 0:d69efd0ee139 | 89 | |
cgraham | 0:d69efd0ee139 | 90 | /* Create an input stream for reading from a memory buffer. |
cgraham | 0:d69efd0ee139 | 91 | * |
cgraham | 0:d69efd0ee139 | 92 | * Alternatively, you can use a custom stream that reads directly from e.g. |
cgraham | 0:d69efd0ee139 | 93 | * a file or a network socket. |
cgraham | 0:d69efd0ee139 | 94 | */ |
cgraham | 0:d69efd0ee139 | 95 | pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize); |
cgraham | 0:d69efd0ee139 | 96 | |
cgraham | 0:d69efd0ee139 | 97 | /* Function to read from a pb_istream_t. You can use this if you need to |
cgraham | 0:d69efd0ee139 | 98 | * read some custom header data, or to read data in field callbacks. |
cgraham | 0:d69efd0ee139 | 99 | */ |
cgraham | 0:d69efd0ee139 | 100 | bool pb_read(pb_istream_t *stream, uint8_t *buf, size_t count); |
cgraham | 0:d69efd0ee139 | 101 | |
cgraham | 0:d69efd0ee139 | 102 | |
cgraham | 0:d69efd0ee139 | 103 | /************************************************ |
cgraham | 0:d69efd0ee139 | 104 | * Helper functions for writing field callbacks * |
cgraham | 0:d69efd0ee139 | 105 | ************************************************/ |
cgraham | 0:d69efd0ee139 | 106 | |
cgraham | 0:d69efd0ee139 | 107 | /* Decode the tag for the next field in the stream. Gives the wire type and |
cgraham | 0:d69efd0ee139 | 108 | * field tag. At end of the message, returns false and sets eof to true. */ |
cgraham | 0:d69efd0ee139 | 109 | bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof); |
cgraham | 0:d69efd0ee139 | 110 | |
cgraham | 0:d69efd0ee139 | 111 | /* Skip the field payload data, given the wire type. */ |
cgraham | 0:d69efd0ee139 | 112 | bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type); |
cgraham | 0:d69efd0ee139 | 113 | |
cgraham | 0:d69efd0ee139 | 114 | /* Decode an integer in the varint format. This works for bool, enum, int32, |
cgraham | 0:d69efd0ee139 | 115 | * int64, uint32 and uint64 field types. */ |
cgraham | 0:d69efd0ee139 | 116 | bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest); |
cgraham | 0:d69efd0ee139 | 117 | |
cgraham | 0:d69efd0ee139 | 118 | /* Decode an integer in the zig-zagged svarint format. This works for sint32 |
cgraham | 0:d69efd0ee139 | 119 | * and sint64. */ |
cgraham | 0:d69efd0ee139 | 120 | bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest); |
cgraham | 0:d69efd0ee139 | 121 | |
cgraham | 0:d69efd0ee139 | 122 | /* Decode a fixed32, sfixed32 or float value. You need to pass a pointer to |
cgraham | 0:d69efd0ee139 | 123 | * a 4-byte wide C variable. */ |
cgraham | 0:d69efd0ee139 | 124 | bool pb_decode_fixed32(pb_istream_t *stream, void *dest); |
cgraham | 0:d69efd0ee139 | 125 | |
cgraham | 0:d69efd0ee139 | 126 | /* Decode a fixed64, sfixed64 or double value. You need to pass a pointer to |
cgraham | 0:d69efd0ee139 | 127 | * a 8-byte wide C variable. */ |
cgraham | 0:d69efd0ee139 | 128 | bool pb_decode_fixed64(pb_istream_t *stream, void *dest); |
cgraham | 0:d69efd0ee139 | 129 | |
cgraham | 0:d69efd0ee139 | 130 | /* Make a limited-length substream for reading a PB_WT_STRING field. */ |
cgraham | 0:d69efd0ee139 | 131 | bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream); |
cgraham | 0:d69efd0ee139 | 132 | void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream); |
cgraham | 0:d69efd0ee139 | 133 | |
cgraham | 0:d69efd0ee139 | 134 | #ifdef __cplusplus |
cgraham | 0:d69efd0ee139 | 135 | } /* extern "C" */ |
cgraham | 0:d69efd0ee139 | 136 | #endif |
cgraham | 0:d69efd0ee139 | 137 | |
cgraham | 0:d69efd0ee139 | 138 | #endif |