amprate.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. * amprate.c -
  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/ResetDevice.c"
  83. #include "../plc/LocalDevices.c"
  84. #include "../plc/Antiphon.c"
  85. #include "../plc/NetworkTraffic2.c"
  86. #include "../plc/PhyRates2.c"
  87. #include "../plc/RxRates2.c"
  88. #include "../plc/StationRole.c"
  89. #include "../plc/PLCSelect.c"
  90. #include "../plc/Traffic2.c"
  91. #include "../plc/Transmit.c"
  92. #include "../plc/VersionInfo1.c"
  93. #include "../plc/WaitForStart.c"
  94. #endif
  95. #ifndef MAKEFILE
  96. #include "../tools/getoptv.c"
  97. #include "../tools/putoptv.c"
  98. #include "../tools/version.c"
  99. #include "../tools/uintspec.c"
  100. #include "../tools/hexdump.c"
  101. #include "../tools/hexdecode.c"
  102. #include "../tools/hexencode.c"
  103. #include "../tools/todigit.c"
  104. #include "../tools/checkfilename.c"
  105. #include "../tools/checksum32.c"
  106. #include "../tools/fdchecksum32.c"
  107. #include "../tools/synonym.c"
  108. #include "../tools/typename.c"
  109. #include "../tools/error.c"
  110. #endif
  111. #ifndef MAKEFILE
  112. #include "../ether/openchannel.c"
  113. #include "../ether/closechannel.c"
  114. #include "../ether/readpacket.c"
  115. #include "../ether/sendpacket.c"
  116. #include "../ether/channel.c"
  117. #endif
  118. #ifndef MAKEFILE
  119. #include "../mme/EthernetHeader.c"
  120. #include "../mme/EthernetHeader.c"
  121. #include "../mme/QualcommHeader.c"
  122. #include "../mme/QualcommHeader1.c"
  123. #include "../mme/UnwantedMessage.c"
  124. #include "../mme/MMECode.c"
  125. #endif
  126. /*====================================================================*
  127. * program constants;
  128. *--------------------------------------------------------------------*/
  129. #define AMPRATE_LOOP 1
  130. #define AMPRATE_WAIT 0
  131. /*====================================================================*
  132. *
  133. * void manager (struct plc * plc, signed count, signed pause);
  134. *
  135. * perform operations in logical order despite any order specfied
  136. * on the command line; for example, read PIB before writing PIB;
  137. *
  138. * operation order is controlled by the order of "if" statements
  139. * shown here; the entire operation sequence can be repeated with
  140. * an optional pause between each iteration;
  141. *
  142. *
  143. *--------------------------------------------------------------------*/
  144. void manager (struct plc * plc, signed count, signed pause)
  145. {
  146. while (count--)
  147. {
  148. if (_anyset (plc->flags, PLC_VERSION))
  149. {
  150. VersionInfo1 (plc);
  151. }
  152. if (_anyset (plc->flags, PLC_LOCAL_TRAFFIC))
  153. {
  154. Traffic2 (plc);
  155. }
  156. if (_anyset (plc->flags, PLC_NETWORK_TRAFFIC))
  157. {
  158. NetworkTraffic2 (plc);
  159. }
  160. if (_anyset (plc->flags, PLC_NETWORK))
  161. {
  162. PhyRates2 (plc);
  163. }
  164. if (_anyset (plc->flags, PLC_RXONLY))
  165. {
  166. RxRates2 (plc);
  167. }
  168. if (_anyset (plc->flags, PLC_RESET_DEVICE))
  169. {
  170. ResetDevice (plc);
  171. }
  172. sleep (pause);
  173. }
  174. return;
  175. }
  176. /*====================================================================*
  177. *
  178. * int main (int argc, char const * argv[]);
  179. *
  180. * parse command line, populate plc structure and perform selected
  181. * operations; show help summary if asked; see getoptv and putoptv
  182. * to understand command line parsing and help summary display; see
  183. * plc.h for the definition of struct plc;
  184. *
  185. * the command line accepts multiple MAC addresses and the program
  186. * performs the specified operations on each address, in turn; the
  187. * address order is significant but the option order is not; the
  188. * default address is a local broadcast that causes all devices on
  189. * the local H1 interface to respond but not those at the remote
  190. * end of the powerline;
  191. *
  192. * the default address is 00:B0:52:00:00:01; omitting the address
  193. * will automatically address the local device; some options will
  194. * cancel themselves if this makes no sense;
  195. *
  196. * the default interface is eth1 because most people use eth0 as
  197. * their principle network connection; you can specify another
  198. * interface with -i or define environment string PLC to make
  199. * that the default interface and save typing;
  200. *
  201. *
  202. *--------------------------------------------------------------------*/
  203. int main (int argc, char const * argv [])
  204. {
  205. extern struct channel channel;
  206. static char const * optv [] =
  207. {
  208. "cd:ei:l:nNo:qrRtTuvw:x",
  209. "device [device] [...]",
  210. "Qualcomm Atheros AR7x00 PHY Rate Monitor",
  211. "c\tdisplay coded PHY rates",
  212. "d n\ttraffic duration is (n) seconds per leg [" LITERAL (PLC_ECHOTIME) "]",
  213. "e\tredirect stderr to stdout",
  214. #if defined (WINPCAP) || defined (LIBPCAP)
  215. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  216. #else
  217. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  218. #endif
  219. "l n\tloop (n) times [" LITERAL (AMPRATE_LOOP) "]",
  220. "n\tnetwork TX/RX information",
  221. "N\tnetwork RX only information",
  222. "o n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  223. "q\tquiet mode",
  224. "r\trequest device information",
  225. "R\treset device with VS_RS_DEV",
  226. "t\tgenerate network traffic (one-to-many)",
  227. "T\tgenerate network traffic (many-to-many)",
  228. "u\tdisplay uncoded PHY rates",
  229. "v\tverbose mode",
  230. "w n\twait (n) seconds [" LITERAL (AMPRATE_WAIT) "]",
  231. "x\texit on error",
  232. (char const *) (0)
  233. };
  234. #include "../plc/plc.c"
  235. signed loop = AMPRATE_LOOP;
  236. signed wait = AMPRATE_WAIT;
  237. signed c;
  238. optind = 1;
  239. if (getenv (PLCDEVICE))
  240. {
  241. #if defined (WINPCAP) || defined (LIBPCAP)
  242. channel.ifindex = atoi (getenv (PLCDEVICE));
  243. #else
  244. channel.ifname = strdup (getenv (PLCDEVICE));
  245. #endif
  246. }
  247. plc.timer = PLC_ECHOTIME;
  248. while ((c = getoptv (argc, argv, optv)) != -1)
  249. {
  250. switch (c)
  251. {
  252. case 'c':
  253. _clrbits (plc.flags, PLC_UNCODED_RATES);
  254. break;
  255. case 'd':
  256. plc.timer = (unsigned)(uintspec (optarg, 1, 60));
  257. break;
  258. case 'e':
  259. dup2 (STDOUT_FILENO, STDERR_FILENO);
  260. break;
  261. case 'i':
  262. #if defined (WINPCAP) || defined (LIBPCAP)
  263. channel.ifindex = atoi (optarg);
  264. #else
  265. channel.ifname = optarg;
  266. #endif
  267. break;
  268. case 'l':
  269. loop = (unsigned)(uintspec (optarg, 0, UINT_MAX));
  270. break;
  271. case 'n':
  272. _setbits (plc.flags, PLC_NETWORK);
  273. break;
  274. case 'N':
  275. _setbits (plc.flags, PLC_RXONLY);
  276. break;
  277. case 'o':
  278. channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
  279. break;
  280. case 'q':
  281. _setbits (plc.flags, PLC_SILENCE);
  282. break;
  283. case 'r':
  284. _setbits (plc.flags, PLC_VERSION);
  285. break;
  286. case 'R':
  287. _setbits (plc.flags, PLC_RESET_DEVICE);
  288. break;
  289. case 't':
  290. _setbits (plc.flags, PLC_LOCAL_TRAFFIC);
  291. break;
  292. case 'T':
  293. _setbits (plc.flags, PLC_NETWORK_TRAFFIC);
  294. break;
  295. case 'u':
  296. _setbits (plc.flags, PLC_UNCODED_RATES);
  297. break;
  298. case 'v':
  299. _setbits (channel.flags, CHANNEL_VERBOSE);
  300. _setbits (plc.flags, PLC_VERBOSE);
  301. break;
  302. case 'w':
  303. wait = (unsigned)(uintspec (optarg, 0, 3600));
  304. break;
  305. case 'x':
  306. _setbits (plc.flags, PLC_BAILOUT);
  307. break;
  308. default:
  309. break;
  310. }
  311. }
  312. argc -= optind;
  313. argv += optind;
  314. if (_allclr (plc.flags, (PLC_VERSION | PLC_LOCAL_TRAFFIC | PLC_NETWORK_TRAFFIC | PLC_RESET_DEVICE | PLC_RXONLY)))
  315. {
  316. _setbits (plc.flags, PLC_NETWORK);
  317. }
  318. openchannel (&channel);
  319. if (!(plc.message = malloc (sizeof (* plc.message))))
  320. {
  321. error (1, errno, PLC_NOMEMORY);
  322. }
  323. if (!argc)
  324. {
  325. manager (&plc, loop, wait);
  326. }
  327. while ((argc) && (* argv))
  328. {
  329. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  330. {
  331. error (1, errno, PLC_BAD_MAC, * argv);
  332. }
  333. manager (&plc, loop, wait);
  334. argv++;
  335. argc--;
  336. }
  337. free (plc.message);
  338. closechannel (&channel);
  339. exit (0);
  340. }