Skip to content
Snippets Groups Projects
Select Git revision
  • fe9c421834693afe7eda7ebf619d0b7085549a1d
  • master default protected
  • esp32-nimble-wiki
  • rahix/hw-lock-new-mutex
  • dx/somewhat-more-dynamic-config
  • schneider/sdk-0.2.1-7
  • schneider/bsec
  • dx/meh-bdf-to-stm
  • dx/flatten-config-module
  • genofire/ble-follow-py
  • schneider/ble-stability
  • schneider/ble-stability-new-phy
  • add_menu_vibration
  • plaetzchen/ios-workaround
  • blinkisync-as-preload
  • schneider/max30001-pycardium
  • schneider/max30001-epicaridum
  • schneider/max30001
  • schneider/stream-locks
  • schneider/fundamental-test
  • schneider/ble-buffers
  • v1.12
  • v1.11
  • v1.10
  • v1.9
  • v1.8
  • v1.7
  • v1.6
  • v1.5
  • v1.4
  • v1.3
  • v1.2
  • v1.1
  • v1.0
  • release-1
  • bootloader-v1
  • v0.0
37 results

config.c

Blame
  • Forked from card10 / firmware
    Source project has a limited visibility.
    • swym's avatar
      f1d63669
      feat(epicardium): read card10.cfg · f1d63669
      swym authored and rahix's avatar rahix committed
      Adds simple config parser along with config_ API that:
      
      - supports default values for options
      - allows typed querying of config values
      - types supported: boolean, integer, floating point and string
      
      unknown options are ignored and LOG_WARNed on the console
      f1d63669
      History
      feat(epicardium): read card10.cfg
      swym authored and rahix's avatar rahix committed
      Adds simple config parser along with config_ API that:
      
      - supports default values for options
      - allows typed querying of config values
      - types supported: boolean, integer, floating point and string
      
      unknown options are ignored and LOG_WARNed on the console
    config.c 7.16 KiB
    #include "modules/log.h"
    #include "modules/config.h"
    #include "modules/filesystem.h"
    
    #include <assert.h>
    #include <stdbool.h>
    #include <ctype.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    #define CONFIG_MAX_LINE_LENGTH 80
    
    enum OptionType {
    	OptionType_Boolean,
    	OptionType_Int,
    	OptionType_Float,
    	OptionType_String,
    };
    
    struct config_option {
    	const char *name;
    	enum OptionType type;
    	union {
    		bool boolean;
    		long integer;
    		double floating_point;
    		char *string;
    	} value;
    };
    
    static struct config_option s_options[_EpicOptionCount] = {
    /* clang-format off */
    	#define INIT_Boolean(v)		        { .boolean        = (v) }
    	#define INIT_Int(v)  		        { .integer        = (v) }
    	#define INIT_Float(v)  		        { .floating_point = (v) }
    	#define INIT_String(v)  	        { .string         = (v) }
    	#define INIT_(tp, v)                INIT_ ## tp (v)
    	#define INIT(tp, v)                 INIT_ (tp, v)
    
    	#define CARD10_SETTING(identifier, spelling, tp, default_value)     \
    		[Option ## identifier] = { .name  = (spelling),                 \
    					               .type  = OptionType_ ## tp,          \
    					               .value = INIT(tp, (default_value)) },
    
    	#include "modules/config.def"
    	/* clang-format on */
    };
    
    static struct config_option *findOption(const char *key)
    {
    	for (int i = 0; i < _EpicOptionCount; ++i) {
    		if (!strcmp(key, s_options[i].name)) {
    			return &s_options[i];
    		}
    	}
    	return NULL;
    }
    
    static bool set_bool(struct config_option *opt, const char *value)
    {
    	bool val;
    	if (!strcmp(value, "1")) {
    		val = true;
    	} else if (!strcmp(value, "true")) {
    		val = true;
    	} else if (!strcmp(value, "0")) {
    		val = false;
    	} else if (!strcmp(value, "false")) {
    		val = false;