weeder.c.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. weeder.c
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='WatchdogReport.c.html' title=' WatchdogReport.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='WriteCFG.c.html' title=' WriteCFG.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  24. *
  25. * All rights reserved.
  26. *
  27. * Redistribution and use in source and binary forms, with or
  28. * without modification, are permitted (subject to the limitations
  29. * in the disclaimer below) provided that the following conditions
  30. * are met:
  31. *
  32. * * Redistributions of source code must retain the above copyright
  33. * notice, this list of conditions and the following disclaimer.
  34. *
  35. * * Redistributions in binary form must reproduce the above
  36. * copyright notice, this list of conditions and the following
  37. * disclaimer in the documentation and/or other materials
  38. * provided with the distribution.
  39. *
  40. * * Neither the name of Qualcomm Atheros nor the names of
  41. * its contributors may be used to endorse or promote products
  42. * derived from this software without specific prior written
  43. * permission.
  44. *
  45. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  46. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  47. * COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; AND ANY EXPRESS OR
  48. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  49. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  50. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  51. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  52. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  53. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  54. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  56. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  57. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59. *
  60. *--------------------------------------------------------------------*/
  61. /*====================================================================*
  62. *
  63. * weeder.c - Weeder Solid State Relay Module Controller;
  64. *
  65. * Contributor(s):
  66. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  67. * Nathaniel Houghton &lt;nhoughto@qca.qualcomm.com&gt;
  68. * Mathieu Olivari &lt;mathieu@qca.qualcomm.com&gt;
  69. *
  70. *--------------------------------------------------------------------*/
  71. /*====================================================================*
  72. * system header files;
  73. *--------------------------------------------------------------------*/
  74. #include &lt;unistd.h&gt;
  75. #include &lt;stdlib.h&gt;
  76. #include &lt;string.h&gt;
  77. #include &lt;ctype.h&gt;
  78. #if defined (__linux__)
  79. # include &lt;termios.h&gt;
  80. #elif defined (__APPLE__)
  81. # include &lt;termios.h&gt;
  82. #elif defined (__OpenBSD__)
  83. # include &lt;termios.h&gt;
  84. #elif defined (WIN32)
  85. # include &lt;windows.h&gt;
  86. #else
  87. #error &quot;Unknown Environment&quot;
  88. #endif
  89. /*====================================================================*
  90. * custom header files;
  91. *--------------------------------------------------------------------*/
  92. #include &quot;../tools/getoptv.h&quot;
  93. #include &quot;../tools/putoptv.h&quot;
  94. #include &quot;../tools/version.h&quot;
  95. #include &quot;../tools/number.h&quot;
  96. #include &quot;../tools/symbol.h&quot;
  97. #include &quot;../tools/timer.h&quot;
  98. #include &quot;../tools/files.h&quot;
  99. #include &quot;../tools/flags.h&quot;
  100. #include &quot;../tools/error.h&quot;
  101. /*====================================================================*
  102. * custom source files;
  103. *--------------------------------------------------------------------*/
  104. #ifndef MAKEFILE
  105. #include &quot;../tools/getoptv.c&quot;
  106. #include &quot;../tools/putoptv.c&quot;
  107. #include &quot;../tools/version.c&quot;
  108. #include &quot;../tools/uintspec.c&quot;
  109. #include &quot;../tools/synonym.c&quot;
  110. #include &quot;../tools/todigit.c&quot;
  111. #include &quot;../tools/error.c&quot;
  112. #endif
  113. /*====================================================================*
  114. * program constants;
  115. *--------------------------------------------------------------------*/
  116. #define WEEDER_UNITS &quot;BA&quot;
  117. #define WEEDER_LEDS 5
  118. #define WEEDER_BITS 7
  119. #define WEEDER_WAIT 25
  120. #define WEEDER_ECHO 0
  121. #define WEEDER_MODE 1
  122. #define WEEDER_BUFFER_LENGTH 10
  123. #define WEEDER_STRING_LENGTH 15
  124. #ifdef WIN32
  125. # define WEEDER_PORT &quot;com1:&quot;
  126. #else
  127. # define WEEDER_PORT &quot;/dev/ttyS0&quot;
  128. #endif
  129. #define WEEDER_SILENCE (1 &lt;&lt; 0)
  130. #define WEEDER_VERBOSE (1 &lt;&lt; 1)
  131. #define WEEDER_DISPLAY (1 &lt;&lt; 2)
  132. #define WEEDER_NEWLINE (1 &lt;&lt; 2)
  133. /*====================================================================*
  134. * program variables;
  135. *--------------------------------------------------------------------*/
  136. static struct _term_ const modes [] =
  137. {
  138. {
  139. &quot;off&quot;,
  140. &quot;0&quot;
  141. },
  142. {
  143. &quot;on&quot;,
  144. &quot;1&quot;
  145. }
  146. };
  147. static char buffer [WEEDER_BUFFER_LENGTH];
  148. static char string [WEEDER_STRING_LENGTH];
  149. static signed length = 0;
  150. static signed offset = 0;
  151. /*====================================================================*
  152. *
  153. * void function1 (struct _file_ * port, char const * units, unsigned wait, unsigned echo);
  154. *
  155. * send echo command to Weeder Solid State Relay modules B then A;
  156. * Standard Atheros relay modules were wired in reverse order for
  157. * some reason;
  158. *
  159. *--------------------------------------------------------------------*/
  160. static void function1 (struct _file_ * port, char const * units, unsigned wait, unsigned echo)
  161. {
  162. extern char buffer [];
  163. extern signed length;
  164. while (*units)
  165. {
  166. length = 0;
  167. buffer [length++] = *units++;
  168. buffer [length++] = 'X';
  169. buffer [length++] = '0' + (echo &amp; 1);
  170. buffer [length++] = '\r';
  171. if (write (port-&gt;file, buffer, length) != length)
  172. {
  173. error (1, errno, FILE_CANTSAVE, port-&gt;name);
  174. }
  175. SLEEP (wait);
  176. }
  177. return;
  178. }
  179. /*====================================================================*
  180. *
  181. * void function2 (struct _file_ * port, char const * units, unsigned wait, unsigned mode, unsigned data);
  182. *
  183. * send write command to Weeder Solid State Relay modules B then A
  184. * because Qualcomm Atheros relay modules are wired in reverse order
  185. * for some reason;
  186. *
  187. *--------------------------------------------------------------------*/
  188. static void function2 (struct _file_ * port, char const * units, unsigned wait, unsigned mode, unsigned data)
  189. {
  190. extern char buffer [WEEDER_BUFFER_LENGTH];
  191. extern signed length;
  192. length = 0;
  193. buffer [length++] = *units++;
  194. buffer [length++] = 'W';
  195. buffer [length++] = '0' + (mode &amp; 1);
  196. buffer [length++] = '0';
  197. buffer [length++] = '0';
  198. while (length &lt; WEEDER_BITS)
  199. {
  200. buffer [length++] = '0' + (data &amp; 1);
  201. data &gt;&gt;= 1;
  202. }
  203. buffer [length++] = '\r';
  204. if (write (port-&gt;file, buffer, length) != length)
  205. {
  206. error (1, errno, FILE_CANTSAVE, port-&gt;name);
  207. }
  208. SLEEP (wait);
  209. length = 0;
  210. buffer [length++] = *units++;
  211. buffer [length++] = 'W';
  212. while (length &lt; WEEDER_BITS)
  213. {
  214. buffer [length++] = '0' + (data &amp; 1);
  215. data &gt;&gt;= 1;
  216. }
  217. buffer [length++] = '\r';
  218. if (write (port-&gt;file, buffer, length) != length)
  219. {
  220. error (1, errno, FILE_CANTSAVE, port-&gt;name);
  221. }
  222. SLEEP (wait);
  223. return;
  224. }
  225. /*====================================================================*
  226. *
  227. * void function3 (struct _file_ * port, char const * units, unsigned wait);
  228. *
  229. * read weeder solid state controller and display settings on the
  230. * console as attenuation;
  231. *
  232. *--------------------------------------------------------------------*/
  233. static void function3 (struct _file_ * port, char const * units, unsigned wait)
  234. {
  235. extern char buffer [WEEDER_BUFFER_LENGTH];
  236. extern char string [WEEDER_STRING_LENGTH];
  237. extern signed length;
  238. extern signed offset;
  239. unsigned number = 0;
  240. memset (string, 0, sizeof (string));
  241. for (offset = 0; *units; offset += WEEDER_LEDS)
  242. {
  243. length = 0;
  244. buffer [length++] = *units++;
  245. buffer [length++] = 'R';
  246. buffer [length++] = '\r';
  247. if (write (port-&gt;file, buffer, length) != length)
  248. {
  249. error (1, errno, FILE_CANTSAVE, port-&gt;name);
  250. }
  251. SLEEP (wait);
  252. memset (buffer, 0, sizeof (buffer));
  253. if (read (port-&gt;file, buffer, WEEDER_LEDS + 2) == -1)
  254. {
  255. error (1, errno, FILE_CANTREAD, port-&gt;name);
  256. }
  257. SLEEP (wait);
  258. memcpy (&amp;string [offset], &amp;buffer [1], WEEDER_LEDS);
  259. }
  260. while (offset-- &gt; 3)
  261. {
  262. number &lt;&lt;= 1;
  263. number |= string [offset] - '0';
  264. }
  265. printf (&quot;%d\n&quot;, number);
  266. return;
  267. }
  268. /*====================================================================*
  269. *
  270. * int main (int argc, char const * argv []);
  271. *
  272. *--------------------------------------------------------------------*/
  273. int main (int argc, char const * argv [])
  274. {
  275. static char const * optv [] =
  276. {
  277. &quot;e:m:o:p:iqrvw:&quot;,
  278. &quot;&quot;,
  279. &quot;Weeder Solid State Relay Module Controller&quot;,
  280. &quot;e n\techo is (n) [&quot; LITERAL (WEEDER_ECHO) &quot;]&quot;,
  281. &quot;m n\tmode is (n) [&quot; LITERAL (WEEDER_MODE) &quot;]&quot;,
  282. &quot;o s\tunit order is (s) [&quot; WEEDER_UNITS &quot;]&quot;,
  283. &quot;p f\tport is (f) [&quot; WEEDER_PORT &quot;]&quot;,
  284. &quot;q\tquiet mode&quot;,
  285. &quot;r\tread attenuator value&quot;,
  286. &quot;v\tverbose mode&quot;,
  287. &quot;w n\twait (n) millseconds [&quot; LITERAL (WEEDER_WAIT) &quot;]&quot;,
  288. (char const *) (0)
  289. };
  290. struct _file_ port =
  291. {
  292. -1,
  293. WEEDER_PORT
  294. };
  295. #if defined (WIN32)
  296. HANDLE hSerial;
  297. DCB dcbSerial =
  298. {
  299. 0
  300. };
  301. #else
  302. struct termios termios;
  303. #endif
  304. char const * units = WEEDER_UNITS;
  305. unsigned wait = WEEDER_WAIT;
  306. unsigned echo = WEEDER_ECHO;
  307. unsigned mode = WEEDER_MODE;
  308. unsigned data = 0;
  309. flag_t flags = (flag_t)(0);
  310. signed c;
  311. optind = 1;
  312. if (getenv (&quot;WEEDER&quot;))
  313. {
  314. port.name = strdup (getenv (&quot;WEEDER&quot;));
  315. }
  316. while ((c = getoptv (argc, argv, optv)) != -1)
  317. {
  318. switch (c)
  319. {
  320. case 'e':
  321. echo = (unsigned)(uintspec (synonym (optarg, modes, SIZEOF (modes)), 0, 1));
  322. break;
  323. case 'm':
  324. mode = (unsigned)(uintspec (synonym (optarg, modes, SIZEOF (modes)), 0, 1));
  325. break;
  326. case 'n':
  327. _setbits (flags, WEEDER_NEWLINE);
  328. break;
  329. case 'o':
  330. units = optarg;
  331. break;
  332. case 'p':
  333. port.name = optarg;
  334. break;
  335. case 'w':
  336. wait = (unsigned)(uintspec (optarg, 5, 100));
  337. break;
  338. case 'q':
  339. _setbits (flags, WEEDER_SILENCE);
  340. break;
  341. case 'r':
  342. _setbits (flags, WEEDER_DISPLAY);
  343. break;
  344. case 'v':
  345. _setbits (flags, WEEDER_VERBOSE);
  346. break;
  347. default:
  348. break;
  349. }
  350. }
  351. argc -= optind;
  352. argv += optind;
  353. if ((argc) &amp;&amp; (* argv))
  354. {
  355. data = (unsigned)(uintspec (* argv, 0, 0x7F));
  356. }
  357. #if defined (WIN32)
  358. hSerial = CreateFile (port.name, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  359. if (hSerial == INVALID_HANDLE_VALUE)
  360. {
  361. error (1, errno, FILE_CANTOPEN, port.name);
  362. }
  363. dcbSerial.DCBlength = sizeof (dcbSerial);
  364. if (!GetCommState (hSerial, &amp;dcbSerial))
  365. {
  366. error (1, 0, FILE_CANTREAD &quot; state&quot;, port.name);
  367. }
  368. dcbSerial.BaudRate = CBR_9600;
  369. dcbSerial.ByteSize = 8;
  370. dcbSerial.StopBits = ONESTOPBIT;
  371. dcbSerial.Parity = NOPARITY;
  372. if (!SetCommState (hSerial, &amp;dcbSerial))
  373. {
  374. error (1, 0, FILE_CANTSAVE, port.name);
  375. }
  376. CloseHandle (hSerial);
  377. if ((port.file = open (port.name, O_BINARY | O_RDWR)) == -1)
  378. {
  379. error (1, errno, FILE_CANTOPEN &quot; state&quot;, port.name);
  380. }
  381. #else
  382. if ((port.file = open (port.name, O_RDWR|O_NOCTTY|O_NDELAY)) == -1)
  383. {
  384. error (1, 0, FILE_CANTOPEN, port.name);
  385. }
  386. tcgetattr (port.file, &amp;termios);
  387. termios.c_cflag = CS8;
  388. cfsetospeed (&amp;termios, B9600);
  389. tcsetattr (port.file, TCSANOW, &amp;termios);
  390. #endif
  391. function1 (&amp;port, units, wait, echo);
  392. if ((argc) &amp;&amp; (* argv))
  393. {
  394. function2 (&amp;port, units, wait, mode, data);
  395. }
  396. if (_anyset (flags, WEEDER_DISPLAY))
  397. {
  398. function3 (&amp;port, units, wait);
  399. }
  400. close (port.file);
  401. exit (0);
  402. }
  403. </pre>
  404. <div class='footerlink'>
  405. [<a href='WatchdogReport.c.html' title=' WatchdogReport.c '>PREV</a>]
  406. [<a href='toolkit.html' title=' Index '>HOME</a>]
  407. [<a href='WriteCFG.c.html' title=' WriteCFG.c '>NEXT</a>]
  408. </div>
  409. </body>
  410. </html>