int6kuart.c 33 KB

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