8#include <boost/asio.hpp>
9#include <boost/bind/bind.hpp>
19 unsigned int protocolVersion, ip::tcp::socket socket, std::shared_ptr<DummyBackend> registerSpace)
20 : _state(ACCEPT_NEW_COMMAND), _heartbeatCount(0), _helloCount(0), _dont_answer(false), _registerSpace(registerSpace),
21 _protocolVersion(protocolVersion), _currentClientConnection(
std::move(socket)) {
22 if(protocolVersion == 0) {
25 else if(protocolVersion == 1) {
29 throw std::invalid_argument(
"RebotDummyServer: unknown protocol version");
40 auto self(shared_from_this());
41 std::shared_ptr<std::vector<uint32_t>> buffer;
45 boost::asio::buffer(*buffer), [
this, self, buffer](boost::system::error_code ec, std::size_t) {
61 auto self(shared_from_this());
63 boost::asio::buffer(
_dataBuffer), [
this, self](boost::system::error_code ec, std::size_t) {
68 std::cerr << ec << std::endl;
90 uint32_t requestedAction = buffer.at(0);
91 switch(requestedAction) {
110 std::cout <<
"Instruction unknown in all protocol versions " << requestedAction << std::endl;
121 uint64_t registerAddress = buffer.at(1);
124 registerAddress = registerAddress * 4;
125 int32_t wordToWrite = buffer.at(2);
127 _registerSpace->write(bar, registerAddress, &wordToWrite,
sizeof(wordToWrite));
134 uint64_t registerAddress = buffer.at(1);
135 registerAddress = registerAddress * 4;
136 uint32_t numberOfWordsToRead = buffer.at(2);
142 std::vector<uint32_t> dataToSend(numberOfWordsToRead);
145 int32_t* startAddressForReadInData =
reinterpret_cast<int32_t*
>(dataToSend.data());
146 _registerSpace->read(bar, registerAddress, startAddressForReadInData, numberOfWordsToRead *
sizeof(int32_t));
173 : _protocolVersion(protocolVersion), _io(), _connectionAcceptor(_io, ip::tcp::endpoint(ip::tcp::v4(), portNumber)),
174 _socket(_io), _registerSpace(
std::make_shared<
DummyBackend>(mapFile)) {
177 uint64_t registerAddress = 0x04;
178 int32_t wordToWrite = 0xDEADBEEF;
181 _registerSpace->open();
182 _registerSpace->write(bar, registerAddress, &wordToWrite,
sizeof(wordToWrite));
185 void RebotDummyServer::do_accept() {
186 _connectionAcceptor.async_accept(_socket, [
this](boost::system::error_code ec) {
188 if(_currentSession.expired()) {
189 auto newSession = std::make_shared<RebotDummySession>(_protocolVersion, std::move(_socket), _registerSpace);
190 _currentSession = newSession;
194 auto code = boost::system::errc::make_error_code(boost::system::errc::connection_refused);
212 return not _io.stopped();
The dummy device opens a mapping file instead of a device, and implements all registers defined in th...
RebotDummyServer(unsigned int portNumber, std::string mapFile, unsigned int protocolVersion)
static const uint32_t INSIDE_MULTI_WORD_WRITE
std::atomic< uint32_t > _heartbeatCount
static const uint32_t MULTI_WORD_WRITE
ip::tcp::socket _currentClientConnection
void writeWordToRequestedAddress(std::vector< uint32_t > &buffer)
void processReceivedPackage(std::vector< uint32_t > &buffer)
std::vector< uint32_t > _dataBuffer
static const uint32_t HELLO
RebotDummySession(unsigned int protocolVersion, ip::tcp::socket socket, std::shared_ptr< DummyBackend > regsiterSpace)
std::atomic< uint32_t > _helloCount
void sendSingleWord(int32_t response)
void readRegisterAndSendData(std::vector< uint32_t > &buffer)
static const uint32_t PING
void write(std::vector< uint32_t > data)
static const uint32_t MULTI_WORD_READ
static const int32_t READ_SUCCESS_INDICATION
std::unique_ptr< DummyProtocolImplementor > _protocolImplementor
static const int BUFFER_SIZE_IN_WORDS
virtual ~RebotDummySession()
static const int32_t UNKNOWN_INSTRUCTION
static const uint32_t SINGLE_WORD_WRITE
std::shared_ptr< DummyBackend > _registerSpace
std::atomic< bool > _dont_answer
std::atomic< bool > stop_rebot_server
Only put commands which don't exist in all versions, or behave differently.
Only put commands which don't exist in all versions, or behave differently.