Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cpu model strategy #134

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/library/hw_identifier/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
52 changes: 52 additions & 0 deletions src/library/hw_identifier/cpu_strategy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* cpu_strategy.cpp
*
* Created on: Nov 23, 2021
* Author: Tibalt
*/
#include <string.h>
#include "../os/os.h"
#include "cpu_strategy.hpp"

using namespace std;
namespace license {
namespace hw_identifier {

static array<uint8_t, HW_IDENTIFIER_PROPRIETARY_DATA> generate_id_by_model(const uint32_t &cpu_info) {
array<uint8_t, HW_IDENTIFIER_PROPRIETARY_DATA> 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<uint8_t, HW_IDENTIFIER_PROPRIETARY_DATA> &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<HwIdentifier> CPUStrategy::alternative_ids() const {
array<uint8_t, HW_IDENTIFIER_PROPRIETARY_DATA> data;
FUNCTION_RETURN result = generate_cpu_pc_id(data);
vector<HwIdentifier> 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 */
27 changes: 27 additions & 0 deletions src/library/hw_identifier/cpu_strategy.hpp
Original file line number Diff line number Diff line change
@@ -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<HwIdentifier> alternative_ids() const;
};

} // namespace hw_identifier
} /* namespace license */

#endif /* SRC_LIBRARY_PC_IDENTIFIER_CPU_STRATEGY_HPP_*/
4 changes: 4 additions & 0 deletions src/library/hw_identifier/identification_strategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "default_strategy.hpp"
#include "ethernet.hpp"
#include "disk_strategy.hpp"
#include "cpu_strategy.hpp"
namespace license {
namespace hw_identifier {

Expand Down Expand Up @@ -47,6 +48,9 @@ std::unique_ptr<IdentificationStrategy> IdentificationStrategy::get_strategy(LCC
case STRATEGY_DISK:
result = unique_ptr<IdentificationStrategy>(dynamic_cast<IdentificationStrategy*>(new DiskStrategy()));
break;
case STRATEGY_CPU_MODEL:
result = unique_ptr<IdentificationStrategy>(dynamic_cast<IdentificationStrategy*>(new CPUStrategy()));
break;
default:
throw logic_error("strategy not supported");
}
Expand Down
8 changes: 8 additions & 0 deletions src/library/os/linux/os_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef NDEBUG
#include <valgrind/memcheck.h>
#endif
#include "../cpu_info.hpp"

//#ifdef USE_DISK_MODEL
///#define PARSE_ID_FUNC parse_disk_id
Expand Down Expand Up @@ -312,6 +313,13 @@ static void set_preferred_disks(std::vector<DiskInfo> &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
Expand Down
1 change: 1 addition & 0 deletions src/library/os/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ typedef struct {
} DiskInfo;

FUNCTION_RETURN getDiskInfos(std::vector<DiskInfo>& 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]);
Expand Down
6 changes: 6 additions & 0 deletions src/library/os/windows/os_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion src/templates/licensecc_properties.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
6 changes: 6 additions & 0 deletions test/functional/hw_identifier_it_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<os::OsAdapterInfo> adapters;
FUNCTION_RETURN result_adapterInfos = os::getAdapterInfos(adapters);
Expand Down