Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 39 additions & 32 deletions src/main/c/Posix/PosixHelperFunctions.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,34 +319,17 @@ static char isPtyDevice(const char *deviceLink)
return isPtyDevice;
}

static char* isSymlinkedTtyDevice(const char *deviceLink)
static char* isSymlink(const char *deviceLink)
{
// Attempt to read the path that the potential TTY device link points to
struct stat fileInfo;
char *symlink = NULL;
if ((lstat(deviceLink, &fileInfo) == 0) && fileInfo.st_size && ((fileInfo.st_mode & S_IFMT) == S_IFLNK))
{
ssize_t bufferSize = fileInfo.st_size + 1;
symlink = (char*)malloc(bufferSize);
if (symlink)
{
ssize_t symlinkSize = readlink(deviceLink, symlink, bufferSize);
if (symlinkSize >= 0)
{
symlink[symlinkSize] = '\0';
if (!strstr(symlink, "dev/tty"))
{
free(symlink);
symlink = NULL;
}
}
else
{
free(symlink);
symlink = NULL;
}
}
// realpath automatically adds /dev and allocates the correct buffer size
symlink = realpath(deviceLink, NULL);
}
// Note: Calling function will check if symlink points to serial port
return symlink;
}

Expand Down Expand Up @@ -739,7 +722,7 @@ void searchForComPorts(serialPortVector* comPorts)
closedir(directoryIterator);
}

// Search through the system DEV directory for Bluetooth and PTY devices and symlinks
// Search through the system DEV directory for Bluetooth and PTY devices
directoryIterator = opendir("/dev/");
if (directoryIterator)
{
Expand All @@ -760,17 +743,41 @@ void searchForComPorts(serialPortVector* comPorts)
pushBack(comPorts, fileName, "PTY Device", "Pseudo-Terminal Device", "0-0", "Unknown", "Unknown", "Unknown", -1, -1, 0);
else if (isBluetoothDevice(fileName))
pushBack(comPorts, fileName, "Bluetooth Device", "Bluetooth Device", "0-0", "Unknown", "Unknown", "Unknown", -1, -1, 0);
else

// Read next TTY directory entry
directoryEntry = readdir(directoryIterator);
}
closedir(directoryIterator);
}

// Search through the system DEV directory again for symlinks
// Can't combine with loop above because otherwise symlinks to Bluetooth devices
// will only be found if they come after Bluetooth device in the iterator
directoryIterator = opendir("/dev/");
if (directoryIterator)
{
struct dirent *directoryEntry = readdir(directoryIterator);
while (directoryEntry)
{
// Ensure that the file name buffer is large enough
if ((16 + strlen(directoryEntry->d_name)) > fileNameMaxLength)
{
// Copy symlinked port details from its existing TTY enumeration
char *symlink = isSymlinkedTtyDevice(fileName);
if (symlink)
{
const serialPort *port = fetchPort(comPorts, symlink);
if (port)
pushBack(comPorts, fileName, port->friendlyName, port->portDescription, port->portLocation, port->serialNumber, port->manufacturer, port->deviceDriver, port->vendorID, port->productID, 1);
free(symlink);
}
fileNameMaxLength = 16 + strlen(directoryEntry->d_name);
fileName = (char*)realloc(fileName, fileNameMaxLength);
}

// Build full filename
strcpy(fileName, "/dev/");
strcat(fileName, directoryEntry->d_name);
// Find target of symbolic link if it is one
char *targetDevice = isSymlink(fileName);
if (targetDevice)
{
// check if symlink points to a serial port
const serialPort *port = fetchPort(comPorts, targetDevice);
if (port)
pushBack(comPorts, fileName, port->friendlyName, port->portDescription, port->portLocation, port->serialNumber, port->manufacturer, port->deviceDriver, port->vendorID, port->productID, 1);
free(targetDevice);
}

// Read next TTY directory entry
Expand Down
2 changes: 1 addition & 1 deletion src/test/c/testPollPosix.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ int configPort(serialPort *port)
// Apply changes
if (fcntl(port->handle, F_SETFL, flags))
return 0;
if (setConfigOptions(port->handle, baudRate, &options))
if (setCustomBaudRate(port->handle, baudRate))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change intended to be kept? If so, there doesn't seem to be any point of setting the options variable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because 54bd919 removed setConfigOptions from the library and replaced it with setCustomBaudRate. It's not functionally a part of this change, just required to compile testPollPosix.

I also included is as a separate PR #615 before.

You are right that this was a quick hack to make testPollPosix compile and that it would be good to change/remove more code in testPollPosix. I'll leave it up to @hedgecrw to decide whether to keep this change or have a broken testPollPosix until someone does the full work.

Heiko

return 0;
return 1;
}
Expand Down