canbus: Linux SocketCAN frames for Racket
| (require canbus) | package: canbus |
This package is a frame-level transport for the Linux kernel’s SocketCAN interface: it opens a raw CAN socket on one explicitly named interface, exchanges classic CAN and CAN FD frames in the kernel’s own wire layouts (struct can_frame and struct canfd_frame), bounds receiving both in time and in queue size, and shuts down cleanly.
The wire-frame codec is pure Racket and runs everywhere; the transport uses the C library and works on Linux only (virtual vcan interfaces included). DBC decoding, ISO-TP, diagnostics and vehicle workflows stay in their own family packages (dbc, Flashpilot, S2S).
1 Layering
(canbus frame) —
the frame model and the kernel wire codec: classic and FD layouts, EFF/RTR/ERR flag composition, identifier masks, validating construction. (canbus interfaces) —
discovery of the system’s CAN interfaces via /sys/class/net. (canbus bus) —
the transport: explicit interface selection, frame I/O, bounded receive, clean shutdown. (canbus logging) —
the package logger, routed through Racket’s standard logger facilities.
2 Quick start
Bring up a virtual bus (Linux):
sudo modprobe vcan |
sudo ip link add dev vcan0 type vcan |
sudo ip link set up vcan0 |
Send and receive frames with two sockets (a raw socket does not receive its own frames by default):
(require canbus) (define tx (open-can "vcan0")) (define rx (open-can "vcan0" #:fd? #t)) (can-send! tx (make-can-frame 291 #"ping??")) (can-recv! rx #:timeout-ms 500) (can-send! rx (make-can-frame 32506645 (make-bytes 64 205) #:extended? #t #:brs? #t)) (can-send! tx (make-can-frame 256 #"" #:remote? #t)) (can-recv! rx #:timeout-ms 500) (close-can! tx) (close-can! rx)
call-with-can-bus scopes the socket with guaranteed shutdown:
(call-with-can-bus "vcan0" (lambda (bus) (can-send! bus (make-can-frame 801 #"scoped")) (can-recv! bus #:timeout-ms 250)))
3 Frames
struct
(struct can-frame (id data extended? remote? error? brs? esi?))
id : exact-nonnegative-integer? data : bytes? extended? : boolean? remote? : boolean? error? : boolean? brs? : boolean? esi? : boolean?
procedure
(make-can-frame id data [ #:extended? extended? #:remote? remote? #:error? error? #:brs? brs? #:esi? esi?]) → can-frame? id : exact-nonnegative-integer? data : bytes? extended? : boolean? = #f remote? : boolean? = #f error? : boolean? = #f brs? : boolean? = #f esi? : boolean? = #f
procedure
(can-frame->bytes f) → bytes?
f : can-frame?
procedure
(can-frame->fd-bytes f) → bytes?
f : can-frame?
procedure
(bytes->can-frame buf [n]) → can-frame?
buf : bytes? n : exact-nonnegative-integer? = (bytes-length buf)
procedure
(can-frame-classic? f) → boolean?
f : can-frame?
4 Interfaces
procedure
(can-interfaces) → (listof string?)
5 Transport
procedure
procedure
(open-can interface-name [ #:fd? fd? #:queue-size queue-size]) → can-bus? interface-name : string? fd? : boolean? = #f queue-size : exact-positive-integer? = 1024
Errors raise exn:fail:canbus.
procedure
bus : can-bus? frame : can-frame?
procedure
(can-recv! bus [#:timeout-ms timeout-ms]) → (or/c can-frame? #f)
bus : can-bus? timeout-ms : (or/c #f exact-nonnegative-integer?) = #f
procedure
(close-can! bus) → void?
bus : can-bus?
procedure
(call-with-can-bus interface-name proc [ #:fd? fd? #:queue-size queue-size]) → any interface-name : string? proc : (-> can-bus? any) fd? : boolean? = #f queue-size : exact-positive-integer? = 1024
procedure
(can-bus-open? bus) → boolean?
bus : can-bus?
procedure
bus : can-bus?
6 Notes
Catalog name: the package is canbus because the name socketcan was registered in 2015 by an abandoned third-party package whose builds have failed for years. This repository (turlamai/canbus) is the only maintained implementation.
The reader thread polls the socket with MSG_DONTWAIT and sleeps ~5ms when idle: blocking foreign calls would stall every Racket thread, so the transport never blocks inside recv().
Physical adapter qualification (bit-timing, error handling on real silicon) is tracked in the repository’s implementation issue, separate from the automated virtual-CAN suite.