# HG changeset patch # User Michiel Broek # Date 1713603198 -7200 # Node ID 3a5b019e9acce8566313acf6b5f3728687ca9796 # Parent f94b525f7563eecabca8b8d814dd76f3c0db8856 Better handling of a removed DS18B20 temperature sensor. diff -r f94b525f7563 -r 3a5b019e9acc thermferm/one-wire.c --- a/thermferm/one-wire.c Fri Apr 19 20:56:55 2024 +0200 +++ b/thermferm/one-wire.c Sat Apr 20 10:53:18 2024 +0200 @@ -295,15 +295,17 @@ */ if (cur_w1 != NULL) { - if ((strcmp(cur_w1->family, (char *)"10") == 0) || (strcmp(cur_w1->family, (char *)"22") == 0) || (strcmp(cur_w1->family, (char *)"28") == 0) || - (strcmp(cur_w1->family, (char *)"3b") == 0) || (strcmp(cur_w1->family, (char *)"42") == 0)) { + if (((strcmp(cur_w1->family, (char *)"10") == 0) || (strcmp(cur_w1->family, (char *)"22") == 0) || (strcmp(cur_w1->family, (char *)"28") == 0) || + (strcmp(cur_w1->family, (char *)"3b") == 0) || (strcmp(cur_w1->family, (char *)"42") == 0)) && + (cur_w1->present == DEVPRESENT_YES)) { devfile = xstrcpy((char *)"/sys/bus/w1/devices/"); devfile = xstrcat(devfile, cur_w1->address); devfile = xstrcat(devfile, (char *)"/resolution"); if ((fp = fopen(devfile, "r+"))) { if ((fgets(buffer, 25, fp))) { sscanf(buffer, "%d", &value); - if (value != W1_TEMP_RESOLUTION) { + /* If device is removed, value is negative (errno?) */ + if ((value > 0) && (value != W1_TEMP_RESOLUTION)) { syslog(LOG_NOTICE, "One-wire device %s set resolution from %d to %d", cur_w1->address, value, W1_TEMP_RESOLUTION); fseek(fp, 0L, SEEK_SET); sprintf(buffer, "%d", W1_TEMP_RESOLUTION); @@ -347,7 +349,7 @@ } free(devfile); devfile = NULL; - } + } /* if temperature sensor and present */ for (;;) { if (cur_w1->next != NULL) {