forked from ASWATFZLLC/spandsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht30.c
7410 lines (7072 loc) · 251 KB
/
t30.c
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SpanDSP - a series of DSP components for telephony
*
* t30.c - ITU T.30 FAX transfer processing
*
* Written by Steve Underwood <[email protected]>
*
* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Steve Underwood
*
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2.1,
* as published by the Free Software Foundation.
*
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*! \file */
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif
#include <stdlib.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#if defined(HAVE_TGMATH_H)
#include <tgmath.h>
#endif
#if defined(HAVE_MATH_H)
#include <math.h>
#endif
#if defined(HAVE_STDBOOL_H)
#include <stdbool.h>
#else
#include "spandsp/stdbool.h"
#endif
#include "floating_fudge.h"
#include <tiffio.h>
#include "spandsp/telephony.h"
#include "spandsp/alloc.h"
#include "spandsp/logging.h"
#include "spandsp/bit_operations.h"
#include "spandsp/queue.h"
#include "spandsp/power_meter.h"
#include "spandsp/complex.h"
#include "spandsp/tone_generate.h"
#include "spandsp/async.h"
#include "spandsp/hdlc.h"
#include "spandsp/fsk.h"
#include "spandsp/v29rx.h"
#include "spandsp/v29tx.h"
#include "spandsp/v27ter_rx.h"
#include "spandsp/v27ter_tx.h"
#include "spandsp/timezone.h"
#include "spandsp/t4_rx.h"
#include "spandsp/t4_tx.h"
#include "spandsp/image_translate.h"
#include "spandsp/t81_t82_arith_coding.h"
#include "spandsp/t85.h"
#include "spandsp/t42.h"
#include "spandsp/t43.h"
#include "spandsp/t4_t6_decode.h"
#include "spandsp/t4_t6_encode.h"
#include "spandsp/t30_fcf.h"
#include "spandsp/t35.h"
#include "spandsp/t30.h"
#include "spandsp/t30_api.h"
#include "spandsp/t30_logging.h"
#include "spandsp/private/logging.h"
#include "spandsp/private/timezone.h"
#include "spandsp/private/t81_t82_arith_coding.h"
#include "spandsp/private/t85.h"
#include "spandsp/private/t42.h"
#include "spandsp/private/t43.h"
#include "spandsp/private/t4_t6_decode.h"
#include "spandsp/private/t4_t6_encode.h"
#include "spandsp/private/image_translate.h"
#include "spandsp/private/t4_rx.h"
#include "spandsp/private/t4_tx.h"
#include "spandsp/private/t30.h"
#include "spandsp/private/t30_dis_dtc_dcs_bits.h"
#include "t30_local.h"
/*! The maximum permitted number of retries of a single command allowed. */
#define MAX_COMMAND_TRIES 6
/*! The maximum permitted number of retries of a single response request allowed. This
is not specified in T.30. However, if you don't apply some limit a messed up FAX
terminal could keep you retrying all day. Its a backstop protection. */
#define MAX_RESPONSE_TRIES 6
/* T.30 defines the following call phases:
Phase A: Call set-up.
Exchange of CNG, CED and the called terminal identification.
Phase B: Pre-message procedure for identifying and selecting the required facilities.
Capabilities negotiation, and training, up the the confirmation to receive.
Phase C: Message transmission (includes phasing and synchronization where appropriate).
Transfer of the message at high speed.
Phase D: Post-message procedure, including end-of-message and confirmation and multi-document procedures.
End of message and acknowledgement.
Phase E: Call release
Final call disconnect. */
enum
{
T30_PHASE_IDLE = 0, /* Freshly initialised */
T30_PHASE_A_CED, /* Doing the CED (answer) sequence */
T30_PHASE_A_CNG, /* Doing the CNG (caller) sequence */
T30_PHASE_B_RX, /* Receiving pre-message control messages */
T30_PHASE_B_TX, /* Transmitting pre-message control messages */
T30_PHASE_C_NON_ECM_RX, /* Receiving a document message in non-ECM mode */
T30_PHASE_C_NON_ECM_TX, /* Transmitting a document message in non-ECM mode */
T30_PHASE_C_ECM_RX, /* Receiving a document message in ECM (HDLC) mode */
T30_PHASE_C_ECM_TX, /* Transmitting a document message in ECM (HDLC) mode */
T30_PHASE_D_RX, /* Receiving post-message control messages */
T30_PHASE_D_TX, /* Transmitting post-message control messages */
T30_PHASE_E, /* In phase E */
T30_PHASE_CALL_FINISHED /* Call completely finished */
};
static const char *phase_names[] =
{
"IDLE",
"A_CED",
"A_CNG",
"B_RX",
"B_TX",
"C_NON_ECM_RX",
"C_NON_ECM_TX",
"C_ECM_RX",
"C_ECM_TX",
"D_RX",
"D_TX",
"E",
"CALL_FINISHED"
};
/* These state names are modelled after places in the T.30 flow charts. */
enum
{
T30_STATE_IDLE = 0,
T30_STATE_ANSWERING,
T30_STATE_B,
T30_STATE_C,
T30_STATE_D,
T30_STATE_D_TCF,
T30_STATE_D_POST_TCF,
T30_STATE_F_TCF,
T30_STATE_F_CFR,
T30_STATE_F_FTT,
T30_STATE_F_DOC_NON_ECM,
T30_STATE_F_POST_DOC_NON_ECM,
T30_STATE_F_DOC_ECM,
T30_STATE_F_POST_DOC_ECM,
T30_STATE_F_POST_RCP_MCF,
T30_STATE_F_POST_RCP_PPR,
T30_STATE_F_POST_RCP_RNR,
T30_STATE_R,
T30_STATE_T,
T30_STATE_I,
T30_STATE_II,
T30_STATE_II_Q,
T30_STATE_III_Q,
T30_STATE_IV,
T30_STATE_IV_PPS_NULL,
T30_STATE_IV_PPS_Q,
T30_STATE_IV_PPS_RNR,
T30_STATE_IV_CTC,
T30_STATE_IV_EOR,
T30_STATE_IV_EOR_RNR,
T30_STATE_CALL_FINISHED
};
static const char *state_names[] =
{
"IDLE",
"ANSWERING",
"B",
"C",
"D",
"D_TCF",
"D_POST_TCF",
"F_TCF",
"F_CFR",
"F_FTT",
"F_DOC_NON_ECM",
"F_POST_DOC_NON_ECM",
"F_DOC_ECM",
"F_POST_DOC_ECM",
"F_POST_RCP_MCF",
"F_POST_RCP_PPR",
"F_POST_RCP_RNR",
"R",
"T",
"I",
"II",
"II_Q",
"III_Q",
"IV",
"IV_PPS_NULL",
"IV_PPS_Q",
"IV_PPS_RNR",
"IV_CTC",
"IV_EOR",
"IV_EOR_RNR",
"CALL_FINISHED"
};
enum
{
T30_MIN_SCAN_20MS = 0,
T30_MIN_SCAN_5MS = 1,
T30_MIN_SCAN_10MS = 2,
T30_MIN_SCAN_40MS = 4,
T30_MIN_SCAN_0MS = 7,
};
enum
{
T30_MODE_SEND_DOC = 1,
T30_MODE_RECEIVE_DOC
};
/*! These are internal assessments of received image quality, used to determine whether we
continue, retrain, or abandon the call. This is only relevant to non-ECM operation. */
enum
{
T30_COPY_QUALITY_PERFECT = 0,
T30_COPY_QUALITY_GOOD,
T30_COPY_QUALITY_POOR,
T30_COPY_QUALITY_BAD
};
enum
{
DISBIT1 = 0x01,
DISBIT2 = 0x02,
DISBIT3 = 0x04,
DISBIT4 = 0x08,
DISBIT5 = 0x10,
DISBIT6 = 0x20,
DISBIT7 = 0x40,
DISBIT8 = 0x80
};
/*! There are high level indications of what is happening at any instant, to guide the cleanup
continue, retrain, or abandoning of the call. */
enum
{
OPERATION_IN_PROGRESS_NONE = 0,
OPERATION_IN_PROGRESS_T4_RX,
OPERATION_IN_PROGRESS_T4_TX,
OPERATION_IN_PROGRESS_POST_T4_RX,
OPERATION_IN_PROGRESS_POST_T4_TX
};
/* All timers specified in milliseconds */
/*! Time-out T0 defines the amount of time an automatic calling terminal waits for the called terminal
to answer the call.
T0 begins after the dialling of the number is completed and is reset:
a) when T0 times out; or
b) when timer T1 is started; or
c) if the terminal is capable of detecting any condition which indicates that the call will not be
successful, when such a condition is detected.
The recommended value of T0 is 60+-5s. However, when it is anticipated that a long call set-up
time may be encountered, an alternative value of up to 120s may be used.
NOTE - National regulations may require the use of other values for T0. */
#define DEFAULT_TIMER_T0 60000
/*! Time-out T1 defines the amount of time two terminals will continue to attempt to identify each
other. T1 is 35+-5s, begins upon entering phase B, and is reset upon detecting a valid signal or
when T1 times out.
For operating methods 3 and 4 (see 3.1), the calling terminal starts time-out T1 upon reception of
the V.21 modulation scheme.
For operating method 4 bis a (see 3.1), the calling terminal starts time-out T1 upon starting
transmission using the V.21 modulation scheme.
Annex A says T1 is also the timeout to be used for the receipt of the first HDLC frame after the
start of high speed flags in ECM mode. This seems a strange reuse of the T1 name, so we distinguish
it here by calling it T1A. */
#define DEFAULT_TIMER_T1 35000
#define DEFAULT_TIMER_T1A 35000
/*! Time-out T2 makes use of the tight control between commands and responses to detect the loss of
command/response synchronization. T2 is 6+-1s, and begins when initiating a command search
(e.g., the first entrance into the "command received" subroutine, reference flow diagram in section 5.2).
T2 is reset when an HDLC flag is received or when T2 times out. */
#define DEFAULT_TIMER_T2 7000
/*! Once HDLC flags begin, T2 is reset, and a 3s timer begins. This timer is unnamed in T.30. Here we
term it T2_FLAGGED. No tolerance is specified for this timer. T2_FLAGGED specifies the maximum
time to wait for the end of a frame, after the initial flag has been seen. */
#define DEFAULT_TIMER_T2_FLAGGED 3000
/*! If the HDLC carrier falls during reception, we need to apply a minimum time before continuing. If we
don't, there are circumstances where we could continue and reply before the incoming signals have
really finished. E.g. if a bad DCS is received in a DCS-TCF sequence, we need wait for the TCF
carrier to pass, before continuing. This timer is specified as 200ms, but no tolerance is specified.
It is unnamed in T.30. Here we term it T2_DROPPED */
#define DEFAULT_TIMER_T2_DROPPED 200
/*! Timer T2C is a fake timer state for internal use */
/*! Time-out T3 defines the amount of time a terminal will attempt to alert the local operator in
response to a procedural interrupt. Failing to achieve operator intervention, the terminal will
discontinue this attempt and shall issue other commands or responses. T3 is 10+-5s, begins on the
first detection of a procedural interrupt command/response signal (i.e., PIN/PIP or PRI-Q) and is
reset when T3 times out or when the operator initiates a line request. */
#define DEFAULT_TIMER_T3 15000
/*! Time-out T4 defines the amount of time a terminal will wait for flags to begin, when waiting for a
response from a remote terminal. T2 is 3s +-15%, and begins when initiating a response search
(e.g., the first entrance into the "response received" subroutine, reference flow diagram in section 5.2).
T4 is reset when an HDLC flag is received or when T4 times out.
NOTE - For manual FAX units, the value of timer T4 may be either 3.0s +-15% or 4.5s +-15%.
If the value of 4.5s is used, then after detection of a valid response to the first DIS, it may
be reduced to 3.0s +-15%. T4 = 3.0s +-15% for automatic units. */
#define DEFAULT_TIMER_T4 3450
/*! Once HDLC flags begin, T4 is reset, and a 3s timer begins. This timer is unnamed in T.30. Here we
term it T4_FLAGGED. No tolerance is specified for this timer. T4_FLAGGED specifies the maximum time
to wait for the end of a frame, after the initial flag has been seen. Note that a different timer
is used for the fast HDLC in ECM mode, to provide time for physical paper handling. */
#define DEFAULT_TIMER_T4_FLAGGED 3000
/*! If the HDLC carrier falls during reception, we need to apply a minimum time before continuing. if we
don't, there are circumstances where we could continue and reply before the incoming signals have
really finished. E.g. if a bad DCS is received in a DCS-TCF sequence, we need to wait for the TCF
carrier to pass, before continuing. This timer is specified as 200ms, but no tolerance is specified.
It is unnamed in T.30. Here we term it T4_DROPPED */
#define DEFAULT_TIMER_T4_DROPPED 200
/*! Timer T4C is a fake timer state for internal use */
/*! Time-out T5 is defined for the optional T.4 error correction mode. Time-out T5 defines the amount
of time waiting for clearance of the busy condition of the receiving terminal. T5 is 60+-5s and
begins on the first detection of the RNR response. T5 is reset when T5 times out or the MCF or PIP
response is received or when the ERR or PIN response is received in the flow control process after
transmitting the EOR command. If the timer T5 has expired, the DCN command is transmitted for
call release. */
#define DEFAULT_TIMER_T5 65000
/*! (Annex C - ISDN) Time-out T6 defines the amount of time two terminals will continue to attempt to
identify each other. T6 is 5+-0.5s. The timeout begins upon entering Phase B, and is reset upon
detecting a valid signal, or when T6 times out. */
#define DEFAULT_TIMER_T6 5000
/*! (Annex C - ISDN) Time-out T7 is used to detect loss of command/response synchronization. T7 is 6+-1s.
The timeout begins when initiating a command search (e.g., the first entrance into the "command received"
subroutine - see flow diagram in C.5) and is reset upon detecting a valid signal or when T7 times out. */
#define DEFAULT_TIMER_T7 7000
/*! (Annex C - ISDN) Time-out T8 defines the amount of time waiting for clearance of the busy condition
of the receiving terminal. T8 is 10+-1s. The timeout begins on the first detection of the combination
of no outstanding corrections and the RNR response. T8 is reset when T8 times out or MCF response is
received. If the timer T8 expires, a DCN command is transmitted for call release. */
#define DEFAULT_TIMER_T8 10000
/*! Final time we allow for things to flush through the system, before we disconnect, in milliseconds.
200ms should be fine for a PSTN call. For a T.38 call something longer is desirable. This delay is
to allow sufficient time for the last message to be flushed all the way through to the far end. */
#define FINAL_FLUSH_TIME 1000
/*! The number of PPRs received before CTC or EOR is sent in ECM mode. T.30 defines this as 4,
but it could be varied, and the Japanese spec, for example, does make this value a
variable. */
#define PPR_LIMIT_BEFORE_CTC_OR_EOR 4
/* HDLC message header byte values */
#define ADDRESS_FIELD 0xFF
#define CONTROL_FIELD_NON_FINAL_FRAME 0x03
#define CONTROL_FIELD_FINAL_FRAME 0x13
enum
{
TIMER_IS_IDLE = 0,
TIMER_IS_T2,
TIMER_IS_T1A,
TIMER_IS_T2_FLAGGED,
TIMER_IS_T2_DROPPED,
TIMER_IS_T2C,
TIMER_IS_T4,
TIMER_IS_T4_FLAGGED,
TIMER_IS_T4_DROPPED,
TIMER_IS_T4C
};
/* Start points in the fallback table for different capabilities */
/*! The starting point in the modem fallback sequence if V.17 is allowed */
#define T30_V17_FALLBACK_START 0
/*! The starting point in the modem fallback sequence if V.17 is not allowed */
#define T30_V29_FALLBACK_START 3
/*! The starting point in the modem fallback sequence if V.29 is not allowed */
#define T30_V27TER_FALLBACK_START 6
static const struct
{
int bit_rate;
int modem_type;
int which;
uint8_t dcs_code;
} fallback_sequence[] =
{
{14400, T30_MODEM_V17, T30_SUPPORT_V17, (DISBIT6 )},
{12000, T30_MODEM_V17, T30_SUPPORT_V17, (DISBIT6 | DISBIT4 )},
{ 9600, T30_MODEM_V17, T30_SUPPORT_V17, (DISBIT6 | DISBIT3)},
{ 9600, T30_MODEM_V29, T30_SUPPORT_V29, ( DISBIT3)},
{ 7200, T30_MODEM_V17, T30_SUPPORT_V17, (DISBIT6 | DISBIT4 | DISBIT3)},
{ 7200, T30_MODEM_V29, T30_SUPPORT_V29, ( DISBIT4 | DISBIT3)},
{ 4800, T30_MODEM_V27TER, T30_SUPPORT_V27TER, ( DISBIT4 )},
{ 2400, T30_MODEM_V27TER, T30_SUPPORT_V27TER, (0 )},
{ 0, 0, 0, (0 )}
};
static void queue_phase(t30_state_t *s, int phase);
static void set_phase(t30_state_t *s, int phase);
static void set_state(t30_state_t *s, int state);
static void shut_down_hdlc_tx(t30_state_t *s);
static void send_frame(t30_state_t *s, const uint8_t *fr, int frlen);
static void send_simple_frame(t30_state_t *s, int type);
static void send_dcn(t30_state_t *s);
static void repeat_last_command(t30_state_t *s);
static void terminate_call(t30_state_t *s);
static void start_final_pause(t30_state_t *s);
static void decode_20digit_msg(t30_state_t *s, char *msg, const uint8_t *pkt, int len);
static void decode_url_msg(t30_state_t *s, char *msg, const uint8_t *pkt, int len);
static int decode_nsf_nss_nsc(t30_state_t *s, uint8_t *msg[], const uint8_t *pkt, int len);
static int send_cfr_sequence(t30_state_t *s, int start);
static int build_dcs(t30_state_t *s);
static void set_min_scan_time(t30_state_t *s);
static void timer_t2_start(t30_state_t *s);
static void timer_t2_flagged_start(t30_state_t *s);
static void timer_t2_dropped_start(t30_state_t *s);
static void timer_t4_start(t30_state_t *s);
static void timer_t4_flagged_start(t30_state_t *s);
static void timer_t4_dropped_start(t30_state_t *s);
static void timer_t2_t4_stop(t30_state_t *s);
/*! Test a specified bit within a DIS, DTC or DCS frame */
#define test_ctrl_bit(s,bit) ((s)[3 + ((bit - 1)/8)] & (1 << ((bit - 1)%8)))
/*! Set a specified bit within a DIS, DTC or DCS frame */
#define set_ctrl_bit(s,bit) (s)[3 + ((bit - 1)/8)] |= (1 << ((bit - 1)%8))
/*! Set a specified block of bits within a DIS, DTC or DCS frame */
#define set_ctrl_bits(s,val,bit) (s)[3 + ((bit - 1)/8)] |= ((val) << ((bit - 1)%8))
/*! Clear a specified bit within a DIS, DTC or DCS frame */
#define clr_ctrl_bit(s,bit) (s)[3 + ((bit - 1)/8)] &= ~(1 << ((bit - 1)%8))
static int find_fallback_entry(int dcs_code)
{
int i;
/* The table is short, and not searched often, so a brain-dead linear scan seems OK */
for (i = 0; fallback_sequence[i].bit_rate; i++)
{
if (fallback_sequence[i].dcs_code == dcs_code)
break;
/*endif*/
}
/*endfor*/
if (fallback_sequence[i].bit_rate == 0)
return -1;
/*endif*/
return i;
}
/*- End of function --------------------------------------------------------*/
static int step_fallback_entry(t30_state_t *s)
{
while (fallback_sequence[++s->current_fallback].bit_rate)
{
if ((fallback_sequence[s->current_fallback].which & s->current_permitted_modems))
break;
/*endif*/
}
/*endwhile*/
if (fallback_sequence[s->current_fallback].bit_rate == 0)
{
/* Reset the fallback sequence */
s->current_fallback = 0;
return -1;
}
/*endif*/
/* We need to update the minimum scan time, in case we are in non-ECM mode. */
set_min_scan_time(s);
/* Now we need to rebuild the DCS message we will send. */
build_dcs(s);
return s->current_fallback;
}
/*- End of function --------------------------------------------------------*/
static int terminate_operation_in_progress(t30_state_t *s)
{
/* Make sure any FAX in progress is tidied up. If the tidying up has
already happened, repeating it here is harmless. */
switch (s->operation_in_progress)
{
case OPERATION_IN_PROGRESS_T4_TX:
t4_tx_release(&s->t4.tx);
s->operation_in_progress = OPERATION_IN_PROGRESS_POST_T4_TX;
break;
case OPERATION_IN_PROGRESS_T4_RX:
t4_rx_release(&s->t4.rx);
s->operation_in_progress = OPERATION_IN_PROGRESS_POST_T4_RX;
break;
}
/*endswitch*/
return 0;
}
/*- End of function --------------------------------------------------------*/
static int tx_start_page(t30_state_t *s)
{
if (t4_tx_start_page(&s->t4.tx))
{
terminate_operation_in_progress(s);
return -1;
}
/*endif*/
s->ecm_block = 0;
s->error_correcting_mode_retries = 0;
span_log(&s->logging, SPAN_LOG_FLOW, "Starting page %d of transfer\n", s->tx_page_number + 1);
return 0;
}
/*- End of function --------------------------------------------------------*/
static int tx_end_page(t30_state_t *s)
{
s->retries = 0;
if (t4_tx_end_page(&s->t4.tx) == 0)
{
s->tx_page_number++;
s->ecm_block = 0;
}
/*endif*/
return 0;
}
/*- End of function --------------------------------------------------------*/
static int rx_start_page(t30_state_t *s)
{
int i;
t4_rx_set_image_width(&s->t4.rx, s->image_width);
t4_rx_set_sub_address(&s->t4.rx, s->rx_info.sub_address);
t4_rx_set_dcs(&s->t4.rx, s->rx_dcs_string);
t4_rx_set_far_ident(&s->t4.rx, s->rx_info.ident);
t4_rx_set_vendor(&s->t4.rx, s->vendor);
t4_rx_set_model(&s->t4.rx, s->model);
t4_rx_set_rx_encoding(&s->t4.rx, s->line_compression);
t4_rx_set_x_resolution(&s->t4.rx, s->x_resolution);
t4_rx_set_y_resolution(&s->t4.rx, s->y_resolution);
if (t4_rx_start_page(&s->t4.rx))
return -1;
/*endif*/
/* Clear the ECM buffer */
for (i = 0; i < 256; i++)
s->ecm_len[i] = -1;
/*endfor*/
s->ecm_block = 0;
s->ecm_frames = -1;
s->ecm_frames_this_tx_burst = 0;
s->error_correcting_mode_retries = 0;
return 0;
}
/*- End of function --------------------------------------------------------*/
static int rx_end_page(t30_state_t *s)
{
if (t4_rx_end_page(&s->t4.rx) == 0)
{
s->rx_page_number++;
s->ecm_block = 0;
}
/*endif*/
return 0;
}
/*- End of function --------------------------------------------------------*/
static void report_rx_ecm_page_result(t30_state_t *s)
{
t4_stats_t stats;
/* This is only used for ECM pages, as copy_quality() does a similar job for non-ECM
pages as a byproduct of assessing copy quality. */
t4_rx_get_transfer_statistics(&s->t4.rx, &stats);
span_log(&s->logging, SPAN_LOG_FLOW, "Page no = %d\n", stats.pages_transferred);
span_log(&s->logging, SPAN_LOG_FLOW, "Image size = %d x %d pixels\n", stats.width, stats.length);
span_log(&s->logging, SPAN_LOG_FLOW, "Image resolution = %d/m x %d/m\n", stats.x_resolution, stats.y_resolution);
span_log(&s->logging, SPAN_LOG_FLOW, "Compression = %s (%d)\n", t4_compression_to_str(stats.compression), stats.compression);
span_log(&s->logging, SPAN_LOG_FLOW, "Compressed image size = %d bytes\n", stats.line_image_size);
}
/*- End of function --------------------------------------------------------*/
static int copy_quality(t30_state_t *s)
{
t4_stats_t stats;
int quality;
t4_rx_get_transfer_statistics(&s->t4.rx, &stats);
/* There is no specification for judging copy quality. However, we need to classify
it at three levels, to control what we do next: OK; tolerable, but retrain;
intolerable. */
/* Based on the thresholds used in the TSB85 tests, we consider:
<5% bad rows is OK
<15% bad rows to be tolerable, but retrain
>15% bad rows to be intolerable
*/
/* This is called before the page is confirmed, so we need to add one to get the page
number right */
span_log(&s->logging, SPAN_LOG_FLOW, "Page no = %d\n", stats.pages_transferred + 1);
span_log(&s->logging, SPAN_LOG_FLOW, "Image size = %d x %d pixels\n", stats.width, stats.length);
span_log(&s->logging, SPAN_LOG_FLOW, "Image resolution = %d/m x %d/m\n", stats.x_resolution, stats.y_resolution);
span_log(&s->logging, SPAN_LOG_FLOW, "Compression = %s (%d)\n", t4_compression_to_str(stats.compression), stats.compression);
span_log(&s->logging, SPAN_LOG_FLOW, "Compressed image size = %d bytes\n", stats.line_image_size);
span_log(&s->logging, SPAN_LOG_FLOW, "Bad rows = %d\n", stats.bad_rows);
span_log(&s->logging, SPAN_LOG_FLOW, "Longest bad row run = %d\n", stats.longest_bad_row_run);
/* Don't treat a page as perfect because it has zero bad rows out of zero total rows. A zero row
page has got to be some kind of total page failure. */
if (stats.bad_rows == 0 && stats.length != 0)
{
span_log(&s->logging, SPAN_LOG_FLOW, "Page quality is perfect\n");
quality = T30_COPY_QUALITY_PERFECT;
}
else if (stats.bad_rows*20 < stats.length)
{
span_log(&s->logging, SPAN_LOG_FLOW, "Page quality is good\n");
quality = T30_COPY_QUALITY_GOOD;
}
else if (stats.bad_rows*20 < stats.length*3)
{
span_log(&s->logging, SPAN_LOG_FLOW, "Page quality is poor\n");
quality = T30_COPY_QUALITY_POOR;
}
else
{
span_log(&s->logging, SPAN_LOG_FLOW, "Page quality is bad\n");
quality = T30_COPY_QUALITY_BAD;
}
/*endif*/
return quality;
}
/*- End of function --------------------------------------------------------*/
static void report_tx_result(t30_state_t *s, int result)
{
t4_stats_t stats;
if (span_log_test(&s->logging, SPAN_LOG_FLOW))
{
t4_tx_get_transfer_statistics(&s->t4.tx, &stats);
span_log(&s->logging,
SPAN_LOG_FLOW,
"%s - delivered %d pages\n",
(result) ? "Success" : "Failure",
stats.pages_transferred);
}
/*endif*/
}
/*- End of function --------------------------------------------------------*/
static void release_resources(t30_state_t *s)
{
if (s->tx_info.nsf)
{
span_free(s->tx_info.nsf);
s->tx_info.nsf = NULL;
}
/*endif*/
s->tx_info.nsf_len = 0;
if (s->tx_info.nsc)
{
span_free(s->tx_info.nsc);
s->tx_info.nsc = NULL;
}
/*endif*/
s->tx_info.nsc_len = 0;
if (s->tx_info.nss)
{
span_free(s->tx_info.nss);
s->tx_info.nss = NULL;
}
/*endif*/
s->tx_info.nss_len = 0;
if (s->tx_info.tsa)
{
span_free(s->tx_info.tsa);
s->tx_info.tsa = NULL;
}
/*endif*/
if (s->tx_info.ira)
{
span_free(s->tx_info.ira);
s->tx_info.ira = NULL;
}
/*endif*/
if (s->tx_info.cia)
{
span_free(s->tx_info.cia);
s->tx_info.cia = NULL;
}
/*endif*/
if (s->tx_info.isp)
{
span_free(s->tx_info.isp);
s->tx_info.isp = NULL;
}
/*endif*/
if (s->tx_info.csa)
{
span_free(s->tx_info.csa);
s->tx_info.csa = NULL;
}
/*endif*/
if (s->rx_info.nsf)
{
span_free(s->rx_info.nsf);
s->rx_info.nsf = NULL;
}
/*endif*/
s->rx_info.nsf_len = 0;
if (s->rx_info.nsc)
{
span_free(s->rx_info.nsc);
s->rx_info.nsc = NULL;
}
/*endif*/
s->rx_info.nsc_len = 0;
if (s->rx_info.nss)
{
span_free(s->rx_info.nss);
s->rx_info.nss = NULL;
}
/*endif*/
s->rx_info.nss_len = 0;
if (s->rx_info.tsa)
{
span_free(s->rx_info.tsa);
s->rx_info.tsa = NULL;
}
/*endif*/
if (s->rx_info.ira)
{
span_free(s->rx_info.ira);
s->rx_info.ira = NULL;
}
/*endif*/
if (s->rx_info.cia)
{
span_free(s->rx_info.cia);
s->rx_info.cia = NULL;
}
/*endif*/
if (s->rx_info.isp)
{
span_free(s->rx_info.isp);
s->rx_info.isp = NULL;
}
/*endif*/
if (s->rx_info.csa)
{
span_free(s->rx_info.csa);
s->rx_info.csa = NULL;
}
/*endif*/
}
/*- End of function --------------------------------------------------------*/
static uint8_t check_next_tx_step(t30_state_t *s)
{
int res;
int more;
res = t4_tx_next_page_has_different_format(&s->t4.tx);
if (res == 0)
{
span_log(&s->logging, SPAN_LOG_FLOW, "More pages to come with the same format\n");
return (s->local_interrupt_pending) ? T30_PRI_MPS : T30_MPS;
}
/*endif*/
if (res > 0)
{
span_log(&s->logging, SPAN_LOG_FLOW, "More pages to come with a different format\n");
s->tx_start_page = t4_tx_get_current_page_in_file(&s->t4.tx) + 1;
return (s->local_interrupt_pending) ? T30_PRI_EOM : T30_EOM;
}
/*endif*/
/* Call a user handler, if one is set, to check if another document is to be sent.
If so, we send an EOM, rather than an EOP. Then we will renegotiate, and the new
document will begin. */
if (s->document_handler)
more = s->document_handler(s->document_user_data, 0);
else
more = false;
/*endif*/
if (more)
{
span_log(&s->logging, SPAN_LOG_FLOW, "Another document to send\n");
//if (test_ctrl_bit(s->far_dis_dtc_frame, T30_DIS_BIT_MULTIPLE_SELECTIVE_POLLING_CAPABLE))
// return T30_EOS;
///*endif*/
return (s->local_interrupt_pending) ? T30_PRI_EOM : T30_EOM;
}
/*endif*/
span_log(&s->logging, SPAN_LOG_FLOW, "No more pages to send\n");
return (s->local_interrupt_pending) ? T30_PRI_EOP : T30_EOP;
}
/*- End of function --------------------------------------------------------*/
static int get_partial_ecm_page(t30_state_t *s)
{
int i;
int len;
s->ppr_count = 0;
s->ecm_progress = 0;
/* Fill our partial page buffer with a partial page. Use the negotiated preferred frame size
as the basis for the size of the frames produced. */
/* We fill the buffer with complete HDLC frames, ready to send out. */
/* The frames are all marked as not being final frames. When sent, the are followed by a partial
page signal, which is marked as the final frame. */
for (i = 3; i < 32 + 3; i++)
s->ecm_frame_map[i] = 0xFF;
/*endfor*/
for (i = 0; i < 256; i++)
{
s->ecm_len[i] = -1;
s->ecm_data[i][0] = ADDRESS_FIELD;
s->ecm_data[i][1] = CONTROL_FIELD_NON_FINAL_FRAME;
s->ecm_data[i][2] = T4_FCD;
/* These frames contain a frame sequence number within the partial page (one octet) followed
by some image data. */
s->ecm_data[i][3] = (uint8_t) i;
if (s->document_get_handler)
len = s->document_get_handler(s->document_get_user_data, &s->ecm_data[i][4], s->octets_per_ecm_frame);
else
len = t4_tx_get(&s->t4.tx, &s->ecm_data[i][4], s->octets_per_ecm_frame);
/*endif*/
if (len < s->octets_per_ecm_frame)
{
/* The document is not big enough to fill the entire buffer */
/* We need to pad to a full frame, as most receivers expect that. */
if (len > 0)
{
memset(&s->ecm_data[i][4 + len], 0, s->octets_per_ecm_frame - len);
s->ecm_len[i++] = (int16_t) (s->octets_per_ecm_frame + 4);
}
/*endif*/
s->ecm_frames = i;
span_log(&s->logging, SPAN_LOG_FLOW, "Partial document buffer contains %d frames (%d per frame)\n", i, s->octets_per_ecm_frame);
s->ecm_at_page_end = true;
return i;
}
/*endif*/
s->ecm_len[i] = (int16_t) (4 + len);
}
/*endfor*/
/* We filled the entire buffer */
s->ecm_frames = 256;
span_log(&s->logging, SPAN_LOG_FLOW, "Partial page buffer full (%d per frame)\n", s->octets_per_ecm_frame);
s->ecm_at_page_end = (t4_tx_image_complete(&s->t4.tx) == SIG_STATUS_END_OF_DATA);
return 256;
}
/*- End of function --------------------------------------------------------*/
static int send_next_ecm_frame(t30_state_t *s)
{
int i;
uint8_t frame[3];
if (s->ecm_current_tx_frame < s->ecm_frames)
{
/* Search for the next frame, within the current partial page, which has
not been tagged as transferred OK. */
for (i = s->ecm_current_tx_frame; i < s->ecm_frames; i++)
{
if (s->ecm_len[i] >= 0)
{
send_frame(s, s->ecm_data[i], s->ecm_len[i]);
s->ecm_current_tx_frame = i + 1;
s->ecm_frames_this_tx_burst++;
return 0;
}
/*endif*/
}
/*endfor*/
s->ecm_current_tx_frame = s->ecm_frames;
}
/*endif*/
if (s->ecm_current_tx_frame < s->ecm_frames + 3)
{
/* We have sent all the FCD frames. Send three RCP frames, as per
T.4/A.1 and T.4/A.2. The repeats are to minimise the risk of a bit
error stopping the receiving end from recognising the RCP. */
s->ecm_current_tx_frame++;
/* The RCP frame is an odd man out, as its a simple 1 byte control
frame, but is specified to not have the final bit set. It doesn't
seem to have the DIS received bit set, either. */
frame[0] = ADDRESS_FIELD;
frame[1] = CONTROL_FIELD_NON_FINAL_FRAME;
frame[2] = T4_RCP;
send_frame(s, frame, 3);
/* In case we are just after a CTC/CTR exchange, which kicked us back
to long training */
s->short_train = true;
return 0;
}
/*endif*/
return -1;
}
/*- End of function --------------------------------------------------------*/
static void send_rr(t30_state_t *s)
{
if (s->current_status != T30_ERR_TX_T5EXP)
send_simple_frame(s, T30_RR);
else
send_dcn(s);
/*endif*/
}
/*- End of function --------------------------------------------------------*/
static int send_first_ecm_frame(t30_state_t *s)
{
s->ecm_current_tx_frame = 0;
s->ecm_frames_this_tx_burst = 0;
return send_next_ecm_frame(s);
}
/*- End of function --------------------------------------------------------*/
static void print_frame(t30_state_t *s, const char *io, const uint8_t *msg, int len)
{
span_log(&s->logging,
SPAN_LOG_FLOW,
"%s %s with%s final frame tag\n",
io,
t30_frametype(msg[2]),
(msg[1] & 0x10) ? "" : "out");
span_log_buf(&s->logging, SPAN_LOG_FLOW, io, msg, len);
}
/*- End of function --------------------------------------------------------*/
static void shut_down_hdlc_tx(t30_state_t *s)
{
if (s->send_hdlc_handler)
s->send_hdlc_handler(s->send_hdlc_user_data, NULL, 0);
/*endif*/
}
/*- End of function --------------------------------------------------------*/
static void send_frame(t30_state_t *s, const uint8_t *msg, int len)
{
print_frame(s, "Tx: ", msg, len);
if (s->real_time_frame_handler)
s->real_time_frame_handler(s->real_time_frame_user_data, false, msg, len);
/*endif*/
if (s->send_hdlc_handler)
s->send_hdlc_handler(s->send_hdlc_user_data, msg, len);
/*endif*/
}
/*- End of function --------------------------------------------------------*/
static void send_simple_frame(t30_state_t *s, int type)
{
uint8_t frame[3];
/* The simple command/response frames are always final frames */
frame[0] = ADDRESS_FIELD;
frame[1] = CONTROL_FIELD_FINAL_FRAME;
frame[2] = (uint8_t) (type | s->dis_received);
send_frame(s, frame, 3);
}
/*- End of function --------------------------------------------------------*/
static void send_20digit_msg_frame(t30_state_t *s, int cmd, char *msg)
{
size_t len;
int p;
uint8_t frame[23];
len = strlen(msg);
p = 0;
frame[p++] = ADDRESS_FIELD;