diff options
author | Stef Walter <stefw@redhat.com> | 2014-02-25 10:29:23 +0100 |
---|---|---|
committer | Jakub Hrozek <jhrozek@redhat.com> | 2014-04-19 10:32:50 +0200 |
commit | dff909d473f43a6bd0f0286fa2d279c0ebe945c6 (patch) | |
tree | 61b2f3e2c33e28b51a8ffed64a73de364093557a /src/tests | |
parent | 7a9a6ee1b5f5479c3a6958401f9b34c571c3b6bf (diff) | |
download | sssd-dff909d473f43a6bd0f0286fa2d279c0ebe945c6.tar.gz sssd-dff909d473f43a6bd0f0286fa2d279c0ebe945c6.tar.xz sssd-dff909d473f43a6bd0f0286fa2d279c0ebe945c6.zip |
sbus: Add type-safe DBus method handlers and finish functions
Type safe method handlers allow methods not to have to do tedious
unwrapping and wrapping of DBus method call messages or replies.
Arguments of the following DBus types are supported in type-safe
method handlers. In addition arrays of these are supported.
y: uint8_t
b: bool (but no arrays, yet)
n: int16_t
q: uint16_t
i: int32_t
u: uint32_t
x: int64_t
t: uint64_t
d: double
s: char * (utf8 string)
o: char * (object path)
As an exception, arrays of booleans are not supported, but could be
added later. Other more complex types could be added later if desired.
If a method has other argument types, then it must be marked as having
a raw handler (see below).
Internally each method can have a type specific invoker function which
unpacks the incoming arguments and invokes the method handler with the
correct arguments.
Each method also has a finish which accepts the type-safe out arguments
(ie: return values) and builds the reply message. Like other request
'finish' functions, these free the request talloc context, and are to
be used in place of sbus_request_finish() or friends.
Raw method handlers parse their own method arguments, and prepare their
own reply (ideally using sbus_request_finish() helpers). They can also
do strange things like have variable arguments. To mark a DBus method
as having a raw method handler use the following annotation:
<annotation name="org.freedesktop.sssd.RawHandler" value="true"/>
Raw methods do not have invokers or finish functions.
I've left all of the internal peer to peer communication using raw
method handlers. No code changes here.
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/sbus_codegen_tests.c | 40 | ||||
-rwxr-xr-x | src/tests/sbus_codegen_tests.xml | 15 | ||||
-rw-r--r-- | src/tests/sbus_codegen_tests_generated.c | 124 | ||||
-rw-r--r-- | src/tests/sbus_codegen_tests_generated.h | 32 |
4 files changed, 199 insertions, 12 deletions
diff --git a/src/tests/sbus_codegen_tests.c b/src/tests/sbus_codegen_tests.c index 8055cf206..798d394df 100644 --- a/src/tests/sbus_codegen_tests.c +++ b/src/tests/sbus_codegen_tests.c @@ -53,7 +53,7 @@ START_TEST(test_interfaces) /* Explicit C Symbol */ ck_assert_str_eq(test_pilot_meta.name, "com.planetexpress.Pilot"); - ck_assert(test_pilot_meta.methods == NULL); /* no methods */ + ck_assert(test_pilot_meta.methods != NULL); ck_assert(test_pilot_meta.signals == NULL); /* no signals */ ck_assert(test_pilot_meta.properties != NULL); @@ -113,16 +113,42 @@ START_TEST(test_signals) END_TEST static int -mock_move_universe(struct sbus_request *dbus_req, void *data) +mock_move_universe(struct sbus_request *dbus_req, void *data, + bool arg_smoothly, uint32_t arg_speed_factor) { - /* not called */ - return 0; + /* + * The above arguments should match the handler signature, + * and the below finish function should have the right signature. + * + * Not called, just testing compilation + */ + ck_assert(FALSE); + return com_planetexpress_Ship_MoveUniverse_finish(dbus_req, "here"); } static int -mock_crash_now(struct sbus_request *dbus_req, void *data) +mock_crash_now(struct sbus_request *dbus_req, void *data, + const char *where) { - /* not called */ + /* + * One argument, no return value, yet a finish function should + * have been generated. + * + * Not called, just testing compilation + */ + ck_assert(FALSE); + return com_planetexpress_Ship_crash_now_finish(dbus_req); +} + +static int +mock_land(struct sbus_request *req, void *data) +{ + /* + * Raw handler, no finish function, no special arguments. + * + * Not called, just testing compilation + */ + ck_assert(FALSE); return 0; } @@ -132,6 +158,7 @@ START_TEST(test_vtable) { &com_planetexpress_Ship_meta, 0 }, mock_move_universe, mock_crash_now, + mock_land, }; /* @@ -141,6 +168,7 @@ START_TEST(test_vtable) */ ck_assert(vtable.crash_now == mock_crash_now); ck_assert(vtable.MoveUniverse == mock_move_universe); + ck_assert(vtable.Land == mock_land); } END_TEST diff --git a/src/tests/sbus_codegen_tests.xml b/src/tests/sbus_codegen_tests.xml index e571dbc17..331538ef9 100755 --- a/src/tests/sbus_codegen_tests.xml +++ b/src/tests/sbus_codegen_tests.xml @@ -33,6 +33,12 @@ <!-- A method with a specific c name --> <method name="Crash"> <annotation value="crash_now" name="org.freedesktop.DBus.GLib.CSymbol"/> + <arg name="where" type="s" direction="in"/> + </method> + + <!-- A method without a type-safe handler --> + <method name="Land"> + <annotation name="org.freedesktop.sssd.RawHandler" value="true"/> </method> </interface> @@ -45,6 +51,15 @@ <!-- A property --> <property name="FullName" type="s" access="readwrite"/> + + <!-- A simple method --> + <method name="Blink"> + <!-- This is an uint32 arg --> + <arg name="duration" type="u" direction="in"/> + <!-- This is a boolean return value --> + <arg name="crashed" type="b" direction="out"/> + </method> + </interface> </node> diff --git a/src/tests/sbus_codegen_tests_generated.c b/src/tests/sbus_codegen_tests_generated.c index ccf612996..b5ff08d9d 100644 --- a/src/tests/sbus_codegen_tests_generated.c +++ b/src/tests/sbus_codegen_tests_generated.c @@ -5,6 +5,15 @@ #include "sbus/sssd_dbus_meta.h" #include "sbus_codegen_tests_generated.h" +/* invokes a handler with a 'bu' DBus signature */ +static int invoke_bu_method(struct sbus_request *dbus_req, void *function_ptr); + +/* invokes a handler with a 's' DBus signature */ +static int invoke_s_method(struct sbus_request *dbus_req, void *function_ptr); + +/* invokes a handler with a 'u' DBus signature */ +static int invoke_u_method(struct sbus_request *dbus_req, void *function_ptr); + /* arguments for com.planetexpress.Ship.MoveUniverse */ const struct sbus_arg_meta com_planetexpress_Ship_MoveUniverse__in[] = { { "smoothly", "b" }, @@ -18,6 +27,25 @@ const struct sbus_arg_meta com_planetexpress_Ship_MoveUniverse__out[] = { { NULL, } }; +int com_planetexpress_Ship_MoveUniverse_finish(struct sbus_request *req, const char *arg_where_we_crashed) +{ + return sbus_request_return_and_finish(req, + DBUS_TYPE_STRING, &arg_where_we_crashed, + DBUS_TYPE_INVALID); +} + +/* arguments for com.planetexpress.Ship.Crash */ +const struct sbus_arg_meta com_planetexpress_Ship_crash_now__in[] = { + { "where", "s" }, + { NULL, } +}; + +int com_planetexpress_Ship_crash_now_finish(struct sbus_request *req) +{ + return sbus_request_return_and_finish(req, + DBUS_TYPE_INVALID); +} + /* methods for com.planetexpress.Ship */ const struct sbus_method_meta com_planetexpress_Ship__methods[] = { { @@ -25,12 +53,21 @@ const struct sbus_method_meta com_planetexpress_Ship__methods[] = { com_planetexpress_Ship_MoveUniverse__in, com_planetexpress_Ship_MoveUniverse__out, offsetof(struct com_planetexpress_Ship, MoveUniverse), + invoke_bu_method, }, { "Crash", /* name */ - NULL, /* no in_args */ + com_planetexpress_Ship_crash_now__in, NULL, /* no out_args */ offsetof(struct com_planetexpress_Ship, crash_now), + invoke_s_method, + }, + { + "Land", /* name */ + NULL, /* no in_args */ + NULL, /* no out_args */ + offsetof(struct com_planetexpress_Ship, Land), + NULL, /* no invoker */ }, { NULL, } }; @@ -68,6 +105,38 @@ const struct sbus_interface_meta com_planetexpress_Ship_meta = { com_planetexpress_Ship__properties }; +/* arguments for com.planetexpress.Pilot.Blink */ +const struct sbus_arg_meta test_pilot_Blink__in[] = { + { "duration", "u" }, + { NULL, } +}; + +/* arguments for com.planetexpress.Pilot.Blink */ +const struct sbus_arg_meta test_pilot_Blink__out[] = { + { "crashed", "b" }, + { NULL, } +}; + +int test_pilot_Blink_finish(struct sbus_request *req, bool arg_crashed) +{ + dbus_bool_t cast_crashed = arg_crashed; + return sbus_request_return_and_finish(req, + DBUS_TYPE_BOOLEAN, &cast_crashed, + DBUS_TYPE_INVALID); +} + +/* methods for com.planetexpress.Pilot */ +const struct sbus_method_meta test_pilot__methods[] = { + { + "Blink", /* name */ + test_pilot_Blink__in, + test_pilot_Blink__out, + offsetof(struct test_pilot, Blink), + invoke_u_method, + }, + { NULL, } +}; + /* property info for com.planetexpress.Pilot */ const struct sbus_property_meta test_pilot__properties[] = { { @@ -81,7 +150,58 @@ const struct sbus_property_meta test_pilot__properties[] = { /* interface info for com.planetexpress.Pilot */ const struct sbus_interface_meta test_pilot_meta = { "com.planetexpress.Pilot", /* name */ - NULL, /* no methods */ + test_pilot__methods, NULL, /* no signals */ test_pilot__properties }; + +/* invokes a handler with a 'bu' DBus signature */ +static int invoke_bu_method(struct sbus_request *dbus_req, void *function_ptr) +{ + dbus_bool_t arg_0; + uint32_t arg_1; + int (*handler)(struct sbus_request *, void *, bool, uint32_t) = function_ptr; + + if (!sbus_request_parse_or_finish(dbus_req, + DBUS_TYPE_BOOLEAN, &arg_0, + DBUS_TYPE_UINT32, &arg_1, + DBUS_TYPE_INVALID)) { + return EOK; /* request handled */ + } + + return (handler)(dbus_req, dbus_req->intf->instance_data, + arg_0, + arg_1); +} + +/* invokes a handler with a 's' DBus signature */ +static int invoke_s_method(struct sbus_request *dbus_req, void *function_ptr) +{ + const char * arg_0; + int (*handler)(struct sbus_request *, void *, const char *) = function_ptr; + + if (!sbus_request_parse_or_finish(dbus_req, + DBUS_TYPE_STRING, &arg_0, + DBUS_TYPE_INVALID)) { + return EOK; /* request handled */ + } + + return (handler)(dbus_req, dbus_req->intf->instance_data, + arg_0); +} + +/* invokes a handler with a 'u' DBus signature */ +static int invoke_u_method(struct sbus_request *dbus_req, void *function_ptr) +{ + uint32_t arg_0; + int (*handler)(struct sbus_request *, void *, uint32_t) = function_ptr; + + if (!sbus_request_parse_or_finish(dbus_req, + DBUS_TYPE_UINT32, &arg_0, + DBUS_TYPE_INVALID)) { + return EOK; /* request handled */ + } + + return (handler)(dbus_req, dbus_req->intf->instance_data, + arg_0); +} diff --git a/src/tests/sbus_codegen_tests_generated.h b/src/tests/sbus_codegen_tests_generated.h index f41eca74b..23091a7bf 100644 --- a/src/tests/sbus_codegen_tests_generated.h +++ b/src/tests/sbus_codegen_tests_generated.h @@ -15,32 +15,56 @@ #define COM_PLANETEXPRESS_SHIP "com.planetexpress.Ship" #define COM_PLANETEXPRESS_SHIP_MOVEUNIVERSE "MoveUniverse" #define COM_PLANETEXPRESS_SHIP_CRASH_NOW "Crash" +#define COM_PLANETEXPRESS_SHIP_LAND "Land" #define COM_PLANETEXPRESS_SHIP_BECAMESENTIENT "BecameSentient" #define COM_PLANETEXPRESS_SHIP_COLOR "Color" /* constants for com.planetexpress.Pilot */ #define TEST_PILOT "com.planetexpress.Pilot" +#define TEST_PILOT_BLINK "Blink" #define TEST_PILOT_FULLNAME "FullName" /* ------------------------------------------------------------------------ - * DBus Vtable handler structures + * DBus handlers * * These structures are filled in by implementors of the different * dbus interfaces to handle method calls. * * Handler functions of type sbus_msg_handler_fn accept raw messages, - * other handlers will be typed appropriately. If a handler that is + * other handlers are typed appropriately. If a handler that is * set to NULL is invoked it will result in a * org.freedesktop.DBus.Error.NotSupported error for the caller. + * + * Handlers have a matching xxx_finish() function (unless the method has + * accepts raw messages). These finish functions the + * sbus_request_return_and_finish() with the appropriate arguments to + * construct a valid reply. Once a finish function has been called, the + * @dbus_req it was called with is freed and no longer valid. */ /* vtable for com.planetexpress.Ship */ struct com_planetexpress_Ship { struct sbus_vtable vtable; /* derive from sbus_vtable */ - sbus_msg_handler_fn MoveUniverse; - sbus_msg_handler_fn crash_now; + int (*MoveUniverse)(struct sbus_request *req, void *data, bool arg_smoothly, uint32_t arg_speed_factor); + int (*crash_now)(struct sbus_request *req, void *data, const char *arg_where); + sbus_msg_handler_fn Land; }; +/* finish function for MoveUniverse */ +int com_planetexpress_Ship_MoveUniverse_finish(struct sbus_request *req, const char *arg_where_we_crashed); + +/* finish function for Crash */ +int com_planetexpress_Ship_crash_now_finish(struct sbus_request *req); + +/* vtable for com.planetexpress.Pilot */ +struct test_pilot { + struct sbus_vtable vtable; /* derive from sbus_vtable */ + int (*Blink)(struct sbus_request *req, void *data, uint32_t arg_duration); +}; + +/* finish function for Blink */ +int test_pilot_Blink_finish(struct sbus_request *req, bool arg_crashed); + /* ------------------------------------------------------------------------ * DBus Interface Metadata * |