Skip to content

Commit

Permalink
pynfb: [FEATURE] add AbstractBaseComp into libnfb to enable classes w…
Browse files Browse the repository at this point in the history
…ith self-managed component
  • Loading branch information
martinspinler committed Jul 31, 2023
1 parent e2d0212 commit dcc2d43
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
30 changes: 5 additions & 25 deletions pynfb/nfb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
3 changes: 3 additions & 0 deletions pynfb/nfb/libnfb.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,6 @@ cdef class Nfb:
cdef public bytes _dtb

cdef dict __dict__

cdef class AbstractBaseComp:
cdef dict __dict__
40 changes: 40 additions & 0 deletions pynfb/nfb/libnfb.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dcc2d43

Please sign in to comment.