canbus:   Linux Socket  CAN frames for Racket
1 Layering
2 Quick start
3 Frames
can-frame
make-can-frame
can-frame->bytes
can-frame->fd-bytes
bytes->can-frame
can-frame-classic?
4 Interfaces
can-interfaces
5 Transport
canbus-available?
open-can
can-send!
can-recv!
close-can!
call-with-can-bus
can-bus-open?
can-bus-drops
6 Notes
9.3

canbus: Linux SocketCAN frames for Racket🔗ℹ

turlamai

 (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?
One CAN or CAN FD frame. id is the 11-bit (standard) or 29-bit (extended) identifier, data the payload (at most 8 bytes classic, 64 FD), extended? selects the identifier format, and remote? (RTR), error? (CAN_ERR), brs? (bitrate switch) and esi? (error state indicator) map to the kernel flags of the same names. A frame needs the FD wire layout when it sets brs?/esi? or carries more than 8 payload bytes (can-frame-classic?).

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
Validating constructor: the identifier must fit the selected format, the payload the selected layout, and remote requests carry neither data nor FD flags.

procedure

(can-frame->bytes f) → bytes?

  f : can-frame?
Encodes one struct can_frame (16 bytes, little-endian). Raises for frames that need the FD layout.

procedure

(can-frame->fd-bytes f) → bytes?

  f : can-frame?
Encodes one struct canfd_frame (72 bytes); the FD flags byte carries BRS and ESI. Raises for remote requests (CAN FD has none).

procedure

(bytes->can-frame buf [n]) → can-frame?

  buf : bytes?
  n : exact-nonnegative-integer? = (bytes-length buf)
Decodes one kernel frame: a 16-byte buffer as can_frame, a 72-byte one as canfd_frame. Remote requests decode with empty data (the requested-length DLC has no representation in the model); illegal lengths raise.

procedure

(can-frame-classic? f) → boolean?

  f : can-frame?
Whether the frame fits the classic wire layout.

4 Interfaces🔗ℹ

procedure

(can-interfaces) → (listof string?)

Names of the system’s CAN interfaces (physical and vcan), read from /sys/class/net by interface type. Empty off Linux. A discovery helper only: opening an interface does not require it to be listed here.

5 Transport🔗ℹ

procedure

(canbus-available?) → boolean?

Whether the transport can run here, checked for real: the system must be Linux with the needed libc symbols, and one throwaway socket(AF_CAN, SOCK_RAW, CAN_RAW) must succeed (some hosted virtualization kernels refuse AF_CAN even with the kernel modules loaded). The result is cached after the first call.

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
Opens one raw CAN socket bound to interface-name, selected explicitly by name; an unknown interface raises with a hint for bringing it up. #:fd? #t enables CAN FD frames on the socket. A dedicated reader thread fills the receive queue, which holds at most queue-size frames — overflow drops the oldest frame, because CAN is latest-wins — and counts the drops (can-bus-drops).

Errors raise exn:fail:canbus.

procedure

(can-send! bus frame) → void?

  bus : can-bus?
  frame : can-frame?
Sends one frame. FD layout is used exactly when the frame needs it (FD flags or more than 8 payload bytes); sending such a frame on a classic socket raises.

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
Pulls one frame from the receive queue. With #:timeout-ms #f (the default) the call blocks until a frame arrives; with a timeout it is bounded and returns #f when the deadline passes (#:timeout-ms 0 is a non-blocking poll). Bounded receiving means no caller ever waits forever on a silent bus.

procedure

(close-can! bus) → void?

  bus : can-bus?
Stops the reader thread, closes the socket, and makes every later operation on the bus raise. Idempotent.

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
Opens a bus, calls proc with it, and closes it — including when proc escapes with an exception.

procedure

(can-bus-open? bus) → boolean?

  bus : can-bus?
Whether the bus’s socket is open.

procedure

(can-bus-drops bus) → exact-nonnegative-integer?

  bus : can-bus?
How many receive frames the queue dropped over the bus’s lifetime — the observable cost of a slow consumer.

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.