2
0

pseudo_terminal_tests.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * pseudo_terminal_tests.c - pseudo terminal handling tests.
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2012 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. #include <inttypes.h>
  26. #include <stdlib.h>
  27. #if defined(WIN32)
  28. #include <windows.h>
  29. #else
  30. #if defined(__APPLE__)
  31. #include <util.h>
  32. #elif defined(__FreeBSD__)
  33. #include <libutil.h>
  34. #include <termios.h>
  35. #include <sys/socket.h>
  36. #else
  37. #include <pty.h>
  38. #endif
  39. #include <sys/ioctl.h>
  40. #include <fcntl.h>
  41. #include <unistd.h>
  42. #include <errno.h>
  43. #include <fcntl.h>
  44. #include <poll.h>
  45. #include <errno.h>
  46. #endif
  47. #include "spandsp.h"
  48. #include "spandsp/t30_fcf.h"
  49. #include "spandsp-sim.h"
  50. #undef SPANDSP_EXPOSE_INTERNAL_STRUCTURES
  51. #include "pseudo_terminals.h"
  52. static int master(void)
  53. {
  54. modem_t modem[10];
  55. char buf[1024];
  56. int len;
  57. int i;
  58. for (i = 0; i < 10; i++)
  59. {
  60. if (pseudo_terminal_create(&modem[i]))
  61. {
  62. printf("Failure\n");
  63. exit(2);
  64. }
  65. printf("%s %s\n", modem[i].devlink, modem[i].stty);
  66. }
  67. for (;;)
  68. {
  69. for (i = 0; i < 10; i++)
  70. {
  71. len = read(modem[i].master, buf, 4);
  72. if (len >= 0)
  73. {
  74. buf[len] = '\0';
  75. printf("%d %d '%s' %s\n", i, len, buf, strerror(errno));
  76. }
  77. }
  78. }
  79. for (i = 0; i < 10; i++)
  80. {
  81. if (pseudo_terminal_close(&modem[i]))
  82. {
  83. printf("Failure\n");
  84. exit(2);
  85. }
  86. }
  87. return 0;
  88. }
  89. /*- End of function --------------------------------------------------------*/
  90. static int slave(void)
  91. {
  92. int fd[10];
  93. char name[64];
  94. int i;
  95. int j;
  96. for (i = 0; i < 10; i++)
  97. {
  98. sprintf(name, "/dev/spandsp/%d", i);
  99. if ((fd[i] = open(name, O_RDWR)) < 0)
  100. {
  101. printf("Failed to open %s\n", name);
  102. exit(2);
  103. }
  104. printf("%s\n", name);
  105. }
  106. for (j = 0; j < 10; j++)
  107. {
  108. for (i = 0; i < 10; i++)
  109. {
  110. write(fd[i], "FRED", 4);
  111. }
  112. }
  113. for (i = 0; i < 10; i++)
  114. {
  115. if (close(fd[i]))
  116. {
  117. printf("Failed to close %d\n", i);
  118. exit(2);
  119. }
  120. }
  121. return 0;
  122. }
  123. /*- End of function --------------------------------------------------------*/
  124. int main(int argc, char *argv[])
  125. {
  126. if (argc < 2)
  127. master();
  128. else
  129. slave();
  130. return 0;
  131. }
  132. /*- End of function --------------------------------------------------------*/
  133. /*- End of file ------------------------------------------------------------*/