ptsctl.c.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. ptsctl.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='psread.c.html' title=' psread.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='PushButton.c.html' title=' PushButton.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. * ptsctl.c - PTS Module Controller;
  64. *
  65. * Contributor(s):
  66. * Nathaniel Houghton &lt;nhoughto@qca.qualcomm.com&gt;
  67. * Charles Maier &lt;cmaier@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/timer.h&quot;
  101. #include &quot;../tools/error.h&quot;
  102. /*====================================================================*
  103. * custom source files;
  104. *--------------------------------------------------------------------*/
  105. #ifndef MAKEFILE
  106. #include &quot;../tools/getoptv.c&quot;
  107. #include &quot;../tools/putoptv.c&quot;
  108. #include &quot;../tools/version.c&quot;
  109. #include &quot;../tools/uintspec.c&quot;
  110. #include &quot;../tools/synonym.c&quot;
  111. #include &quot;../tools/todigit.c&quot;
  112. #include &quot;../tools/error.c&quot;
  113. #endif
  114. /*====================================================================*
  115. * program constants;
  116. *--------------------------------------------------------------------*/
  117. #define PTSCTL_DEBUG 0
  118. #define PTSCTL_UNITS &quot;CBA&quot;
  119. #define PTSCTL_LEDS 5
  120. #define PTSCTL_BITS 7
  121. #define PTSCTL_WAIT 50
  122. #define PTSCTL_ECHO 0
  123. #define PTSCTL_MODE 1
  124. #define PTSCTL_LINE_ATTN 127
  125. #define PTSCTL_GRND_ATTN 127
  126. #define PTSCTL_BUFFER_SIZE 10
  127. #define PTSCTL_STRING_SIZE 15
  128. #ifdef WIN32
  129. # define PTSCTL_PORT &quot;com1:&quot;
  130. #else
  131. # define PTSCTL_PORT &quot;/dev/ttyS0&quot;
  132. #endif
  133. #define PTSCTL_SILENCE (1 &lt;&lt; 0)
  134. #define PTSCTL_VERBOSE (1 &lt;&lt; 1)
  135. #define PTSCTL_CHANGE (1 &lt;&lt; 2)
  136. #define PTSCTL_DISPLAY (1 &lt;&lt; 3)
  137. #define PTSCTL_ITERATE (1 &lt;&lt; 4)
  138. /*====================================================================*
  139. * program variables;
  140. *--------------------------------------------------------------------*/
  141. static const struct _term_ modes [] =
  142. {
  143. {
  144. &quot;off&quot;,
  145. &quot;0&quot;
  146. },
  147. {
  148. &quot;on&quot;,
  149. &quot;1&quot;
  150. }
  151. };
  152. static char buffer [PTSCTL_BUFFER_SIZE];
  153. static char string [PTSCTL_STRING_SIZE];
  154. static signed length = 0;
  155. static signed offset = 0;
  156. /*====================================================================*
  157. *
  158. * void cycle (char * string, unsigned offset, unsigned length);
  159. *
  160. * rotate a number of consecutive characters starting at a given
  161. * offset within a string; this is used to shift the character,
  162. * that represents the power on/off bit, out of the way during
  163. * data conversions from binary to ASCII and ASCII to binary;
  164. *
  165. *--------------------------------------------------------------------*/
  166. static void cycle (char * string, unsigned offset, unsigned length)
  167. {
  168. signed c = string [offset];
  169. memcpy (&amp;string [offset], &amp;string [offset + 1], length);
  170. string [offset + length] = c;
  171. return;
  172. }
  173. /*====================================================================*
  174. *
  175. * void function1 (struct _file_ * port, char const * units, unsigned wait, unsigned echo);
  176. *
  177. * send echo command to Weeder Solid State Relay modules in an order
  178. * specified by units;
  179. *
  180. *--------------------------------------------------------------------*/
  181. static void function1 (struct _file_ * port, char const * units, unsigned wait, unsigned echo)
  182. {
  183. extern char buffer [PTSCTL_BUFFER_SIZE];
  184. extern signed length;
  185. while (*units)
  186. {
  187. length = 0;
  188. buffer [length++] = *units++;
  189. buffer [length++] = 'X';
  190. buffer [length++] = '0' + (echo &amp; 1);
  191. buffer [length++] = '\r';
  192. if (write (port-&gt;file, buffer, length) != length)
  193. {
  194. error (1, errno, FILE_CANTSAVE, port-&gt;name);
  195. }
  196. SLEEP (wait);
  197. }
  198. return;
  199. }
  200. /*====================================================================*
  201. *
  202. * void function2 (struct _file_ * port, char const * units, unsigned wait, unsigned data);
  203. *
  204. * send write command to Weeder Solid State Relay modules in an
  205. * order specified by units;
  206. *
  207. *--------------------------------------------------------------------*/
  208. static void function2 (struct _file_ * port, char const * units, unsigned wait, unsigned data)
  209. {
  210. extern char buffer [PTSCTL_BUFFER_SIZE];
  211. extern char string [PTSCTL_STRING_SIZE];
  212. extern signed length;
  213. extern signed offset;
  214. memset (string, 0, sizeof (string));
  215. memset (buffer, 0, sizeof (buffer));
  216. for (offset = 0; offset &lt; (signed)(sizeof (string)); offset++)
  217. {
  218. string [offset] = '0' + (data &amp; 1);
  219. data &gt;&gt;= 1;
  220. }
  221. cycle (string, 0, 5);
  222. for (offset = 0; *units; offset += PTSCTL_LEDS)
  223. {
  224. length = 0;
  225. buffer [length++] = *units++;
  226. buffer [length++] = 'W';
  227. memcpy (&amp;buffer [length], &amp;string [offset], PTSCTL_LEDS);
  228. length += PTSCTL_LEDS;
  229. buffer [length++] = '\r';
  230. if (write (port-&gt;file, buffer, length) != length)
  231. {
  232. error (1, errno, FILE_CANTSAVE, port-&gt;name);
  233. }
  234. SLEEP (wait);
  235. }
  236. return;
  237. }
  238. /*====================================================================*
  239. *
  240. * void function3 (struct _file_ * port, char const * units, unsigned wait);
  241. *
  242. * read weeder solid state modules and display settings on the
  243. * console as attenuation;
  244. *
  245. *--------------------------------------------------------------------*/
  246. static void function3 (struct _file_ * port, char const * units, unsigned wait)
  247. {
  248. extern char buffer [PTSCTL_BUFFER_SIZE];
  249. extern char string [PTSCTL_STRING_SIZE];
  250. extern signed length;
  251. extern signed offset;
  252. signed value1 = 0;
  253. signed value2 = 0;
  254. memset (string, 0, sizeof (string));
  255. for (offset = 0; *units; offset += PTSCTL_LEDS)
  256. {
  257. length = 0;
  258. buffer [length++] = *units++;
  259. buffer [length++] = 'R';
  260. buffer [length++] = '\r';
  261. if (write (port-&gt;file, buffer, length) != length)
  262. {
  263. error (1, errno, FILE_CANTSAVE, port-&gt;name);
  264. }
  265. SLEEP (wait);
  266. memset (buffer, 0, sizeof (buffer));
  267. if (read (port-&gt;file, buffer, PTSCTL_LEDS + 2) == -1)
  268. {
  269. error (1, errno, FILE_CANTREAD, port-&gt;name);
  270. }
  271. memcpy (&amp;string [offset], &amp;buffer [1], PTSCTL_LEDS);
  272. SLEEP (wait);
  273. }
  274. cycle (string, PTSCTL_LEDS, 2);
  275. while (--offset &gt; PTSCTL_BITS)
  276. {
  277. value1 &lt;&lt;= 1;
  278. value1 |= string [offset] - '0';
  279. }
  280. while (offset-- &gt; 0)
  281. {
  282. value2 &lt;&lt;= 1;
  283. value2 |= string [offset] - '0';
  284. }
  285. if ((value1 &gt;= 0) &amp;&amp; (value2 &gt;= 0))
  286. {
  287. printf (&quot;%d %d\n&quot;, value1, value2);
  288. }
  289. return;
  290. }
  291. /*====================================================================*
  292. *
  293. * void function4 (struct _file_ * port, char const * units, unsigned wait);
  294. *
  295. * sequence through all attenuator settings at one second intervals;
  296. * this function can be used to debug program additions and changes;
  297. *
  298. *--------------------------------------------------------------------*/
  299. static void function4 (struct _file_ * port, char const * units, unsigned wait)
  300. {
  301. signed value;
  302. for (value = 0; value &lt; 128; value++)
  303. {
  304. function2 (port, units, wait, (value &lt;&lt; 8) | (value &lt;&lt; 1) | 1);
  305. function3 (port, units, wait);
  306. SLEEP (wait);
  307. }
  308. return;
  309. }
  310. /*====================================================================*
  311. *
  312. * int main (int argc, char const * argv []);
  313. *
  314. *--------------------------------------------------------------------*/
  315. int main (int argc, char const * argv [])
  316. {
  317. static char const * optv [] =
  318. {
  319. &quot;f:g:n:p:iqrvw:z&quot;,
  320. &quot;&quot;,
  321. &quot;PTS Module Controller&quot;,
  322. &quot;f f\tport is (f) [&quot; PTSCTL_PORT &quot;]&quot;,
  323. &quot;g n\tline ground attenuation is (n) [&quot; LITERAL (PTSCTL_GRND_ATTN) &quot;]&quot;,
  324. &quot;n n\tline neutral attenuation is (n) [&quot; LITERAL (PTSCTL_LINE_ATTN) &quot;]&quot;,
  325. &quot;p n\tpower is (n) [&quot; LITERAL (PTSCTL_MODE) &quot;]&quot;,
  326. &quot;q\tquiet mode&quot;,
  327. &quot;r\tread and display attenuator settings&quot;,
  328. &quot;v\tverbose mode&quot;,
  329. &quot;w n\twait (n) millseconds [&quot; LITERAL (PTSCTL_WAIT) &quot;]&quot;,
  330. (char const *) (0)
  331. };
  332. struct _file_ port =
  333. {
  334. -1,
  335. PTSCTL_PORT
  336. };
  337. #if defined (WIN32)
  338. HANDLE hSerial;
  339. DCB dcbSerial =
  340. {
  341. 0
  342. };
  343. #else
  344. struct termios termios;
  345. #endif
  346. char const * units = PTSCTL_UNITS;
  347. unsigned wait = PTSCTL_WAIT;
  348. unsigned mode = PTSCTL_MODE;
  349. unsigned echo = PTSCTL_ECHO;
  350. unsigned line = PTSCTL_LINE_ATTN;
  351. unsigned grnd = PTSCTL_GRND_ATTN;
  352. unsigned data = 0;
  353. flag_t flags = (flag_t)(0);
  354. signed c;
  355. optind = 1;
  356. if (getenv (&quot;PTSCTL&quot;))
  357. {
  358. port.name = strdup (getenv (&quot;PTSCTL&quot;));
  359. }
  360. while ((c = getoptv (argc, argv, optv)) != -1)
  361. {
  362. switch (c)
  363. {
  364. case 'f':
  365. port.name = optarg;
  366. break;
  367. case 'g':
  368. _setbits (flags, PTSCTL_CHANGE);
  369. grnd = (unsigned)(uintspec (optarg, 0, 0x7F));
  370. break;
  371. case 'n':
  372. _setbits (flags, PTSCTL_CHANGE);
  373. line = (unsigned)(uintspec (optarg, 0, 0x7F));
  374. break;
  375. case 'p':
  376. _setbits (flags, PTSCTL_CHANGE);
  377. mode = (unsigned)(uintspec (synonym (optarg, modes, SIZEOF (modes)), 0, 1));
  378. break;
  379. case 'w':
  380. wait = (unsigned)(uintspec (optarg, 5, 100));
  381. break;
  382. case 'q':
  383. _setbits (flags, PTSCTL_SILENCE);
  384. break;
  385. case 'r':
  386. _setbits (flags, PTSCTL_DISPLAY);
  387. break;
  388. case 'v':
  389. _setbits (flags, PTSCTL_VERBOSE);
  390. break;
  391. case 'z':
  392. _setbits (flags, PTSCTL_ITERATE);
  393. break;
  394. default:
  395. break;
  396. }
  397. }
  398. argc -= optind;
  399. argv += optind;
  400. if (argc)
  401. {
  402. error (1, ENOTSUP, ERROR_TOOMANY);
  403. }
  404. #if defined (WIN32)
  405. hSerial = CreateFile (port.name, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  406. if (hSerial == INVALID_HANDLE_VALUE)
  407. {
  408. error (1, errno, FILE_CANTOPEN, port.name);
  409. }
  410. dcbSerial.DCBlength = sizeof (dcbSerial);
  411. if (!GetCommState (hSerial, &amp;dcbSerial))
  412. {
  413. error (1, 0, FILE_CANTREAD &quot; state&quot;, port.name);
  414. }
  415. dcbSerial.BaudRate = CBR_9600;
  416. dcbSerial.ByteSize = 8;
  417. dcbSerial.StopBits = ONESTOPBIT;
  418. dcbSerial.Parity = NOPARITY;
  419. if (!SetCommState (hSerial, &amp;dcbSerial))
  420. {
  421. error (1, 0, FILE_CANTSAVE &quot; state&quot;, port.name);
  422. }
  423. CloseHandle (hSerial);
  424. if ((port.file = open (port.name, O_BINARY | O_RDWR)) == -1)
  425. {
  426. error (1, errno, FILE_CANTOPEN, port.name);
  427. }
  428. #else
  429. if ((port.file = open (port.name, O_RDWR|O_NOCTTY|O_NDELAY)) == -1)
  430. {
  431. error (1, 0, FILE_CANTOPEN, port.name);
  432. }
  433. tcgetattr (port.file, &amp;termios);
  434. termios.c_cflag = CS8;
  435. cfsetospeed (&amp;termios, B9600);
  436. tcsetattr (port.file, TCSANOW, &amp;termios);
  437. #endif
  438. function1 (&amp;port, units, wait, echo);
  439. if (_anyset (flags, PTSCTL_CHANGE))
  440. {
  441. data = line &lt;&lt; 8 | grnd &lt;&lt; 1 | mode;
  442. function2 (&amp;port, units, wait, data);
  443. }
  444. if (_anyset (flags, PTSCTL_DISPLAY))
  445. {
  446. function3 (&amp;port, units, wait);
  447. }
  448. if (_anyset (flags, PTSCTL_ITERATE))
  449. {
  450. function4 (&amp;port, units, wait);
  451. }
  452. close (port.file);
  453. return (0);
  454. }
  455. </pre>
  456. <div class='footerlink'>
  457. [<a href='psread.c.html' title=' psread.c '>PREV</a>]
  458. [<a href='toolkit.html' title=' Index '>HOME</a>]
  459. [<a href='PushButton.c.html' title=' PushButton.c '>NEXT</a>]
  460. </div>
  461. </body>
  462. </html>