-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
mod_spandsp_fax.c
2567 lines (2096 loc) · 85.2 KB
/
mod_spandsp_fax.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
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (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 FreeSWITCH mod_fax.
*
* The Initial Developer of the Original Code is
* Massimo Cetra <devel@navynet.it>
*
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Brian West <brian@freeswitch.org>
* Anthony Minessale II <anthm@freeswitch.org>
* Steve Underwood <steveu@coppice.org>
* Antonio Gallo <agx@linux.it>
* mod_spandsp_fax.c -- Fax applications provided by SpanDSP
*
*/
#include "mod_spandsp.h"
#include "udptl.h"
#define LOCAL_FAX_MAX_DATAGRAM 400
#define MAX_FEC_ENTRIES 4
#define MAX_FEC_SPAN 4
#define DEFAULT_FEC_ENTRIES 3
#define DEFAULT_FEC_SPAN 3
/*****************************************************************************
OUR DEFINES AND STRUCTS
*****************************************************************************/
typedef enum {
T38_MODE,
AUDIO_MODE,
T38_GATEWAY_MODE
} transport_mode_t;
typedef enum {
T38_MODE_UNKNOWN = 0,
T38_MODE_NEGOTIATED = 1,
T38_MODE_REQUESTED = 2,
T38_MODE_REFUSED = -1,
} t38_mode_t;
const char * get_t38_status(t38_mode_t mode)
{
const char *str = "off";
switch(mode) {
case T38_MODE_NEGOTIATED:
str = "negotiated";
break;
case T38_MODE_REQUESTED:
str = "requested";
break;
case T38_MODE_REFUSED:
str = "refused";
break;
default:
break;
}
return str;
}
struct pvt_s {
switch_core_session_t *session;
mod_spandsp_fax_application_mode_t app_mode;
t30_state_t *t30;
fax_state_t *fax_state;
t38_terminal_state_t *t38_state;
t38_gateway_state_t *t38_gateway_state;
t38_core_state_t *t38_core;
switch_mutex_t *mutex;
udptl_state_t *udptl_state;
char *filename;
char *ident;
char *header;
char *timezone;
int use_ecm;
int disable_v17;
int enable_tep;
int enable_colour_fax;
int enable_image_resizing;
int enable_colour_to_bilevel;
int enable_grayscale_to_bilevel;
int verbose;
switch_log_level_t verbose_log_level;
FILE *trace_file;
int caller;
int tx_page_start;
int tx_page_end;
int done;
t38_mode_t t38_mode;
struct pvt_s *next;
};
typedef struct pvt_s pvt_t;
static void launch_timer_thread(void);
static struct {
pvt_t *head;
switch_mutex_t *mutex;
switch_thread_t *thread;
int thread_running;
} t38_state_list;
static void wake_thread(int force)
{
if (force) {
switch_thread_cond_signal(spandsp_globals.cond);
return;
}
if (switch_mutex_trylock(spandsp_globals.cond_mutex) == SWITCH_STATUS_SUCCESS) {
switch_thread_cond_signal(spandsp_globals.cond);
switch_mutex_unlock(spandsp_globals.cond_mutex);
}
}
static int add_pvt(pvt_t *pvt)
{
int r = 0;
if (t38_state_list.thread_running == 1) {
switch_mutex_lock(t38_state_list.mutex);
pvt->next = t38_state_list.head;
t38_state_list.head = pvt;
switch_mutex_unlock(t38_state_list.mutex);
r = 1;
wake_thread(0);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error launching thread\n");
}
return r;
}
static int del_pvt(pvt_t *del_pvt)
{
pvt_t *p, *l = NULL;
int r = 0;
switch_mutex_lock(t38_state_list.mutex);
for (p = t38_state_list.head; p; p = p->next) {
if (p == del_pvt) {
if (l) {
l->next = p->next;
} else {
t38_state_list.head = p->next;
}
p->next = NULL;
r = 1;
break;
}
l = p;
}
switch_mutex_unlock(t38_state_list.mutex);
wake_thread(0);
return r;
}
static void *SWITCH_THREAD_FUNC timer_thread_run(switch_thread_t *thread, void *obj)
{
switch_timer_t timer = { 0 };
pvt_t *pvt;
int samples = 160;
int ms = 20;
if (switch_core_timer_init(&timer, "soft", ms, samples, NULL) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "timer init failed.\n");
t38_state_list.thread_running = -1;
goto end;
}
t38_state_list.thread_running = 1;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "FAX timer thread started.\n");
switch_mutex_lock(spandsp_globals.cond_mutex);
while(t38_state_list.thread_running == 1) {
switch_mutex_lock(t38_state_list.mutex);
if (!t38_state_list.head) {
switch_mutex_unlock(t38_state_list.mutex);
switch_thread_cond_wait(spandsp_globals.cond, spandsp_globals.cond_mutex);
switch_core_timer_sync(&timer);
continue;
}
for (pvt = t38_state_list.head; pvt; pvt = pvt->next) {
if (pvt->udptl_state && pvt->session && switch_channel_ready(switch_core_session_get_channel(pvt->session))) {
switch_mutex_lock(pvt->mutex);
t38_terminal_send_timeout(pvt->t38_state, samples);
switch_mutex_unlock(pvt->mutex);
}
}
switch_mutex_unlock(t38_state_list.mutex);
switch_core_timer_next(&timer);
}
switch_mutex_unlock(spandsp_globals.cond_mutex);
end:
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "FAX timer thread ended.\n");
if (timer.timer_interface) {
switch_core_timer_destroy(&timer);
}
return NULL;
}
static void launch_timer_thread(void)
{
switch_threadattr_t *thd_attr = NULL;
switch_threadattr_create(&thd_attr, spandsp_globals.pool);
switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
switch_thread_create(&t38_state_list.thread, thd_attr, timer_thread_run, NULL, spandsp_globals.pool);
}
/*****************************************************************************
LOGGING AND HELPER FUNCTIONS
*****************************************************************************/
static void counter_increment(void)
{
switch_mutex_lock(spandsp_globals.mutex);
spandsp_globals.total_sessions++;
switch_mutex_unlock(spandsp_globals.mutex);
}
void mod_spandsp_log_message(void *user_data, int level, const char *msg)
{
int fs_log_level;
mod_spandsp_log_data_t *log_data = (mod_spandsp_log_data_t *)user_data;
switch_core_session_t *session = log_data ? log_data->session : NULL;
switch_log_level_t verbose_log_level = log_data ? log_data->verbose_log_level : spandsp_globals.verbose_log_level;
switch (level) {
case SPAN_LOG_NONE:
return;
case SPAN_LOG_ERROR:
case SPAN_LOG_PROTOCOL_ERROR:
fs_log_level = SWITCH_LOG_ERROR;
break;
case SPAN_LOG_WARNING:
case SPAN_LOG_PROTOCOL_WARNING:
fs_log_level = SWITCH_LOG_WARNING;
break;
case SPAN_LOG_FLOW:
case SPAN_LOG_FLOW_2:
case SPAN_LOG_FLOW_3:
default: /* SPAN_LOG_DEBUG, SPAN_LOG_DEBUG_2, SPAN_LOG_DEBUG_3 */
fs_log_level = verbose_log_level;
break;
}
if (!zstr(msg)) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), fs_log_level, "%s", msg);
if (log_data && log_data->trace_file) {
fwrite(msg, strlen(msg) * sizeof(const char), 1, log_data->trace_file);
}
}
}
static int phase_b_handler(void *user_data, int result)
{
t30_stats_t t30_stats;
switch_core_session_t *session;
switch_channel_t *channel;
const char *local_ident;
const char *far_ident;
char *fax_transfer_rate = NULL;
char *fax_document_total_pages = NULL;
pvt_t *pvt;
switch_event_t *event;
const char *total_pages_str = "";
pvt = (pvt_t *) user_data;
switch_assert(pvt);
session = pvt->session;
switch_assert(session);
channel = switch_core_session_get_channel(session);
switch_assert(channel);
t30_get_transfer_statistics(pvt->t30, &t30_stats);
local_ident = switch_str_nil(t30_get_tx_ident(pvt->t30));
far_ident = switch_str_nil(t30_get_rx_ident(pvt->t30));
fax_transfer_rate = switch_core_session_sprintf(session, "%i", t30_stats.bit_rate);
if (fax_transfer_rate) {
switch_channel_set_variable(channel, "fax_transfer_rate", fax_transfer_rate);
}
if (pvt->app_mode == FUNCTION_TX) {
fax_document_total_pages = switch_core_session_sprintf(session, "%i", t30_stats.pages_in_file);
if (fax_document_total_pages) {
switch_channel_set_variable(channel, "fax_document_total_pages", fax_document_total_pages);
}
}
switch_channel_set_variable(channel, "fax_ecm_used", (t30_stats.error_correcting_mode) ? "on" : "off");
switch_channel_set_variable(channel, "fax_t38_status", get_t38_status(pvt->t38_mode));
switch_channel_set_variable(channel, "fax_local_station_id", local_ident);
switch_channel_set_variable(channel, "fax_remote_station_id", far_ident);
switch_channel_set_variable(channel, "fax_remote_country", switch_str_nil(t30_get_rx_country(pvt->t30)));
switch_channel_set_variable(channel, "fax_remote_vendor", switch_str_nil(t30_get_rx_vendor(pvt->t30)));
switch_channel_set_variable(channel, "fax_remote_model", switch_str_nil(t30_get_rx_model(pvt->t30)));
if (pvt->app_mode == FUNCTION_TX) {
total_pages_str = switch_core_session_sprintf(session, "Total fax pages: %s\n", fax_document_total_pages);
}
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO,
"=== Negotiation Result =======================================================\n"
"Remote station id: %s\n"
"Local station id: %s\n"
"Transfer Rate: %i\n"
"ECM status %s\n"
"T38 status %s\n"
"remote country: %s\n"
"remote vendor: %s\n"
"remote model: %s\n"
"%s"
"==============================================================================\n",
far_ident,
local_ident,
t30_stats.bit_rate,
(t30_stats.error_correcting_mode) ? "on" : "off",
get_t38_status(pvt->t38_mode),
switch_str_nil(t30_get_rx_country(pvt->t30)),
switch_str_nil(t30_get_rx_vendor(pvt->t30)),
switch_str_nil(t30_get_rx_model(pvt->t30)),
total_pages_str);
switch_channel_execute_on(channel, "execute_on_fax_phase_b");
/* Fire event */
if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, pvt->app_mode == FUNCTION_TX ? SPANDSP_EVENT_TXFAXNEGOCIATERESULT : SPANDSP_EVENT_RXFAXNEGOCIATERESULT) == SWITCH_STATUS_SUCCESS) {
switch_channel_event_set_data(channel, event);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "uuid", switch_core_session_get_uuid(session));
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-transfer-rate", fax_transfer_rate);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-ecm-used", (t30_stats.error_correcting_mode) ? "on" : "off");
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-t38-status", get_t38_status(pvt->t38_mode));
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-local-station-id", local_ident);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-remote-station-id", far_ident);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-remote-country", switch_str_nil(t30_get_rx_country(pvt->t30)));
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-remote-vendor", switch_str_nil(t30_get_rx_vendor(pvt->t30)));
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-remote-model", switch_str_nil(t30_get_rx_model(pvt->t30)));
if (pvt->app_mode == FUNCTION_TX) {
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-document-total-pages", fax_document_total_pages);
}
switch_event_fire(&event);
}
return T30_ERR_OK;
}
static int phase_d_handler(void *user_data, int msg)
{
t30_stats_t t30_stats;
char *fax_file_image_resolution = NULL;
char *fax_line_image_resolution = NULL;
char *fax_file_image_pixel_size = NULL;
char *fax_line_image_pixel_size = NULL;
char *fax_image_size = NULL;
char *fax_bad_rows = NULL;
char *fax_encoding = NULL;
char *fax_longest_bad_row_run = NULL;
char *fax_document_transferred_pages = NULL;
char *fax_document_total_pages = NULL;
const char *total_pages_str = "";
switch_core_session_t *session;
switch_channel_t *channel;
pvt_t *pvt;
switch_event_t *event;
pvt = (pvt_t *) user_data;
switch_assert(pvt);
session = pvt->session;
switch_assert(session);
channel = switch_core_session_get_channel(session);
switch_assert(channel);
t30_get_transfer_statistics(pvt->t30, &t30_stats);
/* Set Channel Variable */
fax_line_image_resolution = switch_core_session_sprintf(session, "%ix%i", t30_stats.x_resolution, t30_stats.y_resolution);
if (fax_line_image_resolution) {
switch_channel_set_variable(channel, "fax_image_resolution", fax_line_image_resolution);
}
fax_file_image_resolution = switch_core_session_sprintf(session, "%ix%i", t30_stats.image_x_resolution, t30_stats.image_y_resolution);
if (fax_file_image_resolution) {
switch_channel_set_variable(channel, "fax_file_image_resolution", fax_file_image_resolution);
}
fax_line_image_pixel_size = switch_core_session_sprintf(session, "%ix%i", t30_stats.width, t30_stats.length);
if (fax_line_image_pixel_size) {
switch_channel_set_variable(channel, "fax_image_pixel_size", fax_line_image_pixel_size);;
}
fax_file_image_pixel_size = switch_core_session_sprintf(session, "%ix%i", t30_stats.image_width, t30_stats.image_length);
if (fax_file_image_pixel_size) {
switch_channel_set_variable(channel, "fax_file_image_pixel_size", fax_file_image_pixel_size);;
}
fax_image_size = switch_core_session_sprintf(session, "%d", t30_stats.image_size);
if (fax_image_size) {
switch_channel_set_variable(channel, "fax_image_size", fax_image_size);
}
fax_bad_rows = switch_core_session_sprintf(session, "%d", t30_stats.bad_rows);
if (fax_bad_rows) {
switch_channel_set_variable(channel, "fax_bad_rows", fax_bad_rows);
}
fax_longest_bad_row_run = switch_core_session_sprintf(session, "%d", t30_stats.longest_bad_row_run);
if (fax_longest_bad_row_run) {
switch_channel_set_variable(channel, "fax_longest_bad_row_run", fax_longest_bad_row_run);
}
fax_encoding = switch_core_session_sprintf(session, "%d", t30_stats.compression);
if (fax_encoding) {
switch_channel_set_variable(channel, "fax_encoding", fax_encoding);
}
switch_channel_set_variable(channel, "fax_encoding_name", t4_compression_to_str(t30_stats.compression));
fax_document_transferred_pages = switch_core_session_sprintf(session, "%d", (pvt->app_mode == FUNCTION_TX) ? t30_stats.pages_tx : t30_stats.pages_rx);
if (fax_document_transferred_pages) {
switch_channel_set_variable(channel, "fax_document_transferred_pages", fax_document_transferred_pages);
}
if (pvt->app_mode == FUNCTION_TX) {
fax_document_total_pages = switch_core_session_sprintf(session, "%i", t30_stats.pages_in_file);
if (fax_document_total_pages) {
switch_channel_set_variable(channel, "fax_document_total_pages", fax_document_total_pages);
}
}
if (pvt->app_mode == FUNCTION_TX) {
total_pages_str = switch_core_session_sprintf(session, "Total fax pages: %s\n", fax_document_total_pages);
}
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO,
"==== Page %s===========================================================\n"
"Page no = %d\n"
"%s"
"Image type = %s (%s in the file)\n"
"Image size = %d x %d pixels (%d x %d pixels in the file)\n"
"Image resolution = %d/m x %d/m (%d/m x %d/m in the file)\n"
"Compression = %s (%d)\n"
"Compressed image size = %d bytes\n"
"Bad rows = %d\n"
"Longest bad row run = %d\n"
"==============================================================================\n",
pvt->app_mode == FUNCTION_TX ? "Sent ====": "Received ",
(pvt->app_mode == FUNCTION_TX) ? t30_stats.pages_tx : t30_stats.pages_rx,
total_pages_str,
t4_image_type_to_str(t30_stats.type), t4_image_type_to_str(t30_stats.image_type),
t30_stats.width, t30_stats.length, t30_stats.image_width, t30_stats.image_length,
t30_stats.x_resolution, t30_stats.y_resolution, t30_stats.image_x_resolution, t30_stats.image_y_resolution,
t4_compression_to_str(t30_stats.compression), t30_stats.compression,
t30_stats.image_size,
t30_stats.bad_rows,
t30_stats.longest_bad_row_run);
switch_channel_execute_on(channel, "execute_on_fax_phase_d");
if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, pvt->app_mode == FUNCTION_TX ? SPANDSP_EVENT_TXFAXPAGERESULT : SPANDSP_EVENT_RXFAXPAGERESULT) == SWITCH_STATUS_SUCCESS) {
switch_channel_event_set_data(channel, event);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "uuid", switch_core_session_get_uuid(session));
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-document-transferred-pages", fax_document_transferred_pages);
if (pvt->app_mode == FUNCTION_TX) {
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-document-total-pages", fax_document_total_pages);
}
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-image-resolution", fax_line_image_resolution);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-file-image-resolution", fax_file_image_resolution);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-image-size", fax_image_size);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-image-pixel-size", fax_line_image_pixel_size);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-file-image-pixel-size", fax_file_image_pixel_size);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-bad-rows", fax_bad_rows);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-longest-bad-row-run", fax_longest_bad_row_run);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-encoding", fax_encoding);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-encoding-name", t4_compression_to_str(t30_stats.compression));
switch_event_fire(&event);
}
return T30_ERR_OK; /* I don't think this does anything */
}
/*
* Called at the end of the document
*/
static void phase_e_handler(void *user_data, int result)
{
t30_stats_t t;
const char *local_ident;
const char *far_ident;
switch_core_session_t *session;
switch_channel_t *channel;
pvt_t *pvt;
char *fax_document_transferred_pages = NULL;
char *fax_document_total_pages = NULL;
char *fax_image_resolution = NULL;
char *fax_image_size = NULL;
char *fax_bad_rows = NULL;
char *fax_transfer_rate = NULL;
char *fax_result_code = NULL;
switch_event_t *event;
const char *var;
char *expanded;
const char *fax_result_str = "";
pvt = (pvt_t *) user_data;
switch_assert(pvt);
session = pvt->session;
switch_assert(session);
channel = switch_core_session_get_channel(session);
switch_assert(channel);
t30_get_transfer_statistics(pvt->t30, &t);
local_ident = switch_str_nil(t30_get_tx_ident(pvt->t30));
far_ident = switch_str_nil(t30_get_rx_ident(pvt->t30));
if (result == T30_ERR_OK) {
if (pvt->app_mode == FUNCTION_TX) {
fax_result_str = switch_core_session_sprintf(session, "Fax successfully sent.\n");
} else if (pvt->app_mode == FUNCTION_RX) {
fax_result_str = switch_core_session_sprintf(session, "Fax successfully received.\n");
} else {
fax_result_str = switch_core_session_sprintf(session, "Fax successfully managed. How ?\n");
}
switch_channel_set_variable(channel, "fax_success", "1");
} else {
fax_result_str = switch_core_session_sprintf(session, "Fax processing not successful - result (%d) %s.\n", result,
t30_completion_code_to_str(result));
switch_channel_set_variable(channel, "fax_success", "0");
}
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO,
"==============================================================================\n"
"%s"
"Remote station id: %s\n"
"Local station id: %s\n"
"Pages transferred: %i\n"
"Total fax pages: %i\n"
"Image resolution: %ix%i\n"
"Transfer Rate: %i\n"
"ECM status %s\n"
"T38 status %s\n"
"remote country: %s\n"
"remote vendor: %s\n"
"remote model: %s\n"
"==============================================================================\n",
fax_result_str,
far_ident,
local_ident,
pvt->app_mode == FUNCTION_TX ? t.pages_tx : t.pages_rx,
t.pages_in_file,
t.x_resolution, t.y_resolution,
t.bit_rate,
(t.error_correcting_mode) ? "on" : "off",
get_t38_status(pvt->t38_mode),
switch_str_nil(t30_get_rx_country(pvt->t30)),
switch_str_nil(t30_get_rx_vendor(pvt->t30)),
switch_str_nil(t30_get_rx_model(pvt->t30)));
/*
Set our channel variables, variables are also used in event
*/
fax_result_code = switch_core_session_sprintf(session, "%i", result);
if (fax_result_code) {
switch_channel_set_variable(channel, "fax_result_code", fax_result_code);
}
switch_channel_set_variable(channel, "fax_result_text", t30_completion_code_to_str(result));
switch_channel_set_variable(channel, "fax_ecm_used", (t.error_correcting_mode) ? "on" : "off");
switch_channel_set_variable(channel, "fax_t38_status", get_t38_status(pvt->t38_mode));
switch_channel_set_variable(channel, "fax_local_station_id", local_ident);
switch_channel_set_variable(channel, "fax_remote_station_id", far_ident);
fax_document_transferred_pages = switch_core_session_sprintf(session, "%i", pvt->app_mode == FUNCTION_TX ? t.pages_tx : t.pages_rx);
if (fax_document_transferred_pages) {
switch_channel_set_variable(channel, "fax_document_transferred_pages", fax_document_transferred_pages);
}
fax_document_total_pages = switch_core_session_sprintf(session, "%i", t.pages_in_file);
if (fax_document_total_pages) {
switch_channel_set_variable(channel, "fax_document_total_pages", fax_document_total_pages);
}
fax_image_resolution = switch_core_session_sprintf(session, "%ix%i", t.x_resolution, t.y_resolution);
if (fax_image_resolution) {
switch_channel_set_variable(channel, "fax_image_resolution", fax_image_resolution);
}
fax_image_size = switch_core_session_sprintf(session, "%d", t.image_size);
if (fax_image_size) {
switch_channel_set_variable(channel, "fax_image_size", fax_image_size);
}
fax_bad_rows = switch_core_session_sprintf(session, "%d", t.bad_rows);
if (fax_bad_rows) {
switch_channel_set_variable(channel, "fax_bad_rows", fax_bad_rows);
}
fax_transfer_rate = switch_core_session_sprintf(session, "%i", t.bit_rate);
if (fax_transfer_rate) {
switch_channel_set_variable(channel, "fax_transfer_rate", fax_transfer_rate);
}
/* switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING); */
pvt->done = 1;
/* Fire event */
if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, pvt->app_mode == FUNCTION_TX ? SPANDSP_EVENT_TXFAXRESULT : SPANDSP_EVENT_RXFAXRESULT) == SWITCH_STATUS_SUCCESS) {
switch_channel_event_set_data(channel, event);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-success", (result == T30_ERR_OK) ? "1" : "0");
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-result-code", fax_result_code);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-result-text", t30_completion_code_to_str(result));
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-document-transferred-pages", fax_document_transferred_pages);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-document-total-pages", fax_document_total_pages);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-image-resolution", fax_image_resolution);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-image-size", fax_image_size);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-bad-rows", fax_bad_rows);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-transfer-rate", fax_transfer_rate);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-ecm-used", (t.error_correcting_mode) ? "on" : "off");
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-t38-status", get_t38_status(pvt->t38_mode));
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-local-station-id", local_ident);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-remote-station-id", far_ident);
switch_event_fire(&event);
}
if ((var = switch_channel_get_variable(channel, "system_on_fax_result"))) {
expanded = switch_channel_expand_variables(channel, var);
switch_system(expanded, SWITCH_FALSE);
if (expanded != var) {
free(expanded);
}
}
switch_channel_execute_on(channel, "execute_on_fax_result");
if (result == T30_ERR_OK) {
if ((var = switch_channel_get_variable(channel, "system_on_fax_success"))) {
expanded = switch_channel_expand_variables(channel, var);
switch_system(expanded, SWITCH_FALSE);
if (expanded != var) {
free(expanded);
}
}
switch_channel_execute_on(channel, "execute_on_fax_success");
switch_channel_api_on(channel, "api_on_fax_success");
} else {
if ((var = switch_channel_get_variable(channel, "system_on_fax_failure"))) {
expanded = switch_channel_expand_variables(channel, var);
switch_system(expanded, SWITCH_FALSE);
if (expanded != var) {
free(expanded);
}
}
switch_channel_execute_on(channel, "execute_on_fax_failure");
switch_channel_api_on(channel, "api_on_fax_failure");
}
}
static int t38_tx_packet_handler(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
{
switch_frame_t out_frame = { 0 };
switch_core_session_t *session;
pvt_t *pvt;
uint8_t pkt[LOCAL_FAX_MAX_DATAGRAM];
int x;
int r = 0;
pvt = (pvt_t *) user_data;
session = pvt->session;
/* we need to build a real packet here and make write_frame.packet and write_frame.packetlen point to it */
out_frame.flags = SFF_UDPTL_PACKET | SFF_PROXY_PACKET;
out_frame.packet = pkt;
out_frame.buflen = LOCAL_FAX_MAX_DATAGRAM;
if ((r = udptl_build_packet(pvt->udptl_state, pkt, buf, len)) > 0) {
out_frame.packetlen = r;
//switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "WRITE %d udptl bytes\n", out_frame.packetlen);
for (x = 0; x < count; x++) {
if (switch_core_session_write_frame(session, &out_frame, SWITCH_IO_FLAG_NONE, 0) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "INVALID WRITE: %d:%d\n", out_frame.packetlen, count);
r = -1;
break;
}
}
} else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "INVALID PACKETLEN: %d PASSED: %d:%d\n", r, len, count);
}
if (r < 0) {
t30_state_t *t30;
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "TERMINATING T30 STATE\n");
if (pvt->t38_state && (t30 = t38_terminal_get_t30_state(pvt->t38_state))) {
t30_terminate(t30);
}
switch_yield(10000);
}
return r < 0 ? r : 0;
}
static switch_status_t spanfax_init(pvt_t *pvt, transport_mode_t trans_mode)
{
switch_core_session_t *session;
switch_channel_t *channel;
fax_state_t *fax;
t38_terminal_state_t *t38;
t30_state_t *t30;
const char *tmp;
const char *tz;
int fec_entries = DEFAULT_FEC_ENTRIES;
int fec_span = DEFAULT_FEC_SPAN;
int compressions;
session = (switch_core_session_t *) pvt->session;
switch_assert(session);
channel = switch_core_session_get_channel(session);
switch_assert(channel);
if ((tmp = switch_channel_get_variable(channel, "t38_gateway_redundancy"))) {
int tmp_value;
tmp_value = atoi(tmp);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "FAX changing redundancy from %d:%d to %d:%d\n", fec_span, fec_entries, tmp_value, tmp_value );
fec_entries = tmp_value;
fec_span = tmp_value;
}
switch (trans_mode) {
case AUDIO_MODE:
if (pvt->fax_state == NULL) {
pvt->fax_state = (fax_state_t *) switch_core_session_alloc(pvt->session, sizeof(fax_state_t));
}
if (pvt->fax_state == NULL) {
return SWITCH_STATUS_FALSE;
}
fax = pvt->fax_state;
pvt->t30 = fax_get_t30_state(fax);
t30 = pvt->t30;
memset(fax, 0, sizeof(fax_state_t));
if (fax_init(fax, pvt->caller) == NULL) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot initialize my fax structs\n");
return SWITCH_STATUS_FALSE;
}
fax_set_transmit_on_idle(fax, TRUE);
{
mod_spandsp_log_data_t *log_data = switch_core_session_alloc(pvt->session, sizeof(*log_data));
log_data->session = pvt->session;
log_data->verbose_log_level = pvt->verbose_log_level;
log_data->trace_file = pvt->trace_file;
span_log_set_message_handler(fax_get_logging_state(fax), mod_spandsp_log_message, log_data);
span_log_set_message_handler(t30_get_logging_state(t30), mod_spandsp_log_message, log_data);
}
if (pvt->verbose) {
span_log_set_level(fax_get_logging_state(fax), SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
span_log_set_level(t30_get_logging_state(t30), SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
}
break;
case T38_MODE:
{
switch_core_session_message_t msg = { 0 };
if (pvt->t38_state == NULL) {
pvt->t38_state = (t38_terminal_state_t *) switch_core_session_alloc(pvt->session, sizeof(t38_terminal_state_t));
}
if (pvt->t38_state == NULL) {
return SWITCH_STATUS_FALSE;
}
if (pvt->udptl_state == NULL) {
pvt->udptl_state = (udptl_state_t *) switch_core_session_alloc(pvt->session, sizeof(udptl_state_t));
}
if (pvt->udptl_state == NULL) {
t38_terminal_free(pvt->t38_state);
pvt->t38_state = NULL;
return SWITCH_STATUS_FALSE;
}
t38 = pvt->t38_state;
pvt->t30 = t38_terminal_get_t30_state(t38);
t30 = pvt->t30;
memset(t38, 0, sizeof(t38_terminal_state_t));
if (t38_terminal_init(t38, pvt->caller, t38_tx_packet_handler, pvt) == NULL) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot initialize my T.38 structs\n");
return SWITCH_STATUS_FALSE;
}
pvt->t38_core = t38_terminal_get_t38_core_state(pvt->t38_state);
if (udptl_init(pvt->udptl_state, UDPTL_ERROR_CORRECTION_REDUNDANCY, fec_span, fec_entries,
(udptl_rx_packet_handler_t *) t38_core_rx_ifp_packet, (void *) pvt->t38_core) == NULL) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot initialize my UDPTL structs\n");
return SWITCH_STATUS_FALSE;
}
msg.from = __FILE__;
msg.message_id = SWITCH_MESSAGE_INDICATE_UDPTL_MODE;
switch_core_session_receive_message(pvt->session, &msg);
{
mod_spandsp_log_data_t *log_data = switch_core_session_alloc(pvt->session, sizeof(*log_data));
log_data->session = pvt->session;
log_data->verbose_log_level = pvt->verbose_log_level;
log_data->trace_file = pvt->trace_file;
span_log_set_message_handler(t38_terminal_get_logging_state(t38), mod_spandsp_log_message, log_data);
span_log_set_message_handler(t30_get_logging_state(t30), mod_spandsp_log_message, log_data);
}
if (pvt->verbose) {
span_log_set_level(t38_terminal_get_logging_state(t38), SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
span_log_set_level(t30_get_logging_state(t30), SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
}
}
break;
case T38_GATEWAY_MODE:
if (pvt->t38_gateway_state == NULL) {
pvt->t38_gateway_state = (t38_gateway_state_t *) switch_core_session_alloc(pvt->session, sizeof(t38_gateway_state_t));
}
if (pvt->udptl_state == NULL) {
pvt->udptl_state = (udptl_state_t *) switch_core_session_alloc(pvt->session, sizeof(udptl_state_t));
}
if (t38_gateway_init(pvt->t38_gateway_state, t38_tx_packet_handler, pvt) == NULL) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot initialize my T.38 structs\n");
t38_gateway_free(pvt->t38_gateway_state);
pvt->t38_gateway_state = NULL;
return SWITCH_STATUS_FALSE;
}
pvt->t38_core = t38_gateway_get_t38_core_state(pvt->t38_gateway_state);
if (udptl_init(pvt->udptl_state, UDPTL_ERROR_CORRECTION_REDUNDANCY, fec_span, fec_entries,
(udptl_rx_packet_handler_t *) t38_core_rx_ifp_packet, (void *) pvt->t38_core) == NULL) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot initialize my UDPTL structs\n");
t38_gateway_free(pvt->t38_gateway_state);
udptl_release(pvt->udptl_state);
pvt->udptl_state = NULL;
return SWITCH_STATUS_FALSE;
}
t38_gateway_set_transmit_on_idle(pvt->t38_gateway_state, TRUE);
if (switch_true(switch_channel_get_variable(channel, "fax_v17_disabled"))) {
t38_gateway_set_supported_modems(pvt->t38_gateway_state, T30_SUPPORT_V29 | T30_SUPPORT_V27TER);
} else {
t38_gateway_set_supported_modems(pvt->t38_gateway_state, T30_SUPPORT_V17 | T30_SUPPORT_V29 | T30_SUPPORT_V27TER);
}
t38_gateway_set_tep_mode(pvt->t38_gateway_state, pvt->enable_tep);
t38_gateway_set_ecm_capability(pvt->t38_gateway_state, pvt->use_ecm);
switch_channel_set_variable(channel, "fax_ecm_requested", pvt->use_ecm ? "true" : "false");
{
mod_spandsp_log_data_t *log_data = switch_core_session_alloc(pvt->session, sizeof(*log_data));
log_data->session = pvt->session;
log_data->verbose_log_level = pvt->verbose_log_level;
log_data->trace_file = pvt->trace_file;
span_log_set_message_handler(t38_gateway_get_logging_state(pvt->t38_gateway_state), mod_spandsp_log_message, log_data);
span_log_set_message_handler(t38_core_get_logging_state(pvt->t38_core), mod_spandsp_log_message, log_data);
}
if (pvt->verbose) {
span_log_set_level(t38_gateway_get_logging_state(pvt->t38_gateway_state), SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
span_log_set_level(t38_core_get_logging_state(pvt->t38_core), SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
}
t38_set_t38_version(pvt->t38_core, 0);
return SWITCH_STATUS_SUCCESS;
default:
assert(0); /* What? */
return SWITCH_STATUS_SUCCESS;
} /* Switch trans mode */
/* All the things which are common to audio and T.38 FAX setup */
t30_set_tx_ident(t30, pvt->ident);
t30_set_tx_page_header_info(t30, pvt->header);
if (pvt->timezone && pvt->timezone[0]) {
if ((tz = switch_lookup_timezone(pvt->timezone)))
t30_set_tx_page_header_tz(t30, tz);
else
t30_set_tx_page_header_tz(t30, pvt->timezone);
}
t30_set_phase_e_handler(t30, phase_e_handler, pvt);
t30_set_phase_d_handler(t30, phase_d_handler, pvt);
t30_set_phase_b_handler(t30, phase_b_handler, pvt);
t30_set_supported_image_sizes(t30,
T4_SUPPORT_LENGTH_US_LETTER
| T4_SUPPORT_LENGTH_US_LEGAL
| T4_SUPPORT_LENGTH_UNLIMITED
| T4_SUPPORT_WIDTH_215MM
| T4_SUPPORT_WIDTH_255MM
| T4_SUPPORT_WIDTH_303MM);
t30_set_supported_bilevel_resolutions(t30,
T4_RESOLUTION_R8_STANDARD
| T4_RESOLUTION_R8_FINE
| T4_RESOLUTION_R8_SUPERFINE
| T4_RESOLUTION_R16_SUPERFINE
| T4_RESOLUTION_200_100
| T4_RESOLUTION_200_200
| T4_RESOLUTION_200_400
| T4_RESOLUTION_400_400);
compressions = T4_COMPRESSION_T4_1D
| T4_COMPRESSION_T4_2D
| T4_COMPRESSION_T6
| T4_COMPRESSION_T85
| T4_COMPRESSION_T85_L0;
if (pvt->enable_colour_fax) {
t30_set_supported_colour_resolutions(t30, T4_RESOLUTION_100_100
| T4_RESOLUTION_200_200
| T4_RESOLUTION_300_300
| T4_RESOLUTION_400_400);
compressions |= (T4_COMPRESSION_COLOUR | T4_COMPRESSION_T42_T81);
} else {
t30_set_supported_colour_resolutions(t30, 0);
}
if (pvt->enable_image_resizing)
compressions |= T4_COMPRESSION_RESCALING;
if (pvt->enable_colour_to_bilevel)