1 /*
2  * SpanDSP - a series of DSP components for telephony
3  *
4  * tsb85_tests.c
5  *
6  * Written by Steve Underwood <steveu@coppice.org>
7  *
8  * Copyright (C) 2008 Steve Underwood
9  *
10  * All rights reserved.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 2.1,
14  * as published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25 
26 /*! \file */
27 
28 #if defined(HAVE_CONFIG_H)
29 #include "config.h"
30 #endif
31 
32 #include <inttypes.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <ctype.h>
37 #if defined(HAVE_TGMATH_H)
38 #include <tgmath.h>
39 #endif
40 #if defined(HAVE_MATH_H)
41 #include <math.h>
42 #endif
43 #include "floating_fudge.h"
44 #include <assert.h>
45 #include <fcntl.h>
46 #include <time.h>
47 #include <unistd.h>
48 #include <sndfile.h>
49 
50 #if defined(HAVE_LIBXML_XMLMEMORY_H)
51 #include <libxml/xmlmemory.h>
52 #endif
53 #if defined(HAVE_LIBXML_PARSER_H)
54 #include <libxml/parser.h>
55 #endif
56 #if defined(HAVE_LIBXML_XINCLUDE_H)
57 #include <libxml/xinclude.h>
58 #endif
59 
60 #include "spandsp.h"
61 #include "spandsp-sim.h"
62 
63 #include "fax_utils.h"
64 #include "fax_tester.h"
65 
66 #define OUTPUT_TIFF_FILE_NAME   "tsb85.tif"
67 
68 #define OUTPUT_WAVE_FILE_NAME   "tsb85.wav"
69 
70 #define SAMPLES_PER_CHUNK       160
71 
72 SNDFILE *out_handle;
73 
74 const char *output_tiff_file_name;
75 
76 bool log_audio = false;
77 
78 faxtester_state_t *state;
79 
exchange(faxtester_state_t * s)80 static void exchange(faxtester_state_t *s)
81 {
82     int16_t amp[SAMPLES_PER_CHUNK];
83     int16_t out_amp[2*SAMPLES_PER_CHUNK];
84     int len;
85     int i;
86     int total_audio_time;
87     logging_state_t *logging;
88 
89     output_tiff_file_name = OUTPUT_TIFF_FILE_NAME;
90 
91     if (log_audio)
92     {
93         if ((out_handle = sf_open_telephony_write(OUTPUT_WAVE_FILE_NAME, 2)) == NULL)
94         {
95             fprintf(stderr, "    Cannot create audio file '%s'\n", OUTPUT_WAVE_FILE_NAME);
96             printf("Test failed\n");
97             exit(2);
98         }
99         /*endif*/
100     }
101     /*endif*/
102 
103     total_audio_time = 0;
104 
105     faxtester_set_transmit_on_idle(s, true);
106 
107     s->far_fax = fax_init(NULL, false);
108     s->far_t30 = fax_get_t30_state(s->far_fax);
109     s->far_tag = 'A';
110 
111     if (s->far_fax)
112         logging = fax_get_logging_state(s->far_fax);
113     else
114         logging = t38_terminal_get_logging_state(s->far_t38);
115     span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW);
116     span_log_set_tag(logging, "A");
117 
118     logging = t30_get_logging_state(s->far_t30);
119     span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW);
120     span_log_set_tag(logging, "A");
121 
122 #if 0
123     span_log_set_level(&fax.modems.v27ter_rx.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW);
124     span_log_set_tag(&fax.modems.v27ter_rx.logging, "A");
125     span_log_set_level(&fax.modems.v29_rx.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW);
126     span_log_set_tag(&fax.modems.v29_rx.logging, "A");
127     span_log_set_level(&fax.modems.v17_rx.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW);
128     span_log_set_tag(&fax.modems.v17_rx.logging, "A");
129 #endif
130     while (faxtester_next_step(s) == 0)
131         ;
132     /*endwhile*/
133     for (;;)
134     {
135         len = fax_tx(s->far_fax, amp, SAMPLES_PER_CHUNK);
136         faxtester_rx(s, amp, len);
137         if (log_audio)
138         {
139             for (i = 0;  i < len;  i++)
140                 out_amp[2*i + 0] = amp[i];
141             /*endfor*/
142         }
143         /*endif*/
144 
145         total_audio_time += SAMPLES_PER_CHUNK;
146 
147         logging = t30_get_logging_state(s->far_t30);
148         span_log_bump_samples(logging, len);
149 #if 0
150         span_log_bump_samples(&fax.modems.v27ter_rx.logging, len);
151         span_log_bump_samples(&fax.modems.v29_rx.logging, len);
152         span_log_bump_samples(&fax.modems.v17_rx.logging, len);
153 #endif
154         logging = fax_get_logging_state(s->far_fax);
155         span_log_bump_samples(logging, len);
156 
157         logging = faxtester_get_logging_state(s);
158         span_log_bump_samples(logging, len);
159 
160         len = faxtester_tx(s, amp, SAMPLES_PER_CHUNK);
161         if (fax_rx(s->far_fax, amp, len))
162             break;
163         /*endif*/
164         if (log_audio)
165         {
166             for (i = 0;  i < len;  i++)
167                 out_amp[2*i + 1] = amp[i];
168             /*endfor*/
169             if (sf_writef_short(out_handle, out_amp, SAMPLES_PER_CHUNK) != SAMPLES_PER_CHUNK)
170                 break;
171             /*endif*/
172         }
173         /*endif*/
174         if (s->test_for_call_clear  &&  !s->far_end_cleared_call)
175         {
176             s->call_clear_timer += len;
177             if (!t30_call_active(s->far_t30))
178             {
179                 span_log(faxtester_get_logging_state(s), SPAN_LOG_FLOW, "Far end cleared after %dms (limits %dms to %dms)\n", s->call_clear_timer/8, s->timein_x, s->timeout);
180                 if (s->call_clear_timer/8 < s->timein_x  ||  s->call_clear_timer/8 > s->timeout_x)
181                 {
182                     printf("Test failed\n");
183                     exit(2);
184                 }
185                 span_log(faxtester_get_logging_state(s), SPAN_LOG_FLOW, "Clear time OK\n");
186                 s->far_end_cleared_call = true;
187                 s->test_for_call_clear = false;
188                 while (faxtester_next_step(s) == 0)
189                     ;
190                 /*endwhile*/
191             }
192             /*endif*/
193         }
194         /*endif*/
195     }
196     /*endfor*/
197     if (log_audio)
198     {
199         if (sf_close_telephony(out_handle))
200         {
201             fprintf(stderr, "    Cannot close audio file '%s'\n", OUTPUT_WAVE_FILE_NAME);
202             printf("Test failed\n");
203             exit(2);
204         }
205         /*endif*/
206     }
207     /*endif*/
208 }
209 /*- End of function --------------------------------------------------------*/
210 
main(int argc,char * argv[])211 int main(int argc, char *argv[])
212 {
213     const char *xml_file_name;
214     const char *test_name;
215     logging_state_t *logging;
216     int opt;
217 
218 #if 0
219     string_test();
220 #endif
221 
222     xml_file_name = "../spandsp/tsb85.xml";
223     test_name = "MRGN01";
224     log_audio = false;
225     while ((opt = getopt(argc, argv, "lx:")) != -1)
226     {
227         switch (opt)
228         {
229         case 'l':
230             log_audio = true;
231             break;
232         case 'x':
233             xml_file_name = optarg;
234             break;
235         default:
236             //usage();
237             exit(2);
238             break;
239         }
240     }
241     argc -= optind;
242     argv += optind;
243     if (argc > 0)
244         test_name = argv[0];
245 
246     if ((state = faxtester_init(NULL, xml_file_name, test_name)) == NULL)
247     {
248         fprintf(stderr, "Cannot start FAX tester instance\n");
249         printf("Test failed\n");
250         exit(2);
251     }
252     logging = faxtester_get_logging_state(state);
253     span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW);
254     span_log_set_tag(logging, "B");
255     /* We found the test we want, so run it. */
256     exchange(state);
257     faxtester_free(state);
258     printf("Done\n");
259     return 0;
260 }
261 /*- End of function --------------------------------------------------------*/
262 /*- End of file ------------------------------------------------------------*/
263