forked from T38Modem/t38modem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpmodeme.cxx
4636 lines (4139 loc) · 136 KB
/
pmodeme.cxx
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
/*
* pmodeme.cxx
*
* T38FAX Pseudo Modem
*
* Copyright (c) 2001-2011 Vyacheslav Frolov
*
* Open H323 Project
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Open H323 Library.
*
* The Initial Developer of the Original Code is Vyacheslav Frolov
*
* Contributor(s): Equivalence Pty ltd
*
* $Log: pmodeme.cxx,v $
* Revision 1.107 2011-01-19 17:17:54 vfrolov
* Extended AT#CIDFMT command for setting value format for NMBR tag
*
* Revision 1.107 2011/01/19 17:17:54 vfrolov
* Extended AT#CIDFMT command for setting value format for NMBR tag
*
* Revision 1.106 2011/01/14 20:32:11 vfrolov
* Added DATE, TIME and NAME tags to Caller ID report
* Added AT#CIDFMT command
*
* Revision 1.105 2011/01/12 12:23:43 vfrolov
* Replaced hardcoded workaround for mgetty-voice by conditional one
*
* Revision 1.104 2010/12/28 12:29:07 vfrolov
* Disabled echo in non-command state
*
* Revision 1.103 2010/10/12 16:46:25 vfrolov
* Implemented fake streams
*
* Revision 1.102 2010/10/08 12:31:03 vfrolov
* Optimized Mutex usage
*
* Revision 1.101 2010/10/08 06:06:39 vfrolov
* Added diagErrorMask
*
* Revision 1.100 2010/10/06 16:54:19 vfrolov
* Redesigned engine opening/closing
*
* Revision 1.99 2010/10/06 10:13:23 vfrolov
* Fixed previous fix
*
* Revision 1.98 2010/10/06 09:06:48 vfrolov
* Fixed crash at dialing reset (reported by gorod225)
*
* Revision 1.97 2010/09/29 11:52:59 vfrolov
* Redesigned engine attaching/detaching
*
* Revision 1.96 2010/09/22 15:51:13 vfrolov
* Moved ResetModemState() to EngineBase
*
* Revision 1.95 2010/09/14 06:35:10 vfrolov
* Implemented dial string terminated by a semicolon ("ATD<dialstring>;[...]")
*
* Revision 1.94 2010/09/10 18:08:07 vfrolov
* Implemented +VTD command
* Cleaned up code
*
* Revision 1.93 2010/09/10 05:34:12 vfrolov
* Allowed "+ A B C D" dialing digits
* Added ignoring unrecognized dialing digits (V.250)
* Added missing on-hook's
*
* Revision 1.92 2010/09/08 17:22:23 vfrolov
* Redesigned modem engine (continue)
*
* Revision 1.91 2010/07/09 04:46:55 vfrolov
* Implemented alternate route
*
* Revision 1.90 2010/07/08 05:11:34 vfrolov
* Redesigned modem engine (continue)
*
* Revision 1.89 2010/07/07 13:40:56 vfrolov
* Fixed ussue with call clearing in stSend state
* Added missing responce tracing
*
* Revision 1.88 2010/07/07 08:22:47 vfrolov
* Redesigned modem engine
*
* Revision 1.87 2010/03/23 08:58:14 vfrolov
* Fixed issues with +FTS and +FRS
*
* Revision 1.86 2010/03/18 08:42:17 vfrolov
* Added named tracing of data types
*
* Revision 1.85 2010/02/05 14:55:33 vfrolov
* Used S7 timeout
*
* Revision 1.84 2010/02/02 09:51:11 vfrolov
* Added missing timerRing.Stop()
*
* Revision 1.83 2010/02/02 08:41:56 vfrolov
* Implemented ringing indication for voice class dialing
*
* Revision 1.82 2010/01/28 10:30:37 vfrolov
* Added cleaning user input buffers for non-audio classes
*
* Revision 1.81 2010/01/27 14:03:38 vfrolov
* Added missing mutexes
*
* Revision 1.80 2010/01/22 14:11:40 vfrolov
* Added missing characters # and * for extension numbers
* Thanks to Dmitry
*
* Revision 1.79 2009/12/02 09:06:42 vfrolov
* Added a short delay after transmitting of signal before call clearing
*
* Revision 1.78 2009/11/20 16:37:27 vfrolov
* Fixed audio class application blocking by forced T.38 mode
*
* Revision 1.77 2009/11/19 11:18:16 vfrolov
* Added handling T.38 CED indication
*
* Revision 1.76 2009/11/18 19:08:47 vfrolov
* Moved common code to class EngineBase
*
* Revision 1.75 2009/11/17 11:25:48 vfrolov
* Added missing delay before sending fax-no-force requestmode
* Redesigned handling stConnectHandle state
*
* Revision 1.74 2009/11/06 10:02:29 vfrolov
* Fixed typo in fax-no-force
*
* Revision 1.73 2009/11/02 18:06:33 vfrolov
* Added fax-no-forse requestmode
*
* Revision 1.72 2009/10/27 18:25:22 vfrolov
* Added reseting send state on detaching engines
*
* Revision 1.71 2009/10/01 13:31:12 vfrolov
* Ported to OPAL SVN trunk
*
* Revision 1.70 2009/07/29 17:12:35 vfrolov
* Wait audioEngine on stConnectHandle
*
* Revision 1.69 2009/07/10 15:23:31 vfrolov
* Implicitly dial modifier '@' will continue of called number
*
* Revision 1.68 2009/07/10 14:04:13 vfrolov
* Changed usage multiple dial modifiers '@'
* each next '@' overrides previous '@'
* ("ATD4444@123@456" eq "ATD4444@456", "ATD4444@123@" eq "ATD4444")
* Dial modifiers (except 'D') can be used after '@'
* Dial modifiers 'T' and 'P' can be used instead 'D'
*
* Revision 1.67 2009/07/06 08:28:44 vfrolov
* Added DTMF shielding
*
* Revision 1.66 2009/07/03 16:38:17 vfrolov
* Added more state tracing
*
* Revision 1.65 2009/07/03 16:22:56 vfrolov
* Added missing pPlayTone deletings
* Added more state tracing
*
* Revision 1.64 2009/07/02 15:09:48 vfrolov
* Improved state tracing
*
* Revision 1.63 2009/07/02 06:55:30 vfrolov
* Fixed +VSM=? and +VLS=? for modem type autodetecting by VentaFax
* Thanks Dmitry (gorod225)
*
* Revision 1.62 2009/07/02 05:41:37 vfrolov
* Enabled +VIT > 0 (for compatibility with some voice applications)
*
* Revision 1.61 2009/07/01 15:11:58 vfrolov
* Fixed codec 128,"8-BIT LINEAR"
*
* Revision 1.60 2009/07/01 10:52:06 vfrolov
* Enabled +VSM=<cml> w/o <vsr>
*
* Revision 1.59 2009/07/01 08:20:39 vfrolov
* Implemented +VIP command
*
* Revision 1.58 2009/06/30 13:55:27 vfrolov
* Added +VSM codecs
* 128,"8-BIT LINEAR",8,0,(8000),(0),(0)
* 130,"UNSIGNED PCM",8,0,(8000),(0),(0)
* 131,"G.711 ULAW",8,0,(8000),(0),(0)
* Added +VLS
* 5,"ST",00000000,00000000,00000000
*
* Revision 1.57 2009/06/30 10:50:33 vfrolov
* Added +VSM codecs
* 0,"SIGNED PCM",8,0,(8000),(0),(0)
* 1,"UNSIGNED PCM",8,0,(8000),(0),(0)
*
* Revision 1.56 2009/06/29 15:36:38 vfrolov
* Added ability to dial in connection establised state
*
* Revision 1.55 2009/06/29 13:28:42 vfrolov
* Added +VSM codecs
* 4,"G711U",8,0,(8000),(0),(0)
* 5,"G711A",8,0,(8000),(0),(0)
*
* Revision 1.54 2009/06/25 16:48:52 vfrolov
* Added stub for +VSD command
*
* Revision 1.53 2009/06/25 12:46:38 vfrolov
* Implemented dialing followed answering ("ATD<dialstring>;A")
*
* Revision 1.52 2009/06/24 13:12:58 vfrolov
* Implemented +VEM and +VIT commands
*
* Revision 1.51 2009/06/24 12:48:37 vfrolov
* Added stubs for +VGR and +VGT commands
*
* Revision 1.50 2009/06/24 12:19:01 vfrolov
* Added stubs for +VRA and +VRN
*
* Revision 1.49 2009/06/24 08:04:46 vfrolov
* Added semicolon concatenating of commands
*
* Revision 1.48 2009/06/22 16:05:48 vfrolov
* Added ability to dial extension numbers
*
* Revision 1.47 2009/05/06 09:17:23 vfrolov
* Enabled dialing characters # and *
*
* Revision 1.46 2008/09/10 11:15:00 frolov
* Ported to OPAL SVN trunk
*
* Revision 1.45 2008/09/10 07:05:06 frolov
* Fixed doubled mutex lock
*
* Revision 1.44 2007/08/27 10:55:21 vfrolov
* Added missing moreFrames = FALSE
*
* Revision 1.43 2007/08/24 16:12:14 vfrolov
* Disabled CNG sending in voice class mode
*
* Revision 1.42 2007/05/04 09:58:57 vfrolov
* Fixed Attach(audioEngine)
*
* Revision 1.41 2007/04/24 16:26:02 vfrolov
* Fixed unexpected state change
*
* Revision 1.40 2007/04/09 08:07:12 vfrolov
* Added symbolic logging ModemCallbackParam
*
* Revision 1.39 2007/03/30 07:56:36 vfrolov
* Included g711.c
*
* Revision 1.38 2007/03/23 14:54:20 vfrolov
* Fixed compiler warnings
*
* Revision 1.37 2007/03/23 10:14:35 vfrolov
* Implemented voice mode functionality
*
* Revision 1.36 2007/03/22 16:26:04 vfrolov
* Fixed compiler warnings
*
* Revision 1.35 2007/02/22 16:00:33 vfrolov
* Implemented AT#HCLR command
*
* Revision 1.34 2006/12/11 11:19:48 vfrolov
* Fixed race condition with modem Callback
*
* Revision 1.33 2006/12/07 10:53:24 vfrolov
* Added OnParentStop()
*
* Revision 1.32 2006/12/01 13:35:25 vfrolov
* Fixed modem locking after unexpected dial while connection
*
* Revision 1.31 2005/11/22 16:39:36 vfrolov
* Fixed MSVC compile warning
*
* Revision 1.30 2005/03/05 15:42:39 vfrolov
* Added missing check for PTRACING
* Fixed typo in T38DLE trace
*
* Revision 1.29 2005/03/04 16:35:38 vfrolov
* Implemented AT#DFRMC command
* Redisigned class Profile
*
* Revision 1.28 2005/02/16 12:14:47 vfrolov
* Send CONNECT just before data for AT+FRM command
*
* Revision 1.27 2005/02/10 10:35:18 vfrolov
* Fixed AT prefix searching
*
* Revision 1.26 2005/02/03 11:32:11 vfrolov
* Fixed MSVC compile warnings
*
* Revision 1.25 2005/02/01 11:43:46 vfrolov
* Implemented ATV0 command (numeric format for result codes)
* Implemented AT+FMI?, AT+FMM? and AT+FMR? commands
* Added stubs for ATBn, ATX3 and AT+FCLASS=0 commands
* Added stub for AT+FLO command
*
* Revision 1.24 2004/10/27 13:36:26 vfrolov
* Decreased binary, DLE, and callback tracing
*
* Revision 1.23 2004/07/06 16:07:24 vfrolov
* Included ptlib.h for precompiling
*
* Revision 1.22 2004/06/24 17:20:22 vfrolov
* Added stub for ATXn command
*
* Revision 1.21 2004/05/09 07:46:11 csoutheren
* Updated to compile with new PIsDescendant function
*
* Revision 1.20 2004/03/01 17:14:34 vfrolov
* Fixed binary log in command mode
*
* Revision 1.19 2003/12/04 16:09:51 vfrolov
* Implemented FCS generation
* Implemented ECM support
*
* Revision 1.18 2003/01/08 16:58:58 vfrolov
* Added cbpOutBufNoFull and isOutBufFull()
*
* Revision 1.17 2002/12/30 12:49:29 vfrolov
* Added tracing thread's CPU usage (Linux only)
*
* Revision 1.16 2002/12/20 10:12:50 vfrolov
* Implemented tracing with PID of thread (for LinuxThreads)
* or ID of thread (for other POSIX Threads)
*
* Revision 1.15 2002/12/19 10:31:33 vfrolov
* Changed usage multiple dial modifiers 'L' (for secure reasons)
* each next 'L' overrides previous 'L'
* ("ATD4444L123L456" eq "ATD4444L456", "ATD4444L123L" eq "ATD4444")
* Added dial modifier 'D' - continue dial number
* ("ATD000L123D4444" eq "ATD0004444L123")
* Added mising spaces into "NMBR = " and "NDID = "
*
* Revision 1.14 2002/11/05 13:59:11 vfrolov
* Implemented Local Party Number dial modifier 'L'
* (put dial string 1234L5678 to dial 1234 from 5678)
*
* Revision 1.13 2002/05/15 16:10:52 vfrolov
* Reimplemented AT+FTS and AT+FRS
* Added workaround "Reset state stSendAckWait"
*
* Revision 1.12 2002/05/07 10:36:16 vfrolov
* Added more code for case stResetHandle
* Changed duration of CED (T.30 requires 2.6...4.0 secs)
*
* Revision 1.11 2002/04/19 14:06:04 vfrolov
* Implemented T.38 mode request dial modifiers
* F - enable
* V - disable
*
* Revision 1.10 2002/04/03 02:45:36 vfrolov
* Implemented AT#CID=10 - ANI/DNIS reporting between RINGs
*
* Revision 1.9 2002/03/01 14:59:48 vfrolov
* Get data for Revision string from version.h
*
* Revision 1.8 2002/03/01 10:00:26 vfrolov
* Added Copyright header
* Implemented connection established handling and mode change request
* Implemented ATI8 command
* Fixed some deadlocks
* Added some other changes
*
* Revision 1.7 2002/02/11 08:40:15 vfrolov
* More clear call implementation
*
* Revision 1.6 2002/01/10 06:10:02 craigs
* Added MPL header
*
* Revision 1.5 2002/01/06 03:48:45 craigs
* Added changes to support efax 0.9
* Thanks to Vyacheslav Frolov
*
* Revision 1.4 2002/01/03 21:36:00 craigs
* Added change to use S1 register for number of rings on answer
* Thanks to Vyacheslav Frolov
*
* Revision 1.3 2002/01/02 04:49:37 craigs
* Added support for ATS0 register
*
* Revision 1.2 2002/01/01 23:59:52 craigs
* Lots of additional implementation thanks to Vyacheslav Frolov
*
*/
#include <ptlib.h>
#include <ptclib/dtmf.h>
#include "pmodemi.h"
#include "pmodeme.h"
#include "dle.h"
#include "fcs.h"
#include "t38engine.h"
#include "version.h"
///////////////////////////////////////////////////////////////
static const char Manufacturer[] = "Frolov,Holtschneider,Davidson";
static const char Model[] = "T38FAX";
#define _TOSTR(s) #s
#define TOSTR(s) _TOSTR(s)
static const char Revision[] = TOSTR(MAJOR_VERSION) "." TOSTR(MINOR_VERSION) "." TOSTR(BUILD_NUMBER);
///////////////////////////////////////////////////////////////
#define DeclareStringParam(name) \
public: \
void name(const PString &_s##name) { s##name = _s##name; } \
const PString &name() const { return s##name; } \
protected: \
PString s##name;
///////////////////////////////////////////////////////////////
class Profile
{
protected:
#define PROFILE_SIZE_STD 40
#define PROFILE_SIZE_EXT 10
#define PROFILE_SIZE_VOICE 10
#define PROFILE_SIZE (PROFILE_SIZE_STD + PROFILE_SIZE_EXT + PROFILE_SIZE_VOICE)
enum {
MinRegStd = 0,
MaxRegStd = MinRegStd + PROFILE_SIZE_STD - 1,
MinRegExt = MaxRegStd + 1,
MaxRegExt = MinRegExt + PROFILE_SIZE_EXT - 1,
MinRegVoice = MaxRegExt + 1,
MaxRegVoice = MinRegVoice + PROFILE_SIZE_VOICE - 1,
MaxBit = 7,
};
public:
Profile();
#define DeclareRegisterBit(name, byte, bit) \
void name(PBoolean val) { SetBit(byte, bit, val); } \
PBoolean name() const { PBoolean val; GetBit(byte, bit, val); return val; }
DeclareRegisterBit(Echo, 23, 0);
DeclareRegisterBit(asciiResultCodes, 23, 6);
DeclareRegisterBit(noResultCodes, 23, 7);
#define DeclareRegisterByte(name, byte) \
void name(BYTE val) { SetReg(byte, val); } \
BYTE name() const { BYTE val; GetReg(byte, val); return val; }
DeclareRegisterByte(AutoAnswer, 0);
DeclareRegisterByte(RingCount, 1);
DeclareRegisterByte(S7, 7);
DeclareRegisterByte(DialTimeComma, 8);
DeclareRegisterByte(DialTimeDTMF, 11);
DeclareRegisterByte(IfcByDCE, MinRegExt + 0);
DeclareRegisterByte(IfcByDTE, MinRegExt + 1);
DeclareRegisterByte(ClearMode, MinRegExt + 2);
DeclareRegisterByte(DelayFrmConnect, MinRegExt + 3);
DeclareRegisterByte(DidMode, MinRegExt + 4);
DeclareRegisterByte(CidMode, MinRegExt + 5);
#if 5 >= PROFILE_SIZE_EXT
#error *** The PROFILE_SIZE_EXT is too small to declare register ***
#endif
DeclareRegisterByte(Vtd, MinRegVoice + 0);
DeclareRegisterByte(Vcml, MinRegVoice + 1);
DeclareRegisterByte(Vsds, MinRegVoice + 2);
DeclareRegisterByte(Vsdi, MinRegVoice + 3);
DeclareRegisterByte(VgrInterval, MinRegVoice + 4);
DeclareRegisterByte(VgtInterval, MinRegVoice + 5);
DeclareRegisterByte(VraInterval, MinRegVoice + 6);
DeclareRegisterByte(VrnInterval, MinRegVoice + 7);
DeclareRegisterByte(CidNameFmt, MinRegVoice + 8);
DeclareRegisterByte(CidNmbrFmt, MinRegVoice + 9);
#if 9 >= PROFILE_SIZE_VOICE
#error *** The PROFILE_SIZE_VOICE is too small to declare register ***
#endif
void Flo(BYTE val) { IfcByDTE(val); IfcByDCE(val); }
BYTE Flo() const {
if (IfcByDTE() == IfcByDCE())
return IfcByDTE();
return 255;
}
PBoolean SetBit(PINDEX r, PINDEX b, PBoolean val) {
if( !ChkRB(r, b) ) return FALSE;
BYTE msk = MaskB(b);
if( val ) S[r] |= msk;
else S[r] &= ~msk;
return TRUE;
}
PBoolean GetBit(PINDEX r, PINDEX b, PBoolean &val) const {
if (!ChkRB(r, b)) {
val = 0;
return FALSE;
}
BYTE msk = MaskB(b);
val = (S[r] & msk) ? TRUE : FALSE;
return TRUE;
}
PBoolean SetReg(PINDEX r, BYTE val) {
if( !ChkR(r) ) return FALSE;
S[r] = val;
return TRUE;
}
PBoolean GetReg(PINDEX r, BYTE &val) const {
if (!ChkR(r)) {
val = 0;
return FALSE;
}
val = S[r];
return TRUE;
}
PBoolean SetBits(PINDEX r, PINDEX bl, PINDEX bh, BYTE val) {
if( !ChkRBB(r, bl, bh) ) return FALSE;
BYTE msk = MaskBB(bl, bh);
S[r] &= ~msk;
S[r] &= (val << bl) & msk;
return TRUE;
}
PBoolean GetBits(PINDEX r, PINDEX bl, PINDEX bh, BYTE &val) const {
if (!ChkRBB(r, bl, bh)) {
val = 0;
return FALSE;
}
BYTE msk = MaskBB(bl, bh);
val = BYTE((S[r] & msk) >> bl);
return TRUE;
}
Profile &operator=(const Profile &p);
Profile &SetVoiceProfile(const Profile &p);
void ModemClass(const PString &_modemClass) {
modemClass = _modemClass;
if (modemClass == "1") {
modemClassId = EngineBase::mcFax;
}
#ifdef AUD
else
if (modemClass == "8") {
modemClassId = EngineBase::mcAudio;
}
#endif
else {
modemClassId = EngineBase::mcUndefined;
}
}
const PString &ModemClass() const { return modemClass; }
EngineBase::ModemClass ModemClassId() const { return modemClassId; }
protected:
static PBoolean ChkR(PINDEX r) {
return r < PROFILE_SIZE;
}
static PBoolean ChkB(PINDEX b) {
return b <= MaxBit;
}
static PBoolean ChkRB(PINDEX r, PINDEX b) {
return ChkR(r) && ChkB(b);
}
static PBoolean ChkRBB(PINDEX r, PINDEX bl, PINDEX bh) {
return ChkR(r) && ChkB(bl) && ChkB(bh) && bl <= bh;
}
static BYTE MaskB(PINDEX b) {
return "\x01\x02\x04\x08\x10\x20\x40\x80"[b];
}
static BYTE MaskBB(PINDEX bl, PINDEX bh) { // bl <= bh
return BYTE(("\x01\x03\x07\x0F\x1F\x3F\x7F\xFF"[bh - bl]) << bl);
}
BYTE S[PROFILE_SIZE]; // S-registers
PString modemClass;
EngineBase::ModemClass modemClassId;
#undef PROFILE_SIZE
#undef PROFILE_SIZE_VOICE
#undef PROFILE_SIZE_EXT
#undef PROFILE_SIZE_STD
};
static const Profile Profiles[1];
///////////////////////////////////////////////////////////////
class Timeout : public PTimer
{
PCLASSINFO(Timeout, PTimer);
public:
Timeout(const PNotifier &callback, PBoolean _continuous = FALSE)
: state(0), continuous(_continuous) {
SetNotifier(callback);
}
void Start(unsigned period) {
PWaitAndSignal mutexWait(Mutex);
state = 1;
if( continuous ) {
RunContinuous(period);
OnTimeout();
} else {
PTimer::operator=(period);
}
}
void Stop() {
PWaitAndSignal mutexWait(Mutex);
state = 0;
PTimer::operator=(0);
}
PBoolean Get() {
PWaitAndSignal mutexWait(Mutex);
if (state == 2) {
state = continuous ? 1 : 0;
return TRUE;
}
return FALSE;
}
protected:
void OnTimeout() {
PWaitAndSignal mutexWait(Mutex);
if( state == 1 )
state = 2;
PTimer::OnTimeout();
}
int state;
PBoolean continuous;
PMutex Mutex;
};
///////////////////////////////////////////////////////////////
enum CallState { // ---------+---------------------------+--------+------------------------------------------------+
// | on sending command | on | on received command |
// +---------------------------+clearing+------------------------------------------------+
// |"dial"|"answer"|"clearcall"|off_hook|"call"|"alerting"|"established"| "clearcall" |
// ---------+------+--------+-----+-----+--------+------+----------+-------------+----------+-----+
// off_hook | true | true |true |false| true |false | true | true | true |false|
// ---------+------+--------+-----+-----+--------+------+----------+-------------+----------+-----+
cstCleared, // )--+ <--+ <--+ )--+ <--+ <--+ |
// | | | | | | |
// | | | | | | |
cstDialing, // <--+ )--+ )--+ )--+ | )--+ )--+ )--+ )--+ )--+ |
// | | | | | | | | |
cstAlerted, // )--+ )--+ )--+ | <--+ )--+ )--+ )--+ |
// | | | | | | | |
// | | | | | | | |
cstCalled, // )--+ )--+ )--+ | <--+ | | )--+ |
// | | | | | | | |
cstAnswering, // <--+ )--+ )--+ )--+ )--+ )--+ )--+ |
// | | | | | | |
// | | | | | | |
cstEstablished, // )--+ )--+ )--+ <--+ )--+ )--+ |
// | | | | | |
cstReleasing, // <--+ )--+ )--+ <--+ )--+ |
}; // ---------+------+--------+-----+-----+--------+------+----------+-------------+----------+-----+
#if PTRACING
static ostream & operator<<(ostream & out, CallState state)
{
switch (state) {
case cstCleared: return out << "cstCleared";
case cstDialing: return out << "cstDialing";
case cstAlerted: return out << "cstAlerted";
case cstCalled: return out << "cstCalled";
case cstAnswering: return out << "cstAnswering";
case cstEstablished: return out << "cstEstablished";
case cstReleasing: return out << "cstReleasing";
}
return out << "cst" << INT(state);
}
#endif
///////////////////////////////////////////////////////////////
#if PTRACING
struct CallStateAndSubState {
CallStateAndSubState(CallState s, int ss) : state(s), subState(ss) {}
CallState state;
int subState;
};
static ostream & operator<<(ostream & out, const CallStateAndSubState &stateAndSubState)
{
out << stateAndSubState.state;
if (stateAndSubState.state == cstReleasing)
out << "." << stateAndSubState.subState;
return out;
}
#endif
///////////////////////////////////////////////////////////////
enum State {
stCommand,
stDial,
stConnectWait,
stConnectHandle,
stReqModeAckWait,
stReqModeAckHandle,
stSend,
stSendBufEmptyHandle,
stSendAckWait,
stSendAckHandle,
stRecvBegWait,
stRecvBegHandle,
stRecv,
};
#if PTRACING
static ostream & operator<<(ostream & out, State state)
{
switch (state) {
case stCommand: return out << "stCommand";
case stDial: return out << "stDial";
case stConnectWait: return out << "stConnectWait";
case stConnectHandle: return out << "stConnectHandle";
case stReqModeAckWait: return out << "stReqModeAckWait";
case stReqModeAckHandle: return out << "stReqModeAckHandle";
case stSend: return out << "stSend";
case stSendBufEmptyHandle: return out << "stSendBufEmptyHandle";
case stSendAckWait: return out << "stSendAckWait";
case stSendAckHandle: return out << "stSendAckHandle";
case stRecvBegWait: return out << "stRecvBegWait";
case stRecvBegHandle: return out << "stRecvBegHandle";
case stRecv: return out << "stRecv";
}
return out << "st" << INT(state);
}
#endif
///////////////////////////////////////////////////////////////
enum SubStateConnectHandle {
chConnected,
#ifdef AUD
chWaitAudioEngine,
chAudioEngineAttached,
chWaitPlayTone,
chTonePlayed,
#endif
chConnectionEstablishDelay,
chConnectionEstablished,
};
#if PTRACING
static ostream & operator<<(ostream & out, SubStateConnectHandle subState)
{
switch (subState) {
case chConnected: return out << "chConnected";
#ifdef AUD
case chWaitAudioEngine: return out << "chWaitAudioEngine";
case chAudioEngineAttached: return out << "chAudioEngineAttached";
case chWaitPlayTone: return out << "chWaitPlayTone";
case chTonePlayed: return out << "chTonePlayed";
#endif
case chConnectionEstablishDelay: return out << "chConnectionEstablishDelay";
case chConnectionEstablished: return out << "chConnectionEstablished";
}
return out << "ch" << INT(subState);
}
#endif
///////////////////////////////////////////////////////////////
#if PTRACING
struct StateAndSubState {
StateAndSubState(State s, int ss) : state(s), subState(ss) {}
State state;
int subState;
};
static ostream & operator<<(ostream & out, const StateAndSubState &stateAndSubState)
{
out << stateAndSubState.state;
if (stateAndSubState.state == stConnectHandle)
out << "." << SubStateConnectHandle(stateAndSubState.subState);
return out;
}
#endif
///////////////////////////////////////////////////////////////
enum ModemClassEngine {
#ifdef AUD
mceAudio,
#endif
mceT38,
mceNumberOfItems,
};
#if PTRACING
static ostream & operator<<(ostream & out, ModemClassEngine mce)
{
switch (mce) {
#ifdef AUD
case mceAudio: return out << "mceAudio";
#endif
case mceT38: return out << "mceT38";
default: break;
}
return out << "mce" << INT(mce);
}
#endif
///////////////////////////////////////////////////////////////
#define DeclareResultCode(name, v0, v1) \
PString name() const { return P.asciiResultCodes() ? (v1) : (v0); }
///////////////////////////////////////////////////////////////
class ModemEngineBody : public PObject
{
PCLASSINFO(ModemEngineBody, PObject);
public:
/**@name Construction */
//@{
ModemEngineBody(ModemEngine &_parent, const PNotifier &_callbackEndPoint);
~ModemEngineBody();
//@}
/**@name Operations */
//@{
PBoolean Request(PStringToString &request);
EngineBase *NewPtrEngine(ModemClassEngine mce);
void OnParentStop();
void HandleData(const PBYTEArray &buf, PBYTEArray &bresp);
void CheckState(PBYTEArray &bresp);
void CheckStatePost();
PBoolean IsReady() const {
PWaitAndSignal mutexWait(Mutex);
return state == stCommand && !off_hook && callState == cstCleared && (PTime() - lastOnHookActivity) > 5*1000;
}
PBoolean isOutBufFull() const {
PWaitAndSignal mutexWait(Mutex);
return currentClassEngine && currentClassEngine->isOutBufFull();
}
//@}
protected:
PBoolean Echo() const { return P.Echo(); }
PBoolean HandleClass1Cmd(const char **ppCmd, PString &resp, PBoolean &ok, PBoolean &crlf);
PBoolean HandleClass8Cmd(const char **ppCmd, PString &resp, PBoolean &ok, PBoolean &crlf);
PBoolean Answer();
void HandleCmd(PString &resp);
void HandleCmdRest(PString &resp);
unsigned EstablishmentTimeout() const {
return unsigned(P.S7() ? P.S7() : Profiles[0].S7()) * 1000;
}
void ResetDleData() {
dleData.Clean();
dataCount = 0;
moreFrames = FALSE;
}
PBoolean SetBitRevDleData() {
switch (P.ModemClassId()) {
#ifdef AUD
case EngineBase::mcAudio:
dleData.BitRev(
#ifdef ALAW_132_BIT_REVERSE
P.Vcml() == 132 ? TRUE :
#endif
FALSE);
break;
#endif
case EngineBase::mcFax:
dleData.BitRev(TRUE);
break;
default:
return FALSE;
}
return TRUE;
}
PBoolean SendSilence(int ms) {
if (P.ModemClassId() == EngineBase::mcFax && currentClassEngine) {
dataType = EngineBase::dtSilence;
SetState(stSend);
if (currentClassEngine->SendStart(dataType, ms)) {
SetState(stSendAckWait);
if (currentClassEngine->SendStop(FALSE, NextSeq()))
return TRUE;
}
}
SetState(stCommand);
return FALSE;
}
PBoolean _SendStart(EngineBase::DataType dt, int br, PString &resp) {
dataType = dt;
ResetDleData();
SetBitRevDleData();
SetState(stSend);
if (!currentClassEngine || !currentClassEngine->SendStart(dataType, br)) {
SetState(stCommand);
return FALSE;
}
resp = RC_CONNECT();
return TRUE;
}
PBoolean SendStart(EngineBase::DataType dt, int br, PString &resp) {
PWaitAndSignal mutexWait(Mutex);
return _SendStart(dt, br, resp);
}
PBoolean RecvStart(EngineBase::DataType dt, int br) {
PBoolean done = FALSE;
PWaitAndSignal mutexWait(Mutex);
dataType = dt;
SetBitRevDleData();
SetState(stRecvBegWait);
timeout.Start(60000);
if (!currentClassEngine || !currentClassEngine->RecvWait(dataType, br, NextSeq(), done)) {
SetState(stCommand);
timeout.Stop();
return FALSE;
}
if (done) {
SetState(stRecvBegHandle);
timeout.Stop();
parent.SignalDataReady();
}
return TRUE;
}
void _AttachEngine(ModemClassEngine mce);
void _DetachEngine(ModemClassEngine mce);
void _ClearCall();
int NextSeq() { ++seq; return seq &= EngineBase::cbpUserDataMask; }
ModemEngine &parent;
EngineBase *activeEngines[mceNumberOfItems];
EngineBase *currentClassEngine;
PBoolean enableFakeIn[mceNumberOfItems];
PBoolean enableFakeOut[mceNumberOfItems];
const PNotifier callbackEndPoint;
PDECLARE_NOTIFIER(PObject, ModemEngineBody, OnEngineCallback);
PDECLARE_NOTIFIER(PObject, ModemEngineBody, OnTimerCallback);
const PNotifier engineCallback;
const PNotifier timerCallback;
Timeout timerRing;
Timeout timerBusy;
Timeout timeout;
PTime lastOnHookActivity;
int seq;
PBoolean forceFaxMode;
PBoolean connectionEstablished;
PBoolean off_hook;
int lockReleasingState;
enum CallDirection {
cdUndefined,
cdOutgoing,
cdIncoming,
};
CallDirection callDirection;
CallState callState;
int callSubState;
State state;
int subState;
#define TRACE_STATE(level, header) \
myPTRACE(level, "T38Modem\t" << header \
" " << (off_hook ? "off" : "on") << "-hook" \
" " << CallStateAndSubState(callState, callSubState) << \
" " << StateAndSubState(state, subState))
PBoolean OffHook() {
if (!off_hook) {
off_hook = TRUE;
TRACE_STATE(4, "ModemEngineBody::OffHook:");