Skip to content

Commit

Permalink
[FS]: Post rebase fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ad3154 committed Jun 26, 2024
1 parent 313b796 commit bacdaac
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 116 deletions.
44 changes: 16 additions & 28 deletions examples/file_server_client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ void update_CAN_network(void *)
isobus::CANNetworkManager::CANNetwork.update();
}

void raw_can_glue(isobus::HardwareInterfaceCANFrame &rawFrame, void *parentPointer)
{
isobus::CANNetworkManager::CANNetwork.can_lib_process_rx_message(rawFrame, parentPointer);
}

enum class ExampleStateMachineState
{
OpenFile,
Expand All @@ -45,19 +40,16 @@ int main()
{
std::signal(SIGINT, signal_handler);
isobus::CANStackLogger::set_can_stack_logger_sink(&logger);
isobus::CANStackLogger::set_log_level(isobus::CANStackLogger::LoggingLevel::Debug); // Change this to Debug to see more information
std::shared_ptr<isobus::InternalControlFunction> TestInternalECU = nullptr;
std::shared_ptr<isobus::PartneredControlFunction> TestPartnerFS = nullptr;
std::shared_ptr<isobus::FileServerClient> TestFileServerClient = nullptr;
std::shared_ptr<CANHardwarePlugin> canDriver = nullptr;
isobus::CANStackLogger::set_log_level(isobus::CANStackLogger::LoggingLevel::Debug);
std::shared_ptr<isobus::CANHardwarePlugin> canDriver = nullptr;
#if defined(ISOBUS_SOCKETCAN_AVAILABLE)
canDriver = std::make_shared<SocketCANInterface>("can0");
canDriver = std::make_shared<isobus::SocketCANInterface>("can0");
#elif defined(ISOBUS_WINDOWSPCANBASIC_AVAILABLE)
canDriver = std::make_shared<PCANBasicWindowsPlugin>(PCAN_USBBUS1);
canDriver = std::make_shared<isobus::PCANBasicWindowsPlugin>(PCAN_USBBUS1);
#elif defined(ISOBUS_WINDOWSINNOMAKERUSB2CAN_AVAILABLE)
canDriver = std::make_shared<InnoMakerUSB2CANWindowsPlugin>(0); // CAN0
canDriver = std::make_shared<isobus::InnoMakerUSB2CANWindowsPlugin>(0); // CAN0
#elif defined(ISOBUS_MACCANPCAN_AVAILABLE)
canDriver = std::make_shared<MacCANPCANPlugin>(PCAN_USBBUS1);
canDriver = std::make_shared<isobus::MacCANPCANPlugin>(PCAN_USBBUS1);
#endif

if (nullptr == canDriver)
Expand All @@ -67,24 +59,20 @@ int main()
return -1;
}

CANHardwareInterface::set_number_of_can_channels(1);
CANHardwareInterface::assign_can_channel_frame_handler(0, canDriver);
isobus::CANHardwareInterface::set_number_of_can_channels(1);
isobus::CANHardwareInterface::assign_can_channel_frame_handler(0, canDriver);

if ((!CANHardwareInterface::start()) || (!canDriver->get_is_valid()))
if ((!isobus::CANHardwareInterface::start()) || (!canDriver->get_is_valid()))
{
std::cout << "Failed to start hardware interface. The CAN driver might be invalid." << std::endl;
return -2;
}

CANHardwareInterface::add_can_lib_update_callback(update_CAN_network, nullptr);
CANHardwareInterface::add_raw_can_message_rx_callback(raw_can_glue, nullptr);

std::this_thread::sleep_for(std::chrono::milliseconds(250));

isobus::NAME TestDeviceNAME(0);

// Make sure you change these for your device!!!!
// This is an example device that is using a manufacturer code that is currently unused at time of writing
// Consider customizing these values to match your device
TestDeviceNAME.set_arbitrary_address_capable(true);
TestDeviceNAME.set_industry_group(1);
TestDeviceNAME.set_device_class(0);
Expand All @@ -93,22 +81,22 @@ int main()
TestDeviceNAME.set_ecu_instance(0);
TestDeviceNAME.set_function_instance(0);
TestDeviceNAME.set_device_class_instance(0);
TestDeviceNAME.set_manufacturer_code(64);
TestDeviceNAME.set_manufacturer_code(1407);

std::vector<isobus::NAMEFilter> fsNameFilters;
const isobus::NAMEFilter testFilter(isobus::NAME::NAMEParameters::FunctionCode, static_cast<std::uint8_t>(isobus::NAME::Function::FileServerOrPrinter));
fsNameFilters.push_back(testFilter);

TestInternalECU = std::make_shared<isobus::InternalControlFunction>(TestDeviceNAME, 0x1C, 0);
TestPartnerFS = std::make_shared<isobus ::PartneredControlFunction>(0, fsNameFilters);
TestFileServerClient = std::make_shared<isobus::FileServerClient>(TestPartnerFS, TestInternalECU);
auto TestInternalECU = isobus::CANNetworkManager::CANNetwork.create_internal_control_function(TestDeviceNAME, 0);
auto TestPartnerFS = isobus::CANNetworkManager::CANNetwork.create_partnered_control_function(0, fsNameFilters);
auto TestFileServerClient = std::make_shared<isobus::FileServerClient>(TestPartnerFS, TestInternalECU);

TestFileServerClient->initialize(true);

ExampleStateMachineState state = ExampleStateMachineState::OpenFile;
std::string fileNameToUse = "FSExampleFile.txt";
std::uint8_t fileHandle = isobus::FileServerClient::INVALID_FILE_HANDLE;
const std::string fileExampleContents = "This is an example file! Visit us on Github https://github.com/ad3154/Isobus-plus-plus";
const std::string fileExampleContents = "This is an example file! Visit us on Github https://github.com/Open-Agriculture/AgIsoStack-plus-plus";

while (running)
{
Expand Down Expand Up @@ -179,6 +167,6 @@ int main()
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

CANHardwareInterface::stop();
isobus::CANHardwareInterface::stop();
return 0;
}
9 changes: 4 additions & 5 deletions isobus/include/isobus/isobus/isobus_file_server_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include "isobus/isobus/can_network_manager.hpp"
#include "isobus/isobus/can_partnered_control_function.hpp"
#include "isobus/isobus/can_protocol.hpp"
#include "isobus/utility/processing_flags.hpp"

#include <map>
Expand Down Expand Up @@ -289,7 +288,7 @@ namespace isobus
InitializeVolumeRequest = 0x40 ///< Prepare the volume to accept files and directories. All data is lost upon completion
};

