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" |
Andrew Jeffery | 23a4821 | 2018-08-10 14:41:48 +0930 | [diff] [blame] | 12 | #include "transport.h" |
| 13 | |
Andrew Jeffery | 4414fb8 | 2018-08-20 12:13:09 +0930 | [diff] [blame^] | 14 | static int transport_dbus_set_events(struct mbox_context *context, |
| 15 | uint8_t events) |
| 16 | { |
| 17 | /* FIXME ! */ |
| 18 | MSG_ERR("%s is unimplemented!\n", __func__); |
| 19 | return 0; |
| 20 | } |
| 21 | |
| 22 | static int transport_dbus_clear_events(struct mbox_context *context, |
| 23 | uint8_t events) |
Andrew Jeffery | 23a4821 | 2018-08-10 14:41:48 +0930 | [diff] [blame] | 24 | { |
| 25 | /* FIXME ! */ |
| 26 | MSG_ERR("%s is unimplemented!\n", __func__); |
| 27 | return 0; |
| 28 | } |
| 29 | |
| 30 | static const struct transport_ops transport_dbus_ops = { |
Andrew Jeffery | 4414fb8 | 2018-08-20 12:13:09 +0930 | [diff] [blame^] | 31 | .set_events = transport_dbus_set_events, |
| 32 | .clear_events = transport_dbus_clear_events, |
Andrew Jeffery | 23a4821 | 2018-08-10 14:41:48 +0930 | [diff] [blame] | 33 | }; |
Andrew Jeffery | 23140be | 2018-09-05 14:15:07 +0930 | [diff] [blame] | 34 | |
| 35 | static int transport_dbus_get_info(sd_bus_message *m, void *userdata, |
| 36 | sd_bus_error *ret_error) |
| 37 | { |
| 38 | struct mbox_context *context = userdata; |
| 39 | struct protocol_get_info io; |
| 40 | sd_bus_message *n; |
| 41 | int rc; |
| 42 | |
| 43 | if (!context) { |
| 44 | MSG_ERR("DBUS Internal Error\n"); |
| 45 | return -EINVAL; |
| 46 | } |
| 47 | |
| 48 | rc = sd_bus_message_read_basic(m, 'y', &io.req.api_version); |
| 49 | if (rc < 0) { |
| 50 | MSG_ERR("DBUS error reading message: %s\n", strerror(-rc)); |
| 51 | return rc; |
| 52 | } |
| 53 | |
| 54 | rc = context->protocol->get_info(context, &io); |
| 55 | if (rc < 0) { |
| 56 | return rc; |
| 57 | } |
| 58 | |
Andrew Jeffery | 23a4821 | 2018-08-10 14:41:48 +0930 | [diff] [blame] | 59 | /* Switch transport to DBus. This is fine as DBus signals are async */ |
| 60 | context->transport = &transport_dbus_ops; |
Andrew Jeffery | 4414fb8 | 2018-08-20 12:13:09 +0930 | [diff] [blame^] | 61 | context->transport->set_events(context, context->bmc_events); |
Andrew Jeffery | 23a4821 | 2018-08-10 14:41:48 +0930 | [diff] [blame] | 62 | |
Andrew Jeffery | 23140be | 2018-09-05 14:15:07 +0930 | [diff] [blame] | 63 | rc = sd_bus_message_new_method_return(m, &n); |
| 64 | if (rc < 0) { |
| 65 | MSG_ERR("sd_bus_message_new_method_return failed: %d\n", rc); |
| 66 | return rc; |
| 67 | } |
| 68 | |
| 69 | if (API_VERSION_2 != io.resp.api_version) { |
| 70 | MSG_ERR("Unsupported protocol version for DBus transport: %d\n", |
| 71 | io.resp.api_version); |
| 72 | return rc; |
| 73 | } |
| 74 | |
| 75 | rc = sd_bus_message_append(n, "yyq", |
| 76 | io.resp.api_version, |
| 77 | io.resp.v2.block_size_shift, |
| 78 | io.resp.v2.timeout); |
| 79 | if (rc < 0) { |
| 80 | MSG_ERR("sd_bus_message_append failed!\n"); |
| 81 | return rc; |
| 82 | } |
| 83 | |
| 84 | return sd_bus_send(NULL, n, NULL); |
| 85 | } |
| 86 | |
| 87 | static const sd_bus_vtable protocol_unversioned_vtable[] = { |
| 88 | SD_BUS_VTABLE_START(0), |
| 89 | SD_BUS_METHOD("GetInfo", "y", "yyq", &transport_dbus_get_info, |
| 90 | SD_BUS_VTABLE_UNPRIVILEGED), |
| 91 | SD_BUS_VTABLE_END |
| 92 | }; |
| 93 | |
| 94 | static const sd_bus_vtable protocol_v2_vtable[] = { |
| 95 | SD_BUS_VTABLE_START(0), |
| 96 | SD_BUS_METHOD("GetInfo", "y", "yyq", &transport_dbus_get_info, |
| 97 | SD_BUS_VTABLE_UNPRIVILEGED), |
| 98 | SD_BUS_VTABLE_END |
| 99 | }; |
| 100 | |
| 101 | int transport_dbus_init(struct mbox_context *context) |
| 102 | { |
| 103 | int rc; |
| 104 | |
| 105 | rc = sd_bus_add_object_vtable(context->bus, NULL, |
| 106 | MBOX_DBUS_OBJECT, |
| 107 | MBOX_DBUS_PROTOCOL_IFACE, |
| 108 | protocol_unversioned_vtable, |
| 109 | context); |
| 110 | if (rc < 0) { |
| 111 | return rc; |
| 112 | } |
| 113 | |
| 114 | rc = sd_bus_add_object_vtable(context->bus, NULL, |
| 115 | MBOX_DBUS_OBJECT, |
| 116 | /* TODO: Make this clearer? */ MBOX_DBUS_PROTOCOL_IFACE ".v2", |
| 117 | protocol_v2_vtable, context); |
| 118 | |
| 119 | return rc; |
| 120 | } |
| 121 | |
| 122 | #define __unused __attribute__((unused)) |
| 123 | void transport_dbus_free(struct mbox_context *context __unused) |
| 124 | { |
| 125 | return; |
| 126 | } |