int6keth.c.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. int6keth.c
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='int6kdetect.c.html' title=' int6kdetect.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='int6kf.c.html' title=' int6kf.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  24. *
  25. * All rights reserved.
  26. *
  27. * Redistribution and use in source and binary forms, with or
  28. * without modification, are permitted (subject to the limitations
  29. * in the disclaimer below) provided that the following conditions
  30. * are met:
  31. *
  32. * * Redistributions of source code must retain the above copyright
  33. * notice, this list of conditions and the following disclaimer.
  34. *
  35. * * Redistributions in binary form must reproduce the above
  36. * copyright notice, this list of conditions and the following
  37. * disclaimer in the documentation and/or other materials
  38. * provided with the distribution.
  39. *
  40. * * Neither the name of Qualcomm Atheros nor the names of
  41. * its contributors may be used to endorse or promote products
  42. * derived from this software without specific prior written
  43. * permission.
  44. *
  45. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  46. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  47. * COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; AND ANY EXPRESS OR
  48. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  49. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  50. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  51. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  52. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  53. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  54. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  56. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  57. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59. *
  60. *--------------------------------------------------------------------*/
  61. /*====================================================================*
  62. *
  63. * int6keth.c - Atheros Ethernet PHY Settings;
  64. *
  65. *
  66. * Contributor(s):
  67. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  68. * Matthieu Poullet &lt;m.poullet@avm.de&gt;
  69. *
  70. *--------------------------------------------------------------------*/
  71. /*====================================================================*&quot;
  72. * system header files;
  73. *--------------------------------------------------------------------*/
  74. #include &lt;unistd.h&gt;
  75. #include &lt;stdlib.h&gt;
  76. #include &lt;stdint.h&gt;
  77. /*====================================================================*
  78. * custom header files;
  79. *--------------------------------------------------------------------*/
  80. #include &quot;../tools/getoptv.h&quot;
  81. #include &quot;../tools/putoptv.h&quot;
  82. #include &quot;../tools/memory.h&quot;
  83. #include &quot;../tools/number.h&quot;
  84. #include &quot;../tools/symbol.h&quot;
  85. #include &quot;../tools/types.h&quot;
  86. #include &quot;../tools/flags.h&quot;
  87. #include &quot;../tools/files.h&quot;
  88. #include &quot;../tools/error.h&quot;
  89. #include &quot;../plc/plc.h&quot;
  90. #include &quot;../ether/channel.h&quot;
  91. #include &quot;../mme/mme.h&quot;
  92. /*====================================================================*
  93. * custom source files;
  94. *--------------------------------------------------------------------*/
  95. #ifndef MAKEFILE
  96. #include &quot;../mme/UnwantedMessage.c&quot;
  97. #include &quot;../plc/Devices.c&quot;
  98. #endif
  99. #ifndef MAKEFILE
  100. #include &quot;../tools/assist.c&quot;
  101. #include &quot;../tools/error.c&quot;
  102. #include &quot;../tools/codelist.c&quot;
  103. #include &quot;../tools/getoptv.c&quot;
  104. #include &quot;../tools/putoptv.c&quot;
  105. #include &quot;../tools/version.c&quot;
  106. #include &quot;../tools/uintspec.c&quot;
  107. #include &quot;../tools/hexdump.c&quot;
  108. #include &quot;../tools/hexencode.c&quot;
  109. #include &quot;../tools/hexdecode.c&quot;
  110. #include &quot;../tools/hexstring.c&quot;
  111. #include &quot;../tools/todigit.c&quot;
  112. #include &quot;../tools/checkfilename.c&quot;
  113. #include &quot;../tools/checksum32.c&quot;
  114. #include &quot;../tools/fdchecksum32.c&quot;
  115. #include &quot;../tools/strfbits.c&quot;
  116. #include &quot;../tools/typename.c&quot;
  117. #include &quot;../tools/lookup.c&quot;
  118. #include &quot;../tools/synonym.c&quot;
  119. #include &quot;../tools/uintspec.c&quot;
  120. #endif
  121. #ifndef MAKEFILE
  122. #include &quot;../ether/openchannel.c&quot;
  123. #include &quot;../ether/closechannel.c&quot;
  124. #include &quot;../ether/readpacket.c&quot;
  125. #include &quot;../ether/sendpacket.c&quot;
  126. #include &quot;../ether/channel.c&quot;
  127. #endif
  128. #ifndef MAKEFILE
  129. #include &quot;../mme/MMECode.c&quot;
  130. #include &quot;../mme/EthernetHeader.c&quot;
  131. #include &quot;../mme/QualcommHeader.c&quot;
  132. #endif
  133. /*====================================================================*
  134. * program constants;
  135. *--------------------------------------------------------------------*/
  136. #define MCONTROL_READ 0x00
  137. #define MCONTROL_WRITE 0x01
  138. #define ETH_PORT 0
  139. #define NEGOTIATE SIZEOF (negotiate)
  140. #define SPEEDS SIZEOF (speeds)
  141. #define DUPLEX SIZEOF (duplex)
  142. #define CONTROL SIZEOF (control)
  143. #define ADVCAP SIZEOF (advcap)
  144. static struct _code_ const negotiate [] =
  145. {
  146. {
  147. 0,
  148. &quot;Off&quot;
  149. },
  150. {
  151. 1,
  152. &quot;On&quot;
  153. }
  154. };
  155. static struct _code_ const speeds [] =
  156. {
  157. {
  158. 0,
  159. &quot;10&quot;
  160. },
  161. {
  162. 1,
  163. &quot;100&quot;
  164. },
  165. {
  166. 2,
  167. &quot;1000&quot;
  168. }
  169. };
  170. static struct _code_ const duplex [] =
  171. {
  172. {
  173. 0,
  174. &quot;Half&quot;
  175. },
  176. {
  177. 1,
  178. &quot;Full&quot;
  179. }
  180. };
  181. static struct _code_ const control [] =
  182. {
  183. {
  184. 0,
  185. &quot;Off&quot;
  186. },
  187. {
  188. 1,
  189. &quot;Tx&quot;
  190. },
  191. {
  192. 2,
  193. &quot;Rx&quot;
  194. },
  195. {
  196. 3,
  197. &quot;On&quot;
  198. }
  199. };
  200. static struct _code_ const advcap [] =
  201. {
  202. {
  203. 1,
  204. &quot;100Full&quot;
  205. },
  206. {
  207. 2,
  208. &quot;100Half&quot;
  209. },
  210. {
  211. 4,
  212. &quot;10Full&quot;
  213. },
  214. {
  215. 8,
  216. &quot;10Half&quot;
  217. },
  218. {
  219. 16,
  220. &quot;1000Full&quot;
  221. }
  222. };
  223. #define RATES SIZEOF (rates)
  224. #define MODES SIZEOF (modes)
  225. #define LINKS SIZEOF (links)
  226. #define FLOWS SIZEOF (flows)
  227. static char const * rates [] =
  228. {
  229. &quot;10&quot;,
  230. &quot;100&quot;,
  231. &quot;1000&quot;
  232. };
  233. static char const * modes [] =
  234. {
  235. &quot;Half&quot;,
  236. &quot;Full&quot;
  237. };
  238. static char const * links [] =
  239. {
  240. &quot;Unknown&quot;,
  241. &quot;Off&quot;,
  242. &quot;On&quot;
  243. };
  244. static char const * flows [] =
  245. {
  246. &quot;Off&quot;,
  247. &quot;Tx&quot;,
  248. &quot;Rx&quot;,
  249. &quot;On&quot;
  250. };
  251. /*====================================================================*
  252. * program variables;
  253. *--------------------------------------------------------------------*/
  254. #ifndef __GNUC__
  255. #pragma pack (push,1)
  256. #endif
  257. typedef struct __packed phy_settings
  258. {
  259. uint8_t MCONTROL;
  260. uint8_t AUTONEGOTIATE;
  261. uint8_t ADVCAPS;
  262. uint8_t ESPEED;
  263. uint8_t EDUPLEX;
  264. uint8_t EFLOWCONTROL;
  265. }
  266. phy_settings;
  267. typedef struct __packed phy_readings
  268. {
  269. uint8_t MSTATUS;
  270. uint8_t ESPEED;
  271. uint8_t EDUPLEX;
  272. uint8_t ELINKSTATUS;
  273. uint8_t EFLOWCONTROL;
  274. }
  275. phy_readings;
  276. #ifndef __GNUC__
  277. #pragma pack (pop)
  278. #endif
  279. /*====================================================================*
  280. *
  281. * signed PHYSettings (struct channel * channel, struct phy_settings * settings, flag_t flags);
  282. *
  283. * plc.h
  284. *
  285. * read and display Ethernet PHY settings or write then read and
  286. * display settings;
  287. *
  288. *
  289. * Contributor(s):
  290. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  291. *
  292. *--------------------------------------------------------------------*/
  293. signed PHYSettings (struct channel * channel, struct phy_settings * settings, flag_t flags)
  294. {
  295. struct message message;
  296. signed packetsize;
  297. #ifndef __GNUC__
  298. #pragma pack (push,1)
  299. #endif
  300. struct __packed vs_enet_settings_request
  301. {
  302. struct ethernet_hdr ethernet;
  303. struct qualcomm_hdr qualcomm;
  304. uint8_t MCONTROL;
  305. uint8_t AUTONEGOTIATE;
  306. uint8_t ADVCAPS;
  307. uint8_t ESPEED;
  308. uint8_t EDUPLEX;
  309. uint8_t EFLOWCONTROL;
  310. }
  311. * request = (struct vs_enet_settings_request *) (&amp;message);
  312. struct __packed vs_enet_settings_confirm
  313. {
  314. struct ethernet_hdr ethernet;
  315. struct qualcomm_hdr qualcomm;
  316. uint8_t MSTATUS;
  317. uint8_t ESPEED;
  318. uint8_t EDUPLEX;
  319. uint8_t ELINKSTATUS;
  320. uint8_t EFLOWCONTROL;
  321. }
  322. * confirm = (struct vs_enet_settings_confirm *) (&amp;message);
  323. #ifndef __GNUC__
  324. #pragma pack (pop)
  325. #endif
  326. char address [ETHER_ADDR_LEN * 3];
  327. memset (&amp;message, 0, sizeof (message));
  328. EthernetHeader (&amp;request-&gt;ethernet, channel-&gt;peer, channel-&gt;host, channel-&gt;type);
  329. QualcommHeader (&amp;request-&gt;qualcomm, 0, (VS_ENET_SETTINGS | MMTYPE_REQ));
  330. request-&gt;MCONTROL = settings-&gt;MCONTROL;
  331. request-&gt;AUTONEGOTIATE = settings-&gt;AUTONEGOTIATE;
  332. request-&gt;ADVCAPS = settings-&gt;ADVCAPS;
  333. request-&gt;ESPEED = settings-&gt;ESPEED;
  334. request-&gt;EDUPLEX = settings-&gt;EDUPLEX;
  335. request-&gt;EFLOWCONTROL = settings-&gt;EFLOWCONTROL;
  336. if (sendpacket (channel, &amp;message, (ETHER_MIN_LEN - ETHER_CRC_LEN)) &lt; 0)
  337. {
  338. error (1, errno, CHANNEL_CANTSEND);
  339. }
  340. while ((packetsize = readpacket (channel, &amp;message, sizeof (message))) &gt; 0)
  341. {
  342. if (UnwantedMessage (&amp;message, packetsize, 0, (VS_ENET_SETTINGS | MMTYPE_CNF)))
  343. {
  344. continue;
  345. }
  346. if ((confirm-&gt;MSTATUS == 1) || (confirm-&gt;MSTATUS == 3))
  347. {
  348. error (0, 0, &quot;%s: %s (0x%0X): &quot;, PLC_WONTDOIT, MMECode (confirm-&gt;qualcomm.MMTYPE, confirm-&gt;MSTATUS), confirm-&gt;MSTATUS);
  349. continue;
  350. }
  351. if (_anyset (flags, PLC_ANALYSE))
  352. {
  353. printf (&quot;Bits Mode Link Flow\n&quot;);
  354. printf (&quot;%4d &quot;, confirm-&gt;ESPEED);
  355. printf (&quot;%4d &quot;, confirm-&gt;EDUPLEX);
  356. printf (&quot;%4d &quot;, confirm-&gt;ELINKSTATUS);
  357. printf (&quot;%4d\n&quot;, confirm-&gt;EFLOWCONTROL);
  358. }
  359. else
  360. {
  361. printf (&quot;%s %s &quot;, channel-&gt;ifname, hexstring (address, sizeof (address), channel-&gt;host, sizeof (channel-&gt;host)));
  362. printf (&quot;Speed=%s &quot;, rates [confirm-&gt;ESPEED]);
  363. printf (&quot;Duplex=%s &quot;, modes [confirm-&gt;EDUPLEX]);
  364. printf (&quot;LinkStatus=%s &quot;, links [confirm-&gt;ELINKSTATUS]);
  365. printf (&quot;FlowControl=%s\n&quot;, flows [confirm-&gt;EFLOWCONTROL]);
  366. }
  367. }
  368. if (packetsize &lt; 0)
  369. {
  370. error (1, errno, CHANNEL_CANTREAD);
  371. }
  372. return (0);
  373. }
  374. /*====================================================================*
  375. *
  376. * int main (int argc, char const * argv[]);
  377. *
  378. * parse command line, populate plc structure and perform selected
  379. * operations; show help summary if asked; see getoptv and putoptv
  380. * to understand command line parsing and help summary display; see
  381. * plc.h for the definition of struct plc;
  382. *
  383. * the command line accepts multiple MAC addresses and the program
  384. * performs the specified operations on each address, in turn; the
  385. * address order is significant but the option order is not; the
  386. * default address is a local broadcast that causes all devices on
  387. * the local H1 interface to respond but not those at the remote
  388. * end of the powerline;
  389. *
  390. * the default address is 00:B0:52:00:00:01; omitting the address
  391. * will automatically address the local device; some options will
  392. * cancel themselves if this makes no sense;
  393. *
  394. * the default interface is eth1 because most people use eth0 as
  395. * their principle network connection; you can specify another
  396. * interface with -i or define environment string PLC to make
  397. * that the default interface and save typing;
  398. *
  399. *
  400. *--------------------------------------------------------------------*/
  401. int main (int argc, char const * argv [])
  402. {
  403. extern struct channel channel;
  404. static char const * optv [] =
  405. {
  406. &quot;a:d:ef:i:n:p:qrs:tvw&quot;,
  407. &quot;device [device] [...] [&gt; stdout]&quot;,
  408. &quot;Qualcomm Atheros Ethernet PHY Settings&quot;,
  409. &quot;a s\tadvertise capabilities as (s) ['1000Full'|'100Full'|'100Half'|10Full'|'10Half']&quot;,
  410. &quot;d s\tduplex setting is (s) ['half'|'full']&quot;,
  411. &quot;e\tredirect stderr to stdout&quot;,
  412. &quot;f s\tflow control is (s) ['on'|'tx'|'rx'|'off']&quot;,
  413. #if defined (WINPCAP) || defined (LIBPCAP)
  414. &quot;i n\thost interface is (n) [&quot; LITERAL (CHANNEL_ETHNUMBER) &quot;]&quot;,
  415. #else
  416. &quot;i s\thost interface is (s) [&quot; LITERAL (CHANNEL_ETHDEVICE) &quot;]&quot;,
  417. #endif
  418. &quot;n s\tauto-negotiate mode is (s) ['on'|'off']&quot;,
  419. &quot;p n\tport number is (n) [&quot; LITERAL (ETH_PORT) &quot;]&quot;,
  420. &quot;q\tquiet mode&quot;,
  421. &quot;r\tread settings instead of write settings&quot;,
  422. &quot;s s\ttransmission speed in mbps is (s) ['10'|'100'|'1000']&quot;,
  423. &quot;v\tverbose mode&quot;,
  424. &quot;w\twrite settings instead of read settings&quot;,
  425. (char const *) (0)
  426. };
  427. struct phy_settings settings =
  428. {
  429. 0,
  430. 1,
  431. 0,
  432. 0,
  433. 0,
  434. 0
  435. };
  436. flag_t flags = (flag_t)(0);
  437. signed c;
  438. if (getenv (PLCDEVICE))
  439. {
  440. #if defined (WINPCAP) || defined (LIBPCAP)
  441. channel.ifindex = atoi (getenv (PLCDEVICE));
  442. #else
  443. channel.ifname = strdup (getenv (PLCDEVICE));
  444. #endif
  445. }
  446. optind = 1;
  447. while ((c = getoptv (argc, argv, optv)) != -1)
  448. {
  449. switch (c)
  450. {
  451. case 'a':
  452. if ((c = lookup (optarg, advcap, ADVCAP)) == -1)
  453. {
  454. assist (optarg, &quot;capability&quot;, advcap, ADVCAP);
  455. }
  456. settings.ADVCAPS |= (uint8_t)(c);
  457. break;
  458. case 'd':
  459. if ((c = lookup (optarg, duplex, DUPLEX)) == -1)
  460. {
  461. assist (optarg, &quot;duplex&quot;, duplex, DUPLEX);
  462. }
  463. settings.EDUPLEX = (uint8_t)(c);
  464. break;
  465. case 'e':
  466. dup2 (STDOUT_FILENO, STDERR_FILENO);
  467. break;
  468. case 'f':
  469. if ((c = lookup (optarg, control, CONTROL)) == -1)
  470. {
  471. assist (optarg, &quot;control&quot;, control, CONTROL);
  472. }
  473. settings.EFLOWCONTROL = (uint8_t)(c);
  474. break;
  475. case 'n':
  476. if ((c = lookup (optarg, negotiate, NEGOTIATE)) == -1)
  477. {
  478. assist (optarg, &quot;auto-negotiate&quot;, negotiate, NEGOTIATE);
  479. }
  480. settings.AUTONEGOTIATE = (uint8_t)(c);
  481. break;
  482. case 's':
  483. if ((c = lookup (optarg, speeds, SPEEDS)) == -1)
  484. {
  485. assist (optarg, &quot;speed&quot;, speeds, SPEEDS);
  486. }
  487. settings.ESPEED = (uint8_t)(c);
  488. break;
  489. case 't':
  490. _setbits (flags, PLC_ANALYSE);
  491. break;
  492. case 'i':
  493. #if defined (WINPCAP) || defined (LIBPCAP)
  494. channel.ifindex = atoi (optarg);
  495. #else
  496. channel.ifname = optarg;
  497. #endif
  498. break;
  499. case 'p':
  500. settings.MCONTROL &amp;= 0x0F;
  501. settings.MCONTROL |= (unsigned)(uintspec (optarg, 0, 7)) &lt;&lt; 4;
  502. break;
  503. case 'q':
  504. _setbits (channel.flags, CHANNEL_SILENCE);
  505. break;
  506. case 'r':
  507. settings.MCONTROL &amp;= 0xF0;
  508. settings.MCONTROL |= 0x00;
  509. break;
  510. case 'v':
  511. _setbits (channel.flags, CHANNEL_VERBOSE);
  512. break;
  513. case 'w':
  514. settings.MCONTROL &amp;= 0xF0;
  515. settings.MCONTROL |= 0x01;
  516. break;
  517. default:
  518. break;
  519. }
  520. }
  521. argc -= optind;
  522. argv += optind;
  523. openchannel (&amp;channel);
  524. if (!argc)
  525. {
  526. PHYSettings (&amp;channel, &amp;settings, flags);
  527. }
  528. while ((argc) &amp;&amp; (* argv))
  529. {
  530. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  531. {
  532. error (1, errno, PLC_BAD_MAC, * argv);
  533. }
  534. PHYSettings (&amp;channel, &amp;settings, flags);
  535. argc--;
  536. argv++;
  537. }
  538. closechannel (&amp;channel);
  539. exit (0);
  540. }
  541. </pre>
  542. <div class='footerlink'>
  543. [<a href='int6kdetect.c.html' title=' int6kdetect.c '>PREV</a>]
  544. [<a href='toolkit.html' title=' Index '>HOME</a>]
  545. [<a href='int6kf.c.html' title=' int6kf.c '>NEXT</a>]
  546. </div>
  547. </body>
  548. </html>