diff --git a/src/main/c/Android/jni/AndroidHelperFunctions.c b/src/main/c/Android/jni/AndroidHelperFunctions.c index 79ed802..88eb4db 100644 --- a/src/main/c/Android/jni/AndroidHelperFunctions.c +++ b/src/main/c/Android/jni/AndroidHelperFunctions.c @@ -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); } } } diff --git a/src/main/c/Android/jni/AndroidHelperFunctions.h b/src/main/c/Android/jni/AndroidHelperFunctions.h index d9b9065..f78102e 100644 --- a/src/main/c/Android/jni/AndroidHelperFunctions.h +++ b/src/main/c/Android/jni/AndroidHelperFunctions.h @@ -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); diff --git a/src/main/c/Linux/LinuxHelperFunctions.c b/src/main/c/Linux/LinuxHelperFunctions.c index 102047f..8836776 100644 --- a/src/main/c/Linux/LinuxHelperFunctions.c +++ b/src/main/c/Linux/LinuxHelperFunctions.c @@ -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); diff --git a/src/main/c/Linux/LinuxHelperFunctions.h b/src/main/c/Linux/LinuxHelperFunctions.h index 2d8377c..4844ac5 100644 --- a/src/main/c/Linux/LinuxHelperFunctions.h +++ b/src/main/c/Linux/LinuxHelperFunctions.h @@ -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); diff --git a/src/main/c/Windows/WindowsHelperFunctions.c b/src/main/c/Windows/WindowsHelperFunctions.c index a412fa2..e5277f3 100644 --- a/src/main/c/Windows/WindowsHelperFunctions.c +++ b/src/main/c/Windows/WindowsHelperFunctions.c @@ -28,7 +28,7 @@ #include #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 diff --git a/src/main/c/Windows/WindowsHelperFunctions.h b/src/main/c/Windows/WindowsHelperFunctions.h index 8f0de19..476e0a6 100644 --- a/src/main/c/Windows/WindowsHelperFunctions.h +++ b/src/main/c/Windows/WindowsHelperFunctions.h @@ -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__ diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPort.java b/src/main/java/com/fazecast/jSerialComm/SerialPort.java index 38bb5fc..0ec48d7 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPort.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPort.java @@ -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. + *

+ * 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. * diff --git a/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java b/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java index aca2192..7298117 100644 --- a/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java +++ b/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java @@ -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;