refactor(dbus): piControl driver reset with PiControlIoctl class
Replaces inline ioctl logic with the new `PiControlIoctl` class for cleaner and reusable code. This improves readability, encapsulates device operations, and simplifies error handling for resetting the piControl driver.
This commit is contained in:
@@ -2,13 +2,11 @@
|
|||||||
# SPDX-FileCopyrightText: 2025 KUNBUS GmbH
|
# SPDX-FileCopyrightText: 2025 KUNBUS GmbH
|
||||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
"""D-Bus interfaces for piControl."""
|
"""D-Bus interfaces for piControl."""
|
||||||
import os
|
|
||||||
from fcntl import ioctl
|
|
||||||
from logging import getLogger
|
from logging import getLogger
|
||||||
|
|
||||||
from pydbus.generic import signal
|
from pydbus.generic import signal
|
||||||
|
|
||||||
from .process_image_helper import ResetDriverWatchdog
|
from .process_image_helper import PiControlIoctl, ResetDriverWatchdog
|
||||||
|
|
||||||
log = getLogger(__name__)
|
log = getLogger(__name__)
|
||||||
|
|
||||||
@@ -41,21 +39,9 @@ class InterfacePiControl:
|
|||||||
log.debug("enter InterfacePiControl.ResetDriver")
|
log.debug("enter InterfacePiControl.ResetDriver")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
fd = os.open(self.picontrol_device, os.O_WRONLY)
|
picontrol_ioctl = PiControlIoctl(self.picontrol_device)
|
||||||
except Exception as e:
|
picontrol_ioctl.ioctl(PiControlIoctl.IOCTL_RESET_DRIVER)
|
||||||
log.warning(f"could not open ${self.picontrol_device} to reset driver")
|
|
||||||
raise e
|
|
||||||
|
|
||||||
execption = None
|
|
||||||
try:
|
|
||||||
# KB_RESET _IO('K', 12 ) // reset the piControl driver including the config file
|
|
||||||
ioctl(fd, 19212)
|
|
||||||
log.info("reset piControl driver")
|
log.info("reset piControl driver")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.warning(f"could not reset piControl driver: ${e}")
|
log.warning(f"could not reset piControl driver: ${e}")
|
||||||
execption = e
|
raise e
|
||||||
finally:
|
|
||||||
os.close(fd)
|
|
||||||
|
|
||||||
if execption:
|
|
||||||
raise execption
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ The ResetDriverWatchdog class is a copy of revpipyload project module "watchdogs
|
|||||||
https://github.com/naruxde/revpipyload/blob/b51c2b617a57cc7d96fd67e1da9f090a0624eacb/src/revpipyload/watchdogs.py
|
https://github.com/naruxde/revpipyload/blob/b51c2b617a57cc7d96fd67e1da9f090a0624eacb/src/revpipyload/watchdogs.py
|
||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
|
from ctypes import c_int
|
||||||
from fcntl import ioctl
|
from fcntl import ioctl
|
||||||
from logging import getLogger
|
from logging import getLogger
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
@@ -100,3 +101,24 @@ class ResetDriverWatchdog(Thread):
|
|||||||
rc = self._triggered
|
rc = self._triggered
|
||||||
self._triggered = False
|
self._triggered = False
|
||||||
return rc
|
return rc
|
||||||
|
|
||||||
|
|
||||||
|
class PiControlIoctl:
|
||||||
|
IOCTL_RESET_DRIVER = 19212
|
||||||
|
|
||||||
|
def __init__(self, picontrol_device: str):
|
||||||
|
self.picontrol_device = picontrol_device
|
||||||
|
|
||||||
|
def ioctl(self, request, arg=0):
|
||||||
|
if type(arg) is c_int:
|
||||||
|
arg = arg.value
|
||||||
|
|
||||||
|
_fd = os.open(self.picontrol_device, os.O_WRONLY)
|
||||||
|
return_value = ioctl(_fd, 19212, arg)
|
||||||
|
os.close(_fd)
|
||||||
|
|
||||||
|
return return_value
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
return self.picontrol_device
|
||||||
|
|||||||
Reference in New Issue
Block a user