DEV Community

Nikita Vakula
Nikita Vakula

Posted on

Simulating GPIO Sysfs Interface with FUSE and C++

Developing embedded systems often involves interacting with General-Purpose Input/Output (GPIO) pins to control various aspects of the device. The GPIO sysfs interface provides a crucial pathway for accessing and manipulating these pins. However, the lack of physical access to hardware can sometimes hinder development progress. This is where simulating the GPIO sysfs interface with FUSE (Filesystem in Userspace) and C++ comes into play, offering a valuable tool for:

  1. Accelerating development cycles:Bypass the need for physical hardware during testing and development phases, saving time and resources.
  2. Enhancing testing capabilities: Craft comprehensive test scenarios without relying on actual hardware, leading to more robust and reliable embedded software.
  3. Easier experimentation and learning: Explore GPIO functionalities freely in a virtual environment, allowing for deeper understanding.

The motivation

The inspiration for this project came from a real-world scenario where access to the physical embedded device was not always possible. Working with GPIO sysfs in such conditions can be cumbersome. Initially considering LD_PRELOAD for code injection, the complexities involved led to the exploration of a more elegant solution - FUSE. It allows building custom filesystems in the user space, making it ideal for emulating the GPIO sysfs interface. With FUSE, you gain:

  1. A user-friendly environment: Create a virtual representation of your embedded device's GPIO pins, complete with directories and files that mirror the real-world counterparts.
  2. Seamless interaction: Read and write to these virtual pins, simulating input and output operations on your physical device.
  3. Efficient integration: Leverage the Linux kernel's capabilities for tasks like asynchronous monitoring and pin state changes, ensuring a realistic and responsive experience.
  4. Focus on clean code: FUSE handles complex state management, allowing you to concentrate on writing clear and maintainable code for your project.

Essential Filesystem Hooks

The core of this project lies in implementing key filesystem hooks with libfuse:

  • getattr: Returns information about a file or directory, crucial for file system navigation.

  • readdir: Lists the contents of a directory, enabling users to explore the simulated GPIO pins.
    read: Facilitates reading the state of GPIO pins, providing valuable information to clients.

  • write: Allows users to modify the state of GPIO pins, mimicking real-world interactions.

  • poll: Notifies the kernel of changes in GPIO pin states, enabling asynchronous monitoring through tools like epoll.

Automatic Pin Management

The project also automates the management of GPIO pins when writing to /export or /unexport. This mimics the behavior of the kernel, automatically adding or removing entries based on user input.
Additionally, you can explore advanced features like:

  • Error handling: Simulate real-world scenarios where pin access might be restricted or encounter errors, providing a comprehensive testing environment.

  • Performance monitoring: Track the execution time of different operations and resource usage to optimize your embedded code.


While LD_PRELOAD is a powerful tool for code injection, it introduces complexities in managing the state of each read operation. FUSE, being a complete userspace filesystem, provides a cleaner and more maintainable solution.


Simulating GPIO sysfs using FUSE and C++ offers a convenient and powerful solution for developers facing limitations in accessing embedded devices. Whether for testing, development, or simply experimenting with GPIO pins, this project provides a flexible and user-friendly alternative to the traditional approach. The combination of libfuse and C++ makes the implementation straightforward, allowing developers to focus on their projects rather than wrestling with hardware constraints.
Feel free to explore the project on GitHub, and don't hesitate to contribute or share your feedback. Happy coding!

Top comments (0)