From 7c6e2a2d64963ceb33bac490a58763b5ff559ff6 Mon Sep 17 00:00:00 2001
From: Martin Teichmann <martin.teichmann@gmail.com>
Date: Sun, 29 Jan 2023 08:36:48 +0000
Subject: [PATCH] improve error messages

---
 ebpfcat/ebpfcat.py  | 11 +++--------
 ebpfcat/ethercat.py | 15 ++++++++++++---
 2 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/ebpfcat/ebpfcat.py b/ebpfcat/ebpfcat.py
index 1754604..42c32a1 100644
--- a/ebpfcat/ebpfcat.py
+++ b/ebpfcat/ebpfcat.py
@@ -211,18 +211,13 @@ class EBPFTerminal(Terminal):
     compatibility = None
     position_offset = 0, 0
 
-    def __init_subclass__(cls):
-        cls.pdo = {}
-        for c in cls.__mro__[::-1]:
-            for k, v in c.__dict__.items():
-                if isinstance(v, PacketDesc):
-                    cls.pdo[k] = v
-
     async def initialize(self, relative, absolute):
         await super().initialize(relative, absolute)
         if (self.compatibility is not None and
                 (self.vendorId, self.productCode) not in self.compatibility):
-            raise RuntimeError(f"Incompatible Terminal: {self.vendorId}:{self.productCode} ({relative}, {absolute})")
+            raise RuntimeError(
+                f"Incompatible Terminal: {self.vendorId}:{self.productCode} "
+                f"({relative}, {absolute})")
 
     def allocate(self, packet, readonly):
         if self.pdo_in_sz:
diff --git a/ebpfcat/ethercat.py b/ebpfcat/ethercat.py
index 9674348..a4d211d 100644
--- a/ebpfcat/ethercat.py
+++ b/ebpfcat/ethercat.py
@@ -334,7 +334,8 @@ class EtherCat(Protocol):
             out += b"\0" * data
         elif data is not None:
             out += data
-        assert isinstance(pos, int) and isinstance(offset, int)
+        assert isinstance(pos, int) and isinstance(offset, int), \
+            f"pos: {pos} offset: {offset}"
         self.send_queue.put_nowait((cmd, out, idx, pos, offset, future))
         ret = await future
         if data is None:
@@ -531,11 +532,15 @@ class Terminal:
                 return eeprom
             eeprom[hd] = await get_data(ws * 2)
 
+    def has_mailbox(self):
+        return self.mbx_out_off is not None and self.mbx_in_off is not None
+
     async def mbx_send(self, type, *args, data=None, address=0, priority=0, channel=0):
         """send data to the mailbox"""
         status, = await self.read(0x805, "B")  # always using mailbox 0, OK?
         if status & 8:
             raise RuntimeError("mailbox full, read first")
+        assert self.mbx_out_off is not None, "not send mailbox defined"
         await self.write(self.mbx_out_off, "HHBB",
                                 datasize(args, data),
                                 address, channel | priority << 6,
@@ -551,6 +556,7 @@ class Terminal:
         while status & 8 == 0:
             # always using mailbox 1, OK?
             status, = await self.read(0x80D, "B")
+        assert self.mbx_in_off is not None, "not receive mailbox defined"
         dlen, address, prio, type, data = await self.read(
                 self.mbx_in_off, "HHBB", data=self.mbx_in_sz - 6)
         return MBXType(type & 0xf), data[:dlen]
@@ -586,7 +592,9 @@ class Terminal:
                 raise RuntimeError(f"expected CoE, got {type}")
             coecmd, sdocmd, idx, subidx, size = unpack("<HBHBI", data[:10])
             if coecmd >> 12 != CoECmd.SDORES.value:
-                raise RuntimeError(f"expected CoE SDORES (3), got {coecmd>>12:x}")
+                raise RuntimeError(
+                    f"expected CoE SDORES (3), got {coecmd>>12:x} "
+                    f"for {index:X}:{9 if subindex is None else subindex:02X}")
             if idx != index:
                 raise RuntimeError(f"requested index {index}, got {idx}")
             if sdocmd & 2:
@@ -605,7 +613,8 @@ class Terminal:
                     raise RuntimeError(f"expected CoE, got {type}")
                 coecmd, sdocmd = unpack("<HB", data[:3])
                 if coecmd >> 12 != CoECmd.SDORES.value:
-                    raise RuntimeError(f"expected CoE cmd SDORES, got {coecmd}")
+                    raise RuntimeError(
+                        f"expected CoE cmd SDORES, got {coecmd}")
                 if sdocmd & 0xe0 != 0:
                     raise RuntimeError(f"requested index {index}, got {idx}")
                 if sdocmd & 1 and len(data) == 7:
-- 
GitLab