int6keth.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. * int6keth.c - Atheros Ethernet PHY Settings;
  44. *
  45. *
  46. * Contributor(s):
  47. * Charles Maier
  48. * Matthieu Poullet
  49. *
  50. *--------------------------------------------------------------------*/
  51. /*====================================================================*"
  52. * system header files;
  53. *--------------------------------------------------------------------*/
  54. #include <unistd.h>
  55. #include <stdlib.h>
  56. #include <stdint.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. #include "../ether/channel.h"
  71. #include "../mme/mme.h"
  72. /*====================================================================*
  73. * custom source files;
  74. *--------------------------------------------------------------------*/
  75. #ifndef MAKEFILE
  76. #include "../mme/UnwantedMessage.c"
  77. #include "../plc/Devices.c"
  78. #endif
  79. #ifndef MAKEFILE
  80. #include "../tools/assist.c"
  81. #include "../tools/error.c"
  82. #include "../tools/codelist.c"
  83. #include "../tools/getoptv.c"
  84. #include "../tools/putoptv.c"
  85. #include "../tools/version.c"
  86. #include "../tools/uintspec.c"
  87. #include "../tools/hexdump.c"
  88. #include "../tools/hexencode.c"
  89. #include "../tools/hexdecode.c"
  90. #include "../tools/hexstring.c"
  91. #include "../tools/todigit.c"
  92. #include "../tools/checkfilename.c"
  93. #include "../tools/checksum32.c"
  94. #include "../tools/fdchecksum32.c"
  95. #include "../tools/strfbits.c"
  96. #include "../tools/typename.c"
  97. #include "../tools/lookup.c"
  98. #include "../tools/synonym.c"
  99. #include "../tools/uintspec.c"
  100. #endif
  101. #ifndef MAKEFILE
  102. #include "../ether/openchannel.c"
  103. #include "../ether/closechannel.c"
  104. #include "../ether/readpacket.c"
  105. #include "../ether/sendpacket.c"
  106. #include "../ether/channel.c"
  107. #endif
  108. #ifndef MAKEFILE
  109. #include "../mme/MMECode.c"
  110. #include "../mme/EthernetHeader.c"
  111. #include "../mme/QualcommHeader.c"
  112. #endif
  113. /*====================================================================*
  114. * program constants;
  115. *--------------------------------------------------------------------*/
  116. #define MCONTROL_READ 0x00
  117. #define MCONTROL_WRITE 0x01
  118. #define ETH_PORT 0
  119. #define NEGOTIATE SIZEOF (negotiate)
  120. #define SPEEDS SIZEOF (speeds)
  121. #define DUPLEX SIZEOF (duplex)
  122. #define CONTROL SIZEOF (control)
  123. #define ADVCAP SIZEOF (advcap)
  124. static struct _code_ const negotiate [] =
  125. {
  126. {
  127. 0,
  128. "Off"
  129. },
  130. {
  131. 1,
  132. "On"
  133. }
  134. };
  135. static struct _code_ const speeds [] =
  136. {
  137. {
  138. 0,
  139. "10"
  140. },
  141. {
  142. 1,
  143. "100"
  144. },
  145. {
  146. 2,
  147. "1000"
  148. }
  149. };
  150. static struct _code_ const duplex [] =
  151. {
  152. {
  153. 0,
  154. "Half"
  155. },
  156. {
  157. 1,
  158. "Full"
  159. }
  160. };
  161. static struct _code_ const control [] =
  162. {
  163. {
  164. 0,
  165. "Off"
  166. },
  167. {
  168. 1,
  169. "Tx"
  170. },
  171. {
  172. 2,
  173. "Rx"
  174. },
  175. {
  176. 3,
  177. "On"
  178. }
  179. };
  180. static struct _code_ const advcap [] =
  181. {
  182. {
  183. 1,
  184. "100Full"
  185. },
  186. {
  187. 2,
  188. "100Half"
  189. },
  190. {
  191. 4,
  192. "10Full"
  193. },
  194. {
  195. 8,
  196. "10Half"
  197. },
  198. {
  199. 16,
  200. "1000Full"
  201. }
  202. };
  203. #define RATES SIZEOF (rates)
  204. #define MODES SIZEOF (modes)
  205. #define LINKS SIZEOF (links)
  206. #define FLOWS SIZEOF (flows)
  207. static char const * rates [] =
  208. {
  209. "10",
  210. "100",
  211. "1000"
  212. };
  213. static char const * modes [] =
  214. {
  215. "Half",
  216. "Full"
  217. };
  218. static char const * links [] =
  219. {
  220. "Unknown",
  221. "Off",
  222. "On"
  223. };
  224. static char const * flows [] =
  225. {
  226. "Off",
  227. "Tx",
  228. "Rx",
  229. "On"
  230. };
  231. /*====================================================================*
  232. * program variables;
  233. *--------------------------------------------------------------------*/
  234. #ifndef __GNUC__
  235. #pragma pack (push,1)
  236. #endif
  237. typedef struct __packed phy_settings
  238. {
  239. uint8_t MCONTROL;
  240. uint8_t AUTONEGOTIATE;
  241. uint8_t ADVCAPS;
  242. uint8_t ESPEED;
  243. uint8_t EDUPLEX;
  244. uint8_t EFLOWCONTROL;
  245. }
  246. phy_settings;
  247. typedef struct __packed phy_readings
  248. {
  249. uint8_t MSTATUS;
  250. uint8_t ESPEED;
  251. uint8_t EDUPLEX;
  252. uint8_t ELINKSTATUS;
  253. uint8_t EFLOWCONTROL;
  254. }
  255. phy_readings;
  256. #ifndef __GNUC__
  257. #pragma pack (pop)
  258. #endif
  259. /*====================================================================*
  260. *
  261. * signed PHYSettings (struct channel * channel, struct phy_settings * settings, flag_t flags);
  262. *
  263. * plc.h
  264. *
  265. * read and display Ethernet PHY settings or write then read and
  266. * display settings;
  267. *
  268. *
  269. * Contributor(s):
  270. * Charles Maier
  271. *
  272. *--------------------------------------------------------------------*/
  273. signed PHYSettings (struct channel * channel, struct phy_settings * settings, flag_t flags)
  274. {
  275. struct message message;
  276. signed packetsize;
  277. #ifndef __GNUC__
  278. #pragma pack (push,1)
  279. #endif
  280. struct __packed vs_enet_settings_request
  281. {
  282. struct ethernet_hdr ethernet;
  283. struct qualcomm_hdr qualcomm;
  284. uint8_t MCONTROL;
  285. uint8_t AUTONEGOTIATE;
  286. uint8_t ADVCAPS;
  287. uint8_t ESPEED;
  288. uint8_t EDUPLEX;
  289. uint8_t EFLOWCONTROL;
  290. }
  291. * request = (struct vs_enet_settings_request *) (&message);
  292. struct __packed vs_enet_settings_confirm
  293. {
  294. struct ethernet_hdr ethernet;
  295. struct qualcomm_hdr qualcomm;
  296. uint8_t MSTATUS;
  297. uint8_t ESPEED;
  298. uint8_t EDUPLEX;
  299. uint8_t ELINKSTATUS;
  300. uint8_t EFLOWCONTROL;
  301. }
  302. * confirm = (struct vs_enet_settings_confirm *) (&message);
  303. #ifndef __GNUC__
  304. #pragma pack (pop)
  305. #endif
  306. char address [ETHER_ADDR_LEN * 3];
  307. memset (&message, 0, sizeof (message));
  308. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  309. QualcommHeader (&request->qualcomm, 0, (VS_ENET_SETTINGS | MMTYPE_REQ));
  310. request->MCONTROL = settings->MCONTROL;
  311. request->AUTONEGOTIATE = settings->AUTONEGOTIATE;
  312. request->ADVCAPS = settings->ADVCAPS;
  313. request->ESPEED = settings->ESPEED;
  314. request->EDUPLEX = settings->EDUPLEX;
  315. request->EFLOWCONTROL = settings->EFLOWCONTROL;
  316. if (sendpacket (channel, &message, (ETHER_MIN_LEN - ETHER_CRC_LEN)) < 0)
  317. {
  318. error (1, errno, CHANNEL_CANTSEND);
  319. }
  320. while ((packetsize = readpacket (channel, &message, sizeof (message))) > 0)
  321. {
  322. if (UnwantedMessage (&message, packetsize, 0, (VS_ENET_SETTINGS | MMTYPE_CNF)))
  323. {
  324. continue;
  325. }
  326. if ((confirm->MSTATUS == 1) || (confirm->MSTATUS == 3))
  327. {
  328. error (0, 0, "%s: %s (0x%0X): ", PLC_WONTDOIT, MMECode (confirm->qualcomm.MMTYPE, confirm->MSTATUS), confirm->MSTATUS);
  329. continue;
  330. }
  331. if (_anyset (flags, PLC_ANALYSE))
  332. {
  333. printf ("Bits Mode Link Flow\n");
  334. printf ("%4d ", confirm->ESPEED);
  335. printf ("%4d ", confirm->EDUPLEX);
  336. printf ("%4d ", confirm->ELINKSTATUS);
  337. printf ("%4d\n", confirm->EFLOWCONTROL);
  338. }
  339. else
  340. {
  341. printf ("%s %s ", channel->ifname, hexstring (address, sizeof (address), channel->host, sizeof (channel->host)));
  342. printf ("Speed=%s ", rates [confirm->ESPEED]);
  343. printf ("Duplex=%s ", modes [confirm->EDUPLEX]);
  344. printf ("LinkStatus=%s ", links [confirm->ELINKSTATUS]);
  345. printf ("FlowControl=%s\n", flows [confirm->EFLOWCONTROL]);
  346. }
  347. }
  348. if (packetsize < 0)
  349. {
  350. error (1, errno, CHANNEL_CANTREAD);
  351. }
  352. return (0);
  353. }
  354. /*====================================================================*
  355. *
  356. * int main (int argc, char const * argv[]);
  357. *
  358. * parse command line, populate plc structure and perform selected
  359. * operations; show help summary if asked; see getoptv and putoptv
  360. * to understand command line parsing and help summary display; see
  361. * plc.h for the definition of struct plc;
  362. *
  363. * the command line accepts multiple MAC addresses and the program
  364. * performs the specified operations on each address, in turn; the
  365. * address order is significant but the option order is not; the
  366. * default address is a local broadcast that causes all devices on
  367. * the local H1 interface to respond but not those at the remote
  368. * end of the powerline;
  369. *
  370. * the default address is 00:B0:52:00:00:01; omitting the address
  371. * will automatically address the local device; some options will
  372. * cancel themselves if this makes no sense;
  373. *
  374. * the default interface is eth1 because most people use eth0 as
  375. * their principle network connection; you can specify another
  376. * interface with -i or define environment string PLC to make
  377. * that the default interface and save typing;
  378. *
  379. *
  380. *--------------------------------------------------------------------*/
  381. int main (int argc, char const * argv [])
  382. {
  383. extern struct channel channel;
  384. static char const * optv [] =
  385. {
  386. "a:d:ef:i:n:p:qrs:tvw",
  387. "device [device] [...] [> stdout]",
  388. "Qualcomm Atheros Ethernet PHY Settings",
  389. "a s\tadvertise capabilities as (s) ['1000Full'|'100Full'|'100Half'|10Full'|'10Half']",
  390. "d s\tduplex setting is (s) ['half'|'full']",
  391. "e\tredirect stderr to stdout",
  392. "f s\tflow control is (s) ['on'|'tx'|'rx'|'off']",
  393. #if defined (WINPCAP) || defined (LIBPCAP)
  394. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  395. #else
  396. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  397. #endif
  398. "n s\tauto-negotiate mode is (s) ['on'|'off']",
  399. "p n\tport number is (n) [" LITERAL (ETH_PORT) "]",
  400. "q\tquiet mode",
  401. "r\tread settings instead of write settings",
  402. "s s\ttransmission speed in mbps is (s) ['10'|'100'|'1000']",
  403. "v\tverbose mode",
  404. "w\twrite settings instead of read settings",
  405. (char const *) (0)
  406. };
  407. struct phy_settings settings =
  408. {
  409. 0,
  410. 1,
  411. 0,
  412. 0,
  413. 0,
  414. 0
  415. };
  416. flag_t flags = (flag_t)(0);
  417. signed c;
  418. if (getenv (PLCDEVICE))
  419. {
  420. #if defined (WINPCAP) || defined (LIBPCAP)
  421. channel.ifindex = atoi (getenv (PLCDEVICE));
  422. #else
  423. channel.ifname = strdup (getenv (PLCDEVICE));
  424. #endif
  425. }
  426. optind = 1;
  427. while ((c = getoptv (argc, argv, optv)) != -1)
  428. {
  429. switch (c)
  430. {
  431. case 'a':
  432. if ((c = lookup (optarg, advcap, ADVCAP)) == -1)
  433. {
  434. assist (optarg, "capability", advcap, ADVCAP);
  435. }
  436. settings.ADVCAPS |= (uint8_t)(c);
  437. break;
  438. case 'd':
  439. if ((c = lookup (optarg, duplex, DUPLEX)) == -1)
  440. {
  441. assist (optarg, "duplex", duplex, DUPLEX);
  442. }
  443. settings.EDUPLEX = (uint8_t)(c);
  444. break;
  445. case 'e':
  446. dup2 (STDOUT_FILENO, STDERR_FILENO);
  447. break;
  448. case 'f':
  449. if ((c = lookup (optarg, control, CONTROL)) == -1)
  450. {
  451. assist (optarg, "control", control, CONTROL);
  452. }
  453. settings.EFLOWCONTROL = (uint8_t)(c);
  454. break;
  455. case 'n':
  456. if ((c = lookup (optarg, negotiate, NEGOTIATE)) == -1)
  457. {
  458. assist (optarg, "auto-negotiate", negotiate, NEGOTIATE);
  459. }
  460. settings.AUTONEGOTIATE = (uint8_t)(c);
  461. break;
  462. case 's':
  463. if ((c = lookup (optarg, speeds, SPEEDS)) == -1)
  464. {
  465. assist (optarg, "speed", speeds, SPEEDS);
  466. }
  467. settings.ESPEED = (uint8_t)(c);
  468. break;
  469. case 't':
  470. _setbits (flags, PLC_ANALYSE);
  471. break;
  472. case 'i':
  473. #if defined (WINPCAP) || defined (LIBPCAP)
  474. channel.ifindex = atoi (optarg);
  475. #else
  476. channel.ifname = optarg;
  477. #endif
  478. break;
  479. case 'p':
  480. settings.MCONTROL &= 0x0F;
  481. settings.MCONTROL |= (unsigned)(uintspec (optarg, 0, 7)) << 4;
  482. break;
  483. case 'q':
  484. _setbits (channel.flags, CHANNEL_SILENCE);
  485. break;
  486. case 'r':
  487. settings.MCONTROL &= 0xF0;
  488. settings.MCONTROL |= 0x00;
  489. break;
  490. case 'v':
  491. _setbits (channel.flags, CHANNEL_VERBOSE);
  492. break;
  493. case 'w':
  494. settings.MCONTROL &= 0xF0;
  495. settings.MCONTROL |= 0x01;
  496. break;
  497. default:
  498. break;
  499. }
  500. }
  501. argc -= optind;
  502. argv += optind;
  503. openchannel (&channel);
  504. if (!argc)
  505. {
  506. PHYSettings (&channel, &settings, flags);
  507. }
  508. while ((argc) && (* argv))
  509. {
  510. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  511. {
  512. error (1, errno, PLC_BAD_MAC, * argv);
  513. }
  514. PHYSettings (&channel, &settings, flags);
  515. argc--;
  516. argv++;
  517. }
  518. closechannel (&channel);
  519. exit (0);
  520. }