]> Kevux Git Server - fll/commitdiff
Feature: Add f_fss_simple_packet_encode() for encoding the Simple Packet header.
authorKevin Day <thekevinday@gmail.com>
Thu, 14 Dec 2023 02:19:20 +0000 (20:19 -0600)
committerKevin Day <thekevinday@gmail.com>
Thu, 14 Dec 2023 02:26:07 +0000 (20:26 -0600)
level_0/f_fss/c/fss/simple_packet.c
level_0/f_fss/c/fss/simple_packet.h
level_0/f_fss/data/build/settings-tests
level_0/f_fss/tests/unit/c/test-fss-simple_packet_encode.c [new file with mode: 0644]
level_0/f_fss/tests/unit/c/test-fss-simple_packet_encode.h [new file with mode: 0644]
level_0/f_fss/tests/unit/c/test-fss.c
level_0/f_fss/tests/unit/c/test-fss.h

index e73cd30faee6b390afb048764445e5bedf980943..fe0366754833d7734d6e75f0157e16134d90c326 100644 (file)
@@ -34,6 +34,55 @@ extern "C" {
   }
 #endif // _di_f_fss_simple_packet_destroy_
 
+#ifndef _di_f_fss_simple_packet_encode_
+  f_status_t f_fss_simple_packet_encode(const uint8_t control, const uint32_t size, f_string_dynamic_t * const destination) {
+    #ifndef _di_level_0_parameter_checking_
+      if (!destination) return F_status_set_error(F_parameter);
+    #endif // _di_level_0_parameter_checking_
+
+    {
+      const f_status_t status = f_memory_array_increase_by(F_fss_simple_packet_block_header_size_d, sizeof(f_char_t), (void **) &destination->string, &destination->used, &destination->size);
+      if (F_status_is_error(status)) return status;
+    }
+
+    destination->string[destination->used++] = (uint8_t) control;
+
+    #ifdef _is_F_endian_little
+      // Big Endian.
+      if (control & F_fss_simple_packet_endian_d) {
+        destination->string[destination->used++] = ((uint8_t) size) & 0xff;
+        destination->string[destination->used++] = (((uint8_t) size) >> 8) & 0xff;
+        destination->string[destination->used++] = (((uint8_t) size) >> 16) & 0xff;
+        destination->string[destination->used++] = (((uint8_t) size) >> 24) & 0xff;
+      }
+      // Little Endian.
+      else {
+        destination->string[destination->used++] = (((uint8_t) size) >> 24) & 0xff;
+        destination->string[destination->used++] = (((uint8_t) size) >> 16) & 0xff;
+        destination->string[destination->used++] = (((uint8_t) size) >> 8) & 0xff;
+        destination->string[destination->used++] = ((uint8_t) size) & 0xff;
+      }
+    #else
+      // Big Endian.
+      if (control & F_fss_simple_packet_endian_d) {
+        destination->string[destination->used++] = (((uint8_t) size) >> 24) & 0xff;
+        destination->string[destination->used++] = (((uint8_t) size) >> 16) & 0xff;
+        destination->string[destination->used++] = (((uint8_t) size) >> 8) & 0xff;
+        destination->string[destination->used++] = ((uint8_t) size) & 0xff;
+      }
+      // Little Endian.
+      else {
+        destination->string[destination->used++] = ((uint8_t) size)) & 0xff;
+        destination->string[destination->used++] = (((uint8_t) size) >> 8) & 0xff;
+        destination->string[destination->used++] = (((uint8_t) size) >> 16) & 0xff;
+        destination->string[destination->used++] = (((uint8_t) size) >> 24) & 0xff;
+      }
+    #endif // _is_F_endian_little
+
+    return F_okay;
+  }
+#endif // _di_f_fss_simple_packet_encode_
+
 #ifndef _di_f_fss_simple_packet_extract_
   f_status_t f_fss_simple_packet_extract(const f_string_static_t buffer, f_fss_simple_packet_t * const packet) {
     #ifndef _di_level_0_parameter_checking_
index 0a9257ee74ee98eb7fffc221844bd735ae83498b..6f14c175caae2d48cb679a7928c5aae9221b452e 100644 (file)
@@ -204,6 +204,7 @@ extern "C" {
  *
  * @param simple_packet
  *   The FSS-000F (Simple Packet) to delete.
+ *   Must not be NULL.
  *
  * @return
  *   F_okay on success.
@@ -225,6 +226,7 @@ extern "C" {
  *
  * @param simple_packet
  *   The FSS-000F (Simple Packet) to destroy.
+ *   Must not be NULL.
  *
  * @return
  *   F_okay on success.
@@ -242,6 +244,32 @@ extern "C" {
 #endif // _di_f_fss_simple_packet_destroy_
 
 /**
+ * Encode the control and size parts of the FSS-000F (Simple Packet) packet structure into a string.
+ *
+ * This encodes only the header parts of the packet, which consists of the control block and the size block.
+ *
+ * @param control
+ *   The bits to encode into the control block.
+ * @param size
+ *   The size to encode into the size block.
+ * @param destination
+ *   The string buffer to encode the packet parts into.
+ *   Must not be NULL.
+ *
+ * @return
+ *   F_okay on success.
+ *
+ *   F_parameter (with error bit) if a parameter is invalid.
+ *
+ *   Errors (with error bit) from: f_memory_array_resize().
+ *
+ * @see f_memory_array_resize()
+ */
+#ifndef _di_f_fss_simple_packet_encode_
+  extern f_status_t f_fss_simple_packet_encode(const uint8_t control, const uint32_t size, f_string_dynamic_t * const destination);
+#endif // _di_f_fss_simple_packet_encode_
+
+/**
  * Extract the different parts of the FSS-000F (Simple Packet) string into a packet structure.
  *
  * The buffer is processed as binary data, therefore, NULL and other control data are considered valid data and are not ignored.
@@ -252,6 +280,7 @@ extern "C" {
  * @param packet
  *   The packet extracted from the given buffer, without doing anything to the payload.
  *   The caller can allocate the payload and extract it at any time by just selecting the string from F_fss_simple_packet_block_header_size_d until at most F_fss_simple_packet_block_payload_size_d.
+ *   Must not be NULL.
  *
  * @return
  *   F_okay on success (The end of the Payload Block is assumed to be the remainder of the buffer or F_fss_simple_packet_block_payload_size_d, whichever is smaller).
@@ -274,6 +303,7 @@ extern "C" {
  *   This buffer is considered binary data and so any NULL found within is treated as a valid part of the buffer.
  * @param packet
  *   The packet range extracted from the given buffer, with the payload being represented by a range.
+ *   Must not be NULL.
  *
  * @return
  *   F_okay on success (The end of the Payload Block is assumed to be the remainder of the buffer or F_fss_simple_packet_block_payload_size_d, whichever is smaller).
index 18ed23aa78106920e11d76bd208ee08f81f0127e..3d3b51d02b1d9d24fdd71105938a8f77dfe0da79 100644 (file)
@@ -36,7 +36,7 @@ build_sources_program test-fss-set_delete.c test-fss-set_destroy.c
 build_sources_program test-fss-sets_delete_callback.c test-fss-sets_destroy_callback.c test-fss-setss_delete_callback.c test-fss-setss_destroy_callback.c
 build_sources_program test-fss-set_quote_delete.c test-fss-set_quote_destroy.c
 build_sources_program test-fss-set_quotes_delete_callback.c test-fss-set_quotes_destroy_callback.c test-fss-set_quotess_delete_callback.c test-fss-set_quotess_destroy_callback.c
-build_sources_program test-fss-simple_packet_extract.c test-fss-simple_packet_extract_range.c
+build_sources_program test-fss-simple_packet_encode.c test-fss-simple_packet_extract.c test-fss-simple_packet_extract_range.c
 build_sources_program test-fss-simple_packet_delete.c test-fss-simple_packet_destroy.c
 build_sources_program test-fss-simple_packets_delete_callback.c test-fss-simple_packets_destroy_callback.c test-fss-simple_packetss_delete_callback.c test-fss-simple_packetss_destroy_callback.c
 
diff --git a/level_0/f_fss/tests/unit/c/test-fss-simple_packet_encode.c b/level_0/f_fss/tests/unit/c/test-fss-simple_packet_encode.c
new file mode 100644 (file)
index 0000000..00d3d12
--- /dev/null
@@ -0,0 +1,87 @@
+#include "test-fss.h"
+#include "test-fss-simple_packet_encode.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void test__f_fss_simple_packet_encode__parameter_checking(void **state) {
+
+  {
+    const f_status_t status = f_fss_simple_packet_encode(0, 0, 0);
+
+    assert_int_equal(status, F_status_set_error(F_parameter));
+  }
+}
+
+void test__f_fss_simple_packet_encode__works_big_endian(void **state) {
+
+  const f_char_t datas[3][5] = {
+    { 0x80, 0x05, 0x00, 0x00, 0x00 },
+    { 0x80, 0x06, 0x00, 0x00, 0x00 },
+    { 0x80, 0x07, 0x00, 0x00, 0x00 },
+  };
+
+  const f_number_unsigned_t size[] = {
+    5,
+    6,
+    7,
+  };
+
+  {
+    for (uint8_t i = 0; i < 3; ++i) {
+
+      f_string_dynamic_t dynamic = f_string_dynamic_t_initialize;
+
+      const f_status_t status = f_fss_simple_packet_encode(F_fss_simple_packet_endian_d, size[i], &dynamic);
+
+      assert_int_equal(status, F_okay);
+      assert_int_equal(dynamic.string[0], datas[i][0]);
+      assert_int_equal(dynamic.string[1], datas[i][1]);
+      assert_int_equal(dynamic.string[2], datas[i][2]);
+      assert_int_equal(dynamic.string[3], datas[i][3]);
+      assert_int_equal(dynamic.string[4], datas[i][4]);
+      assert_int_equal(dynamic.used, F_fss_simple_packet_block_header_size_d);
+
+      free(dynamic.string);
+    } // for
+  }
+}
+
+void test__f_fss_simple_packet_encode__works_little_endian(void **state) {
+
+  const f_char_t datas[3][5] = {
+    { 0x00, 0x00, 0x00, 0x00, 0x05 },
+    { 0x00, 0x00, 0x00, 0x00, 0x06 },
+    { 0x00, 0x00, 0x00, 0x00, 0x07 },
+  };
+
+  const f_number_unsigned_t size[] = {
+    5,
+    6,
+    7,
+  };
+
+  {
+    for (uint8_t i = 0; i < 3; ++i) {
+
+      f_string_dynamic_t dynamic = f_string_dynamic_t_initialize;
+
+      const f_status_t status = f_fss_simple_packet_encode(0, size[i], &dynamic);
+
+      assert_int_equal(status, F_okay);
+      assert_int_equal(dynamic.string[0], datas[i][0]);
+      assert_int_equal(dynamic.string[1], datas[i][1]);
+      assert_int_equal(dynamic.string[2], datas[i][2]);
+      assert_int_equal(dynamic.string[3], datas[i][3]);
+      assert_int_equal(dynamic.string[4], datas[i][4]);
+      assert_int_equal(dynamic.used, F_fss_simple_packet_block_header_size_d);
+
+      free(dynamic.string);
+    } // for
+  }
+}
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
diff --git a/level_0/f_fss/tests/unit/c/test-fss-simple_packet_encode.h b/level_0/f_fss/tests/unit/c/test-fss-simple_packet_encode.h
new file mode 100644 (file)
index 0000000..3aee967
--- /dev/null
@@ -0,0 +1,34 @@
+/**
+ * FLL - Level 0
+ *
+ * Project: FSS
+ * API Version: 0.7
+ * Licenses: lgpl-2.1-or-later
+ *
+ * Test the array types in the FSS project.
+ */
+#ifndef _TEST__F_fss__simple_packet_encode
+#define _TEST__F_fss__simple_packet_encode
+
+/**
+ * Test that the function correctly fails on invalid parameter.
+ *
+ * @see f_fss_simple_packet_encode()
+ */
+extern void test__f_fss_simple_packet_encode__parameter_checking(void **state);
+
+/**
+ * Test that the function works, with the control bit set to big endian.
+ *
+ * @see f_fss_simple_packet_encode()
+ */
+extern void test__f_fss_simple_packet_encode__works_big_endian(void **state);
+
+/**
+ * Test that the function works, with the control bit set to little endian.
+ *
+ * @see f_fss_simple_packet_encode()
+ */
+extern void test__f_fss_simple_packet_encode__works_little_endian(void **state);
+
+#endif // _TEST__F_fss__simple_packet_encode
index cebf91dddd62a90c883f09a60dc945c58cc3a6a3..950351b62c795c874a3bbdd29abe4eef0d518831 100644 (file)
@@ -129,11 +129,13 @@ int main(void) {
     cmocka_unit_test(test__f_fss_simple_packetss_delete_callback__works),
     cmocka_unit_test(test__f_fss_simple_packetss_destroy_callback__works),
 
+    cmocka_unit_test(test__f_fss_simple_packet_encode__works_little_endian),
     cmocka_unit_test(test__f_fss_simple_packet_extract__works_little_endian),
     cmocka_unit_test(test__f_fss_simple_packet_extract_range__works_little_endian),
 
     cmocka_unit_test(test__f_fss_simple_packet_extract__works_big_endian),
     cmocka_unit_test(test__f_fss_simple_packet_extract_range__works_big_endian),
+    cmocka_unit_test(test__f_fss_simple_packet_encode__works_big_endian),
 
     cmocka_unit_test(test__f_fss_simple_packet_extract__returns_packet_too_small),
     cmocka_unit_test(test__f_fss_simple_packet_extract_range__returns_packet_too_small),
@@ -170,6 +172,7 @@ int main(void) {
 
       cmocka_unit_test(test__f_fss_simple_packet_delete__parameter_checking),
       cmocka_unit_test(test__f_fss_simple_packet_destroy__parameter_checking),
+      cmocka_unit_test(test__f_fss_simple_packet_encode__parameter_checking),
       cmocka_unit_test(test__f_fss_simple_packet_extract__parameter_checking),
       cmocka_unit_test(test__f_fss_simple_packet_extract_range__parameter_checking),
 
index 8c0c0bd1e4163a055d3167ccc0bdc537d8bec90b..5b7433acea2d450f2073ca33cf92f50c8eb77dfb 100644 (file)
@@ -67,6 +67,7 @@
 #include "test-fss-set_quotes_destroy_callback.h"
 #include "test-fss-set_quotess_delete_callback.h"
 #include "test-fss-set_quotess_destroy_callback.h"
+#include "test-fss-simple_packet_encode.h"
 #include "test-fss-simple_packet_extract.h"
 #include "test-fss-simple_packet_extract_range.h"
 #include "test-fss-simple_packet_delete.h"