Gpiod_line_event_get_fd returns -1 due to operation not permitted

gpiod_line_event_get_fd() function is returning -1 and the last error occured is Operation Not Permitted.

How to make it work?

  1. Check User Permissions: Ensure that the user running your application has the necessary permissions to access the GPIO devices. This can usually be achieved by adding the user to a group that has access to the GPIO devices, often gpio or dialout on many Linux systems. You can add a user to the gpio group with a command like sudo usermod -a -G gpio yourusername, then log out and back in for the changes to take effect.
  2. Verify GPIO Configuration: Make sure that the GPIO line you are trying to access has been correctly exported and configured for event detection. This involves setting the correct direction (input or output) and enabling the desired event types (rising edge, falling edge, etc.).
  3. Use sudo for Testing: As a quick test to see if permissions are indeed the issue, try running your application with sudo to provide it with root access. If the function works as expected when run as root, this confirms that the issue is related to user permissions.
  4. Check Device Tree Configuration: On Toradex modules, ensure that the GPIO line you’re trying to access is not being used by the system for another purpose and is correctly configured in the device tree. Conflicts or incorrect configurations in the device tree can lead to access issues.