thermferm/devices.c

changeset 395
e32e83550963
parent 394
14231379bec2
child 408
ec507c1f1df7
equal deleted inserted replaced
394:14231379bec2 395:e32e83550963
234 234
235 while (retries--) { 235 while (retries--) {
236 if ((write(fn, &val, 1)) == 1) { 236 if ((write(fn, &val, 1)) == 1) {
237 rc = 0; 237 rc = 0;
238 goto leave; 238 goto leave;
239 // } else {
240 // syslog(LOG_NOTICE, "write_w1() write %s try=%d: %s", addr, retries, strerror(errno));
241 } 239 }
242 } 240 }
243 syslog(LOG_NOTICE, "write_w1() write %s fatal: %s", addr, strerror(errno)); 241 syslog(LOG_NOTICE, "write_w1() write %s fatal: %s", addr, strerror(errno));
244 242
245 } else { 243 } else {
391 return 0; 389 return 0;
392 } 390 }
393 391
394 392
395 /* 393 /*
396 * Returns 0 if failed. 394 * Returns DEVPRESENT_NO if failed.
397 * Returns 1 if success, value contains new value. 395 * Returns DEVPRESENT_YES if success, value contains new value.
398 */ 396 */
399 int device_in(char *uuid, int *value) 397 int device_in(char *uuid, int *value)
400 { 398 {
401 devices_list *device; 399 devices_list *device;
400 int tmp, present;
402 401
403 if (uuid == NULL) 402 if (uuid == NULL)
404 return 0; 403 return 0;
405 404
406 #ifdef HAVE_WIRINGPI_H 405 #ifdef HAVE_WIRINGPI_H
407 piLock(LOCK_DEVICES); 406 piLock(LOCK_DEVICES);
408 #endif 407 #endif
409 408
410 for (device = Config.devices; device; device = device->next) { 409 for (device = Config.devices; device; device = device->next) {
411 if (! strcmp(uuid, device->uuid)) { 410 if (! strcmp(uuid, device->uuid)) {
412 411 present = device->present;
412 if (present == DEVPRESENT_YES) {
413 tmp = device->value + device->offset;
414 } else {
415 tmp = 0;
416 }
417 #ifdef HAVE_WIRINGPI_H
418 piUnlock(LOCK_DEVICES);
419 #endif
420 *value = tmp;
421 return present;
413 } 422 }
414 } 423 }
415 424
416 #ifdef HAVE_WIRINGPI_H 425 #ifdef HAVE_WIRINGPI_H
417 piUnlock(LOCK_DEVICES); 426 piUnlock(LOCK_DEVICES);
418 #endif 427 #endif
419 428
420 return 0; 429 return DEVPRESENT_NO;
421 } 430 }
422 431
423 432
424 433
425 434
478 ndev->direction = DEVDIR_IN_ANALOG; 487 ndev->direction = DEVDIR_IN_ANALOG;
479 ndev->description = xstrcpy((char *)"DS1820 Digital thermometer"); 488 ndev->description = xstrcpy((char *)"DS1820 Digital thermometer");
480 } else if (strcmp(buf, (char *)"28") == 0) { 489 } else if (strcmp(buf, (char *)"28") == 0) {
481 ndev->direction = DEVDIR_IN_ANALOG; 490 ndev->direction = DEVDIR_IN_ANALOG;
482 ndev->description = xstrcpy((char *)"DS18B20 Digital thermometer"); 491 ndev->description = xstrcpy((char *)"DS18B20 Digital thermometer");
483 } else if (strcmp(buf, (char *)"29") == 0) {
484 ndev->description = xstrcpy((char *)"DS2408 8 Channel addressable switch/LCD");
485 ndev->direction = DEVDIR_IN_BIN;
486 } else if (strcmp(buf, (char *)"3a") == 0) { 492 } else if (strcmp(buf, (char *)"3a") == 0) {
487 ndev->description = xstrcpy((char *)"DS2413 Dual channel addressable switch"); 493 ndev->description = xstrcpy((char *)"DS2413 Dual channel addressable switch");
488 ndev->direction = DEVDIR_IN_BIN; 494 ndev->direction = DEVDIR_IN_BIN;
489 } else if (strcmp(buf, (char *)"3b") == 0) { 495 } else if (strcmp(buf, (char *)"3b") == 0) {
490 ndev->direction = DEVDIR_IN_ANALOG; 496 ndev->direction = DEVDIR_IN_ANALOG;
794 } 800 }
795 free(addr); 801 free(addr);
796 addr = NULL; 802 addr = NULL;
797 } /* if temperature sensor */ 803 } /* if temperature sensor */
798 /* 804 /*
799 * DS2408 8 Channel addressable switch
800 */
801 if (strncmp(device->address, (char *)"29", 2) == 0) {
802 /*
803 * Always read current state and set these bits
804 * in the subdevices value. We do this even with
805 * the output bits, they should match.
806 */
807 addr = xstrcpy((char *)"/sys/bus/w1/devices/");
808 addr = xstrcat(addr, device->address);
809 addr = xstrcat(addr, (char *)"/state");
810 if ((fp = fopen(addr, "r"))) {
811 if (device->present != DEVPRESENT_YES) {
812 syslog(LOG_NOTICE, "DS2408 %s is back", device->address);
813 #ifdef HAVE_WIRINGPI_H
814 piLock(LOCK_DEVICES);
815 #endif
816 device->present = DEVPRESENT_YES;
817 #ifdef HAVE_WIRINGPI_H
818 piUnlock(LOCK_DEVICES);
819 #endif
820 }
821
822 fclose(fp);
823 } else {
824 if (device->present != DEVPRESENT_NO) {
825 syslog(LOG_NOTICE, "DS2408 %s is missing", device->address);
826 #ifdef HAVE_WIRINGPI_H
827 piLock(LOCK_DEVICES);
828 #endif
829 device->present = DEVPRESENT_NO;
830 #ifdef HAVE_WIRINGPI_H
831 piUnlock(LOCK_DEVICES);
832 #endif
833 }
834 }
835 free(addr);
836 addr = NULL;
837 }
838 /*
839 * DS2413 Dual channel addressable switch 805 * DS2413 Dual channel addressable switch
840 */ 806 */
841 if (strncmp(device->address, (char *)"3a", 2) == 0) { 807 if (strncmp(device->address, (char *)"3a", 2) == 0) {
842 addr = xstrcpy((char *)"/sys/bus/w1/devices/"); 808 addr = xstrcpy((char *)"/sys/bus/w1/devices/");
843 addr = xstrcat(addr, device->address); 809 addr = xstrcat(addr, device->address);

mercurial