IpbusMock  1.0.0
Library which integrates IPbus use in Phoenix
Loading...
Searching...
No Matches
ipbrw.cpp
Go to the documentation of this file.
1#include "ipbrw.h"
2#include <bits/stdc++.h>
3#include <cstdlib> // for rand() function
4#include <iostream>
5
12IpBusImpl::IpBusImpl(const std::string &connectionFilePath,
13 const std::string &connectionId) {
14 uhal::ConnectionManager connectionMgr(connectionFilePath);
16 new uhal::HwInterface(connectionMgr.getDevice(connectionId));
17}
18
26
33uint32_t IpBusImpl::getSize(const std::string &nodeId) const {
34 return this->hardwareInterface->getNode(nodeId).getSize();
35}
36
44bool IpBusImpl::read(const std::string &nodeId, uint32_t &result) {
45 const uhal::Node &lNode = this->hardwareInterface->getNode(nodeId);
46 uhal::ValWord<uint32_t> lReg = lNode.read();
47 this->hardwareInterface->dispatch();
48 if (!lReg.valid()) {
49 std::cerr << "Error: Register read failed." << std::endl;
50 return false;
51 } else {
52 result = lReg.value();
53 return true;
54 }
55}
56
64bool IpBusImpl::write(const std::string &nodeId, const uint32_t value) {
65 const uhal::Node &lNode = this->hardwareInterface->getNode(nodeId);
66 lNode.write(value);
67 this->hardwareInterface->dispatch();
68 return true;
69}
70
79bool IpBusImpl::readBlock(const std::string &nodeId,
80 std::vector<uint32_t> &results,
81 const uint32_t &aSize) {
82 const uhal::Node &lNode = this->hardwareInterface->getNode(nodeId);
83 uhal::ValVector<uint32_t> lReg = lNode.readBlock(aSize);
84 this->hardwareInterface->dispatch();
85 if (!lReg.valid()) {
86 std::cerr << "Error: Register read failed." << std::endl;
87 return false;
88 } else {
89 results.clear();
90 copy(lReg.begin(), lReg.end(), back_inserter(results));
91 return true;
92 }
93}
94
102bool IpBusImpl::writeBlock(const std::string &nodeId,
103 const std::vector<uint32_t> values) {
104 const uhal::Node &lNode = this->hardwareInterface->getNode(nodeId);
105 lNode.writeBlock(values);
106 this->hardwareInterface->dispatch();
107 return true;
108}
109
117IpBusImpl *IpBusImpl::pipbus_createSocket(const std::string &connectionFilePath,
118 const std::string &connectionId) {
119 static IpBusImpl _self(connectionFilePath, connectionId);
120 return &_self;
121}
122
128void IpBusImpl::setLogLevelTo(const std::string level) {
129 if (level == "Debug") {
130 uhal::setLogLevelTo(uhal::Debug());
131 } else if (level == "Info") {
132 uhal::setLogLevelTo(uhal::Info());
133 } else if (level == "Notice") {
134 uhal::setLogLevelTo(uhal::Notice());
135 } else if (level == "Warning") {
136 uhal::setLogLevelTo(uhal::Warning());
137 } else if (level == "Error") {
138 uhal::setLogLevelTo(uhal::Error());
139 } else if (level == "Fatal") {
140 uhal::setLogLevelTo(uhal::Fatal());
141 } else {
142 std::cerr << "Invalid log level. Use Debug, Info, Notice, Warning, "
143 << "Error, or Fatal." << std::endl;
144 uhal::setLogLevelTo(uhal::Info());
145 }
146}
147
152 if (hardwareInterface != nullptr) {
153 try {
154 // uHAL does not provide a close method,
155 // But we can delete the HwInterface to close the connection.
156 delete hardwareInterface;
157 hardwareInterface = nullptr;
158 } catch (const std::exception& e) {
159 std::cerr << "Error closing connection: " << e.what() << std::endl;
160 }
161 }
162}
bool writeBlock(const std::string &nodeId, const std::vector< uint32_t > values)
Writes a block of data to a block of registers or a block-write port.
Definition ipbrw.cpp:102
~IpBusImpl()
Destructor of IpBusImpl.
Definition ipbrw.cpp:22
uint32_t getSize(const std::string &nodeId) const
Computes the node size.
Definition ipbrw.cpp:33
bool readBlock(const std::string &nodeId, std::vector< uint32_t > &results, const uint32_t &aSize)
Reads a block of data from a block of registers or a block-read port.
Definition ipbrw.cpp:79
IpBusImpl(const std::string &connectionFilePath, const std::string &connectionId)
Constructor of IpBusImpl.
Definition ipbrw.cpp:12
bool read(const std::string &nodeId, uint32_t &res)
Reads a register using the uHAL library.
Definition ipbrw.cpp:44
static IpBusImpl * pipbus_createSocket(const std::string &connectionFilePath, const std::string &connectionId)
Creates an IpBusImpl instance.
Definition ipbrw.cpp:117
uhal::HwInterface * hardwareInterface
Definition ipbrw.h:29
bool write(const std::string &nodeId, const uint32_t values)
Writes a register using the uHAL library.
Definition ipbrw.cpp:64
void close()
Close the IpBusImpl connection.
Definition ipbrw.cpp:151
static void setLogLevelTo(const std::string level)
Sets the uHAL log level to the given level.
Definition ipbrw.cpp:128