sbus_codegen revision 66277b21179d95f6e96abed01a20ccbccf27ce99
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson# Stef Walter <stefw@redhat.com>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson# Copyright (C) 2014 Red Hat
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews# This program is free software; you can redistribute it and/or modify
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews# it under the terms of the GNU General Public License as published by
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews# the Free Software Foundation; either version 3 of the License, or
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews# (at your option) any later version.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews# This program is distributed in the hope that it will be useful,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews# but WITHOUT ANY WARRANTY; without even the implied warranty of
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson# GNU General Public License for more details.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein# You should have received a copy of the GNU General Public License
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein# along with this program. If not, see <http://www.gnu.org/licenses/>.
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson# Some parser code from GLib
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews# Copyright (C) 2008-2011 Red Hat, Inc.
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews# This library is free software; you can redistribute it and/or
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence# modify it under the terms of the GNU Lesser General Public
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews# License as published by the Free Software Foundation; either
d8e34837cd6c88c42b3ecdb9107a43ecf8252e79David Lawrence# version 2 of the License, or (at your option) any later version.
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews# This library is distributed in the hope that it will be useful,
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews# but WITHOUT ANY WARRANTY; without even the implied warranty of
364a82f7c25b62967678027043425201a5e5171aBob Halley# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson# Lesser General Public License for more details.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence# You should have received a copy of the GNU Lesser General
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews# Public License along with this library; if not, write to the
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews# Boston, MA 02111-1307, USA.
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews# Portions by: David Zeuthen <davidz@redhat.com>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson# DBus interfaces are defined here:
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence# http://dbus.freedesktop.org/doc/dbus-specification.html#introspection-format
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson# The introspection data format has become the standard way to represent a
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson# DBus interface. For many examples see /usr/share/dbus-1/interfaces/ on a
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson# typical linux machine.
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews# A word about annotations. These are extra flags or values that can be
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews# assigned to anything. So far, the codegen supports this annotation:
600cfa2ba4c50017581b6c14e3a688a82ecebbe0David Lawrence# - An annotation specified in the specification that tells us what C symbol
600cfa2ba4c50017581b6c14e3a688a82ecebbe0David Lawrence# to generate for a given interface or method. By default the codegen will
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson# build up a symbol name from the DBus name.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson# -----------------------------------------------------------------------------
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson # Lets us print problems like a compiler would
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return "%s:%d: %s" % (self.file, self.line, message)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson raise DBusXmlException('No name on element')
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return self.annotations.get("org.freedesktop.DBus.GLib.CSymbol", self.name)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson# The basic types that we support marshalling right now. These
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson# are the ones we can pass as basic arguments to libdbus directly.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson# If the dbus and sssd types are identical we pass things directly.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson# otherwise some copying is necessary.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson 'y': ( "DBUS_TYPE_BYTE", "uint8_t", "uint8_t" ),
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson 'b': ( "DBUS_TYPE_BOOLEAN", "dbus_bool_t", "bool" ),
7bf91d95eb5a49117d5080bbd03839b9de5c8423Andreas Gustafsson 'n': ( "DBUS_TYPE_INT16", "int16_t", "int16_t" ),
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington 'q': ( "DBUS_TYPE_UINT16", "uint16_t", "uint16_t" ),
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington 'i': ( "DBUS_TYPE_INT32", "int32_t", "int32_t" ),
d0aebc5a55b6145297d94f8aee939852357c59fcMark Andrews 'u': ( "DBUS_TYPE_UINT32", "uint32_t", "uint32_t" ),
d0aebc5a55b6145297d94f8aee939852357c59fcMark Andrews 'x': ( "DBUS_TYPE_INT64", "int64_t", "int64_t" ),
d0aebc5a55b6145297d94f8aee939852357c59fcMark Andrews 't': ( "DBUS_TYPE_UINT64", "uint64_t", "uint64_t" ),
d0aebc5a55b6145297d94f8aee939852357c59fcMark Andrews 'd': ( "DBUS_TYPE_DOUBLE", "double", "double" ),
d0aebc5a55b6145297d94f8aee939852357c59fcMark Andrews 's': ( "DBUS_TYPE_STRING", "const char *", "const char *" ),
d0aebc5a55b6145297d94f8aee939852357c59fcMark Andrews 'o': ( "DBUS_TYPE_OBJECT_PATH", "const char *", "const char *" ),
2883651930dc85cacae940fe2a81277dfc14807dBrian Wellington type = type[1:]
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson (self.dbus_constant, self.dbus_type, self.sssd_type) = BASIC_TYPES[type]
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson # If types are not identical, we can't do array (yet)
2883651930dc85cacae940fe2a81277dfc14807dBrian Wellington self.is_basic = (self.dbus_type == self.sssd_type)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if not self.only_basic_args() and not self.use_raw_handler():
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson raise DBusXmlException("Method has complex arguments and requires " +
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson "the 'org.freedesktop.sssd.RawHandler' annotation")
68e4926b2262571e004b4be00b905ec776c01d9cMichael Graff return "%s_%s" % (self.iface.c_name(), self.c_name())
68e4926b2262571e004b4be00b905ec776c01d9cMichael Graff return self.annotations.get(anno, self.iface.annotations.get(anno)) == 'true'
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews return "".join([arg.type for arg in self.in_args])
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews return "%s_%s" % (self.iface.c_name(), self.c_name())
d788d738e3d29037651b42566519c9a0a66ba219Mark Andrews raise DBusXmlException('Invalid access type %s'%self.access)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence return "%s_%s" % (self.iface.c_name(), self.c_name())
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return "%s_get_%s" % (self.iface.c_name(), self.c_name())
42a5f9c8f535fb2a6d1cbfaa38533176e1f1667aBob Halley sig = "void (*%s)(struct sbus_request *, void *data, %s *" % (name, self.sssd_type)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence return self.annotations.get("org.freedesktop.DBus.GLib.CSymbol",
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson# -----------------------------------------------------------------------------
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence# Code Generation
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson # NOTE: Would like to use the following syntax for this function
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson # but need to wait until python3 until it is supported:
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson assert not kwargs, "unknown keyword argument(s): %s" % str(kwargs)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssondef method_arg_types(args, with_names=False):
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssondef method_function_pointer(meth, name, with_names=False):
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return "int (*%s)(struct sbus_request *%s, void *%s%s)" % \
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson method_arg_types(meth.in_args, with_names))
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return prop.getter_signature(prop.getter_name())
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff out("/* invokes a handler with a '%s' DBus signature */", signature)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence out("static int invoke_%s_method(struct sbus_request *dbus_req, void *function_ptr);", signature)
ec7493d8d1966a3dc5f5306fc0a96519e0de6dceAndreas Gustafssondef source_method_invoker(signature, args):
ec7493d8d1966a3dc5f5306fc0a96519e0de6dceAndreas Gustafsson out("/* invokes a handler with a '%s' DBus signature */", signature)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence out("static int invoke_%s_method(struct sbus_request *dbus_req, void *function_ptr)", signature)
42a5f9c8f535fb2a6d1cbfaa38533176e1f1667aBob Halley out(" int (*handler)(struct sbus_request *, void *%s) = function_ptr;", method_arg_types(args))
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley out(" if (!sbus_request_parse_or_finish(dbus_req,")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" DBUS_TYPE_ARRAY, %s, &arg_%d, &len_%d,",
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" %s, &arg_%d,", arg.dbus_constant, i)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence out(" return (handler)(dbus_req, dbus_req->intf->handler_data", new_line=False)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencedef source_prop_types(prop, type_prefix=False):
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence prefix = "%s_" % prop.type if type_prefix else ""
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" %s *%sout_val;", prop.dbus_type, prefix)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" %s %sprop_val;", prop.sssd_type, prefix)
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson out(" %s %sout_val;", prop.dbus_type, prefix)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssondef source_prop_handler(prop, type_prefix=False):
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson prefix = "%s_" % prop.type if type_prefix else ""
692ae2fff922f1c072169d1ddda8e600cb572a9bMark Andrews out(" %s", prop.getter_signature("%shandler" % prefix), new_line=False)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out("/* invokes a getter with an array of '%s' DBus type */", prop.dbus_type)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out("/* invokes a getter with a '%s' DBus type */", prop.dbus_type)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out("static int %s(struct sbus_request *dbus_req, void *function_ptr)",
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson out(" %s", prop.getter_signature("handler"), new_line=False)
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson out(" (handler)(dbus_req, dbus_req->intf->handler_data, &prop_val", new_line=False)
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson out(" out_val = prop_val == NULL ? \"\" : prop_val;")
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson out(" out_val = prop_val == NULL ? \"/\" : prop_val;")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" return sbus_request_return_array_as_variant(dbus_req, %s, (uint8_t*)out_val, prop_len, sizeof(%s));", prop.dbus_constant, prop.sssd_type)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" return sbus_request_return_as_variant(dbus_req, %s, &out_val);", prop.dbus_constant)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssondef source_getall_invoker(iface, prop_invokers):
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out("/* invokes GetAll for the '%s' interface */", iface.name)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out("static int invoke_%s_get_all(struct sbus_request *dbus_req, void *function_ptr)",
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" struct sbus_interface *intf = dbus_req->intf;")
11c7a43642def4aaa2bed001ff018a0bb0b65c29Danny Mayer out(" const struct sbus_property_meta *property;")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson iface_types = [ p.type for p in iface.properties ]
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence for prop in [ p for p in prop_invokers.values() if p.type in iface_types ]:
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson source_prop_types(prop, type_prefix=True)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" reply = dbus_message_new_method_return(dbus_req->message);")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" dbus_message_iter_init_append(reply, &iter);")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" dbret = dbus_message_iter_open_container(")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" DBUS_DICT_ENTRY_END_CHAR_AS_STRING,")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" property = sbus_meta_find_property(intf->vtable->meta, \"%s\");", prop.c_name())
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" if (property != NULL && property->flags & SBUS_PROPERTY_READABLE) {")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" %s_handler = VTABLE_FUNC(intf->vtable, property->vtable_offset_get);", prop.type)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" (%s_handler)(dbus_req, dbus_req->intf->handler_data, &%s_prop_val", prop.type, prop.type, new_line=False)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence out(", &%s_prop_len", prop.type, new_line=False)
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews out(" %s_out_val = %s_prop_val == NULL ? \"\" : %s_prop_val;",
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" %s_out_val = %s_prop_val == NULL ? \"/\" : %s_prop_val;",
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" %s_out_val = %s_prop_val;", prop.type, prop.type)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" ret = sbus_add_array_as_variant_to_dict(&iter_dict, \"%s\", %s, (uint8_t*)%s_out_val, %s_prop_len, sizeof(%s));", prop.c_name(), prop.dbus_constant, prop.type, prop.type, prop.sssd_type)
806c235ecf533b98d068b3f8df9d7abbe1e30cf9Mark Andrews out(" ret = sbus_add_variant_to_dict(&iter_dict, \"%s\", %s, &%s_out_val);", prop.c_name(), prop.dbus_constant, prop.type)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" dbret = dbus_message_iter_close_container(&iter, &iter_dict);")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence out(" return sbus_request_finish(dbus_req, reply);")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if meth.use_raw_handler() or not meth.in_args:
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews forward_method_invoker(signature, meth.in_args)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson for (signature, meth) in invokers.items():
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson source_method_invoker(signature, meth.in_args)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff out("static int %s(struct sbus_request *dbus_req, void *function_ptr);",
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff out("int %s_finish(struct sbus_request *req%s)",
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson meth.fq_c_name(), method_arg_types(meth.out_args, with_names=True))
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence out(" %s cast_%s = arg_%s;", arg.dbus_type, arg.c_name(), arg.c_name())
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson out(" return sbus_request_return_and_finish(req,")
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews out("%s, &cast_%s,", arg.dbus_constant, arg.c_name())
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out("%s, &arg_%s,", arg.dbus_constant, arg.c_name())
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson out(" %s *%s", arg.dbus_type, arg.c_name())
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" %s %s;", arg.dbus_type, arg.c_name())
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence types = [arg.sssd_type for arg in meth.in_args]
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out("/* arguments for %s.%s */", parent.iface.name, parent.name)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out("const struct sbus_arg_meta %s%s[] = {", parent.fq_c_name(), suffix)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" { \"%s\", \"%s\" },", arg.name, arg.type)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out("const struct sbus_method_meta %s__methods[] = {", iface.c_name())
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" offsetof(struct %s, %s),", iface.c_name(), meth.c_name())
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if meth.use_raw_handler() or not meth.in_args:
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" invoke_%s_method,", meth.in_signature())
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out("const struct sbus_signal_meta %s__signals[] = {", iface.c_name())
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out("const struct sbus_property_meta %s__properties[] = {", iface.c_name())
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" SBUS_PROPERTY_READABLE | SBUS_PROPERTY_WRITABLE,")
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence out(" offsetof(struct %s, %s),", iface.c_name(), prop.getter_name())
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson out("/* interface info for %s */", iface.name)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out("extern const struct sbus_interface_meta %s_meta;", iface.c_name())
bfe313722d5b2eff6c80336ed1f19c2f99a53de6Andreas Gustafsson out("/* interface info for %s */", iface.name)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff out("const struct sbus_interface_meta %s_meta = {", iface.c_name())
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence out(" invoke_%s_get_all, /* GetAll invoker */", iface.c_name())
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssondef generate_source(ifaces, filename, include_header=None):
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out("/* The following definitions are auto-generated from %s */", basename)
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson out("#include \"sbus/sssd_dbus_meta.h\"")
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson out("#include \"%s\"", os.path.basename(include_header))
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson meth_invokers = forward_method_invokers(ifaces)
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson prop_invokers = forward_prop_invokers(ifaces)
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson source_properties(iface, iface.properties)
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson # always generate getall, for interfaces without properties
bed7a352934efc8055902d83c750dca2d0cd6aaaAndreas Gustafsson source_getall_invoker(iface, prop_invokers)
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson out("/* finish function for %s */", meth.name)
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson out("int %s_finish(struct sbus_request *req%s);",
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson meth.fq_c_name(), method_arg_types(meth.out_args, with_names=True))
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" struct sbus_vtable vtable; /* derive from sbus_vtable */")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" %s;", method_function_pointer(meth, meth.c_name(), with_names=True))
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence out("#define %s \"%s\"", iface.c_name().upper(), iface.name)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out("#define %s \"%s\"", meth.fq_c_name().upper(), meth.name)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out("#define %s \"%s\"", sig.fq_c_name().upper(), sig.name)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out("#define %s \"%s\"", prop.fq_c_name().upper(), prop.name)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson guard = "__%s__" % re.sub(r'([^_A-Z0-9])', "_", basename.upper())
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence out("/* The following declarations are auto-generated from %s */", basename)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out("/* ------------------------------------------------------------------------")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" * Various constants of interface and method names mostly for use by clients")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out("/* ------------------------------------------------------------------------")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" * These structures are filled in by implementors of the different")
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff out(" * dbus interfaces to handle method calls.")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" * Handler functions of type sbus_msg_handler_fn accept raw messages,")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" * other handlers are typed appropriately. If a handler that is")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" * set to NULL is invoked it will result in a")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" * org.freedesktop.DBus.Error.NotSupported error for the caller.")
4e57d3ff7d92abdef4b0b6aebc23a9dfae2ba040Andreas Gustafsson out(" * Handlers have a matching xxx_finish() function (unless the method has")
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence out(" * accepts raw messages). These finish functions the")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" * sbus_request_return_and_finish() with the appropriate arguments to")
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff out(" * construct a valid reply. Once a finish function has been called, the")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" * @dbus_req it was called with is freed and no longer valid.")
c0d0a59d1b665423b8a0d1829d0f0da121cb3473Andreas Gustafsson out("/* ------------------------------------------------------------------------")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" * These structure definitions are filled in with the information about")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence out(" * the interfaces, methods, properties and so on.")
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson out(" * The actual definitions are found in the accompanying C file next")
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson# -----------------------------------------------------------------------------
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson# XML Interface Parsing
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson raise DBusXmlException("Missing attribute '%s'" % name)
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson raise DBusXmlException("Empty attribute '%s'" % name)
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson parser = xml.parsers.expat.ParserCreate()
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson parser.CommentHandler = self.handle_comment
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson parser.CharacterDataHandler = self.handle_char_data
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson parser.StartElementHandler = self.handle_start_element
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson parser.EndElementHandler = self.handle_end_element
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson elif self.cur_object and name == STATE_ANNOTATION:
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson self.cur_object.annotations[expect_attr(attrs, 'name')] = val
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson method = Method(self.cur_object, expect_attr(attrs, 'name'))
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson signal = Signal(self.cur_object, expect_attr(attrs, 'name'))
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson raise DBusXmlException('Invalid direction "%s"' % direction)
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews assert False, 'Unhandled state "%s" while entering element with name "%s"' % (self.state, name)
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews parser = optparse.OptionParser("usage: %prog [options] introspect.xml ...")
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews parser.set_description("sbus_codegen generates sbus interface structures \
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews print >> sys.stderr, "sbus_codegen: no input file specified"
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews print >> sys.stderr, "sbus_codegen: specify --mode=header or --mode=source"
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews generate_header(parser.parsed_interfaces, filename)