brewco/rdconfig.c

changeset 438
7d1ec160d751
parent 435
4b1ed6897d80
child 441
bde74a8f2ad7
equal deleted inserted replaced
437:5664743eaf2f 438:7d1ec160d751
40 40
41 void killconfig(void) 41 void killconfig(void)
42 { 42 {
43 units_list *unit; 43 units_list *unit;
44 devices_list *device; 44 devices_list *device;
45 #ifdef USE_SIMULATOR
46 simulator_list *simulator;
47 #endif
48 45
49 if (Config.name) 46 if (Config.name)
50 free(Config.name); 47 free(Config.name);
51 Config.name = NULL; 48 Config.name = NULL;
52 49
93 free(device); 90 free(device);
94 } 91 }
95 Config.devices = NULL; 92 Config.devices = NULL;
96 93
97 #ifdef USE_SIMULATOR 94 #ifdef USE_SIMULATOR
98 for (simulator = Config.simulators; simulator; simulator = simulator->next) { 95 if (Config.simulator)
99 if (simulator->uuid) 96 free(Config.simulator);
100 free(simulator->uuid); 97 Config.simulator = NULL;
101 if (simulator->name)
102 free(simulator->name);
103 free(simulator);
104 }
105 Config.simulators = NULL;
106 #endif 98 #endif
107 } 99 }
108 100
109 101
110 102
116 char *mypath = NULL; 108 char *mypath = NULL;
117 xmlTextWriterPtr writer; 109 xmlTextWriterPtr writer;
118 xmlBufferPtr buf; 110 xmlBufferPtr buf;
119 units_list *unit; 111 units_list *unit;
120 devices_list *device; 112 devices_list *device;
121 #ifdef USE_SIMULATOR
122 simulator_list *simulator;
123 #endif
124 113
125 /* 114 /*
126 * Create a new XML buffer, to which the XML document will be written 115 * Create a new XML buffer, to which the XML document will be written
127 */ 116 */
128 if ((buf = xmlBufferCreate()) == NULL) { 117 if ((buf = xmlBufferCreate()) == NULL) {
600 return 1; 589 return 1;
601 } 590 }
602 } 591 }
603 592
604 #ifdef USE_SIMULATOR 593 #ifdef USE_SIMULATOR
605 if (Config.simulators) { 594 if (Config.simulator) {
606 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "SIMULATORS")) < 0) { 595 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "SIMULATOR")) < 0) {
607 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement"); 596 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement");
597 return 1;
598 }
599 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ROOM_TEMPERATURE", "%f", Config.simulator->room_temperature)) < 0) {
600 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
608 return 1; 601 return 1;
609 } 602 }
610 for (simulator = Config.simulators; simulator; simulator = simulator->next) { 603 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HLT_TEMPERATURE", "%f", Config.simulator->hlt_temperature)) < 0) {
611 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "SIMULATOR")) < 0) { 604 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
612 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement"); 605 return 1;
613 return 1; 606 }
614 } 607 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HLT_HEATER_TEMP", "%f", Config.simulator->hlt_heater_temp)) < 0) {
615 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "VERSION", "%d", simulator->version)) < 0) { 608 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
616 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement"); 609 return 1;
617 return 1; 610 }
618 } 611 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HLT_HEATER_TIME", "%d", Config.simulator->hlt_heater_time)) < 0) {
619 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID", "%s", simulator->uuid)) < 0) { 612 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
620 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); 613 return 1;
621 return 1; 614 }
622 } 615 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HLT_HEATER_SIZE", "%f", Config.simulator->hlt_heater_size)) < 0) {
623 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "NAME", "%s", simulator->name)) < 0) { 616 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
624 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); 617 return 1;
625 return 1; 618 }
626 } 619 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HLT_HEATER_STATE", "%d", Config.simulator->hlt_heater_state)) < 0) {
627 620 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
628 if ((rc = xmlTextWriterEndElement(writer)) < 0) { 621 return 1;
629 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement"); 622 }
630 return 1; 623
631 } 624 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_TEMPERATURE", "%f", Config.simulator->mlt_temperature)) < 0) {
632 } 625 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
626 return 1;
627 }
628 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_HEATER_TEMP", "%f", Config.simulator->mlt_heater_temp)) < 0) {
629 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
630 return 1;
631 }
632 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_HEATER_TIME", "%d", Config.simulator->mlt_heater_time)) < 0) {
633 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
634 return 1;
635 }
636 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_HEATER_SIZE", "%f", Config.simulator->mlt_heater_size)) < 0) {
637 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
638 return 1;
639 }
640 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_HEATER_STATE", "%d", Config.simulator->mlt_heater_state)) < 0) {
641 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
642 return 1;
643 }
644
645 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "S_HLT_TEMP", "%f", Config.simulator->s_hlt_temp)) < 0) {
646 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
647 return 1;
648 }
649 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "S_MLT_TEMP", "%f", Config.simulator->s_mlt_temp)) < 0) {
650 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement");
651 return 1;
652 }
653
633 if ((rc = xmlTextWriterEndElement(writer)) < 0) { 654 if ((rc = xmlTextWriterEndElement(writer)) < 0) {
634 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement"); 655 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement");
635 return 1; 656 return 1;
636 } 657 }
637 } 658 }
1297 1318
1298 #ifdef USE_SIMULATOR 1319 #ifdef USE_SIMULATOR
1299 int parseSimulator(xmlDocPtr doc, xmlNodePtr cur) 1320 int parseSimulator(xmlDocPtr doc, xmlNodePtr cur)
1300 { 1321 {
1301 xmlChar *key; 1322 xmlChar *key;
1302 simulator_list *simulator, *tmp;
1303 int ival; 1323 int ival;
1304 float fval; 1324 float fval;
1305 1325
1306 simulator = (simulator_list *)malloc(sizeof(simulator_list)); 1326 /*
1307 simulator->next = NULL; 1327 * First time, allocate memory and set defaults.
1308 simulator->version = 1; 1328 */
1309 simulator->uuid = simulator->name = NULL; 1329 if (! Config.simulator) {
1310 simulator->room_temperature = simulator->hlt_heater_temp = simulator->mlt_heater_temp = simulator->s_hlt_temp = simulator->s_mlt_temp = 20.0; 1330 Config.simulator = (simulator_var *)malloc(sizeof(simulator_var));
1311 simulator->hlt_heater_temp = simulator->hlt_heater_size = simulator->mlt_heater_temp = simulator->mlt_heater_size = 0.0; 1331 Config.simulator->room_temperature = Config.simulator->hlt_temperature = Config.simulator->hlt_heater_temp = \
1312 simulator->hlt_heater_time = simulator->mlt_heater_time = simulator->hlt_heater_state = simulator->mlt_heater_state = 0; 1332 Config.simulator->mlt_temperature = Config.simulator->mlt_heater_temp = \
1313 simulator->s_hlt_temp = simulator->s_mlt_temp = 0.0; 1333 Config.simulator->s_hlt_temp = Config.simulator->s_mlt_temp = 0.0;
1334 Config.simulator->hlt_heater_size = Config.simulator->mlt_heater_size = 0.0;
1335 Config.simulator->hlt_heater_time = Config.simulator->mlt_heater_time = Config.simulator->hlt_heater_state = Config.simulator->mlt_heater_state = 0;
1336 }
1314 1337
1315 cur = cur->xmlChildrenNode; 1338 cur = cur->xmlChildrenNode;
1316 while (cur != NULL) { 1339 while (cur != NULL) {
1317 if ((!xmlStrcmp(cur->name, (const xmlChar *)"VERSION"))) {
1318 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1319 if (xmlStrcmp(key, (const xmlChar *)"1")) {
1320 xmlFree(key);
1321 return 1;
1322 }
1323 simulator->version = 1;
1324 xmlFree(key);
1325 }
1326 if ((!xmlStrcmp(cur->name, (const xmlChar *)"UUID"))) {
1327 simulator->uuid = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1328 }
1329 if ((!xmlStrcmp(cur->name, (const xmlChar *)"NAME"))) {
1330 simulator->name = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1331 }
1332 if ((!xmlStrcmp(cur->name, (const xmlChar *)"ROOM_TEMPERATURE"))) { 1340 if ((!xmlStrcmp(cur->name, (const xmlChar *)"ROOM_TEMPERATURE"))) {
1333 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 1341 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1334 if (sscanf((const char *)key, "%f", &fval) == 1) 1342 if (sscanf((const char *)key, "%f", &fval) == 1)
1335 simulator->room_temperature = fval; 1343 Config.simulator->room_temperature = fval;
1336 xmlFree(key); 1344 xmlFree(key);
1337 } 1345 }
1338 if ((!xmlStrcmp(cur->name, (const xmlChar *)"HLT_TEMPERATURE"))) { 1346 if ((!xmlStrcmp(cur->name, (const xmlChar *)"HLT_TEMPERATURE"))) {
1339 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 1347 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1340 if (sscanf((const char *)key, "%f", &fval) == 1) 1348 if (sscanf((const char *)key, "%f", &fval) == 1)
1341 simulator->hlt_temperature = fval; 1349 Config.simulator->hlt_temperature = fval;
1342 xmlFree(key); 1350 xmlFree(key);
1343 } 1351 }
1344 if ((!xmlStrcmp(cur->name, (const xmlChar *)"HLT_HEATER_TEMP"))) { 1352 if ((!xmlStrcmp(cur->name, (const xmlChar *)"HLT_HEATER_TEMP"))) {
1345 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 1353 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1346 if (sscanf((const char *)key, "%f", &fval) == 1) 1354 if (sscanf((const char *)key, "%f", &fval) == 1)
1347 simulator->hlt_heater_temp = fval; 1355 Config.simulator->hlt_heater_temp = fval;
1348 xmlFree(key); 1356 xmlFree(key);
1349 } 1357 }
1350 if ((!xmlStrcmp(cur->name, (const xmlChar *)"HLT_HEATER_TIME"))) { 1358 if ((!xmlStrcmp(cur->name, (const xmlChar *)"HLT_HEATER_TIME"))) {
1351 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 1359 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1352 if (sscanf((const char *)key, "%d", &ival) == 1) 1360 if (sscanf((const char *)key, "%d", &ival) == 1)
1353 simulator->hlt_heater_time = ival; 1361 Config.simulator->hlt_heater_time = ival;
1354 xmlFree(key); 1362 xmlFree(key);
1355 } 1363 }
1356 if ((!xmlStrcmp(cur->name, (const xmlChar *)"HLT_HEATER_SIZE"))) { 1364 if ((!xmlStrcmp(cur->name, (const xmlChar *)"HLT_HEATER_SIZE"))) {
1357 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 1365 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1358 if (sscanf((const char *)key, "%f", &fval) == 1) 1366 if (sscanf((const char *)key, "%f", &fval) == 1)
1359 simulator->hlt_heater_size = fval; 1367 Config.simulator->hlt_heater_size = fval;
1360 xmlFree(key); 1368 xmlFree(key);
1361 } 1369 }
1362 if ((!xmlStrcmp(cur->name, (const xmlChar *)"HLT_HEATER_STATE"))) { 1370 if ((!xmlStrcmp(cur->name, (const xmlChar *)"HLT_HEATER_STATE"))) {
1363 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 1371 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1364 if (sscanf((const char *)key, "%d", &ival) == 1) 1372 if (sscanf((const char *)key, "%d", &ival) == 1)
1365 simulator->hlt_heater_state = ival; 1373 Config.simulator->hlt_heater_state = ival;
1366 xmlFree(key); 1374 xmlFree(key);
1367 } 1375 }
1368 if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_TEMPERATURE"))) { 1376 if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_TEMPERATURE"))) {
1369 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 1377 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1370 if (sscanf((const char *)key, "%f", &fval) == 1) 1378 if (sscanf((const char *)key, "%f", &fval) == 1)
1371 simulator->mlt_temperature = fval; 1379 Config.simulator->mlt_temperature = fval;
1372 xmlFree(key); 1380 xmlFree(key);
1373 } 1381 }
1374 if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_HEATER_TEMP"))) { 1382 if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_HEATER_TEMP"))) {
1375 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 1383 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1376 if (sscanf((const char *)key, "%f", &fval) == 1) 1384 if (sscanf((const char *)key, "%f", &fval) == 1)
1377 simulator->mlt_heater_temp = fval; 1385 Config.simulator->mlt_heater_temp = fval;
1378 xmlFree(key); 1386 xmlFree(key);
1379 } 1387 }
1380 if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_HEATER_TIME"))) { 1388 if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_HEATER_TIME"))) {
1381 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 1389 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1382 if (sscanf((const char *)key, "%d", &ival) == 1) 1390 if (sscanf((const char *)key, "%d", &ival) == 1)
1383 simulator->mlt_heater_time = ival; 1391 Config.simulator->mlt_heater_time = ival;
1384 xmlFree(key); 1392 xmlFree(key);
1385 } 1393 }
1386 if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_HEATER_SIZE"))) { 1394 if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_HEATER_SIZE"))) {
1387 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 1395 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1388 if (sscanf((const char *)key, "%f", &fval) == 1) 1396 if (sscanf((const char *)key, "%f", &fval) == 1)
1389 simulator->mlt_heater_size = fval; 1397 Config.simulator->mlt_heater_size = fval;
1390 xmlFree(key); 1398 xmlFree(key);
1391 } 1399 }
1392 if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_HEATER_STATE"))) { 1400 if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_HEATER_STATE"))) {
1393 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 1401 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1394 if (sscanf((const char *)key, "%d", &ival) == 1) 1402 if (sscanf((const char *)key, "%d", &ival) == 1)
1395 simulator->mlt_heater_state = ival; 1403 Config.simulator->mlt_heater_state = ival;
1396 xmlFree(key); 1404 xmlFree(key);
1397 } 1405 }
1398 if ((!xmlStrcmp(cur->name, (const xmlChar *)"S_HLT_TEMP"))) { 1406 if ((!xmlStrcmp(cur->name, (const xmlChar *)"S_HLT_TEMP"))) {
1399 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 1407 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1400 if (sscanf((const char *)key, "%f", &fval) == 1) 1408 if (sscanf((const char *)key, "%f", &fval) == 1)
1401 simulator->s_hlt_temp = fval; 1409 Config.simulator->s_hlt_temp = fval;
1402 xmlFree(key); 1410 xmlFree(key);
1403 } 1411 }
1404 if ((!xmlStrcmp(cur->name, (const xmlChar *)"S_MLT_TEMP"))) { 1412 if ((!xmlStrcmp(cur->name, (const xmlChar *)"S_MLT_TEMP"))) {
1405 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 1413 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1406 if (sscanf((const char *)key, "%f", &fval) == 1) 1414 if (sscanf((const char *)key, "%f", &fval) == 1)
1407 simulator->s_mlt_temp = fval; 1415 Config.simulator->s_mlt_temp = fval;
1408 xmlFree(key); 1416 xmlFree(key);
1409 } 1417 }
1410 cur = cur->next; 1418 cur = cur->next;
1411 } 1419 }
1412 1420
1413 if (Config.simulators == NULL) {
1414 Config.simulators = simulator;
1415 } else {
1416 for (tmp = Config.simulators; tmp; tmp = tmp->next) {
1417 if (tmp->next == NULL) {
1418 tmp->next = simulator;
1419 break;
1420 }
1421 }
1422 }
1423
1424 return 0;
1425 }
1426
1427
1428
1429 int parseSimulators(xmlDocPtr doc, xmlNodePtr cur)
1430 {
1431 cur = cur->xmlChildrenNode;
1432 while (cur != NULL) {
1433 if ((!xmlStrcmp(cur->name, (const xmlChar *)"SIMULATOR"))) {
1434 parseSimulator(doc, cur);
1435 }
1436 cur = cur->next;
1437 }
1438 return 0; 1421 return 0;
1439 } 1422 }
1440 #endif 1423 #endif
1441 1424
1442 1425
1534 } 1517 }
1535 if ((!xmlStrcmp(cur->name, (const xmlChar *)"DEVICES"))) { 1518 if ((!xmlStrcmp(cur->name, (const xmlChar *)"DEVICES"))) {
1536 parseDevices(doc, cur); 1519 parseDevices(doc, cur);
1537 } 1520 }
1538 #ifdef USE_SIMULATOR 1521 #ifdef USE_SIMULATOR
1539 if ((!xmlStrcmp(cur->name, (const xmlChar *)"SIMULATORS"))) { 1522 if ((!xmlStrcmp(cur->name, (const xmlChar *)"SIMULATOR"))) {
1540 parseSimulators(doc, cur); 1523 parseSimulator(doc, cur);
1541 } 1524 }
1542 #endif 1525 #endif
1543 cur = cur->next; 1526 cur = cur->next;
1544 } 1527 }
1545 xmlFreeDoc(doc); 1528 xmlFreeDoc(doc);
1546 1529
1547 free(mypath); 1530 free(mypath);
1548 mypath = NULL; 1531 mypath = NULL;
1549 1532
1533 #ifdef USE_SIMULATOR
1534 /*
1535 * If we didn't find any simulator values, initialize a new simulator.
1536 */
1537 if (! Config.simulator) {
1538 Config.simulator = (simulator_var *)malloc(sizeof(simulator_var));
1539 syslog(LOG_NOTICE, "rdconfig() init a new simulator");
1540 Config.simulator->room_temperature = Config.simulator->hlt_temperature = Config.simulator->hlt_heater_temp = \
1541 Config.simulator->mlt_temperature = Config.simulator->mlt_heater_temp = \
1542 Config.simulator->s_hlt_temp = Config.simulator->s_mlt_temp = 20.0;
1543 Config.simulator->hlt_heater_size = Config.simulator->mlt_heater_size = 0.0;
1544 Config.simulator->hlt_heater_time = Config.simulator->mlt_heater_time = Config.simulator->hlt_heater_state = Config.simulator->mlt_heater_state = 0;
1545 }
1546 #endif
1547
1550 return rc; 1548 return rc;
1551 } 1549 }
1552 1550
1553 1551
1554 1552

mercurial