Skip to content

Commit

Permalink
cdba-server: Introduce helper for sending measurements
Browse files Browse the repository at this point in the history
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
quic-bjorande committed Nov 28, 2023
1 parent bc5e25f commit 1cef583
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 15 deletions.
36 changes: 22 additions & 14 deletions cdb_assist.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

#include "cdba-server.h"
#include "device.h"
#include "status.h"

struct cdb_assist {
char serial[9];
Expand Down Expand Up @@ -341,20 +342,27 @@ static void cdb_gpio(struct cdb_assist *cdb, int gpio, bool on)
static void cdb_assist_print_status(struct device *dev)
{
struct cdb_assist *cdb = dev->cdb;
char buf[128];
int n;

n = sprintf(buf, "%umV %umA%s%s%s%s%s ref: %umV",
cdb->voltage_set,
cdb->current_actual,
cdb->vbat ? " vbat" : "",
cdb->vbus ? " vbus" : "",
cdb->btn[0] ? " btn1" : "",
cdb->btn[1] ? " btn2" : "",
cdb->btn[2] ? " btn3" : "",
cdb->vref);

cdba_send_buf(MSG_STATUS_UPDATE, n, buf);
struct status_value vbat[] = {
{
.unit = STATUS_MV,
.value = cdb->voltage_set,
},
{
.unit = STATUS_MA,
.value = cdb->current_actual,
},
{}
};
struct status_value vref[] = {
{
.unit = STATUS_MV,
.value = cdb->vref,
},
{}
};

status_send_values("vbat", vbat);
status_send_values("vref", vref);
}

static void cdb_set_voltage(struct cdb_assist *cdb, unsigned mV)
Expand Down
3 changes: 2 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ server_srcs = ['cdba-server.c',
'local-gpio.c',
'console.c',
'qcomlt_dbg.c',
'ppps.c']
'ppps.c',
'status.c']

if gpiod_dep.version().version_compare('>=2.0')
server_srcs += ['local-gpio-v2.c']
Expand Down
79 changes: 79 additions & 0 deletions status.c
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);
}
20 changes: 20 additions & 0 deletions status.h
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

0 comments on commit 1cef583

Please sign in to comment.