void gimmix_config_save (Conf *conf) { FILE *fp; cfg_t *cfg; cfg_opt_t *sopts; cfg_opt_t opts[] = { CFG_SIMPLE_STR ("mpd_hostname", NULL), CFG_SIMPLE_INT ("mpd_port", 0), CFG_SIMPLE_STR ("mpd_password", NULL), CFG_SIMPLE_BOOL ("enable_systray", false), CFG_SIMPLE_BOOL ("enable_notify", false), CFG_END() }; cfg = cfg_init(opts, 0); char *rcfile = cfg_tilde_expand ("~/.gimmixrc"); if((fp = fopen(rcfile, "w"))) { fprintf (fp, "# Gimmix configuration\n"); fprintf (fp, "\n# MPD hostname (default: localhost)\n"); if (conf->hostname) cfg_setstr(cfg, "mpd_hostname", conf->hostname); sopts = cfg_getopt (cfg, "mpd_hostname"); cfg_opt_print (sopts, fp); fprintf (fp, "\n# MPD port (default: 6600)\n"); if (conf->port > 0) cfg_setint(cfg, "mpd_port", conf->port); else cfg_setint(cfg, "mpd_port", 0); sopts = cfg_getopt (cfg, "mpd_port"); cfg_opt_print (sopts, fp); fprintf (fp, "\n# MPD password (leave blank for no password) \n"); if (conf->password) cfg_setstr(cfg, "mpd_password", conf->password); sopts = cfg_getopt (cfg, "mpd_password"); cfg_opt_print (sopts, fp); fprintf (fp, "\n# Enable/Disable systray icon (Enable = true, Disable = false)\n"); if (conf->systray_enable == 1) cfg_setbool(cfg, "enable_systray", true); else cfg_setbool(cfg, "enable_systray", false); sopts = cfg_getopt (cfg, "enable_systray"); cfg_opt_print (sopts, fp); fprintf (fp, "\n# Enable/Disable system tray notifications (Enable = true, Disable = false) \n"); if (conf->notify_enable == 1) cfg_setbool(cfg, "enable_notify", true); else cfg_setbool(cfg, "enable_notify", false); sopts = cfg_getopt (cfg, "enable_notify"); cfg_opt_print (sopts, fp); free (rcfile); fclose (fp); } else { fprintf (stderr, "Error while saving config.\n"); } cfg_free_value (opts); cfg_free (cfg); return; }
int main(int argc, char* argv[] ) { int i; pthread_t *pS0Thread[NUM_GPIOS]; pthread_t *pW1Thread; cfg_t *cfg; char logBuffer[LOG_BUFFER_SIZE+1]; cfg_opt_t opts[] = { CFG_SIMPLE_FLOAT("thermal_coefficient", &confOptions.termCoefficent), CFG_SIMPLE_INT("flow_per_hour", &confOptions.literAnHour), CFG_SIMPLE_INT("log_level", &confOptions.logLevel), CFG_SIMPLE_INT("temp_reader_delay", &confOptions.tempReaderDelay), CFG_SIMPLE_INT("temp_reader_interval", &confOptions.tempReaderIntval), CFG_SIMPLE_BOOL("push_bullet_notification", &confOptions.bulletNotificationEnabled), CFG_SIMPLE_BOOL("email_notification", &confOptions.emailNotificationEnabled), CFG_SIMPLE_STR("emoncms_api_key", &confOptions.emonApiKey), CFG_SIMPLE_BOOL("client_watchdog", &confOptions.watchdogEnabled), CFG_SIMPLE_INT("gpio_4_s0_in_use", &confOptions.numGPIO4S0InUse), CFG_SIMPLE_STR("therm_id_vorlauf", &confOptions.thermIdVorlauf), CFG_SIMPLE_STR("therm_id_ruecklauf", &confOptions.thermIdRuecklauf), CFG_SIMPLE_INT("oil_consumption_per_hour", &confOptions.oilConsumptionPerHour), CFG_END() }; confOptions.run_mode = DEFAULT_RUN_MODE; confOptions.termCoefficent = CONF_DEFAULT_THERMCOEFF; confOptions.literAnHour = CONF_DEFAULT_LITERANHOUR; confOptions.logLevel = CONF_DEFAULT_LOGLEVEL; confOptions.tempReaderDelay = CONF_DEFAULT_READERDELAY; confOptions.tempReaderIntval = CONF_DEFAULT_READERINTVAL; confOptions.bulletNotificationEnabled = CONF_DEFAULT_BULLETNOTICICATION; confOptions.emailNotificationEnabled = CONF_DEFAULT_EMAILNOTICICATION; confOptions.emonApiKey = CONF_DEFAULT_APIKEY; confOptions.watchdogEnabled = CONF_DEFAULT_WATCHDOGENABLED; confOptions.numGPIO4S0InUse = CONF_DEFAULT_NUMS0PINS; confOptions.thermIdVorlauf = strdup(CONF_DEFAULT_THERMIDVORLAUF); confOptions.thermIdRuecklauf = strdup(CONF_DEFAULT_THERMIDRUECKLAUF); if( (cfg = cfg_init(opts, 0)) != NULL ) { cfg_parse(cfg, CONF_FILE_PATH); #ifdef DEBUG_ALL printf("thermal_coefficient ....: %f\n", confOptions.termCoefficent); printf("flow_per_hour ..........: %ld\n", confOptions.literAnHour); printf("log_level ..............: %ld\n", confOptions.logLevel); printf("temp_reader_delay ......: %ld\n", confOptions.tempReaderDelay); printf("temp_reader_interval ...: %ld\n", confOptions.tempReaderIntval); printf("push_bullet_notification: %s\n", confOptions.bulletNotificationEnabled?"true":"false"); printf("email_notification .....: %s\n", confOptions.emailNotificationEnabled?"true":"false"); printf("emoncms_api_key ........: %s\n", confOptions.emonApiKey?confOptions.emonApiKey:"NULL"); printf("client_watchdog ........: %s\n", confOptions.watchdogEnabled?"true":"false"); printf("gpio_4_s0_in_use .......: %ld\n", confOptions.numGPIO4S0InUse); printf("therm_id_vorlauf .......: %s\n", confOptions.thermIdVorlauf?confOptions.thermIdVorlauf:"NULL"); printf("therm_id_ruecklauf .....: %s\n", confOptions.thermIdRuecklauf?confOptions.thermIdRuecklauf:"NULL"); printf("oil_consumption_per_hour: %ld\n", confOptions.oilConsumptionPerHour); #endif // DEBUG_ALL } else { exit(3); } mainEndFlag = 0; if( confOptions.emonApiKey != NULL && confOptions.thermIdVorlauf != NULL && confOptions.thermIdRuecklauf != NULL && confOptions.tempReaderDelay <= READER_DELAY_MAX && confOptions.tempReaderDelay >= 0 ) { if (gpioInitialise() < 0) { // pigpio initialisation failed. fprintf(stderr, "cannot initialize pigpio!\n"); return(1); } else { signal( SIGINT, sig_handler ); signal( SIGQUIT, sig_handler ); signal( SIGKILL, sig_handler ); signal( SIGTERM, sig_handler ); signal( SIGALRM, alrm_handler ); // pigpio initialisation okay ... for( i = 0; i < NUM_GPIOS; i++) { #ifdef DEBUG_ALL printf("Index %d\n", i); #endif // DEBUG_ALL gpioSetMode( gpioInUse[i].bcm_gpio, PI_INPUT ); // set GPIO as an input gpioSetPullUpDown( gpioInUse[i].bcm_gpio, PI_PUD_DOWN); // activate internal pull-up // create thread for this gpio pS0Thread[i] = gpioStartThread(tickerThread, (void*) &gpioInUse[i]); // gpioSetAlertFunc( gpioInUse[i].bcm_gpio, ticker[i] ); // set event handler "ticker" } if( (pW1Thread = gpioStartThread(W1ThermThread, NULL)) == NULL ) { fprintf(stderr, "W1Thread failed to start ...\n"); for( i = 0; i < NUM_GPIOS; i++) { if( pS0Thread[i] != NULL ) { gpioStopThread( pS0Thread[i] ); } } gpioTerminate(); } else { while( !mainEndFlag ) { gpioSleep(PI_TIME_RELATIVE, 0, 10000); // hold 1/100 sec high level } for( i = 0; i < NUM_GPIOS; i++) { if( pS0Thread[i] != NULL ) { gpioStopThread( pS0Thread[i] ); } } gpioStopThread( pW1Thread ); gpioTerminate(); cfg_free(cfg); if(confOptions.emonApiKey) { free(confOptions.emonApiKey); } if(confOptions.thermIdVorlauf) { free(confOptions.thermIdVorlauf); } if(confOptions.thermIdRuecklauf) { free(confOptions.thermIdRuecklauf); } } } } else { confFail(); } return(0); }
Conf * gimmix_config_init (void) { cfg_t *cfg = NULL; char *rcfile; Conf *conf; int ret; int port = 1; cfg_bool_t systray_enable = true; cfg_bool_t notify_enable = true; conf = (Conf*)malloc(sizeof(Conf)); char *host = NULL; char *pass = NULL; cfg_opt_t opts[] = { CFG_SIMPLE_STR ("mpd_hostname", &host), CFG_SIMPLE_INT ("mpd_port", &port), CFG_SIMPLE_STR ("mpd_password", &pass), CFG_SIMPLE_BOOL ("enable_systray", &systray_enable), CFG_SIMPLE_BOOL ("enable_notify", ¬ify_enable), CFG_END() }; cfg = cfg_init (opts, 0); rcfile = cfg_tilde_expand ("~/.gimmixrc"); ret = cfg_parse (cfg, rcfile); free (rcfile); if (ret == CFG_PARSE_ERROR) { free (conf); return NULL; } if (host != NULL) strncpy (conf->hostname, host, 255); if (pass != NULL) strncpy (conf->password, pass, 255); conf->port = port; if (systray_enable == true) { conf->systray_enable = 1; } else { conf->systray_enable = 0; } if (notify_enable == true) { conf->notify_enable = 1; } else { conf->notify_enable = 0; } /* Free the memory */ cfg_free_value (opts); if (cfg) cfg_free (cfg); return conf; }
int main(void) { static cfg_bool_t verbose = cfg_false; static char *server = NULL; static double delay = 1.356e-32; static char *username = NULL; /* Although the macro used to specify an integer option is called * CFG_SIMPLE_INT(), it actually expects a long int. On a 64 bit system * where ints are 32 bit and longs 64 bit (such as the x86-64 or amd64 * architectures), you will get weird effects if you use an int here. * * If you use the regular (non-"simple") options, ie CFG_INT() and use * cfg_getint(), this is not a problem as the data types are implicitly * cast. */ static long int debug = 1; cfg_opt_t opts[] = { CFG_SIMPLE_BOOL("verbose", &verbose), CFG_SIMPLE_STR("server", &server), CFG_SIMPLE_STR("user", &username), CFG_SIMPLE_INT("debug", &debug), CFG_SIMPLE_FLOAT("delay", &delay), CFG_END() }; cfg_t *cfg; /* set default value for the server option */ server = strdup("gazonk"); cfg = cfg_init(opts, 0); cfg_parse(cfg, "simple.conf"); printf("verbose: %s\n", verbose ? "true" : "false"); printf("server: %s\n", server); printf("username: %s\n", username); printf("debug: %ld\n", debug); printf("delay: %G\n", delay); printf("setting username to 'foo'\n"); /* using cfg_setstr here is not necessary at all, the equivalent * code is: * free(username); * username = strdup("foo"); */ cfg_setstr(cfg, "user", "foo"); printf("username: %s\n", username); /* print the parsed values to another file */ { FILE *fp = fopen("simple.conf.out", "w"); cfg_print(cfg, fp); fclose(fp); } cfg_free(cfg); /* You are responsible for freeing string values. */ free(server); free(username); return 0; }