Andrew Jeffery | 23140be | 2018-09-05 14:15:07 +0930 | [diff] [blame^] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2018 IBM Corp. |
| 3 | #include "config.h" |
| 4 | |
| 5 | #include <errno.h> |
| 6 | #include <systemd/sd-bus.h> |
| 7 | |
| 8 | #include "common.h" |
| 9 | #include "dbus.h" |
| 10 | #include "mboxd.h" |
| 11 | #include "protocol.h" |
| 12 | |
| 13 | static int transport_dbus_get_info(sd_bus_message *m, void *userdata, |
| 14 | sd_bus_error *ret_error) |
| 15 | { |
| 16 | struct mbox_context *context = userdata; |
| 17 | struct protocol_get_info io; |
| 18 | sd_bus_message *n; |
| 19 | int rc; |
| 20 | |
| 21 | if (!context) { |
| 22 | MSG_ERR("DBUS Internal Error\n"); |
| 23 | return -EINVAL; |
| 24 | } |
| 25 | |
| 26 | rc = sd_bus_message_read_basic(m, 'y', &io.req.api_version); |
| 27 | if (rc < 0) { |
| 28 | MSG_ERR("DBUS error reading message: %s\n", strerror(-rc)); |
| 29 | return rc; |
| 30 | } |
| 31 | |
| 32 | rc = context->protocol->get_info(context, &io); |
| 33 | if (rc < 0) { |
| 34 | return rc; |
| 35 | } |
| 36 | |
| 37 | rc = sd_bus_message_new_method_return(m, &n); |
| 38 | if (rc < 0) { |
| 39 | MSG_ERR("sd_bus_message_new_method_return failed: %d\n", rc); |
| 40 | return rc; |
| 41 | } |
| 42 | |
| 43 | if (API_VERSION_2 != io.resp.api_version) { |
| 44 | MSG_ERR("Unsupported protocol version for DBus transport: %d\n", |
| 45 | io.resp.api_version); |
| 46 | return rc; |
| 47 | } |
| 48 | |
| 49 | rc = sd_bus_message_append(n, "yyq", |
| 50 | io.resp.api_version, |
| 51 | io.resp.v2.block_size_shift, |
| 52 | io.resp.v2.timeout); |
| 53 | if (rc < 0) { |
| 54 | MSG_ERR("sd_bus_message_append failed!\n"); |
| 55 | return rc; |
| 56 | } |
| 57 | |
| 58 | return sd_bus_send(NULL, n, NULL); |
| 59 | } |
| 60 | |
| 61 | static const sd_bus_vtable protocol_unversioned_vtable[] = { |
| 62 | SD_BUS_VTABLE_START(0), |
| 63 | SD_BUS_METHOD("GetInfo", "y", "yyq", &transport_dbus_get_info, |
| 64 | SD_BUS_VTABLE_UNPRIVILEGED), |
| 65 | SD_BUS_VTABLE_END |
| 66 | }; |
| 67 | |
| 68 | static const sd_bus_vtable protocol_v2_vtable[] = { |
| 69 | SD_BUS_VTABLE_START(0), |
| 70 | SD_BUS_METHOD("GetInfo", "y", "yyq", &transport_dbus_get_info, |
| 71 | SD_BUS_VTABLE_UNPRIVILEGED), |
| 72 | SD_BUS_VTABLE_END |
| 73 | }; |
| 74 | |
| 75 | int transport_dbus_init(struct mbox_context *context) |
| 76 | { |
| 77 | int rc; |
| 78 | |
| 79 | rc = sd_bus_add_object_vtable(context->bus, NULL, |
| 80 | MBOX_DBUS_OBJECT, |
| 81 | MBOX_DBUS_PROTOCOL_IFACE, |
| 82 | protocol_unversioned_vtable, |
| 83 | context); |
| 84 | if (rc < 0) { |
| 85 | return rc; |
| 86 | } |
| 87 | |
| 88 | rc = sd_bus_add_object_vtable(context->bus, NULL, |
| 89 | MBOX_DBUS_OBJECT, |
| 90 | /* TODO: Make this clearer? */ MBOX_DBUS_PROTOCOL_IFACE ".v2", |
| 91 | protocol_v2_vtable, context); |
| 92 | |
| 93 | return rc; |
| 94 | } |
| 95 | |
| 96 | #define __unused __attribute__((unused)) |
| 97 | void transport_dbus_free(struct mbox_context *context __unused) |
| 98 | { |
| 99 | return; |
| 100 | } |