blob: 152dc9510bec7a8a774b4df5dde4453d2d3509db [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
Andrew Jeffery4414fb82018-08-20 12:13:09 +093014static 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
22static int transport_dbus_clear_events(struct mbox_context *context,
23 uint8_t events)
Andrew Jeffery23a48212018-08-10 14:41:48 +093024{
25 /* FIXME ! */
26 MSG_ERR("%s is unimplemented!\n", __func__);
27 return 0;
28}
29
30static const struct transport_ops transport_dbus_ops = {
Andrew Jeffery4414fb82018-08-20 12:13:09 +093031 .set_events = transport_dbus_set_events,
32 .clear_events = transport_dbus_clear_events,
Andrew Jeffery23a48212018-08-10 14:41:48 +093033};
Andrew Jeffery23140be2018-09-05 14:15:07 +093034
35static 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 Jeffery23a48212018-08-10 14:41:48 +093059 /* Switch transport to DBus. This is fine as DBus signals are async */
60 context->transport = &transport_dbus_ops;
Andrew Jeffery4414fb82018-08-20 12:13:09 +093061 context->transport->set_events(context, context->bmc_events);
Andrew Jeffery23a48212018-08-10 14:41:48 +093062
Andrew Jeffery23140be2018-09-05 14:15:07 +093063 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
87static 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
94static 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
101int 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))
123void transport_dbus_free(struct mbox_context *context __unused)
124{
125 return;
126}