Add initial framework for getting self-reported device descriptions from USB devices

This commit is contained in:
hedgecrw85 2018-04-01 10:14:46 -05:00
parent 6b40f8b74d
commit 2f06d28c8a
8 changed files with 94 additions and 43 deletions

View File

@ -40,7 +40,7 @@
#endif
#include "AndroidHelperFunctions.h"
void push_back(struct charPairVector* vector, const char* firstString, const char* secondString)
void push_back(struct charTupleVector* vector, const char* firstString, const char* secondString, const char* thirdString)
{
// Allocate memory for new string storage
vector->length++;
@ -50,12 +50,26 @@ void push_back(struct charPairVector* vector, const char* firstString, const cha
newMemory = (char**)realloc(vector->second, vector->length*sizeof(char*));
if (newMemory)
vector->second = newMemory;
newMemory = (char**)realloc(vector->third, vector->length*sizeof(char*));
if (newMemory)
vector->third = newMemory;
// Store new strings
vector->first[vector->length-1] = (char*)malloc(strlen(firstString)+1);
vector->second[vector->length-1] = (char*)malloc(strlen(secondString)+1);
vector->third[vector->length-1] = (char*)malloc(strlen(thirdString)+1);
strcpy(vector->first[vector->length-1], firstString);
strcpy(vector->second[vector->length-1], secondString);
strcpy(vector->third[vector->length-1], thirdString);
}
char keyExists(struct charTupleVector* vector, const char* key)
{
size_t i;
for (i = 0; i < vector->length; ++i)
if (strcmp(key, vector->first[i]) == 0)
return 1;
return 0;
}
void getFriendlyName(const char* productFile, char* friendlyName)
@ -110,7 +124,7 @@ void getDriverName(const char* directoryToSearch, char* friendlyName)
closedir(directoryIterator);
}
void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathToSearch)
void recursiveSearchForComPorts(charTupleVector* comPorts, const char* fullPathToSearch)
{
// Open the directory
DIR *directoryIterator = opendir(fullPathToSearch);
@ -150,10 +164,10 @@ void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathTo
strcat(productFile, "/driver/module/drivers");
getDriverName(productFile, friendlyName);
if (friendlyName[0] != '\0')
push_back(comPorts, systemName, friendlyName);
push_back(comPorts, systemName, friendlyName, friendlyName);
}
else
push_back(comPorts, systemName, friendlyName);
push_back(comPorts, systemName, friendlyName, friendlyName);
// Clean up memory
free(productFile);
@ -163,7 +177,7 @@ void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathTo
else
{
// Search for more serial ports within the directory
charPairVector newComPorts = { (char**)malloc(1), (char**)malloc(1), 0 };
charTupleVector newComPorts = { (char**)malloc(1), (char**)malloc(1), (char**)malloc(1), 0 };
char* nextDirectory = (char*)malloc(strlen(fullPathToSearch) + strlen(directoryEntry->d_name) + 5);
strcpy(nextDirectory, fullPathToSearch);
strcat(nextDirectory, directoryEntry->d_name);
@ -173,12 +187,14 @@ void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathTo
int i;
for (i = 0; i < newComPorts.length; ++i)
{
push_back(comPorts, newComPorts.first[i], newComPorts.second[i]);
push_back(comPorts, newComPorts.first[i], newComPorts.second[i], newComPorts.third[i]);
free(newComPorts.first[i]);
free(newComPorts.second[i]);
free(newComPorts.third[i]);
}
free(newComPorts.first);
free(newComPorts.second);
free(newComPorts.third);
}
}
}

View File

@ -26,15 +26,16 @@
#ifndef __ANDROID_HELPER_FUNCTIONS_HEADER_H__
#define __ANDROID_HELPER_FUNCTIONS_HEADER_H__
typedef struct charPairVector
typedef struct charTupleVector
{
char **first, **second;
char **first, **second, **third;
size_t length;
} charPairVector;
void push_back(struct charPairVector* vector, const char* firstString, const char* secondString);
} charTupleVector;
void push_back(struct charTupleVector* vector, const char* firstString, const char* secondString, const char* thirdString);
char keyExists(struct charTupleVector* vector, const char* key);
void getDriverName(const char* directoryToSearch, char* friendlyName);
void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathToSearch);
void recursiveSearchForComPorts(charTupleVector* comPorts, const char* fullPathToSearch);
void getFriendlyName(const char* productFile, char* friendlyName);
unsigned int getBaudRateCode(int baudRate);
void setBaudRate(int portFD, int baudRate);

