2
0

dummy_modems_tests.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * dummy_modems_tests.c - Tests for data_modems connected together by sockets.
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2011 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 General Public License version 2, as
  14. * 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 General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * 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. /*! \page dummy_modems_tests_page Dummy data modems tests
  27. \section dummy_modems_tests_page_sec_1 What does it do?
  28. */
  29. #if defined(HAVE_CONFIG_H)
  30. #include "config.h"
  31. #endif
  32. #if defined(HAVE_FL_FL_H) && defined(HAVE_FL_FL_CARTESIAN_H) && defined(HAVE_FL_FL_AUDIO_METER_H)
  33. #define ENABLE_GUI
  34. #endif
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <fcntl.h>
  38. #include <unistd.h>
  39. #include <string.h>
  40. #include <assert.h>
  41. #include <termios.h>
  42. #include <sndfile.h>
  43. //#define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
  44. #include "spandsp.h"
  45. #include "spandsp-sim.h"
  46. #include "pseudo_terminals.h"
  47. #include "socket_harness.h"
  48. #if defined(ENABLE_GUI)
  49. #include "media_monitor.h"
  50. #endif
  51. #define OUTPUT_WAVE_FILE_NAME "dummy_modems.wav"
  52. #define SAMPLES_PER_CHUNK 160
  53. SNDFILE *wave_handle = NULL;
  54. int16_t wave_buffer[4096];
  55. data_modems_state_t *data_modem_state;
  56. static int get_msg(void *user_data, uint8_t msg[], int len)
  57. {
  58. return 0;
  59. }
  60. /*- End of function --------------------------------------------------------*/
  61. static void put_msg(void *user_data, const uint8_t msg[], int len)
  62. {
  63. if (len < 0)
  64. printf("Status %s\n", signal_status_to_str(len));
  65. else
  66. printf("Put %d '%s'\n", len, msg);
  67. }
  68. /*- End of function --------------------------------------------------------*/
  69. static void terminal_callback(void *user_data, const uint8_t msg[], int len)
  70. {
  71. printf("terminal callback %d\n", len);
  72. }
  73. /*- End of function --------------------------------------------------------*/
  74. static int termios_callback(void *user_data, struct termios *termios)
  75. {
  76. printf("termios callback\n");
  77. return 0;
  78. }
  79. /*- End of function --------------------------------------------------------*/
  80. static void hangup_callback(void *user_data, int status)
  81. {
  82. }
  83. /*- End of function --------------------------------------------------------*/
  84. static int terminal_free_space_callback(void *user_data)
  85. {
  86. return 42;
  87. }
  88. /*- End of function --------------------------------------------------------*/
  89. static int rx_callback(void *user_data, const int16_t amp[], int samples)
  90. {
  91. int i;
  92. int out_samples;
  93. out_samples = data_modems_rx((data_modems_state_t *) user_data, amp, samples);
  94. if (wave_handle)
  95. {
  96. for (i = 0; i < samples; i++)
  97. wave_buffer[2*i] = amp[i];
  98. }
  99. return out_samples;
  100. }
  101. /*- End of function --------------------------------------------------------*/
  102. static int rx_fillin_callback(void *user_data, int samples)
  103. {
  104. return data_modems_rx_fillin((data_modems_state_t *) user_data, samples);
  105. }
  106. /*- End of function --------------------------------------------------------*/
  107. static int tx_callback(void *user_data, int16_t amp[], int samples)
  108. {
  109. int i;
  110. int out_samples;
  111. out_samples = data_modems_tx((data_modems_state_t *) user_data, amp, samples);
  112. if (wave_handle)
  113. {
  114. if (out_samples < samples)
  115. memset(&amp[out_samples], 0, (samples - out_samples)*2);
  116. for (i = 0; i < samples; i++)
  117. wave_buffer[2*i + 1] = amp[i];
  118. sf_writef_short(wave_handle, wave_buffer, samples);
  119. }
  120. return samples;
  121. }
  122. /*- End of function --------------------------------------------------------*/
  123. static int modem_tests(int use_gui, int log_audio, bool calling_party)
  124. {
  125. logging_state_t *logging;
  126. socket_harness_state_t *s;
  127. /* Now set up and run the modems */
  128. if ((data_modem_state = data_modems_init(NULL,
  129. calling_party,
  130. put_msg,
  131. get_msg,
  132. NULL)) == NULL)
  133. {
  134. fprintf(stderr, " Cannot start the data modem\n");
  135. exit(2);
  136. }
  137. logging = data_modems_get_logging_state(data_modem_state);
  138. span_log_set_level(logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_DATE);
  139. span_log_set_tag(logging, "Modem");
  140. if ((s = socket_harness_init(NULL,
  141. "/tmp/modemsocket",
  142. "modemA",
  143. calling_party,
  144. terminal_callback,
  145. termios_callback,
  146. hangup_callback,
  147. terminal_free_space_callback,
  148. rx_callback,
  149. rx_fillin_callback,
  150. tx_callback,
  151. data_modem_state)) == NULL)
  152. {
  153. fprintf(stderr, " Cannot start the socket harness\n");
  154. exit(2);
  155. }
  156. wave_handle = NULL;
  157. if (log_audio)
  158. {
  159. if ((wave_handle = sf_open_telephony_write(OUTPUT_WAVE_FILE_NAME, 2)) == NULL)
  160. {
  161. fprintf(stderr, " Cannot create audio file '%s'\n", OUTPUT_WAVE_FILE_NAME);
  162. exit(2);
  163. }
  164. }
  165. socket_harness_run(s);
  166. if (log_audio)
  167. {
  168. if (sf_close_telephony(wave_handle))
  169. {
  170. fprintf(stderr, " Cannot close audio file '%s'\n", OUTPUT_WAVE_FILE_NAME);
  171. exit(2);
  172. }
  173. }
  174. return 0;
  175. }
  176. /*- End of function --------------------------------------------------------*/
  177. int main(int argc, char *argv[])
  178. {
  179. int log_audio;
  180. int use_gui;
  181. int opt;
  182. bool calling_party;
  183. log_audio = false;
  184. calling_party = false;
  185. use_gui = false;
  186. while ((opt = getopt(argc, argv, "acgl")) != -1)
  187. {
  188. switch (opt)
  189. {
  190. case 'a':
  191. calling_party = false;
  192. break;
  193. case 'c':
  194. calling_party = true;
  195. break;
  196. case 'g':
  197. #if defined(ENABLE_GUI)
  198. use_gui = true;
  199. #else
  200. fprintf(stderr, "Graphical monitoring not available\n");
  201. exit(2);
  202. #endif
  203. break;
  204. case 'l':
  205. log_audio = true;
  206. break;
  207. default:
  208. //usage();
  209. exit(2);
  210. break;
  211. }
  212. }
  213. if (modem_tests(use_gui, log_audio, calling_party))
  214. exit(2);
  215. printf("Tests passed\n");
  216. return 0;
  217. }
  218. /*- End of function --------------------------------------------------------*/
  219. /*- End of file ------------------------------------------------------------*/