forked from rene0/dcf77pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.c
824 lines (771 loc) · 17.9 KB
/
input.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
// Copyright 2013-2020 René Ladan and Udo Klein and "JsBergbau"
// SPDX-License-Identifier: BSD-2-Clause
#include "input.h"
#include "json_object.h"
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <time.h>
#include <unistd.h>
#if defined(__FreeBSD__)
# include <sys/param.h>
# if __FreeBSD_version >= 900022
# include <sys/gpio.h>
# include <sys/ioccom.h>
# else
# define NOLIVE 1
# endif
#elif defined(__NetBSD__)
# warning NetBSD, GPIO support not yet implemented
# define NOLIVE 1
#elif defined(__linux__)
/* NOP */
#elif defined(__APPLE__) && (defined(__OSX__) || defined(__MACH__))
# warning MacOS, GPIO support available but no port for Rapberry Pi
# define NOLIVE 1
# define MACOS 1
#elif defined(__CYGWIN__)
# warning Cygwin, GPIO support not yet implemented
# define NOLIVE 1
#elif defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
# error Use Cygwin to use this software on Windows
#else
# error Unsupported operating system, please send a patch to the author
#endif
/** maximum number of bits in a minute */
#define BUFLEN 60
static int bitpos; /* second */
static unsigned dec_bp; /* bitpos decrease in file mode */
static int buffer[BUFLEN]; /* wrap after BUFLEN positions */
static FILE *logfile; /* auto-appended in live mode */
static int fd; /* gpio file */
static struct hardware hw;
static struct bitinfo bit;
static unsigned acc_minlen;
static int cutoff;
static struct GB_result gb_res;
static unsigned filemode = 0; /* 0 = no file, 1 = input, 2 = output */
int
set_mode_file(const char * const infilename)
{
if (filemode == 1) {
fprintf(stderr, "Already initialized to live mode.\n");
cleanup();
return -1;
}
if (infilename == NULL) {
fprintf(stderr, "infilename is NULL\n");
return -1;
}
logfile = fopen(infilename, "r");
if (logfile == NULL) {
perror("fopen(logfile)");
return errno;
}
filemode = 2;
return 0;
}
int
set_mode_live(struct json_object *config)
{
#if defined(NOLIVE)
fprintf(stderr,
"No GPIO interface available, disabling live decoding\n");
cleanup();
return -1;
#else
#if defined(__FreeBSD__)
struct gpio_pin pin;
#endif
char buf[64];
struct json_object *value;
int res;
if (filemode == 2) {
fprintf(stderr, "Already initialized to file mode.\n");
cleanup();
return -1;
}
/* fill hardware structure and initialize hardware */
if (json_object_object_get_ex(config, "pin", &value)) {
hw.pin = (unsigned)json_object_get_int(value);
} else {
fprintf(stderr, "Key 'pin' not found\n");
cleanup();
return EX_DATAERR;
}
if (json_object_object_get_ex(config, "activehigh", &value)) {
hw.active_high = (bool)json_object_get_boolean(value);
} else {
fprintf(stderr, "Key 'activehigh' not found\n");
cleanup();
return EX_DATAERR;
}
if (json_object_object_get_ex(config, "freq", &value)) {
hw.freq = (unsigned)json_object_get_int(value);
} else {
fprintf(stderr, "Key 'freq' not found\n");
cleanup();
return EX_DATAERR;
}
if (hw.freq < 10 || hw.freq > 155000 || (hw.freq & 1) == 1) {
fprintf(stderr, "hw.freq must be an even number between 10 and"
"155000 inclusive\n");
cleanup();
return EX_DATAERR;
}
bit.signal = malloc(hw.freq / 2);
#if defined(__FreeBSD__)
if (json_object_object_get_ex(config, "iodev", &value)) {
hw.iodev = (unsigned)json_object_get_int(value);
} else {
fprintf(stderr, "Key 'iodev' not found\n");
cleanup();
return EX_DATAERR;
}
res = snprintf(buf, sizeof(buf), "/dev/gpioc%u", hw.iodev);
if (res < 0 || res >= sizeof(buf)) {
fprintf(stderr, "hw.iodev too high? (%i)\n", res);
cleanup();
return EX_DATAERR;
}
fd = open(buf, O_RDWR);
if (fd < 0) {
fprintf(stderr, "open %s: ", buf);
perror(NULL);
cleanup();
return errno;
}
pin.gp_pin = hw.pin;
pin.gp_flags = GPIO_PIN_INPUT;
if (ioctl(fd, GPIOSETCONFIG, &pin) < 0) {
perror("ioctl(GPIOSETCONFIG)");
cleanup();
return errno;
}
#elif defined(__linux__)
fd = open("/sys/class/gpio/export", O_WRONLY);
if (fd < 0) {
perror("open(/sys/class/gpio/export)");
cleanup();
return errno;
}
res = snprintf(buf, sizeof(buf), "%u", hw.pin);
if (res < 0 || res >= sizeof(buf)) {
fprintf(stderr, "hw.pin too high? (%i)\n", res);
cleanup();
return EX_DATAERR;
}
if (write(fd, buf, res) < 0) {
if (errno != EBUSY) {
perror("write(export)");
cleanup();
return errno; /* EBUSY -> pin already exported ? */
}
}
if (close(fd) == -1) {
perror("close(export)");
cleanup();
return errno;
}
res = snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%u/direction",
hw.pin);
if (res < 0 || res >= sizeof(buf)) {
fprintf(stderr, "hw.pin too high? (%i)\n", res);
cleanup();
return EX_DATAERR;
}
fd = open(buf, O_RDWR);
if (fd < 0) {
perror("open(direction)");
cleanup();
return errno;
}
if (write(fd, "in", 3) < 0) {
perror("write(in)");
cleanup();
return errno;
}
if (close(fd) == -1) {
perror("close(direction)");
cleanup();
return errno;
}
res = snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%u/value",
hw.pin);
if (res < 0 || res >= sizeof(buf)) {
fprintf(stderr, "hw.pin too high? (%i)\n", res);
cleanup();
return EX_DATAERR;
}
fd = open(buf, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
perror("open(value)");
cleanup();
return errno;
}
#endif
filemode = 1;
return 0;
#endif
}
void
cleanup(void)
{
if (fd > 0 && close(fd) == -1) {
#if defined(__FreeBSD__)
perror("close(/dev/gpioc*)");
#elif defined(__linux__)
perror("close(/sys/class/gpio/*)");
#endif
}
fd = 0;
if (logfile != NULL) {
if (fclose(logfile) == EOF) {
perror("fclose(logfile)");
} else {
logfile = NULL;
}
}
free(bit.signal);
}
int
get_pulse(void)
{
int tmpch;
#if defined(NOLIVE)
tmpch = 2;
#else
int count = 0;
#if defined(__FreeBSD__)
struct gpio_req req;
req.gp_pin = hw.pin;
count = ioctl(fd, GPIOGET, &req);
tmpch = (req.gp_value == GPIO_PIN_HIGH) ? 1 : 0;
if (count < 0) {
#elif defined(__linux__)
count = read(fd, &tmpch, 1);
tmpch -= '0';
if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
return 2; /* rewind to prevent EBUSY/no read failed */
if (count != 1) {
#endif
return 2; /* hardware failure? */
}
if (!hw.active_high) {
tmpch = 1 - tmpch;
}
#endif
return tmpch;
}
/*
* Clear the cutoff value and the state values, except emark_toolong and
* emark_late to be able to determine if this flag can be cleared again.
*/
static void
set_new_state(void)
{
if (!gb_res.skip) {
cutoff = -1;
}
gb_res.bad_io = false;
gb_res.bitval = ebv_none;
if (gb_res.marker != emark_toolong && gb_res.marker != emark_late) {
gb_res.marker = emark_none;
}
gb_res.hwstat = ehw_ok;
gb_res.done = false;
gb_res.skip = false;
}
static void
reset_frequency(void)
{
if (logfile != NULL) {
fprintf(logfile, "%s",
bit.realfreq <= hw.freq * 500000 ? "<" :
bit.realfreq > hw.freq * 1000000 ? ">" : "");
}
bit.realfreq = hw.freq * 1000000;
bit.freq_reset = true;
}
static void
reset_bitlen(void)
{
if (logfile != NULL) {
fprintf(logfile, "!");
}
bit.bit0 = bit.realfreq / 10;
bit.bit20 = bit.realfreq / 5;
bit.bitlen_reset = true;
}
/*
* The bits are decoded from the signal using an exponential low-pass filter
* in conjunction with a Schmitt trigger. The idea and the initial
* implementation for this come from Udo Klein, with permission.
* http://blog.blinkenlight.net/experiments/dcf77/binary-clock/#comment-5916
*/
struct GB_result
get_bit_live(void)
{
char outch = '?';
bool adj_freq = true;
bool newminute = false;
unsigned stv = 1;
struct timespec slp;
#if !defined(MACOS)
struct timespec tp0, tp1;
#endif
unsigned sec2;
long long a, y = 1000000000;
static int init_bit = 2;
bool is_eom = gb_res.marker == emark_minute ||
gb_res.marker == emark_late;
bit.freq_reset = false;
bit.bitlen_reset = false;
set_new_state();
/*
* One period is either 1000 ms or 2000 ms long (normal or padding for
* last). The active part is either 100 ms ('0') or 200 ms ('1') long.
* The maximum allowed values as percentage of the second length are
* specified as half the value and the whole value of the lengths of
* bit 0 and bit 20 respectively.
*
* ~A > 3/2 * realfreq: end-of-minute
* ~A > 5/2 * realfreq: timeout
*/
if (init_bit == 2) {
bit.realfreq = hw.freq * 1000000;
bit.bit0 = bit.realfreq / 10;
bit.bit20 = bit.realfreq / 5;
}
sec2 = 1000000000 / (hw.freq * hw.freq);
/* Set up filter, reach 50% after hw.freq/20 samples (i.e. 50 ms) */
a = 1000000000 - (long long)(1000000000 * exp2(-20.0 / hw.freq));
bit.tlow = -1;
bit.tlast0 = -1;
for (bit.t = 0; bit.t < hw.freq * 2; bit.t++) {
#if !defined(MACOS)
(void)clock_gettime(CLOCK_MONOTONIC, &tp0);
#endif
int p = get_pulse();
if (p == 2) {
gb_res.bad_io = true;
outch = '*';
break;
}
if (bit.signal != NULL) {
if ((bit.t & 7) == 0) {
bit.signal[bit.t / 8] = 0;
}
/* clear data from previous second */
bit.signal[bit.t / 8] |=
p << (unsigned char)(bit.t & 7);
}
if (y >= 0 && y < a / 2) {
bit.tlast0 = (int)bit.t;
}
y += a * (p * 1000000000 - y) / 1000000000;
/*
* Prevent algorithm collapse during thunderstorms or
* scheduler abuse
*/
if (bit.realfreq <= hw.freq * 500000 ||
bit.realfreq > hw.freq * 1000000) {
reset_frequency();
adj_freq = false;
}
if (bit.t > bit.realfreq * 2500000) {
if (bit.tlow <= hw.freq / 20) {
gb_res.hwstat = ehw_receive;
outch = 'r';
} else if (bit.tlow * 100 / bit.t >= 99) {
gb_res.hwstat = ehw_transmit;
outch = 'x';
} else {
gb_res.hwstat = ehw_random;
outch = '#';
}
adj_freq = false;
break; /* timeout */
}
/*
* Schmitt trigger, maximize value to introduce hysteresis and
* to avoid infinite memory.
*/
if (y < 500000000 && stv == 1) {
/* end of high part of second */
y = 0;
stv = 0;
bit.tlow = (int)bit.t;
}
if (y > 500000000 && stv == 0) {
/* end of low part of second */
newminute = bit.t * 2000000 > bit.realfreq * 3;
if (init_bit == 2) {
init_bit--;
}
if (newminute) {
/*
* Reset the frequency and the EOM flag if two
* consecutive EOM markers come in, which means
* something is wrong.
*/
if (is_eom) {
if (gb_res.marker == emark_minute) {
gb_res.marker = emark_none;
} else if (gb_res.marker ==
emark_late) {
gb_res.marker = emark_toolong;
}
reset_frequency();
adj_freq = false;
} else {
if (gb_res.marker == emark_none) {
gb_res.marker = emark_minute;
} else if (gb_res.marker ==
emark_toolong) {
gb_res.marker = emark_late;
}
}
}
break; /* start of new second */
}
long long twait = (long long)(sec2 * bit.realfreq / 1000000);
#if !defined(MACOS)
(void)clock_gettime(CLOCK_MONOTONIC, &tp1);
twait = twait - (tp1.tv_sec - tp0.tv_sec) *
1000000000 - (tp1.tv_nsec - tp0.tv_nsec);
#endif
slp.tv_sec = twait / 1000000000;
slp.tv_nsec = twait % 1000000000;
while (twait > 0 && nanosleep(&slp, &slp) > 0)
; /* empty loop */
}
if (bit.t >= hw.freq * 2) {
/* this can actually happen */
if (gb_res.hwstat == ehw_ok) {
gb_res.hwstat = ehw_random;
outch = '#';
}
reset_frequency();
adj_freq = false;
}
if (!gb_res.bad_io && gb_res.hwstat == ehw_ok) {
if (2 * bit.realfreq * bit.tlow * (1 + (newminute ? 1 : 0)) <
(bit.bit0 + bit.bit20) * bit.t) {
/* zero bit, ~100 ms active signal */
gb_res.bitval = ebv_0;
outch = '0';
buffer[bitpos] = 0;
} else if (bit.realfreq * bit.tlow * (1 + (newminute ? 1 : 0)) <
(bit.bit0 + bit.bit20) * bit.t) {
/* one bit, ~200 ms active signal */
gb_res.bitval = ebv_1;
outch = '1';
buffer[bitpos] = 1;
} else {
/* bad radio signal, retain old value */
gb_res.bitval = ebv_none;
outch = '_';
adj_freq = false;
}
}
if (!gb_res.bad_io) {
if (init_bit == 1) {
init_bit--;
} else if (gb_res.hwstat == ehw_ok &&
gb_res.marker == emark_none) {
unsigned long long avg;
if (bitpos == 0 && gb_res.bitval == ebv_0) {
bit.bit0 +=
((long long)(bit.tlow * 1000000 -
bit.bit0) / 2);
}
if (bitpos == 20 && gb_res.bitval == ebv_1) {
bit.bit20 +=
((long long)(bit.tlow * 1000000 -
bit.bit20) / 2);
}
/* Force sane values during e.g. a thunderstorm */
if (2 * bit.bit20 < bit.bit0 * 3 ||
bit.bit20 > bit.bit0 * 3) {
reset_bitlen();
adj_freq = false;
}
avg = (bit.bit20 - bit.bit0) / 2;
if (bit.bit0 + avg < bit.realfreq / 10 ||
bit.bit0 - avg > bit.realfreq / 10) {
reset_bitlen();
adj_freq = false;
}
if (bit.bit20 + avg < bit.realfreq / 5 ||
bit.bit20 - avg > bit.realfreq / 5) {
reset_bitlen();
adj_freq = false;
}
}
}
if (adj_freq) {
if (newminute) {
bit.realfreq +=
((long long)(bit.t * 500000 - bit.realfreq) / 20);
} else {
bit.realfreq +=
((long long)(bit.t * 1000000 - bit.realfreq) / 20);
}
}
acc_minlen += 1000000 * bit.t / (bit.realfreq / 1000);
if (logfile != NULL) {
fprintf(logfile, "%c", outch);
if (gb_res.marker == emark_minute ||
gb_res.marker == emark_late) {
fprintf(logfile, "a%uc%6.4f\n", acc_minlen,
(double)((bit.t * 1e6) / bit.realfreq));
}
}
if (gb_res.marker == emark_minute || gb_res.marker == emark_late) {
cutoff = bit.t * 1000000 / (bit.realfreq / 10000);
}
return gb_res;
}
/* Skip over invalid characters */
static int
skip_invalid(void)
{
int inch = EOF;
do {
int oldinch = inch;
if (feof(logfile)) {
break;
}
inch = getc(logfile);
/*
* \r\n is implicitly converted because \r is invalid character
* \n\r is implicitly converted because \n is found first
* \n is OK
* convert \r to \n
*/
if (oldinch == '\r' && inch != '\n') {
ungetc(inch, logfile);
inch = '\n';
}
} while (strchr("01\nxr#*_ac", inch) == NULL);
return inch;
}
struct GB_result
get_bit_file(void)
{
static int oldinch;
static bool read_acc_minlen;
int inch;
char co[6];
set_new_state();
inch = skip_invalid();
/*
* bit.t is set to fake value for compatibility with old log files not
* storing acc_minlen values or to increase time when mainloop() splits
* too long minutes.
*/
switch (inch) {
case EOF:
gb_res.done = true;
return gb_res;
case '0':
case '1':
buffer[bitpos] = inch - (int)'0';
gb_res.bitval = (inch == (int)'0') ? ebv_0 : ebv_1;
bit.t = 1000;
break;
case '\n':
/*
* Skip multiple consecutive EOM markers, these are made
* impossible by the reset_minlen() invocation in
* get_bit_live()
*/
gb_res.skip = true;
if (oldinch != '\n') {
bit.t = read_acc_minlen ? 0 : 1000;
read_acc_minlen = false;
/*
* The marker checks must be inside the oldinch
* check to prevent spurious emin_short errors.
*/
if (gb_res.marker == emark_none) {
gb_res.marker = emark_minute;
} else if (gb_res.marker == emark_toolong) {
gb_res.marker = emark_late;
}
} else {
bit.t = 0;
}
break;
case 'x':
gb_res.hwstat = ehw_transmit;
bit.t = 2500;
break;
case 'r':
gb_res.hwstat = ehw_receive;
bit.t = 2500;
break;
case '#':
gb_res.hwstat = ehw_random;
bit.t = 2500;
break;
case '*':
gb_res.bad_io = true;
bit.t = 0;
break;
case '_':
/* retain old value in buffer[bitpos] */
gb_res.bitval = ebv_none;
bit.t = 1000;
break;
case 'a':
/* acc_minlen, up to 2^32-1 ms */
gb_res.skip = true;
bit.t = 0;
if (fscanf(logfile, "%10u", &acc_minlen) != 1) {
gb_res.done = true;
}
read_acc_minlen = !gb_res.done;
break;
case 'c':
/* cutoff for newminute */
gb_res.skip = true;
bit.t = 0;
if (fscanf(logfile, "%6c", co) != 1) {
gb_res.done = true;
}
if (!gb_res.done && (co[1] == '.')) {
cutoff = (co[0] - '0') * 10000 +
(int)strtol(co + 2, NULL, 10);
}
break;
default:
break;
}
if (!read_acc_minlen) {
acc_minlen += bit.t;
}
/*
* Read-ahead 1 character to check if a minute marker is coming. This
* prevents emark_toolong or emark_late being set 1 bit early.
*/
oldinch = inch;
inch = skip_invalid();
if (!feof(logfile)) {
if (dec_bp == 0 && bitpos > 0 && oldinch != '\n' &&
(inch == '\n' || inch == 'a' || inch == 'c')) {
dec_bp = 1;
}
} else {
gb_res.done = true;
}
ungetc(inch, logfile);
return gb_res;
}
bool
is_space_bit(int bitpos)
{
return (bitpos == 1 || bitpos == 15 || bitpos == 16 ||
bitpos == 19 || bitpos == 20 || bitpos == 21 || bitpos == 28 ||
bitpos == 29 || bitpos == 35 || bitpos == 36 || bitpos == 42 ||
bitpos == 45 || bitpos == 50 || bitpos == 58 || bitpos == 59 ||
bitpos == 60);
}
struct GB_result
next_bit(void)
{
if (dec_bp == 1) {
bitpos--;
dec_bp = 2;
}
if (gb_res.marker == emark_minute || gb_res.marker == emark_late) {
bitpos = 0;
dec_bp = 0;
} else if (!gb_res.skip) {
bitpos++;
}
if (bitpos == BUFLEN) {
gb_res.marker = emark_toolong;
bitpos = 0;
return gb_res;
}
if (gb_res.marker == emark_toolong) {
gb_res.marker = emark_none; /* fits again */
}
else if (gb_res.marker == emark_late) {
gb_res.marker = emark_minute; /* cannot happen? */
}
return gb_res;
}
int
get_bitpos(void)
{
return bitpos;
}
const int * const
get_buffer(void)
{
return buffer;
}
struct hardware
get_hardware_parameters(void)
{
return hw;
}
void
*flush_logfile(/*@unused@*/ void *arg)
{
for (;;)
{
fflush(logfile);
sleep(60);
}
}
int
append_logfile(const char * const logfilename)
{
pthread_t flush_thread;
if (logfilename == NULL) {
fprintf(stderr, "logfilename is NULL\n");
return -1;
}
logfile = fopen(logfilename, "a");
if (logfile == NULL) {
return errno;
}
fprintf(logfile, "\n--new log--\n\n");
return pthread_create(&flush_thread, NULL, flush_logfile, NULL);
}
int
close_logfile(void)
{
int f;
f = fclose(logfile);
return (f == EOF) ? errno : 0;
}
struct bitinfo
get_bitinfo(void)
{
return bit;
}
unsigned
get_acc_minlen(void)
{
return acc_minlen;
}
void
reset_acc_minlen(void)
{
acc_minlen = 0;
}
int
get_cutoff(void)
{
return cutoff;
}