From 3235e172d1a9b7666adb16cd09b9a1821fd4d662 Mon Sep 17 00:00:00 2001 From: tibalt Date: Tue, 23 Nov 2021 17:17:24 +0800 Subject: [PATCH] add cpu model strategy Signed-off-by: tibalt --- src/library/hw_identifier/CMakeLists.txt | 1 + src/library/hw_identifier/cpu_strategy.cpp | 52 +++++++++++++++++++ src/library/hw_identifier/cpu_strategy.hpp | 27 ++++++++++ .../hw_identifier/identification_strategy.cpp | 4 ++ src/library/os/linux/os_linux.cpp | 8 +++ src/library/os/os.h | 1 + src/library/os/windows/os_win.cpp | 6 +++ src/templates/licensecc_properties.h.in | 3 +- test/functional/hw_identifier_it_test.cpp | 6 +++ 9 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/library/hw_identifier/cpu_strategy.cpp create mode 100644 src/library/hw_identifier/cpu_strategy.hpp diff --git a/src/library/hw_identifier/CMakeLists.txt b/src/library/hw_identifier/CMakeLists.txt index 09ba5e05..68778f10 100644 --- a/src/library/hw_identifier/CMakeLists.txt +++ b/src/library/hw_identifier/CMakeLists.txt @@ -3,6 +3,7 @@ add_library(hw_identifier OBJECT default_strategy.cpp ethernet.cpp disk_strategy.cpp + cpu_strategy.cpp identification_strategy.cpp hw_identifier.cpp ) diff --git a/src/library/hw_identifier/cpu_strategy.cpp b/src/library/hw_identifier/cpu_strategy.cpp new file mode 100644 index 00000000..41674920 --- /dev/null +++ b/src/library/hw_identifier/cpu_strategy.cpp @@ -0,0 +1,52 @@ +/* + * cpu_strategy.cpp + * + * Created on: Nov 23, 2021 + * Author: Tibalt + */ +#include +#include "../os/os.h" +#include "cpu_strategy.hpp" + +using namespace std; +namespace license { +namespace hw_identifier { + +static array generate_id_by_model(const uint32_t &cpu_info) { + array a_disk_id = {}; + memcpy(&a_disk_id[0], &cpu_info, sizeof(cpu_info)); + return a_disk_id; +} + + +static FUNCTION_RETURN generate_cpu_pc_id(array &cpu_id) { + uint32_t model; + FUNCTION_RETURN result_diskinfos = getCPUModel(model); + if (result_diskinfos != FUNC_RET_OK) { + return result_diskinfos; + } + cpu_id = generate_id_by_model(model); + return FUNC_RET_OK ; +} + +CPUStrategy::~CPUStrategy() {} + +LCC_API_HW_IDENTIFICATION_STRATEGY CPUStrategy::identification_strategy() const { + return LCC_API_HW_IDENTIFICATION_STRATEGY::STRATEGY_CPU_MODEL; +} + +std::vector CPUStrategy::alternative_ids() const { + array data; + FUNCTION_RETURN result = generate_cpu_pc_id(data); + vector identifiers; + if (result == FUNC_RET_OK) { + HwIdentifier pc_id; + pc_id.set_identification_strategy(identification_strategy()); + pc_id.set_data(data); + identifiers.push_back(pc_id); + } + return identifiers; +} + +} // namespace hw_identifier +} /* namespace license */ diff --git a/src/library/hw_identifier/cpu_strategy.hpp b/src/library/hw_identifier/cpu_strategy.hpp new file mode 100644 index 00000000..4ac285b9 --- /dev/null +++ b/src/library/hw_identifier/cpu_strategy.hpp @@ -0,0 +1,27 @@ +/* + * cpu_strategy.hpp + * + * Created on: Nov 23, 2021 + * Author: Tibalt + */ + +#ifndef SRC_LIBRARY_PC_IDENTIFIER_CPU_STRATEGY_HPP_ +#define SRC_LIBRARY_PC_IDENTIFIER_CPU_STRATEGY_HPP_ + +#include "identification_strategy.hpp" + +namespace license { +namespace hw_identifier { + +class CPUStrategy : public IdentificationStrategy { +public: + inline CPUStrategy(){}; + virtual ~CPUStrategy(); + virtual LCC_API_HW_IDENTIFICATION_STRATEGY identification_strategy() const; + virtual std::vector alternative_ids() const; +}; + +} // namespace hw_identifier +} /* namespace license */ + +#endif /* SRC_LIBRARY_PC_IDENTIFIER_CPU_STRATEGY_HPP_*/ diff --git a/src/library/hw_identifier/identification_strategy.cpp b/src/library/hw_identifier/identification_strategy.cpp index 38233d03..05c56fb2 100644 --- a/src/library/hw_identifier/identification_strategy.cpp +++ b/src/library/hw_identifier/identification_strategy.cpp @@ -3,6 +3,7 @@ #include "default_strategy.hpp" #include "ethernet.hpp" #include "disk_strategy.hpp" +#include "cpu_strategy.hpp" namespace license { namespace hw_identifier { @@ -47,6 +48,9 @@ std::unique_ptr IdentificationStrategy::get_strategy(LCC case STRATEGY_DISK: result = unique_ptr(dynamic_cast(new DiskStrategy())); break; + case STRATEGY_CPU_MODEL: + result = unique_ptr(dynamic_cast(new CPUStrategy())); + break; default: throw logic_error("strategy not supported"); } diff --git a/src/library/os/linux/os_linux.cpp b/src/library/os/linux/os_linux.cpp index bbf43414..35995a33 100644 --- a/src/library/os/linux/os_linux.cpp +++ b/src/library/os/linux/os_linux.cpp @@ -20,6 +20,7 @@ #ifndef NDEBUG #include #endif +#include "../cpu_info.hpp" //#ifdef USE_DISK_MODEL ///#define PARSE_ID_FUNC parse_disk_id @@ -312,6 +313,13 @@ static void set_preferred_disks(std::vector &diskInfos, std::unordered return; } + +FUNCTION_RETURN getCPUModel(uint32_t &cpu_model){ + license::os::CpuInfo cpu; + cpu_model = cpu.model(); + return FUNCTION_RETURN::FUNC_RET_OK; +} + /** * First try to read disk_infos from /dev/disk/by-uuid folder, if fails try to use * blkid cache to see what's in there, then try to exclude removable disks diff --git a/src/library/os/os.h b/src/library/os/os.h index 36a1c699..a950ca8f 100644 --- a/src/library/os/os.h +++ b/src/library/os/os.h @@ -34,6 +34,7 @@ typedef struct { } DiskInfo; FUNCTION_RETURN getDiskInfos(std::vector& diskInfos); +FUNCTION_RETURN getCPUModel(uint32_t &cpu_model); FUNCTION_RETURN getUserHomePath(char[MAX_PATH]); FUNCTION_RETURN getModuleName(char buffer[MAX_PATH]); FUNCTION_RETURN getMachineName(unsigned char identifier[6]); diff --git a/src/library/os/windows/os_win.cpp b/src/library/os/windows/os_win.cpp index 06166bdc..87960912 100644 --- a/src/library/os/windows/os_win.cpp +++ b/src/library/os/windows/os_win.cpp @@ -8,6 +8,7 @@ #include "../../base/string_utils.h" #include "../../base/logger.h" #include "../os.h" +#include "../cpu_info.hpp" using namespace std; FUNCTION_RETURN getMachineName(unsigned char identifier[6]) { @@ -89,3 +90,8 @@ FUNCTION_RETURN getModuleName(char buffer[MAX_PATH]) { } return result; } +FUNCTION_RETURN getCPUModel(uint32_t &cpu_model){ + license::os::CpuInfo cpu; + cpu_model = cpu.model(); + return FUNCTION_RETURN::FUNC_RET_OK; +} diff --git a/src/templates/licensecc_properties.h.in b/src/templates/licensecc_properties.h.in index 3bce633a..74e45894 100644 --- a/src/templates/licensecc_properties.h.in +++ b/src/templates/licensecc_properties.h.in @@ -103,10 +103,11 @@ enum LCC_API_HW_IDENTIFICATION_STRATEGY { * Not yet implemented */ STRATEGY_CPU_SIZE = 3, + STRATEGY_CPU_MODEL = 4, /** * Not yet implemented */ - STRATEGY_HOST_NAME = 4, + STRATEGY_HOST_NAME = 5, STRATEGY_NONE = -2 }; diff --git a/test/functional/hw_identifier_it_test.cpp b/test/functional/hw_identifier_it_test.cpp index ae92cb54..4d653ba8 100644 --- a/test/functional/hw_identifier_it_test.cpp +++ b/test/functional/hw_identifier_it_test.cpp @@ -58,6 +58,12 @@ BOOST_AUTO_TEST_CASE(volid_lic_file) { } } +BOOST_AUTO_TEST_CASE(cpu_lic_file) { + HwIdentifier identifier_out; + generate_and_verify_license(LCC_API_HW_IDENTIFICATION_STRATEGY::STRATEGY_CPU_MODEL, "cpu_lic_file"); +} + + BOOST_AUTO_TEST_CASE(strategy_mac_address) { vector adapters; FUNCTION_RETURN result_adapterInfos = os::getAdapterInfos(adapters);