Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ad3154 committed Jun 30, 2024
1 parent bacdaac commit 5bbc362
Show file tree
Hide file tree
Showing 4 changed files with 700 additions and 186 deletions.
86 changes: 76 additions & 10 deletions examples/file_server_client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ void signal_handler(int)
running = false;
}

void update_CAN_network(void *)
{
isobus::CANNetworkManager::CANNetwork.update();
}

enum class ExampleStateMachineState
{
OpenFile,
WaitForFileToBeOpen,
WriteFile,
WaitForFileWrite,
CloseFile,
GetVolumeInfo,
WaitForVolumeInfo,
ChangeToRoot,
OpenListOfVolumes,
WaitForOpenListOfVolumes,
ReadListOfVolumes,
ExampleComplete
};

Expand All @@ -50,6 +51,8 @@ int main()
canDriver = std::make_shared<isobus::InnoMakerUSB2CANWindowsPlugin>(0); // CAN0
#elif defined(ISOBUS_MACCANPCAN_AVAILABLE)
canDriver = std::make_shared<isobus::MacCANPCANPlugin>(PCAN_USBBUS1);
#elif defined(ISOBUS_SYS_TEC_AVAILABLE)
canDriver = std::make_shared<isobus::SysTecWindowsPlugin>();
#endif

if (nullptr == canDriver)
Expand Down Expand Up @@ -97,21 +100,24 @@ int main()
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/Open-Agriculture/AgIsoStack-plus-plus";
bool volumeStatusReceived = false;

while (running)
{
// A little state machine to run our example.
// Most functions on FS client interface are async, and can take a variable amount of time to complete, so
// you will need to have some kind of stateful wrapper to manage your file operations.
// This is essentially unavoidable, as interacting with files over the bus is a fairly involved process.
// This is essentially unavoidable, as interacting with files over the bus is a fairly involved, slow process.
//
// This state machine demonstrates a bunch of different kinds of operations, and you may not need them all for your application.
switch (state)
{
// Let's open a file
case ExampleStateMachineState::OpenFile:
{
if (TestFileServerClient->open_file(fileNameToUse, true, true, isobus::FileServerClient::FileOpenMode::OpenFileForReadingAndWriting, isobus::FileServerClient::FilePointerMode::AppendMode))
{
state = ExampleStateMachineState::WaitForFileToBeOpen;
isobus::CANStackLogger::info("[Example]: Opening File");
}
}
break;
Expand All @@ -124,15 +130,17 @@ int main()
if (isobus::FileServerClient::INVALID_FILE_HANDLE != fileHandle)
{
state = ExampleStateMachineState::WriteFile;
isobus::CANStackLogger::info("[Example]: File is open");
}
}
break;

case ExampleStateMachineState::WriteFile:
{
if (TestFileServerClient->write_file(fileHandle, reinterpret_cast<const std::uint8_t *>(fileExampleContents.data()), fileExampleContents.size()))
if (TestFileServerClient->write_file(fileHandle, reinterpret_cast<const std::uint8_t *>(fileExampleContents.data()), fileExampleContents.size(), nullptr))
{
state = ExampleStateMachineState::WaitForFileWrite;
isobus::CANStackLogger::info("[Example]: Writing file");
}
}
break;
Expand All @@ -141,8 +149,9 @@ int main()
{
if (isobus::FileServerClient::FileState::FileOpen == TestFileServerClient->get_file_state(fileHandle))
{
// If the file is back in the open state, then writing is done
// If the file is back in the open state, then writing is done. This can be checked instead of waiting for a callback if you want.
state = ExampleStateMachineState::CloseFile;
isobus::CANStackLogger::info("[Example]: Write complete. Closing file.");
}
}
break;
Expand All @@ -152,7 +161,64 @@ int main()
{
if (TestFileServerClient->close_file(TestFileServerClient->get_file_handle(fileNameToUse)))
{
state = ExampleStateMachineState::ExampleComplete;
state = ExampleStateMachineState::GetVolumeInfo;
isobus::CANStackLogger::info("[Example]: File Closed.");
}
}
break;

// You don't really need to query the volume info if you don't want to. We do it
// here just to show how to do it. It's helpful if you want to see if you're dealing with a USB drive vs on-board disk or something.
// Just be aware that you'll get different results depending on your current directory if you don't request a specific volume name.
case ExampleStateMachineState::GetVolumeInfo:
{
TestFileServerClient->get_volume_status_event_dispatcher().add_listener([&volumeStatusReceived](isobus::FileServerClient::VolumeStatusInfo status) { volumeStatusReceived = true; });
if (TestFileServerClient->request_current_volume_status("")) // A blank volume name requests the volume of our "current directory"
{
isobus::CANStackLogger::info("[Example]: Requesting current volume information");
state = ExampleStateMachineState::WaitForVolumeInfo;
}
}
break;

case ExampleStateMachineState::WaitForVolumeInfo:
{
if (volumeStatusReceived)
{
isobus::CANStackLogger::info("[Example]: Done.");
state = ExampleStateMachineState::ChangeToRoot;
}
}
break;

case ExampleStateMachineState::ChangeToRoot:
{
if (TestFileServerClient->change_directory(std::string("\\\\")))
{
isobus::CANStackLogger::info("[Example]: Changing to the root directory.");
state = ExampleStateMachineState::OpenListOfVolumes;
}
}
break;

case ExampleStateMachineState::OpenListOfVolumes:
{
if (TestFileServerClient->open_file(std::string("."), false, false, isobus::FileServerClient::FileOpenMode::OpenDirectory, isobus::FileServerClient::FilePointerMode::RandomAccess))
{
isobus::CANStackLogger::info("[Example]: Requesting volume list.");
state = ExampleStateMachineState::WaitForOpenListOfVolumes;
}
}
break;

case ExampleStateMachineState::WaitForOpenListOfVolumes:
{
// When we get a valid file handle, that means the directory has been opened and is ready to be interacted with
fileHandle = TestFileServerClient->get_file_handle(".");
if (isobus::FileServerClient::INVALID_FILE_HANDLE != fileHandle)
{
TestFileServerClient->read_file(fileHandle, 2048, nullptr);
state = ExampleStateMachineState::ExampleComplete; // todo wait
}
}
break;
Expand Down
Loading

0 comments on commit 5bbc362

Please sign in to comment.