2
0

glue.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. Copyright (c) 2013-2024 Bruce A Henderson
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "serial/serial.h"
  20. extern "C" {
  21. #include "blitz.h"
  22. struct STimeout {
  23. BBUINT interByteTimeout;
  24. BBUINT readTimeoutConstant;
  25. BBUINT readTimeoutMultiplier;
  26. BBUINT writeTimeoutConstant;
  27. BBUINT writeTimeoutMultiplier;
  28. };
  29. BBObject * io_serial_TSerialException__create(BBString * what);
  30. BBObject * io_serial_TIOException__create(BBString * what);
  31. BBObject * io_serial_TPortNotOpenedException__create(BBString * what);
  32. void bmx_serial_throw_serialexception(serial::SerialException &e);
  33. void bmx_serial_throw_ioexception(serial::IOException &e);
  34. void bmx_serial_throw_portnotopenexception(serial::PortNotOpenedException &e);
  35. serial::Serial * bmx_serial_create_nt(BBString * port, int baudrate, int bytesize, int parity, int stopbits, int flowcontrol, int dtrcontrol);
  36. serial::Serial * bmx_serial_create(struct STimeout * timeout, BBString * port, int baudrate, int bytesize, int parity, int stopbits, int flowcontrol, int dtrcontrol);
  37. void bmx_serial_open(serial::Serial * ser);
  38. void bmx_serial_close(serial::Serial * ser);
  39. int bmx_serial_isopen(serial::Serial * ser);
  40. int bmx_serial_available(serial::Serial * ser);
  41. int bmx_serial_read(serial::Serial * ser, uint8_t * buffer, int size);
  42. BBString * bmx_serial_readline(serial::Serial * ser, int size, BBString * eol);
  43. int bmx_serial_write(serial::Serial * ser, uint8_t * data, int size);
  44. int bmx_serial_writestring(serial::Serial * ser, BBString * data);
  45. void bmx_serial_setport(serial::Serial * ser, BBString * port);
  46. BBString * bmx_serial_getport(serial::Serial * ser);
  47. void bmx_serial_setbaudrate(serial::Serial * ser, int baudrate);
  48. int bmx_serial_getbaudrate(serial::Serial * ser);
  49. void bmx_serial_setbytesize(serial::Serial * ser, int bytesize);
  50. int bmx_serial_getbytesize(serial::Serial * ser);
  51. void bmx_serial_setparity(serial::Serial * ser, int parity);
  52. int bmx_serial_getparity(serial::Serial * ser);
  53. void bmx_serial_setstopbits(serial::Serial * ser, int stopbits);
  54. int bmx_serial_getstopbits(serial::Serial * ser);
  55. void bmx_serial_setflowcontrol(serial::Serial * ser, int flowcontrol);
  56. int bmx_serial_getflowcontrol(serial::Serial * ser);
  57. void bmx_serial_flush(serial::Serial * ser);
  58. void bmx_serial_flushinput(serial::Serial * ser);
  59. void bmx_serial_flushoutput(serial::Serial * ser);
  60. void bmx_serial_sendbreak(serial::Serial * ser, int duration);
  61. void bmx_serial_setbreak(serial::Serial * ser, int level);
  62. void bmx_serial_setrts(serial::Serial * ser, int level);
  63. void bmx_serial_setdtr(serial::Serial * ser, int dtrcontrol);
  64. void bmx_serial_waitforchange(serial::Serial * ser);
  65. int bmx_serial_getcts(serial::Serial * ser);
  66. int bmx_serial_getdsr(serial::Serial * ser);
  67. int bmx_serial_getri(serial::Serial * ser);
  68. int bmx_serial_getcd(serial::Serial * ser);
  69. BBUINT bmx_serial_timeout_max();
  70. void bmx_serial_timeout_gettimeout(serial::Serial * ser, struct STimeout * timeout);
  71. void bmx_serial_timeout_settimeout(serial::Serial * ser, BBUINT interByteTimeout, BBUINT readTimeoutConstant, BBUINT readTimeoutMultiplier, BBUINT writeTimeoutConstant,
  72. BBUINT writeTimeoutMultiplier);
  73. }
  74. // ********************************************************
  75. void bmx_serial_throw_serialexception(serial::SerialException &e) {
  76. bbExThrow(io_serial_TSerialException__create(bbStringFromUTF8String((unsigned char*)e.what())));
  77. }
  78. void bmx_serial_throw_ioexception(serial::IOException &e) {
  79. bbExThrow(io_serial_TIOException__create(bbStringFromUTF8String((unsigned char*)e.what())));
  80. }
  81. void bmx_serial_throw_portnotopenexception(serial::PortNotOpenedException &e) {
  82. bbExThrow(io_serial_TPortNotOpenedException__create(bbStringFromUTF8String((unsigned char*)e.what())));
  83. }
  84. // ********************************************************
  85. serial::Serial * bmx_serial_create_serial(serial::Timeout timeout, BBString * port, int baudrate, int bytesize, int parity, int stopbits, int flowcontrol, int dtrcontrol) {
  86. serial::Serial * ser = 0;
  87. try {
  88. if (port == &bbEmptyString) {
  89. ser = new serial::Serial("", baudrate, timeout, static_cast<serial::bytesize_t>(bytesize),
  90. static_cast<serial::parity_t>(parity), static_cast<serial::stopbits_t>(stopbits), static_cast<serial::flowcontrol_t>(flowcontrol),
  91. static_cast<serial::dtrcontrol_t>(dtrcontrol));
  92. } else {
  93. char * s = (char*)bbStringToUTF8String(port);
  94. ser = new serial::Serial(s, baudrate, timeout, static_cast<serial::bytesize_t>(bytesize),
  95. static_cast<serial::parity_t>(parity), static_cast<serial::stopbits_t>(stopbits), static_cast<serial::flowcontrol_t>(flowcontrol),
  96. static_cast<serial::dtrcontrol_t>(dtrcontrol));
  97. bbMemFree(s);
  98. }
  99. } catch (serial::PortNotOpenedException &e) {
  100. bmx_serial_throw_portnotopenexception(e);
  101. } catch (std::exception & e) {
  102. // note : not a real serial exception - catch-all.
  103. bbExThrow(io_serial_TSerialException__create(bbStringFromUTF8String((unsigned char*)e.what())));
  104. }
  105. return ser;
  106. }
  107. serial::Serial * bmx_serial_create_nt(BBString * port, int baudrate, int bytesize, int parity, int stopbits, int flowcontrol, int dtrcontrol) {
  108. serial::Timeout t;
  109. return bmx_serial_create_serial(t, port, baudrate, bytesize, parity, stopbits, flowcontrol, dtrcontrol);
  110. }
  111. serial::Serial * bmx_serial_create(struct STimeout * timeout, BBString * port, int baudrate, int bytesize, int parity, int stopbits, int flowcontrol, int dtrcontrol) {
  112. serial::Timeout t(timeout->interByteTimeout, timeout->readTimeoutConstant, timeout->readTimeoutMultiplier, timeout->writeTimeoutConstant, timeout->writeTimeoutMultiplier);
  113. return bmx_serial_create_serial(t, port, baudrate, bytesize, parity, stopbits, flowcontrol, dtrcontrol);
  114. }
  115. void bmx_serial_open(serial::Serial * ser) {
  116. try {
  117. ser->open();
  118. } catch (serial::IOException &e) {
  119. bmx_serial_throw_ioexception(e);
  120. } catch (serial::SerialException &e) {
  121. bmx_serial_throw_serialexception(e);
  122. }
  123. }
  124. void bmx_serial_close(serial::Serial * ser) {
  125. ser->close();
  126. }
  127. int bmx_serial_isopen(serial::Serial * ser) {
  128. return static_cast<int>(ser->isOpen());
  129. }
  130. int bmx_serial_available(serial::Serial * ser) {
  131. int ret = 0;
  132. try {
  133. ret = static_cast<int>(ser->available());
  134. } catch (serial::IOException &e) {
  135. bmx_serial_throw_ioexception(e);
  136. } catch (serial::SerialException &e) {
  137. bmx_serial_throw_serialexception(e);
  138. }
  139. return ret;
  140. }
  141. int bmx_serial_read(serial::Serial * ser, uint8_t * buffer, int size) {
  142. try {
  143. return static_cast<int>(ser->read(buffer, size));
  144. } catch (serial::PortNotOpenedException &e) {
  145. bmx_serial_throw_portnotopenexception(e);
  146. } catch (serial::IOException &e) {
  147. bmx_serial_throw_ioexception(e);
  148. } catch (serial::SerialException &e) {
  149. bmx_serial_throw_serialexception(e);
  150. }
  151. }
  152. BBString * bmx_serial_readline(serial::Serial * ser, int size, BBString * eol) {
  153. char * s = (char*)bbStringToUTF8String(eol);
  154. BBString * ret = 0;
  155. try {
  156. ret = bbStringFromUTF8String((unsigned char*)ser->readline(size, s).c_str());
  157. bbMemFree(s);
  158. } catch (serial::PortNotOpenedException &e) {
  159. bbMemFree(s);
  160. bmx_serial_throw_portnotopenexception(e);
  161. } catch (serial::IOException &e) {
  162. bbMemFree(s);
  163. bmx_serial_throw_ioexception(e);
  164. } catch (serial::SerialException &e) {
  165. bbMemFree(s);
  166. bmx_serial_throw_serialexception(e);
  167. }
  168. return ret;
  169. }
  170. int bmx_serial_write(serial::Serial * ser, uint8_t * data, int size) {
  171. int ret = 0;
  172. try {
  173. ret = static_cast<int>(ser->write(data, size));
  174. } catch (serial::PortNotOpenedException &e) {
  175. bmx_serial_throw_portnotopenexception(e);
  176. } catch (serial::IOException &e) {
  177. bmx_serial_throw_ioexception(e);
  178. } catch (serial::SerialException &e) {
  179. bmx_serial_throw_serialexception(e);
  180. }
  181. return ret;
  182. }
  183. int bmx_serial_writestring(serial::Serial * ser, BBString * data) {
  184. char * s = (char*)bbStringToUTF8String(data);
  185. int ret = 0;
  186. try {
  187. ret = static_cast<int>(ser->write(s));
  188. bbMemFree(s);
  189. } catch (serial::PortNotOpenedException &e) {
  190. bbMemFree(s);
  191. bmx_serial_throw_portnotopenexception(e);
  192. } catch (serial::IOException &e) {
  193. bbMemFree(s);
  194. bmx_serial_throw_ioexception(e);
  195. } catch (serial::SerialException &e) {
  196. bbMemFree(s);
  197. bmx_serial_throw_serialexception(e);
  198. }
  199. return ret;
  200. }
  201. void bmx_serial_setport(serial::Serial * ser, BBString * port) {
  202. char * s = (char*)bbStringToUTF8String(port);
  203. try {
  204. ser->setPort(s);
  205. bbMemFree(s);
  206. } catch (serial::IOException &e) {
  207. bbMemFree(s);
  208. bmx_serial_throw_ioexception(e);
  209. } catch (serial::SerialException &e) {
  210. bbMemFree(s);
  211. bmx_serial_throw_serialexception(e);
  212. }
  213. }
  214. BBString * bmx_serial_getport(serial::Serial * ser) {
  215. return bbStringFromUTF8String((unsigned char*)ser->getPort().c_str());
  216. }
  217. void bmx_serial_setbaudrate(serial::Serial * ser, int baudrate) {
  218. try {
  219. ser->setBaudrate(baudrate);
  220. } catch (serial::IOException &e) {
  221. bmx_serial_throw_ioexception(e);
  222. } catch (serial::SerialException &e) {
  223. bmx_serial_throw_serialexception(e);
  224. }
  225. }
  226. int bmx_serial_getbaudrate(serial::Serial * ser) {
  227. return ser->getBaudrate();
  228. }
  229. void bmx_serial_setbytesize(serial::Serial * ser, int bytesize) {
  230. try {
  231. ser->setBytesize(static_cast<serial::bytesize_t>(bytesize));
  232. } catch (serial::IOException &e) {
  233. bmx_serial_throw_ioexception(e);
  234. } catch (serial::SerialException &e) {
  235. bmx_serial_throw_serialexception(e);
  236. }
  237. }
  238. int bmx_serial_getbytesize(serial::Serial * ser) {
  239. return static_cast<int>(ser->getBytesize());
  240. }
  241. void bmx_serial_setparity(serial::Serial * ser, int parity) {
  242. try {
  243. ser->setParity(static_cast<serial::parity_t>(parity));
  244. } catch (serial::IOException &e) {
  245. bmx_serial_throw_ioexception(e);
  246. } catch (serial::SerialException &e) {
  247. bmx_serial_throw_serialexception(e);
  248. }
  249. }
  250. int bmx_serial_getparity(serial::Serial * ser) {
  251. return static_cast<int>(ser->getParity());
  252. }
  253. void bmx_serial_setstopbits(serial::Serial * ser, int stopbits) {
  254. try {
  255. ser->setStopbits(static_cast<serial::stopbits_t>(stopbits));
  256. } catch (serial::IOException &e) {
  257. bmx_serial_throw_ioexception(e);
  258. } catch (serial::SerialException &e) {
  259. bmx_serial_throw_serialexception(e);
  260. }
  261. }
  262. int bmx_serial_getstopbits(serial::Serial * ser) {
  263. return static_cast<int>(ser->getStopbits());
  264. }
  265. void bmx_serial_setflowcontrol(serial::Serial * ser, int flowcontrol) {
  266. try {
  267. ser->setFlowcontrol(static_cast<serial::flowcontrol_t>(flowcontrol));
  268. } catch (serial::IOException &e) {
  269. bmx_serial_throw_ioexception(e);
  270. } catch (serial::SerialException &e) {
  271. bmx_serial_throw_serialexception(e);
  272. }
  273. }
  274. int bmx_serial_getflowcontrol(serial::Serial * ser) {
  275. return static_cast<int>(ser->getFlowcontrol());
  276. }
  277. void bmx_serial_flush(serial::Serial * ser) {
  278. try {
  279. ser->flush();
  280. } catch (serial::PortNotOpenedException &e) {
  281. bmx_serial_throw_portnotopenexception(e);
  282. } catch (serial::IOException &e) {
  283. bmx_serial_throw_ioexception(e);
  284. } catch (serial::SerialException &e) {
  285. bmx_serial_throw_serialexception(e);
  286. }
  287. }
  288. void bmx_serial_flushinput(serial::Serial * ser) {
  289. try {
  290. ser->flushInput();
  291. } catch (serial::IOException &e) {
  292. bmx_serial_throw_ioexception(e);
  293. } catch (serial::SerialException &e) {
  294. bmx_serial_throw_serialexception(e);
  295. }
  296. }
  297. void bmx_serial_flushoutput(serial::Serial * ser) {
  298. try {
  299. ser->flushOutput();
  300. } catch (serial::IOException &e) {
  301. bmx_serial_throw_ioexception(e);
  302. } catch (serial::SerialException &e) {
  303. bmx_serial_throw_serialexception(e);
  304. }
  305. }
  306. void bmx_serial_sendbreak(serial::Serial * ser, int duration) {
  307. try {
  308. ser->sendBreak(duration);
  309. } catch (serial::IOException &e) {
  310. bmx_serial_throw_ioexception(e);
  311. } catch (serial::SerialException &e) {
  312. bmx_serial_throw_serialexception(e);
  313. }
  314. }
  315. void bmx_serial_setbreak(serial::Serial * ser, int level) {
  316. try {
  317. ser->setBreak(static_cast<bool>(level));
  318. } catch (serial::PortNotOpenedException &e) {
  319. bmx_serial_throw_portnotopenexception(e);
  320. } catch (serial::SerialException &e) {
  321. bmx_serial_throw_serialexception(e);
  322. }
  323. }
  324. void bmx_serial_setrts(serial::Serial * ser, int level) {
  325. try {
  326. ser->setRTS(static_cast<bool>(level));
  327. } catch (serial::PortNotOpenedException &e) {
  328. bmx_serial_throw_portnotopenexception(e);
  329. } catch (serial::SerialException &e) {
  330. bmx_serial_throw_serialexception(e);
  331. }
  332. }
  333. void bmx_serial_setdtr(serial::Serial * ser, int dtrcontrol) {
  334. try {
  335. ser->setDTR(static_cast<serial::dtrcontrol_t>(dtrcontrol));
  336. } catch (serial::PortNotOpenedException &e) {
  337. bmx_serial_throw_portnotopenexception(e);
  338. } catch (serial::SerialException &e) {
  339. bmx_serial_throw_serialexception(e);
  340. }
  341. }
  342. void bmx_serial_waitforchange(serial::Serial * ser) {
  343. try {
  344. ser->waitForChange();
  345. } catch (serial::PortNotOpenedException &e) {
  346. bmx_serial_throw_portnotopenexception(e);
  347. } catch (serial::SerialException &e) {
  348. bmx_serial_throw_serialexception(e);
  349. }
  350. }
  351. int bmx_serial_getcts(serial::Serial * ser) {
  352. try {
  353. return static_cast<int>(ser->getCTS());
  354. } catch (serial::PortNotOpenedException &e) {
  355. bmx_serial_throw_portnotopenexception(e);
  356. } catch (serial::IOException &e) {
  357. bmx_serial_throw_ioexception(e);
  358. } catch (serial::SerialException &e) {
  359. bmx_serial_throw_serialexception(e);
  360. }
  361. }
  362. int bmx_serial_getdsr(serial::Serial * ser) {
  363. try {
  364. return static_cast<int>(ser->getDSR());
  365. } catch (serial::PortNotOpenedException &e) {
  366. bmx_serial_throw_portnotopenexception(e);
  367. } catch (serial::IOException &e) {
  368. bmx_serial_throw_ioexception(e);
  369. } catch (serial::SerialException &e) {
  370. bmx_serial_throw_serialexception(e);
  371. }
  372. }
  373. int bmx_serial_getri(serial::Serial * ser) {
  374. try {
  375. return static_cast<int>(ser->getRI());
  376. } catch (serial::PortNotOpenedException &e) {
  377. bmx_serial_throw_portnotopenexception(e);
  378. } catch (serial::IOException &e) {
  379. bmx_serial_throw_ioexception(e);
  380. } catch (serial::SerialException &e) {
  381. bmx_serial_throw_serialexception(e);
  382. }
  383. }
  384. int bmx_serial_getcd(serial::Serial * ser) {
  385. try {
  386. return static_cast<int>(ser->getCD());
  387. } catch (serial::PortNotOpenedException &e) {
  388. bmx_serial_throw_portnotopenexception(e);
  389. } catch (serial::IOException &e) {
  390. bmx_serial_throw_ioexception(e);
  391. } catch (serial::SerialException &e) {
  392. bmx_serial_throw_serialexception(e);
  393. }
  394. }
  395. // ********************************************************
  396. BBUINT bmx_serial_timeout_max() {
  397. return serial::Timeout::max();
  398. }
  399. void bmx_serial_timeout_gettimeout(serial::Serial * ser, struct STimeout * timeout) {
  400. serial::Timeout t = ser->getTimeout();
  401. timeout->interByteTimeout = t.inter_byte_timeout;
  402. timeout->readTimeoutConstant = t.read_timeout_constant;
  403. timeout->readTimeoutMultiplier = t.read_timeout_multiplier;
  404. timeout->writeTimeoutConstant = t.write_timeout_constant;
  405. timeout->writeTimeoutMultiplier = t.write_timeout_multiplier;
  406. }
  407. void bmx_serial_timeout_settimeout(serial::Serial * ser, BBUINT interByteTimeout, BBUINT readTimeoutConstant, BBUINT readTimeoutMultiplier, BBUINT writeTimeoutConstant,
  408. BBUINT writeTimeoutMultiplier) {
  409. serial::Timeout t(interByteTimeout, readTimeoutConstant, readTimeoutMultiplier, writeTimeoutConstant, writeTimeoutMultiplier);
  410. ser->setTimeout(t);
  411. }