View File

@ -38,7 +38,7 @@
extern int ioctl(int __fd, unsigned long int __request, ...) __THROW;
void push_back(struct charPairVector* vector, const char* firstString, const char* secondString)
void push_back(struct charTupleVector* vector, const char* firstString, const char* secondString, const char* thirdString)
{
// Allocate memory for new string storage
vector->length++;
@ -48,15 +48,20 @@ void push_back(struct charPairVector* vector, const char* firstString, const cha
newMemory = (char**)realloc(vector->second, vector->length*sizeof(char*));
if (newMemory)
vector->second = newMemory;
newMemory = (char**)realloc(vector->third, vector->length*sizeof(char*));
if (newMemory)
vector->third = newMemory;
// Store new strings
vector->first[vector->length-1] = (char*)malloc(strlen(firstString)+1);
vector->second[vector->length-1] = (char*)malloc(strlen(secondString)+1);
vector->third[vector->length-1] = (char*)malloc(strlen(thirdString)+1);
strcpy(vector->first[vector->length-1], firstString);
strcpy(vector->second[vector->length-1], secondString);
strcpy(vector->third[vector->length-1], thirdString);
}
char keyExists(struct charPairVector* vector, const char* key)
char keyExists(struct charTupleVector* vector, const char* key)
{
size_t i;
for (i = 0; i < vector->length; ++i)
@ -117,7 +122,7 @@ void getDriverName(const char* directoryToSearch, char* friendlyName)
closedir(directoryIterator);
}
void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathToSearch)
void recursiveSearchForComPorts(charTupleVector* comPorts, const char* fullPathToSearch)
{
// Open the directory
DIR *directoryIterator = opendir(fullPathToSearch);
@ -170,23 +175,23 @@ void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathTo
{
strcpy(friendlyName, "Bluetooth Port ");
strcat(friendlyName, directoryEntry->d_name);
push_back(comPorts, systemName, friendlyName);
push_back(comPorts, systemName, friendlyName, friendlyName);
}
else if (((strlen(directoryEntry->d_name) >= 6) && (directoryEntry->d_name[3] == 'A') && (directoryEntry->d_name[4] == 'M') && (directoryEntry->d_name[5] == 'A')) ||
((ioctl(fd, TIOCGSERIAL, &serialInfo) == 0) && (serialInfo.type != PORT_UNKNOWN)))
{
strcpy(friendlyName, "Physical Port ");
strcat(friendlyName, directoryEntry->d_name+3);
push_back(comPorts, systemName, friendlyName);
push_back(comPorts, systemName, friendlyName, friendlyName);
}
close(fd);
}
}
else
push_back(comPorts, systemName, friendlyName);
push_back(comPorts, systemName, friendlyName, friendlyName);
}
else
push_back(comPorts, systemName, friendlyName);
push_back(comPorts, systemName, friendlyName, friendlyName);
// Clean up memory
free(productFile);
@ -196,7 +201,7 @@ void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathTo
else
{
// Search for more serial ports within the directory
charPairVector newComPorts = { (char**)malloc(1), (char**)malloc(1), 0 };
charTupleVector newComPorts = { (char**)malloc(1), (char**)malloc(1), (char**)malloc(1), 0 };
char* nextDirectory = (char*)malloc(strlen(fullPathToSearch) + strlen(directoryEntry->d_name) + 5);
strcpy(nextDirectory, fullPathToSearch);
strcat(nextDirectory, directoryEntry->d_name);
@ -206,12 +211,14 @@ void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathTo
int i;
for (i = 0; i < newComPorts.length; ++i)
{
push_back(comPorts, newComPorts.first[i], newComPorts.second[i]);
push_back(comPorts, newComPorts.first[i], newComPorts.second[i], newComPorts.third[i]);
free(newComPorts.first[i]);
free(newComPorts.second[i]);
free(newComPorts.third[i]);
}
free(newComPorts.first);
free(newComPorts.second);
free(newComPorts.third);
}
}
}
@ -222,7 +229,7 @@ void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathTo
closedir(directoryIterator);
}
void lastDitchSearchForComPorts(charPairVector* comPorts)
void lastDitchSearchForComPorts(charTupleVector* comPorts)
{
// Open the linux dev directory
DIR *directoryIterator = opendir("/dev/");
@ -250,7 +257,7 @@ void lastDitchSearchForComPorts(charPairVector* comPorts)
// Determine if port is already in the list, and add it if not
if (!keyExists(comPorts, systemName))
push_back(comPorts, systemName, friendlyName);
push_back(comPorts, systemName, friendlyName, friendlyName);
// Clean up memory
free(systemName);
@ -270,7 +277,7 @@ void lastDitchSearchForComPorts(charPairVector* comPorts)
// Determine if port is already in the list, and add it if not
if (!keyExists(comPorts, systemName))
push_back(comPorts, systemName, friendlyName);
push_back(comPorts, systemName, friendlyName, friendlyName);
// Clean up memory
free(systemName);

View File

@ -26,17 +26,17 @@
#ifndef __LINUX_HELPER_FUNCTIONS_HEADER_H__
#define __LINUX_HELPER_FUNCTIONS_HEADER_H__
typedef struct charPairVector
typedef struct charTupleVector
{
char **first, **second;
char **first, **second, **third;
size_t length;
} charPairVector;
void push_back(struct charPairVector* vector, const char* firstString, const char* secondString);
char keyExists(struct charPairVector* vector, const char* key);
} charTupleVector;
void push_back(struct charTupleVector* vector, const char* firstString, const char* secondString, const char* thirdString);
char keyExists(struct charTupleVector* vector, const char* key);
void getDriverName(const char* directoryToSearch, char* friendlyName);
void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathToSearch);
void lastDitchSearchForComPorts(charPairVector* comPorts);
void recursiveSearchForComPorts(charTupleVector* comPorts, const char* fullPathToSearch);
void lastDitchSearchForComPorts(charTupleVector* comPorts);
void getFriendlyName(const char* productFile, char* friendlyName);
unsigned int getBaudRateCode(int baudRate);
void setBaudRate(int portFD, int baudRate);

