mirror of
https://github.com/espressif/openthread.git
synced 2026-07-29 23:27:46 +00:00
[posix] add uart-exclusive option to enable flock / TIOCEXCL (#13015)
When uart-exclusive is specified as a radio URL parameter, the UART device is locked using flock(LOCK_EX) to prevent concurrent access, and TIOCEXCL is set where supported.
This commit is contained in:
@@ -49,6 +49,7 @@
|
||||
#endif
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/resource.h>
|
||||
#include <sys/stat.h>
|
||||
@@ -463,6 +464,26 @@ int HdlcInterface::OpenFile(const Url::Url &aRadioUrl)
|
||||
ExitNow();
|
||||
}
|
||||
|
||||
if (aRadioUrl.HasParam("uart-exclusive"))
|
||||
{
|
||||
// Lock the device early to prevent concurrent access
|
||||
if (flock(fd, LOCK_EX | LOCK_NB) == -1)
|
||||
{
|
||||
perror("flock uart failed, device already in use");
|
||||
close(fd);
|
||||
fd = -1;
|
||||
ExitNow();
|
||||
}
|
||||
|
||||
#ifdef TIOCEXCL
|
||||
// Set exclusive access mode if supported by the platform
|
||||
if (ioctl(fd, TIOCEXCL) == -1)
|
||||
{
|
||||
LogWarn("ioctl(TIOCEXCL) failed: %s", strerror(errno));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
if (isatty(fd))
|
||||
{
|
||||
struct termios tios;
|
||||
|
||||
@@ -85,6 +85,7 @@ const char *otSysGetRadioUrlHelpString(void)
|
||||
" uart-flow-control Enable flow control, disabled by default.\n" \
|
||||
" uart-init-deassert Deassert lines on init when flow control is disabled.\n" \
|
||||
" uart-reset Reset connection after hard resetting RCP(USB CDC ACM).\n" \
|
||||
" uart-exclusive Lock uart device using flock / TIOCEXCL.\n" \
|
||||
"\n"
|
||||
#else
|
||||
#define OT_SPINEL_HDLC_RADIO_URL_HELP_BUS
|
||||
|
||||
Reference in New Issue
Block a user