f46e60a6a005961f1fe85ff335e5bc1f474205a0
[apps/agl-service-can-low-level.git] / tests / test_encode1.c
1 /* A very simple encoding test case using person.proto.
2  * Just puts constant data in the fields and writes the
3  * data to stdout.
4  */
5
6 #include <stdio.h>
7 #include <pb_encode.h>
8 #include "person.pb.h"
9
10 /* This binds the pb_ostream_t into the stdout stream */
11 bool streamcallback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
12 {
13     FILE *file = (FILE*) stream->state;
14     return fwrite(buf, 1, count, file) == count;
15 }
16
17 int main()
18 {
19     /* Initialize the structure with constants */
20     Person person = {"Test Person 99", 99, true, "test@person.com",
21         1, {{"555-12345678", true, Person_PhoneType_MOBILE}}};
22     
23     /* Prepare the stream, output goes directly to stdout */
24     pb_ostream_t stream = {&streamcallback, stdout, SIZE_MAX, 0};
25     
26     /* Now encode it and check if we succeeded. */
27     if (pb_encode(&stream, Person_fields, &person))
28         return 0; /* Success */
29     else
30         return 1; /* Failure */
31 }