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