go-gpiocdev: Permission Denied on only SetValue()
I have the following code for opening and closing GPIO pins. I double checked the pins I’m using, they are for sure Output pins and I’m configuring them as output at the start of the program by also setting them defautl to 0 and asActiveHigh. I checked my permissions on the Raspberry and I’m the god in the machine. I can easily use pinctrl set command with sudo or without sudo to change the pin states and confirm it with raspi-gpio get that they are changed.
My problem is, when my app tries to set pin value to 1 or 0, I get permission denied from line.SetValue() method. I tried running my program as sudo in the raspberry or as root but made no difference, that’s why I’m thinking there might be a bug in the code or I might be missing something. Here is my function:
func (g *GPIOEngine) toggleIOPin(chipname string, pinNumber string, activeState int) error {
pin, err := rpi.Pin(pinNumber)
if err != nil {
g.logger.Debug("Failed to get pin")
return err
}
line, err := g.chips[chipname].RequestLine(pin)
if err != nil {
g.logger.Debug("Failed to request line")
return err
}
defer line.Close()
err = line.SetValue(activeState)
if err != nil {
g.logger.Debug("Failed to set value")
return err
}
return nil
}
and this is how I initialize g.chips to make things clear for you:
for _, chipName := range gpiocdev.Chips() {
logger.Debugf("Chip: %s Initialized", chipName)
chip, err := gpiocdev.NewChip(chipName)
if err != nil {
return nil, err
}
chips[chipName] = chip
}
Error:
[DEBUG]: 2024/05/30 22:36:00 /home/guney/workspace/gronpio/internal/engines/gpio_engine.go:135: Failed to set value
[ERROR]: 2024/05/30 22:36:00 /home/guney/workspace/gronpio/internal/services/output_devices_service.go:53: permission denied
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 15 (7 by maintainers)
If you insist then I need to explain further…
It is required by the kernel that the line is requested as an output before you can set it. This is to demonstrate that you know that is what you want, and not allow you to go messing with output lines by accident. If you request the line as-is then you can still read the current value before reconfiguring the line as an output to change that value.
Sorry if that feels redundant to you, but that is the way it is and I’m not about to change it.