plctool.c 18 KB

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