edsu.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * edsu.c - Qualcomm Atheros Ethernet II Data Send Utility
  11. *
  12. * send one or more files over Ethernet using IEEE 802.2 Ethernet
  13. * Frames;
  14. *
  15. * this program can be used as a data source when testing AR6405
  16. * UART applications; use this program to send files and program
  17. * edru to read and display or save them at the other end;
  18. *
  19. * Contributor(s):
  20. * Charles Maier <cmaier@qca.qualcomm.com>
  21. *
  22. *--------------------------------------------------------------------*/
  23. /*====================================================================*
  24. * system header files;
  25. *--------------------------------------------------------------------*/
  26. #include <stdlib.h>
  27. #include <limits.h>
  28. #include <memory.h>
  29. #include <signal.h>
  30. #include <unistd.h>
  31. /*====================================================================*
  32. * custom header files;
  33. *--------------------------------------------------------------------*/
  34. #include "../tools/getoptv.h"
  35. #include "../tools/putoptv.h"
  36. #include "../tools/memory.h"
  37. #include "../tools/number.h"
  38. #include "../tools/error.h"
  39. #include "../tools/files.h"
  40. #include "../tools/flags.h"
  41. #include "../ether/ether.h"
  42. #include "../ether/channel.h"
  43. /*====================================================================*
  44. * custom source files;
  45. *--------------------------------------------------------------------*/
  46. #ifndef MAKEFILE
  47. #include "../tools/getoptv.c"
  48. #include "../tools/putoptv.c"
  49. #include "../tools/version.c"
  50. #include "../tools/efreopen.c"
  51. #include "../tools/basespec.c"
  52. #include "../tools/uintspec.c"
  53. #include "../tools/todigit.c"
  54. #include "../tools/hexdump.c"
  55. #include "../tools/hexencode.c"
  56. #include "../tools/error.c"
  57. #endif
  58. #ifndef MAKEFILE
  59. #include "../ether/channel.c"
  60. #include "../ether/openchannel.c"
  61. #include "../ether/closechannel.c"
  62. #include "../ether/readpacket.c"
  63. #include "../ether/sendpacket.c"
  64. #endif
  65. /*====================================================================*
  66. * program constants;
  67. *--------------------------------------------------------------------*/
  68. #define EDSU_INTERFACE "PLC"
  69. #define EDSU_ETHERTYPE ETH_P_802_2
  70. #define EDSU_PAUSE 0
  71. /*====================================================================*
  72. *
  73. * signed function (struct channel * channel, unsigned pause, signed fd);
  74. *
  75. * read a file and transmit it over network as a stream of Ethernet
  76. * frames; pause between frames to prevent over-loading the remote
  77. * host;
  78. *
  79. *--------------------------------------------------------------------*/
  80. signed function (struct channel * channel, unsigned pause, signed fd)
  81. {
  82. struct ethernet_frame frame;
  83. signed length = sizeof (frame.frame_data);
  84. memcpy (frame.frame_dhost, channel->peer, sizeof (frame.frame_dhost));
  85. memcpy (frame.frame_shost, channel->host, sizeof (frame.frame_shost));
  86. while ((length = read (fd, frame.frame_data, sizeof (frame.frame_data))) > 0)
  87. {
  88. frame.frame_type = htons (length);
  89. if (length < ETHERMIN)
  90. {
  91. length = ETHERMIN;
  92. }
  93. length += ETHER_HDR_LEN;
  94. if (sendpacket (channel, & frame, length) < 0)
  95. {
  96. error (1, errno, CHANNEL_CANTSEND);
  97. }
  98. sleep (pause);
  99. }
  100. return (0);
  101. }
  102. /*====================================================================*
  103. *
  104. * int main (int argc, char const * argv []);
  105. *
  106. *--------------------------------------------------------------------*/
  107. int main (int argc, char const * argv [])
  108. {
  109. extern struct channel channel;
  110. static char const * optv [] =
  111. {
  112. "e:d:i:p:qv",
  113. "file [file] [...]",
  114. "Qualcomm Atheros Ethernet II Data Send Utility",
  115. "e x\tethertype is (x) [" LITERAL (EDSU_ETHERTYPE) "]",
  116. "d x\tdestination address is (x) [00:B0:52:00:00:01]",
  117. #if defined (WINPCAP) || defined (LIBPCAP)
  118. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  119. #else
  120. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  121. #endif
  122. "p n\tpause (n) seconds between frames [" LITERAL (EDSU_PAUSE) "]",
  123. "q\tquiet mode",
  124. "v\tverbose mode",
  125. (char const *) (0)
  126. };
  127. unsigned pause = EDSU_PAUSE;
  128. signed c;
  129. if (getenv (EDSU_INTERFACE))
  130. {
  131. #if defined (WINPCAP) || defined (LIBPCAP)
  132. channel.ifindex = atoi (getenv (EDSU_INTERFACE));
  133. #else
  134. channel.ifname = strdup (getenv (EDSU_INTERFACE));
  135. #endif
  136. }
  137. channel.type = EDSU_ETHERTYPE;
  138. while (~ (c = getoptv (argc, argv, optv)))
  139. {
  140. switch (c)
  141. {
  142. case 'e':
  143. channel.type = (uint16_t) (basespec (optarg, 16, sizeof (channel.type)));
  144. break;
  145. case 'd':
  146. if (! hexencode (channel.peer, sizeof (channel.peer), optarg))
  147. {
  148. error (1, errno, "%s", optarg);
  149. }
  150. break;
  151. case 'i':
  152. #if defined (WINPCAP) || defined (LIBPCAP)
  153. channel.ifindex = atoi (optarg);
  154. #else
  155. channel.ifname = optarg;
  156. #endif
  157. break;
  158. case 'p':
  159. pause = (unsigned) (uintspec (optarg, 0, UCHAR_MAX));
  160. break;
  161. case 'q':
  162. _setbits (channel.flags, CHANNEL_SILENCE);
  163. break;
  164. case 'v':
  165. _setbits (channel.flags, CHANNEL_VERBOSE);
  166. break;
  167. default:
  168. break;
  169. }
  170. }
  171. argc -= optind;
  172. argv += optind;
  173. openchannel (& channel);
  174. if (! argc)
  175. {
  176. function (& channel, pause, STDIN_FILENO);
  177. }
  178. while ((argc) && (* argv))
  179. {
  180. if (efreopen (* argv, "rb", stdin))
  181. {
  182. function (& channel, pause, fileno (stdin));
  183. }
  184. argc--;
  185. argv++;
  186. }
  187. closechannel (& channel);
  188. exit (0);
  189. }