55
66#include " client_router.hpp"
77
8+ #include " dsdl_helpers.hpp"
89#include " gateway.hpp"
910#include " pipe/client_pipe.hpp"
1011
12+ #include " ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp"
13+ #include " ocvsmd/common/ipc/RouteConnect_1_0.hpp"
14+ #include " ocvsmd/common/ipc/Route_1_0.hpp"
15+ #include " uavcan/primitive/Empty_1_0.hpp"
16+
1117#include < cetl/cetl.hpp>
18+ #include < cetl/pf17/cetlpf.hpp>
19+ #include < cetl/visit_helpers.hpp>
1220
21+ #include < cerrno>
1322#include < cstdint>
1423#include < memory>
1524#include < unordered_map>
@@ -27,15 +36,34 @@ namespace
2736class ClientRouterImpl final : public ClientRouter
2837{
2938public:
30- explicit ClientRouterImpl (pipe::ClientPipe::Ptr client_pipe)
31- : client_pipe_{std::move (client_pipe)}
39+ ClientRouterImpl (cetl::pmr::memory_resource& memory, pipe::ClientPipe::Ptr client_pipe)
40+ : memory_{memory}
41+ , client_pipe_{std::move (client_pipe)}
3242 , next_tag_{0 }
3343 {
3444 CETL_DEBUG_ASSERT (client_pipe_, " " );
3545 }
3646
3747 // ClientRouter
3848
49+ cetl::pmr::memory_resource& memory () override
50+ {
51+ return memory_;
52+ }
53+
54+ void start () override
55+ {
56+ client_pipe_->start ([this ](const auto & pipe_event_var) {
57+ //
58+ return cetl::visit (
59+ [this ](const auto & pipe_event) {
60+ //
61+ return handlePipeEvent (pipe_event);
62+ },
63+ pipe_event_var);
64+ });
65+ }
66+
3967 CETL_NODISCARD detail::Gateway::Ptr makeGateway () override
4068 {
4169 const Tag new_tag = ++next_tag_;
@@ -109,6 +137,73 @@ class ClientRouterImpl final : public ClientRouter
109137
110138 }; // GatewayImpl
111139
140+ int handlePipeEvent (const pipe::ClientPipe::Event::Connected)
141+ {
142+ Route_1_0 route{&memory_};
143+ auto & route_conn = route.set_connect ();
144+ route_conn.version .major = VERSION_MAJOR;
145+ route_conn.version .minor = VERSION_MINOR;
146+
147+ return tryPerformOnSerialized<Route_1_0>(route, [this ](const auto payload) {
148+ //
149+ return client_pipe_->sendMessage (payload);
150+ });
151+ }
152+
153+ int handlePipeEvent (const pipe::ClientPipe::Event::Message& msg)
154+ {
155+ Route_1_0 route_msg{&memory_};
156+ const auto result_size = tryDeserializePayload (msg.payload , route_msg);
157+ if (!result_size.has_value ())
158+ {
159+ return EINVAL;
160+ }
161+
162+ const auto remaining_payload = msg.payload .subspan (result_size.value ());
163+
164+ cetl::visit (cetl::make_overloaded (
165+ //
166+ [this ](const uavcan::primitive::Empty_1_0&) {},
167+ [this ](const RouteConnect_1_0& route_conn) {
168+ //
169+ handleRouteConnect (route_conn);
170+ },
171+ [this , remaining_payload](const RouteChannelMsg_1_0& route_channel) {
172+ //
173+ handleRouteChannelMsg (route_channel, remaining_payload);
174+ }),
175+ route_msg.union_value );
176+
177+ return 0 ;
178+ }
179+
180+ int handlePipeEvent (const pipe::ClientPipe::Event::Disconnected)
181+ {
182+ for (auto & pair : tag_to_gateway_)
183+ {
184+ pair.second ->event (detail::Gateway::Event::Disconnected{});
185+ }
186+ return 0 ;
187+ }
188+
189+ void handleRouteConnect (const RouteConnect_1_0&)
190+ {
191+ for (auto & pair : tag_to_gateway_)
192+ {
193+ pair.second ->event (detail::Gateway::Event::Connected{});
194+ }
195+ }
196+
197+ void handleRouteChannelMsg (const RouteChannelMsg_1_0& route_channel_msg, pipe::ClientPipe::Payload payload)
198+ {
199+ const auto it = tag_to_gateway_.find (route_channel_msg.tag );
200+ if (it != tag_to_gateway_.end ())
201+ {
202+ it->second ->event (detail::Gateway::Event::Message{payload});
203+ }
204+ }
205+
206+ cetl::pmr::memory_resource& memory_;
112207 pipe::ClientPipe::Ptr client_pipe_;
113208 Tag next_tag_;
114209 std::unordered_map<Tag, detail::Gateway::Ptr> tag_to_gateway_;
@@ -117,9 +212,9 @@ class ClientRouterImpl final : public ClientRouter
117212
118213} // namespace
119214
120- ClientRouter::Ptr ClientRouter::make (pipe::ClientPipe::Ptr client_pipe)
215+ ClientRouter::Ptr ClientRouter::make (cetl::pmr::memory_resource& memory, pipe::ClientPipe::Ptr client_pipe)
121216{
122- return std::make_unique<ClientRouterImpl>(std::move (client_pipe));
217+ return std::make_unique<ClientRouterImpl>(memory, std::move (client_pipe));
123218}
124219
125220} // namespace ipc
0 commit comments