mirror of https://github.com/nirenjan/libx52.git
				
				
				
			Move opening from environment into separate function
							parent
							
								
									d53e56c491
								
							
						
					
					
						commit
						c9ffb415c8
					
				|  | @ -14,17 +14,17 @@ CC = $(PTHREAD_CC) | |||
| # libusb stub library for use by test programs
 | ||||
| check_LTLIBRARIES = libusbx52.la | ||||
| 
 | ||||
| libusbx52_la_SOURCES = usb_x52_stub.c | ||||
| libusbx52_la_SOURCES = usb_x52_stub.c fopen_env.c | ||||
| libusbx52_la_CFLAGS = @LIBUSB_CFLAGS@ | ||||
| libusbx52_la_LDFLAGS = -rpath /nowhere -module | ||||
| 
 | ||||
| # Utility programs for use by tests
 | ||||
| check_PROGRAMS = x52test_create_device_list x52test_log_actions | ||||
| 
 | ||||
| x52test_create_device_list_SOURCES = util/create_device_list.c | ||||
| x52test_create_device_list_SOURCES = util/create_device_list.c $(libusbx52_la_SOURCES) | ||||
| x52test_create_device_list_CFLAGS = @LIBUSB_CFLAGS@ | ||||
| 
 | ||||
| x52test_log_actions_SOURCES = util/log_actions.c usb_x52_stub.c | ||||
| x52test_log_actions_SOURCES = util/log_actions.c $(libusbx52_la_SOURCES) | ||||
| x52test_log_actions_CFLAGS = @X52_INCLUDE@ @LIBUSB_CFLAGS@ | ||||
| 
 | ||||
| EXTRA_DIST = README.md libusbx52.h | ||||
|  |  | |||
|  | @ -0,0 +1,22 @@ | |||
| /*
 | ||||
|  * LibUSB stub driver for testing the Saitek X52/X52 Pro | ||||
|  * Common functionality | ||||
|  * | ||||
|  * Copyright (C) 2020 Nirenjan Krishnan (nirenjan@nirenjan.org) | ||||
|  * | ||||
|  * SPDX-License-Identifier: GPL-2.0-only WITH Classpath-exception-2.0 | ||||
|  */ | ||||
| 
 | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| 
 | ||||
| FILE * fopen_env(const char *env, const char *env_default, const char *mode) | ||||
| { | ||||
|     // Get the filename from the environment. Use defaults if unset or empty
 | ||||
|     const char *filename = getenv(env); | ||||
|     if (filename == NULL || filename[0] == '\0') { | ||||
|         filename = env_default; | ||||
|     } | ||||
| 
 | ||||
|     return fopen(filename, mode); | ||||
| } | ||||
|  | @ -7,7 +7,6 @@ | |||
|  */ | ||||
| 
 | ||||
| #include <stdio.h> | ||||
| // #include <libusb-1.0/libusb.h>
 | ||||
| #include <libusb.h> | ||||
| 
 | ||||
| struct libusb_device { | ||||
|  | @ -65,3 +64,5 @@ struct libusb_device_handle { | |||
|  */ | ||||
| #define DEFAULT_OUTPUT_DATA_FILE        "/tmp/libusbx52_output_data" | ||||
| 
 | ||||
| /* Open file from environment variable */ | ||||
| FILE * fopen_env(const char *env, const char *env_default, const char *mode); | ||||
|  |  | |||
|  | @ -21,7 +21,6 @@ int libusb_init(libusb_context **ctx) | |||
|     unsigned int vid; | ||||
|     unsigned int pid; | ||||
|     int parsed; | ||||
|     char *dev_list_file; | ||||
|     FILE *dev_list; | ||||
|     int i; | ||||
| 
 | ||||
|  | @ -38,15 +37,7 @@ int libusb_init(libusb_context **ctx) | |||
|         goto init_err_recovery; | ||||
|     } | ||||
| 
 | ||||
|     /*
 | ||||
|      * 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"); | ||||
|     dev_list = fopen_env(INPUT_DEVICE_LIST_ENV, DEFAULT_INPUT_DEVICE_LIST_FILE, "r"); | ||||
|     if (dev_list == NULL) { | ||||
|         rc = LIBUSB_ERROR_IO; | ||||
|         goto init_err_recovery; | ||||
|  | @ -210,8 +201,6 @@ 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) { | ||||
|  | @ -225,15 +214,8 @@ int libusb_open(libusb_device *dev, libusb_device_handle **handle) | |||
|     tmp_hdl->ctx = dev->context; | ||||
|     tmp_hdl->dev = dev; | ||||
| 
 | ||||
|     /*
 | ||||
|      * 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"); | ||||
|     tmp_hdl->packet_data_file = fopen_env(OUTPUT_DATA_FILE_ENV, | ||||
|                                           DEFAULT_OUTPUT_DATA_FILE, "w"); | ||||
| 
 | ||||
|     /* Make sure that the file opened correctly */ | ||||
|     if (tmp_hdl->packet_data_file == NULL) { | ||||
|  | @ -280,7 +262,7 @@ int libusb_control_transfer(libusb_device_handle *dev_handle, | |||
| { | ||||
|     /* Always log the control transfer */ | ||||
|     fprintf(dev_handle->packet_data_file, | ||||
|         "%s: RqType: %02x bRequest: %02x wValue: %04x wIndex: %04x timeout: %d\n", | ||||
|         "%s: RqType: %02x bRequest: %02x wValue: %04x wIndex: %04x timeout: %u\n", | ||||
|         __func__, request_type, bRequest, wValue, wIndex, timeout); | ||||
|     if (data != NULL) { | ||||
|         int i; | ||||
|  |  | |||
|  | @ -16,20 +16,15 @@ | |||
| 
 | ||||
| int main(int argc, char *argv[]) | ||||
| { | ||||
|     char *data_file; | ||||
|     FILE *data; | ||||
|     char **id_pair; | ||||
|     unsigned int vid; | ||||
|     unsigned int pid; | ||||
|     int i; | ||||
| 
 | ||||
|     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"); | ||||
|     data = fopen_env(INPUT_DEVICE_LIST_ENV, DEFAULT_INPUT_DEVICE_LIST_FILE, "w"); | ||||
|     if (data == NULL) { | ||||
|         fprintf(stderr, "Unable to open %s for writing\n", data_file); | ||||
|         fprintf(stderr, "Unable to open device list file for writing\n"); | ||||
|         fprintf(stderr, "%s\n", strerror(errno)); | ||||
|     } | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue