this is a sample for mbed(LPC1768)

Committer:
1
Date:
Thu Nov 19 10:17:55 2015 +0800
Revision:
0:3163adfd2cf1
????????????????????????
1.callback
2.thread
3.auto-connect,time-up

Who changed what in which revision?

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