diff --git a/drivers/serial/pty.c b/drivers/serial/pty.c index 48c224d140e..b6c06806f1a 100644 --- a/drivers/serial/pty.c +++ b/drivers/serial/pty.c @@ -40,6 +40,7 @@ #include #include +#include #include #include #include @@ -348,19 +349,47 @@ static int pty_ioctl(FAR struct file *filep, int cmd, unsigned long arg) { FAR struct inode *inode; FAR struct pty_dev_s *dev; + FAR struct pty_devpair_s *devpair; int ret; DEBUGASSERT(filep != NULL && filep->f_inode != NULL); - inode = filep->f_inode; - dev = inode->i_private; - DEBUGASSERT(dev != NULL); + inode = filep->f_inode; + dev = inode->i_private; + DEBUGASSERT(dev != NULL && dev->pd_devpair != NULL); + devpair = dev->pd_devpair; /* Handle IOCTL commands */ switch (cmd) { /* PTY IOCTL commands would be handled here */ - /* There aren't any yet */ + + case TIOCGPTN: /* Get Pty Number (of pty-mux device): FAR int* */ + { +#ifdef CONFIG_DISABLE_PSEUDOFS_OPERATIONS + ret = -ENOSYS; +#else + FAR int *ptyno = (FAR int *)((uintptr_t)arg); + if (ptyno == NULL) + { + ret = -EINVAL; + } + else + { + *ptyno = (int)devpair->pp_minor; + ret = OK; + } +#endif + } + break; + + case TIOCSPTLCK: /* Lock/unlock Pty: int */ + ret = -ENOSYS; /* Not implemented */ + break; + + case TIOCGPTLCK: /* Get Pty lock state: FAR int* */ + ret = -ENOSYS; /* Not implemented */ + break; /* Any unrecognized IOCTL commands will be passed to the contained * pipe driver. diff --git a/include/nuttx/serial/tioctl.h b/include/nuttx/serial/tioctl.h index 2044195e502..e4c01690edb 100644 --- a/include/nuttx/serial/tioctl.h +++ b/include/nuttx/serial/tioctl.h @@ -146,7 +146,7 @@ /* Marking a line as local */ -#define TIOCGSOFTCAR _TIOC(0x0023) /* Get software carrier flag: FAR int */ +#define TIOCGSOFTCAR _TIOC(0x0023) /* Get software carrier flag: FAR int* */ #define TIOCSSOFTCAR _TIOC(0x0024) /* Set software carrier flag: FAR const int */ /* Get/set serial line info */ @@ -160,10 +160,16 @@ #define TIOCMIWAIT _TIOC(0x0028) /* Wait for a change on serial input line(s): void */ #define TIOCGICOUNT _TIOC(0x0029) /* Read serial port interrupt count: FAR struct serial_icounter_struct */ +/* Pseudo-terminals */ + +#define TIOCGPTN _TIOC(0x002a) /* Get Pty Number (of pty-mux device): FAR int* */ +#define TIOCSPTLCK _TIOC(0x002b) /* Lock/unlock Pty: int */ +#define TIOCGPTLCK _TIOC(0x002c) /* Get Pty lock state: FAR int* */ + /* RS-485 Support */ -#define TIOCSRS485 _TIOC(0x002a) /* Set RS485 mode, arg: pointer to struct serial_rs485 */ -#define TIOCGRS485 _TIOC(0x002b) /* Get RS485 mode, arg: pointer to struct serial_rs485 */ +#define TIOCSRS485 _TIOC(0x002d) /* Set RS485 mode, arg: pointer to struct serial_rs485 */ +#define TIOCGRS485 _TIOC(0x002e) /* Get RS485 mode, arg: pointer to struct serial_rs485 */ /* Definitions for flags used in struct serial_rs485 (Linux compatible) */ @@ -174,14 +180,14 @@ /* Single-wire UART support */ -#define TIOCSSINGLEWIRE _TIOC(0x002c) /* Set single-wire mode */ -#define TIOCGSINGLEWIRE _TIOC(0x002d) /* Get single-wire mode */ +#define TIOCSSINGLEWIRE _TIOC(0x002f) /* Set single-wire mode */ +#define TIOCGSINGLEWIRE _TIOC(0x0030) /* Get single-wire mode */ # define SER_SINGLEWIRE_ENABLED (1 << 0) /* Enable/disable single-wire support */ /* Debugging */ -#define TIOCSERGSTRUCT _TIOC(0x002e) /* Get device TTY structure */ +#define TIOCSERGSTRUCT _TIOC(0x0031) /* Get device TTY structure */ /******************************************************************************************** * Public Type Definitions diff --git a/include/stdlib.h b/include/stdlib.h index aa259c9d50c..25d4e36c408 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -207,6 +207,18 @@ struct mallinfo mallinfo(void); int mallinfo(FAR struct mallinfo *info); #endif +/* Pseudo-Terminals */ + +#ifdef CONFIG_PSEUDOTERM_SUSV1 +FAR char *ptsname(int fd); +int ptsname_r(int fd, FAR char *buf, size_t buflen); + +#if 0 /* Not implemented */ +int grantpt(int fd); +int unlockpt(int fd); +#endif +#endif + /* Arithmetic */ int abs(int j); diff --git a/libc/stdlib/Make.defs b/libc/stdlib/Make.defs index 68eb75e68b4..460fb1dd8f9 100644 --- a/libc/stdlib/Make.defs +++ b/libc/stdlib/Make.defs @@ -45,6 +45,10 @@ ifeq ($(CONFIG_FS_WRITABLE),y) CSRCS += lib_mktemp.c lib_mkstemp.c endif +ifeq ($(CONFIG_PSEUDOTERM_SUSV1),y) +CSRCS += lib_ptsname.c lib_ptsnamer.c +endif + # Add the stdlib directory to the build DEPPATH += --dep-path stdlib diff --git a/libc/stdlib/lib_ptsname.c b/libc/stdlib/lib_ptsname.c new file mode 100644 index 00000000000..04cd6460dd5 --- /dev/null +++ b/libc/stdlib/lib_ptsname.c @@ -0,0 +1,74 @@ +/**************************************************************************** + * libc/stdlib/lib_ptsname.c + * + * Copyright (C) 2016 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#ifdef CONFIG_PSEUDOTERM_SUSV1 + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: ptsname + * + * Description: + * The ptsname() function returns the name of the slave pseudoterminal + * device corresponding to the master referred to by fd. + * + * Returned Values: + * On success, ptsname() returns a pointer to a string in static storage + * which will be overwritten by subsequent calls. This pointer must not + * be freed. On failure, NULL is returned. + * + * ENOTTY fd does not refer to a pseudoterminal master device. + * + ****************************************************************************/ + +FAR char *ptsname(int fd) +{ + static char devname[16]; + int ret = ptsname_r(fd, devname, 16); + return ret < 0 ? NULL : devname; +} + +#endif /* CONFIG_PSEUDOTERM_SUSV1 */ diff --git a/libc/stdlib/lib_ptsnamer.c b/libc/stdlib/lib_ptsnamer.c new file mode 100644 index 00000000000..e5df9c0ec7e --- /dev/null +++ b/libc/stdlib/lib_ptsnamer.c @@ -0,0 +1,95 @@ +/**************************************************************************** + * libc/stdlib/lib_ptsnamer.c + * + * Copyright (C) 2016 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include + +#ifdef CONFIG_PSEUDOTERM_SUSV1 + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: ptsname_r + * + * Description: + * The ptsname_r() function is the reentrant equivalent of ptsname(). + * It returns the name of the slave pseudoterminal device as a null- + * terminated string in the buffer pointed to by buf. The buflen + * argument specifies the number of bytes available in buf. + * + * Returned Values: + * On success, ptsname_r() returns 0. On failure, a nonzero value is + * returned and errno is set to indicate the error. + * + * EINVAL (ptsname_r() only) buf is NULL. + * ENOTTY fd does not refer to a pseudoterminal master device. + * ERANGE (ptsname_r() only) buf is too small. + * + ****************************************************************************/ + +int ptsname_r(int fd, FAR char *buf, size_t buflen) +{ + int ptyno; + int ret; + + DEBUGASSERT(buf != NULL); + + /* Get the slave PTY number */ + + ret = ioctl(fd, TIOCGPTN, (unsigned long)((uintptr_t)&ptyno)); + if (ret < 0) + { + return ret; + } + + /* Create the device name. This current does not handler EINVAL or ERANGE + * error detection. + */ + + snprintf(buf, buflen, "/dev/pts/%d", ptyno); + return OK; +} + +#endif /* CONFIG_PSEUDOTERM_SUSV1 */