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-License-Identifier: GPL-2.0-or-later
|
||||
"""D-Bus interfaces for piControl."""
|
||||
import os
|
||||
from fcntl import ioctl
|
||||
from logging import getLogger
|
||||
|
||||
from pydbus.generic import signal
|
||||
|
||||
from .process_image_helper import ResetDriverWatchdog
|
||||
from .process_image_helper import PiControlIoctl, ResetDriverWatchdog
|
||||
|
||||
log = getLogger(__name__)
|
||||
|
||||
@@ -41,21 +39,9 @@ class InterfacePiControl:
|
||||
log.debug("enter InterfacePiControl.ResetDriver")
|
||||
|
||||
try:
|
||||
fd = os.open(self.picontrol_device, os.O_WRONLY)
|
||||
except Exception as e:
|
||||
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)
|
||||
picontrol_ioctl = PiControlIoctl(self.picontrol_device)
|
||||
picontrol_ioctl.ioctl(PiControlIoctl.IOCTL_RESET_DRIVER)
|
||||
log.info("reset piControl driver")
|
||||
except Exception as e:
|
||||
log.warning(f"could not reset piControl driver: ${e}")
|
||||
execption = e
|
||||
finally:
|
||||
os.close(fd)
|
||||
|
||||
if execption:
|
||||
raise execption
|
||||
raise e
|
||||
|
||||
@@ -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
|
||||
"""
|
||||
import os
|
||||
from ctypes import c_int
|
||||
from fcntl import ioctl
|
||||
from logging import getLogger
|
||||
from threading import Thread
|
||||
@@ -100,3 +101,24 @@ class ResetDriverWatchdog(Thread):
|
||||
rc = self._triggered
|
||||
self._triggered = False
|
||||
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