ptsctl.c 13 KB

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