[alsa-devel] [PATCH V3 0/4] BAT: some fixes
From: "Lu, Han" han.lu@intel.com
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(-)
From: "Lu, Han" han.lu@intel.com
Remove redundant error messages.
Signed-off-by: Lu, Han han.lu@intel.com
diff --git a/bat/bat.c b/bat/bat.c index 24c74e8..4320e22 100644 --- a/bat/bat.c +++ b/bat/bat.c @@ -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)
From: "Lu, Han" han.lu@intel.com
Use colon instead of comma to separate frequency parameters, for in several locale comma may be handled as decimal point.
Signed-off-by: Lu, Han han.lu@intel.com
diff --git a/bat/bat.c b/bat/bat.c index 4320e22..2320984 100644 --- a/bat/bat.c +++ b/bat/bat.c @@ -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 {
From: "Lu, Han" han.lu@intel.com
Use rational parameters and clearer comments.
Signed-off-by: Lu, Han han.lu@intel.com
diff --git a/bat/bat.c b/bat/bat.c index 2320984..02b1abd 100644 --- a/bat/bat.c +++ b/bat/bat.c @@ -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); } }
On Fri, 16 Oct 2015 10:12:05 +0200, han.lu@intel.com wrote:
From: "Lu, Han" han.lu@intel.com
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
Signed-off-by: Lu, Han han.lu@intel.com
diff --git a/bat/bat.c b/bat/bat.c index 2320984..02b1abd 100644 --- a/bat/bat.c +++ b/bat/bat.c @@ -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);
-- 1.9.1
Hi Takashi,
-----Original Message----- From: Takashi Iwai [mailto:tiwai@suse.de] Sent: Friday, October 16, 2015 4:44 PM To: Lu, Han Cc: Girdwood, Liam R; Gautier, Bernard; alsa-devel@alsa-project.org Subject: Re: [PATCH V3 3/4] BAT: Adjust parameters and strings
On Fri, 16 Oct 2015 10:12:05 +0200, han.lu@intel.com wrote:
From: "Lu, Han" han.lu@intel.com
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.
Sorry! I'll be more careful about change log comments. And I'll use pre-defined name instead of argv[0].
BR, Han Lu
thanks,
Takashi
Signed-off-by: Lu, Han han.lu@intel.com
diff --git a/bat/bat.c b/bat/bat.c index 2320984..02b1abd 100644 --- a/bat/bat.c +++ b/bat/bat.c @@ -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);
-- 1.9.1
From: "Lu, Han" han.lu@intel.com
Use dynamic temp file instead of fixed temp file to store recorded wav data, for better security.
Signed-off-by: Lu, Han han.lu@intel.com
diff --git a/bat/bat.c b/bat/bat.c index 02b1abd..3813ba8 100644 --- a/bat/bat.c +++ b/bat/bat.c @@ -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; } diff --git a/bat/common.h b/bat/common.h index f0254ab..90bc3e2 100644 --- a/bat/common.h +++ b/bat/common.h @@ -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)
participants (3)
-
han.lu@intel.com
-
Lu, Han
-
Takashi Iwai