ChimeraTK-DeviceAccess  03.18.00
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 
25 namespace 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
ChimeraTK::RebotBackend::open
void open() override
The function opens the connection to the device.
Definition: RebotBackend.cc:94
ChimeraTK::RebotBackend::write
void write(uint8_t bar, uint32_t addressInBytes, int32_t const *data, size_t sizeInBytes) override
Definition: RebotBackend.cc:117
ChimeraTK::RebotBackend::read
void read(uint8_t bar, uint32_t addressInBytes, int32_t *data, size_t sizeInBytes) override
Definition: RebotBackend.cc:105
ChimeraTK::RebotBackend::_threadInformerMutex
boost::shared_ptr< ThreadInformerMutex > _threadInformerMutex
Definition: RebotBackend.h:49
ChimeraTK::Rebot::Connection
Handles the communication over TCP protocol with RebotDevice-based devices.
Definition: Connection.h:19
NumericAddressedBackend.h
ChimeraTK::NumericAddressedBackend
Base class for address-based device backends (e.g.
Definition: NumericAddressedBackend.h:20
ChimeraTK::RebotBackend::_heartbeatThread
boost::thread _heartbeatThread
Definition: RebotBackend.h:76
ChimeraTK::RebotBackend::minimumTransferAlignment
size_t minimumTransferAlignment([[maybe_unused]] uint64_t bar) const override
Determines the supported minimum alignment for any read/write requests.
Definition: RebotBackend.h:72
ChimeraTK::RebotBackend::RebotBackend
RebotBackend(std::string boardAddr, std::string port, const std::string &mapFileName="", uint32_t connectionTimeout_sec=DEFAULT_CONNECTION_TIMEOUT_sec)
Definition: RebotBackend.cc:68
ChimeraTK::RebotBackend::_port
std::string _port
Definition: RebotBackend.h:47
ChimeraTK::RebotBackend::_boardAddr
std::string _boardAddr
Definition: RebotBackend.h:46
ChimeraTK::RebotBackend::heartbeatLoop
void heartbeatLoop(const boost::shared_ptr< ThreadInformerMutex > &threadInformerMutex)
Definition: RebotBackend.cc:161
ChimeraTK::ThreadInformerMutex::mutex
std::mutex mutex
Definition: RebotBackend.h:40
ChimeraTK::RebotBackend
Definition: RebotBackend.h:44
ChimeraTK::RebotBackend::closeImpl
void closeImpl() override
All backends derrived from NumericAddressedBackend must implement closeImpl() instead of close.
Definition: RebotBackend.cc:129
ChimeraTK::ThreadInformerMutex
Definition: RebotBackend.h:39
ChimeraTK::RebotBackend::readDeviceInfo
std::string readDeviceInfo() override
Return a device information string containing hardware details like the firmware version number or th...
Definition: RebotBackend.h:67
ChimeraTK::ThreadInformerMutex::quitThread
bool quitThread
Definition: RebotBackend.h:41
ChimeraTK::RebotProtocolImplementor
This is the base class of the code which implements the ReboT protocol.
Definition: RebotProtocolImplementor.h:14
ChimeraTK::RebotBackend::_lastSendTime
boost::chrono::steady_clock::time_point _lastSendTime
The time when the last command (read/write/heartbeat) was send.
Definition: RebotBackend.h:55
ChimeraTK::RebotBackend::_connection
boost::shared_ptr< Rebot::Connection > _connection
Definition: RebotBackend.h:52
ChimeraTK::RebotBackend::createInstance
static boost::shared_ptr< DeviceBackend > createInstance(std::string address, std::map< std::string, std::string > parameters)
Definition: RebotBackend.cc:139
ChimeraTK::RebotBackend::DEFAULT_CONNECTION_TIMEOUT_sec
const static uint32_t DEFAULT_CONNECTION_TIMEOUT_sec
Definition: RebotBackend.h:78
ChimeraTK::RebotBackend::~RebotBackend
~RebotBackend() override
Definition: RebotBackend.cc:76
ChimeraTK
Definition: DummyBackend.h:16
ChimeraTK::RebotBackend::_connectionTimeout
unsigned int _connectionTimeout
Definition: RebotBackend.h:56
ChimeraTK::RebotBackend::_protocolImplementor
std::unique_ptr< RebotProtocolImplementor > _protocolImplementor
Definition: RebotBackend.h:53