Skip to content

Commit b0ebbb3

Browse files
committed
impl handleRouteChannelEnd
1 parent cc119cb commit b0ebbb3

File tree

10 files changed

+122
-67
lines changed

10 files changed

+122
-67
lines changed

src/common/dsdl/ocvsmd/common/ipc/Route.1.0.dsdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@union
22

33
uavcan.primitive.Empty.1.0 empty
4-
RouteConnect.1.0 connect
4+
RouteConnect.0.1 connect
55
RouteChannelMsg.1.0 channel_msg
66
RouteChannelEnd.1.0 channel_end
77

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
uavcan.node.Version.1.0 version
2+
int32 error_code
3+
4+
@extent 64

src/common/dsdl/ocvsmd/common/ipc/RouteConnect.1.0.dsdl

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/common/ipc/client_router.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include "ocvsmd/common/ipc/RouteChannelEnd_1_0.hpp"
1414
#include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp"
15-
#include "ocvsmd/common/ipc/RouteConnect_1_0.hpp"
15+
#include "ocvsmd/common/ipc/RouteConnect_0_1.hpp"
1616
#include "ocvsmd/common/ipc/Route_1_0.hpp"
1717
#include "uavcan/primitive/Empty_1_0.hpp"
1818

@@ -133,7 +133,7 @@ class ClientRouterImpl final : public ClientRouter
133133
, endpoint_{endpoint}
134134
, next_sequence_{0}
135135
{
136-
::syslog(LOG_DEBUG, "Gateway(tag=%zu).", endpoint.getTag());
136+
::syslog(LOG_DEBUG, "Gateway(tag=%zu).", endpoint.getTag()); // NOLINT
137137
}
138138

139139
GatewayImpl(const GatewayImpl&) = delete;
@@ -143,7 +143,7 @@ class ClientRouterImpl final : public ClientRouter
143143

