Skip to content

Commit

Permalink
Reformatted udp_board to clang_tidy format
Browse files Browse the repository at this point in the history
  • Loading branch information
bsutherland333 committed Sep 8, 2023
1 parent 5df075d commit 31b2a74
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 46 deletions.
92 changes: 59 additions & 33 deletions boards/udp/udp_board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,24 @@
* \author Daniel Koch
*/

#include <iostream>
#include "udp_board.hpp"

#include <iostream>

using boost::asio::ip::udp;

namespace rosflight_firmware
{
UDPBoard::UDPBoard(std::string bind_host, uint16_t bind_port, std::string remote_host,
uint16_t remote_port)
: bind_host_(std::move(bind_host)), bind_port_(bind_port), remote_host_(std::move(remote_host)),
remote_port_(remote_port), io_service_(), socket_(io_service_), write_in_progress_(false)
{}
UDPBoard::UDPBoard(std::string bind_host, uint16_t bind_port, std::string remote_host, uint16_t remote_port) :
bind_host_(std::move(bind_host)),
bind_port_(bind_port),
remote_host_(std::move(remote_host)),
remote_port_(remote_port),
io_service_(),
socket_(io_service_),
write_in_progress_(false)
{
}

UDPBoard::~UDPBoard()
{
Expand All @@ -55,11 +61,13 @@ UDPBoard::~UDPBoard()
io_service_.stop();
socket_.close();

if (io_thread_.joinable()) { io_thread_.join(); }
if (io_thread_.joinable())
{
io_thread_.join();
}
}

void UDPBoard::set_ports(std::string bind_host, uint16_t bind_port, std::string remote_host,
uint16_t remote_port)
void UDPBoard::set_ports(std::string bind_host, uint16_t bind_port, std::string remote_host, uint16_t remote_port)
{
bind_host_ = std::move(bind_host);
bind_port_ = bind_port;
Expand All @@ -70,7 +78,7 @@ void UDPBoard::set_ports(std::string bind_host, uint16_t bind_port, std::string
void UDPBoard::serial_init(uint32_t baud_rate, uint32_t dev)
{
// can throw an uncaught boost::system::system_error exception
(void) dev;
(void)dev;

udp::resolver resolver(io_service_);

Expand All @@ -93,7 +101,7 @@ void UDPBoard::serial_init(uint32_t baud_rate, uint32_t dev)

void UDPBoard::serial_flush() {}

void UDPBoard::serial_write(const uint8_t * src, size_t len)
void UDPBoard::serial_write(const uint8_t* src, size_t len)
{
auto buffer = new Buffer(src, len);

Expand All @@ -115,12 +123,16 @@ uint8_t UDPBoard::serial_read()
{
MutexLock lock(read_mutex_);

if (read_queue_.empty()) { return 0; }
if (read_queue_.empty())
{
return 0;
}

Buffer * buffer = read_queue_.front();
Buffer* buffer = read_queue_.front();
uint8_t byte = buffer->consume_byte();

if (buffer->empty()) {
if (buffer->empty())
{
read_queue_.pop_front();
delete buffer;
}
Expand All @@ -129,18 +141,21 @@ uint8_t UDPBoard::serial_read()

void UDPBoard::async_read()
{
if (!socket_.is_open()) { return; }
if (!socket_.is_open())
{
return;
}

MutexLock lock(read_mutex_);
socket_.async_receive_from(
boost::asio::buffer(read_buffer_, MAVLINK_MAX_PACKET_LEN), remote_endpoint_,
boost::bind(&UDPBoard::async_read_end, this, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
socket_.async_receive_from(boost::asio::buffer(read_buffer_, MAVLINK_MAX_PACKET_LEN), remote_endpoint_,
boost::bind(&UDPBoard::async_read_end, this, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}

void UDPBoard::async_read_end(const boost::system::error_code & error, size_t bytes_transferred)
void UDPBoard::async_read_end(const boost::system::error_code& error, size_t bytes_transferred)
{
if (!error) {
if (!error)
{
MutexLock lock(read_mutex_);
read_queue_.push_back(new Buffer(read_buffer_, bytes_transferred));
}
Expand All @@ -149,39 +164,50 @@ void UDPBoard::async_read_end(const boost::system::error_code & error, size_t by

void UDPBoard::async_write(bool check_write_state)
{
if (check_write_state && write_in_progress_) { return; }
if (check_write_state && write_in_progress_)
{
return;
}

MutexLock lock(write_mutex_);
if (write_queue_.empty()) { return; }
if (write_queue_.empty())
{
return;
}

write_in_progress_ = true;
Buffer * buffer = write_queue_.front();
Buffer* buffer = write_queue_.front();
socket_.async_send_to(boost::asio::buffer(buffer->dpos(), buffer->nbytes()), remote_endpoint_,
boost::bind(&UDPBoard::async_write_end, this,
boost::asio::placeholders::error,
boost::bind(&UDPBoard::async_write_end, this, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}

void UDPBoard::async_write_end(const boost::system::error_code & error, size_t bytes_transferred)
void UDPBoard::async_write_end(const boost::system::error_code& error, size_t bytes_transferred)
{
if (!error) {
if (!error)
{
MutexLock lock(write_mutex_);

if (write_queue_.empty()) {
if (write_queue_.empty())
{
write_in_progress_ = false;
return;
}

Buffer * buffer = write_queue_.front();
Buffer* buffer = write_queue_.front();
buffer->pos += bytes_transferred;
if (buffer->empty()) {
if (buffer->empty())
{
write_queue_.pop_front();
delete buffer;
}

if (write_queue_.empty()) {
if (write_queue_.empty())
{
write_in_progress_ = false;
} else {
}
else
{
async_write(false);
}
}
Expand Down
27 changes: 14 additions & 13 deletions boards/udp/udp_board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,33 @@
#ifndef ROSFLIGHT_FIRMWARE_UDP_BOARD_H
#define ROSFLIGHT_FIRMWARE_UDP_BOARD_H

#include <list>
#include <string>
#include "board.h"
#include "mavlink/mavlink.h"

#include <boost/asio.hpp>
#include <boost/thread.hpp>

#include "board.h"
#include "mavlink/mavlink.h"
#include <list>
#include <string>

namespace rosflight_firmware
{
class UDPBoard : public Board
{
public:
explicit UDPBoard(std::string bind_host = "localhost", uint16_t bind_port = 14525,
std::string remote_host = "localhost", uint16_t remote_port = 14520);
explicit UDPBoard(std::string bind_host = "localhost",
uint16_t bind_port = 14525,
std::string remote_host = "localhost",
uint16_t remote_port = 14520);
~UDPBoard();

void serial_init(uint32_t baud_rate, uint32_t dev) override;
void serial_write(const uint8_t * src, size_t len) override;
void serial_write(const uint8_t *src, size_t len) override;
uint16_t serial_bytes_available() override;
uint8_t serial_read() override;
void serial_flush() override;

void set_ports(std::string bind_host, uint16_t bind_port, std::string remote_host,
uint16_t remote_port);
void set_ports(std::string bind_host, uint16_t bind_port, std::string remote_host, uint16_t remote_port);

private:
struct Buffer
Expand All @@ -73,13 +74,13 @@ class UDPBoard : public Board

Buffer() : len(0), pos(0) {}

Buffer(const uint8_t * src, size_t length) : len(length), pos(0)
Buffer(const uint8_t *src, size_t length) : len(length), pos(0)
{
assert(length <= MAVLINK_MAX_PACKET_LEN); //! \todo Do something less catastrophic here
memcpy(data, src, length);
}

const uint8_t * dpos() const { return data + pos; }
const uint8_t *dpos() const { return data + pos; }
size_t nbytes() const { return len - pos; }
void add_byte(uint8_t byte) { data[len++] = byte; }
uint8_t consume_byte() { return data[pos++]; }
Expand All @@ -90,10 +91,10 @@ class UDPBoard : public Board
typedef boost::lock_guard<boost::recursive_mutex> MutexLock;

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);

std::string bind_host_;
uint16_t bind_port_;
Expand Down

0 comments on commit 31b2a74

Please sign in to comment.