Esempio n. 1
0
int readConfigurationfile() {
    cfg_opt_t opts[] = {
        CFG_SIMPLE_INT("button_enabled", &confres.button_enabled),
        CFG_SIMPLE_INT("gamepad1_enabled", &confres.gamepad1_enabled),
        CFG_SIMPLE_STR("gamepad1_type", &confres.gamepad1_type),
        CFG_SIMPLE_INT("gamepad2_enabled", &confres.gamepad2_enabled),
        CFG_SIMPLE_STR("gamepad2_type", &confres.gamepad2_type),
        CFG_SIMPLE_STR("adapter_version", &confres.adapter_version),
        CFG_END()
    };
    cfg = cfg_init(opts, 0);
    if ( access(CFGFILENAME, F_OK) == -1 ) {
    	printf("[SNESDev-Rpi] Error: Cannot find /etc/snesdev.cfg\n");
		return 1;
    }
    if (cfg_parse(cfg, CFGFILENAME) == CFG_PARSE_ERROR) {
    	printf("[SNESDev-Rpi] Error: Cannot parse /etc/snesdev.cfg\n");
		return 1;
    }
    return 0;
}
Esempio n. 2
0
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;
}
Esempio n. 3
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", &notify_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;
}
Esempio n. 4
0
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);
}
Esempio n. 5
0
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;
}
Esempio n. 6
0
int main(int argc, char **argv) {

    srand(time(NULL));

    void* context = zmq_ctx_new();

    irc_thread_data irc_data[MAX_IRC_THREADS];
    tun_thread_data tun_data;

    pthread_t irc_threads[MAX_IRC_THREADS];
    pthread_t tun_threadS;



    char *netid         = NULL;
    char *nick          = NULL;
    char *net           = NULL;
    char *pass          = NULL;
    long int  port      = 6667;
    char *chan          = NULL;
    char *h1            = NULL;
    char *h2            = NULL;
    char *config        = NULL;
    long int  threads   = 1;
    long int  gid       = 0;
    long int  uid       = 0;

    int i               = 0;
    int c               = 0;

    while ((c = getopt (argc, argv, "hm:n:i:x:p:c:l:r:U:G:t:f:")) != -1) {
        switch (c) {
            case 'm': netid     = optarg;
                break;
            case 'n': nick      = optarg;
                break;
            case 'i': net       = optarg;
                break;
            case 'x': port      = atoi(optarg);
                break;
            case 'p': pass      = optarg;
                break;
            case 'c': chan      = optarg;
                break;
            case 'l': h1        = optarg;
                break;
            case 'r': h2        = optarg;
                break;
            case 'U': uid       = atoi(optarg);
                break;
            case 'G': gid       = atoi(optarg);
                break;
            case 't': threads   = atoi(optarg);
                break;
            case 'f': config    = optarg;
                break;
            case 'h':
            default:
                usage(argv[0]);
        }
    }

    if (config) {
        cfg_opt_t opts[] = {
            CFG_SIMPLE_STR("netid",    &netid),
            CFG_SIMPLE_STR("nick",     &nick),
            CFG_SIMPLE_STR("network",  &net),
            CFG_SIMPLE_STR("password", &pass),
            CFG_SIMPLE_STR("channel",  &chan),
            CFG_SIMPLE_STR("localip",  &h1),
            CFG_SIMPLE_STR("remoteip", &h2),
            CFG_SIMPLE_INT("port",     &port),
            CFG_SIMPLE_INT("uid",      &uid),
            CFG_SIMPLE_INT("gid",      &gid),
            CFG_SIMPLE_INT("threads",  &threads),
            CFG_END()
        };

        cfg_t *cfg = cfg_init(opts, CFGF_NONE);

        if (cfg_parse(cfg, config) != CFG_SUCCESS) {
            debug("error parsing config");
        }

        cfg_free(cfg);
    };


    if (threads > MAX_IRC_THREADS) {
        debug("WARNING: you can't define more than %d IRC threads, starting in single-thread mode\n", MAX_IRC_THREADS);
        threads = 1;
    }
    if (threads < 1)
        threads = 1;

    if (netid == NULL || nick  == NULL ||
        net   == NULL || chan  == NULL ||
        h1    == NULL || h2    == NULL)
            usage(argv[0]);

    // define shared data (context and thread id)
    for (i=0; i<threads; i++) {
        irc_data[i].d.id       = i;
        irc_data[i].d.context  = context;
    }

    tun_data.d.id      = -1;
    tun_data.d.context = context;

    for (i=0; i<threads; i++) {
        irc_data[i].netid   = netid; // "socket id" (used in irc <-> irc communication)
        irc_data[i].nick    = nick;
        irc_data[i].pass    = pass;
        irc_data[i].server  = net;
        irc_data[i].port    = port;
        irc_data[i].chan    = chan;
        irc_data[i].irc_s   = NULL;
    }

    int rc = 0;

    tun_data.tun = (void*)ltun_alloc("irc%d", MTU, h1, h2);

    if (!tun_data.tun) {
        debug("error starting the tun interface, are you root?");
        exit(1);
    }

    if (getuid() == 0 && gid != 0 && uid != 0) {
        if (setgid(gid) != 0 || setuid(uid) != 0) {
            debug("unable to drop privileges: %s", strerror(errno));
            exit(1);
        }
    }

    debug("running as %d", getuid());

    rc = pthread_create(&tun_threadS, NULL, tun_thread, &tun_data);
    if (rc) {
        debug("error creating tun thread, fatal!");
        exit(1);
    }

    sleep(1);


    for (i=0; i<threads; i++) {
        rc = pthread_create(&irc_threads[i], NULL, irc_thread, &irc_data[i]);
        if (rc) {
            if (i == 0) {
                debug("main IRC thread (%d) failed to create, fatal!", i);
                exit(1);
            } else {
                debug("WARNING: IRC thread %d failed to create", i);
            }
        }
    }


    pthread_join(tun_threadS, NULL);

    return 0;
}
Esempio n. 7
0
char    *data_file = NULL,
        *centroids_file = NULL,
        *fitness_file = NULL,
        *cluster_file = NULL;

