amptool.c 17 KB

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