-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagnosticSinks.cpp
More file actions
273 lines (228 loc) · 8.42 KB
/
Copy pathDiagnosticSinks.cpp
File metadata and controls
273 lines (228 loc) · 8.42 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "HALAL/Services/Diagnostics/Diagnostics.hpp"
#if defined(HAL_UART_MODULE_ENABLED) && !defined(SIM_ON)
#include "HALAL/Services/Communication/UART/UART.hpp"
#endif
#ifdef STLIB_ETH
#include "HALAL/Models/Packets/Order.hpp"
#endif
namespace Diagnostics {
namespace {
constexpr uint16_t DIAGNOSTIC_FAULT_ORDER_ID = 1555;
constexpr uint16_t DIAGNOSTIC_WARNING_ORDER_ID = 2555;
constexpr uint16_t DIAGNOSTIC_OK_ORDER_ID = 3000;
constexpr uint8_t DIAGNOSTIC_CHAR_TYPE = 3;
constexpr uint8_t DIAGNOSTIC_ERROR_HANDLER_KIND = 5;
constexpr uint8_t DIAGNOSTIC_WARNING_KIND = 7;
constexpr size_t transport_buffer_capacity = 2 + 1 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 2 +
Config::origin_capacity + 1 +
Config::formatted_message_capacity + 1;
size_t bounded_strnlen(const char* src, size_t max_length) {
if (src == nullptr) {
return 0;
}
size_t length = 0;
while (length < max_length && src[length] != '\0') {
length++;
}
return length;
}
template <size_t Capacity> void copy_c_string(char (&dst)[Capacity], const char* src) {
if (Capacity == 0) {
return;
}
if (src == nullptr) {
dst[0] = '\0';
return;
}
const size_t length = bounded_strnlen(src, Capacity - 1);
memcpy(dst, src, length);
dst[length] = '\0';
}
#if defined(HAL_UART_MODULE_ENABLED) && !defined(SIM_ON)
class UartDiagnosticSink final : public DiagnosticSink {
public:
bool publish(const DiagnosticRecord& record) override {
if (!UART::printf_ready) {
return false;
}
char description[Config::formatted_message_capacity + 1]{};
DiagnosticFormatter::describe(record, description, sizeof(description));
printf("%u: %s%s", std::to_underlying(record.severity), description, endl);
return true;
}
};
#endif
#ifdef STLIB_ETH
class DiagnosticTransportOrder final : public Order {
public:
static inline OrderProtocol* forward_target = nullptr;
DiagnosticTransportOrder() {
Packet::packets[DIAGNOSTIC_FAULT_ORDER_ID] = this;
Packet::packets[DIAGNOSTIC_WARNING_ORDER_ID] = this;
Packet::packets[DIAGNOSTIC_OK_ORDER_ID] = this;
orders[DIAGNOSTIC_FAULT_ORDER_ID] = this;
orders[DIAGNOSTIC_WARNING_ORDER_ID] = this;
orders[DIAGNOSTIC_OK_ORDER_ID] = this;
}
void set_record(const DiagnosticRecord& record, const char* description) {
id = id_for(record.severity);
kind = kind_for(record.severity);
copy_c_string(origin, record.origin);
copy_c_string(message, description);
counter = record.timestamp.counter;
second = record.timestamp.second;
minute = record.timestamp.minute;
hour = record.timestamp.hour;
day = record.timestamp.day;
month = record.timestamp.month;
year = record.timestamp.year;
size = encoded_size();
}
void set_callback(void (*callback)(void)) override { this->callback = callback; }
void parse(OrderProtocol* socket, uint8_t* data) override {
(void)socket;
if (data == nullptr)
return;
memcpy(&id, data, sizeof(id));
size_t off = sizeof(id) + sizeof(uint8_t);
kind = data[off++];
size_t len = bounded_strnlen(reinterpret_cast<char*>(data + off), sizeof(origin) - 1);
memcpy(origin, data + off, len);
origin[len] = '\0';
off += len + 1;
len = bounded_strnlen(reinterpret_cast<char*>(data + off), sizeof(message) - 1);
memcpy(message, data + off, len);
message[len] = '\0';
off += len + 1;
memcpy(
&counter,
data + off,
sizeof(counter) + sizeof(second) + sizeof(minute) + sizeof(hour) + sizeof(day) +
sizeof(month) + sizeof(year)
);
size = off + sizeof(counter) + sizeof(second) + sizeof(minute) + sizeof(hour) +
sizeof(day) + sizeof(month) + sizeof(year);
}
void process() override {
switch (id) {
case DIAGNOSTIC_FAULT_ORDER_ID:
Hub::publish_runtime_fault(message, false, 0, origin, "");
break;
case DIAGNOSTIC_WARNING_ORDER_ID:
Hub::publish_runtime_warning(message, false, 0, origin, "");
break;
case DIAGNOSTIC_OK_ORDER_ID:
Hub::publish_runtime_info(message, false, 0, origin, "");
break;
}
}
uint8_t* build() override {
auto bytes = span<byte>(buffer.data(), buffer.size());
size_t offset = 0;
write(bytes, offset, id);
write(bytes, offset, DIAGNOSTIC_CHAR_TYPE);
write(bytes, offset, kind);
write_string(bytes, offset, origin);
write_string(bytes, offset, message);
write(bytes, offset, counter);
write(bytes, offset, second);
write(bytes, offset, minute);
write(bytes, offset, hour);
write(bytes, offset, day);
write(bytes, offset, month);
write(bytes, offset, year);
size = offset;
return reinterpret_cast<uint8_t*>(buffer.data());
}
size_t get_size() override { return size; }
void reset_for_receive() { size = 0; }
uint16_t get_id() override { return id; }
void set_pointer(size_t index, void* pointer) override {
(void)index;
(void)pointer;
}
private:
size_t encoded_size() const {
return sizeof(id) + sizeof(DIAGNOSTIC_CHAR_TYPE) + sizeof(kind) +
bounded_strnlen(origin, sizeof(origin)) + 1 +
bounded_strnlen(message, sizeof(message)) + 1 + sizeof(counter) + sizeof(second) +
sizeof(minute) + sizeof(hour) + sizeof(day) + sizeof(month) + sizeof(year);
}
static uint16_t id_for(Severity severity) {
switch (severity) {
case Severity::FAULT:
return DIAGNOSTIC_FAULT_ORDER_ID;
case Severity::WARNING:
return DIAGNOSTIC_WARNING_ORDER_ID;
case Severity::INFO:
return DIAGNOSTIC_OK_ORDER_ID;
}
return DIAGNOSTIC_OK_ORDER_ID;
}
static uint8_t kind_for(Severity severity) {
return severity == Severity::FAULT ? DIAGNOSTIC_ERROR_HANDLER_KIND
: DIAGNOSTIC_WARNING_KIND;
}
template <typename Value> static void write(span<byte> bytes, size_t& offset, Value value) {
memcpy(bytes.data() + offset, &value, sizeof(Value));
offset += sizeof(Value);
}
static void write_string(span<byte> bytes, size_t& offset, const char* text) {
const size_t length = bounded_strnlen(text, bytes.size() - offset - 1);
memcpy(bytes.data() + offset, text, length);
offset += length;
bytes[offset++] = byte{0};
}
uint16_t id;
void (*callback)(void) = nullptr;
uint8_t kind{DIAGNOSTIC_WARNING_KIND};
char origin[Config::origin_capacity + 1]{};
char message[Config::formatted_message_capacity + 1]{};
uint16_t counter{0};
uint8_t second{0};
uint8_t minute{0};
uint8_t hour{0};
uint8_t day{0};
uint8_t month{0};
uint16_t year{0};
size_t size{0};
array<byte, transport_buffer_capacity> buffer{};
};
class OrderProtocolDiagnosticSink final : public DiagnosticSink {
public:
explicit OrderProtocolDiagnosticSink(OrderProtocol* target = nullptr) : target_socket(target) {
DiagnosticTransportOrder::forward_target = target;
}
bool publish(const DiagnosticRecord& record) override {
if (target_socket == nullptr) {
return false;
}
char description[Config::formatted_message_capacity + 1]{};
DiagnosticFormatter::describe(record, description, sizeof(description));
transport_order.set_record(record, description);
transport_order.build();
bool delivered = target_socket->send_order(transport_order);
transport_order.reset_for_receive();
return delivered;
}
private:
OrderProtocol* target_socket;
DiagnosticTransportOrder transport_order{};
};
#endif
} // namespace
void Runtime::install_default_sinks() {
if (defaults_installed) {
return;
}
#if defined(HAL_UART_MODULE_ENABLED) && !defined(SIM_ON)
(void)Hub::emplace_sink<UartDiagnosticSink>();
#endif
defaults_installed = true;
}
#ifdef STLIB_ETH
void install_ethernet_sink(OrderProtocol* target) {
(void)Hub::emplace_sink<OrderProtocolDiagnosticSink>(target);
}
#endif
} // namespace Diagnostics