ampstat.c 10 KB

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