static const char* get_config_value ( struct lcfg *cfg, const char *key, int verbose ) { void *data; size_t len; if ( lcfg_value_get ( cfg, key, &data, &len ) != lcfg_status_ok ) { fprintf ( stderr, "Key %s is not found in the configuration file", key ); } const char *val = ( const char * ) data; if ( verbose ) { fprintf ( stdout, "%s = %s\n", key, val ); } return val; }
int main(int argc, char* argv[]) { pthread_t temperature_thread,templog_thread,setpoint_thread; int not_connect = 0; int c; char *channel = NULL; pthread_mutex_t mutex; pthread_cond_t condvar; int ret; int done = 0; gre_io_t *send_handle; thdata data1; size_t len; struct lcfg *cfg = lcfg_new("./brew.cfg"); //open the log openlog("Fermtroller", LOG_PID, LOG_USER); if ( lcfg_parse(cfg) != lcfg_status_ok ) { syslog(LOG_INFO,"Error reading config file : %s\n", lcfg_error_get(cfg)); } else { lcfg_accept(cfg, read_cfg,0); } lcfg_value_get(cfg, "wort_address",(void *)&temp_wort, &len); lcfg_value_get(cfg, "ambient_address",(void *)&temp_ambient, &len); if ( lcfg_value_get(cfg, "web_address", (void *)&web_address, &len) != lcfg_status_ok ) { syslog(LOG_ERR,"Error reading web_address : %s\n", lcfg_error_get(cfg)); } lcfg_value_get(cfg, "user",(void *)&user,&len); lcfg_value_get(cfg,"pass",(void *)&pass,&len); // assign the GPIO file descriptors cool_fd = open(cool_control,O_WRONLY); heat_fd = open(heat_control,O_WRONLY); data1.temp_data.temp1 = 0.0; data1.temp_data.temp2 = 0.0; data1.good_read = 0; data1.heatcool = 1; data1.debug = 0; data1.read_freq = 30; // default read frequency is 30 seconds control_temp("0","0"); while ((c = getopt( argc, argv, "vc:p:t:r:")) != -1 ) { switch(c) { case 'v': data1.debug = 1; break; case 'c': channel = optarg; syslog(LOG_INFO,"Setting channel to : %s\n", channel); break; case 't': data1.temp_data.set_point= atof(optarg); syslog(LOG_INFO,"Setting Ferment temp to %f\n", data1.temp_data.set_point); break; case 'r': data1.read_freq = atoi(optarg); syslog(LOG_INFO,"Setting read frequency to %d\n", data1.read_freq); break; } } #ifdef STORYBOARD if ( channel == NULL ) { syslog(LOG_ERR,"You must supply a greio channel temp_driver -c <storyboard channel>\n"); exit(-1); } send_handle = gre_io_open(channel, GRE_IO_TYPE_WRONLY); if(send_handle == NULL && channel != NULL ) { syslog(LOG_ERR,"Can't open send handle\n"); while ( not_connect == 0 ) { send_handle = gre_io_open(channel, GRE_IO_TYPE_WRONLY); if ( send_handle != NULL ) { not_connect = 1; } syslog(LOG_INFO,"Waiting to connect\n"); sleep(1); } } // push the send handle into the thread data pointer so we can access it in our check_tmep thread. data1.send_handle = send_handle; #endif FILE * t_fd = fopen("/tmp/set_point", "r"); char temp_buf[32]; long length; if ( t_fd ) { while ( fgets(temp_buf, 32, t_fd) != NULL ) { } } fclose(t_fd); if ( temp_buf ) { data1.temp_data.set_point = atof(temp_buf); syslog(LOG_DEBUG, "Setpoint converted to float is %f\n", data1.temp_data.set_point); } syslog(LOG_INFO,"Fermtroller starting"); pthread_create(&temperature_thread, NULL, (void *)&check_temperature, &data1); sleep(1); pthread_create(&templog_thread, NULL, (void *)&log_temp, &data1); pthread_create(&setpoint_thread, NULL, (void *)&read_setpoint, &data1); ret = pthread_mutex_init(&mutex, NULL); if(ret != 0) { syslog(LOG_ERR,"Mutex error\n"); return -1; } ret = pthread_cond_init(&condvar, NULL); if(ret != 0) { syslog(LOG_ERR,"Condvar error\n"); return -1; } //get_input(&data1); while(!done) { syslog(LOG_INFO,"waiting\n"); pthread_mutex_lock(&mutex); pthread_cond_wait(&condvar, &mutex); } lcfg_delete(cfg); free (temp_wort); free (temp_ambient); free (web_address); free (user); free (pass); syslog(LOG_INFO,"Fermtroller shutting down\n"); close(cool_fd); close(heat_fd); closelog(); return 0; }