Use some early returns to reduce deeply indented callbacks.

This commit is contained in:
Martin Ling
2022-08-07 17:24:02 +01:00
parent 344af5094b
commit bcaebc00c3

View File

@ -414,7 +414,11 @@ int rx_callback(hackrf_transfer* transfer)
size_t bytes_written; size_t bytes_written;
unsigned int i; unsigned int i;
if (file != NULL) { if (file == NULL) {
stop_main_loop();
return -1;
}
byte_count += transfer->valid_length; byte_count += transfer->valid_length;
bytes_to_write = transfer->valid_length; bytes_to_write = transfer->valid_length;
if (limit_num_samples) { if (limit_num_samples) {
@ -450,10 +454,8 @@ int rx_callback(hackrf_transfer* transfer)
transfer->buffer, transfer->buffer,
(stream_size - stream_tail)); (stream_size - stream_tail));
memcpy(stream_buf, memcpy(stream_buf,
transfer->buffer + transfer->buffer + (stream_size - stream_tail),
(stream_size - stream_tail), bytes_to_write - (stream_size - stream_tail));
bytes_to_write -
(stream_size - stream_tail));
}; };
__atomic_store_n( __atomic_store_n(
&stream_tail, &stream_tail,
@ -472,10 +474,6 @@ int rx_callback(hackrf_transfer* transfer)
return 0; return 0;
} }
} }
} else {
stop_main_loop();
return -1;
}
} }
int tx_callback(hackrf_transfer* transfer) int tx_callback(hackrf_transfer* transfer)
@ -494,41 +492,7 @@ int tx_callback(hackrf_transfer* transfer)
stream_amplitude += abs((signed char) transfer->buffer[i]); stream_amplitude += abs((signed char) transfer->buffer[i]);
} }
if (file != NULL) { if (file == NULL) { // transceiver_mode == TRANSCEIVER_MODE_SS
if (limit_num_samples) {
if (bytes_to_read >= bytes_to_xfer) {
/*
* In this condition, we probably tx some of the previous
* buffer contents at the end. :-(
*/
bytes_to_read = bytes_to_xfer;
}
bytes_to_xfer -= bytes_to_read;
}
bytes_read = fread(transfer->buffer, 1, bytes_to_read, file);
if (limit_num_samples && (bytes_to_xfer == 0)) {
stop_main_loop();
return -1;
}
if (bytes_read != bytes_to_read) {
if (repeat) {
fprintf(stderr,
"Input file end reached. Rewind to beginning.\n");
rewind(file);
fread(transfer->buffer + bytes_read,
1,
bytes_to_read - bytes_read,
file);
return 0;
} else {
stop_main_loop();
return -1; /* not repeat mode, end of file */
}
} else {
return 0;
}
} else { // transceiver_mode == TRANSCEIVER_MODE_SS
/* Transmit continuous wave with specific amplitude */ /* Transmit continuous wave with specific amplitude */
if (limit_num_samples) { if (limit_num_samples) {
if (bytes_to_read >= bytes_to_xfer) { if (bytes_to_read >= bytes_to_xfer) {
@ -547,6 +511,39 @@ int tx_callback(hackrf_transfer* transfer)
return 0; return 0;
} }
} }
if (limit_num_samples) {
if (bytes_to_read >= bytes_to_xfer) {
/*
* In this condition, we probably tx some of the previous
* buffer contents at the end. :-(
*/
bytes_to_read = bytes_to_xfer;
}
bytes_to_xfer -= bytes_to_read;
}
bytes_read = fread(transfer->buffer, 1, bytes_to_read, file);
if (limit_num_samples && (bytes_to_xfer == 0)) {
stop_main_loop();
return -1;
}
if (bytes_read != bytes_to_read) {
if (repeat) {
fprintf(stderr, "Input file end reached. Rewind to beginning.\n");
rewind(file);
fread(transfer->buffer + bytes_read,
1,
bytes_to_read - bytes_read,
file);
return 0;
} else {
stop_main_loop();
return -1; /* not repeat mode, end of file */
}
} else {
return 0;
}
} }
static int update_stats(hackrf_device* device, hackrf_m0_state* state, stats_t* stats) static int update_stats(hackrf_device* device, hackrf_m0_state* state, stats_t* stats)