Debian Codegraph @

faxd/Class2Ersatz.c++

raw
1 /* $Id$ */
2 /*
3 * Copyright (c) 1990-1996 Sam Leffler
4 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
5 * HylaFAX is a trademark of Silicon Graphics
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26 #include "Class2Ersatz.h"
27 #include "ModemConfig.h"
28
29 #include <stdlib.h>
30 #include <ctype.h>
31
32 Class2ErsatzModem::Class2ErsatzModem(FaxServer& s, const ModemConfig& c)
33 : Class2Modem(s,c)
34 {
35 serviceType = SERVICE_CLASS2;
36 setupDefault(classCmd, conf.class2Cmd, "AT+FCLASS=2");
37 setupDefault(mfrQueryCmd, conf.mfrQueryCmd, "AT+FMFR?");
38 setupDefault(modelQueryCmd, conf.modelQueryCmd, "AT+FMDL?");
39 setupDefault(revQueryCmd, conf.revQueryCmd, "AT+FREV?");
40 setupDefault(dccQueryCmd, conf.class2DCCQueryCmd, "AT+FDCC=?");
41 setupDefault(abortCmd, conf.class2AbortCmd, "AT+FK");
42
43 setupDefault(borCmd, conf.class2BORCmd, "AT+FBOR=0");
44 setupDefault(tbcCmd, conf.class2TBCCmd, "AT+FTBC=0");
45 setupDefault(crCmd, conf.class2CRCmd, "AT+FCR=1");
46 setupDefault(phctoCmd, conf.class2PHCTOCmd, "AT+FPHCTO=30");
47 setupDefault(bugCmd, conf.class2BUGCmd, "AT+FBUG=1");
48 setupDefault(lidCmd, conf.class2LIDCmd, "AT+FLID");
49 setupDefault(dccCmd, conf.class2DCCCmd, "AT+FDCC");
50 setupDefault(disCmd, conf.class2DISCmd, "AT+FDIS");
51 setupDefault(cigCmd, conf.class2CIGCmd, "AT+FCIG");
52 setupDefault(splCmd, conf.class2SPLCmd, "AT+FSPL");
53 setupDefault(ptsCmd, conf.class2PTSCmd, "AT+FPTS");
54 setupDefault(minspCmd, conf.class2MINSPCmd, "AT+FMINSP");
55
56 setupDefault(noFlowCmd, conf.class2NFLOCmd, "");
57 setupDefault(softFlowCmd, conf.class2SFLOCmd, "");
58 setupDefault(hardFlowCmd, conf.class2HFLOCmd, "");
59 }
60
61 Class2ErsatzModem::~Class2ErsatzModem()
62 {
63 }
64
65 ATResponse
66 Class2ErsatzModem::atResponse(char* buf, long ms)
67 {
68 if (FaxModem::atResponse(buf, ms) == AT_OTHER &&
69 (buf[0] == '+' && buf[1] == 'F')) {
70 if (strneq(buf, "+FHNG:", 6)) {
71 processHangup(buf+6);
72 lastResponse = AT_FHNG;
73 hadHangup = true;
74 } else if (strneq(buf, "+FCON", 5))
75 lastResponse = AT_FCON;
76 else if (strneq(buf, "+FPOLL", 6))
77 lastResponse = AT_FPOLL;
78 else if (strneq(buf, "+FDIS:", 6))
79 lastResponse = AT_FDIS;
80 else if (strneq(buf, "+FNSF:", 6))
81 lastResponse = AT_FNSF;
82 else if (strneq(buf, "+FCSI:", 6))
83 lastResponse = AT_FCSI;
84 else if (strneq(buf, "+FPTS:", 6))
85 lastResponse = AT_FPTS;
86 else if (strneq(buf, "+FDCS:", 6))
87 lastResponse = AT_FDCS;
88 else if (strneq(buf, "+FNSS:", 6))
89 lastResponse = AT_FNSS;
90 else if (strneq(buf, "+FTSI:", 6))
91 lastResponse = AT_FTSI;
92 else if (strneq(buf, "+FET:", 5))
93 lastResponse = AT_FET;
94 else if (strneq(buf, "+FPA:", 5))
95 lastResponse = AT_FPA;
96 else if (strneq(buf, "+FSA:", 5))
97 lastResponse = AT_FSA;
98 else if (strneq(buf, "+FPW:", 5))
99 lastResponse = AT_FPW;
100 }
101 return (lastResponse);
102 }
103
104 /*
105 * Handle the page-end protocol.
106 */
107 bool
108 Class2ErsatzModem::pageDone(u_int ppm, u_int& ppr)
109 {
110 ppr = 0; // something invalid
111 if (ppm == PPH_SKIP)
112 {
113 protoTrace("pageDone: PPH_SKIP");
114 ppr = PPR_MCF;
115 return true;
116 }
117
118
119 if (class2Cmd("AT+FET", ppm, AT_NOTHING)) {
120 for (;;) {
121 switch (atResponse(rbuf, conf.pageDoneTimeout)) {
122 case AT_FPTS:
123 if (sscanf(rbuf+6, "%u", &ppr) != 1) {
124 protoTrace("MODEM protocol botch (\"%s\"), %s",
125 rbuf, "can not parse PPR");
126 return (false); // force termination
127 }
128 /*
129 * This hack produces more problems than solves (latest ZyXELs
130 * seem not to have the bug described below). But if there is
131 * some delay between +FPTS and OK, and the next command is
132 * feeded before OK is returned, some modems (e.g. Xircom)
133 * will then return an error (+FHNG:2). Nonetheless I daren't
134 * remove this code completely :-) -- dbely
135 */
136 #if 0
137 /*
138 * (In some firmware revisions...) The ZyXEL modem
139 * appears to drop DCD when the remote side drops
140 * carrier, no matter whether DCD is configured to
141 * follow carrier or not. This results in a stream
142 * of empty lines, sometimes* followed by the requisite
143 * trailing OK. As a hack workaround to deal with
144 * the situation we accept the post page response if
145 * this is the last page that we're sending and the
146 * page is good (i.e. we would hang up immediately anyway).
147 */
148 if (ppm == PPM_EOP && ppr == PPR_MCF)
149 return (true);
150 #endif
151 break;
152 case AT_OK: // normal result code
153 case AT_ERROR: // possible if page retransmit
154 return (true);
155 case AT_FHNG:
156 waitFor(AT_OK);
157 /*
158 * Certain modems respond +FHNG:0 on the final page
159 * w/o providing post-page status when sending to
160 * broken fax machines. We interpret this to be an
161 * acknowledgement, even though it's really a violation
162 * of the spec.
163 */
164 if (ppm == PPM_EOP && ppr == 0 && isNormalHangup()) {
165 ppr = PPR_MCF;
166 return (true);
167 }
168 return (isNormalHangup());
169 case AT_EMPTYLINE:
170 case AT_TIMEOUT:
171 case AT_NOCARRIER:
172 case AT_NODIALTONE:
173 case AT_NOANSWER:
174 goto bad;
175 }
176 }
177 }
178 bad:
179 processHangup("50"); // Unspecified Phase D error
180 return (false);
181 }
182
183 /*
184 * Abort a data transfer in progress.
185 */
186 void
187 Class2ErsatzModem::abortDataTransfer()
188 {
189 protoTrace("SEND abort data transfer");
190 char c = CAN;
191 putModemData(&c, 1);
192 }
193
194 /*
195 * Send an end-of-transmission signal to the modem.
196 */
197 bool
198 Class2ErsatzModem::sendEOT()
199 {
200 static char EOT[] = { DLE, ETX };
201 return (putModemData(EOT, sizeof (EOT)));
202 }
203
204 /*
205 * Send a page of data using the ``stream interface''.
206 */
207 bool
208 Class2ErsatzModem::sendPage(TIFF* tif, u_int pageChop, bool cover)
209 {
210 protoTrace("SEND begin page");
211 if (flowControl == FLOW_XONXOFF)
212 setXONXOFF(FLOW_XONXOFF, FLOW_NONE, ACT_FLUSH);
213 bool rc = sendPageData(tif, pageChop, cover);
214 if (rc && conf.class2SendRTC)
215 rc = sendRTC(params);
216 if (rc)
217 rc = sendEOT();
218 else
219 abortDataTransfer();
220 if (flowControl == FLOW_XONXOFF)
221 setXONXOFF(getInputFlow(), FLOW_XONXOFF, ACT_DRAIN);
222 protoTrace("SEND end page");
223 return (rc ?
224 (waitFor(AT_OK, conf.pageDoneTimeout) && hangupCode[0] == '\0') : rc);
225 }