17 struct owparser_state {
18 double prev_delta[20];
22 static struct owparser_state *allocate_parser_state(const char **datastr)
27 * Count how many sensor entries we need. First entry belongs
28 * to server address or mount point and last one is NULL. So
29 * the index final is the count of actual valid sensor
32 for (i = 0; datastr[i]; i++)
35 return calloc(sizeof(struct owparser_state), i);
38 static double max_glitch_delta(const struct owparser_state *s)
43 for (i = 0; i < ARRAY_SIZE(s->prev_delta); i++)
44 max_delta = max(s->prev_delta[i], max_delta);
49 static int might_be_glitch(double data, const struct owparser_state *s)
51 double max_delta, delta;
53 /* The known bad data value from the sensor */
57 max_delta = max_glitch_delta(s);
59 /* Probably no enough data yet, so no glitch detection */
64 * Simple glitch detection. If delta to previous value is more
65 * than twice as larger as any of the older delta, we might
68 delta = fabs(data - s->prev_data);
70 return delta > max_delta * 2;
73 static void update_glitch_data(double data, struct owparser_state *s)
78 for (i = 1; i < ARRAY_SIZE(s->prev_delta); i++) {
79 s->prev_delta[i - 1] = s->prev_delta[i];
80 max_delta = max(s->prev_delta[i], max_delta);
83 /* Avoid storing the first incorrect delta value */
84 if (s->prev_data || max_delta)
85 s->prev_delta[--i] = fabs(data - s->prev_data);
90 static int parse_opts(const char *str, char *ow_path, size_t pathlen,
94 const char *start_str = str;
95 const char offset_str[] = "offset=";
101 * Skip the onewire path entry. Options begin after the first
108 /* Copy the onewire path without options */
109 strncpy(ow_path, start_str, pathlen);
110 ow_path[str - start_str] = '\0';
112 /* Get the next non-space, which is where the argument begins */
117 if (strncmp(str, offset_str, sizeof(offset_str) - 1))
119 str += sizeof(offset_str) - 1;
121 *offset = strtod(str, &endptr);
129 static int make_uncached(char *path, size_t len)
132 char p1[1024], p2[1024], *p = path;
134 if (strstr(path, "/uncached/"))
137 p1[sizeof(p1) - 1] = '\0';
138 p2[sizeof(p2) - 1] = '\0';
141 * Naively assume the "uncached" string can be put after the
144 while (*p && *p != '/')
153 strncpy(p1, path, sizeof(p1) - 1);
154 strncpy(p2, p, sizeof(p2) - 1);
155 ret = snprintf(path, len, "%s/uncached/%s", p1, p2);
157 /* No actual data overflow, snprintf just couldn't fit all data in the buffer */
158 if (ret >= RRD_DATA_MAX_LEN)
159 pr_err("Buffer overlfow\n");
164 static int owfs_read(const char *mount_point, const char *path, char **res)
170 snprintf(file, sizeof(file), "%s/%s", mount_point, path);
172 fd = open(file, O_RDONLY | O_CLOEXEC);
174 pr_err("Failed to open file %s: %m\n", file);
178 ret = read(fd, result, sizeof(result));
180 pr_err("Failed to read from file %s: %m\n", file);
184 *res = strndup(result, sizeof(result));
192 static int is_mount_point(const char *str)
195 * Filesystem paths begin with a slash, everything else must
196 * be a network addresses
204 static int onewire_parser(char *rrd_data, const char **parser_data, void **s)
207 const char *server_addr, *mount_point;
208 struct owparser_state *state = *s;
211 int max_str = RRD_DATA_MAX_LEN;
212 int is_mountpoint = is_mount_point(parser_data[0]);
215 pr_err("No parser data available\n");
220 *s = state = allocate_parser_state(parser_data);
223 mount_point = parser_data[0];
226 pr_err("Server address not specified\n");
230 server_addr = parser_data[0];
233 pr_err("Server address not specified\n");
237 h = OWNET_init(server_addr);
239 pr_err("Failed to connect to server %s\n", server_addr);
244 while (parser_data[i]) {
245 double offset = 0, data, prev_data = 85;
251 if (!strcmp("U", parser_data[i])) {
253 ret = snprintf(rrd_data, max_str, "U");
259 parse_opts(parser_data[i], ow_path, sizeof(ow_path), &offset);
266 pr_info("Reading data for entry %s with offset of %.2f\n",
270 ret = owfs_read(mount_point, ow_path, &tmp);
272 ret = OWNET_read(h, ow_path, &tmp);
274 /* Skip leading white space */
276 for (j = 0; j < ret && *tmp2 == ' '; j++)
283 * Older versions of OWNET_read did not NULL
286 memcpy(buf, tmp, min(ret, sizeof(buf) -1));
289 data = strtod(buf, &endptr);
295 * If we read the almost same value as
296 * previously, it's not a glitch
298 if (glitches && prev_data != 85) {
299 double d = max_glitch_delta(&state[i]);
301 if (fabs(data - prev_data) <= d * 2)
305 if (might_be_glitch(data, &state[i]) &&
306 glitches < 4 && retries < 7) {
309 pr_info("Retrying due to a glitch: %f\n", data);
316 * In case of failure, retry with uncached
317 * data. This is likely to help as it forces a
318 * retry even if the sensor is missing from
319 * the cache. We treat "85" also as a failure,
320 * as temp sensors some times report 85 under
323 ret = make_uncached(ow_path, sizeof(ow_path));
324 if (retries >= 10 || ret < 0) {
325 pr_err("Failed to read entry %s: %m\n",
334 update_glitch_data(data, &state[i]);
337 pr_err("Failed to parse data %s\n", buf);
343 ret = snprintf(rrd_data, max_str, "%f", data);
352 ret = snprintf(rrd_data, max_str, ":");
364 static struct parser_info onewire_parser_info = {
366 .parse = onewire_parser,
369 static int init_onewire_parser(void)
371 return register_parser(&onewire_parser_info);
374 struct plugin_info plugin_info = {
375 .name = "onewire_parser",
376 .init = init_onewire_parser,
377 .version = RRDD_VERSION,