-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from tiagocoutinho/gpio
Add modprobe info to github actions
- Loading branch information
Showing
14 changed files
with
891 additions
and
22 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
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,13 @@ | ||
# | ||
# This file is part of the linuxpy project | ||
# | ||
# Copyright (c) 2024 Tiago Coutinho | ||
# Distributed under the GPLv3 license. See LICENSE for more info. | ||
|
||
from pathlib import Path | ||
|
||
from linuxpy.types import Optional | ||
|
||
from .mounts import configfs | ||
|
||
CONFIGFS_PATH: Optional[Path] = configfs() |
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,47 @@ | ||
# | ||
# This file is part of the linuxpy project | ||
# | ||
# Copyright (c) 2024 Tiago Coutinho | ||
# Distributed under the GPLv3 license. See LICENSE for more info. | ||
|
||
""" | ||
## Configfs GPIO Simulator | ||
The configfs GPIO Simulator (gpio-sim) provides a way to create simulated GPIO chips | ||
for testing purposes. The lines exposed by these chips can be accessed using the | ||
standard GPIO character device interface as well as manipulated using sysfs attributes. | ||
To be able to create GPIOSim chips, the user will need permissions. | ||
Here is an example of what to put in /etc/udev/rules.d/80-gpio-sim.rules that gives | ||
access to users belonging to the `input` group: | ||
``` | ||
KERNEL=="gpio-sim", SUBSYSTEM=="config", RUN+="/bin/chown -R root:input /sys/kernel/config/gpio-sim" | ||
KERNEL=="gpio-sim", SUBSYSTEM=="config", RUN+="/bin/chmod -R 775 /sys/kernel/config/gpio-sim" | ||
``` | ||
See https://www.kernel.org/doc/html/latest/admin-guide/gpio/gpio-sim.html for details | ||
""" | ||
|
||
from pathlib import Path | ||
|
||
from linuxpy.configfs import CONFIGFS_PATH | ||
from linuxpy.gpio.device import get_chip_info | ||
from linuxpy.types import Optional | ||
|
||
GPIOSIM_PATH: Optional[Path] = None if CONFIGFS_PATH is None else CONFIGFS_PATH / "gpio-sim" | ||
|
||
|
||
def find_gpio_sim_file(num_lines=None) -> Optional[Path]: | ||
"""Best effort to find | ||
Returns: | ||
_type_: _description_ | ||
""" | ||
for path in sorted(Path("/dev").glob("gpiochip*")): | ||
with path.open("rb") as fobj: | ||
info = get_chip_info(fobj) | ||
if "gpio-sim" in info.label: | ||
if num_lines is None or info.lines == num_lines: | ||
return path |
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,49 @@ | ||
# | ||
# This file is part of the linuxpy project | ||
# | ||
# Copyright (c) 2024 Tiago Coutinho | ||
# Distributed under the GPLv3 license. See LICENSE for more info. | ||
|
||
import functools | ||
from pathlib import Path | ||
|
||
from .proc import PROC_PATH | ||
from .types import Generator, NamedTuple, Optional | ||
|
||
MOUNTS_PATH: Path = PROC_PATH / "mounts" | ||
|
||
|
||
class MountInfo(NamedTuple): | ||
dev_type: str | ||
mount_point: str | ||
fs_type: str | ||
attrs: list[str] | ||
|
||
|
||
def gen_read() -> Generator[MountInfo, None, None]: | ||
data = MOUNTS_PATH.read_text() | ||
for line in data.splitlines(): | ||
dev_type, mount_point, fs_type, attrs, *_ = line.split() | ||
yield MountInfo(dev_type, mount_point, fs_type, attrs.split(",")) | ||
|
||
|
||
@functools.cache | ||
def cache() -> tuple[MountInfo, ...]: | ||
return tuple(gen_read()) | ||
|
||
|
||
@functools.cache | ||
def get_mount_point(dev_type, fs_type=None) -> Optional[Path]: | ||
if fs_type is None: | ||
fs_type = dev_type | ||
for _dev_type, mount_point, _fs_type, *_ in cache(): | ||
if dev_type == _dev_type and fs_type == _fs_type: | ||
return Path(mount_point) | ||
|
||
|
||
def sysfs() -> Optional[Path]: | ||
return get_mount_point("sysfs") | ||
|
||
|
||
def configfs() -> Optional[Path]: | ||
return get_mount_point("configfs") |
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,50 @@ | ||
from pathlib import Path | ||
|
||
from linuxpy.util import try_numeric | ||
|
||
PROC_PATH = Path("/proc") | ||
|
||
CPU_INFO_PATH: Path = PROC_PATH / "cpuinfo" | ||
MEM_INFO_PATH: Path = PROC_PATH / "meminfo" | ||
MODULES_PATH: Path = PROC_PATH / "modules" | ||
|
||
|
||
def iter_cpu_info(): | ||
data = CPU_INFO_PATH.read_text() | ||
for cpu in data.split("\n\n"): | ||
info = {} | ||
for line in cpu.splitlines(): | ||
key, value = map(str.strip, line.split(":", 1)) | ||
if "flags" in key or key == "bugs": | ||
value = value.split() | ||
else: | ||
value = try_numeric(value) | ||
info[key] = value | ||
yield info | ||
|
||
|
||
def iter_mem_info(): | ||
data = MEM_INFO_PATH.read_text() | ||
for line in data.splitlines(): | ||
key, value = map(str.strip, line.split(":", 1)) | ||
if value.endswith(" kB"): | ||
value = try_numeric(value[:-3]) * 1024 | ||
else: | ||
value = try_numeric(value) | ||
yield key, value | ||
|
||
|
||
def iter_modules(): | ||
data = MODULES_PATH.read_text() | ||
for line in data.splitlines(): | ||
fields = line.split() | ||
mod = { | ||
"name": fields[0], | ||
"size": int(fields[1]), | ||
"use_count": int(fields[2]), | ||
"dependencies": [] if fields[3] == "-" else [dep for dep in fields[3].split(",") if dep], | ||
} | ||
if len(fields) > 5: | ||
mod["state"] = fields[4] | ||
mod["offset"] = int(fields[5], 16) | ||
yield mod |
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
Oops, something went wrong.