Harmonise indentation styles after merging code from usb_audio_testing

This commit is contained in:
Sam Chesney
2017-06-15 14:55:00 +01:00
parent 90885475e4
commit 595d26c6f2

View File

@@ -6,7 +6,8 @@
/* the device's vendor and product id */ /* the device's vendor and product id */
#define XMOS_VID 0x20b1 #define XMOS_VID 0x20b1
typedef struct device_pid_t { typedef struct device_pid_t
{
const char *device_name; const char *device_name;
unsigned int pid; unsigned int pid;
} device_pid_t; } device_pid_t;
@@ -54,7 +55,8 @@ static int find_xmos_device(unsigned int id, unsigned int pid, unsigned int list
int found = 0; int found = 0;
size_t count = libusb_get_device_list(NULL, &devs); size_t count = libusb_get_device_list(NULL, &devs);
if ((int)count < 0) { if ((int)count < 0)
{
printf("ERROR: get_device_list returned %d\n", (int)count); printf("ERROR: get_device_list returned %d\n", (int)count);
exit(1); exit(1);
} }
@@ -115,38 +117,45 @@ static int find_xmos_device(unsigned int id, unsigned int pid, unsigned int list
return devh ? 0 : -1; return devh ? 0 : -1;
} }
int xmos_dfu_resetdevice(void) { int xmos_dfu_resetdevice(void)
{
libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_RESETDEVICE, 0, 0, NULL, 0, 0); libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_RESETDEVICE, 0, 0, NULL, 0, 0);
return 0; return 0;
} }
int xmos_dfu_revertfactory(void) { int xmos_dfu_revertfactory(void)
{
libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_REVERTFACTORY, 0, 0, NULL, 0, 0); libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_REVERTFACTORY, 0, 0, NULL, 0, 0);
return 0; return 0;
} }
int xmos_dfu_resetintodfu(unsigned int interface) { int xmos_dfu_resetintodfu(unsigned int interface)
{
libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_RESETINTODFU, 0, interface, NULL, 0, 0); libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_RESETINTODFU, 0, interface, NULL, 0, 0);
return 0; return 0;
} }
int xmos_dfu_resetfromdfu(unsigned int interface) { int xmos_dfu_resetfromdfu(unsigned int interface)
{
libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_RESETFROMDFU, 0, interface, NULL, 0, 0); libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_RESETFROMDFU, 0, interface, NULL, 0, 0);
return 0; return 0;
} }
int dfu_detach(unsigned int interface, unsigned int timeout) { int dfu_detach(unsigned int interface, unsigned int timeout)
{
libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, DFU_DETACH, timeout, interface, NULL, 0, 0); libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, DFU_DETACH, timeout, interface, NULL, 0, 0);
return 0; return 0;
} }
int dfu_getState(unsigned int interface, unsigned char *state) { int dfu_getState(unsigned int interface, unsigned char *state)
{
libusb_control_transfer(devh, DFU_REQUEST_FROM_DEV, DFU_GETSTATE, 0, interface, state, 1, 0); libusb_control_transfer(devh, DFU_REQUEST_FROM_DEV, DFU_GETSTATE, 0, interface, state, 1, 0);
return 0; return 0;
} }
int dfu_getStatus(unsigned int interface, unsigned char *state, unsigned int *timeout, int dfu_getStatus(unsigned int interface, unsigned char *state, unsigned int *timeout,
unsigned char *nextState, unsigned char *strIndex) { unsigned char *nextState, unsigned char *strIndex)
{
unsigned int data[2]; unsigned int data[2];
libusb_control_transfer(devh, DFU_REQUEST_FROM_DEV, DFU_GETSTATUS, 0, interface, (unsigned char *)data, 6, 0); libusb_control_transfer(devh, DFU_REQUEST_FROM_DEV, DFU_GETSTATUS, 0, interface, (unsigned char *)data, 6, 0);
@@ -157,43 +166,50 @@ int dfu_getStatus(unsigned int interface, unsigned char *state, unsigned int *ti
return 0; return 0;
} }
int dfu_clrStatus(unsigned int interface) { int dfu_clrStatus(unsigned int interface)
{
libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, DFU_CLRSTATUS, 0, interface, NULL, 0, 0); libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, DFU_CLRSTATUS, 0, interface, NULL, 0, 0);
return 0; return 0;
} }
int dfu_abort(unsigned int interface) { int dfu_abort(unsigned int interface)
{
libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, DFU_ABORT, 0, interface, NULL, 0, 0); libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, DFU_ABORT, 0, interface, NULL, 0, 0);
return 0; return 0;
} }
int xmos_dfu_save_state(unsigned int interface) { int xmos_dfu_save_state(unsigned int interface)
{
libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_SAVESTATE, 0, interface, NULL, 0, 0); libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_SAVESTATE, 0, interface, NULL, 0, 0);
printf("Save state command sent\n"); printf("Save state command sent\n");
return 0; return 0;
} }
int xmos_dfu_restore_state(unsigned int interface) { int xmos_dfu_restore_state(unsigned int interface)
{
libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_RESTORESTATE, 0, interface, NULL, 0, 0); libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_RESTORESTATE, 0, interface, NULL, 0, 0);
printf("Restore state command sent\n"); printf("Restore state command sent\n");
return 0; return 0;
} }
int dfu_download(unsigned int interface, unsigned int block_num, unsigned int size, unsigned char *data) { int dfu_download(unsigned int interface, unsigned int block_num, unsigned int size, unsigned char *data)
{
//printf("... Downloading block number %d size %d\r", block_num, size); //printf("... Downloading block number %d size %d\r", block_num, size);
/* Returns actual data size transferred */ /* Returns actual data size transferred */
unsigned int transfered = libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, DFU_DNLOAD, block_num, interface, data, size, 0); unsigned int transfered = libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, DFU_DNLOAD, block_num, interface, data, size, 0);
return transfered; return transfered;
} }
int dfu_upload(unsigned int interface, unsigned int block_num, unsigned int size, unsigned char*data) { int dfu_upload(unsigned int interface, unsigned int block_num, unsigned int size, unsigned char*data)
{
unsigned int numBytes = 0; unsigned int numBytes = 0;
numBytes = libusb_control_transfer(devh, DFU_REQUEST_FROM_DEV, DFU_UPLOAD, block_num, interface, (unsigned char *)data, size, 0); numBytes = libusb_control_transfer(devh, DFU_REQUEST_FROM_DEV, DFU_UPLOAD, block_num, interface, (unsigned char *)data, size, 0);
return numBytes; return numBytes;
} }
int write_dfu_image(char *file) { int write_dfu_image(char *file)
{
int i = 0; int i = 0;
FILE* inFile = NULL; FILE* inFile = NULL;
int image_size = 0; int image_size = 0;
@@ -209,20 +225,23 @@ int write_dfu_image(char *file) {
unsigned int dfuBlockCount = 0; unsigned int dfuBlockCount = 0;
inFile = fopen( file, "rb" ); inFile = fopen( file, "rb" );
if( inFile == NULL ) { if( inFile == NULL )
{
fprintf(stderr,"Error: Failed to open input data file.\n"); fprintf(stderr,"Error: Failed to open input data file.\n");
return -1; return -1;
} }
/* Discover the size of the image. */ /* Discover the size of the image. */
if( 0 != fseek( inFile, 0, SEEK_END ) ) { if( 0 != fseek( inFile, 0, SEEK_END ) )
{
fprintf(stderr,"Error: Failed to discover input data file size.\n"); fprintf(stderr,"Error: Failed to discover input data file size.\n");
return -1; return -1;
} }
image_size = (int)ftell( inFile ); image_size = (int)ftell( inFile );
if( 0 != fseek( inFile, 0, SEEK_SET ) ) { if( 0 != fseek( inFile, 0, SEEK_SET ) )
{
fprintf(stderr,"Error: Failed to input file pointer.\n"); fprintf(stderr,"Error: Failed to input file pointer.\n");
return -1; return -1;
} }
@@ -234,7 +253,8 @@ int write_dfu_image(char *file) {
dfuBlockCount = 0; dfuBlockCount = 0;
for (i = 0; i < num_blocks; i++) { for (i = 0; i < num_blocks; i++)
{
memset(block_data, 0x0, block_size); memset(block_data, 0x0, block_size);
fread(block_data, 1, block_size, inFile); fread(block_data, 1, block_size, inFile);
int transferred = dfu_download(0, dfuBlockCount, block_size, block_data); int transferred = dfu_download(0, dfuBlockCount, block_size, block_data);
@@ -249,7 +269,8 @@ int write_dfu_image(char *file) {
dfuBlockCount++; dfuBlockCount++;
} }
if (remainder) { if (remainder)
{
memset(block_data, 0x0, block_size); memset(block_data, 0x0, block_size);
fread(block_data, 1, remainder, inFile); fread(block_data, 1, remainder, inFile);
dfu_download(0, dfuBlockCount, block_size, block_data); dfu_download(0, dfuBlockCount, block_size, block_data);
@@ -263,27 +284,32 @@ int write_dfu_image(char *file) {
printf("... Download complete\n"); printf("... Download complete\n");
return 0; return 0;
} }
int read_dfu_image(char *file) { int read_dfu_image(char *file)
{
FILE *outFile = NULL; FILE *outFile = NULL;
unsigned int block_count = 0; unsigned int block_count = 0;
unsigned int block_size = 64; unsigned int block_size = 64;
unsigned char block_data[64]; unsigned char block_data[64];
outFile = fopen( file, "wb" ); outFile = fopen( file, "wb" );
if( outFile == NULL ) { if( outFile == NULL )
{
fprintf(stderr,"Error: Failed to open output data file.\n"); fprintf(stderr,"Error: Failed to open output data file.\n");
return -1; return -1;
} }
printf("... Uploading image (%s) from device\n", file); printf("... Uploading image (%s) from device\n", file);
while (1) { while (1)
{
unsigned int numBytes = 0; unsigned int numBytes = 0;
numBytes = dfu_upload(0, block_count, 64, block_data); numBytes = dfu_upload(0, block_count, 64, block_data);
if (numBytes == 0) if (numBytes == 0)
{
break; break;
}
fwrite(block_data, 1, block_size, outFile); fwrite(block_data, 1, block_size, outFile);
block_count++; block_count++;
} }
@@ -292,14 +318,16 @@ int read_dfu_image(char *file) {
return 0; return 0;
} }
static void print_device_list(FILE *file, const char *indent) { static void print_device_list(FILE *file, const char *indent)
for (int i = 0; i < sizeof(pidList)/sizeof(pidList[0]); i++) { {
for (int i = 0; i < sizeof(pidList)/sizeof(pidList[0]); i++)
{
fprintf(file, "%s%-30s (0x%0x)\n", indent, pidList[i].device_name, pidList[i].pid); fprintf(file, "%s%-30s (0x%0x)\n", indent, pidList[i].device_name, pidList[i].pid);
} }
} }
static void print_usage(const char *program_name, const char *error_msg) { static void print_usage(const char *program_name, const char *error_msg)
{
fprintf(stderr, "ERROR: %s\n\n", error_msg); fprintf(stderr, "ERROR: %s\n\n", error_msg);
fprintf(stderr, "Usage:\n"); fprintf(stderr, "Usage:\n");
fprintf(stderr, " %s --listdevices\n", program_name); fprintf(stderr, " %s --listdevices\n", program_name);
@@ -318,17 +346,21 @@ static void print_usage(const char *program_name, const char *error_msg) {
exit(1); exit(1);
} }
static unsigned int select_pid(char *device_pid) { static unsigned int select_pid(char *device_pid)
{
// Try interpreting the name as a hex value // Try interpreting the name as a hex value
char * endptr = device_pid; char * endptr = device_pid;
int pid = strtol(device_pid, &endptr, 16); int pid = strtol(device_pid, &endptr, 16);
if (endptr != device_pid && *endptr == '\0') { if (endptr != device_pid && *endptr == '\0')
{
return pid; return pid;
} }
// Otherwise do a lookup of names // Otherwise do a lookup of names
for (int i = 0; i < sizeof(pidList)/sizeof(pidList[0]); i++) { for (int i = 0; i < sizeof(pidList)/sizeof(pidList[0]); i++)
if (strcmp(device_pid, pidList[i].device_name) == 0) { {
if (strcmp(device_pid, pidList[i].device_name) == 0)
{
return pidList[i].pid; return pidList[i].pid;
} }
} }
@@ -338,7 +370,8 @@ static unsigned int select_pid(char *device_pid) {
return 0; return 0;
} }
int main(int argc, char **argv) { int main(int argc, char **argv)
{
unsigned char dfuState = 0; unsigned char dfuState = 0;
unsigned char nextDfuState = 0; unsigned char nextDfuState = 0;
unsigned int timeout = 0; unsigned int timeout = 0;
@@ -356,39 +389,50 @@ int main(int argc, char **argv) {
const char *program_name = argv[0]; const char *program_name = argv[0];
int r = libusb_init(NULL); int r = libusb_init(NULL);
if (r < 0) { if (r < 0)
{
fprintf(stderr, "failed to initialise libusb\n"); fprintf(stderr, "failed to initialise libusb\n");
return -1; return -1;
} }
if (argc == 2) { if (argc == 2)
if (strcmp(argv[1], "--listdevices") == 0) { {
if (strcmp(argv[1], "--listdevices") == 0)
{
find_xmos_device(0, 0, 1); find_xmos_device(0, 0, 1);
return 0; return 0;
} }
print_usage(program_name, "Not enough options passed to dfu application"); print_usage(program_name, "Not enough options passed to dfu application");
} }
if (argc < 3) { if (argc < 3)
{
print_usage(program_name, "Not enough options passed to dfu application"); print_usage(program_name, "Not enough options passed to dfu application");
} }
char *device_pid = argv[1]; char *device_pid = argv[1];
char *command = argv[2]; char *command = argv[2];
if (strcmp(command, "--download") == 0) { if (strcmp(command, "--download") == 0)
if (argc < 4) { {
if (argc < 4)
{
print_usage(program_name, "No filename specified for download option"); print_usage(program_name, "No filename specified for download option");
} }
firmware_filename = argv[3]; firmware_filename = argv[3];
download = 1; download = 1;
} else if (strcmp(command, "--upload") == 0) { }
if (argc < 4) { else if (strcmp(command, "--upload") == 0)
{
if (argc < 4)
{
print_usage(program_name, "No filename specified for upload option"); print_usage(program_name, "No filename specified for upload option");
} }
firmware_filename = argv[3]; firmware_filename = argv[3];
upload = 1; upload = 1;
} else if (strcmp(command, "--revertfactory") == 0) { }
else if (strcmp(command, "--revertfactory") == 0)
{
revert = 1; revert = 1;
} }
else if(strcmp(command, "--savecustomstate") == 0) else if(strcmp(command, "--savecustomstate") == 0)
@@ -399,12 +443,14 @@ int main(int argc, char **argv) {
{ {
restore = 1; restore = 1;
} }
else { else
{
print_usage(program_name, "Invalid option passed to dfu application"); print_usage(program_name, "Invalid option passed to dfu application");
} }
unsigned int pid = select_pid(device_pid); unsigned int pid = select_pid(device_pid);
if (pid == 0) { if (pid == 0)
{
return -1; return -1;
} }
//#define START_IN_DFU 1 //#define START_IN_DFU 1
@@ -417,7 +463,8 @@ int main(int argc, char **argv) {
} }
r = libusb_claim_interface(devh, XMOS_DFU_IF); r = libusb_claim_interface(devh, XMOS_DFU_IF);
if (r < 0) { if (r < 0)
{
fprintf(stderr, "Error claiming interface %d %d\n", XMOS_DFU_IF, r); fprintf(stderr, "Error claiming interface %d %d\n", XMOS_DFU_IF, r);
return -1; return -1;
} }
@@ -451,7 +498,8 @@ int main(int argc, char **argv) {
// NOW IN DFU APPLICATION MODE // NOW IN DFU APPLICATION MODE
r = find_xmos_device(0, pid, 0); r = find_xmos_device(0, pid, 0);
if (r < 0) { if (r < 0)
{
fprintf(stderr, "Could not find/open device\n"); fprintf(stderr, "Could not find/open device\n");
return -1; return -1;
} }
@@ -478,33 +526,38 @@ int main(int argc, char **argv) {
default: default:
printf("Unknown error code: %d\n", r); printf("Unknown error code: %d\n", r);
break; break;
} }
return -1; return -1;
} }
printf("... DFU firmware upgrade device opened\n"); printf("... DFU firmware upgrade device opened\n");
if (download) { if (download)
{
write_dfu_image(firmware_filename); write_dfu_image(firmware_filename);
xmos_dfu_resetfromdfu(XMOS_DFU_IF); xmos_dfu_resetfromdfu(XMOS_DFU_IF);
} else if (upload) { }
else if (upload)
{
read_dfu_image(firmware_filename); read_dfu_image(firmware_filename);
xmos_dfu_resetfromdfu(XMOS_DFU_IF); xmos_dfu_resetfromdfu(XMOS_DFU_IF);
} else if (revert) { }
else if (revert)
{
printf("... Reverting device to factory image\n"); printf("... Reverting device to factory image\n");
xmos_dfu_revertfactory(); xmos_dfu_revertfactory();
// Give device time to revert firmware // Give device time to revert firmware
system("sleep 2"); system("sleep 2");
xmos_dfu_resetfromdfu(XMOS_DFU_IF); xmos_dfu_resetfromdfu(XMOS_DFU_IF);
} }
else{ else
{
xmos_dfu_resetfromdfu(XMOS_DFU_IF); xmos_dfu_resetfromdfu(XMOS_DFU_IF);
} }
printf("... Returning device to application mode\n"); printf("... Returning device to application mode\n");
} }
// END OF DFU APPLICATION MODE // END OF DFU APPLICATION MODE
libusb_release_interface(devh, 0); libusb_release_interface(devh, 0);
libusb_close(devh); libusb_close(devh);