View File

@ -28,7 +28,7 @@
#include <string.h>
#include "WindowsHelperFunctions.h"
void push_back(struct charPairVector* vector, const char* firstString, const char* secondString)
void push_back(struct charTupleVector* vector, const char* firstString, const char* secondString, const char* thirdString)
{
// Allocate memory for new string storage
vector->length++;
@ -38,12 +38,26 @@ void push_back(struct charPairVector* vector, const char* firstString, const cha
newMemory = (char**)realloc(vector->second, vector->length*sizeof(char*));
if (newMemory)
vector->second = newMemory;
newMemory = (char**)realloc(vector->third, vector->length*sizeof(char*));
if (newMemory)
vector->third = newMemory;
// Store new strings
vector->first[vector->length-1] = (char*)malloc(strlen(firstString)+1);
vector->second[vector->length-1] = (char*)malloc(strlen(secondString)+1);
vector->third[vector->length-1] = (char*)malloc(strlen(thirdString)+1);
strcpy(vector->first[vector->length-1], firstString);
strcpy(vector->second[vector->length-1], secondString);
strcpy(vector->third[vector->length-1], thirdString);
}
char keyExists(struct charTupleVector* vector, const char* key)
{
size_t i;
for (i = 0; i < vector->length; ++i)
if (strcmp(key, vector->first[i]) == 0)
return 1;
return 0;
}
#endif

View File

@ -26,11 +26,12 @@
#ifndef __WINDOWS_HELPER_FUNCTIONS_HEADER_H__
#define __WINDOWS_HELPER_FUNCTIONS_HEADER_H__
typedef struct charPairVector
typedef struct charTupleVector
{
char **first, **second;
char **first, **second, **third;
size_t length;
} charPairVector;
void push_back(struct charPairVector* vector, const char* firstString, const char* secondString);
} charTupleVector;
void push_back(struct charTupleVector* vector, const char* firstString, const char* secondString, const char* thirdString);
char keyExists(struct charTupleVector* vector, const char* key);
#endif // #ifndef __WINDOWS_HELPER_FUNCTIONS_HEADER_H__

