diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..3dc9718 --- /dev/null +++ b/.clang-format @@ -0,0 +1,30 @@ +--- +Language: Cpp +BasedOnStyle: google +IndentWidth: 2 +BraceWrapping: + AfterClass: true + AfterControlStatement: true + AfterEnum: true + AfterFunction: true + AfterNamespace: true + AfterObjCDeclaration: true + AfterStruct: true + AfterUnion: true + BeforeCatch: true + BeforeElse: true + IndentBraces: false +BreakBeforeBraces: Custom +ColumnLimit: 120 +AlignTrailingComments: true +PointerAlignment: Right +UseTab: Never +AllowShortIfStatementsOnASingleLine: false +AccessModifierOffset: -2 +IndentCaseLabels: false +AlignAfterOpenBracket: Align +BinPackArguments: true +BinPackParameters: false +ExperimentalAutoDetectBinPacking: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortFunctionsOnASingleLine: Inline \ No newline at end of file diff --git a/examples/serial_loopback.cpp b/examples/serial_loopback.cpp index 459a6e6..3b7d727 100644 --- a/examples/serial_loopback.cpp +++ b/examples/serial_loopback.cpp @@ -20,22 +20,24 @@ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ /** * @file serial_loopback.cpp * @author Daniel Koch * - * This example is designed for use with a USB-to-UART adapter with the RX and TX pins connected together (loopback). - * Sends a series of bytes out and prints them to the console as they are received back. + * This example is designed for use with a USB-to-UART adapter with the RX and + * TX pins connected together (loopback). Sends a series of bytes out and prints + * them to the console as they are received back. */ #include @@ -48,7 +50,6 @@ #define NUM_BYTES 64 - /** * @brief Callback function for the async_comm library * @@ -65,7 +66,6 @@ void callback(const uint8_t* buf, size_t len) } } - int main(int argc, char** argv) { // initialize diff --git a/examples/serial_protocol.cpp b/examples/serial_protocol.cpp index d215648..629b653 100644 --- a/examples/serial_protocol.cpp +++ b/examples/serial_protocol.cpp @@ -20,35 +20,38 @@ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ /** * @file serial_protocol.cpp * @author Daniel Koch * - * This example implements a simple serial protocol, and tests the async_comm library using that protocol on a serial - * loopback (USB-to-UART converter with the RX and TX pins connected together). + * This example implements a simple serial protocol, and tests the async_comm + * library using that protocol on a serial loopback (USB-to-UART converter with + * the RX and TX pins connected together). * * The message defined by the serial protocol has the following format: * - * | Field | Type | Size (bytes) | Description | + * | Field | Type | Size (bytes) | Description | * |------------|----------|--------------|------------------------------------------------------| - * | Start Byte | | 1 | Identifies the beginning of a message, value is 0xA5 | - * | `id` | uint32_t | 4 | Sequential message ID | - * | `v1` | uint32_t | 4 | The first data field | - * | `v2` | uint32_t | 4 | The second data field | - * | CRC | uint8_t | 1 | Cyclic redundancy check (CRC) byte | + * | Start Byte | | 1 | Identifies the beginning of a + * message, value is 0xA5 | | `id` | uint32_t | 4 | Sequential + * message ID | | `v1` | uint32_t | 4 | The + * first data field | | `v2` | uint32_t | + * 4 | The second data field | | CRC + * | uint8_t | 1 | Cyclic redundancy check (CRC) byte | * - * The "payload" of the message is the part that contains the actual data, and consists of the `id`, `v1`, and `v2` - * fields. + * The "payload" of the message is the part that contains the actual data, and + * consists of the `id`, `v1`, and `v2` fields. * * The parser is implemented as a finite state machine. */ @@ -78,28 +81,29 @@ #define NUM_START_BITS 1 #define NUM_STOP_BITS 1 - /** * @brief Recursively update the cyclic redundancy check (CRC) * * This uses the CRC-8-CCITT polynomial. * - * Source: http://www.nongnu.org/avr-libc/user-manual/group__util__crc.html#gab27eaaef6d7fd096bd7d57bf3f9ba083 + * Source: + * http://www.nongnu.org/avr-libc/user-manual/group__util__crc.html#gab27eaaef6d7fd096bd7d57bf3f9ba083 * - * @param inCrc The current CRC value. This should be initialized to 0 before processing first byte. + * @param inCrc The current CRC value. This should be initialized to 0 before + * processing first byte. * @param inData The byte being processed * @return The new CRC value */ uint8_t update_crc(uint8_t inCrc, uint8_t inData) { - uint8_t i; - uint8_t data; + uint8_t i; + uint8_t data; data = inCrc ^ inData; - for ( i = 0; i < 8; i++ ) + for (i = 0; i < 8; i++) { - if (( data & 0x80 ) != 0 ) + if ((data & 0x80) != 0) { data <<= 1; data ^= 0x07; @@ -112,7 +116,6 @@ uint8_t update_crc(uint8_t inCrc, uint8_t inData) return data; } - /** * @brief Pack message contents into a buffer * @param[out] dst Buffer in which to store the message @@ -120,24 +123,24 @@ uint8_t update_crc(uint8_t inCrc, uint8_t inData) * @param[in] v1 First data field of the message * @param[in] v2 Second data field of the message * - * @post The specified buffer contains the complete message packet, including start byte and CRC byte + * @post The specified buffer contains the complete message packet, including + * start byte and CRC byte */ void pack_message(uint8_t* dst, uint32_t id, uint32_t v1, uint32_t v2) { dst[0] = START_BYTE; - memcpy(dst+1, &id, 4); - memcpy(dst+5, &v1, 4); - memcpy(dst+9, &v2, 4); + memcpy(dst + 1, &id, 4); + memcpy(dst + 5, &v1, 4); + memcpy(dst + 9, &v2, 4); uint8_t crc = 0; - for (size_t i = 0; i < PACKET_LEN-1; i++) + for (size_t i = 0; i < PACKET_LEN - 1; i++) { crc = update_crc(crc, dst[i]); } - dst[PACKET_LEN-1] = crc; + dst[PACKET_LEN - 1] = crc; } - /** * @brief Unpack the contents of a message payload buffer * @param[in] src The buffer to unpack @@ -148,14 +151,13 @@ void pack_message(uint8_t* dst, uint32_t id, uint32_t v1, uint32_t v2) * @pre Buffer contains a valid message payload * @post Payload contents have been placed into the specified variables */ -void unpack_payload(uint8_t* src, uint32_t *id, uint32_t *v1, uint32_t *v2) +void unpack_payload(uint8_t* src, uint32_t* id, uint32_t* v1, uint32_t* v2) { memcpy(id, src, 4); - memcpy(v1, src+4, 4); - memcpy(v2, src+8, 4); + memcpy(v1, src + 4, 4); + memcpy(v2, src + 8, 4); } - /** * @brief States for the parser state machine */ @@ -166,16 +168,17 @@ enum ParseState PARSE_STATE_GOT_PAYLOAD }; -ParseState parse_state = PARSE_STATE_IDLE; //!< Current state of the parser state machine -uint8_t receive_buffer[PAYLOAD_LEN]; //!< Buffer for accumulating received payload +ParseState parse_state = PARSE_STATE_IDLE; //!< Current state of the parser state machine +uint8_t receive_buffer[PAYLOAD_LEN]; //!< Buffer for accumulating received payload -volatile int receive_count = 0; //!< Keeps track of how many valid messages have been received -bool received[NUM_MSGS]; //!< Keeps track of which messages we've received back - -std::mutex mutex; //!< mutex for synchronization between the main thread and callback thread -std::condition_variable condition_variable; //!< condition variable used to suspend main thread until all messages have been received back -volatile bool all_messages_received = false; //!< flag for whether all messages have been received back +volatile int receive_count = 0; //!< Keeps track of how many valid messages have been received +bool received[NUM_MSGS]; //!< Keeps track of which messages we've received back +std::mutex mutex; //!< mutex for synchronization between the main thread and + //!< callback thread +std::condition_variable condition_variable; //!< condition variable used to suspend main thread + //!< until all messages have been received back +volatile bool all_messages_received = false; //!< flag for whether all messages have been received back /** * @brief Passes a received byte through the parser state machine @@ -223,13 +226,12 @@ void parse_byte(uint8_t byte) } condition_variable.notify_one(); } - } // otherwise ignore it + } // otherwise ignore it parse_state = PARSE_STATE_IDLE; break; } } - /** * @brief Callback function for the async_comm library * @@ -246,7 +248,6 @@ void callback(const uint8_t* buf, size_t len) } } - int main(int argc, char** argv) { // initialize @@ -281,7 +282,7 @@ int main(int argc, char** argv) uint8_t buffer[PACKET_LEN]; for (uint32_t i = 0; i < NUM_MSGS; i++) { - pack_message(buffer, i, i*2, i*4); + pack_message(buffer, i, i * 2, i * 4); serial.send_bytes(buffer, PACKET_LEN); } auto finish_write = std::chrono::high_resolution_clock::now(); @@ -289,7 +290,7 @@ int main(int argc, char** argv) // wait to receive all messages { std::unique_lock lock(mutex); - condition_variable.wait(lock, []{ return all_messages_received; }); + condition_variable.wait(lock, [] { return all_messages_received; }); } auto finish_read = std::chrono::high_resolution_clock::now(); @@ -320,8 +321,8 @@ int main(int argc, char** argv) std::printf("Elapsed read time: %fms\n", read_time.count()); int num_bytes = NUM_MSGS * PACKET_LEN; - double expected_time = num_bytes * (8 + NUM_START_BITS + NUM_STOP_BITS) / (double) BAUD_RATE; - std::printf("Expected read time: %fms\n", expected_time*1e3); + double expected_time = num_bytes * (8 + NUM_START_BITS + NUM_STOP_BITS) / (double)BAUD_RATE; + std::printf("Expected read time: %fms\n", expected_time * 1e3); std::printf("Total: %d bytes\n", num_bytes); return 0; diff --git a/examples/tcp_client_hello_world.cpp b/examples/tcp_client_hello_world.cpp index df63874..6595e9f 100644 --- a/examples/tcp_client_hello_world.cpp +++ b/examples/tcp_client_hello_world.cpp @@ -20,14 +20,15 @@ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ /** @@ -47,7 +48,6 @@ #include #include - /** * @brief Callback function for the async_comm library * @@ -64,7 +64,6 @@ void callback(const uint8_t* buf, size_t len) } } - int main() { // open TCP connection @@ -81,7 +80,7 @@ int main() for (size_t i = 0; i < 10; ++i) { std::string msg = "hello world " + std::to_string(i) + "!"; - tcp_client.send_bytes((uint8_t*) msg.data(), msg.size()); + tcp_client.send_bytes((uint8_t*)msg.data(), msg.size()); std::this_thread::sleep_for(std::chrono::milliseconds(500)); } diff --git a/examples/udp_hello_world.cpp b/examples/udp_hello_world.cpp index 250a50a..531a72c 100644 --- a/examples/udp_hello_world.cpp +++ b/examples/udp_hello_world.cpp @@ -20,22 +20,23 @@ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ /** * @file udp_hello_world.cpp * @author Daniel Koch * - * This example opens two UDP objects listening on different ports on the local host, and then uses each to send a - * simple "hello world" message to the other. + * This example opens two UDP objects listening on different ports on the local + * host, and then uses each to send a simple "hello world" message to the other. */ #include @@ -48,7 +49,6 @@ #include #include - /** * @brief Callback function for the async_comm library * @@ -65,7 +65,6 @@ void callback(const uint8_t* buf, size_t len) } } - int main() { // open UDP ports @@ -83,7 +82,7 @@ int main() // send message one direction char message1[] = "hello world 1!"; - udp2.send_bytes((uint8_t*) message1, std::strlen(message1)); + udp2.send_bytes((uint8_t*)message1, std::strlen(message1)); // wait for all bytes to be received std::this_thread::sleep_for(std::chrono::milliseconds(500)); @@ -91,7 +90,7 @@ int main() // send message the other direction char message2[] = "hello world 2!"; - udp1.send_bytes((uint8_t*) message2, std::strlen(message2)); + udp1.send_bytes((uint8_t*)message2, std::strlen(message2)); // wait for all bytes to be received std::this_thread::sleep_for(std::chrono::milliseconds(500)); diff --git a/include/async_comm/comm.h b/include/async_comm/comm.h index 5ff6b72..b71aabe 100644 --- a/include/async_comm/comm.h +++ b/include/async_comm/comm.h @@ -20,14 +20,15 @@ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ /** @@ -53,7 +54,6 @@ namespace async_comm { - /** * @class Comm * @brief Abstract base class for an asynchronous communication port @@ -63,9 +63,10 @@ class Comm public: /** * @brief Set up asynchronous communication base class - * @param message_handler Custom message handler, or omit for default handler + * @param message_handler Custom message handler, or omit for default + * handler */ - Comm(MessageHandler& message_handler = default_message_handler_); + Comm(MessageHandler &message_handler = default_message_handler_); virtual ~Comm(); /** @@ -84,7 +85,7 @@ class Comm * @param src Address of the buffer * @param len Number of bytes to send */ - void send_bytes(const uint8_t * src, size_t len); + void send_bytes(const uint8_t *src, size_t len); /** * @brief Send a single byte over the port @@ -93,33 +94,50 @@ class Comm inline void send_byte(uint8_t data) { send_bytes(&data, 1); } /** - * @brief Register a callback function for when bytes are received on the port + * @brief Register a callback function for when bytes are received on the + * port * - * The callback function needs to accept two parameters. The first is of type `const uint8_t*`, and is a constant - * pointer to the data buffer. The second is of type `size_t`, and specifies the number of bytes available in the - * buffer. + * The callback function needs to accept two parameters. The first is of + * type `const uint8_t*`, and is a constant pointer to the data buffer. The + * second is of type `size_t`, and specifies the number of bytes available + * in the buffer. * - * @warning The data buffer passed to the callback function will be invalid after the callback function exits. If you - * want to store the data for later processing, you must copy the data to a new buffer rather than storing the - * pointer to the buffer. + * @warning The data buffer passed to the callback function will be invalid + * after the callback function exits. If you want to store the data for + * later processing, you must copy the data to a new buffer rather than + * storing the pointer to the buffer. * * @param fun Function to call when bytes are received */ - void register_receive_callback(std::function fun); + void register_receive_callback(std::function fun); protected: - static constexpr size_t READ_BUFFER_SIZE = 1024; static constexpr size_t WRITE_BUFFER_SIZE = 1024; class DefaultMessageHandler : public MessageHandler { public: - inline void debug(const std::string &message) override { std::cout << "[async_comm][DEBUG]: " << message << std::endl; } - inline void info(const std::string &message) override { std::cout << "[async_comm][INFO]: " << message << std::endl; } - inline void warn(const std::string &message) override { std::cerr << "[async_comm][WARN]: " << message << std::endl; } - inline void error(const std::string &message) override { std::cerr << "[async_comm][ERROR]: " << message << std::endl; } - inline void fatal(const std::string &message) override { std::cerr << "[async_comm][FATAL]: " << message << std::endl; } + inline void debug(const std::string &message) override + { + std::cout << "[async_comm][DEBUG]: " << message << std::endl; + } + inline void info(const std::string &message) override + { + std::cout << "[async_comm][INFO]: " << message << std::endl; + } + inline void warn(const std::string &message) override + { + std::cerr << "[async_comm][WARN]: " << message << std::endl; + } + inline void error(const std::string &message) override + { + std::cerr << "[async_comm][ERROR]: " << message << std::endl; + } + inline void fatal(const std::string &message) override + { + std::cerr << "[async_comm][FATAL]: " << message << std::endl; + } }; static DefaultMessageHandler default_message_handler_; @@ -127,23 +145,22 @@ class Comm virtual bool do_init() = 0; virtual void do_close() = 0; virtual void do_async_read(const boost::asio::mutable_buffers_1 &buffer, - boost::function handler) = 0; + boost::function handler); virtual void do_async_write(const boost::asio::const_buffers_1 &buffer, - boost::function handler) = 0; + boost::function handler); - MessageHandler& message_handler_; + MessageHandler &message_handler_; boost::asio::io_service io_service_; private: - struct ReadBuffer { uint8_t data[READ_BUFFER_SIZE]; size_t len; - ReadBuffer(const uint8_t * buf, size_t len) : len(len) + ReadBuffer(const uint8_t *buf, size_t len) : len(len) { - assert(len <= READ_BUFFER_SIZE); // only checks in debug mode + assert(len <= READ_BUFFER_SIZE); // only checks in debug mode memcpy(data, buf, len); } }; @@ -156,13 +173,13 @@ class Comm WriteBuffer() : len(0), pos(0) {} - WriteBuffer(const uint8_t * buf, size_t len) : len(len), pos(0) + WriteBuffer(const uint8_t *buf, size_t len) : len(len), pos(0) { - assert(len <= WRITE_BUFFER_SIZE); // only checks in debug mode + assert(len <= WRITE_BUFFER_SIZE); // only checks in debug mode memcpy(data, buf, len); } - const uint8_t * dpos() const { return data + pos; } + const uint8_t *dpos() const { return data + pos; } size_t nbytes() const { return len - pos; } }; @@ -170,10 +187,10 @@ class Comm typedef std::lock_guard mutex_lock; void async_read(); - void async_read_end(const boost::system::error_code& error, size_t bytes_transferred); + void async_read_end(const boost::system::error_code &error, size_t bytes_transferred); void async_write(bool check_write_state); - void async_write_end(const boost::system::error_code& error, size_t bytes_transferred); + void async_write_end(const boost::system::error_code &error, size_t bytes_transferred); void process_callbacks(); @@ -191,9 +208,9 @@ class Comm std::recursive_mutex write_mutex_; bool write_in_progress_; - std::function receive_callback_; + std::function receive_callback_; }; -} // namespace async_comm +} // namespace async_comm -#endif // ASYNC_COMM_COMM_H +#endif // ASYNC_COMM_COMM_H diff --git a/include/async_comm/message_handler.h b/include/async_comm/message_handler.h index 8fcff2d..d1d29b3 100644 --- a/include/async_comm/message_handler.h +++ b/include/async_comm/message_handler.h @@ -20,14 +20,15 @@ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ /** @@ -43,7 +44,6 @@ namespace async_comm { - /** * @class MessageHandler * @brief Abstract base class for message handler @@ -56,12 +56,12 @@ class MessageHandler { public: virtual void debug(const std::string& message) = 0; - virtual void info(const std::string& message) = 0; - virtual void warn(const std::string& message) = 0; + virtual void info(const std::string& message) = 0; + virtual void warn(const std::string& message) = 0; virtual void error(const std::string& message) = 0; virtual void fatal(const std::string& message) = 0; }; -} // namespace async_comm +} // namespace async_comm -#endif // ASYNC_COMM_MESSAGE_HANDLER_H \ No newline at end of file +#endif // ASYNC_COMM_MESSAGE_HANDLER_H \ No newline at end of file diff --git a/include/async_comm/serial.h b/include/async_comm/serial.h index 7ce90da..192941e 100644 --- a/include/async_comm/serial.h +++ b/include/async_comm/serial.h @@ -20,14 +20,15 @@ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ /** @@ -43,12 +44,11 @@ #include #include -#include #include +#include namespace async_comm { - /** * @class Serial * @brief Asynchronous communication class for a serial port @@ -60,13 +60,13 @@ class Serial : public Comm * @brief Open a serial port * @param port The port to open (e.g. "/dev/ttyUSB0") * @param baud_rate The baud rate for the serial port (e.g. 115200) - * @param message_handler Custom message handler, or omit for default handler + * @param message_handler Custom message handler, or omit for default + * handler * */ - Serial(std::string port, unsigned int baud_rate, MessageHandler& message_handler = default_message_handler_); + Serial(std::string port, unsigned int baud_rate, MessageHandler &message_handler = default_message_handler_); ~Serial(); - /** * @brief Set serial port baud rate * @param baud_rate The baud rate for the serial port (e.g. 115200) @@ -79,9 +79,9 @@ class Serial : public Comm bool do_init() override; void do_close() override; void do_async_read(const boost::asio::mutable_buffers_1 &buffer, - boost::function handler) override; + boost::function handler) override; void do_async_write(const boost::asio::const_buffers_1 &buffer, - boost::function handler) override; + boost::function handler) override; std::string port_; unsigned int baud_rate_; @@ -89,6 +89,6 @@ class Serial : public Comm boost::asio::serial_port serial_port_; }; -} // namespace async_comm +} // namespace async_comm -#endif // ASYNC_COMM_SERIAL_H +#endif // ASYNC_COMM_SERIAL_H diff --git a/include/async_comm/tcp_client.h b/include/async_comm/tcp_client.h index db9b33a..6c91527 100644 --- a/include/async_comm/tcp_client.h +++ b/include/async_comm/tcp_client.h @@ -20,14 +20,15 @@ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ /** @@ -43,12 +44,11 @@ #include #include -#include #include +#include namespace async_comm { - /** * @class TCPClient * @brief Asynchronous communication class for a TCP client @@ -60,10 +60,12 @@ class TCPClient : public Comm * @brief Connect to a TCP socket as a client * @param host The host where the TCP server is running * @param port The port on which the TCP server is listening - * @param message_handler Custom message handler, or omit for default handler + * @param message_handler Custom message handler, or omit for default + * handler */ - TCPClient(std::string host = DEFAULT_HOST, uint16_t port = DEFAULT_PORT, - MessageHandler& message_handler = default_message_handler_); + TCPClient(std::string host = DEFAULT_HOST, + uint16_t port = DEFAULT_PORT, + MessageHandler &message_handler = default_message_handler_); ~TCPClient(); private: @@ -74,9 +76,9 @@ class TCPClient : public Comm bool do_init() override; void do_close() override; void do_async_read(const boost::asio::mutable_buffers_1 &buffer, - boost::function handler) override; + boost::function handler) override; void do_async_write(const boost::asio::const_buffers_1 &buffer, - boost::function handler) override; + boost::function handler) override; std::string host_; uint16_t port_; @@ -85,6 +87,6 @@ class TCPClient : public Comm boost::asio::ip::tcp::endpoint endpoint_; }; -} // namespace async_comm +} // namespace async_comm -#endif // ASYNC_COMM_TCP_CLIENT_H +#endif // ASYNC_COMM_TCP_CLIENT_H diff --git a/include/async_comm/udp.h b/include/async_comm/udp.h index 96568a3..2bfa2e2 100644 --- a/include/async_comm/udp.h +++ b/include/async_comm/udp.h @@ -20,14 +20,15 @@ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ /** @@ -43,12 +44,11 @@ #include #include -#include #include +#include namespace async_comm { - /** * @class UDP * @brief Asynchronous communication class for a UDP socket @@ -58,15 +58,19 @@ class UDP : public Comm public: /** * @brief Bind a UDP socket - * @param bind_host The bind host where this application is listening (usually "localhost") + * @param bind_host The bind host where this application is listening + * (usually "localhost") * @param bind_port The bind port where this application is listening * @param remote_host The remote host to communicate with * @param remote_port The port on the remote host - * @param message_handler Custom message handler, or omit for default handler + * @param message_handler Custom message handler, or omit for default + * handler */ - UDP(std::string bind_host = DEFAULT_BIND_HOST, uint16_t bind_port = DEFAULT_BIND_PORT, - std::string remote_host = DEFAULT_REMOTE_HOST, uint16_t remote_port = DEFAULT_REMOTE_PORT, - MessageHandler& message_handler = default_message_handler_); + UDP(std::string bind_host = DEFAULT_BIND_HOST, + uint16_t bind_port = DEFAULT_BIND_PORT, + std::string remote_host = DEFAULT_REMOTE_HOST, + uint16_t remote_port = DEFAULT_REMOTE_PORT, + MessageHandler &message_handler = default_message_handler_); ~UDP(); private: @@ -79,9 +83,9 @@ class UDP : public Comm bool do_init() override; void do_close() override; void do_async_read(const boost::asio::mutable_buffers_1 &buffer, - boost::function handler) override; + boost::function handler) override; void do_async_write(const boost::asio::const_buffers_1 &buffer, - boost::function handler) override; + boost::function handler) override; std::string bind_host_; uint16_t bind_port_; @@ -94,6 +98,6 @@ class UDP : public Comm boost::asio::ip::udp::endpoint remote_endpoint_; }; -} // namespace async_comm +} // namespace async_comm -#endif // ASYNC_COMM_UDP_H +#endif // ASYNC_COMM_UDP_H diff --git a/include/async_comm/util/message_handler_ros.h b/include/async_comm/util/message_handler_ros.h index 83616a1..8292fad 100644 --- a/include/async_comm/util/message_handler_ros.h +++ b/include/async_comm/util/message_handler_ros.h @@ -20,14 +20,15 @@ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ /** @@ -46,25 +47,25 @@ namespace async_comm { namespace util { - /** * @class MessageHandlerROS * @brief Message handler implementation for ROS environments * * This is a convenience message handler implementation for ROS-based projects. - * The implementation simply forwards messages to the appropriate rosconsole loggers. + * The implementation simply forwards messages to the appropriate rosconsole + * loggers. */ class MessageHandlerROS : public MessageHandler { public: inline void debug(const std::string &message) override { ROS_DEBUG("[async_comm]: %s", message.c_str()); } - inline void info(const std::string &message) override { ROS_INFO("[async_comm]: %s", message.c_str()); } - inline void warn(const std::string &message) override { ROS_WARN("[async_comm]: %s", message.c_str()); } + inline void info(const std::string &message) override { ROS_INFO("[async_comm]: %s", message.c_str()); } + inline void warn(const std::string &message) override { ROS_WARN("[async_comm]: %s", message.c_str()); } inline void error(const std::string &message) override { ROS_ERROR("[async_comm]: %s", message.c_str()); } inline void fatal(const std::string &message) override { ROS_FATAL("[async_comm]: %s", message.c_str()); } }; -} // namespace util -} // namespace async_comm +} // namespace util +} // namespace async_comm -#endif // ASYNC_COMM_MESSAGE_HANDLER_ROS_H \ No newline at end of file +#endif // ASYNC_COMM_MESSAGE_HANDLER_ROS_H \ No newline at end of file diff --git a/src/comm.cpp b/src/comm.cpp index a1e7acd..97827a2 100644 --- a/src/comm.cpp +++ b/src/comm.cpp @@ -20,14 +20,15 @@ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ /** @@ -37,26 +38,23 @@ #include -#include #include +#include namespace async_comm { - Comm::DefaultMessageHandler Comm::default_message_handler_; -Comm::Comm(MessageHandler& message_handler) : - message_handler_(message_handler), - io_service_(), - new_data_(false), - shutdown_requested_(false), - write_in_progress_(false) +Comm::Comm(MessageHandler& message_handler) + : message_handler_(message_handler), + io_service_(), + new_data_(false), + shutdown_requested_(false), + write_in_progress_(false) { } -Comm::~Comm() -{ -} +Comm::~Comm() {} bool Comm::init() { @@ -94,7 +92,7 @@ void Comm::close() } } -void Comm::send_bytes(const uint8_t *src, size_t len) +void Comm::send_bytes(const uint8_t* src, size_t len) { mutex_lock lock(write_mutex_); @@ -114,16 +112,15 @@ void Comm::register_receive_callback(std::function void Comm::async_read() { - if (!is_open()) return; + if (!is_open()) + return; do_async_read(boost::asio::buffer(read_buffer_, READ_BUFFER_SIZE), - boost::bind(&Comm::async_read_end, - this, - boost::asio::placeholders::error, + boost::bind(&Comm::async_read_end, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } -void Comm::async_read_end(const boost::system::error_code &error, size_t bytes_transferred) +void Comm::async_read_end(const boost::system::error_code& error, size_t bytes_transferred) { if (error) { @@ -154,13 +151,11 @@ void Comm::async_write(bool check_write_state) write_in_progress_ = true; WriteBuffer& buffer = write_queue_.front(); do_async_write(boost::asio::buffer(buffer.dpos(), buffer.nbytes()), - boost::bind(&Comm::async_write_end, - this, - boost::asio::placeholders::error, + boost::bind(&Comm::async_write_end, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } -void Comm::async_write_end(const boost::system::error_code &error, size_t bytes_transferred) +void Comm::async_write_end(const boost::system::error_code& error, size_t bytes_transferred) { if (error) { @@ -197,7 +192,7 @@ void Comm::process_callbacks() { // wait for either new data or a shutdown request std::unique_lock lock(callback_mutex_); - condition_variable_.wait(lock, [this]{ return new_data_ || shutdown_requested_; }); + condition_variable_.wait(lock, [this] { return new_data_ || shutdown_requested_; }); // if shutdown requested, end thread execution if (shutdown_requested_) @@ -222,4 +217,4 @@ void Comm::process_callbacks() } } -} // namespace async_comm +} // namespace async_comm diff --git a/src/serial.cpp b/src/serial.cpp index aa6e93b..8d14b7a 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -20,14 +20,15 @@ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ /** @@ -37,18 +38,14 @@ #include -#include +#include using boost::asio::serial_port_base; namespace async_comm { - -Serial::Serial(std::string port, unsigned int baud_rate, MessageHandler& message_handler) : - Comm(message_handler), - port_(port), - baud_rate_(baud_rate), - serial_port_(io_service_) +Serial::Serial(std::string port, unsigned int baud_rate, MessageHandler &message_handler) + : Comm(message_handler), port_(port), baud_rate_(baud_rate), serial_port_(io_service_) { } @@ -63,6 +60,7 @@ bool Serial::set_baud_rate(unsigned int baud_rate) try { serial_port_.set_option(serial_port_base::baud_rate(baud_rate_)); + serial_port_.open(port_); } catch (boost::system::system_error e) { @@ -104,15 +102,15 @@ void Serial::do_close() } void Serial::do_async_read(const boost::asio::mutable_buffers_1 &buffer, - boost::function handler) + boost::function handler) { serial_port_.async_read_some(buffer, handler); } void Serial::do_async_write(const boost::asio::const_buffers_1 &buffer, - boost::function handler) + boost::function handler) { serial_port_.async_write_some(buffer, handler); } -} // namespace async_comm +} // namespace async_comm diff --git a/src/tcp_client.cpp b/src/tcp_client.cpp index 870c415..1dde77d 100644 --- a/src/tcp_client.cpp +++ b/src/tcp_client.cpp @@ -20,14 +20,15 @@ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ /** @@ -43,12 +44,8 @@ using boost::asio::ip::tcp; namespace async_comm { - -TCPClient::TCPClient(std::string host, uint16_t port, MessageHandler& message_handler) : - Comm(message_handler), - host_(host), - port_(port), - socket_(io_service_) +TCPClient::TCPClient(std::string host, uint16_t port, MessageHandler &message_handler) + : Comm(message_handler), host_(host), port_(port), socket_(io_service_) { } @@ -75,8 +72,8 @@ bool TCPClient::do_init() socket_.connect(endpoint_); socket_.set_option(tcp::socket::reuse_address(true)); - socket_.set_option(tcp::socket::send_buffer_size(WRITE_BUFFER_SIZE*1024)); - socket_.set_option(tcp::socket::receive_buffer_size(READ_BUFFER_SIZE*1024)); + socket_.set_option(tcp::socket::send_buffer_size(WRITE_BUFFER_SIZE * 1024)); + socket_.set_option(tcp::socket::receive_buffer_size(READ_BUFFER_SIZE * 1024)); } catch (boost::system::system_error e) { @@ -93,15 +90,15 @@ void TCPClient::do_close() } void TCPClient::do_async_read(const boost::asio::mutable_buffers_1 &buffer, - boost::function handler) + boost::function handler) { socket_.async_receive(buffer, handler); } void TCPClient::do_async_write(const boost::asio::const_buffers_1 &buffer, - boost::function handler) + boost::function handler) { socket_.async_send(buffer, handler); } -} // namespace async_comm +} // namespace async_comm diff --git a/src/udp.cpp b/src/udp.cpp index ab826f9..00b2b99 100644 --- a/src/udp.cpp +++ b/src/udp.cpp @@ -20,14 +20,15 @@ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ /** @@ -43,16 +44,17 @@ using boost::asio::ip::udp; namespace async_comm { - - -UDP::UDP(std::string bind_host, uint16_t bind_port, std::string remote_host, uint16_t remote_port, - MessageHandler& message_handler) : - Comm(message_handler), - bind_host_(bind_host), - bind_port_(bind_port), - remote_host_(remote_host), - remote_port_(remote_port), - socket_(io_service_) +UDP::UDP(std::string bind_host, + uint16_t bind_port, + std::string remote_host, + uint16_t remote_port, + MessageHandler &message_handler) + : Comm(message_handler), + bind_host_(bind_host), + bind_port_(bind_port), + remote_host_(remote_host), + remote_port_(remote_port), + socket_(io_service_) { } @@ -82,8 +84,8 @@ bool UDP::do_init() socket_.bind(bind_endpoint_); socket_.set_option(udp::socket::reuse_address(true)); - socket_.set_option(udp::socket::send_buffer_size(WRITE_BUFFER_SIZE*1024)); - socket_.set_option(udp::socket::receive_buffer_size(READ_BUFFER_SIZE*1024)); + socket_.set_option(udp::socket::send_buffer_size(WRITE_BUFFER_SIZE * 1024)); + socket_.set_option(udp::socket::receive_buffer_size(READ_BUFFER_SIZE * 1024)); } catch (boost::system::system_error e) { @@ -100,15 +102,15 @@ void UDP::do_close() } void UDP::do_async_read(const boost::asio::mutable_buffers_1 &buffer, - boost::function handler) + boost::function handler) { socket_.async_receive_from(buffer, remote_endpoint_, handler); } void UDP::do_async_write(const boost::asio::const_buffers_1 &buffer, - boost::function handler) + boost::function handler) { socket_.async_send_to(buffer, remote_endpoint_, handler); } -} // namespace async_comm +} // namespace async_comm