summaryrefslogtreecommitdiff
path: root/kvmd/apps/otgconf/__init__.py
blob: bfbc432604e4a223a4cbdbcd9ed957cdc7734f5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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)


@contextlib.contextmanager
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)