SingingCat 0
application
buf_to_struct.c
1#include "protobuf.h"
2#include "main-header.h"
3#include <pb_encode.h>
4#include <pb_decode.h>
5
6void proto_release(void *buf) {
7}
8
9
10// return 0 if ok, otherwise error
11int buf_to_struct(void *buf, int bufsize, void *iter, void *out) {
12 /*
13 * printf("Buf: 0x%X, Iter: 0x%X, bufsize: %i\n",buf,iter,bufsize);
14 * printf("Buf contents:\n");
15 * hexdump(buf,bufsize);
16 */
17 pb_istream_t stream = pb_istream_from_buffer(buf, bufsize);
18 bool status = pb_decode(&stream, iter, out);
19
20 if (!status) {
21 printf("Proto decoding failed: %s\r\n", PB_GET_ERROR(&stream));
22 return 1;
23 }
24 return 0;
25}
26int struct_to_buf(void *buf, int bufsize, void *iter, void *in) {
27 printf("Buf: 0x%X, Iter: 0x%X\r\n", buf, iter);
28 pb_ostream_t stream = pb_ostream_from_buffer(buf, bufsize);
29 bool status = pb_encode(&stream, iter, in);
30
31 if (!status) {
32 printf("Proto encoding failed: %s\r\n", PB_GET_ERROR(&stream));
33 return -1;
34 }
35 printf("Bytes encoded: %i\r\n", stream.bytes_written);
36 return stream.bytes_written;
37}
Definition: streams.h:14