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
|
/*
* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright (c) 2013 Free Software Foundation, Inc.
* Copyright (c) 2023, 2026 Leah Rowe <leah@libreboot.org>
*
* This program receives text encoded as pulses on the PC speaker,
* and decodes them. This is a special type of interface provided
* by coreboot and GRUB, for computers that lack serial ports.
*
* Usage example:
* parec --channels=1 --rate=48000 --format=s16le | ./spkmodem-recv
*
* Originally provided by GNU GRUB, this version is a heavily
* modified fork that complies with the OpenBSD Kernel Source
* File Style Guide (KNF) instead of GNU coding standards; it
* emphasises strict error handling, portability and code
* quality, as characterised by OpenBSD projects.
*
* This fork of spkmodem-recv is provided with Libreboot releases:
* https://libreboot.org/
*/
#define _POSIX_SOURCE
/*
* For OpenBSD define, to detect version
* for deciding whether to use pledge(2)
*/
#ifdef __OpenBSD__
#include <sys/param.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define SAMPLES_PER_FRAME 240
#define MAX_SAMPLES (2 * SAMPLES_PER_FRAME)
#define FREQ_SEP_MIN 5
#define FREQ_SEP_MAX 15
#define FREQ_DATA_MIN 15
#define FREQ_DATA_THRESHOLD 25
#define FREQ_DATA_MAX 60
#define THRESHOLD 500
#define READ_BUF 4096
struct decoder_state {
signed short frame[MAX_SAMPLES];
unsigned char pulse[MAX_SAMPLES];
signed short inbuf[READ_BUF];
size_t inpos;
size_t inlen;
int ringpos;
int sep_pos;
/*
* Sliding window pulse counters
* used to detect modem tones
*/
int freq_data;
int freq_separator;
int sample_count;
int ascii_bit;
unsigned char ascii;
int debug;
};
static const char *argv0;
static void handle_audio(struct decoder_state *st);
static int valid_signal(struct decoder_state *st);
static void decode_pulse(struct decoder_state *st);
static signed short read_sample(struct decoder_state *st);
static int set_ascii_bit(struct decoder_state *st);
static void print_char(struct decoder_state *st);
static void print_stats(struct decoder_state *st);
static void reset_char(struct decoder_state *st);
static void err(int errval, const char *msg, ...);
static void usage(void);
static const char *progname(void);
int getopt(int, char * const *, const char *);
extern char *optarg;
extern int optind;
extern int opterr;
extern int optopt;
#ifndef errno
extern int errno;
#endif
int
main(int argc, char **argv)
{
struct decoder_state st;
int c;
#if defined (__OpenBSD__) && defined(OpenBSD)
#if OpenBSD >= 509
if (pledge("stdio", NULL) == -1)
err(errno, "pledge");
#endif
#endif
memset(&st, 0, sizeof(st));
st.ascii_bit = 7;
st.ringpos = 0;
st.sep_pos = SAMPLES_PER_FRAME;
argv0 = argv[0];
while ((c = getopt(argc, argv, "d")) != -1) {
if (c != 'd')
usage();
st.debug = 1;
break;
}
setvbuf(stdout, NULL, _IONBF, 0);
for (;;)
handle_audio(&st);
return EXIT_SUCCESS;
}
static void
handle_audio(struct decoder_state *st)
{
int sample;
if (st->sample_count > (3 * SAMPLES_PER_FRAME))
reset_char(st);
if (!valid_signal(st)) {
decode_pulse(st);
return;
}
if (set_ascii_bit(st) < 0)
print_char(st);
st->sample_count = 0;
for (sample = 0; sample < SAMPLES_PER_FRAME; sample++)
decode_pulse(st);
}
static int
valid_signal(struct decoder_state *st)
{
return (st->freq_separator > FREQ_SEP_MIN &&
st->freq_separator < FREQ_SEP_MAX &&
st->freq_data > FREQ_DATA_MIN &&
st->freq_data < FREQ_DATA_MAX);
}
static void
decode_pulse(struct decoder_state *st)
{
unsigned char old_ring, old_sep;
unsigned char new_pulse;
old_ring = st->pulse[st->ringpos];
old_sep = st->pulse[st->sep_pos];
st->freq_data -= old_ring;
st->freq_data += old_sep;
st->freq_separator -= old_sep;
st->frame[st->ringpos] = read_sample(st);
if ((unsigned)(st->frame[st->ringpos] + THRESHOLD)
> (unsigned)(2 * THRESHOLD))
new_pulse = 1;
else
new_pulse = 0;
st->pulse[st->ringpos] = new_pulse;
st->freq_separator += new_pulse;
st->ringpos++;
if (st->ringpos >= MAX_SAMPLES)
st->ringpos = 0;
st->sep_pos++;
if (st->sep_pos >= MAX_SAMPLES)
st->sep_pos = 0;
st->sample_count++;
}
static signed short
read_sample(struct decoder_state *st)
{
size_t n;
while (st->inpos >= st->inlen) {
n = fread(st->inbuf, sizeof(st->inbuf[0]),
READ_BUF, stdin);
if (n == 0) {
if (feof(stdin))
exit(EXIT_SUCCESS);
err(errno, "stdin read");
}
st->inpos = 0;
st->inlen = n;
}
return st->inbuf[st->inpos++];
}
static int
set_ascii_bit(struct decoder_state *st)
{
if (st->debug)
print_stats(st);
if (st->freq_data < FREQ_DATA_THRESHOLD)
st->ascii |= (1 << st->ascii_bit);
st->ascii_bit--;
return st->ascii_bit;
}
static void
print_char(struct decoder_state *st)
{
if (st->debug)
printf("<%c,%x>", st->ascii, st->ascii);
else
putchar(st->ascii);
reset_char(st);
}
static void
print_stats(struct decoder_state *st)
{
long pos;
if ((pos = ftell(stdin)) == -1) {
printf("%d %d %d\n",
st->freq_data,
st->freq_separator,
FREQ_DATA_THRESHOLD);
return;
}
printf("%d %d %d @%ld\n",
st->freq_data,
st->freq_separator,
FREQ_DATA_THRESHOLD,
pos - sizeof(st->frame));
}
static void
reset_char(struct decoder_state *st)
{
st->ascii = 0;
st->ascii_bit = 7;
}
static void
err(int errval, const char *msg, ...)
{
va_list ap;
fprintf(stderr, "%s: ", progname());
va_start(ap, msg);
vfprintf(stderr, msg, ap);
va_end(ap);
fprintf(stderr, ": %s\n", strerror(errval));
exit(EXIT_FAILURE);
}
static void
usage(void)
{
fprintf(stderr, "usage: %s [-d]\n", progname());
exit(EXIT_FAILURE);
}
static const char *
progname(void)
{
const char *p;
if (argv0 == NULL || *argv0 == '\0')
return "";
p = strrchr(argv0, '/');
if (p)
return p + 1;
else
return argv0;
}
|