Skip to content

[can, test, docs] Add generic CAN device type - #68

Open
AlexLanzano wants to merge 2 commits into
wolfSSL:mainfrom
AlexLanzano:can
Open

AlexLanzano wants to merge 2 commits into
wolfSSL:mainfrom
AlexLanzano:can

Conversation

@AlexLanzano

@AlexLanzano AlexLanzano commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Adds a generic CAN device type covering classic CAN and CAN FD. No platform drivers yet.

API

  • whal_Can_Init / whal_Can_Deinit
  • whal_Can_Send: queues one frame; the generic layer rejects frames the format can't encode
  • whal_Can_Recv: polls for one frame, WHAL_ENOTREADY when none
  • whal_Can_GetState: error state and TX/RX error counters
  • whal_Can_Recover: manual bus-off recovery

Design

  • Bit timing, mode and acceptance filters are static driver config applied in Init. No runtime filter API.
  • whal_Can_Frame uses a byte length. whal_Can_LenToDlc / whal_Can_DlcToLen convert to and from DLC codes for drivers.
  • WHAL_CFG_CAN_FD sizes the payload for 64 bytes; it is 8 bytes without it.
  • Bus-off recovery is never automatic; the application decides when to call Recover.

Tests

  • CAN dispatch and frame validation tests in tests/core.
  • -DWHAL_CFG_CAN_FD added to the core-tests CI matrix.

@AlexLanzano
AlexLanzano requested review from wolfSSL-Fenrir-bot and a balanced review from Copilot December 4, 2026 03:44
@AlexLanzano AlexLanzano self-assigned this Dec 4, 2026

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #68

Scan targets checked: wolfhal-src, wolfhal-bugs
Not reviewed (no scan target covers them): docs/writing_a_driver.md, tests/core/Makefile
Coverage: 3 of 4 in-scope changed file(s) opened by the reviewer; not opened: wolfHAL/wolfHAL.h

Findings: 2
2 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Reported findings require changes before merge.

Review tier: Max

Comment thread wolfHAL/can/can.h Resolved
Comment thread tests/core/test_dispatch.c Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Classic CAN validation still accepts the unencodable ESI flag, and the receive guidance uses FD length decoding that can overrun an 8-byte buffer.

2 open issues
What changed in this PR

Adds a generic CAN device interface to wolfHAL, supporting classic CAN and CAN FD through driver dispatch.

Changes:

  • Defines CAN frames, filters, state reporting, and payload-length helpers.
  • Adds dispatch functions with transmit-frame validation.
  • Adds core tests and CAN driver documentation.
File Description
wolfHAL/​wolfHAL.h Exposes the CAN interface through the umbrella header.
wolfHAL/​can/​can.h Defines CAN types, driver interface, and public API.
tests/​core/​test_dispatch.c Adds CAN validation, dispatch, and length-conversion tests.
tests/​core/​Makefile Includes CAN implementation in core tests.
src/​can/​can.c Implements validation and driver dispatch.
docs/​writing_a_driver.md Documents CAN driver responsibilities.

Review effort: Balanced

Comment thread src/can/can.c Resolved
Comment thread docs/writing_a_driver.md Resolved
@AlexLanzano
AlexLanzano force-pushed the can branch 2 times, most recently from 8727965 to a834627 Compare December 4, 2026 04:08
@AlexLanzano
AlexLanzano requested review from wolfSSL-Fenrir-bot and a balanced review from Copilot December 4, 2026 04:09

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #68

Scan targets checked: wolfhal-src, wolfhal-bugs
Not reviewed (no scan target covers them): .github/workflows/core-tests.yml, docs/writing_a_driver.md, tests/core/Makefile
Coverage: 0 of 3 in-scope changed file(s) opened by the reviewer; not opened: src/can/can.c, tests/core/test_dispatch.c, wolfHAL/can/can.h

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Lite

@wolfSSL-Fenrir-bot
wolfSSL-Fenrir-bot dismissed their stale review December 4, 2026 04:09

Fenrir's latest completed scan found no issues; clearing the prior automated change request.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The review identifies no active issues with the new generic CAN abstraction for classic CAN and CAN FD.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@LinuxJedi LinuxJedi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of things I flagged. I recommend adding a loopback test if you can. SocketCAN in Linux or amd-versal2-virt which you are already running can add virtual CAN-FD devices:

https://www.qemu.org/docs/master/system/arm/xlnx-versal-virt.html#using-canfds-for-versal-virt

Comment thread wolfHAL/can/can.h Outdated
0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64
};

return lens[dlc & 0xF];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong, and I think the resolved Fenrir issue flagged it too. In CAN-FD, a DLC of 9-15 with FDF set to 0 means a length of 8.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I only fixed the overflow portion of that issue. Missed this.
Fixed.

Comment thread src/can/can.c
frame->len > WHAL_CAN_CLASSIC_MAX_DATA_LEN)
return WHAL_EINVAL;

return WHAL_SUCCESS;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If invalid flags are passed, such as 0x80 for example, this will succeed. It should probably fail.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Add can.h with the frame and filter types and the Init, Deinit, Send,
Recv, GetState and Recover API, plus the can.c dispatcher and its tests.
Round-trips standard, extended, zero-length, remote and FD frames through BOARD_CAN_DEV in loopback mode.
@AlexLanzano

Copy link
Copy Markdown
Member Author

A couple of things I flagged. I recommend adding a loopback test if you can. SocketCAN in Linux or amd-versal2-virt which you are already running can add virtual CAN-FD devices:

https://www.qemu.org/docs/master/system/arm/xlnx-versal-virt.html#using-canfds-for-versal-virt

I just added a generic loopback test. Was holding off on this until we had a driver implementation but it doesnt hurt to add it now.

@AlexLanzano
AlexLanzano requested a review from LinuxJedi December 4, 2026 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants