diff --git a/lib/libusbx52/libusbx52.h b/lib/libusbx52/libusbx52.h index ed69519..b7326c1 100644 --- a/lib/libusbx52/libusbx52.h +++ b/lib/libusbx52/libusbx52.h @@ -35,19 +35,35 @@ struct libusb_device_handle { }; /** - * @brief File location of the device list file + * @brief Device list file environment variable + * + * This is used by the test driver to create a temporary environment for + * the device list + */ +#define INPUT_DEVICE_LIST_ENV "LIBUSBX52_DEVICE_LIST" + +/** + * @brief Default file location of the device list file * * This file contains a list of VIDs and PIDs in hexadecimal format separated * by spaces. There must be an even number of entries, each pair corresponding * to a (VID, PID) tuple identifying a single USB device. */ -#define INPUT_DEVICE_LIST_FILE "/tmp/libusbx52_device_list" +#define DEFAULT_INPUT_DEVICE_LIST_FILE "/tmp/libusbx52_device_list" /** - * @brief File location of the communication data file + * @brief Output data environment variable + * + * This is used by the test driver to create a temporary environment for + * the output data + */ +#define OUTPUT_DATA_FILE_ENV "LIBUSBX52_OUTPUT_DATA" + +/** + * @brief Default file location of the communication data file * * This file contains the libusb APIs called by libx52, after a device has * been opened, i.e., all APIs that operate on a libusb_device_handle */ -#define OUTPUT_DATA_FILE "/tmp/libusbx52_output_data" +#define DEFAULT_OUTPUT_DATA_FILE "/tmp/libusbx52_output_data" diff --git a/lib/libusbx52/usb_x52_stub.c b/lib/libusbx52/usb_x52_stub.c index 609bc33..83ebd30 100644 --- a/lib/libusbx52/usb_x52_stub.c +++ b/lib/libusbx52/usb_x52_stub.c @@ -21,6 +21,7 @@ int libusb_init(libusb_context **ctx) int vid; int pid; int parsed; + char *dev_list_file; FILE *dev_list; int i; @@ -37,7 +38,15 @@ int libusb_init(libusb_context **ctx) goto init_err_recovery; } - dev_list = fopen(INPUT_DEVICE_LIST_FILE, "r"); + /* + * Get the device file name from the environment, + * use a default name if unset or empty + */ + dev_list_file = getenv(INPUT_DEVICE_LIST_ENV); + if (dev_list_file == NULL || dev_list_file[0] == '\0') { + dev_list_file = DEFAULT_INPUT_DEVICE_LIST_FILE; + } + dev_list = fopen(dev_list_file, "r"); if (dev_list == NULL) { rc = LIBUSB_ERROR_IO; goto init_err_recovery; @@ -182,6 +191,8 @@ int libusb_get_device_descriptor(libusb_device *dev, int libusb_open(libusb_device *dev, libusb_device_handle **handle) { + char *output_data_file; + /* Allocate a handle for the application */ libusb_device_handle *tmp_hdl = calloc(1, sizeof(*tmp_hdl)); if (tmp_hdl == NULL) { @@ -194,7 +205,16 @@ int libusb_open(libusb_device *dev, libusb_device_handle **handle) /* Populate the handle structure with the right values */ tmp_hdl->ctx = dev->context; tmp_hdl->dev = dev; - tmp_hdl->packet_data_file = fopen(OUTPUT_DATA_FILE, "w"); + + /* + * Get the name of the output data file from the environment, + * use a default name if the environment variable is unset or empty + */ + output_data_file = getenv(OUTPUT_DATA_FILE_ENV); + if (output_data_file == NULL || output_data_file[0] == '\0') { + output_data_file = DEFAULT_OUTPUT_DATA_FILE; + } + tmp_hdl->packet_data_file = fopen(output_data_file, "w"); /* Make sure that the file opened correctly */ if (tmp_hdl->packet_data_file == NULL) { diff --git a/lib/libusbx52/util/create_device_list.c b/lib/libusbx52/util/create_device_list.c index e2c3591..419e624 100644 --- a/lib/libusbx52/util/create_device_list.c +++ b/lib/libusbx52/util/create_device_list.c @@ -12,12 +12,14 @@ */ #include +#include #include #include #include "libusbx52.h" int main(int argc, char *argv[]) { + char *data_file; FILE *data; char **id_pair; int vid; @@ -25,10 +27,13 @@ int main(int argc, char *argv[]) int parsed; int i; - data = fopen(INPUT_DEVICE_LIST_FILE, "w"); + data_file = getenv(INPUT_DEVICE_LIST_ENV); + if (data_file == NULL || data_file[0] == '\0') { + data_file = DEFAULT_INPUT_DEVICE_LIST_FILE; + } + data = fopen(data_file, "w"); if (data == NULL) { - fprintf(stderr, "Unable to open %s for writing\n", - INPUT_DEVICE_LIST_FILE); + fprintf(stderr, "Unable to open %s for writing\n", data_file); fprintf(stderr, "%s\n", strerror(errno)); }