about message type id's:
------------------------

nanopb upstream (https://code.google.com/p/nanopb/source/list) as of
commit 62a7e4ff2b34 has introduced a key feature: it is now possible
to associate a msgid option with each message, and have those id's
emitted by the nanopb generator such that a descriptor mapping
descriptors, msgid's, names, and other paramters can be automatically
constructed.

Thus we can associate an integer ID with each message, which is useful
for runtime type checking of messages. There is no runtime overhead for
this option.

A typical usage scenario would be with multiframe ringbuffers, when
using different protobuf messages: the multiframe flags field holds
the type id, which is used to determine how to actually process the
frame's content.

msgid's have to be unique across all messages and all proto files. This must be maintained manually.

The absolute value of the msgid option is irrelevant as it is only used as a key; uniqueness counts. msgid's need not be sequential.

Any msgid value must be between 1 and msgidType.MSGID_MAX. This is
checked at startup time in libmkproto and will result in an assert if
violated. Values above MSGID_MAX to 4095 are reserved message ID
values, see enum msgidType in types.proto .The reason is the
limited-size field (12 bits) in the multiframe ring flag (see
mfields_t).

Convention for adding msgid's:
------------------------------

Since no proto file is expected to reasonable have more than 100 message definitions, each proto file gets a base message id, recorded in a comment on the top of the file:

e.g. canon.proto:
        // msgid base: 100

config.proto:
	// msgid base: 200

and so forth.

To tag a message with a msgid, proceed like so (using canon.proto as example):

// make sure nanopb.proto is imported as this defines the option

import "nanopb.proto";
// msgid base: 100

// for each message, add the option like so:
message Emc_Traj_Set_G5x {

    option (nanopb_msgopt).msgid = 101; // assign sequentially for all messages

    required EmcPose         origin = 10;
    required OriginIndex   g5x_index = 20;
}

Accessing the msgid if the message name is known:
-------------------------------------------------

Google Protobuf, Python bindings:

def msgid(m):
    return m.DESCRIPTOR.GetOptions().Extensions[nanopb_msgopt].msgid


Google Protobuf, C++ bindings:

uint32 msgid(const ::google::protobuf::Message  &m)
{
  const ::google::protobuf::MessageOptions& options = m.GetDescriptor()->options();
  return options.GetExtension(nanopb_msgopt).msgid();
}

Nanopb:

use the <messagename>_msgid symbol which defines an integer constant

NB: due to the use of 'package machinetalk;', for example, the nanopb symbol for
message 'Container' is 'pb_Container_msgid'
