summaryrefslogtreecommitdiff
path: root/kvmd/plugins/ugpio/otgconf.py
blob: 37a407b534e4ba1edd231b1a87c12e222b37d483 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# ========================================================================== #
#                                                                            #
#    KVMD - The main PiKVM daemon.                                           #
#                                                                            #
#    Copyright (C) 2018-2023  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 asyncio

from typing import Callable
from typing import Any

from ...logging import get_logger

from ...inotify import InotifyMask
from ...inotify import Inotify

from ... import aiotools
from ... import usb

from ...yamlconf import Section

from ...validators.basic import valid_stripped_string_not_empty

from . import BaseUserGpioDriver


# =====
class Plugin(BaseUserGpioDriver):
    def __init__(
        self,
        instance_name: str,
        notifier: aiotools.AioNotifier,

        otg_config: Section,  # XXX: Not from options, see /kvmd/apps/kvmd/__init__.py for details
    ) -> None:

        super().__init__(instance_name, notifier)

        self.__udc: str = otg_config.udc
        self.__init_delay: float = otg_config.init_delay

        gadget: str = otg_config.gadget
        self.__udc_path = usb.get_gadget_path(gadget, usb.G_UDC)
        self.__functions_path = usb.get_gadget_path(gadget, usb.G_FUNCTIONS)
        self.__profile_path = usb.get_gadget_path(gadget, usb.G_PROFILE)

        self.__lock = asyncio.Lock()

    @classmethod
    def get_pin_validator(cls) -> Callable[[Any], Any]:
        return valid_stripped_string_not_empty

    def prepare(self) -> None:
        self.__udc = usb.find_udc(self.__udc)
        get_logger().info("Using UDC %s", self.__udc)

    async def run(self) -> None:
        logger = get_logger(0)
        while True:
            try:
                while True:
                    self._notifier.notify()
                    if os.path.isfile(self.__udc_path):
                        break
                    await asyncio.sleep(5)

                with Inotify() as inotify:
                    await inotify.watch(InotifyMask.ALL_MODIFY_EVENTS, os.path.dirname(self.__udc_path))
                    await inotify.watch(InotifyMask.ALL_MODIFY_EVENTS, self.__profile_path)
                    self._notifier.notify()
                    while True:
                        need_restart = False
                        need_notify = False
                        for event in (await inotify.get_series(timeout=1)):
                            need_notify = True
                            if event.mask & (InotifyMask.DELETE_SELF | InotifyMask.MOVE_SELF | InotifyMask.UNMOUNT):
                                logger.warning("Got fatal inotify event: %s; reinitializing OTG-bind ...", event)
                                need_restart = True
                                break
                        if need_restart:
                            break
                        if need_notify:
                            self._notifier.notify()
            except Exception:
                logger.exception("Unexpected OTG-bind watcher error")
                await asyncio.sleep(1)

    async def read(self, pin: str) -> bool:
        if pin == "udc":
            return self.__is_udc_enabled()
        return os.path.exists(self.__get_fdest_path(pin))

    async def write(self, pin: str, state: bool) -> None:
        async with self.__lock:
            if self.read(pin) == state:
                return
            if pin == "udc":
                if state:
                    self.__recreate_profile()
                self.__set_udc_enabled(state)
            else:
                if self.__is_udc_enabled():
                    self.__set_udc_enabled(False)
                try:
                    if state:
                        os.symlink(self.__get_fsrc_path(pin), self.__get_fdest_path(pin))
                    else:
                        os.unlink(self.__get_fdest_path(pin))
                except (FileNotFoundError, FileExistsError):
                    pass
                finally:
                    self.__recreate_profile()
                    try:
                        await asyncio.sleep(self.__init_delay)
                    finally:
                        self.__set_udc_enabled(True)

    def __recreate_profile(self) -> None:
        # XXX: See pikvm/pikvm#1235
        # After unbind and bind, the gadgets stop working,
        # unless we recreate their links in the profile.
        # Some kind of kernel bug.
        for func in os.listdir(self.__profile_path):
            path = self.__get_fdest_path(func)
            if os.path.islink(path):
                try:
                    os.unlink(path)
                    os.symlink(self.__get_fsrc_path(func), path)
                except (FileNotFoundError, FileNotFoundError):
                    pass

    def __get_fsrc_path(self, func: str) -> str:
        return os.path.join(self.__functions_path, func)

    def __get_fdest_path(self, func: str) -> str:
        return os.path.join(self.__profile_path, func)

    def __set_udc_enabled(self, enabled: bool) -> None:
        with open(self.__udc_path, "w") as file:
            file.write(self.__udc if enabled else "\n")

    def __is_udc_enabled(self) -> bool:
        with open(self.__udc_path) as file:
            return bool(file.read().strip())

    def __str__(self) -> str:
        return f"GPIO({self._instance_name})"

    __repr__ = __str__