improve exit handling on device removal

This commit is contained in:
Steve Markgraf 2019-08-26 19:26:30 +02:00
parent b8d33bfd82
commit 077613efc5
5 changed files with 35 additions and 11 deletions

View File

@ -85,6 +85,12 @@ void fl2k_callback(fl2k_data_info_t *data_info)
int r, left = FL2K_BUF_LEN;
static uint32_t repeat_cnt = 0;
if (data_info->device_error) {
fprintf(stderr, "Device error, exiting.\n");
do_exit = 1;
return;
}
data_info->sampletype_signed = 1;
data_info->r_buf = txbuf;

View File

@ -419,6 +419,7 @@ void fm_modulator_stereo(int use_rds)
void fl2k_callback(fl2k_data_info_t *data_info)
{
if (data_info->device_error) {
fprintf(stderr, "Device error, exiting.\n");
do_exit = 1;
pthread_cond_signal(&fm_cond);
}

View File

@ -107,6 +107,12 @@ void fl2k_callback(fl2k_data_info_t *data_info)
int r;
struct timeval tv = { 1, 0 };
if (data_info->device_error) {
fprintf(stderr, "Device error, exiting.\n");
do_exit = 1;
return;
}
if (!connected)
return;
@ -122,6 +128,12 @@ void fl2k_callback(fl2k_data_info_t *data_info)
if (r) {
received = recv(sock, txbuf + (FL2K_BUF_LEN - left), left, 0);
if (!received) {
fprintf(stderr, "Connection was closed!\n");
fl2k_stop_tx(dev);
do_exit = 1;
}
left -= received;
}
}

View File

@ -213,6 +213,12 @@ static void ppm_test(uint32_t len)
void fl2k_callback(fl2k_data_info_t *data_info)
{
if (data_info->device_error) {
fprintf(stderr, "Device error, exiting.\n");
do_exit = 1;
return;
}
/* drop first couple of callbacks until everything is settled */
if (cb_cnt > 20) {
ppm_test(FL2K_BUF_LEN);
@ -222,7 +228,6 @@ void fl2k_callback(fl2k_data_info_t *data_info)
data_info->r_buf = buffer;
cb_cnt++;
}
}
int main(int argc, char **argv)
@ -295,9 +300,6 @@ int main(int argc, char **argv)
while (!do_exit)
sleep_ms(500);
if (do_exit)
fprintf(stderr, "\nUser cancel, exiting...\n");
exit:
fl2k_close(dev);
free(buffer);

View File

@ -529,6 +529,7 @@ static void LIBUSB_CALL _libusb_callback(struct libusb_transfer *xfer)
fl2k_xfer_info_t *next_xfer_info;
fl2k_dev_t *dev = (fl2k_dev_t *)xfer_info->dev;
struct libusb_transfer *next_xfer = NULL;
int r = 0;
if (LIBUSB_TRANSFER_COMPLETED == xfer->status) {
/* resubmit transfer */
@ -541,8 +542,7 @@ static void LIBUSB_CALL _libusb_callback(struct libusb_transfer *xfer)
/* Submit next filled transfer */
next_xfer_info->state = BUF_SUBMITTED;
libusb_submit_transfer(next_xfer);
r = libusb_submit_transfer(next_xfer);
xfer_info->state = BUF_EMPTY;
pthread_cond_signal(&dev->buf_cond);
} else {
@ -551,17 +551,21 @@ static void LIBUSB_CALL _libusb_callback(struct libusb_transfer *xfer)
* stops to output data and hangs
* (happens only in the hacked 'gapless'
* mode without HSYNC and VSYNC) */
libusb_submit_transfer(xfer);
r = libusb_submit_transfer(xfer);
pthread_cond_signal(&dev->buf_cond);
dev->underflow_cnt++;
}
}
} else if (LIBUSB_TRANSFER_CANCELLED != xfer->status) {
}
if (((LIBUSB_TRANSFER_CANCELLED != xfer->status) &&
(LIBUSB_TRANSFER_COMPLETED != xfer->status)) ||
(r == LIBUSB_ERROR_NO_DEVICE)) {
dev->dev_lost = 1;
fl2k_stop_tx(dev);
pthread_cond_signal(&dev->buf_cond);
fprintf(stderr, "cb transfer status: %d, "
"canceling...\n", xfer->status);
fprintf(stderr, "cb transfer status: %d, submit "
"transfer %d, canceling...\n", xfer->status, r);
}
}
@ -919,7 +923,6 @@ static void *fl2k_sample_worker(void *arg)
if (dev->dev_lost && dev->cb) {
data_info.device_error = 1;
dev->cb(&data_info);
fl2k_stop_tx(dev);
}
pthread_exit(NULL);