From 2e199593e90c33b7432032e553ede41db3f0bf52 Mon Sep 17 00:00:00 2001 From: Chris M Date: Fri, 18 Oct 2024 07:00:43 +1100 Subject: [PATCH] Adding product name to DMI data class --- dmiinfo/dmiinfo.cpp | 54 +++++++++++++++++++++++++++------------------ dmiinfo/dmiinfo.h | 11 ++++++++- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/dmiinfo/dmiinfo.cpp b/dmiinfo/dmiinfo.cpp index 6062a013..da8ea97e 100644 --- a/dmiinfo/dmiinfo.cpp +++ b/dmiinfo/dmiinfo.cpp @@ -26,27 +26,32 @@ \******************************************************************************************/ DMIInfo::DMIInfo() { - mainboard = ""; - manufacturer = ""; + mainboard = readWMIQuery("SELECT * FROM Win32_BaseBoard", "Product"); + manufacturer = readWMIQuery("SELECT * FROM Win32_BaseBoard", "Manufacturer"); + product_name = readWMIQuery("SELECT * FROM Win32_ComputerSystem", "Model"); +} + +std::string DMIInfo::readWMIQuery(std::string query, std::string key) +{ HRESULT hres; Wmi wmi; // Query WMI for Win32_PnPSignedDriver entries with names matching "SMBUS" or "SM BUS" // These devices may be browsed under Device Manager -> System Devices - std::vector q_res_BaseBoard; - hres = wmi.query("SELECT * FROM Win32_BaseBoard", q_res_BaseBoard); + std::vector q_result; + hres = wmi.query(query, q_result); - if (hres) + if(hres) { LOG_DEBUG("[DMI Info] Unable to read from %s", WMI); - return; + return ""; } - for (QueryObj &i : q_res_BaseBoard) + for(QueryObj &obj : q_result) { - manufacturer = i["Manufacturer"].c_str(); - mainboard = i["Product"].c_str(); + return obj[key]; } + return ""; } #else /* WIN32 */ @@ -62,22 +67,24 @@ DMIInfo::DMIInfo() \******************************************************************************************/ DMIInfo::DMIInfo() { - mainboard = ""; - manufacturer = ""; + mainboard = readFilePath(SYSFS_MB_DMI "/board_vendor"); + manufacturer = readFilePath(SYSFS_MB_DMI "/board_name"); + product_name = readFilePath(SYSFS_PC_DMI "/product_name"); +} - if ((access(SYSFSDMI "/board_vendor", R_OK)!=0) && (access(SYSFSDMI "/board_name", R_OK)!=0)) +std::string DMIInfo::readFilePath(std::string path) +{ + if(access(path.c_str(), R_OK)!=0) { - LOG_DEBUG("[DMI Info] Unable to read from %s", SYSFSDMI); - return; + LOG_DEBUG("[DMI Info] Unable to read from %s", path); + return ""; } + std::string read_path; - std::ifstream mftr(SYSFSDMI "/board_vendor", std::ifstream::in); - getline(mftr, manufacturer); - mftr.close(); - - std::ifstream prdt(SYSFSDMI "/board_name", std::ifstream::in); - getline(prdt, mainboard); - prdt.close(); + std::ifstream path_stream(path, std::ifstream::in); + getline(path_stream, read_path); + path_stream.close(); + return read_path; } #endif /* WIN32 */ @@ -95,3 +102,8 @@ std::string DMIInfo::getManufacturer() { return manufacturer; } + +std::string DMIInfo::getProductName() +{ + return product_name; +} diff --git a/dmiinfo/dmiinfo.h b/dmiinfo/dmiinfo.h index 845c0459..5e0d5d37 100644 --- a/dmiinfo/dmiinfo.h +++ b/dmiinfo/dmiinfo.h @@ -21,7 +21,8 @@ #include //Linux specific filesystem operation #include -#define SYSFSDMI "/sys/devices/virtual/dmi/id/" //Linux specific file path +#define SYSFS_MB_DMI "/sys/devices/virtual/dmi/id/" //Linux file path for Motherboard +#define SYSFS_PC_DMI "/sys/class/dmi/id/" //Linux file path for Product info #endif class DMIInfo @@ -30,9 +31,17 @@ public: DMIInfo(); ~DMIInfo(); +#ifdef WIN32 + std::string readWMIQuery(std::string query, std::string key); +#else + std::string readFilePath(std::string path); +#endif + std::string getMainboard(); std::string getManufacturer(); + std::string getProductName(); private: std::string mainboard; std::string manufacturer; + std::string product_name; };