thermferm/rdconfig.c

changeset 158
f1b7e2ef90be
parent 136
264e5ee5abfc
child 161
493e39bb0a08
--- a/thermferm/rdconfig.c	Thu Jul 31 13:43:09 2014 +0200
+++ b/thermferm/rdconfig.c	Thu Jul 31 17:44:50 2014 +0200
@@ -35,7 +35,9 @@
 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" };
-
+const char	DEVTYPE[7][6] = { "NA", "W1", "GPIO", "RC433", "DHT", "I2C", "SPI" };
+const char	DEVPRESENT[4][6] = { "UNDEF", "NO", "YES", "ERROR" };
+const char	DEVDIR[6][11] = { "UNDEF", "IN_BIN", "OUT_BIN", "IN_ANALOG", "OUT_ANALOG", "OUT_PWM" };
 
 
 
@@ -119,6 +121,7 @@
     units_list		*tmp3;
     profiles_list	*tmp4;
     prof_step		*tmp5;
+    devices_list	*device;
 
     /* 
      * Create a new XML buffer, to which the XML document will be written
@@ -495,6 +498,80 @@
 	}
     }
 
+    if (Config.devices) {
+	if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "DEVICES")) < 0) {
+	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
+	    return 1;
+	}
+	for (device = Config.devices; device; device = device->next) {
+	    if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "DEVICE")) < 0) {
+		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
+		return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "VERSION", "%d", device->version)) < 0) {
+		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
+		return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID", "%s", device->uuid)) < 0) {
+		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+		return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "TYPE", "%s", DEVTYPE[device->type])) < 0) {
+		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+		return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "DIRECTION", "%s", DEVDIR[device->direction])) < 0) {
+		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+		return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "VALUE", "%d", device->value)) < 0) {
+		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
+		return 1;
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "PRESENT", "%s", DEVPRESENT[device->present])) < 0) {
+		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+		return 1;                           
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ADDRESS", "%s", device->address)) < 0) {
+		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
+		return 1;                           
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "SUBDEVICE", "%d", device->subdevice)) < 0) {
+		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
+		return 1;                           
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "GPIOPIN", "%d", device->gpiopin)) < 0) {
+		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
+		return 1;                           
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "DESCRIPTION", "%s", device->description)) < 0) {
+		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
+		return 1;                           
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "INUSE", "%d", device->inuse)) < 0) {
+		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
+		return 1;                           
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "COMMENT", "%s", device->comment)) < 0) {
+		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
+		return 1;                           
+	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "TIMESTAMP", "%d", (int)device->timestamp)) < 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
      */
@@ -1000,6 +1077,137 @@
 
 
 
+int parseDevice(xmlDocPtr doc, xmlNodePtr cur)
+{
+    xmlChar             *key;
+    devices_list	*device, *tmp;
+    int                 i, ival;
+
+    device = (devices_list *)malloc(sizeof(devices_list));
+    device->next = NULL;
+    device->version = 1;
+    device->uuid = device->address = device->description = device->comment = NULL;
+    device->type = device->direction = device->present = device->subdevice = device->inuse = 0;
+    device->gpiopin = -1;
+    device->timestamp = (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;
+	    }
+	    device->version = 1;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"UUID"))) {
+	    device->uuid = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"TYPE"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    for (i = 0; i < 7; i++) {
+		if (! xmlStrcmp(key, (const xmlChar *)DEVTYPE[i])) {
+		    device->type = i;
+		    break;
+		}
+	    }
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"DIRECTION"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    for (i = 0; i < 6; i++) {
+		if (! xmlStrcmp(key, (const xmlChar *)DEVDIR[i])) {
+		    device->direction = i;
+		    break;
+		}
+	    }
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"VALUE"))) {
+		key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+		if (sscanf((const char *)key, "%d", &ival) == 1)
+			device->value = ival;
+		xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"PRESENT"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    for (i = 0; i < 4; i++) {
+		if (! xmlStrcmp(key, (const xmlChar *)DEVDIR[i])) {
+		    device->present = i;
+		    break;
+		}
+	    }
+	    xmlFree(key);                           
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"ADDRESS"))) {
+	    device->address = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"SUBDEVICE"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%d", &ival) == 1)
+		device->subdevice = ival;
+	    xmlFree(key);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"GPIOPIN"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%d", &ival) == 1)
+		device->gpiopin = ival;
+	    xmlFree(key);                                       
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"DESCRPTION"))) {
+	    device->description = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"INUSE"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%d", &ival) == 1)
+		device->inuse = ival;
+	    xmlFree(key);                                       
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"COMMENT"))) {
+	    device->comment = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"TIMESTAMP"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%d", &ival) == 1)
+		device->timestamp = (time_t)ival;
+	    xmlFree(key);                                   
+	}
+
+	cur = cur->next;
+    }
+
+    if (Config.devices == NULL) {
+	Config.devices = device;
+    } else {
+	for (tmp = Config.devices; tmp; tmp = tmp->next) {
+	    if (tmp->next == NULL) {
+		tmp->next = device;
+		break;
+	    }
+	}
+    }
+
+    return 0;
+}
+
+
+
+int parseDevices(xmlDocPtr doc, xmlNodePtr cur)
+{
+    cur = cur->xmlChildrenNode;
+    while (cur != NULL) {
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"DEVICE"))) {
+	    parseDevice(doc, cur);
+	}
+	cur = cur->next;
+    }
+    return 0;
+}
+
+
+
 int rdconfig(void) 
 {
     int		rc = 0, ival;
@@ -1094,6 +1302,9 @@
 	if ((!xmlStrcmp(cur->name, (const xmlChar *)"PROFILES"))) {
 	    parseProfiles(doc, cur);
 	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"DEVICES"))) {
+	    parseDevices(doc, cur);
+	}
 	cur = cur->next;
     }
     xmlFreeDoc(doc);

mercurial