static void assignFriendlyName()

in src/main/c/Posix/PosixHelperFunctions.c [446:497]


static void assignFriendlyName(const char* portDevPath, char* friendlyName)
{
	// Manually assign a friendly name if the port type is known from its name
	const char *portName = 1 + strrchr(portDevPath, '/');
	if ((strlen(portName) >= 5) && (portName[3] == 'A') && (portName[4] == 'P'))
		strcpy(friendlyName, "Advantech Extended Serial Port");
	else if ((strlen(portName) >= 6) && (portName[0] == 'r') && (portName[1] == 'f') && (portName[2] == 'c') &&
			(portName[3] == 'o') && (portName[4] == 'm') && (portName[5] == 'm'))
		strcpy(friendlyName, "Bluetooth-Based Serial Port");
	else
	{
		// Assign a friendly name based on the serial port driver in use
		char nameAssigned = 0;
		FILE *input = fopen("/proc/tty/drivers", "rb");
		if (input)
		{
			// Read the TTY drivers file line by line
			int maxLineSize = 256;
			char *line = (char*)malloc(maxLineSize);
			while (!nameAssigned && fgets(line, maxLineSize, input))
			{
				// Parse the prefix, name, and type fields
				int i = 0;
				char *token, *remainder = line, *prefix = NULL, *name = NULL, *type = NULL;
				while ((token = strtok_r(remainder, " \t\r\n", &remainder)))
					if (++i == 1)
						name = token;
					else if (i == 2)
						prefix = token;
					else if (i == 5)
						type = token;

				// Assign a friendly name if a matching port prefix was found
				if (prefix && name && type && (strcmp(type, "serial") == 0) && (strstr(portDevPath, prefix) == portDevPath))
				{
					strcpy(friendlyName, "Serial Device (");
					strcat(friendlyName, name);
					strcat(friendlyName, ")");
					nameAssigned = 1;
				}
			}

			// Close the drivers file and clean up memory
			free(line);
			fclose(input);
		}

		// If no driver prefix could be found, just assign a generic friendly name
		if (!nameAssigned)
			strcpy(friendlyName, "USB-Based Serial Port");
	}
}