MythTV  master
MythExternControl.h
Go to the documentation of this file.
1 /* -*- Mode: c++ -*-
2  *
3  * Copyright (C) John Poet 2018
4  *
5  * This file is part of MythTV
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef MYTHEXTERNCONTROL_H
22 #define MYTHEXTERNCONTROL_H
23 
24 #include "MythExternRecApp.h"
25 
26 #include <atomic>
27 #include <vector>
28 #include <queue>
29 #include <condition_variable>
30 #include <thread>
31 
32 #include <QString>
33 
34 class MythExternControl;
35 
36 class Buffer : QObject
37 {
38  Q_OBJECT
39 
40  public:
41  static constexpr uint16_t kMaxQueue { 500 };
42 
43  explicit Buffer(MythExternControl * parent);
44  ~Buffer(void) override = default;
45  void Start(void) {
46  m_thread = std::thread(&Buffer::Run, this);
47  }
48  void Join(void) {
49  if (m_thread.joinable())
50  m_thread.join();
51  }
52  bool Fill(const QByteArray & buffer);
53 
54  std::chrono::time_point<std::chrono::system_clock> HeartBeat(void) const
55  { return m_heartbeat; }
56 
57  protected:
58  void Run(void);
59 
60  private:
61  using block_t = std::vector<uint8_t>;
62  using stack_t = std::queue<block_t>;
63 
65 
66  std::thread m_thread;
67 
69  bool m_dataSeen {false};
70 
71  std::chrono::time_point<std::chrono::system_clock> m_heartbeat;
72 };
73 
74 class Commands : public QObject
75 {
76  Q_OBJECT
77 
78  public:
79  explicit Commands(MythExternControl * parent)
80  : m_parent(parent) {}
81  ~Commands(void) override = default;
82  void Start(void) {
83  m_thread = std::thread(&Commands::Run, this);
84  }
85  void Join(void) {
86  if (m_thread.joinable())
87  m_thread.join();
88  }
89 
90  bool SendStatus(const QString & command,
91  const QString & status,
92  const QString & serial,
93  const QString & response = "");
94  bool ProcessCommand(const QString & query);
95 
96  protected:
97  void Run(void);
98  bool Open(void);
99  void Close(void);
100  void StartStreaming(const QString & serial);
101  void StopStreaming(const QString & serial, bool silent);
102  void LockTimeout(const QString & serial) const;
103  void HasTuner(const QString & serial) const;
104  void HasPictureAttributes(const QString & serial) const;
105  void SetBlockSize(const QString & serial, int blksz);
106  void TuneChannel(const QString & serial, const QVariantMap & args);
107  void TuneStatus(const QString & serial);
108  void LoadChannels(const QString & serial);
109  void FirstChannel(const QString & serial);
110  void NextChannel(const QString & serial);
111  void Cleanup(void);
112 
113  private:
114  std::thread m_thread;
115 
116  size_t m_repCmdCnt { 0 };
117  QString m_prevStatus;
118  QString m_prevMsgBuf;
119 
121  int m_apiVersion { -1 };
122 };
123 
124 class MythExternControl : public QObject
125 {
126  Q_OBJECT
127 
128  friend class Buffer;
129  friend class Commands;
130 
131  public:
132  MythExternControl(void);
133  ~MythExternControl(void) override;
134 
135  QString Desc(void) const { return QString("%1: ").arg(m_desc); }
136 
137  void Terminate(void);
138 
139  void Error(const QString & msg);
140  void Fatal(const QString & msg);
141 
142  QString ErrorString(void) const { return m_errmsg; }
143  void ClearError(void) { m_errmsg.clear(); }
144 
145  signals:
146  void Open(void);
147  void Close(void);
148  void StartStreaming(const QString & serial);
149  void StopStreaming(const QString & serial, bool silent);
150  void LockTimeout(const QString & serial);
151  void HasTuner(const QString & serial);
152  void HasPictureAttributes(const QString & serial);
153  void SetBlockSize(const QString & serial, int blksz);
154  void TuneChannel(const QString & serial, const QVariantMap & args);
155  void TuneStatus(const QString & serial);
156  void LoadChannels(const QString & serial);
157  void FirstChannel(const QString & serial);
158  void NextChannel(const QString & serial);
159  void Cleanup(void);
160  void DataStarted(void);
161 
162  public slots:
163  void SetDescription(const QString & desc) { m_desc = desc; }
164  void SendMessage(const QString & command,
165  const QString & serial,
166  const QString & message,
167  const QString & status = "");
168 
169  void ErrorMessage(const QString & msg);
170  void Opened(void);
171  void Done(void);
172  void Streaming(bool val);
173  void Fill(const QByteArray & buffer) { m_buffer.Fill(buffer); }
174 
175  protected:
178  QString m_desc;
179 
180  std::atomic<bool> m_run {true};
181  std::atomic<bool> m_commandsRunning {true};
182  std::atomic<bool> m_bufferRunning {true};
183  std::mutex m_runMutex;
184  std::condition_variable m_runCond;
185  std::mutex m_msgMutex;
186 
187  bool m_fatal {false};
188  QString m_errmsg;
189 
190  std::mutex m_flowMutex;
191  std::condition_variable m_flowCond;
192  std::atomic<bool> m_streaming {false};
193  std::atomic<bool> m_xon {false};
194  std::atomic<bool> m_ready {false};
195 };
196 
197 #endif // MYTHEXTERNCONTROL_H
build_compdb.args
args
Definition: build_compdb.py:11
Buffer::HeartBeat
std::chrono::time_point< std::chrono::system_clock > HeartBeat(void) const
Definition: MythExternControl.h:54
MythExternControl::Cleanup
void Cleanup(void)
Commands::m_thread
std::thread m_thread
Definition: MythExternControl.h:114
Commands::Start
void Start(void)
Definition: MythExternControl.h:82
Commands::TuneChannel
void TuneChannel(const QString &serial, const QVariantMap &args)
Definition: MythExternControl.cpp:175
MythExternControl::Fill
void Fill(const QByteArray &buffer)
Definition: MythExternControl.h:173
Commands::TuneStatus
void TuneStatus(const QString &serial)
Definition: MythExternControl.cpp:180
MythExternControl::m_fatal
bool m_fatal
Definition: MythExternControl.h:187
MythExternControl::m_desc
QString m_desc
Definition: MythExternControl.h:178
MythExternControl::~MythExternControl
~MythExternControl(void) override
Definition: MythExternControl.cpp:51
MythExternControl::m_xon
std::atomic< bool > m_xon
Definition: MythExternControl.h:193
MythExternControl::FirstChannel
void FirstChannel(const QString &serial)
Commands::m_prevMsgBuf
QString m_prevMsgBuf
Definition: MythExternControl.h:118
Buffer::Join
void Join(void)
Definition: MythExternControl.h:48
Commands::NextChannel
void NextChannel(const QString &serial)
Definition: MythExternControl.cpp:195
Commands::SendStatus
bool SendStatus(const QString &command, const QString &status, const QString &serial, const QString &response="")
Definition: MythExternControl.cpp:205
Commands::Commands
Commands(MythExternControl *parent)
Definition: MythExternControl.h:79
MythExternControl::m_ready
std::atomic< bool > m_ready
Definition: MythExternControl.h:194
MythExternControl::m_run
std::atomic< bool > m_run
Definition: MythExternControl.h:180
Buffer::~Buffer
~Buffer(void) override=default
Buffer::m_dataSeen
bool m_dataSeen
Definition: MythExternControl.h:69
MythExternControl::HasTuner
void HasTuner(const QString &serial)
Buffer::Start
void Start(void)
Definition: MythExternControl.h:45
Commands::~Commands
~Commands(void) override=default
Commands::Open
bool Open(void)
Commands::m_apiVersion
int m_apiVersion
Definition: MythExternControl.h:121
MythExternControl::TuneChannel
void TuneChannel(const QString &serial, const QVariantMap &args)
MythExternControl::m_errmsg
QString m_errmsg
Definition: MythExternControl.h:188
MythExternControl::Opened
void Opened(void)
Definition: MythExternControl.cpp:58
MythExternControl::LockTimeout
void LockTimeout(const QString &serial)
Commands::Run
void Run(void)
Definition: MythExternControl.cpp:443
MythExternRecApp.h
MythExternControl::Error
void Error(const QString &msg)
Definition: MythExternControl.cpp:97
Commands::Close
void Close(void)
Definition: MythExternControl.cpp:136
MythExternControl::Streaming
void Streaming(bool val)
Definition: MythExternControl.cpp:66
Commands::StartStreaming
void StartStreaming(const QString &serial)
Definition: MythExternControl.cpp:145
Commands::m_prevStatus
QString m_prevStatus
Definition: MythExternControl.h:117
MythExternControl::m_streaming
std::atomic< bool > m_streaming
Definition: MythExternControl.h:192
Commands::HasTuner
void HasTuner(const QString &serial) const
Definition: MythExternControl.cpp:160
Buffer::m_data
stack_t m_data
Definition: MythExternControl.h:68
Commands::LoadChannels
void LoadChannels(const QString &serial)
Definition: MythExternControl.cpp:185
MythExternControl::m_flowMutex
std::mutex m_flowMutex
Definition: MythExternControl.h:190
MythExternControl::Fatal
void Fatal(const QString &msg)
Definition: MythExternControl.cpp:108
MythExternControl::MythExternControl
MythExternControl(void)
Definition: MythExternControl.cpp:41
Commands::LockTimeout
void LockTimeout(const QString &serial) const
Definition: MythExternControl.cpp:155
MythExternControl::m_runCond
std::condition_variable m_runCond
Definition: MythExternControl.h:184
MythExternControl::m_flowCond
std::condition_variable m_flowCond
Definition: MythExternControl.h:191
Buffer::m_parent
MythExternControl * m_parent
Definition: MythExternControl.h:64
MythExternControl::m_msgMutex
std::mutex m_msgMutex
Definition: MythExternControl.h:185
Commands::Cleanup
void Cleanup(void)
Definition: MythExternControl.cpp:200
MythExternControl::StopStreaming
void StopStreaming(const QString &serial, bool silent)
Commands::HasPictureAttributes
void HasPictureAttributes(const QString &serial) const
Definition: MythExternControl.cpp:165
MythExternControl::ErrorString
QString ErrorString(void) const
Definition: MythExternControl.h:142
Commands::m_repCmdCnt
size_t m_repCmdCnt
Definition: MythExternControl.h:116
MythExternControl::TuneStatus
void TuneStatus(const QString &serial)
MythExternControl
Definition: MythExternControl.h:124
Buffer::m_thread
std::thread m_thread
Definition: MythExternControl.h:66
MythExternControl::NextChannel
void NextChannel(const QString &serial)
MythExternControl::m_runMutex
std::mutex m_runMutex
Definition: MythExternControl.h:183
Buffer::Buffer
Buffer(MythExternControl *parent)
Definition: MythExternControl.cpp:510
Buffer
Definition: MythExternControl.h:36
MythExternControl::m_bufferRunning
std::atomic< bool > m_bufferRunning
Definition: MythExternControl.h:182
Commands::Join
void Join(void)
Definition: MythExternControl.h:85
MythExternControl::DataStarted
void DataStarted(void)
MythExternControl::StartStreaming
void StartStreaming(const QString &serial)
Commands
Definition: MythExternControl.h:74
MythExternControl::Close
void Close(void)
Commands::SetBlockSize
void SetBlockSize(const QString &serial, int blksz)
Definition: MythExternControl.cpp:170
MythExternControl::Done
void Done(void)
Definition: MythExternControl.cpp:77
MythExternControl::LoadChannels
void LoadChannels(const QString &serial)
Buffer::m_heartbeat
std::chrono::time_point< std::chrono::system_clock > m_heartbeat
Definition: MythExternControl.h:71
MythExternControl::SetBlockSize
void SetBlockSize(const QString &serial, int blksz)
MythExternControl::Open
void Open(void)
MythExternControl::SetDescription
void SetDescription(const QString &desc)
Definition: MythExternControl.h:163
MythExternControl::ClearError
void ClearError(void)
Definition: MythExternControl.h:143
Buffer::kMaxQueue
static constexpr uint16_t kMaxQueue
Definition: MythExternControl.h:41
Buffer::stack_t
std::queue< block_t > stack_t
Definition: MythExternControl.h:62
Buffer::Fill
bool Fill(const QByteArray &buffer)
Definition: MythExternControl.cpp:516
Commands::m_parent
MythExternControl * m_parent
Definition: MythExternControl.h:120
MythExternControl::m_commandsRunning
std::atomic< bool > m_commandsRunning
Definition: MythExternControl.h:181
uint16_t
unsigned short uint16_t
Definition: iso6937tables.h:3
MythExternControl::HasPictureAttributes
void HasPictureAttributes(const QString &serial)
MythExternControl::Terminate
void Terminate(void)
Definition: MythExternControl.cpp:72
MythExternControl::Desc
QString Desc(void) const
Definition: MythExternControl.h:135
Buffer::Run
void Run(void)
Definition: MythExternControl.cpp:562
Commands::StopStreaming
void StopStreaming(const QString &serial, bool silent)
Definition: MythExternControl.cpp:150
Commands::FirstChannel
void FirstChannel(const QString &serial)
Definition: MythExternControl.cpp:190
MythExternControl::m_buffer
Buffer m_buffer
Definition: MythExternControl.h:176
Buffer::block_t
std::vector< uint8_t > block_t
Definition: MythExternControl.h:61
MythExternControl::ErrorMessage
void ErrorMessage(const QString &msg)
Definition: MythExternControl.cpp:124
MythExternControl::SendMessage
void SendMessage(const QString &command, const QString &serial, const QString &message, const QString &status="")
Definition: MythExternControl.cpp:115
Commands::ProcessCommand
bool ProcessCommand(const QString &query)
Definition: MythExternControl.cpp:268
MythExternControl::m_commands
Commands m_commands
Definition: MythExternControl.h:177