144144
~GatewayImpl()
145145
{
146-
::syslog(LOG_DEBUG, "~Gateway(tag=%zu, seq=%zu).", endpoint_.getTag(), next_sequence_);
146+
::syslog(LOG_DEBUG, "~Gateway(tag=%zu, seq=%zu).", endpoint_.getTag(), next_sequence_); // NOLINT
147147

148148
// `next_sequence_ == 0` means that this gateway was never used for sending messages,
149149
// and so remote router never knew about it (its tag) - no need to post "ChEnd" event.
@@ -278,24 +278,27 @@ class ClientRouterImpl final : public ClientRouter
278278
channel_end.tag = endpoint.getTag();
279279
channel_end.error_code = 0; // No error b/c it's a normal channel completion.
280280

281-
const int result = tryPerformOnSerialized(route, [this](const auto payload) {
281+
const int error = tryPerformOnSerialized(route, [this](const auto payload) {
282282
//
283283
return client_pipe_->send({{payload}});
284284
});
285285
// Best efforts strategy - gateway anyway is gone, so nowhere to report.
286-
(void) result;
286+
(void) error;
287287
}
288288
}
289289

290290
CETL_NODISCARD int handlePipeEvent(const pipe::ClientPipe::Event::Connected) const
291291
{
292-
// TODO: log client pipe connection
292+
::syslog(LOG_DEBUG, "Pipe is connected."); // NOLINT
293293

294+
// It's not enough to consider the server route connected by the pipe event.
295+
// We gonna initiate `RouteConnect` negotiation (see `handleRouteConnect`).
296+
//
294297
Route_1_0 route{&memory_};
295298
auto& route_conn = route.set_connect();
296299
route_conn.version.major = VERSION_MAJOR;
297300
route_conn.version.minor = VERSION_MINOR;
298-
301+
//
299302
return tryPerformOnSerialized(route, [this](const auto payload) {
300303
//
301304
return client_pipe_->send({{payload}});
@@ -317,7 +320,7 @@ class ClientRouterImpl final : public ClientRouter
317320
cetl::visit(cetl::make_overloaded(
318321
//
319322
[this](const uavcan::primitive::Empty_1_0&) {},
320-
[this](const RouteConnect_1_0& route_conn) {
323+
[this](const RouteConnect_0_1& route_conn) {
321324
//
322325
handleRouteConnect(route_conn);
323326
},
@@ -336,6 +339,8 @@ class ClientRouterImpl final : public ClientRouter
336339

337340
CETL_NODISCARD int handlePipeEvent(const pipe::ClientPipe::Event::Disconnected)
338341
{
342+
::syslog(LOG_DEBUG, "Pipe is disconnected."); // NOLINT
343+
339344
if (is_connected_)
340345
{
341346
is_connected_ = false;
@@ -356,8 +361,14 @@ class ClientRouterImpl final : public ClientRouter
356361
return 0;
357362
}
358363

359-
void handleRouteConnect(const RouteConnect_1_0&)
364+
void handleRouteConnect(const RouteConnect_0_1& rt_conn)
360365
{
366+
::syslog(LOG_DEBUG, // NOLINT
367+
"Route connect response (ver='%d.%d', err=%d).",
368+
static_cast<int>(rt_conn.version.major),
369+
static_cast<int>(rt_conn.version.minor),
370+
static_cast<int>(rt_conn.error_code));
371+
361372
if (!is_connected_)
362373
{
363374
is_connected_ = true;
@@ -391,6 +402,8 @@ class ClientRouterImpl final : public ClientRouter
391402

392403
void handleRouteChannelEnd(const RouteChannelEnd_1_0& route_ch_end)
393404
{
405+
::syslog(LOG_DEBUG, "Route Ch End (tag=%zu, err=%d).", route_ch_end.tag, route_ch_end.error_code); // NOLINT
406+
394407
const Endpoint endpoint{route_ch_end.tag};
395408
const auto error_code = static_cast<ErrorCode>(route_ch_end.error_code);
396409

src/common/ipc/server_router.cpp

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "pipe/server_pipe.hpp"
1212

1313
#include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp"
14-
#include "ocvsmd/common/ipc/RouteConnect_1_0.hpp"
14+
#include "ocvsmd/common/ipc/RouteConnect_0_1.hpp"
1515
#include "ocvsmd/common/ipc/Route_1_0.hpp"
1616
#include "uavcan/primitive/Empty_1_0.hpp"
1717

@@ -137,7 +137,7 @@ class ServerRouterImpl final : public ServerRouter
137137
, endpoint_{endpoint}
138138
, next_sequence_{0}
139139
{
140-
::syslog(LOG_DEBUG, "Gateway(cl=%zu, tag=%zu).", endpoint.getClientId(), endpoint.getTag());
140+
::syslog(LOG_DEBUG, "Gateway(cl=%zu, tag=%zu).", endpoint.getClientId(), endpoint.getTag()); // NOLINT
141141
}
142142

143143
GatewayImpl(const GatewayImpl&) = delete;
@@ -147,7 +147,7 @@ class ServerRouterImpl final : public ServerRouter
147147

148148
~GatewayImpl()
149149
{
150-
::syslog(LOG_DEBUG, "~Gateway(cl=%zu, tag=%zu).", endpoint_.getClientId(), endpoint_.getTag());
150+
::syslog(LOG_DEBUG, "~Gateway(cl=%zu, tag=%zu).", endpoint_.getClientId(), endpoint_.getTag()); // NOLINT
151151

152152
router_.onGatewayDisposal(endpoint_);
153153
}
@@ -258,18 +258,22 @@ class ServerRouterImpl final : public ServerRouter
258258
channel_end.tag = endpoint.getTag();
259259
channel_end.error_code = 0; // No error b/c it's a normal channel completion.
260260

261-
const int result = tryPerformOnSerialized(route, [this, &endpoint](const auto payload) {
261+
const int error = tryPerformOnSerialized(route, [this, &endpoint](const auto payload) {
262262
//
263263
return server_pipe_->send(endpoint.getClientId(), {{payload}});
264264
});
265265
// Best efforts strategy - gateway anyway is gone, so nowhere to report.
266-
(void) result;
266+
(void) error;
267267
}
268268
}
269269

270-
CETL_NODISCARD int handlePipeEvent(const pipe::ServerPipe::Event::Connected conn)
270+
CETL_NODISCARD static int handlePipeEvent(const pipe::ServerPipe::Event::Connected& pipe_conn)
271271
{
272-
connected_client_ids_.insert(conn.client_id);
272+
::syslog(LOG_DEBUG, "Pipe is connected (cl=%zu).", pipe_conn.client_id); // NOLINT
273+
274+
// It's not enough to consider the client router connected by the pipe event.
275+
// We gonna wait for `RouteConnect` negotiation (see `handleRouteConnect`).
276+
// But for now everything is fine.
273277
return 0;
274278
}
275279

@@ -288,7 +292,7 @@ class ServerRouterImpl final : public ServerRouter
288292
cetl::visit(cetl::make_overloaded(
289293
//
290294
[this](const uavcan::primitive::Empty_1_0&) {},
291-
[this, &msg](const RouteConnect_1_0& route_conn) {
295+
[this, &msg](const RouteConnect_0_1& route_conn) {
292296
//
293297
handleRouteConnect(msg.client_id, route_conn);
294298
},
@@ -307,27 +311,39 @@ class ServerRouterImpl final : public ServerRouter
307311
return 0;
308312
}
309313

310-
CETL_NODISCARD static int handlePipeEvent(const pipe::ServerPipe::Event::Disconnected)
314+
CETL_NODISCARD static int handlePipeEvent(const pipe::ServerPipe::Event::Disconnected& disconn)
311315
{
316+
::syslog(LOG_DEBUG, "Pipe is disconnected (cl=%zu).", disconn.client_id); // NOLINT
317+
312318
// TODO: Implement! disconnected for all gateways which belong to the corresponding client id
313319
return 0;
314320
}
315321

316-
void handleRouteConnect(const pipe::ServerPipe::ClientId client_id, const RouteConnect_1_0&) const
322+
void handleRouteConnect(const pipe::ServerPipe::ClientId client_id, const RouteConnect_0_1& rt_conn)
317323
{
318-
// TODO: log client route connection
324+
::syslog(LOG_DEBUG, // NOLINT
325+
"Route connect request (cl=%zu, ver='%d.%d', err=%d).",
326+
client_id,
327+
static_cast<int>(rt_conn.version.major),
328+
static_cast<int>(rt_conn.version.minor),
329+
static_cast<int>(rt_conn.error_code));
319330

320331
Route_1_0 route{&memory_};
321-
322-
auto& route_conn = route.set_connect();
332+
auto& route_conn = route.set_connect();
323333
route_conn.version.major = VERSION_MAJOR;
324334
route_conn.version.minor = VERSION_MINOR;
325-
326-
const int result = tryPerformOnSerialized(route, [this, client_id](const auto payload) {
335+
// In the future, we might have version comparison logic here,
336+
// and potentially refuse the connection if the versions are incompatible.
337+
route_conn.error_code = 0;
338+
//
339+
const int error = tryPerformOnSerialized(route, [this, client_id](const auto payload) {
327340
//
328341
return server_pipe_->send(client_id, {{payload}});
329342
});
330-
(void) result;
343+
if (0 != error)
344+
{
345+
connected_client_ids_.insert(client_id);
346+
}
331347
}
332348

333349
void handleRouteChannelMsg(const pipe::ServerPipe::ClientId client_id,
@@ -362,7 +378,19 @@ class ServerRouterImpl final : public ServerRouter
362378
// TODO: log unsolicited message
363379
}
364380

365-
void handleRouteChannelEnd(const pipe::ServerPipe::ClientId client_id, const RouteChannelEnd_1_0& route_ch_end) {}
381+
void handleRouteChannelEnd(const pipe::ServerPipe::ClientId client_id, const RouteChannelEnd_1_0& route_ch_end)
382+
{
383+
::syslog(LOG_DEBUG, "Route Ch End (tag=%zu, err=%d).", route_ch_end.tag, route_ch_end.error_code); // NOLINT
384+
385+
const Endpoint endpoint{route_ch_end.tag, client_id};
386+
const auto error_code = static_cast<ErrorCode>(route_ch_end.error_code);
387+
388+
findAndActOnRegisteredGateway(endpoint, [this, error_code](auto& gateway, auto found_it) {
389+
//
390+
endpoint_to_gateway_.erase(found_it);
391+
gateway.event(detail::Gateway::Event::Completed{error_code});
392+
});
393+
}
366394

367395
cetl::pmr::memory_resource& memory_;
368396
pipe::ServerPipe::Ptr server_pipe_;

src/daemon/engine/application.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ cetl::optional<std::string> Application::init()
7878
ipc_exec_cmd_ch_ = std::move(ch);
7979
ipc_exec_cmd_ch_->subscribe([this](const auto&) {
8080
//
81-
::syslog(LOG_DEBUG, "Client nested msg");
82-
ExecCmd r1{&memory_};
83-
const int res1 = ipc_exec_cmd_ch_->send(r1);
81+
::syslog(LOG_DEBUG, "Client nested msg"); // NOLINT
82+
const ExecCmd r1{&memory_};
83+
const int res1 = ipc_exec_cmd_ch_->send(r1);
8484
(void) res1;
8585
ipc_exec_cmd_ch_.reset();
8686
});

src/sdk/daemon.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class DaemonImpl final : public Daemon
7474
[](const ExecCmdChannel::Completed& completed) {
7575
//
7676
// NOLINTNEXTLINE *-vararg
77-
::syslog(LOG_DEBUG, "🔴 Ch Completed (err=%d).", completed.error_code);
77+
::syslog(LOG_DEBUG, "🔴 Ch Completed (err=%d).", static_cast<int>(completed.error_code));
7878
}),
7979
event_var);
8080
});
@@ -100,7 +100,7 @@ CETL_NODISCARD std::unique_ptr<Daemon> Daemon::make( //
100100
libcyphal::IExecutor& executor)
101101
{
102102
auto daemon = std::make_unique<DaemonImpl>(memory, executor);
103-
if (daemon->start())
103+
if (0 != daemon->start())
104104
{
105105
return nullptr;
106106
}

test/common/ipc/ipc_gtest_helpers.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#include "ocvsmd/common/ipc/RouteChannelEnd_1_0.hpp"
1313
#include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp"
14-
#include "ocvsmd/common/ipc/RouteConnect_1_0.hpp"
14+
#include "ocvsmd/common/ipc/RouteConnect_0_1.hpp"
1515
#include "ocvsmd/common/ipc/Route_1_0.hpp"
1616

1717
#include <uavcan/node/Version_1_0.hpp>
@@ -49,9 +49,9 @@ inline void PrintTo(const uavcan::node::Version_1_0& ver, std::ostream* os)
4949
*os << "Version_1_0{'" << static_cast<int>(ver.major) << "." << static_cast<int>(ver.minor) << "'}";
5050
}
5151

52-
inline void PrintTo(const RouteConnect_1_0& conn, std::ostream* os)
52+
inline void PrintTo(const RouteConnect_0_1& conn, std::ostream* os)
5353
{
54-
*os << "RouteConnect_1_0{ver=";
54+
*os << "RouteConnect_0_1{ver=";
5555
PrintTo(conn.version, os);
5656
*os << "}";
5757
}
@@ -76,7 +76,7 @@ inline void PrintTo(const Route_1_0& route, std::ostream* os)
7676

7777
// MARK: - Equitable-s for matching:
7878

79-
inline bool operator==(const RouteConnect_1_0& lhs, const RouteConnect_1_0& rhs)
79+
inline bool operator==(const RouteConnect_0_1& lhs, const RouteConnect_0_1& rhs)
8080
{
8181
return lhs.version.major == rhs.version.major && lhs.version.minor == rhs.version.minor;
8282
}
@@ -165,11 +165,12 @@ testing::PolymorphicMatcher<PayloadMatcher<T>> PayloadWith(
165165
}
166166

167167
inline auto PayloadOfRouteConnect(cetl::pmr::memory_resource& mr,
168-
const std::uint8_t ver_major = VERSION_MAJOR,
169-
const std::uint8_t ver_minor = VERSION_MINOR)
168+
const std::uint8_t ver_major = VERSION_MAJOR,
169+
const std::uint8_t ver_minor = VERSION_MINOR,
170+
ErrorCode error_code = ErrorCode::Success)
170171
{
171-
const RouteConnect_1_0 connect{{ver_major, ver_minor, &mr}, &mr};
172-
return PayloadWith<Route_1_0>(testing::VariantWith<RouteConnect_1_0>(connect), mr);
172+
const RouteConnect_0_1 route_conn{{ver_major, ver_minor, &mr}, static_cast<std::int32_t>(error_code), &mr};
173+
return PayloadWith<Route_1_0>(testing::VariantWith<RouteConnect_0_1>(route_conn), mr);
173174
}
174175

175176
template <typename Msg>

0 commit comments

Comments
 (0)