-
Notifications
You must be signed in to change notification settings - Fork 1
/
eia_linux.c
75 lines (52 loc) · 1.11 KB
/
eia_linux.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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <termios.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include "terminal.h"
#define TTY_DEVICE "/dev/ttyS2"
#define TTY_SPEED 115200
static int tty_fd;
static void exit_handler(void)
{
close(tty_fd);
}
void eia_init(void)
{
int result;
struct termios tio;
tty_fd = open(TTY_DEVICE, O_RDWR | O_NOCTTY);
if (tty_fd == -1) {
fprintf(stderr, "open() failed with errno: %d\n", errno);
exit(1);
}
atexit(exit_handler);
cfmakeraw(&tio);
cfsetospeed(&tio, B115200);
result = ioctl(tty_fd, TCSETS, &tio);
if (result == -1) {
fprintf(stderr, "ioctl() failed with errno: %d\n", errno);
exit(1);
}
}
void eia_send(uint8_t c)
{
fprintf(stderr, ">>> 0x%02x %c\n", c, isprint(c) ? c : ' ');
write(tty_fd, &c, 1);
}
void eia_update(void)
{
uint8_t c;
int result;
result = read(tty_fd, &c, 1);
if (result == 1) {
fprintf(stderr, "< 0x%02x %c\n", c, isprint(c) ? c : ' ');
terminal_handle_byte(c);
}
}