blob: d9b8689bc9fb8d8de123ce8e56ecaad58868dc4a [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
40 tty_init_termios(console);
41
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
Jeremy Kerre2826c72024-07-05 10:54:21 +080052 rc = type->baudrate(handler, console->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
85 assert(console->tty.type == TTY_DEVICE_UART);
86 console->tty.uart.baud = speed;
87 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;
102 uint64_t baudrate;
103 int r;
104
105 assert(console->tty.type == TTY_DEVICE_UART);
106 baudrate = parse_baud_to_int(console->tty.uart.baud);
107 if (!baudrate) {
108 warnx("Invalid baud rate: '%d'", console->tty.uart.baud);
109 }
110
111 r = sd_bus_message_append(reply, "t", baudrate);
112
113 return r;
114}
115
Ninad Palsulebd992c92023-05-09 18:30:01 -0500116static int method_connect(sd_bus_message *msg, void *userdata,
117 sd_bus_error *err)
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500118{
119 struct console *console = userdata;
Ninad Palsulebd992c92023-05-09 18:30:01 -0500120 int rc;
121 int socket_fd = -1;
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500122
Ninad Palsulebd992c92023-05-09 18:30:01 -0500123 if (!console) {
124 warnx("Internal error: Console pointer is null");
125 sd_bus_error_set_const(err, DBUS_ERR, "Internal error");
126 return sd_bus_reply_method_error(msg, err);
127 }
128
129 /* Register the consumer. */
130 socket_fd = dbus_create_socket_consumer(console);
131 if (socket_fd < 0) {
132 rc = socket_fd;
133 warnx("Failed to create socket consumer: %s", strerror(rc));
134 sd_bus_error_set_const(err, DBUS_ERR,
135 "Failed to create socket consumer");
136 return sd_bus_reply_method_error(msg, err);
137 }
138
139 rc = sd_bus_reply_method_return(msg, "h", socket_fd);
140
141 /* Close the our end */
142 close(socket_fd);
143
144 return rc;
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500145}
146
Jonathan Doman9598b862023-06-01 10:21:25 -0700147static const sd_bus_vtable console_uart_vtable[] = {
148 SD_BUS_VTABLE_START(0),
149 SD_BUS_WRITABLE_PROPERTY("Baud", "t", get_baud_handler,
150 set_baud_handler, 0,
151 SD_BUS_VTABLE_UNPRIVILEGED |
152 SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
153 SD_BUS_VTABLE_END,
154};
155
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500156static const sd_bus_vtable console_access_vtable[] = {
157 SD_BUS_VTABLE_START(0),
Ninad Palsulebd992c92023-05-09 18:30:01 -0500158 SD_BUS_METHOD("Connect", SD_BUS_NO_ARGS, "h", method_connect,
159 SD_BUS_VTABLE_UNPRIVILEGED),
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500160 SD_BUS_VTABLE_END,
161};
162
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930163int dbus_init(struct console *console,
164 struct config *config __attribute__((unused)))
Ninad Palsulee258e512023-04-26 22:17:57 -0500165{
166 char obj_name[dbus_obj_path_len];
167 char dbus_name[dbus_obj_path_len];
168 int dbus_poller = 0;
169 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
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930193 if (console->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
236 dbus_poller = POLLFD_DBUS;
237
238 console->pollfds[dbus_poller].fd = fd;
239 console->pollfds[dbus_poller].events = POLLIN;
Andrew Jeffery498a4a82024-07-09 15:07:05 +0930240
241 return 0;
Ninad Palsulee258e512023-04-26 22:17:57 -0500242}