REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
transport.cpp
Go to the documentation of this file.
1#include <proto/main.pb.h>
2#include "Udp.h"
3#include "utils/logging.h"
4
5#include <Arduino.h>
6#include <Client.h>
7#include <pb_common.h>
8#include <pb_decode.h>
9#include <pb_encode.h>
10#include <string_view>
11#include <cassert>
12
13namespace transport {
14
15bool client_ostream_callback(pb_ostream_t *pb_stream, const pb_byte_t *buf, size_t count) {
16 auto stream = static_cast<Stream *>(pb_stream->state);
17 while (true) {
18 auto write_count = stream->write(buf, count);
19 if (write_count == count)
20 break;
21
22 auto client = dynamic_cast<Client *>(stream);
23 if (client && !client->connected())
24 return false;
25
26 buf += write_count;
27 count -= write_count;
28 }
29 return true;
30}
31
32pb_MessageV1& init_v1_message(pb_Envelope& envelope, pb_size_t which_kind) {
33 envelope = pb_Envelope_init_default;
34 envelope.which_kind = pb_Envelope_message_v1_tag;
35 auto& message = envelope.kind.message_v1 = pb_MessageV1_init_default;
36 message.which_kind = which_kind;
37 return message;
38}
39
40const pb_MessageV1& as_v1_message(const pb_Envelope& envelope, pb_size_t which_kind) {
41 auto& message = envelope.kind.message_v1;
42 assert(envelope.which_kind == pb_Envelope_message_v1_tag);
43 assert(message.which_kind == which_kind);
44 return message;
45}
46
47void create_error_message(pb_Envelope& envelope, std::string_view error_description, pb_ErrorCode error_code = pb_ErrorCode_None) {
48 auto& msg_out = init_v1_message(envelope, pb_MessageV1_error_message_tag);
49 msg_out.has_id = false;
50 auto& err_msg = msg_out.kind.error_message;
51 auto it = error_description.copy(err_msg.description, std::size(err_msg.description) - 1);
52 err_msg.description[it] = 0;
53 err_msg.code = error_code;
54}
55
56void create_success_message(pb_Envelope& envelope) {
57 init_v1_message(envelope, pb_MessageV1_success_message_tag);
58}
59
60bool client_istream_callback(pb_istream_t *stream, pb_byte_t *buf, size_t count){
61 auto client = static_cast<Client *>(stream->state);
62 while (true) {
63 auto read_count = client->readBytes(buf, count);
64
65 if (count == read_count)
66 return true;
67
68 if (!client->connected())
69 return false;
70
71 buf += read_count;
72 count -= read_count;
73 }
74}
75
76
77} // namespace utils
void create_error_message(pb_Envelope &envelope, std::string_view error_description, pb_ErrorCode error_code=pb_ErrorCode_None)
Definition transport.cpp:47
bool client_ostream_callback(pb_ostream_t *pb_stream, const pb_byte_t *buf, size_t count)
Definition transport.cpp:15
bool client_istream_callback(pb_istream_t *stream, pb_byte_t *buf, size_t count)
Definition transport.cpp:60
pb_MessageV1 & init_v1_message(pb_Envelope &envelope, pb_size_t which_kind)
Definition transport.cpp:32
void create_success_message(pb_Envelope &envelope)
Definition transport.cpp:56
const pb_MessageV1 & as_v1_message(const pb_Envelope &envelope, pb_size_t which_kind)
Definition transport.cpp:40