| 1 |
/* |
| 2 |
* cwdaemon - morse sounding daemon for the parallel or serial port |
| 3 |
* Copyright (C) 2002 - 2005 Joop Stakenborg <[email protected]> |
| 4 |
* and many authors, see the AUTHORS file. |
| 5 |
* Copyright (C) 2012 - 2015 Kamil Ignacak <[email protected]> |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Library General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 20 |
* 02110-1301, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#define _BSD_SOURCE /* S_IFMT and related. */ |
| 24 |
|
| 25 |
#include "config.h" |
| 26 |
|
| 27 |
# if HAVE_STDIO_H |
| 28 |
# include <stdio.h> |
| 29 |
#endif |
| 30 |
#if STDC_HEADERS |
| 31 |
# include <stdlib.h> |
| 32 |
# include <stddef.h> |
| 33 |
#else |
| 34 |
# if HAVE_STDLIB_H |
| 35 |
# include <stdlib.h> |
| 36 |
# endif |
| 37 |
#endif |
| 38 |
#if HAVE_UNISTD_H |
| 39 |
# include <unistd.h> |
| 40 |
#endif |
| 41 |
#if HAVE_SYS_IOCTL_H |
| 42 |
# include <sys/ioctl.h> |
| 43 |
#endif |
| 44 |
#if HAVE_FCNTL_H |
| 45 |
# include <fcntl.h> |
| 46 |
#endif |
| 47 |
#if HAVE_TERMIOS_H |
| 48 |
# include <termios.h> |
| 49 |
#endif |
| 50 |
#if HAVE_SYS_STAT_H |
| 51 |
# include <sys/stat.h> |
| 52 |
#endif |
| 53 |
#if (defined(__unix__) || defined(unix)) && !defined(USG) |
| 54 |
# include <sys/param.h> |
| 55 |
#endif |
| 56 |
|
| 57 |
#include "cwdaemon.h" |
| 58 |
|
| 59 |
/** |
| 60 |
\file ttys.c |
| 61 |
|
| 62 |
Serial port functions. |
| 63 |
*/ |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
\brief Get fd for serial port |
| 68 |
|
| 69 |
Check to see whether \p fname is a tty type character device |
| 70 |
capable of TIOCM*. |
| 71 |
This should be platform independent. |
| 72 |
|
| 73 |
\return -1 if the device isn't a suitable tty device |
| 74 |
\return a file descriptor if the device is a suitable tty device |
| 75 |
*/ |
| 76 |
int dev_get_tty(const char *fname) |
| 77 |
{ |
| 78 |
char nm[MAXPATHLEN]; |
| 79 |
struct stat st; |
| 80 |
int fd, m; |
| 81 |
|
| 82 |
m = snprintf(nm, sizeof(nm), "/dev/%s", fname); |
| 83 |
if (m >= sizeof(nm)) |
| 84 |
return (-1); |
| 85 |
if ((fd = open(nm, O_RDWR | O_NOCTTY | O_NONBLOCK)) == -1) |
| 86 |
return (-1); |
| 87 |
if (fstat(fd, &st) == -1) |
| 88 |
goto out; |
| 89 |
if ((st.st_mode & S_IFMT) != S_IFCHR) |
| 90 |
goto out; |
| 91 |
if (ioctl(fd, TIOCMGET, &m) == -1) |
| 92 |
goto out; |
| 93 |
return (fd); |
| 94 |
out: |
| 95 |
close(fd); |
| 96 |
return (-1); |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
int |
| 101 |
ttys_init (cwdevice * dev, int fd) |
| 102 |
{ |
| 103 |
|
| 104 |
dev->fd = fd; |
| 105 |
dev->reset (dev); |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
int |
| 110 |
ttys_free (cwdevice * dev) |
| 111 |
{ |
| 112 |
dev->reset (dev); |
| 113 |
close (dev->fd); |
| 114 |
return 0; |
| 115 |
} |
| 116 |
|
| 117 |
int |
| 118 |
ttys_reset (cwdevice * dev) |
| 119 |
{ |
| 120 |
ttys_cw (dev, OFF); |
| 121 |
ttys_ptt (dev, OFF); |
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
/* CW keying - bit1 (pin 4 for DB-9) */ |
| 127 |
int |
| 128 |
ttys_cw (cwdevice * dev, int onoff) |
| 129 |
{ |
| 130 |
int result, y = TIOCM_DTR; |
| 131 |
|
| 132 |
result = ioctl (dev->fd, onoff ? TIOCMBIS : TIOCMBIC, &y); |
| 133 |
if (result < 0) |
| 134 |
{ |
| 135 |
cwdaemon_errmsg("Ioctl serial port %s", dev->desc); |
| 136 |
exit (1); |
| 137 |
} |
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
/* SSB PTT keying - 0 bit (pin 7 for DB-9) */ |
| 142 |
int |
| 143 |
ttys_ptt (cwdevice * dev, int onoff) |
| 144 |
{ |
| 145 |
int result, y = TIOCM_RTS; |
| 146 |
|
| 147 |
result = ioctl (dev->fd, onoff ? TIOCMBIS : TIOCMBIC, &y); |
| 148 |
if (result < 0) |
| 149 |
{ |
| 150 |
cwdaemon_errmsg("Ioctl serial port %s", dev->desc); |
| 151 |
exit (1); |
| 152 |
} |
| 153 |
return 0; |
| 154 |
} |