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  , m_apiVersion(-1) {}
82  ~Commands(void) override = default;
83  void Start(void) {
84  m_thread = std::thread(&Commands::Run, this);
85  }
86  void Join(void) {
87  if (m_thread.joinable())
88  m_thread.join();
89  }
90 
91  bool SendStatus(const QString & command, const QString & status);
92  bool SendStatus(const QString & command, const QString & serial,
93  const QString & status);
94  bool ProcessCommand(const QString & cmd);
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 QString & channum);
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 
118 };
119 
120 class MythExternControl : public QObject
121 {
122  Q_OBJECT
123 
124  friend class Buffer;
125  friend class Commands;
126 
127  public:
128  MythExternControl(void);
129  ~MythExternControl(void) override;
130 
131  QString Desc(void) const { return QString("%1: ").arg(m_desc); }
132 
133  void Terminate(void);
134 
135  void Error(const QString & msg);
136  void Fatal(const QString & msg);
137 
138  QString ErrorString(void) const { return m_errmsg; }
139  void ClearError(void) { m_errmsg.clear(); }
140 
141  signals:
142  void Open(void);
143  void Close(void);
144  void StartStreaming(const QString & serial);
145  void StopStreaming(const QString & serial, bool silent);
146  void LockTimeout(const QString & serial) const;
147  void HasTuner(const QString & serial) const;
148  void HasPictureAttributes(const QString & serial) const;
149  void SetBlockSize(const QString & serial, int blksz);
150  void TuneChannel(const QString & serial, const QString & channum);
151  void TuneStatus(const QString & serial);
152  void LoadChannels(const QString & serial);
153  void FirstChannel(const QString & serial);
154  void NextChannel(const QString & serial);
155  void Cleanup(void);
156  void DataStarted(void);
157 
158  public slots:
159  void SetDescription(const QString & desc) { m_desc = desc; }
160  void SendMessage(const QString & cmd, const QString & serial,
161  const QString & msg);
162  void ErrorMessage(const QString & msg);
163  void Opened(void);
164  void Done(void);
165  void Streaming(bool val);
166  void Fill(const QByteArray & buffer) { m_buffer.Fill(buffer); }
167 
168  protected:
171  QString m_desc;
172 
173  std::atomic<bool> m_run {true};
174  std::atomic<bool> m_commands_running {true};
175  std::atomic<bool> m_buffer_running {true};
176  std::mutex m_run_mutex;
177  std::condition_variable m_run_cond;
178  std::mutex m_msg_mutex;
179 
180  bool m_fatal {false};
181  QString m_errmsg;
182 
183  std::mutex m_flow_mutex;
184  std::condition_variable m_flow_cond;
185  std::atomic<bool> m_streaming {false};
186  std::atomic<bool> m_xon {false};
187  std::atomic<bool> m_ready {false};
188 };
189 
190 #endif // MYTHEXTERNCONTROL_H
MythExternControl::HasTuner
void HasTuner(const QString &serial) const
Buffer::HeartBeat
std::chrono::time_point< std::chrono::system_clock > HeartBeat(void) const
Definition: MythExternControl.h:54
MythExternControl::Cleanup
void Cleanup(void)
MythExternControl::m_buffer_running
std::atomic< bool > m_buffer_running
Definition: MythExternControl.h:175
Commands::m_thread
std::thread m_thread
Definition: MythExternControl.h:114
Commands::Start
void Start(void)
Definition: MythExternControl.h:83
MythExternControl::Fill
void Fill(const QByteArray &buffer)
Definition: MythExternControl.h:166
Commands::TuneStatus
void TuneStatus(const QString &serial)
Definition: MythExternControl.cpp:176
MythExternControl::m_fatal
bool m_fatal
Definition: MythExternControl.h:180
MythExternControl::m_desc
QString m_desc
Definition: MythExternControl.h:171
MythExternControl::~MythExternControl
~MythExternControl(void) override
Definition: MythExternControl.cpp:48
MythExternControl::m_xon
std::atomic< bool > m_xon
Definition: MythExternControl.h:186
MythExternControl::FirstChannel
void FirstChannel(const QString &serial)
MythExternControl::m_flow_cond
std::condition_variable m_flow_cond
Definition: MythExternControl.h:184
Buffer::Join
void Join(void)
Definition: MythExternControl.h:48
Commands::NextChannel
void NextChannel(const QString &serial)
Definition: MythExternControl.cpp:191
Commands::TuneChannel
void TuneChannel(const QString &serial, const QString &channum)
Definition: MythExternControl.cpp:171
slots
unsigned int slots[4]
Definition: element.cpp:38
Commands::Commands
Commands(MythExternControl *parent)
Definition: MythExternControl.h:79
MythExternControl::m_ready
std::atomic< bool > m_ready
Definition: MythExternControl.h:187
MythExternControl::m_run
std::atomic< bool > m_run
Definition: MythExternControl.h:173
Buffer::~Buffer
~Buffer(void) override=default
Buffer::m_dataSeen
bool m_dataSeen
Definition: MythExternControl.h:69
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:117
MythExternControl::m_errmsg
QString m_errmsg
Definition: MythExternControl.h:181
MythExternControl::Opened
void Opened(void)
Definition: MythExternControl.cpp:55
Commands::Run
void Run(void)
Definition: MythExternControl.cpp:437
MythExternControl::m_flow_mutex
std::mutex m_flow_mutex
Definition: MythExternControl.h:183
MythExternRecApp.h
Buffer::constants
constants
Definition: MythExternControl.h:41
MythExternControl::Error
void Error(const QString &msg)
Definition: MythExternControl.cpp:94
Commands::Close
void Close(void)
Definition: MythExternControl.cpp:132
MythExternControl::Streaming
void Streaming(bool val)
Definition: MythExternControl.cpp:63
Commands::StartStreaming
void StartStreaming(const QString &serial)
Definition: MythExternControl.cpp:141
MythExternControl::m_streaming
std::atomic< bool > m_streaming
Definition: MythExternControl.h:185
Commands::HasTuner
void HasTuner(const QString &serial) const
Definition: MythExternControl.cpp:156
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:181
MythExternControl::Fatal
void Fatal(const QString &msg)
Definition: MythExternControl.cpp:105
MythExternControl::MythExternControl
MythExternControl(void)
Definition: MythExternControl.cpp:38
Commands::LockTimeout
void LockTimeout(const QString &serial) const
Definition: MythExternControl.cpp:151
MythExternControl::HasPictureAttributes
void HasPictureAttributes(const QString &serial) const
Buffer::m_parent
MythExternControl * m_parent
Definition: MythExternControl.h:64
Commands::Cleanup
void Cleanup(void)
Definition: MythExternControl.cpp:196
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:161
MythExternControl::ErrorString
QString ErrorString(void) const
Definition: MythExternControl.h:138
MythExternControl::TuneStatus
void TuneStatus(const QString &serial)
MythExternControl
Definition: MythExternControl.h:120
Buffer::m_thread
std::thread m_thread
Definition: MythExternControl.h:66
MythExternControl::NextChannel
void NextChannel(const QString &serial)
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:112
Buffer
Definition: MythExternControl.h:36
Commands::Join
void Join(void)
Definition: MythExternControl.h:86
MythExternControl::DataStarted
void DataStarted(void)
MythExternControl::StartStreaming
void StartStreaming(const QString &serial)
Commands
Definition: MythExternControl.h:74
MythExternControl::LockTimeout
void LockTimeout(const QString &serial) const
MythExternControl::Close
void Close(void)
MythExternControl::m_msg_mutex
std::mutex m_msg_mutex
Definition: MythExternControl.h:178
Commands::SetBlockSize
void SetBlockSize(const QString &serial, int blksz)
Definition: MythExternControl.cpp:166
MythExternControl::Done
void Done(void)
Definition: MythExternControl.cpp:74
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:159
MythExternControl::ClearError
void ClearError(void)
Definition: MythExternControl.h:139
Commands::SendStatus
bool SendStatus(const QString &command, const QString &status)
Definition: MythExternControl.cpp:201
Buffer::stack_t
std::queue< block_t > stack_t
Definition: MythExternControl.h:62
MythExternControl::m_run_mutex
std::mutex m_run_mutex
Definition: MythExternControl.h:176
MythExternControl::m_run_cond
std::condition_variable m_run_cond
Definition: MythExternControl.h:177
Buffer::Fill
bool Fill(const QByteArray &buffer)
Definition: MythExternControl.cpp:511
Commands::m_parent
MythExternControl * m_parent
Definition: MythExternControl.h:116
Commands::ProcessCommand
bool ProcessCommand(const QString &cmd)
Definition: MythExternControl.cpp:251
MythExternControl::Terminate
void Terminate(void)
Definition: MythExternControl.cpp:69
MythExternControl::Desc
QString Desc(void) const
Definition: MythExternControl.h:131
Buffer::Run
void Run(void)
Definition: MythExternControl.cpp:557
MythExternControl::m_commands_running
std::atomic< bool > m_commands_running
Definition: MythExternControl.h:174
Commands::StopStreaming
void StopStreaming(const QString &serial, bool silent)
Definition: MythExternControl.cpp:146
Commands::FirstChannel
void FirstChannel(const QString &serial)
Definition: MythExternControl.cpp:186
MythExternControl::m_buffer
Buffer m_buffer
Definition: MythExternControl.h:169
Buffer::block_t
std::vector< uint8_t > block_t
Definition: MythExternControl.h:61
MythExternControl::ErrorMessage
void ErrorMessage(const QString &msg)
Definition: MythExternControl.cpp:120
MythExternControl::m_commands
Commands m_commands
Definition: MythExternControl.h:170