thermferm/rdconfig.c

Tue, 24 Jun 2014 22:38:46 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 24 Jun 2014 22:38:46 +0200
changeset 75
4b976601737d
parent 72
f7cb53c50ee1
child 76
d2c7b32f27d6
permissions
-rw-r--r--

Writes a basic xml configuration next to the plain ascii config file

/*****************************************************************************
 * Copyright (C) 2014
 *   
 * Michiel Broek <mbroek at mbse dot eu>
 *
 * This file is part of the mbsePi-apps
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * mbsePi-apps is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with EC-65K; see the file COPYING.  If not, write to the Free
 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *****************************************************************************/

#include "thermferm.h"


int		debug = FALSE;
static char	*mypath;
static char	*k, *v;
static int	linecnt = 0;
sys_config	Config;			/* System configuration		*/

#define MY_ENCODING "utf-8"


//static int getstr(char **);
static int getw1(char **);
#ifdef HAVE_WIRINGPI_H
static int getint(char **);
#endif
static int getuch(char **);
static int getfloat(char **);
//static int getbyt(char **);
//static int gethex(char **);

#define XSTR(x) #x
#define STR(x) XSTR(x)

/*
 * System configuration table
 */
key_list keytab[] = {
    {(char *)"w1therm",			getw1,		(char **)&Config.w1therms},
#ifdef HAVE_WIRINGPI_H
    {(char *)"lcd_cols",		getint,		(char **)&Config.lcd_cols},
    {(char *)"lcd_rows",		getint,		(char **)&Config.lcd_rows},
#endif
    {(char *)"cs_mode",			getuch,		(char **)&Config.cs_mode},
    {(char *)"cs_beerSet",		getfloat,	(char **)&Config.cs_beerSet},
    {(char *)"cs_fridgeSet",		getfloat,	(char **)&Config.cs_fridgeSet},
    {(char *)"cs_heatEstimator",	getfloat,	(char **)&Config.cs_heatEstimator},
    {(char *)"cs_coolEstimator",	getfloat,	(char **)&Config.cs_coolEstimator},
    {(char *)"cc_tempFormat",		getuch,		(char **)&Config.cc_tempFormat},
    {(char *)"cc_tempSetMin",		getfloat,	(char **)&Config.cc_tempSetMin},
    {(char *)"cc_tempSetMax",		getfloat,	(char **)&Config.cc_tempSetMax},
    {(char *)"cc_idleRangeH",		getfloat,	(char **)&Config.cc_idleRangeH},
    {(char *)"cc_idleRangeL",		getfloat,	(char **)&Config.cc_idleRangeL},
    {NULL,				NULL,		NULL}
};



void killconfig(void)
{
    w1_therm	*tmp1, *old1;

    if (Config.name)
	free(Config.name);
    Config.name = NULL;

    for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) {
	old1 = tmp1->next;
	if (tmp1->master)
	    free(tmp1->master);
	if (tmp1->name)
	    free(tmp1->name);
	if (tmp1->alias)
	    free(tmp1->alias);
	free(tmp1);
    }
    Config.w1therms = NULL;
    Config.my_port = 6554;

#ifdef HAVE_WIRINGPI_H
    Config.lcd_cols = 16;
    Config.lcd_rows = 2;
#endif

    defaultControlSettings();
    defaultControlConstants();
}



