k-fsw/app/src/parameters/csp_table.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <stdint.h> | ||
| 3 | |||
| 4 | #include <zephyr/sys/util.h> | ||
| 5 | |||
| 6 | #include <kfsw/comms/csp.h> | ||
| 7 | #define KFSW_LOG_MODULE KFSW_LOG_MODULE_CSP | ||
| 8 | #include <kfsw/services/log.h> | ||
| 9 | #include <kfsw/services/parameter.h> | ||
| 10 | |||
| 11 | #include "tables.h" | ||
| 12 | |||
| 13 | /* Interface counters, summed over all interfaces. */ | ||
| 14 | struct csp_totals { | ||
| 15 | uint32_t tx_packets; | ||
| 16 | uint32_t rx_packets; | ||
| 17 | uint32_t tx_errors; | ||
| 18 | uint32_t rx_errors; | ||
| 19 | uint32_t dropped; | ||
| 20 | uint8_t interfaces; | ||
| 21 | }; | ||
| 22 | |||
| 23 | static uint32_t csp_tx_packets; | ||
| 24 | static uint32_t csp_rx_packets; | ||
| 25 | static uint32_t csp_tx_errors; | ||
| 26 | static uint32_t csp_rx_errors; | ||
| 27 | static uint32_t csp_dropped; | ||
| 28 | static uint8_t csp_interfaces; | ||
| 29 | static uint8_t csp_router_running; | ||
| 30 | |||
| 31 | /* Read-only: libcsp's active CIDR table cannot be replaced while routing. */ | ||
| 32 | static char csp_route_table[KFSW_CSP_ROUTE_TABLE_MAX_LENGTH + 1U] = CONFIG_KFSW_CSP_ROUTE_TABLE; | ||
| 33 | |||
| 34 | ✗ | static bool accumulate_interface(const struct kfsw_csp_interface_info *info, void *context) | |
| 35 | { | ||
| 36 | ✗ | struct csp_totals *totals = context; | |
| 37 | |||
| 38 | ✗ | totals->tx_packets += info->tx_packets; | |
| 39 | ✗ | totals->rx_packets += info->rx_packets; | |
| 40 | ✗ | totals->tx_errors += info->tx_errors; | |
| 41 | ✗ | totals->rx_errors += info->rx_errors; | |
| 42 | ✗ | totals->dropped += info->dropped_packets; | |
| 43 | ✗ | if (totals->interfaces < UINT8_MAX) { | |
| 44 | ✗ | totals->interfaces++; | |
| 45 | } | ||
| 46 | ✗ | return true; | |
| 47 | } | ||
| 48 | |||
| 49 | ✗ | static void sample_totals(void) | |
| 50 | { | ||
| 51 | ✗ | struct csp_totals totals = {0}; | |
| 52 | |||
| 53 | ✗ | kfsw_csp_visit_interfaces(accumulate_interface, &totals); | |
| 54 | ✗ | csp_tx_packets = totals.tx_packets; | |
| 55 | ✗ | csp_rx_packets = totals.rx_packets; | |
| 56 | ✗ | csp_tx_errors = totals.tx_errors; | |
| 57 | ✗ | csp_rx_errors = totals.rx_errors; | |
| 58 | ✗ | csp_dropped = totals.dropped; | |
| 59 | ✗ | csp_interfaces = totals.interfaces; | |
| 60 | ✗ | } | |
| 61 | |||
| 62 | ✗ | static void sample_tx_packets(void *value) | |
| 63 | { | ||
| 64 | ✗ | sample_totals(); | |
| 65 | ✗ | *(uint32_t *)value = csp_tx_packets; | |
| 66 | ✗ | } | |
| 67 | |||
| 68 | ✗ | static void sample_rx_packets(void *value) | |
| 69 | { | ||
| 70 | ✗ | sample_totals(); | |
| 71 | ✗ | *(uint32_t *)value = csp_rx_packets; | |
| 72 | ✗ | } | |
| 73 | |||
| 74 | ✗ | static void sample_tx_errors(void *value) | |
| 75 | { | ||
| 76 | ✗ | sample_totals(); | |
| 77 | ✗ | *(uint32_t *)value = csp_tx_errors; | |
| 78 | ✗ | } | |
| 79 | |||
| 80 | ✗ | static void sample_rx_errors(void *value) | |
| 81 | { | ||
| 82 | ✗ | sample_totals(); | |
| 83 | ✗ | *(uint32_t *)value = csp_rx_errors; | |
| 84 | ✗ | } | |
| 85 | |||
| 86 | ✗ | static void sample_dropped(void *value) | |
| 87 | { | ||
| 88 | ✗ | sample_totals(); | |
| 89 | ✗ | *(uint32_t *)value = csp_dropped; | |
| 90 | ✗ | } | |
| 91 | |||
| 92 | ✗ | static void sample_interfaces(void *value) | |
| 93 | { | ||
| 94 | ✗ | sample_totals(); | |
| 95 | ✗ | *(uint8_t *)value = csp_interfaces; | |
| 96 | ✗ | } | |
| 97 | |||
| 98 | /* The reliable transport's own verbosity, which the ground may need to raise | ||
| 99 | * for one pass. Not persistent: a reset returns to quiet. | ||
| 100 | */ | ||
| 101 | static uint8_t csp_rdp_trace; | ||
| 102 | |||
| 103 | ✗ | static void sample_rdp_trace(void *value) | |
| 104 | { | ||
| 105 | ✗ | *(uint8_t *)value = kfsw_csp_get_rdp_trace(); | |
| 106 | ✗ | } | |
| 107 | |||
| 108 | 2 | static int validate_rdp_trace(const union kfsw_param_scalar *value) | |
| 109 | { | ||
| 110 | 2 | return (value->u8 > 2U) ? -ERANGE : 0; | |
| 111 | } | ||
| 112 | |||
| 113 | 2 | static void apply_rdp_trace(const union kfsw_param_scalar *value) | |
| 114 | { | ||
| 115 | 2 | (void)kfsw_csp_set_rdp_trace(value->u8); | |
| 116 | 2 | } | |
| 117 | |||
| 118 | /* The packet dump, under the same rule as the verbosity above. */ | ||
| 119 | static uint8_t csp_packet_trace; | ||
| 120 | |||
| 121 | ✗ | static void sample_packet_trace(void *value) | |
| 122 | { | ||
| 123 | ✗ | *(uint8_t *)value = kfsw_csp_get_packet_trace() ? 1U : 0U; | |
| 124 | ✗ | } | |
| 125 | |||
| 126 | 2 | static int validate_packet_trace(const union kfsw_param_scalar *value) | |
| 127 | { | ||
| 128 | 2 | return (value->u8 > 1U) ? -ERANGE : 0; | |
| 129 | } | ||
| 130 | |||
| 131 | 2 | static void apply_packet_trace(const union kfsw_param_scalar *value) | |
| 132 | { | ||
| 133 | 2 | kfsw_csp_set_packet_trace(value->u8 != 0U); | |
| 134 | 2 | } | |
| 135 | |||
| 136 | /* libcsp's own error counters, sampled together. */ | ||
| 137 | static uint8_t csp_buffer_out; | ||
| 138 | static uint8_t csp_conn_out; | ||
| 139 | static uint8_t csp_conn_ovf; | ||
| 140 | static uint8_t csp_conn_noroute; | ||
| 141 | static uint8_t csp_invalid_reply; | ||
| 142 | static uint8_t csp_last_error; | ||
| 143 | static uint8_t csp_last_can_error; | ||
| 144 | |||
| 145 | ✗ | static void sample_counters(void) | |
| 146 | { | ||
| 147 | struct kfsw_csp_counters counters; | ||
| 148 | |||
| 149 | ✗ | kfsw_csp_get_counters(&counters); | |
| 150 | ✗ | csp_buffer_out = counters.buffer_out; | |
| 151 | ✗ | csp_conn_out = counters.conn_out; | |
| 152 | ✗ | csp_conn_ovf = counters.conn_overflow; | |
| 153 | ✗ | csp_conn_noroute = counters.conn_noroute; | |
| 154 | ✗ | csp_invalid_reply = counters.invalid_reply; | |
| 155 | ✗ | csp_last_error = counters.last_error; | |
| 156 | ✗ | csp_last_can_error = counters.last_can_error; | |
| 157 | ✗ | } | |
| 158 | |||
| 159 | ✗ | static void sample_buffer_out(void *value) | |
| 160 | { | ||
| 161 | ✗ | sample_counters(); | |
| 162 | ✗ | *(uint8_t *)value = csp_buffer_out; | |
| 163 | ✗ | } | |
| 164 | |||
| 165 | ✗ | static void sample_conn_out(void *value) | |
| 166 | { | ||
| 167 | ✗ | sample_counters(); | |
| 168 | ✗ | *(uint8_t *)value = csp_conn_out; | |
| 169 | ✗ | } | |
| 170 | |||
| 171 | ✗ | static void sample_conn_ovf(void *value) | |
| 172 | { | ||
| 173 | ✗ | sample_counters(); | |
| 174 | ✗ | *(uint8_t *)value = csp_conn_ovf; | |
| 175 | ✗ | } | |
| 176 | |||
| 177 | ✗ | static void sample_conn_noroute(void *value) | |
| 178 | { | ||
| 179 | ✗ | sample_counters(); | |
| 180 | ✗ | *(uint8_t *)value = csp_conn_noroute; | |
| 181 | ✗ | } | |
| 182 | |||
| 183 | ✗ | static void sample_invalid_reply(void *value) | |
| 184 | { | ||
| 185 | ✗ | sample_counters(); | |
| 186 | ✗ | *(uint8_t *)value = csp_invalid_reply; | |
| 187 | ✗ | } | |
| 188 | |||
| 189 | ✗ | static void sample_last_error(void *value) | |
| 190 | { | ||
| 191 | ✗ | sample_counters(); | |
| 192 | ✗ | *(uint8_t *)value = csp_last_error; | |
| 193 | ✗ | } | |
| 194 | |||
| 195 | ✗ | static void sample_last_can_error(void *value) | |
| 196 | { | ||
| 197 | ✗ | sample_counters(); | |
| 198 | ✗ | *(uint8_t *)value = csp_last_can_error; | |
| 199 | ✗ | } | |
| 200 | |||
| 201 | ✗ | static void sample_router_running(void *value) | |
| 202 | { | ||
| 203 | struct kfsw_csp_info info; | ||
| 204 | |||
| 205 | ✗ | kfsw_csp_get_info(&info); | |
| 206 | ✗ | *(uint8_t *)value = info.router_running ? 1U : 0U; | |
| 207 | ✗ | } | |
| 208 | |||
| 209 | static const struct kfsw_param_definition csp_param_definitions[] = { | ||
| 210 | { | ||
| 211 | .offset = 0x00U, | ||
| 212 | .type = KFSW_PARAM_U32, | ||
| 213 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 214 | .name = "tx_packets", | ||
| 215 | .description = "Packets sent across every interface", | ||
| 216 | .value = &csp_tx_packets, | ||
| 217 | .default_value = {.u32 = 0U}, | ||
| 218 | .sample = sample_tx_packets, | ||
| 219 | }, | ||
| 220 | { | ||
| 221 | .offset = 0x04U, | ||
| 222 | .type = KFSW_PARAM_U32, | ||
| 223 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 224 | .name = "rx_packets", | ||
| 225 | .description = "Packets received across every interface", | ||
| 226 | .value = &csp_rx_packets, | ||
| 227 | .default_value = {.u32 = 0U}, | ||
| 228 | .sample = sample_rx_packets, | ||
| 229 | }, | ||
| 230 | { | ||
| 231 | .offset = 0x08U, | ||
| 232 | .type = KFSW_PARAM_U32, | ||
| 233 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 234 | .name = "tx_errors", | ||
| 235 | .description = "Transmit errors across every interface", | ||
| 236 | .value = &csp_tx_errors, | ||
| 237 | .default_value = {.u32 = 0U}, | ||
| 238 | .sample = sample_tx_errors, | ||
| 239 | }, | ||
| 240 | { | ||
| 241 | .offset = 0x0cU, | ||
| 242 | .type = KFSW_PARAM_U32, | ||
| 243 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 244 | .name = "rx_errors", | ||
| 245 | .description = "Receive errors across every interface", | ||
| 246 | .value = &csp_rx_errors, | ||
| 247 | .default_value = {.u32 = 0U}, | ||
| 248 | .sample = sample_rx_errors, | ||
| 249 | }, | ||
| 250 | { | ||
| 251 | .offset = 0x10U, | ||
| 252 | .type = KFSW_PARAM_U32, | ||
| 253 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 254 | .name = "dropped", | ||
| 255 | .description = "Packets dropped across every interface", | ||
| 256 | .value = &csp_dropped, | ||
| 257 | .default_value = {.u32 = 0U}, | ||
| 258 | .sample = sample_dropped, | ||
| 259 | }, | ||
| 260 | { | ||
| 261 | .offset = 0x14U, | ||
| 262 | .type = KFSW_PARAM_U8, | ||
| 263 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 264 | .name = "interfaces", | ||
| 265 | .description = "Interfaces registered with the router", | ||
| 266 | .value = &csp_interfaces, | ||
| 267 | .default_value = {.u8 = 0U}, | ||
| 268 | .sample = sample_interfaces, | ||
| 269 | }, | ||
| 270 | { | ||
| 271 | .offset = 0x16U, | ||
| 272 | .type = KFSW_PARAM_U8, | ||
| 273 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 274 | .name = "buffer_out", | ||
| 275 | .description = "Times no packet buffer was free", | ||
| 276 | .value = &csp_buffer_out, | ||
| 277 | .default_value = {.u8 = 0U}, | ||
| 278 | .sample = sample_buffer_out, | ||
| 279 | }, | ||
| 280 | { | ||
| 281 | .offset = 0x17U, | ||
| 282 | .type = KFSW_PARAM_U8, | ||
| 283 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 284 | .name = "conn_out", | ||
| 285 | .description = "Times no connection slot was free", | ||
| 286 | .value = &csp_conn_out, | ||
| 287 | .default_value = {.u8 = 0U}, | ||
| 288 | .sample = sample_conn_out, | ||
| 289 | }, | ||
| 290 | { | ||
| 291 | .offset = 0x18U, | ||
| 292 | .type = KFSW_PARAM_U8, | ||
| 293 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 294 | .name = "conn_ovf", | ||
| 295 | .description = "Packets dropped by a full connection queue", | ||
| 296 | .value = &csp_conn_ovf, | ||
| 297 | .default_value = {.u8 = 0U}, | ||
| 298 | .sample = sample_conn_ovf, | ||
| 299 | }, | ||
| 300 | { | ||
| 301 | .offset = 0x19U, | ||
| 302 | .type = KFSW_PARAM_U8, | ||
| 303 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 304 | .name = "conn_noroute", | ||
| 305 | .description = "Packets dropped with no route to the destination", | ||
| 306 | .value = &csp_conn_noroute, | ||
| 307 | .default_value = {.u8 = 0U}, | ||
| 308 | .sample = sample_conn_noroute, | ||
| 309 | }, | ||
| 310 | { | ||
| 311 | .offset = 0x1aU, | ||
| 312 | .type = KFSW_PARAM_U8, | ||
| 313 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 314 | .name = "invalid_reply", | ||
| 315 | .description = "Replies that matched no open connection", | ||
| 316 | .value = &csp_invalid_reply, | ||
| 317 | .default_value = {.u8 = 0U}, | ||
| 318 | .sample = sample_invalid_reply, | ||
| 319 | }, | ||
| 320 | { | ||
| 321 | .offset = 0x1bU, | ||
| 322 | .type = KFSW_PARAM_U8, | ||
| 323 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 324 | .name = "last_error", | ||
| 325 | .description = "Code of the last libcsp error", | ||
| 326 | .value = &csp_last_error, | ||
| 327 | .default_value = {.u8 = 0U}, | ||
| 328 | .sample = sample_last_error, | ||
| 329 | }, | ||
| 330 | { | ||
| 331 | .offset = 0x1cU, | ||
| 332 | .type = KFSW_PARAM_U8, | ||
| 333 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 334 | .name = "last_can_error", | ||
| 335 | .description = "Code of the last CAN framing error", | ||
| 336 | .value = &csp_last_can_error, | ||
| 337 | .default_value = {.u8 = 0U}, | ||
| 338 | .sample = sample_last_can_error, | ||
| 339 | }, | ||
| 340 | { | ||
| 341 | .offset = 0x1dU, | ||
| 342 | .type = KFSW_PARAM_U8, | ||
| 343 | .flags = KFSW_PARAM_FLAG_CONFIGURATION | KFSW_PARAM_FLAG_LIVE, | ||
| 344 | .name = "rdp_trace", | ||
| 345 | .description = "RDP detail: 0 off, 1 errors, 2 with the protocol", | ||
| 346 | .value = &csp_rdp_trace, | ||
| 347 | .default_value = {.u8 = 0U}, | ||
| 348 | .validate = validate_rdp_trace, | ||
| 349 | .changed = apply_rdp_trace, | ||
| 350 | .sample = sample_rdp_trace, | ||
| 351 | }, | ||
| 352 | { | ||
| 353 | .offset = 0x1eU, | ||
| 354 | .type = KFSW_PARAM_U8, | ||
| 355 | .flags = KFSW_PARAM_FLAG_CONFIGURATION | KFSW_PARAM_FLAG_LIVE, | ||
| 356 | .name = "packet_trace", | ||
| 357 | .description = "Packet dump: 0 off, 1 on", | ||
| 358 | .value = &csp_packet_trace, | ||
| 359 | .default_value = {.u8 = 0U}, | ||
| 360 | .validate = validate_packet_trace, | ||
| 361 | .changed = apply_packet_trace, | ||
| 362 | .sample = sample_packet_trace, | ||
| 363 | }, | ||
| 364 | { | ||
| 365 | .offset = 0x20U, | ||
| 366 | .type = KFSW_PARAM_STRING, | ||
| 367 | .capacity = KFSW_CSP_ROUTE_TABLE_MAX_LENGTH + 1U, | ||
| 368 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 369 | .name = "route_table", | ||
| 370 | .description = "Compiled routes in libcsp CIDR syntax", | ||
| 371 | .value = csp_route_table, | ||
| 372 | .default_text = CONFIG_KFSW_CSP_ROUTE_TABLE, | ||
| 373 | }, | ||
| 374 | { | ||
| 375 | .offset = 0x15U, | ||
| 376 | .type = KFSW_PARAM_U8, | ||
| 377 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 378 | .name = "router_running", | ||
| 379 | .description = "Whether the CSP router thread is running", | ||
| 380 | .value = &csp_router_running, | ||
| 381 | .default_value = {.u8 = 0U}, | ||
| 382 | .sample = sample_router_running, | ||
| 383 | }, | ||
| 384 | }; | ||
| 385 | |||
| 386 | KFSW_PARAM_TABLE_FITS_THE_LINK(csp_param_definitions); | ||
| 387 | |||
| 388 | const struct kfsw_param_definition_set kfsw_csp_param_definitions = { | ||
| 389 | .table = KFSW_PARAM_TABLE_CSP, | ||
| 390 | .name = KFSW_PARAM_TABLE_CSP_NAME, | ||
| 391 | .description = "CSP counters and the route table", | ||
| 392 | .definitions = csp_param_definitions, | ||
| 393 | .count = ARRAY_SIZE(csp_param_definitions), | ||
| 394 | }; | ||
| 395 |