Commit a76bbdb695daa43308602474873f7b662e5f394a
1 parent
d7b97b34
Add extra verbose logging and check return from libusb_control_transfer
Showing
1 changed file
with
26 additions
and
7 deletions
main.c
| ... | ... | @@ -155,8 +155,10 @@ int ep_read(void *buf, int len, libusb_device_handle * usb_device) |
| 155 | 155 | LIBUSB_REQUEST_TYPE_VENDOR | |
| 156 | 156 | LIBUSB_ENDPOINT_IN, 0, len & 0xffff, |
| 157 | 157 | len >> 16, buf, len, 1000); |
| 158 | - | |
| 159 | - return len; | |
| 158 | + if(ret >= 0) | |
| 159 | + return len; | |
| 160 | + else | |
| 161 | + return ret; | |
| 160 | 162 | } |
| 161 | 163 | |
| 162 | 164 | void get_options(int argc, char *argv[]) |
| ... | ... | @@ -254,7 +256,7 @@ int second_stage_boot(libusb_device_handle *usb_device) |
| 254 | 256 | sleep(1); |
| 255 | 257 | size = ep_read((unsigned char *)&retcode, sizeof(retcode), usb_device); |
| 256 | 258 | |
| 257 | - if (retcode == 0) | |
| 259 | + if (size > 0 && retcode == 0) | |
| 258 | 260 | { |
| 259 | 261 | printf("Successful read %d bytes \n", size); |
| 260 | 262 | } |
| ... | ... | @@ -303,23 +305,38 @@ int file_server(libusb_device_handle * usb_device) |
| 303 | 305 | |
| 304 | 306 | while(going) |
| 305 | 307 | { |
| 306 | - ep_read(&message, sizeof(message), usb_device); | |
| 307 | - if(verbose) printf("Received message %d: %s\n", message.command, message.fname); | |
| 308 | + char message_name[][20] = {"GetFileSize", "ReadFile", "Done"}; | |
| 309 | + int i = ep_read(&message, sizeof(message), usb_device); | |
| 310 | + if(i < 0) | |
| 311 | + { | |
| 312 | + sleep(1); | |
| 313 | + continue; | |
| 314 | + } | |
| 315 | + if(verbose) printf("Received message %s: %s\n", message_name[message.command], message.fname); | |
| 316 | + | |
| 317 | + // Done can also just be null filename | |
| 318 | + if(strlen(message.fname) == 0) | |
| 319 | + { | |
| 320 | + ep_write(NULL, 0, usb_device); | |
| 321 | + break; | |
| 322 | + } | |
| 323 | + | |
| 308 | 324 | switch(message.command) |
| 309 | 325 | { |
| 310 | 326 | case 0: // Get file size |
| 311 | 327 | if(fp) |
| 312 | 328 | fclose(fp); |
| 313 | 329 | fp = check_file(directory, message.fname); |
| 314 | - if(fp != NULL) | |
| 330 | + if(strlen(message.fname) && fp != NULL) | |
| 315 | 331 | { |
| 316 | 332 | int file_size; |
| 317 | - void *buf; | |
| 318 | 333 | |
| 319 | 334 | fseek(fp, 0, SEEK_END); |
| 320 | 335 | file_size = ftell(fp); |
| 321 | 336 | fseek(fp, 0, SEEK_SET); |
| 322 | 337 | |
| 338 | + if(verbose) printf("File size = %d bytes\n", file_size); | |
| 339 | + | |
| 323 | 340 | int sz = libusb_control_transfer(usb_device, LIBUSB_REQUEST_TYPE_VENDOR, 0, |
| 324 | 341 | file_size & 0xffff, file_size >> 16, NULL, 0, 1000); |
| 325 | 342 | |
| ... | ... | @@ -362,6 +379,7 @@ int file_server(libusb_device_handle * usb_device) |
| 362 | 379 | int sz = ep_write(buf, file_size, usb_device); |
| 363 | 380 | |
| 364 | 381 | fclose(fp); |
| 382 | + fp = NULL; | |
| 365 | 383 | |
| 366 | 384 | if(sz != file_size) |
| 367 | 385 | { |
| ... | ... | @@ -471,6 +489,7 @@ int main(int argc, char *argv[]) |
| 471 | 489 | } |
| 472 | 490 | |
| 473 | 491 | libusb_close(usb_device); |
| 492 | + sleep(5); | |
| 474 | 493 | } |
| 475 | 494 | while(loop); |
| 476 | 495 | ... | ... |