Skip to content

Commit ceb28d0

Browse files
committed
Finish implementation for getting device driver info
1 parent 05edb5a commit ceb28d0

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

src/main/c/Posix/PosixHelperFunctions.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ char getPortDetails(const char *deviceName, char* portLocation, int* vendorID, i
11771177
{
11781178
// Attempt to locate the device in sysctl
11791179
size_t bufferSize = 1024;
1180-
char *stdOutResult = (char*)malloc(bufferSize), *deviceLocation = NULL, *deviceInfo = NULL;
1180+
char *stdOutResult = (char*)malloc(bufferSize), *deviceLocation = NULL, *deviceInfo = NULL, *driverName = NULL;
11811181
snprintf(stdOutResult, bufferSize, "sysctl -a | grep \"ttyname: %s\"", deviceName);
11821182
FILE *pipe = popen(stdOutResult, "r");
11831183
if (pipe)
@@ -1187,14 +1187,18 @@ char getPortDetails(const char *deviceName, char* portLocation, int* vendorID, i
11871187
deviceLocation = stdOutResult;
11881188
*(strstr(deviceLocation, "ttyname:") - 1) = '\0';
11891189
deviceInfo = (char*)malloc(strlen(deviceLocation) + 12);
1190+
driverName = (char*)malloc(strlen(deviceLocation) + 12);
11901191
strcpy(deviceInfo, deviceLocation);
1192+
strcpy(driverName, deviceLocation);
11911193
strcat(deviceLocation, ".%location");
11921194
strcat(deviceInfo, ".%pnpinfo");
1195+
strcat(driverName, ".%driver");
11931196
}
11941197
pclose(pipe);
11951198
}
11961199
strcpy(serialNumber, "Unknown");
11971200
strcpy(manufacturer, "Unknown");
1201+
strcpy(deviceDriver, "Unknown");
11981202

11991203
// Parse port location
12001204
if (deviceLocation)
@@ -1269,12 +1273,32 @@ char getPortDetails(const char *deviceName, char* portLocation, int* vendorID, i
12691273
}
12701274
free(temp);
12711275
}
1272-
// TODO: DEVICE DRIVER
1276+
1277+
// Parse device driver
1278+
if (driverName)
1279+
{
1280+
char *temp = (char*)malloc(64);
1281+
sprintf(temp, "sysctl -a | grep \"%s\"", driverName);
1282+
pipe = popen(temp, "r");
1283+
if (pipe)
1284+
{
1285+
while (fgets(stdOutResult, bufferSize, pipe))
1286+
if (strstr(stdOutResult, ": "))
1287+
{
1288+
strcpy(deviceDriver, strstr(stdOutResult, ": ") + 2);
1289+
break;
1290+
}
1291+
pclose(pipe);
1292+
}
1293+
free(temp);
1294+
}
12731295

12741296
// Clean up memory and return result
12751297
free(stdOutResult);
12761298
if (deviceInfo)
12771299
free(deviceInfo);
1300+
if (driverName)
1301+
free(driverName);
12781302
return (deviceLocation ? 1 : 0);
12791303
}
12801304

@@ -1389,6 +1413,7 @@ char getUsbPortDetails(const char* usbDeviceFile, char* portLocation, char* frie
13891413
size_t bufferSize = 1024;
13901414
strcpy(serialNumber, "Unknown");
13911415
strcpy(manufacturer, "Unknown");
1416+
strcpy(deviceDriver, "Unknown");
13921417
char *stdOutResult = (char*)malloc(bufferSize), *device = (char*)malloc(64);
13931418
snprintf(stdOutResult, bufferSize, "dmesg | grep ucom%s | tail -1", usbDeviceFile + 1);
13941419
FILE *pipe = popen(stdOutResult, "r");
@@ -1469,7 +1494,6 @@ char getUsbPortDetails(const char* usbDeviceFile, char* portLocation, char* frie
14691494
}
14701495
free(usbFile);
14711496
}
1472-
// TODO: DEVICE DRIVER
14731497

14741498
// Clean up memory and return result
14751499
free(device);
@@ -1700,7 +1724,7 @@ void searchForComPorts(serialPortVector* comPorts)
17001724
}
17011725
}
17021726
else
1703-
pushBack(comPorts, comPortCu, friendlyName, friendlyName, portLocation, serialNumber, manufacturer, "TODO", vendorID, productID, 0);
1727+
pushBack(comPorts, comPortCu, friendlyName, friendlyName, portLocation, serialNumber, manufacturer, "Unknown", vendorID, productID, 0);
17041728

17051729
// Check if dialin port is already enumerated
17061730
port = fetchPort(comPorts, comPortTty);
@@ -1723,7 +1747,7 @@ void searchForComPorts(serialPortVector* comPorts)
17231747
}
17241748
}
17251749
else
1726-
pushBack(comPorts, comPortTty, friendlyName, friendlyName, portLocation, serialNumber, manufacturer, "TODO", vendorID, productID, 0);
1750+
pushBack(comPorts, comPortTty, friendlyName, friendlyName, portLocation, serialNumber, manufacturer, "Unknown", vendorID, productID, 0);
17271751
IOObjectRelease(serialPort);
17281752
}
17291753
IOObjectRelease(serialPortIterator);

src/main/c/Windows/SerialPort_Windows.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ static void enumeratePorts(JNIEnv *env)
141141
DWORD devInterfaceIndex = 0;
142142
DEVPROPTYPE devInfoPropType;
143143
SP_DEVINFO_DATA devInfoData;
144+
SP_DRVINFO_DATA_W driverInfoData;
144145
devInfoData.cbSize = sizeof(devInfoData);
146+
driverInfoData.cbSize = sizeof(driverInfoData);
145147
while (SetupDiEnumDeviceInfo(devList, devInterfaceIndex++, &devInfoData))
146148
{
147149
// Attempt to determine the device's Vendor ID and Product ID
@@ -243,8 +245,7 @@ static void enumeratePorts(JNIEnv *env)
243245
}
244246

245247
// Fetch the device driver loaded for this device
246-
wchar_t *driverString = NULL;
247-
// TODO:
248+
wchar_t *driverString = SetupDiGetSelectedDriverW(devList, &devInfoData, &driverInfoData) ? driverInfoData.Description : NULL;
248249

249250
// Fetch the bus-reported device description
250251
DWORD portDescriptionLength = 0;
@@ -352,8 +353,6 @@ static void enumeratePorts(JNIEnv *env)
352353
free(serialNumberString);
353354
if (manufacturerString)
354355
free(manufacturerString);
355-
if (driverString)
356-
free(driverString);
357356
if (friendlyNameMemory)
358357
free(friendlyNameString);
359358
if (portDescriptionMemory)
@@ -375,8 +374,6 @@ static void enumeratePorts(JNIEnv *env)
375374
free(serialNumberString);
376375
if (manufacturerString)
377376
free(manufacturerString);
378-
if (driverString)
379-
free(driverString);
380377
if (friendlyNameMemory)
381378
free(friendlyNameString);
382379
if (portDescriptionMemory)

0 commit comments

Comments
 (0)