thermferm/server.c

changeset 660
a28ef4d9afa4
parent 652
16d3d4b58b5b
child 665
66fae54fa7ba
equal deleted inserted replaced
659:bfab45f4d5cd 660:a28ef4d9afa4
28 #include "lcd-buffer.h" 28 #include "lcd-buffer.h"
29 #include "xutil.h" 29 #include "xutil.h"
30 #include "mqtt.h" 30 #include "mqtt.h"
31 31
32 32
33 extern int my_shutdown;
34 extern int debug; 33 extern int debug;
35 extern int run_pause; 34 extern int run_pause;
36 extern int run_hold; 35 extern int run_hold;
37 extern sys_config Config; 36 extern sys_config Config;
38 extern const char UNITMODE[5][8]; 37 extern const char UNITMODE[5][8];
44 extern const char PROFSTATE[5][6]; 43 extern const char PROFSTATE[5][6];
45 extern pthread_mutex_t mutexes[5]; 44 extern pthread_mutex_t mutexes[5];
46 45
47 46
48 int my_server_state = 0; /* Thread running state */ 47 int my_server_state = 0; /* Thread running state */
49 int s; /* connected socket */ 48 int my_server_shutdown = 0; /* Thread shutdown */
49 //int s; /* connected socket */
50 int ls; /* listen socket */ 50 int ls; /* listen socket */
51 51
52 struct sockaddr_in myaddr_in; /* for local socket address */ 52 struct sockaddr_in myaddr_in; /* for local socket address */
53 struct sockaddr_in peeraddr_in; /* for peer socket address */ 53 struct sockaddr_in peeraddr_in; /* for peer socket address */
54 54
81 81
82 82
83 /* 83 /*
84 * Send message to client 84 * Send message to client
85 */ 85 */
86 int srv_send(const char *format, ...) 86 int srv_send(int s, const char *format, ...)
87 { 87 {
88 char out[SS_BUFSIZE]; 88 char out[SS_BUFSIZE];
89 va_list va_ptr; 89 va_list va_ptr;
90 90
91 if (s == -1) 91 if (s == -1)
118 /* 118 /*
119 * Argument is a buffer of size SS_BUFSIZE. 119 * Argument is a buffer of size SS_BUFSIZE.
120 * Return -1 if error, else the number of received 120 * Return -1 if error, else the number of received
121 * character. \n is line end, ignore \r. 121 * character. \n is line end, ignore \r.
122 */ 122 */
123 int srv_recv(char *buffer) 123 int srv_recv(int s, char *buffer)
124 { 124 {
125 int bytesloaded = 0; 125 int bytesloaded = 0;
126 ssize_t ret; 126 ssize_t ret;
127 unsigned char buf; 127 unsigned char buf;
128 socklen_t fromlen; 128 socklen_t fromlen;
302 * DEVICE DEL uuid 302 * DEVICE DEL uuid
303 * DEVICE LIST 303 * DEVICE LIST
304 * DEVICE GET uuid 304 * DEVICE GET uuid
305 * DEVICE PUT uuid 305 * DEVICE PUT uuid
306 */ 306 */
307 int cmd_device(char *buf) 307 int cmd_device(int s, char *buf)
308 { 308 {
309 char *opt, *param, *kwd, *val, ibuf[SS_BUFSIZE]; 309 char *opt, *param, *kwd, *val, ibuf[SS_BUFSIZE];
310 devices_list *device, *tmpd; 310 devices_list *device, *tmpd;
311 int i, rc, rlen, ival; 311 int i, rc, rlen, ival;
312 uuid_t uu; 312 uuid_t uu;
313 313
314 opt = strtok(buf, " \0"); 314 opt = strtok(buf, " \0");
315 opt = strtok(NULL, " \0"); 315 opt = strtok(NULL, " \0");
316 316
317 if (opt == NULL) { 317 if (opt == NULL) {
318 srv_send((char *)"501 Subcommand missing"); 318 srv_send(s, (char *)"501 Subcommand missing");
319 return 0; 319 return 0;
320 } 320 }
321 param = strtok(NULL, "\0"); 321 param = strtok(NULL, "\0");
322 322
323 if (strcmp(opt, (char *)"HELP") == 0) { 323 if (strcmp(opt, (char *)"HELP") == 0) {
324 srv_send((char *)"100 Help text follows:"); 324 srv_send(s, (char *)"100 Help text follows:");
325 srv_send((char *)"Recognized commands:"); 325 srv_send(s, (char *)"Recognized commands:");
326 srv_send((char *)"DEVICE ADD type Add device (RC433/DHT/I2C/SPI)"); 326 srv_send(s, (char *)"DEVICE ADD type Add device (RC433/DHT/I2C/SPI)");
327 srv_send((char *)"DEVICE DEL uuid Delete device by uuid"); 327 srv_send(s, (char *)"DEVICE DEL uuid Delete device by uuid");
328 srv_send((char *)"DEVICE LIST List all devices"); 328 srv_send(s, (char *)"DEVICE LIST List all devices");
329 srv_send((char *)"DEVICE GET uuid Read device by uuid parameters"); 329 srv_send(s, (char *)"DEVICE GET uuid Read device by uuid parameters");
330 srv_send((char *)"DEVICE PUT uuid Write device by uuid parameters"); 330 srv_send(s, (char *)"DEVICE PUT uuid Write device by uuid parameters");
331 srv_send((char *)"."); 331 srv_send(s, (char *)".");
332 return 0; 332 return 0;
333 } 333 }
334 334
335 if (strcmp(opt, (char *)"LIST") == 0) { 335 if (strcmp(opt, (char *)"LIST") == 0) {
336 srv_send((char *)"212 Devices list follows:"); 336 srv_send(s, (char *)"212 Devices list follows:");
337 for (device = Config.devices; device; device = device->next) { 337 for (device = Config.devices; device; device = device->next) {
338 srv_send((char *)"%s,%s,%d,%d,%s,%s,%d", device->uuid, device->address, device->subdevice, 338 srv_send(s, (char *)"%s,%s,%d,%d,%s,%s,%d", device->uuid, device->address, device->subdevice,
339 device->inuse, device->comment, DEVDIR[device->direction], device->value + device->offset); 339 device->inuse, device->comment, DEVDIR[device->direction], device->value + device->offset);
340 } 340 }
341 srv_send((char *)"."); 341 srv_send(s, (char *)".");
342 return 0; 342 return 0;
343 } 343 }
344 344
345 if (param == NULL) { 345 if (param == NULL) {
346 srv_send((char *)"502 Parameter missing"); 346 srv_send(s, (char *)"502 Parameter missing");
347 return 1; 347 return 1;
348 } 348 }
349 349
350 if (strcmp(opt, (char *)"ADD") == 0) { 350 if (strcmp(opt, (char *)"ADD") == 0) {
351 if ((strcmp(param, (char *)"RC433") == 0) || (strcmp(param, (char *)"DHT") == 0) || 351 if ((strcmp(param, (char *)"RC433") == 0) || (strcmp(param, (char *)"DHT") == 0) ||
381 } 381 }
382 } 382 }
383 } 383 }
384 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); 384 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
385 syslog(LOG_NOTICE, "Device %s added", device->uuid); 385 syslog(LOG_NOTICE, "Device %s added", device->uuid);
386 srv_send((char *)"211 Device %s added", device->uuid); 386 srv_send(s, (char *)"211 Device %s added", device->uuid);
387 return 1; 387 return 1;
388 388
389 } else { 389 } else {
390 srv_send((char *)"503 Parameter error"); 390 srv_send(s, (char *)"503 Parameter error");
391 return 0; 391 return 0;
392 } 392 }
393 } 393 }
394 394
395 if (strcmp(opt, (char *)"DEL") == 0) { 395 if (strcmp(opt, (char *)"DEL") == 0) {
397 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); 397 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
398 rc = delete_Device(param); 398 rc = delete_Device(param);
399 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); 399 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
400 if (rc) { 400 if (rc) {
401 syslog(LOG_NOTICE, "Device %s deleted", param); 401 syslog(LOG_NOTICE, "Device %s deleted", param);
402 srv_send((char *)"211 Device %s deleted", param); 402 srv_send(s, (char *)"211 Device %s deleted", param);
403 return 1; 403 return 1;
404 } else { 404 } else {
405 srv_send((char *)"440 No such device"); 405 srv_send(s, (char *)"440 No such device");
406 return 0; 406 return 0;
407 } 407 }
408 } 408 }
409 409
410 if (strcmp(opt, (char *)"GET") == 0) { 410 if (strcmp(opt, (char *)"GET") == 0) {
412 if (strcmp(device->uuid, param) == 0) { 412 if (strcmp(device->uuid, param) == 0) {
413 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); 413 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
414 int my_value = device->value; 414 int my_value = device->value;
415 int my_timestamp = (int)device->timestamp; 415 int my_timestamp = (int)device->timestamp;
416 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); 416 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
417 srv_send((char *)"213 Device record follows:"); 417 srv_send(s, (char *)"213 Device record follows:");
418 srv_send((char *)"TYPE,%s", DEVTYPE[device->type]); 418 srv_send(s, (char *)"TYPE,%s", DEVTYPE[device->type]);
419 srv_send((char *)"ADDRESS,%s", device->address); 419 srv_send(s, (char *)"ADDRESS,%s", device->address);
420 srv_send((char *)"DIRECTION,%s", DEVDIR[device->direction]); 420 srv_send(s, (char *)"DIRECTION,%s", DEVDIR[device->direction]);
421 srv_send((char *)"VALUE,%d", my_value); 421 srv_send(s, (char *)"VALUE,%d", my_value);
422 srv_send((char *)"OFFSET,%d", device->offset); 422 srv_send(s, (char *)"OFFSET,%d", device->offset);
423 srv_send((char *)"PRESENT,%s", DEVPRESENT[device->present]); 423 srv_send(s, (char *)"PRESENT,%s", DEVPRESENT[device->present]);
424 srv_send((char *)"SUBDEVICE,%d", device->subdevice); 424 srv_send(s, (char *)"SUBDEVICE,%d", device->subdevice);
425 srv_send((char *)"GPIOPIN,%d", device->gpiopin); 425 srv_send(s, (char *)"GPIOPIN,%d", device->gpiopin);
426 srv_send((char *)"DESCRIPTION,%s", device->description); 426 srv_send(s, (char *)"DESCRIPTION,%s", device->description);
427 srv_send((char *)"INUSE,%d", device->inuse); 427 srv_send(s, (char *)"INUSE,%d", device->inuse);
428 srv_send((char *)"COMMENT,%s", device->comment); 428 srv_send(s, (char *)"COMMENT,%s", device->comment);
429 srv_send((char *)"TIMESTAMP,%d", my_timestamp); 429 srv_send(s, (char *)"TIMESTAMP,%d", my_timestamp);
430 srv_send((char *)"."); 430 srv_send(s, (char *)".");
431 return 0; 431 return 0;
432 } 432 }
433 } 433 }
434 srv_send((char *)"440 No such device"); 434 srv_send(s, (char *)"440 No such device");
435 return 0; 435 return 0;
436 } 436 }
437 437
438 if (strcmp(opt, (char *)"PUT") == 0) { 438 if (strcmp(opt, (char *)"PUT") == 0) {
439 for (device = Config.devices; device; device = device->next) { 439 for (device = Config.devices; device; device = device->next) {
440 if (strcmp(device->uuid, param) == 0) { 440 if (strcmp(device->uuid, param) == 0) {
441 while (1) { 441 while (1) {
442 rlen = srv_recv(ibuf); 442 rlen = srv_recv(s, ibuf);
443 if (rlen == -1) { 443 if (rlen == -1) {
444 return 0; 444 return 0;
445 } 445 }
446 if (strlen(ibuf)) { 446 if (strlen(ibuf)) {
447 if (strcmp(ibuf, (char *)".") == 0) { 447 if (strcmp(ibuf, (char *)".") == 0) {
448 srv_send((char *)"219 Accepted Device record"); 448 srv_send(s, (char *)"219 Accepted Device record");
449 return 1; 449 return 1;
450 } 450 }
451 kwd = strtok(ibuf, ",\0"); 451 kwd = strtok(ibuf, ",\0");
452 val = strtok(NULL, "\0"); 452 val = strtok(NULL, "\0");
453 if (kwd && val) { 453 if (kwd && val) {
557 } 557 }
558 } 558 }
559 } 559 }
560 } 560 }
561 } 561 }
562 srv_send((char *)"440 No such device"); 562 srv_send(s, (char *)"440 No such device");
563 return 0; 563 return 0;
564 } 564 }
565 565
566 srv_send((char *)"504 Subcommand error"); 566 srv_send(s, (char *)"504 Subcommand error");
567 return 0; 567 return 0;
568 } 568 }
569 569
570 570
571 571
572 /* 572 /*
573 * GLOBAL GET 573 * GLOBAL GET
574 * GLOBAL PUT 574 * GLOBAL PUT
575 */ 575 */
576 int cmd_global(char *buf) 576 int cmd_global(int s, char *buf)
577 { 577 {
578 char *opt, *kwd, *val, ibuf[SS_BUFSIZE]; 578 char *opt, *kwd, *val, ibuf[SS_BUFSIZE];
579 int ival, rlen; 579 int ival, rlen;
580 580
581 opt = strtok(buf, " \0"); 581 opt = strtok(buf, " \0");
582 opt = strtok(NULL, "\0"); 582 opt = strtok(NULL, "\0");
583 583
584 if (opt == NULL) { 584 if (opt == NULL) {
585 srv_send((char *)"501 Subcommand missing"); 585 srv_send(s, (char *)"501 Subcommand missing");
586 return 0; 586 return 0;
587 } 587 }
588 588
589 if (strcmp(opt, (char *)"HELP") == 0) { 589 if (strcmp(opt, (char *)"HELP") == 0) {
590 srv_send((char *)"100 Help text follows:"); 590 srv_send(s, (char *)"100 Help text follows:");
591 srv_send((char *)"Recognized commands:"); 591 srv_send(s, (char *)"Recognized commands:");
592 srv_send((char *)"GLOBAL GET Get global settings"); 592 srv_send(s, (char *)"GLOBAL GET Get global settings");
593 srv_send((char *)"GLOBAL PUT Put global settings"); 593 srv_send(s, (char *)"GLOBAL PUT Put global settings");
594 srv_send((char *)"."); 594 srv_send(s, (char *)".");
595 return 0; 595 return 0;
596 } 596 }
597 597
598 if (strcmp(opt, (char *)"GET") == 0) { 598 if (strcmp(opt, (char *)"GET") == 0) {
599 srv_send((char *)"213 Global Settings record follows:"); 599 srv_send(s, (char *)"213 Global Settings record follows:");
600 srv_send((char *)"RELEASE,%s", VERSION); 600 srv_send(s, (char *)"RELEASE,%s", VERSION);
601 srv_send((char *)"NAME,%s", Config.name); 601 srv_send(s, (char *)"NAME,%s", Config.name);
602 srv_send((char *)"PORT,%d", Config.my_port); 602 srv_send(s, (char *)"PORT,%d", Config.my_port);
603 srv_send((char *)"TEMPFORMAT,%c", Config.tempFormat); 603 srv_send(s, (char *)"TEMPFORMAT,%c", Config.tempFormat);
604 srv_send((char *)"TEMP_ADDRESS,%s", Config.temp_address); 604 srv_send(s, (char *)"TEMP_ADDRESS,%s", Config.temp_address);
605 srv_send((char *)"TEMP_STATE,%s", TEMPSTATE[Config.temp_state]); 605 srv_send(s, (char *)"TEMP_STATE,%s", TEMPSTATE[Config.temp_state]);
606 srv_send((char *)"TEMP_VALUE,%.1f", Config.temp_value / 1000.0); 606 srv_send(s, (char *)"TEMP_VALUE,%.1f", Config.temp_value / 1000.0);
607 srv_send((char *)"HUM_ADDRESS,%s", Config.hum_address); 607 srv_send(s, (char *)"HUM_ADDRESS,%s", Config.hum_address);
608 srv_send((char *)"HUM_STATE,%s", TEMPSTATE[Config.hum_state]); 608 srv_send(s, (char *)"HUM_STATE,%s", TEMPSTATE[Config.hum_state]);
609 srv_send((char *)"HUM_VALUE,%.0f", Config.hum_value / 1000.0); 609 srv_send(s, (char *)"HUM_VALUE,%.0f", Config.hum_value / 1000.0);
610 srv_send((char *)"TEMP_HUM_IDX,%d", Config.temp_hum_idx); 610 srv_send(s, (char *)"TEMP_HUM_IDX,%d", Config.temp_hum_idx);
611 srv_send((char *)"LCD_COLS,%d", Config.lcd_cols); 611 srv_send(s, (char *)"LCD_COLS,%d", Config.lcd_cols);
612 srv_send((char *)"LCD_ROWS,%d", Config.lcd_rows); 612 srv_send(s, (char *)"LCD_ROWS,%d", Config.lcd_rows);
613 srv_send((char *)"NEXT_UNIT,%d", Config.next_unit); 613 srv_send(s, (char *)"NEXT_UNIT,%d", Config.next_unit);
614 srv_send((char *)"MQTT_HOST,%s", Config.mqtt_host); 614 srv_send(s, (char *)"MQTT_HOST,%s", Config.mqtt_host);
615 srv_send((char *)"MQTT_PORT,%d", Config.mqtt_port); 615 srv_send(s, (char *)"MQTT_PORT,%d", Config.mqtt_port);
616 srv_send((char *)"MQTT_USER,%s", Config.mqtt_username); 616 srv_send(s, (char *)"MQTT_USER,%s", Config.mqtt_username);
617 srv_send((char *)"MQTT_PASS,%s", Config.mqtt_password); 617 srv_send(s, (char *)"MQTT_PASS,%s", Config.mqtt_password);
618 srv_send((char *)"."); 618 srv_send(s, (char *)".");
619 return 0; 619 return 0;
620 } 620 }
621 621
622 if (strcmp(opt, (char *)"PUT") == 0) { 622 if (strcmp(opt, (char *)"PUT") == 0) {
623 int mqtt_reconnect = 0; 623 int mqtt_reconnect = 0;
624 while (1) { 624 while (1) {
625 rlen = srv_recv(ibuf); 625 rlen = srv_recv(s, ibuf);
626 if (rlen == -1) { 626 if (rlen == -1) {
627 return 0; 627 return 0;
628 } 628 }
629 if (strlen(ibuf)) { 629 if (strlen(ibuf)) {
630 if (strcmp(ibuf, (char *)".") == 0) { 630 if (strcmp(ibuf, (char *)".") == 0) {
631 srv_send((char *)"219 Accepted Global record"); 631 srv_send(s, (char *)"219 Accepted Global record");
632 if (mqtt_reconnect) 632 if (mqtt_reconnect)
633 mqtt_connect(); 633 mqtt_connect();
634 return 1; 634 return 1;
635 } 635 }
636 kwd = strtok(ibuf, ",\0"); 636 kwd = strtok(ibuf, ",\0");
757 } 757 }
758 } 758 }
759 } 759 }
760 } 760 }
761 761
762 srv_send((char *)"504 Subcommand error"); 762 srv_send(s, (char *)"504 Subcommand error");
763 return 0; 763 return 0;
764 } 764 }
765 765
766 766
767 767
768 /* 768 /*
769 * LIST 769 * LIST
770 */ 770 */
771 int cmd_list(char *buf) 771 int cmd_list(int s, char *buf)
772 { 772 {
773 char *opt; 773 char *opt;
774 units_list *unit; 774 units_list *unit;
775 775
776 opt = strtok(buf, " \0"); 776 opt = strtok(buf, " \0");
778 778
779 if (opt == NULL) { 779 if (opt == NULL) {
780 /* 780 /*
781 * Default, list available units 781 * Default, list available units
782 */ 782 */
783 srv_send((char *)"212 Fermenter list follows:"); 783 srv_send(s, (char *)"212 Fermenter list follows:");
784 for (unit = Config.units; unit; unit = unit->next) { 784 for (unit = Config.units; unit; unit = unit->next) {
785 srv_send((char *)"%s,%s,%s", unit->uuid, unit->alias, UNITMODE[unit->mode]); 785 srv_send(s, (char *)"%s,%s,%s", unit->uuid, unit->alias, UNITMODE[unit->mode]);
786 } 786 }
787 srv_send((char *)"."); 787 srv_send(s, (char *)".");
788 return 0; 788 return 0;
789 789
790 } else if (strcmp(opt, (char *)"HELP") == 0) { 790 } else if (strcmp(opt, (char *)"HELP") == 0) {
791 srv_send((char *)"100 Help text follows:"); 791 srv_send(s, (char *)"100 Help text follows:");
792 srv_send((char *)"Recognized commands:"); 792 srv_send(s, (char *)"Recognized commands:");
793 srv_send((char *)"LIST List available units"); 793 srv_send(s, (char *)"LIST List available units");
794 srv_send((char *)"."); 794 srv_send(s, (char *)".");
795 return 0; 795 return 0;
796 } 796 }
797 797
798 srv_send((char *)"504 Subcommand error"); 798 srv_send(s, (char *)"504 Subcommand error");
799 return 0; 799 return 0;
800 } 800 }
801 801
802 802
803 803
842 * SIMULATOR DEL uuid 842 * SIMULATOR DEL uuid
843 * SIMULATOR LIST 843 * SIMULATOR LIST
844 * SIMULATOR GET uuid 844 * SIMULATOR GET uuid
845 * SIMULATOR PUT uuid 845 * SIMULATOR PUT uuid
846 */ 846 */
847 int cmd_simulator(char *buf) 847 int cmd_simulator(int s, char *buf)
848 { 848 {
849 char *opt, *param, *kwd, *val, ibuf[SS_BUFSIZE]; 849 char *opt, *param, *kwd, *val, ibuf[SS_BUFSIZE];
850 simulator_list *simulator, *tmps; 850 simulator_list *simulator, *tmps;
851 int rc, rlen, ival; 851 int rc, rlen, ival;
852 float fval; 852 float fval;
854 854
855 opt = strtok(buf, " \0"); 855 opt = strtok(buf, " \0");
856 opt = strtok(NULL, " \0"); 856 opt = strtok(NULL, " \0");
857 857
858 if (opt == NULL) { 858 if (opt == NULL) {
859 srv_send((char *)"501 Subcommand missing"); 859 srv_send(s, (char *)"501 Subcommand missing");
860 return 0; 860 return 0;
861 } 861 }
862 param = strtok(NULL, "\0"); 862 param = strtok(NULL, "\0");
863 863
864 if (strcmp(opt, (char *)"HELP") == 0) { 864 if (strcmp(opt, (char *)"HELP") == 0) {
865 srv_send((char *)"100 Help text follows:"); 865 srv_send(s, (char *)"100 Help text follows:");
866 srv_send((char *)"Recognized commands:"); 866 srv_send(s, (char *)"Recognized commands:");
867 srv_send((char *)"SIMULATOR ADD name Add a new Simulator with name"); 867 srv_send(s, (char *)"SIMULATOR ADD name Add a new Simulator with name");
868 srv_send((char *)"SIMULATOR DEL uuid Delete Simulator by uuid"); 868 srv_send(s, (char *)"SIMULATOR DEL uuid Delete Simulator by uuid");
869 srv_send((char *)"SIMULATOR LIST List all Simulators"); 869 srv_send(s, (char *)"SIMULATOR LIST List all Simulators");
870 srv_send((char *)"SIMULATOR GET uuid Get Simulator record by uuid"); 870 srv_send(s, (char *)"SIMULATOR GET uuid Get Simulator record by uuid");
871 srv_send((char *)"SIMULATOR PUT uuid Put Simulator record by uuid"); 871 srv_send(s, (char *)"SIMULATOR PUT uuid Put Simulator record by uuid");
872 srv_send((char *)"."); 872 srv_send(s, (char *)".");
873 return 0; 873 return 0;
874 } 874 }
875 875
876 if (strcmp(opt, (char *)"LIST") == 0) { 876 if (strcmp(opt, (char *)"LIST") == 0) {
877 srv_send((char *)"212 Simulators list follows:"); 877 srv_send(s, (char *)"212 Simulators list follows:");
878 for (simulator = Config.simulators; simulator; simulator = simulator->next) { 878 for (simulator = Config.simulators; simulator; simulator = simulator->next) {
879 srv_send((char *)"%s,%s", simulator->uuid, simulator->name); 879 srv_send(s, (char *)"%s,%s", simulator->uuid, simulator->name);
880 } 880 }
881 srv_send((char *)"."); 881 srv_send(s, (char *)".");
882 return 0; 882 return 0;
883 } 883 }
884 884
885 if (param == NULL) { 885 if (param == NULL) {
886 srv_send((char *)"502 Parameter missing"); 886 srv_send(s, (char *)"502 Parameter missing");
887 return 0; 887 return 0;
888 } 888 }
889 889
890 if (strcmp(opt, (char *)"ADD") == 0) { 890 if (strcmp(opt, (char *)"ADD") == 0) {
891 891
892 /* 892 /*
893 * For now, only one simulator is allowed. 893 * For now, only one simulator is allowed.
894 */ 894 */
895 if (Config.simulators) { 895 if (Config.simulators) {
896 srv_send((char *)"441 Maximum simulators reached"); 896 srv_send(s, (char *)"441 Maximum simulators reached");
897 return 0; 897 return 0;
898 } 898 }
899 899
900 simulator = (simulator_list *)malloc(sizeof(simulator_list)); 900 simulator = (simulator_list *)malloc(sizeof(simulator_list));
901 simulator->next = NULL; 901 simulator->next = NULL;
929 } 929 }
930 } 930 }
931 } 931 }
932 932
933 syslog(LOG_NOTICE, "Simulator %s added", simulator->uuid); 933 syslog(LOG_NOTICE, "Simulator %s added", simulator->uuid);
934 srv_send((char *)"211 Simulator %s added", simulator->uuid); 934 srv_send(s, (char *)"211 Simulator %s added", simulator->uuid);
935 return 1; 935 return 1;
936 } 936 }
937 937
938 if (strcmp(opt, (char *)"DEL") == 0) { 938 if (strcmp(opt, (char *)"DEL") == 0) {
939 rc = delete_Simulator(param); 939 rc = delete_Simulator(param);
940 if (rc) { 940 if (rc) {
941 syslog(LOG_NOTICE, "Simulator %s deleted", param); 941 syslog(LOG_NOTICE, "Simulator %s deleted", param);
942 srv_send((char *)"211 Simulator %s deleted", param); 942 srv_send(s, (char *)"211 Simulator %s deleted", param);
943 return 1; 943 return 1;
944 } else { 944 } else {
945 srv_send((char *)"440 No such simulator"); 945 srv_send(s, (char *)"440 No such simulator");
946 return 0; 946 return 0;
947 } 947 }
948 } 948 }
949 949
950 if (strcmp(opt, (char *)"GET") == 0) { 950 if (strcmp(opt, (char *)"GET") == 0) {
951 for (simulator = Config.simulators; simulator; simulator = simulator->next) { 951 for (simulator = Config.simulators; simulator; simulator = simulator->next) {
952 if (strcmp(simulator->uuid, param) == 0) { 952 if (strcmp(simulator->uuid, param) == 0) {
953 srv_send((char *)"213 Simulator record follows:"); 953 srv_send(s, (char *)"213 Simulator record follows:");
954 srv_send((char *)"NAME,%s", simulator->name); 954 srv_send(s, (char *)"NAME,%s", simulator->name);
955 srv_send((char *)"VOLUME_AIR,%d", simulator->volume_air); 955 srv_send(s, (char *)"VOLUME_AIR,%d", simulator->volume_air);
956 srv_send((char *)"VOLUME_BEER,%d", simulator->volume_beer); 956 srv_send(s, (char *)"VOLUME_BEER,%d", simulator->volume_beer);
957 srv_send((char *)"ROOM_TEMPERATURE,%.1f", simulator->room_temperature); 957 srv_send(s, (char *)"ROOM_TEMPERATURE,%.1f", simulator->room_temperature);
958 srv_send((char *)"ROOM_HUMIDITY,%.1f", simulator->room_humidity); 958 srv_send(s, (char *)"ROOM_HUMIDITY,%.1f", simulator->room_humidity);
959 srv_send((char *)"AIR_TEMPERATURE,%.3f", simulator->air_temperature); 959 srv_send(s, (char *)"AIR_TEMPERATURE,%.3f", simulator->air_temperature);
960 srv_send((char *)"BEER_TEMPERATURE,%.3f", simulator->beer_temperature); 960 srv_send(s, (char *)"BEER_TEMPERATURE,%.3f", simulator->beer_temperature);
961 srv_send((char *)"CHILLER_TEMPERATURE,%.3f", simulator->chiller_temperature); 961 srv_send(s, (char *)"CHILLER_TEMPERATURE,%.3f", simulator->chiller_temperature);
962 srv_send((char *)"COOLER_TEMP,%.1f", simulator->cooler_temp); 962 srv_send(s, (char *)"COOLER_TEMP,%.1f", simulator->cooler_temp);
963 srv_send((char *)"COOLER_TIME,%d", simulator->cooler_time); 963 srv_send(s, (char *)"COOLER_TIME,%d", simulator->cooler_time);
964 srv_send((char *)"COOLER_SIZE,%.3f", simulator->cooler_size); 964 srv_send(s, (char *)"COOLER_SIZE,%.3f", simulator->cooler_size);
965 srv_send((char *)"HEATER_TEMP,%.1f", simulator->heater_temp); 965 srv_send(s, (char *)"HEATER_TEMP,%.1f", simulator->heater_temp);
966 srv_send((char *)"HEATER_TIME,%d", simulator->heater_time); 966 srv_send(s, (char *)"HEATER_TIME,%d", simulator->heater_time);
967 srv_send((char *)"HEATER_SIZE,%.3f", simulator->heater_size); 967 srv_send(s, (char *)"HEATER_SIZE,%.3f", simulator->heater_size);
968 srv_send((char *)"HEATER_STATE,%d", simulator->heater_state); 968 srv_send(s, (char *)"HEATER_STATE,%d", simulator->heater_state);
969 srv_send((char *)"COOLER_STATE,%d", simulator->cooler_state); 969 srv_send(s, (char *)"COOLER_STATE,%d", simulator->cooler_state);
970 srv_send((char *)"FRIGO_ISOLATION,%.3f", simulator->frigo_isolation); 970 srv_send(s, (char *)"FRIGO_ISOLATION,%.3f", simulator->frigo_isolation);
971 srv_send((char *)"."); 971 srv_send(s, (char *)".");
972 return 0; 972 return 0;
973 } 973 }
974 } 974 }
975 srv_send((char *)"440 No such simulator"); 975 srv_send(s, (char *)"440 No such simulator");
976 return 0; 976 return 0;
977 } 977 }
978 978
979 if (strcmp(opt, (char *)"PUT") == 0) { 979 if (strcmp(opt, (char *)"PUT") == 0) {
980 for (simulator = Config.simulators; simulator; simulator = simulator->next) { 980 for (simulator = Config.simulators; simulator; simulator = simulator->next) {
981 if (strcmp(simulator->uuid, param) == 0) { 981 if (strcmp(simulator->uuid, param) == 0) {
982 while (1) { 982 while (1) {
983 rlen = srv_recv(ibuf); 983 rlen = srv_recv(s, ibuf);
984 if (rlen == -1) { 984 if (rlen == -1) {
985 return 0; 985 return 0;
986 } 986 }
987 if (strlen(ibuf)) { 987 if (strlen(ibuf)) {
988 if (strcmp(ibuf, (char *)".") == 0) { 988 if (strcmp(ibuf, (char *)".") == 0) {
989 srv_send((char *)"219 Accepted Simulator record"); 989 srv_send(s, (char *)"219 Accepted Simulator record");
990 return 1; 990 return 1;
991 } 991 }
992 kwd = strtok(ibuf, ",\0"); 992 kwd = strtok(ibuf, ",\0");
993 val = strtok(NULL, "\0"); 993 val = strtok(NULL, "\0");
994 if (kwd && val) { 994 if (kwd && val) {
1117 } 1117 }
1118 } 1118 }
1119 } 1119 }
1120 } 1120 }
1121 } 1121 }
1122 srv_send((char *)"440 No such simulator"); 1122 srv_send(s, (char *)"440 No such simulator");
1123 return 0; 1123 return 0;
1124 } 1124 }
1125 1125
1126 srv_send((char *)"504 Subcommand error"); 1126 srv_send(s, (char *)"504 Subcommand error");
1127 return 0; 1127 return 0;
1128 } 1128 }
1129 #endif 1129 #endif
1130 1130
1131 1131
1283 * UNIT DEL uuid 1283 * UNIT DEL uuid
1284 * UNIT LIST 1284 * UNIT LIST
1285 * UNIT GET uuid 1285 * UNIT GET uuid
1286 * UNIT PUT uuid 1286 * UNIT PUT uuid
1287 */ 1287 */
1288 int cmd_unit(char *buf) 1288 int cmd_unit(int s, char *buf)
1289 { 1289 {
1290 char *opt, *param = NULL, *kwd, *val, ibuf[SS_BUFSIZE]; 1290 char *opt, *param = NULL, *kwd, *val, ibuf[SS_BUFSIZE];
1291 units_list *unit, *tmpu; 1291 units_list *unit, *tmpu;
1292 uuid_t uu; 1292 uuid_t uu;
1293 int ival, i, rc, rlen; 1293 int ival, i, rc, rlen;
1295 1295
1296 opt = strtok(buf, " \0"); 1296 opt = strtok(buf, " \0");
1297 opt = strtok(NULL, " \0"); 1297 opt = strtok(NULL, " \0");
1298 1298
1299 if (opt == NULL) { 1299 if (opt == NULL) {
1300 srv_send((char *)"501 Subcommand missing"); 1300 srv_send(s, (char *)"501 Subcommand missing");
1301 return 0; 1301 return 0;
1302 } 1302 }
1303 param = strtok(NULL, "\0"); 1303 param = strtok(NULL, "\0");
1304 1304
1305 if (strcmp(opt, (char *)"HELP") == 0) { 1305 if (strcmp(opt, (char *)"HELP") == 0) {
1306 srv_send((char *)"100 Help text follows:"); 1306 srv_send(s, (char *)"100 Help text follows:");
1307 srv_send((char *)"Recognized commands:"); 1307 srv_send(s, (char *)"Recognized commands:");
1308 srv_send((char *)"UNIT ADD name Add a new Unit with name"); 1308 srv_send(s, (char *)"UNIT ADD name Add a new Unit with name");
1309 srv_send((char *)"UNIT DEL uuid Delete Unit by uuid"); 1309 srv_send(s, (char *)"UNIT DEL uuid Delete Unit by uuid");
1310 srv_send((char *)"UNIT LIST List all Units"); 1310 srv_send(s, (char *)"UNIT LIST List all Units");
1311 srv_send((char *)"UNIT GET uuid Get Unit record by uuid"); 1311 srv_send(s, (char *)"UNIT GET uuid Get Unit record by uuid");
1312 srv_send((char *)"UNIT PUT uuid Put Unit record by uuid"); 1312 srv_send(s, (char *)"UNIT PUT uuid Put Unit record by uuid");
1313 srv_send((char *)"."); 1313 srv_send(s, (char *)".");
1314 return 0; 1314 return 0;
1315 } 1315 }
1316 1316
1317 if ((strcmp(opt, (char *)"LIST") == 0) && (param == NULL)) { 1317 if ((strcmp(opt, (char *)"LIST") == 0) && (param == NULL)) {
1318 srv_send((char *)"212 Fermenter list follows:"); 1318 srv_send(s, (char *)"212 Fermenter list follows:");
1319 for (unit = Config.units; unit; unit = unit->next) { 1319 for (unit = Config.units; unit; unit = unit->next) {
1320 if (strlen(unit->product_code) && strlen(unit->product_name)) { 1320 if (strlen(unit->product_code) && strlen(unit->product_name)) {
1321 srv_send((char *)"%s,%s %s,%s", unit->uuid, unit->product_code, unit->product_name, UNITMODE[unit->mode]); 1321 srv_send(s, (char *)"%s,%s %s,%s", unit->uuid, unit->product_code, unit->product_name, UNITMODE[unit->mode]);
1322 } else { 1322 } else {
1323 srv_send((char *)"%s,%s,%s", unit->uuid, unit->alias, UNITMODE[unit->mode]); 1323 srv_send(s, (char *)"%s,%s,%s", unit->uuid, unit->alias, UNITMODE[unit->mode]);
1324 } 1324 }
1325 } 1325 }
1326 srv_send((char *)"."); 1326 srv_send(s, (char *)".");
1327 return 0; 1327 return 0;
1328 } 1328 }
1329 1329
1330 if (param == NULL) { 1330 if (param == NULL) {
1331 srv_send((char *)"502 Parameter missing"); 1331 srv_send(s, (char *)"502 Parameter missing");
1332 return 0; 1332 return 0;
1333 } 1333 }
1334 1334
1335 if (strcmp(opt, (char *)"ADD") == 0) { 1335 if (strcmp(opt, (char *)"ADD") == 0) {
1336 char an[128]; 1336 char an[128];
1394 } 1394 }
1395 lcd_buf_reset(); 1395 lcd_buf_reset();
1396 run_pause = FALSE; 1396 run_pause = FALSE;
1397 1397
1398 syslog(LOG_NOTICE, "Unit %s added", unit->uuid); 1398 syslog(LOG_NOTICE, "Unit %s added", unit->uuid);
1399 srv_send((char *)"211 Unit %s added", unit->uuid); 1399 srv_send(s, (char *)"211 Unit %s added", unit->uuid);
1400 return 1; 1400 return 1;
1401 } 1401 }
1402 1402
1403 if (strcmp(opt, (char *)"DEL") == 0) { 1403 if (strcmp(opt, (char *)"DEL") == 0) {
1404 /* 1404 /*
1415 lcd_buf_reset(); 1415 lcd_buf_reset();
1416 run_pause = FALSE; 1416 run_pause = FALSE;
1417 1417
1418 if (rc) { 1418 if (rc) {
1419 syslog(LOG_NOTICE, "Unit %s deleted", param); 1419 syslog(LOG_NOTICE, "Unit %s deleted", param);
1420 srv_send((char *)"211 Unit %s deleted", param); 1420 srv_send(s, (char *)"211 Unit %s deleted", param);
1421 return 1; 1421 return 1;
1422 } else { 1422 } else {
1423 srv_send((char *)"440 No such unit"); 1423 srv_send(s, (char *)"440 No such unit");
1424 return 0; 1424 return 0;
1425 } 1425 }
1426 } 1426 }
1427 1427
1428 if (strcmp(opt, (char *)"GET") == 0) { 1428 if (strcmp(opt, (char *)"GET") == 0) {
1429 for (unit = Config.units; unit; unit = unit->next) { 1429 for (unit = Config.units; unit; unit = unit->next) {
1430 if (strcmp(param, unit->uuid) == 0) { 1430 if (strcmp(param, unit->uuid) == 0) {
1431 srv_send((char *)"213 Unit listing follows:"); 1431 srv_send(s, (char *)"213 Unit listing follows:");
1432 srv_send((char *)"UUID,%s", unit->uuid); 1432 srv_send(s, (char *)"UUID,%s", unit->uuid);
1433 srv_send((char *)"ALIAS,%s", unit->alias); 1433 srv_send(s, (char *)"ALIAS,%s", unit->alias);
1434 srv_send((char *)"PRODUCT_NAME,%s", unit->product_name); 1434 srv_send(s, (char *)"PRODUCT_NAME,%s", unit->product_name);
1435 srv_send((char *)"PRODUCT_CODE,%s", unit->product_code); 1435 srv_send(s, (char *)"PRODUCT_CODE,%s", unit->product_code);
1436 srv_send((char *)"MODE,%s", UNITMODE[unit->mode]); 1436 srv_send(s, (char *)"MODE,%s", UNITMODE[unit->mode]);
1437 srv_send((char *)"STAGE,%s", UNITSTAGE[unit->stage]); 1437 srv_send(s, (char *)"STAGE,%s", UNITSTAGE[unit->stage]);
1438 srv_send((char *)"VOLUME,%2f", unit->volume); 1438 srv_send(s, (char *)"VOLUME,%2f", unit->volume);
1439 srv_send((char *)"AIR_ADDRESS,%s", unit->air_address); 1439 srv_send(s, (char *)"AIR_ADDRESS,%s", unit->air_address);
1440 srv_send((char *)"AIR_STATE,%s", TEMPSTATE[unit->air_state]); 1440 srv_send(s, (char *)"AIR_STATE,%s", TEMPSTATE[unit->air_state]);
1441 srv_send((char *)"AIR_TEMPERATURE,%.3f", unit->air_temperature / 1000.0); 1441 srv_send(s, (char *)"AIR_TEMPERATURE,%.3f", unit->air_temperature / 1000.0);
1442 srv_send((char *)"AIR_IDX,%d", unit->air_idx); 1442 srv_send(s, (char *)"AIR_IDX,%d", unit->air_idx);
1443 srv_send((char *)"BEER_ADDRESS,%s", MBSE_SS(unit->beer_address)); 1443 srv_send(s, (char *)"BEER_ADDRESS,%s", MBSE_SS(unit->beer_address));
1444 srv_send((char *)"BEER_ADDRESS2,%s", MBSE_SS(unit->beer_address2)); 1444 srv_send(s, (char *)"BEER_ADDRESS2,%s", MBSE_SS(unit->beer_address2));
1445 srv_send((char *)"BEER_STATE,%s", TEMPSTATE[unit->beer_state]); 1445 srv_send(s, (char *)"BEER_STATE,%s", TEMPSTATE[unit->beer_state]);
1446 srv_send((char *)"BEER_TEMPERATURE,%.3f", unit->beer_temperature / 1000.0); 1446 srv_send(s, (char *)"BEER_TEMPERATURE,%.3f", unit->beer_temperature / 1000.0);
1447 srv_send((char *)"BEER_IDX,%d", unit->beer_idx); 1447 srv_send(s, (char *)"BEER_IDX,%d", unit->beer_idx);
1448 srv_send((char *)"CHILLER_ADDRESS,%s", MBSE_SS(unit->chiller_address)); 1448 srv_send(s, (char *)"CHILLER_ADDRESS,%s", MBSE_SS(unit->chiller_address));
1449 srv_send((char *)"CHILLER_STATE,%s", TEMPSTATE[unit->chiller_state]); 1449 srv_send(s, (char *)"CHILLER_STATE,%s", TEMPSTATE[unit->chiller_state]);
1450 srv_send((char *)"CHILLER_TEMPERATURE,%.3f", unit->chiller_temperature / 1000.0); 1450 srv_send(s, (char *)"CHILLER_TEMPERATURE,%.3f", unit->chiller_temperature / 1000.0);
1451 srv_send((char *)"CHILLER_IDX,%d", unit->chiller_idx); 1451 srv_send(s, (char *)"CHILLER_IDX,%d", unit->chiller_idx);
1452 srv_send((char *)"HEATER_ADDRESS,%s", unit->heater_address); 1452 srv_send(s, (char *)"HEATER_ADDRESS,%s", unit->heater_address);
1453 srv_send((char *)"HEATER_STATE,%d", unit->heater_state); 1453 srv_send(s, (char *)"HEATER_STATE,%d", unit->heater_state);
1454 srv_send((char *)"HEATER_DELAY,%d", unit->heater_delay); 1454 srv_send(s, (char *)"HEATER_DELAY,%d", unit->heater_delay);
1455 srv_send((char *)"HEATER_USAGE,%d", unit->heater_usage); 1455 srv_send(s, (char *)"HEATER_USAGE,%d", unit->heater_usage);
1456 srv_send((char *)"HEATER_IDX,%d", unit->heater_idx); 1456 srv_send(s, (char *)"HEATER_IDX,%d", unit->heater_idx);
1457 if (unit->PID_heat) { 1457 if (unit->PID_heat) {
1458 srv_send((char *)"PIDH_IMAX,%.1f", unit->PID_heat->iMax); 1458 srv_send(s, (char *)"PIDH_IMAX,%.1f", unit->PID_heat->iMax);
1459 srv_send((char *)"PIDH_IDLERANGE,%.2f", unit->PID_heat->idleRange); 1459 srv_send(s, (char *)"PIDH_IDLERANGE,%.2f", unit->PID_heat->idleRange);
1460 srv_send((char *)"PIDH_PGAIN,%.3f", unit->PID_heat->pGain); 1460 srv_send(s, (char *)"PIDH_PGAIN,%.3f", unit->PID_heat->pGain);
1461 srv_send((char *)"PIDH_IGAIN,%.3f", unit->PID_heat->iGain); 1461 srv_send(s, (char *)"PIDH_IGAIN,%.3f", unit->PID_heat->iGain);
1462 srv_send((char *)"PIDH_DGAIN,%.3f", unit->PID_heat->dGain); 1462 srv_send(s, (char *)"PIDH_DGAIN,%.3f", unit->PID_heat->dGain);
1463 srv_send((char *)"PIDH_SV,%.2f", unit->PID_heat->SetP); 1463 srv_send(s, (char *)"PIDH_SV,%.2f", unit->PID_heat->SetP);
1464 } 1464 }
1465 srv_send((char *)"COOLER_ADDRESS,%s", unit->cooler_address); 1465 srv_send(s, (char *)"COOLER_ADDRESS,%s", unit->cooler_address);
1466 srv_send((char *)"COOLER_STATE,%d", unit->cooler_state); 1466 srv_send(s, (char *)"COOLER_STATE,%d", unit->cooler_state);
1467 srv_send((char *)"COOLER_DELAY,%d", unit->cooler_delay); 1467 srv_send(s, (char *)"COOLER_DELAY,%d", unit->cooler_delay);
1468 srv_send((char *)"COOLER_USAGE,%d", unit->cooler_usage); 1468 srv_send(s, (char *)"COOLER_USAGE,%d", unit->cooler_usage);
1469 srv_send((char *)"COOLER_IDX,%d", unit->cooler_idx); 1469 srv_send(s, (char *)"COOLER_IDX,%d", unit->cooler_idx);
1470 if (unit->PID_cool) { 1470 if (unit->PID_cool) {
1471 srv_send((char *)"PIDC_IMAX,%.1f", unit->PID_cool->iMax); 1471 srv_send(s, (char *)"PIDC_IMAX,%.1f", unit->PID_cool->iMax);
1472 srv_send((char *)"PIDC_IDLERANGE,%.2f", unit->PID_cool->idleRange); 1472 srv_send(s, (char *)"PIDC_IDLERANGE,%.2f", unit->PID_cool->idleRange);
1473 srv_send((char *)"PIDC_PGAIN,%.3f", unit->PID_cool->pGain); 1473 srv_send(s, (char *)"PIDC_PGAIN,%.3f", unit->PID_cool->pGain);
1474 srv_send((char *)"PIDC_IGAIN,%.3f", unit->PID_cool->iGain); 1474 srv_send(s, (char *)"PIDC_IGAIN,%.3f", unit->PID_cool->iGain);
1475 srv_send((char *)"PIDC_DGAIN,%.3f", unit->PID_cool->dGain); 1475 srv_send(s, (char *)"PIDC_DGAIN,%.3f", unit->PID_cool->dGain);
1476 srv_send((char *)"PIDC_SV,%.2f", unit->PID_cool->SetP); 1476 srv_send(s, (char *)"PIDC_SV,%.2f", unit->PID_cool->SetP);
1477 } 1477 }
1478 srv_send((char *)"FAN_ADDRESS,%s", unit->fan_address); 1478 srv_send(s, (char *)"FAN_ADDRESS,%s", unit->fan_address);
1479 srv_send((char *)"FAN_STATE,%d", unit->fan_state); 1479 srv_send(s, (char *)"FAN_STATE,%d", unit->fan_state);
1480 srv_send((char *)"FAN_DELAY,%d", unit->fan_delay); 1480 srv_send(s, (char *)"FAN_DELAY,%d", unit->fan_delay);
1481 srv_send((char *)"FAN_USAGE,%d", unit->fan_usage); 1481 srv_send(s, (char *)"FAN_USAGE,%d", unit->fan_usage);
1482 srv_send((char *)"FAN_IDX,%d", unit->fan_idx); 1482 srv_send(s, (char *)"FAN_IDX,%d", unit->fan_idx);
1483 srv_send((char *)"LIGHT_ADDRESS,%s", unit->light_address); 1483 srv_send(s, (char *)"LIGHT_ADDRESS,%s", unit->light_address);
1484 srv_send((char *)"LIGHT_STATE,%d", unit->light_state); 1484 srv_send(s, (char *)"LIGHT_STATE,%d", unit->light_state);
1485 srv_send((char *)"LIGHT_DELAY,%d", unit->light_delay); 1485 srv_send(s, (char *)"LIGHT_DELAY,%d", unit->light_delay);
1486 srv_send((char *)"LIGHT_USAGE,%d", unit->light_usage); 1486 srv_send(s, (char *)"LIGHT_USAGE,%d", unit->light_usage);
1487 srv_send((char *)"LIGHT_IDX,%d", unit->light_idx); 1487 srv_send(s, (char *)"LIGHT_IDX,%d", unit->light_idx);
1488 srv_send((char *)"DOOR_ADDRESS,%s", unit->door_address); 1488 srv_send(s, (char *)"DOOR_ADDRESS,%s", unit->door_address);
1489 srv_send((char *)"DOOR_STATE,%d", unit->door_state); 1489 srv_send(s, (char *)"DOOR_STATE,%d", unit->door_state);
1490 srv_send((char *)"DOOR_IDX,%d", unit->door_idx); 1490 srv_send(s, (char *)"DOOR_IDX,%d", unit->door_idx);
1491 srv_send((char *)"PSU_ADDRESS,%s", unit->psu_address); 1491 srv_send(s, (char *)"PSU_ADDRESS,%s", unit->psu_address);
1492 srv_send((char *)"PSU_STATE,%d", unit->psu_state); 1492 srv_send(s, (char *)"PSU_STATE,%d", unit->psu_state);
1493 srv_send((char *)"PSU_IDX,%d", unit->psu_idx); 1493 srv_send(s, (char *)"PSU_IDX,%d", unit->psu_idx);
1494 srv_send((char *)"FRIDGE_SET_LO,%.1f", unit->fridge_set_lo); 1494 srv_send(s, (char *)"FRIDGE_SET_LO,%.1f", unit->fridge_set_lo);
1495 srv_send((char *)"FRIDGE_SET_HI,%.1f", unit->fridge_set_hi); 1495 srv_send(s, (char *)"FRIDGE_SET_HI,%.1f", unit->fridge_set_hi);
1496 srv_send((char *)"BEER_SET_LO,%.1f", unit->beer_set_lo); 1496 srv_send(s, (char *)"BEER_SET_LO,%.1f", unit->beer_set_lo);
1497 srv_send((char *)"BEER_SET_HI,%.1f", unit->beer_set_hi); 1497 srv_send(s, (char *)"BEER_SET_HI,%.1f", unit->beer_set_hi);
1498 if (unit->profile_uuid) { 1498 if (unit->profile_uuid) {
1499 srv_send((char *)"PROFILE_UUID,%s", unit->profile_uuid); 1499 srv_send(s, (char *)"PROFILE_UUID,%s", unit->profile_uuid);
1500 srv_send((char *)"PROFILE_NAME,%s", unit->profile_name); 1500 srv_send(s, (char *)"PROFILE_NAME,%s", unit->profile_name);
1501 srv_send((char *)"PROFILE_INITTEMP_LO,%.1f", unit->profile_inittemp_lo); 1501 srv_send(s, (char *)"PROFILE_INITTEMP_LO,%.1f", unit->profile_inittemp_lo);
1502 srv_send((char *)"PROFILE_INITTEMP_HI,%.1f", unit->profile_inittemp_hi); 1502 srv_send(s, (char *)"PROFILE_INITTEMP_HI,%.1f", unit->profile_inittemp_hi);
1503 srv_send((char *)"PROFILE_FRIDGE_MODE,%d", unit->profile_fridge_mode); 1503 srv_send(s, (char *)"PROFILE_FRIDGE_MODE,%d", unit->profile_fridge_mode);
1504 srv_send((char *)"PROFILE_DURATION,%d", unit->profile_duration); 1504 srv_send(s, (char *)"PROFILE_DURATION,%d", unit->profile_duration);
1505 srv_send((char *)"PROFILE_TOTALSTEPS,%d", unit->profile_totalsteps); 1505 srv_send(s, (char *)"PROFILE_TOTALSTEPS,%d", unit->profile_totalsteps);
1506 srv_send((char *)"PROF_STARTED,%d", (int)unit->prof_started); 1506 srv_send(s, (char *)"PROF_STARTED,%d", (int)unit->prof_started);
1507 if (unit->prof_state == PROFILE_RUN) { 1507 if (unit->prof_state == PROFILE_RUN) {
1508 srv_send((char *)"PROF_STATE,%s %d%%", PROFSTATE[unit->prof_state], unit->prof_percent); 1508 srv_send(s, (char *)"PROF_STATE,%s %d%%", PROFSTATE[unit->prof_state], unit->prof_percent);
1509 } else { 1509 } else {
1510 srv_send((char *)"PROF_STATE,%s", PROFSTATE[unit->prof_state]); 1510 srv_send(s, (char *)"PROF_STATE,%s", PROFSTATE[unit->prof_state]);
1511 } 1511 }
1512 srv_send((char *)"PROF_TARGET_LO,%.3f", unit->prof_target_lo); 1512 srv_send(s, (char *)"PROF_TARGET_LO,%.3f", unit->prof_target_lo);
1513 srv_send((char *)"PROF_TARGET_HI,%.3f", unit->prof_target_hi); 1513 srv_send(s, (char *)"PROF_TARGET_HI,%.3f", unit->prof_target_hi);
1514 srv_send((char *)"PROF_FRIDGE_MODE,%d", unit->prof_fridge_mode); 1514 srv_send(s, (char *)"PROF_FRIDGE_MODE,%d", unit->prof_fridge_mode);
1515 srv_send((char *)"PROF_PEAK_ABS,%.3f", unit->prof_peak_abs); 1515 srv_send(s, (char *)"PROF_PEAK_ABS,%.3f", unit->prof_peak_abs);
1516 srv_send((char *)"PROF_PEAK_REL,%.3f", unit->prof_peak_rel); 1516 srv_send(s, (char *)"PROF_PEAK_REL,%.3f", unit->prof_peak_rel);
1517 srv_send((char *)"PROF_PRIMARY_DONE,%d", (int)unit->prof_primary_done); 1517 srv_send(s, (char *)"PROF_PRIMARY_DONE,%d", (int)unit->prof_primary_done);
1518 } 1518 }
1519 srv_send((char *)"TEMP_SET_MIN,%.1f", unit->temp_set_min); 1519 srv_send(s, (char *)"TEMP_SET_MIN,%.1f", unit->temp_set_min);
1520 srv_send((char *)"TEMP_SET_MAX,%.1f", unit->temp_set_max); 1520 srv_send(s, (char *)"TEMP_SET_MAX,%.1f", unit->temp_set_max);
1521 srv_send((char *)"ALARM,%d", unit->alarm_flag); 1521 srv_send(s, (char *)"ALARM,%d", unit->alarm_flag);
1522 srv_send((char *)"."); 1522 srv_send(s, (char *)".");
1523 return 0; 1523 return 0;
1524 } 1524 }
1525 } 1525 }
1526 srv_send((char *)"440 No such unit"); 1526 srv_send(s, (char *)"440 No such unit");
1527 return 0; 1527 return 0;
1528 } 1528 }
1529 1529
1530 if (strcmp(opt, (char *)"PUT") == 0) { 1530 if (strcmp(opt, (char *)"PUT") == 0) {
1531 /* 1531 /*
1540 1540
1541 for (unit = Config.units ; unit; unit = unit->next) { 1541 for (unit = Config.units ; unit; unit = unit->next) {
1542 if (strcmp(unit->uuid, param) == 0) { 1542 if (strcmp(unit->uuid, param) == 0) {
1543 while (1) { 1543 while (1) {
1544 unit->mqtt_flag = 0; 1544 unit->mqtt_flag = 0;
1545 rlen = srv_recv(ibuf); 1545 rlen = srv_recv(s, ibuf);
1546 if (rlen == -1) { 1546 if (rlen == -1) {
1547 run_pause = FALSE; 1547 run_pause = FALSE;
1548 return 0; 1548 return 0;
1549 } 1549 }
1550 if (strlen(ibuf)) { 1550 if (strlen(ibuf)) {
1551 if (strcmp(ibuf, (char *)".") == 0) { 1551 if (strcmp(ibuf, (char *)".") == 0) {
1552 srv_send((char *)"219 Accepted Unit record"); 1552 srv_send(s, (char *)"219 Accepted Unit record");
1553 run_pause = FALSE; 1553 run_pause = FALSE;
1554 return 1; 1554 return 1;
1555 } 1555 }
1556 kwd = strtok(ibuf, ",\0"); 1556 kwd = strtok(ibuf, ",\0");
1557 val = strtok(NULL, "\0"); 1557 val = strtok(NULL, "\0");
2066 } 2066 }
2067 } 2067 }
2068 } 2068 }
2069 } 2069 }
2070 } 2070 }
2071 srv_send((char *)"440 No such unit"); 2071 srv_send(s, (char *)"440 No such unit");
2072 run_pause = FALSE; 2072 run_pause = FALSE;
2073 return 0; 2073 return 0;
2074 } 2074 }
2075 2075
2076 srv_send((char *)"504 Subcommand error"); 2076 srv_send(s, (char *)"504 Subcommand error");
2077 return 0; 2077 return 0;
2078 } 2078 }
2079 2079
2080 2080
2081 2081
2082 void cmd_server(void) 2082 void cmd_server(int s)
2083 { 2083 {
2084 char buf[SS_BUFSIZE]; 2084 char buf[SS_BUFSIZE];
2085 int rlen; 2085 int rlen;
2086 2086
2087 rlen = srv_recv(buf); 2087 rlen = srv_recv(s, buf);
2088 if (rlen != -1) { 2088 if (rlen != -1) {
2089 if (strlen(buf)) { 2089 if (strlen(buf)) {
2090 /* 2090 /*
2091 * Process commands from the client 2091 * Process commands from the client
2092 */ 2092 */
2093 if (strncmp(buf, "DEVICE", 6) == 0) { 2093 if (strncmp(buf, "DEVICE", 6) == 0) {
2094 if (cmd_device(buf)) 2094 if (cmd_device(s, buf))
2095 wrconfig(); 2095 wrconfig();
2096 2096
2097 } else if (strncmp(buf, "GLOBAL", 6) == 0) { 2097 } else if (strncmp(buf, "GLOBAL", 6) == 0) {
2098 if (cmd_global(buf)) 2098 if (cmd_global(s, buf))
2099 wrconfig(); 2099 wrconfig();
2100 2100
2101 } else if (strncmp(buf, "HELP", 4) == 0) { 2101 } else if (strncmp(buf, "HELP", 4) == 0) {
2102 srv_send((char *)"100 Help text follows"); 2102 srv_send(s, (char *)"100 Help text follows");
2103 srv_send((char *)"Recognized commands:"); 2103 srv_send(s, (char *)"Recognized commands:");
2104 srv_send((char *)""); 2104 srv_send(s, (char *)"");
2105 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890 2105 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890
2106 srv_send((char *)"DEVICE <CMD> [parameters] Device commands"); 2106 srv_send(s, (char *)"DEVICE <CMD> [parameters] Device commands");
2107 srv_send((char *)"DEVICE HELP Device help screen"); 2107 srv_send(s, (char *)"DEVICE HELP Device help screen");
2108 srv_send((char *)"GLOBAL <CMD> [parameters] Global commands"); 2108 srv_send(s, (char *)"GLOBAL <CMD> [parameters] Global commands");
2109 srv_send((char *)"GLOBAL HELP Global help screen"); 2109 srv_send(s, (char *)"GLOBAL HELP Global help screen");
2110 srv_send((char *)"LIST <CMD> [parameters] List commands"); 2110 srv_send(s, (char *)"LIST <CMD> [parameters] List commands");
2111 srv_send((char *)"LIST HELP List help screen"); 2111 srv_send(s, (char *)"LIST HELP List help screen");
2112 srv_send((char *)"PING Check if server is alive"); 2112 srv_send(s, (char *)"PING Check if server is alive");
2113 #ifdef USE_SIMULATOR 2113 #ifdef USE_SIMULATOR
2114 srv_send((char *)"SIMULATOR <CMD> [parameters] Simulator commands"); 2114 srv_send(s, (char *)"SIMULATOR <CMD> [parameters] Simulator commands");
2115 srv_send((char *)"SIMULATOR HELP Simulator help screen"); 2115 srv_send(s, (char *)"SIMULATOR HELP Simulator help screen");
2116 #endif 2116 #endif
2117 srv_send((char *)"UNIT <CMD> [parameters] Unit commands"); 2117 srv_send(s, (char *)"UNIT <CMD> [parameters] Unit commands");
2118 srv_send((char *)"UNIT HELP Unit help screen"); 2118 srv_send(s, (char *)"UNIT HELP Unit help screen");
2119 srv_send((char *)"."); 2119 srv_send(s, (char *)".");
2120 2120
2121 } else if (strncmp(buf, "LIST", 4) == 0) { 2121 } else if (strncmp(buf, "LIST", 4) == 0) {
2122 cmd_list(buf); 2122 cmd_list(s, buf);
2123 2123
2124 } else if (strncmp(buf, "PING", 4) == 0) { 2124 } else if (strncmp(buf, "PING", 4) == 0) {
2125 srv_send((char *)"101 PONG"); 2125 srv_send(s, (char *)"101 PONG");
2126 2126
2127 #ifdef USE_SIMULATOR 2127 #ifdef USE_SIMULATOR
2128 } else if (strncmp(buf, "SIMULATOR", 9) == 0) { 2128 } else if (strncmp(buf, "SIMULATOR", 9) == 0) {
2129 if (cmd_simulator(buf)) 2129 if (cmd_simulator(s, buf))
2130 wrconfig(); 2130 wrconfig();
2131 2131
2132 #endif 2132 #endif
2133 } else if (strncmp(buf, "UNIT", 4) == 0) { 2133 } else if (strncmp(buf, "UNIT", 4) == 0) {
2134 if (cmd_unit(buf)) 2134 if (cmd_unit(s, buf))
2135 wrconfig(); 2135 wrconfig();
2136 2136
2137 } else { 2137 } else {
2138 srv_send((char *)"500 Unknown command"); 2138 srv_send(s, (char *)"500 Unknown command");
2139 } 2139 }
2140 } 2140 }
2141 } 2141 }
2142 2142
2143 close(s); 2143 close(s);
2144 } 2144 }
2145 2145
2146
2147 static void cleanup_handler(void *arg)
2148 {
2149 syslog(LOG_NOTICE, "Thread my_server_loop stopped (cleanup_handler)");
2150 close(ls);
2151 my_server_state = 0;
2152 }
2146 2153
2147 2154
2148 void *my_server_loop(void *threadid) 2155 void *my_server_loop(void *threadid)
2149 { 2156 {
2150 socklen_t addrlen; 2157 socklen_t addrlen;
2151 int optval = 1; 2158 int s, optval = 1;
2152 2159
2153 my_server_state = 1; 2160 my_server_state = 1;
2154 syslog(LOG_NOTICE, "Thread my_server_loop started"); 2161 syslog(LOG_NOTICE, "Thread my_server_loop started");
2155 if (debug) 2162 if (debug)
2156 fprintf(stdout, "Thread my_server_loop started\n"); 2163 fprintf(stdout, "Thread my_server_loop started\n");
2164
2165 /*
2166 * Prepare thread to stop in blocking accept() call.
2167 */
2168 pthread_cleanup_push(cleanup_handler, NULL);
2169 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
2170 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
2157 2171
2158 memset((char *)&myaddr_in, 0, sizeof(struct sockaddr_in)); 2172 memset((char *)&myaddr_in, 0, sizeof(struct sockaddr_in));
2159 memset((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in)); 2173 memset((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in));
2160 myaddr_in.sin_family = AF_INET; 2174 myaddr_in.sin_family = AF_INET;
2161 myaddr_in.sin_addr.s_addr = INADDR_ANY; 2175 myaddr_in.sin_addr.s_addr = INADDR_ANY;
2174 close(ls); 2188 close(ls);
2175 my_server_state = 0; 2189 my_server_state = 0;
2176 return 0; 2190 return 0;
2177 } 2191 }
2178 2192
2179 if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) { 2193 // if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) {
2180 syslog(LOG_NOTICE, "Can't setsockopt SO_REUSEADDR socket: %s", strerror(errno)); 2194 // syslog(LOG_NOTICE, "Can't setsockopt SO_REUSEADDR socket: %s", strerror(errno));
2181 close(ls); 2195 // close(ls);
2182 my_server_state = 0; 2196 // my_server_state = 0;
2183 return 0; 2197 // return 0;
2184 } 2198 // }
2185 2199
2186 if (bind(ls, (struct sockaddr *)&myaddr_in, sizeof(struct sockaddr_in)) == -1) { 2200 if (bind(ls, (struct sockaddr *)&myaddr_in, sizeof(struct sockaddr_in)) == -1) {
2187 syslog(LOG_NOTICE, "Can't bind to listen socket: %s", strerror(errno)); 2201 syslog(LOG_NOTICE, "Can't bind to listen socket: %s", strerror(errno));
2188 close(ls); 2202 close(ls);
2189 my_server_state = 0; 2203 my_server_state = 0;
2196 my_server_state = 0; 2210 my_server_state = 0;
2197 return 0; 2211 return 0;
2198 } 2212 }
2199 2213
2200 syslog(LOG_NOTICE, "listen socket created %d", ls); 2214 syslog(LOG_NOTICE, "listen socket created %d", ls);
2201
2202 2215
2203 /* 2216 /*
2204 * Loop forever until the external shutdown variable is set. 2217 * Loop forever until the external shutdown variable is set.
2205 */ 2218 */
2206 for (;;) { 2219 for (;;) {
2211 * arrives. Then it will return the address of 2224 * arrives. Then it will return the address of
2212 * the connecting peer, and a new socket 2225 * the connecting peer, and a new socket
2213 * descriptor, s, for that connection. 2226 * descriptor, s, for that connection.
2214 */ 2227 */
2215 s = accept(ls, (struct sockaddr *)&peeraddr_in, &addrlen); 2228 s = accept(ls, (struct sockaddr *)&peeraddr_in, &addrlen);
2229 syslog(LOG_NOTICE, "my_server_loop accept socket %d", s);
2216 if (s == -1) { 2230 if (s == -1) {
2217 syslog(LOG_NOTICE, "my_server_loop accept failed %s", strerror(errno)); 2231 syslog(LOG_NOTICE, "my_server_loop accept failed %s", strerror(errno));
2218 if (debug) 2232 break;
2219 fprintf(stdout, "my_server_loop accept failed %s\n", strerror(errno)); 2233 }
2220 my_server_state = 0; 2234 if (my_server_shutdown)
2221 return 0; 2235 break;
2222 } 2236
2223 2237 cmd_server(s);
2224 cmd_server(); 2238
2225 2239 if (my_server_shutdown)
2226 if (my_shutdown) { 2240 break;
2227 syslog(LOG_NOTICE, "Thread my_server_loop stopped");
2228 if (debug)
2229 fprintf(stdout, "Thread my_server_loop stopped\n");
2230 my_server_state = 0;
2231 return 0;
2232 }
2233 mDelay(100); 2241 mDelay(100);
2234 } 2242 }
2243
2244 close(ls);
2245 pthread_cleanup_pop(my_server_state);
2246 syslog(LOG_NOTICE, "Thread my_server_loop stopped");
2247 my_server_state = 0;
2248 return 0;
2235 } 2249 }
2236 2250
2237 2251

mercurial