IpbusMock  1.0.0
Library which integrates IPbus use in Phoenix
Loading...
Searching...
No Matches
HwInterface.cpp
Go to the documentation of this file.
1#include "HwInterface.h"
2#include <filesystem>
3#include <iostream>
4
5using namespace PIpBusInterface;
6
7// Static member initialization
9
23 std::string connectionId,
24 PSocketMode::PSocketMode mock_mode,
25 std::string mock_directory)
28 if (mock_directory.empty()) {
29 mock_directory = connectionId + "_mock_dir"; // Default directory
30 }
31 std::filesystem::create_directory(mock_directory);
32 manager.setMode(mock_mode);
33
34 log(uhal::Debug(),
35 "Creating HwInterface. Adding server socket for device: ", connectionId,
36 " from ", connectionFilePath);
37 data_stream_assert(manager.addServerSocket(connectionId, PSocketParam{"localhost", 3390}, Backend::server(connectionFilePath, connectionId), "./", Mock::server()));
38}
39
47 nodes.clear();
48 log(uhal::Debug(), "Deleting HwInterface:", connectionFilePath, ", ",
50}
51
62Node &HwInterface::getNode(const std::string nodeId) {
63 if (nodes.count(nodeId)) {
64 return *nodes[nodeId];
65 }
66 nodes[nodeId] = std::unique_ptr<Node>(new Node(this, nodeId));
67 return *nodes[nodeId];
68}
69
79bool HwInterface::write(const std::string &nodeId, const uint32_t &aValue) {
80 std::vector<uint32_t> data = {aValue};
81 UaMessage msgOut{connectionId, nodeId, &data};
82 return manager.sendMsg(connectionId, msgOut);
83}
84
93uint32_t HwInterface::read(const std::string nodeId) const {
96 std::vector<uint32_t> *valuePtr = UaMessage.value.get();
97 return valuePtr->at(0);
98}
99
107bool HwInterface::writeBlock(const std::string &nodeId,
108 const std::vector<uint32_t> &aValues) const {
109 UaMessage msgOut{connectionId, nodeId, &aValues};
110 return manager.sendMsg(connectionId, msgOut);
111}
112
120std::vector<uint32_t> HwInterface::readBlock(const std::string &nodeId,
121 const uint32_t &aSize) const {
122 UaMessage msg{connectionId, nodeId, aSize};
123 manager.recvMsg(connectionId, msg);
124 return *msg.value.get();
125}
126
136Node::Node(HwInterface *_hwInterface, const std::string _nodeId)
137 : hwInterface(_hwInterface), nodeId(_nodeId) {
138 log(uhal::Debug(), "Creating node: ", nodeId);
139}
140
146Node::~Node() { log(uhal::Debug(), "Deleting node: ", nodeId); }
147
156bool Node::write(const uint32_t &aValue) const {
157 hwInterface->write(nodeId, aValue);
158 return true;
159}
160
168uint32_t Node::read() const { return hwInterface->read(nodeId); }
169
178std::vector<uint32_t> Node::readBlock(const uint32_t &aSize) const {
179 return hwInterface->readBlock(nodeId, aSize);
180}
181
188bool Node::writeBlock(const std::vector<uint32_t> &aValues) const {
189 return hwInterface->writeBlock(nodeId, aValues);
190}
PSocketMode::PSocketMode mock_mode
Definition HwInterface.h:52
virtual ~HwInterface()
Destructor for the HwInterface class.
std::vector< uint32_t > readBlock(const std::string &nodeId, const uint32_t &aSize) const
Reads a block of data from a specific node.
bool write(const std::string &nodeId, const uint32_t &aValue)
Writes data to a specific node.
Node & getNode(const std::string nodeId)
Retrieves a node by its identifier.
bool writeBlock(const std::string &nodeId, const std::vector< uint32_t > &aValues) const
Writes a block of data to a specific node.
static SocketManager manager
Definition HwInterface.h:54
std::map< std::string, std::unique_ptr< Node > > nodes
Definition HwInterface.h:55
uint32_t read(const std::string nodeId) const
Reads data from a specific node.
HwInterface(std::string connectionFilePath, std::string connectionId, PSocketMode::PSocketMode _mock_mode, std::string mock_directory="")
Constructor for the HwInterface class.
bool writeBlock(const std::vector< uint32_t > &aValues) const
Writes a block of data to a specific node.
uint32_t read() const
Reads a value from the node.
~Node()
Destructor for the Node class.
Node(HwInterface *_hwInterface, const std::string _nodeId)
Constructor for the Node class.
bool write(const uint32_t &aValue) const
Writes a value to the node.
std::vector< uint32_t > readBlock(const uint32_t &aSize) const
Reads a block of data from the node.
HwInterface * hwInterface
Definition HwInterface.h:34
Set of parameters to be passed as message.
Definition uaMessage.h:10
std::unique_ptr< std::vector< uint32_t > > value
Definition uaMessage.h:17
PGenericSocketManager< std::string, PIpBusBackend, PMockBackend > SocketManager
Definition HwInterface.h:14