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

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 74
879bd09e2b96
child 76
d2c7b32f27d6

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

configure file | annotate | diff | comparison | revisions
configure.ac file | annotate | diff | comparison | revisions
thermferm/rdconfig.c file | annotate | diff | comparison | revisions
thermferm/sensors.c file | annotate | diff | comparison | revisions
thermferm/server.c file | annotate | diff | comparison | revisions
thermferm/thermferm.c file | annotate | diff | comparison | revisions
thermferm/thermferm.h file | annotate | diff | comparison | revisions
--- a/configure	Tue Jun 24 20:21:07 2014 +0200
+++ b/configure	Tue Jun 24 22:38:46 2014 +0200
@@ -2032,7 +2032,7 @@
 
 
 PACKAGE="mbsePi-apps"
-VERSION="0.0.7"
+VERSION="0.0.8"
 COPYRIGHT="Copyright (C) 2014 Michiel Broek, All Rights Reserved"
 CYEARS="2014"
 
@@ -3657,6 +3657,8 @@
 
 done
 
+else
+  as_fn_error $? "libxml2 not found" "$LINENO" 5
 fi
 
 
--- a/configure.ac	Tue Jun 24 20:21:07 2014 +0200
+++ b/configure.ac	Tue Jun 24 22:38:46 2014 +0200
@@ -8,7 +8,7 @@
 dnl General settings
 dnl After changeing the version number, run autoconf!
 PACKAGE="mbsePi-apps"
-VERSION="0.0.7"
+VERSION="0.0.8"
 COPYRIGHT="Copyright (C) 2014 Michiel Broek, All Rights Reserved"
 CYEARS="2014"
 AC_SUBST(PACKAGE)
@@ -69,6 +69,8 @@
   LIBS="$LIBS -lxml2"
   CFLAGS="$CFLAGS $(xml2-config --cflags)"
   AC_CHECK_HEADERS(libxml/xmlmemory.h,LIBXML2=Yes,LIBXML2=No)
+else
+  AC_MSG_ERROR(libxml2 not found)
 fi
 
 
--- a/thermferm/rdconfig.c	Tue Jun 24 20:21:07 2014 +0200
+++ b/thermferm/rdconfig.c	Tue Jun 24 22:38:46 2014 +0200
@@ -23,12 +23,13 @@
 #include "thermferm.h"
 
 
-bool		debug = FALSE;
+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 **);
@@ -100,12 +101,14 @@
 
 
 
-int wrconfig(char *config)
+int wrconfig(char *config, char *confxml)
 {
-    int		rc = 0;
-    FILE	*fp;
-    w1_therm    *tmp1;
-    units_list	*tmp3;
+    int			rc = 0;
+    FILE		*fp;
+    xmlTextWriterPtr	writer;
+    xmlBufferPtr	buf;
+    w1_therm    	*tmp1;
+    units_list		*tmp3;
 
     if (getenv((char *)"USER") == NULL) {
 	mypath = xstrcpy((char *)"/root");
@@ -192,6 +195,164 @@
     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;
 }
 
--- a/thermferm/sensors.c	Tue Jun 24 20:21:07 2014 +0200
+++ b/thermferm/sensors.c	Tue Jun 24 22:38:46 2014 +0200
@@ -23,7 +23,7 @@
 #include "thermferm.h"
 
 
-extern bool		debug;
+extern int		debug;
 extern sys_config	Config;
 extern int		my_shutdown;
 
--- a/thermferm/server.c	Tue Jun 24 20:21:07 2014 +0200
+++ b/thermferm/server.c	Tue Jun 24 22:38:46 2014 +0200
@@ -23,8 +23,8 @@
 #include "thermferm.h"
 
 
-extern bool		my_shutdown;
-extern bool		debug;
+extern int		my_shutdown;
+extern int		debug;
 extern int		current_unit;
 #ifdef HAVE_WIRINGPI_H
 extern int		lcdHandle;
--- a/thermferm/thermferm.c	Tue Jun 24 20:21:07 2014 +0200
+++ b/thermferm/thermferm.c	Tue Jun 24 22:38:46 2014 +0200
@@ -30,10 +30,10 @@
 key_t			key = 5680;		/* key to be passed to shmget()		*/
 int			shmid;
 
-bool			my_shutdown = false;
+int			my_shutdown = FALSE;
 static pid_t		pgrp, mypid;
 
-extern bool		debug;
+extern int		debug;
 extern sys_config	Config;
 #ifdef HAVE_WIRINGPI_H
 extern int		lcdHandle;
@@ -77,7 +77,7 @@
 	default:	syslog(LOG_NOTICE, "die() on signal %d", onsig);
     }
 
-    my_shutdown = true;
+    my_shutdown = TRUE;
 }
 
 
