Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HW]: Add Peak Ethernet Gateway DR support #455

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions hardware_integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ if("WindowsPCANBasic" IN_LIST CAN_DRIVER)
list(APPEND HARDWARE_INTEGRATION_SRC "pcan_basic_windows_plugin.cpp")
list(APPEND HARDWARE_INTEGRATION_INCLUDE "pcan_basic_windows_plugin.hpp")
endif()
if("PeakEthernetGateway" IN_LIST CAN_DRIVER)
list(APPEND HARDWARE_INTEGRATION_SRC "peak_ethernet_gateway_plugin.cpp")
list(APPEND HARDWARE_INTEGRATION_INCLUDE "peak_ethernet_gateway_plugin.hpp")
if(WIN32)
find_library(SOCKET_LIB ws2_32)
if(SOCKET_LIB)
message(STATUS "Linking Winsock2 due to PEAK Ethernet Gateway")
endif()
endif()
endif()
if("VirtualCAN" IN_LIST CAN_DRIVER)
list(APPEND HARDWARE_INTEGRATION_SRC "virtual_can_plugin.cpp")
list(APPEND HARDWARE_INTEGRATION_INCLUDE "virtual_can_plugin.hpp")
Expand Down Expand Up @@ -127,8 +137,9 @@ add_library(${PROJECT_NAME}::HardwareIntegration ALIAS HardwareIntegration)

target_compile_features(HardwareIntegration PUBLIC cxx_std_11)
set_target_properties(HardwareIntegration PROPERTIES CXX_EXTENSIONS OFF)
target_link_libraries(HardwareIntegration PRIVATE ${PROJECT_NAME}::Utility
${PROJECT_NAME}::Isobus)
target_link_libraries(
HardwareIntegration PRIVATE ${PROJECT_NAME}::Utility ${PROJECT_NAME}::Isobus
${SOCKET_LIB})
GwnDaan marked this conversation as resolved.
Show resolved Hide resolved

if("WindowsPCANBasic" IN_LIST CAN_DRIVER)
if(MSVC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#include "isobus/hardware_integration/pcan_basic_windows_plugin.hpp"
#endif

#ifdef ISOBUS_PEAKETHERNETGATEWAY_AVAILABLE
#include "isobus/hardware_integration/peak_ethernet_gateway_plugin.hpp"
#endif

#ifdef ISOBUS_VIRTUALCAN_AVAILABLE
#include "isobus/hardware_integration/virtual_can_plugin.hpp"
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//================================================================================================
/// @file peak_ethernet_gateway_plugin.hpp
///
/// @brief An interface for using a PEAK Ethernet Gateway DR.
/// @author Adrian Del Grosso
///
/// @copyright 2024 The Open-Agriculture Developers
//================================================================================================
#ifndef PEAK_ETHERNET_GATEWAY_PLUGIN_HPP
#define PEAK_ETHERNET_GATEWAY_PLUGIN_HPP

#include <deque>
#include <memory>
#include <string>

#ifdef WIN32
#include <winsock.h>
#else
#include <sys/socket.h>
#endif

#include "isobus/hardware_integration/can_hardware_plugin.hpp"
#include "isobus/isobus/can_hardware_abstraction.hpp"
#include "isobus/isobus/can_message_frame.hpp"

namespace isobus
{
/// @brief Peak Ethernet Gateway CAN hardware plugin
class PeakEthernetGatewayPlugin : public CANHardwarePlugin
{
public:
/// @brief Constructor for a PeakEthernetGatewayPlugin
/// @param[in] targetIPAddress The IP address of the gateway
/// @param[in] txPort The port to send data to
/// @param[in] rxPort The port to receive data from (this is the port the gateway sends data from)
/// @param[in] useUDP If `true`, use UDP, otherwise use TCP
PeakEthernetGatewayPlugin(std::string targetIPAddress,
std::uint16_t txPort,
std::uint16_t rxPort,
bool useUDP = true);

/// @brief The destructor for PCANBasicWindowsPlugin
virtual ~PeakEthernetGatewayPlugin();

/// @brief Returns if the connection with the hardware is valid
/// @returns `true` if connected, `false` if not connected
bool get_is_valid() const override;

/// @brief Closes the connection to the hardware
void close() override;

/// @brief Connects to the hardware you specified in the constructor's channel argument
void open() override;

/// @brief Returns a frame from the hardware (synchronous), or `false` if no frame can be read.
/// @param[in, out] canFrame The CAN frame that was read
/// @returns `true` if a CAN frame was read, otherwise `false`
bool read_frame(isobus::CANMessageFrame &canFrame) override;

/// @brief Writes a frame to the bus (synchronous)
/// @param[in] canFrame The frame to write to the bus
/// @returns `true` if the frame was written, otherwise `false`
bool write_frame(const isobus::CANMessageFrame &canFrame) override;

private:
/// @brief Opens the receive socket
/// @returns `true` if the socket was opened, otherwise `false`
bool open_rx_socket();

/// @brief Opens the transmit socket
/// @returns `true` if the socket was opened, otherwise `false`
bool open_tx_socket();

static constexpr std::uint32_t RX_BUFFER_SIZE_BYTES = 2048; ///< Default size of the receive buffer

std::unique_ptr<std::uint8_t> rxBuffer; ///< The receive buffer (multiple frames can be in the buffer, per ethernet frame)
std::deque<isobus::CANMessageFrame> rxQueue; ///< The queue of received frames, which get passed to the CAN Hardware Abstraction when needed
std::string ipAddress; ///< The IP address of the gateway
int rxSocket = 0; ///< The receive socket identifier
int txSocket = 0; ///< The transmit socket identifier
std::uint16_t sendPort; ///< The port to send data to
std::uint16_t receivePort; ///< The port to receive data from
bool isOpen = false; ///< If the connection is open
bool udp; ///< If the connection is using UDP
};
}
#endif // PEAK_ETHERNET_GATEWAY_PLUGIN_HPP
Loading
Loading