How libcsp/libcsp works
Project Type and Purpose
• This is an open-source, modular C library implementing the Cubesat Space Protocol (CSP), a lightweight TCP/IP-like stack for embedded systems.
• It provides a connection-oriented and connection-less transport, routing, quality-of-service, and a socket API, running on multiple OS targets (FreeRTOS, Zephyr, POSIX/Linux).
High-Level Architectural Layers
a. Application Layer
• Examples and user programs (e.g., examples/, Python clients) invoking the CSP socket API.
b. API Layer
• Thread-safe socket API and service interfaces (src/csp_*, include/csp/*.h).
c. Core Protocol Stack
• Transport: connection management (csp_conn, csp_rdp, csp_sfp)
• Routing: router task, routing table (csp_route, csp_rtable_*)
• Buffer & Queue management: zero-copy buffers, queues (csp_buffer, csp_qfifo)
• Utilities: CRC, HMAC/sha1 (src/crypto, src/csp_crc32, src/csp_hmac)
d. Abstraction Layer
• OS abstraction (src/arch/*): semaphores, clocks, queues, system calls for FreeRTOS, Zephyr, POSIX
• Network interface abstraction (src/interfaces/*): CAN, UDP, KISS, loopback, ZMQ hub
• Driver implementations (src/drivers/*): CAN socketcan, eth_linux, usart_kiss, etc.
e. Bindings & Tools
• Python bindings (src/bindings/python/pycsp.c)
• Build scripts and CI configurations (CMakeLists.txt, meson.build, .github/workflows)
f. Documentation & Tests
• Sphinx docs (doc/), unittests (unittests/), examples illustrating usage
Component Relationships and Data Flows
• Applications call the CSP socket API → Core CSP transports calls to router and connection modules → Core CSP requests buffer allocation → Abstraction layer provides OS primitives (mutexes, queues) → CSP passes packets down to interface plugins → Drivers transmit frames on physical/virtual media → Incoming frames follow reverse path into the core and up to applications.
• Crypto modules are invoked within the transport layer for integrity/authentication.
• OS abstraction “arch” modules sit between core and real RTOS or Linux.
Key Architectural Patterns
• Layered design: clear separation between API, core logic, abstraction, and hardware/drivers.
• Plugin modules: compile-time inclusion of specific interfaces and drivers.
• OS-agnostic core: single codebase for different RTOS and POSIX environments.
• Zero-copy buffer and queue system for efficiency in embedded contexts.