aboutsummaryrefslogtreecommitdiff
path: root/lib/serial/list.c
blob: 9f86bac8ed58c13d924734321cfd51066b4864ed (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
#include "../internals.h"

/* `spl_candidates`: candidates to serial ports listing.
 * `spl_last_successful_candidate`: last candidate which returned
 * a valid iterator. */

typedef TIO_EXTERN_TYPE(int) spl_func
	OF((tio_iter_t **));

TIO_LOCAL_DATA(spl_func *) spl_candidates[] = {
#ifndef LIBTIO_DISABLED_WINDOWS
	&tio_list_windows_serial_ports,
#endif

#ifndef LIBTIO_DISABLED_UNIX
	&tio_list_unix_serial_ports_using_dev_serial,
	&tio_list_unix_serial_ports_using_cu_devices,
#endif

	NULL
};

TIO_LOCAL_DATA(spl_func *) spl_last_successful_candidate = 0;

/* `tio_list_serial_ports()`: list the serial ports, in an agnostic way. */

TIO_EXTERN(int) tio_list_serial_ports(tio_iter_t **iterp)
{
	int err;

	if (spl_last_successful_candidate) {
		err = (*spl_last_successful_candidate)(iterp);
		if (err != tio_error_op)
			return (err);
	}

	{
		spl_func **func;

		for (func = spl_candidates; func; func++) {
			err = (**func)(iterp);
			if (err == tio_error_op)
				continue ;

			spl_last_successful_candidate = *func;
			return (err);
		}
	}

	return (tio_error_op);
}