int wrconfig(char *config, char *confxml)
{
    int			rc = 0;
    FILE		*fp;
    xmlTextWriterPtr	writer;
    xmlBufferPtr	buf;
    w1_therm    	*tmp1;
    units_list		*tmp3;

    if (getenv((char *)"USER") == NULL) {
	mypath = xstrcpy((char *)"/root");
    } else {
    	mypath = xstrcpy(getenv((char *)"HOME"));
    }
    mypath = xstrcat(mypath, (char *)"/mbsepi-apps/");
    mypath = xstrcat(mypath, config);

    if (debug)
	fprintf(stdout, "Writing %s\n", mypath);

    if ((fp = fopen(mypath, "w")) == NULL) {
	syslog(LOG_NOTICE, "could not rewrite %s", mypath);
	return 1;
    }

    fprintf(fp, "# Configuration file for thermferm %s\n", VERSION);
    fprintf(fp, "\n");

#ifdef HAVE_WIRINGPI_H
    fprintf(fp, "# LCD display\n");
    fprintf(fp, "#\n");
    fprintf(fp, "lcd_cols	%d\n", Config.lcd_cols);
    fprintf(fp, "lcd_rows	%d\n", Config.lcd_rows);
    fprintf(fp, "\n");
#endif

    fprintf(fp, "# DS18B20 temperature sensors on the 1-wire bus.\n");
    fprintf(fp, "#\n");
    fprintf(fp, "# kwd		master		bus	name		alias\n");
    for (tmp1 = Config.w1therms; tmp1; tmp1 = tmp1->next) {
	fprintf(fp, "w1therm		%s	%d	%s	%s\n", tmp1->master, tmp1->bus, tmp1->name, tmp1->alias);
    }
    fprintf(fp, "\n");

    fprintf(fp, "# Fermenter Units.\n");
    fprintf(fp, "#\n");
    for (tmp3 = Config.units; tmp3; tmp3 = tmp3->next) {
	fprintf(fp, "unit_uid	%s\n", tmp3->uid);
	if (tmp3->name)
	    fprintf(fp, "unit_name	%s\n", tmp3->name);
	if (tmp3->volume > 0.0)
	    fprintf(fp, "unit_volume	%.1f\n", tmp3->volume);
	if (tmp3->air_address)
	    fprintf(fp, "unit_air_address	%s\n", tmp3->air_address);
	if (tmp3->beer_address)
	    fprintf(fp, "unit_beer_address	%s\n", tmp3->beer_address);
	if (tmp3->io_address)
	    fprintf(fp, "unit_io_address	%s\n", tmp3->io_address);
	fprintf(fp, "unit_heater_available	%s\n", tmp3->heater_available ? "yes":"no");
	fprintf(fp, "unit_cooler_available	%s\n", tmp3->cooler_available ? "yes":"no");
	fprintf(fp, "unit_fan_available	%s\n", tmp3->fan_available ? "yes":"no");
	fprintf(fp, "unit_light_available	%s\n", tmp3->light_available ? "yes":"no");
	fprintf(fp, "unit_mode	%d\n", tmp3->mode);
	if (tmp3->profile) {
	    fprintf(fp, "unit_profile	%s\n", tmp3->profile);
	    fprintf(fp, "unit_prof_started	%d\n", (int)tmp3->prof_started);
	}
    }
    fprintf(fp, "\n");

    fprintf(fp, "# Control Settings.\n");
    fprintf(fp, "#\n");
    fprintf(fp, "cs_mode			%c\n", Config.cs_mode);
    fprintf(fp, "cs_beerSet		%.1f\n", Config.cs_beerSet);
    fprintf(fp, "cs_fridgeSet		%.1f\n", Config.cs_fridgeSet);
    fprintf(fp, "cs_heatEstimator	%.1f\n", Config.cs_heatEstimator);
    fprintf(fp, "cs_coolEstimator	%.1f\n", Config.cs_coolEstimator);
    fprintf(fp, "\n");

    fprintf(fp, "# Control Constants.\n");
    fprintf(fp, "#\n");
    fprintf(fp, "cc_tempFormat		%c\n", Config.cc_tempFormat);
    fprintf(fp, "cc_tempSetMin		%.1f\n", Config.cc_tempSetMin);
    fprintf(fp, "cc_tempSetMax		%.1f\n", Config.cc_tempSetMax);
    fprintf(fp, "cc_idleRangeH		%.1f\n", Config.cc_idleRangeH);
    fprintf(fp, "cc_idleRangeL		%.1f\n", Config.cc_idleRangeL);
    fprintf(fp, "\n");

    fprintf(fp, "# End of generated configuration\n");
    fclose(fp);
    syslog(LOG_NOTICE, "Written %s rc=%d", mypath, rc);
    free(mypath);
    mypath = NULL;


    /* 
     * Create a new XML buffer, to which the XML document will be written
     */
    if ((buf = xmlBufferCreate()) == NULL) {
	syslog(LOG_NOTICE, "wrconfig: error creating the xml buffer");
	return 1;
    }

    /* 
     * Create a new XmlWriter for memory, with no compression.
     */
    if ((writer = xmlNewTextWriterMemory(buf, 0)) == NULL) {
	syslog(LOG_NOTICE, "wrconfig: error creating the xml writer");
	return 1;
    }

    /*
     * Use indentation instead of one long line
     */
    if ((rc = xmlTextWriterSetIndent(writer, 2)) < 0) {
	syslog(LOG_NOTICE, "wrconfig: error setting Indent");
	return 1;
    }

    /* 
     * Start the document with the xml default for the version,
     * encoding ISO 8859-1 and the default for the standalone
     * declaration. 
     */
    if ((rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL)) < 0) {
	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartDocument");
	return 1;
    }

    /* 
     * Start an element named "THERMFERM". Since thist is the first
     * element, this will be the root element of the document.
     */
    if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "THERMFERM")) < 0) {
	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
	return 1;
    }

    /* 
     * Add an attribute with name "VERSION" and value "1" to THERMFERM. 
     */
    if ((rc = xmlTextWriterWriteElement(writer, BAD_CAST "VERSION", BAD_CAST "1")) < 0) {
	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
	return 1;
    }

    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "LISTEN_PORT", "%d", Config.my_port)) < 0) {
	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
	return 1;
    }

