-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
comm.cpp
80 lines (66 loc) · 1.7 KB
/
comm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// (C) 2024 by Folkert van Heusden
// Released under MIT license
#include "gen.h"
#include <cassert>
#include <cstring>
#include "comm.h"
#if defined(ARDUINO)
#include "comm_arduino.h"
#endif
#if defined(ESP32)
#include "comm_esp32_hardwareserial.h"
#endif
#if IS_POSIX
#include "comm_posix_tty.h"
#endif
#include "comm_tcp_socket_client.h"
#include "comm_tcp_socket_server.h"
#include "log.h"
comm::comm()
{
}
comm::~comm()
{
}
void comm::println(const char *const s)
{
send_data(reinterpret_cast<const uint8_t *>(s), strlen(s));
send_data(reinterpret_cast<const uint8_t *>("\r\n"), 2);
}
void comm::println(const std::string & in)
{
send_data(reinterpret_cast<const uint8_t *>(in.c_str()), in.size());
send_data(reinterpret_cast<const uint8_t *>("\r\n"), 2);
}
comm *comm::deserialize(const JsonVariantConst j, bus *const b)
{
std::string type = j["comm-backend-type"];
comm *d = nullptr;
if (type == "tcp-server")
d = comm_tcp_socket_server::deserialize(j);
else if (type == "tcp-client")
d = comm_tcp_socket_client::deserialize(j);
#if defined(ESP32)
else if (type == "hardware-serial")
d = comm_esp32_hardwareserial::deserialize(j);
#endif
#if defined(ARDUINO)
else if (type == "arduino")
d = comm_arduino::deserialize(j);
#endif
#if IS_POSIX
else if (type == "posix")
d = comm_posix_tty::deserialize(j);
#endif
else {
DOLOG(warning, false, "comm::deserialize: \"%s\" not de-serialized", type.c_str());
return nullptr;
}
assert(d);
if (!d->begin()) {
delete d;
DOLOG(warning, false, "comm::deserialize: begin() \"%s\" failed", type.c_str());
return nullptr;
}
return d;
}