Code-style consistency improvement:
Apply clang-format-all.sh using the _clang-format file through all the cpp/.h files. make sure not to apply it to certain serialization structures, since some parser expects the * as part of the name, instead of type. This commit contains no other changes aside from adding and applying clang-format-all.sh
This commit is contained in:
@@ -25,13 +25,13 @@
|
||||
#include "serial/serial.h"
|
||||
|
||||
using serial::PortInfo;
|
||||
using std::istringstream;
|
||||
using std::ifstream;
|
||||
using std::getline;
|
||||
using std::vector;
|
||||
using std::string;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
using std::getline;
|
||||
using std::ifstream;
|
||||
using std::istringstream;
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
static vector<string> glob(const vector<string>& patterns);
|
||||
static string basename(const string& path);
|
||||
@@ -47,289 +47,287 @@ static string format(const char* format, ...);
|
||||
vector<string>
|
||||
glob(const vector<string>& patterns)
|
||||
{
|
||||
vector<string> paths_found;
|
||||
vector<string> paths_found;
|
||||
|
||||
if(patterns.size() == 0)
|
||||
return paths_found;
|
||||
if (patterns.size() == 0)
|
||||
return paths_found;
|
||||
|
||||
glob_t glob_results;
|
||||
glob_t glob_results;
|
||||
|
||||
int glob_retval = glob(patterns[0].c_str(), 0, NULL, &glob_results);
|
||||
int glob_retval = glob(patterns[0].c_str(), 0, NULL, &glob_results);
|
||||
|
||||
vector<string>::const_iterator iter = patterns.begin();
|
||||
vector<string>::const_iterator iter = patterns.begin();
|
||||
|
||||
while(++iter != patterns.end())
|
||||
{
|
||||
glob_retval = glob(iter->c_str(), GLOB_APPEND, NULL, &glob_results);
|
||||
}
|
||||
while (++iter != patterns.end())
|
||||
{
|
||||
glob_retval = glob(iter->c_str(), GLOB_APPEND, NULL, &glob_results);
|
||||
}
|
||||
|
||||
for(int path_index = 0; path_index < glob_results.gl_pathc; path_index++)
|
||||
{
|
||||
paths_found.push_back(glob_results.gl_pathv[path_index]);
|
||||
}
|
||||
for (int path_index = 0; path_index < glob_results.gl_pathc; path_index++)
|
||||
{
|
||||
paths_found.push_back(glob_results.gl_pathv[path_index]);
|
||||
}
|
||||
|
||||
globfree(&glob_results);
|
||||
globfree(&glob_results);
|
||||
|
||||
return paths_found;
|
||||
return paths_found;
|
||||
}
|
||||
|
||||
string
|
||||
basename(const string& path)
|
||||
{
|
||||
size_t pos = path.rfind("/");
|
||||
size_t pos = path.rfind("/");
|
||||
|
||||
if(pos == std::string::npos)
|
||||
return path;
|
||||
if (pos == std::string::npos)
|
||||
return path;
|
||||
|
||||
return string(path, pos+1, string::npos);
|
||||
return string(path, pos + 1, string::npos);
|
||||
}
|
||||
|
||||
string
|
||||
dirname(const string& path)
|
||||
{
|
||||
size_t pos = path.rfind("/");
|
||||
size_t pos = path.rfind("/");
|
||||
|
||||
if(pos == std::string::npos)
|
||||
return path;
|
||||
else if(pos == 0)
|
||||
return "/";
|
||||
if (pos == std::string::npos)
|
||||
return path;
|
||||
else if (pos == 0)
|
||||
return "/";
|
||||
|
||||
return string(path, 0, pos);
|
||||
return string(path, 0, pos);
|
||||
}
|
||||
|
||||
bool
|
||||
path_exists(const string& path)
|
||||
bool path_exists(const string& path)
|
||||
{
|
||||
struct stat sb;
|
||||
struct stat sb;
|
||||
|
||||
if( stat(path.c_str(), &sb ) == 0 )
|
||||
return true;
|
||||
if (stat(path.c_str(), &sb) == 0)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
string
|
||||
realpath(const string& path)
|
||||
{
|
||||
char* real_path = realpath(path.c_str(), NULL);
|
||||
char* real_path = realpath(path.c_str(), NULL);
|
||||
|
||||
string result;
|
||||
string result;
|
||||
|
||||
if(real_path != NULL)
|
||||
{
|
||||
result = real_path;
|
||||
if (real_path != NULL)
|
||||
{
|
||||
result = real_path;
|
||||
|
||||
free(real_path);
|
||||
}
|
||||
free(real_path);
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
string
|
||||
usb_sysfs_friendly_name(const string& sys_usb_path)
|
||||
{
|
||||
unsigned int device_number = 0;
|
||||
unsigned int device_number = 0;
|
||||
|
||||
istringstream( read_line(sys_usb_path + "/devnum") ) >> device_number;
|
||||
istringstream(read_line(sys_usb_path + "/devnum")) >> device_number;
|
||||
|
||||
string manufacturer = read_line( sys_usb_path + "/manufacturer" );
|
||||
string manufacturer = read_line(sys_usb_path + "/manufacturer");
|
||||
|
||||
string product = read_line( sys_usb_path + "/product" );
|
||||
string product = read_line(sys_usb_path + "/product");
|
||||
|
||||
string serial = read_line( sys_usb_path + "/serial" );
|
||||
string serial = read_line(sys_usb_path + "/serial");
|
||||
|
||||
if( manufacturer.empty() && product.empty() && serial.empty() )
|
||||
return "";
|
||||
if (manufacturer.empty() && product.empty() && serial.empty())
|
||||
return "";
|
||||
|
||||
return format("%s %s %s", manufacturer.c_str(), product.c_str(), serial.c_str() );
|
||||
return format("%s %s %s", manufacturer.c_str(), product.c_str(), serial.c_str());
|
||||
}
|
||||
|
||||
vector<string>
|
||||
get_sysfs_info(const string& device_path)
|
||||
{
|
||||
string device_name = basename( device_path );
|
||||
string device_name = basename(device_path);
|
||||
|
||||
string friendly_name;
|
||||
string friendly_name;
|
||||
|
||||
string hardware_id;
|
||||
string hardware_id;
|
||||
|
||||
string sys_device_path = format( "/sys/class/tty/%s/device", device_name.c_str() );
|
||||
string sys_device_path = format("/sys/class/tty/%s/device", device_name.c_str());
|
||||
|
||||
if( device_name.compare(0,6,"ttyUSB") == 0 )
|
||||
{
|
||||
sys_device_path = dirname( dirname( realpath( sys_device_path ) ) );
|
||||
if (device_name.compare(0, 6, "ttyUSB") == 0)
|
||||
{
|
||||
sys_device_path = dirname(dirname(realpath(sys_device_path)));
|
||||
|
||||
if( path_exists( sys_device_path ) )
|
||||
{
|
||||
friendly_name = usb_sysfs_friendly_name( sys_device_path );
|
||||
if (path_exists(sys_device_path))
|
||||
{
|
||||
friendly_name = usb_sysfs_friendly_name(sys_device_path);
|
||||
|
||||
hardware_id = usb_sysfs_hw_string( sys_device_path );
|
||||
}
|
||||
}
|
||||
else if( device_name.compare(0,6,"ttyACM") == 0 )
|
||||
{
|
||||
sys_device_path = dirname( realpath( sys_device_path ) );
|
||||
hardware_id = usb_sysfs_hw_string(sys_device_path);
|
||||
}
|
||||
}
|
||||
else if (device_name.compare(0, 6, "ttyACM") == 0)
|
||||
{
|
||||
sys_device_path = dirname(realpath(sys_device_path));
|
||||
|
||||
if( path_exists( sys_device_path ) )
|
||||
{
|
||||
friendly_name = usb_sysfs_friendly_name( sys_device_path );
|
||||
if (path_exists(sys_device_path))
|
||||
{
|
||||
friendly_name = usb_sysfs_friendly_name(sys_device_path);
|
||||
|
||||
hardware_id = usb_sysfs_hw_string( sys_device_path );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Try to read ID string of PCI device
|
||||
hardware_id = usb_sysfs_hw_string(sys_device_path);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Try to read ID string of PCI device
|
||||
|
||||
string sys_id_path = sys_device_path + "/id";
|
||||
string sys_id_path = sys_device_path + "/id";
|
||||
|
||||
if( path_exists( sys_id_path ) )
|
||||
hardware_id = read_line( sys_id_path );
|
||||
}
|
||||
if (path_exists(sys_id_path))
|
||||
hardware_id = read_line(sys_id_path);
|
||||
}
|
||||
|
||||
if( friendly_name.empty() )
|
||||
friendly_name = device_name;
|
||||
if (friendly_name.empty())
|
||||
friendly_name = device_name;
|
||||
|
||||
if( hardware_id.empty() )
|
||||
hardware_id = "n/a";
|
||||
if (hardware_id.empty())
|
||||
hardware_id = "n/a";
|
||||
|
||||
vector<string> result;
|
||||
result.push_back(friendly_name);
|
||||
result.push_back(hardware_id);
|
||||
vector<string> result;
|
||||
result.push_back(friendly_name);
|
||||
result.push_back(hardware_id);
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
string
|
||||
read_line(const string& file)
|
||||
{
|
||||
ifstream ifs(file.c_str(), ifstream::in);
|
||||
ifstream ifs(file.c_str(), ifstream::in);
|
||||
|
||||
string line;
|
||||
string line;
|
||||
|
||||
if(ifs)
|
||||
{
|
||||
getline(ifs, line);
|
||||
}
|
||||
if (ifs)
|
||||
{
|
||||
getline(ifs, line);
|
||||
}
|
||||
|
||||
return line;
|
||||
return line;
|
||||
}
|
||||
|
||||
string
|
||||
format(const char* format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_list ap;
|
||||
|
||||
size_t buffer_size_bytes = 256;
|
||||
size_t buffer_size_bytes = 256;
|
||||
|
||||
string result;
|
||||
string result;
|
||||
|
||||
char* buffer = (char*)malloc(buffer_size_bytes);
|
||||
char* buffer = (char*)malloc(buffer_size_bytes);
|
||||
|
||||
if( buffer == NULL )
|
||||
return result;
|
||||
if (buffer == NULL)
|
||||
return result;
|
||||
|
||||
bool done = false;
|
||||
bool done = false;
|
||||
|
||||
unsigned int loop_count = 0;
|
||||
unsigned int loop_count = 0;
|
||||
|
||||
while(!done)
|
||||
{
|
||||
va_start(ap, format);
|
||||
while (!done)
|
||||
{
|
||||
va_start(ap, format);
|
||||
|
||||
int return_value = vsnprintf(buffer, buffer_size_bytes, format, ap);
|
||||
int return_value = vsnprintf(buffer, buffer_size_bytes, format, ap);
|
||||
|
||||
if( return_value < 0 )
|
||||
{
|
||||
done = true;
|
||||
}
|
||||
else if( return_value >= buffer_size_bytes )
|
||||
{
|
||||
// Realloc and try again.
|
||||
if (return_value < 0)
|
||||
{
|
||||
done = true;
|
||||
}
|
||||
else if (return_value >= buffer_size_bytes)
|
||||
{
|
||||
// Realloc and try again.
|
||||
|
||||
buffer_size_bytes = return_value + 1;
|
||||
buffer_size_bytes = return_value + 1;
|
||||
|
||||
char* new_buffer_ptr = (char*)realloc(buffer, buffer_size_bytes);
|
||||
char* new_buffer_ptr = (char*)realloc(buffer, buffer_size_bytes);
|
||||
|
||||
if( new_buffer_ptr == NULL )
|
||||
{
|
||||
done = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer = new_buffer_ptr;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = buffer;
|
||||
done = true;
|
||||
}
|
||||
if (new_buffer_ptr == NULL)
|
||||
{
|
||||
done = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer = new_buffer_ptr;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = buffer;
|
||||
done = true;
|
||||
}
|
||||
|
||||
va_end(ap);
|
||||
va_end(ap);
|
||||
|
||||
if( ++loop_count > 5 )
|
||||
done = true;
|
||||
}
|
||||
if (++loop_count > 5)
|
||||
done = true;
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
free(buffer);
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
string
|
||||
usb_sysfs_hw_string(const string& sysfs_path)
|
||||
{
|
||||
string serial_number = read_line( sysfs_path + "/serial" );
|
||||
string serial_number = read_line(sysfs_path + "/serial");
|
||||
|
||||
if( serial_number.length() > 0 )
|
||||
{
|
||||
serial_number = format( "SNR=%s", serial_number.c_str() );
|
||||
}
|
||||
if (serial_number.length() > 0)
|
||||
{
|
||||
serial_number = format("SNR=%s", serial_number.c_str());
|
||||
}
|
||||
|
||||
string vid = read_line( sysfs_path + "/idVendor" );
|
||||
string vid = read_line(sysfs_path + "/idVendor");
|
||||
|
||||
string pid = read_line( sysfs_path + "/idProduct" );
|
||||
string pid = read_line(sysfs_path + "/idProduct");
|
||||
|
||||
return format("USB VID:PID=%s:%s %s", vid.c_str(), pid.c_str(), serial_number.c_str() );
|
||||
return format("USB VID:PID=%s:%s %s", vid.c_str(), pid.c_str(), serial_number.c_str());
|
||||
}
|
||||
|
||||
vector<PortInfo>
|
||||
serial::list_ports()
|
||||
{
|
||||
vector<PortInfo> results;
|
||||
vector<PortInfo> results;
|
||||
|
||||
vector<string> search_globs;
|
||||
search_globs.push_back("/dev/ttyACM*");
|
||||
search_globs.push_back("/dev/ttyS*");
|
||||
search_globs.push_back("/dev/ttyUSB*");
|
||||
search_globs.push_back("/dev/tty.*");
|
||||
search_globs.push_back("/dev/cu.*");
|
||||
vector<string> search_globs;
|
||||
search_globs.push_back("/dev/ttyACM*");
|
||||
search_globs.push_back("/dev/ttyS*");
|
||||
search_globs.push_back("/dev/ttyUSB*");
|
||||
search_globs.push_back("/dev/tty.*");
|
||||
search_globs.push_back("/dev/cu.*");
|
||||
|
||||
vector<string> devices_found = glob( search_globs );
|
||||
vector<string> devices_found = glob(search_globs);
|
||||
|
||||
vector<string>::iterator iter = devices_found.begin();
|
||||
vector<string>::iterator iter = devices_found.begin();
|
||||
|
||||
while( iter != devices_found.end() )
|
||||
{
|
||||
string device = *iter++;
|
||||
while (iter != devices_found.end())
|
||||
{
|
||||
string device = *iter++;
|
||||
|
||||
vector<string> sysfs_info = get_sysfs_info( device );
|
||||
vector<string> sysfs_info = get_sysfs_info(device);
|
||||
|
||||
string friendly_name = sysfs_info[0];
|
||||
string friendly_name = sysfs_info[0];
|
||||
|
||||
string hardware_id = sysfs_info[1];
|
||||
string hardware_id = sysfs_info[1];
|
||||
|
||||
PortInfo device_entry;
|
||||
device_entry.port = device;
|
||||
device_entry.description = friendly_name;
|
||||
device_entry.hardware_id = hardware_id;
|
||||
PortInfo device_entry;
|
||||
device_entry.port = device;
|
||||
device_entry.description = friendly_name;
|
||||
device_entry.hardware_id = hardware_id;
|
||||
|
||||
results.push_back( device_entry );
|
||||
results.push_back(device_entry);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return results;
|
||||
return results;
|
||||
}
|
||||
|
||||
#endif // defined(__linux__)
|
||||
#endif // defined(__linux__)
|
||||
|
||||
@@ -20,267 +20,266 @@ using std::vector;
|
||||
|
||||
#define HARDWARE_ID_STRING_LENGTH 128
|
||||
|
||||
string cfstring_to_string( CFStringRef cfstring );
|
||||
string get_device_path( io_object_t& serial_port );
|
||||
string get_class_name( io_object_t& obj );
|
||||
io_registry_entry_t get_parent_iousb_device( io_object_t& serial_port );
|
||||
string get_string_property( io_object_t& device, const char* property );
|
||||
uint16_t get_int_property( io_object_t& device, const char* property );
|
||||
string cfstring_to_string(CFStringRef cfstring);
|
||||
string get_device_path(io_object_t& serial_port);
|
||||
string get_class_name(io_object_t& obj);
|
||||
io_registry_entry_t get_parent_iousb_device(io_object_t& serial_port);
|
||||
string get_string_property(io_object_t& device, const char* property);
|
||||
uint16_t get_int_property(io_object_t& device, const char* property);
|
||||
string rtrim(const string& str);
|
||||
|
||||
string
|
||||
cfstring_to_string( CFStringRef cfstring )
|
||||
cfstring_to_string(CFStringRef cfstring)
|
||||
{
|
||||
char cstring[MAXPATHLEN];
|
||||
string result;
|
||||
char cstring[MAXPATHLEN];
|
||||
string result;
|
||||
|
||||
if( cfstring )
|
||||
{
|
||||
Boolean success = CFStringGetCString( cfstring,
|
||||
cstring,
|
||||
sizeof(cstring),
|
||||
kCFStringEncodingASCII );
|
||||
if (cfstring)
|
||||
{
|
||||
Boolean success = CFStringGetCString(cfstring,
|
||||
cstring,
|
||||
sizeof(cstring),
|
||||
kCFStringEncodingASCII);
|
||||
|
||||
if( success )
|
||||
result = cstring;
|
||||
}
|
||||
if (success)
|
||||
result = cstring;
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
string
|
||||
get_device_path( io_object_t& serial_port )
|
||||
get_device_path(io_object_t& serial_port)
|
||||
{
|
||||
CFTypeRef callout_path;
|
||||
string device_path;
|
||||
CFTypeRef callout_path;
|
||||
string device_path;
|
||||
|
||||
callout_path = IORegistryEntryCreateCFProperty( serial_port,
|
||||
CFSTR(kIOCalloutDeviceKey),
|
||||
kCFAllocatorDefault,
|
||||
0 );
|
||||
callout_path = IORegistryEntryCreateCFProperty(serial_port,
|
||||
CFSTR(kIOCalloutDeviceKey),
|
||||
kCFAllocatorDefault,
|
||||
0);
|
||||
|
||||
if (callout_path)
|
||||
{
|
||||
if( CFGetTypeID(callout_path) == CFStringGetTypeID() )
|
||||
device_path = cfstring_to_string( static_cast<CFStringRef>(callout_path) );
|
||||
if (callout_path)
|
||||
{
|
||||
if (CFGetTypeID(callout_path) == CFStringGetTypeID())
|
||||
device_path = cfstring_to_string(static_cast<CFStringRef>(callout_path));
|
||||
|
||||
CFRelease(callout_path);
|
||||
}
|
||||
CFRelease(callout_path);
|
||||
}
|
||||
|
||||
return device_path;
|
||||
return device_path;
|
||||
}
|
||||
|
||||
string
|
||||
get_class_name( io_object_t& obj )
|
||||
get_class_name(io_object_t& obj)
|
||||
{
|
||||
string result;
|
||||
io_name_t class_name;
|
||||
kern_return_t kern_result;
|
||||
string result;
|
||||
io_name_t class_name;
|
||||
kern_return_t kern_result;
|
||||
|
||||
kern_result = IOObjectGetClass( obj, class_name );
|
||||
kern_result = IOObjectGetClass(obj, class_name);
|
||||
|
||||
if( kern_result == KERN_SUCCESS )
|
||||
result = class_name;
|
||||
if (kern_result == KERN_SUCCESS)
|
||||
result = class_name;
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
io_registry_entry_t
|
||||
get_parent_iousb_device( io_object_t& serial_port )
|
||||
get_parent_iousb_device(io_object_t& serial_port)
|
||||
{
|
||||
io_object_t device = serial_port;
|
||||
io_registry_entry_t parent = 0;
|
||||
io_registry_entry_t result = 0;
|
||||
kern_return_t kern_result = KERN_FAILURE;
|
||||
string name = get_class_name(device);
|
||||
io_object_t device = serial_port;
|
||||
io_registry_entry_t parent = 0;
|
||||
io_registry_entry_t result = 0;
|
||||
kern_return_t kern_result = KERN_FAILURE;
|
||||
string name = get_class_name(device);
|
||||
|
||||
// Walk the IO Registry tree looking for this devices parent IOUSBDevice.
|
||||
while( name != "IOUSBDevice" )
|
||||
{
|
||||
kern_result = IORegistryEntryGetParentEntry( device,
|
||||
kIOServicePlane,
|
||||
&parent );
|
||||
// Walk the IO Registry tree looking for this devices parent IOUSBDevice.
|
||||
while (name != "IOUSBDevice")
|
||||
{
|
||||
kern_result = IORegistryEntryGetParentEntry(device,
|
||||
kIOServicePlane,
|
||||
&parent);
|
||||
|
||||
if(kern_result != KERN_SUCCESS)
|
||||
{
|
||||
result = 0;
|
||||
break;
|
||||
}
|
||||
if (kern_result != KERN_SUCCESS)
|
||||
{
|
||||
result = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
device = parent;
|
||||
device = parent;
|
||||
|
||||
name = get_class_name(device);
|
||||
}
|
||||
name = get_class_name(device);
|
||||
}
|
||||
|
||||
if(kern_result == KERN_SUCCESS)
|
||||
result = device;
|
||||
if (kern_result == KERN_SUCCESS)
|
||||
result = device;
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
string
|
||||
get_string_property( io_object_t& device, const char* property )
|
||||
get_string_property(io_object_t& device, const char* property)
|
||||
{
|
||||
string property_name;
|
||||
string property_name;
|
||||
|
||||
if( device )
|
||||
{
|
||||
CFStringRef property_as_cfstring = CFStringCreateWithCString (
|
||||
kCFAllocatorDefault,
|
||||
property,
|
||||
kCFStringEncodingASCII );
|
||||
if (device)
|
||||
{
|
||||
CFStringRef property_as_cfstring = CFStringCreateWithCString(
|
||||
kCFAllocatorDefault,
|
||||
property,
|
||||
kCFStringEncodingASCII);
|
||||
|
||||
CFTypeRef name_as_cfstring = IORegistryEntryCreateCFProperty(
|
||||
device,
|
||||
property_as_cfstring,
|
||||
kCFAllocatorDefault,
|
||||
0 );
|
||||
CFTypeRef name_as_cfstring = IORegistryEntryCreateCFProperty(
|
||||
device,
|
||||
property_as_cfstring,
|
||||
kCFAllocatorDefault,
|
||||
0);
|
||||
|
||||
if( name_as_cfstring )
|
||||
{
|
||||
if( CFGetTypeID(name_as_cfstring) == CFStringGetTypeID() )
|
||||
property_name = cfstring_to_string( static_cast<CFStringRef>(name_as_cfstring) );
|
||||
if (name_as_cfstring)
|
||||
{
|
||||
if (CFGetTypeID(name_as_cfstring) == CFStringGetTypeID())
|
||||
property_name = cfstring_to_string(static_cast<CFStringRef>(name_as_cfstring));
|
||||
|
||||
CFRelease(name_as_cfstring);
|
||||
}
|
||||
CFRelease(name_as_cfstring);
|
||||
}
|
||||
|
||||
if(property_as_cfstring)
|
||||
CFRelease(property_as_cfstring);
|
||||
}
|
||||
if (property_as_cfstring)
|
||||
CFRelease(property_as_cfstring);
|
||||
}
|
||||
|
||||
return property_name;
|
||||
return property_name;
|
||||
}
|
||||
|
||||
uint16_t
|
||||
get_int_property( io_object_t& device, const char* property )
|
||||
get_int_property(io_object_t& device, const char* property)
|
||||
{
|
||||
uint16_t result = 0;
|
||||
uint16_t result = 0;
|
||||
|
||||
if( device )
|
||||
{
|
||||
CFStringRef property_as_cfstring = CFStringCreateWithCString (
|
||||
kCFAllocatorDefault,
|
||||
property,
|
||||
kCFStringEncodingASCII );
|
||||
if (device)
|
||||
{
|
||||
CFStringRef property_as_cfstring = CFStringCreateWithCString(
|
||||
kCFAllocatorDefault,
|
||||
property,
|
||||
kCFStringEncodingASCII);
|
||||
|
||||
CFTypeRef number = IORegistryEntryCreateCFProperty( device,
|
||||
property_as_cfstring,
|
||||
kCFAllocatorDefault,
|
||||
0 );
|
||||
CFTypeRef number = IORegistryEntryCreateCFProperty(device,
|
||||
property_as_cfstring,
|
||||
kCFAllocatorDefault,
|
||||
0);
|
||||
|
||||
if(property_as_cfstring)
|
||||
CFRelease(property_as_cfstring);
|
||||
if (property_as_cfstring)
|
||||
CFRelease(property_as_cfstring);
|
||||
|
||||
if( number )
|
||||
{
|
||||
if( CFGetTypeID(number) == CFNumberGetTypeID() )
|
||||
{
|
||||
bool success = CFNumberGetValue( static_cast<CFNumberRef>(number),
|
||||
kCFNumberSInt16Type,
|
||||
&result );
|
||||
if (number)
|
||||
{
|
||||
if (CFGetTypeID(number) == CFNumberGetTypeID())
|
||||
{
|
||||
bool success = CFNumberGetValue(static_cast<CFNumberRef>(number),
|
||||
kCFNumberSInt16Type,
|
||||
&result);
|
||||
|
||||
if( !success )
|
||||
result = 0;
|
||||
}
|
||||
if (!success)
|
||||
result = 0;
|
||||
}
|
||||
|
||||
CFRelease(number);
|
||||
}
|
||||
CFRelease(number);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
string rtrim(const string& str)
|
||||
{
|
||||
string result = str;
|
||||
string result = str;
|
||||
|
||||
string whitespace = " \t\f\v\n\r";
|
||||
string whitespace = " \t\f\v\n\r";
|
||||
|
||||
std::size_t found = result.find_last_not_of(whitespace);
|
||||
std::size_t found = result.find_last_not_of(whitespace);
|
||||
|
||||
if (found != std::string::npos)
|
||||
result.erase(found+1);
|
||||
else
|
||||
result.clear();
|
||||
if (found != std::string::npos)
|
||||
result.erase(found + 1);
|
||||
else
|
||||
result.clear();
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
vector<PortInfo>
|
||||
serial::list_ports(void)
|
||||
{
|
||||
vector<PortInfo> devices_found;
|
||||
CFMutableDictionaryRef classes_to_match;
|
||||
io_iterator_t serial_port_iterator;
|
||||
io_object_t serial_port;
|
||||
mach_port_t master_port;
|
||||
kern_return_t kern_result;
|
||||
vector<PortInfo> devices_found;
|
||||
CFMutableDictionaryRef classes_to_match;
|
||||
io_iterator_t serial_port_iterator;
|
||||
io_object_t serial_port;
|
||||
mach_port_t master_port;
|
||||
kern_return_t kern_result;
|
||||
|
||||
kern_result = IOMasterPort(MACH_PORT_NULL, &master_port);
|
||||
kern_result = IOMasterPort(MACH_PORT_NULL, &master_port);
|
||||
|
||||
if(kern_result != KERN_SUCCESS)
|
||||
return devices_found;
|
||||
if (kern_result != KERN_SUCCESS)
|
||||
return devices_found;
|
||||
|
||||
classes_to_match = IOServiceMatching(kIOSerialBSDServiceValue);
|
||||
classes_to_match = IOServiceMatching(kIOSerialBSDServiceValue);
|
||||
|
||||
if (classes_to_match == NULL)
|
||||
return devices_found;
|
||||
if (classes_to_match == NULL)
|
||||
return devices_found;
|
||||
|
||||
CFDictionarySetValue( classes_to_match,
|
||||
CFSTR(kIOSerialBSDTypeKey),
|
||||
CFSTR(kIOSerialBSDAllTypes) );
|
||||
CFDictionarySetValue(classes_to_match,
|
||||
CFSTR(kIOSerialBSDTypeKey),
|
||||
CFSTR(kIOSerialBSDAllTypes));
|
||||
|
||||
kern_result = IOServiceGetMatchingServices(master_port, classes_to_match, &serial_port_iterator);
|
||||
kern_result = IOServiceGetMatchingServices(master_port, classes_to_match, &serial_port_iterator);
|
||||
|
||||
if (KERN_SUCCESS != kern_result)
|
||||
return devices_found;
|
||||
if (KERN_SUCCESS != kern_result)
|
||||
return devices_found;
|
||||
|
||||
while ( (serial_port = IOIteratorNext(serial_port_iterator)) )
|
||||
{
|
||||
string device_path = get_device_path( serial_port );
|
||||
io_registry_entry_t parent = get_parent_iousb_device( serial_port );
|
||||
IOObjectRelease(serial_port);
|
||||
while ((serial_port = IOIteratorNext(serial_port_iterator)))
|
||||
{
|
||||
string device_path = get_device_path(serial_port);
|
||||
io_registry_entry_t parent = get_parent_iousb_device(serial_port);
|
||||
IOObjectRelease(serial_port);
|
||||
|
||||
if( device_path.empty() )
|
||||
continue;
|
||||
if (device_path.empty())
|
||||
continue;
|
||||
|
||||
PortInfo port_info;
|
||||
port_info.port = device_path;
|
||||
port_info.description = "n/a";
|
||||
port_info.hardware_id = "n/a";
|
||||
PortInfo port_info;
|
||||
port_info.port = device_path;
|
||||
port_info.description = "n/a";
|
||||
port_info.hardware_id = "n/a";
|
||||
|
||||
string device_name = rtrim( get_string_property( parent, "USB Product Name" ) );
|
||||
string vendor_name = rtrim( get_string_property( parent, "USB Vendor Name") );
|
||||
string description = rtrim( vendor_name + " " + device_name );
|
||||
if( !description.empty() )
|
||||
port_info.description = description;
|
||||
string device_name = rtrim(get_string_property(parent, "USB Product Name"));
|
||||
string vendor_name = rtrim(get_string_property(parent, "USB Vendor Name"));
|
||||
string description = rtrim(vendor_name + " " + device_name);
|
||||
if (!description.empty())
|
||||
port_info.description = description;
|
||||
|
||||
string serial_number = rtrim(get_string_property( parent, "USB Serial Number" ) );
|
||||
uint16_t vendor_id = get_int_property( parent, "idVendor" );
|
||||
uint16_t product_id = get_int_property( parent, "idProduct" );
|
||||
string serial_number = rtrim(get_string_property(parent, "USB Serial Number"));
|
||||
uint16_t vendor_id = get_int_property(parent, "idVendor");
|
||||
uint16_t product_id = get_int_property(parent, "idProduct");
|
||||
|
||||
if( vendor_id && product_id )
|
||||
{
|
||||
char cstring[HARDWARE_ID_STRING_LENGTH];
|
||||
if (vendor_id && product_id)
|
||||
{
|
||||
char cstring[HARDWARE_ID_STRING_LENGTH];
|
||||
|
||||
if(serial_number.empty())
|
||||
serial_number = "None";
|
||||
if (serial_number.empty())
|
||||
serial_number = "None";
|
||||
|
||||
int ret = snprintf( cstring, HARDWARE_ID_STRING_LENGTH, "USB VID:PID=%04x:%04x SNR=%s",
|
||||
vendor_id,
|
||||
product_id,
|
||||
serial_number.c_str() );
|
||||
int ret = snprintf(cstring, HARDWARE_ID_STRING_LENGTH, "USB VID:PID=%04x:%04x SNR=%s",
|
||||
vendor_id,
|
||||
product_id,
|
||||
serial_number.c_str());
|
||||
|
||||
if( (ret >= 0) && (ret < HARDWARE_ID_STRING_LENGTH) )
|
||||
port_info.hardware_id = cstring;
|
||||
}
|
||||
if ((ret >= 0) && (ret < HARDWARE_ID_STRING_LENGTH))
|
||||
port_info.hardware_id = cstring;
|
||||
}
|
||||
|
||||
devices_found.push_back(port_info);
|
||||
}
|
||||
devices_found.push_back(port_info);
|
||||
}
|
||||
|
||||
IOObjectRelease(serial_port_iterator);
|
||||
return devices_found;
|
||||
IOObjectRelease(serial_port_iterator);
|
||||
return devices_found;
|
||||
}
|
||||
|
||||
#endif // defined(__APPLE__)
|
||||
#endif // defined(__APPLE__)
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
#include <cstring>
|
||||
|
||||
using serial::PortInfo;
|
||||
using std::vector;
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
static const DWORD port_name_max_length = 256;
|
||||
static const DWORD friendly_name_max_length = 256;
|
||||
@@ -27,8 +27,8 @@ static const DWORD hardware_id_max_length = 256;
|
||||
std::string utf8_encode(const std::wstring &wstr)
|
||||
{
|
||||
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
|
||||
std::string strTo( size_needed, 0 );
|
||||
WideCharToMultiByte (CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
|
||||
std::string strTo(size_needed, 0);
|
||||
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
|
||||
return strTo;
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ serial::list_ports()
|
||||
vector<PortInfo> devices_found;
|
||||
|
||||
HDEVINFO device_info_set = SetupDiGetClassDevs(
|
||||
(const GUID *) &GUID_DEVCLASS_PORTS,
|
||||
(const GUID *)&GUID_DEVCLASS_PORTS,
|
||||
NULL,
|
||||
NULL,
|
||||
DIGCF_PRESENT);
|
||||
@@ -48,7 +48,7 @@ serial::list_ports()
|
||||
|
||||
device_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
|
||||
|
||||
while(SetupDiEnumDeviceInfo(device_info_set, device_info_set_index, &device_info_data))
|
||||
while (SetupDiEnumDeviceInfo(device_info_set, device_info_set_index, &device_info_data))
|
||||
{
|
||||
device_info_set_index++;
|
||||
|
||||
@@ -66,26 +66,26 @@ serial::list_ports()
|
||||
DWORD port_name_length = port_name_max_length;
|
||||
|
||||
LONG return_code = RegQueryValueEx(
|
||||
hkey,
|
||||
_T("PortName"),
|
||||
NULL,
|
||||
NULL,
|
||||
(LPBYTE)port_name,
|
||||
&port_name_length);
|
||||
hkey,
|
||||
_T("PortName"),
|
||||
NULL,
|
||||
NULL,
|
||||
(LPBYTE)port_name,
|
||||
&port_name_length);
|
||||
|
||||
RegCloseKey(hkey);
|
||||
|
||||
if(return_code != EXIT_SUCCESS)
|
||||
if (return_code != EXIT_SUCCESS)
|
||||
continue;
|
||||
|
||||
if(port_name_length > 0 && port_name_length <= port_name_max_length)
|
||||
port_name[port_name_length-1] = '\0';
|
||||
if (port_name_length > 0 && port_name_length <= port_name_max_length)
|
||||
port_name[port_name_length - 1] = '\0';
|
||||
else
|
||||
port_name[0] = '\0';
|
||||
|
||||
// Ignore parallel ports
|
||||
|
||||
if(_tcsstr(port_name, _T("LPT")) != NULL)
|
||||
if (_tcsstr(port_name, _T("LPT")) != NULL)
|
||||
continue;
|
||||
|
||||
// Get port friendly name
|
||||
@@ -94,16 +94,16 @@ serial::list_ports()
|
||||
DWORD friendly_name_actual_length = 0;
|
||||
|
||||
BOOL got_friendly_name = SetupDiGetDeviceRegistryProperty(
|
||||
device_info_set,
|
||||
&device_info_data,
|
||||
SPDRP_FRIENDLYNAME,
|
||||
NULL,
|
||||
(PBYTE)friendly_name,
|
||||
friendly_name_max_length,
|
||||
&friendly_name_actual_length);
|
||||
device_info_set,
|
||||
&device_info_data,
|
||||
SPDRP_FRIENDLYNAME,
|
||||
NULL,
|
||||
(PBYTE)friendly_name,
|
||||
friendly_name_max_length,
|
||||
&friendly_name_actual_length);
|
||||
|
||||
if(got_friendly_name == TRUE && friendly_name_actual_length > 0)
|
||||
friendly_name[friendly_name_actual_length-1] = '\0';
|
||||
if (got_friendly_name == TRUE && friendly_name_actual_length > 0)
|
||||
friendly_name[friendly_name_actual_length - 1] = '\0';
|
||||
else
|
||||
friendly_name[0] = '\0';
|
||||
|
||||
@@ -113,28 +113,28 @@ serial::list_ports()
|
||||
DWORD hardware_id_actual_length = 0;
|
||||
|
||||
BOOL got_hardware_id = SetupDiGetDeviceRegistryProperty(
|
||||
device_info_set,
|
||||
&device_info_data,
|
||||
SPDRP_HARDWAREID,
|
||||
NULL,
|
||||
(PBYTE)hardware_id,
|
||||
hardware_id_max_length,
|
||||
&hardware_id_actual_length);
|
||||
device_info_set,
|
||||
&device_info_data,
|
||||
SPDRP_HARDWAREID,
|
||||
NULL,
|
||||
(PBYTE)hardware_id,
|
||||
hardware_id_max_length,
|
||||
&hardware_id_actual_length);
|
||||
|
||||
if(got_hardware_id == TRUE && hardware_id_actual_length > 0)
|
||||
hardware_id[hardware_id_actual_length-1] = '\0';
|
||||
if (got_hardware_id == TRUE && hardware_id_actual_length > 0)
|
||||
hardware_id[hardware_id_actual_length - 1] = '\0';
|
||||
else
|
||||
hardware_id[0] = '\0';
|
||||
|
||||
#ifdef UNICODE
|
||||
std::string portName = utf8_encode(port_name);
|
||||
std::string friendlyName = utf8_encode(friendly_name);
|
||||
std::string hardwareId = utf8_encode(hardware_id);
|
||||
#else
|
||||
std::string portName = port_name;
|
||||
std::string friendlyName = friendly_name;
|
||||
std::string hardwareId = hardware_id;
|
||||
#endif
|
||||
#ifdef UNICODE
|
||||
std::string portName = utf8_encode(port_name);
|
||||
std::string friendlyName = utf8_encode(friendly_name);
|
||||
std::string hardwareId = utf8_encode(hardware_id);
|
||||
#else
|
||||
std::string portName = port_name;
|
||||
std::string friendlyName = friendly_name;
|
||||
std::string hardwareId = hardware_id;
|
||||
#endif
|
||||
|
||||
PortInfo port_entry;
|
||||
port_entry.port = portName;
|
||||
@@ -149,4 +149,4 @@ serial::list_ports()
|
||||
return devices_found;
|
||||
}
|
||||
|
||||
#endif // #if defined(_WIN32)
|
||||
#endif // #if defined(_WIN32)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user