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