Split dbus related code into separate file
Moved dbus related code into separate file. This can be used to disable
dbus interface if required.
Change-Id: I880639983edaf00c64acc4acb786203c51b414e7
Signed-off-by: Ninad Palsule <ninadpalsule@us.ibm.com>
diff --git a/console-dbus.c b/console-dbus.c
new file mode 100644
index 0000000..0672e5b
--- /dev/null
+++ b/console-dbus.c
@@ -0,0 +1,175 @@
+/**
+ * Copyright © 2023 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <errno.h>
+#include <err.h>
+
+#include "console-server.h"
+
+/* size of the dbus object path length */
+const size_t dbus_obj_path_len = 1024;
+
+#define DBUS_ERR "org.openbmc.error"
+#define INTF_NAME "xyz.openbmc_project.Console"
+#define DBUS_NAME "xyz.openbmc_project.Console.%s"
+#define OBJ_NAME "/xyz/openbmc_project/console/%s"
+
+static void tty_change_baudrate(struct console *console)
+{
+ struct handler *handler;
+ int i;
+ int rc;
+
+ tty_init_termios(console);
+
+ for (i = 0; i < console->n_handlers; i++) {
+ handler = console->handlers[i];
+ if (!handler->baudrate) {
+ continue;
+ }
+
+ rc = handler->baudrate(handler, console->tty_baud);
+ if (rc) {
+ warnx("Can't set terminal baudrate for handler %s",
+ handler->name);
+ }
+ }
+}
+
+static int method_set_baud_rate(sd_bus_message *msg, void *userdata,
+ sd_bus_error *err)
+{
+ struct console *console = userdata;
+ uint32_t baudrate;
+ speed_t speed;
+ int r;
+
+ if (!console) {
+ sd_bus_error_set_const(err, DBUS_ERR, "Internal error");
+ return sd_bus_reply_method_return(msg, "x", 0);
+ }
+
+ r = sd_bus_message_read(msg, "u", &baudrate);
+ if (r < 0) {
+ sd_bus_error_set_const(err, DBUS_ERR, "Bad message");
+ return sd_bus_reply_method_return(msg, "x", -EINVAL);
+ }
+
+ speed = parse_int_to_baud(baudrate);
+ if (!speed) {
+ warnx("Invalid baud rate: '%u'", baudrate);
+ return sd_bus_reply_method_return(msg, "x", -EINVAL);
+ }
+
+ console->tty_baud = speed;
+ tty_change_baudrate(console);
+
+ return sd_bus_reply_method_return(msg, "x", r);
+}
+
+static int get_handler(sd_bus *bus __attribute__((unused)),
+ const char *path __attribute__((unused)),
+ const char *interface __attribute__((unused)),
+ const char *property __attribute__((unused)),
+ sd_bus_message *reply, void *userdata,
+ sd_bus_error *error __attribute__((unused)))
+{
+ struct console *console = userdata;
+ uint32_t baudrate;
+ int r;
+
+ baudrate = parse_baud_to_int(console->tty_baud);
+ if (!baudrate) {
+ warnx("Invalid baud rate: '%d'", console->tty_baud);
+ }
+
+ r = sd_bus_message_append(reply, "u", baudrate);
+
+ return r;
+}
+
+static const sd_bus_vtable console_vtable[] = {
+ SD_BUS_VTABLE_START(0),
+ SD_BUS_METHOD("setBaudRate", "u", "x", method_set_baud_rate,
+ SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_PROPERTY("baudrate", "u", get_handler, 0, 0),
+ SD_BUS_VTABLE_END,
+};
+
+void dbus_init(struct console *console,
+ struct config *config __attribute__((unused)))
+{
+ char obj_name[dbus_obj_path_len];
+ char dbus_name[dbus_obj_path_len];
+ int dbus_poller = 0;
+ int fd;
+ int r;
+ size_t bytes;
+
+ if (!console) {
+ warnx("Couldn't get valid console");
+ return;
+ }
+
+ r = sd_bus_default_system(&console->bus);
+ if (r < 0) {
+ warnx("Failed to connect to system bus: %s", strerror(-r));
+ return;
+ }
+
+ /* Register support console interface */
+ bytes = snprintf(obj_name, dbus_obj_path_len, OBJ_NAME,
+ console->console_id);
+ if (bytes >= dbus_obj_path_len) {
+ warnx("Console id '%s' is too long. There is no enough space in the buffer.",
+ console->console_id);
+ return;
+ }
+
+ r = sd_bus_add_object_vtable(console->bus, NULL, obj_name, INTF_NAME,
+ console_vtable, console);
+ if (r < 0) {
+ warnx("Failed to issue method call: %s", strerror(-r));
+ return;
+ }
+
+ bytes = snprintf(dbus_name, dbus_obj_path_len, DBUS_NAME,
+ console->console_id);
+ if (bytes >= dbus_obj_path_len) {
+ warnx("Console id '%s' is too long. There is no enough space in the buffer.",
+ console->console_id);
+ return;
+ }
+
+ r = sd_bus_request_name(console->bus, dbus_name,
+ SD_BUS_NAME_ALLOW_REPLACEMENT |
+ SD_BUS_NAME_REPLACE_EXISTING);
+ if (r < 0) {
+ warnx("Failed to acquire service name: %s", strerror(-r));
+ return;
+ }
+
+ fd = sd_bus_get_fd(console->bus);
+ if (fd < 0) {
+ warnx("Couldn't get the bus file descriptor");
+ return;
+ }
+
+ dbus_poller = POLLFD_DBUS;
+
+ console->pollfds[dbus_poller].fd = fd;
+ console->pollfds[dbus_poller].events = POLLIN;
+}
diff --git a/console-server.c b/console-server.c
index 169d8f9..d06cc32 100644
--- a/console-server.c
+++ b/console-server.c
@@ -38,52 +38,6 @@
#include "console-server.h"
-/* size of the dbus object path length */
-const size_t dbus_obj_path_len = 1024;
-
-#define DBUS_ERR "org.openbmc.error"
-#define INTF_NAME "xyz.openbmc_project.console"
-#define DBUS_NAME "xyz.openbmc_project.Console.%s"
-#define OBJ_NAME "/xyz/openbmc_project/console/%s"
-
-struct console {
- const char *tty_kname;
- char *tty_sysfs_devnode;
- char *tty_dev;
- const char *console_id;
- int tty_sirq;
- uint16_t tty_lpc_addr;
- speed_t tty_baud;
- int tty_fd;
-
- struct ringbuffer *rb;
-
- struct handler **handlers;
- long n_handlers;
-
- struct poller **pollers;
- long n_pollers;
-
- struct pollfd *pollfds;
- struct sd_bus *bus;
-};
-
-struct poller {
- struct handler *handler;
- void *data;
- poller_event_fn_t event_fn;
- poller_timeout_fn_t timeout_fn;
- struct timeval timeout;
- bool remove;
-};
-
-/* we have two extra entry in the pollfds array for the VUART tty */
-enum internal_pollfds {
- POLLFD_HOSTTTY = 0,
- POLLFD_DBUS = 1,
- MAX_INTERNAL_POLLFD = 2,
-};
-
/* size of the shared backlog ringbuffer */
const size_t buffer_size = 128ul * 1024ul;
@@ -214,7 +168,7 @@
/**
* Set termios attributes on the console tty.
*/
-static void tty_init_termios(struct console *console)
+void tty_init_termios(struct console *console)
{
struct termios termios;
int rc;
@@ -242,28 +196,6 @@
}
}
-static void tty_change_baudrate(struct console *console)
-{
- struct handler *handler;
- int i;
- int rc;
-
- tty_init_termios(console);
-
- for (i = 0; i < console->n_handlers; i++) {
- handler = console->handlers[i];
- if (!handler->baudrate) {
- continue;
- }
-
- rc = handler->baudrate(handler, console->tty_baud);
- if (rc) {
- warnx("Can't set terminal baudrate for handler %s",
- handler->name);
- }
- }
-}
-
/**
* Open and initialise the serial device
*/
@@ -370,131 +302,6 @@
return write_buf_to_fd(console->tty_fd, data, len);
}
-static int method_set_baud_rate(sd_bus_message *msg, void *userdata,
- sd_bus_error *err)
-{
- struct console *console = userdata;
- uint32_t baudrate;
- speed_t speed;
- int r;
-
- if (!console) {
- sd_bus_error_set_const(err, DBUS_ERR, "Internal error");
- return sd_bus_reply_method_return(msg, "x", 0);
- }
-
- r = sd_bus_message_read(msg, "u", &baudrate);
- if (r < 0) {
- sd_bus_error_set_const(err, DBUS_ERR, "Bad message");
- return sd_bus_reply_method_return(msg, "x", -EINVAL);
- }
-
- speed = parse_int_to_baud(baudrate);
- if (!speed) {
- warnx("Invalid baud rate: '%u'", baudrate);
- return sd_bus_reply_method_return(msg, "x", -EINVAL);
- }
-
- console->tty_baud = speed;
- tty_change_baudrate(console);
-
- return sd_bus_reply_method_return(msg, "x", r);
-}
-
-static int get_handler(sd_bus *bus __attribute__((unused)),
- const char *path __attribute__((unused)),
- const char *interface __attribute__((unused)),
- const char *property __attribute__((unused)),
- sd_bus_message *reply, void *userdata,
- sd_bus_error *error __attribute__((unused)))
-{
- struct console *console = userdata;
- uint32_t baudrate;
- int r;
-
- baudrate = parse_baud_to_int(console->tty_baud);
- if (!baudrate) {
- warnx("Invalid baud rate: '%d'", console->tty_baud);
- }
-
- r = sd_bus_message_append(reply, "u", baudrate);
-
- return r;
-}
-
-static const sd_bus_vtable console_vtable[] = {
- SD_BUS_VTABLE_START(0),
- SD_BUS_METHOD("setBaudRate", "u", "x", method_set_baud_rate,
- SD_BUS_VTABLE_UNPRIVILEGED),
- SD_BUS_PROPERTY("baudrate", "u", get_handler, 0, 0),
- SD_BUS_VTABLE_END,
-};
-
-static void dbus_init(struct console *console,
- struct config *config __attribute__((unused)))
-{
- char obj_name[dbus_obj_path_len];
- char dbus_name[dbus_obj_path_len];
- int dbus_poller = 0;
- int fd;
- int r;
- size_t bytes;
-
- if (!console) {
- warnx("Couldn't get valid console");
- return;
- }
-
- r = sd_bus_default_system(&console->bus);
- if (r < 0) {
- warnx("Failed to connect to system bus: %s", strerror(-r));
- return;
- }
-
- /* Register support console interface */
- bytes = snprintf(obj_name, dbus_obj_path_len, OBJ_NAME,
- console->console_id);
- if (bytes >= dbus_obj_path_len) {
- warnx("Console id '%s' is too long. There is no enough space in the buffer.",
- console->console_id);
- return;
- }
-
- r = sd_bus_add_object_vtable(console->bus, NULL, obj_name, INTF_NAME,
- console_vtable, console);
- if (r < 0) {
- warnx("Failed to issue method call: %s", strerror(-r));
- return;
- }
-
- bytes = snprintf(dbus_name, dbus_obj_path_len, DBUS_NAME,
- console->console_id);
- if (bytes >= dbus_obj_path_len) {
- warnx("Console id '%s' is too long. There is no enough space in the buffer.",
- console->console_id);
- return;
- }
-
- r = sd_bus_request_name(console->bus, dbus_name,
- SD_BUS_NAME_ALLOW_REPLACEMENT |
- SD_BUS_NAME_REPLACE_EXISTING);
- if (r < 0) {
- warnx("Failed to acquire service name: %s", strerror(-r));
- return;
- }
-
- fd = sd_bus_get_fd(console->bus);
- if (fd < 0) {
- warnx("Couldn't get the bus file descriptor");
- return;
- }
-
- dbus_poller = POLLFD_DBUS;
-
- console->pollfds[dbus_poller].fd = fd;
- console->pollfds[dbus_poller].events = POLLIN;
-}
-
static void handlers_init(struct console *console, struct config *config)
{
/* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
diff --git a/console-server.h b/console-server.h
index f42b3ad..9418e23 100644
--- a/console-server.h
+++ b/console-server.h
@@ -79,6 +79,45 @@
typedef enum poller_ret (*poller_timeout_fn_t)(struct handler *handler,
void *data);
+/* Console server structure */
+struct console {
+ const char *tty_kname;
+ char *tty_sysfs_devnode;
+ char *tty_dev;
+ const char *console_id;
+ int tty_sirq;
+ uint16_t tty_lpc_addr;
+ speed_t tty_baud;
+ int tty_fd;
+
+ struct ringbuffer *rb;
+
+ struct handler **handlers;
+ long n_handlers;
+
+ struct poller **pollers;
+ long n_pollers;
+
+ struct pollfd *pollfds;
+ struct sd_bus *bus;
+};
+
+/* poller API */
+struct poller {
+ struct handler *handler;
+ void *data;
+ poller_event_fn_t event_fn;
+ poller_timeout_fn_t timeout_fn;
+ struct timeval timeout;
+ bool remove;
+};
+/* we have two extra entry in the pollfds array for the VUART tty */
+enum internal_pollfds {
+ POLLFD_HOSTTTY = 0,
+ POLLFD_DBUS = 1,
+ MAX_INTERNAL_POLLFD = 2,
+};
+
struct poller *console_poller_register(struct console *console,
struct handler *handler,
poller_event_fn_t poller_fn,
@@ -143,6 +182,9 @@
console_ringbuffer_consumer_register(struct console *console,
ringbuffer_poll_fn_t poll_fn, void *data);
+/* Console server API */
+void tty_init_termios(struct console *console);
+
/* config API */
struct config;
const char *config_get_value(struct config *config, const char *name);
@@ -164,6 +206,10 @@
/* utils */
int write_buf_to_fd(int fd, const uint8_t *buf, size_t len);
+/* console-dbus API */
+void dbus_init(struct console *console,
+ struct config *config __attribute__((unused)));
+
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#ifndef offsetof
diff --git a/meson.build b/meson.build
index de714c1..bf296cd 100644
--- a/meson.build
+++ b/meson.build
@@ -46,6 +46,7 @@
executable('obmc-console-server',
'config.c',
+ 'console-dbus.c',
'console-server.c',
'console-socket.c',
'log-handler.c',