From f9bbe02432aa77a4845c07b7f6a689cdaaf6ec41 Mon Sep 17 00:00:00 2001
From: Dominik Loidolt <dominik.loidolt@univie.ac.at>
Date: Thu, 24 Oct 2024 15:35:57 +0200
Subject: [PATCH] Refactor: extract chunk type functionality into separate file

Move chunk type determination logic from cmp_icu.c into its own file
---
 lib/icu_compress/cmp_chunk_type.c | 82 +++++++++++++++++++++++++++++++
 lib/icu_compress/cmp_chunk_type.h | 44 +++++++++++++++++
 lib/icu_compress/cmp_icu.c        | 79 +----------------------------
 lib/icu_compress/meson.build      |  1 +
 4 files changed, 128 insertions(+), 78 deletions(-)
 create mode 100644 lib/icu_compress/cmp_chunk_type.c
 create mode 100644 lib/icu_compress/cmp_chunk_type.h

diff --git a/lib/icu_compress/cmp_chunk_type.c b/lib/icu_compress/cmp_chunk_type.c
new file mode 100644
index 0000000..c6b9c45
--- /dev/null
+++ b/lib/icu_compress/cmp_chunk_type.c
@@ -0,0 +1,82 @@
+/**
+ * @file   cmp_chunk_type.h
+ * @author Dominik Loidolt (dominik.loidolt@univie.ac.at)
+ * @date   2024
+ *
+ * @copyright GPLv2
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * @brief functions and definitions for determining the chunk type of PLATO data
+ */
+
+#include "cmp_chunk_type.h"
+#include "../common/cmp_debug.h"
+#include "../common/cmp_data_types.h"
+
+
+/**
+ * @brief get the chunk_type of a collection
+ * @details map a sub-service to a chunk service according to
+ *	DetailedBudgetWorking_2023-10-11
+ *
+ * @param col	pointer to a collection header
+ *
+ * @returns chunk type of the collection, CHUNK_TYPE_UNKNOWN on
+ *	failure
+ */
+
+enum chunk_type cmp_col_get_chunk_type(const struct collection_hdr *col)
+{
+	enum chunk_type chunk_type;
+
+	switch (cmp_col_get_subservice(col)) {
+	case SST_NCxx_S_SCIENCE_IMAGETTE:
+		chunk_type = CHUNK_TYPE_NCAM_IMAGETTE;
+		break;
+	case SST_NCxx_S_SCIENCE_SAT_IMAGETTE:
+		chunk_type = CHUNK_TYPE_SAT_IMAGETTE;
+		break;
+	case SST_NCxx_S_SCIENCE_OFFSET:
+	case SST_NCxx_S_SCIENCE_BACKGROUND:
+		chunk_type = CHUNK_TYPE_OFFSET_BACKGROUND;
+		break;
+	case SST_NCxx_S_SCIENCE_SMEARING:
+		chunk_type = CHUNK_TYPE_SMEARING;
+		break;
+	case SST_NCxx_S_SCIENCE_S_FX:
+	case SST_NCxx_S_SCIENCE_S_FX_EFX:
+	case SST_NCxx_S_SCIENCE_S_FX_NCOB:
+	case SST_NCxx_S_SCIENCE_S_FX_EFX_NCOB_ECOB:
+		chunk_type = CHUNK_TYPE_SHORT_CADENCE;
+		break;
+	case SST_NCxx_S_SCIENCE_L_FX:
+	case SST_NCxx_S_SCIENCE_L_FX_EFX:
+	case SST_NCxx_S_SCIENCE_L_FX_NCOB:
+	case SST_NCxx_S_SCIENCE_L_FX_EFX_NCOB_ECOB:
+		chunk_type = CHUNK_TYPE_LONG_CADENCE;
+		break;
+	case SST_FCx_S_SCIENCE_IMAGETTE:
+	case SST_FCx_S_SCIENCE_OFFSET_VALUES:
+	case SST_FCx_S_BACKGROUND_VALUES:
+		chunk_type = CHUNK_TYPE_F_CHAIN;
+		break;
+	case SST_NCxx_S_SCIENCE_F_FX:
+	case SST_NCxx_S_SCIENCE_F_FX_EFX:
+	case SST_NCxx_S_SCIENCE_F_FX_NCOB:
+	case SST_NCxx_S_SCIENCE_F_FX_EFX_NCOB_ECOB:
+		debug_print("Error: No chunk is defined for fast cadence subservices");
+		/* fall through */
+	default:
+		chunk_type = CHUNK_TYPE_UNKNOWN;
+		break;
+	}
+
+	return chunk_type;
+}
diff --git a/lib/icu_compress/cmp_chunk_type.h b/lib/icu_compress/cmp_chunk_type.h
new file mode 100644
index 0000000..b726692
--- /dev/null
+++ b/lib/icu_compress/cmp_chunk_type.h
@@ -0,0 +1,44 @@
+/**
+ * @file   cmp_chunk_type.h
+ * @author Dominik Loidolt (dominik.loidolt@univie.ac.at)
+ * @date   2024
+ *
+ * @copyright GPLv2
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * @brief functions and definitions for determining the chunk type of PLATO data
+ */
+
+#ifndef CMP_CHUNK_TYPE_H
+#define CMP_CHUNK_TYPE_H
+
+#include "../common/cmp_data_types.h"
+
+
+/**
+ * @brief types of chunks containing different types of collections
+ *	according to DetailedBudgetWorking_2023-10-11
+ */
+
+enum chunk_type {
+	CHUNK_TYPE_UNKNOWN,
+	CHUNK_TYPE_NCAM_IMAGETTE,
+	CHUNK_TYPE_SHORT_CADENCE,
+	CHUNK_TYPE_LONG_CADENCE,
+	CHUNK_TYPE_SAT_IMAGETTE,
+	CHUNK_TYPE_OFFSET_BACKGROUND, /* N-CAM */
+	CHUNK_TYPE_SMEARING,
+	CHUNK_TYPE_F_CHAIN
+};
+
+
+enum chunk_type cmp_col_get_chunk_type(const struct collection_hdr *col);
+
+#endif /* CMP_CHUNK_TYPE_H */
diff --git a/lib/icu_compress/cmp_icu.c b/lib/icu_compress/cmp_icu.c
index b57d2ac..5d8fbdb 100644
--- a/lib/icu_compress/cmp_icu.c
+++ b/lib/icu_compress/cmp_icu.c
@@ -30,6 +30,7 @@
 #include "../common/cmp_entity.h"
 #include "../common/cmp_error.h"
 #include "../common/leon_inttypes.h"