/// @brief Enuerates the transmit flags (CAN messages that support non-state-machine-driven retries)
/// @brief Enumerates the transmit flags (CAN messages that support non-state-machine-driven retries)
enum class TransmitFlags
{
ClientToServerStatus = 0, ///< Flag to send the maintenance message to the file server
Expand All @@ -308,7 +307,7 @@ namespace isobus
FileState state = FileState::Uninitialized; ///< A sub-state-machine state for the file
FileOpenMode openMode = FileOpenMode::OpenFileForReadingOnly; ///< The file open mode (read only, write only, etc)
FilePointerMode pointerMode = FilePointerMode::AppendMode; ///< Where the file pointer should be set for this file
std::uint32_t timstamp_ms = 0; ///< A timestamp to track when file operations take too long
std::uint32_t timestamp_ms = 0; ///< A timestamp to track when file operations take too long
std::uint8_t attributesBitField = 0; ///< The reported file attributes
std::uint8_t transactionNumberForRequest = 0; ///< The TAN for the latest request corresponding to this file
std::uint8_t handle = INVALID_FILE_HANDLE; ///< The file handle associated with this file
Expand All @@ -331,12 +330,12 @@ namespace isobus

/// @brief A generic way for a protocol to process a received message
/// @param[in] message A received CAN message
void process_message(CANMessage *const message);
void process_message(const CANMessage &message);

/// @brief A generic way for a protocol to process a received message
/// @param[in] message A received CAN message
/// @param[in] parent Provides the context to the actual TP manager object
static void process_message(CANMessage *const message, void *parent);
static void process_message(const CANMessage &message, void *parent);

/// @brief The data callback passed to the network manger's send function for the transport layer messages
/// @details We upload the data with callbacks to avoid making a complete copy of the data to
Expand Down
82 changes: 39 additions & 43 deletions isobus/src/isobus_file_server_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ namespace isobus
return CANNetworkManager::CANNetwork.send_can_message(static_cast<std::uint32_t>(CANLibParameterGroupNumber::ClientToFileServer),
buffer.data(),
buffer.size(),
myControlFunction.get(),
partnerControlFunction.get(),
CANIdentifier::PriorityLowest7);
myControlFunction,
partnerControlFunction,
CANIdentifier::CANPriority::PriorityLowest7);
}

bool FileServerClient::initialize(bool spawnThread)
Expand Down Expand Up @@ -464,21 +464,20 @@ namespace isobus
}
}

