-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cdba-server: Introduce helper for sending measurements
The current format of the cdb_assist backend status messages was never intended to be parsed by a non-human, and the format doesn't extend well to other backends. Extend the format to be formatted as a json string, with a timestamp, implemented in a separate status module. The button status is skipped from the new status message, as it reflect the output-values as set by cdba itself, rather than any measured state. The timestamp is offset to the first measurement, to avoid disclosing the uptime of the machine cdba-server is running on. Signed-off-by: Bjorn Andersson <[email protected]>
- Loading branch information
1 parent
bc5e25f
commit 1cef583
Showing
4 changed files
with
123 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include <err.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <time.h> | ||
|
||
#include "cdba-server.h" | ||
#include "status.h" | ||
|
||
static const char *sz_units[] = { | ||
[STATUS_MV] = "mv", | ||
[STATUS_MA] = "ma", | ||
[STATUS_GPIO] = "gpio", | ||
}; | ||
|
||
static void status_get_ts(struct timespec *ts) | ||
{ | ||
static struct timespec t0; | ||
struct timespec t; | ||
|
||
if (!t0.tv_sec && !t0.tv_nsec) | ||
clock_gettime(CLOCK_MONOTONIC, &t0); | ||
|
||
clock_gettime(CLOCK_MONOTONIC, &t); | ||
|
||
if (t.tv_nsec < t0.tv_nsec) { | ||
ts->tv_sec = t.tv_sec - t0.tv_sec - 1; | ||
ts->tv_nsec = 1000000000 + (t.tv_nsec - t0.tv_nsec); | ||
} else { | ||
ts->tv_sec = t.tv_sec - t0.tv_sec; | ||
ts->tv_nsec = t.tv_nsec - t0.tv_nsec; | ||
} | ||
} | ||
|
||
void status_send_values(const char *id, struct status_value *values) | ||
{ | ||
struct status_value *value; | ||
struct timespec ts; | ||
char chunk[32]; | ||
char buf[256]; | ||
size_t len; | ||
size_t n; | ||
|
||
status_get_ts(&ts); | ||
|
||
len = snprintf(buf, sizeof(buf), "{\"ts\":%ld.%03ld, \"%s\":{ ", ts.tv_sec, ts.tv_nsec / 1000000, id); | ||
|
||
for (value = values; value->unit; value++) { | ||
if (value != values) { | ||
if (len + 3 >= sizeof(buf)) { | ||
warnx("status message overflow"); | ||
return; | ||
} | ||
|
||
strcpy(buf + len, ", "); | ||
len += 2; | ||
} | ||
|
||
n = snprintf(chunk, sizeof(chunk), "\"%s\": %u", sz_units[value->unit], value->value); | ||
|
||
if (len + n + 1>= sizeof(buf)) { | ||
warnx("status message overflow"); | ||
return; | ||
} | ||
|
||
strcpy(buf + len, chunk); | ||
len += n; | ||
} | ||
|
||
if (len + 4 >= sizeof(buf)) { | ||
warnx("status message overflow"); | ||
return; | ||
} | ||
|
||
strcpy(buf + len, "}}\n"); | ||
len += 3; | ||
|
||
cdba_send_buf(MSG_STATUS_UPDATE, len, buf); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef __STATUS_H__ | ||
#define __STATUS_H__ | ||
|
||
#include <stdlib.h> | ||
|
||
enum status_unit { | ||
STATUS_EOF, | ||
STATUS_MV, | ||
STATUS_MA, | ||
STATUS_GPIO, | ||
}; | ||
|
||
struct status_value { | ||
enum status_unit unit; | ||
unsigned int value; | ||
}; | ||
|
||
void status_send_values(const char *id, struct status_value *values); | ||
|
||
#endif |