diff --git a/src/main/c/Posix/PosixHelperFunctions.c b/src/main/c/Posix/PosixHelperFunctions.c index deb8b4f..64203dc 100644 --- a/src/main/c/Posix/PosixHelperFunctions.c +++ b/src/main/c/Posix/PosixHelperFunctions.c @@ -2,7 +2,7 @@ * PosixHelperFunctions.c * * Created on: Mar 10, 2015 - * Last Updated on: Jan 17, 2022 + * Last Updated on: Jan 18, 2022 * Author: Will Hedgecock * * Copyright (C) 2012-2022 Fazecast, Inc. @@ -1065,8 +1065,8 @@ int setBaudRateCustom(int portFD, baud_rate baudRate) return -1; } -// BSD-specific functionality -#elif defined(__FreeBSD__) || defined(__OpenBSD__) +// FreeBSD-specific functionality +#elif defined(__FreeBSD__) char getPortLocation(const char *deviceName, char* portLocation) { @@ -1189,10 +1189,7 @@ void searchForComPorts(serialPortVector* comPorts) struct stat fileStats; stat(systemName, &fileStats); if (!S_ISDIR(fileStats.st_mode)) - { - char isUSB = getPortLocation(directoryEntry->d_name + 3, portLocation); pushBack(comPorts, systemName, friendlyName, friendlyName, portLocation); - } // Clean up memory free(friendlyName); @@ -1222,6 +1219,188 @@ int setBaudRateCustom(int portFD, baud_rate baudRate) return -1; } +// OpenBSD-specific functionality +#elif defined(__OpenBSD__) + +char getUsbPortDetails(const char* usbDeviceFile, char* portLocation, char* friendlyName, char** description) +{ + // Only continue if this is a USB device + char found = 0; + sprintf(portLocation, "0-0"); + if (usbDeviceFile[0] != 'U') + return found; + + // Attempt to locate the device in dmesg + size_t bufferSize = 1024; + char *stdOutResult = (char*)malloc(bufferSize), *device = (char*)malloc(64); + snprintf(stdOutResult, bufferSize, "dmesg | grep ucom%s | tail -1", usbDeviceFile + 1); + FILE *pipe = popen(stdOutResult, "r"); + device[0] = '\0'; + if (pipe) + { + while (fgets(stdOutResult, bufferSize, pipe)) + if (strstr(stdOutResult, " at ")) + { + found = 1; + sprintf(friendlyName, "ucom%s", usbDeviceFile + 1); + strcpy(device, strstr(stdOutResult, " at ") + 4); + device[strcspn(device, "\r\n")] = '\0'; + } + pclose(pipe); + } + + // Parse port location and description + char *address = NULL, *port = NULL; + if (device[0]) + { + char* usbFile = (char*)malloc(64); + for (int bus = 0; (bus < 255) && (!address || !port); ++bus) + { + // Ensure this bus exists + struct stat fileStats; + sprintf(usbFile, "/dev/usb%d", bus); + if (stat(usbFile, &fileStats)) + continue; + + // Read the USB address and description + snprintf(stdOutResult, bufferSize, "usbdevs -v -d /dev/usb%d 2>/dev/null | grep -B 2 %s", bus, device); + pipe = popen(stdOutResult, "r"); + if (pipe) + { + while (fgets(stdOutResult, bufferSize, pipe)) + { + if (strstr(stdOutResult, "addr ") && strrchr(stdOutResult, ',')) + { + char *product = strrchr(stdOutResult, ',') + 2; + product[strcspn(product, "\r\n")] = '\0'; + *description = (char*)realloc(*description, strlen(product) + 1); + memcpy(*description, product, strlen(product) + 1); + } + if (strstr(stdOutResult, "addr ")) + { + address = strstr(stdOutResult, "addr ") + 5; + while ((address[0] != '\0') && (address[0] == '0')) + address = address + 1; + *(strchr(address, ':')) = '\0'; + sprintf(portLocation, "%d-%s", bus, address); + } + } + pclose(pipe); + } + + // Read the USB port location + snprintf(stdOutResult, bufferSize, "dmesg | grep \"%s at \" | tail -1", device); + pipe = popen(stdOutResult, "r"); + if (pipe) + { + while (fgets(stdOutResult, bufferSize, pipe)) + if (strstr(stdOutResult, "port ")) + { + port = strstr(stdOutResult, "port ") + 5; + *(strchr(port, ' ')) = '\0'; + strcat(portLocation, "."); + strcat(portLocation, port); + } + pclose(pipe); + } + } + free(usbFile); + } + + // Clean up memory and return result + free(device); + free(stdOutResult); + return found; +} + +void searchForComPorts(serialPortVector* comPorts) +{ + // Open the FreeBSD dev directory + DIR *directoryIterator = opendir("/dev/"); + if (directoryIterator) + { + // Read all files in the current directory + struct dirent *directoryEntry = readdir(directoryIterator); + while (directoryEntry) + { + // See if the file names a potential serial port + if ((strlen(directoryEntry->d_name) >= 4) && (directoryEntry->d_name[0] != '.') && + (((directoryEntry->d_name[0] == 't') && (directoryEntry->d_name[1] == 't') && (directoryEntry->d_name[2] == 'y') && (directoryEntry->d_name[3] != 'v')) || + ((directoryEntry->d_name[0] == 'c') && (directoryEntry->d_name[1] == 'u') && (directoryEntry->d_name[2] == 'a')) || + ((directoryEntry->d_name[0] == 'd') && (directoryEntry->d_name[1] == 't') && (directoryEntry->d_name[2] == 'y')))) + { + // Determine system name of port + char* systemName = (char*)malloc(256); + strcpy(systemName, "/dev/"); + strcat(systemName, directoryEntry->d_name); + + // Set static friendly name and description + char* friendlyName = (char*)malloc(64); + char* description = (char*)malloc(32); + strcpy(friendlyName, "Serial Port"); + strcpy(description, "Serial Port"); + + // Determine location and description of port + char* portLocation = (char*)malloc(256); + char isUSB = getUsbPortDetails(directoryEntry->d_name + 3, portLocation, friendlyName, &description); + + // Update friendly name + if ((directoryEntry->d_name[0] != 'c') && (directoryEntry->d_name[0] != 'd')) + strcat(friendlyName, " (Dial-In)"); + + // Check if port is already enumerated + serialPort *port = fetchPort(comPorts, systemName); + if (port) + { + // See if device has changed locations + port->enumerated = 1; + if (isUSB) + { + int oldLength = strlen(port->portLocation); + int newLength = strlen(portLocation); + if (oldLength != newLength) + { + port->portLocation = (char*)realloc(port->portLocation, newLength + 1); + strcpy(port->portLocation, portLocation); + } + else if (memcmp(port->portLocation, portLocation, newLength)) + strcpy(port->portLocation, portLocation); + } + } + else + { + // Add the port to the list if it is not a directory + struct stat fileStats; + stat(systemName, &fileStats); + if (!S_ISDIR(fileStats.st_mode) && isUSB) + pushBack(comPorts, systemName, friendlyName, description, portLocation); + } + + // Clean up memory + free(portLocation); + free(description); + free(friendlyName); + free(systemName); + } + directoryEntry = readdir(directoryIterator); + } + + // Close the directory + closedir(directoryIterator); + } +} + +baud_rate getBaudRateCode(baud_rate baudRate) +{ + return baudRate; +} + +int setBaudRateCustom(int portFD, baud_rate baudRate) +{ + // All baud rates allowed by default on this OS + return -1; +} + // Apple-specific functionality #elif defined(__APPLE__) diff --git a/src/main/resources/Android/arm64-v8a/libjSerialComm.so b/src/main/resources/Android/arm64-v8a/libjSerialComm.so index 3d252b6..28372a6 100644 Binary files a/src/main/resources/Android/arm64-v8a/libjSerialComm.so and b/src/main/resources/Android/arm64-v8a/libjSerialComm.so differ diff --git a/src/main/resources/Android/armeabi-v7a/libjSerialComm.so b/src/main/resources/Android/armeabi-v7a/libjSerialComm.so index 9ebddf7..23d5442 100644 Binary files a/src/main/resources/Android/armeabi-v7a/libjSerialComm.so and b/src/main/resources/Android/armeabi-v7a/libjSerialComm.so differ diff --git a/src/main/resources/Android/x86/libjSerialComm.so b/src/main/resources/Android/x86/libjSerialComm.so index 7d4b6ab..b4dd5da 100644 Binary files a/src/main/resources/Android/x86/libjSerialComm.so and b/src/main/resources/Android/x86/libjSerialComm.so differ diff --git a/src/main/resources/Android/x86_64/libjSerialComm.so b/src/main/resources/Android/x86_64/libjSerialComm.so index e4d61bd..4f08ec0 100644 Binary files a/src/main/resources/Android/x86_64/libjSerialComm.so and b/src/main/resources/Android/x86_64/libjSerialComm.so differ diff --git a/src/main/resources/FreeBSD/arm64/libjSerialComm.so b/src/main/resources/FreeBSD/arm64/libjSerialComm.so index 5bedc68..28f5077 100644 Binary files a/src/main/resources/FreeBSD/arm64/libjSerialComm.so and b/src/main/resources/FreeBSD/arm64/libjSerialComm.so differ diff --git a/src/main/resources/FreeBSD/x86/libjSerialComm.so b/src/main/resources/FreeBSD/x86/libjSerialComm.so index 0f27153..0bc2ce3 100644 Binary files a/src/main/resources/FreeBSD/x86/libjSerialComm.so and b/src/main/resources/FreeBSD/x86/libjSerialComm.so differ diff --git a/src/main/resources/FreeBSD/x86_64/libjSerialComm.so b/src/main/resources/FreeBSD/x86_64/libjSerialComm.so index 1785f97..c334adf 100644 Binary files a/src/main/resources/FreeBSD/x86_64/libjSerialComm.so and b/src/main/resources/FreeBSD/x86_64/libjSerialComm.so differ diff --git a/src/main/resources/Linux/armv5/libjSerialComm.so b/src/main/resources/Linux/armv5/libjSerialComm.so index 15fcc2c..2ed91be 100644 Binary files a/src/main/resources/Linux/armv5/libjSerialComm.so and b/src/main/resources/Linux/armv5/libjSerialComm.so differ diff --git a/src/main/resources/Linux/armv6/libjSerialComm.so b/src/main/resources/Linux/armv6/libjSerialComm.so index c2a6435..502f24e 100644 Binary files a/src/main/resources/Linux/armv6/libjSerialComm.so and b/src/main/resources/Linux/armv6/libjSerialComm.so differ diff --git a/src/main/resources/Linux/armv6hf/libjSerialComm.so b/src/main/resources/Linux/armv6hf/libjSerialComm.so index 7064f33..9627e3e 100644 Binary files a/src/main/resources/Linux/armv6hf/libjSerialComm.so and b/src/main/resources/Linux/armv6hf/libjSerialComm.so differ diff --git a/src/main/resources/Linux/armv7/libjSerialComm.so b/src/main/resources/Linux/armv7/libjSerialComm.so index 92236c7..e0d7455 100644 Binary files a/src/main/resources/Linux/armv7/libjSerialComm.so and b/src/main/resources/Linux/armv7/libjSerialComm.so differ diff --git a/src/main/resources/Linux/armv7hf/libjSerialComm.so b/src/main/resources/Linux/armv7hf/libjSerialComm.so index 444c67d..5771051 100644 Binary files a/src/main/resources/Linux/armv7hf/libjSerialComm.so and b/src/main/resources/Linux/armv7hf/libjSerialComm.so differ diff --git a/src/main/resources/Linux/armv8_32/libjSerialComm.so b/src/main/resources/Linux/armv8_32/libjSerialComm.so index 0522d9a..1ded335 100644 Binary files a/src/main/resources/Linux/armv8_32/libjSerialComm.so and b/src/main/resources/Linux/armv8_32/libjSerialComm.so differ diff --git a/src/main/resources/Linux/armv8_64/libjSerialComm.so b/src/main/resources/Linux/armv8_64/libjSerialComm.so index e22fc20..b6bae1b 100644 Binary files a/src/main/resources/Linux/armv8_64/libjSerialComm.so and b/src/main/resources/Linux/armv8_64/libjSerialComm.so differ diff --git a/src/main/resources/Linux/ppc64le/libjSerialComm.so b/src/main/resources/Linux/ppc64le/libjSerialComm.so index cd1d4c9..89b32eb 100644 Binary files a/src/main/resources/Linux/ppc64le/libjSerialComm.so and b/src/main/resources/Linux/ppc64le/libjSerialComm.so differ diff --git a/src/main/resources/Linux/x86/libjSerialComm.so b/src/main/resources/Linux/x86/libjSerialComm.so index 3da818b..7b45bda 100644 Binary files a/src/main/resources/Linux/x86/libjSerialComm.so and b/src/main/resources/Linux/x86/libjSerialComm.so differ diff --git a/src/main/resources/Linux/x86_64/libjSerialComm.so b/src/main/resources/Linux/x86_64/libjSerialComm.so index 7b1db46..46c751e 100644 Binary files a/src/main/resources/Linux/x86_64/libjSerialComm.so and b/src/main/resources/Linux/x86_64/libjSerialComm.so differ diff --git a/src/main/resources/OSX/aarch64/libjSerialComm.jnilib b/src/main/resources/OSX/aarch64/libjSerialComm.jnilib index af87c70..d55b2c0 100755 Binary files a/src/main/resources/OSX/aarch64/libjSerialComm.jnilib and b/src/main/resources/OSX/aarch64/libjSerialComm.jnilib differ diff --git a/src/main/resources/OSX/x86/libjSerialComm.jnilib b/src/main/resources/OSX/x86/libjSerialComm.jnilib index 8c7f069..b6c36ec 100644 Binary files a/src/main/resources/OSX/x86/libjSerialComm.jnilib and b/src/main/resources/OSX/x86/libjSerialComm.jnilib differ diff --git a/src/main/resources/OSX/x86_64/libjSerialComm.jnilib b/src/main/resources/OSX/x86_64/libjSerialComm.jnilib index dcedf26..ede227e 100755 Binary files a/src/main/resources/OSX/x86_64/libjSerialComm.jnilib and b/src/main/resources/OSX/x86_64/libjSerialComm.jnilib differ diff --git a/src/main/resources/OpenBSD/amd64/libjSerialComm.so b/src/main/resources/OpenBSD/amd64/libjSerialComm.so index 587aa1a..1ccf977 100644 Binary files a/src/main/resources/OpenBSD/amd64/libjSerialComm.so and b/src/main/resources/OpenBSD/amd64/libjSerialComm.so differ diff --git a/src/main/resources/OpenBSD/x86/libjSerialComm.so b/src/main/resources/OpenBSD/x86/libjSerialComm.so index 94c313d..6c8872d 100644 Binary files a/src/main/resources/OpenBSD/x86/libjSerialComm.so and b/src/main/resources/OpenBSD/x86/libjSerialComm.so differ diff --git a/src/main/resources/Solaris/sparcv8plus_32/libjSerialComm.so b/src/main/resources/Solaris/sparcv8plus_32/libjSerialComm.so index bf0ec40..6221a41 100644 Binary files a/src/main/resources/Solaris/sparcv8plus_32/libjSerialComm.so and b/src/main/resources/Solaris/sparcv8plus_32/libjSerialComm.so differ diff --git a/src/main/resources/Solaris/sparcv9_64/libjSerialComm.so b/src/main/resources/Solaris/sparcv9_64/libjSerialComm.so index 25833ad..1454e4e 100644 Binary files a/src/main/resources/Solaris/sparcv9_64/libjSerialComm.so and b/src/main/resources/Solaris/sparcv9_64/libjSerialComm.so differ diff --git a/src/main/resources/Solaris/x86/libjSerialComm.so b/src/main/resources/Solaris/x86/libjSerialComm.so index 32634be..f02cc13 100644 Binary files a/src/main/resources/Solaris/x86/libjSerialComm.so and b/src/main/resources/Solaris/x86/libjSerialComm.so differ diff --git a/src/main/resources/Solaris/x86_64/libjSerialComm.so b/src/main/resources/Solaris/x86_64/libjSerialComm.so index 35fa97c..aa4b9a6 100644 Binary files a/src/main/resources/Solaris/x86_64/libjSerialComm.so and b/src/main/resources/Solaris/x86_64/libjSerialComm.so differ