Skip to contentSkip to conversation

BAT: some fixes

PATCH In review [email protected] 2 participants 7 messages Oct 16, 2015

lore
Share & tools
lore
#0 lore raw

v3 cover BAT: some fixes

From: "Lu, Han" <[email protected]>

some fixes and adjusts.

Lu, Han (4):
BAT: Merge message strings
BAT: Use colon instead of comma for separation
BAT: Adjust parameters and strings
BAT: Use dynamic temp file

 bat/bat.c    | 77 +++++++++++++++++++++++++++++++++---------------------------
 bat/common.h |  2 +-
 2 files changed, 43 insertions(+), 36 deletions(-)
Signature · 1 line
1.9.1
#1 lore raw

PATCH v3 1/4 BAT: Merge message strings

From: "Lu, Han" <[email protected]>

Remove redundant error messages.

bat/bat.c +16 -26
index 24c74e8..4320e22 100644
@@ -41,43 +41,33 @@ static int get_duration(struct bat *bat)
 	char *ptrf, *ptri;
 
 	duration_f = strtof(bat->narg, &ptrf);
-	if (duration_f == HUGE_VALF || duration_f == -HUGE_VALF) {
-		fprintf(bat->err, _("duration float overflow: %f %d\n"),
-				duration_f, -errno);
-		return -errno;
-	} else if (duration_f == 0.0 && errno != 0) {
-		fprintf(bat->err, _("duration float underflow: %f %d\n"),
-				duration_f, -errno);
-		return -errno;
-	}
+	if (duration_f == HUGE_VALF || duration_f == -HUGE_VALF
+			|| (duration_f == 0.0 && errno != 0))
+		goto err_exit;
 
 	duration_i = strtol(bat->narg, &ptri, 10);
-	if (duration_i == LONG_MAX) {
-		fprintf(bat->err, _("duration long overflow: %ld %d\n"),
-				duration_i, -errno);
-		return -errno;
-	} else if (duration_i == LONG_MIN) {
-		fprintf(bat->err, _("duration long underflow: %ld %d\n"),
-				duration_i, -errno);
-		return -errno;
-	}
+	if (duration_i == LONG_MAX || duration_i == LONG_MIN)
+		goto err_exit;
 
-	if (*ptrf == 's') {
+	if (*ptrf == 's')
 		bat->frames = duration_f * bat->rate;
-	} else if (*ptri == 0) {
+	else if (*ptri == 0)
 		bat->frames = duration_i;
-	} else {
-		fprintf(bat->err, _("invalid duration: %s\n"), bat->narg);
-		return -EINVAL;
-	}
+	else
+		bat->frames = -1;
 
 	if (bat->frames <= 0 || bat->frames > MAX_FRAMES) {
-		fprintf(bat->err, _("duration out of range: (0, %d(%ds))\n"),
-				MAX_FRAMES, (bat->frames / bat->rate));
+		fprintf(bat->err, _("Invalid duration. Range: (0, %d(%fs))\n"),
+				MAX_FRAMES, (double)MAX_FRAMES / bat->rate);
 		return -EINVAL;
 	}
 
 	return 0;
+
+err_exit:
+	fprintf(bat->err, _("Duration overflow/underflow: %d\n"), -errno);
+
+	return -errno;
 }
 
 static void get_sine_frequencies(struct bat *bat, char *freq)
Signature · 1 line
1.9.1
#2 lore raw

PATCH v3 2/4 BAT: Use colon instead of comma for separation

In reply to [email protected]

From: "Lu, Han" <[email protected]>

Use colon instead of comma to separate frequency parameters, for in several locale comma may be handled as decimal point.

bat/bat.c +1 -1
index 4320e22..2320984 100644
@@ -74,7 +74,7 @@ static void get_sine_frequencies(struct bat *bat, char *freq)
 {
 	char *tmp1;
 
-	tmp1 = strchr(freq, ',');
+	tmp1 = strchr(freq, ':');
 	if (tmp1 == NULL) {
 		bat->target_freq[1] = bat->target_freq[0] = atof(optarg);
 	} else {
Signature · 1 line
1.9.1
#3 lore raw

PATCH v3 3/4 BAT: Adjust parameters and strings

In reply to [email protected]

From: "Lu, Han" <[email protected]>

Use rational parameters and clearer comments.

bat/bat.c +5 -5
index 2320984..02b1abd 100644
@@ -268,16 +268,16 @@ static void test_capture(struct bat *bat)
 	}
 }
 
