From b1e60a35dfd4020f4d890723c9af620ef5f63fef Mon Sep 17 00:00:00 2001 From: Paul Molloy Date: Sat, 19 Nov 2016 16:51:50 -0600 Subject: [PATCH] host\Makefile: fixing make debug, Zed's logic seems backwards to me. host\include\dbg.h: reversing DEBUG logic. host\source\inlprog.c: working through making code more robust with proper error checking via "Zed's AWESOME debug macros" ;) --- host/Makefile | 9 +++-- host/include/dbg.h | 6 +-- host/source/inlprog.c | 89 +++++++++++++++++++++++++++++-------------- 3 files changed, 69 insertions(+), 35 deletions(-) diff --git a/host/Makefile b/host/Makefile index 29a48c7..1c8c5fd 100644 --- a/host/Makefile +++ b/host/Makefile @@ -1,5 +1,5 @@ #compiler flags: -Wall Warnings all, -g gdb debug markings, -CFLAGS=-Wall -g -O +CFLAGS=-Wall -O INCLUDE= -I ./include WINLIB= -L ./winlib LIBUSB= -lusb-1.0 @@ -20,12 +20,13 @@ unix: $(CC) $(INCLUDE) $(SOURCES) -o $(TARGET) $(CFLAGS) $(LIBUSB) # "make debug" will build program with debug print messages -# -DNDEBUG show debug logs -debug: CFLAGS += -DNDEBUG +# -DDEBUG show debug logs +# -g build with gdb debug tags +debug: CFLAGS += -DDEBUG -g debug: all #unix debug build -unixdebug: CFLAGS += -DNDEBUG +unixdebug: CFLAGS += -DDEBUG -g unixdebug: unix #clean on unix and windows(.exe) diff --git a/host/include/dbg.h b/host/include/dbg.h index 0a7bfac..a7663e7 100644 --- a/host/include/dbg.h +++ b/host/include/dbg.h @@ -5,11 +5,11 @@ #include #include -#ifdef NDEBUG -#define debug(M, ...) -#else +#ifdef DEBUG #define debug(M, ...) fprintf(stderr, "DEBUG %s:%d: " M "\n",\ __FILE__, __LINE__, ##__VA_ARGS__) +#else +#define debug(M, ...) #endif #define clean_errno() (errno == 0 ? "None" : strerror(errno)) diff --git a/host/source/inlprog.c b/host/source/inlprog.c index 0370a9f..4323ba9 100644 --- a/host/source/inlprog.c +++ b/host/source/inlprog.c @@ -5,6 +5,11 @@ #include #include +//uncomment to DEBUG this file alone +//#define DEBUG +//"make debug" to get DEBUG msgs on entire program +#include "dbg.h" + //control transfer request types //uint8_t libusb_control_setup::bmRequestType //Request type. @@ -35,20 +40,19 @@ int main(int argc, char *argv[]) { - int error = 0; //context set to NULL since only acting as single user of libusb libusb_context *context = NULL; + debug("Initalizing libusb"); //initialize libusb must be called prior to any other libusb function //returns 0 on success LIBUSB_ERROR code on failure //int libusb_init ( libusb_context ** context) - error = libusb_init(&context); - if (error) - return error; + int usb_init = libusb_init(&context); + check( usb_init == LIBUSB_SUCCESS, "Failed to initialize libusb: %s", libusb_strerror(usb_init)); //void libusb_set_debug ( libusb_context * ctx, int level ) - libusb_set_debug(context, LIBUSB_LOG_LEVEL_NONE); + libusb_set_debug(context, LIBUSB_LOG_LEVEL_ERROR); //LIBUSB_LOG_LEVEL_NONE (0) : no messages ever printed by the library (default) //LIBUSB_LOG_LEVEL_ERROR (1) : error messages are printed to stderr //LIBUSB_LOG_LEVEL_WARNING (2) : warning and error messages are printed to stderr @@ -61,14 +65,13 @@ int main(int argc, char *argv[]) libusb_device **device_list = NULL; //ssize_t libusb_get_device_list (libusb_context *ctx, libusb_device ***list) // Returns a list of USB devices currently attached to the system. - // return value is number of devices plus one as list is null terminated - ssize_t count = libusb_get_device_list( context, &device_list); - if (count < 0) - //TODO error returns libusb error code - return count; + // return value is number of devices plus one as list is null terminated, or LIBUSB_ERROR if negative. + // Must free device list after done with it + ssize_t dev_count = libusb_get_device_list( context, &device_list); + check( dev_count >= 0, "libusb unable to find any devices: %s", libusb_strerror(dev_count)); + int rv = 0; ssize_t i = 0; - error = 0; libusb_device *retroprog = NULL; libusb_device *device = NULL; @@ -76,29 +79,25 @@ int main(int argc, char *argv[]) libusb_device_handle *handle = NULL; unsigned char str[256]; -// printf("searching %d devices\n", count-1); - for( i=0; i 0) { printf("manf_ascii: %s\n",str); @@ -125,10 +124,12 @@ int main(int argc, char *argv[]) break; } } + check( retroprog != NULL, "Could not find INL retro-prog USB device"); //free device list now that INL retro-prog was found and opened //void libusb_free_device_list ( libusb_device ** list, int unref_devices ) libusb_free_device_list( device_list, 1); //don't completely understand the unref_devices = 1... + device_list = NULL; //denote that device list is free if end up in error //Guess this is what you're supposed to do.. // the process of opening a device can be viewed as follows: // @@ -226,12 +227,44 @@ int main(int argc, char *argv[]) exit(1); } + //free device list if it was left open + if (device_list) { + printf("Whoops! freeing device list\n"); + libusb_free_device_list( device_list, 1); + } //must close device before exiting libusb_close(handle); + handle = NULL; //delete handle reference so error won't retry closing //deinitialize libusb to be called after closing all devices and before teminating application libusb_exit(context); + return 0; +error: + printf("program went to error\n"); + + //must close device before exiting + // libusb_close(handle); + + if (device_list) { + printf("freeing device list\n"); + libusb_free_device_list( device_list, 1); + } + + if (handle) { + printf("closing usb device\n"); + libusb_close(handle); + } + + if (usb_init == LIBUSB_SUCCESS) { + //deinitialize libusb to be called after closing all devices and before teminating application + printf("exiting libusb\n"); + libusb_exit(context); + } + + + return 1; + }