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
24void trim(char *str) {
25 unsigned int start = 0, end = strlen(str) - 1;
26
27 // Remove leading whitespace
28 while (isspace(str[start])) {
29 start++;
30 }
31
32 // Remove trailing whitespace
33 while (end > start && isspace(str[end])) {
34 end--;
35 }
36
37 // If the string was trimmed, adjust the null terminator
38 if (start > 0 || end < (strlen(str) - 1)) {
39 memmove(str, str + start, end - start + 1);
40 str[end - start + 1] = '\0';
41 }
42}
43
44void msg::Broker::handleMessage(transport::Transport &transport, pb_Envelope &in_envelope) {
45 auto msg_stream = transport.ctrl_output();
46
47 if (in_envelope.which_kind != pb_Envelope_message_v1_tag) {
48 msg_stream->report_error("handler only supports v1 messages!");
49 return;
50 }
51
52 int envelope_kind = in_envelope.which_kind;
53 int message_kind;
54 if (envelope_kind == pb_Envelope_generic_tag) {
55 message_kind = in_envelope.kind.generic.which_kind;
56 }else if (envelope_kind == pb_Envelope_message_v1_tag) {
57 message_kind = in_envelope.kind.message_v1.which_kind;
58 }else {
59 msg_stream->report_error("Unknown envelope kind.");
60 return;
61 }
62
63 // Select message handler
64 auto msg_handler = msg::handlers::Registry::get().lookup(envelope_kind, message_kind);
65 if (!msg_handler) {
66 msg_stream->report_error("Error unknown handler for message type.");
67 return;
68 }
69 //auto requiredClearance = msg::handlers::Registry::get().requiredClearance(msg_type);
70 if (msg_handler->handle(transport, in_envelope))
71 return;
72
73 msg_stream->report_error("Error while handling streaming message.");
74 LOG_ALWAYS("Error while handling message.");
75}
76
77utils::SerialLineReader serial_line_reader;
78
79bool msg::Broker::process(transport::Transport &transport) {
80 pb_Envelope envelope;
81 auto result = transport.ctrl_input()->read(envelope);
82 if (result.is_ok()) {
83 handleMessage(transport, envelope);
84 pb_release(pb_Envelope_fields, &envelope);
85 return true;
86 }
87
88 transport.ctrl_output()->report_error(result.err_value());
89 LOG_ALWAYS(result.err_value().c_str());
90 LOG_ALWAYS("Malformed input. Expecting protobuf.");
91 return false;
92}
utils::SerialLineReader serial_line_reader
Definition protocol.cpp:77
void trim(char *str)
Definition protocol.cpp:24