thermferm/server.c

changeset 645
49eb753a958b
parent 614
389097dc665d
child 646
e3edc783006b
equal deleted inserted replaced
644:07cc86900473 645:49eb753a958b
1 /***************************************************************************** 1 /*****************************************************************************
2 * Copyright (C) 2008-2022 2 * Copyright (C) 2008-2024
3 * 3 *
4 * Michiel Broek <mbroek at mbse dot eu> 4 * Michiel Broek <mbroek at mbse dot eu>
5 * 5 *
6 * This file is part of the mbsePi-apps 6 * This file is part of the mbsePi-apps
7 * 7 *
39 extern const char TEMPSTATE[3][8]; 39 extern const char TEMPSTATE[3][8];
40 extern const char DEVTYPE[8][6]; 40 extern const char DEVTYPE[8][6];
41 extern const char DEVPRESENT[4][6]; 41 extern const char DEVPRESENT[4][6];
42 extern const char DEVDIR[7][11]; 42 extern const char DEVDIR[7][11];
43 extern const char PROFSTATE[5][6]; 43 extern const char PROFSTATE[5][6];
44 extern pthread_mutex_t mutexes[5];
44 45
45 46
46 int s; /* connected socket */ 47 int s; /* connected socket */
47 int ls; /* listen socket */ 48 int ls; /* listen socket */
48 49
164 { 165 {
165 devices_list *device; 166 devices_list *device;
166 167
167 for (device = Config.devices; device; device = device->next) { 168 for (device = Config.devices; device; device = device->next) {
168 if (strcmp(device->uuid, uuid) == 0) { 169 if (strcmp(device->uuid, uuid) == 0) {
169 #ifdef HAVE_WIRINGPI_H 170 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
170 piLock(LOCK_DEVICES);
171 #endif
172 if (plus == TRUE) { 171 if (plus == TRUE) {
173 device->inuse++; 172 device->inuse++;
174 } else { 173 } else {
175 if (device->inuse) 174 if (device->inuse)
176 device->inuse--; 175 device->inuse--;
177 } 176 }
178 #ifdef HAVE_WIRINGPI_H 177 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
179 piUnlock(LOCK_DEVICES);
180 #endif
181 } 178 }
182 } 179 }
183 } 180 }
184 181
185 182
369 device->address = xstrcpy((char *)"Enter address here"); 366 device->address = xstrcpy((char *)"Enter address here");
370 device->gpiopin = -1; 367 device->gpiopin = -1;
371 device->description = xstrcpy((char *)"Describe me here"); 368 device->description = xstrcpy((char *)"Describe me here");
372 device->comment = xstrcpy((char *)"Comment here"); 369 device->comment = xstrcpy((char *)"Comment here");
373 370
374 #ifdef HAVE_WIRINGPI_H 371 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
375 piLock(LOCK_DEVICES);
376 #endif
377 if (Config.devices == NULL) { 372 if (Config.devices == NULL) {
378 Config.devices = device; 373 Config.devices = device;
379 } else { 374 } else {
380 for (tmpd = Config.devices; tmpd; tmpd = tmpd->next) { 375 for (tmpd = Config.devices; tmpd; tmpd = tmpd->next) {
381 if (tmpd->next == NULL) { 376 if (tmpd->next == NULL) {
382 tmpd->next = device; 377 tmpd->next = device;
383 break; 378 break;
384 } 379 }
385 } 380 }
386 } 381 }
387 #ifdef HAVE_WIRINGPI_H 382 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
388 piUnlock(LOCK_DEVICES);
389 #endif
390 syslog(LOG_NOTICE, "Device %s added", device->uuid); 383 syslog(LOG_NOTICE, "Device %s added", device->uuid);
391 srv_send((char *)"211 Device %s added", device->uuid); 384 srv_send((char *)"211 Device %s added", device->uuid);
392 return 1; 385 return 1;
393 386
394 } else { 387 } else {
397 } 390 }
398 } 391 }
399 392
400 if (strcmp(opt, (char *)"DEL") == 0) { 393 if (strcmp(opt, (char *)"DEL") == 0) {
401 394
402 #ifdef HAVE_WIRINGPI_H 395 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
403 piLock(LOCK_DEVICES);
404 #endif
405 rc = delete_Device(param); 396 rc = delete_Device(param);
406 #ifdef HAVE_WIRINGPI_H 397 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
407 piUnlock(LOCK_DEVICES);
408 #endif
409 if (rc) { 398 if (rc) {
410 syslog(LOG_NOTICE, "Device %s deleted", param); 399 syslog(LOG_NOTICE, "Device %s deleted", param);
411 srv_send((char *)"211 Device %s deleted", param); 400 srv_send((char *)"211 Device %s deleted", param);
412 return 1; 401 return 1;
413 } else { 402 } else {
417 } 406 }
418 407
419 if (strcmp(opt, (char *)"GET") == 0) { 408 if (strcmp(opt, (char *)"GET") == 0) {
420 for (device = Config.devices; device; device = device->next) { 409 for (device = Config.devices; device; device = device->next) {
421 if (strcmp(device->uuid, param) == 0) { 410 if (strcmp(device->uuid, param) == 0) {
422 #ifdef HAVE_WIRINGPI_H 411 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
423 piLock(LOCK_DEVICES);
424 #endif
425 int my_value = device->value; 412 int my_value = device->value;
426 int my_timestamp = (int)device->timestamp; 413 int my_timestamp = (int)device->timestamp;
427 #ifdef HAVE_WIRINGPI_H 414 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
428 piUnlock(LOCK_DEVICES);
429 #endif
430 srv_send((char *)"213 Device record follows:"); 415 srv_send((char *)"213 Device record follows:");
431 srv_send((char *)"TYPE,%s", DEVTYPE[device->type]); 416 srv_send((char *)"TYPE,%s", DEVTYPE[device->type]);
432 srv_send((char *)"ADDRESS,%s", device->address); 417 srv_send((char *)"ADDRESS,%s", device->address);
433 srv_send((char *)"DIRECTION,%s", DEVDIR[device->direction]); 418 srv_send((char *)"DIRECTION,%s", DEVDIR[device->direction]);
434 srv_send((char *)"VALUE,%d", my_value); 419 srv_send((char *)"VALUE,%d", my_value);
465 val = strtok(NULL, "\0"); 450 val = strtok(NULL, "\0");
466 if (kwd && val) { 451 if (kwd && val) {
467 if (strcmp(kwd, (char *)"TYPE") == 0) { 452 if (strcmp(kwd, (char *)"TYPE") == 0) {
468 for (i = 0; i < 8; i++) { 453 for (i = 0; i < 8; i++) {
469 if (strcmp(val, DEVTYPE[i]) == 0) { 454 if (strcmp(val, DEVTYPE[i]) == 0) {
470 #ifdef HAVE_WIRINGPI_H 455 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
471 piLock(LOCK_DEVICES);
472 #endif
473 if (device->type != i) 456 if (device->type != i)
474 syslog(LOG_NOTICE, "Device %s changed type %s to %s", device->uuid, DEVTYPE[device->type], DEVTYPE[i]); 457 syslog(LOG_NOTICE, "Device %s changed type %s to %s", device->uuid, DEVTYPE[device->type], DEVTYPE[i]);
475 device->type = i; 458 device->type = i;
476 #ifdef HAVE_WIRINGPI_H 459 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
477 piUnlock(LOCK_DEVICES);
478 #endif
479 break; 460 break;
480 } 461 }
481 } 462 }
482 463
483 } else if (strcmp(kwd, (char *)"DIRECTION") == 0) { 464 } else if (strcmp(kwd, (char *)"DIRECTION") == 0) {
484 for (i = 0; i < 7; i++) { 465 for (i = 0; i < 7; i++) {
485 if (strcmp(val, DEVDIR[i]) == 0) { 466 if (strcmp(val, DEVDIR[i]) == 0) {
486 #ifdef HAVE_WIRINGPI_H 467 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
487 piLock(LOCK_DEVICES);
488 #endif
489 if (device->direction != i) 468 if (device->direction != i)
490 syslog(LOG_NOTICE, "Device %s changed direction %s to %s", device->uuid, DEVDIR[device->type], DEVDIR[i]); 469 syslog(LOG_NOTICE, "Device %s changed direction %s to %s", device->uuid, DEVDIR[device->type], DEVDIR[i]);
491 device->direction = i; 470 device->direction = i;
492 #ifdef HAVE_WIRINGPI_H 471 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
493 piUnlock(LOCK_DEVICES);
494 #endif
495 break; 472 break;
496 } 473 }
497 } 474 }
498 475
499 } else if (strcmp(kwd, (char *)"VALUE") == 0) { 476 } else if (strcmp(kwd, (char *)"VALUE") == 0) {
500 if (sscanf(val, "%d", &ival) == 1) { 477 if (sscanf(val, "%d", &ival) == 1) {
501 #ifdef HAVE_WIRINGPI_H 478 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
502 piLock(LOCK_DEVICES);
503 #endif
504 if (device->value != ival) 479 if (device->value != ival)
505 syslog(LOG_NOTICE, "Device %s changed value %d to %d", device->uuid, device->value, ival); 480 syslog(LOG_NOTICE, "Device %s changed value %d to %d", device->uuid, device->value, ival);
506 device->value = ival; 481 device->value = ival;
507 #ifdef HAVE_WIRINGPI_H 482 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
508 piUnlock(LOCK_DEVICES);
509 #endif
510 } 483 }
511 484
512 } else if (strcmp(kwd, (char *)"OFFSET") == 0) { 485 } else if (strcmp(kwd, (char *)"OFFSET") == 0) {
513 if (sscanf(val, "%d", &ival) == 1) { 486 if (sscanf(val, "%d", &ival) == 1) {
514 #ifdef HAVE_WIRINGPI_H 487 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
515 piLock(LOCK_DEVICES);
516 #endif
517 if (device->offset != ival) 488 if (device->offset != ival)
518 syslog(LOG_NOTICE, "Device %s changed offset %d to %d", device->uuid, device->offset, ival); 489 syslog(LOG_NOTICE, "Device %s changed offset %d to %d", device->uuid, device->offset, ival);
519 device->offset = ival; 490 device->offset = ival;
520 #ifdef HAVE_WIRINGPI_H 491 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
521 piUnlock(LOCK_DEVICES);
522 #endif
523 } 492 }
524 493
525 } else if (strcmp(kwd, (char *)"PRESENT") == 0) { 494 } else if (strcmp(kwd, (char *)"PRESENT") == 0) {
526 for (i = 0; i < 4; i++) { 495 for (i = 0; i < 4; i++) {
527 if (strcmp(val, DEVPRESENT[i]) == 0) { 496 if (strcmp(val, DEVPRESENT[i]) == 0) {
528 #ifdef HAVE_WIRINGPI_H 497 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
529 piLock(LOCK_DEVICES);
530 #endif
531 if (device->present != i) 498 if (device->present != i)
532 syslog(LOG_NOTICE, "Device %s changed present %s to %s", device->uuid, DEVPRESENT[device->present], DEVPRESENT[i]); 499 syslog(LOG_NOTICE, "Device %s changed present %s to %s", device->uuid, DEVPRESENT[device->present], DEVPRESENT[i]);
533 device->present = i; 500 device->present = i;
534 #ifdef HAVE_WIRINGPI_H 501 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
535 piUnlock(LOCK_DEVICES);
536 #endif
537 break; 502 break;
538 } 503 }
539 } 504 }
540 505
541 } else if (strcmp(kwd, (char *)"ADDRESS") == 0) { 506 } else if (strcmp(kwd, (char *)"ADDRESS") == 0) {
542 #ifdef HAVE_WIRINGPI_H 507 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
543 piLock(LOCK_DEVICES);
544 #endif
545 if (device->address) { 508 if (device->address) {
546 if (strcmp(device->address, val)) 509 if (strcmp(device->address, val))
547 syslog(LOG_NOTICE, "Device %s changed address `%s' to `%s'", device->uuid, device->address, val); 510 syslog(LOG_NOTICE, "Device %s changed address `%s' to `%s'", device->uuid, device->address, val);
548 free(device->address); 511 free(device->address);
549 } 512 }
550 device->address = xstrcpy(val); 513 device->address = xstrcpy(val);
551 #ifdef HAVE_WIRINGPI_H 514 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
552 piUnlock(LOCK_DEVICES);
553 #endif
554 515
555 } else if (strcmp(kwd, (char *)"SUBDEVICE") == 0) { 516 } else if (strcmp(kwd, (char *)"SUBDEVICE") == 0) {
556 if (sscanf(val, "%d", &ival) == 1) { 517 if (sscanf(val, "%d", &ival) == 1) {
557 #ifdef HAVE_WIRINGPI_H 518 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
558 piLock(LOCK_DEVICES);
559 #endif
560 if (device->subdevice != ival) 519 if (device->subdevice != ival)
561 syslog(LOG_NOTICE, "Device %s changed subdevice %d to %d", device->uuid, device->subdevice, ival); 520 syslog(LOG_NOTICE, "Device %s changed subdevice %d to %d", device->uuid, device->subdevice, ival);
562 device->subdevice = ival; 521 device->subdevice = ival;
563 #ifdef HAVE_WIRINGPI_H 522 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
564 piUnlock(LOCK_DEVICES);
565 #endif
566 } 523 }
567 524
568 } else if (strcmp(kwd, (char *)"GPIOPIN") == 0) { 525 } else if (strcmp(kwd, (char *)"GPIOPIN") == 0) {
569 if (sscanf(val, "%d", &ival) == 1) { 526 if (sscanf(val, "%d", &ival) == 1) {
570 #ifdef HAVE_WIRINGPI_H 527 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
571 piLock(LOCK_DEVICES);
572 #endif
573 if (device->gpiopin != ival) 528 if (device->gpiopin != ival)
574 syslog(LOG_NOTICE, "Device %s changed gpiopin %d to %d", device->uuid, device->gpiopin, ival); 529 syslog(LOG_NOTICE, "Device %s changed gpiopin %d to %d", device->uuid, device->gpiopin, ival);
575 device->gpiopin = ival; 530 device->gpiopin = ival;
576 #ifdef HAVE_WIRINGPI_H 531 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
577 piUnlock(LOCK_DEVICES);
578 #endif
579 } 532 }
580 533
581 } else if (strcmp(kwd, (char *)"DESCRIPTION") == 0) { 534 } else if (strcmp(kwd, (char *)"DESCRIPTION") == 0) {
582 #ifdef HAVE_WIRINGPI_H 535 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
583 piLock(LOCK_DEVICES);
584 #endif
585 if (device->description) { 536 if (device->description) {
586 if (strcmp(device->description, val)) 537 if (strcmp(device->description, val))
587 syslog(LOG_NOTICE, "Device %s changed description `%s' to `%s'", device->uuid, device->description, val); 538 syslog(LOG_NOTICE, "Device %s changed description `%s' to `%s'", device->uuid, device->description, val);
588 free(device->description); 539 free(device->description);
589 } 540 }
590 device->description = xstrcpy(val); 541 device->description = xstrcpy(val);
591 #ifdef HAVE_WIRINGPI_H 542 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
592 piUnlock(LOCK_DEVICES);
593 #endif
594 543
595 } else if (strcmp(kwd, (char *)"COMMENT") == 0) { 544 } else if (strcmp(kwd, (char *)"COMMENT") == 0) {
596 #ifdef HAVE_WIRINGPI_H 545 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
597 piLock(LOCK_DEVICES);
598 #endif
599 if (device->comment) { 546 if (device->comment) {
600 if (strcmp(device->comment, val)) 547 if (strcmp(device->comment, val))
601 syslog(LOG_NOTICE, "Device %s changed comment `%s' to `%s'", device->uuid, device->comment, val); 548 syslog(LOG_NOTICE, "Device %s changed comment `%s' to `%s'", device->uuid, device->comment, val);
602 free(device->comment); 549 free(device->comment);
603 } 550 }
604 device->comment = xstrcpy(val); 551 device->comment = xstrcpy(val);
605 #ifdef HAVE_WIRINGPI_H 552 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
606 piUnlock(LOCK_DEVICES);
607 #endif
608 553
609 } 554 }
610 } 555 }
611 } 556 }
612 } 557 }

mercurial