diff --git a/src/main/c/Posix/PosixHelperFunctions.c b/src/main/c/Posix/PosixHelperFunctions.c index d197ee7..1ee5dbc 100644 --- a/src/main/c/Posix/PosixHelperFunctions.c +++ b/src/main/c/Posix/PosixHelperFunctions.c @@ -342,22 +342,39 @@ void recursiveSearchForComPorts(serialPortVector* comPorts, const char* fullPath strcpy(systemName, "/dev/"); strcat(systemName, directoryEntry->d_name); + // Determine location of port + char* portLocation = (char*)malloc(128); + char* productFile = (char*)malloc(strlen(fullPathToSearch) + strlen(directoryEntry->d_name) + 30); + strcpy(productFile, fullPathToSearch); + strcat(productFile, directoryEntry->d_name); + strcat(productFile, "/device/.."); + char isUSB = getPortLocation(productFile, portLocation); + if (!isUSB) + isUSB = driverGetPortLocation(1, "/sys/bus/usb/devices/", directoryEntry->d_name, portLocation, 0); + // 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 { // See if device has a registered friendly name - char* portLocation = (char*)malloc(128); char* friendlyName = (char*)malloc(256); - char* productFile = (char*)malloc(strlen(fullPathToSearch) + strlen(directoryEntry->d_name) + 30); - strcpy(productFile, fullPathToSearch); - strcat(productFile, directoryEntry->d_name); - strcat(productFile, "/device/.."); - char isUSB = getPortLocation(productFile, portLocation); - if (!isUSB) - isUSB = driverGetPortLocation(1, "/sys/bus/usb/devices/", directoryEntry->d_name, portLocation, 0); strcat(productFile, "/product"); getFriendlyName(productFile, friendlyName); if (friendlyName[0] == '\0') // Must be a physical (or emulated) port @@ -442,12 +459,12 @@ void recursiveSearchForComPorts(serialPortVector* comPorts, const char* fullPath } // Clean up memory - free(productFile); free(friendlyName); - free(portLocation); } // Clean up memory + free(portLocation); + free(productFile); free(systemName); } else @@ -538,27 +555,44 @@ void lastDitchSearchForComPorts(serialPortVector* comPorts) strcpy(systemName, "/dev/"); strcat(systemName, directoryEntry->d_name); + // Determine location of port + char* portLocation = (char*)malloc(128); + char isUSB = driverGetPortLocation(1, "/sys/bus/usb/devices/", directoryEntry->d_name, portLocation, 0); + // 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 { // Set static friendly name - char* portLocation = (char*)malloc(128); char* friendlyName = (char*)malloc(256); strcpy(friendlyName, "USB-Based Serial Port"); // Add the port to the list - char isUSB = driverGetPortLocation(1, "/sys/bus/usb/devices/", directoryEntry->d_name, portLocation, 0); pushBack(comPorts, systemName, friendlyName, friendlyName, portLocation); // Clean up memory free(friendlyName); - free(portLocation); } // Clean up memory + free(portLocation); free(systemName); } else if ((strlen(directoryEntry->d_name) >= 6) && (directoryEntry->d_name[0] == 't') && (directoryEntry->d_name[1] == 't') && (directoryEntry->d_name[2] == 'y') && @@ -569,27 +603,44 @@ void lastDitchSearchForComPorts(serialPortVector* comPorts) strcpy(systemName, "/dev/"); strcat(systemName, directoryEntry->d_name); + // Determine location of port + char* portLocation = (char*)malloc(128); + char isUSB = driverGetPortLocation(1, "/sys/bus/usb/devices/", directoryEntry->d_name, portLocation, 0); + // 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 { // Set static friendly name - char* portLocation = (char*)malloc(128); char* friendlyName = (char*)malloc(256); strcpy(friendlyName, "Advantech Extended Serial Port"); // Add the port to the list - char isUSB = driverGetPortLocation(1, "/sys/bus/usb/devices/", directoryEntry->d_name, portLocation, 0); pushBack(comPorts, systemName, friendlyName, friendlyName, portLocation); // Clean up memory free(friendlyName); - free(portLocation); } // Clean up memory + free(portLocation); free(systemName); } else if ((strlen(directoryEntry->d_name) >= 6) && (directoryEntry->d_name[0] == 'r') && (directoryEntry->d_name[1] == 'f') && (directoryEntry->d_name[2] == 'c') && @@ -1001,9 +1052,69 @@ int setBaudRateCustom(int portFD, baud_rate baudRate) return -1; } -// FreeBSD-specific functionality +// BSD-specific functionality #elif defined(__FreeBSD__) +char getPortLocation(const char *deviceName, char* portLocation) +{ + // Attempt to locate the device in sysctl + size_t bufferSize = 1024; + char *stdOutResult = (char*)malloc(bufferSize), *device = NULL; + snprintf(stdOutResult, bufferSize, "sysctl -a | grep \"ttyname: %s\"", deviceName); + FILE *pipe = popen(stdOutResult, "r"); + if (pipe) + { + while (!device && fgets(stdOutResult, bufferSize, pipe)) + { + device = stdOutResult; + *(strstr(device, "ttyname:") - 1) = '\0'; + strcat(device, ".%location"); + } + pclose(pipe); + } + + // Parse port location + if (device) + { + char *temp = (char*)malloc(64); + sprintf(portLocation, "sysctl -a | grep \"%s\"", device); + pipe = popen(portLocation, "r"); + strcpy(portLocation, "0-0"); + if (pipe) + { + while (fgets(stdOutResult, bufferSize, pipe)) + if (strstr(stdOutResult, "bus") && strstr(stdOutResult, "hubaddr") && strstr(stdOutResult, "port")) + { + char *cursor = strstr(stdOutResult, "bus=") + 4; + size_t length = (size_t)(strchr(cursor, ' ') - cursor); + memcpy(portLocation, cursor, length); + portLocation[length] = '\0'; + strcat(portLocation, "-"); + cursor = strstr(stdOutResult, "hubaddr=") + 8; + length = (size_t)(strchr(cursor, ' ') - cursor); + memcpy(temp, cursor, length); + temp[length] = '\0'; + strcat(portLocation, temp); + strcat(portLocation, "."); + cursor = strstr(stdOutResult, "port=") + 5; + length = (size_t)(strchr(cursor, ' ') - cursor); + memcpy(temp, cursor, length); + temp[length] = '\0'; + strcat(portLocation, temp); + break; + } + pclose(pipe); + } + free(temp); + } + else + strcpy(portLocation, "0-0"); + + // Clean up memory and return result + free(stdOutResult); + return (device ? 1 : 0); +} + void searchForComPorts(serialPortVector* comPorts) { // Open the FreeBSD dev directory @@ -1029,14 +1140,32 @@ void searchForComPorts(serialPortVector* comPorts) strcpy(systemName, "/dev/"); strcat(systemName, directoryEntry->d_name); + // Determine location of port + char* portLocation = (char*)malloc(256); + char isUSB = getPortLocation(directoryEntry->d_name + 3, portLocation); + // 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 { // Set static friendly name - char *location = (char*)malloc(256); char* friendlyName = (char*)malloc(256); if (directoryEntry->d_name[0] == 'c') strcpy(friendlyName, "Serial Port"); @@ -1048,68 +1177,16 @@ void searchForComPorts(serialPortVector* comPorts) stat(systemName, &fileStats); if (!S_ISDIR(fileStats.st_mode)) { - size_t bufferSize = 1024; - char *stdOutResult = (char*)malloc(bufferSize), *device = NULL; - snprintf(stdOutResult, bufferSize, "sysctl -a | grep \"ttyname: %s\"", directoryEntry->d_name + 3); - FILE *pipe = popen(stdOutResult, "r"); - if (pipe) - { - while (!device && fgets(stdOutResult, bufferSize, pipe)) - { - device = stdOutResult; - *(strstr(device, "ttyname:") - 1) = '\0'; - strcat(device, ".%location"); - } - pclose(pipe); - } - - // Add port to the list and clean up memory - if (device) - { - char *location = (char*)malloc(256), *temp = (char*)malloc(64); - snprintf(location, bufferSize, "sysctl -a | grep \"%s\"", device); - pipe = popen(location, "r"); - strcpy(location, "0-0"); - if (pipe) - { - while (fgets(stdOutResult, bufferSize, pipe)) - if (strstr(stdOutResult, "bus") && strstr(stdOutResult, "hubaddr") && strstr(stdOutResult, "port")) - { - char *cursor = strstr(stdOutResult, "bus=") + 4; - size_t length = (size_t)(strchr(cursor, ' ') - cursor); - memcpy(location, cursor, length); - location[length] = '\0'; - strcat(location, "-"); - cursor = strstr(stdOutResult, "hubaddr=") + 8; - length = (size_t)(strchr(cursor, ' ') - cursor); - memcpy(temp, cursor, length); - temp[length] = '\0'; - strcat(location, temp); - strcat(location, "."); - cursor = strstr(stdOutResult, "port=") + 5; - length = (size_t)(strchr(cursor, ' ') - cursor); - memcpy(temp, cursor, length); - temp[length] = '\0'; - strcat(location, temp); - break; - } - pclose(pipe); - } - pushBack(comPorts, systemName, friendlyName, friendlyName, location); - free(location); - free(temp); - } - else - pushBack(comPorts, systemName, friendlyName, friendlyName, "0-0"); - free(stdOutResult); + char isUSB = getPortLocation(directoryEntry->d_name + 3, portLocation); + pushBack(comPorts, systemName, friendlyName, friendlyName, portLocation); } // Clean up memory free(friendlyName); - free(location); } // Clean up memory + free(portLocation); free(systemName); } } @@ -1218,7 +1295,22 @@ void searchForComPorts(serialPortVector* comPorts) // Check if callout port is already enumerated port = fetchPort(comPorts, comPortCu); 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 pushBack(comPorts, comPortCu, friendlyName, friendlyName, portLocation); @@ -1226,7 +1318,22 @@ void searchForComPorts(serialPortVector* comPorts) port = fetchPort(comPorts, comPortTty); strcat(friendlyName, " (Dial-In)"); 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 pushBack(comPorts, comPortTty, friendlyName, friendlyName, portLocation); IOObjectRelease(serialPort); diff --git a/src/main/resources/Android/arm64-v8a/libjSerialComm.so b/src/main/resources/Android/arm64-v8a/libjSerialComm.so index 23ef4d1..ea8b124 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 af0d368..3e8502c 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 f0a9bc7..891e0fd 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 db5e9ca..1cbc299 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 373f4e0..df19712 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 e64a7ee..fcb0762 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 ff80670..2b81274 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 6270ba7..330e969 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 3ad663a..fa23d38 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 13ee4b9..232ffb8 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 b1141da..4152173 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 95b9ce9..1521340 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 7c5c3d0..06d9c2a 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 e148598..1b94ce4 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 36a2cbc..4098a2c 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 bd4a61d..fc891f1 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 37dd68a..06fd0ee 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 195fe72..151cd67 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 ccb2f08..6a1390d 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 8138892..1bfb4df 100755 Binary files a/src/main/resources/OSX/x86_64/libjSerialComm.jnilib and b/src/main/resources/OSX/x86_64/libjSerialComm.jnilib differ