60 payload = xstrcat(payload, (char *)"\",\"present\":\""); |
60 payload = xstrcat(payload, (char *)"\",\"present\":\""); |
61 payload = xstrcat(payload, (char *)DEVPRESENT[dev_w1->present]); |
61 payload = xstrcat(payload, (char *)DEVPRESENT[dev_w1->present]); |
62 payload = xstrcat(payload, (char *)"\",\"value\":"); |
62 payload = xstrcat(payload, (char *)"\",\"value\":"); |
63 snprintf(vbuf, 63, "%d", dev_w1->value); |
63 snprintf(vbuf, 63, "%d", dev_w1->value); |
64 payload = xstrcat(payload, vbuf); |
64 payload = xstrcat(payload, vbuf); |
|
65 payload = xstrcat(payload, (char *)",\"resolution\":"); |
|
66 snprintf(vbuf, 63, "%d", dev_w1->resolution); |
|
67 payload = xstrcat(payload, vbuf); |
65 payload = xstrcat(payload, (char *)",\"timestamp\":"); |
68 payload = xstrcat(payload, (char *)",\"timestamp\":"); |
66 snprintf(vbuf, 63, "%ld", (long)dev_w1->timestamp); |
69 snprintf(vbuf, 63, "%ld", (long)dev_w1->timestamp); |
67 payload = xstrcat(payload, vbuf); |
70 payload = xstrcat(payload, vbuf); |
68 payload = xstrcat(payload, (char *)"}"); |
71 payload = xstrcat(payload, (char *)"}"); |
69 |
72 |
350 * cur_w1 points to the next not handled device. |
357 * cur_w1 points to the next not handled device. |
351 */ |
358 */ |
352 if (cur_w1 != NULL) { |
359 if (cur_w1 != NULL) { |
353 |
360 |
354 if (((strcmp(cur_w1->family, (char *)"10") == 0) || (strcmp(cur_w1->family, (char *)"22") == 0) || (strcmp(cur_w1->family, (char *)"28") == 0) || |
361 if (((strcmp(cur_w1->family, (char *)"10") == 0) || (strcmp(cur_w1->family, (char *)"22") == 0) || (strcmp(cur_w1->family, (char *)"28") == 0) || |
355 (strcmp(cur_w1->family, (char *)"3b") == 0) || (strcmp(cur_w1->family, (char *)"42") == 0)) && |
362 (strcmp(cur_w1->family, (char *)"3b") == 0) || (strcmp(cur_w1->family, (char *)"42") == 0)) && (cur_w1->present == DEVPRESENT_YES)) { |
356 (cur_w1->present == DEVPRESENT_YES)) { |
363 |
|
364 /* |
|
365 * Check and correct resolution. Only do this for new sensors |
|
366 * or known sensors that are plugged in again. |
|
367 */ |
|
368 if (cur_w1->resolution != W1_TEMP_RESOLUTION) { |
|
369 devfile = xstrcpy((char *)"/sys/bus/w1/devices/"); |
|
370 devfile = xstrcat(devfile, cur_w1->address); |
|
371 devfile = xstrcat(devfile, (char *)"/resolution"); |
|
372 if ((fp = fopen(devfile, "r+"))) { |
|
373 if ((fgets(buffer, 25, fp))) { |
|
374 sscanf(buffer, "%d", &value); |
|
375 /* If device is removed, value is negative (errno?) */ |
|
376 if ((value > 0) && (value != W1_TEMP_RESOLUTION)) { |
|
377 syslog(LOG_NOTICE, "One-wire device %s set resolution from %d to %d", cur_w1->address, value, W1_TEMP_RESOLUTION); |
|
378 fseek(fp, 0L, SEEK_SET); |
|
379 sprintf(buffer, "%d", W1_TEMP_RESOLUTION); |
|
380 fputs(buffer, fp); |
|
381 } |
|
382 cur_w1->resolution = W1_TEMP_RESOLUTION; |
|
383 } |
|
384 fclose(fp); |
|
385 } else { |
|
386 syslog(LOG_NOTICE, "One-wire device %s open: %s", cur_w1->address, strerror(errno)); |
|
387 } |
|
388 free(devfile); |
|
389 } |
|
390 |
|
391 /* |
|
392 * Set conversion wait time. Skip this?? |
|
393 */ |
|
394 conv_time = 760; |
357 devfile = xstrcpy((char *)"/sys/bus/w1/devices/"); |
395 devfile = xstrcpy((char *)"/sys/bus/w1/devices/"); |
358 devfile = xstrcat(devfile, cur_w1->address); |
396 devfile = xstrcat(devfile, cur_w1->address); |
359 devfile = xstrcat(devfile, (char *)"/resolution"); |
397 devfile = xstrcat(devfile, (char *)"/conv_time"); |
360 if ((fp = fopen(devfile, "r+"))) { |
398 if ((fp = fopen(devfile, "r"))) { |
361 if ((fgets(buffer, 25, fp))) { |
399 if ((fgets(buffer, 25, fp))) { |
362 sscanf(buffer, "%d", &value); |
400 sscanf(buffer, "%d", &conv_time); |
363 /* If device is removed, value is negative (errno?) */ |
401 } |
364 if ((value > 0) && (value != W1_TEMP_RESOLUTION)) { |
402 fclose(fp); |
365 syslog(LOG_NOTICE, "One-wire device %s set resolution from %d to %d", cur_w1->address, value, W1_TEMP_RESOLUTION); |
403 } |
366 fseek(fp, 0L, SEEK_SET); |
404 free(devfile); |
367 sprintf(buffer, "%d", W1_TEMP_RESOLUTION); |
405 |
368 fputs(buffer, fp); |
406 /* |
369 } |
407 * Read temperature for one sensor. |
370 } |
408 * Bulk read does not work for unknown reason. |
371 fclose(fp); |
409 * It may be because there are also DS2413 devices. |
372 free(devfile); |
410 */ |
373 |
411 devfile = xstrcpy((char *)"/sys/bus/w1/devices/"); |
374 conv_time = 760; |
412 devfile = xstrcat(devfile, cur_w1->address); |
375 devfile = xstrcpy((char *)"/sys/bus/w1/devices/"); |
413 devfile = xstrcat(devfile, (char *)"/temperature"); |
376 devfile = xstrcat(devfile, cur_w1->address); |
414 if ((fp = fopen(devfile, "r"))) { |
377 devfile = xstrcat(devfile, (char *)"/conv_time"); |
415 mDelay(conv_time); |
378 if ((fp = fopen(devfile, "r"))) { |
416 if ((fgets(buffer, 25, fp))) { |
379 if ((fgets(buffer, 25, fp))) { |
417 sscanf(buffer, "%d", &value); |
380 sscanf(buffer, "%d", &conv_time); |
418 if (cur_w1->value != value) { |
|
419 cur_w1->timestamp = time(NULL); |
|
420 changed = true; |
|
421 if (debug) |
|
422 syslog(LOG_NOTICE, "One-wire device %s temperature read %d => %d", cur_w1->address, cur_w1->value, value); |
381 } |
423 } |
382 fclose(fp); |
424 cur_w1->value = value; /* devices.c will pick this up */ |
383 } |
425 } else { |
384 free(devfile); |
426 syslog(LOG_NOTICE, "One-wire device %s temperature read error", cur_w1->address); |
385 |
427 } |
386 devfile = xstrcpy((char *)"/sys/bus/w1/devices/"); |
428 fclose(fp); |
387 devfile = xstrcat(devfile, cur_w1->address); |
|
388 devfile = xstrcat(devfile, (char *)"/temperature"); |
|
389 if ((fp = fopen(devfile, "r"))) { |
|
390 mDelay(conv_time); |
|
391 if ((fgets(buffer, 25, fp))) { |
|
392 sscanf(buffer, "%d", &value); |
|
393 if (cur_w1->value != value) { |
|
394 cur_w1->timestamp = time(NULL); |
|
395 changed = true; |
|
396 if (debug) |
|
397 syslog(LOG_NOTICE, "One-wire device %s temperature read %d => %d", cur_w1->address, cur_w1->value, value); |
|
398 } |
|
399 cur_w1->value = value; /* devices.c will pick this up */ |
|
400 } else { |
|
401 syslog(LOG_NOTICE, "One-wire device %s temperature read error", cur_w1->address); |
|
402 } |
|
403 fclose(fp); |
|
404 } |
|
405 |
|
406 } else { |
429 } else { |
407 syslog(LOG_NOTICE, "One-wire device %s open: %s", cur_w1->address, strerror(errno)); |
430 syslog(LOG_NOTICE, "One-wire device %s open: %s", cur_w1->address, strerror(errno)); |
408 } |
431 } |
409 free(devfile); |
432 free(devfile); |
410 devfile = NULL; |
433 devfile = NULL; |