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

Add infrastructure to monitor the VT IOP loading progress #533

Merged
merged 14 commits into from
Feb 16, 2025
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ namespace isobus
LanguageCommandInterface languageCommandInterface; ///< The language command interface for the server
std::shared_ptr<InternalControlFunction> serverInternalControlFunction; ///< The internal control function for the server
std::vector<std::shared_ptr<VirtualTerminalServerManagedWorkingSet>> managedWorkingSetList; ///< The list of managed working sets
std::map<std::shared_ptr<VirtualTerminalServerManagedWorkingSet>, bool> managedWorkingSetIopLoadStateMap; ///< A map to hold the IOP load state per session
std::shared_ptr<VirtualTerminalServerManagedWorkingSet> activeWorkingSet; ///< The active working set
std::uint32_t statusMessageTimestamp_ms = 0; ///< The timestamp of the last status message sent
std::uint16_t activeWorkingSetDataMaskObjectID = NULL_OBJECT_ID; ///< The object ID of the active working set's data mask
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ namespace isobus
/// @returns true if the working set should be deleted, otherwise false
bool is_deletion_requested() const;

/// @brief Set the IOP size used for download percentage calculations
/// @param[in] newIopSize IOP size in bytes
void set_iop_size(std::uint32_t newIopSize);

/// @brief Function to retrieve the IOP load progress
/// @returns state of the IOP loading in percentage (0-100.0). Returns 0 if the IOP size is not set.
float iop_load_percentage() const;

/// @brief Function to check the IOP loading state
/// @returns returns true if the IOP size is known but the transfer is not finished
bool is_object_pool_transfer_in_progress() const;

private:
/// @brief Sets the object pool processing state to a new value
/// @param[in] value The new state of processing the object pool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ namespace isobus

std::mutex managedWorkingSetMutex; ///< A mutex to protect the interface of the managed working set
VTColourTable workingSetColourTable; ///< This working set's colour table
std::uint32_t iopSize = 0; ///< Total size of the IOP in bytes
std::uint32_t transferredIopSize = 0; ///< Total number of IOP bytes transferred
std::map<std::uint16_t, std::shared_ptr<VTObject>> vtObjectTree; ///< The C++ object representation (deserialized) of the object pool being managed
std::vector<std::vector<std::uint8_t>> iopFilesRawData; ///< Raw IOP File data from the client
std::uint16_t workingSetID = NULL_OBJECT_ID; ///< Stores the object ID of the working set object itself
Expand Down
2 changes: 2 additions & 0 deletions isobus/src/isobus_virtual_terminal_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ namespace isobus
{
CANStackLogger::debug("[VT Server]: Callback indicated there may be enough memory, but since there is overhead associated to object storage it is impossible to be sure.", requiredMemory);
}
cf->set_iop_size(requiredMemory);

std::array<std::uint8_t, CAN_DATA_LENGTH> buffer = { 0 };
buffer[0] = static_cast<std::uint8_t>(Function::GetMemoryMessage);
Expand Down Expand Up @@ -447,6 +448,7 @@ namespace isobus
auto loadedVersion = parentServer->load_version(versionLabel, message.get_source_control_function()->get_NAME());
if (!loadedVersion.empty())
{
cf->set_iop_size(loadedVersion.size());
cf->add_iop_raw_data(loadedVersion);
}
else
Expand Down
41 changes: 41 additions & 0 deletions isobus/src/isobus_virtual_terminal_server_managed_working_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//================================================================================================
#include "isobus/isobus/isobus_virtual_terminal_server_managed_working_set.hpp"

#include "isobus/isobus/can_network_manager.hpp"
#include "isobus/isobus/can_stack_logger.hpp"
#include "isobus/utility/to_string.hpp"

Expand Down Expand Up @@ -127,6 +128,41 @@ namespace isobus
return workingSetDeletionRequested;
}

void VirtualTerminalServerManagedWorkingSet::set_iop_size(std::uint32_t newIopSize)
{
const std::lock_guard<std::mutex> lock(managedWorkingSetMutex);
iopSize = newIopSize;
}

float VirtualTerminalServerManagedWorkingSet::iop_load_percentage() const
{
if (processingState != ObjectPoolProcessingThreadState::None || transferredIopSize > iopSize)
{
return 100.0f;
}

// if IOP transfer is not completed check if there is an ongoing IOP transfer to us
auto sessions = CANNetworkManager::CANNetwork.get_active_transport_protocol_sessions(0);
auto currentTransferredIopSize = transferredIopSize;
for (const auto &session : sessions)
{
if (session->get_source()->get_address() == get_control_function()->get_address() &&
(static_cast<std::uint32_t>(CANLibParameterGroupNumber::ECUtoVirtualTerminal) == session->get_parameter_group_number()) &&
(session->get_data().size() >= 1) &&
(session->get_data().get_byte(0) == 0x11)) // ObjectPoolTransferMessage
{
currentTransferredIopSize += session->get_total_bytes_transferred();
}
}

if (currentTransferredIopSize > iopSize)
{
return 100.0f;
}

return (currentTransferredIopSize / (float)iopSize) * 100.0f;
}

void VirtualTerminalServerManagedWorkingSet::set_object_pool_processing_state(ObjectPoolProcessingThreadState value)
{
const std::lock_guard<std::mutex> lock(managedWorkingSetMutex);
Expand Down Expand Up @@ -170,4 +206,9 @@ namespace isobus
}
}

bool VirtualTerminalServerManagedWorkingSet::is_object_pool_transfer_in_progress() const
{
return iop_load_percentage() != 0.0f;
}

} // namespace isobus
1 change: 1 addition & 0 deletions isobus/src/isobus_virtual_terminal_working_set_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace isobus

void VirtualTerminalWorkingSetBase::add_iop_raw_data(const std::vector<std::uint8_t> &dataToAdd)
{
transferredIopSize += dataToAdd.size();
iopFilesRawData.push_back(dataToAdd);
}

Expand Down
Loading