1 #ident "$Id: class1lib.c,v 4.23 2009/03/19 15:31:21 gert Exp $ Copyright (c) Gert Doering"
2
3 /* class1lib.c
4 *
5 * Low-level functions to handle class 1 fax --
6 * send a frame, receive a frame, dump frame to log file, ...
7 *
8 * $Log: class1lib.c,v $
9 * Revision 4.23 2009/03/19 15:31:21 gert
10 * add CVS tags
11 *
12 */
13
14 #ifdef CLASS1
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <signal.h>
21 #include <ctype.h>
22 #include <errno.h>
23
24 #include "mgetty.h"
25 #include "fax_lib.h"
26 #include "tio.h"
27 #include "class1.h"
28 #include "version.h"
29
30 /* static variables
31 *
32 * only set by functions in this module and used by other functions,
33 * but have to be module-global
34 */
35
36 #define F1LID 20
37 static char fax1_local_id[F1LID]; /* local system ID */
38 static int fax1_min = 2400, /* min/max speed */
39 fax1_max = 14400;
40 static int fax1_res; /* flag for normal resolution */
41
42 uch fax1_dis = 0x00; /* "X"-bit (last received DIS) */
43
44 static int fax1_fth, fax1_ftm, /* modem carrier capabilities */
45 fax1_frh, fax1_frm;
46
47 /* symbolic constants for capability check
48 */
49 #define V17 0xF00
50 #define V17_14400 0x800
51 #define V17_12000 0x400
52 #define V17_9600 0x200
53 #define V17_7200 0x100
54 #define V29 0x0F0
55 #define V29_9600 0x080
56 #define V29_7200 0x040
57 #define V27ter 0x00e
58 #define V27t_4800 0x008
59 #define V27t_2400 0x004
60 #define V21 0x001
61
62 /* table of baud rate / carrier number / DCS bits
63 */
64 struct fax1_btable fax1_btable[] = {
65 { 14400, V17_14400, 145, 146, 0x20 /* 0001 */ },
66 { 12000, V17_12000, 121, 122, 0x28 /* 0101 */ },
67 { 9600, V17_9600, 97, 98, 0x24 /* 1001 */ },
68 { 9600, V29_9600, 96, 96, 0x04 /* 1000 */ },
69 { 7200, V17_7200, 73, 74, 0x2c /* 1101 */ },
70 { 7200, V29_7200, 72, 72, 0x0c /* 1100 */ },
71 { 4800, V27t_4800, 48, 48, 0x08 /* 0100 */ },
72 { 2400, V27t_2400, 24, 24, 0x00 /* 0000 */ },
73 { 300, V21, 3, 3, 0 },
74 { -1, -1,0,0,0 }};
75
76 /* pointer to current modulation in fax1_btable
77 * (increment == fallback after FTT!)
78 */
79 struct fax1_btable * dcs_btp = fax1_btable;
80
81 /* table of bits-to-scan line time mappings, index = bits
82 */
83 struct fax1_st_table { int st; int ms_n; int ms_f; char * txt; };
84 struct fax1_st_table fax1_st_table[8] = {
85 /* ST, ms normal, ms fine bits */
86 { 5, 20, 20, " 20ms" }, /* 0 = 000 */
87 { 1, 5, 5, " 5ms" }, /* 1 = 100 */
88 { 3, 10, 10, " 10ms" }, /* 2 = 010 */
89 { 4, 20, 10, " 20/10ms" }, /* 3 = 110 */
90 { 7, 40, 40, " 40ms" }, /* 4 = 001 */
91 { 6, 40, 20, " 40/20ms" }, /* 5 = 101 */
92 { 2, 10, 5, " 10/5ms" }, /* 6 = 011 */
93 { 0, 0, 0, " 0ms" }}; /* 7 = 111 */
94
95 int fax1_set_l_id _P2( (fd, fax_id), int fd, char * fax_id )
96 {
97 int i,l;
98 char *p;
99
100 l = strlen( fax_id );
101 if ( l > F1LID ) { l = F1LID; }
102
103 /* bytes are transmitted in REVERSE order! */
104 p = &fax1_local_id[F1LID-1];
105
106 for ( i=0; i<l; i++ ) *(p--) = *(fax_id++);
107 for ( ; i<F1LID; i++ ) *(p--) = ' ';
108
109 return NOERROR;
110 }
111
112 static int fax1_carriers _P1((p), char * p )
113 {
114 int cbits = 0; /* carrier bits (V17_14400, ...) */
115 int cnr; /* carrier number (3,24,...) */
116 char * ep;
117 struct fax1_btable * btp; /* pointer to baud rate table */
118
119 while( *p )
120 {
121 cnr = strtol( p, &ep, 10 );
122
123 if ( *ep ) ep++; /* skip "," */
124 p = ep;
125
126 btp = fax1_btable;
127 while( btp->speed > 0 )
128 {
129 if ( cnr == btp->c_short || cnr == btp->c_long )
130 { cbits |= btp->flag; break; }
131 btp++;
132 }
133 }
134 return cbits;
135 }
136
137 /* step down max speed (at retrain time)
138 */
_P0(void)139 void fax1_reduce_max _P0(void)
140 {
141 if ( dcs_btp->speed > 2400 )
142 {
143 fax1_max = dcs_btp->speed - 2400;
144 lprintf( L_NOISE, "reduce max speed to %d", fax1_max );
145 }
146 }
147
148 /* set fine/normal resolution flags and min/max transmission speed
149 * including finding out maximum speed modem can do!
150 */
151 int fax1_set_fdcc _P4( (fd, fine, max, min),
152 int fd, int fine, int max, int min )
153 {
154 char * p;
155
156 lprintf( L_MESG, "fax1_set_fdcc: fine=%d, max=%d, min=%d", fine, max, min );
157
158 fax1_max = max;
159 fax1_min = min;
160 fax1_res = fine;
161
162 if ( fax1_min == 0 ) fax1_min=2400;
163
164 if ( fax1_max < fax1_min ||
165 fax1_max < 2400 || fax1_max > 14400 ||
166 fax1_min < 2400 || fax1_min > 14400 )
167 {
168 lprintf( L_WARN, "min/max values (%d/%d) out of range, use 2400/14400", min, max );
169 fax1_min = 2400; fax1_max = 14400;
170 }
171
172 if ( fax1_res < 0 || fax1_res > 1 )
173 {
174 lprintf( L_WARN, "fax1_res (%d) out of range, use fine (1)", fax1_res );
175 fax1_res = 1;
176 }
177
178 #if 0 /* we don't support anything but AT+FTH=3 yet */
179 p = mdm_get_idstring( "AT+FTH=?", 1, fd );
180 fax1_fth = fax1_carriers( p );
181 lprintf( L_MESG, "modem can send HDLC headers: %03x", fax1_fth );
182 #else
183 fax1_fth = 001;
184 #endif
185
186 p = mdm_get_idstring( "AT+FTM=?", 1, fd );
187 fax1_ftm = fax1_carriers( p );
188 lprintf( L_MESG, "modem can send page data: %03x", fax1_ftm );
189
190 #if 0 /* we don't support anything but AT+FRH=3 yet */
191 p = mdm_get_idstring( "AT+FRH=?", 1, fd );
192 fax1_frh = fax1_carriers( p );
193 lprintf( L_MESG, "modem can recv HDLC headers: %03x", fax1_fth );
194 #else
195 fax1_frh = 001;
196 #endif
197
198 p = mdm_get_idstring( "AT+FRM=?", 1, fd );
199 fax1_frm = fax1_carriers( p );
200 lprintf( L_MESG, "modem can recv page data: %03x", fax1_ftm );
201
202 return NOERROR;
203 }
204
205
206 /* timeout handler
207 */
208 static boolean fax1_got_timeout = FALSE;
209
fax1_sig_alarm(SIG_HDLR_ARGS)210 RETSIGTYPE fax1_sig_alarm(SIG_HDLR_ARGS)
211 {
212 signal( SIGALRM, fax1_sig_alarm );
213 lprintf( L_WARN, "Warning: fax1: got alarm signal!" );
214 fax1_got_timeout = TRUE;
215 }
216
217 /* receive ONE frame, put it into *framebuf
218 *
219 * timeout set to "timout * 1/10 seconds"
220 */
221 int fax1_receive_frame _P4 ( (fd, carrier, timeout, framebuf),
222 int fd, int carrier,
223 int timeout, uch * framebuf)
224 {
225 int count=0; /* bytes in frame */
226 int rc = NOERROR; /* return code */
227 char gotsync = FALSE; /* got 0xff frame sync */
228 char WasDLE = FALSE; /* got <DLE> character */
229 char * line, c;
230
231 if ( timeout > 0 )
232 {
233 signal( SIGALRM, fax1_sig_alarm );
234 alarm( (timeout/10)+1 );
235 }
236
237 if ( carrier > 0 )
238 {
239 char cmd[20];
240 sprintf( cmd, "AT+FRH=%d", carrier );
241
242 /*!!! TOOD: DO NOT USE fax_send (FAX_COMMAND_DELAY) */
243
244 /* normally, fax_send() can only fail if something is really
245 * messed up with the serial port, e.g. "stuck flow control"
246 */
247 if ( fax_send( cmd, fd ) == ERROR )
248 { alarm(0); return ERROR; }
249
250 /* wait for CONNECT/NO CARRIER */
251 line = mdm_get_line( fd );
252 if ( line != NULL && strcmp( line, cmd ) == 0 )
253 { line = mdm_get_line( fd ); } /* skip echo */
254
255 if ( line == NULL || strcmp( line, "CONNECT" ) != 0 )
256 {
257 alarm(0);
258 if ( line == NULL )
259 {
260 char cancel_str[] = { CAN };
261 lprintf( L_WARN, "fax1_receive_frame: no carrier (timeout), send CAN" );
262 write( fd, cancel_str, 1 );
263 alarm(2);
264 line = mdm_get_line( fd );
265 alarm(0);
266 }
267 else
268 lprintf( L_WARN, "fax1_receive_frame: no carrier (%s)", line );
269 return ERROR;
270 }
271 }
272
273 lprintf( L_NOISE, "fax1_receive_frame: got:" );
274
275 /* we have a CONNECT now - now find the first byte of the frame
276 * (0xFF), and read in <DLE> shielded data up to the <DLE><ETX>
277 */
278
279 while(1)
280 {
281 if ( mdm_read_byte( fd, &c ) != 1 )
282 {
283 lprintf( L_ERROR, "fax1_receive_frame: cannot read byte, return" );
284 rc = ERROR; break;
285 }
286
287 /*!!!! TODO: CONNECT/ERROR statt Frame-Daten erkennen */
288
289 lputc( L_NOISE, c );
290
291 if ( !gotsync ) /* wait for preamble */
292 {
293 if ( c == (char) 0xFF ) gotsync = TRUE;
294 continue;
295 }
296
297 /* got preamble, all further bytes are put into buffer */
298
299 /* enough room? */
300 if ( count >= FRAMESIZE-5 )
301 {
302 lprintf( L_ERROR, "fax1_receive_frame: too many octets in frame" );
303 rc = ERROR; break;
304 }
305
306 if ( WasDLE ) /* previous character was DLE */
307 {
308 if ( c == DLE ) /* DLE DLE -> DLE */
309 { framebuf[count++] = DLE; }
310 else if ( c == SUB ) /* DLE SUB -> DLE DLE */
311 { framebuf[count++] = DLE; framebuf[count++] = DLE; }
312 else if ( c == ETX ) /* end of frame detected */
313 { rc = count; break; }
314
315 WasDLE = 0;
316 continue;
317 }
318
319 /* previous character was not DLE, check for DLE now... */
320 if ( c == DLE )
321 {
322 WasDLE = 1; continue;
323 }
324
325 /* all other characters are stored in buffer */
326 framebuf[count++] = c;
327 }
328
329 /* now read OK / ERROR response codes (only if we're still happy) */
330 if ( rc != ERROR )
331 {
332 line = mdm_get_line( fd );
333
334 if ( line == NULL || /* timeout ... */
335 strcmp( line, "ERROR" ) == 0 ) /* or FCS error */
336 {
337 lprintf( L_MESG, "fax1_receive_frame: dropping frame" );
338 rc = ERROR;
339 }
340 }
341
342 /* turn off alarm */
343 alarm(0);
344
345 if ( rc > 0 )
346 {
347 fax1_dump_frame( '<', framebuf, count );
348 }
349
350 return rc;
351 }
352
353
354 void fax1_dump_frame _P3((io, frame, len),
355 char io, unsigned char * frame, int len)
356 {
357 int fcf = frame[1];
358
359 lprintf( L_MESG, "%c frame type: 0x%02x len: %d %s%s", io,
360 fcf, len, frame[0]&0x10? "final": "non-final",
361 (fcf & 0x0e) && ( fcf & 0x01 ) ? " X": "");
362
363 if ( fcf & 0x0e ) fcf &= ~0x01; /* clear "X" bit */
364
365 switch( fcf )
366 {
367 /* simple frames */
368 case T30_CSI:
369 lprintf( L_NOISE, "CSI: '%20.20s'", &frame[2] ); break;
370 case T30_CIG:
371 lprintf( L_NOISE, "CIG: '%20.20s'", &frame[2] ); break;
372 case T30_TSI:
373 lprintf( L_NOISE, "TSI: '%20.20s'", &frame[2] ); break;
374 case T30_NSF:
375 lprintf( L_NOISE, "NSF" ); break;
376 case T30_CFR:
377 lprintf( L_NOISE, "CFR" ); break;
378 case T30_FTT:
379 lprintf( L_NOISE, "FTT" ); break;
380 case T30_MCF:
381 lprintf( L_NOISE, "MCF" ); break;
382 case T30_RTP:
383 lprintf( L_NOISE, "RTP" ); break;
384 case T30_RTN:
385 lprintf( L_NOISE, "RTN" ); break;
386
387 case T30_EOM:
388 lprintf( L_NOISE, "EOM" ); break;
389 case T30_MPS:
390 lprintf( L_NOISE, "MPS" ); break;
391 case T30_EOP:
392 lprintf( L_NOISE, "EOP" ); break;
393
394 case T30_DCN:
395 lprintf( L_NOISE, "DCN" ); break;
396
397 /* complicated ones... */
398 case T30_DIS:
399 lprintf( L_NOISE, "DIS:" );
400 if ( frame[2] & 0x40 ) lputs( L_NOISE, " V8" );
401 lputs( L_NOISE, frame[2] & 0x80 ? " 64": " 256" );
402
403 if ( frame[3] & 0x01 ) lputs( L_NOISE, " +FPO" );
404 if ( frame[3] & 0x02 ) lputs( L_NOISE, " RCV" );
405
406 switch( (frame[3] >> 2) &0x0f )
407 {
408 case 0x00: lputs( L_NOISE, " V27ter_fb" ); break;
409 case 0x02: lputs( L_NOISE, " V27ter" ); break;
410 case 0x01: lputs( L_NOISE, " V29" ); break;
411 case 0x03: lputs( L_NOISE, " V27ter+V29" ); break;
412 case 0x0b: lputs( L_NOISE, " V27ter+V29+V17" ); break;
413 default: lputs( L_NOISE, " V.???" ); break;
414 }
415
416 if ( frame[3] & 0x40 ) lputs( L_NOISE, " 200" );
417 if ( frame[3] & 0x80 ) lputs( L_NOISE, " 2D" );
418
419 switch( frame[4] & 0x03 )
420 {
421 case 0x00: lputs( L_NOISE, " 215mm" ); break;
422 case 0x02: lputs( L_NOISE, " 215+255+303" ); break;
423 case 0x01: lputs( L_NOISE, " 215+255" ); break;
424 }
425 switch( (frame[4]>>2) & 0x03 )
426 {
427 case 0x00: lputs( L_NOISE, " A4" ); break;
428 case 0x02: lputs( L_NOISE, " unlim" ); break;
429 case 0x01: lputs( L_NOISE, " A4+B4" ); break;
430 }
431 lputs( L_NOISE, fax1_st_table[ (frame[4]>>4) & 0x07 ].txt );
432
433 if ( ( frame[4] & 0x80 ) == 0 ) break; /* extent bit */
434
435 if ( frame[5] & 0x04 ) lputs( L_NOISE, " ECM" );
436 if ( frame[5] & 0x40 ) lputs( L_NOISE, " T.6" );
437 if ( ( frame[5] & 0x80 ) == 0 ) break; /* extent bit */
438
439 if ( ( frame[6] & 0x80 ) == 0 ) break; /* extent bit */
440 /* the next bytes specify 300/400 dpi, color fax, ... */
441
442 break;
443 case T30_DCS:
444 lprintf( L_NOISE, "DCS:" );
445
446 if ( frame[2] & 0x40 ) lputs( L_NOISE, " V8" );
447 lputs( L_NOISE, frame[2] & 0x80 ? " 64": " 256" );
448
449 if ( frame[3] & 0x02 ) lputs( L_NOISE, " RCV!" );
450
451 switch( (frame[3] >> 2) &0x0f )
452 {
453 case 0x00: lputs( L_NOISE, " V27ter_2400" ); break;
454 case 0x02: lputs( L_NOISE, " V27ter_4800" ); break;
455 case 0x01: lputs( L_NOISE, " V29_9600" ); break;
456 case 0x03: lputs( L_NOISE, " V29_7200" ); break;
457 case 0x04: lputs( L_NOISE, " V33_14400" ); break;
458 case 0x06: lputs( L_NOISE, " V33_12000" ); break;
459 case 0x08: lputs( L_NOISE, " V17_14400" ); break;
460 case 0x0a: lputs( L_NOISE, " V17_12000" ); break;
461 case 0x09: lputs( L_NOISE, " V17_9600" ); break;
462 case 0x0b: lputs( L_NOISE, " V17_7200" ); break;
463 default: lputs( L_NOISE, " V.???" ); break;
464 }
465
466 if ( frame[3] & 0x40 ) lputs( L_NOISE, " 200!" );
467 if ( frame[3] & 0x80 ) lputs( L_NOISE, " 2D!" );
468
469 switch( frame[4] & 0x03 )
470 {
471 case 0x00: lputs( L_NOISE, " 1728/215mm" ); break;
472 case 0x02: lputs( L_NOISE, " 2432/303mm" ); break;
473 case 0x01: lputs( L_NOISE, " 2048/255mm" ); break;
474 }
475 switch( (frame[4]>>2) & 0x03 )
476 {
477 case 0x00: lputs( L_NOISE, " A4" ); break;
478 case 0x02: lputs( L_NOISE, " unlim" ); break;
479 case 0x01: lputs( L_NOISE, " B4" ); break;
480 }
481 lputs( L_NOISE, fax1_st_table[ (frame[4]>>4) & 0x07 ].txt );
482
483 if ( ( frame[4] & 0x80 ) == 0 ) break; /* extent bit */
484
485 if ( frame[5] & 0x04 ) lputs( L_NOISE, " ECM" );
486 if ( frame[5] & 0x40 ) lputs( L_NOISE, " T.6" );
487 if ( ( frame[5] & 0x80 ) == 0 ) break; /* extent bit */
488
489 if ( ( frame[6] & 0x80 ) == 0 ) break; /* extent bit */
490 /* the next bytes specify 300/400 dpi, color fax, ... */
491
492 break;
493 default:
494 lprintf( L_NOISE, "frame FCF 0x%02x not yet decoded", fcf );
495 }
496 }
497
498 /* send arbitrary frame
499 */
500 int fax1_send_frame _P4( (fd, carrier, frame, len),
501 int fd, int carrier, uch * frame, int len )
502 {
503 char * line;
504 static int carrier_active = -2; /* inter-frame marker */
505 uch dle_buf[FRAMESIZE*2+2]; /* for DLE-coded frame */
506 int r,w;
507
508 fax1_dump_frame( '>', frame, len );
509
510 /* this should never take more than a few msec, so the 10s timeout
511 * is more a safeguard for modem lockups, implementation mistakes, etc.
512 * - make sure we're not getting stuck in protocol loops
513 */
514 alarm(10);
515
516 /* send AT+FTH=3, wait for CONNECT
517 * (but only if we've not sent an non-final frame before!)
518 */
519
520 /* special-case (AT+FTS=8;+FTH=3)
521 */
522 if ( carrier < 0 ) /* T30_CAR_HAVE_V21 */
523 {
524 carrier_active = -carrier; /* -> T30_CAR_V21 */
525 carrier = T30_CAR_SAME;
526 }
527
528 /* catch internal out-of-sync condition ('canthappen')
529 * (this is OK for the very first frame sent in receive mode - ugly, yes)
530 */
531 if ( carrier == T30_CAR_SAME && carrier_active == -1 )
532 {
533 errno = EINVAL;
534 lprintf( L_ERROR, "fax1_send_frame: internal error - no carrier, but T30_CAR_SAME requested" );
535 return ERROR;
536 }
537
538 if ( carrier > 0 && carrier_active != carrier )
539 {
540 char cmd[20];
541
542 sprintf( cmd, "AT+FTH=%d", carrier );
543 if ( fax_send( cmd, fd ) == ERROR )
544 { alarm(0); carrier_active=-1; return ERROR; }
545
546 /* wait for CONNECT/NO CARRIER */
547 line = mdm_get_line( fd );
548 if ( line != NULL && strcmp( line, cmd ) == 0 )
549 { line = mdm_get_line( fd ); } /* skip echo */
550
551 if ( line == NULL || strcmp( line, "CONNECT" ) != 0 )
552 {
553 alarm(0);
554 lprintf( L_WARN, "fax1_send_frame: no carrier (%s)", line );
555 carrier_active=-1;
556 return ERROR;
557 }
558
559 carrier_active=carrier;
560 }
561
562 /* first 0xff is mandatory, but not passed by caller */
563 dle_buf[0] = 0xff;
564 w=1;
565
566 /* send <DLE> encoded frame data */
567 for( r=0; r<len; r++ )
568 {
569 if ( frame[r] == DLE ) { dle_buf[w++] = DLE; }
570 dle_buf[w++] = frame[r];
571 }
572
573 /* end-of-frame: <DLE><ETX> */
574 dle_buf[w++] = DLE; dle_buf[w++] = ETX;
575
576 lprintf( L_JUNK, "fax1_send_frame: %d/%d", len+1, w );
577
578 if ( write( fd, dle_buf, w ) != w )
579 {
580 lprintf( L_ERROR, "fax1_send_frame: can't write all %d bytes", w );
581 alarm(0);
582 fax_hangup=TRUE;
583 return ERROR;
584 }
585
586 /*!!! alarm */
587 /*!!! LASAT schickt "CONNECT\r\nOK" bzw. nur "OK" (final/non-final)
588 * --> ist das normal und richtig so??!?
589 *
590 * Nein... - es kommt immer entweder-oder, aber nach CONNECT muss
591 * man OHNE neues AT+FTH *SOFORT* weitersenden!
592 */
593 line = mdm_get_line( fd );
594 lprintf( L_NOISE, "fax1_send_frame: frame sent, got '%s'", line );
595
596 if ( frame[0] & T30_FINAL )
597 {
598 carrier_active = -1; /* carrier is off */
599 lprintf( L_NOISE, "carrier is off - OK='%s'", line );
600 }
601
602 /* as we're sending, we shouldn't see NO CARRIER response - but this
603 * can happen, e.g. when the modem notices a remote hangup (ISDN etc.)
604 */
605 if ( line == NULL || strcmp( line, "NO CARRIER" ) == 0 )
606 {
607 lprintf( L_WARN, "fax1_send_frame: unexpected post-frame string '%s', assuming carrier off", line? line: "(null)" );
608 carrier_active = -1;
609 return ERROR;
610 }
611
612 #if 0
613 if ( line != NULL && strcmp( line, "CONNECT" ) == 0 )
614 {
615 line = mdm_get_line( fd );
616 lprintf( L_NOISE, "fax1_send_frame(2): got '%s'", line );
617 }
618 #endif
619
620 return NOERROR;
621 }
622
623 /* send simple frame, consisting only of FCF and no arguments
624 * (non-final frame)
625 */
626 int fax1_send_simf_nonfinal _P3( (fd, carrier, fcf),
627 int fd, int carrier, uch fcf )
628 {
629 uch frame[2];
630 frame[0] = 0x03;
631 frame[1] = fcf;
632 return fax1_send_frame( fd, carrier, frame, 2 );
633 }
634
635 /* send simple frame, consisting only of FCF and no arguments
636 * (final frame)
637 */
638 int fax1_send_simf_final _P3( (fd, carrier, fcf),
639 int fd, int carrier, uch fcf )
640 {
641 uch frame[2];
642 frame[0] = 0x03 | T30_FINAL;
643 frame[1] = fcf;
644 return fax1_send_frame( fd, carrier, frame, 2 );
645 }
646
647 /* send "disconnect now" frame (DCN), and move internal state to "game over"
648 * Note: this is always a "final" frame
649 */
650 int fax1_send_dcn _P2((fd, code), int fd, int code )
651 {
652 if ( code != -1 )
653 { fax_hangup_code = code; fax_hangup = TRUE; }
654 return fax1_send_simf_final( fd, T30_CAR_V21, T30_DCN|fax1_dis );
655 }
656
657 /* send non-standard frame (NSF) with mgetty T.35 vendor ID
658 */
659 int fax1_send_nsf _P2( (fd, carrier), int fd, int carrier )
660 {
661 uch frame[FRAMESIZE];
662
663 /* NSF is never final frame */
664 frame[0] = 0x03;
665 frame[1] = T30_NSF;
666 frame[2] = 0x20; /* germany */
667 frame[3] = 0x81; /* mgetty + */
668 frame[4] = 0x70; /* sendfax */
669 frame[5] = 0x00; /* frame version */
670 strncpy( &frame[6], "mgetty ", 7 );
671 strncpy( &frame[13], VERSION_SHORT, 7 );
672
673 return fax1_send_frame( fd, carrier, frame, 20 );
674 }
675
676 /* send local identification (CSI, CIG or TSI)
677 * Note: "final" bit is never set, as these frames are always optional.
678 */
679 int fax1_send_idframe _P3((fd,fcf,carrier), int fd, uch fcf, int carrier)
680 {
681 unsigned char frame[F1LID+2];
682
683 frame[0] = 0x03;
684 frame[1] = fcf;
685 memcpy( &frame[2], fax1_local_id, F1LID );
686
687 return fax1_send_frame( fd, carrier, frame, sizeof(frame) );
688 }
689
690 void fax1_copy_id _P1((frame), uch * frame )
691 {
692 int w, r;
693 char c;
694
695 frame += 2; /* go to start of ID */
696 r = F1LID-1; w = 0;
697
698 while ( r>= 0 && isspace(frame[r]) ) r--; /* skip leading whitespace */
699
700 while ( r>=0 ) /* copy backwards! */
701 {
702 c = frame[r--];
703 if ( c == '"' || c == '\'' ) fax_remote_id[w++] = '_';
704 else fax_remote_id[w++] = c;
705 }
706 while( w>0 && isspace(fax_remote_id[w-1]) ) w--;
707 fax_remote_id[w]=0;
708
709 lprintf( L_MESG, "fax_id: '%s'", fax_remote_id );
710 }
711
712 /* set local capabilities in DIS frame, announce to remote sender
713 */
714 int fax1_send_dis _P1( (fd), int fd )
715 {
716 uch frame[FRAMESIZE];
717 int speedbits = 0;
718 int r_flags;
719 struct fax1_btable * dis_btp = fax1_btable;
720
721 /* start with modem capabilities, restrain by FDCC values */
722 r_flags = fax1_frm;
723 while( dis_btp->speed > 2400 )
724 {
725 if ( dis_btp->speed < fax1_min || dis_btp->speed > fax1_max )
726 {
727 r_flags &= ~(dis_btp->flag);
728 lprintf( L_NOISE, "fax1_dis: %d out of range -> r_flags=%03x", dis_btp->speed, r_flags );
729 }
730 dis_btp++;
731 }
732
733 /* some devices advertise V.17, but it doesn't work - so provide override
734 * switch via modem_quirks that unconditionally turns off V.17
735 */
736 if ( modem_quirks & MQ_C1_NO_V17 ) r_flags &= ~V17;
737
738
739 /* this is a bit messy, but I don't know a really elegant way to
740 * use the fax1_btable structure for that (see T.30, Table 2, p.46)
741 */
742 if ( (r_flags & V17) && (r_flags & V29) && (r_flags & V27ter) )
743 {
744 speedbits = 0x2c; /* 1101 */
745 }
746 else
747 {
748 if ( r_flags & V27t_4800 ) { speedbits |= 0x08; /* 0100 */ }
749 if ( r_flags & V29 ) { speedbits |= 0x04; /* 1000 */ }
750 }
751
752 lprintf( L_NOISE, "fax1_dis: r_flags=%03x -> speedbits=0x%02x",
753 r_flags, speedbits );
754
755 /* DIS is always final frame */
756 frame[0] = 0x03 | T30_FINAL;
757 frame[1] = T30_DIS;
758
759 frame[2] = 0x00; /* bits 1..8: group 1/2 - unwanted */
760 frame[3] = 0x00 | /* bit 9: can transmit - TODO: polling! */
761 0x02 | /* bit 10: can receive */
762 speedbits | /* bit 11..14: receive rates - TODO!! */
763 ((fax1_res&1) <<6) | /* bit 15: can fine */
764 0x00; /* bit 16: can 2D */
765 frame[4] = 0x08 | /* bits 17..20 = 215mm width, unlim. length */
766 0x70 | /* bits 21..23 = 0ms scan time */
767 0x00; /* bit 24: extend bit - final */
768
769 return fax1_send_frame( fd, T30_CAR_SAME, frame, 5 );
770 }
771
772 /* parse incoming DIS frame, set remote capability flags
773 */
774
775 fax_param_t remote_cap;
776
777 void fax1_parse_dis _P1((frame), uch * frame )
778 {
779 remote_cap.vr = remote_cap.br = remote_cap.wd = remote_cap.ln =
780 remote_cap.df = remote_cap.ec = remote_cap.bf = remote_cap.st = 0;
781
782 frame += 2; /* go to start of FIF */
783
784 /* bit 9: ready to transmit fax (polling) */
785 if ( frame[1] & 0x01 ) fax_to_poll = TRUE;
786
787 /* bit 10: receiving capabilities */
788 if ( ( frame[1] & 0x02 ) == 0 )
789 {
790 /*!!!! HANDLE THIS */
791 lprintf( L_WARN, "remote station can't receive!" );
792 fax_hangup = TRUE; fax_hangup_code = 21; return;
793 }
794
795 switch( frame[1] & 0x3c ) /* bits 11..14 - data signalling rate */
796 {
797 case 0x00: remote_cap.br = V27t_2400; break;
798 case 0x08: remote_cap.br = V27ter; break;
799 case 0x04: remote_cap.br = V29; break;
800 case 0x0c: remote_cap.br = V29 | V27ter; break;
801 case 0x1c: remote_cap.br = V29 | V27ter; break; /* V.33 */
802 case 0x2c: remote_cap.br = V17 | V29 | V27ter; break;
803 default:
804 lprintf( L_WARN, "unknown signalling rate: 0x%02x, use V27ter", frame[1] & 0x3c );
805 remote_cap.br = V27ter;
806 }
807
808 if ( frame[1] & 0x40 ) /* bit 15: fine res. */
809 {
810 remote_cap.vr = 1;
811 /*!! check bits 42 + 43 for "super-fine" (300/400 dpi) */
812 }
813
814 if ( frame[1] & 0x80 ) /* bit 16: 2D */
815 remote_cap.df = 1; /* df??? */
816
817 /* bit 17+18: recording width, valid: 0/1/2 = 215/255/303 mm */
818 remote_cap.wd = frame[2] & 0x03;
819
820 /* bit 19+20: recording length, valid: 0/1/2 = A4/B4/unlimited */
821 remote_cap.ln = ( frame[2] >> 2 ) & 0x03;
822
823 /* bit 21-23: minimum scan line time */
824 remote_cap.st = fax1_st_table[ (frame[2] >> 4) & 0x07 ].st;
825
826 if ( frame[2] & 0x80 ) /* extend bit */
827 {
828 /* bit 27: ECM */
829 if ( frame[3] & 0x04 ) remote_cap.ec = 1;
830 }
831
832 fax1_dis = 0x01; /* set "X" bit (= received DIS OK) */
833
834 lprintf( L_MESG, "+FIS: %d,%03x,%d,%d,%d,%d,%d,%d",
835 remote_cap.vr, remote_cap.br, remote_cap.wd,
836 remote_cap.ln, remote_cap.df, remote_cap.ec,
837 remote_cap.bf, remote_cap.st );
838 }
839
840 int fax1_send_dcs _P2((fd, s_time), int fd, int s_time )
841 {
842 uch framebuf[FRAMESIZE];
843 int i;
844
845 /* find baud/carrier table entry that has a speed not over
846 * "speed", and that uses a modulation scheme supported by both
847 * the local and remote modem
848 */
849 dcs_btp = fax1_btable;
850
851 while( dcs_btp->speed > 2400 &&
852 ( dcs_btp->speed > fax1_max ||
853 ( dcs_btp->flag & fax1_ftm & remote_cap.br ) == 0 ) ) dcs_btp++;
854
855 lprintf( L_NOISE, "+DCS: 1,%03x", dcs_btp->flag );
856
857 /*!!! calculate ALL values from DIS and to-be-sent page */
858 framebuf[0] = 0x03 | T30_FINAL; /* DCS is always final frame */
859 framebuf[1] = fax1_dis | T30_DCS; /* FCF */
860 framebuf[2] = 0; /* bits 1..8 */
861 framebuf[3] = 0x02 | /* bit 10: receiver operation */
862 dcs_btp->dcs_bits | /* bits 11..14: signalling rate */
863 ((fax1_res&remote_cap.vr)<<6) | /* bit 15: fine mode */
864 0x00; /* bit 16: 2D */
865 framebuf[4] = 0x00 | /* bit 17+18: 215 mm width */
866 0x04 | /* bit 19+20: B4 length */
867 0x00 | /* bits 21-23: scan line time */
868 0x00; /* bit 24: extend bit - final */
869
870 /* calculate correct bit settings for scan line time (bits 21-23) */
871 for( i=0;i<=7;i++ )
872 {
873 if ( fax1_st_table[i].ms_n == s_time )
874 { framebuf[4] |= i<<4; break; }
875 }
876 return fax1_send_frame( fd, T30_CAR_V21, framebuf, 5 );
877 }
878
879 /* parse incoming DCS frame
880 * put communication parameters into global variables (dcs_btp, fax_par_d)
881 */
882 /* TODO: error handling? "I don't understand this -> DCN" */
883 void fax1_parse_dcs _P1((frame), uch *frame)
884 {
885 /* bit rate + modulation requested (bits 11..14) */
886 dcs_btp = fax1_btable;
887 while( dcs_btp->speed > 2400 &&
888 dcs_btp->dcs_bits != (frame[3] & 0x3c) ) dcs_btp++;
889
890 fax_par_d.br = dcs_btp->speed/2400 - 1;
891
892 /* fine resolution: bit 15 (98/196 lpi) */
893 fax_par_d.vr = ( frame[3] & 0x40 )? 1: 0;
894
895 /* 2D: bit 16 (1D/2D mod read) */
896 fax_par_d.df = ( frame[3] & 0x80 )? 1: 0;
897
898 /* page width: bits 17+18 (byte 4, bits 0+1) */
899 fax_par_d.wd = frame[4] & 0x03;
900
901 /* page length: bits 19+20 */
902 fax_par_d.ln = (frame[4] >> 2 ) & 0x03;
903
904 /* scan line time: bits 21-23 */
905 fax_par_d.st = fax1_st_table[ (frame[4] >> 4) & 0x07 ].st;
906
907 /* extend bit? */
908 if ( ( frame[4] && 0x80 ) == 0 ) goto done;
909
910 /* ECM - TODO */
911
912 done:
913 lprintf( L_NOISE, "DCS: speed=%d, flag=%03x",
914 dcs_btp->speed, dcs_btp->flag );
915 }
916
917 int fax1_init_FRM _P2((fd,carrier), int fd, int carrier )
918 {
919 char cmd[20];
920 char *line;
921 int timeout = 30;
922
923 /* TODO: is this sufficient timeout handling? */
924 if ( timeout > 0 )
925 {
926 signal( SIGALRM, fax1_sig_alarm );
927 alarm( (timeout/10)+1 );
928 }
929
930 sprintf( cmd, "AT+FRM=%d", carrier );
931 if ( fax_send( cmd, fd ) )
932 { alarm(0); return ERROR; }
933
934 /* wait for CONNECT/NO CARRIER */
935 line = mdm_get_line( fd );
936 if ( line != NULL && strcmp( line, cmd ) == 0 )
937 { line = mdm_get_line( fd ); } /* skip echo */
938
939 alarm(0);
940 if ( line == NULL || strcmp( line, "CONNECT" ) != 0 )
941 {
942 lprintf( L_WARN, "fax1_init_FRM: no carrier (%s)", line );
943 return ERROR;
944 }
945 return NOERROR;
946 }
947
948 #endif /* CLASS1 */
949