From 0bd981910ad3014d5a0cf6f8252a2613faea422a Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Sun, 10 Nov 2019 22:08:40 +0100
Subject: [PATCH] feat(config): Upgrade some calls to epics

---
 epicardium/epicardium.h        | 47 ++++++++++++++++++++++++++++++++++
 epicardium/modules/config.c    | 17 ++++++------
 epicardium/modules/config.h    |  5 ----
 epicardium/modules/lifecycle.c |  2 +-
 4 files changed, 57 insertions(+), 14 deletions(-)

diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h
index 13358966..496efe82 100644
--- a/epicardium/epicardium.h
+++ b/epicardium/epicardium.h
@@ -143,6 +143,9 @@ typedef _Bool bool;
 
 #define API_WS2812_WRITE           0x0120
 
+#define API_CONFIG_GET_STRING      0x130
+#define API_CONFIG_GET_INTEGER     0x131
+#define API_CONFIG_GET_BOOLEAN     0x132
 /* clang-format on */
 
 typedef uint32_t api_int_id_t;
@@ -1924,5 +1927,49 @@ API(API_USB_CDCACM, int epic_usb_cdcacm(void));
  */
 API(API_WS2812_WRITE, void epic_ws2812_write(uint8_t pin, uint8_t *pixels, uint32_t n_bytes));
 
+
+/**
+ * Configuration
+ * ======
+ */
+
+/**
+ * Read an integer from the configuration file
+ *
+ * :param char* key: Name of the option to read
+ * :param int* value: Place to read the value into
+ * :return: `0` on success or a negative value if an error occured. Possible
+ *    errors:
+ *
+ *    - ``-ENOENT``: Value can not be read
+ */
+API(API_CONFIG_GET_INTEGER, int epic_config_get_integer(const char *key, int *value));
+
+/**
+ * Read a boolean from the configuration file
+ *
+ * :param char* key: Name of the option to read
+ * :param bool* value: Place to read the value into
+ * :return: `0` on success or a negative value if an error occured. Possible
+ *    errors:
+ *
+ *    - ``-ENOENT``: Value can not be read
+ */
+API(API_CONFIG_GET_BOOLEAN, int epic_config_get_boolean(const char *key, bool *value));
+
+/**
+ * Read a string from the configuration file
+ *
+ * :param char* key: Name of the option to read
+ * :param char* buf: Place to read the string into
+ * :param size_t buf_len: Size of the provided buffer
+ * :return: `0` on success or a negative value if an error occured. Possible
+ *    errors:
+ *
+ *    - ``-ENOENT``: Value can not be read
+ */
+API(API_CONFIG_GET_STRING, int epic_config_get_string(const char *key, char *buf, size_t buf_len));
+
+
 #endif /* _EPICARDIUM_H */
 
diff --git a/epicardium/modules/config.c b/epicardium/modules/config.c
index 80153ba4..78e24cd1 100644
--- a/epicardium/modules/config.c
+++ b/epicardium/modules/config.c
@@ -1,6 +1,7 @@
 #include "modules/log.h"
 #include "modules/config.h"
 #include "modules/filesystem.h"
+#include "epicardium.h"
 
 #include <assert.h>
 #include <stdbool.h>
@@ -319,7 +320,7 @@ static size_t read_config_offset(size_t seek_offset, char *buf, size_t buf_len)
 }
 
 // returns error if not found or invalid
-int config_get_integer(const char *key, int *value)
+int epic_config_get_integer(const char *key, int *value)
 {
 	config_slot *slot = find_config_slot(key);
 	if (slot && slot->value != NOT_INT_MAGIC) {
@@ -333,7 +334,7 @@ int config_get_integer(const char *key, int *value)
 int config_get_integer_with_default(const char *key, int default_value)
 {
 	int value;
-	int ret = config_get_integer(key, &value);
+	int ret = epic_config_get_integer(key, &value);
 	if (ret) {
 		return default_value;
 	} else {
@@ -342,7 +343,7 @@ int config_get_integer_with_default(const char *key, int default_value)
 }
 
 // returns error if not found
-int config_get_string(const char *key, char *buf, size_t buf_len)
+int epic_config_get_string(const char *key, char *buf, size_t buf_len)
 {
 	config_slot *slot = find_config_slot(key);
 	if (!(slot && slot->value_offset)) {
@@ -366,7 +367,7 @@ int config_get_string(const char *key, char *buf, size_t buf_len)
 char *config_get_string_with_default(
 	const char *key, char *buf, size_t buf_len, char *dflt
 ) {
-	int ret = config_get_string(key, buf, buf_len);
+	int ret = epic_config_get_string(key, buf, buf_len);
 	if (ret) {
 		return dflt;
 	} else {
@@ -375,10 +376,10 @@ char *config_get_string_with_default(
 }
 
 // returns error if not found or invalid
-int config_get_boolean(const char *key, bool *value)
+int epic_config_get_boolean(const char *key, bool *value)
 {
 	int int_value;
-	int ret = config_get_integer(key, &int_value);
+	int ret = epic_config_get_integer(key, &int_value);
 
 	if (ret == 0) {
 		*value = !!int_value;
@@ -386,7 +387,7 @@ int config_get_boolean(const char *key, bool *value)
 	}
 
 	char buf[MAX_LINE_LENGTH + 1];
-	config_get_string(key, buf, MAX_LINE_LENGTH);
+	epic_config_get_string(key, buf, MAX_LINE_LENGTH);
 
 	if (buf == NULL) {
 		return -ENOENT;
@@ -407,7 +408,7 @@ int config_get_boolean(const char *key, bool *value)
 bool config_get_boolean_with_default(const char *key, bool default_value)
 {
 	bool value;
-	int ret = config_get_boolean(key, &value);
+	int ret = epic_config_get_boolean(key, &value);
 	if (ret) {
 		return default_value;
 	} else {
diff --git a/epicardium/modules/config.h b/epicardium/modules/config.h
index 4c8bdc60..c95d504c 100644
--- a/epicardium/modules/config.h
+++ b/epicardium/modules/config.h
@@ -7,11 +7,6 @@
 //initialize configuration values and load card10.cfg
 void load_config(void);
 
-// returns error if not found
-int config_get_integer(const char *key, int *value);
-int config_get_boolean(const char *key, bool *value);
-int config_get_string(const char *key, char *buf, size_t buf_len);
-
 // returns default_value if not found or invalid
 bool config_get_boolean_with_default(const char *key, bool default_value);
 int config_get_integer_with_default(const char *key, int default_value);
diff --git a/epicardium/modules/lifecycle.c b/epicardium/modules/lifecycle.c
index 5a2e457d..2e72880f 100644
--- a/epicardium/modules/lifecycle.c
+++ b/epicardium/modules/lifecycle.c
@@ -362,7 +362,7 @@ void vLifecycleTask(void *pvParameters)
 
 	hardware_init();
 
-	execute_elfs = config_get_boolean("execute_elf", false);
+	execute_elfs = config_get_boolean_with_default("execute_elf", false);
 
 	/* When triggered, reset core 1 to menu */
 	while (1) {
-- 
GitLab