Skip to content

Commit

Permalink
added support for 'COMMAND_BEGIN_SOFTWARE_UPDATE'
Browse files Browse the repository at this point in the history
  • Loading branch information
serges147 committed Jan 30, 2025
1 parent 494299a commit 3fca5a1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
[submodule "submodules/libcyphal"]
path = submodules/libcyphal
url = https://github.com/OpenCyphal-Garage/libcyphal.git
branch = sshirokov/416_tid
14 changes: 8 additions & 6 deletions libcyphal_demo/src/exec_cmd_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#ifndef COMMAND_PROVIDER_HPP_INCLUDED
#define COMMAND_PROVIDER_HPP_INCLUDED

#include "libcyphal/application/node.hpp"
#include "libcyphal/presentation/presentation.hpp"
#include "libcyphal/presentation/server.hpp"
#include "libcyphal/types.hpp"
Expand All @@ -28,9 +29,10 @@
template <typename Derived>
class ExecCmdProvider // NOSONAR cpp:S4963
{
public:
using Service = uavcan::node::ExecuteCommand_1_3;
using Server = libcyphal::presentation::ServiceServer<Service>;

public:
/// Defines the response type for the ExecuteCommand provider.
///
using Response = Service::Response;
Expand All @@ -48,10 +50,11 @@ class ExecCmdProvider // NOSONAR cpp:S4963

/// Factory method to create a ExecuteCommand instance.
///
/// @param node The application layer node instance. In use to access heartbeat producer.
/// @param presentation The presentation layer instance. In use to create 'ExecuteCommand' service server.
/// @return The ExecuteCommand provider instance or a failure.
///
static auto make(libcyphal::presentation::Presentation& presentation)
static auto make(libcyphal::application::Node& node, libcyphal::presentation::Presentation& presentation)
-> libcyphal::Expected<Derived, libcyphal::presentation::Presentation::MakeFailure>
{
auto maybe_srv = presentation.makeServer<Service>();
Expand All @@ -60,7 +63,7 @@ class ExecCmdProvider // NOSONAR cpp:S4963
return std::move(*failure);
}

return Derived{presentation, cetl::get<Server>(std::move(maybe_srv))};
return Derived{node, presentation, cetl::get<Server>(std::move(maybe_srv))};
}

ExecCmdProvider(ExecCmdProvider&& other) noexcept
Expand Down Expand Up @@ -108,9 +111,7 @@ class ExecCmdProvider // NOSONAR cpp:S4963
return false;
}

private:
using Server = libcyphal::presentation::ServiceServer<Service>;

protected:
ExecCmdProvider(const libcyphal::presentation::Presentation& presentation, Server&& server)
: alloc_{&presentation.memory()}
, server_{std::move(server)}
Expand All @@ -119,6 +120,7 @@ class ExecCmdProvider // NOSONAR cpp:S4963
setupOnRequestCallback();
}

private:
void setupOnRequestCallback()
{
server_.setOnRequestCallback([this](const auto& arg, auto continuation) {
Expand Down
29 changes: 22 additions & 7 deletions libcyphal_demo/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
#include <libcyphal/transport/transport.hpp>
#include <libcyphal/types.hpp>

#include <uavcan/node/Health_1_0.hpp>
#include <uavcan/node/Mode_1_0.hpp>

#include <algorithm>
#include <array>
#include <chrono>
#include <cstdint>
#include <cstring>
#include <ios>
#include <iomanip>
#include <ios>
#include <iostream>
#include <unistd.h> // execve
#include <utility>
Expand All @@ -36,7 +38,13 @@ namespace
class AppExecCmdProvider final : public ExecCmdProvider<AppExecCmdProvider>
{
public:
using ExecCmdProvider::ExecCmdProvider;
AppExecCmdProvider(libcyphal::application::Node& node,
const libcyphal::presentation::Presentation& presentation,
Server&& server)
: ExecCmdProvider{presentation, std::move(server)}
, node_{node}
{
}

bool should_break() const noexcept
{
Expand Down Expand Up @@ -80,14 +88,21 @@ class AppExecCmdProvider final : public ExecCmdProvider<AppExecCmdProvider>
restart_required_ = true;
break;

case Request::COMMAND_BEGIN_SOFTWARE_UPDATE:
//
std::cout << "🚧 COMMAND_BEGIN_SOFTWARE_UPDATE (file='" << parameter << "')\n";
node_.heartbeatProducer().message().mode.value = uavcan::node::Mode_1_0::SOFTWARE_UPDATE;
break;

default:
return ExecCmdProvider::onCommand(command, parameter, response);
}
return true;
}

bool should_power_off_{false};
bool restart_required_{false};
libcyphal::application::Node& node_;
bool should_power_off_{false};
bool restart_required_{false};

}; // AppExecCmdProvider

Expand Down Expand Up @@ -196,8 +211,8 @@ libcyphal::Expected<bool, ExitCode> run_application(const char* const root_path)

// 6. Bring up the command execution provider.
//
auto maybe_exec_cmd_provider = AppExecCmdProvider::make(presentation);
if (const auto* failure = cetl::get_if<libcyphal::application::Node::MakeFailure>(&maybe_node))
auto maybe_exec_cmd_provider = AppExecCmdProvider::make(node, presentation);
if (const auto* failure = cetl::get_if<libcyphal::application::Node::MakeFailure>(&maybe_exec_cmd_provider))
{
std::cerr << "❌ Failed to create exec cmd provider.\n";
return ExitCode::ExecCmdProviderCreationFailure;
Expand Down

0 comments on commit 3fca5a1

Please sign in to comment.