diff --git a/hardware_integration/include/isobus/hardware_integration/toucan_vscp_canal.hpp b/hardware_integration/include/isobus/hardware_integration/toucan_vscp_canal.hpp index e6d38dbc..dd4e5954 100644 --- a/hardware_integration/include/isobus/hardware_integration/toucan_vscp_canal.hpp +++ b/hardware_integration/include/isobus/hardware_integration/toucan_vscp_canal.hpp @@ -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. diff --git a/hardware_integration/src/toucan_vscp_canal.cpp b/hardware_integration/src/toucan_vscp_canal.cpp index 954f9aea..c5aa475e 100644 --- a/hardware_integration/src/toucan_vscp_canal.cpp +++ b/hardware_integration/src/toucan_vscp_canal.cpp @@ -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) : @@ -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; + } }