From afdfe12ddaa207d090ecc80cc87f70207df2420a Mon Sep 17 00:00:00 2001 From: Stefan Rueger Date: Sun, 18 Aug 2024 01:54:10 +0100 Subject: [PATCH] Reformat spacing/comments in src/dfu.c --- src/dfu.c | 307 +++++++++++++++++++++++++++--------------------------- 1 file changed, 151 insertions(+), 156 deletions(-) diff --git a/src/dfu.c b/src/dfu.c index 0aea930b..e5d9d870 100644 --- a/src/dfu.c +++ b/src/dfu.c @@ -30,12 +30,11 @@ #include "dfu.h" -#include "usbdevs.h" /* for USB_VENDOR_ATMEL */ +#include "usbdevs.h" // For USB_VENDOR_ATMEL -/* If we don't have LibUSB, define dummy functions that report an error. */ +// If we don't have LibUSB, define dummy functions that report an error #ifndef HAVE_LIBUSB - struct dfu_dev *dfu_open(const char *port_name) { pmsg_error("no USB support compiled for avrdude\n"); return NULL; @@ -46,11 +45,9 @@ int dfu_init(struct dfu_dev *dfu, unsigned short usb_vid, unsigned short usb_pid } void dfu_close(struct dfu_dev *dfu) { - /* nothing */ } -int dfu_getstatus(struct dfu_dev *dfu, struct dfu_status *status) -{ +int dfu_getstatus(struct dfu_dev *dfu, struct dfu_status *status) { return -1; } @@ -58,29 +55,28 @@ int dfu_clrstatus(struct dfu_dev *dfu) { return -1; } -int dfu_download(struct dfu_dev *dfu, void * ptr, int size) { +int dfu_download(struct dfu_dev *dfu, void *ptr, int size) { return -1; } -int dfu_upload(struct dfu_dev *dfu, void * ptr, int size) { +int dfu_upload(struct dfu_dev *dfu, void *ptr, int size) { return -1; } #else -/* If we DO have LibUSB, we can define the real functions. */ +// If we DO have LibUSB, we can define the real functions -/* DFU data structures and constants. - */ +// DFU data structures and constants -#define DFU_TIMEOUT 200 /* ms */ +#define DFU_TIMEOUT 200 // ms #define DFU_DNLOAD 1 #define DFU_UPLOAD 2 #define DFU_GETSTATUS 3 #define DFU_CLRSTATUS 4 -#define DFU_GETSTATE 5 /* FLIPv1 only; not used */ -#define DFU_ABORT 6 /* FLIPv1 only */ +#define DFU_GETSTATE 5 // FLIPv1 only; not used +#define DFU_ABORT 6 // FLIPv1 only /* Block counter global variable. Incremented each time a DFU_DNLOAD command * is sent to the device. @@ -89,7 +85,7 @@ int dfu_upload(struct dfu_dev *dfu, void * ptr, int size) { /* INTERNAL FUNCTION PROTOTYPES */ -static char * get_usb_string(usb_dev_handle * dev_handle, int index); +static char *get_usb_string(usb_dev_handle *dev_handle, int index); /* EXPORTED FUNCTION DEFINITIONS */ @@ -105,17 +101,17 @@ struct dfu_dev *dfu_open(const char *port_spec) { * function, where we actually open the device. */ - if (!str_starts(port_spec, "usb")) { + if(!str_starts(port_spec, "usb")) { pmsg_error("invalid port specification %s for USB device\n", port_spec); return NULL; } if(':' == port_spec[3]) { - bus_name = mmt_strdup(port_spec + 3 + 1); + bus_name = mmt_strdup(port_spec + 3 + 1); - dev_name = strchr(bus_name, ':'); - if(NULL != dev_name) - *dev_name++ = '\0'; + dev_name = strchr(bus_name, ':'); + if(NULL != dev_name) + *dev_name++ = '\0'; } /* Allocate the dfu_dev structure and save the bus_name and dev_name @@ -128,7 +124,7 @@ struct dfu_dev *dfu_open(const char *port_spec) { dfu->dev_name = dev_name; dfu->timeout = DFU_TIMEOUT; - /* LibUSB initialization. */ + // LibUSB initialization usb_init(); usb_find_busses(); @@ -137,8 +133,7 @@ struct dfu_dev *dfu_open(const char *port_spec) { return dfu; } -int dfu_init(struct dfu_dev *dfu, unsigned short vid, unsigned short pid) -{ +int dfu_init(struct dfu_dev *dfu, unsigned short vid, unsigned short pid) { struct usb_device *found = NULL; struct usb_device *dev; struct usb_bus *bus; @@ -152,7 +147,7 @@ int dfu_init(struct dfu_dev *dfu, unsigned short vid, unsigned short pid) * we use the default vendor and product id. */ - if (pid == 0 && dfu->dev_name == NULL) { + if(pid == 0 && dfu->dev_name == NULL) { pmsg_error("no DFU support for part; specify PID in config or USB address (via -P) to override\n"); return -1; } @@ -167,23 +162,23 @@ int dfu_init(struct dfu_dev *dfu, unsigned short vid, unsigned short pid) * id, the product id must match. */ - for (bus = usb_busses; !found && bus != NULL; bus = bus->next) { - for (dev = bus->devices; !found && dev != NULL; dev = dev->next) { - if (dfu->bus_name != NULL && !str_eq(bus->dirname, dfu->bus_name)) - continue; - if (dfu->dev_name != NULL) { - if (!str_eq(dev->filename, dfu->dev_name)) - continue; - } else if (vid != dev->descriptor.idVendor) + for(bus = usb_busses; !found && bus != NULL; bus = bus->next) { + for(dev = bus->devices; !found && dev != NULL; dev = dev->next) { + if(dfu->bus_name != NULL && !str_eq(bus->dirname, dfu->bus_name)) continue; - else if (pid != 0 && pid != dev->descriptor.idProduct) + if(dfu->dev_name != NULL) { + if(!str_eq(dev->filename, dfu->dev_name)) + continue; + } else if(vid != dev->descriptor.idVendor) + continue; + else if(pid != 0 && pid != dev->descriptor.idProduct) continue; found = dev; } } - if (found == NULL) { + if(found == NULL) { /* We could try to be more informative here. For example, we could report * why the match failed, and if we came across another DFU-capable part. */ @@ -193,79 +188,71 @@ int dfu_init(struct dfu_dev *dfu, unsigned short vid, unsigned short pid) } pmsg_notice2("found VID=0x%04x PID=0x%04x at %s:%s\n", - found->descriptor.idVendor, found->descriptor.idProduct, - found->bus->dirname, found->filename); + found->descriptor.idVendor, found->descriptor.idProduct, found->bus->dirname, found->filename); dfu->dev_handle = usb_open(found); - if (dfu->dev_handle == NULL) { + if(dfu->dev_handle == NULL) { pmsg_error("USB device at %s:%s: %s\n", found->bus->dirname, found->filename, usb_strerror()); return -1; } - /* Save device, configuration, interface and endpoint descriptors. */ + // Save device, configuration, interface and endpoint descriptors memcpy(&dfu->dev_desc, &found->descriptor, sizeof(dfu->dev_desc)); memcpy(&dfu->conf_desc, found->config, sizeof(dfu->conf_desc)); dfu->conf_desc.interface = NULL; - memcpy(&dfu->intf_desc, found->config->interface->altsetting, - sizeof(dfu->intf_desc)); + memcpy(&dfu->intf_desc, found->config->interface->altsetting, sizeof(dfu->intf_desc)); dfu->intf_desc.endpoint = &dfu->endp_desc; - if (found->config->interface->altsetting->endpoint != 0) - memcpy(&dfu->endp_desc, found->config->interface->altsetting->endpoint, - sizeof(dfu->endp_desc)); + if(found->config->interface->altsetting->endpoint != 0) + memcpy(&dfu->endp_desc, found->config->interface->altsetting->endpoint, sizeof(dfu->endp_desc)); - /* Get strings. */ + // Get strings - dfu->manf_str = get_usb_string(dfu->dev_handle, - dfu->dev_desc.iManufacturer); + dfu->manf_str = get_usb_string(dfu->dev_handle, dfu->dev_desc.iManufacturer); - dfu->prod_str = get_usb_string(dfu->dev_handle, - dfu->dev_desc.iProduct); + dfu->prod_str = get_usb_string(dfu->dev_handle, dfu->dev_desc.iProduct); - dfu->serno_str = get_usb_string(dfu->dev_handle, - dfu->dev_desc.iSerialNumber); + dfu->serno_str = get_usb_string(dfu->dev_handle, dfu->dev_desc.iSerialNumber); return 0; } -void dfu_close(struct dfu_dev *dfu) -{ - if (dfu->dev_handle != NULL) +void dfu_close(struct dfu_dev *dfu) { + if(dfu->dev_handle != NULL) usb_close(dfu->dev_handle); - if (dfu->bus_name != NULL) + if(dfu->bus_name != NULL) mmt_free(dfu->bus_name); - if (dfu->manf_str != NULL) + if(dfu->manf_str != NULL) mmt_free(dfu->manf_str); - if (dfu->prod_str != NULL) + if(dfu->prod_str != NULL) mmt_free(dfu->prod_str); - if (dfu->serno_str != NULL) + if(dfu->serno_str != NULL) mmt_free(dfu->serno_str); } -int dfu_getstatus(struct dfu_dev *dfu, struct dfu_status *status) -{ +int dfu_getstatus(struct dfu_dev *dfu, struct dfu_status *status) { int result; pmsg_trace("%s(): issuing control IN message\n", __func__); result = usb_control_msg(dfu->dev_handle, 0x80 | USB_TYPE_CLASS | USB_RECIP_INTERFACE, DFU_GETSTATUS, 0, 0, - (char*) status, sizeof(struct dfu_status), dfu->timeout); + (char *) status, sizeof(struct dfu_status), dfu->timeout); - if (result < 0) { + if(result < 0) { pmsg_error("unable to get DFU status: %s\n", usb_strerror()); return -1; } - if (result < (int) sizeof(struct dfu_status)) { + if(result < (int) sizeof(struct dfu_status)) { pmsg_error("unable to get DFU status: %s\n", "short read"); return -1; } - if (result > (int) sizeof(struct dfu_status)) { + if(result > (int) sizeof(struct dfu_status)) { pmsg_error("oversize read (should not happen)\n"); return -1; } @@ -273,23 +260,20 @@ int dfu_getstatus(struct dfu_dev *dfu, struct dfu_status *status) pmsg_trace("%s(): bStatus 0x%02x, bwPollTimeout %d, bState 0x%02x, iString %d\n", __func__, status->bStatus, status->bwPollTimeout[0] | (status->bwPollTimeout[1] << 8) | (status->bwPollTimeout[2] << 16), - status->bState, - status->iString); + status->bState, status->iString); return 0; } -int dfu_clrstatus(struct dfu_dev *dfu) -{ +int dfu_clrstatus(struct dfu_dev *dfu) { int result; pmsg_trace("%s(): issuing control OUT message\n", __func__); - result = usb_control_msg(dfu->dev_handle, - USB_TYPE_CLASS | USB_RECIP_INTERFACE, DFU_CLRSTATUS, 0, 0, - NULL, 0, dfu->timeout); + result = usb_control_msg(dfu->dev_handle, USB_TYPE_CLASS | USB_RECIP_INTERFACE, + DFU_CLRSTATUS, 0, 0, NULL, 0, dfu->timeout); - if (result < 0) { + if(result < 0) { pmsg_error("unable to clear DFU status: %s\n", usb_strerror()); return -1; } @@ -297,17 +281,15 @@ int dfu_clrstatus(struct dfu_dev *dfu) return 0; } -int dfu_abort(struct dfu_dev *dfu) -{ +int dfu_abort(struct dfu_dev *dfu) { int result; pmsg_trace("%s(): issuing control OUT message\n", __func__); - result = usb_control_msg(dfu->dev_handle, - USB_TYPE_CLASS | USB_RECIP_INTERFACE, DFU_ABORT, 0, 0, - NULL, 0, dfu->timeout); + result = usb_control_msg(dfu->dev_handle, USB_TYPE_CLASS | USB_RECIP_INTERFACE, + DFU_ABORT, 0, 0, NULL, 0, dfu->timeout); - if (result < 0) { + if(result < 0) { pmsg_error("unable to reset DFU state: %s\n", usb_strerror()); return -1; } @@ -315,29 +297,26 @@ int dfu_abort(struct dfu_dev *dfu) return 0; } - -int dfu_dnload(struct dfu_dev *dfu, void *ptr, int size) -{ +int dfu_dnload(struct dfu_dev *dfu, void *ptr, int size) { int result; - pmsg_trace("%s(): issuing control OUT message, wIndex = %d, ptr = %p, size = %d\n", __func__, - cx->dfu_wIndex, ptr, size); + pmsg_trace("%s(): issuing control OUT message, wIndex = %d, ptr = %p, size = %d\n", + __func__, cx->dfu_wIndex, ptr, size); - result = usb_control_msg(dfu->dev_handle, - USB_TYPE_CLASS | USB_RECIP_INTERFACE, DFU_DNLOAD, cx->dfu_wIndex++, 0, - ptr, size, dfu->timeout); + result = usb_control_msg(dfu->dev_handle, USB_TYPE_CLASS | USB_RECIP_INTERFACE, + DFU_DNLOAD, cx->dfu_wIndex++, 0, ptr, size, dfu->timeout); - if (result < 0) { + if(result < 0) { pmsg_error("DFU_DNLOAD failed: %s\n", usb_strerror()); return -1; } - if (result < size) { + if(result < size) { pmsg_error("DFU_DNLOAD failed: short write\n"); return -1; } - if (result > size) { + if(result > size) { pmsg_error("DFU_DNLOAD failed: oversize write (should not happen)\n"); return -1; } @@ -345,28 +324,26 @@ int dfu_dnload(struct dfu_dev *dfu, void *ptr, int size) return 0; } -int dfu_upload(struct dfu_dev *dfu, void *ptr, int size) -{ +int dfu_upload(struct dfu_dev *dfu, void *ptr, int size) { int result; - pmsg_trace("%s(): issuing control IN message, wIndex = %d, ptr = %p, size = %d\n", __func__, - cx->dfu_wIndex, ptr, size); + pmsg_trace("%s(): issuing control IN message, wIndex = %d, ptr = %p, size = %d\n", + __func__, cx->dfu_wIndex, ptr, size); - result = usb_control_msg(dfu->dev_handle, - 0x80 | USB_TYPE_CLASS | USB_RECIP_INTERFACE, DFU_UPLOAD, cx->dfu_wIndex++, 0, - ptr, size, dfu->timeout); + result = usb_control_msg(dfu->dev_handle, 0x80 | USB_TYPE_CLASS | USB_RECIP_INTERFACE, + DFU_UPLOAD, cx->dfu_wIndex++, 0, ptr, size, dfu->timeout); - if (result < 0) { + if(result < 0) { pmsg_error("DFU_UPLOAD failed: %s\n", usb_strerror()); return -1; } - if (result < size) { + if(result < size) { pmsg_error("DFU_UPLOAD failed: %s\n", "short read"); return -1; } - if (result > size) { + if(result > size) { pmsg_error("oversize read (should not happen)\n"); return -1; } @@ -374,100 +351,118 @@ int dfu_upload(struct dfu_dev *dfu, void *ptr, int size) return 0; } -void dfu_show_info(struct dfu_dev *dfu) -{ - if (dfu->manf_str != NULL) - msg_info(" USB Vendor : %s (0x%04hX)\n", - dfu->manf_str, (unsigned short) dfu->dev_desc.idVendor); +void dfu_show_info(struct dfu_dev *dfu) { + if(dfu->manf_str != NULL) + msg_info(" USB Vendor : %s (0x%04hX)\n", dfu->manf_str, (unsigned short) dfu->dev_desc.idVendor); else - msg_info(" USB Vendor : 0x%04hX\n", - (unsigned short) dfu->dev_desc.idVendor); + msg_info(" USB Vendor : 0x%04hX\n", (unsigned short) dfu->dev_desc.idVendor); - if (dfu->prod_str != NULL) - msg_info(" USB Product : %s (0x%04hX)\n", - dfu->prod_str, (unsigned short) dfu->dev_desc.idProduct); + if(dfu->prod_str != NULL) + msg_info(" USB Product : %s (0x%04hX)\n", dfu->prod_str, (unsigned short) dfu->dev_desc.idProduct); else - msg_info(" USB Product : 0x%04hX\n", - (unsigned short) dfu->dev_desc.idProduct); + msg_info(" USB Product : 0x%04hX\n", (unsigned short) dfu->dev_desc.idProduct); msg_info(" USB Release : %u.%u.%u\n", - (dfu->dev_desc.bcdDevice >> 8) & 0xFF, - (dfu->dev_desc.bcdDevice >> 4) & 0xF, - (dfu->dev_desc.bcdDevice >> 0) & 0xF); + (dfu->dev_desc.bcdDevice >> 8) & 0xFF, (dfu->dev_desc.bcdDevice >> 4) & 0xF, (dfu->dev_desc.bcdDevice >> 0) & 0xF); - if (dfu->serno_str != NULL) + if(dfu->serno_str != NULL) msg_info(" USB Serial No : %s\n", dfu->serno_str); } /* INTERNAL FUNCTION DEFINITIONS */ -char * get_usb_string(usb_dev_handle * dev_handle, int index) { +char *get_usb_string(usb_dev_handle *dev_handle, int index) { char buffer[256]; - char * str; + char *str; int result; - if (index == 0) + if(index == 0) return NULL; - result = usb_get_string_simple(dev_handle, index, buffer, sizeof(buffer)-1); + result = usb_get_string_simple(dev_handle, index, buffer, sizeof(buffer) - 1); - if (result < 0) { + if(result < 0) { cx->usb_access_error = 1; pmsg_error("unable to read USB device string %d: %s\n", index, usb_strerror()); return NULL; } - str = mmt_malloc(result+1); + str = mmt_malloc(result + 1); memcpy(str, buffer, result); str[result] = '\0'; return str; } - -#endif /* defined(HAVE_LIBUSB) */ +#endif // defined(HAVE_LIBUSB) /* EXPORTED FUNCTIONS THAT DO NO REQUIRE LIBUSB */ -const char * dfu_status_str(int bStatus) -{ +const char *dfu_status_str(int bStatus) { switch (bStatus) { - case DFU_STATUS_OK: return "OK"; - case DFU_STATUS_ERR_TARGET: return "ERR_TARGET"; - case DFU_STATUS_ERR_FILE: return "ERR_FILE"; - case DFU_STATUS_ERR_WRITE: return "ERR_WRITE"; - case DFU_STATUS_ERR_ERASE: return "ERR_ERASE"; - case DFU_STATUS_ERR_CHECK_ERASED: return "ERR_CHECK_ERASED"; - case DFU_STATUS_ERR_PROG: return "ERR_PROG"; - case DFU_STATUS_ERR_VERIFY: return "ERR_VERIFY"; - case DFU_STATUS_ERR_ADDRESS: return "ERR_ADDRESS"; - case DFU_STATUS_ERR_NOTDONE: return "ERR_NOTDONE"; - case DFU_STATUS_ERR_FIRMWARE: return "ERR_FIRMWARE"; - case DFU_STATUS_ERR_VENDOR: return "ERR_VENDOR"; - case DFU_STATUS_ERR_USBR: return "ERR_USBR"; - case DFU_STATUS_ERR_POR: return "ERR_POR"; - case DFU_STATUS_ERR_UNKNOWN: return "ERR_UNKNOWN"; - case DFU_STATUS_ERR_STALLEDPKT: return "ERR_STALLEDPKT"; - default: return "Unknown"; + case DFU_STATUS_OK: + return "OK"; + case DFU_STATUS_ERR_TARGET: + return "ERR_TARGET"; + case DFU_STATUS_ERR_FILE: + return "ERR_FILE"; + case DFU_STATUS_ERR_WRITE: + return "ERR_WRITE"; + case DFU_STATUS_ERR_ERASE: + return "ERR_ERASE"; + case DFU_STATUS_ERR_CHECK_ERASED: + return "ERR_CHECK_ERASED"; + case DFU_STATUS_ERR_PROG: + return "ERR_PROG"; + case DFU_STATUS_ERR_VERIFY: + return "ERR_VERIFY"; + case DFU_STATUS_ERR_ADDRESS: + return "ERR_ADDRESS"; + case DFU_STATUS_ERR_NOTDONE: + return "ERR_NOTDONE"; + case DFU_STATUS_ERR_FIRMWARE: + return "ERR_FIRMWARE"; + case DFU_STATUS_ERR_VENDOR: + return "ERR_VENDOR"; + case DFU_STATUS_ERR_USBR: + return "ERR_USBR"; + case DFU_STATUS_ERR_POR: + return "ERR_POR"; + case DFU_STATUS_ERR_UNKNOWN: + return "ERR_UNKNOWN"; + case DFU_STATUS_ERR_STALLEDPKT: + return "ERR_STALLEDPKT"; + default: + return "Unknown"; } } -const char * dfu_state_str(int bState) -{ +const char *dfu_state_str(int bState) { switch (bState) { - case DFU_STATE_APP_IDLE: return "APP_IDLE"; - case DFU_STATE_APP_DETACH: return "APP_DETACH"; - case DFU_STATE_DFU_IDLE: return "DFU_IDLE"; - case DFU_STATE_DFU_DLOAD_SYNC: return "DFU_DLOAD_SYNC"; - case DFU_STATE_DFU_DNBUSY: return "DFU_DNBUSY"; - case DFU_STATE_DFU_DNLOAD_IDLE: return "DFU_DNLOAD_IDLE"; - case DFU_STATE_DFU_MANIFEST_SYNC: return "DFU_MANIFEST_SYNC"; - case DFU_STATE_DFU_MANIFEST: return "DFU_MANIFEST"; - case DFU_STATE_DFU_MANIFEST_WAIT_RESET: return "DFU_MANIFEST_WAIT_RESET"; - case DFU_STATE_DFU_UPLOAD_IDLE: return "DFU_UPLOAD_IDLE"; - case DFU_STATE_DFU_ERROR: return "DFU_ERROR"; - default: return "Unknown"; + case DFU_STATE_APP_IDLE: + return "APP_IDLE"; + case DFU_STATE_APP_DETACH: + return "APP_DETACH"; + case DFU_STATE_DFU_IDLE: + return "DFU_IDLE"; + case DFU_STATE_DFU_DLOAD_SYNC: + return "DFU_DLOAD_SYNC"; + case DFU_STATE_DFU_DNBUSY: + return "DFU_DNBUSY"; + case DFU_STATE_DFU_DNLOAD_IDLE: + return "DFU_DNLOAD_IDLE"; + case DFU_STATE_DFU_MANIFEST_SYNC: + return "DFU_MANIFEST_SYNC"; + case DFU_STATE_DFU_MANIFEST: + return "DFU_MANIFEST"; + case DFU_STATE_DFU_MANIFEST_WAIT_RESET: + return "DFU_MANIFEST_WAIT_RESET"; + case DFU_STATE_DFU_UPLOAD_IDLE: + return "DFU_UPLOAD_IDLE"; + case DFU_STATE_DFU_ERROR: + return "DFU_ERROR"; + default: + return "Unknown"; } } -