weeder.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. * weeder.c - Weeder Solid State Relay Module Controller;
  44. *
  45. * Contributor(s):
  46. * Charles Maier
  47. * Nathaniel Houghton
  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/error.h"
  81. /*====================================================================*
  82. * custom source files;
  83. *--------------------------------------------------------------------*/
  84. #ifndef MAKEFILE
  85. #include "../tools/getoptv.c"
  86. #include "../tools/putoptv.c"
  87. #include "../tools/version.c"
  88. #include "../tools/uintspec.c"
  89. #include "../tools/synonym.c"
  90. #include "../tools/todigit.c"
  91. #include "../tools/error.c"
  92. #endif
  93. /*====================================================================*
  94. * program constants;
  95. *--------------------------------------------------------------------*/
  96. #define WEEDER_UNITS "BA"
  97. #define WEEDER_LEDS 5
  98. #define WEEDER_BITS 7
  99. #define WEEDER_WAIT 25
  100. #define WEEDER_ECHO 0
  101. #define WEEDER_MODE 1
  102. #define WEEDER_BUFFER_LENGTH 10
  103. #define WEEDER_STRING_LENGTH 15
  104. #ifdef WIN32
  105. # define WEEDER_PORT "com1:"
  106. #else
  107. # define WEEDER_PORT "/dev/ttyS0"
  108. #endif
  109. #define WEEDER_SILENCE (1 << 0)
  110. #define WEEDER_VERBOSE (1 << 1)
  111. #define WEEDER_DISPLAY (1 << 2)
  112. #define WEEDER_NEWLINE (1 << 2)
  113. /*====================================================================*
  114. * program variables;
  115. *--------------------------------------------------------------------*/
  116. static struct _term_ const modes [] =
  117. {
  118. {
  119. "off",
  120. "0"
  121. },
  122. {
  123. "on",
  124. "1"
  125. }
  126. };
  127. static char buffer [WEEDER_BUFFER_LENGTH];
  128. static char string [WEEDER_STRING_LENGTH];
  129. static signed length = 0;
  130. static signed offset = 0;
  131. /*====================================================================*
  132. *
  133. * void function1 (struct _file_ * port, char const * units, unsigned wait, unsigned echo);
  134. *
  135. * send echo command to Weeder Solid State Relay modules B then A;
  136. * Standard Atheros relay modules were wired in reverse order for
  137. * some reason;
  138. *
  139. *--------------------------------------------------------------------*/
  140. static void function1 (struct _file_ * port, char const * units, unsigned wait, unsigned echo)
  141. {
  142. extern char buffer [];
  143. extern signed length;
  144. while (*units)
  145. {
  146. length = 0;
  147. buffer [length++] = *units++;
  148. buffer [length++] = 'X';
  149. buffer [length++] = '0' + (echo & 1);
  150. buffer [length++] = '\r';
  151. if (write (port->file, buffer, length) != length)
  152. {
  153. error (1, errno, FILE_CANTSAVE, port->name);
  154. }
  155. SLEEP (wait);
  156. }
  157. return;
  158. }
  159. /*====================================================================*
  160. *
  161. * void function2 (struct _file_ * port, char const * units, unsigned wait, unsigned mode, unsigned data);
  162. *
  163. * send write command to Weeder Solid State Relay modules B then A
  164. * because Qualcomm Atheros relay modules are wired in reverse order
  165. * for some reason;
  166. *
  167. *--------------------------------------------------------------------*/
  168. static void function2 (struct _file_ * port, char const * units, unsigned wait, unsigned mode, unsigned data)
  169. {
  170. extern char buffer [WEEDER_BUFFER_LENGTH];
  171. extern signed length;
  172. length = 0;
  173. buffer [length++] = *units++;
  174. buffer [length++] = 'W';
  175. buffer [length++] = '0' + (mode & 1);
  176. buffer [length++] = '0';
  177. buffer [length++] = '0';
  178. while (length < WEEDER_BITS)
  179. {
  180. buffer [length++] = '0' + (data & 1);
  181. data >>= 1;
  182. }
  183. buffer [length++] = '\r';
  184. if (write (port->file, buffer, length) != length)
  185. {
  186. error (1, errno, FILE_CANTSAVE, port->name);
  187. }
  188. SLEEP (wait);
  189. length = 0;
  190. buffer [length++] = *units++;
  191. buffer [length++] = 'W';
  192. while (length < WEEDER_BITS)
  193. {
  194. buffer [length++] = '0' + (data & 1);
  195. data >>= 1;
  196. }
  197. buffer [length++] = '\r';
  198. if (write (port->file, buffer, length) != length)
  199. {
  200. error (1, errno, FILE_CANTSAVE, port->name);
  201. }
  202. SLEEP (wait);
  203. return;
  204. }
  205. /*====================================================================*
  206. *
  207. * void function3 (struct _file_ * port, char const * units, unsigned wait);
  208. *
  209. * read weeder solid state controller and display settings on the
  210. * console as attenuation;
  211. *
  212. *--------------------------------------------------------------------*/
  213. static void function3 (struct _file_ * port, char const * units, unsigned wait)
  214. {
  215. extern char buffer [WEEDER_BUFFER_LENGTH];
  216. extern char string [WEEDER_STRING_LENGTH];
  217. extern signed length;
  218. extern signed offset;
  219. unsigned number = 0;
  220. memset (string, 0, sizeof (string));
  221. for (offset = 0; *units; offset += WEEDER_LEDS)
  222. {
  223. length = 0;
  224. buffer [length++] = *units++;
  225. buffer [length++] = 'R';
  226. buffer [length++] = '\r';
  227. if (write (port->file, buffer, length) != length)
  228. {
  229. error (1, errno, FILE_CANTSAVE, port->name);
  230. }
  231. SLEEP (wait);
  232. memset (buffer, 0, sizeof (buffer));
  233. if (read (port->file, buffer, WEEDER_LEDS + 2) == -1)
  234. {
  235. error (1, errno, FILE_CANTREAD, port->name);
  236. }
  237. SLEEP (wait);
  238. memcpy (&string [offset], &buffer [1], WEEDER_LEDS);
  239. }
  240. while (offset-- > 3)
  241. {
  242. number <<= 1;
  243. number |= string [offset] - '0';
  244. }
  245. printf ("%u\n", number);
  246. return;
  247. }
  248. /*====================================================================*
  249. *
  250. * int main (int argc, char const * argv []);
  251. *
  252. *--------------------------------------------------------------------*/
  253. int main (int argc, char const * argv [])
  254. {
  255. static char const * optv [] =
  256. {
  257. "e:m:o:p:iqrvw:",
  258. "",
  259. "Weeder Solid State Relay Module Controller",
  260. "e n\techo is (n) [" LITERAL (WEEDER_ECHO) "]",
  261. "m n\tmode is (n) [" LITERAL (WEEDER_MODE) "]",
  262. "o s\tunit order is (s) [" WEEDER_UNITS "]",
  263. "p f\tport is (f) [" WEEDER_PORT "]",
  264. "q\tquiet mode",
  265. "r\tread attenuator value",
  266. "v\tverbose mode",
  267. "w n\twait (n) millseconds [" LITERAL (WEEDER_WAIT) "]",
  268. (char const *) (0)
  269. };
  270. struct _file_ port =
  271. {
  272. -1,
  273. WEEDER_PORT
  274. };
  275. #if defined (WIN32)
  276. HANDLE hSerial;
  277. DCB dcbSerial =
  278. {
  279. 0
  280. };
  281. #else
  282. struct termios termios;
  283. #endif
  284. char const * units = WEEDER_UNITS;
  285. unsigned wait = WEEDER_WAIT;
  286. unsigned echo = WEEDER_ECHO;
  287. unsigned mode = WEEDER_MODE;
  288. unsigned data = 0;
  289. flag_t flags = (flag_t)(0);
  290. signed c;
  291. optind = 1;
  292. if (getenv ("WEEDER"))
  293. {
  294. port.name = strdup (getenv ("WEEDER"));
  295. }
  296. while ((c = getoptv (argc, argv, optv)) != -1)
  297. {
  298. switch (c)
  299. {
  300. case 'e':
  301. echo = (unsigned)(uintspec (synonym (optarg, modes, SIZEOF (modes)), 0, 1));
  302. break;
  303. case 'm':
  304. mode = (unsigned)(uintspec (synonym (optarg, modes, SIZEOF (modes)), 0, 1));
  305. break;
  306. case 'n':
  307. _setbits (flags, WEEDER_NEWLINE);
  308. break;
  309. case 'o':
  310. units = optarg;
  311. break;
  312. case 'p':
  313. port.name = optarg;
  314. break;
  315. case 'w':
  316. wait = (unsigned)(uintspec (optarg, 5, 100));
  317. break;
  318. case 'q':
  319. _setbits (flags, WEEDER_SILENCE);
  320. break;
  321. case 'r':
  322. _setbits (flags, WEEDER_DISPLAY);
  323. break;
  324. case 'v':
  325. _setbits (flags, WEEDER_VERBOSE);
  326. break;
  327. default:
  328. break;
  329. }
  330. }
  331. argc -= optind;
  332. argv += optind;
  333. if ((argc) && (* argv))
  334. {
  335. data = (unsigned)(uintspec (* argv, 0, 0x7F));
  336. }
  337. #if defined (WIN32)
  338. hSerial = CreateFile (port.name, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  339. if (hSerial == INVALID_HANDLE_VALUE)
  340. {
  341. error (1, errno, FILE_CANTOPEN, port.name);
  342. }
  343. dcbSerial.DCBlength = sizeof (dcbSerial);
  344. if (!GetCommState (hSerial, &dcbSerial))
  345. {
  346. error (1, 0, FILE_CANTREAD " state", port.name);
  347. }
  348. dcbSerial.BaudRate = CBR_9600;
  349. dcbSerial.ByteSize = 8;
  350. dcbSerial.StopBits = ONESTOPBIT;
  351. dcbSerial.Parity = NOPARITY;
  352. if (!SetCommState (hSerial, &dcbSerial))
  353. {
  354. error (1, 0, FILE_CANTSAVE, port.name);
  355. }
  356. CloseHandle (hSerial);
  357. if ((port.file = open (port.name, O_BINARY | O_RDWR)) == -1)
  358. {
  359. error (1, errno, FILE_CANTOPEN " state", port.name);
  360. }
  361. #else
  362. if ((port.file = open (port.name, O_RDWR|O_NOCTTY|O_NDELAY)) == -1)
  363. {
  364. error (1, 0, FILE_CANTOPEN, port.name);
  365. }
  366. tcgetattr (port.file, &termios);
  367. termios.c_cflag = CS8;
  368. cfsetospeed (&termios, B9600);
  369. tcsetattr (port.file, TCSANOW, &termios);
  370. #endif
  371. function1 (&port, units, wait, echo);
  372. if ((argc) && (* argv))
  373. {
  374. function2 (&port, units, wait, mode, data);
  375. }
  376. if (_anyset (flags, WEEDER_DISPLAY))
  377. {
  378. function3 (&port, units, wait);
  379. }
  380. close (port.file);
  381. exit (0);
  382. }