plcstat.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. * plcstat.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/Confirm.c"
  76. #include "../plc/Display.c"
  77. #include "../plc/Failure.c"
  78. #include "../plc/Request.c"
  79. #include "../plc/ReadMME.c"
  80. #include "../plc/SendMME.c"
  81. #include "../plc/Devices.c"
  82. #include "../plc/LinkStatistics.c"
  83. #include "../plc/LocalDevices.c"
  84. #include "../plc/NetworkInformation2.c"
  85. #include "../plc/Topology2.c"
  86. #include "../plc/Platform.c"
  87. #include "../plc/WaitForStart.c"
  88. #endif
  89. #ifndef MAKEFILE
  90. #include "../tools/error.c"
  91. #include "../tools/getoptv.c"
  92. #include "../tools/putoptv.c"
  93. #include "../tools/version.c"
  94. #include "../tools/uintspec.c"
  95. #include "../tools/hexdump.c"
  96. #include "../tools/hexencode.c"
  97. #include "../tools/hexdecode.c"
  98. #include "../tools/hexstring.c"
  99. #include "../tools/todigit.c"
  100. #include "../tools/synonym.c"
  101. #include "../tools/typename.c"
  102. #endif
  103. #ifndef MAKEFILE
  104. #include "../ether/openchannel.c"
  105. #include "../ether/closechannel.c"
  106. #include "../ether/readpacket.c"
  107. #include "../ether/sendpacket.c"
  108. #include "../ether/channel.c"
  109. #endif
  110. #ifndef MAKEFILE
  111. #include "../mme/EthernetHeader.c"
  112. #include "../mme/QualcommHeader.c"
  113. #include "../mme/QualcommHeader1.c"
  114. #include "../mme/UnwantedMessage.c"
  115. #include "../mme/MMECode.c"
  116. #endif
  117. /*====================================================================*
  118. * program constants;
  119. *--------------------------------------------------------------------*/
  120. #define LINKIDS (sizeof (linkids) / sizeof (struct _term_))
  121. #define DIRECTIONS (sizeof (directions) / sizeof (struct _term_))
  122. #define AMPSTAT_LOOP 1
  123. #define AMPSTAT_WAIT 0
  124. /*====================================================================*
  125. * program constants;
  126. *--------------------------------------------------------------------*/
  127. static const struct _term_ linkids [] =
  128. {
  129. {
  130. "CSMA-ALL",
  131. "0xFC"
  132. },
  133. {
  134. "CSMA-CAP0",
  135. "0x00"
  136. },
  137. {
  138. "CSMA-CAP1",
  139. "0x01"
  140. },
  141. {
  142. "CSMA-CAP2",
  143. "0x02"
  144. },
  145. {
  146. "CSMA-CAP3",
  147. "0x03"
  148. },
  149. {
  150. "CSMA-PEER",
  151. "0xF8"
  152. },
  153. };
  154. static const struct _term_ directions [] =
  155. {
  156. {
  157. "both",
  158. "2"
  159. },
  160. {
  161. "rx",
  162. "1"
  163. },
  164. {
  165. "tx",
  166. "0"
  167. }
  168. };
  169. /*====================================================================*
  170. *
  171. * void manager (struct plc * plc);
  172. *
  173. * perform operations in logical order despite any order specfied
  174. * on the command line; for example read PIB before writing PIB;
  175. *
  176. * operation order is controlled by the order of "if" statements
  177. * shown here; the entire operation sequence can be repeated with
  178. * an optional pause between each iteration;
  179. *
  180. *
  181. *--------------------------------------------------------------------*/
  182. void manager (struct plc * plc, signed count, signed pause)
  183. {
  184. while (count--)
  185. {
  186. if (_anyset (plc->flags, PLC_ANALYSE))
  187. {
  188. Topology2 (plc);
  189. }
  190. if (_anyset (plc->flags, PLC_NETWORK))
  191. {
  192. NetworkInformation2 (plc);
  193. }
  194. if (_anyset (plc->flags, PLC_LINK_STATS))
  195. {
  196. LinkStatistics (plc);
  197. }
  198. sleep (pause);
  199. }
  200. return;
  201. }
  202. /*====================================================================*
  203. *
  204. * int main (int argc, char const * argv[]);
  205. *
  206. * parse command line, populate plc structure and perform selected
  207. * operations; show help summary if asked; see getoptv and putoptv
  208. * to understand command line parsing and help summary display; see
  209. * plc.h for the definition of struct plc;
  210. *
  211. * the command line accepts multiple MAC addresses and the program
  212. * performs the specified operations on each address, in turn; the
  213. * address order is significant but the option order is not; the
  214. * default address is a local broadcast that causes all devices on
  215. * the local H1 interface to respond but not those at the remote
  216. * end of the powerline;
  217. *
  218. * the default address is 00:B0:52:00:00:01; omitting the address
  219. * will automatically address the local device; some options will
  220. * cancel themselves if this makes no sense;
  221. *
  222. * the default interface is eth1 because most people use eth0 as
  223. * their principle network connection; you can specify another
  224. * interface with -i or define environment string PLC to make
  225. * that the default interface and save typing;
  226. *
  227. *
  228. *--------------------------------------------------------------------*/
  229. int main (int argc, char const * argv [])
  230. {
  231. extern struct channel channel;
  232. static char const * optv [] =
  233. {
  234. "Cd:ei:l:mp:qs:tvw:",
  235. "device [device] [...] [> stdout]",
  236. "Qualcomm Atheros AR7x00 Link Statistics",
  237. #if defined (WINPCAP) || defined (LIBPCAP)
  238. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  239. #else
  240. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  241. #endif
  242. "C\tclear statistics without reading using VS_LNK_STATS",
  243. "d n\tdirection (0=tx, 1=rx, 2=both) for VS_LNK_STATS",
  244. "e\tredirect stderr to stdout",
  245. "l n\tloop n times [" LITERAL (AMPSTAT_LOOP) "]",
  246. "s n\tLink ID for VS_LNK_STATS (see Programmer's Guide)",
  247. "m\tprint network membership information using VS_NW_INFO",
  248. "p x\tpeer node address for options -s",
  249. "q\tquiet mode",
  250. "t\tprint network topology using VS_NW_INFO with VS_SW_VER",
  251. "v\tverbose mode",
  252. "w n\twait n seconds [" LITERAL (AMPSTAT_WAIT) "]",
  253. (char const *) (0)
  254. };
  255. #include "../plc/plc.c"
  256. signed loop = AMPSTAT_LOOP;
  257. signed wait = AMPSTAT_WAIT;
  258. signed c;
  259. if (getenv (PLCDEVICE))
  260. {
  261. #if defined (WINPCAP) || defined (LIBPCAP)
  262. channel.ifindex = atoi (getenv (PLCDEVICE));
  263. #else
  264. channel.ifname = strdup (getenv (PLCDEVICE));
  265. #endif
  266. }
  267. optind = 1;
  268. plc.pushbutton = 0;
  269. while ((c = getoptv (argc, argv, optv)) != -1)
  270. {
  271. switch (c)
  272. {
  273. case 'C':
  274. _setbits (plc.flags, PLC_LINK_STATS);
  275. plc.pushbutton = 1;
  276. break;
  277. case 'd':
  278. _setbits (plc.flags, PLC_LINK_STATS);
  279. plc.module = (uint8_t)(uintspec (synonym (optarg, directions, DIRECTIONS), 0, UCHAR_MAX));
  280. break;
  281. case 'e':
  282. dup2 (STDOUT_FILENO, STDERR_FILENO);
  283. break;
  284. case 'i':
  285. #if defined (WINPCAP) || defined (LIBPCAP)
  286. channel.ifindex = atoi (optarg);
  287. #else
  288. channel.ifname = optarg;
  289. #endif
  290. break;
  291. case 'm':
  292. _setbits (plc.flags, PLC_NETWORK);
  293. break;
  294. case 'p':
  295. _setbits (plc.flags, PLC_LINK_STATS);
  296. if (!hexencode (plc.RDA, sizeof (plc.RDA), (char const *)(optarg)))
  297. {
  298. error (1, errno, PLC_BAD_MAC, optarg);
  299. }
  300. break;
  301. case 'l':
  302. loop = (unsigned)(uintspec (optarg, 0, UINT_MAX));
  303. break;
  304. case 'q':
  305. _setbits (channel.flags, CHANNEL_SILENCE);
  306. _setbits (plc.flags, PLC_SILENCE);
  307. break;
  308. case 's':
  309. _setbits (plc.flags, PLC_LINK_STATS);
  310. plc.action = (uint8_t)(uintspec (synonym (optarg, linkids, LINKIDS), 0, UCHAR_MAX));
  311. break;
  312. case 't':
  313. _setbits (plc.flags, PLC_ANALYSE);
  314. break;
  315. case 'v':
  316. _setbits (channel.flags, CHANNEL_VERBOSE);
  317. _setbits (plc.flags, PLC_VERBOSE);
  318. break;
  319. case 'w':
  320. wait = (unsigned)(uintspec (optarg, 0, 3600));
  321. break;
  322. default:
  323. break;
  324. }
  325. }
  326. argc -= optind;
  327. argv += optind;
  328. openchannel (&channel);
  329. if (!(plc.message = malloc (sizeof (* plc.message))))
  330. {
  331. error (1, errno, PLC_NOMEMORY);
  332. }
  333. if (!argc)
  334. {
  335. manager (&plc, loop, wait);
  336. }
  337. while ((argc) && (* argv))
  338. {
  339. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  340. {
  341. error (1, errno, PLC_BAD_MAC, * argv);
  342. }
  343. manager (&plc, loop, wait);
  344. argc--;
  345. argv++;
  346. }
  347. free (plc.message);
  348. closechannel (&channel);
  349. return (0);
  350. }