Skip to content

Commit

Permalink
cdba-server: Allow external status command
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
quic-bjorande committed Nov 28, 2023
1 parent c8e29e3 commit 81e4687
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 1 deletion.
4 changes: 4 additions & 0 deletions device.c
Original file line number Diff line number Diff line change
Expand Up @@ -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])))

Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions device.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ struct device {
void *cdb;
void *console;

char *status_cmd;

struct list_head node;
};

Expand Down
2 changes: 2 additions & 0 deletions device_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 9 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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']
Expand Down
4 changes: 4 additions & 0 deletions schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
94 changes: 94 additions & 0 deletions status-cmd.c
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 0 deletions status-cmd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef __STATUS_CMD_H__
#define __STATUS_CMD_H__

struct device;

int status_cmd_open(struct device *dev);

#endif
5 changes: 5 additions & 0 deletions status.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions status.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 81e4687

Please sign in to comment.