MIP_SDK  latest-2-g34f3e39
MicroStrain Communications Library for embedded systems
recording_connection.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include <iostream>
6 #include <memory>
7 #include <ostream>
8 
9 namespace microstrain
10 {
11  namespace connections
12  {
16 
22  {
23  public:
24  static constexpr const char* TYPE = "Recording";
25 
26  RecordingConnection(Connection* connection, std::ostream* recvStream = nullptr, std::ostream* sendStream = nullptr);
27  ~RecordingConnection() override = default;
28 
29  bool sendToDevice(const uint8_t* data, size_t length) override;
30  bool recvFromDevice(uint8_t* buffer, size_t max_length, unsigned int wait_time_ms, size_t* length_out, EmbeddedTimestamp* timestamp_out) override;
31 
32  bool isConnected() const override
33  {
34  if (mConnection)
35  return mConnection->isConnected();
36 
37  return false;
38  };
39 
40  bool connect() override
41  {
42  if (mConnection)
43  return mConnection->connect();
44 
45  return false;
46  };
47  bool disconnect() override
48  {
49  if (mConnection)
50  return mConnection->disconnect();
51 
52  return false;
53  };
54 
55  const char* interfaceName() const override { return mConnection->interfaceName(); }
56  uint32_t parameter() const override { return mConnection->parameter(); }
57 
58  uint64_t recvFileBytesWritten() const
59  {
60  return mRecvFileWritten;
61  }
62 
63  uint64_t sendFileBytesWritten() const
64  {
65  return mSendFileWritten;
66  }
67 
68  protected:
70 
71  // Files may be NULL to not record one direction or the other
72  std::ostream* mRecvFile;
73  std::ostream* mSendFile;
74 
75  uint64_t mRecvFileWritten = 0;
76  uint64_t mSendFileWritten = 0;
77  };
78 
84  template<typename ConnectionType>
86  {
87  public:
101  template<class... Args>
102  RecordingConnectionWrapper(std::ostream* recvStream, std::ostream* sendStream, Args&&... args) :
103  RecordingConnection(new ConnectionType(std::forward<Args>(args)...), recvStream, sendStream)
104  {}
105 
108  };
109 
112  } // namespace connections
113 } // namespace microstrain
microstrain::connections::RecordingConnectionWrapper::RecordingConnectionWrapper
RecordingConnectionWrapper(std::ostream *recvStream, std::ostream *sendStream, Args &&... args)
Creates a RecordingConnectionWrapper that will write received bytes to recvStream,...
Definition: recording_connection.hpp:102
microstrain::connections::RecordingConnection::mRecvFileWritten
uint64_t mRecvFileWritten
Definition: recording_connection.hpp:75
microstrain::connections::RecordingConnection::sendFileBytesWritten
uint64_t sendFileBytesWritten() const
Definition: recording_connection.hpp:63
microstrain::Connection::interfaceName
virtual const char * interfaceName() const =0
microstrain::connections::RecordingConnectionWrapper::~RecordingConnectionWrapper
~RecordingConnectionWrapper() override
Deconstructs the RecordingConnectionWrapper as well as the underlying connection object made in the c...
Definition: recording_connection.hpp:107
microstrain::Connection::parameter
virtual uint32_t parameter() const =0
microstrain::connections::RecordingConnection::connect
bool connect() override
Definition: recording_connection.hpp:40
microstrain::connections::RecordingConnection::RecordingConnection
RecordingConnection(Connection *connection, std::ostream *recvStream=nullptr, std::ostream *sendStream=nullptr)
Creates a RecordingConnection that will write received bytes to recvStream, and sent bytes to sendStr...
Definition: recording_connection.cpp:20
microstrain::connections::RecordingConnectionWrapper
Template wrapper for a recording connection.
Definition: recording_connection.hpp:85
microstrain::connections::RecordingConnection::sendToDevice
bool sendToDevice(const uint8_t *data, size_t length) override
Sends bytes to the device.
Definition: recording_connection.cpp:27
microstrain::connections::RecordingConnection::recvFromDevice
bool recvFromDevice(uint8_t *buffer, size_t max_length, unsigned int wait_time_ms, size_t *length_out, EmbeddedTimestamp *timestamp_out) override
Receives bytes from the device.
Definition: recording_connection.cpp:41
microstrain::connections::RecordingConnection
Can be used with another connection to communicate with a device, and record the data at the same tim...
Definition: recording_connection.hpp:21
connection.hpp
microstrain::connections::RecordingConnection::recvFileBytesWritten
uint64_t recvFileBytesWritten() const
Definition: recording_connection.hpp:58
microstrain::connections::RecordingConnection::interfaceName
const char * interfaceName() const override
Definition: recording_connection.hpp:55
microstrain::EmbeddedTimestamp
C::microstrain_embedded_timestamp EmbeddedTimestamp
Definition: embedded_time.hpp:9
microstrain::Connection::disconnect
virtual bool disconnect()=0
microstrain::connections::RecordingConnection::disconnect
bool disconnect() override
Definition: recording_connection.hpp:47
microstrain::connections::RecordingConnection::isConnected
bool isConnected() const override
Definition: recording_connection.hpp:32
microstrain::connections::RecordingConnection::parameter
uint32_t parameter() const override
Definition: recording_connection.hpp:56
microstrain::Connection::isConnected
virtual bool isConnected() const =0
microstrain::Connection
Represents a type of connection.
Definition: connection.hpp:24
microstrain::connections::RecordingConnection::mConnection
Connection * mConnection
Definition: recording_connection.hpp:69
microstrain::connections::RecordingConnection::mRecvFile
std::ostream * mRecvFile
Definition: recording_connection.hpp:72
microstrain::connections::RecordingConnection::mSendFileWritten
uint64_t mSendFileWritten
Definition: recording_connection.hpp:76
microstrain::connections::RecordingConnection::mSendFile
std::ostream * mSendFile
Definition: recording_connection.hpp:73
microstrain::connections::RecordingConnection::TYPE
static constexpr const char * TYPE
Definition: recording_connection.hpp:24
microstrain
Definition: embedded_time.h:8
microstrain::Connection::connect
virtual bool connect()=0
microstrain::connections::RecordingConnection::~RecordingConnection
~RecordingConnection() override=default