transport: dbus: Add get_info

Change-Id: I59cf0155b3f8600527cca23a14f41ef7d916a831
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/Makefile.am b/Makefile.am
index 3a7c3d4..abfdc6e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -9,6 +9,7 @@
 	control_dbus.c \
 	lpc.c \
 	protocol.c \
+	transport_dbus.c \
 	transport_mbox.c \
 	windows.c \
 	mtd.c
diff --git a/mboxd.c b/mboxd.c
index 3c10998..dece231 100644
--- a/mboxd.c
+++ b/mboxd.c
@@ -34,6 +34,7 @@
 #include "flash.h"
 #include "lpc.h"
 #include "transport_mbox.h"
+#include "transport_dbus.h"
 #include "windows.h"
 #include "vpnor/mboxd_pnor_partition_table.h"
 
@@ -75,12 +76,18 @@
 		return rc;
 	}
 
+	rc = transport_dbus_init(context);
+	if (rc < 0) {
+		MSG_ERR("Failed to initialise DBus protocol interface: %s\n",
+			strerror(-rc));
+		return rc;
+	}
+
 	rc = sd_bus_request_name(context->bus, MBOX_DBUS_NAME,
 				 SD_BUS_NAME_ALLOW_REPLACEMENT |
 				 SD_BUS_NAME_REPLACE_EXISTING);
 	if (rc < 0) {
-		MSG_ERR("Failed to request name on the bus: %s\n",
-			strerror(-rc));
+		MSG_ERR("Failed to request DBus name: %s\n", strerror(-rc));
 		return rc;
 	}
 
@@ -97,6 +104,7 @@
 
 static void dbus_free(struct mbox_context *context)
 {
+	transport_dbus_free(context);
 	control_dbus_free(context);
 	control_legacy_free(context);
 	sd_bus_unref(context->bus);
diff --git a/transport_dbus.c b/transport_dbus.c
new file mode 100644
index 0000000..0444284
--- /dev/null
+++ b/transport_dbus.c
@@ -0,0 +1,100 @@
+// SPDX-License-Identifier: Apache-2.0
+// Copyright (C) 2018 IBM Corp.
+#include "config.h"
+
+#include <errno.h>
+#include <systemd/sd-bus.h>
+
+#include "common.h"
+#include "dbus.h"
+#include "mboxd.h"
+#include "protocol.h"
+
+static int transport_dbus_get_info(sd_bus_message *m, void *userdata,
+					sd_bus_error *ret_error)
+{
+	struct mbox_context *context = userdata;
+	struct protocol_get_info io;
+	sd_bus_message *n;
+	int rc;
+
+	if (!context) {
+		MSG_ERR("DBUS Internal Error\n");
+		return -EINVAL;
+	}
+
+	rc = sd_bus_message_read_basic(m, 'y', &io.req.api_version);
+	if (rc < 0) {
+		MSG_ERR("DBUS error reading message: %s\n", strerror(-rc));
+		return rc;
+	}
+
+	rc = context->protocol->get_info(context, &io);
+	if (rc < 0) {
+		return rc;
+	}
+
+	rc = sd_bus_message_new_method_return(m, &n);
+	if (rc < 0) {
+		MSG_ERR("sd_bus_message_new_method_return failed: %d\n", rc);
+		return rc;
+	}
+
+	if (API_VERSION_2 != io.resp.api_version) {
+		MSG_ERR("Unsupported protocol version for DBus transport: %d\n",
+			io.resp.api_version);
+		return rc;
+	}
+
+	rc = sd_bus_message_append(n, "yyq",
+				   io.resp.api_version,
+				   io.resp.v2.block_size_shift,
+				   io.resp.v2.timeout);
+	if (rc < 0) {
+		MSG_ERR("sd_bus_message_append failed!\n");
+		return rc;
+	}
+
+	return sd_bus_send(NULL, n, NULL);
+}
+
+static const sd_bus_vtable protocol_unversioned_vtable[] = {
+	SD_BUS_VTABLE_START(0),
+	SD_BUS_METHOD("GetInfo", "y", "yyq", &transport_dbus_get_info,
+		      SD_BUS_VTABLE_UNPRIVILEGED),
+	SD_BUS_VTABLE_END
+};
+
+static const sd_bus_vtable protocol_v2_vtable[] = {
+	SD_BUS_VTABLE_START(0),
+	SD_BUS_METHOD("GetInfo", "y", "yyq", &transport_dbus_get_info,
+		      SD_BUS_VTABLE_UNPRIVILEGED),
+	SD_BUS_VTABLE_END
+};
+
+int transport_dbus_init(struct mbox_context *context)
+{
+	int rc;
+
+	rc = sd_bus_add_object_vtable(context->bus, NULL,
+					MBOX_DBUS_OBJECT,
+					MBOX_DBUS_PROTOCOL_IFACE,
+					protocol_unversioned_vtable,
+					context);
+	if (rc < 0) {
+		return rc;
+	}
+
+	rc = sd_bus_add_object_vtable(context->bus, NULL,
+					MBOX_DBUS_OBJECT,
+	/* TODO: Make this clearer? */	MBOX_DBUS_PROTOCOL_IFACE ".v2",
+					protocol_v2_vtable, context);
+
+	return rc;
+}
+
+#define __unused __attribute__((unused))
+void transport_dbus_free(struct mbox_context *context __unused)
+{
+	return;
+}
diff --git a/transport_dbus.h b/transport_dbus.h
new file mode 100644
index 0000000..6f93f35
--- /dev/null
+++ b/transport_dbus.h
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+/* Copyright (C) 2018 IBM Corp. */
+
+#ifndef TRANSPORT_DBUS_H
+#define TRANSPORT_DBUS_H
+
+#include "dbus.h"
+
+int transport_dbus_init(struct mbox_context *context);
+void transport_dbus_free(struct mbox_context *context);
+
+#endif /* TRANSPORT_DBUS_H */