kfsw-services/src/log_remote.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <string.h> | ||
| 3 | |||
| 4 | #include <zephyr/kernel.h> | ||
| 5 | #include <zephyr/sys/byteorder.h> | ||
| 6 | #include <zephyr/sys/crc.h> | ||
| 7 | #include <zephyr/sys/util.h> | ||
| 8 | #include <csp/csp.h> | ||
| 9 | |||
| 10 | #include <kfsw/comms/csp.h> | ||
| 11 | #include <kfsw/services/event.h> | ||
| 12 | #include <kfsw/services/log_history.h> | ||
| 13 | #include <kfsw/services/log_remote.h> | ||
| 14 | |||
| 15 | /* | ||
| 16 | * Wire format, version 2. Every message starts with the version, a type and | ||
| 17 | * the requester's nonce, so a stray reply from an older read is ignored. | ||
| 18 | * | ||
| 19 | * request version, stream, min level, count u16, nonce[8] | ||
| 20 | * start header, format, first u64, end u64, overwritten u64 | ||
| 21 | * message header, sequence u64, uptime ms u64, module, severity, flags, length, data | ||
| 22 | * event header, sequence u64, boot u64, utc u64, utc valid, uptime us u64, | ||
| 23 | * source u16, id u16, severity, length, payload | ||
| 24 | * end header, status, records sent u16 | ||
| 25 | * | ||
| 26 | * Integers are big-endian. A message carries text, or with FLAG_PACKAGE the | ||
| 27 | * record's cbprintf package followed by an IEEE CRC32 of the node-rendered | ||
| 28 | * text (without NUL). FLAG_TEXT_CRC marks the four-byte trailer; length still | ||
| 29 | * counts only package bytes, so older readers reject it rather than misdecode. | ||
| 30 | */ | ||
| 31 | #define WIRE_VERSION 2U | ||
| 32 | #define TYPE_START 0U | ||
| 33 | #define TYPE_MESSAGE 1U | ||
| 34 | #define TYPE_END 2U | ||
| 35 | #define TYPE_EVENT 3U | ||
| 36 | #define NONCE_SIZE 8U | ||
| 37 | #define REQUEST_SIZE (5U + NONCE_SIZE) | ||
| 38 | #define HEADER_SIZE (2U + NONCE_SIZE) | ||
| 39 | #define START_SIZE (HEADER_SIZE + 25U) | ||
| 40 | #define MESSAGE_HEADER_SIZE (HEADER_SIZE + 20U) | ||
| 41 | #define EVENT_HEADER_SIZE (HEADER_SIZE + 39U) | ||
| 42 | #define END_SIZE (HEADER_SIZE + 3U) | ||
| 43 | #define FLAG_TRUNCATED BIT(0) | ||
| 44 | #define FLAG_PACKAGE BIT(1) | ||
| 45 | #define FLAG_TEXT_CRC BIT(2) | ||
| 46 | #define TEXT_CRC_SIZE 4U | ||
| 47 | /* A round number under what the encrypted link takes, leaving some margin. */ | ||
| 48 | #define WIRE_DATA_MAX 190U | ||
| 49 | |||
| 50 | #define STATUS_COMPLETE 0U | ||
| 51 | #define STATUS_CHANGED 1U | ||
| 52 | #define STATUS_NO_BUFFER 2U | ||
| 53 | #define STATUS_NOT_SERVED 3U | ||
| 54 | |||
| 55 | BUILD_ASSERT(MESSAGE_HEADER_SIZE + WIRE_DATA_MAX <= KFSW_CSP_PAYLOAD_MAX, | ||
| 56 | "a log record must fit the tightest transport, not just a CSP buffer"); | ||
| 57 | BUILD_ASSERT(KFSW_LOG_ENCODED_SIZE + TEXT_CRC_SIZE <= WIRE_DATA_MAX, "a package travels whole"); | ||
| 58 | BUILD_ASSERT(EVENT_HEADER_SIZE + KFSW_EVENT_MAX_PAYLOAD_SIZE <= KFSW_CSP_PAYLOAD_MAX, | ||
| 59 | "a journal record must fit the tightest transport"); | ||
| 60 | |||
| 61 | static csp_socket_t log_socket; | ||
| 62 | static bool running; | ||
| 63 | static K_MUTEX_DEFINE(start_lock); | ||
| 64 | |||
| 65 | 180 | static csp_packet_t *new_reply(uint8_t type, const uint8_t *nonce, size_t size) | |
| 66 | { | ||
| 67 | 180 | csp_packet_t *packet = csp_buffer_get(size); | |
| 68 | |||
| 69 | 180 | if (packet != NULL) { | |
| 70 | 180 | packet->data[0] = WIRE_VERSION; | |
| 71 | 180 | packet->data[1] = type; | |
| 72 | 180 | memcpy(&packet->data[2], nonce, NONCE_SIZE); | |
| 73 | 180 | packet->length = size; | |
| 74 | } | ||
| 75 | 180 | return packet; | |
| 76 | } | ||
| 77 | |||
| 78 | 66 | static bool send_start(csp_conn_t *connection, const uint8_t *nonce, | |
| 79 | const struct kfsw_log_remote_start *start) | ||
| 80 | { | ||
| 81 | 66 | csp_packet_t *reply = new_reply(TYPE_START, nonce, START_SIZE); | |
| 82 | |||
| 83 | 66 | if (reply == NULL) { | |
| 84 | ✗ | return false; | |
| 85 | } | ||
| 86 | 66 | reply->data[10] = start->format; | |
| 87 | 66 | sys_put_be64(start->first, &reply->data[11]); | |
| 88 | 66 | sys_put_be64(start->end, &reply->data[19]); | |
| 89 | 66 | sys_put_be64(start->overwritten, &reply->data[27]); | |
| 90 | 66 | csp_send(connection, reply); | |
| 91 | 66 | return true; | |
| 92 | } | ||
| 93 | |||
| 94 | 66 | static void send_end(csp_conn_t *connection, const uint8_t *nonce, uint8_t status, uint16_t sent) | |
| 95 | { | ||
| 96 | 66 | csp_packet_t *reply = new_reply(TYPE_END, nonce, END_SIZE); | |
| 97 | |||
| 98 | 66 | if (reply != NULL) { | |
| 99 | 66 | reply->data[10] = status; | |
| 100 | 66 | sys_put_be16(sent, &reply->data[11]); | |
| 101 | 66 | csp_send(connection, reply); | |
| 102 | } | ||
| 103 | 66 | } | |
| 104 | |||
| 105 | 48 | static bool send_message(csp_conn_t *connection, const uint8_t *nonce, uint8_t format, | |
| 106 | const struct kfsw_log_encoded *encoded) | ||
| 107 | { | ||
| 108 | struct kfsw_log_record record; | ||
| 109 | 48 | const uint8_t *data = encoded->data; | |
| 110 | 48 | size_t length = encoded->size; | |
| 111 | 48 | uint8_t flags = encoded->truncated ? FLAG_TRUNCATED : 0U; | |
| 112 | csp_packet_t *reply; | ||
| 113 | |||
| 114 | 48 | if ((format == KFSW_LOG_REMOTE_DICTIONARY) && encoded->package) { | |
| 115 | 8 | kfsw_log_history_format(encoded, &record); | |
| 116 | 8 | flags = (record.truncated ? FLAG_TRUNCATED : 0U) | FLAG_PACKAGE | FLAG_TEXT_CRC; | |
| 117 | } else { | ||
| 118 | 40 | kfsw_log_history_format(encoded, &record); | |
| 119 | 40 | length = strlen(record.text); | |
| 120 | 40 | if (length > WIRE_DATA_MAX) { | |
| 121 | ✗ | length = WIRE_DATA_MAX; | |
| 122 | ✗ | record.truncated = true; | |
| 123 | } | ||
| 124 | 40 | flags = record.truncated ? FLAG_TRUNCATED : 0U; | |
| 125 | 40 | data = (const uint8_t *)record.text; | |
| 126 | } | ||
| 127 | 48 | reply = new_reply(TYPE_MESSAGE, nonce, | |
| 128 | 48 | MESSAGE_HEADER_SIZE + length + | |
| 129 | 48 | ((flags & FLAG_TEXT_CRC) ? TEXT_CRC_SIZE : 0U)); | |
| 130 | 48 | if (reply == NULL) { | |
| 131 | ✗ | return false; | |
| 132 | } | ||
| 133 | 48 | sys_put_be64(encoded->sequence, &reply->data[10]); | |
| 134 | 48 | sys_put_be64(encoded->uptime_ms, &reply->data[18]); | |
| 135 | 48 | reply->data[26] = encoded->module; | |
| 136 | 48 | reply->data[27] = encoded->severity; | |
| 137 | 48 | reply->data[28] = flags; | |
| 138 | 48 | reply->data[29] = (uint8_t)length; | |
| 139 | 48 | memcpy(&reply->data[30], data, length); | |
| 140 | 48 | if ((flags & FLAG_TEXT_CRC) != 0U) { | |
| 141 | 8 | sys_put_be32(crc32_ieee((const uint8_t *)record.text, strlen(record.text)), | |
| 142 | 8 | &reply->data[MESSAGE_HEADER_SIZE + length]); | |
| 143 | } | ||
| 144 | 48 | csp_send(connection, reply); | |
| 145 | 48 | return true; | |
| 146 | } | ||
| 147 | |||
| 148 | 60 | static void serve_log(csp_conn_t *connection, const uint8_t *nonce, uint8_t minimum, uint16_t count) | |
| 149 | { | ||
| 150 | struct kfsw_log_history_window window; | ||
| 151 | struct kfsw_log_encoded encoded; | ||
| 152 | 60 | struct kfsw_log_remote_start start = {.format = kfsw_log_remote_format()}; | |
| 153 | 60 | uint8_t status = STATUS_COMPLETE; | |
| 154 | 60 | uint16_t sent = 0U; | |
| 155 | |||
| 156 | 60 | if (kfsw_log_history_window(count, &window) != 0) { | |
| 157 | ✗ | return; | |
| 158 | } | ||
| 159 | 60 | start.first = window.first; | |
| 160 | 60 | start.end = window.end; | |
| 161 | 60 | start.overwritten = window.overwritten; | |
| 162 | 60 | if (!send_start(connection, nonce, &start)) { | |
| 163 | ✗ | return; | |
| 164 | } | ||
| 165 | 114 | for (uint64_t sequence = window.first; sequence < window.end; sequence++) { | |
| 166 | 66 | if (kfsw_log_history_get_encoded(sequence, &encoded) != 0) { | |
| 167 | 12 | status = STATUS_CHANGED; | |
| 168 | 12 | break; | |
| 169 | } | ||
| 170 | 54 | if (encoded.severity < minimum) { | |
| 171 | 6 | continue; | |
| 172 | } | ||
| 173 | 48 | if (!send_message(connection, nonce, start.format, &encoded)) { | |
| 174 | ✗ | status = STATUS_NO_BUFFER; | |
| 175 | ✗ | break; | |
| 176 | } | ||
| 177 | 48 | sent++; | |
| 178 | } | ||
| 179 | 60 | send_end(connection, nonce, status, sent); | |
| 180 | } | ||
| 181 | |||
| 182 | #if CONFIG_KFSW_JOURNAL | ||
| 183 | static bool send_event(csp_conn_t *connection, const uint8_t *nonce, | ||
| 184 | const struct kfsw_journal_record *record) | ||
| 185 | { | ||
| 186 | const struct kfsw_event_record *event = &record->event; | ||
| 187 | const size_t length = MIN(event->payload_size, KFSW_EVENT_MAX_PAYLOAD_SIZE); | ||
| 188 | csp_packet_t *reply = new_reply(TYPE_EVENT, nonce, EVENT_HEADER_SIZE + length); | ||
| 189 | |||
| 190 | if (reply == NULL) { | ||
| 191 | return false; | ||
| 192 | } | ||
| 193 | sys_put_be64(record->sequence, &reply->data[10]); | ||
| 194 | sys_put_be64(record->boot, &reply->data[18]); | ||
| 195 | sys_put_be64((uint64_t)record->utc_seconds, &reply->data[26]); | ||
| 196 | reply->data[34] = record->utc_valid ? 1U : 0U; | ||
| 197 | sys_put_be64(event->monotonic_us, &reply->data[35]); | ||
| 198 | sys_put_be16(event->source, &reply->data[43]); | ||
| 199 | sys_put_be16(event->id, &reply->data[45]); | ||
| 200 | reply->data[47] = event->severity; | ||
| 201 | reply->data[48] = (uint8_t)length; | ||
| 202 | memcpy(&reply->data[49], event->payload, length); | ||
| 203 | csp_send(connection, reply); | ||
| 204 | return true; | ||
| 205 | } | ||
| 206 | #endif | ||
| 207 | |||
| 208 | 6 | static void serve_journal(csp_conn_t *connection, const uint8_t *nonce, uint16_t count) | |
| 209 | { | ||
| 210 | 6 | const struct kfsw_log_remote_start start = {.format = KFSW_LOG_REMOTE_TEXT}; | |
| 211 | |||
| 212 | 6 | if (!send_start(connection, nonce, &start)) { | |
| 213 | ✗ | return; | |
| 214 | } | ||
| 215 | #if CONFIG_KFSW_JOURNAL | ||
| 216 | struct kfsw_journal_stats stats; | ||
| 217 | struct kfsw_journal_record record; | ||
| 218 | uint8_t status = STATUS_COMPLETE; | ||
| 219 | uint16_t sent = 0U; | ||
| 220 | uint16_t age; | ||
| 221 | |||
| 222 | kfsw_journal_get_stats(&stats); | ||
| 223 | /* Oldest first, the order the log uses. */ | ||
| 224 | for (age = MIN(count, stats.held); age > 0U; age--) { | ||
| 225 | if (kfsw_journal_get(age - 1U, &record) != 0) { | ||
| 226 | status = STATUS_CHANGED; | ||
| 227 | break; | ||
| 228 | } | ||
| 229 | if (!send_event(connection, nonce, &record)) { | ||
| 230 | status = STATUS_NO_BUFFER; | ||
| 231 | break; | ||
| 232 | } | ||
| 233 | sent++; | ||
| 234 | } | ||
| 235 | send_end(connection, nonce, status, sent); | ||
| 236 | #else | ||
| 237 | ARG_UNUSED(count); | ||
| 238 | 6 | send_end(connection, nonce, STATUS_NOT_SERVED, 0U); | |
| 239 | #endif | ||
| 240 | } | ||
| 241 | |||
| 242 | 102 | static void serve(csp_conn_t *connection, csp_packet_t *request) | |
| 243 | { | ||
| 244 | uint8_t nonce[NONCE_SIZE]; | ||
| 245 | uint8_t stream; | ||
| 246 | uint8_t minimum; | ||
| 247 | uint16_t count; | ||
| 248 | |||
| 249 | /* A malformed request gets no reply; the requester times out. */ | ||
| 250 | 102 | if ((request->length != REQUEST_SIZE) || (request->data[0] != WIRE_VERSION) || | |
| 251 | 90 | (request->data[1] > KFSW_LOG_REMOTE_JOURNAL) || (request->data[2] > 3U)) { | |
| 252 | 24 | csp_buffer_free(request); | |
| 253 | 36 | return; | |
| 254 | } | ||
| 255 | 78 | stream = request->data[1]; | |
| 256 | 78 | minimum = request->data[2]; | |
| 257 | 78 | count = sys_get_be16(&request->data[3]); | |
| 258 | 78 | memcpy(nonce, &request->data[5], sizeof(nonce)); | |
| 259 | 78 | csp_buffer_free(request); | |
| 260 | 78 | if ((count == 0U) || (count > KFSW_LOG_HISTORY_MAX_READ)) { | |
| 261 | 12 | return; | |
| 262 | } | ||
| 263 | 66 | if (stream == KFSW_LOG_REMOTE_JOURNAL) { | |
| 264 | 6 | serve_journal(connection, nonce, count); | |
| 265 | } else { | ||
| 266 | 60 | serve_log(connection, nonce, minimum, count); | |
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | 6 | static void log_server(void *arg1, void *arg2, void *arg3) | |
| 271 | { | ||
| 272 | ARG_UNUSED(arg1); | ||
| 273 | ARG_UNUSED(arg2); | ||
| 274 | ARG_UNUSED(arg3); | ||
| 275 | |||
| 276 | 210 | for (;;) { | |
| 277 | 216 | csp_conn_t *connection = csp_accept(&log_socket, 100U); | |
| 278 | csp_packet_t *request; | ||
| 279 | |||
| 280 | 210 | if (connection == NULL) { | |
| 281 | 108 | continue; | |
| 282 | } | ||
| 283 | 102 | request = csp_read(connection, 0U); | |
| 284 | 102 | if (request != NULL) { | |
| 285 | 102 | serve(connection, request); | |
| 286 | } | ||
| 287 | 102 | (void)csp_close(connection); | |
| 288 | } | ||
| 289 | } | ||
| 290 | |||
| 291 | K_THREAD_DEFINE(kfsw_log_remote_thread, CONFIG_KFSW_LOG_REMOTE_STACK_SIZE, log_server, NULL, NULL, | ||
| 292 | NULL, CONFIG_KFSW_LOG_REMOTE_PRIORITY, 0, SYS_FOREVER_MS); | ||
| 293 | |||
| 294 | 20 | int kfsw_log_remote_server_start(void) | |
| 295 | { | ||
| 296 | struct kfsw_csp_info info; | ||
| 297 | 20 | int result = 0; | |
| 298 | |||
| 299 | 20 | k_mutex_lock(&start_lock, K_FOREVER); | |
| 300 | 20 | if (running) { | |
| 301 | 6 | goto done; | |
| 302 | } | ||
| 303 | 14 | kfsw_csp_get_info(&info); | |
| 304 | 14 | if (!info.initialized || !info.router_running) { | |
| 305 | 6 | result = -ENETDOWN; | |
| 306 | 6 | goto done; | |
| 307 | } | ||
| 308 | 8 | memset(&log_socket, 0, sizeof(log_socket)); | |
| 309 | 8 | log_socket.opts = CSP_SO_CRC32REQ; | |
| 310 | 8 | if (csp_listen(&log_socket, 1U) != CSP_ERR_NONE) { | |
| 311 | ✗ | (void)csp_socket_close(&log_socket); | |
| 312 | ✗ | result = -EIO; | |
| 313 | ✗ | goto done; | |
| 314 | } | ||
| 315 | 8 | if (csp_bind(&log_socket, CONFIG_KFSW_LOG_REMOTE_PORT) != CSP_ERR_NONE) { | |
| 316 | ✗ | (void)csp_socket_close(&log_socket); | |
| 317 | ✗ | result = -EADDRINUSE; | |
| 318 | ✗ | goto done; | |
| 319 | } | ||
| 320 | 8 | running = true; | |
| 321 | 8 | k_thread_start(kfsw_log_remote_thread); | |
| 322 | 20 | done: | |
| 323 | 20 | k_mutex_unlock(&start_lock); | |
| 324 | 20 | return result; | |
| 325 | } | ||
| 326 | |||
| 327 | 36 | static bool parse_start(const csp_packet_t *packet, struct kfsw_log_remote_start *start) | |
| 328 | { | ||
| 329 | 36 | if (packet->length != START_SIZE) { | |
| 330 | ✗ | return false; | |
| 331 | } | ||
| 332 | 36 | start->format = packet->data[10]; | |
| 333 | 36 | start->first = sys_get_be64(&packet->data[11]); | |
| 334 | 36 | start->end = sys_get_be64(&packet->data[19]); | |
| 335 | 36 | start->overwritten = sys_get_be64(&packet->data[27]); | |
| 336 | 36 | return true; | |
| 337 | } | ||
| 338 | |||
| 339 | 24 | static bool parse_message(const csp_packet_t *packet, struct kfsw_log_remote_message *message) | |
| 340 | { | ||
| 341 | 24 | const size_t length = (packet->length >= MESSAGE_HEADER_SIZE) ? packet->data[29] : 0U; | |
| 342 | 48 | const bool text_crc = (packet->length >= MESSAGE_HEADER_SIZE) && | |
| 343 | 24 | ((packet->data[28] & FLAG_TEXT_CRC) != 0U); | |
| 344 | |||
| 345 | 24 | if ((packet->length < MESSAGE_HEADER_SIZE) || | |
| 346 | 24 | (packet->length != MESSAGE_HEADER_SIZE + length + (text_crc ? TEXT_CRC_SIZE : 0U)) || | |
| 347 | 24 | (length + (text_crc ? TEXT_CRC_SIZE : 0U) > WIRE_DATA_MAX) || | |
| 348 | 8 | (text_crc && ((packet->data[28] & FLAG_PACKAGE) == 0U))) { | |
| 349 | ✗ | return false; | |
| 350 | } | ||
| 351 | 24 | *message = (struct kfsw_log_remote_message){ | |
| 352 | 24 | .sequence = sys_get_be64(&packet->data[10]), | |
| 353 | 24 | .uptime_ms = sys_get_be64(&packet->data[18]), | |
| 354 | 24 | .module = packet->data[26], | |
| 355 | 24 | .severity = packet->data[27], | |
| 356 | 24 | .truncated = (packet->data[28] & FLAG_TRUNCATED) != 0U, | |
| 357 | 24 | .package = (packet->data[28] & FLAG_PACKAGE) != 0U, | |
| 358 | 24 | .size = (uint8_t)length, | |
| 359 | .text_crc_present = text_crc, | ||
| 360 | }; | ||
| 361 | 24 | memcpy(message->data, &packet->data[30], length); | |
| 362 | 24 | if (text_crc) { | |
| 363 | 8 | message->text_crc = sys_get_be32(&packet->data[MESSAGE_HEADER_SIZE + length]); | |
| 364 | } | ||
| 365 | /* Text is terminated for the caller; a package is used by its size. */ | ||
| 366 | 24 | if (!message->package) { | |
| 367 | 16 | message->data[length] = '\0'; | |
| 368 | } | ||
| 369 | 24 | return true; | |
| 370 | } | ||
| 371 | |||
| 372 | ✗ | static bool parse_event(const csp_packet_t *packet, struct kfsw_journal_record *record) | |
| 373 | { | ||
| 374 | ✗ | const size_t length = (packet->length > EVENT_HEADER_SIZE) ? packet->data[48] : 0U; | |
| 375 | |||
| 376 | ✗ | if ((packet->length < EVENT_HEADER_SIZE) || | |
| 377 | ✗ | (packet->length != EVENT_HEADER_SIZE + length) || | |
| 378 | (length > KFSW_EVENT_MAX_PAYLOAD_SIZE)) { | ||
| 379 | ✗ | return false; | |
| 380 | } | ||
| 381 | ✗ | *record = (struct kfsw_journal_record){ | |
| 382 | ✗ | .sequence = sys_get_be64(&packet->data[10]), | |
| 383 | ✗ | .boot = sys_get_be64(&packet->data[18]), | |
| 384 | ✗ | .utc_seconds = (int64_t)sys_get_be64(&packet->data[26]), | |
| 385 | ✗ | .utc_valid = packet->data[34] != 0U, | |
| 386 | .event = | ||
| 387 | { | ||
| 388 | ✗ | .monotonic_us = sys_get_be64(&packet->data[35]), | |
| 389 | ✗ | .source = sys_get_be16(&packet->data[43]), | |
| 390 | ✗ | .id = sys_get_be16(&packet->data[45]), | |
| 391 | ✗ | .severity = packet->data[47], | |
| 392 | ✗ | .payload_size = (uint8_t)length, | |
| 393 | }, | ||
| 394 | }; | ||
| 395 | ✗ | memcpy(record->event.payload, &packet->data[49], length); | |
| 396 | ✗ | return true; | |
| 397 | } | ||
| 398 | |||
| 399 | /* 0 to keep reading, 1 when the read is over, or a negative errno. */ | ||
| 400 | 96 | static int handle_reply(const csp_packet_t *packet, const struct kfsw_log_remote_visitor *visitor, | |
| 401 | void *context) | ||
| 402 | { | ||
| 403 | struct kfsw_log_remote_start start; | ||
| 404 | struct kfsw_log_remote_message message; | ||
| 405 | struct kfsw_journal_record record; | ||
| 406 | |||
| 407 | 96 | switch (packet->data[1]) { | |
| 408 | 36 | case TYPE_START: | |
| 409 | 36 | if (!parse_start(packet, &start)) { | |
| 410 | ✗ | return -EBADMSG; | |
| 411 | } | ||
| 412 | 36 | if (visitor->start != NULL) { | |
| 413 | 36 | visitor->start(&start, context); | |
| 414 | } | ||
| 415 | 36 | return 0; | |
| 416 | 24 | case TYPE_MESSAGE: | |
| 417 | 24 | if (!parse_message(packet, &message)) { | |
| 418 | ✗ | return -EBADMSG; | |
| 419 | } | ||
| 420 | 24 | return ((visitor->message == NULL) || visitor->message(&message, context)) ? 0 : 1; | |
| 421 | ✗ | case TYPE_EVENT: | |
| 422 | ✗ | if (!parse_event(packet, &record)) { | |
| 423 | ✗ | return -EBADMSG; | |
| 424 | } | ||
| 425 | ✗ | return ((visitor->event == NULL) || visitor->event(&record, context)) ? 0 : 1; | |
| 426 | 36 | case TYPE_END: | |
| 427 | 36 | if (packet->length != END_SIZE) { | |
| 428 | ✗ | return -EBADMSG; | |
| 429 | } | ||
| 430 | 36 | if (packet->data[10] == STATUS_COMPLETE) { | |
| 431 | 24 | return 1; | |
| 432 | } | ||
| 433 | 12 | return (packet->data[10] == STATUS_NOT_SERVED) ? -ENOTSUP : -EIO; | |
| 434 | ✗ | default: | |
| 435 | ✗ | return -EBADMSG; | |
| 436 | } | ||
| 437 | } | ||
| 438 | |||
| 439 | 60 | int kfsw_log_remote_read(uint16_t node, enum kfsw_log_remote_stream stream, uint16_t count, | |
| 440 | uint8_t min_level, const struct kfsw_log_remote_visitor *visitor, | ||
| 441 | void *context) | ||
| 442 | { | ||
| 443 | uint8_t nonce[NONCE_SIZE]; | ||
| 444 | csp_packet_t *request; | ||
| 445 | csp_conn_t *connection; | ||
| 446 | 60 | bool answered = false; | |
| 447 | 60 | int result = 0; | |
| 448 | |||
| 449 | 60 | if ((visitor == NULL) || (node == 0U) || (node >= KFSW_CSP_BROADCAST_ADDRESS) || | |
| 450 | 54 | (stream > KFSW_LOG_REMOTE_JOURNAL) || (count == 0U) || | |
| 451 | 42 | (count > KFSW_LOG_HISTORY_MAX_READ) || (min_level > 3U)) { | |
| 452 | 24 | return -EINVAL; | |
| 453 | } | ||
| 454 | 36 | sys_put_be64(k_cycle_get_64() ^ ((uint64_t)node << 48), nonce); | |
| 455 | 36 | connection = csp_connect(CSP_PRIO_NORM, node, CONFIG_KFSW_LOG_REMOTE_PORT, | |
| 456 | CONFIG_KFSW_LOG_REMOTE_TIMEOUT_MS, | ||
| 457 | (CSP_O_CRC32 | KFSW_CSP_AUTHENTICATED_CONNECTION)); | ||
| 458 | 36 | if (connection == NULL) { | |
| 459 | ✗ | return -ENOTCONN; | |
| 460 | } | ||
| 461 | 36 | request = csp_buffer_get(REQUEST_SIZE); | |
| 462 | 36 | if (request == NULL) { | |
| 463 | ✗ | (void)csp_close(connection); | |
| 464 | ✗ | return -ENOBUFS; | |
| 465 | } | ||
| 466 | 36 | request->data[0] = WIRE_VERSION; | |
| 467 | 36 | request->data[1] = (uint8_t)stream; | |
| 468 | 36 | request->data[2] = min_level; | |
| 469 | 36 | sys_put_be16(count, &request->data[3]); | |
| 470 | 36 | memcpy(&request->data[5], nonce, sizeof(nonce)); | |
| 471 | 36 | request->length = REQUEST_SIZE; | |
| 472 | 36 | csp_send(connection, request); | |
| 473 | |||
| 474 | 132 | while (result == 0) { | |
| 475 | 96 | csp_packet_t *reply = csp_read(connection, CONFIG_KFSW_LOG_REMOTE_TIMEOUT_MS); | |
| 476 | |||
| 477 | 96 | if (reply == NULL) { | |
| 478 | ✗ | result = answered ? -EIO : -ETIMEDOUT; | |
| 479 | ✗ | break; | |
| 480 | } | ||
| 481 | /* A reply to another read, or from another version, is not ours. */ | ||
| 482 | 192 | if ((reply->length >= HEADER_SIZE) && (reply->data[0] == WIRE_VERSION) && | |
| 483 | 96 | (memcmp(&reply->data[2], nonce, sizeof(nonce)) == 0)) { | |
| 484 | 96 | answered = true; | |
| 485 | 96 | result = handle_reply(reply, visitor, context); | |
| 486 | } | ||
| 487 | 96 | csp_buffer_free(reply); | |
| 488 | } | ||
| 489 | 36 | (void)csp_close(connection); | |
| 490 | 36 | return (result == 1) ? 0 : result; | |
| 491 | } | ||
| 492 |