view loadtools/buzplaybu.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 7ade15d4e0cb
children
line wrap: on
line source

/*
 * fc-buzplay: this module implements the legacy BU mode.
 */

#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>

buzplay_bu_file(filename)
	char *filename;
{
	FILE *f;
	char linebuf[256], *cp, *num1, *num2;
	int lineno;
	char *targv[4];
	u_long n1, n2, total_ms;
	int rc, timeout;

	f = fopen(filename, "r");
	if (!f) {
		perror(filename);
		return(-1);
	}
	printf("Uploading the melody to the target\n");
	targv[0] = "I";
	targv[1] = 0;
	tpinterf_make_cmd(targv);
	if (tpinterf_send_cmd() < 0) {
		fclose(f);
		return(-1);
	}
	rc = tpinterf_pass_output(1);
	if (rc) {
		fclose(f);
		return(rc);
	}
	targv[0] = "E";
	targv[3] = 0;
	total_ms = 0;
	for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) {
		cp = index(linebuf, '\n');
		if (!cp) {
			fprintf(stderr, "%s line %d: missing newline\n",
				filename, lineno);
			fclose(f);
			return(-1);
		}
		for (cp = linebuf; isspace(*cp); cp++)
			;
		if (*cp == '\0' || *cp == '#')
			continue;
		if (!isdigit(*cp)) {
inv:			fprintf(stderr, "%s line %d: unexpected content\n",
				filename, lineno);
			fclose(f);
			return(-1);
		}
		for (num1 = cp; *cp && !isspace(*cp); cp++)
			if (!isdigit(*cp))
				goto inv;
		if (isspace(*cp))
			*cp++ = '\0';
		while (isspace(*cp))
			cp++;
		if (!isdigit(*cp))
			goto inv;
		for (num2 = cp; *cp && !isspace(*cp); cp++)
			if (!isdigit(*cp))
				goto inv;
		if (isspace(*cp))
			*cp++ = '\0';
		while (isspace(*cp))
			cp++;
		if (*cp != '\0' && *cp != '#')
			goto inv;
		n1 = strtoul(num1, 0, 10);
		n2 = strtoul(num2, 0, 10);
		if (n1 > 255) {
			fprintf(stderr,
				"%s line %d: the tone number is out of range\n",
				filename, lineno);
			fclose(f);
			return(-1);
		}
		if (n2 < 1 || n2 > 0xFFFF) {
			fprintf(stderr,
			"%s line %d: the duration number is out of range\n",
				filename, lineno);
			fclose(f);
			return(-1);
		}
		/* send it to the target */
		targv[1] = num1;
		targv[2] = num2;
		tpinterf_make_cmd(targv);
		if (tpinterf_send_cmd() < 0) {
			fclose(f);
			return(-1);
		}
		rc = tpinterf_pass_output(1);
		if (rc) {
			fclose(f);
			return(rc);
		}
		/* account for the duration */
		total_ms += n2 * 5;
	}
	fclose(f);
	if (!total_ms) {
		fprintf(stderr, "%s is empty!\n", filename);
		return(-1);
	}
	printf("Requesting play of the uploaded melody on the target\n");
	targv[0] = "P";
	targv[1] = 0;
	tpinterf_make_cmd(targv);
	if (tpinterf_send_cmd() < 0)
		return(-1);
	timeout = total_ms / 1000 + 2;
	return tpinterf_pass_output(timeout);
}

cmd_play_bu(argc, argv)
	char **argv;
{
	buzplay_bu_file(argv[1]);
}