Browse Source

method `get_packet_length` is now public; transmit: adapt payload length check for fixed length mode

Fabian Peter Hammerle 3 years ago
parent
commit
0846ac2e96
2 changed files with 39 additions and 22 deletions
  1. 1 0
      CHANGELOG.md
  2. 38 22
      cc1101/__init__.py

+ 1 - 0
CHANGELOG.md

@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
   via new method `set_packet_length_mode(PacketLengthMode.FIXED)`
 - new enum `options.PacketLengthMode`
 - new method `get_packet_length_mode()`
+- method `get_packet_length()` is now public
 - added configured packet length to `CC1101`'s string representation
   (`≤n` indicates variable length mode, `=n` fixed length mode)
 

+ 38 - 22
cc1101/__init__.py

@@ -389,22 +389,11 @@ class CC1101:
                 "≤"
                 if self.get_packet_length_mode() == PacketLengthMode.VARIABLE
                 else "=",
-                self._get_packet_length(),
+                self.get_packet_length(),
             ),
         )
         return "CC1101({})".format(", ".join(attrs))
 
-    def _get_packet_length(self) -> int:
-        """
-        packet length in fixed packet length mode,
-        maximum packet length in variable packet length mode.
-
-        > In variable packet length mode, [...]
-        > any packet received with a length byte
-        > with a value greater than PKTLEN will be discarded.
-        """
-        return self._read_single_byte(ConfigurationRegisterAddress.PKTLEN)
-
     def get_configuration_register_values(
         self,
         start_register: ConfigurationRegisterAddress = min(
@@ -421,6 +410,19 @@ class CC1101:
             for i, v in enumerate(values)
         }
 
+    def get_packet_length(self) -> int:
+        """
+        PKTLEN
+
+        Packet length in fixed packet length mode,
+        maximum packet length in variable packet length mode.
+
+        > In variable packet length mode, [...]
+        > any packet received with a length byte
+        > with a value greater than PKTLEN will be discarded.
+        """
+        return self._read_single_byte(ConfigurationRegisterAddress.PKTLEN)
+
     def _disable_data_whitening(self):
         """
         PKTCTRL0.WHITE_DATA
@@ -496,9 +498,31 @@ class CC1101:
         # see "15.2 Packet Format"
         # > In variable packet length mode, [...]
         # > The first byte written to the TXFIFO must be different from 0.
-        if payload[0] == 0:
+        packet_length_mode = self.get_packet_length_mode()
+        packet_length = self.get_packet_length()
+        if packet_length_mode == PacketLengthMode.VARIABLE:
+            if not payload:
+                raise ValueError("empty payload {!r}".format(payload))
+            if payload[0] == 0:
+                raise ValueError(
+                    "in variable packet length mode the first byte of the payload must not be null"
+                    + "\npayload: {!r}".format(payload)
+                )
+            if len(payload) > packet_length:
+                raise ValueError(
+                    "payload exceeds maximum payload length of {} bytes".format(
+                        packet_length
+                    )
+                    + "\nsee .get_packet_length()"
+                    + "\npayload: {!r}".format(payload)
+                )
+        elif (
+            packet_length_mode == PacketLengthMode.FIXED
+            and len(payload) != packet_length
+        ):
             raise ValueError(
-                "in variable packet length mode the first byte of payload must not be null"
+                "expected payload length of {} bytes, got {}"
+                + "\nsee .set_packet_length_mode() and .get_packet_length()"
                 + "\npayload: {!r}".format(payload)
             )
         marcstate = self.get_main_radio_control_state_machine_state()
@@ -508,14 +532,6 @@ class CC1101:
                     marcstate.name
                 )
             )
-        max_packet_length = self._get_packet_length()
-        if len(payload) > max_packet_length:
-            raise ValueError(
-                "payload exceeds maximum payload length of {} bytes".format(
-                    max_packet_length
-                )
-                + "\npayload: {!r}".format(payload)
-            )
         self._flush_tx_fifo_buffer()
         self._write_burst(FIFORegisterAddress.TX, list(payload))
         _LOGGER.info(