File: PTC.cpp

package info (click to toggle)
hamfax 0.5.1-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 416 kB
  • ctags: 441
  • sloc: cpp: 3,188; sh: 197; makefile: 124
file content (186 lines) | stat: -rw-r--r-- 4,210 bytes parent folder | download
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// HamFax -- an application for sending and receiving amateur radio facsimiles
// Copyright (C) 2001 Christof Schmitt, DH1CS <cschmitt@users.sourceforge.net>
//  
// This program is free software; 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 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.

#include "PTC.hpp"
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <qtimer.h>
#include "Error.hpp"

PTC::PTC(QObject* parent) 
	: QObject(parent), deviceName("/dev/ttyS0"),
	  device(-1), fm(true), speed(38400), deviation(0), notifier(0)
{
}

PTC::~PTC(void)
{
	if(device!=-1) {
           	write(device,"\377",1); // return from fax mode
		::close(device);
	}
}

void PTC::setDeviceName(const QString& s)
{
	deviceName=s;
}

void PTC::open(void)
{
	try {
		device=::open(deviceName,O_RDWR|O_NOCTTY|O_NONBLOCK);
		if(device==-1) {
			throw Error("could not open serial device");
		}
		
		struct termios options;
		tcgetattr(device,&options);
		switch(speed) {
		case 38400:
			cfsetispeed(&options,B38400);
			cfsetospeed(&options,B38400);
			break;
		case 57600:
			cfsetispeed(&options,B57600);
			cfsetospeed(&options,B57600);
			break;
		case 115200:
			cfsetispeed(&options,B115200);
			cfsetospeed(&options,B115200);
			break;
		}
		options.c_cflag|=CRTSCTS;
		options.c_cc[VMIN]=0;
		options.c_cc[VTIME]=0;
		cfmakeraw(&options);
		if(tcsetattr(device,TCSAFLUSH,&options)==-1) {
			throw Error("could not set serial parameters");
		}
		write(device,"\r\r",2);
		QString s=QString("FAX MBAUD %1\r").arg(speed);
		write(device,s,s.length());
		s=QString("FAX DEVIATION %1\r").arg(deviation);
		write(device,s,s.length());
		s=QString("FAX %1\r").arg(fm ? "JVCOMM :HamFax FM":"AMFAX");
		write(device,s,s.length());
		usleep(300000);
		tcflush(device,TCIOFLUSH);
	} catch(Error) {
		close();
		throw;
	}
}

void PTC::startInput(void)
{
	open();
	notifier=new QSocketNotifier(device,QSocketNotifier::Read);
	connect(notifier,SIGNAL(activated(int)),this,SLOT(read(int)));
	emit newSampleRate(speed/10);
}

void PTC::startOutput(void)
{
	open();
	notifier=new QSocketNotifier(device,QSocketNotifier::Write);
	connect(notifier,SIGNAL(activated(int)),this,SLOT(checkSpace(int)));
	emit newSampleRate(speed/20);
}

void PTC::end(void)
{
	if(device!=-1) {
		notifier->setEnabled(false);
		if(notifier->type()==QSocketNotifier::Read) {
			disconnect(notifier,SIGNAL(activated(int)),
				   this,SLOT(read(int)));
			close();
		} else {
			disconnect(notifier,SIGNAL(activated(int)),
				   this,SLOT(checkSpace(int)));
			int i;
			ioctl(device,TIOCOUTQ,&i);
			QTimer::singleShot(1000*(i+8000)*20/speed,
					   this,SLOT(close()));
		}
		delete notifier;
	}
}

void PTC::close(void)
{
	if(device!=-1) {
		write(device,"\377",1); // return from fax mode
		::close(device);
		device=-1;
		emit deviceClosed();
	}
}

void PTC::transmit(double* samples, int count)
{
	try {
		notifier->setEnabled(false);
		unsigned char buf[count];
		for(int i=0; i<count; i++) {
			buf[i]=static_cast<unsigned char>
				(samples[i]*(fm?240.0:63.0));
		}
		tcflush(device,TCIFLUSH);
		if(write(device,buf,count)!=count) {
			throw Error();
		}
		notifier->setEnabled(true);
	} catch(Error) {
		close();
	}
}

void PTC::setDeviation(int dev)
{
	deviation=dev;
}

void PTC::setFM(bool fm)
{
	this->fm=fm;
}

void PTC::setSpeed(int s)
{
	speed=s;
}

void PTC::read(int fd)
{
	int n=512;
	unsigned char charbuffer[n];
	n=::read(device,charbuffer,n);
	int intbuffer[n];
	for(int i=0; i<n; i++) {
		intbuffer[i]=charbuffer[i];
	}
	emit data(intbuffer,n);
}

void PTC::checkSpace(int fd)
{
	emit spaceLeft(512);
}