#ifdef HAVE_WIRINGPI_H
    /* 
     * Start an element named "LCDS" as child of THERMFERM.
     */
    if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "LCDS")) < 0) {
	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
	return 1;
    }
    if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "LCD")) < 0) {
	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
	return 1;
    }
    if ((rc = xmlTextWriterWriteElement(writer, BAD_CAST "VERSION", BAD_CAST "1")) < 0) {
	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
	return 1;
    }
    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "COLUMNS", "%d", Config.lcd_cols)) < 0) {
	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
	return 1;
    }
    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ROWS", "%d", Config.lcd_rows)) < 0) {
	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
	return 1;
    }
    /* 
     * Close the element named LCD.
     */
    if ((rc = xmlTextWriterEndElement(writer)) < 0) {
	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement");
	return 1;
    }
    /*
     * Close the element LCDS.
     */
    if ((rc = xmlTextWriterEndElement(writer)) < 0) {
	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement");
	return 1;
    }
#endif

    /*
     * Fermenter units
     */
    if (Config.units) {
	if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "FERMENTERS")) < 0) {
	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
	    return 1;
	}
	for (tmp3 = Config.units; tmp3; tmp3 = tmp3->next) {
	    if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "UNIT")) < 0) {
		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
		return 1;
	    }
	    if ((rc = xmlTextWriterWriteElement(writer, BAD_CAST "VERSION", BAD_CAST "1")) < 0) {
		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
		return 1;
	    }

	    if ((rc = xmlTextWriterEndElement(writer)) < 0) {
		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement");
		return 1;
	    }
	}
	if ((rc = xmlTextWriterEndElement(writer)) < 0) {
	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement");
	    return 1;
	}
    }

    /*
     * All done, close any open elements
     */
    if ((rc = xmlTextWriterEndDocument(writer)) < 0) {
	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndDocument");
	return 1;
    }
    xmlFreeTextWriter(writer);

    /*
     * Now write the XML configuration
     */
    if (getenv((char *)"USER") == NULL) {
	mypath = xstrcpy((char *)"/root");
    } else {
	mypath = xstrcpy(getenv((char *)"HOME"));
    }
    mypath = xstrcat(mypath, (char *)"/mbsepi-apps/");
    mypath = xstrcat(mypath, confxml);

    if (debug)
	fprintf(stdout, "Writing %s\n", mypath);

    if ((fp = fopen(mypath, "w")) == NULL) {
	syslog(LOG_NOTICE, "could not rewrite %s", mypath);
	return 1;
    }

    fprintf(fp, "%s", (const char *) buf->content);
    fclose(fp);
    xmlBufferFree(buf);

    return rc;
}



