You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2020/07/07 08:11:04 UTC

[plc4x] branch develop updated: - Fixed a bug that caused problems if the number of bits in the last byte + number of bits in the first byte are more than 8 - Added some more tests

This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 9c22bcd  - Fixed a bug that caused problems if the number of bits in the last byte + number of bits in the first byte are more than 8 - Added some more tests
9c22bcd is described below

commit 9c22bcd9d1330dcf90d601dce8bd539916f29d84
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Tue Jul 7 10:10:56 2020 +0200

    - Fixed a bug that caused problems if the number of bits in the last byte + number of bits in the first byte are more than 8
    - Added some more tests
---
 sandbox/plc4c/spi/src/read_buffer.c       | 24 ++++++++++++++++++++----
 sandbox/plc4c/spi/test/read_buffer_test.c | 15 +++++++++++++++
 2 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/sandbox/plc4c/spi/src/read_buffer.c b/sandbox/plc4c/spi/src/read_buffer.c
index ac2132a..d7c4b4b 100644
--- a/sandbox/plc4c/spi/src/read_buffer.c
+++ b/sandbox/plc4c/spi/src/read_buffer.c
@@ -105,7 +105,7 @@ plc4c_return_code plc4c_spi_read_unsigned_bits_internal(
 
   // In this case we also need more than one following byte.
   else {
-    // TODO: For debugging ...
+    // TODO: For debugging ... (Just that I can see the values in the debugger)
     uint8_t* original_value = value;
 
     // Find out how many bytes we need to read.
@@ -136,6 +136,22 @@ plc4c_return_code plc4c_spi_read_unsigned_bits_internal(
 
     // Read the bits of the first byte
     uint8_t cur_byte = plc4c_spi_read_unsigned_byte_get_byte_internal(buf, 0);
+
+    // In the case that the number of bits read from the first and last
+    // byte are more than 8, we gotta put that excess data into it's own
+    // output byte.
+    if(num_bits_first_byte + num_bits_last_byte > 8) {
+      uint8_t excess_bits = num_bits_first_byte + num_bits_last_byte - 8;
+      *value = plc4c_spi_read_unsigned_byte_internal(
+          cur_byte, excess_bits, buf->curPosBit);
+      // Move on to the next output byte
+      value = plc4c_is_bigendian() ? value + 1 : value - 1;
+      // Update the read-pointer.
+      buf->curPosBit += excess_bits;
+      // Change the number of bits read, as we already read some.
+      num_bits_first_byte = num_bits_first_byte - excess_bits;
+    }
+
     uint8_t high_level_part = plc4c_spi_read_unsigned_byte_internal(
         cur_byte, num_bits_first_byte, buf->curPosBit);
     // For each of the following bytes.
@@ -303,7 +319,7 @@ plc4c_return_code plc4c_spi_read_unsigned_short(plc4c_spi_read_buffer* buf,
   plc4c_return_code res =
       plc4c_spi_read_unsigned_bits_internal(buf, num_bits, value);
   // Shift the bits to the right position.
-  if (res == OK) {
+  if ((res == OK) && plc4c_is_bigendian()) {
     if (num_bits <= 8) {
       *value = *value >> 8;
     }
@@ -322,7 +338,7 @@ plc4c_return_code plc4c_spi_read_unsigned_int(plc4c_spi_read_buffer* buf,
   plc4c_return_code res =
       plc4c_spi_read_unsigned_bits_internal(buf, num_bits, value);
   // Shift the bits to the right position.
-  if (res == OK) {
+  if ((res == OK) && plc4c_is_bigendian()) {
     if (num_bits <= 8) {
       *value = *value >> 24;
     } else if (num_bits <= 16) {
@@ -345,7 +361,7 @@ plc4c_return_code plc4c_spi_read_unsigned_long(plc4c_spi_read_buffer* buf,
   plc4c_return_code res =
       plc4c_spi_read_unsigned_bits_internal(buf, num_bits, value);
   // Shift the bits to the right position.
-  if (res == OK) {
+  if ((res == OK) && plc4c_is_bigendian()) {
     if (num_bits <= 8) {
       *value = *value >> 56;
     } else if (num_bits <= 16) {
diff --git a/sandbox/plc4c/spi/test/read_buffer_test.c b/sandbox/plc4c/spi/test/read_buffer_test.c
index 14419fe..061445c 100644
--- a/sandbox/plc4c/spi/test/read_buffer_test.c
+++ b/sandbox/plc4c/spi/test/read_buffer_test.c
@@ -334,6 +334,21 @@ void test_plc4c_spi_read_unsigned_short(void) {
   read_buffer->curPosByte = 0;
   read_buffer->curPosBit = 1;
   test_plc4c_spi_read_unsigned_short_args("Short starting at bit 1 but beading full last byte", read_buffer, 15, OK, 258);
+
+  // Read only fractions of a short
+  read_buffer->curPosByte = 0;
+  read_buffer->curPosBit = 0;
+  test_plc4c_spi_read_unsigned_short_args("Short only reading 2 byte", read_buffer, 2, OK, 0);
+  test_plc4c_spi_read_unsigned_short_args("Short only reading 4 byte", read_buffer, 4, OK, 0);
+  test_plc4c_spi_read_unsigned_short_args("Short only reading 6 byte", read_buffer, 6, OK, 16);
+  test_plc4c_spi_read_unsigned_short_args("Short only reading 8 byte", read_buffer, 8, OK, 32);
+  test_plc4c_spi_read_unsigned_short_args("Short only reading 10 byte", read_buffer, 10, OK, 193);
+  test_plc4c_spi_read_unsigned_short_args("Short only reading 12 byte", read_buffer, 12, OK, 20);
+  test_plc4c_spi_read_unsigned_short_args("Short only reading 14 byte", read_buffer, 14, OK, 1543);
+
+  // We only have 8 bytes, so with this we would exceed the range.
+  test_plc4c_spi_read_unsigned_short_args("Try to read mode bytes than the buffer has available", read_buffer, 16, OUT_OF_RANGE, 0);
+  test_plc4c_spi_read_unsigned_short_args("Try to read too many bytes for a short", read_buffer, 18, OUT_OF_RANGE, 0);
 }
 
 void test_plc4c_spi_read_unsigned_int_args(