-static void usage(struct bat *bat, char *argv[])
+static void usage(struct bat *bat, const char *command)
 {
 	fprintf(bat->log,
 _("Usage:%s [Option]...\n"
 "\n"
 "-h, --help             help\n"
-"-D                     sound card\n"
+"-D                     pcm for both playback and capture\n"
 "-P                     playback pcm\n"
 "-C                     capture pcm\n"
-"-f                     sample size\n"
+"-f                     sample format\n"
 "-c                     number of channels\n"
 "-r                     sampling rate\n"
 "-n                     frames to capture\n"
@@ -289,7 +289,7 @@ _("Usage:%s [Option]...\n"
 "    --file=#           input file\n"
 "    --saveplay=#       save playback content to target file, for debug\n"
 "    --local            internal loop, bypass hardware\n"
-), argv[0]);
+), command);
 	fprintf(bat->log, _("Recognized sample formats are: %s %s %s %s\n"),
 			snd_pcm_format_name(SND_PCM_FORMAT_U8),
 			snd_pcm_format_name(SND_PCM_FORMAT_S16_LE),
@@ -402,7 +402,7 @@ static void parse_arguments(struct bat *bat, int argc, char *argv[])
 			break;
 		case 'h':
 		default:
-			usage(bat, argv);
+			usage(bat, argv[0]);
 			exit(EXIT_SUCCESS);
 		}
 	}
Signature · 1 line
1.9.1
#6 lore raw

PATCH v3 4/4 BAT: Use dynamic temp file

In reply to [email protected]

From: "Lu, Han" <[email protected]>

Use dynamic temp file instead of fixed temp file to store recorded wav data, for better security.

bat/bat.c +20 -3
index 02b1abd..3813ba8 100644
@@ -452,6 +452,7 @@ static int validate_options(struct bat *bat)
 static int bat_init(struct bat *bat)
 {
 	int err = 0;
+	char name[] = TEMP_RECORD_FILE_NAME;
 
 	/* Determine logging to a file or stdout and stderr */
 	if (bat->logarg) {
@@ -474,10 +475,23 @@ static int bat_init(struct bat *bat)
 	}
 
 	/* Determine capture file */
-	if (bat->local)
+	if (bat->local) {
 		bat->capture.file = bat->playback.file;
-	else
-		bat->capture.file = TEMP_RECORD_FILE_NAME;
+	} else {
+		/* create temp file for sound record and analysis */
+		err = mkstemp(name);
+		if (err == -1) {
+			fprintf(bat->err, _("Fail to create record file: %d\n"),
+					-errno);
+			return -errno;
+		}
+		/* store file name which is dynamically created */
+		bat->capture.file = strdup(name);
+		if (bat->capture.file == NULL)
+			return -errno;
+		/* close temp file */
+		close(err);
+	}
 
 	/* Initial for playback */
 	if (bat->playback.file == NULL) {
@@ -591,8 +605,11 @@ analyze:
 	err = analyze_capture(&bat);
 out:
 	fprintf(bat.log, _("\nReturn value is %d\n"), err);
+
 	if (bat.logarg)
 		fclose(bat.log);
+	if (!bat.local)
+		free(bat.capture.file);
 
 	return err;
 }
bat/common.h +1 -1
index f0254ab..90bc3e2 100644
@@ -15,7 +15,7 @@
 
 #include <alsa/asoundlib.h>
 
-#define TEMP_RECORD_FILE_NAME		"/tmp/bat.wav"
+#define TEMP_RECORD_FILE_NAME		"/tmp/bat.wav.XXXXXX"
 
 #define OPT_BASE			300
 #define OPT_LOG				(OPT_BASE + 1)
Signature · 1 line
1.9.1
#4 lore raw

Re: [PATCH V3 3/4] BAT: Adjust parameters and strings

In reply to [email protected]
Quoted 4 lines · On Fri, 16 Oct 2015 10:12:05 +0200, [email protected] wrote:

From: "Lu, Han" <[email protected]>

Use rational parameters and clearer comments.

This doesn't match with what you actually changed exactly.

In general, please give more care and love to the changelog comments. This is the only verbose information the later reader can receive.

Also, using argv[0] as the command name isn't appropriate. You can pass a different name to argv[0] freely. Use a constant pre-defined program name instead.

thanks,

Takashi

Quoted 47 lines
bat/bat.c +5 -5
index 2320984..02b1abd 100644
@@ -268,16 +268,16 @@ static void test_capture(struct bat *bat)
 	}
 }
 
