cdba-server: Allow external status command

While the CDB Assist and QcomLT Debugboard is measuring voltage and
current on the DC jack directly in the backend, other backends and
further measurements can be performed using custom tooling.

Introduce support for invoking an external "status-cmd" to produce such
status updates.

The status command should on its stdout produce json-formatted status
updates following the same format as the cdba-server:

  {"ts":%d.%03d, "name": {["mv"|"ma"]: %u}(, "name2": {["mv"|"ma"]: %u})*}

with the ts aquired using clock_gettime(CLOCK_MONOTONIC) and provided in
seconds and milliseconds since the first measurement.

Signed-off-by: Bjorn Andersson <quic_bjorande@quicinc.com>
This commit is contained in:
Bjorn Andersson
2023-11-05 14:47:10 -06:00
parent c8e29e3400
commit 81e46878ed
9 changed files with 129 additions and 1 deletions

View File

@@ -45,6 +45,7 @@
#include "fastboot.h"
#include "list.h"
#include "ppps.h"
#include "status-cmd.h"
#define ARRAY_SIZE(x) ((sizeof(x)/sizeof((x)[0])))
@@ -268,6 +269,9 @@ void device_status_enable(struct device *device)
if (device_has_control(device, status_enable))
device_control(device, status_enable);
if (device->status_cmd)
status_cmd_open(device);
device->status_enabled = true;
}

View File

@@ -58,6 +58,8 @@ struct device {
void *cdb;
void *console;
char *status_cmd;
struct list_head node;
};

View File

@@ -193,6 +193,8 @@ static void parse_board(struct device_parser *dp)
dev->ppps_path = strdup(value);
} else if (!strcmp(key, "ppps3_path")) {
dev->ppps3_path = strdup(value);
} else if (!strcmp(key, "status-cmd")) {
dev->status_cmd = strdup(value);
} else {
fprintf(stderr, "device parser: unknown key \"%s\"\n", key);
exit(1);

View File

@@ -58,6 +58,13 @@ server_deps = [dependency('libudev', required: server_opt),
dependency('yaml-0.1', required: server_opt),
gpiod_dep,
ftdi_dep]
# E.g. Debian reuires -lutil for forkpty
if not compiler.has_function('forkpty')
util_dep = compiler.find_library('util')
server_deps += util_dep
endif
server_srcs = ['cdba-server.c',
'cdb_assist.c',
'circ_buf.c',
@@ -72,7 +79,8 @@ server_srcs = ['cdba-server.c',
'console.c',
'qcomlt_dbg.c',
'ppps.c',
'status.c']
'status.c',
'status-cmd.c']
if gpiod_dep.version().version_compare('>=2.0')
server_srcs += ['local-gpio-v2.c']

View File

@@ -73,6 +73,10 @@ properties:
description: USB device name, like 2-2:1.0/2-2-port2
type: string
status-cmd:
description: Command to execute for generating status updates
type: string
qcomlt_debug_board:
description: Qlt Debug Board control tty device path
$ref: "#/$defs/device_path"

94
status-cmd.c Normal file
View File

@@ -0,0 +1,94 @@
/*
* Copyright (c) 2023, Qualcomm Innovaction Center, Inc
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <err.h>
#include <pty.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "cdba-server.h"
#include "device.h"
#include "status.h"
#include "status-cmd.h"
static void launch_status_cmd(struct device *dev)
{
char *tokens[100];
char *p;
int t = 0;
p = strtok(dev->status_cmd, " ");
while (p) {
tokens[t++] = p;
p = strtok(NULL, " ");
if (t == 100)
exit(1);
}
tokens[t] = NULL;
execvp(tokens[0], tokens);
exit(1);
}
static int status_data(int fd, void *data)
{
char buf[128];
ssize_t n;
n = read(fd, buf, sizeof(buf));
if (n <= 0)
return n;
status_send_raw(buf, n);
return 0;
}
int status_cmd_open(struct device *dev)
{
pid_t status_pid;
int fd;
status_pid = forkpty(&fd, NULL, NULL, NULL);
if (status_pid < 0)
err(1, "failed to fork");
if(status_pid == 0) {
launch_status_cmd(dev);
/* Notreached */
}
watch_add_readfd(fd, status_data, dev);
return 0;
}

8
status-cmd.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef __STATUS_CMD_H__
#define __STATUS_CMD_H__
struct device;
int status_cmd_open(struct device *dev);
#endif

View File

@@ -77,3 +77,8 @@ void status_send_values(const char *id, struct status_value *values)
cdba_send_buf(MSG_STATUS_UPDATE, len, buf);
}
void status_send_raw(const char *data, size_t len)
{
cdba_send_buf(MSG_STATUS_UPDATE, len, data);
}

View File

@@ -16,5 +16,6 @@ struct status_value {
};
void status_send_values(const char *id, struct status_value *values);
void status_send_raw(const char *data, size_t len);
#endif