ChimeraTK-DeviceAccess 03.25.00
Loading...
Searching...
No Matches
RebotBackend.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3#pragma once
4
6
7#include <boost/algorithm/string.hpp>
8#include <boost/array.hpp>
9#include <boost/asio.hpp>
10#include <boost/chrono.hpp>
11#include <boost/make_shared.hpp>
12#include <boost/shared_ptr.hpp>
13#include <boost/thread.hpp>
14
15#include <cstdio>
16#include <cstdlib>
17#include <iomanip>
18#include <iostream>
19#include <memory>
20#include <mutex>
21#include <sstream>
22#include <string>
23#include <vector>
24
25namespace ChimeraTK {
26 namespace Rebot {
27 class Connection;
28 } // namespace Rebot
29
31
32 // A helper class that contains a mutex and a flag.
33 // The idea is to put it into a shared pointer and hand it to a thread which is
34 // sleeping a long time. You can then detach the thread, tell it to finish and
35 // continue with the constructor without having to wait for the tread to wake up
36 // and finish before you can join it. The thread locks the mutex and checks if
37 // it should finish when it wakes up, which it can do because the mutex and flag
38 // still exist thanks to the shared pointer.
40 std::mutex mutex;
41 bool quitThread{false};
42 };
43
45 protected:
46 std::string _boardAddr;
47 std::string _port;
48
49 boost::shared_ptr<ThreadInformerMutex> _threadInformerMutex;
50 // Only access the following membergs when holding the mutex. They are
51 // also accessed by the heartbeat thread
52 boost::shared_ptr<Rebot::Connection> _connection;
53 std::unique_ptr<RebotProtocolImplementor> _protocolImplementor;
55 boost::chrono::steady_clock::time_point _lastSendTime;
56 unsigned int _connectionTimeout;
57
58 public:
59 RebotBackend(std::string boardAddr, std::string port, const std::string& mapFileName = "",
60 uint32_t connectionTimeout_sec = DEFAULT_CONNECTION_TIMEOUT_sec);
61 ~RebotBackend() override;
63 void open() override;
64 void closeImpl() override;
65 void read(uint8_t bar, uint32_t addressInBytes, int32_t* data, size_t sizeInBytes) override;
66 void write(uint8_t bar, uint32_t addressInBytes, int32_t const* data, size_t sizeInBytes) override;
67 std::string readDeviceInfo() override { return {"RebotDevice"}; }
68
69 static boost::shared_ptr<DeviceBackend> createInstance(
70 std::string address, std::map<std::string, std::string> parameters);
71
72 size_t minimumTransferAlignment([[maybe_unused]] uint64_t bar) const override { return 4; }
73
74 protected:
75 void heartbeatLoop(const boost::shared_ptr<ThreadInformerMutex>& threadInformerMutex);
76 boost::thread _heartbeatThread;
77
78 const static uint32_t DEFAULT_CONNECTION_TIMEOUT_sec{5};
79 };
80
81} // namespace ChimeraTK
Base class for address-based device backends (e.g.
Handles the communication over TCP protocol with RebotDevice-based devices.
Definition Connection.h:19
boost::chrono::steady_clock::time_point _lastSendTime
The time when the last command (read/write/heartbeat) was send.
void open() override
The function opens the connection to the device.
boost::thread _heartbeatThread
boost::shared_ptr< ThreadInformerMutex > _threadInformerMutex
unsigned int _connectionTimeout
size_t minimumTransferAlignment(uint64_t bar) const override
Determines the supported minimum alignment for any read/write requests.
void heartbeatLoop(const boost::shared_ptr< ThreadInformerMutex > &threadInformerMutex)
std::unique_ptr< RebotProtocolImplementor > _protocolImplementor
std::string readDeviceInfo() override
Return a device information string containing hardware details like the firmware version number or th...
static const uint32_t DEFAULT_CONNECTION_TIMEOUT_sec
boost::shared_ptr< Rebot::Connection > _connection
void closeImpl() override
All backends derrived from NumericAddressedBackend must implement closeImpl() instead of close.
static boost::shared_ptr< DeviceBackend > createInstance(std::string address, std::map< std::string, std::string > parameters)
This is the base class of the code which implements the ReboT protocol.