Publish curated LumaOps source from c85ee9ef8f1454f5c305398902afbb9e6738de40
Managed validation / full (push) Failing after 8s

This commit is contained in:
Public Source Publisher committed 2026-09-30 00:26:45 +00:00
1 parent 91f8b3e3c1
commit def7920856
2271 files changed
+501225 -501178

No files matched your search

+21 -21
View File
@@ -1,21 +1,21 @@
/*---------------------------------------------------------*\
| find_usb_serial_port.h |
| |
| Finds the serial port path(s) of USB serial port devices|
| given the USB VID and PID of the device |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string *> find_usb_serial_port(unsigned short vid, unsigned short pid);
/*---------------------------------------------------------*\
| find_usb_serial_port.h |
| |
| Finds the serial port path(s) of USB serial port devices|
| given the USB VID and PID of the device |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string *> find_usb_serial_port(unsigned short vid, unsigned short pid);
+147 -147
View File
@@ -1,147 +1,147 @@
/*---------------------------------------------------------*\
| find_usb_serial_port_linux.cpp |
| |
| Finds the serial port path(s) of USB serial port devices|
| given the USB VID and PID of the device |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#include "find_usb_serial_port.h"
#include <unistd.h>
#include <dirent.h>
/*---------------------------------------------------------------------*\
| |
| find_usb_serial_port |
| |
| This function returns the name of the first USB serial port matching|
| the given USB product and vendor ID. |
| |
| vid: Vendor ID code |
| pid: Product ID code |
| |
| returns: std::string containing port name "COMx" or "/dev/ttyX" |
| |
\*---------------------------------------------------------------------*/
std::vector<std::string *> find_usb_serial_port(unsigned short vid, unsigned short pid)
{
std::vector<std::string *> ret_vector;
std::string * tmp_string;
DIR* dir;
char symlink_path[1024] = {0};
struct dirent* ent;
char vid_pid[10] = {0}; //Store VID/PID
/*-----------------------------------------------------------------*\
| Open /sys/class/tty |
\*-----------------------------------------------------------------*/
dir = opendir("/sys/class/tty");
if(dir == NULL)
{
return ret_vector;
}
/*-----------------------------------------------------------------*\
| Loop through all symlinks in /sys/class/tty directory to find |
| paths with "usb" in them. These links should have the USB device |
| index which can be used to find the VID/PID |
\*-----------------------------------------------------------------*/
ent = readdir(dir);
while(ent != NULL)
{
if(ent->d_type == DT_LNK)
{
char tty_path[1024];
strcpy(tty_path, "/sys/class/tty/");
strcat(tty_path, ent->d_name);
/*-----------------------------------------------------------------*\
| readlink() does not null-terminate, so manually terminate it |
\*-----------------------------------------------------------------*/
ssize_t link_path_size = readlink(tty_path, symlink_path, 1024);
if(link_path_size < 0 || link_path_size >= 1024)
{
/*-----------------------------------------------------------------*\
| readlink failed or buffer too small, skip this device |
\*-----------------------------------------------------------------*/
ent = readdir(dir);
continue;
}
symlink_path[link_path_size] = '\0';
char * usb_string = strstr(symlink_path, "usb");
if(usb_string != NULL)
{
char * usb_dev = strstr(usb_string, "/");
usb_dev++;
char * usb_end = strstr(usb_dev, "/tty");
*usb_end = '\0';
usb_end = strrchr(usb_dev, '/');
*usb_end = '\0';
char usb_path[1024];
strcpy(usb_path, "/sys/bus/usb/devices/");
strcat(usb_path, usb_dev);
char vendor_path[1024];
char product_path[1024];
strcpy(vendor_path, usb_path);
strcat(vendor_path, "/idVendor");
strcpy(product_path, usb_path);
strcat(product_path, "/idProduct");
std::ifstream vendor_file;
std::ifstream product_file;
std::string vendor_string;
std::string product_string;
vendor_file.open(vendor_path);
product_file.open(product_path);
std::getline(vendor_file, vendor_string);
std::getline(product_file, product_string);
snprintf(vid_pid, 10, "%04x", vid);
if(strcmp(vid_pid, vendor_string.c_str()) == 0)
{
snprintf(vid_pid, 10, "%04x", pid);
if(strcmp(vid_pid, product_string.c_str()) == 0)
{
char* port_string = NULL;
for(int i = strlen(tty_path); i > 0; i--)
{
if(tty_path[i] == '/')
{
port_string = &tty_path[i + 1];
break;
}
}
tmp_string = new std::string("/dev/");
tmp_string->append(port_string);
ret_vector.push_back(tmp_string);
}
}
}
}
ent = readdir(dir);
}
closedir(dir);
return ret_vector;
} /* find_usb_serial_port() */
/*---------------------------------------------------------*\
| find_usb_serial_port_linux.cpp |
| |
| Finds the serial port path(s) of USB serial port devices|
| given the USB VID and PID of the device |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#include "find_usb_serial_port.h"
#include <unistd.h>
#include <dirent.h>
/*---------------------------------------------------------------------*\
| |
| find_usb_serial_port |
| |
| This function returns the name of the first USB serial port matching|
| the given USB product and vendor ID. |
| |
| vid: Vendor ID code |
| pid: Product ID code |
| |
| returns: std::string containing port name "COMx" or "/dev/ttyX" |
| |
\*---------------------------------------------------------------------*/
std::vector<std::string *> find_usb_serial_port(unsigned short vid, unsigned short pid)
{
std::vector<std::string *> ret_vector;
std::string * tmp_string;
DIR* dir;
char symlink_path[1024] = {0};
struct dirent* ent;
char vid_pid[10] = {0}; //Store VID/PID
/*-----------------------------------------------------------------*\
| Open /sys/class/tty |
\*-----------------------------------------------------------------*/
dir = opendir("/sys/class/tty");
if(dir == NULL)
{
return ret_vector;
}
/*-----------------------------------------------------------------*\
| Loop through all symlinks in /sys/class/tty directory to find |
| paths with "usb" in them. These links should have the USB device |
| index which can be used to find the VID/PID |
\*-----------------------------------------------------------------*/
ent = readdir(dir);
while(ent != NULL)
{
if(ent->d_type == DT_LNK)
{
char tty_path[1024];
strcpy(tty_path, "/sys/class/tty/");
strcat(tty_path, ent->d_name);
/*-----------------------------------------------------------------*\
| readlink() does not null-terminate, so manually terminate it |
\*-----------------------------------------------------------------*/
ssize_t link_path_size = readlink(tty_path, symlink_path, 1024);
if(link_path_size < 0 || link_path_size >= 1024)
{
/*-----------------------------------------------------------------*\
| readlink failed or buffer too small, skip this device |
\*-----------------------------------------------------------------*/
ent = readdir(dir);
continue;
}
symlink_path[link_path_size] = '\0';
char * usb_string = strstr(symlink_path, "usb");
if(usb_string != NULL)
{
char * usb_dev = strstr(usb_string, "/");
usb_dev++;
char * usb_end = strstr(usb_dev, "/tty");
*usb_end = '\0';
usb_end = strrchr(usb_dev, '/');
*usb_end = '\0';
char usb_path[1024];
strcpy(usb_path, "/sys/bus/usb/devices/");
strcat(usb_path, usb_dev);
char vendor_path[1024];
char product_path[1024];
strcpy(vendor_path, usb_path);
strcat(vendor_path, "/idVendor");
strcpy(product_path, usb_path);
strcat(product_path, "/idProduct");
std::ifstream vendor_file;
std::ifstream product_file;
std::string vendor_string;
std::string product_string;
vendor_file.open(vendor_path);
product_file.open(product_path);
std::getline(vendor_file, vendor_string);
std::getline(product_file, product_string);
snprintf(vid_pid, 10, "%04x", vid);
if(strcmp(vid_pid, vendor_string.c_str()) == 0)
{
snprintf(vid_pid, 10, "%04x", pid);
if(strcmp(vid_pid, product_string.c_str()) == 0)
{
char* port_string = NULL;
for(int i = strlen(tty_path); i > 0; i--)
{
if(tty_path[i] == '/')
{
port_string = &tty_path[i + 1];
break;
}
}
tmp_string = new std::string("/dev/");
tmp_string->append(port_string);
ret_vector.push_back(tmp_string);
}
}
}
}
ent = readdir(dir);
}
closedir(dir);
return ret_vector;
} /* find_usb_serial_port() */
+173 -173
View File
@@ -1,173 +1,173 @@
/*---------------------------------------------------------*\
| find_usb_serial_port_macos.cpp |
| |
| Finds the serial port path(s) of USB serial port devices|
| given the USB VID and PID of the device |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#include "find_usb_serial_port.h"
std::string exec(const char* cmd)
{
char buffer[128];
std::string result = "";
FILE* pipe = popen(cmd, "r");
if(!pipe)
{
throw std::runtime_error("popen() failed!");
}
try
{
while(fgets(buffer, sizeof(buffer), pipe) != NULL)
{
result += buffer;
}
}
catch(...)
{
pclose(pipe);
throw;
}
pclose(pipe);
return result;
}
/*---------------------------------------------------------------------*\
| |
| find_usb_serial_port |
| |
| This function returns the name of the first USB serial port matching|
| the given USB product and vendor ID. |
| |
| vid: Vendor ID code |
| pid: Product ID code |
| |
| returns: std::string containing port name "COMx" or "/dev/ttyX" |
| |
\*---------------------------------------------------------------------*/
std::vector<std::string *> find_usb_serial_port(unsigned short vid, unsigned short pid)
{
/*-----------------------------------------------------*\
| Strings to search for in ioreg output |
\*-----------------------------------------------------*/
#define IO_CALLOUT_STR "\"IOCalloutDevice\" ="
#define ID_VENDOR_STR "\"idVendor\" = "
#define ID_PRODUCT_STR "\"idProduct\" = "
/*-----------------------------------------------------*\
| Return variables |
\*-----------------------------------------------------*/
std::vector<std::string *> ret_vector;
std::string * tmp_string;
/*-----------------------------------------------------*\
| Execute command to list USB devices |
| |
| Top level entry lines in ioreg output start with |
| "+-o". Start the string with an extra newline so |
| that we can search for "\n+-0" to identify only hits |
| at the beginning of a line. |
\*-----------------------------------------------------*/
std::string out_string = "\n" + exec("ioreg -r -c IOUSBHostDevice -l");
/*-----------------------------------------------------*\
| Append desired VID and PID to search strings |
\*-----------------------------------------------------*/
std::string vid_string = ID_VENDOR_STR + std::to_string(vid);
std::string pid_string = ID_PRODUCT_STR + std::to_string(pid);
/*-----------------------------------------------------*\
| Start position counter at 0 |
\*-----------------------------------------------------*/
std::size_t pos = 0;
/*-----------------------------------------------------*\
| Loop through ioreg output, loop exits when "\n+-o" |
| string cannot be found. |
\*-----------------------------------------------------*/
while(1)
{
/*-------------------------------------------------*\
| Variables to store positions in string |
\*-------------------------------------------------*/
std::size_t next_pos;
std::size_t vid_pos;
std::size_t pid_pos;
/*-------------------------------------------------*\
| Search for the next 2 iterations of "\n+-o" so |
| that we can check if hits are in between them |
\*-------------------------------------------------*/
pos = out_string.find("\n+-o", pos);
next_pos = out_string.find("\n+-o", pos + 1);
/*-------------------------------------------------*\
| Search for the vendor and product ID strings in |
| and verify that they are between pos and next_pos |
\*-------------------------------------------------*/
vid_pos = out_string.find(vid_string, pos);
pid_pos = out_string.find(pid_string, pos);
/*-------------------------------------------------*\
| Verify that VID/PID matches are within this |
| device block by checking that their positions are |
| less than next_pos. If next_pos is invalid, |
| this is the last block, in which case check if |
| VID/PID positions are valid. |
\*-------------------------------------------------*/
if(((vid_pos < next_pos) && (pid_pos < next_pos)) || ((pos == std::string::npos) && (vid_pos != std::string::npos) && (pid_pos != std::string::npos)))
{
/*---------------------------------------------*\
| Variables to store positions in string |
\*---------------------------------------------*/
std::size_t dev_pos;
std::size_t start_pos;
std::size_t end_pos;
/*---------------------------------------------*\
| Look for the IO callout device tag and then |
| get the start and end positions of its value |
\*---------------------------------------------*/
dev_pos = out_string.find(IO_CALLOUT_STR, pos + 1);
start_pos = out_string.find("\"", dev_pos + sizeof(IO_CALLOUT_STR)) + 1;
end_pos = out_string.find("\"\n", start_pos);
/*---------------------------------------------*\
| Ensure the IO callout device tag is within |
| this device's section |
\*---------------------------------------------*/
if(dev_pos < next_pos)
{
tmp_string = new std::string(out_string.substr(start_pos, end_pos-start_pos));
ret_vector.push_back(tmp_string);
}
}
/*-------------------------------------------------*\
| If we've reached the end of the string, break out |
| of the loop |
\*-------------------------------------------------*/
if(pos == std::string::npos)
{
break;
}
/*-------------------------------------------------*\
| Increment position |
\*-------------------------------------------------*/
pos++;
}
/*-----------------------------------------------------*\
| Return vector of detected strings |
\*-----------------------------------------------------*/
return(ret_vector);
}
/*---------------------------------------------------------*\
| find_usb_serial_port_macos.cpp |
| |
| Finds the serial port path(s) of USB serial port devices|
| given the USB VID and PID of the device |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#include "find_usb_serial_port.h"
std::string exec(const char* cmd)
{
char buffer[128];
std::string result = "";
FILE* pipe = popen(cmd, "r");
if(!pipe)
{
throw std::runtime_error("popen() failed!");
}
try
{
while(fgets(buffer, sizeof(buffer), pipe) != NULL)
{
result += buffer;
}
}
catch(...)
{
pclose(pipe);
throw;
}
pclose(pipe);
return result;
}
/*---------------------------------------------------------------------*\
| |
| find_usb_serial_port |
| |
| This function returns the name of the first USB serial port matching|
| the given USB product and vendor ID. |
| |
| vid: Vendor ID code |
| pid: Product ID code |
| |
| returns: std::string containing port name "COMx" or "/dev/ttyX" |
| |
\*---------------------------------------------------------------------*/
std::vector<std::string *> find_usb_serial_port(unsigned short vid, unsigned short pid)
{
/*-----------------------------------------------------*\
| Strings to search for in ioreg output |
\*-----------------------------------------------------*/
#define IO_CALLOUT_STR "\"IOCalloutDevice\" ="
#define ID_VENDOR_STR "\"idVendor\" = "
#define ID_PRODUCT_STR "\"idProduct\" = "
/*-----------------------------------------------------*\
| Return variables |
\*-----------------------------------------------------*/
std::vector<std::string *> ret_vector;
std::string * tmp_string;
/*-----------------------------------------------------*\
| Execute command to list USB devices |
| |
| Top level entry lines in ioreg output start with |
| "+-o". Start the string with an extra newline so |
| that we can search for "\n+-0" to identify only hits |
| at the beginning of a line. |
\*-----------------------------------------------------*/
std::string out_string = "\n" + exec("ioreg -r -c IOUSBHostDevice -l");
/*-----------------------------------------------------*\
| Append desired VID and PID to search strings |
\*-----------------------------------------------------*/
std::string vid_string = ID_VENDOR_STR + std::to_string(vid);
std::string pid_string = ID_PRODUCT_STR + std::to_string(pid);
/*-----------------------------------------------------*\
| Start position counter at 0 |
\*-----------------------------------------------------*/
std::size_t pos = 0;
/*-----------------------------------------------------*\
| Loop through ioreg output, loop exits when "\n+-o" |
| string cannot be found. |
\*-----------------------------------------------------*/
while(1)
{
/*-------------------------------------------------*\
| Variables to store positions in string |
\*-------------------------------------------------*/
std::size_t next_pos;
std::size_t vid_pos;
std::size_t pid_pos;
/*-------------------------------------------------*\
| Search for the next 2 iterations of "\n+-o" so |
| that we can check if hits are in between them |
\*-------------------------------------------------*/
pos = out_string.find("\n+-o", pos);
next_pos = out_string.find("\n+-o", pos + 1);
/*-------------------------------------------------*\
| Search for the vendor and product ID strings in |
| and verify that they are between pos and next_pos |
\*-------------------------------------------------*/
vid_pos = out_string.find(vid_string, pos);
pid_pos = out_string.find(pid_string, pos);
/*-------------------------------------------------*\
| Verify that VID/PID matches are within this |
| device block by checking that their positions are |
| less than next_pos. If next_pos is invalid, |
| this is the last block, in which case check if |
| VID/PID positions are valid. |
\*-------------------------------------------------*/
if(((vid_pos < next_pos) && (pid_pos < next_pos)) || ((pos == std::string::npos) && (vid_pos != std::string::npos) && (pid_pos != std::string::npos)))
{
/*---------------------------------------------*\
| Variables to store positions in string |
\*---------------------------------------------*/
std::size_t dev_pos;
std::size_t start_pos;
std::size_t end_pos;
/*---------------------------------------------*\
| Look for the IO callout device tag and then |
| get the start and end positions of its value |
\*---------------------------------------------*/
dev_pos = out_string.find(IO_CALLOUT_STR, pos + 1);
start_pos = out_string.find("\"", dev_pos + sizeof(IO_CALLOUT_STR)) + 1;
end_pos = out_string.find("\"\n", start_pos);
/*---------------------------------------------*\
| Ensure the IO callout device tag is within |
| this device's section |
\*---------------------------------------------*/
if(dev_pos < next_pos)
{
tmp_string = new std::string(out_string.substr(start_pos, end_pos-start_pos));
ret_vector.push_back(tmp_string);
}
}
/*-------------------------------------------------*\
| If we've reached the end of the string, break out |
| of the loop |
\*-------------------------------------------------*/
if(pos == std::string::npos)
{
break;
}
/*-------------------------------------------------*\
| Increment position |
\*-------------------------------------------------*/
pos++;
}
/*-----------------------------------------------------*\
| Return vector of detected strings |
\*-----------------------------------------------------*/
return(ret_vector);
}
+135 -135
View File
@@ -1,135 +1,135 @@
/*---------------------------------------------------------*\
| find_usb_serial_port_win.cpp |
| |
| Finds the serial port path(s) of USB serial port devices|
| given the USB VID and PID of the device |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#include "find_usb_serial_port.h"
#include <initguid.h>
#include <windows.h>
#include <Setupapi.h>
//Buffer length
#define BUFF_LEN 20
#pragma comment (lib, "Setupapi.lib")
#pragma comment(lib, "advapi32")
/*---------------------------------------------------------------------*\
| |
| find_usb_serial_port |
| |
| This function returns the name of the first USB serial port matching|
| the given USB product and vendor ID. |
| |
| vid: Vendor ID code |
| pid: Product ID code |
| |
| returns: std::string containing port name "COMx" or "/dev/ttyX" |
| |
\*---------------------------------------------------------------------*/
std::vector<std::string *> find_usb_serial_port(unsigned short vid, unsigned short pid)
{
std::vector<std::string *> ret_vector;
std::string * tmp_string;
HDEVINFO DeviceInfoSet;
DWORD DeviceIndex = 0;
SP_DEVINFO_DATA DeviceInfoData;
const char * DevEnum = "USB";
char ExpectedDeviceId[80] = {0}; //Store hardware id
char vid_pid[10] = {0}; //Store VID/PID
char szBuffer[1024] = {0};
DEVPROPTYPE ulPropertyType;
DWORD dwSize = 0;
/*-----------------------------------------------------------------*\
| Create device hardware id |
| "vid_ABCD&pid_CDEF" |
\*-----------------------------------------------------------------*/
strcpy(ExpectedDeviceId, "USB\\VID_");
snprintf(vid_pid, 10, "%04X", vid);
strcat(ExpectedDeviceId, vid_pid);
strcat(ExpectedDeviceId, "&PID_");
snprintf(vid_pid, 10, "%04X", pid);
strcat(ExpectedDeviceId, vid_pid);
/*-----------------------------------------------------------------*\
| SetupDiGetClassDevs returns a handle to a device information set |
\*-----------------------------------------------------------------*/
DeviceInfoSet = SetupDiGetClassDevs( NULL, DevEnum, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
if (DeviceInfoSet == INVALID_HANDLE_VALUE)
{
return ret_vector;
}
/*-----------------------------------------------------------------*\
| Set up Device Info Data |
\*-----------------------------------------------------------------*/
memset(&DeviceInfoData, 0, sizeof(SP_DEVINFO_DATA));
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
/*-----------------------------------------------------------------*\
| Receive information about an enumerated device |
\*-----------------------------------------------------------------*/
while (SetupDiEnumDeviceInfo( DeviceInfoSet, DeviceIndex, &DeviceInfoData))
{
DeviceIndex++;
/*-------------------------------------------------------------*\
| Retrieves a specified Plug and Play device property |
\*-------------------------------------------------------------*/
if (SetupDiGetDeviceRegistryProperty (DeviceInfoSet, &DeviceInfoData, SPDRP_HARDWAREID, &ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize))
{
HKEY hDeviceRegistryKey;
/*-----------------------------------------------------*\
| Check if the string for this device property matches |
| our expected device string |
\*-----------------------------------------------------*/
if(strncmp(ExpectedDeviceId, szBuffer, 21) == 0)
{
hDeviceRegistryKey = SetupDiOpenDevRegKey(DeviceInfoSet, &DeviceInfoData,DICS_FLAG_GLOBAL, 0,DIREG_DEV, KEY_READ);
if (hDeviceRegistryKey == INVALID_HANDLE_VALUE)
{
break;
}
else
{
char pszPortName[BUFF_LEN];
DWORD dwSize = sizeof(pszPortName);
DWORD dwType = 0;
/*-----------------------------------------------------*\
| Read in the name of the port |
\*-----------------------------------------------------*/
if( (RegQueryValueEx(hDeviceRegistryKey,"PortName", NULL, &dwType, (LPBYTE) pszPortName, &dwSize) == ERROR_SUCCESS) && (dwType == REG_SZ))
{
if(strncmp(pszPortName, "COM", 3) == 0)
{
tmp_string = new std::string(pszPortName);
ret_vector.push_back(tmp_string);
}
}
// Close the key now that we are finished with it
RegCloseKey(hDeviceRegistryKey);
}
}
}
}
if (DeviceInfoSet)
{
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
}
return ret_vector;
} /* find_usb_serial_port() */
/*---------------------------------------------------------*\
| find_usb_serial_port_win.cpp |
| |
| Finds the serial port path(s) of USB serial port devices|
| given the USB VID and PID of the device |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#include "find_usb_serial_port.h"
#include <initguid.h>
#include <windows.h>
#include <Setupapi.h>
//Buffer length
#define BUFF_LEN 20
#pragma comment (lib, "Setupapi.lib")
#pragma comment(lib, "advapi32")
/*---------------------------------------------------------------------*\
| |
| find_usb_serial_port |
| |
| This function returns the name of the first USB serial port matching|
| the given USB product and vendor ID. |
| |
| vid: Vendor ID code |
| pid: Product ID code |
| |
| returns: std::string containing port name "COMx" or "/dev/ttyX" |
| |
\*---------------------------------------------------------------------*/
std::vector<std::string *> find_usb_serial_port(unsigned short vid, unsigned short pid)
{
std::vector<std::string *> ret_vector;
std::string * tmp_string;
HDEVINFO DeviceInfoSet;
DWORD DeviceIndex = 0;
SP_DEVINFO_DATA DeviceInfoData;
const char * DevEnum = "USB";
char ExpectedDeviceId[80] = {0}; //Store hardware id
char vid_pid[10] = {0}; //Store VID/PID
char szBuffer[1024] = {0};
DEVPROPTYPE ulPropertyType;
DWORD dwSize = 0;
/*-----------------------------------------------------------------*\
| Create device hardware id |
| "vid_ABCD&pid_CDEF" |
\*-----------------------------------------------------------------*/
strcpy(ExpectedDeviceId, "USB\\VID_");
snprintf(vid_pid, 10, "%04X", vid);
strcat(ExpectedDeviceId, vid_pid);
strcat(ExpectedDeviceId, "&PID_");
snprintf(vid_pid, 10, "%04X", pid);
strcat(ExpectedDeviceId, vid_pid);
/*-----------------------------------------------------------------*\
| SetupDiGetClassDevs returns a handle to a device information set |
\*-----------------------------------------------------------------*/
DeviceInfoSet = SetupDiGetClassDevs( NULL, DevEnum, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
if (DeviceInfoSet == INVALID_HANDLE_VALUE)
{
return ret_vector;
}
/*-----------------------------------------------------------------*\
| Set up Device Info Data |
\*-----------------------------------------------------------------*/
memset(&DeviceInfoData, 0, sizeof(SP_DEVINFO_DATA));
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
/*-----------------------------------------------------------------*\
| Receive information about an enumerated device |
\*-----------------------------------------------------------------*/
while (SetupDiEnumDeviceInfo( DeviceInfoSet, DeviceIndex, &DeviceInfoData))
{
DeviceIndex++;
/*-------------------------------------------------------------*\
| Retrieves a specified Plug and Play device property |
\*-------------------------------------------------------------*/
if (SetupDiGetDeviceRegistryProperty (DeviceInfoSet, &DeviceInfoData, SPDRP_HARDWAREID, &ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), &dwSize))
{
HKEY hDeviceRegistryKey;
/*-----------------------------------------------------*\
| Check if the string for this device property matches |
| our expected device string |
\*-----------------------------------------------------*/
if(strncmp(ExpectedDeviceId, szBuffer, 21) == 0)
{
hDeviceRegistryKey = SetupDiOpenDevRegKey(DeviceInfoSet, &DeviceInfoData,DICS_FLAG_GLOBAL, 0,DIREG_DEV, KEY_READ);
if (hDeviceRegistryKey == INVALID_HANDLE_VALUE)
{
break;
}
else
{
char pszPortName[BUFF_LEN];
DWORD dwSize = sizeof(pszPortName);
DWORD dwType = 0;
/*-----------------------------------------------------*\
| Read in the name of the port |
\*-----------------------------------------------------*/
if( (RegQueryValueEx(hDeviceRegistryKey,"PortName", NULL, &dwType, (LPBYTE) pszPortName, &dwSize) == ERROR_SUCCESS) && (dwType == REG_SZ))
{
if(strncmp(pszPortName, "COM", 3) == 0)
{
tmp_string = new std::string(pszPortName);
ret_vector.push_back(tmp_string);
}
}
// Close the key now that we are finished with it
RegCloseKey(hDeviceRegistryKey);
}
}
}
}
if (DeviceInfoSet)
{
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
}
return ret_vector;
} /* find_usb_serial_port() */
File diff suppressed because it is too large. Load diff
+159 -159
View File
@@ -1,159 +1,159 @@
/*---------------------------------------------------------*\
| serial_port.h |
| |
| Cross Platform Serial COM Library for Windows and Linux |
| This library provides access to serial ports with a |
| common API for both Windows and Linux systems |
| |
| Adam Honse (calcprogrammer1@gmail.com) 21 Jan 2013 |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#ifndef SERIAL_PORT_H
#define SERIAL_PORT_H
#include <string.h>
#include <stdio.h>
#include <vector>
#include <string>
#ifdef _WIN32
/*---------------------------------------------------------*\
| Windows interferes with std::max unless NOMINMAX defined |
\*---------------------------------------------------------*/
#define NOMINMAX
#include <windows.h>
#endif /* _WIN32 */
#ifdef __linux__
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/file.h>
#include <sys/ioctl.h>
//these types are redefined in asm/termios.h
//to prevent compiler errors from multply
//defining them, use a #define to rename them -
//essentially to undef them before they are redefined
#define winsize undefine_winsize
#define termio undefine_termio
#define termios undefine_termios
#define sgttyb undefine_sgttyb
#define tchars undefine_tchars
#define ltchars undefine_ltchars
#include <asm/termios.h>
#include <asm/ioctls.h>
//ppc has c_ispeed/c_ospeed in termios and termios2 doesn't exist
#if defined(__powerpc__)
#define termios2 termios
#define TCGETS2 TCGETS
#define TCSETS2 TCSETS
#endif
#endif /* __linux__ */
#ifdef __APPLE__
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <IOKit/serial/ioss.h>
#endif /* __APPLE__ */
/*-------------------------------------------------------------------------*\
| Serial Port Enums |
\*-------------------------------------------------------------------------*/
typedef unsigned int serial_port_parity;
enum
{
SERIAL_PORT_PARITY_NONE = 0, /* No parity */
SERIAL_PORT_PARITY_ODD = 1, /* Odd parity */
SERIAL_PORT_PARITY_EVEN = 2, /* Even parity */
};
typedef unsigned int serial_port_size;
enum
{
SERIAL_PORT_SIZE_8 = 0, /* 8 bits per byte */
SERIAL_PORT_SIZE_7 = 1, /* 7 bits per byte */
SERIAL_PORT_SIZE_6 = 2, /* 6 bits per byte */
SERIAL_PORT_SIZE_5 = 3, /* 5 bits per byte */
};
typedef unsigned int serial_port_stop_bits;
enum
{
SERIAL_PORT_STOP_BITS_1 = 0, /* 1 stop bit */
SERIAL_PORT_STOP_BITS_2 = 1, /* 2 stop bits */
};
/*-------------------------------------------------------------------------*\
| Serial Port Class |
| The reason for this class is that serial ports are treated differently |
| on Windows and Linux. By creating a class, those differences can be |
| made invisible to the program and make cross-platform usage easy |
\*-------------------------------------------------------------------------*/
class serial_port
{
public:
static std::vector<std::string> getSerialPorts();
serial_port();
serial_port(const char * name, unsigned int baud);
serial_port(const char * name,
unsigned int baud,
serial_port_parity parity,
serial_port_size size,
serial_port_stop_bits stop_bits,
bool flow_control);
~serial_port();
bool serial_open();
bool serial_open(const char* name);
bool serial_open(const char* name, unsigned int baud);
void serial_close();
void serial_set_baud(unsigned int baud);
int serial_get_baud();
int serial_read(char * buffer, int length);
int serial_write(char * buffer, int length);
void serial_flush_rx();
void serial_flush_tx();
void serial_break();
void serial_set_dtr(bool dtr);
void serial_set_rts(bool rts);
int serial_available();
private:
char port_name[1024];
unsigned int baud_rate;
serial_port_parity parity;
serial_port_size size;
serial_port_stop_bits stop_bits;
bool flow_control;
#ifdef _WIN32
HANDLE file_descriptor;
DCB dcb;
#else
int file_descriptor;
#endif
};
#endif
/*---------------------------------------------------------*\
| serial_port.h |
| |
| Cross Platform Serial COM Library for Windows and Linux |
| This library provides access to serial ports with a |
| common API for both Windows and Linux systems |
| |
| Adam Honse (calcprogrammer1@gmail.com) 21 Jan 2013 |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#ifndef SERIAL_PORT_H
#define SERIAL_PORT_H
#include <string.h>
#include <stdio.h>
#include <vector>
#include <string>
#ifdef _WIN32
/*---------------------------------------------------------*\
| Windows interferes with std::max unless NOMINMAX defined |
\*---------------------------------------------------------*/
#define NOMINMAX
#include <windows.h>
#endif /* _WIN32 */
#ifdef __linux__
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/file.h>
#include <sys/ioctl.h>
//these types are redefined in asm/termios.h
//to prevent compiler errors from multply
//defining them, use a #define to rename them -
//essentially to undef them before they are redefined
#define winsize undefine_winsize
#define termio undefine_termio
#define termios undefine_termios
#define sgttyb undefine_sgttyb
#define tchars undefine_tchars
#define ltchars undefine_ltchars
#include <asm/termios.h>
#include <asm/ioctls.h>
//ppc has c_ispeed/c_ospeed in termios and termios2 doesn't exist
#if defined(__powerpc__)
#define termios2 termios
#define TCGETS2 TCGETS
#define TCSETS2 TCSETS
#endif
#endif /* __linux__ */
#ifdef __APPLE__
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <IOKit/serial/ioss.h>
#endif /* __APPLE__ */
/*-------------------------------------------------------------------------*\
| Serial Port Enums |
\*-------------------------------------------------------------------------*/
typedef unsigned int serial_port_parity;
enum
{
SERIAL_PORT_PARITY_NONE = 0, /* No parity */
SERIAL_PORT_PARITY_ODD = 1, /* Odd parity */
SERIAL_PORT_PARITY_EVEN = 2, /* Even parity */
};
typedef unsigned int serial_port_size;
enum
{
SERIAL_PORT_SIZE_8 = 0, /* 8 bits per byte */
SERIAL_PORT_SIZE_7 = 1, /* 7 bits per byte */
SERIAL_PORT_SIZE_6 = 2, /* 6 bits per byte */
SERIAL_PORT_SIZE_5 = 3, /* 5 bits per byte */
};
typedef unsigned int serial_port_stop_bits;
enum
{
SERIAL_PORT_STOP_BITS_1 = 0, /* 1 stop bit */
SERIAL_PORT_STOP_BITS_2 = 1, /* 2 stop bits */
};
/*-------------------------------------------------------------------------*\
| Serial Port Class |
| The reason for this class is that serial ports are treated differently |
| on Windows and Linux. By creating a class, those differences can be |
| made invisible to the program and make cross-platform usage easy |
\*-------------------------------------------------------------------------*/
class serial_port
{
public:
static std::vector<std::string> getSerialPorts();
serial_port();
serial_port(const char * name, unsigned int baud);
serial_port(const char * name,
unsigned int baud,
serial_port_parity parity,
serial_port_size size,
serial_port_stop_bits stop_bits,
bool flow_control);
~serial_port();
bool serial_open();
bool serial_open(const char* name);
bool serial_open(const char* name, unsigned int baud);
void serial_close();
void serial_set_baud(unsigned int baud);
int serial_get_baud();
int serial_read(char * buffer, int length);
int serial_write(char * buffer, int length);
void serial_flush_rx();
void serial_flush_tx();
void serial_break();
void serial_set_dtr(bool dtr);
void serial_set_rts(bool rts);
int serial_available();
private:
char port_name[1024];
unsigned int baud_rate;
serial_port_parity parity;
serial_port_size size;
serial_port_stop_bits stop_bits;
bool flow_control;
#ifdef _WIN32
HANDLE file_descriptor;
DCB dcb;
#else
int file_descriptor;
#endif
};
#endif