REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
protocol.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 anabrid GmbH
2// Contact: https://www.anabrid.com/licensing/
3//
4// SPDX-License-Identifier: MIT OR GPL-2.0-or-later
5
6#include "proto/main.pb.h"
7#include <protocol/transport.h>
8#include <net/auth.h>
9#include <net/settings.h>
10#include <protocol/protocol.h>
11#include <protocol/protocol_oob.h>
12#include <protocol/registry.h>
13#include <run/run_manager.h>
14#include <utils/StringPrint.h>
15#include <utils/logging.h>
16#include <utils/serial_lines.h>
17
18#include <algorithm>
19#include <cctype>
20#include <cstring>
21#include <locale>
22#include <utils/streaming_json.h>
23
24
25FASTRUN
26void msg::Broker::handleMessage(transport::Transport &transport, pb_Envelope &in_envelope) {
27 auto msg_stream = transport.ctrl_output();
28
29 int envelope_kind = in_envelope.which_kind;
30 int message_kind;
31 if (envelope_kind == pb_Envelope_generic_tag) {
32 message_kind = in_envelope.kind.generic.which_kind;
33 }else if (envelope_kind == pb_Envelope_message_v1_tag) {
34 message_kind = in_envelope.kind.message_v1.which_kind;
35 }else {
36 msg_stream->report_error("Unknown envelope kind.");
37 return;
38 }
39
40 // Select message handler
41 auto msg_handler = msg::handlers::Registry::get().lookup(envelope_kind, message_kind);
42 if (!msg_handler) {
43 msg_stream->report_error("Error unknown handler for message type.");
44 return;
45 }
46 //auto requiredClearance = msg::handlers::Registry::get().requiredClearance(msg_type);
47 if (msg_handler->handle(in_envelope, transport))
48 return;
49
50 msg_stream->report_error("Error while handling streaming message.");
51 LOG_ALWAYS("Error while handling message.");
52}
53
54utils::SerialLineReader serial_line_reader;
55
56FASTRUN
57bool msg::Broker::process(transport::Transport &transport) {
58 pb_Envelope envelope;
59 auto result = transport.ctrl_input()->read(envelope);
60
61 if (result.is_ok()) {
62 if (!result.ok_value()) return false;
63 handleMessage(transport, envelope);
64 pb_release(pb_Envelope_fields, &envelope);
65 return true;
66 }
67
68 transport.ctrl_output()->report_error(result.err_value());
69 LOG_ALWAYS(std::to_string(envelope.kind.message_v1.which_kind).c_str());
70 LOG_ALWAYS(result.err_value().c_str());
71 LOG_ALWAYS("Malformed input. Expecting protobuf.");
72 return false;
73}
utils::SerialLineReader serial_line_reader
Definition protocol.cpp:54