From 1457831840abc9e6d5f734436c66e0fcaa7c5dad Mon Sep 17 00:00:00 2001 From: B Horn Date: Fri, 31 Jul 2020 09:46:36 +0100 Subject: [PATCH] Modifying find_usb_serial_port() to return a vector --- .../HuePlusControllerDetect.cpp | 19 +++++++----- serial_port/find_usb_serial_port.h | 3 +- serial_port/find_usb_serial_port_linux.cpp | 23 +++++++------- serial_port/find_usb_serial_port_win.cpp | 31 ++++++++++--------- 4 files changed, 42 insertions(+), 34 deletions(-) diff --git a/Controllers/HuePlusController/HuePlusControllerDetect.cpp b/Controllers/HuePlusController/HuePlusControllerDetect.cpp index 67f92c11..54cc80f7 100644 --- a/Controllers/HuePlusController/HuePlusControllerDetect.cpp +++ b/Controllers/HuePlusController/HuePlusControllerDetect.cpp @@ -17,16 +17,21 @@ void DetectHuePlusControllers(std::vector &rgb_controllers) { + size_t i; HuePlusController* new_hueplus; RGBController_HuePlus* new_controller; - std::string portname = find_usb_serial_port(NZXT_HUE_PLUS_VID, NZXT_HUE_PLUS_PID); - if( portname != "" ) - { - new_hueplus = new HuePlusController(); - new_hueplus->Initialize((char *)portname.c_str()); + std::vector ports = find_usb_serial_port(NZXT_HUE_PLUS_VID, NZXT_HUE_PLUS_PID); - new_controller = new RGBController_HuePlus(new_hueplus); - rgb_controllers.push_back(new_controller); + for (i = 0; i < ports.size(); i++) + { + if( *ports[i] != "" ) + { + new_hueplus = new HuePlusController(); + new_hueplus->Initialize((char *)ports[i]->c_str()); + + new_controller = new RGBController_HuePlus(new_hueplus); + rgb_controllers.push_back(new_controller); + } } } /* DetectHuePlusControllers() */ diff --git a/serial_port/find_usb_serial_port.h b/serial_port/find_usb_serial_port.h index 7d651ca3..888f451d 100644 --- a/serial_port/find_usb_serial_port.h +++ b/serial_port/find_usb_serial_port.h @@ -6,5 +6,6 @@ #include #include #include +#include -std::string find_usb_serial_port(unsigned short vid, unsigned short pid); \ No newline at end of file +std::vector find_usb_serial_port(unsigned short vid, unsigned short pid); diff --git a/serial_port/find_usb_serial_port_linux.cpp b/serial_port/find_usb_serial_port_linux.cpp index 15ad3180..79425b4a 100644 --- a/serial_port/find_usb_serial_port_linux.cpp +++ b/serial_port/find_usb_serial_port_linux.cpp @@ -17,13 +17,14 @@ | | \*---------------------------------------------------------------------*/ -std::string find_usb_serial_port(unsigned short vid, unsigned short pid) +std::vector find_usb_serial_port(unsigned short vid, unsigned short pid) { - std::string ret_string = ""; - DIR* dir; - char symlink_path[1024] = {0}; - struct dirent* ent; - char vid_pid[10] = {0}; //Store VID/PID + std::vector 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 | @@ -32,7 +33,7 @@ std::string find_usb_serial_port(unsigned short vid, unsigned short pid) if(dir == NULL) { - return ret_string; + return ret_vector; } /*-----------------------------------------------------------------*\ @@ -109,10 +110,10 @@ std::string find_usb_serial_port(unsigned short vid, unsigned short pid) break; } } - ret_string.append("/dev/"); - ret_string.append(port_string); + tmp_string = new std::string("/dev/"); + tmp_string->append(port_string); - return ret_string; + ret_vector.push_back(tmp_string); } } } @@ -121,6 +122,6 @@ std::string find_usb_serial_port(unsigned short vid, unsigned short pid) ent = readdir(dir); } - return ret_string; + return ret_vector; } /* find_usb_serial_port() */ diff --git a/serial_port/find_usb_serial_port_win.cpp b/serial_port/find_usb_serial_port_win.cpp index f43aa2bb..0c6e4e01 100644 --- a/serial_port/find_usb_serial_port_win.cpp +++ b/serial_port/find_usb_serial_port_win.cpp @@ -24,18 +24,19 @@ | | \*---------------------------------------------------------------------*/ -std::string find_usb_serial_port(unsigned short vid, unsigned short pid) +std::vector find_usb_serial_port(unsigned short vid, unsigned short pid) { - std::string ret_str = ""; - 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; + std::vector 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 | @@ -55,7 +56,7 @@ std::string find_usb_serial_port(unsigned short vid, unsigned short pid) if (DeviceInfoSet == INVALID_HANDLE_VALUE) { - return false; + return ret_vector; } /*-----------------------------------------------------------------*\ @@ -102,8 +103,8 @@ std::string find_usb_serial_port(unsigned short vid, unsigned short pid) { if(strncmp(pszPortName, "COM", 3) == 0) { - ret_str.append(pszPortName); - return ret_str; + tmp_string = new std::string(pszPortName); + ret_vector.push_back(tmp_string); } } @@ -119,6 +120,6 @@ std::string find_usb_serial_port(unsigned short vid, unsigned short pid) SetupDiDestroyDeviceInfoList(DeviceInfoSet); } - return ret_str; + return ret_vector; } /* find_usb_serial_port() */