void searchForComPorts()

in src/main/c/Posix/PosixHelperFunctions.c [1504:1583]


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
				int vendorID = -1, productID = -1;
				char* portLocation = (char*)malloc(256), *serialNumber = (char*)malloc(128), *manufacturer = (char*)malloc(128), *deviceDriver = (char*)malloc(32);
				char isUSB = getUsbPortDetails(directoryEntry->d_name + 3, portLocation, friendlyName, &description, &vendorID, &productID, serialNumber, manufacturer, deviceDriver);

				// 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, serialNumber, manufacturer, deviceDriver, vendorID, productID, S_ISLNK(fileStats.st_mode));
				}

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

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