aboutsummaryrefslogtreecommitdiff
path: root/lib/serial/builtin/unix_dev.c
blob: e165e22a1fedc5cc705a7cee676fe233e8dc5d2c (plain)
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
#include "../../internals.h"

#ifndef LIBTIO_DISABLED_UNIX
# include <errno.h>
# include <string.h>
# include <dirent.h>
# include <unistd.h>

/* `get_path_max()`: get the maximum path size. */

TIO_LOCAL(size_t) get_path_max(void)
{
	ssize_t path_max;

#ifdef PATH_MAX
	path_max = PATH_MAX;
#else
	path_max = pathconf("/", _PC_PATH_MAX);
	if (path_max <= 0)
		path_max = 4096;
#endif
	++path_max;

	return ((size_t)path_max);
}

/* `all_numbers()`: check if a string is entirely composed of numbers. */

# define CHECK_NUM(CAR) \
	{ \
		int c = (CAR); \
		\
		if (c < '0' || c > '9') \
			return (0); \
	}

TIO_LOCAL(int) all_numbers(char const *s, size_t size)
{
	if (size)
		for (; size--; s++)
			CHECK_NUM(*s)
	else
		for (; *s; s++)
			CHECK_NUM(*s)

	return (1);
}

/* ---
 * Cookie and callbacks.
 * --- */

/* The cookie used for /dev iterating. */

TIO_STRUCT(cookie, cookie_t)

struct cookie {
	char *path;
	DIR *dp;

	char *start;
};

/* `next_serial_port()`: get the next serial port using the directory
 * listing.
 *
 * The serial lines are represented:
 * - on MacOS/OS X: "cu.*" (e.g. "cu.usbmodem621").
 * - on FreeBSD: "cuadX" (e.g. "cuad0").
 * - on Linux: "ttySX" (e.g. "ttyS0"). */

# define IS_SERIAL_DEVICE(DEVNAME) \
	(!strncmp((DEVNAME), "cu.", 3) || \
	(!strncmp((DEVNAME), "cuad", 4) && all_numbers(&(DEVNAME)[4], 0)) || \
	(!strncmp((DEVNAME), "ttyS", 4) && all_numbers(&(DEVNAME)[4], 0)))

TIO_HOOK(int) next_serial_port(cookie_t *cookie, char const **ptr)
{
	struct dirent *dr;

	while (1) {
		/* Get the next entry. */

		if (!(dr = readdir(cookie->dp)))
			return (tio_error_iter);

		/* Check the name. */

		if (!IS_SERIAL_DEVICE(dr->d_name))
			continue;

		/* We have found a serial port!
		 * Just copy it into our buffer and return it. */

		strcpy(cookie->start, dr->d_name);
		break;
	}

	*ptr = cookie->path;
	return (0);
}

/* `end_iter()`: end the iterator. */

TIO_HOOK(void) end_iter(cookie_t *cookie)
{
	closedir(cookie->dp);
	tio_free(cookie);
}

/* `dev_cu_functions`: the callbacks structure. */

TIO_LOCAL_DATA(tio_iter_functions_t const) dev_cu_functions = {
	(tio_next_t *)&next_serial_port,
	NULL,
	(tio_end_t *)&end_iter
};

/* ---
 * Open the iterator.
 * --- */

/* `tio_list_unix_serial_ports_using_cu_devices()`: list serial ports
 * using the `/dev` directory, by looking for `cu.*` devices. */

TIO_EXTERN(int) tio_list_unix_serial_ports_using_cu_devices(tio_iter_t **iterp)
{
	cookie_t *cookie;
	size_t path_max = get_path_max();

	/* Create the cookie. */

	if (!(cookie = tio_alloc(1, sizeof(cookie) + path_max)))
		return (tio_error_alloc);

	cookie->path = (char *)(void *)&cookie[1];

	/* Prepare the path and open the directory stream. */

	strcpy(cookie->path, "/dev/");
	cookie->start = strchr(cookie->path, '\0');

	if (!(cookie->dp = opendir(cookie->path))) {
		tio_free(cookie);

		msg((ll_error, "unknown error %d: %s", errno, strerror(errno)));
		return (tio_error_unknown);
	}

	/* Create the iterator. */

	return (tio_iter(iterp, cookie, &dev_cu_functions));
}

#endif