void FileServerClient::process_message(CANMessage *const message)
void FileServerClient::process_message(const CANMessage &message)
{
if ((nullptr != message) &&
(nullptr != partnerControlFunction) &&
(static_cast<std::uint32_t>(CANLibParameterGroupNumber::FileServerToClient) == message->get_identifier().get_parameter_group_number()) &&
((message->get_source_control_function()->get_address() == partnerControlFunction->get_address()) ||
(nullptr == message->get_destination_control_function())))
if ((nullptr != partnerControlFunction) &&
(static_cast<std::uint32_t>(CANLibParameterGroupNumber::FileServerToClient) == message.get_identifier().get_parameter_group_number()) &&
((message.get_source_control_function()->get_address() == partnerControlFunction->get_address()) ||
(nullptr == message.get_destination_control_function())))
{
auto &messageData = message->get_data();
auto &messageData = message.get_data();

switch (messageData[0])
{
case static_cast<std::uint8_t>(FileServerToClientMultiplexor::FileServerStatus):
{
if (CAN_DATA_LENGTH == message->get_data_length())
if (CAN_DATA_LENGTH == message.get_data_length())
{
if (0 == lastServerStatusTimestamp_ms)
{
Expand All @@ -504,7 +503,7 @@ namespace isobus

case static_cast<std::uint8_t>(FileServerToClientMultiplexor::GetFileServerPropertiesResponse):
{
if (CAN_DATA_LENGTH == message->get_data_length())
if (CAN_DATA_LENGTH == message.get_data_length())
{
if (StateMachineState::WaitForGetFileServerPropertiesResponse == get_state())
{
Expand Down Expand Up @@ -532,7 +531,7 @@ namespace isobus

case static_cast<std::uint8_t>(FileServerToClientMultiplexor::ChangeCurrentDirectoryResponse):
{
if (CAN_DATA_LENGTH == message->get_data_length())
if (CAN_DATA_LENGTH == message.get_data_length())
{
if (StateMachineState::WaitForChangeToManufacturerDirectoryResponse == get_state())
{
Expand Down Expand Up @@ -574,7 +573,7 @@ namespace isobus

case static_cast<std::uint8_t>(FileServerToClientMultiplexor::OpenFileResponse):
{
if (CAN_DATA_LENGTH == message->get_data_length())
if (CAN_DATA_LENGTH == message.get_data_length())
{
bool foundMatchingFileInList = false;

Expand Down Expand Up @@ -615,7 +614,7 @@ namespace isobus

case static_cast<std::uint8_t>(FileServerToClientMultiplexor::CloseFileResponse):
{
if (CAN_DATA_LENGTH == message->get_data_length())
if (CAN_DATA_LENGTH == message.get_data_length())
{
bool foundMatchingFileInList = false;

Expand Down Expand Up @@ -655,7 +654,7 @@ namespace isobus

case static_cast<std::uint8_t>(FileServerToClientMultiplexor::WriteFileResponse):
{
if (CAN_DATA_LENGTH == message->get_data_length())
if (CAN_DATA_LENGTH == message.get_data_length())
{
bool foundMatchingFileInList = false;

Expand Down Expand Up @@ -726,11 +725,11 @@ namespace isobus
}
}

void FileServerClient::process_message(CANMessage *const message, void *parent)
void FileServerClient::process_message(const CANMessage &message, void *parent)
{
if (nullptr != parent)
{
reinterpret_cast<FileServerClient *>(parent)->process_message(message);
static_cast<FileServerClient *>(parent)->process_message(message);
}
}

Expand Down Expand Up @@ -805,9 +804,9 @@ namespace isobus
retVal = CANNetworkManager::CANNetwork.send_can_message(static_cast<std::uint32_t>(CANLibParameterGroupNumber::ClientToFileServer),
buffer.data(),
buffer.size(),
myControlFunction.get(),
partnerControlFunction.get(),
CANIdentifier::PriorityLowest7);
myControlFunction,
partnerControlFunction,
CANIdentifier::CANPriority::PriorityLowest7);
}
return retVal;
}
Expand All @@ -826,9 +825,9 @@ namespace isobus
return CANNetworkManager::CANNetwork.send_can_message(static_cast<std::uint32_t>(CANLibParameterGroupNumber::ClientToFileServer),
buffer.data(),
CAN_DATA_LENGTH,
myControlFunction.get(),
partnerControlFunction.get(),
CANIdentifier::PriorityLowest7);
myControlFunction,
partnerControlFunction,
CANIdentifier::CANPriority::PriorityLowest7);
}

bool FileServerClient::send_close_file(std::shared_ptr<FileInfo> fileMetadata) const
Expand All @@ -844,9 +843,9 @@ namespace isobus
return CANNetworkManager::CANNetwork.send_can_message(static_cast<std::uint32_t>(CANLibParameterGroupNumber::ClientToFileServer),
buffer.data(),
CAN_DATA_LENGTH,
myControlFunction.get(),
partnerControlFunction.get(),
CANIdentifier::PriorityLowest7);
myControlFunction,
partnerControlFunction,
CANIdentifier::CANPriority::PriorityLowest7);
}

bool FileServerClient::send_get_file_server_properties() const
Expand All @@ -863,9 +862,9 @@ namespace isobus
return CANNetworkManager::CANNetwork.send_can_message(static_cast<std::uint32_t>(CANLibParameterGroupNumber::ClientToFileServer),
buffer.data(),
CAN_DATA_LENGTH,
myControlFunction.get(),
partnerControlFunction.get(),
CANIdentifier::PriorityLowest7);
myControlFunction,
partnerControlFunction,
CANIdentifier::CANPriority::PriorityLowest7);
}

bool FileServerClient::send_open_file(std::shared_ptr<FileInfo> fileMetadata) const
Expand Down Expand Up @@ -896,19 +895,16 @@ namespace isobus
buffer[5 + i] = fileMetadata->fileName[i];
}

if (buffer.size() < CAN_DATA_LENGTH)
while (buffer.size() < CAN_DATA_LENGTH)
{
for (std::size_t i = buffer.size(); i < CAN_DATA_LENGTH; i++)
{
buffer[i] = 0xFF;
}
buffer.push_back(0xFF);
}
retVal = CANNetworkManager::CANNetwork.send_can_message(static_cast<std::uint32_t>(CANLibParameterGroupNumber::ClientToFileServer),
buffer.data(),
buffer.size(),
myControlFunction.get(),
partnerControlFunction.get(),
CANIdentifier::PriorityLowest7);
myControlFunction,
partnerControlFunction,
CANIdentifier::CANPriority::PriorityLowest7);
}
return retVal;
}
Expand All @@ -930,7 +926,7 @@ namespace isobus
if (nullptr != fileMetadata)
{
fileMetadata->state = state;
fileMetadata->timstamp_ms = SystemTiming::get_timestamp_ms();
fileMetadata->timestamp_ms = SystemTiming::get_timestamp_ms();
}
}

Expand All @@ -956,7 +952,7 @@ namespace isobus
{
set_file_state(file, FileState::WaitForOpenFileResponse);
}
else if (SystemTiming::time_expired_ms(file->timstamp_ms, GENERAL_OPERATION_TIMEOUT))
else if (SystemTiming::time_expired_ms(file->timestamp_ms, GENERAL_OPERATION_TIMEOUT))
{
CANStackLogger::error("[FS]: Timeout trying to send an open file message.");
set_file_state(file, FileState::FileOpenFailed);
Expand All @@ -966,7 +962,7 @@ namespace isobus

case FileState::WaitForOpenFileResponse:
{
if (SystemTiming::time_expired_ms(file->timstamp_ms, GENERAL_OPERATION_TIMEOUT))
if (SystemTiming::time_expired_ms(file->timestamp_ms, GENERAL_OPERATION_TIMEOUT))
{
CANStackLogger::error("[FS]: Timeout waiting to an open file response message.");
set_file_state(file, FileState::FileOpenFailed);
Expand All @@ -980,7 +976,7 @@ namespace isobus
{
set_file_state(file, FileState::WaitForCloseFileResponse);
}
else if (SystemTiming::time_expired_ms(file->timstamp_ms, GENERAL_OPERATION_TIMEOUT))
else if (SystemTiming::time_expired_ms(file->timestamp_ms, GENERAL_OPERATION_TIMEOUT))
{
CANStackLogger::error("[FS]: Timeout trying to send a close file message.");
}
Expand All @@ -989,7 +985,7 @@ namespace isobus

case FileState::WaitForCloseFileResponse:
{
if (SystemTiming::time_expired_ms(file->timstamp_ms, GENERAL_OPERATION_TIMEOUT))
if (SystemTiming::time_expired_ms(file->timestamp_ms, GENERAL_OPERATION_TIMEOUT))
{
// todo
}
Expand Down
Loading

0 comments on commit bacdaac

Please sign in to comment.