From dcc2d430807610dabd994ebcf71365a3b7c2e4a2 Mon Sep 17 00:00:00 2001 From: Martin Spinler Date: Mon, 31 Jul 2023 16:37:31 +0200 Subject: [PATCH] pynfb: [FEATURE] add AbstractBaseComp into libnfb to enable classes with self-managed component --- pynfb/nfb/__init__.py | 30 +++++------------------------- pynfb/nfb/libnfb.pxd | 3 +++ pynfb/nfb/libnfb.pyx | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 25 deletions(-) diff --git a/pynfb/nfb/__init__.py b/pynfb/nfb/__init__.py index 6887de5c..25bb5611 100644 --- a/pynfb/nfb/__init__.py +++ b/pynfb/nfb/__init__.py @@ -5,22 +5,13 @@ from . import libnfb from . import eth +from .libnfb import open + import fdt from typing import Optional -def open(path: str = '0') -> libnfb.Nfb: - """Open a handle to NFB device in system - - :param path: Path to device node, default leads to ``/dev/nfb0`` - :return: The :class:`libnfb.Nfb` object, enhanced by :class:`eth.EthManager` - """ - - dev = libnfb.Nfb(path) - eth.EthManager(dev) - return dev - -class BaseComp(): +class BaseComp(libnfb.AbstractBaseComp): """ BaseComp represents common parent for all classes that manages HW components @@ -30,17 +21,6 @@ class BaseComp(): :ivar libnfb.Comp _comp: Component object """ - DT_COMPATIBLE = None - def __init__(self, dev=libnfb.Nfb.default_device, node: Optional[fdt.Node]=None, index: int=0): - assert self.DT_COMPATIBLE is not None, "DT_COMPATIBLE must be set in derived class" - - self._dev = dev if isinstance(dev, libnfb.Nfb) else open(dev) - - if node: - assert self.DT_COMPATIBLE == node.get_property("compatible").value, "compatible string mismatch" - else: - node = self._dev.fdt_get_compatible(self.DT_COMPATIBLE)[index] - - self._node = node - self._comp = self._dev.comp_open(node) + super().__init__(dev, node, index) + self._comp = self._dev.comp_open(self._node) diff --git a/pynfb/nfb/libnfb.pxd b/pynfb/nfb/libnfb.pxd index c0533c9e..f5dce494 100644 --- a/pynfb/nfb/libnfb.pxd +++ b/pynfb/nfb/libnfb.pxd @@ -134,3 +134,6 @@ cdef class Nfb: cdef public bytes _dtb cdef dict __dict__ + +cdef class AbstractBaseComp: + cdef dict __dict__ diff --git a/pynfb/nfb/libnfb.pyx b/pynfb/nfb/libnfb.pyx index fbf7999e..ce0be3e6 100644 --- a/pynfb/nfb/libnfb.pyx +++ b/pynfb/nfb/libnfb.pyx @@ -16,6 +16,8 @@ import fdt cimport libnetcope +from . import eth + if not hasattr(time, 'time_ns'): time_ns = lambda: int(time.time() * 1000000000) else: @@ -30,6 +32,18 @@ def __batched(iterable, n): batch = tuple(islice(it, n)) +def open(path: str = '0') -> Nfb: + """Open a handle to NFB device in system + + :param path: Path to device node, default leads to ``/dev/nfb0`` + :return: The :class:`libnfb.Nfb` object, enhanced by :class:`eth.EthManager` + """ + + dev = Nfb(path) + eth.EthManager(dev) + return dev + + cdef class Nfb: """ Nfb class instance represents handle to NFB device in system @@ -267,6 +281,32 @@ cdef class Comp: time.sleep(delay) return True + +cdef class AbstractBaseComp: + """ + AbstractBaseComp represents common parent for all classes that manages HW components + + Derived class should set it's own `DT_COMPATIBLE`! + + :ivar libnfb.Nfb _dev: NFB object + :ivar libnfb.Comp _node: fdt.Node object + """ + + DT_COMPATIBLE = None + + def __init__(self, dev=Nfb.default_device, node: Optional[fdt.Node]=None, index: int=0): + assert self.DT_COMPATIBLE is not None, "DT_COMPATIBLE must be set in derived class" + + self._dev = dev if isinstance(dev, Nfb) else open(dev) + + if node: + assert self.DT_COMPATIBLE == node.get_property("compatible").value, "compatible string mismatch" + else: + node = self._dev.fdt_get_compatible(self.DT_COMPATIBLE)[index] + + self._node = node + + cdef class QueueManager: """ :ivar List[NdpQueueRx] rx: List of RX NDP queues