void searchForComPorts()

in src/main/c/Posix/PosixHelperFunctions.c [1305:1387]


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'))))
			{
				// Ensure that the file is not an init or a lock file
				if ((strlen(directoryEntry->d_name) < 5) ||
						(memcmp(".init", directoryEntry->d_name + strlen(directoryEntry->d_name) - 5, 5) &&
						 memcmp(".lock", directoryEntry->d_name + strlen(directoryEntry->d_name) - 5, 5)))
				{
					// Determine system name of port
					char* systemName = (char*)malloc(256);
					strcpy(systemName, "/dev/");
					strcat(systemName, directoryEntry->d_name);

					// Determine location of port
					int vendorID = -1, productID = -1;
					char* portLocation = (char*)malloc(256), *serialNumber = (char*)malloc(128), *manufacturer = (char*)malloc(128), *deviceDriver = (char*)malloc(32);
					char isUSB = getPortDetails(directoryEntry->d_name + 3, portLocation, &vendorID, &productID, serialNumber, manufacturer, deviceDriver);

					// 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* friendlyName = (char*)malloc(256);
						if (directoryEntry->d_name[0] == 'c')
							strcpy(friendlyName, "Serial Port");
						else
							strcpy(friendlyName, "Serial Port (Dial-In)");

						// Add the port to the list if it is not a directory
						struct stat fileStats;
						stat(systemName, &fileStats);
						if (!S_ISDIR(fileStats.st_mode))
							pushBack(comPorts, systemName, friendlyName, friendlyName, portLocation, serialNumber, manufacturer, deviceDriver, vendorID, productID, S_ISLNK(fileStats.st_mode));

						// Clean up memory
						free(friendlyName);
					}

					// Clean up memory
					free(serialNumber);
					free(manufacturer);
					free(deviceDriver);
					free(portLocation);
					free(systemName);
				}
			}
			directoryEntry = readdir(directoryIterator);
		}

		// Close the directory
		closedir(directoryIterator);
	}
}