MIP_SDK  v3.0.0
MicroStrain Communications Library for embedded systems
recording_connection.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../connection.hpp"
4 
5 #include <memory>
6 #include <ostream>
7 #include <iostream>
8 
9 namespace microstrain
10 {
11 namespace connections
12 {
13 
17 
22 {
23 public:
24  static constexpr auto TYPE = "Recording";
25 
26  RecordingConnection(Connection *connection, std::ostream *recvStream = nullptr, std::ostream *sendStream = nullptr);
27  ~RecordingConnection() = 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) return mConnection->connect();
43 
44  return false;
45  };
46  bool disconnect() override
47  {
48  if (mConnection) return mConnection->disconnect();
49 
50  return false;
51  };
52 
53  const char* interfaceName() const override { return mConnection->interfaceName(); }
54  uint32_t parameter() const override { return mConnection->parameter(); }
55 
56  uint64_t recvFileBytesWritten() const
57  {
58  return mRecvFileWritten;
59  }
60 
61  uint64_t sendFileBytesWritten() const
62  {
63  return mSendFileWritten;
64  }
65 
66 protected:
68 
69  // Files may be NULL to not record one direction or the other
70  std::ostream* mRecvFile;
71  std::ostream* mSendFile;
72 
73  uint64_t mRecvFileWritten = 0;
74  uint64_t mSendFileWritten = 0;
75 };
76 
82 template<typename ConnectionType>
84 {
85 public:
91  template<class... Args>
92  RecordingConnectionWrapper(std::ostream* recvStream, std::ostream* sendStream, Args&&... args) : RecordingConnection(new ConnectionType(std::forward<Args>(args)...), recvStream, sendStream) {}
93 
96 };
97 
100 
101 } // namespace connections
102 } // 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:92
microstrain::connections::RecordingConnection::mRecvFileWritten
uint64_t mRecvFileWritten
Definition: recording_connection.hpp:73
microstrain::connections::RecordingConnection::sendFileBytesWritten
uint64_t sendFileBytesWritten() const
Definition: recording_connection.hpp:61
microstrain::connections::RecordingConnection::~RecordingConnection
~RecordingConnection()=default
microstrain::Connection::interfaceName
virtual const char * interfaceName() const =0
microstrain::Connection::parameter
virtual uint32_t parameter() const =0
microstrain::connections::RecordingConnection::TYPE
static constexpr auto TYPE
Definition: recording_connection.hpp:24
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:15
microstrain::connections::RecordingConnectionWrapper
Template wrapper for a recording connection.
Definition: recording_connection.hpp:83
microstrain::connections::RecordingConnection::sendToDevice
bool sendToDevice(const uint8_t *data, size_t length) override
Sends bytes to the device.
Definition: recording_connection.cpp:22
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:36
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
microstrain::connections::RecordingConnection::recvFileBytesWritten
uint64_t recvFileBytesWritten() const
Definition: recording_connection.hpp:56
microstrain::connections::RecordingConnectionWrapper::~RecordingConnectionWrapper
~RecordingConnectionWrapper()
Deconstructs the RecordingConnectionWrapper as well as the underlying connection object made in the c...
Definition: recording_connection.hpp:95
microstrain::connections::RecordingConnection::interfaceName
const char * interfaceName() const override
Definition: recording_connection.hpp:53
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:46
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:54
microstrain::Connection::isConnected
virtual bool isConnected() const =0
microstrain::Connection
Represents a type of connection.
Definition: connection.hpp:25
microstrain::connections::RecordingConnection::mConnection
Connection * mConnection
Definition: recording_connection.hpp:67
microstrain::connections::RecordingConnection::mRecvFile
std::ostream * mRecvFile
Definition: recording_connection.hpp:70
microstrain::connections::RecordingConnection::mSendFileWritten
uint64_t mSendFileWritten
Definition: recording_connection.hpp:74
microstrain::connections::RecordingConnection::mSendFile
std::ostream * mSendFile
Definition: recording_connection.hpp:71
microstrain
Definition: embedded_time.h:8
microstrain::Connection::connect
virtual bool connect()=0