-static void usage(struct bat *bat, char *argv[])
+static void usage(struct bat *bat, const char *command)
 {
 	fprintf(bat->log,
 _("Usage:%s [Option]...\n"
 "\n"
 "-h, --help             help\n"
-"-D                     sound card\n"
+"-D                     pcm for both playback and capture\n"
 "-P                     playback pcm\n"
 "-C                     capture pcm\n"
-"-f                     sample size\n"
+"-f                     sample format\n"
 "-c                     number of channels\n"
 "-r                     sampling rate\n"
 "-n                     frames to capture\n"
@@ -289,7 +289,7 @@ _("Usage:%s [Option]...\n"
 "    --file=#           input file\n"
 "    --saveplay=#       save playback content to target file, for debug\n"
 "    --local            internal loop, bypass hardware\n"
-), argv[0]);
+), command);
 	fprintf(bat->log, _("Recognized sample formats are: %s %s %s %s\n"),
 			snd_pcm_format_name(SND_PCM_FORMAT_U8),
 			snd_pcm_format_name(SND_PCM_FORMAT_S16_LE),
@@ -402,7 +402,7 @@ static void parse_arguments(struct bat *bat, int argc, char *argv[])
 			break;
 		case 'h':
 		default:
-			usage(bat, argv);
+			usage(bat, argv[0]);
 			exit(EXIT_SUCCESS);
 		}
 	}
Signature · 1 line
1.9.1
author
#5 lore raw

Re: [PATCH V3 3/4] BAT: Adjust parameters and strings

Hi Takashi,

Quoted 14 lines

-----Original Message-----
From: Takashi Iwai [mailto:[email protected]]
Sent: Friday, October 16, 2015 4:44 PM
To: Lu, Han
Cc: Girdwood, Liam R; Gautier, Bernard; [email protected]
Subject: Re: [PATCH V3 3/4] BAT: Adjust parameters and strings

Quoted 4 lines · On Fri, 16 Oct 2015 10:12:05 +0200, [email protected] wrote:

From: "Lu, Han" <[email protected]>

Use rational parameters and clearer comments.

Quoted 8 lines

This doesn't match with what you actually changed exactly.

In general, please give more care and love to the changelog comments. This is the only verbose information the later reader can receive.

Also, using argv[0] as the command name isn't appropriate. You can pass a different name to argv[0] freely. Use a constant pre-defined program name instead.

Sorry! I'll be more careful about change log comments. And I'll use pre-defined name instead of argv[0].

BR,
Han Lu

Quoted 56 lines

thanks,

Takashi

Quoted 27 lines
bat/bat.c +3 -3
index 2320984..02b1abd 100644
@@ -268,16 +268,16 @@ static void test_capture(struct bat *bat)
 	}
 }
 
-static void usage(struct bat *bat, char *argv[])
+static void usage(struct bat *bat, const char *command)
 {
 	fprintf(bat->log,
 _("Usage:%s [Option]...\n"
 "\n"
 "-h, --help             help\n"
-"-D                     sound card\n"
+"-D                     pcm for both playback and capture\n"
 "-P                     playback pcm\n"
 "-C                     capture pcm\n"
-"-f                     sample size\n"
+"-f                     sample format\n"
 "-c                     number of channels\n"
 "-r                     sampling rate\n"
 "-n                     frames to capture\n"

bat/bat.c _("Usage:%s [Option]...\n" · line 295

Quoted 8 lines
bat/bat.c +1 -1
@@ -289,7 +289,7 @@ _("Usage:%s [Option]...\n"
 "    --file=#           input file\n"
 "    --saveplay=#       save playback content to target file, for debug\n"
 "    --local            internal loop, bypass hardware\n"
-), argv[0]);
+), command);
 	fprintf(bat->log, _("Recognized sample formats are: %s %s %s %s\n"),
 			snd_pcm_format_name(SND_PCM_FORMAT_U8),
 
	snd_pcm_format_name(SND_PCM_FORMAT_S16_LE),

bat/bat.c static void parse_arguments(struct bat *bat, int argc,

Quoted 1 line
bat/bat.c +0 -0
@@ -402,7 +402,7 @@ static void parse_arguments(struct bat *bat, int argc,

char *argv[])

Quoted 11 lines
bat/bat.c +1 -2
 			break;
 		case 'h':
 		default:
-			usage(bat, argv);
+			usage(bat, argv[0]);
 			exit(EXIT_SUCCESS);
 		}
 	}
--

1.9.1