blob: 796872170fba6ec89f1ef2a00f22bb39ac477891 [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"
24
25/* size of the dbus object path length */
26const size_t dbus_obj_path_len = 1024;
27
Ninad Palsuleb14ca192023-03-31 09:49:00 -050028#define DBUS_ERR "org.openbmc.error"
29#define DBUS_NAME "xyz.openbmc_project.Console.%s"
30#define OBJ_NAME "/xyz/openbmc_project/console/%s"
Jonathan Doman9598b862023-06-01 10:21:25 -070031#define UART_INTF "xyz.openbmc_project.Console.UART"
Ninad Palsuleb14ca192023-03-31 09:49:00 -050032#define ACCESS_INTF "xyz.openbmc_project.Console.Access"
Ninad Palsulee258e512023-04-26 22:17:57 -050033
34static void tty_change_baudrate(struct console *console)
35{
Ninad Palsulee258e512023-04-26 22:17:57 -050036 int i;
37 int rc;
38
39 tty_init_termios(console);
40
41 for (i = 0; i < console->n_handlers; i++) {
Jeremy Kerre2826c72024-07-05 10:54:21 +080042 const struct handler_type *type;
43 struct handler *handler;
44
Ninad Palsulee258e512023-04-26 22:17:57 -050045 handler = console->handlers[i];
Jeremy Kerre2826c72024-07-05 10:54:21 +080046 type = handler->type;
47 if (!type->baudrate) {
Ninad Palsulee258e512023-04-26 22:17:57 -050048 continue;
49 }
50
Jeremy Kerre2826c72024-07-05 10:54:21 +080051 rc = type->baudrate(handler, console->tty.uart.baud);
Ninad Palsulee258e512023-04-26 22:17:57 -050052 if (rc) {
53 warnx("Can't set terminal baudrate for handler %s",
Jeremy Kerre2826c72024-07-05 10:54:21 +080054 type->name);
Ninad Palsulee258e512023-04-26 22:17:57 -050055 }
56 }
57}
58
Jonathan Doman9598b862023-06-01 10:21:25 -070059static int set_baud_handler(sd_bus *bus, const char *path,
60 const char *interface, const char *property,
61 sd_bus_message *msg, void *userdata,
62 sd_bus_error *err __attribute__((unused)))
63{
64 struct console *console = userdata;
65 uint64_t baudrate;
66 speed_t speed;
67 int r;
68
69 if (!console) {
70 return -ENOENT;
71 }
72
73 r = sd_bus_message_read(msg, "t", &baudrate);
74 if (r < 0 || baudrate > UINT32_MAX) {
75 return -EINVAL;
76 }
77
78 speed = parse_int_to_baud((uint32_t)baudrate);
79 if (!speed) {
80 warnx("Invalid baud rate: '%" PRIu64 "'", baudrate);
81 return -EINVAL;
82 }
83
84 assert(console->tty.type == TTY_DEVICE_UART);
85 console->tty.uart.baud = speed;
86 tty_change_baudrate(console);
87
88 sd_bus_emit_properties_changed(bus, path, interface, property, NULL);
89
90 return 1;
91}
92
Jonathan Doman9598b862023-06-01 10:21:25 -070093static int get_baud_handler(sd_bus *bus __attribute__((unused)),
94 const char *path __attribute__((unused)),
95 const char *interface __attribute__((unused)),
96 const char *property __attribute__((unused)),
97 sd_bus_message *reply, void *userdata,
98 sd_bus_error *error __attribute__((unused)))
99{
100 struct console *console = userdata;
101 uint64_t baudrate;
102 int r;
103
104 assert(console->tty.type == TTY_DEVICE_UART);
105 baudrate = parse_baud_to_int(console->tty.uart.baud);
106 if (!baudrate) {
107 warnx("Invalid baud rate: '%d'", console->tty.uart.baud);
108 }
109
110 r = sd_bus_message_append(reply, "t", baudrate);
111
112 return r;
113}
114
Ninad Palsulebd992c92023-05-09 18:30:01 -0500115static int method_connect(sd_bus_message *msg, void *userdata,
116 sd_bus_error *err)
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500117{
118 struct console *console = userdata;
Ninad Palsulebd992c92023-05-09 18:30:01 -0500119 int rc;
120 int socket_fd = -1;
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500121
Ninad Palsulebd992c92023-05-09 18:30:01 -0500122 if (!console) {
123 warnx("Internal error: Console pointer is null");
124 sd_bus_error_set_const(err, DBUS_ERR, "Internal error");
125 return sd_bus_reply_method_error(msg, err);
126 }
127
128 /* Register the consumer. */
129 socket_fd = dbus_create_socket_consumer(console);
130 if (socket_fd < 0) {
131 rc = socket_fd;
132 warnx("Failed to create socket consumer: %s", strerror(rc));
133 sd_bus_error_set_const(err, DBUS_ERR,
134 "Failed to create socket consumer");
135 return sd_bus_reply_method_error(msg, err);
136 }
137
138 rc = sd_bus_reply_method_return(msg, "h", socket_fd);
139
140 /* Close the our end */
141 close(socket_fd);
142
143 return rc;
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500144}
145
Jonathan Doman9598b862023-06-01 10:21:25 -0700146static const sd_bus_vtable console_uart_vtable[] = {
147 SD_BUS_VTABLE_START(0),
148 SD_BUS_WRITABLE_PROPERTY("Baud", "t", get_baud_handler,
149 set_baud_handler, 0,
150 SD_BUS_VTABLE_UNPRIVILEGED |
151 SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
152 SD_BUS_VTABLE_END,
153};
154
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500155static const sd_bus_vtable console_access_vtable[] = {
156 SD_BUS_VTABLE_START(0),
Ninad Palsulebd992c92023-05-09 18:30:01 -0500157 SD_BUS_METHOD("Connect", SD_BUS_NO_ARGS, "h", method_connect,
158 SD_BUS_VTABLE_UNPRIVILEGED),
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500159 SD_BUS_VTABLE_END,
160};
161
Ninad Palsulee258e512023-04-26 22:17:57 -0500162void dbus_init(struct console *console,
163 struct config *config __attribute__((unused)))
164{
165 char obj_name[dbus_obj_path_len];
166 char dbus_name[dbus_obj_path_len];
167 int dbus_poller = 0;
168 int fd;
169 int r;
170 size_t bytes;
171
172 if (!console) {
173 warnx("Couldn't get valid console");
174 return;
175 }
176
Andrew Jeffery12398cd2024-07-09 14:59:33 +0930177 r = sd_bus_default(&console->bus);
Ninad Palsulee258e512023-04-26 22:17:57 -0500178 if (r < 0) {
Andrew Jeffery12398cd2024-07-09 14:59:33 +0930179 warnx("Failed to connect to bus: %s", strerror(-r));
Ninad Palsulee258e512023-04-26 22:17:57 -0500180 return;
181 }
182
183 /* Register support console interface */
184 bytes = snprintf(obj_name, dbus_obj_path_len, OBJ_NAME,
185 console->console_id);
186 if (bytes >= dbus_obj_path_len) {
187 warnx("Console id '%s' is too long. There is no enough space in the buffer.",
188 console->console_id);
189 return;
190 }
191
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930192 if (console->tty.type == TTY_DEVICE_UART) {
Jonathan Doman9598b862023-06-01 10:21:25 -0700193 /* Register UART interface */
194 r = sd_bus_add_object_vtable(console->bus, NULL, obj_name,
195 UART_INTF, console_uart_vtable,
196 console);
197 if (r < 0) {
198 warnx("Failed to register UART interface: %s",
199 strerror(-r));
200 return;
201 }
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500202 }
203
204 /* Register access interface */
205 r = sd_bus_add_object_vtable(console->bus, NULL, obj_name, ACCESS_INTF,
206 console_access_vtable, console);
Ninad Palsulee258e512023-04-26 22:17:57 -0500207 if (r < 0) {
208 warnx("Failed to issue method call: %s", strerror(-r));
209 return;
210 }
211
212 bytes = snprintf(dbus_name, dbus_obj_path_len, DBUS_NAME,
213 console->console_id);
214 if (bytes >= dbus_obj_path_len) {
215 warnx("Console id '%s' is too long. There is no enough space in the buffer.",
216 console->console_id);
217 return;
218 }
219
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500220 /* Finally register the bus name */
Ninad Palsulee258e512023-04-26 22:17:57 -0500221 r = sd_bus_request_name(console->bus, dbus_name,
222 SD_BUS_NAME_ALLOW_REPLACEMENT |
223 SD_BUS_NAME_REPLACE_EXISTING);
224 if (r < 0) {
225 warnx("Failed to acquire service name: %s", strerror(-r));
226 return;
227 }
228
229 fd = sd_bus_get_fd(console->bus);
230 if (fd < 0) {
231 warnx("Couldn't get the bus file descriptor");
232 return;
233 }
234
235 dbus_poller = POLLFD_DBUS;
236
237 console->pollfds[dbus_poller].fd = fd;
238 console->pollfds[dbus_poller].events = POLLIN;
239}