int6kuart.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * int6kuart.c - Qualcomm Atheros Serial Line Device Manager;
  11. *
  12. * Contributor(s):
  13. * Charles Maier <cmaier@qca.qualcomm.com>
  14. * Mathieu Olivari <mathieu@qca.qualcomm.com>
  15. *
  16. *--------------------------------------------------------------------*/
  17. /*====================================================================*
  18. * system header files;
  19. *--------------------------------------------------------------------*/
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #if defined (WIN32)
  24. # include <net/ethernet.h>
  25. #elif defined (__linux__)
  26. # include <net/ethernet.h>
  27. #elif defined (__APPLE__)
  28. # include <net/ethernet.h>
  29. #elif defined (__OpenBSD__)
  30. # include <sys/socket.h>
  31. # include <net/if.h>
  32. # include <net/if_arp.h>
  33. # include <netinet/in.h>
  34. # include <netinet/if_ether.h>
  35. #else
  36. #error "Unknown Environment"
  37. #endif
  38. /*====================================================================*
  39. * custom header files;
  40. *--------------------------------------------------------------------*/
  41. #include "../tools/getoptv.h"
  42. #include "../tools/putoptv.h"
  43. #include "../tools/number.h"
  44. #include "../tools/memory.h"
  45. #include "../tools/endian.h"
  46. #include "../tools/files.h"
  47. #include "../tools/flags.h"
  48. #include "../tools/error.h"
  49. #include "../tools/types.h"
  50. #include "../serial/serial.h"
  51. #include "../plc/plc.h"
  52. /*====================================================================*
  53. * custom source files;
  54. *--------------------------------------------------------------------*/
  55. #ifndef MAKEFILE
  56. #include "../tools/getoptv.c"
  57. #include "../tools/putoptv.c"
  58. #include "../tools/version.c"
  59. #include "../tools/uintspec.c"
  60. #include "../tools/basespec.c"
  61. #include "../tools/synonym.c"
  62. #include "../tools/todigit.c"
  63. #include "../tools/error.c"
  64. #include "../tools/checksum32.c"
  65. #include "../tools/hexencode.c"
  66. #include "../tools/hexdump.c"
  67. #include "../tools/hexstring.c"
  68. #include "../tools/hexdecode.c"
  69. #include "../tools/error.c"
  70. #endif
  71. #ifndef MAKEFILE
  72. #include "../serial/openport.c"
  73. #include "../serial/closeport.c"
  74. #include "../serial/serial.c"
  75. #endif
  76. /*====================================================================*
  77. * program constants;
  78. *--------------------------------------------------------------------*/
  79. #define FRAME_MIN_CHAR 120
  80. #define FRAME_MAX_CHAR 1496
  81. /*====================================================================*
  82. * program variables;
  83. *--------------------------------------------------------------------*/
  84. typedef struct uart
  85. {
  86. struct _file_ port;
  87. struct _file_ pib;
  88. struct _file_ nvm;
  89. struct _file_ eth;
  90. char const * string;
  91. char PIBVersion [3];
  92. char IMGVersion [128];
  93. byte MACAddress [ETHER_ADDR_LEN];
  94. char NMKDigest [16];
  95. byte NMKNumber;
  96. byte module;
  97. uint16_t bfsize;
  98. uint16_t snooze;
  99. uint16_t timeout;
  100. unsigned flags;
  101. }
  102. uart;
  103. /*====================================================================*
  104. *
  105. * void at_writenvm (struct uart * uart);
  106. *
  107. * read firmware image from file and send to device using command
  108. * "ATWPF"; the file descriptor is "nvm" member of struct uart;
  109. *
  110. *--------------------------------------------------------------------*/
  111. static void at_writenvm (struct uart * uart)
  112. {
  113. extern struct command command;
  114. byte memory [UART_BLOCKSIZE];
  115. signed mblock = sizeof (memory);
  116. uint16_t mlength = 0;
  117. uint32_t moffset = 0;
  118. uint32_t mchksum;
  119. uint16_t olength = 0;
  120. uint32_t ooffset = 0;
  121. uint32_t ochksum;
  122. while ((mblock = read (uart->nvm.file, memory, mblock)) > 0)
  123. {
  124. clearcommand ();
  125. insert ('A');
  126. insert ('T');
  127. insert ('W');
  128. insert ('P');
  129. insert ('F');
  130. insert ('1');
  131. insert (',');
  132. mchksum = checksum32 (memory, (size_t) (mblock), 0);
  133. mlength = (uint16_t) (mblock);
  134. mlength = HTOBE16 (mlength);
  135. decode (& mlength, sizeof (mlength));
  136. mlength = BE16TOH (mlength);
  137. insert (',');
  138. moffset = HTOBE32 (moffset);
  139. decode (& moffset, sizeof (moffset));
  140. moffset = BE32TOH (moffset);
  141. insert (',');
  142. mchksum = HTOBE32 (mchksum);
  143. decode (& mchksum, sizeof (mchksum));
  144. mchksum = BE32TOH (mchksum);
  145. insert (',');
  146. decode (memory, mlength);
  147. insert ('\r');
  148. sendcommand (& uart->port, uart->flags);
  149. readcommand (& uart->port, uart->flags);
  150. mustbe ('O');
  151. mustbe ('K');
  152. mustbe ('1');
  153. mustbe (',');
  154. olength = (uint16_t) (hextoint (sizeof (olength)));
  155. if (olength != mlength)
  156. {
  157. command.buffer [command.offset] = (char) (0);
  158. error (1, EINVAL, "[%s]: expected length %X", command.buffer, mlength);
  159. }
  160. mustbe (',');
  161. ooffset = (uint32_t) (hextoint (sizeof (ooffset)));
  162. if (ooffset != moffset)
  163. {
  164. command.buffer [command.offset] = (char) (0);
  165. error (1, EINVAL, "[%s]: expected offset %X", command.buffer, moffset);
  166. }
  167. mustbe (',');
  168. ochksum = (uint32_t) (hextoint (sizeof (ochksum)));
  169. if (ochksum != mchksum)
  170. {
  171. command.buffer [command.offset] = (char) (0);
  172. error (1, EINVAL, "[%s]: expected checksum %X (%X)", command.buffer, mchksum, ochksum);
  173. }
  174. mustbe (',');
  175. encode (memory, mblock);
  176. mustbe ('\r');
  177. moffset += mlength;
  178. if (_allclr (uart->flags, (UART_VERBOSE | UART_SILENCE)))
  179. {
  180. write (STDOUT_FILENO, ".", 1);
  181. }
  182. }
  183. #ifndef WIN32
  184. if (_allclr (uart->flags, (UART_VERBOSE | UART_SILENCE)))
  185. {
  186. write (STDOUT_FILENO, "\n", 1);
  187. }
  188. #endif
  189. return;
  190. }
  191. /*====================================================================*
  192. *
  193. * void at_writepib (struct uart * uart);
  194. *
  195. * read parameter block file and send to device using command
  196. * "ATWPF"; the file descriptor is "pib" member of struct uart;
  197. *
  198. *--------------------------------------------------------------------*/
  199. static void at_writepib (struct uart * uart)
  200. {
  201. extern struct command command;
  202. byte memory [UART_BLOCKSIZE];
  203. signed mblock = sizeof (memory);
  204. uint16_t mlength = 0;
  205. uint16_t moffset = 0;
  206. uint32_t mchksum;
  207. uint16_t olength = 0;
  208. uint16_t ooffset = 0;
  209. uint32_t ochksum;
  210. while ((mblock = read (uart->pib.file, memory, mblock)) > 0)
  211. {
  212. clearcommand ();
  213. insert ('A');
  214. insert ('T');
  215. insert ('W');
  216. insert ('P');
  217. insert ('F');
  218. insert ('2');
  219. insert (',');
  220. mchksum = checksum32 (memory, (size_t) (mblock), 0);
  221. mlength = (uint16_t) (mblock);
  222. mlength = HTOBE16 (mlength);
  223. decode (& mlength, sizeof (mlength));
  224. mlength = BE16TOH (mlength);
  225. insert (',');
  226. moffset = HTOBE16 (moffset);
  227. decode (& moffset, sizeof (moffset));
  228. moffset = BE16TOH (moffset);
  229. insert (',');
  230. mchksum = HTOBE32 (mchksum);
  231. decode (& mchksum, sizeof (mchksum));
  232. mchksum = BE32TOH (mchksum);
  233. insert (',');
  234. decode (memory, mlength);
  235. insert ('\r');
  236. sendcommand (& uart->port, uart->flags);
  237. readcommand (& uart->port, uart->flags);
  238. mustbe ('O');
  239. mustbe ('K');
  240. mustbe ('2');
  241. mustbe (',');
  242. olength = (uint16_t) (hextoint (sizeof (olength)));
  243. if (olength != mlength)
  244. {
  245. command.buffer [command.offset] = (char) (0);
  246. error (1, EINVAL, "[%s]: expected length %X", command.buffer, mlength);
  247. }
  248. mustbe (',');
  249. ooffset = (uint16_t) (hextoint (sizeof (ooffset)));
  250. if (ooffset != moffset)
  251. {
  252. command.buffer [command.offset] = (char) (0);
  253. error (1, EINVAL, "[%s]: expected offset %X", command.buffer, moffset);
  254. }
  255. mustbe (',');
  256. ochksum = (uint32_t) (hextoint (sizeof (ochksum)));
  257. if (ochksum != mchksum)
  258. {
  259. command.buffer [command.offset] = (char) (0);
  260. error (1, EINVAL, "[%s]: expected checksum %X (%X)", command.buffer, mchksum, ochksum);
  261. }
  262. mustbe (',');
  263. encode (memory, mblock);
  264. mustbe ('\r');
  265. moffset += mlength;
  266. if (_allclr (uart->flags, (UART_VERBOSE | UART_SILENCE)))
  267. {
  268. write (STDOUT_FILENO, ".", 1);
  269. }
  270. }
  271. #ifndef WIN32
  272. if (_allclr (uart->flags, (UART_VERBOSE | UART_SILENCE)))
  273. {
  274. write (STDOUT_FILENO, "\n", 1);
  275. }
  276. #endif
  277. return;
  278. }
  279. /*====================================================================*
  280. *
  281. * void at_readpib (struct uart * uart);
  282. *
  283. * read parameter block from device and save to file using command
  284. * "ATRP"; the file descriptor is "pib" member of struct uart;
  285. *
  286. *--------------------------------------------------------------------*/
  287. static void at_readpib (struct uart * uart)
  288. {
  289. extern struct command command;
  290. byte memory [UART_BLOCKSIZE];
  291. signed mblock = sizeof (memory);
  292. uint16_t mextent = 0;
  293. uint16_t mlength = 0;
  294. uint16_t moffset = 0;
  295. uint16_t olength = 0;
  296. uint16_t ooffset = 0;
  297. clearcommand ();
  298. insert ('A');
  299. insert ('T');
  300. insert ('R');
  301. insert ('P');
  302. insert ('2');
  303. insert (',');
  304. insert ('4');
  305. insert ('\r');
  306. sendcommand (& uart->port, uart->flags);
  307. readcommand (& uart->port, uart->flags);
  308. mustbe ('O');
  309. mustbe ('K');
  310. mustbe ('2');
  311. mustbe (',');
  312. mustbe ('4');
  313. mustbe (',');
  314. encode (& mextent, sizeof (mextent));
  315. mextent = LE16TOH (mextent);
  316. mustbe ('\r');
  317. while (mextent)
  318. {
  319. clearcommand ();
  320. insert ('A');
  321. insert ('T');
  322. insert ('R');
  323. insert ('P');
  324. if (mblock > mextent)
  325. {
  326. mblock = mextent;
  327. }
  328. mlength = (uint16_t) (mblock);
  329. mlength = HTOBE16 (mlength);
  330. decode (& mlength, sizeof (mlength));
  331. mlength = BE16TOH (mlength);
  332. insert (',');
  333. moffset = HTOBE16 (moffset);
  334. decode (& moffset, sizeof (moffset));
  335. moffset = BE16TOH (moffset);
  336. insert ('\r');
  337. sendcommand (& uart->port, uart->flags);
  338. readcommand (& uart->port, uart->flags);
  339. mustbe ('O');
  340. mustbe ('K');
  341. olength = (uint16_t) (hextoint (sizeof (olength)));
  342. if (olength != mlength)
  343. {
  344. command.buffer [command.offset] = (char) (0);
  345. error (1, EINVAL, "[%s]: have %d bytes but wanted %d", command.buffer, olength, mlength);
  346. }
  347. mustbe (',');
  348. ooffset = (uint16_t) (hextoint (sizeof (ooffset)));
  349. if (ooffset != moffset)
  350. {
  351. command.buffer [command.offset] = (char) (0);
  352. error (1, EINVAL, "[%s]: expected offset %X", command.buffer, moffset);
  353. }
  354. mustbe (',');
  355. encode (memory, mblock);
  356. if (write (uart->pib.file, memory, mblock) < mblock)
  357. {
  358. command.buffer [command.offset] = (char) (0);
  359. error (1, errno, "[%s]: expected length %d", command.buffer, mblock);
  360. }
  361. mustbe ('\r');
  362. moffset += mblock;
  363. mextent -= mblock;
  364. if (_allclr (uart->flags, (UART_VERBOSE | UART_SILENCE)))
  365. {
  366. write (STDOUT_FILENO, ".", 1);
  367. }
  368. }
  369. #ifndef WIN32
  370. if (_allclr (uart->flags, (UART_VERBOSE | UART_SILENCE)))
  371. {
  372. write (STDOUT_FILENO, "\n", 1);
  373. }
  374. #endif
  375. return;
  376. }
  377. /*====================================================================*
  378. *
  379. * void at_wake (struct uart * uart);
  380. *
  381. * send wake command "+++" to enter command mode;
  382. *
  383. *--------------------------------------------------------------------*/
  384. static void at_wake (struct uart * uart)
  385. {
  386. clearcommand ();
  387. insert ('+');
  388. insert ('+');
  389. insert ('+');
  390. sendcommand (& uart->port, uart->flags);
  391. readcommand (& uart->port, uart->flags);
  392. mustbe ('O');
  393. mustbe ('K');
  394. mustbe ('\r');
  395. return;
  396. }
  397. /*====================================================================*
  398. *
  399. * void at_command (struct uart * uart);
  400. *
  401. * send custom command; use this function to send any serial line
  402. * command that may not be supported by this program;
  403. *
  404. *--------------------------------------------------------------------*/
  405. static void at_command (struct uart * uart)
  406. {
  407. clearcommand ();
  408. while (* uart->string)
  409. {
  410. insert (* uart->string++);
  411. }
  412. insert ('\r');
  413. sendcommand (& uart->port, uart->flags);
  414. readcommand (& uart->port, uart->flags);
  415. clearcommand ();
  416. return;
  417. }
  418. /*====================================================================*
  419. *
  420. * void at_respond (struct uart * uart);
  421. *
  422. * send command "AT" to test command mode; this command does nothing
  423. * but echo "OK";
  424. *
  425. *--------------------------------------------------------------------*/
  426. static void at_respond (struct uart * uart)
  427. {
  428. clearcommand ();
  429. insert ('A');
  430. insert ('T');
  431. insert ('\r');
  432. sendcommand (& uart->port, uart->flags);
  433. readcommand (& uart->port, uart->flags);
  434. mustbe ('O');
  435. mustbe ('K');
  436. mustbe ('\r');
  437. return;
  438. }
  439. /*====================================================================*
  440. *
  441. * void atz (struct uart * uart);
  442. *
  443. * send command "ATZ" to reset the device; no response is expected;
  444. *
  445. *--------------------------------------------------------------------*/
  446. static void atz (struct uart * uart)
  447. {
  448. clearcommand ();
  449. insert ('A');
  450. insert ('T');
  451. insert ('Z');
  452. insert ('\r');
  453. sendcommand (& uart->port, uart->flags);
  454. return;
  455. }
  456. /*====================================================================*
  457. *
  458. * void atrv (struct uart * uart);
  459. *
  460. * read and display the firmware image version using command "ATRV";
  461. * return the version string in IMGVersion member of struct uart;
  462. *
  463. *--------------------------------------------------------------------*/
  464. static void atrv (struct uart * uart)
  465. {
  466. clearcommand ();
  467. insert ('A');
  468. insert ('T');
  469. insert ('R');
  470. insert ('V');
  471. insert ('\r');
  472. sendcommand (& uart->port, uart->flags);
  473. readcommand (& uart->port, uart->flags);
  474. mustbe ('O');
  475. mustbe ('K');
  476. mustbe ('\"');
  477. string (uart->IMGVersion);
  478. mustbe ('\"');
  479. mustbe ('\r');
  480. printf ("%s\n", uart->IMGVersion);
  481. return;
  482. }
  483. /*====================================================================*
  484. *
  485. * void atrpm (struct uart * uart);
  486. *
  487. * read and display the PIB version and MAC address using command
  488. * "ATRPM"; return version string in PIBVersion member and address
  489. * string in MACAddress member of struct
  490. *
  491. *
  492. *--------------------------------------------------------------------*/
  493. static void atrpm (struct uart * uart)
  494. {
  495. char mac [ETHER_ADDR_LEN * 3];
  496. clearcommand ();
  497. insert ('A');
  498. insert ('T');
  499. insert ('R');
  500. insert ('P');
  501. insert ('M');
  502. insert ('\r');
  503. sendcommand (& uart->port, uart->flags);
  504. readcommand (& uart->port, uart->flags);
  505. mustbe ('O');
  506. mustbe ('K');
  507. mustbe ('\"');
  508. string (uart->PIBVersion);
  509. mustbe ('\"');
  510. mustbe (',');
  511. encode (uart->MACAddress, sizeof (uart->MACAddress));
  512. mustbe ('\r');
  513. printf ("%s %s\n", uart->PIBVersion, hexstring (mac, sizeof (mac), uart->MACAddress, sizeof (uart->MACAddress)));
  514. return;
  515. }
  516. /*====================================================================*
  517. *
  518. * void atsk1 (struct uart * uart);
  519. *
  520. * send Set Key command "ATSK"; ask device for NMK; encode returned
  521. * key into uart-NMK;
  522. *
  523. *--------------------------------------------------------------------*/
  524. static void atsk1 (struct uart * uart)
  525. {
  526. char key [48];
  527. clearcommand ();
  528. insert ('A');
  529. insert ('T');
  530. insert ('S');
  531. insert ('K');
  532. insert ('?');
  533. insert ('\r');
  534. sendcommand (& uart->port, uart->flags);
  535. readcommand (& uart->port, uart->flags);
  536. mustbe ('O');
  537. mustbe ('K');
  538. encode (uart->NMKDigest, sizeof (uart->NMKDigest));
  539. mustbe ('\r');
  540. printf ("%s\n", hexstring (key, sizeof (key), uart->NMKDigest, sizeof (uart->NMKDigest)));
  541. return;
  542. }
  543. /*====================================================================*
  544. *
  545. * void atsk2 (struct uart * uart);
  546. *
  547. * send Set Key command "ATSK"; send device the NMK; encode returned
  548. *
  549. *--------------------------------------------------------------------*/
  550. static void atsk2 (struct uart * uart)
  551. {
  552. char key [48];
  553. clearcommand ();
  554. insert ('A');
  555. insert ('T');
  556. insert ('S');
  557. insert ('K');
  558. decode (uart->NMKDigest, sizeof (uart->NMKDigest));
  559. insert ('\r');
  560. sendcommand (& uart->port, uart->flags);
  561. readcommand (& uart->port, uart->flags);
  562. mustbe ('O');
  563. mustbe ('K');
  564. encode (uart->NMKDigest, sizeof (uart->NMKDigest));
  565. mustbe ('\r');
  566. printf ("%s\n", hexstring (key, sizeof (key), uart->NMKDigest, sizeof (uart->NMKDigest)));
  567. return;
  568. }
  569. /*====================================================================*
  570. *
  571. * void atdst1 (struct uart * uart);
  572. *
  573. * read transparent mode destination MAC address command "ATDST?";
  574. *
  575. *--------------------------------------------------------------------*/
  576. static void atdst1 (struct uart * uart)
  577. {
  578. char mac [ETHER_ADDR_LEN * 3];
  579. clearcommand ();
  580. insert ('A');
  581. insert ('T');
  582. insert ('D');
  583. insert ('S');
  584. insert ('T');
  585. insert ('?');
  586. insert ('\r');
  587. sendcommand (& uart->port, uart->flags);
  588. readcommand (& uart->port, uart->flags);
  589. mustbe ('O');
  590. mustbe ('K');
  591. encode (uart->MACAddress, sizeof (uart->MACAddress));
  592. mustbe ('\r');
  593. printf ("%s\n", hexstring (mac, sizeof (mac), uart->MACAddress, sizeof (uart->MACAddress)));
  594. return;
  595. }
  596. /*====================================================================*
  597. *
  598. * void atdst2 (struct uart * uart);
  599. *
  600. * read transparent mode destination MAC address command "ATDST?";
  601. *
  602. *--------------------------------------------------------------------*/
  603. static void atdst2 (struct uart * uart)
  604. {
  605. char mac [ETHER_ADDR_LEN * 3];
  606. clearcommand ();
  607. insert ('A');
  608. insert ('T');
  609. insert ('D');
  610. insert ('S');
  611. insert ('T');
  612. decode (uart->MACAddress, sizeof (uart->MACAddress));
  613. insert ('\r');
  614. sendcommand (& uart->port, uart->flags);
  615. readcommand (& uart->port, uart->flags);
  616. mustbe ('O');
  617. mustbe ('K');
  618. encode (uart->MACAddress, sizeof (uart->MACAddress));
  619. mustbe ('\r');
  620. printf ("%s\n", hexstring (mac, sizeof (mac), uart->MACAddress, sizeof (uart->MACAddress)));
  621. return;
  622. }
  623. /*====================================================================*
  624. *
  625. * void atni (struct uart * uart);
  626. *
  627. * reset device to factory default pib command "ATNI";
  628. *
  629. *--------------------------------------------------------------------*/
  630. static void atni (struct uart * uart)
  631. {
  632. unsigned count;
  633. unsigned index;
  634. uint16_t rxrate;
  635. uint16_t txrate;
  636. byte address [ETHER_ADDR_LEN];
  637. char mac [ETHER_ADDR_LEN * 3];
  638. clearcommand ();
  639. insert ('A');
  640. insert ('T');
  641. insert ('N');
  642. insert ('I');
  643. insert ('?');
  644. insert ('\r');
  645. sendcommand (& uart->port, uart->flags);
  646. readcommand (& uart->port, uart->flags);
  647. mustbe ('O');
  648. mustbe ('K');
  649. count = (unsigned) (hextoint (sizeof (unsigned)));
  650. while (count--)
  651. {
  652. mustbe (',');
  653. index = (unsigned) (hextoint (sizeof (index)));
  654. mustbe (',');
  655. encode (address, sizeof (address));
  656. mustbe (',');
  657. txrate = (uint16_t) (hextoint (sizeof (rxrate)));
  658. mustbe (',');
  659. rxrate = (uint16_t) (hextoint (sizeof (txrate)));
  660. printf ("%d %s %3d RX %3d TX\n", index, hexstring (mac, sizeof (mac), address, sizeof (address)), rxrate, txrate);
  661. }
  662. mustbe ('\r');
  663. return;
  664. }
  665. /*====================================================================*
  666. *
  667. * void atfd (struct uart * uart);
  668. *
  669. * reset device to factory default pib command "ATFD";
  670. *
  671. *--------------------------------------------------------------------*/
  672. static void atfd (struct uart * uart)
  673. {
  674. clearcommand ();
  675. insert ('A');
  676. insert ('T');
  677. insert ('F');
  678. insert ('D');
  679. insert ('\r');
  680. sendcommand (& uart->port, uart->flags);
  681. readcommand (& uart->port, uart->flags);
  682. mustbe ('O');
  683. mustbe ('K');
  684. mustbe ('\r');
  685. return;
  686. }
  687. /*====================================================================*
  688. *
  689. * void atps (struct uart * uart);
  690. *
  691. * etner power save mode command "ATPS";
  692. *
  693. *--------------------------------------------------------------------*/
  694. static void atps (struct uart * uart)
  695. {
  696. extern struct command command;
  697. uint16_t result;
  698. clearcommand ();
  699. insert ('A');
  700. insert ('T');
  701. insert ('P');
  702. insert ('S');
  703. uart->snooze = HTOBE16 (uart->snooze);
  704. decode (& uart->snooze, sizeof (uart->snooze));
  705. uart->snooze = BE16TOH (uart->snooze);
  706. insert ('\r');
  707. sendcommand (& uart->port, uart->flags);
  708. readcommand (& uart->port, uart->flags);
  709. mustbe ('O');
  710. mustbe ('K');
  711. result = (uint16_t) (hextoint (sizeof (result)));
  712. if (result != uart->snooze)
  713. {
  714. error (1, EINVAL, "[%s]: expected timeout %04X", command.buffer, uart->snooze);
  715. }
  716. mustbe ('\r');
  717. return;
  718. }
  719. /*====================================================================*
  720. *
  721. * void ato (struct uart * uart);
  722. *
  723. * exit command mode and enter tyransparent mode command "ATO";
  724. *
  725. *--------------------------------------------------------------------*/
  726. static void ato (struct uart * uart)
  727. {
  728. clearcommand ();
  729. insert ('A');
  730. insert ('T');
  731. insert ('O');
  732. insert ('\r');
  733. sendcommand (& uart->port, uart->flags);
  734. readcommand (& uart->port, uart->flags);
  735. mustbe ('O');
  736. mustbe ('K');
  737. mustbe ('\r');
  738. return;
  739. }
  740. /*====================================================================*
  741. *
  742. * void athsc (struct uart * uart);
  743. *
  744. * exit command mode; enter high speed command mode "ATHSC";
  745. *
  746. *--------------------------------------------------------------------*/
  747. static void athsc (struct uart * uart)
  748. {
  749. clearcommand ();
  750. insert ('A');
  751. insert ('T');
  752. insert ('H');
  753. insert ('S');
  754. insert ('C');
  755. insert ('\r');
  756. sendcommand (& uart->port, uart->flags);
  757. readcommand (& uart->port, uart->flags);
  758. mustbe ('O');
  759. mustbe ('K');
  760. mustbe ('\r');
  761. return;
  762. }
  763. /*====================================================================*
  764. *
  765. * void atwnv (struct uart * uart);
  766. *
  767. * write PIB and/or IMG to NVM "ATWNVx";
  768. *
  769. *--------------------------------------------------------------------*/
  770. static void atwnv (struct uart * uart)
  771. {
  772. extern struct command command;
  773. byte result;
  774. clearcommand ();
  775. insert ('A');
  776. insert ('T');
  777. insert ('W');
  778. insert ('N');
  779. insert ('V');
  780. decode (& uart->module, sizeof (uart->module));
  781. insert ('\r');
  782. sendcommand (& uart->port, uart->flags);
  783. readcommand (& uart->port, uart->flags);
  784. mustbe ('O');
  785. mustbe ('K');
  786. result = (byte) (hextoint (sizeof (result)));
  787. if (result != uart->module)
  788. {
  789. error (1, EINVAL, "[%s]: expected module %d", command.buffer, uart->module);
  790. }
  791. mustbe ('\r');
  792. return;
  793. }
  794. /*====================================================================*
  795. *
  796. * void atbsz1 (struct uart * uart);
  797. *
  798. * get transparent mode buffer size "ATBSZ?";
  799. *
  800. *--------------------------------------------------------------------*/
  801. static void atbsz1 (struct uart * uart)
  802. {
  803. clearcommand ();
  804. insert ('A');
  805. insert ('T');
  806. insert ('B');
  807. insert ('S');
  808. insert ('Z');
  809. insert ('?');
  810. insert ('\r');
  811. sendcommand (& uart->port, uart->flags);
  812. readcommand (& uart->port, uart->flags);
  813. mustbe ('O');
  814. mustbe ('K');
  815. uart->bfsize = (uint16_t) (hextoint (sizeof (uart->bfsize)));
  816. mustbe ('\r');
  817. printf ("%d\n", uart->bfsize);
  818. return;
  819. }
  820. /*====================================================================*
  821. *
  822. * void atbsz2 (struct uart * uart);
  823. *
  824. * set transparent mode buffer size "ATBSZn";
  825. *
  826. *--------------------------------------------------------------------*/
  827. static void atbsz2 (struct uart * uart)
  828. {
  829. extern struct command command;
  830. uint16_t result;
  831. clearcommand ();
  832. insert ('A');
  833. insert ('T');
  834. insert ('B');
  835. insert ('S');
  836. insert ('Z');
  837. uart->bfsize = HTOBE16 (uart->bfsize);
  838. decode (& uart->bfsize, sizeof (uart->bfsize));
  839. uart->bfsize = BE16TOH (uart->bfsize);
  840. insert ('\r');
  841. sendcommand (& uart->port, uart->flags);
  842. readcommand (& uart->port, uart->flags);
  843. mustbe ('O');
  844. mustbe ('K');
  845. result = (uint16_t) (hextoint (sizeof (result)));
  846. if (result != uart->bfsize)
  847. {
  848. error (1, EINVAL, "[%s]: expected buffer size %04X", command.buffer, uart->bfsize);
  849. }
  850. mustbe ('\r');
  851. printf ("%d\n", uart->bfsize);
  852. return;
  853. }
  854. /*====================================================================*
  855. *
  856. * void atto (struct uart * uart);
  857. *
  858. * set transparent mode buffer timeout "ATTO";
  859. *
  860. *--------------------------------------------------------------------*/
  861. static void atto (struct uart * uart)
  862. {
  863. extern struct command command;
  864. uint16_t result;
  865. clearcommand ();
  866. insert ('A');
  867. insert ('T');
  868. insert ('T');
  869. insert ('O');
  870. uart->timeout = HTOBE16 (uart->timeout);
  871. decode (& uart->timeout, sizeof (uart->timeout));
  872. uart->timeout = BE16TOH (uart->timeout);
  873. insert ('\r');
  874. sendcommand (& uart->port, uart->flags);
  875. readcommand (& uart->port, uart->flags);
  876. mustbe ('O');
  877. mustbe ('K');
  878. result = (uint16_t) (hextoint (sizeof (result)));
  879. if (result != uart->timeout)
  880. {
  881. error (1, EINVAL, "[%s]: expected timeout %04X", command.buffer, uart->timeout);
  882. }
  883. mustbe ('\r');
  884. return;
  885. }
  886. /*====================================================================*
  887. *
  888. * void atm (struct uart * uart);
  889. *
  890. *--------------------------------------------------------------------*/
  891. static void atm (struct uart * uart)
  892. {
  893. extern struct command command;
  894. uint8_t buffer [ETHER_MAX_LEN + ETHER_MAX_LEN + 512];
  895. unsigned length = (unsigned) (readframe (uart->eth.file, buffer, sizeof (buffer)));
  896. if (length < FRAME_MIN_CHAR)
  897. {
  898. error (1, ENOTSUP, "Frame specification of %d bytes less than %d minimum", (length >> 1), (FRAME_MIN_CHAR >> 1));
  899. }
  900. if (length > FRAME_MAX_CHAR)
  901. {
  902. error (1, ENOTSUP, "Frame specification of %d bytes more than %d maximum", (length >> 1), (FRAME_MAX_CHAR >> 1));
  903. }
  904. clearcommand ();
  905. insert ('A');
  906. insert ('T');
  907. insert ('M');
  908. memcpy (command.buffer + command.length, buffer, length);
  909. command.length += (signed) (length);
  910. insert ('\r');
  911. sendcommand (& uart->port, uart->flags);
  912. readcommand (& uart->port, uart->flags);
  913. write (STDOUT_FILENO, command.buffer, command.length);
  914. write (STDOUT_FILENO, "\n", sizeof (char));
  915. return;
  916. }
  917. /*====================================================================*
  918. *
  919. * void manager (struct uart * uart);
  920. *
  921. * examine flagword in struct uart and perform requested operations
  922. * in the order that bits are tested; the order that bits are tested
  923. * may be changed as needed;
  924. *
  925. *--------------------------------------------------------------------*/
  926. static void manager (struct uart * uart)
  927. {
  928. if (_anyset (uart->flags, UART_WAKE))
  929. {
  930. at_wake (uart);
  931. }
  932. if (_anyset (uart->flags, UART_COMMAND))
  933. {
  934. at_command (uart);
  935. }
  936. if (_anyset (uart->flags, UART_RESPOND))
  937. {
  938. at_respond (uart);
  939. }
  940. if (_anyset (uart->flags, UART_ATRV))
  941. {
  942. atrv (uart);
  943. }
  944. if (_anyset (uart->flags, UART_ATRPM))
  945. {
  946. atrpm (uart);
  947. }
  948. if (_anyset (uart->flags, UART_ATDST1))
  949. {
  950. atdst1 (uart);
  951. }
  952. if (_anyset (uart->flags, UART_ATDST2))
  953. {
  954. atdst2 (uart);
  955. }
  956. if (_anyset (uart->flags, UART_ATZ))
  957. {
  958. atz (uart);
  959. }
  960. if (_anyset (uart->flags, UART_ATFD))
  961. {
  962. atfd (uart);
  963. }
  964. if (_anyset (uart->flags, UART_ATPS))
  965. {
  966. atps (uart);
  967. }
  968. if (_anyset (uart->flags, UART_ATO))
  969. {
  970. ato (uart);
  971. }
  972. if (_anyset (uart->flags, UART_ATNI))
  973. {
  974. atni (uart);
  975. }
  976. if (_anyset (uart->flags, UART_ATHSC))
  977. {
  978. athsc (uart);
  979. }
  980. if (_anyset (uart->flags, UART_ATSK1))
  981. {
  982. atsk1 (uart);
  983. }
  984. if (_anyset (uart->flags, UART_ATSK2))
  985. {
  986. atsk2 (uart);
  987. }
  988. if (_anyset (uart->flags, UART_ATRP))
  989. {
  990. at_readpib (uart);
  991. }
  992. if (_anyset (uart->flags, UART_ATWPF1))
  993. {
  994. at_writenvm (uart);
  995. }
  996. if (_anyset (uart->flags, UART_ATWPF2))
  997. {
  998. at_writepib (uart);
  999. }
  1000. if (_anyset (uart->flags, UART_ATWNV))
  1001. {
  1002. atwnv (uart);
  1003. }
  1004. if (_anyset (uart->flags, UART_ATBSZ1))
  1005. {
  1006. atbsz1 (uart);
  1007. }
  1008. if (_anyset (uart->flags, UART_ATBSZ2))
  1009. {
  1010. atbsz2 (uart);
  1011. }
  1012. if (_anyset (uart->flags, UART_ATM))
  1013. {
  1014. atm (uart);
  1015. }
  1016. if (_anyset (uart->flags, UART_ATTO))
  1017. {
  1018. atto (uart);
  1019. }
  1020. return;
  1021. }
  1022. /*====================================================================*
  1023. *
  1024. * int main (int argc, char const * argv []);
  1025. *
  1026. *--------------------------------------------------------------------*/
  1027. int main (int argc, char const * argv [])
  1028. {
  1029. static char const * optv [] =
  1030. {
  1031. "bc:C:dD:F:HiImM:n:N:Op:P:qrRS:s:tTvwW:zZ:",
  1032. "",
  1033. "Qualcomm Atheros Serial Line Device Manager",
  1034. "b\tset default host baud rate",
  1035. "c s\tsend custom serial line command (s)",
  1036. "C x\tcommit module (x) to NVM [ATWNVx]",
  1037. "d\tget destination mac address [ATDST?]",
  1038. "D x\tset destination mac address [ATDSTx]",
  1039. "F f\tframe file is (s)",
  1040. "H\tplace device in High Speed Command Mode [ATHSC]",
  1041. "i\tget network information [ATNI]",
  1042. "I\tget PIB version and MAC address [ATRPM]",
  1043. "m\tget network membership key [ATSK?]",
  1044. "M x\tset network membership key [ATSKx]",
  1045. "N f\twrite NVM file (f) to SDRAM [ATWFP1]",
  1046. "O\tplace device in Transparent Mode [ATO]",
  1047. "p f\tread PIB from SDRAM to file (f) [ATRP]",
  1048. "P f\twrite PIB file (f) to SDRAM [ATWFP2]",
  1049. "q\tplace program in quiet mode",
  1050. "r\tget parameter/firmware revision [ATRV]",
  1051. "R\treset device [ATZ]",
  1052. "s f\tserial port is (f) [" DEVICE "]",
  1053. "S n\tenter power save mode for (n) seconds [ATPS]",
  1054. "t\ttest device [AT]",
  1055. "T\treset to factory defaults [ATFD]",
  1056. "v\tplace program verbose mode",
  1057. "w\tplace device in Command Mode [+++]",
  1058. "W x\tset Transparent Mode aggregation timeout [ATTO]",
  1059. "z\tget Transparent Mode buffer size [ATBSZ?]",
  1060. "Z n\tset Transparent Mode buffer size [ATBSZn]",
  1061. (char const *) (0)
  1062. };
  1063. struct uart uart =
  1064. {
  1065. {
  1066. 0,
  1067. DEVICE
  1068. },
  1069. {
  1070. - 1,
  1071. "nvmfile"
  1072. },
  1073. {
  1074. - 1,
  1075. "pibfile"
  1076. },
  1077. {
  1078. - 1,
  1079. "ethfile"
  1080. },
  1081. (char *) (0),
  1082. {
  1083. 0
  1084. },
  1085. {
  1086. 0
  1087. },
  1088. {
  1089. 0
  1090. },
  1091. {
  1092. 0
  1093. },
  1094. (uint8_t) (0),
  1095. (uint8_t) (0),
  1096. (uint16_t) (0),
  1097. (uint16_t) (0),
  1098. (uint16_t) (0),
  1099. (unsigned) (0)
  1100. };
  1101. signed c;
  1102. if (getenv (UART_PORT))
  1103. {
  1104. uart.port.name = strdup (getenv (UART_PORT));
  1105. }
  1106. while (~ (c = getoptv (argc, argv, optv)))
  1107. {
  1108. switch (c)
  1109. {
  1110. case 'b':
  1111. _setbits (uart.flags, UART_DEFAULT);
  1112. break;
  1113. case 'c':
  1114. _setbits (uart.flags, UART_COMMAND);
  1115. uart.string = optarg;
  1116. break;
  1117. case 'C':
  1118. _setbits (uart.flags, UART_ATWNV);
  1119. uart.module = (byte) (basespec (optarg, 16, sizeof (uart.module)));
  1120. break;
  1121. case 'd':
  1122. _setbits (uart.flags, UART_ATDST1);
  1123. break;
  1124. case 'D':
  1125. _setbits (uart.flags, UART_ATDST2);
  1126. if (! hexencode (uart.MACAddress, sizeof (uart.MACAddress), optarg))
  1127. {
  1128. error (1, errno, PLC_BAD_MAC, optarg);
  1129. }
  1130. break;
  1131. case 'F':
  1132. if ((uart.eth.file = open (uart.eth.name = optarg, O_BINARY | O_RDONLY)) == - 1)
  1133. {
  1134. error (1, errno, "%s", uart.eth.name);
  1135. }
  1136. _setbits (uart.flags, UART_ATM);
  1137. break;
  1138. case 'H':
  1139. _setbits (uart.flags, UART_ATHSC);
  1140. break;
  1141. case 'i':
  1142. _setbits (uart.flags, UART_ATNI);
  1143. break;
  1144. case 'I':
  1145. _setbits (uart.flags, UART_ATRPM);
  1146. break;
  1147. case 'm':
  1148. _setbits (uart.flags, UART_ATSK1);
  1149. break;
  1150. case 'M':
  1151. _setbits (uart.flags, UART_ATSK2);
  1152. if (! hexencode (uart.NMKDigest, sizeof (uart.NMKDigest), optarg))
  1153. {
  1154. error (1, errno, PLC_BAD_NMK, optarg);
  1155. }
  1156. break;
  1157. case 'N':
  1158. if ((uart.nvm.file = open (uart.nvm.name = optarg, O_BINARY | O_RDONLY)) == - 1)
  1159. {
  1160. error (1, errno, "%s", uart.nvm.name);
  1161. }
  1162. _setbits (uart.flags, UART_ATWPF1);
  1163. break;
  1164. case 'O':
  1165. _setbits (uart.flags, UART_ATO);
  1166. break;
  1167. case 'P':
  1168. if ((uart.pib.file = open (uart.pib.name = optarg, O_BINARY | O_RDONLY)) == - 1)
  1169. {
  1170. error (1, errno, "%s", uart.pib.name);
  1171. }
  1172. _setbits (uart.flags, UART_ATWPF2);
  1173. break;
  1174. case 'p':
  1175. if ((uart.pib.file = open (uart.pib.name = optarg, O_BINARY | O_CREAT | O_RDWR | O_TRUNC, FILE_FILEMODE)) == - 1)
  1176. {
  1177. error (1, errno, "%s", uart.pib.name);
  1178. }
  1179. #ifndef WIN32
  1180. chown (optarg, getuid (), getgid ());
  1181. #endif
  1182. _setbits (uart.flags, UART_ATRP);
  1183. break;
  1184. case 'q':
  1185. _setbits (uart.flags, UART_SILENCE);
  1186. break;
  1187. case 'r':
  1188. _setbits (uart.flags, UART_ATRV);
  1189. break;
  1190. case 'R':
  1191. _setbits (uart.flags, UART_ATZ);
  1192. break;
  1193. case 'S':
  1194. _setbits (uart.flags, UART_ATPS);
  1195. uart.snooze = (uint16_t) (uintspec (optarg, 1, 900));
  1196. break;
  1197. case 's':
  1198. uart.port.name = optarg;
  1199. break;
  1200. case 'T':
  1201. _setbits (uart.flags, UART_ATFD);
  1202. break;
  1203. case 't':
  1204. _setbits (uart.flags, UART_RESPOND);
  1205. break;
  1206. case 'v':
  1207. _setbits (uart.flags, UART_VERBOSE);
  1208. break;
  1209. case 'w':
  1210. _setbits (uart.flags, UART_WAKE);
  1211. break;
  1212. case 'W':
  1213. _setbits (uart.flags, UART_ATTO);
  1214. uart.timeout = (unsigned) (uintspec (optarg, 1, 2000));
  1215. break;
  1216. case 'z':
  1217. _setbits (uart.flags, UART_ATBSZ1);
  1218. break;
  1219. case 'Z':
  1220. _setbits (uart.flags, UART_ATBSZ2);
  1221. uart.bfsize = (uint16_t) (uintspec (optarg, 1, 1500));
  1222. break;
  1223. default:
  1224. break;
  1225. }
  1226. }
  1227. argc -= optind;
  1228. argv += optind;
  1229. if (argc)
  1230. {
  1231. error (1, ENOTSUP, ERROR_TOOMANY);
  1232. }
  1233. openport (& uart.port, uart.flags);
  1234. manager (& uart);
  1235. closeport (& uart.port);
  1236. exit (0);
  1237. }