Skip to content
This repository was archived by the owner on Oct 5, 2021. It is now read-only.

Commit 75a8bee

Browse files
jeplertormodvolden
authored andcommitted
serial_posix: be more accepting of device paths
On Linux, it is possible to refer to USB serial devices with durable paths such as /dev/serial/by-path/pci-0000:00:14.0-usb-0:1:1.0-port0 or /dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A700abCD-if00-port0. These paths are themselves symlinks to /dev/tty*. I have also found it convenient to place symlinks in my build directories, so that the file 'PORT' names the appropriate device for the project. This patch enables both usages, by first using the standard POSIX API realpath(3) to resolve symbolic links, and only then checks that the device name starts with /dev/tty. Signed-off-by: Jeff Epler <jepler@unpythonic.net>
1 parent ba7602d commit 75a8bee

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

serial_posix.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
#include <fcntl.h>
21+
#include <limits.h>
2122
#include <stdint.h>
2223
#include <stdio.h>
2324
#include <stdlib.h>
@@ -197,13 +198,27 @@ static port_err_t serial_setup(serial_t *h, const serial_baud_t baud,
197198
return PORT_ERR_OK;
198199
}
199200

201+
static int startswith(const char *haystack, const char *needle) {
202+
return strncmp(haystack, needle, strlen(needle)) == 0;
203+
}
204+
205+
static int is_tty(const char *path) {
206+
char resolved[PATH_MAX];
207+
208+
if(!realpath(path, resolved)) return 0;
209+
210+
if(startswith(resolved, "/dev/tty")) return 1;
211+
212+
return 0;
213+
}
214+
200215
static port_err_t serial_posix_open(struct port_interface *port,
201216
struct port_options *ops)
202217
{
203218
serial_t *h;
204219

205220
/* 1. check device name match */
206-
if (strncmp(ops->device, "/dev/tty", strlen("/dev/tty")))
221+
if (!is_tty(ops->device))
207222
return PORT_ERR_NODEV;
208223

209224
/* 2. check options */

0 commit comments

Comments
 (0)