int rdconfig(char *config) 
{
    char	buf[256], *p;
    FILE	*fp;
    int		i, rc = 0;

    killconfig();

    syslog(LOG_NOTICE, "HOME='%s' USER='%s' LOGNAME='%s'", MBSE_SS(getenv((char *)"HOME")), MBSE_SS(getenv((char *)"USER")), MBSE_SS(getenv((char *)"LOGNAME")));

    /*
     * Search config file
     */
    if (getenv((char *)"USER") == NULL) {
	mypath = xstrcpy((char *)"/root");
    } else {
    	mypath = xstrcpy(getenv((char *)"HOME"));
    }
    mypath = xstrcat(mypath, (char *)"/mbsepi-apps/");
    mypath = xstrcat(mypath, config);
    if ((fp = fopen(mypath, "r")) == NULL) {
	/*
	 * Not in the users home directory
	 */
	free(mypath);
	mypath = xstrcpy((char *)"/etc/mbsepi-apps/");
	mypath = xstrcat(mypath, config);
	if ((fp = fopen(mypath, "r")) == NULL) {
	    /*
	     * Try /usr/local/etc
	     */
	    free(mypath);
	    mypath = xstrcpy((char *)"/usr/local/etc/mbsepi-apps/");
	    mypath = xstrcat(mypath, config);
	    if ((fp = fopen(mypath, "r")) == NULL) {
		syslog(LOG_NOTICE, "rdconfig: could find %s", config);
		return 1;
	    }
	}
    }
    syslog(LOG_NOTICE, "rdconfig: using %s", mypath);

    linecnt = 0;
    while (fgets(buf, sizeof(buf) -1, fp)) {
	linecnt++;
	if (*(p = buf + strlen(buf) -1) != '\n') {
	    syslog(LOG_NOTICE, "rdconfig: %s(%d): \"%s\" - line too long", mypath, linecnt, buf);
	    rc = 1;
	    break;
	}
	*p-- = '\0';
	while ((p >= buf) && isspace(*p))
	    *p-- = '\0';
	k = buf;
	while (*k && isspace(*k))
	    k++;
	p = k;
	while (*p && !isspace(*p))
	    p++;
	*p++='\0';
	v = p;
	while (*v && isspace(*v)) 
	    v++;

	if ((*k == '\0') || (*k == '#')) {
	    continue;
	}

	for (i = 0; keytab[i].key; i++)
	    if (strcasecmp(k,keytab[i].key) == 0)
		break;

	if (keytab[i].key == NULL) {
	    syslog(LOG_NOTICE, "rdconfig: %s(%d): %s %s - unknown keyword", mypath, linecnt, MBSE_SS(k), MBSE_SS(v));
	    rc = 1;
	    break;
	} else if ((keytab[i].prc(keytab[i].dest))) {
	    rc = 1;
	    break;
	}

    }
    fclose(fp);

    free(mypath);
    mypath = NULL;

    return rc;
}



/*
static int getstr(char **dest)
{
    if (debug)
	syslog(LOG_NOTICE, "rdconfig: getstr: %s(%d): %s %s", mypath, linecnt, MBSE_SS(k), MBSE_SS(v));

    *dest = xstrcpy(v);
    return 0;
}
*/



#ifdef HAVE_WIRINGPI_H
static int getint(char **dest)
{
    if (debug)
	syslog(LOG_NOTICE, "rdconfig: getint: %s(%d): %s %s", mypath, linecnt, MBSE_SS(k), MBSE_SS(v));

    if (strspn(v,"0123456789") != strlen(v)) 
	syslog(LOG_NOTICE, "rdconfig: %s(%d): %s %s - bad numeric", mypath, linecnt, MBSE_SS(k), MBSE_SS(v));
    else 
	*((int*)dest)=atoi(v);
    return 0;
}
#endif



