-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add functionality for setting up CAN devices
Adds the "candevice" package which provides functions for interacting with CAN devices on the link layer. Allows for setting bitrate, setting device up/down, and retrieving info and stats from the device. Added integration tests for the candevice package, for testing against real hardware.
- Loading branch information
1 parent
0278ae5
commit 8d419f8
Showing
9 changed files
with
676 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
.idea | ||
tools/*/*/ | ||
build/ | ||
|
||
# files generated during release | ||
.generated-go-semantic-release-changelog.md | ||
.semrel/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
//go:build integration | ||
|
||
package candevice | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
const ( | ||
bitrate125K = 125000 | ||
bitrate250K = 250000 | ||
) | ||
|
||
func TestSetBitrate(t *testing.T) { | ||
d, err := New("can0") | ||
if err != nil { | ||
t.Fatal("couldn't set up device:", err) | ||
} | ||
defer d.SetDown() | ||
|
||
if err := setBitrate(d, bitrate125K); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := setBitrate(d, bitrate250K); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Set bitrate on device which is up | ||
if err := d.SetUp(); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := setBitrate(d, bitrate125K); err == nil { | ||
t.Fatal("setting bitrate on device which is up succeeded") | ||
} | ||
if err := d.SetDown(); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Invalid bitrate | ||
if err := setBitrate(d, 0); err == nil { | ||
t.Fatal("setting invalid bitrate succeeded") | ||
} | ||
} | ||
|
||
func TestSetUpDown(t *testing.T) { | ||
d, err := New("can0") | ||
if err != nil { | ||
t.Fatal("couldn't set up device:", err) | ||
} | ||
defer d.SetDown() | ||
|
||
if err := d.SetBitrate(bitrate125K); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Set up twice and set down twice. This checks that calling it twice has no effect | ||
if err := setUp(d); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := setUp(d); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := setDown(d); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := setDown(d); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func setBitrate(d *Device, bitrate uint32) error { | ||
if err := d.SetBitrate(bitrate); err != nil { | ||
return err | ||
} | ||
if err := setUp(d); err != nil { | ||
return err | ||
} | ||
actualBitrate, err := d.Bitrate() | ||
if err != nil { | ||
return err | ||
} | ||
if err := setDown(d); err != nil { | ||
return err | ||
} | ||
if actualBitrate != bitrate { | ||
return fmt.Errorf("expected bitrate: %d, actual: %d", bitrate, bitrate) | ||
} | ||
return nil | ||
} | ||
|
||
func setUp(d *Device) error { | ||
if err := d.SetUp(); err != nil { | ||
return err | ||
} | ||
|
||
isUp, err := d.IsUp() | ||
if err != nil { | ||
return err | ||
} | ||
if !isUp { | ||
return fmt.Errorf("device not up after calling SetUp()") | ||
} | ||
return nil | ||
} | ||
|
||
func setDown(d *Device) error { | ||
if err := d.SetDown(); err != nil { | ||
return err | ||
} | ||
isUp, err := d.IsUp() | ||
if err != nil { | ||
return err | ||
} | ||
if isUp { | ||
return fmt.Errorf("device not down after calling SetDown()") | ||
} | ||
return nil | ||
} |
Oops, something went wrong.