v42_tests.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * v42_tests.c
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2004, 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. /* THIS IS A WORK IN PROGRESS. IT IS NOT FINISHED. */
  26. /*! \page v42_tests_page V.42 tests
  27. \section v42_tests_page_sec_1 What does it do?
  28. These tests connect two instances of V.42 back to back. V.42 frames are
  29. then exchanged between them.
  30. */
  31. #if defined(HAVE_CONFIG_H)
  32. #include "config.h"
  33. #endif
  34. #include <stdlib.h>
  35. #include <unistd.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <assert.h>
  39. #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
  40. #include "spandsp.h"
  41. v42_state_t callerx;
  42. v42_state_t answererx;
  43. v42_state_t *caller;
  44. v42_state_t *answerer;
  45. int variable_length;
  46. int rx_next[3] = {0};
  47. int tx_next[3] = {0};
  48. static void v42_status(void *user_data, int status)
  49. {
  50. v42_state_t *s;
  51. s = (v42_state_t *) user_data;
  52. if (status < 0)
  53. printf("%p: Status is '%s' (%d)\n", s, signal_status_to_str(status), status);
  54. else
  55. printf("%p: Status is '%s' (%d)\n", s, lapm_status_to_str(status), status);
  56. }
  57. /*- End of function --------------------------------------------------------*/
  58. static int v42_get_frames(void *user_data, uint8_t msg[], int len)
  59. {
  60. int i;
  61. int j;
  62. int k;
  63. int x;
  64. v42_state_t *s;
  65. if (len < 0)
  66. {
  67. v42_status(user_data, len);
  68. return 0;
  69. }
  70. s = (v42_state_t *) user_data;
  71. x = (s == caller) ? 1 : 2;
  72. if (variable_length)
  73. {
  74. j = make_mask32(len);
  75. do
  76. k = j & rand();
  77. while (k > len);
  78. }
  79. else
  80. {
  81. k = len;
  82. }
  83. for (i = 0; i < k; i++)
  84. msg[i] = tx_next[x]++;
  85. return k;
  86. }
  87. /*- End of function --------------------------------------------------------*/
  88. static void v42_put_frames(void *user_data, const uint8_t msg[], int len)
  89. {
  90. int i;
  91. v42_state_t *s;
  92. int x;
  93. static int count = 0;
  94. static int xxx = 0;
  95. if (len < 0)
  96. {
  97. v42_status(user_data, len);
  98. return;
  99. }
  100. s = (v42_state_t *) user_data;
  101. x = (s == caller) ? 1 : 2;
  102. for (i = 0; i < len; i++)
  103. {
  104. if (msg[i] != (rx_next[x] & 0xFF))
  105. {
  106. printf("%p: Mismatch 0x%02X 0x%02X\n", user_data, msg[i], rx_next[x] & 0xFF);
  107. exit(2);
  108. }
  109. rx_next[x]++;
  110. }
  111. printf("%p: Got frame len %d\n", user_data, len);
  112. printf("%p: %d Far end busy status %d\n", user_data, count, v42_get_far_busy_status(s));
  113. if (s == caller)
  114. {
  115. if (++count == 5)
  116. {
  117. v42_set_local_busy_status(s, true);
  118. xxx = 1;
  119. }
  120. }
  121. else
  122. {
  123. if (xxx && ++count == 45)
  124. v42_set_local_busy_status(caller, false);
  125. }
  126. }
  127. /*- End of function --------------------------------------------------------*/
  128. int main(int argc, char *argv[])
  129. {
  130. int i;
  131. int bit;
  132. bool insert_caller_bit_errors;
  133. bool insert_answerer_bit_errors;
  134. int opt;
  135. insert_caller_bit_errors = false;
  136. insert_answerer_bit_errors = false;
  137. variable_length = false;
  138. while ((opt = getopt(argc, argv, "bv")) != -1)
  139. {
  140. switch (opt)
  141. {
  142. case 'b':
  143. insert_caller_bit_errors = 11000;
  144. insert_answerer_bit_errors = 10000;
  145. break;
  146. case 'v':
  147. variable_length = true;
  148. break;
  149. default:
  150. //usage();
  151. exit(2);
  152. break;
  153. }
  154. }
  155. caller = v42_init(&callerx, true, true, v42_get_frames, v42_put_frames, (void *) &callerx);
  156. answerer = v42_init(&answererx, false, true, v42_get_frames, v42_put_frames, (void *) &answererx);
  157. v42_set_status_callback(caller, v42_status, (void *) caller);
  158. v42_set_status_callback(answerer, v42_status, (void *) answerer);
  159. v42_restart(caller);
  160. v42_restart(answerer);
  161. span_log_set_level(v42_get_logging_state(caller), SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_DEBUG);
  162. span_log_set_tag(v42_get_logging_state(caller), "caller");
  163. span_log_set_level(v42_get_logging_state(answerer), SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_DEBUG);
  164. span_log_set_tag(v42_get_logging_state(answerer), "answerer");
  165. for (i = 0; i < 1000000; i++)
  166. {
  167. bit = v42_tx_bit(caller);
  168. if (insert_caller_bit_errors && i%insert_caller_bit_errors == 0)
  169. bit ^= 1;
  170. v42_rx_bit(answerer, bit);
  171. bit = v42_tx_bit(answerer);
  172. if (insert_answerer_bit_errors && i%insert_answerer_bit_errors == 0)
  173. bit ^= 1;
  174. v42_rx_bit(caller, bit);
  175. }
  176. return 0;
  177. }
  178. /*- End of function --------------------------------------------------------*/
  179. /*- End of file ------------------------------------------------------------*/