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
|
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "modem.h"
int
faxsend(Modem *m, int argc, char *argv[])
{
int c, count, r, flow;
char buf[128];
verbose("faxsend");
if((r = initfaxmodem(m)) != Eok)
return r;
/* telco just does the dialing */
r = response(m, 120);
switch(r){
case Rok:
break;
default:
r = seterror(m, Enoanswer);
return r;
}
xonoff(m, 1);
verbose("sending");
m->pageno = 1;
while(argc--){
if(m->pageno != 1)
sleep(1000); /* let the paper catch up */
m->valid &= ~(Vfhng|Vfet|Vfpts|Vftsi|Vfdcs);
if((r = openfaxfile(m, *argv)) != Eok)
return r;
verbose("sending geometry");
sprint(buf, "AT+FDT=%ld,%ld,%ld,%ld", m->df, m->vr, m->wd, m->ln);
if(command(m, buf) != Eok)
goto buggery;
if(response(m, 20) != Rconnect){
r = seterror(m, Eincompatible);
goto buggery;
}
/*
* Write the data, stuffing DLE's.
* After each bufferfull check if the remote
* sent us anything, e.g. CAN to abort.
* This also flushes out the ^S/^Q characters
* which the driver insists on sending us.
* (Could fix the driver, of course...).
*/
verbose("sending data");
for(;;){
flow = 0;
count = 0;
c = 0;
while(count < sizeof(buf)-1){
if((c = Bgetc(m->bp)) < 0)
break;
buf[count++] = c;
if(c == '\020')
buf[count++] = c;
}
verbose("sending %d bytes", count);
if(count && write(m->fd, buf, count) < 0){
verbose("write failed: %r");
r = seterror(m, Esys);
goto buggery;
}
/*
* this does really rough flow control since the
* avanstar is even worse
*/
verbose("flow control");
while((r = rawmchar(m, buf)) == Eok || flow){
if(r != Eok){
if(flow-- == 0)
break;
sleep(250);
continue;
}
switch(buf[0]){
case '\030':
verbose("%c", buf[0]);
if(write(m->fd, "\020\003", 2) < 0){
r = seterror(m, Esys);
goto buggery;
}
goto okexit;
case '\021':
flow = 0;
break;
case '\023':
flow = 4;
break;
case '\n':
break;
default:
verbose("%c", buf[0]);
r = seterror(m, Eproto);
goto buggery;
}
}
if(c < 0)
break;
}
/*
* End of page, send DLE+ETX,
* get OK in response.
*/
verbose("sending end of page");
if(write(m->fd, "\020\003", 2) < 0){
r = seterror(m, Esys);
goto buggery;
}
verbose("waiting for OK");
if(response(m, 120) != Rok){
r = seterror(m, Enoresponse);
goto buggery;
}
/*
* Did you hear me? - IT'S THE END OF THE PAGE.
* Argument is 0 if more pages to follow.
* Should get back an FPTS with an indication
* as to whether the page was successfully
* transmitted or not.
*/
sprint(buf, "AT+FET=%d", argc == 0? 2: 0);
if(command(m, buf) != Eok)
goto buggery;
switch(response(m, 20)){
case Rok:
break;
case Rhangup:
if(m->fhng == 0 && argc == 0)
break;
r = seterror(m, Eproto);
goto buggery;
default:
r = seterror(m, Enoresponse);
goto buggery;
}
if((m->valid & Vfpts) == 0 || m->fpts[0] != 1){
r = seterror(m, Eproto);
goto buggery;
}
Bterm(m->bp);
m->pageno++;
argv++;
}
okexit:
xonoff(m, 0);
return Eok;
buggery:
xonoff(m, 0);
Bterm(m->bp);
command(m, "AT+FK");
response(m, 5);
return r;
}
|