File: ttys.c

package info (click to toggle)
cwdaemon 0.9.4-6
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 852 kB
  • ctags: 174
  • sloc: sh: 3,638; ansic: 1,799; makefile: 103
file content (140 lines) | stat: -rw-r--r-- 2,946 bytes parent folder | download | duplicates (5)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * cwdaemon - morse sounding daemon for the parallel or serial port
 * Copyright (C) 2002 -2005 Joop Stakenborg <pg4i@amsat.org>
 *			 and many authors, see the AUTHORS file.
 *
 * This program is free oftware; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

# if HAVE_STDIO_H
# include <stdio.h>
#endif
#if STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# if HAVE_STDLIB_H
#  include <stdlib.h>
# endif
#endif
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
#if HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif
#if HAVE_FCNTL_H
# include <fcntl.h>
#endif
#if HAVE_TERMIOS_H
# include <termios.h>
#endif
#if HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#if (defined(__unix__) || defined(unix)) && !defined(USG)
# include <sys/param.h>
#endif

#include "cwdaemon.h"

/* serial port functions*/

/*
 * dev_is_tty(name): check to see whether 'name' is a tty type character
 *     device capable of TIOCM*.  Returns -1 if the device isn't a
 *     suitable tty device, and a file descriptor if it is.  This should
 *     be platform independent.
 */
int
dev_is_tty(const char *fname)
{
	char nm[MAXPATHLEN];
	struct stat st;
	int fd, m;

	m = snprintf(nm, sizeof(nm), "/dev/%s", fname);
	if (m >= sizeof(nm))
		return (-1);
	if ((fd = open(nm, O_RDWR | O_NOCTTY | O_NONBLOCK)) == -1)
		return (-1);
	if (fstat(fd, &st) == -1)
		goto out;
	if ((st.st_mode & S_IFMT) != S_IFCHR)
		goto out;
	if (ioctl(fd, TIOCMGET, &m) == -1)
		goto out;
	return (fd);
out:
	close(fd);
	return (-1);
	
}

int
ttys_init (cwdevice * dev, int fd)
{

	dev->fd = fd;
	dev->reset (dev);
	return 0;
}

int
ttys_free (cwdevice * dev)
{
	dev->reset (dev);
	close (dev->fd);
	return 0;
}

int
ttys_reset (cwdevice * dev)
{
	ttys_cw (dev, OFF);
	ttys_ptt (dev, OFF);
	return 0;
}


/* CW keying - bit1 (pin 4 for DB-9) */
int
ttys_cw (cwdevice * dev, int onoff)
{
	int result, y = TIOCM_DTR;

	result = ioctl (dev->fd, onoff ? TIOCMBIS : TIOCMBIC, &y);
	if (result < 0)
	{
		errmsg ("Ioctl serial port %s", dev->desc);
		exit (1);
	}
	return 0;
}

/* SSB PTT keying - 0 bit (pin 7 for DB-9) */
int
ttys_ptt (cwdevice * dev, int onoff)
{
	int result, y = TIOCM_RTS;

	result = ioctl (dev->fd, onoff ? TIOCMBIS : TIOCMBIC, &y);
	if (result < 0)
	{
		errmsg ("Ioctl serial port %s", dev->desc);
		exit (1);
	}
	return 0;
}