Browse Source

Added "GET /2/dmi" API endpoint.

feature-dmi
XMRig 3 years ago
parent
commit
8471f7fad3
No known key found for this signature in database GPG Key ID: 446A53638BE94409
  1. 3
      CMakeLists.txt
  2. 10
      src/base/api/Api.cpp
  3. 11
      src/base/api/Api.h
  4. 14
      src/base/api/interfaces/IApiListener.h
  5. 16
      src/base/api/interfaces/IApiRequest.h
  6. 18
      src/base/kernel/interfaces/IBaseListener.h
  7. 42
      src/core/Controller.cpp
  8. 23
      src/core/Controller.h
  9. 47
      src/hw/api/HwApi.cpp
  10. 53
      src/hw/api/HwApi.h
  11. 11
      src/hw/api/api.cmake
  12. 16
      src/hw/dmi/DmiBoard.cpp
  13. 4
      src/hw/dmi/DmiBoard.h
  14. 26
      src/hw/dmi/DmiMemory.cpp
  15. 4
      src/hw/dmi/DmiMemory.h
  16. 34
      src/hw/dmi/DmiReader.cpp
  17. 5
      src/hw/dmi/DmiReader.h

3
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)

10
src/base/api/Api.cpp

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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

11
src/base/api/Api.h

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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"

14
src/base/api/interfaces/IApiListener.h

@ -1,10 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2018 XMRig <support@xmrig.com>
* Copyright 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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

16
src/base/api/interfaces/IApiRequest.h

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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;

18
src/base/kernel/interfaces/IBaseListener.h

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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;
};

42
src/core/Controller.cpp

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <cassert>
@ -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<Network>(this);
# ifdef XMRIG_FEATURE_API
m_hwApi = std::make_shared<HwApi>();
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<Miner>(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();
}

23
src/core/Controller.h

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <memory>
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<Miner> m_miner;
std::shared_ptr<Network> m_network;
# ifdef XMRIG_FEATURE_API
std::shared_ptr<HwApi> m_hwApi;
# endif
};

47
src/hw/api/HwApi.cpp

@ -0,0 +1,47 @@
/* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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<DmiReader>();
m_dmi->read();
}
request.accept();
m_dmi->toJSON(request.reply(), request.doc());
}
# endif
}
}

53
src/hw/api/HwApi.h

@ -0,0 +1,53 @@
/* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_HWAPI_H
#define XMRIG_HWAPI_H
#include "base/api/interfaces/IApiListener.h"
#include <memory>
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<DmiReader> m_dmi;
# endif
};
} /* namespace xmrig */
#endif /* XMRIG_HWAPI_H */

11
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()

16
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

4
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;

26
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

4
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;

34
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);

5
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<void()>;

Loading…
Cancel
Save