Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

680 lines
23KB

  1. //
  2. // basic_serial_port.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef ASIO_BASIC_SERIAL_PORT_HPP
  12. #define ASIO_BASIC_SERIAL_PORT_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include "asio/detail/config.hpp"
  17. #if defined(ASIO_HAS_SERIAL_PORT) \
  18. || defined(GENERATING_DOCUMENTATION)
  19. #include <string>
  20. #include "asio/basic_io_object.hpp"
  21. #include "asio/detail/handler_type_requirements.hpp"
  22. #include "asio/detail/throw_error.hpp"
  23. #include "asio/error.hpp"
  24. #include "asio/serial_port_base.hpp"
  25. #include "asio/serial_port_service.hpp"
  26. #include "asio/detail/push_options.hpp"
  27. namespace asio {
  28. /// Provides serial port functionality.
  29. /**
  30. * The basic_serial_port class template provides functionality that is common
  31. * to all serial ports.
  32. *
  33. * @par Thread Safety
  34. * @e Distinct @e objects: Safe.@n
  35. * @e Shared @e objects: Unsafe.
  36. */
  37. template <typename SerialPortService = serial_port_service>
  38. class basic_serial_port
  39. : public basic_io_object<SerialPortService>,
  40. public serial_port_base
  41. {
  42. public:
  43. /// The native representation of a serial port.
  44. typedef typename SerialPortService::native_handle_type native_handle_type;
  45. /// A basic_serial_port is always the lowest layer.
  46. typedef basic_serial_port<SerialPortService> lowest_layer_type;
  47. /// Construct a basic_serial_port without opening it.
  48. /**
  49. * This constructor creates a serial port without opening it.
  50. *
  51. * @param io_context The io_context object that the serial port will use to
  52. * dispatch handlers for any asynchronous operations performed on the port.
  53. */
  54. explicit basic_serial_port(asio::io_context& io_context)
  55. : basic_io_object<SerialPortService>(io_context)
  56. {
  57. }
  58. /// Construct and open a basic_serial_port.
  59. /**
  60. * This constructor creates and opens a serial port for the specified device
  61. * name.
  62. *
  63. * @param io_context The io_context object that the serial port will use to
  64. * dispatch handlers for any asynchronous operations performed on the port.
  65. *
  66. * @param device The platform-specific device name for this serial
  67. * port.
  68. */
  69. explicit basic_serial_port(asio::io_context& io_context,
  70. const char* device)
  71. : basic_io_object<SerialPortService>(io_context)
  72. {
  73. asio::error_code ec;
  74. this->get_service().open(this->get_implementation(), device, ec);
  75. asio::detail::throw_error(ec, "open");
  76. }
  77. /// Construct and open a basic_serial_port.
  78. /**
  79. * This constructor creates and opens a serial port for the specified device
  80. * name.
  81. *
  82. * @param io_context The io_context object that the serial port will use to
  83. * dispatch handlers for any asynchronous operations performed on the port.
  84. *
  85. * @param device The platform-specific device name for this serial
  86. * port.
  87. */
  88. explicit basic_serial_port(asio::io_context& io_context,
  89. const std::string& device)
  90. : basic_io_object<SerialPortService>(io_context)
  91. {
  92. asio::error_code ec;
  93. this->get_service().open(this->get_implementation(), device, ec);
  94. asio::detail::throw_error(ec, "open");
  95. }
  96. /// Construct a basic_serial_port on an existing native serial port.
  97. /**
  98. * This constructor creates a serial port object to hold an existing native
  99. * serial port.
  100. *
  101. * @param io_context The io_context object that the serial port will use to
  102. * dispatch handlers for any asynchronous operations performed on the port.
  103. *
  104. * @param native_serial_port A native serial port.
  105. *
  106. * @throws asio::system_error Thrown on failure.
  107. */
  108. basic_serial_port(asio::io_context& io_context,
  109. const native_handle_type& native_serial_port)
  110. : basic_io_object<SerialPortService>(io_context)
  111. {
  112. asio::error_code ec;
  113. this->get_service().assign(this->get_implementation(),
  114. native_serial_port, ec);
  115. asio::detail::throw_error(ec, "assign");
  116. }
  117. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  118. /// Move-construct a basic_serial_port from another.
  119. /**
  120. * This constructor moves a serial port from one object to another.
  121. *
  122. * @param other The other basic_serial_port object from which the move will
  123. * occur.
  124. *
  125. * @note Following the move, the moved-from object is in the same state as if
  126. * constructed using the @c basic_serial_port(io_context&) constructor.
  127. */
  128. basic_serial_port(basic_serial_port&& other)
  129. : basic_io_object<SerialPortService>(
  130. ASIO_MOVE_CAST(basic_serial_port)(other))
  131. {
  132. }
  133. /// Move-assign a basic_serial_port from another.
  134. /**
  135. * This assignment operator moves a serial port from one object to another.
  136. *
  137. * @param other The other basic_serial_port object from which the move will
  138. * occur.
  139. *
  140. * @note Following the move, the moved-from object is in the same state as if
  141. * constructed using the @c basic_serial_port(io_context&) constructor.
  142. */
  143. basic_serial_port& operator=(basic_serial_port&& other)
  144. {
  145. basic_io_object<SerialPortService>::operator=(
  146. ASIO_MOVE_CAST(basic_serial_port)(other));
  147. return *this;
  148. }
  149. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  150. /// Get a reference to the lowest layer.
  151. /**
  152. * This function returns a reference to the lowest layer in a stack of
  153. * layers. Since a basic_serial_port cannot contain any further layers, it
  154. * simply returns a reference to itself.
  155. *
  156. * @return A reference to the lowest layer in the stack of layers. Ownership
  157. * is not transferred to the caller.
  158. */
  159. lowest_layer_type& lowest_layer()
  160. {
  161. return *this;
  162. }
  163. /// Get a const reference to the lowest layer.
  164. /**
  165. * This function returns a const reference to the lowest layer in a stack of
  166. * layers. Since a basic_serial_port cannot contain any further layers, it
  167. * simply returns a reference to itself.
  168. *
  169. * @return A const reference to the lowest layer in the stack of layers.
  170. * Ownership is not transferred to the caller.
  171. */
  172. const lowest_layer_type& lowest_layer() const
  173. {
  174. return *this;
  175. }
  176. /// Open the serial port using the specified device name.
  177. /**
  178. * This function opens the serial port for the specified device name.
  179. *
  180. * @param device The platform-specific device name.
  181. *
  182. * @throws asio::system_error Thrown on failure.
  183. */
  184. void open(const std::string& device)
  185. {
  186. asio::error_code ec;
  187. this->get_service().open(this->get_implementation(), device, ec);
  188. asio::detail::throw_error(ec, "open");
  189. }
  190. /// Open the serial port using the specified device name.
  191. /**
  192. * This function opens the serial port using the given platform-specific
  193. * device name.
  194. *
  195. * @param device The platform-specific device name.
  196. *
  197. * @param ec Set the indicate what error occurred, if any.
  198. */
  199. asio::error_code open(const std::string& device,
  200. asio::error_code& ec)
  201. {
  202. return this->get_service().open(this->get_implementation(), device, ec);
  203. }
  204. /// Assign an existing native serial port to the serial port.
  205. /*
  206. * This function opens the serial port to hold an existing native serial port.
  207. *
  208. * @param native_serial_port A native serial port.
  209. *
  210. * @throws asio::system_error Thrown on failure.
  211. */
  212. void assign(const native_handle_type& native_serial_port)
  213. {
  214. asio::error_code ec;
  215. this->get_service().assign(this->get_implementation(),
  216. native_serial_port, ec);
  217. asio::detail::throw_error(ec, "assign");
  218. }
  219. /// Assign an existing native serial port to the serial port.
  220. /*
  221. * This function opens the serial port to hold an existing native serial port.
  222. *
  223. * @param native_serial_port A native serial port.
  224. *
  225. * @param ec Set to indicate what error occurred, if any.
  226. */
  227. asio::error_code assign(const native_handle_type& native_serial_port,
  228. asio::error_code& ec)
  229. {
  230. return this->get_service().assign(this->get_implementation(),
  231. native_serial_port, ec);
  232. }
  233. /// Determine whether the serial port is open.
  234. bool is_open() const
  235. {
  236. return this->get_service().is_open(this->get_implementation());
  237. }
  238. /// Close the serial port.
  239. /**
  240. * This function is used to close the serial port. Any asynchronous read or
  241. * write operations will be cancelled immediately, and will complete with the
  242. * asio::error::operation_aborted error.
  243. *
  244. * @throws asio::system_error Thrown on failure.
  245. */
  246. void close()
  247. {
  248. asio::error_code ec;
  249. this->get_service().close(this->get_implementation(), ec);
  250. asio::detail::throw_error(ec, "close");
  251. }
  252. /// Close the serial port.
  253. /**
  254. * This function is used to close the serial port. Any asynchronous read or
  255. * write operations will be cancelled immediately, and will complete with the
  256. * asio::error::operation_aborted error.
  257. *
  258. * @param ec Set to indicate what error occurred, if any.
  259. */
  260. asio::error_code close(asio::error_code& ec)
  261. {
  262. return this->get_service().close(this->get_implementation(), ec);
  263. }
  264. /// Get the native serial port representation.
  265. /**
  266. * This function may be used to obtain the underlying representation of the
  267. * serial port. This is intended to allow access to native serial port
  268. * functionality that is not otherwise provided.
  269. */
  270. native_handle_type native_handle()
  271. {
  272. return this->get_service().native_handle(this->get_implementation());
  273. }
  274. /// Cancel all asynchronous operations associated with the serial port.
  275. /**
  276. * This function causes all outstanding asynchronous read or write operations
  277. * to finish immediately, and the handlers for cancelled operations will be
  278. * passed the asio::error::operation_aborted error.
  279. *
  280. * @throws asio::system_error Thrown on failure.
  281. */
  282. void cancel()
  283. {
  284. asio::error_code ec;
  285. this->get_service().cancel(this->get_implementation(), ec);
  286. asio::detail::throw_error(ec, "cancel");
  287. }
  288. /// Cancel all asynchronous operations associated with the serial port.
  289. /**
  290. * This function causes all outstanding asynchronous read or write operations
  291. * to finish immediately, and the handlers for cancelled operations will be
  292. * passed the asio::error::operation_aborted error.
  293. *
  294. * @param ec Set to indicate what error occurred, if any.
  295. */
  296. asio::error_code cancel(asio::error_code& ec)
  297. {
  298. return this->get_service().cancel(this->get_implementation(), ec);
  299. }
  300. /// Send a break sequence to the serial port.
  301. /**
  302. * This function causes a break sequence of platform-specific duration to be
  303. * sent out the serial port.
  304. *
  305. * @throws asio::system_error Thrown on failure.
  306. */
  307. void send_break()
  308. {
  309. asio::error_code ec;
  310. this->get_service().send_break(this->get_implementation(), ec);
  311. asio::detail::throw_error(ec, "send_break");
  312. }
  313. /// Send a break sequence to the serial port.
  314. /**
  315. * This function causes a break sequence of platform-specific duration to be
  316. * sent out the serial port.
  317. *
  318. * @param ec Set to indicate what error occurred, if any.
  319. */
  320. asio::error_code send_break(asio::error_code& ec)
  321. {
  322. return this->get_service().send_break(this->get_implementation(), ec);
  323. }
  324. /// Set an option on the serial port.
  325. /**
  326. * This function is used to set an option on the serial port.
  327. *
  328. * @param option The option value to be set on the serial port.
  329. *
  330. * @throws asio::system_error Thrown on failure.
  331. *
  332. * @sa SettableSerialPortOption @n
  333. * asio::serial_port_base::baud_rate @n
  334. * asio::serial_port_base::flow_control @n
  335. * asio::serial_port_base::parity @n
  336. * asio::serial_port_base::stop_bits @n
  337. * asio::serial_port_base::character_size
  338. */
  339. template <typename SettableSerialPortOption>
  340. void set_option(const SettableSerialPortOption& option)
  341. {
  342. asio::error_code ec;
  343. this->get_service().set_option(this->get_implementation(), option, ec);
  344. asio::detail::throw_error(ec, "set_option");
  345. }
  346. /// Set an option on the serial port.
  347. /**
  348. * This function is used to set an option on the serial port.
  349. *
  350. * @param option The option value to be set on the serial port.
  351. *
  352. * @param ec Set to indicate what error occurred, if any.
  353. *
  354. * @sa SettableSerialPortOption @n
  355. * asio::serial_port_base::baud_rate @n
  356. * asio::serial_port_base::flow_control @n
  357. * asio::serial_port_base::parity @n
  358. * asio::serial_port_base::stop_bits @n
  359. * asio::serial_port_base::character_size
  360. */
  361. template <typename SettableSerialPortOption>
  362. asio::error_code set_option(const SettableSerialPortOption& option,
  363. asio::error_code& ec)
  364. {
  365. return this->get_service().set_option(
  366. this->get_implementation(), option, ec);
  367. }
  368. /// Get an option from the serial port.
  369. /**
  370. * This function is used to get the current value of an option on the serial
  371. * port.
  372. *
  373. * @param option The option value to be obtained from the serial port.
  374. *
  375. * @throws asio::system_error Thrown on failure.
  376. *
  377. * @sa GettableSerialPortOption @n
  378. * asio::serial_port_base::baud_rate @n
  379. * asio::serial_port_base::flow_control @n
  380. * asio::serial_port_base::parity @n
  381. * asio::serial_port_base::stop_bits @n
  382. * asio::serial_port_base::character_size
  383. */
  384. template <typename GettableSerialPortOption>
  385. void get_option(GettableSerialPortOption& option)
  386. {
  387. asio::error_code ec;
  388. this->get_service().get_option(this->get_implementation(), option, ec);
  389. asio::detail::throw_error(ec, "get_option");
  390. }
  391. /// Get an option from the serial port.
  392. /**
  393. * This function is used to get the current value of an option on the serial
  394. * port.
  395. *
  396. * @param option The option value to be obtained from the serial port.
  397. *
  398. * @param ec Set to indicate what error occurred, if any.
  399. *
  400. * @sa GettableSerialPortOption @n
  401. * asio::serial_port_base::baud_rate @n
  402. * asio::serial_port_base::flow_control @n
  403. * asio::serial_port_base::parity @n
  404. * asio::serial_port_base::stop_bits @n
  405. * asio::serial_port_base::character_size
  406. */
  407. template <typename GettableSerialPortOption>
  408. asio::error_code get_option(GettableSerialPortOption& option,
  409. asio::error_code& ec)
  410. {
  411. return this->get_service().get_option(
  412. this->get_implementation(), option, ec);
  413. }
  414. /// Write some data to the serial port.
  415. /**
  416. * This function is used to write data to the serial port. The function call
  417. * will block until one or more bytes of the data has been written
  418. * successfully, or until an error occurs.
  419. *
  420. * @param buffers One or more data buffers to be written to the serial port.
  421. *
  422. * @returns The number of bytes written.
  423. *
  424. * @throws asio::system_error Thrown on failure. An error code of
  425. * asio::error::eof indicates that the connection was closed by the
  426. * peer.
  427. *
  428. * @note The write_some operation may not transmit all of the data to the
  429. * peer. Consider using the @ref write function if you need to ensure that
  430. * all data is written before the blocking operation completes.
  431. *
  432. * @par Example
  433. * To write a single data buffer use the @ref buffer function as follows:
  434. * @code
  435. * serial_port.write_some(asio::buffer(data, size));
  436. * @endcode
  437. * See the @ref buffer documentation for information on writing multiple
  438. * buffers in one go, and how to use it with arrays, boost::array or
  439. * std::vector.
  440. */
  441. template <typename ConstBufferSequence>
  442. std::size_t write_some(const ConstBufferSequence& buffers)
  443. {
  444. asio::error_code ec;
  445. std::size_t s = this->get_service().write_some(
  446. this->get_implementation(), buffers, ec);
  447. asio::detail::throw_error(ec, "write_some");
  448. return s;
  449. }
  450. /// Write some data to the serial port.
  451. /**
  452. * This function is used to write data to the serial port. The function call
  453. * will block until one or more bytes of the data has been written
  454. * successfully, or until an error occurs.
  455. *
  456. * @param buffers One or more data buffers to be written to the serial port.
  457. *
  458. * @param ec Set to indicate what error occurred, if any.
  459. *
  460. * @returns The number of bytes written. Returns 0 if an error occurred.
  461. *
  462. * @note The write_some operation may not transmit all of the data to the
  463. * peer. Consider using the @ref write function if you need to ensure that
  464. * all data is written before the blocking operation completes.
  465. */
  466. template <typename ConstBufferSequence>
  467. std::size_t write_some(const ConstBufferSequence& buffers,
  468. asio::error_code& ec)
  469. {
  470. return this->get_service().write_some(
  471. this->get_implementation(), buffers, ec);
  472. }
  473. /// Start an asynchronous write.
  474. /**
  475. * This function is used to asynchronously write data to the serial port.
  476. * The function call always returns immediately.
  477. *
  478. * @param buffers One or more data buffers to be written to the serial port.
  479. * Although the buffers object may be copied as necessary, ownership of the
  480. * underlying memory blocks is retained by the caller, which must guarantee
  481. * that they remain valid until the handler is called.
  482. *
  483. * @param handler The handler to be called when the write operation completes.
  484. * Copies will be made of the handler as required. The function signature of
  485. * the handler must be:
  486. * @code void handler(
  487. * const asio::error_code& error, // Result of operation.
  488. * std::size_t bytes_transferred // Number of bytes written.
  489. * ); @endcode
  490. * Regardless of whether the asynchronous operation completes immediately or
  491. * not, the handler will not be invoked from within this function. Invocation
  492. * of the handler will be performed in a manner equivalent to using
  493. * asio::io_context::post().
  494. *
  495. * @note The write operation may not transmit all of the data to the peer.
  496. * Consider using the @ref async_write function if you need to ensure that all
  497. * data is written before the asynchronous operation completes.
  498. *
  499. * @par Example
  500. * To write a single data buffer use the @ref buffer function as follows:
  501. * @code
  502. * serial_port.async_write_some(asio::buffer(data, size), handler);
  503. * @endcode
  504. * See the @ref buffer documentation for information on writing multiple
  505. * buffers in one go, and how to use it with arrays, boost::array or
  506. * std::vector.
  507. */
  508. template <typename ConstBufferSequence, typename WriteHandler>
  509. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  510. void (asio::error_code, std::size_t))
  511. async_write_some(const ConstBufferSequence& buffers,
  512. ASIO_MOVE_ARG(WriteHandler) handler)
  513. {
  514. // If you get an error on the following line it means that your handler does
  515. // not meet the documented type requirements for a WriteHandler.
  516. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  517. return this->get_service().async_write_some(this->get_implementation(),
  518. buffers, ASIO_MOVE_CAST(WriteHandler)(handler));
  519. }
  520. /// Read some data from the serial port.
  521. /**
  522. * This function is used to read data from the serial port. The function
  523. * call will block until one or more bytes of data has been read successfully,
  524. * or until an error occurs.
  525. *
  526. * @param buffers One or more buffers into which the data will be read.
  527. *
  528. * @returns The number of bytes read.
  529. *
  530. * @throws asio::system_error Thrown on failure. An error code of
  531. * asio::error::eof indicates that the connection was closed by the
  532. * peer.
  533. *
  534. * @note The read_some operation may not read all of the requested number of
  535. * bytes. Consider using the @ref read function if you need to ensure that
  536. * the requested amount of data is read before the blocking operation
  537. * completes.
  538. *
  539. * @par Example
  540. * To read into a single data buffer use the @ref buffer function as follows:
  541. * @code
  542. * serial_port.read_some(asio::buffer(data, size));
  543. * @endcode
  544. * See the @ref buffer documentation for information on reading into multiple
  545. * buffers in one go, and how to use it with arrays, boost::array or
  546. * std::vector.
  547. */
  548. template <typename MutableBufferSequence>
  549. std::size_t read_some(const MutableBufferSequence& buffers)
  550. {
  551. asio::error_code ec;
  552. std::size_t s = this->get_service().read_some(
  553. this->get_implementation(), buffers, ec);
  554. asio::detail::throw_error(ec, "read_some");
  555. return s;
  556. }
  557. /// Read some data from the serial port.
  558. /**
  559. * This function is used to read data from the serial port. The function
  560. * call will block until one or more bytes of data has been read successfully,
  561. * or until an error occurs.
  562. *
  563. * @param buffers One or more buffers into which the data will be read.
  564. *
  565. * @param ec Set to indicate what error occurred, if any.
  566. *
  567. * @returns The number of bytes read. Returns 0 if an error occurred.
  568. *
  569. * @note The read_some operation may not read all of the requested number of
  570. * bytes. Consider using the @ref read function if you need to ensure that
  571. * the requested amount of data is read before the blocking operation
  572. * completes.
  573. */
  574. template <typename MutableBufferSequence>
  575. std::size_t read_some(const MutableBufferSequence& buffers,
  576. asio::error_code& ec)
  577. {
  578. return this->get_service().read_some(
  579. this->get_implementation(), buffers, ec);
  580. }
  581. /// Start an asynchronous read.
  582. /**
  583. * This function is used to asynchronously read data from the serial port.
  584. * The function call always returns immediately.
  585. *
  586. * @param buffers One or more buffers into which the data will be read.
  587. * Although the buffers object may be copied as necessary, ownership of the
  588. * underlying memory blocks is retained by the caller, which must guarantee
  589. * that they remain valid until the handler is called.
  590. *
  591. * @param handler The handler to be called when the read operation completes.
  592. * Copies will be made of the handler as required. The function signature of
  593. * the handler must be:
  594. * @code void handler(
  595. * const asio::error_code& error, // Result of operation.
  596. * std::size_t bytes_transferred // Number of bytes read.
  597. * ); @endcode
  598. * Regardless of whether the asynchronous operation completes immediately or
  599. * not, the handler will not be invoked from within this function. Invocation
  600. * of the handler will be performed in a manner equivalent to using
  601. * asio::io_context::post().
  602. *
  603. * @note The read operation may not read all of the requested number of bytes.
  604. * Consider using the @ref async_read function if you need to ensure that the
  605. * requested amount of data is read before the asynchronous operation
  606. * completes.
  607. *
  608. * @par Example
  609. * To read into a single data buffer use the @ref buffer function as follows:
  610. * @code
  611. * serial_port.async_read_some(asio::buffer(data, size), handler);
  612. * @endcode
  613. * See the @ref buffer documentation for information on reading into multiple
  614. * buffers in one go, and how to use it with arrays, boost::array or
  615. * std::vector.
  616. */
  617. template <typename MutableBufferSequence, typename ReadHandler>
  618. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  619. void (asio::error_code, std::size_t))
  620. async_read_some(const MutableBufferSequence& buffers,
  621. ASIO_MOVE_ARG(ReadHandler) handler)
  622. {
  623. // If you get an error on the following line it means that your handler does
  624. // not meet the documented type requirements for a ReadHandler.
  625. ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  626. return this->get_service().async_read_some(this->get_implementation(),
  627. buffers, ASIO_MOVE_CAST(ReadHandler)(handler));
  628. }
  629. };
  630. } // namespace asio
  631. #include "asio/detail/pop_options.hpp"
  632. #endif // defined(ASIO_HAS_SERIAL_PORT)
  633. // || defined(GENERATING_DOCUMENTATION)
  634. #endif // ASIO_BASIC_SERIAL_PORT_HPP