-
- yield '/* Default values for struct fields */\n'
- for msg in messages:
- yield msg.default_decl(True)
- yield '\n'
-
- yield '/* Initializer values for message structs */\n'
- for msg in messages:
- identifier = '%s_init_default' % msg.name
- yield '#define %-40s %s\n' % (identifier, msg.get_initializer(False))
- for msg in messages:
- identifier = '%s_init_zero' % msg.name
- yield '#define %-40s %s\n' % (identifier, msg.get_initializer(True))
- yield '\n'
-
- yield '/* Field tags (for use in manual encoding/decoding) */\n'
- for msg in sort_dependencies(messages):
- for field in msg.fields:
- yield field.tags()
- for extension in extensions:
- yield extension.tags()
- yield '\n'
-
- yield '/* Struct field encoding specification for nanopb */\n'
- for msg in messages:
- yield msg.fields_declaration() + '\n'
- yield '\n'
-
- yield '/* Maximum encoded size of messages (where known) */\n'
- for msg in messages:
- msize = msg.encoded_size(messages)
- if msize is not None:
- identifier = '%s_size' % msg.name
- yield '#define %-40s %s\n' % (identifier, msize)
- yield '\n'
-
- yield '/* helper macros for message type ids if set with */\n'
- yield '/* option (nanopb_msgopt).msgid = <id>; */\n\n'
-
- yield '#ifdef PB_MSGID\n'
- for msg in messages:
- if hasattr(msg,'msgid'):
- yield '#define PB_MSG_%d %s\n' % (msg.msgid, msg.name)
- yield '\n'
-
- symbol = make_identifier(headername.split('.')[0])
- yield '#define %s_MESSAGES \\\n' % symbol
-
- for msg in messages:
- m = "-1"
- msize = msg.encoded_size(messages)
- if msize is not None:
- m = msize
- if hasattr(msg,'msgid'):
- yield '\tPB_MSG(%d,%s,%s) \\\n' % (msg.msgid, m, msg.name)
- yield '\n'
-
- yield '#endif\n\n'
-
-
- yield '#ifdef __cplusplus\n'
- yield '} /* extern "C" */\n'
- yield '#endif\n'
-
- # End of header
- yield '\n#endif\n'
-
-def generate_source(headername, enums, messages, extensions, options):
- '''Generate content for a source file.'''
-
- yield '/* Automatically generated nanopb constant definitions */\n'
- if options.notimestamp:
- yield '/* Generated by %s */\n\n' % (nanopb_version)
- else:
- yield '/* Generated by %s at %s. */\n\n' % (nanopb_version, time.asctime())
- yield options.genformat % (headername)
- yield '\n'
-
- yield '#if PB_PROTO_HEADER_VERSION != 30\n'
- yield '#error Regenerate this file with the current version of nanopb generator.\n'
- yield '#endif\n'
- yield '\n'
-
- for msg in messages:
- yield msg.default_decl(False)
-
- yield '\n\n'
-
- for msg in messages:
- yield msg.fields_definition() + '\n\n'
-
- for ext in extensions:
- yield ext.extension_def() + '\n'
-
- # Add checks for numeric limits
- if messages:
- largest_msg = max(messages, key = lambda m: m.count_required_fields())
- largest_count = largest_msg.count_required_fields()
- if largest_count > 64:
- yield '\n/* Check that missing required fields will be properly detected */\n'
- yield '#if PB_MAX_REQUIRED_FIELDS < %d\n' % largest_count
- yield '#error Properly detecting missing required fields in %s requires \\\n' % largest_msg.name
- yield ' setting PB_MAX_REQUIRED_FIELDS to %d or more.\n' % largest_count
- yield '#endif\n'
-
- worst = 0
- worst_field = ''
- checks = []
- checks_msgnames = []
- for msg in messages:
- checks_msgnames.append(msg.name)
- for field in msg.fields:
- status = field.largest_field_value()
- if isinstance(status, (str, unicode)):
- checks.append(status)
- elif status > worst:
- worst = status
- worst_field = str(field.struct_name) + '.' + str(field.name)
-
- if worst > 255 or checks:
- yield '\n/* Check that field information fits in pb_field_t */\n'
-
- if worst > 65535 or checks:
- yield '#if !defined(PB_FIELD_32BIT)\n'
- if worst > 65535:
- yield '#error Field descriptor for %s is too large. Define PB_FIELD_32BIT to fix this.\n' % worst_field
- else:
- assertion = ' && '.join(str(c) + ' < 65536' for c in checks)
- msgs = '_'.join(str(n) for n in checks_msgnames)
- yield '/* If you get an error here, it means that you need to define PB_FIELD_32BIT\n'
- yield ' * compile-time option. You can do that in pb.h or on compiler command line.\n'
- yield ' * \n'
- yield ' * The reason you need to do this is that some of your messages contain tag\n'
- yield ' * numbers or field sizes that are larger than what can fit in 8 or 16 bit\n'
- yield ' * field descriptors.\n'
- yield ' */\n'
- yield 'PB_STATIC_ASSERT((%s), YOU_MUST_DEFINE_PB_FIELD_32BIT_FOR_MESSAGES_%s)\n'%(assertion,msgs)
- yield '#endif\n\n'
-
- if worst < 65536:
- yield '#if !defined(PB_FIELD_16BIT) && !defined(PB_FIELD_32BIT)\n'
- if worst > 255:
- yield '#error Field descriptor for %s is too large. Define PB_FIELD_16BIT to fix this.\n' % worst_field
- else:
- assertion = ' && '.join(str(c) + ' < 256' for c in checks)
- msgs = '_'.join(str(n) for n in checks_msgnames)
- yield '/* If you get an error here, it means that you need to define PB_FIELD_16BIT\n'
- yield ' * compile-time option. You can do that in pb.h or on compiler command line.\n'
- yield ' * \n'
- yield ' * The reason you need to do this is that some of your messages contain tag\n'
- yield ' * numbers or field sizes that are larger than what can fit in the default\n'
- yield ' * 8 bit descriptors.\n'
- yield ' */\n'
- yield 'PB_STATIC_ASSERT((%s), YOU_MUST_DEFINE_PB_FIELD_16BIT_FOR_MESSAGES_%s)\n'%(assertion,msgs)
+
+ yield '#ifdef __cplusplus\n'
+ yield 'extern "C" {\n'
+ yield '#endif\n\n'
+
+ if self.enums:
+ yield '/* Enum definitions */\n'
+ for enum in self.enums:
+ yield str(enum) + '\n\n'
+
+ if self.messages:
+ yield '/* Struct definitions */\n'
+ for msg in sort_dependencies(self.messages):
+ yield msg.types()
+ yield str(msg) + '\n\n'
+
+ if self.extensions:
+ yield '/* Extensions */\n'
+ for extension in self.extensions:
+ yield extension.extension_decl()
+ yield '\n'
+
+ if self.messages:
+ yield '/* Default values for struct fields */\n'
+ for msg in self.messages:
+ yield msg.default_decl(True)
+ yield '\n'
+
+ yield '/* Initializer values for message structs */\n'
+ for msg in self.messages:
+ identifier = '%s_init_default' % msg.name
+ yield '#define %-40s %s\n' % (identifier, msg.get_initializer(False))
+ for msg in self.messages:
+ identifier = '%s_init_zero' % msg.name
+ yield '#define %-40s %s\n' % (identifier, msg.get_initializer(True))
+ yield '\n'
+
+ yield '/* Field tags (for use in manual encoding/decoding) */\n'
+ for msg in sort_dependencies(self.messages):
+ for field in msg.fields:
+ yield field.tags()
+ for extension in self.extensions:
+ yield extension.tags()
+ yield '\n'
+
+ yield '/* Struct field encoding specification for nanopb */\n'
+ for msg in self.messages:
+ yield msg.fields_declaration() + '\n'
+ yield '\n'
+
+ yield '/* Maximum encoded size of messages (where known) */\n'
+ for msg in self.messages:
+ msize = msg.encoded_size(self.dependencies)
+ if msize is not None:
+ identifier = '%s_size' % msg.name
+ yield '#define %-40s %s\n' % (identifier, msize)
+ yield '\n'
+
+ yield '/* Message IDs (where set with "msgid" option) */\n'
+
+ yield '#ifdef PB_MSGID\n'
+ for msg in self.messages:
+ if hasattr(msg,'msgid'):
+ yield '#define PB_MSG_%d %s\n' % (msg.msgid, msg.name)
+ yield '\n'
+
+ symbol = make_identifier(headername.split('.')[0])
+ yield '#define %s_MESSAGES \\\n' % symbol
+
+ for msg in self.messages:
+ m = "-1"
+ msize = msg.encoded_size(self.dependencies)
+ if msize is not None:
+ m = msize
+ if hasattr(msg,'msgid'):
+ yield '\tPB_MSG(%d,%s,%s) \\\n' % (msg.msgid, m, msg.name)
+ yield '\n'
+
+ for msg in self.messages:
+ if hasattr(msg,'msgid'):
+ yield '#define %s_msgid %d\n' % (msg.name, msg.msgid)
+ yield '\n'
+