int6k.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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. * int6k.c -
  44. *
  45. *
  46. * Contributor(s):
  47. * Charles Maier
  48. * Nathaniel Houghton
  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. #include "../ether/channel.h"
  72. #include "../key/HPAVKey.h"
  73. #include "../key/keys.h"
  74. #include "../ram/sdram.h"
  75. #include "../pib/pib.h"
  76. #include "../nvm/nvm.h"
  77. /*====================================================================*
  78. * custom source files;
  79. *--------------------------------------------------------------------*/
  80. #ifndef MAKEFILE
  81. #include "../plc/Attributes1.c"
  82. #include "../plc/chipset.c"
  83. #include "../plc/Devices.c"
  84. #include "../plc/Confirm.c"
  85. #include "../plc/Display.c"
  86. #include "../plc/Failure.c"
  87. #include "../plc/FlashNVM.c"
  88. #include "../plc/FactoryDefaults.c"
  89. #include "../plc/Identity1.c"
  90. #include "../plc/HostActionResponse.c"
  91. #include "../plc/NetInfo1.c"
  92. #include "../plc/NVRAMInfo.c"
  93. #include "../plc/PushButton.c"
  94. #include "../plc/SDRAMInfo.c"
  95. #include "../plc/SetNMK.c"
  96. #include "../plc/Request.c"
  97. #include "../plc/ReadFirmware1.c"
  98. #include "../plc/ReadMME.c"
  99. #include "../plc/ReadMFG.c"
  100. #include "../plc/ReadParameters1.c"
  101. #include "../plc/ResetDevice.c"
  102. #include "../plc/SendMME.c"
  103. #include "../plc/VersionInfo1.c"
  104. #include "../plc/WriteCFG.c"
  105. #include "../plc/WriteNVM.c"
  106. #include "../plc/WritePIB.c"
  107. #include "../plc/WaitForReset.c"
  108. #include "../plc/WaitForStart.c"
  109. #include "../plc/WaitForRestart.c"
  110. #include "../plc/WatchdogReport.c"
  111. #include "../plc/StationRole.c"
  112. #include "../plc/ModuleRead.c"
  113. #endif
  114. #ifndef MAKEFILE
  115. #include "../tools/synonym.c"
  116. #include "../tools/getoptv.c"
  117. #include "../tools/putoptv.c"
  118. #include "../tools/version.c"
  119. #include "../tools/uintspec.c"
  120. #include "../tools/hexdump.c"
  121. #include "../tools/hexencode.c"
  122. #include "../tools/hexdecode.c"
  123. #include "../tools/hexstring.c"
  124. #include "../tools/todigit.c"
  125. #include "../tools/checkfilename.c"
  126. #include "../tools/checksum32.c"
  127. #include "../tools/fdchecksum32.c"
  128. #include "../tools/strfbits.c"
  129. #include "../tools/typename.c"
  130. #include "../tools/error.c"
  131. #endif
  132. #ifndef MAKEFILE
  133. #include "../ether/openchannel.c"
  134. #include "../ether/closechannel.c"
  135. #include "../ether/readpacket.c"
  136. #include "../ether/sendpacket.c"
  137. #include "../ether/channel.c"
  138. #endif
  139. #ifndef MAKEFILE
  140. #include "../ram/nvram.c"
  141. #include "../ram/sdramfile.c"
  142. #include "../ram/sdrampeek.c"
  143. #endif
  144. #ifndef MAKEFILE
  145. #include "../nvm/nvmfile1.c"
  146. #endif
  147. #ifndef MAKEFILE
  148. #include "../pib/pibfile1.c"
  149. #include "../pib/pibpeek1.c"
  150. #endif
  151. #ifndef MAKEFILE
  152. #include "../mme/MMECode.c"
  153. #include "../mme/EthernetHeader.c"
  154. #include "../mme/QualcommHeader.c"
  155. #include "../mme/QualcommHeader1.c"
  156. #include "../mme/UnwantedMessage.c"
  157. #endif
  158. #ifndef MAKEFILE
  159. #include "../key/keys.c"
  160. #endif
  161. /*====================================================================*
  162. * program constants;
  163. *--------------------------------------------------------------------*/
  164. #define INT6K_WAIT 0
  165. #define INT6K_LOOP 1
  166. /*====================================================================*
  167. * program variables;
  168. *--------------------------------------------------------------------*/
  169. static struct _term_ const modules [] =
  170. {
  171. {
  172. "nvm",
  173. "1"
  174. },
  175. {
  176. "pib",
  177. "2"
  178. },
  179. {
  180. "both",
  181. "3"
  182. }
  183. };
  184. static struct _term_ const buttons [] =
  185. {
  186. {
  187. "join",
  188. "1"
  189. },
  190. {
  191. "leave",
  192. "2"
  193. },
  194. {
  195. "status",
  196. "3"
  197. }
  198. };
  199. /*====================================================================*
  200. *
  201. * void manager (struct plc * plc, signed count, signed pause);
  202. *
  203. * perform operations in logical order despite any order specfied
  204. * on the command line; for example read PIB before writing PIB;
  205. *
  206. * operation order is controlled by the order of "if" statements
  207. * shown here; the entire operation sequence can be repeated with
  208. * an optional pause between each iteration;
  209. *
  210. *
  211. *--------------------------------------------------------------------*/
  212. static void manager (struct plc * plc, signed count, signed pause)
  213. {
  214. while (count--)
  215. {
  216. if (_anyset (plc->flags, PLC_VERSION))
  217. {
  218. VersionInfo1 (plc);
  219. }
  220. if (_anyset (plc->flags, PLC_ATTRIBUTES))
  221. {
  222. Attributes1 (plc);
  223. }
  224. if (_anyset (plc->flags, PLC_WATCHDOG_REPORT))
  225. {
  226. WatchdogReport (plc);
  227. }
  228. if (_anyset (plc->flags, PLC_NVRAM_INFO))
  229. {
  230. NVRAMInfo (plc);
  231. }
  232. if (_anyset (plc->flags, PLC_SDRAM_INFO))
  233. {
  234. SDRAMInfo (plc);
  235. }
  236. if (_anyset (plc->flags, PLC_READ_IDENTITY))
  237. {
  238. Identity1 (plc);
  239. }
  240. if (_anyset (plc->flags, PLC_NETWORK))
  241. {
  242. NetInfo1 (plc);
  243. }
  244. if (_anyset (plc->flags, PLC_SDRAM_CONFIG))
  245. {
  246. WriteCFG (plc);
  247. }
  248. if (_anyset (plc->flags, PLC_WRITE_MAC))
  249. {
  250. WriteNVM (plc);
  251. }
  252. if (_anyset (plc->flags, PLC_READ_MAC))
  253. {
  254. ReadFirmware1 (plc);
  255. }
  256. if (_anyset (plc->flags, PLC_WRITE_PIB))
  257. {
  258. WritePIB (plc);
  259. }
  260. if (_anyset (plc->flags, PLC_READ_PIB))
  261. {
  262. ReadParameters1 (plc);
  263. }
  264. if (_anyset (plc->flags, PLC_HOST_ACTION))
  265. {
  266. HostActionResponse (plc);
  267. }
  268. if (_anyset (plc->flags, PLC_PUSH_BUTTON))
  269. {
  270. PushButton (plc);
  271. }
  272. if (_anyset (plc->flags, (PLC_SETLOCALKEY | PLC_SETREMOTEKEY)))
  273. {
  274. SetNMK (plc);
  275. }
  276. if (_anyset (plc->flags, PLC_FACTORY_DEFAULTS))
  277. {
  278. FactoryDefaults (plc);
  279. }
  280. if (_anyset (plc->flags, PLC_FLASH_DEVICE))
  281. {
  282. FlashNVM (plc);
  283. WaitForRestart (plc);
  284. }
  285. if (_anyset (plc->flags, PLC_RESET_DEVICE))
  286. {
  287. ResetDevice (plc);
  288. }
  289. sleep (pause);
  290. }
  291. return;
  292. }
  293. /*====================================================================*
  294. *
  295. * int main (int argc, char const * argv[]);
  296. *
  297. * parse command line, populate plc structure and perform selected
  298. * operations; show help summary if asked; see getoptv and putoptv
  299. * to understand command line parsing and help summary display; see
  300. * plc.h for the definition of struct plc;
  301. *
  302. * the command line accepts multiple MAC addresses and the program
  303. * performs the specified operations on each address, in turn; the
  304. * address order is significant but the option order is not; the
  305. * default address is a local broadcast that causes all devices on
  306. * the local H1 interface to respond but not those at the remote
  307. * end of the powerline;
  308. *
  309. * the default address is 00:B0:52:00:00:01; omitting the address
  310. * will automatically address the local device; some options will
  311. * cancel themselves if this makes no sense;
  312. *
  313. * the default interface is eth1 because most people use eth0 as
  314. * their principle network connection; you can specify another
  315. * interface with -i or define environment string PLC to make
  316. * that the default interface and save typing;
  317. *
  318. *
  319. *--------------------------------------------------------------------*/
  320. int main (int argc, char const * argv [])
  321. {
  322. extern struct channel channel;
  323. extern struct _term_ const daks [];
  324. extern struct _term_ const nmks [];
  325. static char const * optv [] =
  326. {
  327. "aB:C:d:D:efFHi:IJ:K:l:mMn:N:p:P:QqrRS:st:Tvw:x",
  328. "device [device] [...]",
  329. "Qualcomm Atheros INT6x00 Powerline Device Manager",
  330. "a\tread device attributes using VS_OP_ATTRIBUTES",
  331. "B n\tperform pushbutton action (n) using MS_PB_ENC [1|2|3|'join'|'leave'|'status']",
  332. "C n\tflash NVRAM with module (n) using VS_MOD_NVM [1|2|3|'nvm'|'pib'|'both']",
  333. "d f\tdump and clear watchdog report to file (f) using VS_WD_RPT",
  334. "D x\tset DAK to (x) for option -J [" DAK1 "]",
  335. "e\tredirect stderr to stdout",
  336. "f\tread NVRAM Configuration using VS_GET_NVM",
  337. "F[F]\tflash [force] NVRAM with PIB and firmware using VS_MOD_NVM",
  338. "H\tstop host action requests using VS_HOST_ACTION.IND",
  339. #if defined (WINPCAP) || defined (LIBPCAP)
  340. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  341. #else
  342. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  343. #endif
  344. "I\tread device identity using VS_RD_MOD",
  345. "J x\tset NMK on remote device (x) via local device using VS_SET_KEY (see -K)",
  346. "K x\tset NMK to (x) for options -J and -M [" NMK1 "]",
  347. "l n\tloop (n) times [" LITERAL (INT6K_LOOP) "]",
  348. "m\tread network membership information using VS_NW_INFO",
  349. "M\tset NMK on local device using VS_SET_KEY (see -K)",
  350. "n f\tread NVM from SDRAM to file (f) using VS_RD_MOD",
  351. "N f\twrite NVM file (f) to SDRAM using VS_WR_MOD",
  352. "p f\tread PIB from SDRAM to file (f) using VS_RD_MOD",
  353. "P f\twrite PIB file (f) to SDRAM using VS_WR_MOD",
  354. "q\tquiet mode",
  355. "Q\tquick flash (return immediately)",
  356. "r\tread hardware and firmware revision using VS_SW_VER",
  357. "R\treset device using VS_RS_DEV",
  358. "s\tread SDRAM Configuration using VS_RD_CBLOCK",
  359. "S f\twrite an SDRAM Configuration file (f) using VS_SET_SDRAM",
  360. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  361. "T\trestore factory defaults using VS_FAC_DEFAULTS",
  362. "v\tverbose mode",
  363. "w n\tpause (n) seconds [" LITERAL (INT6K_WAIT) "]",
  364. "x\texit on error",
  365. (char const *) (0)
  366. };
  367. #include "../plc/plc.c"
  368. signed loop = INT6K_LOOP;
  369. signed wait = INT6K_WAIT;
  370. signed c;
  371. if (getenv (PLCDEVICE))
  372. {
  373. #if defined (WINPCAP) || defined (LIBPCAP)
  374. channel.ifindex = atoi (getenv (PLCDEVICE));
  375. #else
  376. channel.ifname = strdup (getenv (PLCDEVICE));
  377. #endif
  378. }
  379. optind = 1;
  380. while ((c = getoptv (argc, argv, optv)) != -1)
  381. {
  382. switch (c)
  383. {
  384. case 'a':
  385. _setbits (plc.flags, PLC_ATTRIBUTES);
  386. break;
  387. case 'B':
  388. _setbits (plc.flags, PLC_PUSH_BUTTON);
  389. plc.pushbutton = (unsigned)(uintspec (synonym (optarg, buttons, SIZEOF (buttons)), 0, UCHAR_MAX));
  390. break;
  391. case 'C':
  392. _setbits (plc.flags, PLC_FLASH_DEVICE);
  393. plc.module = (unsigned)(uintspec (synonym (optarg, modules, SIZEOF (modules)), 0, UCHAR_MAX));
  394. break;
  395. case 'd':
  396. _setbits (plc.flags, PLC_WATCHDOG_REPORT);
  397. if (!checkfilename (optarg))
  398. {
  399. error (1, EINVAL, "%s", optarg);
  400. }
  401. if ((plc.rpt.file = open (plc.rpt.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
  402. {
  403. error (1, errno, "%s", plc.rpt.name);
  404. }
  405. #ifndef WIN32
  406. chown (optarg, getuid (), getgid ());
  407. #endif
  408. plc.readaction = 3;
  409. break;
  410. case 'D':
  411. if (!hexencode (plc.DAK, sizeof (plc.DAK), synonym (optarg, daks, SIZEOF (daks))))
  412. {
  413. error (1, errno, PLC_BAD_DAK, optarg);
  414. }
  415. break;
  416. case 'e':
  417. dup2 (STDOUT_FILENO, STDERR_FILENO);
  418. break;
  419. case 'f':
  420. _setbits (plc.flags, PLC_NVRAM_INFO);
  421. break;
  422. case 'F':
  423. _setbits (plc.module, (VS_MODULE_MAC | VS_MODULE_PIB));
  424. if (_anyset (plc.flags, PLC_FLASH_DEVICE))
  425. {
  426. _setbits (plc.module, VS_MODULE_FORCE);
  427. }
  428. _setbits (plc.flags, PLC_FLASH_DEVICE);
  429. break;
  430. case 'H':
  431. _setbits (plc.flags, PLC_HOST_ACTION);
  432. break;
  433. case 'I':
  434. _setbits (plc.flags, PLC_READ_IDENTITY);
  435. break;
  436. case 'i':
  437. #if defined (WINPCAP) || defined (LIBPCAP)
  438. channel.ifindex = atoi (optarg);
  439. #else
  440. channel.ifname = optarg;
  441. #endif
  442. break;
  443. case 'J':
  444. if (!hexencode (plc.RDA, sizeof (plc.RDA), (char const *)(optarg)))
  445. {
  446. error (1, errno, PLC_BAD_MAC, optarg);
  447. }
  448. _setbits (plc.flags, PLC_SETREMOTEKEY);
  449. break;
  450. case 'K':
  451. if (!hexencode (plc.NMK, sizeof (plc.NMK), synonym (optarg, nmks, SIZEOF (nmks))))
  452. {
  453. error (1, errno, PLC_BAD_NMK, optarg);
  454. }
  455. break;
  456. case 'M':
  457. _setbits (plc.flags, PLC_SETLOCALKEY);
  458. break;
  459. case 'l':
  460. loop = (unsigned)(uintspec (optarg, 0, UINT_MAX));
  461. break;
  462. case 'm':
  463. _setbits (plc.flags, PLC_NETWORK);
  464. break;
  465. case 'N':
  466. if (!checkfilename (optarg))
  467. {
  468. error (1, EINVAL, "%s", optarg);
  469. }
  470. if ((plc.NVM.file = open (plc.NVM.name = optarg, O_BINARY|O_RDONLY)) == -1)
  471. {
  472. error (1, errno, "%s", plc.NVM.name);
  473. }
  474. if (nvmfile1 (&plc.NVM))
  475. {
  476. error (1, errno, "Bad firmware file: %s", plc.NVM.name);
  477. }
  478. _setbits (plc.flags, PLC_WRITE_MAC);
  479. break;
  480. case 'n':
  481. if (!checkfilename (optarg))
  482. {
  483. error (1, EINVAL, "%s", optarg);
  484. }
  485. if ((plc.nvm.file = open (plc.nvm.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
  486. {
  487. error (1, errno, "%s", plc.nvm.name);
  488. }
  489. #ifndef WIN32
  490. chown (optarg, getuid (), getgid ());
  491. #endif
  492. _setbits (plc.flags, PLC_READ_MAC);
  493. break;
  494. case 'P':
  495. if (!checkfilename (optarg))
  496. {
  497. error (1, EINVAL, "%s", optarg);
  498. }
  499. if ((plc.PIB.file = open (plc.PIB.name = optarg, O_BINARY|O_RDONLY)) == -1)
  500. {
  501. error (1, errno, "%s", plc.PIB.name);
  502. }
  503. if (pibfile1 (&plc.PIB))
  504. {
  505. error (1, errno, "Bad parameter file: %s", plc.PIB.name);
  506. }
  507. _setbits (plc.flags, PLC_WRITE_PIB);
  508. break;
  509. case 'p':
  510. if (!checkfilename (optarg))
  511. {
  512. error (1, EINVAL, "%s", optarg);
  513. }
  514. if ((plc.pib.file = open (plc.pib.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
  515. {
  516. error (1, errno, "%s", plc.pib.name);
  517. }
  518. #ifndef WIN32
  519. chown (optarg, getuid (), getgid ());
  520. #endif
  521. _setbits (plc.flags, PLC_READ_PIB);
  522. break;
  523. case 'Q':
  524. _setbits (plc.flags, PLC_QUICK_FLASH);
  525. break;
  526. case 'q':
  527. _setbits (channel.flags, CHANNEL_SILENCE);
  528. _setbits (plc.flags, PLC_SILENCE);
  529. break;
  530. case 'R':
  531. _setbits (plc.flags, PLC_RESET_DEVICE);
  532. break;
  533. case 'r':
  534. _setbits (plc.flags, PLC_VERSION);
  535. break;
  536. case 'S':
  537. if (!checkfilename (optarg))
  538. {
  539. error (1, EINVAL, "%s", optarg);
  540. }
  541. if ((plc.CFG.file = open (plc.CFG.name = optarg, O_BINARY|O_RDONLY)) == -1)
  542. {
  543. error (1, errno, "%s", plc.CFG.name);
  544. }
  545. if (sdramfile (plc.CFG.file, optarg, plc.flags))
  546. {
  547. error (1, ECANCELED, "SDRAM config file %s is corrupt", optarg);
  548. }
  549. _setbits (plc.flags, PLC_SDRAM_CONFIG);
  550. break;
  551. case 's':
  552. _setbits (plc.flags, PLC_SDRAM_INFO);
  553. break;
  554. case 't':
  555. channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
  556. break;
  557. case 'T':
  558. _setbits (plc.flags, PLC_FACTORY_DEFAULTS);
  559. break;
  560. case 'v':
  561. _setbits (channel.flags, CHANNEL_VERBOSE);
  562. _setbits (plc.flags, PLC_VERBOSE);
  563. break;
  564. case 'V':
  565. _setbits (plc.flags, PLC_SNIFFER);
  566. plc.action = (uint8_t)(uintspec (optarg, 0, UCHAR_MAX));
  567. break;
  568. case 'w':
  569. wait = (unsigned)(uintspec (optarg, 0, 3600));
  570. break;
  571. case 'x':
  572. _setbits (plc.flags, PLC_BAILOUT);
  573. break;
  574. default:
  575. break;
  576. }
  577. }
  578. argc -= optind;
  579. argv += optind;
  580. if (argc != 1)
  581. {
  582. if (plc.nvm.file != -1)
  583. {
  584. error (1, ECANCELED, PLC_NODEVICE);
  585. }
  586. if (plc.pib.file != -1)
  587. {
  588. error (1, ECANCELED, PLC_NODEVICE);
  589. }
  590. if (plc.rpt.file != -1)
  591. {
  592. error (1, ECANCELED, PLC_NODEVICE);
  593. }
  594. }
  595. openchannel (&channel);
  596. if (!(plc.message = malloc (sizeof (* plc.message))))
  597. {
  598. error (1, errno, PLC_NOMEMORY);
  599. }
  600. if (!argc)
  601. {
  602. manager (&plc, loop, wait);
  603. }
  604. while ((argc) && (* argv))
  605. {
  606. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  607. {
  608. error (1, errno, PLC_BAD_MAC, * argv);
  609. }
  610. manager (&plc, loop, wait);
  611. argc--;
  612. argv++;
  613. }
  614. free (plc.message);
  615. closechannel (&channel);
  616. exit (0);
  617. }