Skip to content

Commit

Permalink
[HW]: Allow reconfiguring the TouCAN plugin at runtime
Browse files Browse the repository at this point in the history
This will make this plugin easier to configure after construction
such as in AgISOVirtualTerminal.
  • Loading branch information
ad3154 committed Dec 21, 2023
1 parent 1112626 commit 88f0891
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,20 @@ namespace isobus
/// @returns `true` if the frame was written, otherwise `false`
bool write_frame(const isobus::CANMessageFrame &canFrame) override;

/// @brief Changes previously set configuration parameters. Only works if the device is not open.
/// @param[in] deviceID The id to use for this device
/// @param[in] serialNumber The serial number of the CAN adapter to connect with
/// @param[in] baudRate The baud rate in thousands (250K baud would mean you need to pass in 250)
/// @returns True if the configuration was changed, otherwise false (if the device is open false will be returned)
bool reconfigure(std::int16_t deviceID, std::uint32_t serialNumber, std::uint16_t baudRate = 250);

private:
/// @brief Generates a device name string for the TouCAN device
/// @param[in] deviceID The id to use for this device
/// @param[in] serialNumber The serial number of the CAN adapter to connect with
/// @param[in] baudRate The baud rate in thousands (250K baud would mean you need to pass in 250)
void generate_device_name(std::int16_t deviceID, std::uint32_t serialNumber, std::uint16_t baudRate);

std::string name; ///< A configuration string that is used to connect to the hardware through the CANAL api
std::uint32_t handle = 0; ///< The handle that the driver returns to us for the open hardware
std::uint32_t openResult = CANAL_ERROR_NOT_OPEN; ///< Stores the result of the call to begin CAN communication. Used for is_valid check later.
Expand Down
59 changes: 38 additions & 21 deletions hardware_integration/src/toucan_vscp_canal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,7 @@ namespace isobus
{
TouCANPlugin::TouCANPlugin(std::int16_t deviceID, std::uint32_t serialNumber, std::uint16_t baudRate)
{
std::string deviceConfigString;
std::string serialString;
constexpr std::uint32_t MAX_SERIAL_LENGTH = 999999999;
constexpr std::size_t SERIAL_NUMBER_CHARACTER_REQUIREMENT = 8;

if (serialNumber > MAX_SERIAL_LENGTH)
{
isobus::CANStackLogger::critical("[TouCAN]: Invalid serial number. Must be 8 digits max.");
serialNumber = 0;
}
serialString = isobus::to_string(serialNumber);

if (SERIAL_NUMBER_CHARACTER_REQUIREMENT > serialString.length())
{
serialString.insert(0, SERIAL_NUMBER_CHARACTER_REQUIREMENT - serialString.length(), '0');
}

deviceConfigString += isobus::to_string(deviceID) + ';';
deviceConfigString += serialString + ';';
deviceConfigString += isobus::to_string(baudRate) + ';';
name = deviceConfigString;
generate_device_name(deviceID, serialNumber, baudRate);
}

TouCANPlugin::TouCANPlugin(std::string deviceName) :
Expand Down Expand Up @@ -113,4 +93,41 @@ namespace isobus

return (CANAL_ERROR_SUCCESS == result);
}

bool TouCANPlugin::reconfigure(std::int16_t deviceID, std::uint32_t serialNumber, std::uint16_t baudRate)
{
bool retVal = false;

if (!get_is_valid())
{
generate_device_name(deviceID, serialNumber, baudRate);
retVal = true;
}
return retVal;
}

void TouCANPlugin::generate_device_name(std::int16_t deviceID, std::uint32_t serialNumber, std::uint16_t baudRate)
{
std::string deviceConfigString;
std::string serialString;
constexpr std::uint32_t MAX_SERIAL_LENGTH = 999999999;
constexpr std::size_t SERIAL_NUMBER_CHARACTER_REQUIREMENT = 8;

if (serialNumber > MAX_SERIAL_LENGTH)
{
isobus::CANStackLogger::critical("[TouCAN]: Invalid serial number. Must be 8 digits max.");
serialNumber = 0;
}
serialString = isobus::to_string(serialNumber);

if (SERIAL_NUMBER_CHARACTER_REQUIREMENT > serialString.length())
{
serialString.insert(0, SERIAL_NUMBER_CHARACTER_REQUIREMENT - serialString.length(), '0');
}

deviceConfigString += isobus::to_string(deviceID) + ';';
deviceConfigString += serialString + ';';
deviceConfigString += isobus::to_string(baudRate) + ';';
name = deviceConfigString;
}
}

0 comments on commit 88f0891

Please sign in to comment.