'''Generate header file for nanopb from a ProtoBuf FileDescriptorSet.'''
+nanopb_version = "nanopb-0.1.7-dev"
try:
import google.protobuf.descriptor_pb2 as descriptor
# Generation of single fields
# ---------------------------------------------------------------------------
-
+import time
import os.path
# Values are tuple (c type, pb ltype)
class Enum:
def __init__(self, names, desc, enum_options):
'''desc is EnumDescriptorProto'''
- self.names = names + desc.name
+
+ if enum_options.long_names:
+ self.names = names + desc.name
+ else:
+ self.names = names
+
self.values = [(self.names + x.name, x.number) for x in desc.value]
def __str__(self):
base_name = Names()
for enum in fdesc.enum_type:
- enums.append(Enum(base_name, enum, file_options))
+ enum_options = get_nanopb_suboptions(enum, file_options)
+ enums.append(Enum(base_name, enum, enum_options))
for names, message in iterate_messages(fdesc, base_name):
message_options = get_nanopb_suboptions(message, file_options)
'''
yield '/* Automatically generated nanopb header */\n'
+ yield '/* Generated by %s at %s. */\n\n' % (nanopb_version, time.asctime())
symbol = headername.replace('.', '_').upper()
yield '#ifndef _PB_%s_\n' % symbol
for dependency in dependencies:
noext = os.path.splitext(dependency)[0]
yield '#include "%s.pb.h"\n' % noext
- yield '\n'
+
+ yield '#ifdef __cplusplus\n'
+ yield 'extern "C" {\n'
+ yield '#endif\n\n'
yield '/* Enum definitions */\n'
for enum in enums:
yield 'STATIC_ASSERT((%s), YOU_MUST_DEFINE_PB_FIELD_32BIT)\n' % assertion
yield '#endif\n'
+ yield '\n#ifdef __cplusplus\n'
+ yield '} /* extern "C" */\n'
+ yield '#endif\n'
+
# End of header
yield '\n#endif\n'
'''Generate content for a source file.'''
yield '/* Automatically generated nanopb constant definitions */\n'
+ yield '/* Generated by %s at %s. */\n\n' % (nanopb_version, time.asctime())
yield '#include "%s"\n\n' % headername
for msg in messages:
ext_type = nanopb_pb2.nanopb_fileopt
elif isinstance(subdesc.options, descriptor.MessageOptions):
ext_type = nanopb_pb2.nanopb_msgopt
+ elif isinstance(subdesc.options, descriptor.EnumOptions):
+ ext_type = nanopb_pb2.nanopb_enumopt
else:
raise Exception("Unknown options type")