diff -r 8df3252b688f -r bde74a8f2ad7 brewco/rdsession.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/brewco/rdsession.c Sat Nov 28 20:15:38 2015 +0100 @@ -0,0 +1,371 @@ +/***************************************************************************** + * Copyright (C) 2015 + * + * Michiel Broek + * + * 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 ThermFerm; see the file COPYING. If not, write to the Free + * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + *****************************************************************************/ + +#include "brewco.h" +#include "rdsession.h" +#include "futil.h" +#include "xutil.h" + +extern int debug; + + + +#define MY_ENCODING "utf-8" + + +int do_wrsession(brew_session *brew); +int do_wrsession(brew_session *brew) +{ + int rc = 0; + FILE *fp; + char *mypath = NULL; + xmlTextWriterPtr writer; + xmlBufferPtr buf; + + /* + * Create a new XML buffer, to which the XML document will be written + */ + if ((buf = xmlBufferCreate()) == NULL) { + syslog(LOG_NOTICE, "wrsession: 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, "wrsession: error creating the xml writer"); + return 1; + } + + /* + * Use indentation instead of one long line + */ + if ((rc = xmlTextWriterSetIndent(writer, 2)) < 0) { + syslog(LOG_NOTICE, "wrsession: 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, "wrsession: error at xmlTextWriterStartDocument"); + return 1; + } + + /* + * Start an element named "BREWCO". Since thist is the first + * element, this will be the root element of the document. + */ + if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "BREWCO_BREW")) < 0) { + syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterStartElement"); + return 1; + } + + /* + * Add an attribute with name "VERSION" and value "1" to BRWCO. + */ + if ((rc = xmlTextWriterWriteElement(writer, BAD_CAST "VERSION", BAD_CAST "1")) < 0) { + syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); + return 1; + } + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID_RECIPE", "%s", brew->uuid_recipe)) < 0) { + syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteFormatElement"); + return 1; + } + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID_UNIT", "%s", brew->uuid_unit)) < 0) { + syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteFormatElement"); + return 1; + } + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PREMASH", "%d", brew->premash)) < 0) { + syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); + return 1; + } + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MASHSTEP", "%d", brew->mashstep)) < 0) { + syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); + return 1; + } + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MASHSTATE", "%d", brew->mashstate)) < 0) { + syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); + return 1; + } + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "RESTTIME", "%d", brew->resttime)) < 0) { + syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); + return 1; + } + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "IODINE", "%d", brew->iodine)) < 0) { + syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); + return 1; + } + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MASHREMOVE", "%d", brew->mashremove)) < 0) { + syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); + return 1; + } + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BOILHEATING", "%d", brew->boilheating)) < 0) { + syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); + return 1; + } + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BOILING", "%d", brew->boiling)) < 0) { + syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); + return 1; + } + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BOILTIMER", "%d", brew->boiltimer)) < 0) { + syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); + return 1; + } + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "COOLING", "%d", brew->cooling)) < 0) { + syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); + return 1; + } + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "WHIRLPOOL", "%d", brew->whirlpool)) < 0) { + syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); + return 1; + } + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "CLEANUP", "%d", brew->cleanup)) < 0) { + syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); + return 1; + } + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "STARTTIME", "%d", (int)brew->starttime)) < 0) { + syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); + return 1; + } + if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENDTIME", "%d", (int)brew->endtime)) < 0) { + syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement"); + return 1; + } + + /* + * All done, close any open elements + */ + if ((rc = xmlTextWriterEndDocument(writer)) < 0) { + syslog(LOG_NOTICE, "wrsession: 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 *)"/.brewco/var/"); + mkdirs(mypath, 0755); + mypath = xstrcat(mypath, (char *)"brewing.xml"); + + if (debug) + fprintf(stdout, "Writing %s\n", mypath); + + if ((fp = fopen(mypath, "w")) == NULL) { + syslog(LOG_NOTICE, "could not rewrite %s", mypath); + free(mypath); + return 1; + } + free(mypath); + + fprintf(fp, "%s", (const char *) buf->content); + fclose(fp); + xmlBufferFree(buf); + + return 0; +} + + + +int wrsession(brew_session *brew) +{ + int rc; + + rc = do_wrsession(brew); + syslog(LOG_NOTICE, "Rewritten brewsession, rc=%d", rc); + return rc; +} + + + +/* + * Returns: + * 0 - All is well, session loaded. + * 1 - Something went wrong + * -1 - No brew session available + */ +int rdsession(brew_session *brew) +{ + int ival; + char *mypath; + xmlDocPtr doc; + xmlNodePtr cur; + xmlChar *key; + + 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 *)"/.brewco/var/"); + mkdirs(mypath, 0755); + mypath = xstrcat(mypath, (char *)"brewing.xml"); + + /* + * See if we have a brewing state file. + */ + if (file_exist(mypath, W_OK)) { + syslog(LOG_NOTICE, "rdsession: %s not found, good.", mypath); + free(mypath); + return -1; + } + + if ((doc = xmlParseFile(mypath)) == NULL) { + syslog(LOG_NOTICE, "rdsession: %s not found, should not happen.", mypath); + free(mypath); + return -1; + } + syslog(LOG_NOTICE, "rdsession: using %s", mypath); + + if ((cur = xmlDocGetRootElement(doc)) == NULL) { + syslog(LOG_NOTICE, "XML file %s empty.", mypath); + xmlFreeDoc(doc); + return 1; + } + if (xmlStrcmp(cur->name, (const xmlChar*)"BREWCO")) { + syslog(LOG_NOTICE, "XML file %s is not a valid configuration file.", mypath); + xmlFreeDoc(doc); + return 1; + } + + /* + * Parse session + */ + cur = cur->xmlChildrenNode; + while (cur != NULL) { + if ((!xmlStrcmp(cur->name, (const xmlChar *)"UUID_RECIPE"))) { + brew->uuid_recipe = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); + } + if ((!xmlStrcmp(cur->name, (const xmlChar *)"UUID_UNIT"))) { + brew->uuid_unit = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); + } + if ((!xmlStrcmp(cur->name, (const xmlChar *)"PREMASH"))) { + key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); + if (sscanf((const char *)key, "%d", &ival) == 1) + brew->premash = ival; + xmlFree(key); + } + if ((!xmlStrcmp(cur->name, (const xmlChar *)"MASHSTEP"))) { + key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); + if (sscanf((const char *)key, "%d", &ival) == 1) + brew->mashstep = ival; + xmlFree(key); + } + if ((!xmlStrcmp(cur->name, (const xmlChar *)"MASHSTATE"))) { + key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); + if (sscanf((const char *)key, "%d", &ival) == 1) + brew->mashstate = ival; + xmlFree(key); + } + if ((!xmlStrcmp(cur->name, (const xmlChar *)"RESTTIME"))) { + key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); + if (sscanf((const char *)key, "%d", &ival) == 1) + brew->resttime = ival; + xmlFree(key); + } + if ((!xmlStrcmp(cur->name, (const xmlChar *)"IODINE"))) { + key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); + if (sscanf((const char *)key, "%d", &ival) == 1) + brew->iodine = ival; + xmlFree(key); + } + if ((!xmlStrcmp(cur->name, (const xmlChar *)"MASHREMOVE"))) { + key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); + if (sscanf((const char *)key, "%d", &ival) == 1) + brew->mashremove = ival; + xmlFree(key); + } + if ((!xmlStrcmp(cur->name, (const xmlChar *)"BOILHEATING"))) { + key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); + if (sscanf((const char *)key, "%d", &ival) == 1) + brew->boilheating = ival; + xmlFree(key); + } + if ((!xmlStrcmp(cur->name, (const xmlChar *)"BOILING"))) { + key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); + if (sscanf((const char *)key, "%d", &ival) == 1) + brew->boiling = ival; + xmlFree(key); + } + if ((!xmlStrcmp(cur->name, (const xmlChar *)"BOILTIMER"))) { + key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); + if (sscanf((const char *)key, "%d", &ival) == 1) + brew->boiltimer = ival; + xmlFree(key); + } + if ((!xmlStrcmp(cur->name, (const xmlChar *)"COOLING"))) { + key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); + if (sscanf((const char *)key, "%d", &ival) == 1) + brew->cooling = ival; + xmlFree(key); + } + if ((!xmlStrcmp(cur->name, (const xmlChar *)"WHIRLPOOL"))) { + key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); + if (sscanf((const char *)key, "%d", &ival) == 1) + brew->whirlpool = ival; + xmlFree(key); + } + if ((!xmlStrcmp(cur->name, (const xmlChar *)"CLEANUP"))) { + key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); + if (sscanf((const char *)key, "%d", &ival) == 1) + brew->cleanup = ival; + xmlFree(key); + } + if ((!xmlStrcmp(cur->name, (const xmlChar *)"STARTTIME"))) { + key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); + if (sscanf((const char *)key, "%d", &ival) == 1) + brew->starttime = (time_t)ival; + xmlFree(key); + } + if ((!xmlStrcmp(cur->name, (const xmlChar *)"ENDTIME"))) { + key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); + if (sscanf((const char *)key, "%d", &ival) == 1) + brew->endtime = (time_t)ival; + xmlFree(key); + } + cur = cur->next; + } + xmlFreeDoc(doc); + + free(mypath); + mypath = NULL; + + return 0; +} + + +