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  enum constants {MAX_QUEUE = 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, const QString & status);
91  bool SendStatus(const QString & command, const QString & serial,
92  const QString & status);
93  bool ProcessCommand(const QString & cmd);
94 
95  protected:
96  void Run(void);
97  bool Open(void);
98  void Close(void);
99  void StartStreaming(const QString & serial);
100  void StopStreaming(const QString & serial, bool silent);
101  void LockTimeout(const QString & serial) const;
102  void HasTuner(const QString & serial) const;
103  void HasPictureAttributes(const QString & serial) const;
104  void SetBlockSize(const QString & serial, int blksz);
105  void TuneChannel(const QString & serial, const QString & channum);
106  void TuneStatus(const QString & serial);
107  void LoadChannels(const QString & serial);
108  void FirstChannel(const QString & serial);
109  void NextChannel(const QString & serial);
110  void Cleanup(void);
111 
112  private:
113  std::thread m_thread;
114 
116  int m_apiVersion { -1 };
117 };
118 
119 class MythExternControl : public QObject
120 {
121  Q_OBJECT
122 
123  friend class Buffer;
124  friend class Commands;
125 
126  public:
127  MythExternControl(void);
128  ~MythExternControl(void) override;
129 
130  QString Desc(void) const { return QString("%1: ").arg(m_desc); }
131 
132  void Terminate(void);
133 
134  void Error(const QString & msg);
135  void Fatal(const QString & msg);
136 
137  QString ErrorString(void) const { return m_errmsg; }
138  void ClearError(void) { m_errmsg.clear(); }
139 
140  signals:
141  void Open(void);
142  void Close(void);
143  void StartStreaming(const QString & serial);
144  void StopStreaming(const QString & serial, bool silent);
145  void LockTimeout(const QString & serial);
146  void HasTuner(const QString & serial);
147  void HasPictureAttributes(const QString & serial);
148  void SetBlockSize(const QString & serial, int blksz);
149  void TuneChannel(const QString & serial, const QString & channum);
150  void TuneStatus(const QString & serial);
151  void LoadChannels(const QString & serial);
152  void FirstChannel(const QString & serial);
153  void NextChannel(const QString & serial);
154  void Cleanup(void);
155  void DataStarted(void);
156 
157  public slots:
158  void SetDescription(const QString & desc) { m_desc = desc; }
159  void SendMessage(const QString & cmd, const QString & serial,
160  const QString & msg);
161  void ErrorMessage(const QString & msg);
162  void Opened(void);
163  void Done(void);
164  void Streaming(bool val);
165  void Fill(const QByteArray & buffer) { m_buffer.Fill(buffer); }
166 
167  protected:
170  QString m_desc;
171 
172  std::atomic<bool> m_run {true};
173  std::atomic<bool> m_commandsRunning {true};
174  std::atomic<bool> m_bufferRunning {true};
175  std::mutex m_runMutex;
176  std::condition_variable m_runCond;
177  std::mutex m_msgMutex;
178 
179  bool m_fatal {false};
180  QString m_errmsg;
181 
182  std::mutex m_flowMutex;
183  std::condition_variable m_flowCond;
184  std::atomic<bool> m_streaming {false};
185  std::atomic<bool> m_xon {false};
186  std::atomic<bool> m_ready {false};
187 };
188 
189 #endif // MYTHEXTERNCONTROL_H
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:113
Commands::Start
void Start(void)
Definition: MythExternControl.h:82
MythExternControl::Fill
void Fill(const QByteArray &buffer)
Definition: MythExternControl.h:165
Commands::TuneStatus
void TuneStatus(const QString &serial)
Definition: MythExternControl.cpp:177
MythExternControl::m_fatal
bool m_fatal
Definition: MythExternControl.h:179
MythExternControl::m_desc
QString m_desc
Definition: MythExternControl.h:170
MythExternControl::~MythExternControl
~MythExternControl(void) override
Definition: MythExternControl.cpp:49
MythExternControl::m_xon
std::atomic< bool > m_xon
Definition: MythExternControl.h:185
MythExternControl::FirstChannel
void FirstChannel(const QString &serial)
Buffer::Join
void Join(void)
Definition: MythExternControl.h:48
Commands::NextChannel
void NextChannel(const QString &serial)
Definition: MythExternControl.cpp:192
Commands::TuneChannel
void TuneChannel(const QString &serial, const QString &channum)
Definition: MythExternControl.cpp:172
Commands::Commands
Commands(MythExternControl *parent)
Definition: MythExternControl.h:79
MythExternControl::m_ready
std::atomic< bool > m_ready
Definition: MythExternControl.h:186
MythExternControl::m_run
std::atomic< bool > m_run
Definition: MythExternControl.h:172
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:116
MythExternControl::m_errmsg
QString m_errmsg
Definition: MythExternControl.h:180
MythExternControl::Opened
void Opened(void)
Definition: MythExternControl.cpp:56
MythExternControl::LockTimeout
void LockTimeout(const QString &serial)
Commands::Run
void Run(void)
Definition: MythExternControl.cpp:438
MythExternRecApp.h
Buffer::constants
constants
Definition: MythExternControl.h:41
MythExternControl::Error
void Error(const QString &msg)
Definition: MythExternControl.cpp:95
Commands::Close
void Close(void)
Definition: MythExternControl.cpp:133
MythExternControl::Streaming
void Streaming(bool val)
Definition: MythExternControl.cpp:64
Commands::StartStreaming
void StartStreaming(const QString &serial)
Definition: MythExternControl.cpp:142
MythExternControl::m_streaming
std::atomic< bool > m_streaming
Definition: MythExternControl.h:184
Commands::HasTuner
void HasTuner(const QString &serial) const
Definition: MythExternControl.cpp:157
MythExternControl::TuneChannel
void TuneChannel(const QString &serial, const QString &channum)
Buffer::m_data
stack_t m_data
Definition: MythExternControl.h:68
Commands::LoadChannels
void LoadChannels(const QString &serial)
Definition: MythExternControl.cpp:182
MythExternControl::m_flowMutex
std::mutex m_flowMutex
Definition: MythExternControl.h:182
MythExternControl::Fatal
void Fatal(const QString &msg)
Definition: MythExternControl.cpp:106
MythExternControl::MythExternControl
MythExternControl(void)
Definition: MythExternControl.cpp:39
Commands::LockTimeout
void LockTimeout(const QString &serial) const
Definition: MythExternControl.cpp:152
MythExternControl::m_runCond
std::condition_variable m_runCond
Definition: MythExternControl.h:176
MythExternControl::m_flowCond
std::condition_variable m_flowCond
Definition: MythExternControl.h:183
Buffer::m_parent
MythExternControl * m_parent
Definition: MythExternControl.h:64
MythExternControl::m_msgMutex
std::mutex m_msgMutex
Definition: MythExternControl.h:177
Commands::Cleanup
void Cleanup(void)
Definition: MythExternControl.cpp:197
Buffer::MAX_QUEUE
@ MAX_QUEUE
Definition: MythExternControl.h:41
MythExternControl::StopStreaming
void StopStreaming(const QString &serial, bool silent)
Commands::HasPictureAttributes
void HasPictureAttributes(const QString &serial) const
Definition: MythExternControl.cpp:162
MythExternControl::ErrorString
QString ErrorString(void) const
Definition: MythExternControl.h:137
MythExternControl::TuneStatus
void TuneStatus(const QString &serial)
MythExternControl
Definition: MythExternControl.h:119
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:175
Buffer::Buffer
Buffer(MythExternControl *parent)
Definition: MythExternControl.cpp:505
MythExternControl::SendMessage
void SendMessage(const QString &cmd, const QString &serial, const QString &msg)
Definition: MythExternControl.cpp:113
Buffer
Definition: MythExternControl.h:36
MythExternControl::m_bufferRunning
std::atomic< bool > m_bufferRunning
Definition: MythExternControl.h:174
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:167
MythExternControl::Done
void Done(void)
Definition: MythExternControl.cpp:75
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:158
MythExternControl::ClearError
void ClearError(void)
Definition: MythExternControl.h:138
Commands::SendStatus
bool SendStatus(const QString &command, const QString &status)
Definition: MythExternControl.cpp:202
Buffer::stack_t
std::queue< block_t > stack_t
Definition: MythExternControl.h:62
Buffer::Fill
bool Fill(const QByteArray &buffer)
Definition: MythExternControl.cpp:511
Commands::m_parent
MythExternControl * m_parent
Definition: MythExternControl.h:115
MythExternControl::m_commandsRunning
std::atomic< bool > m_commandsRunning
Definition: MythExternControl.h:173
Commands::ProcessCommand
bool ProcessCommand(const QString &cmd)
Definition: MythExternControl.cpp:252
MythExternControl::HasPictureAttributes
void HasPictureAttributes(const QString &serial)
MythExternControl::Terminate
void Terminate(void)
Definition: MythExternControl.cpp:70
MythExternControl::Desc
QString Desc(void) const
Definition: MythExternControl.h:130
Buffer::Run
void Run(void)
Definition: MythExternControl.cpp:557
Commands::StopStreaming
void StopStreaming(const QString &serial, bool silent)
Definition: MythExternControl.cpp:147
Commands::FirstChannel
void FirstChannel(const QString &serial)
Definition: MythExternControl.cpp:187
MythExternControl::m_buffer
Buffer m_buffer
Definition: MythExternControl.h:168
Buffer::block_t
std::vector< uint8_t > block_t
Definition: MythExternControl.h:61
MythExternControl::ErrorMessage
void ErrorMessage(const QString &msg)
Definition: MythExternControl.cpp:121
MythExternControl::m_commands
Commands m_commands
Definition: MythExternControl.h:169