Skip to content

Commit

Permalink
[HW]: Add Peak Ethernet Gateway DR support
Browse files Browse the repository at this point in the history
Added a CAN plugin for the PEAK ethernet gateway.
  • Loading branch information
ad3154 committed Mar 20, 2024
1 parent f192b2f commit b86466e
Show file tree
Hide file tree
Showing 4 changed files with 481 additions and 1 deletion.
13 changes: 12 additions & 1 deletion 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 @@ -128,7 +138,8 @@ 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)
${PROJECT_NAME}::Isobus
${SOCKET_LIB})

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,79 @@
//================================================================================================
/// @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
explicit PeakEthernetGatewayPlugin(std::string targetIPAddress,
std::uint16_t txPort,
std::uint16_t rxPort,
std::uint8_t CANChannelOnGateway,
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:
bool openRxSocket();
bool openTxSocket();

static constexpr std::uint32_t RX_BUFFER_SIZE_BYTES = 2048;

std::unique_ptr<std::uint8_t> rxBuffer;
std::deque<isobus::CANMessageFrame> rxQueue;
std::string ipAddress;
int rxSocket = 0;
int txSocket = 0;
std::uint16_t sendPort;
std::uint16_t receivePort;
bool isOpen = false;
bool udp;
};
}
#endif // PEAK_ETHERNET_GATEWAY_PLUGIN_HPP
Loading

0 comments on commit b86466e

Please sign in to comment.