b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Stef Walter <stefw@redhat.com>
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Copyright (C) 2014 Red Hat
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# This program is free software; you can redistribute it and/or modify
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# it under the terms of the GNU General Public License as published by
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# the Free Software Foundation; either version 3 of the License, or
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# (at your option) any later version.
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# This program is distributed in the hope that it will be useful,
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# but WITHOUT ANY WARRANTY; without even the implied warranty of
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# GNU General Public License for more details.
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# You should have received a copy of the GNU General Public License
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# along with this program. If not, see <http://www.gnu.org/licenses/>.
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Some parser code from GLib
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Copyright (C) 2008-2011 Red Hat, Inc.
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# This library is free software; you can redistribute it and/or
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# modify it under the terms of the GNU Lesser General Public
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# License as published by the Free Software Foundation; either
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# version 2 of the License, or (at your option) any later version.
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# This library is distributed in the hope that it will be useful,
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# but WITHOUT ANY WARRANTY; without even the implied warranty of
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Lesser General Public License for more details.
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# You should have received a copy of the GNU Lesser General
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Public License along with this library; if not, write to the
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Boston, MA 02111-1307, USA.
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Portions by: David Zeuthen <davidz@redhat.com>
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# DBus interfaces are defined here:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# http://dbus.freedesktop.org/doc/dbus-specification.html#introspection-format
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# The introspection data format has become the standard way to represent a
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# DBus interface. For many examples see /usr/share/dbus-1/interfaces/ on a
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# typical linux machine.
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# A word about annotations. These are extra flags or values that can be
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# assigned to anything. So far, the codegen supports this annotation:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# - An annotation specified in the specification that tells us what C symbol
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# to generate for a given interface or method. By default the codegen will
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# build up a symbol name from the DBus name.
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# -----------------------------------------------------------------------------
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter # Lets us print problems like a compiler would
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter return "%s:%d: %s" % (self.file, self.line, message)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter return self.annotations.get("org.freedesktop.DBus.GLib.CSymbol", self.name)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter# The basic types that we support marshalling right now. These
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter# are the ones we can pass as basic arguments to libdbus directly.
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter# If the dbus and sssd types are identical we pass things directly.
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter# otherwise some copying is necessary.
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 'y': ( "DBUS_TYPE_BYTE", "uint8_t", "uint8_t" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 'b': ( "DBUS_TYPE_BOOLEAN", "dbus_bool_t", "bool" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 'n': ( "DBUS_TYPE_INT16", "int16_t", "int16_t" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 'q': ( "DBUS_TYPE_UINT16", "uint16_t", "uint16_t" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 'i': ( "DBUS_TYPE_INT32", "int32_t", "int32_t" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 'u': ( "DBUS_TYPE_UINT32", "uint32_t", "uint32_t" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 'x': ( "DBUS_TYPE_INT64", "int64_t", "int64_t" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 't': ( "DBUS_TYPE_UINT64", "uint64_t", "uint64_t" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 'd': ( "DBUS_TYPE_DOUBLE", "double", "double" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 's': ( "DBUS_TYPE_STRING", "const char *", "const char *" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter 'o': ( "DBUS_TYPE_OBJECT_PATH", "const char *", "const char *" ),
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter type = type[1:]
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter (self.dbus_constant, self.dbus_type, self.sssd_type) = BASIC_TYPES[type]
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter # If types are not identical, we can't do array (yet)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter self.is_basic = (self.dbus_type == self.sssd_type)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter if not self.only_basic_args() and not self.use_raw_handler():
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter raise DBusXmlException("Method has complex arguments and requires " +
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter "the 'org.freedesktop.sssd.RawHandler' annotation")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter return "%s_%s" % (self.iface.c_name(), self.c_name())
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter return self.annotations.get(anno, self.iface.annotations.get(anno)) == 'true'
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter return "".join([arg.type for arg in self.in_args])
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter return "%s_%s" % (self.iface.c_name(), self.c_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter raise DBusXmlException('Invalid access type %s'%self.access)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter return "%s_%s" % (self.iface.c_name(), self.c_name())
ac744223411099a862a747e7168a30255c003bf7Pavel Březina sig = "void (*%s)(struct sbus_request *, void *data, " % (name)
ac744223411099a862a747e7168a30255c003bf7Pavel Březina return "sbus_invoke_get_%s" % self.get_invoker_name()
ac744223411099a862a747e7168a30255c003bf7Pavel Březina return self.get_invoker_signature(self.getter_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter return self.annotations.get("org.freedesktop.DBus.GLib.CSymbol",
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# -----------------------------------------------------------------------------
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# Code Generation
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter # NOTE: Would like to use the following syntax for this function
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter # but need to wait until python3 until it is supported:
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter assert not kwargs, "unknown keyword argument(s): %s" % str(kwargs)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walterdef method_function_pointer(meth, name, with_names=False):
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter return "int (*%s)(struct sbus_request *%s, void *%s%s)" % \
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("/* invokes a handler with a '%s' DBus signature */", signature)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("static int invoke_%s_method(struct sbus_request *dbus_req, void *function_ptr);", signature)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("/* invokes a handler with a '%s' DBus signature */", signature)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("static int invoke_%s_method(struct sbus_request *dbus_req, void *function_ptr)", signature)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" int (*handler)(struct sbus_request *, void *%s) = function_ptr;", method_arg_types(args))
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" if (!sbus_request_parse_or_finish(dbus_req,")
66277b21179d95f6e96abed01a20ccbccf27ce99Pavel Březina out(" return (handler)(dbus_req, dbus_req->intf->handler_data", new_line=False)
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozek prefix = "%s_" % prop.type if type_prefix else ""
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozek out(" %s *%sprop_val;", prop.sssd_type, prefix)
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozekdef source_prop_handler(prop, type_prefix=False):
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozek prefix = "%s_" % prop.type if type_prefix else ""
4f7f714e118e95896fac5239c7a8b529c39a4758Jakub Hrozek out(" %s", prop.getter_signature("%shandler" % prefix), new_line=False)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("int %s_finish(struct sbus_request *req%s)",
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter meth.fq_c_name(), method_arg_types(meth.out_args, with_names=True))
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" %s cast_%s = arg_%s;", arg.dbus_type, arg.c_name(), arg.c_name())
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" return sbus_request_return_and_finish(req,")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("%s, &cast_%s,", arg.dbus_constant, arg.c_name())
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("%s, &arg_%s,", arg.dbus_constant, arg.c_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("/* arguments for %s.%s */", parent.iface.name, parent.name)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("const struct sbus_arg_meta %s%s[] = {", parent.fq_c_name(), suffix)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("const struct sbus_method_meta %s__methods[] = {", iface.c_name())
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out(" offsetof(struct %s, %s),", iface.c_name(), meth.c_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("const struct sbus_signal_meta %s__signals[] = {", iface.c_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("const struct sbus_property_meta %s__properties[] = {", iface.c_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" SBUS_PROPERTY_READABLE | SBUS_PROPERTY_WRITABLE,")
90e04eae7e54ec892a6f239783df94dab5d1ed9aJakub Hrozek out(" offsetof(struct %s, %s),", iface.c_name(), prop.getter_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("extern const struct sbus_interface_meta %s_meta;", iface.c_name())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("const struct sbus_interface_meta %s_meta = {", iface.c_name())
df4e1db5d41c903ae57fd880acc76a0ad84aa7b2Pavel Březina out(" sbus_invoke_get_all, /* GetAll invoker */")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walterdef generate_source(ifaces, filename, include_header=None):
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("/* The following definitions are auto-generated from %s */", basename)
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out("#include \"%s\"", os.path.basename(include_header))
90e04eae7e54ec892a6f239783df94dab5d1ed9aJakub Hrozek meth_invokers = forward_method_invokers(ifaces)
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out("int %s_finish(struct sbus_request *req%s);",
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter meth.fq_c_name(), method_arg_types(meth.out_args, with_names=True))
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out(" struct sbus_vtable vtable; /* derive from sbus_vtable */")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" %s;", method_function_pointer(meth, meth.c_name(), with_names=True))
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter out("#define %s \"%s\"", iface.c_name().upper(), iface.name)
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter out("#define %s \"%s\"", meth.fq_c_name().upper(), meth.name)
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter out("#define %s \"%s\"", sig.fq_c_name().upper(), sig.name)
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter out("#define %s \"%s\"", prop.fq_c_name().upper(), prop.name)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter guard = "__%s__" % re.sub(r'([^_A-Z0-9])', "_", basename.upper())
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("/* The following declarations are auto-generated from %s */", basename)
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter out("/* ------------------------------------------------------------------------")
c2cc119de8eac712c040b3993f41c967ff2278deStef Walter out(" * Various constants of interface and method names mostly for use by clients")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out("/* ------------------------------------------------------------------------")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out(" * These structures are filled in by implementors of the different")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out(" * dbus interfaces to handle method calls.")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out(" * Handler functions of type sbus_msg_handler_fn accept raw messages,")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" * other handlers are typed appropriately. If a handler that is")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out(" * set to NULL is invoked it will result in a")
fcd8093c58638dc7c4f9cddfc97f273b94ce2eadStef Walter out(" * org.freedesktop.DBus.Error.NotSupported error for the caller.")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" * Handlers have a matching xxx_finish() function (unless the method has")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" * accepts raw messages). These finish functions the")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" * sbus_request_return_and_finish() with the appropriate arguments to")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" * construct a valid reply. Once a finish function has been called, the")
dff909d473f43a6bd0f0286fa2d279c0ebe945c6Stef Walter out(" * @dbus_req it was called with is freed and no longer valid.")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out("/* ------------------------------------------------------------------------")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" * These structure definitions are filled in with the information about")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" * the interfaces, methods, properties and so on.")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter out(" * The actual definitions are found in the accompanying C file next")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# -----------------------------------------------------------------------------
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter# XML Interface Parsing
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter raise DBusXmlException("Missing attribute '%s'" % name)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter raise DBusXmlException("Empty attribute '%s'" % name)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter parser.CharacterDataHandler = self.handle_char_data
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter parser.StartElementHandler = self.handle_start_element
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter parser.EndElementHandler = self.handle_end_element
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter elif self.cur_object and name == STATE_ANNOTATION:
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter self.cur_object.annotations[expect_attr(attrs, 'name')] = val
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter method = Method(self.cur_object, expect_attr(attrs, 'name'))
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter signal = Signal(self.cur_object, expect_attr(attrs, 'name'))
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter raise DBusXmlException('Invalid direction "%s"' % direction)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter assert False, 'Unhandled state "%s" while entering element with name "%s"' % (self.state, name)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter parser = optparse.OptionParser("usage: %prog [options] introspect.xml ...")
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter parser.set_description("sbus_codegen generates sbus interface structures \
e239b5bedd877e6c5002b22ea11926a12b4c781cLukas Slebodnik print("sbus_codegen: no input file specified", file=sys.stderr)
e239b5bedd877e6c5002b22ea11926a12b4c781cLukas Slebodnik print("sbus_codegen: specify --mode=header or --mode=source", file=sys.stderr)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter generate_header(parser.parsed_interfaces, filename)