int6krate.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*
  42. *
  43. * int6krate.c - Atheros INT6x00 PHY Rate monitor
  44. *
  45. *
  46. * Contributor(s):
  47. * Charles Maier
  48. *
  49. *--------------------------------------------------------------------*/
  50. /*====================================================================*"
  51. * system header files;
  52. *--------------------------------------------------------------------*/
  53. #include <unistd.h>
  54. #include <stdlib.h>
  55. #include <stdint.h>
  56. #include <limits.h>
  57. /*====================================================================*
  58. * custom header files;
  59. *--------------------------------------------------------------------*/
  60. #include "../tools/getoptv.h"
  61. #include "../tools/putoptv.h"
  62. #include "../tools/memory.h"
  63. #include "../tools/number.h"
  64. #include "../tools/symbol.h"
  65. #include "../tools/types.h"
  66. #include "../tools/flags.h"
  67. #include "../tools/files.h"
  68. #include "../tools/error.h"
  69. #include "../plc/plc.h"
  70. /*====================================================================*
  71. * custom source files;
  72. *--------------------------------------------------------------------*/
  73. #ifndef MAKEFILE
  74. #include "../plc/chipset.c"
  75. #include "../plc/Devices.c"
  76. #include "../plc/Confirm.c"
  77. #include "../plc/Display.c"
  78. #include "../plc/Failure.c"
  79. #include "../plc/Request.c"
  80. #include "../plc/ReadMME.c"
  81. #include "../plc/SendMME.c"
  82. #include "../plc/Antiphon.c"
  83. #include "../plc/LocalDevices.c"
  84. #include "../plc/ResetDevice.c"
  85. #include "../plc/VersionInfo1.c"
  86. #include "../plc/StationRole.c"
  87. #include "../plc/PhyRates1.c"
  88. #include "../plc/Traffic1.c"
  89. #include "../plc/Transmit.c"
  90. #include "../plc/NetworkTraffic1.c"
  91. #include "../plc/WaitForStart.c"
  92. #endif
  93. #ifndef MAKEFILE
  94. #include "../tools/getoptv.c"
  95. #include "../tools/putoptv.c"
  96. #include "../tools/version.c"
  97. #include "../tools/uintspec.c"
  98. #include "../tools/hexdump.c"
  99. #include "../tools/hexencode.c"
  100. #include "../tools/hexdecode.c"
  101. #include "../tools/todigit.c"
  102. #include "../tools/checkfilename.c"
  103. #include "../tools/checksum32.c"
  104. #include "../tools/error.c"
  105. #include "../tools/fdchecksum32.c"
  106. #include "../tools/hexstring.c"
  107. #include "../tools/synonym.c"
  108. #include "../tools/typename.c"
  109. #endif
  110. #ifndef MAKEFILE
  111. #include "../ether/openchannel.c"
  112. #include "../ether/closechannel.c"
  113. #include "../ether/readpacket.c"
  114. #include "../ether/sendpacket.c"
  115. #include "../ether/channel.c"
  116. #endif
  117. #ifndef MAKEFILE
  118. #include "../mme/EthernetHeader.c"
  119. #include "../mme/EthernetHeader.c"
  120. #include "../mme/QualcommHeader.c"
  121. #include "../mme/QualcommHeader1.c"
  122. #include "../mme/UnwantedMessage.c"
  123. #include "../mme/MMECode.c"
  124. #endif
  125. /*====================================================================*
  126. * program constants;
  127. *--------------------------------------------------------------------*/
  128. #define INT6KRATE_WAIT 0
  129. #define INT6KRATE_LOOP 1
  130. /*====================================================================*
  131. *
  132. * void manager (struct plc * plc, signed count, signed pause);
  133. *
  134. * perform operations in logical order despite any order specfied
  135. * on the command line; for example, read PIB before writing PIB;
  136. *
  137. * operation order is controlled by the order of "if" statements
  138. * shown here; the entire operation sequence can be repeated with
  139. * an optional pause between each iteration;
  140. *
  141. *
  142. *--------------------------------------------------------------------*/
  143. void manager (struct plc * plc, signed count, signed pause)
  144. {
  145. while (count--)
  146. {
  147. if (_anyset (plc->flags, PLC_VERSION))
  148. {
  149. VersionInfo1 (plc);
  150. }
  151. if (_anyset (plc->flags, PLC_LOCAL_TRAFFIC))
  152. {
  153. Traffic1 (plc);
  154. }
  155. if (_anyset (plc->flags, PLC_NETWORK_TRAFFIC))
  156. {
  157. NetworkTraffic1 (plc);
  158. }
  159. if (_anyset (plc->flags, PLC_NETWORK))
  160. {
  161. PhyRates1 (plc);
  162. }
  163. if (_anyset (plc->flags, PLC_RESET_DEVICE))
  164. {
  165. ResetDevice (plc);
  166. }
  167. sleep (pause);
  168. }
  169. return;
  170. }
  171. /*====================================================================*
  172. *
  173. * int main (int argc, char const * argv[]);
  174. *
  175. * parse command line, populate plc structure and perform selected
  176. * operations; show help summary if asked; see getoptv and putoptv
  177. * to understand command line parsing and help summary display; see
  178. * plc.h for the definition of struct plc;
  179. *
  180. * the command line accepts multiple MAC addresses and the program
  181. * performs the specified operations on each address, in turn; the
  182. * address order is significant but the option order is not; the
  183. * default address is a local broadcast that causes all devices on
  184. * the local H1 interface to respond but not those at the remote
  185. * end of the powerline;
  186. *
  187. * the default address is 00:B0:52:00:00:01; omitting the address
  188. * will automatically address the local device; some options will
  189. * cancel themselves if this makes no sense;
  190. *
  191. * the default interface is eth1 because most people use eth0 as
  192. * their principle network connection; you can specify another
  193. * interface with -i or define environment string PLC to make
  194. * that the default interface and save typing;
  195. *
  196. *
  197. *--------------------------------------------------------------------*/
  198. int main (int argc, char const * argv [])
  199. {
  200. extern struct channel channel;
  201. static char const * optv [] =
  202. {
  203. "cd:ei:l:o:nqrRtTuvw:x",
  204. "device [device] [...]",
  205. "Qualcomm Atheros INT6x00 PHY Rate Monitor",
  206. "c\tdisplay coded PHY rates",
  207. "d n\ttraffic duration is (n) seconds per leg [" LITERAL (PLC_ECHOTIME) "]",
  208. "e\tredirect stderr to stdout",
  209. #if defined (WINPCAP) || defined (LIBPCAP)
  210. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  211. #else
  212. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  213. #endif
  214. "l n\tloop (n) times [" LITERAL (INT6KRATE_LOOP) "]",
  215. "n\tnetwork TX/RX information",
  216. "o n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  217. "q\tquiet mode",
  218. "r\trequest device information",
  219. "R\treset device with VS_RS_DEV",
  220. "t\tgenerate network traffic (one-to-many)",
  221. "T\tgenerate network traffic (many-to-many)",
  222. "u\tdisplay uncoded PHY rates",
  223. "v\tverbose mode",
  224. "w n\twait (n) seconds [" LITERAL (INT6KRATE_WAIT) "]",
  225. "x\texit on error",
  226. (char const *) (0)
  227. };
  228. #include "../plc/plc.c"
  229. signed loop = INT6KRATE_LOOP;
  230. signed wait = INT6KRATE_WAIT;
  231. signed c;
  232. optind = 1;
  233. if (getenv (PLCDEVICE))
  234. {
  235. #if defined (WINPCAP) || defined (LIBPCAP)
  236. channel.ifindex = atoi (getenv (PLCDEVICE));
  237. #else
  238. channel.ifname = strdup (getenv (PLCDEVICE));
  239. #endif
  240. }
  241. plc.timer = PLC_ECHOTIME;
  242. while ((c = getoptv (argc, argv, optv)) != -1)
  243. {
  244. switch (c)
  245. {
  246. case 'c':
  247. _clrbits (plc.flags, PLC_UNCODED_RATES);
  248. break;
  249. case 'd':
  250. plc.timer = (unsigned)(uintspec (optarg, 1, 60));
  251. break;
  252. case 'e':
  253. dup2 (STDOUT_FILENO, STDERR_FILENO);
  254. break;
  255. case 'i':
  256. #if defined (WINPCAP) || defined (LIBPCAP)
  257. channel.ifindex = atoi (optarg);
  258. #else
  259. channel.ifname = optarg;
  260. #endif
  261. break;
  262. case 'l':
  263. loop = (unsigned)(uintspec (optarg, 0, UINT_MAX));
  264. break;
  265. case 'n':
  266. _setbits (plc.flags, PLC_NETWORK);
  267. break;
  268. case 'o':
  269. channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
  270. break;
  271. case 'q':
  272. _setbits (plc.flags, PLC_SILENCE);
  273. break;
  274. case 'r':
  275. _setbits (plc.flags, PLC_VERSION);
  276. break;
  277. case 'R':
  278. _setbits (plc.flags, PLC_RESET_DEVICE);
  279. break;
  280. case 't':
  281. _setbits (plc.flags, PLC_LOCAL_TRAFFIC);
  282. break;
  283. case 'T':
  284. _setbits (plc.flags, PLC_NETWORK_TRAFFIC);
  285. break;
  286. case 'u':
  287. _setbits (plc.flags, PLC_UNCODED_RATES);
  288. break;
  289. case 'v':
  290. _setbits (channel.flags, CHANNEL_VERBOSE);
  291. _setbits (plc.flags, PLC_VERBOSE);
  292. break;
  293. case 'w':
  294. wait = (unsigned)(uintspec (optarg, 0, 3600));
  295. break;
  296. case 'x':
  297. _setbits (plc.flags, PLC_BAILOUT);
  298. break;
  299. default:
  300. break;
  301. }
  302. }
  303. argc -= optind;
  304. argv += optind;
  305. if (_allclr (plc.flags, (PLC_VERSION | PLC_LOCAL_TRAFFIC | PLC_NETWORK_TRAFFIC | PLC_RESET_DEVICE)))
  306. {
  307. _setbits (plc.flags, PLC_NETWORK);
  308. }
  309. openchannel (&channel);
  310. if (!(plc.message = malloc (sizeof (* plc.message))))
  311. {
  312. error (1, errno, PLC_NOMEMORY);
  313. }
  314. if (!argc)
  315. {
  316. manager (&plc, loop, wait);
  317. }
  318. while ((argc) && (* argv))
  319. {
  320. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  321. {
  322. error (1, errno, PLC_BAD_MAC, * argv);
  323. }
  324. manager (&plc, loop, wait);
  325. argv++;
  326. argc--;
  327. }
  328. free (plc.message);
  329. closechannel (&channel);
  330. exit (0);
  331. }