view loadtools/tpinterf2.c @ 964:a96cb97b66a2

ringtools/imy: fix duplicate definition of tdma_durations[] The bug was reported by Vadim Yanitskiy <fixeria@osmocom.org>, although the present fix is slightly different from the contributed patch: because main.c doesn't need this tdma_durations[] array at all, let's simply remove the reference to this array from main.c rather than turn it into an extern. I no longer remember my original thought flow that resulted (by mistake) in tdma_durations[] being multiply defined in main.c and durations.c. My intent might have been to define all globals in main.c and have the reference in durations.c be an extern - and I missed that extern - but without clear memory, I have no certainty. In any case, having this data array defined in the same module that fills it (durations.c) is sensible, so let's make it the new way.
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 31 Aug 2023 19:38:18 +0000
parents 8d7dcfd9df53
children
line wrap: on
line source

/*
 * This module provides a more advanced target interface function
 * than tpinterf.c - programmatic capture of target responses.
 * It is linked by some of our programs, but not all.
 */

#include <sys/types.h>
#include <sys/time.h>
#include <sys/errno.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>

extern int errno;

extern int target_fd;

/*
 * This functions reads the serial output from the target until a
 * '=' prompt is received.  All intermediate output is parsed into
 * lines and passed to a callback function.
 *
 * The callback function is called with a pointer to each received
 * line, stored in a buffer ending in NUL, with CRLF stripped.
 * The callback function is expected to return an int.  If the
 * callback return value is negative, this function returns immediately
 * with that negative value.  If the callback return value is positive
 * or zero, it is added to an accumulator.
 *
 * Termination: this function returns when it has received a '=' at
 * the beginning of a line (return value is the callback return
 * accumulator, or 0 if no lines came), if the callback returns a
 * negative value (that value is returned), or if an error is detected
 * within this function (return value -1, and an error message
 * printed on stderr).
 */
tpinterf_capture_output(timeout, callback)
	int timeout;	/* seconds */
	int (*callback)();
{
	char buf[512], *cp;
	char line[1024], *dp = line;
	fd_set fds;
	struct timeval tv;
	int cc, linelen = 0;
	int totout = 0, cbret;

	for (;;) {
		FD_ZERO(&fds);
		FD_SET(target_fd, &fds);
		tv.tv_sec = timeout;
		tv.tv_usec = 0;
		cc = select(target_fd+1, &fds, NULL, NULL, &tv);
		if (cc < 0) {
			if (errno == EINTR)
				continue;
			perror("select");
			return(-1);
		}
		if (cc < 1) {
			fprintf(stderr,
		"error: timeout waiting for \'=\' prompt from target\n");
			return(-1);
		}
		cc = read(target_fd, buf, sizeof buf);
		if (cc <= 0) {
			perror("read after successful select");
			return(-1);
		}
		for (cp = buf; cc; cp++) {
			cc--;
			if (*cp == '=' && !linelen && !cc)
				return(totout);
			if (*cp == '\r')
				continue;
			if (*cp == '\n') {
				*dp = '\0';
				cbret = callback(line);
				if (cbret < 0)
					return(cbret);
				totout += cbret;
				dp = line;
				linelen = 0;
				continue;
			}
			*dp++ = *cp;
			linelen++;
			if (linelen >= sizeof line) {
				fprintf(stderr,
			"error: target response line length exceeds buffer\n");
				return(-1);
			}
		}
	}
}

/* single line response capture mechanism */
/* same line buffer size as in tpinterf_capture_output() */
char target_response_line[1024];

static
oneline_catcher(linein)
	char *linein;
{
	strcpy(target_response_line, linein);
	return(1);
}

tpinterf_capture_output_oneline(timeout)
{
	return tpinterf_capture_output(timeout, oneline_catcher);
}