modified: source/inlprog.c

-introducing getopt to obtain commandline args
modified:   source/usb_operations.c
	-moving USB defn comments in from main
This commit is contained in:
paul eeepc 2016-11-20 15:43:11 -06:00
parent 4cbeff1ecf
commit cfffe554be
2 changed files with 117 additions and 48 deletions

View File

@ -1,54 +1,114 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <errno.h>
#include <unistd.h>
#include <libusb.h>
//uncomment to DEBUG this file alone
//#define DEBUG
#define DEBUG
//"make debug" to get DEBUG msgs on entire program
#include "dbg.h"
#include "usb_operations.h"
//control transfer request types
//uint8_t libusb_control_setup::bmRequestType
//Request type.
// Bits 0:4 determine recipient, see libusb_request_recipient. Bits 5:6 determine type, see libusb_request_type. Bit 7 determines data transfer direction, see libusb_endpoint_direction.
//
//libusb_request_types:
//LIBUSB_REQUEST_TYPE_STANDARD Standard handled by driver during setup/etc
//LIBUSB_REQUEST_TYPE_CLASS Class for use with specific device classes like HID.
//LIBUSB_REQUEST_TYPE_VENDOR Vendor application specific as we choose which is what we'll be utilizing for all transfers
//LIBUSB_REQUEST_TYPE_RESERVED Reserved.
//
//libusb_request_recipients:
//LIBUSB_RECIPIENT_DEVICE Device.
//LIBUSB_RECIPIENT_INTERFACE Interface.
//LIBUSB_RECIPIENT_ENDPOINT Endpoint.
//LIBUSB_RECIPIENT_OTHER Other.
//
//LIBUSB_ENDPOINT_IN In: device-to-host.
//LIBUSB_ENDPOINT_OUT Out: host-to-device.
// vendor requests also defined in firmware
// TODO put in combined .h file for both host and fw
#define REQ_LED_ON 1
#define REQ_LED_OFF 2
//USB timeout
#define SEC_5 5000
int main(int argc, char *argv[])
{
check(argc > 1, "USAGE: inlretro <action o/f>");
int o_flag = 0; //LED ON
int f_flag = 0; //LED OFF
int e_flag = 0; //ERASE
int m_flag = 0; //MIRROR
char *w_value = NULL; //WRITE FILE
char *d_value = NULL; //DUMP FILE
char *s_value = NULL; //SAVE FILE
char *i_value = NULL; //iNES MAPPER
char *b_value = NULL; //BOARD
int index = 0;
int rv = 0;
opterr = 0;
//getopt returns args till done then returns -1
//string of possible args : denotes 1 required additional arg
//:: denotes optional additional arg follows
while( (rv = getopt( argc, argv, "ofemw:d:s:i:b:")) != -1) {
switch(rv) {
case 'o':
o_flag = 1;
break;
case 'f':
f_flag = 1;
break;
case 'e':
e_flag = 1;
break;
case 'm':
m_flag = 1;
break;
case 'w':
w_value = optarg;
break;
case 'd':
d_value = optarg;
break;
case 's':
s_value = optarg;
break;
case 'i':
i_value = optarg;
break;
case 'b':
b_value = optarg;
break;
case '?':
if ( optopt == 'w' ) {
log_err("Option -%c requires an argument.", optopt);
} else if ( optopt == 'd' ) {
log_err("Option -%c requires an argument.", optopt);
} else if ( optopt == 's' ) {
log_err("Option -%c requires an argument.", optopt);
} else if ( optopt == 'i' ) {
log_err("Option -%c requires an argument.", optopt);
} else if ( optopt == 'b' ) {
log_err("Option -%c requires an argument.", optopt);
} else if ( isprint(optopt)) {
log_err("Unknown option -%c .", optopt);
} else {
log_err("Unknown option character '\\x%x'", optopt);
}
log_err("Improper arguements passed");
break;
default:
sentinel("getopt failed to catch all arg cases");
}
}
debug("flags= o:%d f:%d e:%d m:%d", o_flag, f_flag, e_flag, m_flag);
debug("args= w:%s d:%s s:%s i:%s b:%s", w_value, d_value, s_value, i_value, b_value);
for( index = optind; index < argc; index++) {
log_err("Non-option arguement: %s \n", argv[index]);
}
//context set to NULL since only acting as single user of libusb
libusb_context *context = NULL;
//create usb device handle pointer to interact with retro-prog
libusb_device_handle *rprog_handle = NULL;
rprog_handle = open_usb_device( context );
check(rprog_handle != NULL, "Unable to open INL retro-prog usb device handle.");
check( rprog_handle != NULL, "Unable to open INL retro-prog usb device handle.");
int xfr_cnt = 0;
//uint8_t buffer8[8]; //8 is max payload for low speed devices' data packet
@ -56,29 +116,15 @@ int main(int argc, char *argv[])
uint8_t buffer254[254]; //254 is max for non-LONG_TRANSFERS with V-USB
//uint8_t buffer16k[16384]; //16384 is max for LONG_TRANSFERS with V-USB
if (argc < 2) {
printf("USAGE: inlretro <action o/f>\n");
exit(1);
}
char action = argv[1][0];
switch (action) {
case 'o': //ON send REQ_LED_ON
xfr_cnt = usb_write_to_device( rprog_handle,
REQ_LED_ON, (unsigned char *)buffer254, sizeof(buffer254) );
printf("total bytes xfrd: %d \n", xfr_cnt);
break;
case 'f':
xfr_cnt = usb_write_to_device( rprog_handle,
REQ_LED_OFF, (unsigned char *)buffer254, sizeof(buffer254) );
printf("total bytes xfrd: %d \n", xfr_cnt);
break;
default:
printf("Invalid action o-on f-off\n");
exit(1);
if (o_flag) { //ON send REQ_LED_ON
xfr_cnt = usb_write_to_device( rprog_handle,
REQ_LED_ON, (unsigned char *)buffer254, sizeof(buffer254) );
printf("total bytes xfrd: %d \n", xfr_cnt);
}
if (f_flag) { //OFF send REQ_LED_OFF
xfr_cnt = usb_write_to_device( rprog_handle,
REQ_LED_OFF, (unsigned char *)buffer254, sizeof(buffer254) );
printf("total bytes xfrd: %d \n", xfr_cnt);
}
close_usb( context, rprog_handle);

View File

@ -12,6 +12,29 @@
#include "usb_operations.h"
//USB timeout
#define SEC_5 5000
//control transfer request types
//uint8_t libusb_control_setup::bmRequestType
//Request type.
// Bits 0:4 determine recipient, see libusb_request_recipient. Bits 5:6 determine type, see libusb_request_type. Bit 7 determines data transfer direction, see libusb_endpoint_direction.
//
//libusb_request_types:
//LIBUSB_REQUEST_TYPE_STANDARD Standard handled by driver during setup/etc
//LIBUSB_REQUEST_TYPE_CLASS Class for use with specific device classes like HID.
//LIBUSB_REQUEST_TYPE_VENDOR Vendor application specific as we choose which is what we'll be utilizing for all transfers
//LIBUSB_REQUEST_TYPE_RESERVED Reserved.
//
//libusb_request_recipients:
//LIBUSB_RECIPIENT_DEVICE Device.
//LIBUSB_RECIPIENT_INTERFACE Interface.
//LIBUSB_RECIPIENT_ENDPOINT Endpoint.
//LIBUSB_RECIPIENT_OTHER Other.
//
//LIBUSB_ENDPOINT_IN In: device-to-host.
//LIBUSB_ENDPOINT_OUT Out: host-to-device.
libusb_device_handle * open_usb_device( libusb_context *context )
{
int rv = 0;