summaryrefslogtreecommitdiff
path: root/sys/src/cmd/fax/modem.c
blob: d0decc3e7bc6bf4679e2d12ac91f044aaf3ee05f (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <u.h>
#include <libc.h>
#include <bio.h>

#include "modem.h"

typedef struct {
	char	*terse;
	char	*verbose;
	int	result;
	int	(*f)(Modem*);
} ResultCode;

static ResultCode results[] = {
	{ "0",	"OK",		Rok,		0, },
	{ "1",	"CONNECT",	Rconnect,	0, },
	{ "2",	"RING",		Rring,		0, },
	{ "3",	"NO CARRIER", 	Rfailure,	0, },
	{ "4",	"ERROR",	Rrerror,	0, },
	{ "5",	"CONNECT 1200",	Rconnect,	0, },
	{ "6",	"NO DIALTONE",	Rfailure,	0, },
	{ "7",	"BUSY",		Rfailure,	0, },
	{ "8",	"NO ANSWER",	Rfailure,	0, },
	{ "9",	"CONNECT 2400",	Rconnect,	0, },		/* MT1432BA */
	{ "10",	"CONNECT 2400",	Rconnect,	0, },		/* Hayes */
	{ "11",	"CONNECT 4800",	Rconnect,	0, },
	{ "12",	"CONNECT 9600",	Rconnect,	0, },
	{ "13",	"CONNECT 14400",Rconnect,	0, },
	{ "23",	"CONNECT 1275",	Rconnect,	0, },		/* MT1432BA */

	{ "-1",	"+FCON",	Rcontinue,	fcon, },
	{ "-1",	"+FTSI",	Rcontinue,	ftsi, },
	{ "-1",	"+FDCS",	Rcontinue,	fdcs, },
	{ "-1",	"+FCFR",	Rcontinue,	fcfr, },
	{ "-1",	"+FPTS",	Rcontinue,	fpts, },
	{ "-1",	"+FET",		Rcontinue,	fet, },
	{ "-1",	"+FHNG",	Rcontinue,	fhng, },

	{ 0 },
};

void
initmodem(Modem *m, int fd, int cfd, char *type, char *id)
{
	m->fd = fd;
	m->cfd = cfd;
	if(id == 0)
		id = "Plan 9";
	m->id = id;
	m->t = type;
}

int
rawmchar(Modem *m, char *p)
{
	Dir *d;
	int n;

	if(m->icount == 0)
		m->iptr = m->ibuf;

	if(m->icount){
		*p = *m->iptr++;
		m->icount--;
		return Eok;
	}

	m->iptr = m->ibuf;

	if((d = dirfstat(m->fd)) == nil){
		verbose("rawmchar: dirfstat: %r");
		return seterror(m, Esys);
	}
	n = d->length;
	free(d);
	if(n == 0)
		return Enoresponse;

	if(n > sizeof(m->ibuf)-1)
		n = sizeof(m->ibuf)-1;
	if((m->icount = read(m->fd, m->ibuf, n)) <= 0){
		verbose("rawmchar: read: %r");
		m->icount = 0;
		return seterror(m, Esys);
	}
	*p = *m->iptr++;
	m->icount--;

	return Eok;
}

int
getmchar(Modem *m, char *buf, long timeout)
{
	int r, t;

	timeout += time(0);
	while((t = time(0)) <= timeout){
		switch(r = rawmchar(m, buf)){

		case Eok:
			return Eok;

		case Enoresponse:
			sleep(100);
			continue;

		default:
			return r;
		}
	}
	verbose("getmchar: time %ud, timeout %ud", t, timeout);

	return seterror(m, Enoresponse);
}

int
putmchar(Modem *m, char *p)
{
	if(write(m->fd, p, 1) < 0)
		return seterror(m, Esys);
	return Eok;
}

/*
 *  lines terminate with cr-lf
 */
static int
getmline(Modem *m, char *buf, int len, long timeout)
{
	int r, t;
	char *e = buf+len-1;
	char last = 0;

	timeout += time(0);
	while((t = time(0)) <= timeout){
		switch(r = rawmchar(m, buf)){

		case Eok:
			/* ignore ^s ^q which are used for flow */
			if(*buf == '\021' || *buf == '\023')
				continue;
			if(*buf == '\n'){
				/* ignore nl if its not with a cr */
				if(last == '\r'){
					*buf = 0;
					return Eok;
				}
				continue;
			}
			last = *buf;
			if(*buf == '\r')
				continue;
			buf++;
			if(buf == e){
				*buf = 0;
				return Eok;
			}
			continue;

		case Enoresponse:
			sleep(100);
			continue;

		default:
			return r;
		}
	}
	verbose("getmline: time %ud, timeout %ud", t, timeout);

	return seterror(m, Enoresponse);
}

int
command(Modem *m, char *s)
{
	verbose("m->: %s", s);
	if(fprint(m->fd, "%s\r", s) < 0)
		return seterror(m, Esys);
	return Eok;
}

/*
 * Read till we see a message or we time out.
 * BUG: line lengths not checked;
 *	newlines
 */
int
response(Modem *m, int timeout)
{
	int r;
	ResultCode *rp;

	while(getmline(m, m->response, sizeof(m->response), timeout) == Eok){
		if(m->response[0] == 0)
			continue;
		verbose("<-m: %s", m->response);
		for(rp = results; rp->terse; rp++){
			if(strncmp(rp->verbose, m->response, strlen(rp->verbose)))
				continue;
			r = rp->result;
			if(rp->f && (r = (*rp->f)(m)) == Rcontinue)
				break;
			return r;
		}
	}

	m->response[0] = 0;
	return Rnoise;
}

void
xonoff(Modem *m, int i)
{
	char buf[8];

	sprint(buf, "x%d", i);
	i = strlen(buf);
	write(m->cfd, buf, i);
}