View File

@ -263,14 +263,16 @@ public final class SerialPort
{
SerialPort serialPort = new SerialPort();
serialPort.comPort = "/dev/null";
serialPort.portString = "Bad Port";
serialPort.friendlyName = "Bad Port";
serialPort.portDescription = "Bad Port";
return serialPort;
}
// Create SerialPort object
SerialPort serialPort = new SerialPort();
serialPort.comPort = portDescriptor;
serialPort.portString = "User-Specified Port";
serialPort.friendlyName = "User-Specified Port";
serialPort.portDescription = "User-Specified Port";
return serialPort;
}
@ -351,7 +353,7 @@ public final class SerialPort
private volatile SerialPortOutputStream outputStream = null;
private volatile SerialPortDataListener userDataListener = null;
private volatile SerialPortEventListener serialEventListener = null;
private volatile String portString, comPort;
private volatile String comPort, friendlyName, portDescription;
private volatile boolean isOpened = false;
/**
@ -898,7 +900,7 @@ public final class SerialPort
*
* @return A descriptive string representing this serial port.
*/
public final String getDescriptivePortName() { return portString.trim(); }
public final String getDescriptivePortName() { return friendlyName.trim(); }
/**
* Gets the operating system-defined device name corresponding to this serial port.
@ -907,6 +909,16 @@ public final class SerialPort
*/
public final String getSystemPortName() { return (isWindows ? comPort.substring(comPort.lastIndexOf('\\')+1) : comPort.substring(comPort.lastIndexOf('/')+1)); }
/**
* Gets a description of the port as reported by the device itself.
* <p>
* This will only be available for USB-connected devices that report a product description.
* Otherwise, it will return the same value as {@link #getDescriptivePortName()}.
*
* @return The port description as reported by the device itself.
*/
public final String getPortDescription() { return portDescription.trim(); }
/**
* Gets the current baud rate of the serial port.
*

View File

@ -2,7 +2,7 @@
* SerialPortTest.java
*
* Created on: Feb 27, 2015
* Last Updated on: Jan 03, 2018
* Last Updated on: Jan 10, 2018
* Author: Will Hedgecock
*
* Copyright (C) 2012-2018 Fazecast, Inc.
@ -60,7 +60,7 @@ public class SerialPortTest
SerialPort[] ports = SerialPort.getCommPorts();
System.out.println("\nAvailable Ports:\n");
for (int i = 0; i < ports.length; ++i)
System.out.println(" [" + i + "] " + ports[i].getSystemPortName() + ": " + ports[i].getDescriptivePortName());
System.out.println(" [" + i + "] " + ports[i].getSystemPortName() + ": " + ports[i].getDescriptivePortName() + " - " + ports[i].getPortDescription());
SerialPort ubxPort;
System.out.print("\nChoose your desired serial port or enter -1 to specify a port directly: ");
int serialPortChoice = 0;
@ -85,7 +85,7 @@ public class SerialPortTest
byte[] readBuffer = new byte[2048];
boolean openedSuccessfully = ubxPort.openPort();
System.out.println("\nOpening " + ubxPort.getSystemPortName() + ": " + ubxPort.getDescriptivePortName() + ": " + openedSuccessfully);
System.out.println("\nOpening " + ubxPort.getSystemPortName() + ": " + ubxPort.getDescriptivePortName() + " - " + ubxPort.getPortDescription() + ": " + openedSuccessfully);
if (!openedSuccessfully)
return;
System.out.println("Setting read timeout mode to non-blocking");
@ -184,7 +184,7 @@ public class SerialPortTest
System.out.println("\n\nAttempting to read from two serial ports simultaneously\n");
System.out.println("\nAvailable Ports:\n");
for (int i = 0; i < ports.length; ++i)
System.out.println(" [" + i + "] " + ports[i].getSystemPortName() + ": " + ports[i].getDescriptivePortName());
System.out.println(" [" + i + "] " + ports[i].getSystemPortName() + ": " + ports[i].getDescriptivePortName() + " - " + ports[i].getPortDescription());
SerialPort ubxPort2;
System.out.print("\nChoose your second desired serial port, or enter -1 to skip this test: ");
serialPortChoice = 0;