-
Notifications
You must be signed in to change notification settings - Fork 617
cmd/snap-gpio-helper: add gpio-chardev export/unexport commands #15212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ZeyadYasser
merged 21 commits into
canonical:master
from
ZeyadYasser:gpiod-helper-commands
Apr 17, 2025
+1,357
−14
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
9422df2
many: refactor gpio chardev virtual device path
ZeyadYasser c99c29e
cmd/snap-gpio-helper: add gpio-chardev export command
ZeyadYasser 33c2047
cmd/snap-gpio-helper: add gpio-chardev unexport command
ZeyadYasser 046a348
cmd/snap-gpio-helper: require experimental.gpio-chardev-interface fla…
ZeyadYasser d9818b9
fixup! cmd/snap-gpio-helper: address review comments
ZeyadYasser ee46a1c
fixup! many: move gpio chip helpers into gadget/device package
ZeyadYasser c53bd22
fixup! cmd/snap-gpio-helper: fix unit test race
ZeyadYasser 774d8a5
fixup! many: refactor gpio helpers into separate sandbox/gpio package
ZeyadYasser ff050fd
fixup! many: address review comments
ZeyadYasser 8cca557
fixup! many: fix mac os build tests
ZeyadYasser 81c16cd
fixup! sandbox/gpio: fix typos
ZeyadYasser 406a0e4
osutil: add device major/minor number helpers
ZeyadYasser 561bbfd
fixup! sandbox/gpio: address review comments
ZeyadYasser 71e9ff6
fixup! many: address review comments
ZeyadYasser 422970e
fixup! osutil: fix failing mac os unit tests
ZeyadYasser 0f0c4d1
fixup! osutil: remove dependency on CGO for device major/minor helpers
ZeyadYasser 172d333
fixup! sandbox/gpio: fix mixing fs.FileMode and raw syscall file
ZeyadYasser b3752c1
fixup! sandbox/gpio: fix failing mac os tests
ZeyadYasser 168593e
fixup! many: address review comments
ZeyadYasser 200b180
fixup! sandbox/gpio: add stat nil check
ZeyadYasser 0d11203
fixup! many: address review comments
ZeyadYasser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,75 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2025 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package main_test | ||
|
||
import ( | ||
"context" | ||
|
||
. "gopkg.in/check.v1" | ||
|
||
main "github.com/snapcore/snapd/cmd/snap-gpio-helper" | ||
"github.com/snapcore/snapd/strutil" | ||
) | ||
|
||
func (s *snapGpioHelperSuite) TestExportGpioChardevBadLine(c *C) { | ||
called := 0 | ||
restore := main.MockGpioExportGadgetChardevChip(func(ctx context.Context, chipLabels []string, lines strutil.Range, gadgetName, slotName string) error { | ||
called++ | ||
return nil | ||
}) | ||
defer restore() | ||
|
||
for lines, expectedErr := range map[string]string{ | ||
"0-2,1": `invalid lines argument: overlapping range span found "1"`, | ||
"1-0": `invalid lines argument: invalid range span "1-0": ends before it starts`, | ||
"0-": `invalid lines argument: .*: invalid syntax`, | ||
"a": `invalid lines argument: .*: invalid syntax`, | ||
} { | ||
err := main.Run([]string{ | ||
"export-chardev", "label-0", lines, "gadget-name", "slot-name", | ||
}) | ||
c.Check(err, ErrorMatches, expectedErr) | ||
} | ||
|
||
c.Assert(called, Equals, 0) | ||
} | ||
|
||
func (s *snapGpioHelperSuite) TestExportGpioChardev(c *C) { | ||
called := 0 | ||
restore := main.MockGpioExportGadgetChardevChip(func(ctx context.Context, chipLabels []string, lines strutil.Range, gadgetName, slotName string) error { | ||
called++ | ||
c.Check(chipLabels, DeepEquals, []string{"label-0", "label-1"}) | ||
c.Check(lines, DeepEquals, strutil.Range{ | ||
{Start: 0, End: 6}, | ||
{Start: 7, End: 7}, | ||
{Start: 8, End: 100}, | ||
}) | ||
c.Check(gadgetName, Equals, "gadget-name") | ||
c.Check(slotName, Equals, "slot-name") | ||
return nil | ||
}) | ||
defer restore() | ||
|
||
err := main.Run([]string{ | ||
"export-chardev", "label-0,label-1", "7,0-6,8-100", "gadget-name", "slot-name", | ||
}) | ||
c.Check(err, IsNil) | ||
c.Assert(called, Equals, 1) | ||
} |
This file contains hidden or 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 hidden or 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,43 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2025 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package main_test | ||
|
||
import ( | ||
. "gopkg.in/check.v1" | ||
|
||
main "github.com/snapcore/snapd/cmd/snap-gpio-helper" | ||
) | ||
|
||
func (s *snapGpioHelperSuite) TestUnexportGpioChardev(c *C) { | ||
called := 0 | ||
restore := main.MockGpioUnxportGadgetChardevChip(func(gadgetName, slotName string) error { | ||
called++ | ||
c.Check(gadgetName, Equals, "gadget-name") | ||
c.Check(slotName, Equals, "slot-name") | ||
return nil | ||
}) | ||
defer restore() | ||
|
||
err := main.Run([]string{ | ||
"unexport-chardev", "label-0,label-1", "7,0-6,8-100", "gadget-name", "slot-name", | ||
}) | ||
c.Check(err, IsNil) | ||
c.Assert(called, Equals, 1) | ||
} |
This file contains hidden or 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,38 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2025 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/snapcore/snapd/strutil" | ||
"github.com/snapcore/snapd/testutil" | ||
) | ||
|
||
var ( | ||
Run = run | ||
) | ||
|
||
func MockGpioExportGadgetChardevChip(f func(ctx context.Context, chipLabels []string, lines strutil.Range, gadgetName string, slotName string) error) (restore func()) { | ||
return testutil.Mock(&gpioExportGadgetChardevChip, f) | ||
} | ||
|
||
func MockGpioUnxportGadgetChardevChip(f func(gadgetName string, slotName string) error) (restore func()) { | ||
return testutil.Mock(&gpioUnexportGadgetChardevChip, f) | ||
} |
This file contains hidden or 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 hidden or 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,64 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2025 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package main_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
. "gopkg.in/check.v1" | ||
|
||
main "github.com/snapcore/snapd/cmd/snap-gpio-helper" | ||
"github.com/snapcore/snapd/dirs" | ||
"github.com/snapcore/snapd/features" | ||
"github.com/snapcore/snapd/testutil" | ||
) | ||
|
||
// Hook up check.v1 into the "go test" runner | ||
func Test(t *testing.T) { TestingT(t) } | ||
|
||
type snapGpioHelperSuite struct { | ||
testutil.BaseTest | ||
} | ||
|
||
var _ = Suite(&snapGpioHelperSuite{}) | ||
|
||
func (s *snapGpioHelperSuite) SetUpTest(c *C) { | ||
dirs.SetRootDir(c.MkDir()) | ||
s.AddCleanup(func() { dirs.SetRootDir("") }) | ||
|
||
// Mock experimental.gpio-chardev-interface | ||
c.Assert(os.MkdirAll(dirs.FeaturesDir, 0755), IsNil) | ||
c.Assert(os.WriteFile(features.GPIOChardevInterface.ControlFile(), []byte(nil), 0644), IsNil) | ||
} | ||
|
||
func (s *snapGpioHelperSuite) TestGpioChardevExperimentlFlagUnset(c *C) { | ||
c.Assert(os.RemoveAll(features.GPIOChardevInterface.ControlFile()), IsNil) | ||
|
||
err := main.Run([]string{ | ||
"export-chardev", "label-0", "0,2", "gadget-name", "slot-name", | ||
}) | ||
c.Check(err, ErrorMatches, `gpio-chardev interface requires the "experimental.gpio-chardev-interface" flag to be set`) | ||
|
||
err = main.Run([]string{ | ||
"unexport-chardev", "label-0", "0,2", "gadget-name", "slot-name", | ||
}) | ||
c.Check(err, ErrorMatches, `gpio-chardev interface requires the "experimental.gpio-chardev-interface" flag to be set`) | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.