kfsw-comms/src/csp.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <zephyr/kernel.h> | ||
| 2 | |||
| 3 | #include <endian.h> | ||
| 4 | #include <errno.h> | ||
| 5 | #include <string.h> | ||
| 6 | |||
| 7 | #include <csp/csp.h> | ||
| 8 | #if CONFIG_KFSW_CSP_HMAC | ||
| 9 | #include <csp/autoconfig.h> | ||
| 10 | #include <csp/crypto/csp_hmac.h> | ||
| 11 | #include <kfsw_csp_hmac_key.h> | ||
| 12 | /* | ||
| 13 | * Selecting the Kconfig option is not enough on its own: libcsp is built by its | ||
| 14 | * own CMake, and its Zephyr integration has to carry CSP_USE_HMAC across. When | ||
| 15 | * it does not, every signed connection fails to send with "unsupported", which | ||
| 16 | * on a radio reads as a dead link rather than a missing feature. That cost an | ||
| 17 | * afternoon once, so the pinned revision is checked here instead. | ||
| 18 | */ | ||
| 19 | #if !CSP_USE_HMAC | ||
| 20 | #error "KFSW_CSP_HMAC needs a libcsp built with CSP_USE_HMAC; the pinned revision does not map it" | ||
| 21 | #endif | ||
| 22 | #endif | ||
| 23 | #include <csp/csp_cmp.h> | ||
| 24 | #include <csp/csp_debug.h> | ||
| 25 | #include <csp/csp_hooks.h> | ||
| 26 | #include <csp/csp_id.h> | ||
| 27 | #include <csp/csp_iflist.h> | ||
| 28 | #include <csp/csp_interface.h> | ||
| 29 | #include <csp/csp_rtable.h> | ||
| 30 | #include <csp/interfaces/csp_if_lo.h> | ||
| 31 | |||
| 32 | #include <kfsw/platform/time.h> | ||
| 33 | #include <kfsw/platform/wallclock.h> | ||
| 34 | #include <kfsw/comms/csp.h> | ||
| 35 | #if CONFIG_KFSW_CSP_CAN | ||
| 36 | #include <kfsw/comms/can.h> | ||
| 37 | #endif | ||
| 38 | |||
| 39 | #if CONFIG_KFSW_CSP_KISS_UART | ||
| 40 | #include "uart_internal.h" | ||
| 41 | #endif | ||
| 42 | |||
| 43 | /* libcsp does not mask addresses; out of range they corrupt the header. */ | ||
| 44 | BUILD_ASSERT(CONFIG_KFSW_CSP_ADDRESS < KFSW_CSP_BROADCAST_ADDRESS, | ||
| 45 | "the local CSP address must be below the broadcast address"); | ||
| 46 | #if CONFIG_KFSW_CSP_KISS_UART | ||
| 47 | BUILD_ASSERT(CONFIG_KFSW_CSP_UART_PEER_ADDRESS < KFSW_CSP_BROADCAST_ADDRESS, | ||
| 48 | "the UART peer address must be below the broadcast address"); | ||
| 49 | #endif | ||
| 50 | |||
| 51 | static bool initialized; | ||
| 52 | static bool stack_initialized; | ||
| 53 | static bool router_running; | ||
| 54 | static K_MUTEX_DEFINE(lifecycle_lock); | ||
| 55 | |||
| 56 | /* Revision reported by csp ident until the composition sets it. */ | ||
| 57 | static const char *revision = CONFIG_KFSW_CSP_REVISION; | ||
| 58 | |||
| 59 | ✗ | void kfsw_csp_set_revision(const char *value) | |
| 60 | { | ||
| 61 | ✗ | if (initialized || (value == NULL) || (value[0] == '\0')) { | |
| 62 | ✗ | return; | |
| 63 | } | ||
| 64 | ✗ | revision = value; | |
| 65 | } | ||
| 66 | |||
| 67 | /* Loopback handles traffic to this node's address before the route table. */ | ||
| 68 | 54 | static void configure_loopback_address(void) | |
| 69 | { | ||
| 70 | 54 | csp_if_lo.addr = CONFIG_KFSW_CSP_ADDRESS; | |
| 71 | 54 | } | |
| 72 | |||
| 73 | 104 | static int check_route_table(const char *route_table, size_t *entry_count) | |
| 74 | { | ||
| 75 | int entries; | ||
| 76 | |||
| 77 | 104 | if (route_table == NULL || strnlen(route_table, KFSW_CSP_ROUTE_TABLE_MAX_LENGTH + 1U) > | |
| 78 | KFSW_CSP_ROUTE_TABLE_MAX_LENGTH) { | ||
| 79 | ✗ | return CSP_ERR_INVAL; | |
| 80 | } | ||
| 81 | |||
| 82 | 104 | entries = csp_rtable_check(route_table); | |
| 83 | 104 | if (entries < 0) { | |
| 84 | 16 | return entries; | |
| 85 | } | ||
| 86 | /* | ||
| 87 | * The pinned CIDR table clamps its insertion index, so a table that uses every | ||
| 88 | * slot would lose its last route in csp_rtable_load(). | ||
| 89 | */ | ||
| 90 | 88 | if (entries >= CONFIG_CSP_RTABLE_SIZE) { | |
| 91 | 4 | return CSP_ERR_NOMEM; | |
| 92 | } | ||
| 93 | |||
| 94 | 84 | if (entry_count != NULL) { | |
| 95 | 84 | *entry_count = (size_t)entries; | |
| 96 | } | ||
| 97 | 84 | return CSP_ERR_NONE; | |
| 98 | } | ||
| 99 | |||
| 100 | /* Called only before the router starts. */ | ||
| 101 | 80 | static int load_route_table(const char *route_table) | |
| 102 | { | ||
| 103 | size_t expected_entries; | ||
| 104 | 80 | int result = check_route_table(route_table, &expected_entries); | |
| 105 | |||
| 106 | 80 | if (result != CSP_ERR_NONE || expected_entries == 0U) { | |
| 107 | ✗ | return result != CSP_ERR_NONE ? result : CSP_ERR_INVAL; | |
| 108 | } | ||
| 109 | |||
| 110 | 80 | csp_rtable_clear(); | |
| 111 | 80 | result = csp_rtable_load(route_table); | |
| 112 | 80 | if (result < 0 || (size_t)result != expected_entries) { | |
| 113 | /* Clear a partially loaded table. */ | ||
| 114 | ✗ | csp_rtable_clear(); | |
| 115 | ✗ | return result < 0 ? result : CSP_ERR_INVAL; | |
| 116 | } | ||
| 117 | 80 | return CSP_ERR_NONE; | |
| 118 | } | ||
| 119 | |||
| 120 | 48 | int kfsw_csp_route_table_apply(const char *route_table) | |
| 121 | { | ||
| 122 | int result; | ||
| 123 | |||
| 124 | 48 | if (route_table == NULL) { | |
| 125 | ✗ | return CSP_ERR_INVAL; | |
| 126 | } | ||
| 127 | 48 | k_mutex_lock(&lifecycle_lock, K_FOREVER); | |
| 128 | /* libcsp readers keep pointers into the live CIDR table. */ | ||
| 129 | 48 | if (router_running) { | |
| 130 | 20 | result = CSP_ERR_NOTSUP; | |
| 131 | 28 | } else if (!initialized) { | |
| 132 | 2 | result = CSP_ERR_INVAL; | |
| 133 | } else { | ||
| 134 | 26 | result = load_route_table(route_table); | |
| 135 | } | ||
| 136 | 48 | k_mutex_unlock(&lifecycle_lock); | |
| 137 | 48 | return result; | |
| 138 | } | ||
| 139 | |||
| 140 | 54 | static int configure_routes(void) | |
| 141 | { | ||
| 142 | 54 | const char *route_table = CONFIG_KFSW_CSP_ROUTE_TABLE; | |
| 143 | |||
| 144 | /* Nothing leaves the node unless the composition names the link. */ | ||
| 145 | 54 | if (route_table[0] == '\0') { | |
| 146 | 52 | route_table = "0/0 LOOP"; | |
| 147 | } | ||
| 148 | |||
| 149 | 54 | return load_route_table(route_table); | |
| 150 | } | ||
| 151 | |||
| 152 | 22 | static void kfsw_csp_router(void *arg1, void *arg2, void *arg3) | |
| 153 | { | ||
| 154 | ARG_UNUSED(arg1); | ||
| 155 | ARG_UNUSED(arg2); | ||
| 156 | ARG_UNUSED(arg3); | ||
| 157 | |||
| 158 | for (;;) { | ||
| 159 | 1278 | (void)csp_route_work(); | |
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | K_THREAD_DEFINE(kfsw_csp_router_thread, CONFIG_KFSW_CSP_ROUTER_STACK_SIZE, kfsw_csp_router, NULL, | ||
| 164 | NULL, NULL, CONFIG_KFSW_CSP_ROUTER_PRIORITY, 0, SYS_FOREVER_MS); | ||
| 165 | |||
| 166 | 86 | int kfsw_csp_init(void) | |
| 167 | { | ||
| 168 | int result; | ||
| 169 | |||
| 170 | 86 | if (initialized) { | |
| 171 | 4 | return CSP_ERR_NONE; | |
| 172 | } | ||
| 173 | |||
| 174 | 82 | csp_conf.hostname = CONFIG_KFSW_CSP_HOSTNAME; | |
| 175 | 82 | csp_conf.model = CONFIG_KFSW_CSP_MODEL; | |
| 176 | 82 | csp_conf.revision = revision; | |
| 177 | 82 | csp_conf.version = CONFIG_KFSW_CSP_VERSION; | |
| 178 | 82 | if (!stack_initialized) { | |
| 179 | 66 | csp_init(); | |
| 180 | 66 | stack_initialized = true; | |
| 181 | } | ||
| 182 | #if CONFIG_KFSW_CSP_HMAC | ||
| 183 | { | ||
| 184 | static const uint8_t hmac_key[] = KFSW_CSP_HMAC_KEY_BYTES; | ||
| 185 | |||
| 186 | result = csp_hmac_set_key(hmac_key, sizeof(hmac_key)); | ||
| 187 | if (result != CSP_ERR_NONE) { | ||
| 188 | return result; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | #endif | ||
| 192 | /* A disagreement here corrupts every header on the wire, so it is refused | ||
| 193 | in the image that flies and not only in one built with assertions. The | ||
| 194 | caller reports it; this library does not log. */ | ||
| 195 | 82 | if (csp_id_get_max_nodeid() != KFSW_CSP_BROADCAST_ADDRESS) { | |
| 196 | ✗ | return CSP_ERR_INVAL; | |
| 197 | } | ||
| 198 | |||
| 199 | #if CONFIG_KFSW_CSP_KISS_UART | ||
| 200 | 30 | result = kfsw_uart_open_all(); | |
| 201 | 30 | if (result != CSP_ERR_NONE) { | |
| 202 | 28 | return result; | |
| 203 | } | ||
| 204 | #endif | ||
| 205 | |||
| 206 | #if CONFIG_KFSW_CSP_CAN | ||
| 207 | /* Open before loading routes that name this interface. */ | ||
| 208 | 2 | result = kfsw_can_open(); | |
| 209 | 2 | if (result != 0) { | |
| 210 | ✗ | return CSP_ERR_DRIVER; | |
| 211 | } | ||
| 212 | #endif | ||
| 213 | |||
| 214 | 54 | configure_loopback_address(); | |
| 215 | |||
| 216 | 54 | result = configure_routes(); | |
| 217 | 54 | if (result != CSP_ERR_NONE) { | |
| 218 | ✗ | return result; | |
| 219 | } | ||
| 220 | |||
| 221 | 54 | result = csp_bind_callback(csp_service_handler, CSP_PING); | |
| 222 | 54 | if (result != CSP_ERR_NONE) { | |
| 223 | ✗ | return result; | |
| 224 | } | ||
| 225 | |||
| 226 | /* Management port, which answers csp ident. */ | ||
| 227 | 54 | result = csp_bind_callback(csp_service_handler, CSP_CMP); | |
| 228 | 54 | if (result != CSP_ERR_NONE) { | |
| 229 | ✗ | return result; | |
| 230 | } | ||
| 231 | |||
| 232 | 54 | initialized = true; | |
| 233 | 54 | return CSP_ERR_NONE; | |
| 234 | } | ||
| 235 | |||
| 236 | 32 | int kfsw_csp_start(void) | |
| 237 | { | ||
| 238 | 32 | k_mutex_lock(&lifecycle_lock, K_FOREVER); | |
| 239 | 32 | if (!initialized) { | |
| 240 | 4 | k_mutex_unlock(&lifecycle_lock); | |
| 241 | 4 | return CSP_ERR_INVAL; | |
| 242 | } | ||
| 243 | |||
| 244 | 28 | if (router_running) { | |
| 245 | ✗ | k_mutex_unlock(&lifecycle_lock); | |
| 246 | ✗ | return CSP_ERR_NONE; | |
| 247 | } | ||
| 248 | |||
| 249 | 28 | router_running = true; | |
| 250 | 28 | k_thread_start(kfsw_csp_router_thread); | |
| 251 | 28 | k_mutex_unlock(&lifecycle_lock); | |
| 252 | 28 | return CSP_ERR_NONE; | |
| 253 | } | ||
| 254 | |||
| 255 | 258 | void kfsw_csp_get_info(struct kfsw_csp_info *info) | |
| 256 | { | ||
| 257 | 258 | if (info == NULL) { | |
| 258 | ✗ | return; | |
| 259 | } | ||
| 260 | |||
| 261 | 258 | info->address = CONFIG_KFSW_CSP_ADDRESS; | |
| 262 | 258 | info->hostname = CONFIG_KFSW_CSP_HOSTNAME; | |
| 263 | 258 | info->model = CONFIG_KFSW_CSP_MODEL; | |
| 264 | 258 | info->revision = revision; | |
| 265 | 258 | info->initialized = initialized; | |
| 266 | 258 | info->router_running = router_running; | |
| 267 | 258 | info->free_buffers = initialized ? csp_buffer_remaining() : 0; | |
| 268 | 258 | info->libcsp = KFSW_LIBCSP_REVISION; | |
| 269 | 258 | info->protocol = csp_conf.version; | |
| 270 | } | ||
| 271 | |||
| 272 | 4 | void kfsw_csp_visit_interfaces(kfsw_csp_interface_visitor_t visitor, void *context) | |
| 273 | { | ||
| 274 | csp_iface_t *interface; | ||
| 275 | |||
| 276 | 4 | if (!initialized || visitor == NULL) { | |
| 277 | 4 | return; | |
| 278 | } | ||
| 279 | |||
| 280 | ✗ | for (interface = csp_iflist_get(); interface != NULL; interface = interface->next) { | |
| 281 | ✗ | const struct kfsw_csp_interface_info info = { | |
| 282 | ✗ | .name = interface->name, | |
| 283 | ✗ | .address = interface->addr, | |
| 284 | ✗ | .prefix_length = interface->netmask, | |
| 285 | ✗ | .is_default = interface->is_default != 0, | |
| 286 | ✗ | .tx_packets = interface->tx, | |
| 287 | ✗ | .rx_packets = interface->rx, | |
| 288 | ✗ | .tx_errors = interface->tx_error, | |
| 289 | ✗ | .rx_errors = interface->rx_error, | |
| 290 | ✗ | .dropped_packets = interface->drop, | |
| 291 | ✗ | .auth_errors = interface->autherr, | |
| 292 | }; | ||
| 293 | |||
| 294 | ✗ | if (!visitor(&info, context)) { | |
| 295 | ✗ | break; | |
| 296 | } | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | struct route_visitor_context { | ||
| 301 | kfsw_csp_route_visitor_t visitor; | ||
| 302 | void *context; | ||
| 303 | bool keep_visiting; | ||
| 304 | }; | ||
| 305 | |||
| 306 | 18 | static bool visit_route(void *context, csp_route_t *route) | |
| 307 | { | ||
| 308 | 18 | struct route_visitor_context *visitor_context = context; | |
| 309 | |||
| 310 | 18 | if (!visitor_context->keep_visiting) { | |
| 311 | ✗ | return false; | |
| 312 | } | ||
| 313 | |||
| 314 | 18 | const struct kfsw_csp_route_info info = { | |
| 315 | 18 | .address = route->address, | |
| 316 | 18 | .prefix_length = route->netmask, | |
| 317 | 18 | .interface_name = route->iface->name, | |
| 318 | 18 | .via = route->via, | |
| 319 | 18 | .has_via = route->via != CSP_NO_VIA_ADDRESS, | |
| 320 | }; | ||
| 321 | |||
| 322 | 18 | visitor_context->keep_visiting = visitor_context->visitor(&info, visitor_context->context); | |
| 323 | 18 | return visitor_context->keep_visiting; | |
| 324 | } | ||
| 325 | |||
| 326 | 14 | void kfsw_csp_visit_routes(kfsw_csp_route_visitor_t visitor, void *context) | |
| 327 | { | ||
| 328 | 14 | struct route_visitor_context visitor_context = { | |
| 329 | .visitor = visitor, | ||
| 330 | .context = context, | ||
| 331 | .keep_visiting = true, | ||
| 332 | }; | ||
| 333 | |||
| 334 | 14 | if (!initialized || visitor == NULL) { | |
| 335 | 4 | return; | |
| 336 | } | ||
| 337 | |||
| 338 | 10 | csp_rtable_iterate(visit_route, &visitor_context); | |
| 339 | } | ||
| 340 | |||
| 341 | 44 | int kfsw_csp_route_lookup(uint16_t address, struct kfsw_csp_route_info *info) | |
| 342 | { | ||
| 343 | csp_iface_t *interface; | ||
| 344 | 44 | csp_route_t *route = NULL; | |
| 345 | 44 | bool default_interface = false; | |
| 346 | 44 | int result = 0; | |
| 347 | |||
| 348 | 44 | if (info == NULL || address > KFSW_CSP_BROADCAST_ADDRESS) { | |
| 349 | 8 | return -EINVAL; | |
| 350 | } | ||
| 351 | 36 | k_mutex_lock(&lifecycle_lock, K_FOREVER); | |
| 352 | 36 | if (!initialized) { | |
| 353 | 4 | result = -ENETDOWN; | |
| 354 | 4 | goto out; | |
| 355 | } | ||
| 356 | |||
| 357 | 32 | if (address == csp_if_lo.addr) { | |
| 358 | 4 | interface = &csp_if_lo; | |
| 359 | } else { | ||
| 360 | 28 | interface = csp_iflist_get_by_subnet(address, NULL); | |
| 361 | 28 | if (interface == NULL) { | |
| 362 | 24 | route = csp_rtable_find_route(address); | |
| 363 | 24 | interface = route != NULL ? route->iface : csp_iflist_get_by_isdfl(NULL); | |
| 364 | 24 | default_interface = route == NULL; | |
| 365 | } | ||
| 366 | } | ||
| 367 | 32 | if (interface == NULL) { | |
| 368 | 4 | result = -ENOENT; | |
| 369 | 4 | goto out; | |
| 370 | } | ||
| 371 | |||
| 372 | 56 | *info = (struct kfsw_csp_route_info){ | |
| 373 | .address = route != NULL ? route->address : interface->addr, | ||
| 374 | .prefix_length = route != NULL ? route->netmask : interface->netmask, | ||
| 375 | 28 | .interface_name = interface->name, | |
| 376 | .via = route != NULL ? route->via : CSP_NO_VIA_ADDRESS, | ||
| 377 | 28 | .has_via = route != NULL && route->via != CSP_NO_VIA_ADDRESS, | |
| 378 | }; | ||
| 379 | 28 | if (address == csp_if_lo.addr) { | |
| 380 | 4 | info->prefix_length = KFSW_CSP_HOST_BITS; | |
| 381 | 24 | } else if (default_interface) { | |
| 382 | 4 | info->address = 0U; | |
| 383 | 4 | info->prefix_length = 0U; | |
| 384 | } | ||
| 385 | 20 | out: | |
| 386 | 36 | k_mutex_unlock(&lifecycle_lock); | |
| 387 | 36 | return result; | |
| 388 | } | ||
| 389 | |||
| 390 | 24 | int kfsw_csp_route_table_check(const char *route_table, size_t *entry_count) | |
| 391 | { | ||
| 392 | /* Before the interfaces exist the parser can't resolve names, so report | ||
| 393 | * -ENETDOWN instead of an invalid table. | ||
| 394 | */ | ||
| 395 | 24 | if (!initialized) { | |
| 396 | ✗ | return -ENETDOWN; | |
| 397 | } | ||
| 398 | |||
| 399 | 24 | return check_route_table(route_table, entry_count); | |
| 400 | } | ||
| 401 | |||
| 402 | 2 | void kfsw_csp_set_packet_trace(bool enabled) | |
| 403 | { | ||
| 404 | 2 | csp_dbg_packet_print = enabled ? 1U : 0U; | |
| 405 | 2 | } | |
| 406 | |||
| 407 | ✗ | bool kfsw_csp_get_packet_trace(void) | |
| 408 | { | ||
| 409 | ✗ | return csp_dbg_packet_print != 0U; | |
| 410 | } | ||
| 411 | |||
| 412 | 2 | int kfsw_csp_set_rdp_trace(uint8_t level) | |
| 413 | { | ||
| 414 | 2 | if (level > 2U) { | |
| 415 | ✗ | return -ERANGE; | |
| 416 | } | ||
| 417 | 2 | csp_dbg_rdp_print = level; | |
| 418 | 2 | return 0; | |
| 419 | } | ||
| 420 | |||
| 421 | ✗ | uint8_t kfsw_csp_get_rdp_trace(void) | |
| 422 | { | ||
| 423 | ✗ | return csp_dbg_rdp_print; | |
| 424 | } | ||
| 425 | |||
| 426 | 60 | void kfsw_csp_get_counters(struct kfsw_csp_counters *counters) | |
| 427 | { | ||
| 428 | 60 | if (counters == NULL) { | |
| 429 | 4 | return; | |
| 430 | } | ||
| 431 | |||
| 432 | 56 | counters->buffer_out = csp_dbg_buffer_out; | |
| 433 | 56 | counters->conn_out = csp_dbg_conn_out; | |
| 434 | 56 | counters->conn_overflow = csp_dbg_conn_ovf; | |
| 435 | 56 | counters->conn_noroute = csp_dbg_conn_noroute; | |
| 436 | 56 | counters->invalid_reply = csp_dbg_inval_reply; | |
| 437 | 56 | counters->last_error = csp_dbg_errno; | |
| 438 | 56 | counters->last_can_error = csp_dbg_can_errno; | |
| 439 | } | ||
| 440 | |||
| 441 | 48 | void kfsw_csp_clear_counters(void) | |
| 442 | { | ||
| 443 | 48 | csp_dbg_buffer_out = 0U; | |
| 444 | 48 | csp_dbg_conn_out = 0U; | |
| 445 | 48 | csp_dbg_conn_ovf = 0U; | |
| 446 | 48 | csp_dbg_conn_noroute = 0U; | |
| 447 | 48 | csp_dbg_inval_reply = 0U; | |
| 448 | 48 | csp_dbg_errno = 0U; | |
| 449 | 48 | csp_dbg_can_errno = 0U; | |
| 450 | 48 | } | |
| 451 | |||
| 452 | 16 | const char *kfsw_csp_error_name(uint8_t code) | |
| 453 | { | ||
| 454 | 16 | switch (code) { | |
| 455 | 4 | case 0U: | |
| 456 | 4 | return "none"; | |
| 457 | ✗ | case CSP_DBG_ERR_CORRUPT_BUFFER: | |
| 458 | ✗ | return "corrupt buffer"; | |
| 459 | 4 | case CSP_DBG_ERR_MTU_EXCEEDED: | |
| 460 | 4 | return "MTU exceeded"; | |
| 461 | ✗ | case CSP_DBG_ERR_ALREADY_FREE: | |
| 462 | ✗ | return "buffer already free"; | |
| 463 | ✗ | case CSP_DBG_ERR_REFCOUNT: | |
| 464 | ✗ | return "buffer reference count"; | |
| 465 | ✗ | case CSP_DBG_ERR_INVALID_RTABLE_ENTRY: | |
| 466 | ✗ | return "invalid route entry"; | |
| 467 | ✗ | case CSP_DBG_ERR_UNSUPPORTED: | |
| 468 | ✗ | return "unsupported"; | |
| 469 | ✗ | case CSP_DBG_ERR_INVALID_BIND_PORT: | |
| 470 | ✗ | return "invalid bind port"; | |
| 471 | ✗ | case CSP_DBG_ERR_PORT_ALREADY_IN_USE: | |
| 472 | ✗ | return "port in use"; | |
| 473 | ✗ | case CSP_DBG_ERR_ALREADY_CLOSED: | |
| 474 | ✗ | return "connection already closed"; | |
| 475 | ✗ | case CSP_DBG_ERR_INVALID_POINTER: | |
| 476 | ✗ | return "invalid pointer"; | |
| 477 | ✗ | case CSP_DBG_ERR_CLOCK_SET_FAIL: | |
| 478 | ✗ | return "clock set refused"; | |
| 479 | 8 | default: | |
| 480 | 8 | return "unknown"; | |
| 481 | } | ||
| 482 | } | ||
| 483 | |||
| 484 | 12 | const char *kfsw_csp_can_error_name(uint8_t code) | |
| 485 | { | ||
| 486 | 12 | switch (code) { | |
| 487 | 4 | case 0U: | |
| 488 | 4 | return "none"; | |
| 489 | 4 | case CSP_DBG_CAN_ERR_FRAME_LOST: | |
| 490 | 4 | return "frame lost"; | |
| 491 | ✗ | case CSP_DBG_CAN_ERR_RX_OVF: | |
| 492 | ✗ | return "receive overflow"; | |
| 493 | ✗ | case CSP_DBG_CAN_ERR_RX_OUT: | |
| 494 | ✗ | return "no receive buffer"; | |
| 495 | ✗ | case CSP_DBG_CAN_ERR_SHORT_BEGIN: | |
| 496 | ✗ | return "short first frame"; | |
| 497 | ✗ | case CSP_DBG_CAN_ERR_INCOMPLETE: | |
| 498 | ✗ | return "incomplete packet"; | |
| 499 | ✗ | case CSP_DBG_CAN_ERR_UNKNOWN: | |
| 500 | ✗ | return "unknown frame"; | |
| 501 | 4 | default: | |
| 502 | 4 | return "unknown"; | |
| 503 | } | ||
| 504 | } | ||
| 505 | |||
| 506 | #if CONFIG_KFSW_CSP_CLOCK_RTC | ||
| 507 | /* | ||
| 508 | * RTC-backed versions of libcsp's weak clock hooks, so the time survives a | ||
| 509 | * reset. They are in this file so the linker pulls them in. | ||
| 510 | */ | ||
| 511 | void csp_clock_get_time(csp_timestamp_t *time) | ||
| 512 | { | ||
| 513 | int64_t seconds = 0; | ||
| 514 | |||
| 515 | if (time == NULL) { | ||
| 516 | return; | ||
| 517 | } | ||
| 518 | time->tv_nsec = 0U; | ||
| 519 | /* An unset clock reads as zero. */ | ||
| 520 | time->tv_sec = (kfsw_wallclock_get(&seconds) == 0) ? (uint32_t)seconds : 0U; | ||
| 521 | } | ||
| 522 | |||
| 523 | int csp_clock_set_time(const csp_timestamp_t *time) | ||
| 524 | { | ||
| 525 | if (time == NULL) { | ||
| 526 | return CSP_ERR_INVAL; | ||
| 527 | } | ||
| 528 | return (kfsw_wallclock_set((int64_t)time->tv_sec) == 0) ? CSP_ERR_NONE : CSP_ERR_INVAL; | ||
| 529 | } | ||
| 530 | #endif /* CONFIG_KFSW_CSP_CLOCK_RTC */ | ||
| 531 | |||
| 532 | 850 | void kfsw_csp_clock_get(struct kfsw_csp_clock *clock) | |
| 533 | { | ||
| 534 | 850 | csp_timestamp_t now = {0}; | |
| 535 | |||
| 536 | 850 | if (clock == NULL) { | |
| 537 | ✗ | return; | |
| 538 | } | ||
| 539 | /* Same hook as libcsp, so the shell and remote callers see the same time. */ | ||
| 540 | 850 | csp_clock_get_time(&now); | |
| 541 | 850 | clock->seconds = (int32_t)now.tv_sec; | |
| 542 | 850 | clock->nanoseconds = now.tv_nsec; | |
| 543 | } | ||
| 544 | |||
| 545 | 778 | bool kfsw_csp_clock_is_set(const struct kfsw_csp_clock *clock) | |
| 546 | { | ||
| 547 | 778 | return (clock != NULL) && (clock->seconds >= CONFIG_KFSW_CSP_CLOCK_FLOOR); | |
| 548 | } | ||
| 549 | |||
| 550 | 26 | int kfsw_csp_clock_set(const struct kfsw_csp_clock *clock) | |
| 551 | { | ||
| 552 | csp_timestamp_t value; | ||
| 553 | |||
| 554 | 26 | if (clock == NULL) { | |
| 555 | ✗ | return -EINVAL; | |
| 556 | } | ||
| 557 | 26 | value.tv_sec = (uint32_t)clock->seconds; | |
| 558 | 26 | value.tv_nsec = clock->nanoseconds; | |
| 559 | |||
| 560 | 26 | return (csp_clock_set_time(&value) == CSP_ERR_NONE) ? 0 : -ENOTSUP; | |
| 561 | } | ||
| 562 | |||
| 563 | 12 | static int clock_transaction(uint16_t node, uint32_t timeout_ms, struct kfsw_csp_clock *clock, | |
| 564 | bool set) | ||
| 565 | { | ||
| 566 | 12 | struct csp_cmp_clock_msg message = {0}; | |
| 567 | int result; | ||
| 568 | |||
| 569 | /* libcsp does not mask a destination; out of range it goes to another node. */ | ||
| 570 | 12 | if ((clock == NULL) || (node == 0U) || (node >= KFSW_CSP_BROADCAST_ADDRESS)) { | |
| 571 | 8 | return -EINVAL; | |
| 572 | } | ||
| 573 | 4 | if (!initialized) { | |
| 574 | 4 | return -ENETDOWN; | |
| 575 | } | ||
| 576 | |||
| 577 | /* Zero seconds means read only; the other node answers with its time. */ | ||
| 578 | ✗ | if (set) { | |
| 579 | ✗ | message.clock.tv_sec = htobe32((uint32_t)clock->seconds); | |
| 580 | ✗ | message.clock.tv_nsec = htobe32(clock->nanoseconds); | |
| 581 | } | ||
| 582 | |||
| 583 | ✗ | result = csp_cmp_clock(node, timeout_ms, &message); | |
| 584 | ✗ | if (result != CSP_ERR_NONE) { | |
| 585 | ✗ | return -ETIMEDOUT; | |
| 586 | } | ||
| 587 | |||
| 588 | ✗ | clock->seconds = (int32_t)be32toh(message.clock.tv_sec); | |
| 589 | ✗ | clock->nanoseconds = be32toh(message.clock.tv_nsec); | |
| 590 | ✗ | return 0; | |
| 591 | } | ||
| 592 | |||
| 593 | 12 | int kfsw_csp_clock_read(uint16_t node, uint32_t timeout_ms, struct kfsw_csp_clock *clock) | |
| 594 | { | ||
| 595 | 12 | return clock_transaction(node, timeout_ms, clock, false); | |
| 596 | } | ||
| 597 | |||
| 598 | 4 | int kfsw_csp_clock_write(uint16_t node, uint32_t timeout_ms, struct kfsw_csp_clock *clock) | |
| 599 | { | ||
| 600 | 4 | if (!kfsw_csp_clock_is_set(clock)) { | |
| 601 | /* Don't send an unset clock: zero means read on the wire. */ | ||
| 602 | 4 | return -EINVAL; | |
| 603 | } | ||
| 604 | ✗ | return clock_transaction(node, timeout_ms, clock, true); | |
| 605 | } | ||
| 606 | |||
| 607 | ✗ | static void copy_identity_field(char *destination, size_t destination_size, const char *source, | |
| 608 | size_t source_size) | ||
| 609 | { | ||
| 610 | ✗ | size_t length = MIN(destination_size - 1U, source_size); | |
| 611 | |||
| 612 | ✗ | memcpy(destination, source, length); | |
| 613 | ✗ | destination[length] = '\0'; | |
| 614 | ✗ | } | |
| 615 | |||
| 616 | 56 | int kfsw_csp_interface_stats_read(uint16_t node, const char *name, uint32_t timeout_ms, | |
| 617 | struct kfsw_csp_interface_stats *stats) | ||
| 618 | { | ||
| 619 | 56 | struct csp_cmp_if_stats_msg message = {0}; | |
| 620 | size_t length; | ||
| 621 | |||
| 622 | BUILD_ASSERT(KFSW_CSP_INTERFACE_NAME_SIZE == CSP_CMP_ROUTE_IFACE_LEN); | ||
| 623 | 56 | if ((name == NULL) || (stats == NULL) || (timeout_ms == 0U) || | |
| 624 | 44 | (node >= (1UL << csp_id_get_host_bits()))) { | |
| 625 | 16 | return -EINVAL; | |
| 626 | } | ||
| 627 | 40 | length = strnlen(name, sizeof(message.interface)); | |
| 628 | 40 | if ((length == 0U) || (length == sizeof(message.interface))) { | |
| 629 | 8 | return -EINVAL; | |
| 630 | } | ||
| 631 | 32 | if (!initialized || !router_running) { | |
| 632 | 4 | return -ENETDOWN; | |
| 633 | } | ||
| 634 | 28 | memcpy(message.interface, name, length + 1U); | |
| 635 | 28 | if (csp_cmp_if_stats(node, timeout_ms, &message) != CSP_ERR_NONE) { | |
| 636 | 8 | return -ETIMEDOUT; | |
| 637 | } | ||
| 638 | 32 | if ((message.type != CSP_CMP_REPLY) || (message.code != CSP_CMP_IF_STATS) || | |
| 639 | 12 | (memcmp(message.interface, name, length + 1U) != 0)) { | |
| 640 | 12 | return -EBADMSG; | |
| 641 | } | ||
| 642 | |||
| 643 | 8 | struct kfsw_csp_interface_stats result = { | |
| 644 | 8 | .tx_packets = be32toh(message.tx), | |
| 645 | 8 | .rx_packets = be32toh(message.rx), | |
| 646 | 8 | .tx_errors = be32toh(message.tx_error), | |
| 647 | 8 | .rx_errors = be32toh(message.rx_error), | |
| 648 | 8 | .dropped_packets = be32toh(message.drop), | |
| 649 | 8 | .auth_errors = be32toh(message.autherr), | |
| 650 | 8 | .frame_errors = be32toh(message.frame), | |
| 651 | 8 | .tx_bytes = be32toh(message.txbytes), | |
| 652 | 8 | .rx_bytes = be32toh(message.rxbytes), | |
| 653 | 8 | .interrupts = be32toh(message.irq), | |
| 654 | }; | ||
| 655 | 8 | memcpy(result.name, name, length + 1U); | |
| 656 | 8 | *stats = result; | |
| 657 | 8 | return 0; | |
| 658 | } | ||
| 659 | |||
| 660 | ✗ | int kfsw_csp_identify(uint16_t node, uint32_t timeout_ms, struct kfsw_csp_identity *identity) | |
| 661 | { | ||
| 662 | ✗ | const unsigned int host_bits = csp_id_get_host_bits(); | |
| 663 | ✗ | struct csp_cmp_message message = {0}; | |
| 664 | int result; | ||
| 665 | |||
| 666 | BUILD_ASSERT(KFSW_CSP_IDENTITY_HOSTNAME_SIZE > CSP_HOSTNAME_LEN); | ||
| 667 | BUILD_ASSERT(KFSW_CSP_IDENTITY_MODEL_SIZE > CSP_MODEL_LEN); | ||
| 668 | BUILD_ASSERT(KFSW_CSP_IDENTITY_REVISION_SIZE > CSP_CMP_IDENT_REV_LEN); | ||
| 669 | BUILD_ASSERT(KFSW_CSP_IDENTITY_DATE_SIZE > CSP_CMP_IDENT_DATE_LEN); | ||
| 670 | BUILD_ASSERT(KFSW_CSP_IDENTITY_TIME_SIZE > CSP_CMP_IDENT_TIME_LEN); | ||
| 671 | |||
| 672 | ✗ | if (!initialized || !router_running || identity == NULL || node >= (1UL << host_bits)) { | |
| 673 | ✗ | return CSP_ERR_INVAL; | |
| 674 | } | ||
| 675 | |||
| 676 | ✗ | memset(identity, 0, sizeof(*identity)); | |
| 677 | |||
| 678 | ✗ | result = csp_cmp_ident(node, timeout_ms, &message); | |
| 679 | ✗ | if (result != CSP_ERR_NONE) { | |
| 680 | ✗ | return CSP_ERR_TIMEDOUT; | |
| 681 | } | ||
| 682 | |||
| 683 | ✗ | copy_identity_field(identity->hostname, sizeof(identity->hostname), message.ident.hostname, | |
| 684 | CSP_HOSTNAME_LEN); | ||
| 685 | ✗ | copy_identity_field(identity->model, sizeof(identity->model), message.ident.model, | |
| 686 | CSP_MODEL_LEN); | ||
| 687 | ✗ | copy_identity_field(identity->revision, sizeof(identity->revision), message.ident.revision, | |
| 688 | CSP_CMP_IDENT_REV_LEN); | ||
| 689 | ✗ | copy_identity_field(identity->date, sizeof(identity->date), message.ident.date, | |
| 690 | CSP_CMP_IDENT_DATE_LEN); | ||
| 691 | ✗ | copy_identity_field(identity->time, sizeof(identity->time), message.ident.time, | |
| 692 | CSP_CMP_IDENT_TIME_LEN); | ||
| 693 | |||
| 694 | ✗ | return CSP_ERR_NONE; | |
| 695 | } | ||
| 696 | |||
| 697 | 4 | int kfsw_csp_ping(uint16_t node, uint32_t timeout_ms, size_t payload_size, uint32_t *round_trip_us) | |
| 698 | { | ||
| 699 | 4 | const unsigned int host_bits = csp_id_get_host_bits(); | |
| 700 | uint64_t start_us; | ||
| 701 | int elapsed_ms; | ||
| 702 | |||
| 703 | 4 | if (!initialized || !router_running || round_trip_us == NULL || | |
| 704 | ✗ | node >= (1UL << host_bits) || payload_size > CSP_BUFFER_SIZE) { | |
| 705 | 4 | return CSP_ERR_INVAL; | |
| 706 | } | ||
| 707 | |||
| 708 | ✗ | *round_trip_us = 0; | |
| 709 | /* libcsp counts in system ticks, 10 ms on native_sim; time it here instead. */ | ||
| 710 | ✗ | start_us = kfsw_time_monotonic_us(); | |
| 711 | ✗ | elapsed_ms = csp_ping(node, timeout_ms, (unsigned int)payload_size, | |
| 712 | (CSP_O_CRC32 | KFSW_CSP_AUTHENTICATED_CONNECTION)); | ||
| 713 | ✗ | if (elapsed_ms < 0) { | |
| 714 | ✗ | return CSP_ERR_TIMEDOUT; | |
| 715 | } | ||
| 716 | |||
| 717 | ✗ | *round_trip_us = (uint32_t)MIN(kfsw_time_monotonic_us() - start_us, UINT32_MAX); | |
| 718 | ✗ | return CSP_ERR_NONE; | |
| 719 | } | ||
| 720 |