Simplify code by reducing nesting level.

This commit is contained in:
Uwe Hermann 2010-04-09 19:25:29 +02:00
parent 6f5f21f996
commit f6958dabcd
1 changed files with 135 additions and 142 deletions

View File

@ -304,7 +304,9 @@ int upload_firmware(libusb_device *dev)
static void close_device(struct sigrok_device_instance *sdi) static void close_device(struct sigrok_device_instance *sdi)
{ {
if (sdi->usb->devhdl) { if (sdi->usb->devhdl == NULL)
return;
g_message("closing device %d on %d.%d interface %d", sdi->index, g_message("closing device %d on %d.%d interface %d", sdi->index,
sdi->usb->bus, sdi->usb->address, USB_INTERFACE); sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE); libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
@ -312,7 +314,6 @@ static void close_device(struct sigrok_device_instance *sdi)
sdi->usb->devhdl = NULL; sdi->usb->devhdl = NULL;
sdi->status = ST_INACTIVE; sdi->status = ST_INACTIVE;
} }
}
static int configure_probes(GSList * probes) static int configure_probes(GSList * probes)
{ {
@ -386,34 +387,28 @@ static int hw_init(char *deviceinfo)
continue; continue;
} }
if (des.idVendor == USB_VENDOR && des.idProduct == USB_PRODUCT) { if (des.idVendor != USB_VENDOR || des.idProduct != USB_PRODUCT)
/* Definitely a Saleae Logic... */ continue; /* Not a Saleae Logic... */
sdi = sigrok_device_instance_new(devcnt, sdi = sigrok_device_instance_new(devcnt, ST_INITIALIZING,
ST_INITIALIZING, USB_VENDOR_NAME, USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
USB_MODEL_NAME, USB_MODEL_VERSION);
if (!sdi) if (!sdi)
return 0; return 0;
device_instances = device_instances = g_slist_append(device_instances, sdi);
g_slist_append(device_instances, sdi);
if (check_conf_profile(devlist[i]) == 0) { if (check_conf_profile(devlist[i]) == 0) {
if (upload_firmware(devlist[i]) > 0)
/* /*
* Continue on the off chance that the * Continue on the off chance that the device is in a
* device is in a working state. * working state. TODO: Could maybe try a USB reset,
* TODO: Could maybe try a USB reset,
* or uploading the firmware again. * or uploading the firmware again.
*/ */
if (upload_firmware(devlist[i]) > 0)
g_warning("firmware upload failed for device %d", devcnt); g_warning("firmware upload failed for device %d", devcnt);
sdi->usb = usb_device_instance_new sdi->usb = usb_device_instance_new
(libusb_get_bus_number(devlist[i]), 0, NULL); (libusb_get_bus_number(devlist[i]), 0, NULL);
} else { } else {
/* /* Already has the firmware, so fix the new address. */
* Already has the firmware on it, so fix the
* new address.
*/
sdi->usb = usb_device_instance_new sdi->usb = usb_device_instance_new
(libusb_get_bus_number(devlist[i]), (libusb_get_bus_number(devlist[i]),
libusb_get_device_address(devlist[i]), libusb_get_device_address(devlist[i]),
@ -421,7 +416,6 @@ static int hw_init(char *deviceinfo)
} }
devcnt++; devcnt++;
} }
}
libusb_free_device_list(devlist, 1); libusb_free_device_list(devlist, 1);
return devcnt; return devcnt;
@ -617,18 +611,19 @@ void receive_transfer(struct libusb_transfer *transfer)
int cur_buflen, trigger_offset, i; int cur_buflen, trigger_offset, i;
unsigned char *cur_buf, *new_buf; unsigned char *cur_buf, *new_buf;
if (transfer == NULL) {
/* hw_stop_acquisition() is telling us to stop. */ /* hw_stop_acquisition() is telling us to stop. */
if (transfer == NULL)
num_samples = -1; num_samples = -1;
}
if (num_samples == -1) {
/* /*
* Acquisition has already ended, just free any queued up * If acquisition has already ended, just free any queued up
* transfer that come in. * transfer that come in.
*/ */
if (num_samples == -1) {
libusb_free_transfer(transfer); libusb_free_transfer(transfer);
} else { return;
}
g_message("receive_transfer(): status %d received %d bytes", g_message("receive_transfer(): status %d received %d bytes",
transfer->status, transfer->actual_length); transfer->status, transfer->actual_length);
@ -649,9 +644,9 @@ void receive_transfer(struct libusb_transfer *transfer)
if (cur_buflen == 0) { if (cur_buflen == 0) {
empty_transfer_count++; empty_transfer_count++;
if (empty_transfer_count > MAX_EMPTY_TRANSFERS) { if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
/* The FX2 gave up. End the acquisition, the /*
* frontend will work out that the samplecount * The FX2 gave up. End the acquisition, the frontend
* is short. * will work out that the samplecount is short.
*/ */
packet.type = DF_END; packet.type = DF_END;
session_bus(user_data, &packet); session_bus(user_data, &packet);
@ -668,8 +663,7 @@ void receive_transfer(struct libusb_transfer *transfer)
if ((cur_buf[i] & trigger_mask[trigger_stage]) if ((cur_buf[i] & trigger_mask[trigger_stage])
== trigger_value[trigger_stage]) { == trigger_value[trigger_stage]) {
/* Match on this trigger stage. */ /* Match on this trigger stage. */
trigger_buffer[trigger_stage] = trigger_buffer[trigger_stage] = cur_buf[i];
cur_buf[i];
trigger_stage++; trigger_stage++;
if (trigger_stage == NUM_TRIGGER_STAGES if (trigger_stage == NUM_TRIGGER_STAGES
|| trigger_mask[trigger_stage] == 0) { || trigger_mask[trigger_stage] == 0) {
@ -730,7 +724,6 @@ void receive_transfer(struct libusb_transfer *transfer)
*/ */
} }
} }
}
static int hw_start_acquisition(int device_index, gpointer session_device_id) static int hw_start_acquisition(int device_index, gpointer session_device_id)
{ {