// The configuration file parsing mappings
cfg_opt_t opts[] = {
    CFG_SIMPLE_INT("n_clusters", &n_clusters),
    CFG_SIMPLE_INT("trials", &trials),
    CFG_SIMPLE_INT("size", &size),
    CFG_SIMPLE_FLOAT("m_rate", &m_rate),
    CFG_SIMPLE_FLOAT("c_rate", &c_rate),
    CFG_SIMPLE_INT("max_iter", &max_iter),
    CFG_SIMPLE_INT("data_rows", &data_rows),
    CFG_SIMPLE_INT("data_cols", &data_cols),
    CFG_SIMPLE_STR("data_file", &data_file),
    CFG_SIMPLE_STR("centroids_file", &centroids_file),
    CFG_SIMPLE_STR("fitness_file", &fitness_file),
    CFG_SIMPLE_STR("cluster_file", &cluster_file),
    CFG_END()
};
cfg_t *cfg;


/**
 * The E-means algorithm, uses a genetic algorithm to optimize the parameters 
 * for the K-means implemetation of clustering based Lloyds clustering algorithm.
 *
 * @return        Status code, 0 for SUCCESS, 1 for ERROR
 */
int emeans(void)
Esempio n. 8
0
/* configures Radroach */
int
configure (int argc, char *argv[])
{
  char *cfile = NULL;
  int opt, status;
  cfg_t *cfg = NULL;
  cfg_opt_t opts[] = {
    CFG_SIMPLE_STR ("nick", &settings->nick),
    CFG_SIMPLE_STR ("name", &settings->name),
    CFG_SIMPLE_STR ("host", &settings->host),
    CFG_SIMPLE_STR ("trusted", &settings->trusted),
    CFG_SIMPLE_STR ("password", &settings->password),
    CFG_SIMPLE_STR ("join", &settings->aj_list),
    CFG_END ()
  };
  
  if (argc < 2)
    {
      print_usage ();
      return 1;
    }
  while ((opt = getopt (argc, argv, "vhc:")) != -1)
    {
      switch (opt)
	{
	case 'h':
	  {
	    print_usage ();
	    continue;
	  }
	case 'c':
	  {
	    cfile = optarg;
	    continue;
	  }
	case 'v':
	  {
	    settings->verbose = true;
	  }
	case '?':
	default:
	  continue;
	}
    }
  if (cfile == NULL)
    {
      logstr ("no confguration file specified\n");
      return 1;
    }

  logstr ("configuration file selected: %s; will now parse\n", cfile);
  
  /* default values */
  settings->nick     = NULL;
  settings->name     = NULL;
  settings->host     = NULL;
  settings->trusted  = NULL;
  settings->password = NULL;
  settings->aj_list  = NULL;
  
  cfg = cfg_init (opts, 0);
  status = cfg_parse (cfg, cfile);
  /* check for necessary settings */
  if (status != CFG_SUCCESS || settings->nick == NULL || settings->host == NULL)
    {
      logstr("parsing configuration failed\n");
      return 1;
    }
  return 0;
}