tsb85_tests.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. /*! \file */
  26. #if defined(HAVE_CONFIG_H)
  27. #include "config.h"
  28. #endif
  29. #include <inttypes.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <ctype.h>
  34. #if defined(HAVE_TGMATH_H)
  35. #include <tgmath.h>
  36. #endif
  37. #if defined(HAVE_MATH_H)
  38. #include <math.h>
  39. #endif
  40. #include "floating_fudge.h"
  41. #include <assert.h>
  42. #include <fcntl.h>
  43. #include <time.h>
  44. #include <unistd.h>
  45. #include <sndfile.h>
  46. #if defined(HAVE_LIBXML_XMLMEMORY_H)
  47. #include <libxml/xmlmemory.h>
  48. #endif
  49. #if defined(HAVE_LIBXML_PARSER_H)
  50. #include <libxml/parser.h>
  51. #endif
  52. #if defined(HAVE_LIBXML_XINCLUDE_H)
  53. #include <libxml/xinclude.h>
  54. #endif
  55. #include "spandsp.h"
  56. #include "spandsp-sim.h"
  57. #include "fax_utils.h"
  58. #include "fax_tester.h"
  59. #define OUTPUT_TIFF_FILE_NAME "tsb85.tif"
  60. #define OUTPUT_WAVE_FILE_NAME "tsb85.wav"
  61. #define SAMPLES_PER_CHUNK 160
  62. SNDFILE *out_handle;
  63. const char *output_tiff_file_name;
  64. bool log_audio = false;
  65. faxtester_state_t *state;
  66. static void exchange(faxtester_state_t *s)
  67. {
  68. int16_t amp[SAMPLES_PER_CHUNK];
  69. int16_t out_amp[2*SAMPLES_PER_CHUNK];
  70. int len;
  71. int i;
  72. int total_audio_time;
  73. logging_state_t *logging;
  74. output_tiff_file_name = OUTPUT_TIFF_FILE_NAME;
  75. if (log_audio)
  76. {
  77. if ((out_handle = sf_open_telephony_write(OUTPUT_WAVE_FILE_NAME, 2)) == NULL)
  78. {
  79. fprintf(stderr, " Cannot create audio file '%s'\n", OUTPUT_WAVE_FILE_NAME);
  80. printf("Test failed\n");
  81. exit(2);
  82. }
  83. /*endif*/
  84. }
  85. /*endif*/
  86. total_audio_time = 0;
  87. faxtester_set_transmit_on_idle(s, true);
  88. s->far_fax = fax_init(NULL, false);
  89. s->far_t30 = fax_get_t30_state(s->far_fax);
  90. s->far_tag = 'A';
  91. if (s->far_fax)
  92. logging = fax_get_logging_state(s->far_fax);
  93. else
  94. logging = t38_terminal_get_logging_state(s->far_t38);
  95. 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);
  96. span_log_set_tag(logging, "A");
  97. logging = t30_get_logging_state(s->far_t30);
  98. 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);
  99. span_log_set_tag(logging, "A");
  100. #if 0
  101. 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);
  102. span_log_set_tag(&fax.modems.v27ter_rx.logging, "A");
  103. 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);
  104. span_log_set_tag(&fax.modems.v29_rx.logging, "A");
  105. 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);
  106. span_log_set_tag(&fax.modems.v17_rx.logging, "A");
  107. #endif
  108. while (faxtester_next_step(s) == 0)
  109. ;
  110. /*endwhile*/
  111. for (;;)
  112. {
  113. len = fax_tx(s->far_fax, amp, SAMPLES_PER_CHUNK);
  114. faxtester_rx(s, amp, len);
  115. if (log_audio)
  116. {
  117. for (i = 0; i < len; i++)
  118. out_amp[2*i + 0] = amp[i];
  119. /*endfor*/
  120. }
  121. /*endif*/
  122. total_audio_time += SAMPLES_PER_CHUNK;
  123. logging = t30_get_logging_state(s->far_t30);
  124. span_log_bump_samples(logging, len);
  125. #if 0
  126. span_log_bump_samples(&fax.modems.v27ter_rx.logging, len);
  127. span_log_bump_samples(&fax.modems.v29_rx.logging, len);
  128. span_log_bump_samples(&fax.modems.v17_rx.logging, len);
  129. #endif
  130. logging = fax_get_logging_state(s->far_fax);
  131. span_log_bump_samples(logging, len);
  132. logging = faxtester_get_logging_state(s);
  133. span_log_bump_samples(logging, len);
  134. len = faxtester_tx(s, amp, SAMPLES_PER_CHUNK);
  135. if (fax_rx(s->far_fax, amp, len))
  136. break;
  137. /*endif*/
  138. if (log_audio)
  139. {
  140. for (i = 0; i < len; i++)
  141. out_amp[2*i + 1] = amp[i];
  142. /*endfor*/
  143. if (sf_writef_short(out_handle, out_amp, SAMPLES_PER_CHUNK) != SAMPLES_PER_CHUNK)
  144. break;
  145. /*endif*/
  146. }
  147. /*endif*/
  148. if (s->test_for_call_clear && !s->far_end_cleared_call)
  149. {
  150. s->call_clear_timer += len;
  151. if (!t30_call_active(s->far_t30))
  152. {
  153. 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);
  154. if (s->call_clear_timer/8 < s->timein_x || s->call_clear_timer/8 > s->timeout_x)
  155. {
  156. printf("Test failed\n");
  157. exit(2);
  158. }
  159. span_log(faxtester_get_logging_state(s), SPAN_LOG_FLOW, "Clear time OK\n");
  160. s->far_end_cleared_call = true;
  161. s->test_for_call_clear = false;
  162. while (faxtester_next_step(s) == 0)
  163. ;
  164. /*endwhile*/
  165. }
  166. /*endif*/
  167. }
  168. /*endif*/
  169. }
  170. /*endfor*/
  171. if (log_audio)
  172. {
  173. if (sf_close_telephony(out_handle))
  174. {
  175. fprintf(stderr, " Cannot close audio file '%s'\n", OUTPUT_WAVE_FILE_NAME);
  176. printf("Test failed\n");
  177. exit(2);
  178. }
  179. /*endif*/
  180. }
  181. /*endif*/
  182. }
  183. /*- End of function --------------------------------------------------------*/
  184. int main(int argc, char *argv[])
  185. {
  186. const char *xml_file_name;
  187. const char *test_name;
  188. logging_state_t *logging;
  189. int opt;
  190. #if 0
  191. string_test();
  192. #endif
  193. xml_file_name = "../spandsp/tsb85.xml";
  194. test_name = "MRGN01";
  195. log_audio = false;
  196. while ((opt = getopt(argc, argv, "lx:")) != -1)
  197. {
  198. switch (opt)
  199. {
  200. case 'l':
  201. log_audio = true;
  202. break;
  203. case 'x':
  204. xml_file_name = optarg;
  205. break;
  206. default:
  207. //usage();
  208. exit(2);
  209. break;
  210. }
  211. }
  212. argc -= optind;
  213. argv += optind;
  214. if (argc > 0)
  215. test_name = argv[0];
  216. if ((state = faxtester_init(NULL, xml_file_name, test_name)) == NULL)
  217. {
  218. fprintf(stderr, "Cannot start FAX tester instance\n");
  219. printf("Test failed\n");
  220. exit(2);
  221. }
  222. logging = faxtester_get_logging_state(state);
  223. 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);
  224. span_log_set_tag(logging, "B");
  225. /* We found the test we want, so run it. */
  226. exchange(state);
  227. faxtester_free(state);
  228. printf("Done\n");
  229. return 0;
  230. }
  231. /*- End of function --------------------------------------------------------*/
  232. /*- End of file ------------------------------------------------------------*/