+#include "cmp_chunk_type.h"
 
 #include "../cmp_icu.h"
 #include "../cmp_chunk.h"
@@ -1901,84 +1902,6 @@ static uint32_t cmp_ent_build_chunk_header(uint32_t *entity, uint32_t chunk_size
 }
 
 
-/**
- * @brief types of chunks containing different types of collections
- *	according to DetailedBudgetWorking_2023-10-11
- */
-
-enum chunk_type {
-	CHUNK_TYPE_UNKNOWN,
-	CHUNK_TYPE_NCAM_IMAGETTE,
-	CHUNK_TYPE_SHORT_CADENCE,
-	CHUNK_TYPE_LONG_CADENCE,
-	CHUNK_TYPE_SAT_IMAGETTE,
-	CHUNK_TYPE_OFFSET_BACKGROUND, /* N-CAM */
-	CHUNK_TYPE_SMEARING,
-	CHUNK_TYPE_F_CHAIN
-};
-
-
-/**
- * @brief get the chunk_type of a collection
- * @details map a sub-service to a chunk service according to
- *	DetailedBudgetWorking_2023-10-11
- *
- * @param col	pointer to a collection header
- *
- * @returns chunk type of the collection, CHUNK_TYPE_UNKNOWN on
- *	failure
- */
-
-static enum chunk_type cmp_col_get_chunk_type(const struct collection_hdr *col)
-{
-	enum chunk_type chunk_type;
-
-	switch (cmp_col_get_subservice(col)) {
-	case SST_NCxx_S_SCIENCE_IMAGETTE:
-		chunk_type = CHUNK_TYPE_NCAM_IMAGETTE;
-		break;
-	case SST_NCxx_S_SCIENCE_SAT_IMAGETTE:
-		chunk_type = CHUNK_TYPE_SAT_IMAGETTE;
-		break;
-	case SST_NCxx_S_SCIENCE_OFFSET:
-	case SST_NCxx_S_SCIENCE_BACKGROUND:
-		chunk_type = CHUNK_TYPE_OFFSET_BACKGROUND;
-		break;
-	case SST_NCxx_S_SCIENCE_SMEARING:
-		chunk_type = CHUNK_TYPE_SMEARING;
-		break;
-	case SST_NCxx_S_SCIENCE_S_FX:
-	case SST_NCxx_S_SCIENCE_S_FX_EFX:
-	case SST_NCxx_S_SCIENCE_S_FX_NCOB:
-	case SST_NCxx_S_SCIENCE_S_FX_EFX_NCOB_ECOB:
-		chunk_type = CHUNK_TYPE_SHORT_CADENCE;
-		break;
-	case SST_NCxx_S_SCIENCE_L_FX:
-	case SST_NCxx_S_SCIENCE_L_FX_EFX:
-	case SST_NCxx_S_SCIENCE_L_FX_NCOB:
-	case SST_NCxx_S_SCIENCE_L_FX_EFX_NCOB_ECOB:
-		chunk_type = CHUNK_TYPE_LONG_CADENCE;
-		break;
-	case SST_FCx_S_SCIENCE_IMAGETTE:
-	case SST_FCx_S_SCIENCE_OFFSET_VALUES:
-	case SST_FCx_S_BACKGROUND_VALUES:
-		chunk_type = CHUNK_TYPE_F_CHAIN;
-		break;
-	case SST_NCxx_S_SCIENCE_F_FX:
-	case SST_NCxx_S_SCIENCE_F_FX_EFX:
-	case SST_NCxx_S_SCIENCE_F_FX_NCOB:
-	case SST_NCxx_S_SCIENCE_F_FX_EFX_NCOB_ECOB:
-		debug_print("Error: No chunk is defined for fast cadence subservices");
-		/* fall through */
-	default:
-		chunk_type = CHUNK_TYPE_UNKNOWN;
-		break;
-	}
-
-	return chunk_type;
-}
-
-
 /**
  * @brief Set the compression configuration from the compression parameters
  *	based on the chunk type of the collection
diff --git a/lib/icu_compress/meson.build b/lib/icu_compress/meson.build
index 01f4d37..bbddb27 100644
--- a/lib/icu_compress/meson.build
+++ b/lib/icu_compress/meson.build
@@ -1,3 +1,4 @@
 icu_compress_sources = files([
+  'cmp_chunk_type.c',
   'cmp_icu.c'
 ])
-- 
GitLab