thermferm/server.c

changeset 206
78fb6f99e473
parent 195
b34a1b2421fb
child 208
934d45d9751d
equal deleted inserted replaced
205:ca18ff45deba 206:78fb6f99e473
28 #include "xutil.h" 28 #include "xutil.h"
29 29
30 30
31 extern int my_shutdown; 31 extern int my_shutdown;
32 extern int debug; 32 extern int debug;
33 extern char *current_unit;
34 #ifdef HAVE_WIRINGPI_H 33 #ifdef HAVE_WIRINGPI_H
35 extern int lcdHandle; 34 extern int lcdHandle;
36 extern unsigned char lcdbuf[MAX_LCDS][20][4]; 35 extern unsigned char lcdbuf[MAX_LCDS][20][4];
37 #endif 36 #endif
38 extern sys_config Config; 37 extern sys_config Config;
418 } 417 }
419 } 418 }
420 } 419 }
421 srv_send((char *)"440 No such device"); 420 srv_send((char *)"440 No such device");
422 return 1; 421 return 1;
422 }
423
424 srv_send((char *)"502 Unknown command option");
425 return 1;
426 }
427
428
429
430 /*
431 * GLOBAL GET
432 * GLOBAL PUT
433 */
434 int cmd_global(char *buf)
435 {
436 char *opt, *kwd, *val, ibuf[SS_BUFSIZE];
437 int ival, i, rlen;
438 socklen_t fromlen;
439
440 opt = strtok(buf, " \0");
441 opt = strtok(NULL, "\0");
442
443 if (opt == NULL) {
444 srv_send((char *)"502 Missing command option");
445 return 1;
446 }
447
448 if (strcmp(opt, (char *)"GET") == 0) {
449 srv_send((char *)"213 Global Settings record follows:");
450 srv_send((char *)"NAME,%s", Config.name);
451 srv_send((char *)"PORT,%d", Config.my_port);
452 srv_send((char *)"TEMPFORMAT,%c", Config.tempFormat);
453 srv_send((char *)"TEMP_ADDRESS,%s", Config.temp_address);
454 srv_send((char *)"TEMP_STATE,%s", TEMPSTATE[Config.temp_state]);
455 srv_send((char *)"TEMP_VALUE,%.3f", Config.temp_value / 1000.0);
456 srv_send((char *)"HUM_ADDRESS,%s", Config.hum_address);
457 srv_send((char *)"HUM_STATE,%s", TEMPSTATE[Config.hum_state]);
458 srv_send((char *)"HUM_VALUE,%.3f", Config.hum_value / 1000.0);
459 #ifdef HAVE_WIRINGPI_H
460 srv_send((char *)"LCD_COLS,%d", Config.lcd_cols);
461 srv_send((char *)"LCD_ROWS,%d", Config.lcd_rows);
462 #endif
463 srv_send((char *)".");
464 return 1;
465 }
466
467 if (strcmp(opt, (char *)"PUT") == 0) {
468 while (1) {
469 memset((char *)&ibuf, 0, SS_BUFSIZE);
470 fromlen = sizeof(peeraddr_in);
471 rlen = recvfrom(s, ibuf, sizeof(ibuf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen);
472 if (rlen == -1) {
473 syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno));
474 srv_send((char *)"518 recfrom(): %s", strerror(errno));
475 return 1;
476 }
477 for (i = 0; i < strlen(ibuf); i++) {
478 if (ibuf[i] == '\n')
479 ibuf[i] = '\0';
480 if (ibuf[i] == '\r')
481 ibuf[i] = '\0';
482 }
483 for (i = strlen(ibuf) -1; i > 0; i--) {
484 if (ibuf[i] == ' ')
485 ibuf[i] = '\0';
486 else
487 break;
488 }
489 if (strlen(ibuf)) {
490 if (debug) {
491 syslog(LOG_NOTICE, "recv: \"%s\"", ibuf);
492 fprintf(stdout, "recv: \"%s\"\n", ibuf);
493 }
494 if (strcmp(ibuf, (char *)".") == 0) {
495 srv_send((char *)"219 Accepted Global record");
496 return 0;
497 }
498 kwd = strtok(ibuf, ",\0");
499 val = strtok(NULL, "\0");
500 if (kwd) {
501 if (strcmp(kwd, (char *)"NAME") == 0) {
502 if (Config.name)
503 free(Config.name);
504 if (val)
505 Config.name = xstrcpy(val);
506 else
507 Config.name = NULL;
508
509 } else if (val && (strcmp(kwd, (char *)"PORT") == 0)) {
510 if (sscanf(val, "%d", &ival) == 1)
511 Config.my_port = ival;
512
513 } else if (val && (strcmp(kwd, (char *)"TEMPFORMAT") == 0)) {
514 if ((val[0] == 'C') || (val[0] == 'F'))
515 Config.tempFormat = val[0];
516
517 } else if (strcmp(kwd, (char *)"TEMP_ADDRESS") == 0) {
518 if (Config.temp_address) {
519 device_count(FALSE, Config.temp_address);
520 free(Config.temp_address);
521 }
522 if (val) {
523 Config.temp_address = xstrcpy(val);
524 device_count(TRUE, Config.temp_address);
525 } else
526 Config.temp_address = NULL;
527
528 } else if (strcmp(kwd, (char *)"HUM_ADDRESS") == 0) {
529 if (Config.hum_address) {
530 device_count(FALSE, Config.hum_address);
531 free(Config.hum_address);
532 }
533 if (val) {
534 Config.hum_address = xstrcpy(val);
535 device_count(TRUE, Config.hum_address);
536 } else
537 Config.hum_address = NULL;
538
539 #ifdef HAVE_WIRINGPI_H
540 } else if (val && (strcmp(kwd, (char *)"LCD_COLS") == 0)) {
541 if (sscanf(val, "%d", &ival) == 1)
542 Config.lcd_cols = ival;
543
544 } else if (val && (strcmp(kwd, (char *)"LCD_ROWS") == 0)) {
545 if (sscanf(val, "%d", &ival) == 1)
546 Config.lcd_rows = ival;
547 #endif
548 }
549 }
550 }
551 }
423 } 552 }
424 553
425 srv_send((char *)"502 Unknown command option"); 554 srv_send((char *)"502 Unknown command option");
426 return 1; 555 return 1;
427 } 556 }
827 } 956 }
828 957
829 958
830 959
831 /* 960 /*
832 * UNIT uuid
833 * UNIT ADD name 961 * UNIT ADD name
834 * UNIT DEL uuid 962 * UNIT DEL uuid
835 * UNIT LIST 963 * UNIT LIST
836 * UNIT GET uuid 964 * UNIT GET uuid
837 * UNIT PUT uuid 965 * UNIT PUT uuid
852 srv_send((char *)"501 Parameter missing"); 980 srv_send((char *)"501 Parameter missing");
853 return 1; 981 return 1;
854 } 982 }
855 param = strtok(NULL, "\0"); 983 param = strtok(NULL, "\0");
856 984
857 /*
858 * UNIT uuid
859 */
860 if ((strlen(opt) == 36) && (param == NULL)) {
861 /*
862 * Search using uuid
863 */
864 for (unit = Config.units; unit; unit = unit->next) {
865 if (strcmp(opt, unit->uuid) == 0) {
866 srv_send((char *)"210 Unit %s selected", unit->uuid);
867 if (current_unit)
868 free(current_unit);
869 current_unit = xstrcpy(unit->uuid);;
870 return 1;
871 }
872 }
873 srv_send((char *)"410 No such unit");
874 return 1;
875 }
876
877 if ((strcmp(opt, (char *)"LIST") == 0) && (param == NULL)) { 985 if ((strcmp(opt, (char *)"LIST") == 0) && (param == NULL)) {
878 srv_send((char *)"212 Fermenter Units list follows:"); 986 srv_send((char *)"212 Fermenter Units list follows:");
879 for (unit = Config.units; unit; unit = unit->next) { 987 for (unit = Config.units; unit; unit = unit->next) {
880 srv_send((char *)"%s,%s,%s", unit->uuid, unit->name, UNITMODE[unit->mode]); 988 srv_send((char *)"%s,%s,%s", unit->uuid, unit->name, UNITMODE[unit->mode]);
881 } 989 }
893 unit->next = NULL; 1001 unit->next = NULL;
894 unit->version = 1; 1002 unit->version = 1;
895 unit->uuid = malloc(37); 1003 unit->uuid = malloc(37);
896 uuid_generate(uu); 1004 uuid_generate(uu);
897 uuid_unparse(uu, unit->uuid); 1005 uuid_unparse(uu, unit->uuid);
898 if (current_unit)
899 free(current_unit);
900 current_unit = xstrcpy(unit->uuid);
901 unit->name = xstrcpy(param); 1006 unit->name = xstrcpy(param);
902 unit->air_address = unit->beer_address = unit->heater_address = unit->cooler_address = \ 1007 unit->air_address = unit->beer_address = unit->heater_address = unit->cooler_address = \
903 unit->fan_address = unit->door_address = unit->profile = NULL; 1008 unit->fan_address = unit->door_address = unit->profile = NULL;
904 unit->volume = 0.0; 1009 unit->volume = 0.0;
905 unit->air_state = unit->beer_state = 1; 1010 unit->air_state = unit->beer_state = 1;
1236 * Process commands from the client 1341 * Process commands from the client
1237 */ 1342 */
1238 if (strncmp(buf, "DEVICE", 6) == 0) { 1343 if (strncmp(buf, "DEVICE", 6) == 0) {
1239 if (cmd_device(buf) == 0) 1344 if (cmd_device(buf) == 0)
1240 wrconfig(); 1345 wrconfig();
1346 } else if (strncmp(buf, "GLOBAL", 6) == 0) {
1347 if (cmd_global(buf) == 0)
1348 wrconfig();
1241 } else if (strncmp(buf, "HELP", 4) == 0) { 1349 } else if (strncmp(buf, "HELP", 4) == 0) {
1242 srv_send((char *)"100 Help text follows"); 1350 srv_send((char *)"100 Help text follows");
1243 srv_send((char *)"Recognized commands:"); 1351 srv_send((char *)"Recognized commands:");
1244 srv_send((char *)""); 1352 srv_send((char *)"");
1245 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890 1353 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890
1246 srv_send((char *)"DEVICE ADD type Add new Device type"); 1354 srv_send((char *)"DEVICE ADD type Add new Device type");
1247 srv_send((char *)"DEVICE DEL uuid Delete Device by uuid"); 1355 srv_send((char *)"DEVICE DEL uuid Delete Device by uuid");
1248 srv_send((char *)"DEVICE LIST List Devices"); 1356 srv_send((char *)"DEVICE LIST List Devices");
1249 srv_send((char *)"DEVICE GET uuid Get Device record by uuid"); 1357 srv_send((char *)"DEVICE GET uuid Get Device record by uuid");
1250 srv_send((char *)"DEVICE PUT uuid Put Device record by uuid"); 1358 srv_send((char *)"DEVICE PUT uuid Put Device record by uuid");
1359 srv_send((char *)"GLOBAL GET Get global settings");
1360 srv_send((char *)"GLOBAL PUT Put global settings");
1251 srv_send((char *)"LCD Get LCD screen (allways 4 rows of 20 characters)"); 1361 srv_send((char *)"LCD Get LCD screen (allways 4 rows of 20 characters)");
1252 srv_send((char *)"LIST List all fermenter units"); 1362 srv_send((char *)"LIST List all fermenter units");
1253 srv_send((char *)"LIST LOG uuid List logfile data in 1 hour lines"); 1363 srv_send((char *)"LIST LOG uuid List logfile data in 1 hour lines");
1254 srv_send((char *)"PROFILE uuid,name Profile rename"); 1364 srv_send((char *)"PROFILE uuid,name Profile rename");
1255 srv_send((char *)"PROFILE ADD name Add new profile with name"); 1365 srv_send((char *)"PROFILE ADD name Add new profile with name");
1257 srv_send((char *)"PROFILE LIST List available profiles"); 1367 srv_send((char *)"PROFILE LIST List available profiles");
1258 srv_send((char *)"PROFILE GET uuid Get Profile record by uuid"); 1368 srv_send((char *)"PROFILE GET uuid Get Profile record by uuid");
1259 srv_send((char *)"PROFILE PUT uuid Put Profile record by uuid"); 1369 srv_send((char *)"PROFILE PUT uuid Put Profile record by uuid");
1260 srv_send((char *)"PROFILE GETS uuid Profile get steps list"); 1370 srv_send((char *)"PROFILE GETS uuid Profile get steps list");
1261 srv_send((char *)"PROFILE PUTS uuid Profile put steps list"); 1371 srv_send((char *)"PROFILE PUTS uuid Profile put steps list");
1262 srv_send((char *)"UNIT uuid Select unit by uuid");
1263 srv_send((char *)"UNIT ADD name Add a new unit with name"); 1372 srv_send((char *)"UNIT ADD name Add a new unit with name");
1264 srv_send((char *)"UNIT DEL uuid Delete Unit by uuid"); 1373 srv_send((char *)"UNIT DEL uuid Delete Unit by uuid");
1265 srv_send((char *)"UNIT LIST List Units"); 1374 srv_send((char *)"UNIT LIST List all Units");
1266 srv_send((char *)"UNIT GET uuid Get Unit record by uuid"); 1375 srv_send((char *)"UNIT GET uuid Get Unit record by uuid");
1267 srv_send((char *)"UNIT PUT uuid Put Unit record by uuid"); 1376 srv_send((char *)"UNIT PUT uuid Put Unit record by uuid");
1268 srv_send((char *)"."); 1377 srv_send((char *)".");
1269 } else if (strncmp(buf, "LCD", 3) == 0) { 1378 } else if (strncmp(buf, "LCD", 3) == 0) {
1270 #ifdef HAVE_WIRINGPI_H 1379 #ifdef HAVE_WIRINGPI_H

mercurial