diff --git a/CMakeLists.txt b/CMakeLists.txt index fe411cb1c..79486e03e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -198,7 +198,8 @@ if (WITH_EMBEDDED_CONFIG) add_definitions(/DXMRIG_FEATURE_EMBEDDED_CONFIG) endif() -include (src/hw/dmi/dmi.cmake) +include(src/hw/api/api.cmake) +include(src/hw/dmi/dmi.cmake) include_directories(src) include_directories(src/3rdparty) diff --git a/src/base/api/Api.cpp b/src/base/api/Api.cpp index 89e61593c..3a71fd9c3 100644 --- a/src/base/api/Api.cpp +++ b/src/base/api/Api.cpp @@ -1,12 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright 2018-2021 SChernykh + * Copyright 2016-2021 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/base/api/Api.h b/src/base/api/Api.h index 0ee9ca6ae..f412a3172 100644 --- a/src/base/api/Api.h +++ b/src/base/api/Api.h @@ -1,12 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2021 SChernykh + * Copyright 2016-2021 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -31,7 +25,6 @@ #include "base/kernel/interfaces/IBaseListener.h" -#include "base/tools/Object.h" #include "base/tools/String.h" diff --git a/src/base/api/interfaces/IApiListener.h b/src/base/api/interfaces/IApiListener.h index bbf153a69..71ee9c76d 100644 --- a/src/base/api/interfaces/IApiListener.h +++ b/src/base/api/interfaces/IApiListener.h @@ -1,10 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2016-2018 XMRig + * Copyright 2018-2021 SChernykh + * Copyright 2016-2021 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,6 +20,9 @@ #define XMRIG_IAPILISTENER_H +#include "base/tools/Object.h" + + namespace xmrig { @@ -33,6 +32,9 @@ class IApiRequest; class IApiListener { public: + XMRIG_DISABLE_COPY_MOVE(IApiListener) + + IApiListener() = default; virtual ~IApiListener() = default; # ifdef XMRIG_FEATURE_API diff --git a/src/base/api/interfaces/IApiRequest.h b/src/base/api/interfaces/IApiRequest.h index c05e513ec..35fb27d5f 100644 --- a/src/base/api/interfaces/IApiRequest.h +++ b/src/base/api/interfaces/IApiRequest.h @@ -1,12 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright 2018-2021 SChernykh + * Copyright 2016-2021 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,6 +21,7 @@ #include "3rdparty/rapidjson/fwd.h" +#include "base/tools/Object.h" namespace xmrig { @@ -38,6 +33,8 @@ class String; class IApiRequest { public: + XMRIG_DISABLE_COPY_MOVE(IApiRequest) + enum Method { METHOD_DELETE, METHOD_GET, @@ -67,7 +64,8 @@ public: }; - virtual ~IApiRequest() = default; + IApiRequest() = default; + virtual ~IApiRequest() = default; virtual bool accept() = 0; virtual bool hasParseError() const = 0; diff --git a/src/base/kernel/interfaces/IBaseListener.h b/src/base/kernel/interfaces/IBaseListener.h index 1f2123690..80120ab85 100644 --- a/src/base/kernel/interfaces/IBaseListener.h +++ b/src/base/kernel/interfaces/IBaseListener.h @@ -1,12 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2021 SChernykh + * Copyright 2016-2021 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,6 +20,9 @@ #define XMRIG_IBASELISTENER_H +#include "base/tools/Object.h" + + namespace xmrig { @@ -35,7 +32,10 @@ class Config; class IBaseListener { public: - virtual ~IBaseListener() = default; + XMRIG_DISABLE_COPY_MOVE(IBaseListener) + + IBaseListener() = default; + virtual ~IBaseListener() = default; virtual void onConfigChanged(Config *config, Config *previousConfig) = 0; }; diff --git a/src/core/Controller.cpp b/src/core/Controller.cpp index 8f230e002..40c3aeac4 100644 --- a/src/core/Controller.cpp +++ b/src/core/Controller.cpp @@ -1,12 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2021 SChernykh + * Copyright 2016-2021 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -31,6 +25,12 @@ #include "net/Network.h" +#ifdef XMRIG_FEATURE_API +# include "base/api/Api.h" +# include "hw/api/HwApi.h" +#endif + + #include @@ -42,8 +42,6 @@ xmrig::Controller::Controller(Process *process) : xmrig::Controller::~Controller() { - delete m_network; - VirtualMemory::destroy(); } @@ -54,7 +52,12 @@ int xmrig::Controller::init() VirtualMemory::init(config()->cpu().memPoolSize(), config()->cpu().isHugePages()); - m_network = new Network(this); + m_network = std::make_shared(this); + +# ifdef XMRIG_FEATURE_API + m_hwApi = std::make_shared(); + api()->addListener(m_hwApi.get()); +# endif return 0; } @@ -64,7 +67,7 @@ void xmrig::Controller::start() { Base::start(); - m_miner = new Miner(this); + m_miner = std::make_shared(this); network()->connect(); } @@ -74,29 +77,26 @@ void xmrig::Controller::stop() { Base::stop(); - delete m_network; - m_network = nullptr; + m_network.reset(); m_miner->stop(); - - delete m_miner; - m_miner = nullptr; + m_miner.reset(); } xmrig::Miner *xmrig::Controller::miner() const { - assert(m_miner != nullptr); + assert(m_miner); - return m_miner; + return m_miner.get(); } xmrig::Network *xmrig::Controller::network() const { - assert(m_network != nullptr); + assert(m_network); - return m_network; + return m_network.get(); } diff --git a/src/core/Controller.h b/src/core/Controller.h index 947084734..13704f151 100644 --- a/src/core/Controller.h +++ b/src/core/Controller.h @@ -1,12 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2021 SChernykh + * Copyright 2016-2021 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,12 +21,15 @@ #include "base/kernel/Base.h" -#include "base/tools/Object.h" + + +#include namespace xmrig { +class HwApi; class Job; class Miner; class Network; @@ -55,8 +52,12 @@ public: void execCommand(char command); private: - Miner *m_miner = nullptr; - Network *m_network = nullptr; + std::shared_ptr m_miner; + std::shared_ptr m_network; + +# ifdef XMRIG_FEATURE_API + std::shared_ptr m_hwApi; +# endif }; diff --git a/src/hw/api/HwApi.cpp b/src/hw/api/HwApi.cpp new file mode 100644 index 000000000..5aedbf334 --- /dev/null +++ b/src/hw/api/HwApi.cpp @@ -0,0 +1,47 @@ +/* XMRig + * Copyright (c) 2018-2021 SChernykh + * Copyright (c) 2016-2021 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "hw/api/HwApi.h" +#include "base/api/interfaces/IApiRequest.h" +#include "base/tools/String.h" + +#include "base/io/log/Log.h" // FIXME + + +#ifdef XMRIG_FEATURE_DMI +# include "hw/dmi/DmiReader.h" +#endif + + +void xmrig::HwApi::onRequest(IApiRequest &request) +{ + if (request.method() == IApiRequest::METHOD_GET) { +# ifdef XMRIG_FEATURE_DMI + if (request.url() == "/2/dmi") { + if (!m_dmi) { + m_dmi = std::make_shared(); + m_dmi->read(); + } + + request.accept(); + m_dmi->toJSON(request.reply(), request.doc()); + } +# endif + } +} diff --git a/src/hw/api/HwApi.h b/src/hw/api/HwApi.h new file mode 100644 index 000000000..f434a0a5a --- /dev/null +++ b/src/hw/api/HwApi.h @@ -0,0 +1,53 @@ +/* XMRig + * Copyright (c) 2018-2021 SChernykh + * Copyright (c) 2016-2021 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_HWAPI_H +#define XMRIG_HWAPI_H + + +#include "base/api/interfaces/IApiListener.h" + + +#include + + +namespace xmrig { + + +struct DmiReader; + + +class HwApi : public IApiListener +{ +public: + HwApi() = default; + +protected: + void onRequest(IApiRequest &request) override; + +private: +# ifdef XMRIG_FEATURE_DMI + std::shared_ptr m_dmi; +# endif +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_HWAPI_H */ diff --git a/src/hw/api/api.cmake b/src/hw/api/api.cmake new file mode 100644 index 000000000..94b224704 --- /dev/null +++ b/src/hw/api/api.cmake @@ -0,0 +1,11 @@ +if (WITH_HTTP) + add_definitions(/DXMRIG_FEATURE_DMI) + + list(APPEND HEADERS + src/hw/api/HwApi.h + ) + + list(APPEND SOURCES + src/hw/api/HwApi.cpp + ) +endif() diff --git a/src/hw/dmi/DmiBoard.cpp b/src/hw/dmi/DmiBoard.cpp index 5ebfd9aa3..3cbe3dd9a 100644 --- a/src/hw/dmi/DmiBoard.cpp +++ b/src/hw/dmi/DmiBoard.cpp @@ -20,6 +20,7 @@ #include "hw/dmi/DmiBoard.h" +#include "3rdparty/rapidjson/document.h" #include "hw/dmi/DmiTools.h" @@ -32,3 +33,18 @@ void xmrig::DmiBoard::decode(dmi_header *h) m_vendor = dmi_string(h, 0x04); m_product = dmi_string(h, 0x05); } + + +#ifdef XMRIG_FEATURE_API +rapidjson::Value xmrig::DmiBoard::toJSON(rapidjson::Document &doc) const +{ + using namespace rapidjson; + + auto &allocator = doc.GetAllocator(); + Value out(kObjectType); + out.AddMember("vendor", m_vendor.toJSON(doc), allocator); + out.AddMember("product", m_product.toJSON(doc), allocator); + + return out; +} +#endif diff --git a/src/hw/dmi/DmiBoard.h b/src/hw/dmi/DmiBoard.h index dd28a607b..0c64da17c 100644 --- a/src/hw/dmi/DmiBoard.h +++ b/src/hw/dmi/DmiBoard.h @@ -42,6 +42,10 @@ public: void decode(dmi_header *h); +# ifdef XMRIG_FEATURE_API + rapidjson::Value toJSON(rapidjson::Document &doc) const; +# endif + private: String m_product; String m_vendor; diff --git a/src/hw/dmi/DmiMemory.cpp b/src/hw/dmi/DmiMemory.cpp index f02a64ecd..5b36e2bae 100644 --- a/src/hw/dmi/DmiMemory.cpp +++ b/src/hw/dmi/DmiMemory.cpp @@ -20,6 +20,7 @@ #include "hw/dmi/DmiMemory.h" +#include "3rdparty/rapidjson/document.h" #include "hw/dmi/DmiTools.h" @@ -190,3 +191,28 @@ const char *xmrig::DmiMemory::type() const { return dmi_memory_device_type(m_type); } + + +#ifdef XMRIG_FEATURE_API +rapidjson::Value xmrig::DmiMemory::toJSON(rapidjson::Document &doc) const +{ + using namespace rapidjson; + + auto &allocator = doc.GetAllocator(); + Value out(kObjectType); + out.AddMember("slot", m_slot.toJSON(doc), allocator); + out.AddMember("type", StringRef(type()), allocator); + out.AddMember("form_factor", StringRef(formFactor()), allocator); + out.AddMember("size", m_size, allocator); + out.AddMember("speed", m_speed, allocator); + out.AddMember("rank", m_rank, allocator); + out.AddMember("voltage", m_voltage, allocator); + out.AddMember("width", m_width, allocator); + out.AddMember("total_width", m_totalWidth, allocator); + out.AddMember("vendor", m_vendor.toJSON(doc), allocator); + out.AddMember("product", m_product.toJSON(doc), allocator); + out.AddMember("bank", m_bank.toJSON(doc), allocator); + + return out; +} +#endif diff --git a/src/hw/dmi/DmiMemory.h b/src/hw/dmi/DmiMemory.h index b9a0468b0..badee2cfd 100644 --- a/src/hw/dmi/DmiMemory.h +++ b/src/hw/dmi/DmiMemory.h @@ -52,6 +52,10 @@ public: const char *formFactor() const; const char *type() const; +# ifdef XMRIG_FEATURE_API + rapidjson::Value toJSON(rapidjson::Document &doc) const; +# endif + private: String m_bank; String m_product; diff --git a/src/hw/dmi/DmiReader.cpp b/src/hw/dmi/DmiReader.cpp index 9854ff1b4..18211759a 100644 --- a/src/hw/dmi/DmiReader.cpp +++ b/src/hw/dmi/DmiReader.cpp @@ -20,6 +20,8 @@ #include "hw/dmi/DmiReader.h" +#include "3rdparty/fmt/core.h" +#include "3rdparty/rapidjson/document.h" #include "hw/dmi/DmiTools.h" @@ -38,6 +40,38 @@ static void dmi_get_header(dmi_header *h, uint8_t *data) } // namespace xmrig +#ifdef XMRIG_FEATURE_API +rapidjson::Value xmrig::DmiReader::toJSON(rapidjson::Document &doc) const +{ + rapidjson::Value obj; + toJSON(obj, doc); + + return obj; +} + + +void xmrig::DmiReader::toJSON(rapidjson::Value &out, rapidjson::Document &doc) const +{ + using namespace rapidjson; + + auto &allocator = doc.GetAllocator(); + out.SetObject(); + + Value memory(kArrayType); + memory.Reserve(m_memory.size(), allocator); + + for (const auto &value : m_memory) { + memory.PushBack(value.toJSON(doc), allocator); + } + + out.AddMember("smbios", Value(fmt::format("{}.{}.{}", m_version >> 16, m_version >> 8 & 0xff, m_version & 0xff).c_str(), allocator), allocator); + out.AddMember("system", m_system.toJSON(doc), allocator); + out.AddMember("board", m_board.toJSON(doc), allocator); + out.AddMember("memory", memory, allocator); +} +#endif + + bool xmrig::DmiReader::decode(uint8_t *buf, const Cleanup &cleanup) { const bool rc = decode(buf); diff --git a/src/hw/dmi/DmiReader.h b/src/hw/dmi/DmiReader.h index ccf2311ae..a4f7eb8a7 100644 --- a/src/hw/dmi/DmiReader.h +++ b/src/hw/dmi/DmiReader.h @@ -45,6 +45,11 @@ public: bool read(); +# ifdef XMRIG_FEATURE_API + rapidjson::Value toJSON(rapidjson::Document &doc) const; + void toJSON(rapidjson::Value &out, rapidjson::Document &doc) const; +# endif + private: using Cleanup = std::function;