plcrate.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. * plcrate.c -
  44. *
  45. *
  46. * Contributor(s):
  47. * Nathaniel Houghton
  48. * Charles Maier
  49. *
  50. *--------------------------------------------------------------------*/
  51. /*====================================================================*"
  52. * system header files;
  53. *--------------------------------------------------------------------*/
  54. #include <unistd.h>
  55. #include <stdlib.h>
  56. #include <stdint.h>
  57. #include <limits.h>
  58. /*====================================================================*
  59. * custom header files;
  60. *--------------------------------------------------------------------*/
  61. #include "../tools/getoptv.h"
  62. #include "../tools/putoptv.h"
  63. #include "../tools/memory.h"
  64. #include "../tools/number.h"
  65. #include "../tools/symbol.h"
  66. #include "../tools/types.h"
  67. #include "../tools/flags.h"
  68. #include "../tools/files.h"
  69. #include "../tools/error.h"
  70. #include "../plc/plc.h"
  71. /*====================================================================*
  72. * custom source files;
  73. *--------------------------------------------------------------------*/
  74. #ifndef MAKEFILE
  75. #include "../plc/chipset.c"
  76. #include "../plc/Devices.c"
  77. #include "../plc/Confirm.c"
  78. #include "../plc/Display.c"
  79. #include "../plc/Failure.c"
  80. #include "../plc/Request.c"
  81. #include "../plc/ReadMME.c"
  82. #include "../plc/SendMME.c"
  83. #include "../plc/LocalDevices.c"
  84. #include "../plc/PLCSelect.c"
  85. #include "../plc/ResetDevice.c"
  86. #include "../plc/PhyRates2.c"
  87. #include "../plc/Traffic2.c"
  88. #include "../plc/NetworkTraffic2.c"
  89. #include "../plc/Antiphon.c"
  90. #include "../plc/Transmit.c"
  91. #include "../plc/VersionInfo2.c"
  92. #include "../plc/WaitForStart.c"
  93. #endif
  94. #ifndef MAKEFILE
  95. #include "../tools/getoptv.c"
  96. #include "../tools/putoptv.c"
  97. #include "../tools/version.c"
  98. #include "../tools/uintspec.c"
  99. #include "../tools/hexdump.c"
  100. #include "../tools/hexencode.c"
  101. #include "../tools/hexdecode.c"
  102. #include "../tools/todigit.c"
  103. #include "../tools/checkfilename.c"
  104. #include "../tools/checksum32.c"
  105. #include "../tools/error.c"
  106. #include "../tools/fdchecksum32.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 PLCRATE_WAIT 0
  129. #define PLCRATE_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;
  136. *
  137. * operation order is controlled by the order of "if" statements
  138. * shown here; the entire sequence can be repeated with optional
  139. * 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. VersionInfo2 (plc);
  150. }
  151. if (_anyset (plc->flags, PLC_LOCAL_TRAFFIC))
  152. {
  153. Traffic2 (plc);
  154. }
  155. if (_anyset (plc->flags, PLC_NETWORK_TRAFFIC))
  156. {
  157. NetworkTraffic2 (plc);
  158. }
  159. if (_anyset (plc->flags, PLC_NETWORK))
  160. {
  161. PhyRates2 (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:nqrRstTuvw:x",
  204. "device [device] [...]",
  205. "Qualcomm Atheros PLC 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 (PLCRATE_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. "s\tgenerate TX traffic only",
  221. "t\tgenerate network traffic (one-to-many)",
  222. "T\tgenerate network traffic (many-to-many)",
  223. "u\tdisplay uncoded PHY rates",
  224. "v\tverbose mode",
  225. "w n\twait (n) seconds [" LITERAL (PLCRATE_WAIT) "]",
  226. "x\texit on error",
  227. (char const *) (0)
  228. };
  229. #include "../plc/plc.c"
  230. signed loop = PLCRATE_LOOP;
  231. signed wait = PLCRATE_WAIT;
  232. signed c;
  233. optind = 1;
  234. if (getenv (PLCDEVICE))
  235. {
  236. #if defined (WINPCAP) || defined (LIBPCAP)
  237. channel.ifindex = atoi (getenv (PLCDEVICE));
  238. #else
  239. channel.ifname = strdup (getenv (PLCDEVICE));
  240. #endif
  241. }
  242. plc.timer = PLC_ECHOTIME;
  243. while ((c = getoptv (argc, argv, optv)) != -1)
  244. {
  245. switch (c)
  246. {
  247. case 'c':
  248. _clrbits (plc.flags, PLC_UNCODED_RATES);
  249. break;
  250. case 'd':
  251. plc.timer = (unsigned)(uintspec (optarg, 1, 60));
  252. break;
  253. case 'e':
  254. dup2 (STDOUT_FILENO, STDERR_FILENO);
  255. break;
  256. case 'i':
  257. #if defined (WINPCAP) || defined (LIBPCAP)
  258. channel.ifindex = atoi (optarg);
  259. #else
  260. channel.ifname = optarg;
  261. #endif
  262. break;
  263. case 'l':
  264. loop = (unsigned)(uintspec (optarg, 0, UINT_MAX));
  265. break;
  266. case 'n':
  267. _setbits (plc.flags, PLC_NETWORK);
  268. break;
  269. case 'o':
  270. channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
  271. break;
  272. case 'q':
  273. _setbits (plc.flags, PLC_SILENCE);
  274. break;
  275. case 'r':
  276. _setbits (plc.flags, PLC_VERSION);
  277. break;
  278. case 'R':
  279. _setbits (plc.flags, PLC_RESET_DEVICE);
  280. break;
  281. case 's':
  282. _setbits (plc.flags, PLC_TXONLY);
  283. break;
  284. case 't':
  285. _setbits (plc.flags, PLC_LOCAL_TRAFFIC);
  286. break;
  287. case 'T':
  288. _setbits (plc.flags, PLC_NETWORK_TRAFFIC);
  289. break;
  290. case 'u':
  291. _setbits (plc.flags, PLC_UNCODED_RATES);
  292. break;
  293. case 'v':
  294. _setbits (channel.flags, CHANNEL_VERBOSE);
  295. _setbits (plc.flags, PLC_VERBOSE);
  296. break;
  297. case 'w':
  298. wait = (unsigned)(uintspec (optarg, 0, 3600));
  299. break;
  300. case 'x':
  301. _setbits (plc.flags, PLC_BAILOUT);
  302. break;
  303. default:
  304. break;
  305. }
  306. }
  307. argc -= optind;
  308. argv += optind;
  309. if (_allclr (plc.flags, (PLC_VERSION | PLC_LOCAL_TRAFFIC | PLC_NETWORK_TRAFFIC | PLC_RESET_DEVICE)))
  310. {
  311. _setbits (plc.flags, PLC_NETWORK);
  312. }
  313. openchannel (&channel);
  314. if (!(plc.message = malloc (sizeof (* plc.message))))
  315. {
  316. error (1, errno, PLC_NOMEMORY);
  317. }
  318. if (!argc)
  319. {
  320. manager (&plc, loop, wait);
  321. }
  322. while ((argc) && (* argv))
  323. {
  324. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  325. {
  326. error (1, errno, PLC_BAD_MAC, * argv);
  327. }
  328. manager (&plc, loop, wait);
  329. argv++;
  330. argc--;
  331. }
  332. free (plc.message);
  333. closechannel (&channel);
  334. exit (0);
  335. }