blob: 778f1cda61edfc602663e60b74a6ed19b622a64d [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
Andrew Jeffery30ea6382023-05-03 16:56:10 +093017#include <assert.h>
Ninad Palsulee258e512023-04-26 22:17:57 -050018#include <errno.h>
19#include <err.h>
Ninad Palsulebd992c92023-05-09 18:30:01 -050020#include <string.h>
21#include <sys/socket.h>
Ninad Palsulee258e512023-04-26 22:17:57 -050022
Alexander Hansen1e04f442024-06-12 16:35:58 +020023#include "config.h"
Alexander Hansena6b29102024-07-10 15:33:37 +020024#include "console-mux.h"
25#include "console-server.h"
Ninad Palsulee258e512023-04-26 22:17:57 -050026
27/* size of the dbus object path length */
28const size_t dbus_obj_path_len = 1024;
29
Ninad Palsuleb14ca192023-03-31 09:49:00 -050030#define DBUS_ERR "org.openbmc.error"
31#define DBUS_NAME "xyz.openbmc_project.Console.%s"
32#define OBJ_NAME "/xyz/openbmc_project/console/%s"
Jonathan Doman9598b862023-06-01 10:21:25 -070033#define UART_INTF "xyz.openbmc_project.Console.UART"
Ninad Palsuleb14ca192023-03-31 09:49:00 -050034#define ACCESS_INTF "xyz.openbmc_project.Console.Access"
Ninad Palsulee258e512023-04-26 22:17:57 -050035
36static void tty_change_baudrate(struct console *console)
37{
Ninad Palsulee258e512023-04-26 22:17:57 -050038 int i;
39 int rc;
40
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +020041 tty_init_termios(console->server);
Ninad Palsulee258e512023-04-26 22:17:57 -050042
43 for (i = 0; i < console->n_handlers; i++) {
Jeremy Kerre2826c72024-07-05 10:54:21 +080044 const struct handler_type *type;
45 struct handler *handler;
46
Ninad Palsulee258e512023-04-26 22:17:57 -050047 handler = console->handlers[i];
Jeremy Kerre2826c72024-07-05 10:54:21 +080048 type = handler->type;
49 if (!type->baudrate) {
Ninad Palsulee258e512023-04-26 22:17:57 -050050 continue;
51 }
52
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +020053 rc = type->baudrate(handler, console->server->tty.uart.baud);
Ninad Palsulee258e512023-04-26 22:17:57 -050054 if (rc) {
55 warnx("Can't set terminal baudrate for handler %s",
Jeremy Kerre2826c72024-07-05 10:54:21 +080056 type->name);
Ninad Palsulee258e512023-04-26 22:17:57 -050057 }
58 }
59}
60
Jonathan Doman9598b862023-06-01 10:21:25 -070061static int set_baud_handler(sd_bus *bus, const char *path,
62 const char *interface, const char *property,
63 sd_bus_message *msg, void *userdata,
64 sd_bus_error *err __attribute__((unused)))
65{
66 struct console *console = userdata;
67 uint64_t baudrate;
68 speed_t speed;
69 int r;
70
71 if (!console) {
72 return -ENOENT;
73 }
74
75 r = sd_bus_message_read(msg, "t", &baudrate);
76 if (r < 0 || baudrate > UINT32_MAX) {
77 return -EINVAL;
78 }
79
80 speed = parse_int_to_baud((uint32_t)baudrate);
81 if (!speed) {
82 warnx("Invalid baud rate: '%" PRIu64 "'", baudrate);
83 return -EINVAL;
84 }
85
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +020086 assert(console->server->tty.type == TTY_DEVICE_UART);
87 console->server->tty.uart.baud = speed;
Jonathan Doman9598b862023-06-01 10:21:25 -070088 tty_change_baudrate(console);
89
90 sd_bus_emit_properties_changed(bus, path, interface, property, NULL);
91
92 return 1;
93}
94
Jonathan Doman9598b862023-06-01 10:21:25 -070095static int get_baud_handler(sd_bus *bus __attribute__((unused)),
96 const char *path __attribute__((unused)),
97 const char *interface __attribute__((unused)),
98 const char *property __attribute__((unused)),
99 sd_bus_message *reply, void *userdata,
100 sd_bus_error *error __attribute__((unused)))
101{
102 struct console *console = userdata;
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200103 struct console_server *server = console->server;
Jonathan Doman9598b862023-06-01 10:21:25 -0700104 uint64_t baudrate;
105 int r;
106
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200107 assert(server->tty.type == TTY_DEVICE_UART);
108 baudrate = parse_baud_to_int(server->tty.uart.baud);
Jonathan Doman9598b862023-06-01 10:21:25 -0700109 if (!baudrate) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200110 warnx("Invalid baud rate: '%d'", server->tty.uart.baud);
Jonathan Doman9598b862023-06-01 10:21:25 -0700111 }
112
113 r = sd_bus_message_append(reply, "t", baudrate);
114
115 return r;
116}
117
Ninad Palsulebd992c92023-05-09 18:30:01 -0500118static int method_connect(sd_bus_message *msg, void *userdata,
119 sd_bus_error *err)
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500120{
121 struct console *console = userdata;
Ninad Palsulebd992c92023-05-09 18:30:01 -0500122 int rc;
123 int socket_fd = -1;
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500124
Ninad Palsulebd992c92023-05-09 18:30:01 -0500125 if (!console) {
126 warnx("Internal error: Console pointer is null");
127 sd_bus_error_set_const(err, DBUS_ERR, "Internal error");
128 return sd_bus_reply_method_error(msg, err);
129 }
130
Alexander Hansena6b29102024-07-10 15:33:37 +0200131 console_mux_activate(console);
132
Ninad Palsulebd992c92023-05-09 18:30:01 -0500133 /* Register the consumer. */
134 socket_fd = dbus_create_socket_consumer(console);
135 if (socket_fd < 0) {
136 rc = socket_fd;
137 warnx("Failed to create socket consumer: %s", strerror(rc));
138 sd_bus_error_set_const(err, DBUS_ERR,
139 "Failed to create socket consumer");
140 return sd_bus_reply_method_error(msg, err);
141 }
142
143 rc = sd_bus_reply_method_return(msg, "h", socket_fd);
144
145 /* Close the our end */
146 close(socket_fd);
147
148 return rc;
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500149}
150
Jonathan Doman9598b862023-06-01 10:21:25 -0700151static const sd_bus_vtable console_uart_vtable[] = {
152 SD_BUS_VTABLE_START(0),
153 SD_BUS_WRITABLE_PROPERTY("Baud", "t", get_baud_handler,
154 set_baud_handler, 0,
155 SD_BUS_VTABLE_UNPRIVILEGED |
156 SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
157 SD_BUS_VTABLE_END,
158};
159
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500160static const sd_bus_vtable console_access_vtable[] = {
161 SD_BUS_VTABLE_START(0),
Ninad Palsulebd992c92023-05-09 18:30:01 -0500162 SD_BUS_METHOD("Connect", SD_BUS_NO_ARGS, "h", method_connect,
163 SD_BUS_VTABLE_UNPRIVILEGED),
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500164 SD_BUS_VTABLE_END,
165};
166
Alexander Hansen312ecdc2024-07-10 15:33:37 +0200167int dbus_server_init(struct console_server *server)
168{
169 int r;
170 int fd;
171 r = sd_bus_default(&server->bus);
172 if (r < 0) {
173 warnx("Failed to connect to bus: %s", strerror(-r));
174 return -1;
175 }
176
177 fd = sd_bus_get_fd(server->bus);
178 if (fd < 0) {
179 warnx("Couldn't get the bus file descriptor");
180 sd_bus_unref(server->bus);
181 return -1;
182 }
183
184 const ssize_t index = console_server_request_pollfd(server, fd, POLLIN);
185 if (index < 0) {
186 warnx("Error: failed to allocate pollfd");
187 sd_bus_unref(server->bus);
188 return -1;
189 }
190
191 server->dbus_pollfd_index = index;
192 return 0;
193}
194
195void dbus_server_fini(struct console_server *server)
196{
197 if (server->dbus_pollfd_index < server->capacity_pollfds) {
198 console_server_release_pollfd(server,
199 server->dbus_pollfd_index);
200 server->dbus_pollfd_index = SIZE_MAX;
201 }
202
203 sd_bus_unref(server->bus);
204}
205
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930206int dbus_init(struct console *console,
207 struct config *config __attribute__((unused)))
Ninad Palsulee258e512023-04-26 22:17:57 -0500208{
209 char obj_name[dbus_obj_path_len];
210 char dbus_name[dbus_obj_path_len];
Ninad Palsulee258e512023-04-26 22:17:57 -0500211 int r;
212 size_t bytes;
213
214 if (!console) {
215 warnx("Couldn't get valid console");
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930216 return -1;
Ninad Palsulee258e512023-04-26 22:17:57 -0500217 }
218
Ninad Palsulee258e512023-04-26 22:17:57 -0500219 /* Register support console interface */
220 bytes = snprintf(obj_name, dbus_obj_path_len, OBJ_NAME,
221 console->console_id);
222 if (bytes >= dbus_obj_path_len) {
223 warnx("Console id '%s' is too long. There is no enough space in the buffer.",
224 console->console_id);
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930225 return -1;
Ninad Palsulee258e512023-04-26 22:17:57 -0500226 }
227
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200228 if (console->server->tty.type == TTY_DEVICE_UART) {
Jonathan Doman9598b862023-06-01 10:21:25 -0700229 /* Register UART interface */
Alexander Hansen312ecdc2024-07-10 15:33:37 +0200230 r = sd_bus_add_object_vtable(console->server->bus, NULL,
231 obj_name, UART_INTF,
232 console_uart_vtable, console);
Jonathan Doman9598b862023-06-01 10:21:25 -0700233 if (r < 0) {
234 warnx("Failed to register UART interface: %s",
235 strerror(-r));
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930236 return -1;
Jonathan Doman9598b862023-06-01 10:21:25 -0700237 }
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500238 }
239
240 /* Register access interface */
Alexander Hansen312ecdc2024-07-10 15:33:37 +0200241 r = sd_bus_add_object_vtable(console->server->bus, NULL, obj_name,
242 ACCESS_INTF, console_access_vtable,
243 console);
Ninad Palsulee258e512023-04-26 22:17:57 -0500244 if (r < 0) {
245 warnx("Failed to issue method call: %s", strerror(-r));
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930246 return -1;
Ninad Palsulee258e512023-04-26 22:17:57 -0500247 }
248
249 bytes = snprintf(dbus_name, dbus_obj_path_len, DBUS_NAME,
250 console->console_id);
251 if (bytes >= dbus_obj_path_len) {
252 warnx("Console id '%s' is too long. There is no enough space in the buffer.",
253 console->console_id);
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930254 return -1;
Ninad Palsulee258e512023-04-26 22:17:57 -0500255 }
256
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500257 /* Finally register the bus name */
Alexander Hansen312ecdc2024-07-10 15:33:37 +0200258 r = sd_bus_request_name(console->server->bus, dbus_name,
Ninad Palsulee258e512023-04-26 22:17:57 -0500259 SD_BUS_NAME_ALLOW_REPLACEMENT |
260 SD_BUS_NAME_REPLACE_EXISTING);
261 if (r < 0) {
262 warnx("Failed to acquire service name: %s", strerror(-r));
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930263 return -1;
Ninad Palsulee258e512023-04-26 22:17:57 -0500264 }
265
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930266 return 0;
Ninad Palsulee258e512023-04-26 22:17:57 -0500267}