blob: 0672e5bf27fa77b93d51e21050185bec68438e0e [file] [log] [blame]
Ninad Palsulee258e512023-04-26 22:17:57 -05001/**
2 * Copyright © 2023 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <err.h>
19
20#include "console-server.h"
21
22/* size of the dbus object path length */
23const size_t dbus_obj_path_len = 1024;
24
25#define DBUS_ERR "org.openbmc.error"
26#define INTF_NAME "xyz.openbmc_project.Console"
27#define DBUS_NAME "xyz.openbmc_project.Console.%s"
28#define OBJ_NAME "/xyz/openbmc_project/console/%s"
29
30static void tty_change_baudrate(struct console *console)
31{
32 struct handler *handler;
33 int i;
34 int rc;
35
36 tty_init_termios(console);
37
38 for (i = 0; i < console->n_handlers; i++) {
39 handler = console->handlers[i];
40 if (!handler->baudrate) {
41 continue;
42 }
43
44 rc = handler->baudrate(handler, console->tty_baud);
45 if (rc) {
46 warnx("Can't set terminal baudrate for handler %s",
47 handler->name);
48 }
49 }
50}
51
52static int method_set_baud_rate(sd_bus_message *msg, void *userdata,
53 sd_bus_error *err)
54{
55 struct console *console = userdata;
56 uint32_t baudrate;
57 speed_t speed;
58 int r;
59
60 if (!console) {
61 sd_bus_error_set_const(err, DBUS_ERR, "Internal error");
62 return sd_bus_reply_method_return(msg, "x", 0);
63 }
64
65 r = sd_bus_message_read(msg, "u", &baudrate);
66 if (r < 0) {
67 sd_bus_error_set_const(err, DBUS_ERR, "Bad message");
68 return sd_bus_reply_method_return(msg, "x", -EINVAL);
69 }
70
71 speed = parse_int_to_baud(baudrate);
72 if (!speed) {
73 warnx("Invalid baud rate: '%u'", baudrate);
74 return sd_bus_reply_method_return(msg, "x", -EINVAL);
75 }
76
77 console->tty_baud = speed;
78 tty_change_baudrate(console);
79
80 return sd_bus_reply_method_return(msg, "x", r);
81}
82
83static int get_handler(sd_bus *bus __attribute__((unused)),
84 const char *path __attribute__((unused)),
85 const char *interface __attribute__((unused)),
86 const char *property __attribute__((unused)),
87 sd_bus_message *reply, void *userdata,
88 sd_bus_error *error __attribute__((unused)))
89{
90 struct console *console = userdata;
91 uint32_t baudrate;
92 int r;
93
94 baudrate = parse_baud_to_int(console->tty_baud);
95 if (!baudrate) {
96 warnx("Invalid baud rate: '%d'", console->tty_baud);
97 }
98
99 r = sd_bus_message_append(reply, "u", baudrate);
100
101 return r;
102}
103
104static const sd_bus_vtable console_vtable[] = {
105 SD_BUS_VTABLE_START(0),
106 SD_BUS_METHOD("setBaudRate", "u", "x", method_set_baud_rate,
107 SD_BUS_VTABLE_UNPRIVILEGED),
108 SD_BUS_PROPERTY("baudrate", "u", get_handler, 0, 0),
109 SD_BUS_VTABLE_END,
110};
111
112void dbus_init(struct console *console,
113 struct config *config __attribute__((unused)))
114{
115 char obj_name[dbus_obj_path_len];
116 char dbus_name[dbus_obj_path_len];
117 int dbus_poller = 0;
118 int fd;
119 int r;
120 size_t bytes;
121
122 if (!console) {
123 warnx("Couldn't get valid console");
124 return;
125 }
126
127 r = sd_bus_default_system(&console->bus);
128 if (r < 0) {
129 warnx("Failed to connect to system bus: %s", strerror(-r));
130 return;
131 }
132
133 /* Register support console interface */
134 bytes = snprintf(obj_name, dbus_obj_path_len, OBJ_NAME,
135 console->console_id);
136 if (bytes >= dbus_obj_path_len) {
137 warnx("Console id '%s' is too long. There is no enough space in the buffer.",
138 console->console_id);
139 return;
140 }
141
142 r = sd_bus_add_object_vtable(console->bus, NULL, obj_name, INTF_NAME,
143 console_vtable, console);
144 if (r < 0) {
145 warnx("Failed to issue method call: %s", strerror(-r));
146 return;
147 }
148
149 bytes = snprintf(dbus_name, dbus_obj_path_len, DBUS_NAME,
150 console->console_id);
151 if (bytes >= dbus_obj_path_len) {
152 warnx("Console id '%s' is too long. There is no enough space in the buffer.",
153 console->console_id);
154 return;
155 }
156
157 r = sd_bus_request_name(console->bus, dbus_name,
158 SD_BUS_NAME_ALLOW_REPLACEMENT |
159 SD_BUS_NAME_REPLACE_EXISTING);
160 if (r < 0) {
161 warnx("Failed to acquire service name: %s", strerror(-r));
162 return;
163 }
164
165 fd = sd_bus_get_fd(console->bus);
166 if (fd < 0) {
167 warnx("Couldn't get the bus file descriptor");
168 return;
169 }
170
171 dbus_poller = POLLFD_DBUS;
172
173 console->pollfds[dbus_poller].fd = fd;
174 console->pollfds[dbus_poller].events = POLLIN;
175}