-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.c
161 lines (134 loc) · 3.03 KB
/
serial.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
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include "serial.h"
#include "string_set.h"
#define SERIAL_DEVICE_DIRECTORY "/dev"
#define SERIAL_DEVICE_REGEX "^cu\\.(.+)$"
#define SERIAL_DEVICE_FORMAT SERIAL_DEVICE_DIRECTORY "/cu.%s"
bool
serial_port_open(struct serial_port *sport, const char *name)
{
struct termios control;
char path[MAXPATHLEN];
int error;
int fd;
sport->sp_fd = -1;
snprintf(path, sizeof path, SERIAL_DEVICE_FORMAT, name);
fd = open(path, O_RDWR | /*O_NONBLOCK | */O_NOCTTY);
if (fd == -1)
return (false);
sport->sp_fd = fd;
error = ioctl(sport->sp_fd, TIOCEXCL);
if (error != 0) {
close(sport->sp_fd);
sport->sp_fd = -1;
return (false);
}
error = tcgetattr(sport->sp_fd, &control);
if (error != 0) {
close(sport->sp_fd);
sport->sp_fd = -1;
return (false);
}
cfmakeraw(&control);
error = cfsetspeed(&control, B9600);
if (error != 0) {
close(sport->sp_fd);
sport->sp_fd = -1;
return (false);
}
control.c_cflag &= ~PARENB;
control.c_cflag &= ~CSTOPB;
control.c_cflag &= ~CSIZE;
control.c_cflag |= CS8;
control.c_cflag |= CLOCAL;
control.c_iflag |= IXON | IXOFF;
control.c_cc[VMIN] = 1;
control.c_cc[VTIME] = 0;
error = tcsetattr(sport->sp_fd, TCSAFLUSH, &control);
if (error != 0) {
close(sport->sp_fd);
sport->sp_fd = -1;
return (false);
}
return (true);
}
bool
serial_port_read(struct serial_port *sport, char *buf, size_t len)
{
ssize_t rv;
if (sport->sp_fd == -1)
return (false);
rv = read(sport->sp_fd, buf, len);
if (rv == -1)
return (false);
if (len != (size_t)rv)
return (serial_port_read(sport, buf + rv, len - rv));
return (true);
}
bool
serial_port_write(struct serial_port *sport, const char *buf, size_t len)
{
ssize_t rv;
if (sport->sp_fd == -1)
return (false);
rv = write(sport->sp_fd, buf, len);
if (rv == -1)
return (false);
if (len != (size_t)rv)
return (false);
return (true);
}
struct string_set *
serial_port_enumerate(void)
{
struct string_set *ss;
struct dirent *de;
regex_t regex;
int error;
DIR *dir;
error = regcomp(®ex, SERIAL_DEVICE_REGEX, REG_EXTENDED);
if (error != 0)
return (NULL);
dir = opendir(SERIAL_DEVICE_DIRECTORY);
if (dir == NULL) {
regfree(®ex);
return (NULL);
}
ss = string_set_create();
if (ss == NULL) {
regfree(®ex);
closedir(dir);
return (NULL);
}
while ((de = readdir(dir)) != NULL) {
regmatch_t rm[2];
error = regexec(®ex, de->d_name, sizeof rm / sizeof rm[0],
rm, 0);
if (error == REG_NOMATCH)
continue;
if (error != 0) {
abort(); /* XXX Be more useful than that. */
continue;
}
de->d_name[rm[1].rm_eo] = '\0';
string_set_add(ss, de->d_name + rm[1].rm_so);
}
/*
* Note that we return an empty string set if there were no appropriate
* devices found, while we return NULL if there was an error.
*/
regfree(®ex);
closedir(dir);
return (ss);
}