thermferm/rdconfig.c

changeset 51
a03b6dac5398
child 53
37623517e0ef
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/thermferm/rdconfig.c	Sun May 25 22:06:56 2014 +0200
@@ -0,0 +1,350 @@
+/*****************************************************************************
+ * 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"
+
+
+bool		debug = FALSE;
+static char	*mypath;
+static char	*k, *v;
+static int	linecnt = 0;
+sys_config	Config;			/* System configuration		*/
+
+
+
+static int getstr(char **);
+static int getint(char **);
+static int getw1(char **);
+#ifdef HAVE_WIRINGPI_H
+static int getrcs(char **);
+#endif
+//static int getbyt(char **);
+//static int gethex(char **);
+
+#define XSTR(x) #x
+#define STR(x) XSTR(x)
+
+/*
+ * System configuration table
+ */
+key_list keytab[] = {
+    {(char *)"mosq_host",	getstr,		&Config.mosq_host},
+    {(char *)"mosq_port",	getint,		(char **)&Config.mosq_port},
+    {(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},
+    {(char *)"rx433",		getint,		(char **)&Config.rx433},
+    {(char *)"tx433",		getint,		(char **)&Config.tx433},
+    {(char *)"rcswitch",	getrcs,		(char **)&Config.rcswitch},
+#endif
+    {NULL,			NULL,		NULL}
+};
+
+
+
+void killconfig(void)
+{
+    w1_therm	*tmp1, *old1;
+#ifdef HAVE_WIRINGPI_H
+    rc_switch	*tmp2, *old2;
+#endif
+
+    if (Config.name)
+	free(Config.name);
+    Config.name = NULL;
+
+    if (Config.mosq_host)
+	free(Config.mosq_host);
+    Config.mosq_host= (char *)"localhost";
+    Config.mosq_port = 1883;
+
+    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;
+    Config.rx433 = -1;
+    Config.tx433 = -1;
+
+    for (tmp2 = Config.rcswitch; tmp2; tmp2 = old2) {
+	old2 = tmp2->next;
+	if (tmp2->address)
+	    free(tmp2->address);
+	tmp2->address = NULL;
+	if (tmp2->alias)
+	    free(tmp2->alias);
+	tmp2->alias = NULL;
+	free(tmp2);
+    }
+    Config.rcswitch = NULL;
+
+#endif
+}
+
+
+
+int rdconfig(char *config) 
+{
+    char	buf[256], *p;
+    FILE	*fp;
+    int		i, rc = 0;
+
+    killconfig();
+
+    /*
+     * Search config file
+     */
+    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 not open %s", mypath);
+		return 1;
+	    }
+	}
+    }
+
+    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;
+}
+
+
+
+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;
+}
+
+
+
+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;
+}
+
+
+
+#ifdef HAVE_WIRINGPI_H
+static int getrcs(char **dest)
+{
+    char        *p, *q = NULL, *r = NULL;
+    rc_switch   **tmpm;
+
+    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';
+	if (debug)
+	    syslog(LOG_NOTICE, "rdconfig: getrcs: %s(%d): %s %s", mypath, linecnt, v, p);
+    }
+
+    for (tmpm = (rc_switch**)dest; *tmpm; tmpm=&((*tmpm)->next));
+    (*tmpm) = (rc_switch *) xmalloc(sizeof(rc_switch));
+    (*tmpm)->next = NULL;
+    (*tmpm)->address = xstrcpy(v);
+    (*tmpm)->alias = xstrcpy(p);
+
+    return 0;
+}
+#endif
+
+
+/*
+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