Merge pull request #820 from mossmann/tools

Clean up output file handling
This commit is contained in:
Michael Ossmann
2021-01-28 14:30:53 -07:00
committed by GitHub
4 changed files with 100 additions and 94 deletions

View File

@ -96,7 +96,7 @@ int main(int argc, char** argv)
hackrf_device* device = NULL;
int result = HACKRF_SUCCESS;
int option_index = 0;
FILE* fd = NULL;
FILE* infile = NULL;
ssize_t bytes_read;
uint8_t* pdata = &data[0];
@ -128,17 +128,17 @@ int main(int argc, char** argv)
return EXIT_FAILURE;
}
fd = fopen(path, "rb");
if (fd == NULL)
infile = fopen(path, "rb");
if (infile == NULL)
{
fprintf(stderr, "Failed to open file: %s\n", path);
return EXIT_FAILURE;
}
/* Get size of the file */
fseek(fd, 0, SEEK_END); /* Not really portable but work on major OS Linux/Win32 */
length = ftell(fd);
fseek(infile, 0, SEEK_END); /* Not really portable but work on major OS Linux/Win32 */
length = ftell(infile);
/* Move to start */
rewind(fd);
rewind(infile);
printf("File size %d bytes.\n", length);
if (length > MAX_XSVF_LENGTH) {
@ -148,13 +148,13 @@ int main(int argc, char** argv)
}
total_length = length;
bytes_read = fread(data, 1, total_length, fd);
bytes_read = fread(data, 1, total_length, infile);
if (bytes_read != total_length)
{
fprintf(stderr, "Failed to read all bytes (read %d bytes instead of %d bytes).\n",
(int)bytes_read, total_length);
fclose(fd);
fd = NULL;
fclose(infile);
infile = NULL;
return EXIT_FAILURE;
}
@ -179,8 +179,8 @@ int main(int argc, char** argv)
{
fprintf(stderr, "hackrf_cpld_write() failed: %s (%d)\n",
hackrf_error_name(result), result);
fclose(fd);
fd = NULL;
fclose(infile);
infile = NULL;
return EXIT_FAILURE;
}
@ -193,15 +193,15 @@ int main(int argc, char** argv)
{
fprintf(stderr, "hackrf_close() failed: %s (%d)\n",
hackrf_error_name(result), result);
fclose(fd);
fd = NULL;
fclose(infile);
infile = NULL;
return EXIT_FAILURE;
}
hackrf_exit();
if (fd != NULL) {
fclose(fd);
if (infile != NULL) {
fclose(infile);
}
return EXIT_SUCCESS;

View File

@ -168,7 +168,7 @@ int main(int argc, char** argv)
int option_index = 0;
static uint8_t data[MAX_LENGTH];
uint8_t* pdata = &data[0];
FILE* fd = NULL;
FILE* infile = NULL;
bool read = false;
bool write = false;
bool ignore_compat_check = false;
@ -256,24 +256,24 @@ int main(int argc, char** argv)
if( write )
{
fd = fopen(path, "rb");
if(fd == NULL)
infile = fopen(path, "rb");
if(infile == NULL)
{
printf("Error opening file %s\n", path);
return EXIT_FAILURE;
}
/* Get size of the file */
fseek(fd, 0, SEEK_END); /* Not really portable but work on major OS Linux/Win32 */
length = ftell(fd);
fseek(infile, 0, SEEK_END); /* Not really portable but work on major OS Linux/Win32 */
length = ftell(infile);
/* Move to start */
rewind(fd);
rewind(infile);
printf("File size %d bytes.\n", length);
}
if (length == 0) {
fprintf(stderr, "Requested transfer of zero bytes.\n");
if(fd != NULL)
fclose(fd);
if(infile != NULL)
fclose(infile);
usage();
return EXIT_FAILURE;
}
@ -281,15 +281,15 @@ int main(int argc, char** argv)
if ((length > MAX_LENGTH) || (address > MAX_LENGTH)
|| ((address + length) > MAX_LENGTH)) {
fprintf(stderr, "Request exceeds size of flash memory.\n");
if(fd != NULL)
fclose(fd);
if(infile != NULL)
fclose(infile);
usage();
return EXIT_FAILURE;
}
if (read) {
fd = fopen(path, "wb");
if(fd == NULL)
infile = fopen(path, "wb");
if(infile == NULL)
{
printf("Error to open file %s\n", path);
return EXIT_FAILURE;
@ -357,31 +357,31 @@ int main(int argc, char** argv)
if (result != HACKRF_SUCCESS) {
fprintf(stderr, "hackrf_spiflash_read() failed: %s (%d)\n",
hackrf_error_name(result), result);
fclose(fd);
fd = NULL;
fclose(infile);
infile = NULL;
return EXIT_FAILURE;
}
address += xfer_len;
pdata += xfer_len;
tmp_length -= xfer_len;
}
bytes_written = fwrite(data, 1, length, fd);
bytes_written = fwrite(data, 1, length, infile);
if (bytes_written != length) {
fprintf(stderr, "Failed write to file (wrote %d bytes).\n",
(int)bytes_written);
fclose(fd);
fd = NULL;
fclose(infile);
infile = NULL;
return EXIT_FAILURE;
}
}
if(write) {
ssize_t bytes_read = fread(data, 1, length, fd);
ssize_t bytes_read = fread(data, 1, length, infile);
if (bytes_read != length) {
fprintf(stderr, "Failed read file (read %d bytes).\n",
(int)bytes_read);
fclose(fd);
fd = NULL;
fclose(infile);
infile = NULL;
return EXIT_FAILURE;
}
if(!ignore_compat_check) {
@ -389,8 +389,8 @@ int main(int argc, char** argv)
result = compatibility_check(data, length, device);
if(result) {
printf("Compatibility test failed.\n");
fclose(fd);
fd = NULL;
fclose(infile);
infile = NULL;
return EXIT_FAILURE;
}
}
@ -399,8 +399,8 @@ int main(int argc, char** argv)
if (result != HACKRF_SUCCESS) {
fprintf(stderr, "hackrf_spiflash_erase() failed: %s (%d)\n",
hackrf_error_name(result), result);
fclose(fd);
fd = NULL;
fclose(infile);
infile = NULL;
return EXIT_FAILURE;
}
if( !verbose ) printf("Writing %d bytes at 0x%06x.\n", length, address);
@ -411,8 +411,8 @@ int main(int argc, char** argv)
if (result != HACKRF_SUCCESS) {
fprintf(stderr, "hackrf_spiflash_write() failed: %s (%d)\n",
hackrf_error_name(result), result);
fclose(fd);
fd = NULL;
fclose(infile);
infile = NULL;
return EXIT_FAILURE;
}
address += xfer_len;
@ -421,9 +421,9 @@ int main(int argc, char** argv)
}
}
if (fd != NULL) {
fclose(fd);
fd = NULL;
if (infile != NULL) {
fclose(infile);
infile = NULL;
}
if(reset) {

View File

@ -165,7 +165,7 @@ int parse_u32_range(char* s, uint32_t* const value_min, uint32_t* const value_ma
volatile bool do_exit = false;
FILE* fd = NULL;
FILE* outfile = NULL;
volatile uint32_t byte_count = 0;
volatile uint64_t sweep_count = 0;
@ -216,7 +216,7 @@ int rx_callback(hackrf_transfer* transfer) {
char time_str[50];
struct timeval usb_transfer_time;
if(NULL == fd) {
if(NULL == outfile) {
return -1;
}
@ -241,8 +241,8 @@ int rx_callback(hackrf_transfer* transfer) {
for(i=0; i < ifft_bins; i++) {
ifftwOut[i][0] *= 1.0f / ifft_bins;
ifftwOut[i][1] *= 1.0f / ifft_bins;
fwrite(&ifftwOut[i][0], sizeof(float), 1, fd);
fwrite(&ifftwOut[i][1], sizeof(float), 1, fd);
fwrite(&ifftwOut[i][0], sizeof(float), 1, outfile);
fwrite(&ifftwOut[i][1], sizeof(float), 1, outfile);
}
}
sweep_count++;
@ -289,19 +289,19 @@ int rx_callback(hackrf_transfer* transfer) {
record_length = 2 * sizeof(band_edge)
+ (fftSize/4) * sizeof(float);
fwrite(&record_length, sizeof(record_length), 1, fd);
fwrite(&record_length, sizeof(record_length), 1, outfile);
band_edge = frequency;
fwrite(&band_edge, sizeof(band_edge), 1, fd);
fwrite(&band_edge, sizeof(band_edge), 1, outfile);
band_edge = frequency + DEFAULT_SAMPLE_RATE_HZ / 4;
fwrite(&band_edge, sizeof(band_edge), 1, fd);
fwrite(&pwr[1+(fftSize*5)/8], sizeof(float), fftSize/4, fd);
fwrite(&band_edge, sizeof(band_edge), 1, outfile);
fwrite(&pwr[1+(fftSize*5)/8], sizeof(float), fftSize/4, outfile);
fwrite(&record_length, sizeof(record_length), 1, fd);
fwrite(&record_length, sizeof(record_length), 1, outfile);
band_edge = frequency + DEFAULT_SAMPLE_RATE_HZ / 2;
fwrite(&band_edge, sizeof(band_edge), 1, fd);
fwrite(&band_edge, sizeof(band_edge), 1, outfile);
band_edge = frequency + (DEFAULT_SAMPLE_RATE_HZ * 3) / 4;
fwrite(&band_edge, sizeof(band_edge), 1, fd);
fwrite(&pwr[1+fftSize/8], sizeof(float), fftSize/4, fd);
fwrite(&band_edge, sizeof(band_edge), 1, outfile);
fwrite(&pwr[1+fftSize/8], sizeof(float), fftSize/4, outfile);
} else if(ifft_output) {
ifft_idx = (uint32_t) round((frequency - (uint64_t)(FREQ_ONE_MHZ*frequencies[0]))
/ fft_bin_width);
@ -320,7 +320,7 @@ int rx_callback(hackrf_transfer* transfer) {
time_t time_stamp_seconds = time_stamp.tv_sec;
fft_time = localtime(&time_stamp_seconds);
strftime(time_str, 50, "%Y-%m-%d, %H:%M:%S", fft_time);
fprintf(fd, "%s.%06ld, %" PRIu64 ", %" PRIu64 ", %.2f, %u",
fprintf(outfile, "%s.%06ld, %" PRIu64 ", %" PRIu64 ", %.2f, %u",
time_str,
(long int)time_stamp.tv_usec,
(uint64_t)(frequency),
@ -328,10 +328,10 @@ int rx_callback(hackrf_transfer* transfer) {
fft_bin_width,
fftSize);
for(i = 0; (fftSize / 4) > i; i++) {
fprintf(fd, ", %.2f", pwr[i + 1 + (fftSize*5)/8]);
fprintf(outfile, ", %.2f", pwr[i + 1 + (fftSize*5)/8]);
}
fprintf(fd, "\n");
fprintf(fd, "%s.%06ld, %" PRIu64 ", %" PRIu64 ", %.2f, %u",
fprintf(outfile, "\n");
fprintf(outfile, "%s.%06ld, %" PRIu64 ", %" PRIu64 ", %.2f, %u",
time_str,
(long int)time_stamp.tv_usec,
(uint64_t)(frequency+(DEFAULT_SAMPLE_RATE_HZ/2)),
@ -339,9 +339,9 @@ int rx_callback(hackrf_transfer* transfer) {
fft_bin_width,
fftSize);
for(i = 0; (fftSize / 4) > i; i++) {
fprintf(fd, ", %.2f", pwr[i + 1 + (fftSize/8)]);
fprintf(outfile, ", %.2f", pwr[i + 1 + (fftSize/8)]);
}
fprintf(fd, "\n");
fprintf(outfile, "\n");
}
}
return 0;
@ -595,17 +595,17 @@ int main(int argc, char** argv) {
}
if((NULL == path) || (strcmp(path, "-") == 0)) {
fd = stdout;
outfile = stdout;
} else {
fd = fopen(path, "wb");
outfile = fopen(path, "wb");
}
if(NULL == fd) {
if(NULL == outfile) {
fprintf(stderr, "Failed to open file: %s\n", path);
return EXIT_FAILURE;
}
/* Change fd buffer to have bigger one to store or read data on/to HDD */
result = setvbuf(fd , NULL , _IOFBF , FD_BUFFER_SIZE);
/* Change outfile buffer to have bigger one to store or read data on/to HDD */
result = setvbuf(outfile , NULL , _IOFBF , FD_BUFFER_SIZE);
if( result != 0 ) {
fprintf(stderr, "setvbuf() failed: %d\n", result);
usage();
@ -757,10 +757,11 @@ int main(int argc, char** argv) {
fprintf(stderr, "hackrf_exit() done\n");
}
if ( ( fd != NULL ) && ( fd != stdout ) ) {
fclose(fd);
fd = NULL;
fprintf(stderr, "fclose(fd) done\n");
fflush(outfile);
if ( ( outfile != NULL ) && ( outfile != stdout ) ) {
fclose(outfile);
outfile = NULL;
fprintf(stderr, "fclose() done\n");
}
fftwf_free(fftwIn);
fftwf_free(fftwOut);

View File

@ -319,7 +319,7 @@ char* u64toa(uint64_t val, t_u64toa* str)
static volatile bool do_exit = false;
FILE* fd = NULL;
FILE* file = NULL;
volatile uint32_t byte_count = 0;
bool signalsource = false;
@ -380,7 +380,7 @@ int rx_callback(hackrf_transfer* transfer) {
size_t bytes_written;
unsigned int i;
if( fd != NULL )
if( file != NULL )
{
byte_count += transfer->valid_length;
bytes_to_write = transfer->valid_length;
@ -412,7 +412,7 @@ int rx_callback(hackrf_transfer* transfer) {
#endif
return 0;
} else {
bytes_written = fwrite(transfer->buffer, 1, bytes_to_write, fd);
bytes_written = fwrite(transfer->buffer, 1, bytes_to_write, file);
if ((bytes_written != bytes_to_write)
|| (limit_num_samples && (bytes_to_xfer == 0))) {
return -1;
@ -430,7 +430,7 @@ int tx_callback(hackrf_transfer* transfer) {
size_t bytes_read;
unsigned int i;
if( fd != NULL )
if( file != NULL )
{
byte_count += transfer->valid_length;
bytes_to_read = transfer->valid_length;
@ -444,15 +444,15 @@ int tx_callback(hackrf_transfer* transfer) {
}
bytes_to_xfer -= bytes_to_read;
}
bytes_read = fread(transfer->buffer, 1, bytes_to_read, fd);
bytes_read = fread(transfer->buffer, 1, bytes_to_read, file);
if (limit_num_samples && (bytes_to_xfer == 0)) {
return -1;
}
if (bytes_read != bytes_to_read) {
if (repeat) {
fprintf(stderr, "Input file end reached. Rewind to beginning.\n");
rewind(fd);
fread(transfer->buffer + bytes_read, 1, bytes_to_read - bytes_read, fd);
rewind(file);
fread(transfer->buffer + bytes_read, 1, bytes_to_read - bytes_read, file);
return 0;
} else {
return -1; /* not repeat mode, end of file */
@ -892,24 +892,24 @@ int main(int argc, char** argv) {
if( transceiver_mode == TRANSCEIVER_MODE_RX )
{
if (strcmp(path, "-") == 0) {
fd = stdout;
file = stdout;
} else {
fd = fopen(path, "wb");
file = fopen(path, "wb");
}
} else {
if (strcmp(path, "-") == 0) {
fd = stdin;
file = stdin;
} else {
fd = fopen(path, "rb");
file = fopen(path, "rb");
}
}
if( fd == NULL ) {
if( file == NULL ) {
fprintf(stderr, "Failed to open file: %s\n", path);
return EXIT_FAILURE;
}
/* Change fd buffer to have bigger one to store or read data on/to HDD */
result = setvbuf(fd , NULL , _IOFBF , FD_BUFFER_SIZE);
/* Change file buffer to have bigger one to store or read data on/to HDD */
result = setvbuf(file , NULL , _IOFBF , FD_BUFFER_SIZE);
if( result != 0 ) {
fprintf(stderr, "setvbuf() failed: %d\n", result);
usage();
@ -920,7 +920,7 @@ int main(int argc, char** argv) {
/* Write Wav header */
if( receive_wav )
{
fwrite(&wave_file_hdr, 1, sizeof(t_wav_file_hdr), fd);
fwrite(&wave_file_hdr, 1, sizeof(t_wav_file_hdr), file);
}
#ifdef _MSC_VER
@ -1045,7 +1045,7 @@ int main(int argc, char** argv) {
len=_st-stream_head;
else
len=stream_size-stream_head;
bytes_written = fwrite(stream_buf+stream_head, 1, len, fd);
bytes_written = fwrite(stream_buf+stream_head, 1, len, file);
if (len != bytes_written) {
fprintf(stderr, "write failed");
do_exit=true;
@ -1126,24 +1126,29 @@ int main(int argc, char** argv) {
fprintf(stderr, "hackrf_exit() done\n");
}
if(fd != NULL)
if(file != NULL)
{
if( receive_wav )
{
/* Get size of file */
file_pos = ftell(fd);
file_pos = ftell(file);
/* Update Wav Header */
wave_file_hdr.hdr.size = file_pos-8;
wave_file_hdr.fmt_chunk.dwSamplesPerSec = sample_rate_hz;
wave_file_hdr.fmt_chunk.dwAvgBytesPerSec = wave_file_hdr.fmt_chunk.dwSamplesPerSec*2;
wave_file_hdr.data_chunk.chunkSize = file_pos - sizeof(t_wav_file_hdr);
/* Overwrite header with updated data */
rewind(fd);
fwrite(&wave_file_hdr, 1, sizeof(t_wav_file_hdr), fd);
rewind(file);
fwrite(&wave_file_hdr, 1, sizeof(t_wav_file_hdr), file);
}
fclose(fd);
fd = NULL;
fprintf(stderr, "fclose(fd) done\n");
if (file != stdin) {
fflush(file);
}
if ((file != stdout) && (file != stdin)) {
fclose(file);
file = NULL;
fprintf(stderr, "fclose() done\n");
}
}
fprintf(stderr, "exit\n");
return exit_code;