Sun, 27 Jul 2014 21:04:57 +0200
Small fix
/***************************************************************************** * 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 "rdconfig.h" #include "thermferm.h" #include "futil.h" #include "xutil.h" int debug = FALSE; static char *mypath; sys_config Config; /* System configuration */ #define MY_ENCODING "utf-8" const char TEMPSTATE[3][8] = { "OK", "MISSING", "ERROR" }; const char UNITMODE[5][8] = { "OFF", "NONE", "FRIDGE", "BEER", "PROFILE" }; const char UNITmode[5] = { 'o', 'n', 'f', 'b', 'p' }; const char PROFSTATE[4][6] = { "OFF", "PAUSE", "RUN", "DONE" }; void killconfig(void) { w1_therm *tmp1; units_list *tmp2; profiles_list *tmp3; prof_step *tmp4; if (Config.name) free(Config.name); Config.name = NULL; for (tmp1 = Config.w1therms; tmp1; tmp1 = 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; Config.tempFormat = 'C'; if (Config.air_address) free(Config.air_address); Config.air_address = NULL; Config.air_temperature = 20000; Config.air_state = 1; // missing for (tmp2 = Config.units; tmp2; tmp2 = tmp2->next) { if (tmp2->uuid) free(tmp2->uuid); if (tmp2->name) free(tmp2->name); if (tmp2->air_address) free(tmp2->air_address); if (tmp2->beer_address) free(tmp2->beer_address); if (tmp2->io1_address) free(tmp2->io1_address); if (tmp2->io2_address) free(tmp2->io2_address); if (tmp2->profile) free(tmp2->profile); free(tmp2); } Config.units = NULL; for (tmp3 = Config.profiles; tmp3; tmp3 = tmp3->next) { if (tmp3->uuid) free(tmp3->uuid); if (tmp3->name) free(tmp3->name); if (tmp3->steps) { for (tmp4 = tmp3->steps; tmp4; tmp4 = tmp4->next) { free(tmp4); } } free(tmp3); } Config.profiles = NULL; #ifdef HAVE_WIRINGPI_H Config.lcd_cols = 16; Config.lcd_rows = 2; #endif } int wrconfig(void) { int rc = 0; FILE *fp; xmlTextWriterPtr writer; xmlBufferPtr buf; w1_therm *tmp1; units_list *tmp3; profiles_list *tmp4; prof_step *tmp5; /* * 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; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "TEMPFORMAT", "%c", Config.tempFormat)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if (Config.air_address) { if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "AIR_ADDRESS", "%s", Config.air_address)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "AIR_STATE", "%d", Config.air_state)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "AIR_TEMPERATURE", "%d", Config.air_temperature)) < 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; } /* * Start one LCD. It is possible to connect 7 LCD displays on the i2c bus. * However this program doesn't use more then one yet. */ 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 "ADDRESS", "0x%x", Config.lcd_address)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); 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 /* * For backwards compatibility, the old setup */ if (Config.w1therms) { if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "W1THERMS")) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement"); return 1; } for (tmp1 = Config.w1therms; tmp1; tmp1 = tmp1->next) { if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "W1THERM")) < 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 "MASTER", "%s", tmp1->master)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BUS", "%d", tmp1->bus)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "NAME", "%s", tmp1->name)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ALIAS", "%s", tmp1->alias)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); 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; } } /* * 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) { /* * Only configuration items are written, measured values and states * are written to a state file. */ 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 = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID", "%s", tmp3->uuid)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "NAME", "%s", tmp3->name)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "VOLUME", "%.1f", tmp3->volume)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if (tmp3->air_address) { if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "AIR_ADDRESS", "%s", tmp3->air_address)) < 0)) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "AIR_STATE", "%d", tmp3->air_state)) < 0)) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "AIR_TEMPERATURE", "%d", tmp3->air_temperature)) < 0)) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } } if (tmp3->beer_address) { if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BEER_ADDRESS", "%s", tmp3->beer_address)) < 0)) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BEER_STATE", "%d", tmp3->beer_state)) < 0)) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if (((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BEER_TEMPERATURE", "%d", tmp3->beer_temperature)) < 0)) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } } if (tmp3->io1_address && ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "IO1_ADDRESS", "%s", tmp3->io1_address)) < 0)) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if (tmp3->io2_address && ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "IO2_ADDRESS", "%s", tmp3->io2_address)) < 0)) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HEATER", "%s", tmp3->heater_available ? "YES":"NO")) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "COOLER", "%s", tmp3->cooler_available? "YES":"NO")) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "FAN", "%s", tmp3->fan_available ? "YES":"NO")) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MODE", "%s", UNITMODE[tmp3->mode] )) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BEER_SET", "%.1f", tmp3->beer_set)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "FRIDGE_SET", "%.1f", tmp3->fridge_set)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "TEMP_SET_MIN", "%.1f", tmp3->temp_set_min)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "TEMP_SET_MAX", "%.1f", tmp3->temp_set_max)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "IDLE_RANGE_L", "%.1f", tmp3->idle_rangeL)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "IDLE_RANGE_H", "%.1f", tmp3->idle_rangeH)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if (tmp3->profile) { if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PROFILE", "%s", tmp3->profile)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PROF_STARTED", "%d", (unsigned int)tmp3->prof_started)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PROF_STATE", "%s", PROFSTATE[tmp3->prof_state] )) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); 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; } } /* * Fermenting profiles */ if (Config.profiles) { if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "PROFILES")) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement"); return 1; } for (tmp4 = Config.profiles; tmp4; tmp4 = tmp4->next) { if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "PROFILE")) < 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 "UUID", "%s", tmp4->uuid)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "NAME", "%s", tmp4->name)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BUSY", "%d", tmp4->busy)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if (tmp4->steps) { if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "STEPS")) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement"); return 1; } for (tmp5 = tmp4->steps; tmp5; tmp5 = tmp5->next) { if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "STEP")) < 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 "RESTTIME", "%d", tmp5->resttime)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "STEPTIME", "%d", tmp5->steptime)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); return 1; } if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "TARGET", "%.1f", tmp5->target)) < 0) { syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); 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; } } 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 *)"/.thermferm/etc/"); mkdirs(mypath, 0755); mypath = xstrcat(mypath, (char *)"thermferm.xml"); 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; } /* * Parse one LCD display */ #ifdef HAVE_WIRINGPI_H int parseLCD(xmlDocPtr doc, xmlNodePtr cur) { xmlChar *key; int ival; cur = cur->xmlChildrenNode; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *)"COLUMNS"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%d", &ival) == 1) Config.lcd_cols = ival; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"ROWS"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%d", &ival) == 1) Config.lcd_rows = ival; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"ADDRESS"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%x", &ival) == 1) Config.lcd_address = ival; xmlFree(key); } cur = cur->next; } return 0; } int parseLCDs(xmlDocPtr doc, xmlNodePtr cur) { cur = cur->xmlChildrenNode; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *)"LCD"))) { parseLCD(doc, cur); } cur = cur->next; } return 0; } #endif int parseW1therm(xmlDocPtr doc, xmlNodePtr cur) { xmlChar *key; int ival; w1_therm *w1therm, *tmp; w1therm = (w1_therm *)malloc(sizeof(w1_therm)); w1therm->next = NULL; w1therm->master = w1therm->name = w1therm->alias = NULL; w1therm->bus = w1therm->present = w1therm->lastval = w1therm->update = 0; cur = cur->xmlChildrenNode; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *)"VERSION"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (xmlStrcmp(key, (const xmlChar *)"1")) { xmlFree(key); return 1; } xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"MASTER"))) { w1therm->master = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"BUS"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%d", &ival) == 1) w1therm->bus = ival; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"NAME"))) { w1therm->name = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"ALIAS"))) { w1therm->alias = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); } cur = cur->next; } if (Config.w1therms == NULL) { Config.w1therms = w1therm; } else { for (tmp = Config.w1therms; tmp; tmp = tmp->next) { if (tmp->next == NULL) { tmp->next = w1therm; break; } } } return 0; } int parseW1therms(xmlDocPtr doc, xmlNodePtr cur) { cur = cur->xmlChildrenNode; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *)"W1THERM"))) { parseW1therm(doc, cur); } cur = cur->next; } return 0; } /* * Parse a fermenter unit */ int parseUnit(xmlDocPtr doc, xmlNodePtr cur) { xmlChar *key; int i, ival; float val; units_list *unit, *tmp; unit = (units_list *)malloc(sizeof(units_list)); unit->next = NULL; unit->version = 1; unit->uuid = unit->name = unit->air_address = unit->beer_address = unit->io1_address = unit->io2_address = unit->profile = NULL; unit->volume = 0.0; unit->heater_available = unit->cooler_available = unit->fan_available = FALSE; unit->air_temperature = unit->beer_temperature = unit->beer_set = unit->fridge_set = 20.0; unit->air_state = unit->beer_state = 1; // missing unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->mode = unit->prof_state = 0; unit->temp_set_min = 1.0; unit->temp_set_max = 30.0; unit->idle_rangeH = 1.0; unit->idle_rangeL = -1.0; unit->prof_started = (time_t)0; cur = cur->xmlChildrenNode; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *)"VERSION"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (xmlStrcmp(key, (const xmlChar *)"1")) { xmlFree(key); return 1; } unit->version = 1; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"UUID"))) { unit->uuid = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"NAME"))) { unit->name = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"VOLUME"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%f", &val) == 1) unit->volume = val; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"AIR_ADDRESS"))) { unit->air_address = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"AIR_STATE"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%d", &ival) == 1) unit->air_state = ival; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"AIR_TEMPERATURE"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%d", &ival) == 1) unit->air_temperature = ival; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"BEER_ADDRESS"))) { unit->beer_address = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"BEER_STATE"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%d", &ival) == 1) unit->beer_state = ival; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"BEER_TEMPERATURE"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%d", &ival) == 1) unit->beer_temperature = ival; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"IO1_ADDRESS"))) { unit->io1_address = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"IO2_ADDRESS"))) { unit->io2_address = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"HEATER"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (! xmlStrcmp(key, (const xmlChar *)"TRUE")) unit->heater_available = TRUE; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"COOLER"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (! xmlStrcmp(key, (const xmlChar *)"TRUE")) unit->cooler_available = TRUE; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"FAN"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (! xmlStrcmp(key, (const xmlChar *)"TRUE")) unit->fan_available = TRUE; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"MODE"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); for (i = 0; i < 5; i++) { if (! xmlStrcmp(key, (const xmlChar *)UNITMODE[i])) { unit->mode = i; break; } } xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"BEER_SET"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%f", &val) == 1) unit->beer_set = val; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"FRIDGE_SET"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%f", &val) == 1) unit->fridge_set = val; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"TEMP_SET_MIN"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%f", &val) == 1) unit->temp_set_min = val; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"TEMP_SET_MAX"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%f", &val) == 1) unit->temp_set_max = val; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"IDLE_RANGE_L"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%f", &val) == 1) unit->idle_rangeL = val; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"IDLE_RANGE_H"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%f", &val) == 1) unit->idle_rangeH = val; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"PROFILE"))) { unit->profile = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"PROF_STARTED"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%d", &ival) == 1) unit->prof_started = ival; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"PROF_STATE"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); for (i = 0; i < 4; i++) { if (! xmlStrcmp(key, (const xmlChar *)PROFSTATE[i])) { unit->prof_state = i; break; } } xmlFree(key); } cur = cur->next; } if (Config.units == NULL) { Config.units = unit; } else { for (tmp = Config.units; tmp; tmp = tmp->next) { if (tmp->next == NULL) { tmp->next = unit; break; } } } return 0; } int parseFermenters(xmlDocPtr doc, xmlNodePtr cur) { cur = cur->xmlChildrenNode; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *)"UNIT"))) { parseUnit(doc, cur); } cur = cur->next; } return 0; } int parseStep(xmlDocPtr doc, xmlNodePtr cur, prof_step **profstep) { xmlChar *key; int ival; float val; prof_step *step, *tmp; step = (prof_step *)malloc(sizeof(prof_step)); step->next = NULL; step->version = 1; step->steptime = step->resttime = 0; step->target = 20.0; cur = cur->xmlChildrenNode; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *)"VERSION"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (xmlStrcmp(key, (const xmlChar *)"1")) { xmlFree(key); return 1; } step->version = 1; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"RESTTIME"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%d", &ival) == 1) step->resttime = ival; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"STEPTIME"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%d", &ival) == 1) step->steptime = ival; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"TARGET"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%f", &val) == 1) step->target = val; xmlFree(key); } cur = cur->next; } if (*profstep == NULL) { *profstep = step; } else { for (tmp = *profstep; tmp; tmp = tmp->next) { if (tmp->next == NULL) { tmp->next = step; break; } } } return 0; } int parseSteps(xmlDocPtr doc, xmlNodePtr cur, prof_step **step) { cur = cur->xmlChildrenNode; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *)"STEP"))) { parseStep(doc, cur, step); } cur = cur->next; } return 0; } int parseProfile(xmlDocPtr doc, xmlNodePtr cur) { xmlChar *key; profiles_list *profile, *tmp; int ival; profile = (profiles_list *)malloc(sizeof(profiles_list)); profile->next = NULL; profile->version = 1; profile->uuid = profile->name = NULL; profile->busy = 0; profile->steps = NULL; cur = cur->xmlChildrenNode; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *)"VERSION"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (xmlStrcmp(key, (const xmlChar *)"1")) { xmlFree(key); return 1; } profile->version = 1; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"UUID"))) { profile->uuid = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"NAME"))) { profile->name = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"BUSY"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%d", &ival) == 1) profile->busy = ival; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"STEPS"))) { parseSteps(doc, cur, &(profile)->steps); } cur = cur->next; } if (Config.profiles == NULL) { Config.profiles = profile; } else { for (tmp = Config.profiles; tmp; tmp = tmp->next) { if (tmp->next == NULL) { tmp->next = profile; break; } } } return 0; } int parseProfiles(xmlDocPtr doc, xmlNodePtr cur) { cur = cur->xmlChildrenNode; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *)"PROFILE"))) { parseProfile(doc, cur); } cur = cur->next; } return 0; } int rdconfig(void) { int rc = 0, ival; xmlDocPtr doc; xmlNodePtr cur; xmlChar *key; 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 *)"/.thermferm/etc/"); mkdirs(mypath, 0755); mypath = xstrcat(mypath, (char *)"thermferm.xml"); if ((doc = xmlParseFile(mypath)) == NULL) { /* * Not in the users home directory */ free(mypath); mypath = xstrcpy((char *)"/etc/mbsepi-apps/thermferm.xml"); if ((doc = xmlParseFile(mypath)) == NULL) { /* * Try /usr/local/etc */ free(mypath); mypath = xstrcpy((char *)"/usr/local/etc/mbsepi-apps/thermferm.xml"); if ((doc = xmlParseFile(mypath)) == NULL) { syslog(LOG_NOTICE, "rdconfig: could not find thermferm.xml"); return 1; } } } syslog(LOG_NOTICE, "rdconfig: 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*)"THERMFERM")) { syslog(LOG_NOTICE, "XML file %s is not a valid configuration file.", mypath); xmlFreeDoc(doc); return 1; } /* * Parse configuration */ cur = cur->xmlChildrenNode; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *)"VERSION"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (xmlStrcmp(key, (const xmlChar *)"1")) { xmlFree(key); syslog(LOG_NOTICE, "XML file %s is not a valid version", mypath); return 1; } xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"LISTEN_PORT"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (sscanf((const char *)key, "%d", &ival) == 1) Config.my_port = ival; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"TEMPFORMAT"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); Config.tempFormat = key[0]; xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"AIR_ADDRESS"))) { Config.air_address = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); } #ifdef HAVE_WIRINGPI_H if ((!xmlStrcmp(cur->name, (const xmlChar *)"LCDS"))) { parseLCDs(doc, cur); } #endif if ((!xmlStrcmp(cur->name, (const xmlChar *)"W1THERMS"))) { parseW1therms(doc, cur); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"FERMENTERS"))) { parseFermenters(doc, cur); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"PROFILES"))) { parseProfiles(doc, cur); } cur = cur->next; } xmlFreeDoc(doc); free(mypath); mypath = NULL; return rc; }