xref: /aosp_15_r20/external/nanopb-c/examples/using_union_messages/README.txt (revision c8d645cafcee3f91213d30caa0fe303887010b9b)
1*c8d645caSAndroid Build Coastguard WorkerNanopb example "using_union_messages"
2*c8d645caSAndroid Build Coastguard Worker=====================================
3*c8d645caSAndroid Build Coastguard Worker
4*c8d645caSAndroid Build Coastguard WorkerUnion messages is a common technique in Google Protocol Buffers used to
5*c8d645caSAndroid Build Coastguard Workerrepresent a group of messages, only one of which is passed at a time.
6*c8d645caSAndroid Build Coastguard WorkerIt is described in Google's documentation:
7*c8d645caSAndroid Build Coastguard Workerhttps://developers.google.com/protocol-buffers/docs/techniques#union
8*c8d645caSAndroid Build Coastguard Worker
9*c8d645caSAndroid Build Coastguard WorkerThis directory contains an example on how to encode and decode union messages
10*c8d645caSAndroid Build Coastguard Workerwith minimal memory usage. Usually, nanopb would allocate space to store
11*c8d645caSAndroid Build Coastguard Workerall of the possible messages at the same time, even though at most one of
12*c8d645caSAndroid Build Coastguard Workerthem will be used at a time.
13*c8d645caSAndroid Build Coastguard Worker
14*c8d645caSAndroid Build Coastguard WorkerBy using some of the lower level nanopb APIs, we can manually generate the
15*c8d645caSAndroid Build Coastguard Workertop level message, so that we only need to allocate the one submessage that
16*c8d645caSAndroid Build Coastguard Workerwe actually want. Similarly when decoding, we can manually read the tag of
17*c8d645caSAndroid Build Coastguard Workerthe top level message, and only then allocate the memory for the submessage
18*c8d645caSAndroid Build Coastguard Workerafter we already know its type.
19*c8d645caSAndroid Build Coastguard Worker
20*c8d645caSAndroid Build Coastguard WorkerNOTE: There is a newer protobuf feature called `oneof` that is also supported
21*c8d645caSAndroid Build Coastguard Workerby nanopb. It might be a better option for new code.
22*c8d645caSAndroid Build Coastguard Worker
23*c8d645caSAndroid Build Coastguard Worker
24*c8d645caSAndroid Build Coastguard WorkerExample usage
25*c8d645caSAndroid Build Coastguard Worker-------------
26*c8d645caSAndroid Build Coastguard Worker
27*c8d645caSAndroid Build Coastguard WorkerType `make` to run the example. It will build it and run commands like
28*c8d645caSAndroid Build Coastguard Workerfollowing:
29*c8d645caSAndroid Build Coastguard Worker
30*c8d645caSAndroid Build Coastguard Worker./encode 1 | ./decode
31*c8d645caSAndroid Build Coastguard WorkerGot MsgType1: 42
32*c8d645caSAndroid Build Coastguard Worker./encode 2 | ./decode
33*c8d645caSAndroid Build Coastguard WorkerGot MsgType2: true
34*c8d645caSAndroid Build Coastguard Worker./encode 3 | ./decode
35*c8d645caSAndroid Build Coastguard WorkerGot MsgType3: 3 1415
36*c8d645caSAndroid Build Coastguard Worker
37*c8d645caSAndroid Build Coastguard WorkerThis simply demonstrates that the "decode" program has correctly identified
38*c8d645caSAndroid Build Coastguard Workerthe type of the received message, and managed to decode it.
39*c8d645caSAndroid Build Coastguard Worker
40*c8d645caSAndroid Build Coastguard Worker
41*c8d645caSAndroid Build Coastguard WorkerDetails of implementation
42*c8d645caSAndroid Build Coastguard Worker-------------------------
43*c8d645caSAndroid Build Coastguard Worker
44*c8d645caSAndroid Build Coastguard Workerunionproto.proto contains the protocol used in the example. It consists of
45*c8d645caSAndroid Build Coastguard Workerthree messages: MsgType1, MsgType2 and MsgType3, which are collected together
46*c8d645caSAndroid Build Coastguard Workerinto UnionMessage.
47*c8d645caSAndroid Build Coastguard Worker
48*c8d645caSAndroid Build Coastguard Workerencode.c takes one command line argument, which should be a number 1-3. It
49*c8d645caSAndroid Build Coastguard Workerthen fills in and encodes the corresponding message, and writes it to stdout.
50*c8d645caSAndroid Build Coastguard Worker
51*c8d645caSAndroid Build Coastguard Workerdecode.c reads a UnionMessage from stdin. Then it calls the function
52*c8d645caSAndroid Build Coastguard Workerdecode_unionmessage_type() to determine the type of the message. After that,
53*c8d645caSAndroid Build Coastguard Workerthe corresponding message is decoded and the contents of it printed to the
54*c8d645caSAndroid Build Coastguard Workerscreen.
55*c8d645caSAndroid Build Coastguard Worker
56