#define DEFAULT_WORKING_DIR "/"
#define DEFAULT_LOG_DIR "/"
+#define ENV_RESV_MEM_SIZE "NE_RESV_MEM_SIZE"
+#define ENV_WORKING_DIR "NE_WORKING_DIR"
+#define ENV_LOG_DIR "NE_LOG_DIR"
+
static conf_t config;
const conf_t *conf = &config;
* @brief Load configuration with default values
* @param[out] c The configuration object to be updated.
*/
-static void load_conf_default(conf_t *c) {
+static void
+load_conf_default(conf_t *c)
+{
c->reserved_mem_size = DEFAULT_RESV_MEM_SIZE;
- c->working_dir = DEFAULT_WORKING_DIR;
- c->log_dir = DEFAULT_LOG_DIR;
+
+ strncpy (c->working_dir, DEFAULT_WORKING_DIR, strlen(DEFAULT_WORKING_DIR));
+ c->working_dir[strlen(DEFAULT_WORKING_DIR)] = '\x00';
+
+ strncpy (c->log_dir, DEFAULT_LOG_DIR, strlen(DEFAULT_LOG_DIR));
+ c->log_dir[strlen(DEFAULT_LOG_DIR)] = '\x00';
+
/** @todo NYI */
}
/**
- * @brief Load configuration with .ini file.
+ * @brief Load configuration for each config parameter
+ * @param[in] resv_mem_size config string for reserved mem size
+ * @param[in] working_dir config string for working dir
+ * @param[in] log_dir config string for log dir
* @param[out] c The configuration object to be updated.
+ */
+static void
+load_conf_each (const char *resv_mem_size, const char *working_dir,
+ const char *log_dir, conf_t *c)
+{
+ if (resv_mem_size) {
+ uint64_t val = 0;
+ char unit = '\x00';
+
+ sscanf (resv_mem_size, "%lu%c", &val, &unit);
+
+ if (val > 0) {
+ if (unit != '\x00') {
+ if (unit == 'K' || unit == 'k') {
+ val *= 1024;
+ } else if (unit == 'M' || unit == 'm') {
+ val *= 1024 * 1024;
+ } else if (unit == 'G' || unit == 'g') {
+ val *= 1024 * 1024 * 1024;
+ } else
+ fprintf (stderr, "Unknown unit for memory size: %c\n", unit);
+ }
+ c->reserved_mem_size = val;
+ } else
+ fprintf (stderr, "Cannot parse 'resv_mem_size': %s\n", resv_mem_size);
+ }
+
+ if (working_dir && strlen(working_dir) < MAX_DIR_LEN) {
+ strncpy (c->working_dir, working_dir, strlen(working_dir));
+ c->working_dir[strlen(working_dir)] = '\x00';
+ }
+
+ if (log_dir && strlen(log_dir) < MAX_DIR_LEN) {
+ strncpy (c->log_dir, log_dir, strlen(log_dir));
+ c->log_dir[strlen(log_dir)] = '\x00';
+ }
+}
+
+/**
+ * @brief Load configuration with .ini file.
* @param[in] ini The parsed .ini file.
+ * @param[out] c The configuration object to be updated.
*/
-static void load_conf_ini(const dictionary *ini, conf_t *c) {
- /** @todo NYI */
+static void load_conf_ini(const dictionary *ini, conf_t *c)
+{
+ const char *resv_mem_size = iniparser_getstring(ini, "main:resv_mem_size", NULL);
+ const char *working_dir = iniparser_getstring(ini, "main:working_dir", NULL);
+ const char *log_dir = iniparser_getstring(ini, "main:log_dir", NULL);
+
+ load_conf_each (resv_mem_size, working_dir, log_dir, c);
}
/**
* @brief Load configuration with env-vars
* @param[out] c The configuration object to be updated.
*/
-static void load_conf_envvar(conf_t *c) {
- /** @todo NYI */
+static void load_conf_envvar(conf_t *c)
+{
+ const char *resv_mem_size = getenv (ENV_RESV_MEM_SIZE);
+ const char *working_dir = getenv (ENV_WORKING_DIR);
+ const char *log_dir = getenv (ENV_LOG_DIR);
+
+ load_conf_each (resv_mem_size, working_dir, log_dir, c);
}
/**
* @brief Load the configuration from /etc/npu-engine.ini and env-vars.
* @detail Refer to the header file for more detail.
*/
-int load_conf(const char *inipath) {
+int load_conf(const char *inipath)
+{
dictionary *ini;
/* Load up config from default */