Refined serial port selection.

So far up to two ports with the substring "usbserial" in their name were used. This was pretty platform specific and didn't allow to use non-USB ports at all.

Now if the -p option is given its value is used as substring as "usbserial" was used before. However if no -p option is given than up to two USB-backed ports are used.
This commit is contained in:
Oliver Schmidt 2018-02-04 23:16:11 +01:00
parent e575578fd0
commit 65cf9087bf

View File

@ -17,8 +17,6 @@
#include <sys/mman.h> #include <sys/mman.h>
#include <assert.h> #include <assert.h>
#define PORT_STR "usbserial"
#if 0 #if 0
//TODO: Switch xHD client to Z8530 built-in CRC calc //TODO: Switch xHD client to Z8530 built-in CRC calc
/* /*
@ -53,11 +51,23 @@ int main(int argc, char *argv[])
// Process the command line arguments // Process the command line arguments
bool bVerbose = false; bool bVerbose = false;
bool bLog = true; bool bLog = true;
char *pPortname = 0;
char *apFilename[2] = {0, 0}; char *apFilename[2] = {0, 0};
for (int iArg = 1; iArg < argc; iArg++) for (int iArg = 1; iArg < argc; iArg++)
{ {
// Process option
if (argv[iArg][0] == '-') if (argv[iArg][0] == '-')
{ {
if (argv[iArg][1] == 'p')
{
pPortname = &argv[iArg][2];
if (bVerbose)
printf("Port: %s\n", pPortname);
continue;
}
// Process flags
for (int iChar = 1; argv[iArg][iChar]; iChar++) for (int iChar = 1; argv[iArg][iChar]; iChar++)
{ {
switch (argv[iArg][iChar]) switch (argv[iArg][iChar])
@ -79,6 +89,8 @@ int main(int argc, char *argv[])
} }
continue; continue;
} }
// Process disk image
for (int iDrive = 0; iDrive < 2; iDrive++) for (int iDrive = 0; iDrive < 2; iDrive++)
{ {
if (apFilename[iDrive] == 0) if (apFilename[iDrive] == 0)
@ -93,11 +105,12 @@ int main(int argc, char *argv[])
if (apFilename[0] == 0) if (apFilename[0] == 0)
{ {
printf("Usage: %s [-nv] img1 [img2]\n" printf("Usage: %s [-nv] [-p<port>] img1 [img2]\n"
"\t-n\tno logging\n" "\t-n\t\tno logging\n"
"\t-v\tbe verbose\n" "\t-v\t\tbe verbose\n"
"\timg1\tdrive 1 disk image filename\n" "\t-p<port>\t(partial) serial portname\n"
"\timg2\tdrive 2 disk image filename\n", "\timg1\t\tdrive 1 disk image filename\n"
"\timg2\t\tdrive 2 disk image filename\n",
argv[0]); argv[0]);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -135,18 +148,12 @@ int main(int argc, char *argv[])
// Create a configuration for the serial ports // Create a configuration for the serial ports
enum sp_return eResult; enum sp_return eResult;
struct sp_port_config *pSerialConfig = 0; struct sp_port_config *pSerialConfig = 0;
eResult = sp_new_config(&pSerialConfig); assert(sp_new_config(&pSerialConfig) == SP_OK);
assert(eResult == SP_OK); assert(sp_set_config_baudrate(pSerialConfig, 230400) == SP_OK);
eResult = sp_set_config_baudrate(pSerialConfig, 230400); assert(sp_set_config_bits(pSerialConfig, 8) == SP_OK);
assert(eResult == SP_OK); assert(sp_set_config_parity(pSerialConfig, SP_PARITY_NONE) == SP_OK);
eResult = sp_set_config_bits(pSerialConfig, 8); assert(sp_set_config_stopbits(pSerialConfig, 1) == SP_OK);
assert(eResult == SP_OK); assert(sp_set_config_flowcontrol(pSerialConfig, SP_FLOWCONTROL_RTSCTS) == SP_OK);
eResult = sp_set_config_parity(pSerialConfig, SP_PARITY_NONE);
assert(eResult == SP_OK);
eResult = sp_set_config_stopbits(pSerialConfig, 1);
assert(eResult == SP_OK);
eResult = sp_set_config_flowcontrol(pSerialConfig, SP_FLOWCONTROL_RTSCTS);
assert(eResult == SP_OK);
// Find and configure valid serial ports // Find and configure valid serial ports
const int MAX_PORTS = 2; const int MAX_PORTS = 2;
@ -155,32 +162,48 @@ int main(int argc, char *argv[])
{ {
struct sp_port **ports; struct sp_port **ports;
eResult = sp_list_ports(&ports); if (sp_list_ports(&ports) != SP_OK)
assert(eResult == SP_OK); {
printf("Err: Port enum - %s\n", sp_last_error_message());
return EXIT_FAILURE;
}
for (int i = 0; ports[i]; i++) for (int i = 0; ports[i]; i++)
{ {
struct sp_port *pPort = ports[i]; struct sp_port *pPort = ports[i];
if (strstr(sp_get_port_name(pPort), PORT_STR)) bool bValid = false;
if (pPortname)
{
if (strstr(sp_get_port_name(pPort), pPortname))
bValid = true;
}
else
{
if (sp_get_port_transport(pPort) == SP_TRANSPORT_USB)
bValid = true;
}
if (bValid)
{ {
if (bVerbose) if (bVerbose)
{ {
printf("Valid port %d: %s\n", iValidPortCount, sp_get_port_name(pPort)); printf("Valid port %d: %s\n", iValidPortCount, sp_get_port_name(pPort));
printf("\tDescr: %s\n", sp_get_port_description(pPort)); printf("\tDescr: %s\n", sp_get_port_description(pPort));
int iUsbBus = -1; if (sp_get_port_transport(pPort) == SP_TRANSPORT_USB)
int iUsbAddress = -1; {
eResult = sp_get_port_usb_bus_address(pPort, &iUsbBus, &iUsbAddress); int iUsbBus = -1;
assert(eResult == SP_OK); int iUsbAddress = -1;
printf("\tUSB: Bus=%d, Address=%d\n", iUsbBus, iUsbAddress); if (sp_get_port_usb_bus_address(pPort, &iUsbBus, &iUsbAddress) == SP_OK)
int iUsbVid = -1; printf("\tUSB: Bus=%d, Address=%d\n", iUsbBus, iUsbAddress);
int iUsbPid = -1; int iUsbVid = -1;
eResult = sp_get_port_usb_vid_pid(pPort, &iUsbVid, &iUsbPid); int iUsbPid = -1;
assert(eResult == SP_OK); if (sp_get_port_usb_vid_pid(pPort, &iUsbVid, &iUsbPid) == SP_OK)
printf("\tUSB: VendorID=$%04X, ProductID=$%04X\n", iUsbVid, iUsbPid); printf("\tUSB: VendorID=$%04X, ProductID=$%04X\n", iUsbVid, iUsbPid);
printf("\tManufacturer: %s\n", sp_get_port_usb_manufacturer(pPort)); printf("\tManufacturer: %s\n", sp_get_port_usb_manufacturer(pPort));
printf("\tProduct: %s\n", sp_get_port_usb_product(pPort)); printf("\tProduct: %s\n", sp_get_port_usb_product(pPort));
printf("\tSerial #: %s\n", sp_get_port_usb_serial(pPort)); printf("\tSerial #: %s\n", sp_get_port_usb_serial(pPort));
}
} }
// Retain the port and configure it's settings // Retain the port and configure it's settings
@ -194,18 +217,17 @@ int main(int argc, char *argv[])
pPort = apValidPorts[iValidPortCount-1]; pPort = apValidPorts[iValidPortCount-1];
// See if the port can be opened and configured // See if the port can be opened and configured
enum sp_return eResultOpen; eResult = sp_open(pPort, SP_MODE_READ_WRITE);
enum sp_return eResultConfig; if (eResult == SP_OK)
eResultOpen = sp_open(pPort, SP_MODE_READ_WRITE); eResult = sp_set_config(pPort, pSerialConfig);
eResultConfig = sp_set_config(pPort, pSerialConfig); if (eResult != SP_OK)
if (eResultOpen != SP_OK || eResultConfig != SP_OK)
{ {
// Port is not valid // Port is not valid
iValidPortCount--; iValidPortCount--;
sp_close(pPort); sp_close(pPort);
sp_free_port(pPort); sp_free_port(pPort);
if (bVerbose) if (bVerbose)
printf("\n\t--- Could not open this port ---\n\n"); printf("\n\t--- Could not open this port: %s ---\n\n", sp_last_error_message());
} }
} }
} }