test code 123
Fork of LinkNode-Test by
nRF51822_Science_Journal/pb_encode.h@0:1ad0e04b1bc5, 2016-09-01 (annotated)
- Committer:
- youkee
- Date:
- Thu Sep 01 05:14:03 2016 +0000
- Revision:
- 0:1ad0e04b1bc5
change internal time from 1s to 200ms
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
youkee | 0:1ad0e04b1bc5 | 1 | /* pb_encode.h: Functions to encode protocol buffers. Depends on pb_encode.c. |
youkee | 0:1ad0e04b1bc5 | 2 | * The main function is pb_encode. You also need an output 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_ENCODE_H_INCLUDED |
youkee | 0:1ad0e04b1bc5 | 7 | #define PB_ENCODE_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 output streams. You will need to provide |
youkee | 0:1ad0e04b1bc5 | 16 | * a callback function to write the bytes to 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 encoding to abort. |
youkee | 0:1ad0e04b1bc5 | 22 | * 2) You can use state to store your own data (e.g. buffer pointer). |
youkee | 0:1ad0e04b1bc5 | 23 | * 3) pb_write will update bytes_written after your callback runs. |
youkee | 0:1ad0e04b1bc5 | 24 | * 4) Substreams will modify max_size and bytes_written. Don't use them |
youkee | 0:1ad0e04b1bc5 | 25 | * to calculate any pointers. |
youkee | 0:1ad0e04b1bc5 | 26 | */ |
youkee | 0:1ad0e04b1bc5 | 27 | struct pb_ostream_s |
youkee | 0:1ad0e04b1bc5 | 28 | { |
youkee | 0:1ad0e04b1bc5 | 29 | #ifdef PB_BUFFER_ONLY |
youkee | 0:1ad0e04b1bc5 | 30 | /* Callback pointer is not used in buffer-only configuration. |
youkee | 0:1ad0e04b1bc5 | 31 | * Having an int pointer here allows binary compatibility but |
youkee | 0:1ad0e04b1bc5 | 32 | * gives an error if someone tries to assign callback function. |
youkee | 0:1ad0e04b1bc5 | 33 | * Also, NULL pointer marks a 'sizing stream' that does not |
youkee | 0:1ad0e04b1bc5 | 34 | * write anything. |
youkee | 0:1ad0e04b1bc5 | 35 | */ |
youkee | 0:1ad0e04b1bc5 | 36 | int *callback; |
youkee | 0:1ad0e04b1bc5 | 37 | #else |
youkee | 0:1ad0e04b1bc5 | 38 | bool (*callback)(pb_ostream_t *stream, const pb_byte_t *buf, size_t count); |
youkee | 0:1ad0e04b1bc5 | 39 | #endif |
youkee | 0:1ad0e04b1bc5 | 40 | void *state; /* Free field for use by callback implementation. */ |
youkee | 0:1ad0e04b1bc5 | 41 | size_t max_size; /* Limit number of output bytes written (or use SIZE_MAX). */ |
youkee | 0:1ad0e04b1bc5 | 42 | size_t bytes_written; /* Number of bytes written so far. */ |
youkee | 0:1ad0e04b1bc5 | 43 | |
youkee | 0:1ad0e04b1bc5 | 44 | #ifndef PB_NO_ERRMSG |
youkee | 0:1ad0e04b1bc5 | 45 | const char *errmsg; |
youkee | 0:1ad0e04b1bc5 | 46 | #endif |
youkee | 0:1ad0e04b1bc5 | 47 | }; |
youkee | 0:1ad0e04b1bc5 | 48 | |
youkee | 0:1ad0e04b1bc5 | 49 | /*************************** |
youkee | 0:1ad0e04b1bc5 | 50 | * Main encoding functions * |
youkee | 0:1ad0e04b1bc5 | 51 | ***************************/ |
youkee | 0:1ad0e04b1bc5 | 52 | |
youkee | 0:1ad0e04b1bc5 | 53 | /* Encode a single protocol buffers message from C structure into a stream. |
youkee | 0:1ad0e04b1bc5 | 54 | * Returns true on success, false on any failure. |
youkee | 0:1ad0e04b1bc5 | 55 | * The actual struct pointed to by src_struct must match the description in fields. |
youkee | 0:1ad0e04b1bc5 | 56 | * All required fields in the struct are assumed to have been filled in. |
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_ostream_t stream; |
youkee | 0:1ad0e04b1bc5 | 62 | * |
youkee | 0:1ad0e04b1bc5 | 63 | * msg.field1 = 42; |
youkee | 0:1ad0e04b1bc5 | 64 | * stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); |
youkee | 0:1ad0e04b1bc5 | 65 | * pb_encode(&stream, MyMessage_fields, &msg); |
youkee | 0:1ad0e04b1bc5 | 66 | */ |
youkee | 0:1ad0e04b1bc5 | 67 | bool pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct); |
youkee | 0:1ad0e04b1bc5 | 68 | |
youkee | 0:1ad0e04b1bc5 | 69 | /* Same as pb_encode, but prepends the length of the message as a varint. |
youkee | 0:1ad0e04b1bc5 | 70 | * Corresponds to writeDelimitedTo() in Google's protobuf API. |
youkee | 0:1ad0e04b1bc5 | 71 | */ |
youkee | 0:1ad0e04b1bc5 | 72 | bool pb_encode_delimited(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct); |
youkee | 0:1ad0e04b1bc5 | 73 | |
youkee | 0:1ad0e04b1bc5 | 74 | /* Encode the message to get the size of the encoded data, but do not store |
youkee | 0:1ad0e04b1bc5 | 75 | * the data. */ |
youkee | 0:1ad0e04b1bc5 | 76 | bool pb_get_encoded_size(size_t *size, const pb_field_t fields[], const void *src_struct); |
youkee | 0:1ad0e04b1bc5 | 77 | |
youkee | 0:1ad0e04b1bc5 | 78 | /************************************** |
youkee | 0:1ad0e04b1bc5 | 79 | * Functions for manipulating streams * |
youkee | 0:1ad0e04b1bc5 | 80 | **************************************/ |
youkee | 0:1ad0e04b1bc5 | 81 | |
youkee | 0:1ad0e04b1bc5 | 82 | /* Create an output stream for writing into a memory buffer. |
youkee | 0:1ad0e04b1bc5 | 83 | * The number of bytes written can be found in stream.bytes_written after |
youkee | 0:1ad0e04b1bc5 | 84 | * encoding the message. |
youkee | 0:1ad0e04b1bc5 | 85 | * |
youkee | 0:1ad0e04b1bc5 | 86 | * Alternatively, you can use a custom stream that writes directly to e.g. |
youkee | 0:1ad0e04b1bc5 | 87 | * a file or a network socket. |
youkee | 0:1ad0e04b1bc5 | 88 | */ |
youkee | 0:1ad0e04b1bc5 | 89 | pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize); |
youkee | 0:1ad0e04b1bc5 | 90 | |
youkee | 0:1ad0e04b1bc5 | 91 | /* Pseudo-stream for measuring the size of a message without actually storing |
youkee | 0:1ad0e04b1bc5 | 92 | * the encoded data. |
youkee | 0:1ad0e04b1bc5 | 93 | * |
youkee | 0:1ad0e04b1bc5 | 94 | * Example usage: |
youkee | 0:1ad0e04b1bc5 | 95 | * MyMessage msg = {}; |
youkee | 0:1ad0e04b1bc5 | 96 | * pb_ostream_t stream = PB_OSTREAM_SIZING; |
youkee | 0:1ad0e04b1bc5 | 97 | * pb_encode(&stream, MyMessage_fields, &msg); |
youkee | 0:1ad0e04b1bc5 | 98 | * printf("Message size is %d\n", stream.bytes_written); |
youkee | 0:1ad0e04b1bc5 | 99 | */ |
youkee | 0:1ad0e04b1bc5 | 100 | #ifndef PB_NO_ERRMSG |
youkee | 0:1ad0e04b1bc5 | 101 | #define PB_OSTREAM_SIZING {0,0,0,0,0} |
youkee | 0:1ad0e04b1bc5 | 102 | #else |
youkee | 0:1ad0e04b1bc5 | 103 | #define PB_OSTREAM_SIZING {0,0,0,0} |
youkee | 0:1ad0e04b1bc5 | 104 | #endif |
youkee | 0:1ad0e04b1bc5 | 105 | |
youkee | 0:1ad0e04b1bc5 | 106 | /* Function to write into a pb_ostream_t stream. You can use this if you need |
youkee | 0:1ad0e04b1bc5 | 107 | * to append or prepend some custom headers to the message. |
youkee | 0:1ad0e04b1bc5 | 108 | */ |
youkee | 0:1ad0e04b1bc5 | 109 | bool pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count); |
youkee | 0:1ad0e04b1bc5 | 110 | |
youkee | 0:1ad0e04b1bc5 | 111 | |
youkee | 0:1ad0e04b1bc5 | 112 | /************************************************ |
youkee | 0:1ad0e04b1bc5 | 113 | * Helper functions for writing field callbacks * |
youkee | 0:1ad0e04b1bc5 | 114 | ************************************************/ |
youkee | 0:1ad0e04b1bc5 | 115 | |
youkee | 0:1ad0e04b1bc5 | 116 | /* Encode field header based on type and field number defined in the field |
youkee | 0:1ad0e04b1bc5 | 117 | * structure. Call this from the callback before writing out field contents. */ |
youkee | 0:1ad0e04b1bc5 | 118 | bool pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t *field); |
youkee | 0:1ad0e04b1bc5 | 119 | |
youkee | 0:1ad0e04b1bc5 | 120 | /* Encode field header by manually specifing wire type. You need to use this |
youkee | 0:1ad0e04b1bc5 | 121 | * if you want to write out packed arrays from a callback field. */ |
youkee | 0:1ad0e04b1bc5 | 122 | bool pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number); |
youkee | 0:1ad0e04b1bc5 | 123 | |
youkee | 0:1ad0e04b1bc5 | 124 | /* Encode an integer in the varint format. |
youkee | 0:1ad0e04b1bc5 | 125 | * This works for bool, enum, int32, int64, uint32 and uint64 field types. */ |
youkee | 0:1ad0e04b1bc5 | 126 | bool pb_encode_varint(pb_ostream_t *stream, uint64_t value); |
youkee | 0:1ad0e04b1bc5 | 127 | |
youkee | 0:1ad0e04b1bc5 | 128 | /* Encode an integer in the zig-zagged svarint format. |
youkee | 0:1ad0e04b1bc5 | 129 | * This works for sint32 and sint64. */ |
youkee | 0:1ad0e04b1bc5 | 130 | bool pb_encode_svarint(pb_ostream_t *stream, int64_t value); |
youkee | 0:1ad0e04b1bc5 | 131 | |
youkee | 0:1ad0e04b1bc5 | 132 | /* Encode a string or bytes type field. For strings, pass strlen(s) as size. */ |
youkee | 0:1ad0e04b1bc5 | 133 | bool pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size); |
youkee | 0:1ad0e04b1bc5 | 134 | |
youkee | 0:1ad0e04b1bc5 | 135 | /* Encode a fixed32, sfixed32 or float value. |
youkee | 0:1ad0e04b1bc5 | 136 | * You need to pass a pointer to a 4-byte wide C variable. */ |
youkee | 0:1ad0e04b1bc5 | 137 | bool pb_encode_fixed32(pb_ostream_t *stream, const void *value); |
youkee | 0:1ad0e04b1bc5 | 138 | |
youkee | 0:1ad0e04b1bc5 | 139 | /* Encode a fixed64, sfixed64 or double value. |
youkee | 0:1ad0e04b1bc5 | 140 | * You need to pass a pointer to a 8-byte wide C variable. */ |
youkee | 0:1ad0e04b1bc5 | 141 | bool pb_encode_fixed64(pb_ostream_t *stream, const void *value); |
youkee | 0:1ad0e04b1bc5 | 142 | |
youkee | 0:1ad0e04b1bc5 | 143 | /* Encode a submessage field. |
youkee | 0:1ad0e04b1bc5 | 144 | * You need to pass the pb_field_t array and pointer to struct, just like |
youkee | 0:1ad0e04b1bc5 | 145 | * with pb_encode(). This internally encodes the submessage twice, first to |
youkee | 0:1ad0e04b1bc5 | 146 | * calculate message size and then to actually write it out. |
youkee | 0:1ad0e04b1bc5 | 147 | */ |
youkee | 0:1ad0e04b1bc5 | 148 | bool pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct); |
youkee | 0:1ad0e04b1bc5 | 149 | |
youkee | 0:1ad0e04b1bc5 | 150 | #ifdef __cplusplus |
youkee | 0:1ad0e04b1bc5 | 151 | } /* extern "C" */ |
youkee | 0:1ad0e04b1bc5 | 152 | #endif |
youkee | 0:1ad0e04b1bc5 | 153 | |
youkee | 0:1ad0e04b1bc5 | 154 | #endif |