diff options
-rw-r--r-- | kvmd/apps/otgconf/__init__.py | 105 | ||||
-rw-r--r-- | kvmd/apps/otgconf/__main__.py | 24 | ||||
-rwxr-xr-x | setup.py | 1 |
3 files changed, 130 insertions, 0 deletions
diff --git a/kvmd/apps/otgconf/__init__.py b/kvmd/apps/otgconf/__init__.py new file mode 100644 index 00000000..bfbc4326 --- /dev/null +++ b/kvmd/apps/otgconf/__init__.py @@ -0,0 +1,105 @@ +# ========================================================================== # +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2022 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# 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 <https://www.gnu.org/licenses/>. # +# # +# ========================================================================== # + + +import os +import json +import contextlib +import argparse + +from typing import List +from typing import Generator +from typing import Optional + +from ...validators.basic import valid_stripped_string_not_empty + +from ... import env +from ... import usb + +from .. import init + + +# ===== +def _make_config_path(gadget: str, func: str) -> str: + return os.path.join(f"{env.SYSFS_PREFIX}/sys/kernel/config/usb_gadget", gadget, "configs/c.1", func) + + +def _make_func_path(gadget: str, func: str) -> str: + return os.path.join(f"{env.SYSFS_PREFIX}/sys/kernel/config/usb_gadget", gadget, "functions", func) + + +def _udc_stopped(gadget: str, udc: str) -> Generator[None, None, None]: + udc = usb.find_udc(udc) + udc_path = os.path.join(f"{env.SYSFS_PREFIX}/sys/kernel/config/usb_gadget", gadget, "UDC") + with open(udc_path) as udc_file: + enabled = bool(udc_file.read().strip()) + with open(udc_path, "w") as udc_file: + udc_file.write("\n") + try: + yield + finally: + if enabled: + with open(udc_path, "w") as udc_file: + udc_file.write(udc) + + +def _enable_func(gadget: str, udc: str, func: str) -> None: + with _udc_stopped(gadget, udc): + os.symlink(_make_func_path(gadget, func), _make_config_path(gadget, func)) + + +def _disable_func(gadget: str, udc: str, func: str) -> None: + with _udc_stopped(gadget, udc): + os.unlink(_make_config_path(gadget, func)) + + +def _list_funcs(gadget: str, meta_path: str) -> None: + for meta_name in sorted(os.listdir(meta_path)): + with open(os.path.join(meta_path, meta_name)) as meta_file: + meta = json.loads(meta_file.read()) + enabled = os.path.exists(_make_config_path(gadget, meta["func"])) + print(f"{'+' if enabled else '-'} {meta['func']} # {meta['name']}") + + +# ===== +def main(argv: Optional[List[str]]=None) -> None: + (parent_parser, argv, config) = init( + add_help=False, + argv=argv, + ) + parser = argparse.ArgumentParser( + prog="kvmd-otgconf", + description="KVMD OTG low-level runtime configuration tool", + parents=[parent_parser], + ) + parser.add_argument("-l", "--list-funcs", action="store_true", help="List functions") + parser.add_argument("-e", "--enable-func", type=valid_stripped_string_not_empty, metavar="<name>", help="Enable function") + parser.add_argument("-d", "--disable-func", type=valid_stripped_string_not_empty, metavar="<name>", help="Disable function") + options = parser.parse_args(argv[1:]) + + if options.enable_func: + _enable_func(config.otg.gadget, config.otg.udc, options.enable_func) + + if options.disable_func: + _disable_func(config.otg.gadget, config.otg.udc, options.disable_func) + + _list_funcs(config.otg.gadget, config.otg.meta) diff --git a/kvmd/apps/otgconf/__main__.py b/kvmd/apps/otgconf/__main__.py new file mode 100644 index 00000000..3849d1b9 --- /dev/null +++ b/kvmd/apps/otgconf/__main__.py @@ -0,0 +1,24 @@ +# ========================================================================== # +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2022 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# 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 <https://www.gnu.org/licenses/>. # +# # +# ========================================================================== # + + +from . import main +main() @@ -122,6 +122,7 @@ def main() -> None: "kvmd-otg = kvmd.apps.otg:main", "kvmd-otgnet = kvmd.apps.otgnet:main", "kvmd-otgmsd = kvmd.apps.otgmsd:main", + "kvmd-otgconf = kvmd.apps.otgconf:main", "kvmd-htpasswd = kvmd.apps.htpasswd:main", "kvmd-cleanup = kvmd.apps.cleanup:main", "kvmd-ipmi = kvmd.apps.ipmi:main", |