static int getw1(char **dest)
{
    char	*p, *q = NULL, *r = NULL;
    w1_therm	**tmpm;
    int		rc = 0, tmpp;

    for (p = v; *p && !isspace(*p); p++);
    if (*p)
	*p++ = '\0';
    while (*p && isspace(*p))
	p++;
    if (*p == '\0') {
	syslog(LOG_NOTICE, "rdconfig: %s(%d): less then two tokens", mypath, linecnt);
	return 1;
    }

    for (q = p; *q && !isspace(*q); q++);
    if (*q && isspace(*q)) {
	if (*q)
	    *q++ = '\0';
	while (*q && isspace(*q))
	    q++;

	for (r = q; *r && !isspace(*r); r++);
	if (*r)
	    *r++ = '\0';
	rc = sscanf(p, "%d", &tmpp);
	if (rc != 1) {
	    syslog(LOG_NOTICE, "rdconfig: getw1: %s(%d): %s is not a integer value", mypath, linecnt, p);
	    return 1;
	}
	if (debug)
	    syslog(LOG_NOTICE, "rdconfig: getw1: %s(%d): %s %d %s %s", mypath, linecnt, v, tmpp, q, r);
    }

    for (tmpm = (w1_therm**)dest; *tmpm; tmpm=&((*tmpm)->next));
    (*tmpm) = (w1_therm *) xmalloc(sizeof(w1_therm));
    (*tmpm)->next = NULL;
    (*tmpm)->master = xstrcpy(v);
    (*tmpm)->bus = tmpp;
    (*tmpm)->name = xstrcpy(q);
    (*tmpm)->alias = xstrcpy(r);
    (*tmpm)->present = 0;
    (*tmpm)->lastval = 0;
    (*tmpm)->update = 0;

    return 0;
}



static int getuch(char **dest)
{
    if (debug)
	syslog(LOG_NOTICE, "rdconfig: getuch: %s(%d): %s %s", mypath, linecnt, MBSE_SS(k), MBSE_SS(v));

    if (isalnum(v[0])) {
	*((unsigned char*)dest) = v[0];
    } else {
	syslog(LOG_NOTICE, "rdconfig: %s(%d): %s %s - bad character", mypath, linecnt, MBSE_SS(k), MBSE_SS(v));
    }
    return 0;
}



static int getfloat(char **dest)
{
    float	val = 0.0;
    int		rc;

    if (debug)
	syslog(LOG_NOTICE, "rdconfig: getfloat: %s(%d): %s %s", mypath, linecnt, MBSE_SS(k), MBSE_SS(v));

    rc = sscanf(v, "%f", &val);
    if (rc != 1) {
	syslog(LOG_NOTICE, "rdconfig: %s(%d): %s %s - bad float value", mypath, linecnt, MBSE_SS(k), MBSE_SS(v));
	return 1;
    }
    *((float*)dest) = val;

    return 0;
}


/*
static int getbyt(char **dest)
{
    Log_Msg("[rdconfig] getbyt: %s(%d): %s %s", mypath, linecnt, k, v);
    if (strspn(v,"0123456789") != strlen(v))
	Log_Msg("[rdconfig] %s(%d): %s %s - bad numeric", mypath, linecnt, S(k), S(v));
    else
	*((Uint8*)dest)=atoi(v);
    return 0;
}
*/


/*
static int gethex(char **dest)
{
    unsigned int    val = 0;
    int		    rc;

    Log_Msg("[rdconfig] gethex: %s(%d): %s %s", mypath, linecnt, k, v);
    rc = sscanf(v, "%08x", &val);
    if (rc != 1) {
	Log_Msg("[rdconfig] %s(%d): %s %s - bad hex value", mypath, linecnt, S(k), S(v));
	return 1;
    }
    *((int*)dest) = val;

    return 0;
}
*/

mercurial