Set the self-reported port description field
This commit is contained in:
parent
1398951278
commit
8b89baf459
|
@ -2,7 +2,7 @@
|
|||
* SerialPort_OSX.c
|
||||
*
|
||||
* Created on: Feb 25, 2012
|
||||
* Last Updated on: Jan 03, 2018
|
||||
* Last Updated on: Apr 01, 2018
|
||||
* Author: Will Hedgecock
|
||||
*
|
||||
* Copyright (C) 2012-2018 Fazecast, Inc.
|
||||
|
@ -46,7 +46,8 @@ jclass serialCommClass;
|
|||
jmethodID serialCommConstructor;
|
||||
jfieldID serialPortFdField;
|
||||
jfieldID comPortField;
|
||||
jfieldID portStringField;
|
||||
jfieldID friendlyNameField;
|
||||
jfieldID portDescriptionField;
|
||||
jfieldID isOpenedField;
|
||||
jfieldID baudRateField;
|
||||
jfieldID dataBitsField;
|
||||
|
@ -63,7 +64,7 @@ JNIEXPORT jobjectArray JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCommP
|
|||
io_object_t serialPort;
|
||||
io_iterator_t serialPortIterator;
|
||||
int numValues = 0;
|
||||
char portString[1024], comPortCu[1024], comPortTty[1024];
|
||||
char friendlyName[1024], comPortCu[1024], comPortTty[1024], portDescription[1024];
|
||||
|
||||
// Enumerate serial ports on machine
|
||||
IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching(kIOSerialBSDServiceValue), &serialPortIterator);
|
||||
|
@ -78,14 +79,14 @@ JNIEXPORT jobjectArray JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCommP
|
|||
{
|
||||
// Get serial port information
|
||||
serialPort = IOIteratorNext(serialPortIterator);
|
||||
portString[0] = '\0';
|
||||
friendlyName[0] = '\0';
|
||||
io_registry_entry_t parent = 0;
|
||||
io_registry_entry_t service = serialPort;
|
||||
while (service)
|
||||
{
|
||||
if (IOObjectConformsTo(service, "IOUSBDevice"))
|
||||
{
|
||||
IORegistryEntryGetName(service, portString);
|
||||
IORegistryEntryGetName(service, friendlyName);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -99,11 +100,11 @@ JNIEXPORT jobjectArray JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCommP
|
|||
IOObjectRelease(service);
|
||||
|
||||
// Get serial port name and COM value
|
||||
if (portString[0] == '\0')
|
||||
if (friendlyName[0] == '\0')
|
||||
{
|
||||
CFStringRef portStringRef = (CFStringRef)IORegistryEntryCreateCFProperty(serialPort, CFSTR(kIOTTYDeviceKey), kCFAllocatorDefault, 0);
|
||||
CFStringGetCString(portStringRef, portString, sizeof(portString), kCFStringEncodingUTF8);
|
||||
CFRelease(portStringRef);
|
||||
CFStringRef friendlyNameRef = (CFStringRef)IORegistryEntryCreateCFProperty(serialPort, CFSTR(kIOTTYDeviceKey), kCFAllocatorDefault, 0);
|
||||
CFStringGetCString(friendlyNameRef, friendlyName, sizeof(friendlyName), kCFStringEncodingUTF8);
|
||||
CFRelease(friendlyNameRef);
|
||||
}
|
||||
CFStringRef comPortRef = (CFStringRef)IORegistryEntryCreateCFProperty(serialPort, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
|
||||
CFStringGetCString(comPortRef, comPortCu, sizeof(comPortCu), kCFStringEncodingUTF8);
|
||||
|
@ -114,15 +115,17 @@ JNIEXPORT jobjectArray JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCommP
|
|||
|
||||
// Create new SerialComm callout object containing the enumerated values and add to array
|
||||
jobject serialCommObject = (*env)->NewObject(env, serialCommClass, serialCommConstructor);
|
||||
(*env)->SetObjectField(env, serialCommObject, portStringField, (*env)->NewStringUTF(env, portString));
|
||||
(*env)->SetObjectField(env, serialCommObject, portDescriptionField, (*env)->NewStringUTF(env, friendlyName));
|
||||
(*env)->SetObjectField(env, serialCommObject, friendlyNameField, (*env)->NewStringUTF(env, friendlyName));
|
||||
(*env)->SetObjectField(env, serialCommObject, comPortField, (*env)->NewStringUTF(env, comPortCu));
|
||||
(*env)->SetObjectArrayElement(env, arrayObject, i*2, serialCommObject);
|
||||
(*env)->DeleteLocalRef(env, serialCommObject);
|
||||
|
||||
// Create new SerialComm dialin object containing the enumerated values and add to array
|
||||
strcat(portString, " (Dial-In)");
|
||||
strcat(friendlyName, " (Dial-In)");
|
||||
serialCommObject = (*env)->NewObject(env, serialCommClass, serialCommConstructor);
|
||||
(*env)->SetObjectField(env, serialCommObject, portStringField, (*env)->NewStringUTF(env, portString));
|
||||
(*env)->SetObjectField(env, serialCommObject, portDescriptionField, (*env)->NewStringUTF(env, friendlyName));
|
||||
(*env)->SetObjectField(env, serialCommObject, friendlyNameField, (*env)->NewStringUTF(env, friendlyName));
|
||||
(*env)->SetObjectField(env, serialCommObject, comPortField, (*env)->NewStringUTF(env, comPortTty));
|
||||
(*env)->SetObjectArrayElement(env, arrayObject, i*2 + 1, serialCommObject);
|
||||
(*env)->DeleteLocalRef(env, serialCommObject);
|
||||
|
@ -142,7 +145,8 @@ JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_initializeLibrar
|
|||
// Cache
|
||||
serialPortFdField = (*env)->GetFieldID(env, serialCommClass, "portHandle", "J");
|
||||
comPortField = (*env)->GetFieldID(env, serialCommClass, "comPort", "Ljava/lang/String;");
|
||||
portStringField = (*env)->GetFieldID(env, serialCommClass, "portString", "Ljava/lang/String;");
|
||||
friendlyNameField = (*env)->GetFieldID(env, serialCommClass, "friendlyName", "Ljava/lang/String;");
|
||||
portDescriptionField = (*env)->GetFieldID(env, serialCommClass, "portDescription", "Ljava/lang/String;");
|
||||
isOpenedField = (*env)->GetFieldID(env, serialCommClass, "isOpened", "Z");
|
||||
baudRateField = (*env)->GetFieldID(env, serialCommClass, "baudRate", "I");
|
||||
dataBitsField = (*env)->GetFieldID(env, serialCommClass, "dataBits", "I");
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* SerialPort_Windows.c
|
||||
*
|
||||
* Created on: Feb 25, 2012
|
||||
* Last Updated on: Jan 03, 2018
|
||||
* Last Updated on: Apr 01, 2018
|
||||
* Author: Will Hedgecock
|
||||
*
|
||||
* Copyright (C) 2012-2018 Fazecast, Inc.
|
||||
|
@ -24,13 +24,16 @@
|
|||
*/
|
||||
|
||||
#ifdef _WIN32
|
||||
#define WINVER _WIN32_WINNT_WINXP
|
||||
#define _WIN32_WINNT _WIN32_WINNT_WINXP
|
||||
#define NTDDI_VERSION NTDDI_WINXP
|
||||
#define WINVER _WIN32_WINNT_VISTA
|
||||
#define _WIN32_WINNT _WIN32_WINNT_VISTA
|
||||
#define NTDDI_VERSION NTDDI_VISTA
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <initguid.h>
|
||||
#include <windows.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <setupapi.h>
|
||||
#include <devpkey.h>
|
||||
#include "../com_fazecast_jSerialComm_SerialPort.h"
|
||||
#include "WindowsHelperFunctions.h"
|
||||
|
||||
|
@ -39,7 +42,8 @@ jclass serialCommClass;
|
|||
jmethodID serialCommConstructor;
|
||||
jfieldID serialPortHandleField;
|
||||
jfieldID comPortField;
|
||||
jfieldID portStringField;
|
||||
jfieldID friendlyNameField;
|
||||
jfieldID portDescriptionField;
|
||||
jfieldID isOpenedField;
|
||||
jfieldID baudRateField;
|
||||
jfieldID dataBitsField;
|
||||
|
@ -60,7 +64,7 @@ JNIEXPORT jobjectArray JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCommP
|
|||
DWORD subKeyLength1, subKeyLength2, subKeyLength3, friendlyNameLength;
|
||||
|
||||
// Enumerate serial ports on machine
|
||||
charPairVector serialCommPorts = { (char**)malloc(1), (char**)malloc(1), 0 };
|
||||
charTupleVector serialCommPorts = { (char**)malloc(1), (char**)malloc(1), (char**)malloc(1), 0 };
|
||||
if ((RegOpenKeyEx(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_QUERY_VALUE, &keyHandle1) == ERROR_SUCCESS) &&
|
||||
(RegQueryInfoKey(keyHandle1, NULL, NULL, NULL, NULL, NULL, NULL, &numValues, &maxValueLength, &maxComPortLength, NULL, NULL) == ERROR_SUCCESS))
|
||||
{
|
||||
|
@ -85,7 +89,7 @@ JNIEXPORT jobjectArray JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCommP
|
|||
char* descriptionString = strrchr(valueName, '\\') ? (strrchr(valueName, '\\') + 1) : valueName;
|
||||
|
||||
// Add new SerialComm object to vector
|
||||
push_back(&serialCommPorts, comPortString, descriptionString);
|
||||
push_back(&serialCommPorts, comPortString, descriptionString, descriptionString);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -207,15 +211,18 @@ JNIEXPORT jobjectArray JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCommP
|
|||
strcpy(systemPortName, "\\\\.\\");
|
||||
strcat(systemPortName, serialCommPorts.first[i]);
|
||||
env->SetObjectField(serialCommObject, comPortField, env->NewStringUTF(systemPortName));
|
||||
env->SetObjectField(serialCommObject, portStringField, env->NewStringUTF(serialCommPorts.second[i]));
|
||||
env->SetObjectField(serialCommObject, friendlyNameField, env->NewStringUTF(serialCommPorts.second[i]));
|
||||
env->SetObjectField(serialCommObject, portDescriptionField, env->NewStringUTF(serialCommPorts.third[i]));
|
||||
free(serialCommPorts.first[i]);
|
||||
free(serialCommPorts.second[i]);
|
||||
free(serialCommPorts.third[i]);
|
||||
|
||||
// Add new SerialComm object to array
|
||||
env->SetObjectArrayElement(arrayObject, i, serialCommObject);
|
||||
}
|
||||
free(serialCommPorts.first);
|
||||
free(serialCommPorts.second);
|
||||
free(serialCommPorts.third);
|
||||
return arrayObject;
|
||||
}
|
||||
|
||||
|
@ -228,7 +235,8 @@ JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_initializeLibrar
|
|||
// Cache
|
||||
serialPortHandleField = env->GetFieldID(serialCommClass, "portHandle", "J");
|
||||
comPortField = env->GetFieldID(serialCommClass, "comPort", "Ljava/lang/String;");
|
||||
portStringField = env->GetFieldID(serialCommClass, "portString", "Ljava/lang/String;");
|
||||
friendlyNameField = env->GetFieldID(serialCommClass, "friendlyName", "Ljava/lang/String;");
|
||||
portDescriptionField = env->GetFieldID(serialCommClass, "portDescription", "Ljava/lang/String;");
|
||||
isOpenedField = env->GetFieldID(serialCommClass, "isOpened", "Z");
|
||||
baudRateField = env->GetFieldID(serialCommClass, "baudRate", "I");
|
||||
dataBitsField = env->GetFieldID(serialCommClass, "dataBits", "I");
|
||||
|
|
Loading…
Reference in New Issue