blob: ba8356d87623e77b6e305c9d9a782616363e7892 [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
23#include "console-server.h"
Alexander Hansen1e04f442024-06-12 16:35:58 +020024#include "config.h"
Ninad Palsulee258e512023-04-26 22:17:57 -050025
26/* size of the dbus object path length */
27const size_t dbus_obj_path_len = 1024;
28
Ninad Palsuleb14ca192023-03-31 09:49:00 -050029#define DBUS_ERR "org.openbmc.error"
30#define DBUS_NAME "xyz.openbmc_project.Console.%s"
31#define OBJ_NAME "/xyz/openbmc_project/console/%s"
Jonathan Doman9598b862023-06-01 10:21:25 -070032#define UART_INTF "xyz.openbmc_project.Console.UART"
Ninad Palsuleb14ca192023-03-31 09:49:00 -050033#define ACCESS_INTF "xyz.openbmc_project.Console.Access"
Ninad Palsulee258e512023-04-26 22:17:57 -050034
35static void tty_change_baudrate(struct console *console)
36{
Ninad Palsulee258e512023-04-26 22:17:57 -050037 int i;
38 int rc;
39
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +020040 tty_init_termios(console->server);
Ninad Palsulee258e512023-04-26 22:17:57 -050041
42 for (i = 0; i < console->n_handlers; i++) {
Jeremy Kerre2826c72024-07-05 10:54:21 +080043 const struct handler_type *type;
44 struct handler *handler;
45
Ninad Palsulee258e512023-04-26 22:17:57 -050046 handler = console->handlers[i];
Jeremy Kerre2826c72024-07-05 10:54:21 +080047 type = handler->type;
48 if (!type->baudrate) {
Ninad Palsulee258e512023-04-26 22:17:57 -050049 continue;
50 }
51
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +020052 rc = type->baudrate(handler, console->server->tty.uart.baud);
Ninad Palsulee258e512023-04-26 22:17:57 -050053 if (rc) {
54 warnx("Can't set terminal baudrate for handler %s",
Jeremy Kerre2826c72024-07-05 10:54:21 +080055 type->name);
Ninad Palsulee258e512023-04-26 22:17:57 -050056 }
57 }
58}
59
Jonathan Doman9598b862023-06-01 10:21:25 -070060static int set_baud_handler(sd_bus *bus, const char *path,
61 const char *interface, const char *property,
62 sd_bus_message *msg, void *userdata,
63 sd_bus_error *err __attribute__((unused)))
64{
65 struct console *console = userdata;
66 uint64_t baudrate;
67 speed_t speed;
68 int r;
69
70 if (!console) {
71 return -ENOENT;
72 }
73
74 r = sd_bus_message_read(msg, "t", &baudrate);
75 if (r < 0 || baudrate > UINT32_MAX) {
76 return -EINVAL;
77 }
78
79 speed = parse_int_to_baud((uint32_t)baudrate);
80 if (!speed) {
81 warnx("Invalid baud rate: '%" PRIu64 "'", baudrate);
82 return -EINVAL;
83 }
84
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +020085 assert(console->server->tty.type == TTY_DEVICE_UART);
86 console->server->tty.uart.baud = speed;
Jonathan Doman9598b862023-06-01 10:21:25 -070087 tty_change_baudrate(console);
88
89 sd_bus_emit_properties_changed(bus, path, interface, property, NULL);
90
91 return 1;
92}
93
Jonathan Doman9598b862023-06-01 10:21:25 -070094static int get_baud_handler(sd_bus *bus __attribute__((unused)),
95 const char *path __attribute__((unused)),
96 const char *interface __attribute__((unused)),
97 const char *property __attribute__((unused)),
98 sd_bus_message *reply, void *userdata,
99 sd_bus_error *error __attribute__((unused)))
100{
101 struct console *console = userdata;
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200102 struct console_server *server = console->server;
Jonathan Doman9598b862023-06-01 10:21:25 -0700103 uint64_t baudrate;
104 int r;
105
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200106 assert(server->tty.type == TTY_DEVICE_UART);
107 baudrate = parse_baud_to_int(server->tty.uart.baud);
Jonathan Doman9598b862023-06-01 10:21:25 -0700108 if (!baudrate) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200109 warnx("Invalid baud rate: '%d'", server->tty.uart.baud);
Jonathan Doman9598b862023-06-01 10:21:25 -0700110 }
111
112 r = sd_bus_message_append(reply, "t", baudrate);
113
114 return r;
115}
116
Ninad Palsulebd992c92023-05-09 18:30:01 -0500117static int method_connect(sd_bus_message *msg, void *userdata,
118 sd_bus_error *err)
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500119{
120 struct console *console = userdata;
Ninad Palsulebd992c92023-05-09 18:30:01 -0500121 int rc;
122 int socket_fd = -1;
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500123
Ninad Palsulebd992c92023-05-09 18:30:01 -0500124 if (!console) {
125 warnx("Internal error: Console pointer is null");
126 sd_bus_error_set_const(err, DBUS_ERR, "Internal error");
127 return sd_bus_reply_method_error(msg, err);
128 }
129
130 /* Register the consumer. */
131 socket_fd = dbus_create_socket_consumer(console);
132 if (socket_fd < 0) {
133 rc = socket_fd;
134 warnx("Failed to create socket consumer: %s", strerror(rc));
135 sd_bus_error_set_const(err, DBUS_ERR,
136 "Failed to create socket consumer");
137 return sd_bus_reply_method_error(msg, err);
138 }
139
140 rc = sd_bus_reply_method_return(msg, "h", socket_fd);
141
142 /* Close the our end */
143 close(socket_fd);
144
145 return rc;
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500146}
147
Jonathan Doman9598b862023-06-01 10:21:25 -0700148static const sd_bus_vtable console_uart_vtable[] = {
149 SD_BUS_VTABLE_START(0),
150 SD_BUS_WRITABLE_PROPERTY("Baud", "t", get_baud_handler,
151 set_baud_handler, 0,
152 SD_BUS_VTABLE_UNPRIVILEGED |
153 SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
154 SD_BUS_VTABLE_END,
155};
156
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500157static const sd_bus_vtable console_access_vtable[] = {
158 SD_BUS_VTABLE_START(0),
Ninad Palsulebd992c92023-05-09 18:30:01 -0500159 SD_BUS_METHOD("Connect", SD_BUS_NO_ARGS, "h", method_connect,
160 SD_BUS_VTABLE_UNPRIVILEGED),
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500161 SD_BUS_VTABLE_END,
162};
163
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930164int dbus_init(struct console *console,
165 struct config *config __attribute__((unused)))
Ninad Palsulee258e512023-04-26 22:17:57 -0500166{
167 char obj_name[dbus_obj_path_len];
168 char dbus_name[dbus_obj_path_len];
Ninad Palsulee258e512023-04-26 22:17:57 -0500169 int fd;
170 int r;
171 size_t bytes;
172
173 if (!console) {
174 warnx("Couldn't get valid console");
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930175 return -1;
Ninad Palsulee258e512023-04-26 22:17:57 -0500176 }
177
Andrew Jeffery12398cd2024-07-09 14:59:33 +0930178 r = sd_bus_default(&console->bus);
Ninad Palsulee258e512023-04-26 22:17:57 -0500179 if (r < 0) {
Andrew Jeffery12398cd2024-07-09 14:59:33 +0930180 warnx("Failed to connect to bus: %s", strerror(-r));
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930181 return -1;
Ninad Palsulee258e512023-04-26 22:17:57 -0500182 }
183
184 /* Register support console interface */
185 bytes = snprintf(obj_name, dbus_obj_path_len, OBJ_NAME,
186 console->console_id);
187 if (bytes >= dbus_obj_path_len) {
188 warnx("Console id '%s' is too long. There is no enough space in the buffer.",
189 console->console_id);
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930190 return -1;
Ninad Palsulee258e512023-04-26 22:17:57 -0500191 }
192
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200193 if (console->server->tty.type == TTY_DEVICE_UART) {
Jonathan Doman9598b862023-06-01 10:21:25 -0700194 /* Register UART interface */
195 r = sd_bus_add_object_vtable(console->bus, NULL, obj_name,
196 UART_INTF, console_uart_vtable,
197 console);
198 if (r < 0) {
199 warnx("Failed to register UART interface: %s",
200 strerror(-r));
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930201 return -1;
Jonathan Doman9598b862023-06-01 10:21:25 -0700202 }
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500203 }
204
205 /* Register access interface */
206 r = sd_bus_add_object_vtable(console->bus, NULL, obj_name, ACCESS_INTF,
207 console_access_vtable, console);
Ninad Palsulee258e512023-04-26 22:17:57 -0500208 if (r < 0) {
209 warnx("Failed to issue method call: %s", strerror(-r));
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930210 return -1;
Ninad Palsulee258e512023-04-26 22:17:57 -0500211 }
212
213 bytes = snprintf(dbus_name, dbus_obj_path_len, DBUS_NAME,
214 console->console_id);
215 if (bytes >= dbus_obj_path_len) {
216 warnx("Console id '%s' is too long. There is no enough space in the buffer.",
217 console->console_id);
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930218 return -1;
Ninad Palsulee258e512023-04-26 22:17:57 -0500219 }
220
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500221 /* Finally register the bus name */
Ninad Palsulee258e512023-04-26 22:17:57 -0500222 r = sd_bus_request_name(console->bus, dbus_name,
223 SD_BUS_NAME_ALLOW_REPLACEMENT |
224 SD_BUS_NAME_REPLACE_EXISTING);
225 if (r < 0) {
226 warnx("Failed to acquire service name: %s", strerror(-r));
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930227 return -1;
Ninad Palsulee258e512023-04-26 22:17:57 -0500228 }
229
230 fd = sd_bus_get_fd(console->bus);
231 if (fd < 0) {
232 warnx("Couldn't get the bus file descriptor");
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930233 return -1;
Ninad Palsulee258e512023-04-26 22:17:57 -0500234 }
235
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200236 const ssize_t index =
237 console_server_request_pollfd(console->server, fd, POLLIN);
238 if (index < 0) {
239 fprintf(stderr, "Error: failed to allocate pollfd\n");
240 return -1;
241 }
Ninad Palsulee258e512023-04-26 22:17:57 -0500242
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200243 console->dbus_pollfd_index = index;
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930244 return 0;
Ninad Palsulee258e512023-04-26 22:17:57 -0500245}