diff -r ca18ff45deba -r 78fb6f99e473 thermferm/server.c --- a/thermferm/server.c Sun Aug 10 13:26:35 2014 +0200 +++ b/thermferm/server.c Sun Aug 10 14:45:11 2014 +0200 @@ -30,7 +30,6 @@ extern int my_shutdown; extern int debug; -extern char *current_unit; #ifdef HAVE_WIRINGPI_H extern int lcdHandle; extern unsigned char lcdbuf[MAX_LCDS][20][4]; @@ -429,6 +428,136 @@ /* + * GLOBAL GET + * GLOBAL PUT + */ +int cmd_global(char *buf) +{ + char *opt, *kwd, *val, ibuf[SS_BUFSIZE]; + int ival, i, rlen; + socklen_t fromlen; + + opt = strtok(buf, " \0"); + opt = strtok(NULL, "\0"); + + if (opt == NULL) { + srv_send((char *)"502 Missing command option"); + return 1; + } + + if (strcmp(opt, (char *)"GET") == 0) { + srv_send((char *)"213 Global Settings record follows:"); + srv_send((char *)"NAME,%s", Config.name); + srv_send((char *)"PORT,%d", Config.my_port); + srv_send((char *)"TEMPFORMAT,%c", Config.tempFormat); + srv_send((char *)"TEMP_ADDRESS,%s", Config.temp_address); + srv_send((char *)"TEMP_STATE,%s", TEMPSTATE[Config.temp_state]); + srv_send((char *)"TEMP_VALUE,%.3f", Config.temp_value / 1000.0); + srv_send((char *)"HUM_ADDRESS,%s", Config.hum_address); + srv_send((char *)"HUM_STATE,%s", TEMPSTATE[Config.hum_state]); + srv_send((char *)"HUM_VALUE,%.3f", Config.hum_value / 1000.0); +#ifdef HAVE_WIRINGPI_H + srv_send((char *)"LCD_COLS,%d", Config.lcd_cols); + srv_send((char *)"LCD_ROWS,%d", Config.lcd_rows); +#endif + srv_send((char *)"."); + return 1; + } + + if (strcmp(opt, (char *)"PUT") == 0) { + while (1) { + memset((char *)&ibuf, 0, SS_BUFSIZE); + fromlen = sizeof(peeraddr_in); + rlen = recvfrom(s, ibuf, sizeof(ibuf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen); + if (rlen == -1) { + syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno)); + srv_send((char *)"518 recfrom(): %s", strerror(errno)); + return 1; + } + for (i = 0; i < strlen(ibuf); i++) { + if (ibuf[i] == '\n') + ibuf[i] = '\0'; + if (ibuf[i] == '\r') + ibuf[i] = '\0'; + } + for (i = strlen(ibuf) -1; i > 0; i--) { + if (ibuf[i] == ' ') + ibuf[i] = '\0'; + else + break; + } + if (strlen(ibuf)) { + if (debug) { + syslog(LOG_NOTICE, "recv: \"%s\"", ibuf); + fprintf(stdout, "recv: \"%s\"\n", ibuf); + } + if (strcmp(ibuf, (char *)".") == 0) { + srv_send((char *)"219 Accepted Global record"); + return 0; + } + kwd = strtok(ibuf, ",\0"); + val = strtok(NULL, "\0"); + if (kwd) { + if (strcmp(kwd, (char *)"NAME") == 0) { + if (Config.name) + free(Config.name); + if (val) + Config.name = xstrcpy(val); + else + Config.name = NULL; + + } else if (val && (strcmp(kwd, (char *)"PORT") == 0)) { + if (sscanf(val, "%d", &ival) == 1) + Config.my_port = ival; + + } else if (val && (strcmp(kwd, (char *)"TEMPFORMAT") == 0)) { + if ((val[0] == 'C') || (val[0] == 'F')) + Config.tempFormat = val[0]; + + } else if (strcmp(kwd, (char *)"TEMP_ADDRESS") == 0) { + if (Config.temp_address) { + device_count(FALSE, Config.temp_address); + free(Config.temp_address); + } + if (val) { + Config.temp_address = xstrcpy(val); + device_count(TRUE, Config.temp_address); + } else + Config.temp_address = NULL; + + } else if (strcmp(kwd, (char *)"HUM_ADDRESS") == 0) { + if (Config.hum_address) { + device_count(FALSE, Config.hum_address); + free(Config.hum_address); + } + if (val) { + Config.hum_address = xstrcpy(val); + device_count(TRUE, Config.hum_address); + } else + Config.hum_address = NULL; + +#ifdef HAVE_WIRINGPI_H + } else if (val && (strcmp(kwd, (char *)"LCD_COLS") == 0)) { + if (sscanf(val, "%d", &ival) == 1) + Config.lcd_cols = ival; + + } else if (val && (strcmp(kwd, (char *)"LCD_ROWS") == 0)) { + if (sscanf(val, "%d", &ival) == 1) + Config.lcd_rows = ival; +#endif + } + } + } + } + } + + srv_send((char *)"502 Unknown command option"); + return 1; +} + + + +/* * LIST * LIST LOG */ @@ -829,7 +958,6 @@ /* - * UNIT uuid * UNIT ADD name * UNIT DEL uuid * UNIT LIST @@ -854,26 +982,6 @@ } param = strtok(NULL, "\0"); - /* - * UNIT uuid - */ - if ((strlen(opt) == 36) && (param == NULL)) { - /* - * Search using uuid - */ - for (unit = Config.units; unit; unit = unit->next) { - if (strcmp(opt, unit->uuid) == 0) { - srv_send((char *)"210 Unit %s selected", unit->uuid); - if (current_unit) - free(current_unit); - current_unit = xstrcpy(unit->uuid);; - return 1; - } - } - srv_send((char *)"410 No such unit"); - return 1; - } - if ((strcmp(opt, (char *)"LIST") == 0) && (param == NULL)) { srv_send((char *)"212 Fermenter Units list follows:"); for (unit = Config.units; unit; unit = unit->next) { @@ -895,9 +1003,6 @@ unit->uuid = malloc(37); uuid_generate(uu); uuid_unparse(uu, unit->uuid); - if (current_unit) - free(current_unit); - current_unit = xstrcpy(unit->uuid); unit->name = xstrcpy(param); unit->air_address = unit->beer_address = unit->heater_address = unit->cooler_address = \ unit->fan_address = unit->door_address = unit->profile = NULL; @@ -1238,6 +1343,9 @@ if (strncmp(buf, "DEVICE", 6) == 0) { if (cmd_device(buf) == 0) wrconfig(); + } else if (strncmp(buf, "GLOBAL", 6) == 0) { + if (cmd_global(buf) == 0) + wrconfig(); } else if (strncmp(buf, "HELP", 4) == 0) { srv_send((char *)"100 Help text follows"); srv_send((char *)"Recognized commands:"); @@ -1248,6 +1356,8 @@ srv_send((char *)"DEVICE LIST List Devices"); srv_send((char *)"DEVICE GET uuid Get Device record by uuid"); srv_send((char *)"DEVICE PUT uuid Put Device record by uuid"); + srv_send((char *)"GLOBAL GET Get global settings"); + srv_send((char *)"GLOBAL PUT Put global settings"); srv_send((char *)"LCD Get LCD screen (allways 4 rows of 20 characters)"); srv_send((char *)"LIST List all fermenter units"); srv_send((char *)"LIST LOG uuid List logfile data in 1 hour lines"); @@ -1259,10 +1369,9 @@ srv_send((char *)"PROFILE PUT uuid Put Profile record by uuid"); srv_send((char *)"PROFILE GETS uuid Profile get steps list"); srv_send((char *)"PROFILE PUTS uuid Profile put steps list"); - srv_send((char *)"UNIT uuid Select unit by uuid"); srv_send((char *)"UNIT ADD name Add a new unit with name"); srv_send((char *)"UNIT DEL uuid Delete Unit by uuid"); - srv_send((char *)"UNIT LIST List Units"); + srv_send((char *)"UNIT LIST List all Units"); srv_send((char *)"UNIT GET uuid Get Unit record by uuid"); srv_send((char *)"UNIT PUT uuid Put Unit record by uuid"); srv_send((char *)".");