@@ -113,7 +113,7 @@
 	    break;
 
 	switch (c) {
-	    case 'd':	debug = true;
+	    case 'd':	debug = TRUE;
 			break;
 	    case 'h':	help();
 			return 1;
@@ -242,21 +242,29 @@
 #ifdef HAVE_WIRINGPI_H
     rc = piThreadCreate(my_sensors_loop);
 #else
-    rc = pthread_create(&threads[0], NULL, my_sensors_loop, (void *)t );
+    rc = pthread_create(&threads[t], NULL, my_sensors_loop, (void *)t );
 #endif
     if (rc) {
 	fprintf(stderr, "my_sensors_loop thread didn't start rc=%d\n", rc);
 	syslog(LOG_NOTICE, "my_sensors_loop thread didn't start rc=%d", rc);
+#ifndef HAVE_WIRINGPI_H
+    } else {
+	t++;
+#endif
     }
 
 #ifdef HAVE_WIRINGPI_H
     rc = piThreadCreate(my_server_loop);
 #else
-    rc = pthread_create(&threads[1], NULL, my_server_loop, (void *)t );
+    rc = pthread_create(&threads[t], NULL, my_server_loop, (void *)t );
 #endif
     if (rc) {
 	fprintf(stderr, "my_server_loop thread didn't start rc=%d\n", rc);
 	syslog(LOG_NOTICE, "my_server_loop thread didn't start rc=%d", rc);
+#ifndef HAVE_WIRINGPI_H
+    } else {
+	t++;
+#endif
     }
 
     snprintf(buf, 1023, "tempA,tempB");
@@ -268,15 +276,11 @@
 	if (my_shutdown)
 	    run = 0;
 
-	tmp1 = Config.w1therms;
-	if (tmp1->update) {
-	    tmp1->update = FALSE;
-	    lcdupdate = TRUE;
-	}
-	tmp1 = tmp1->next;
-	if (tmp1->update) {
-	    tmp1->update = FALSE;
-	    lcdupdate = TRUE;
+	for (tmp1 = Config.w1therms; tmp1; tmp1 = tmp1->next) {
+	    if (tmp1->update) {
+	    	tmp1->update = FALSE;
+	    	lcdupdate = TRUE;
+	    }
 	}
 
 #ifdef HAVE_WIRINGPI_H
@@ -296,12 +300,14 @@
 	now = time(NULL);
 	if (((int)now - (int)last) > 60) {
 	    last = now;
-	    tmp1 = Config.w1therms;
-	    temp = tmp1->lastval;
-	    tmp1 = tmp1->next;
-	    if (temp && tmp1->lastval && run) {
-	    	snprintf(buf, 1023, "%.2f,%.2f", temp / 1000.0, tmp1->lastval / 1000.0);
-	    	logger((char *)"thermferm.log", (char *)"thermferm", buf);
+	    if (Config.w1therms) {
+	    	tmp1 = Config.w1therms;
+	    	temp = tmp1->lastval;
+	    	tmp1 = tmp1->next;
+	    	if (temp && tmp1->lastval && run) {
+	    	    snprintf(buf, 1023, "%.2f,%.2f", temp / 1000.0, tmp1->lastval / 1000.0);
+	    	    logger((char *)"thermferm.log", (char *)"thermferm", buf);
+	    	}
 	    }
 	}
 	usleep(100000);
@@ -320,7 +326,7 @@
     stopLCD();
 #endif
 
-    wrconfig((char *)"thermferm.conf");
+    wrconfig((char *)"thermferm.conf", (char *)"thermferm.xml");
 
     ulockprog((char *)"thermferm");
 
--- a/thermferm/thermferm.h	Tue Jun 24 20:21:07 2014 +0200
+++ b/thermferm/thermferm.h	Tue Jun 24 22:38:46 2014 +0200
@@ -28,13 +28,13 @@
 #include <arpa/inet.h>
 #include <netdb.h>
 #include <poll.h>
-#include <stdbool.h>
 #ifndef HAVE_WIRINGPI_H
 #include <pthread.h>
 #endif
 #include <libxml/xmlmemory.h>
 #include <libxml/parser.h>
-
+#include <libxml/encoding.h>
+#include <libxml/xmlwriter.h>
 
 #ifdef HAVE_WIRINGPI_H
 /* wiringPi */
@@ -74,13 +74,13 @@
     float		beer_temp;		/* Beer temperature		*/
     char		*io_address;		/* DS2408 address		*/
     unsigned char	io_read;		/* I/O ports read state		*/
-    bool		heater_available;	/* Heater available		*/
+    int			heater_available;	/* Heater available		*/
     int			heater_state;		/* Heater status		*/
-    bool		cooler_available;	/* Cooler available		*/
+    int			cooler_available;	/* Cooler available		*/
     int			cooler_state;		/* Cooler status		*/
-    bool		fan_available;		/* Fan available		*/
+    int			fan_available;		/* Fan available		*/
     int			fan_state;		/* Fan status			*/
-    bool		light_available;	/* Door sensor and int. light	*/
+    int			light_available;	/* Door sensor and int. light	*/
     int			light_state;		/* Door and light status	*/
     int			mode;			/* Unit mode			*/
     char		*profile;		/* Active profile		*/
@@ -137,7 +137,7 @@
 
 void killconfig(void);
 int  rdconfig(char *);
-int  wrconfig(char *);
+int  wrconfig(char *, char *);
 
 
 /* lock.c */

mercurial