--- a/thermferm/server.c Wed Jun 25 19:42:03 2014 +0200 +++ b/thermferm/server.c Thu Jun 26 20:00:00 2014 +0200 @@ -70,44 +70,6 @@ -int unit_add(char *name) -{ - units_list *unit, *tmp; - uuid_t uu; - - unit = (units_list *)malloc(sizeof(units_list)); - unit->next = NULL; - unit->uuid = malloc(37); - uuid_generate(uu); - uuid_unparse(uu, unit->uuid); - unit->name = xstrcpy(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_temp = unit->beer_temp = unit->beer_set = unit->fridge_set = 20.0; - unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->mode = 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; - - - 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; -} - - - /* * Send message to client */ @@ -124,7 +86,7 @@ va_end(va_ptr); if (debug) { - syslog(LOG_NOTICE, "send: \"%s\"", out); + syslog(LOG_NOTICE, "send: \"%s\"", out); fprintf(stdout, "send: \"%s\"\n", out); } @@ -132,7 +94,7 @@ syslog(LOG_NOTICE, "srv_send failed"); return -1; } - + if (send(s, (char *)"\r\n", 2, 0) != 2) { syslog(LOG_NOTICE, "srv_send failed"); return -1; @@ -143,11 +105,56 @@ -void cmd_die(int onsig) +int unit_add(char *buf) { - syslog(LOG_NOTICE, "Server process die on signal %d", onsig); - close(s); - exit(0); + units_list *unit, *tmp; + uuid_t uu; + char *cmd, *opt; + int id = 1; + + cmd = strtok(buf, " \0"); + opt = strtok(NULL, "\0"); + if (opt == NULL) { + srv_send((char *)"501 Parameter missing"); + return 1; + } + + if (debug) + fprintf(stdout, "cmd: '%s' opt: '%s'\n", MBSE_SS(cmd), MBSE_SS(opt)); + + unit = (units_list *)malloc(sizeof(units_list)); + unit->next = NULL; + unit->uuid = malloc(37); + uuid_generate(uu); + uuid_unparse(uu, unit->uuid); + unit->name = xstrcpy(opt); + 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_temp = unit->beer_temp = unit->beer_set = unit->fridge_set = 20.0; + unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->mode = 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; + + if (Config.units == NULL) { + Config.units = unit; + } else { + for (tmp = Config.units; tmp; tmp = tmp->next) { + id++; + if (tmp->next == NULL) { + tmp->next = unit; + break; + } + } + } + + syslog(LOG_NOTICE, "Unit %d with uuid %s added", id, unit->uuid); + srv_send((char *)"211 Unit %d with uuid %s added", id, unit->uuid); + current_unit = id; + return 0; } @@ -180,6 +187,12 @@ if (buf[i] == '\r') buf[i] = '\0'; } + for (i = strlen(buf) -1; i > 0; i--) { + if (buf[i] == ' ') + buf[i] = '\0'; + else + break; + } if (strlen(buf)) { if (debug) { syslog(LOG_NOTICE, "recv: \"%s\"", buf); @@ -189,10 +202,13 @@ /* * Process commands from the client */ - if (strncmp(buf, "HELP", 4) == 0) { + if (strncmp(buf, "ADD", 3) == 0) { + unit_add(buf); + } else if (strncmp(buf, "HELP", 4) == 0) { srv_send((char *)"100 Help text follows"); srv_send((char *)"Recognized commands:"); srv_send((char *)""); + srv_send((char *)"ADD name Add a new unit with \"name\""); srv_send((char *)"LCD Get LCD screen (allways 4 rows of 20 characters)"); srv_send((char *)"."); } else if (strncmp(buf, "LCD", 3) == 0) {