-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathengine.cpp
More file actions
214 lines (186 loc) · 6.55 KB
/
engine.cpp
File metadata and controls
214 lines (186 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
//
#include "engine.hpp"
#include "config.hpp"
#include "cyphal/can_transport_bag.hpp"
#include "cyphal/file_provider.hpp"
#include "cyphal/udp_transport_bag.hpp"
#include "engine_helpers.hpp"
#include "io/socket_address.hpp"
#include "ipc/pipe/server_pipe.hpp"
#include "ipc/pipe/socket_server.hpp"
#include "ipc/server_router.hpp"
#include "svc/file_server/services.hpp"
#include "svc/node/services.hpp"
#include "svc/svc_helpers.hpp"
#include <cetl/pf17/cetlpf.hpp>
#include <libcyphal/application/node.hpp>
#include <libcyphal/types.hpp>
#include <spdlog/spdlog.h>
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <functional>
#include <limits>
#include <memory>
#include <random>
#include <string>
#include <utility>
namespace ocvsmd
{
namespace daemon
{
namespace engine
{
Engine::Engine(Config::Ptr config)
: config_{std::move(config)}
{
}
cetl::optional<std::string> Engine::init()
{
logger_->trace("Initializing engine...");
// 1. Create the transport layer object (try first UDP, then CAN).
// Set the local node ID if configured.
//
if (auto maybe_udp_transport_bag = cyphal::UdpTransportBag::make(memory_, executor_, config_))
{
any_transport_bag_ = std::move(maybe_udp_transport_bag);
}
else
{
#ifdef __linux__
if (auto maybe_can_transport_bag = cyphal::CanTransportBag::make(memory_, executor_, config_))
{
any_transport_bag_ = std::move(maybe_can_transport_bag);
}
else
#endif // __linux__
{
std::string msg = "Failed to create Cyphal transport.";
logger_->error(msg);
return msg;
}
}
if (const auto node_id = config_->getCyphalAppNodeId())
{
any_transport_bag_->getTransport().setLocalNodeId(node_id.value());
}
// 2. Create the presentation layer object.
//
presentation_.emplace(memory_, executor_, any_transport_bag_->getTransport());
presentation_->setTransferIdMap(&transfer_id_map_);
// 3. Create the node object with name.
//
auto maybe_node = libcyphal::application::Node::make(*presentation_);
if (const auto* failure = cetl::get_if<libcyphal::application::Node::MakeFailure>(&maybe_node))
{
(void) failure;
std::string msg = "Failed to create cyphal node.";
logger_->error(msg);
return msg;
}
node_.emplace(cetl::get<libcyphal::application::Node>(std::move(maybe_node)));
// 4. Populate the node info.
//
auto& get_info_prov = node_->getInfoProvider();
get_info_prov //
.setName(NODE_NAME)
.setSoftwareVersion(VERSION_MAJOR, VERSION_MINOR)
.setSoftwareVcsRevisionId(VCS_REVISION_ID)
.setUniqueId(getUniqueId());
// 5. Bring up various providers.
//
file_provider_ = cyphal::FileProvider::make(memory_, *presentation_, config_);
if (file_provider_ == nullptr)
{
std::string msg = "Failed to create cyphal file provider.";
logger_->error(msg);
return msg;
}
// 6. Bring up the IPC router and its services.
//
common::ipc::pipe::ServerPipe::Ptr server_pipe;
{
using ParseResult = common::io::SocketAddress::ParseResult;
auto ipc_connections = config_->getIpcConnections();
if (ipc_connections.empty())
{
std::string msg = "No IPC connections configured.";
logger_->error(msg);
return msg;
}
logger_->debug("Starting with IPC connection '{}'...", ipc_connections.front());
auto maybe_socket_address = common::io::SocketAddress::parse(ipc_connections.front(), 0);
if (const auto* const failure = cetl::get_if<ParseResult::Failure>(&maybe_socket_address))
{
(void) failure;
std::string msg = "Failed to parse IPC connection.";
logger_->error(msg);
return msg;
}
const auto socket_address = cetl::get<ParseResult::Success>(maybe_socket_address);
server_pipe = std::make_unique<common::ipc::pipe::SocketServer>(executor_, socket_address);
}
//
ipc_router_ = common::ipc::ServerRouter::make(memory_, std::move(server_pipe));
//
const svc::ScvContext svc_context{memory_, executor_, *ipc_router_, *presentation_};
svc::node::registerAllServices(svc_context);
svc::file_server::registerAllServices(svc_context, *file_provider_);
//
if (0 != ipc_router_->start())
{
std::string msg = "Failed to start IPC router.";
logger_->error(msg);
return msg;
}
logger_->debug("Engine is initialized.");
return cetl::nullopt;
}
void Engine::runWhile(const std::function<bool()>& loop_predicate)
{
using std::chrono_literals::operator""s;
libcyphal::Duration worst_lateness{0};
while (loop_predicate())
{
const auto spin_result = executor_.spinOnce();
worst_lateness = std::max(worst_lateness, spin_result.worst_lateness);
// Poll awaitable resources but awake at least once per second.
libcyphal::Duration timeout{1s};
if (spin_result.next_exec_time.has_value())
{
timeout = std::min(timeout, spin_result.next_exec_time.value() - executor_.now());
}
if (const auto poll_failure = executor_.pollAwaitableResourcesFor(cetl::make_optional(timeout)))
{
spdlog::warn("Failed to poll awaitable resources (err={}).", failureToErrorCode(*poll_failure));
}
}
spdlog::debug("Run loop predicate is fulfilled (worst_lateness={}us).",
std::chrono::duration_cast<std::chrono::microseconds>(worst_lateness).count());
}
Engine::UniqueId Engine::getUniqueId() const
{
if (const auto unique_id = config_->getCyphalAppUniqueId())
{
return unique_id.value();
}
UniqueId out_unique_id = {};
std::random_device rd; // Seed for the random number engine
std::mt19937 gen{rd()}; // Mersenne Twister engine
std::uniform_int_distribution<std::uint8_t> dis{std::numeric_limits<std::uint8_t>::min(),
std::numeric_limits<std::uint8_t>::max()};
// Populate the default; it is only used at the first run.
for (auto& b : out_unique_id)
{
b = dis(gen);
}
config_->setCyphalAppUniqueId(out_unique_id);
config_->save();
return out_unique_id;
}
} // namespace engine
} // namespace daemon
} // namespace ocvsmd