MOS Source Code
Loading...
Searching...
No Matches
submsg_callback.c
Go to the documentation of this file.
1#include "submsg_callback.pb.h"
2#include <pb_encode.h>
3#include <pb_decode.h>
4#include "unittests.h"
5
6bool msg_callback(pb_istream_t *stream, const pb_field_t *field, void **arg)
7{
8 /* This tests decoding the submessage already in the message level callback. */
9
10 SubMessage *submsg = (SubMessage*)field->pData;
11
12 if (!pb_decode(stream, SubMessage_fields, submsg))
13 PB_RETURN_ERROR(stream, "submsg decode failed");
14
15 if (submsg->foo != 1234)
16 PB_RETURN_ERROR(stream, "submsg.foo wrong value");
17
18 return true;
19}
20
21int main()
22{
23 int status = 0;
24 pb_byte_t buf[64];
25 size_t msglen;
26
27 {
28 pb_ostream_t ostream = pb_ostream_from_buffer(buf, sizeof(buf));
29 MyMessage msg = MyMessage_init_zero;
30
31 msg.which_oneof = MyMessage_submsg_tag;
32 msg.oneof.submsg.foo = 1234;
33
34 if (!pb_encode(&ostream, MyMessage_fields, &msg))
35 {
36 fprintf(stderr, "pb_encode() failed: %s\n", PB_GET_ERROR(&ostream));
37 return 1;
38 }
39
40 msglen = ostream.bytes_written;
41 TEST(msglen > 0);
42 }
43
44 {
45 pb_istream_t istream = pb_istream_from_buffer(buf, msglen);
46 MyMessage msg = MyMessage_init_zero;
47 msg.cb_oneof.funcs.decode = msg_callback;
48
49 if (!pb_decode(&istream, MyMessage_fields, &msg))
50 {
51 fprintf(stderr, "pb_decode() failed: %s\n", PB_GET_ERROR(&istream));
52 return 1;
53 }
54 }
55
56 return status;
57}
#define SubMessage_fields
#define stderr
Definition mos_stdio.h:32
uint_least8_t pb_byte_t
Definition pb.h:227
#define PB_GET_ERROR(stream)
Definition pb.h:891
pb_field_iter_t pb_field_t
Definition pb.h:359
#define PB_RETURN_ERROR(stream, msg)
Definition pb.h:894
bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
Definition pb_decode.c:1182
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
Definition pb_decode.c:143
pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
Definition pb_encode.c:63
bool pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
Definition pb_encode.c:512
bool msg_callback(pb_istream_t *stream, const pb_field_t *field, void **arg)
int main()
#define TEST(x)
Definition test.h:3