-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathconsole-mux.c
320 lines (255 loc) · 6.5 KB
/
console-mux.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <gpiod.h>
#include <limits.h>
#include <stddef.h>
#include <stdlib.h>
#include "console-server.h"
#include "console-mux.h"
#include "config.h"
struct console_gpio {
char *name;
struct gpiod_line *line;
};
struct console_mux {
struct console_gpio *mux_gpios;
size_t n_mux_gpios;
};
static const char *key_mux_gpios = "mux-gpios";
static const char *key_mux_index = "mux-index";
__attribute__((nonnull)) static size_t strtokcnt(const char *str,
const char sep)
{
ssize_t n = 1;
while (*str) {
if (*str == sep) {
n++;
}
str++;
}
return n;
}
__attribute__((nonnull)) static size_t
count_mux_gpios(const char *config_mux_gpios)
{
return strtokcnt(config_mux_gpios, ',');
}
__attribute__((nonnull)) static char *
extract_mux_gpio_name(const char **config_gpio_names)
{
const char *current;
const char *comma;
ptrdiff_t length;
assert(*config_gpio_names);
current = *config_gpio_names;
comma = strchrnul(current, ',');
length = comma - current;
if (length == 0) {
return NULL;
}
char *word = calloc(length + 1, 1);
if (!word) {
return NULL;
}
strncpy(word, current, length);
*config_gpio_names = comma + !!(*comma);
return word;
}
__attribute__((nonnull)) static struct console_gpio *
console_mux_find_gpio_by_index(struct console_gpio *gpio,
const char **config_gpio_names)
{
assert(*config_gpio_names);
gpio->name = extract_mux_gpio_name(config_gpio_names);
if (gpio->name == NULL) {
warnx("could not extract mux gpio name from config '%s'",
*config_gpio_names);
return NULL;
}
gpio->line = gpiod_line_find(gpio->name);
if (gpio->line == NULL) {
warnx("libgpiod: could not find line %s", gpio->name);
free(gpio->name);
return NULL;
}
return gpio;
}
__attribute__((nonnull)) static void
console_mux_release_gpio_lines(struct console_server *server)
{
for (unsigned long i = 0; i < server->mux->n_mux_gpios; i++) {
struct console_gpio *gpio = &server->mux->mux_gpios[i];
gpiod_line_release(gpio->line);
gpiod_line_close_chip(gpio->line);
free(gpio->name);
gpio->name = NULL;
}
}
__attribute__((nonnull)) static int
console_mux_request_gpio_lines(struct console_server *server,
const char *config_gpio_names)
{
const char *current = config_gpio_names;
struct console_gpio *gpio;
int status = 0;
for (server->mux->n_mux_gpios = 0; *current;
server->mux->n_mux_gpios++) {
size_t i = server->mux->n_mux_gpios;
gpio = console_mux_find_gpio_by_index(
&server->mux->mux_gpios[i], ¤t);
if (gpio == NULL) {
console_mux_release_gpio_lines(server);
return -1;
}
status = gpiod_line_request_output(
gpio->line, program_invocation_short_name, 0);
if (status != 0) {
warnx("could not set line %s as output", gpio->name);
warnx("releasing all lines already requested");
console_mux_release_gpio_lines(server);
return -1;
}
}
return 0;
}
int console_server_mux_init(struct console_server *server)
{
const char *config_gpio_names;
size_t max_ngpios;
size_t ngpios;
config_gpio_names = config_get_value(server->config, key_mux_gpios);
if (!config_gpio_names) {
return 0;
}
ngpios = count_mux_gpios(config_gpio_names);
max_ngpios = sizeof(((struct console *)0)->mux_index) * CHAR_BIT;
if (ngpios > max_ngpios) {
return -1;
}
server->mux = calloc(1, sizeof(struct console_mux));
if (!server->mux) {
return -1;
}
server->mux->n_mux_gpios = 0;
server->mux->mux_gpios = calloc(ngpios, sizeof(struct console_gpio));
if (!server->mux->mux_gpios) {
return -1;
}
return console_mux_request_gpio_lines(server, config_gpio_names);
}
void console_server_mux_fini(struct console_server *server)
{
if (!server->mux) {
return;
}
console_mux_release_gpio_lines(server);
free(server->mux->mux_gpios);
server->mux->mux_gpios = NULL;
free(server->mux);
server->mux = NULL;
}
int console_mux_init(struct console *console, struct config *config)
{
if (!console->server->mux) {
return 0;
}
if (console->server->mux->n_mux_gpios == 0) {
return 0;
}
const char *gpio_value = config_get_section_value(
config, console->console_id, key_mux_index);
if (gpio_value == NULL) {
warnx("console %s does not have property %s in config",
console->console_id, key_mux_index);
return -1;
}
errno = 0;
console->mux_index = strtoul(gpio_value, NULL, 0);
if (errno == ERANGE) {
return -1;
}
return 0;
}
static int console_timestamp(char *buffer, size_t size)
{
size_t status;
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = gmtime(&rawtime);
status = strftime(buffer, size, "%Y-%m-%d %H:%M:%S UTC", timeinfo);
return !status;
}
static int console_print_timestamped(struct console *console,
const char *message)
{
#define TIMESTAMP_MAX_SIZE 32
char buf_timestamp[TIMESTAMP_MAX_SIZE];
int status;
char *buf;
status = console_timestamp(buf_timestamp, sizeof(buf_timestamp));
if (status != 0) {
warnx("Error: unable to print timestamp");
return status;
}
status = asprintf(&buf, "[obmc-console] %s %s\n", buf_timestamp,
message);
if (status == -1) {
return -1;
}
ringbuffer_queue(console->rb, (uint8_t *)buf, strlen(buf));
free(buf);
return 0;
}
static int console_mux_set_lines(struct console *console)
{
int status = 0;
for (size_t i = 0; i < console->server->mux->n_mux_gpios; i++) {
struct console_gpio *gpio = &console->server->mux->mux_gpios[i];
const uint8_t value = (console->mux_index >> i) & 0x1;
status = gpiod_line_set_value(gpio->line, value);
if (status != 0) {
warnx("could not set line %s", gpio->name);
return -1;
}
}
return 0;
}
int console_mux_activate(struct console *console)
{
struct console_server *server = console->server;
const bool first_activation = server->active == NULL;
const bool is_active = server->active == console;
int status = 0;
if (is_active) {
return 0;
}
if (server->mux) {
status = console_mux_set_lines(console);
}
if (status != 0) {
warnx("Error: unable to set mux gpios");
return status;
}
server->active = console;
/* Don't print disconnect/connect events on startup */
if (first_activation) {
return 0;
}
for (size_t i = 0; i < server->n_consoles; i++) {
struct console *other = server->consoles[i];
if (other == console) {
continue;
}
console_print_timestamped(other, "DISCONNECTED");
for (long j = 0; j < other->n_handlers; j++) {
struct handler *h = other->handlers[j];
if (h->type->deselect) {
h->type->deselect(h);
}
}
}
console_print_timestamped(console, "CONNECTED");
return 0;
}