MythTV  master
zmserver.h
Go to the documentation of this file.
1 /* Definition of the ZMServer class.
2  * ============================================================
3  * This program is free software; you can redistribute it
4  * and/or modify it under the terms of the GNU General
5  * Public License as published bythe Free Software Foundation;
6  * either version 2, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * ============================================================ */
15 
16 #ifndef ZMSERVER_H
17 #define ZMSERVER_H
18 
19 #include <chrono>
20 #include <cstdint>
21 #include <map>
22 #include <mysql/mysql.h>
23 #include <sstream>
24 #include <string>
25 #include <unistd.h>
26 #include <vector>
27 using namespace std::chrono_literals;
28 using Clock = std::chrono::system_clock;
29 using TimePoint = std::chrono::time_point<Clock>;
30 
31 // the maximum image size we are ever likely to get from ZM
32 #define MAX_IMAGE_SIZE (2048*1536*3)
33 using FrameData = std::array<uint8_t,MAX_IMAGE_SIZE>;
34 
35 extern bool checkVersion(int major, int minor, int revision);
36 extern void loadZMConfig(const std::string &configfile);
37 extern void connectToDatabase(void);
38 extern void kickDatabase(bool debug);
39 
40 // these are shared by all ZMServer's
41 extern MYSQL g_dbConn;
42 extern std::string g_zmversion;
43 extern std::string g_password;
44 extern std::string g_server;
45 extern std::string g_database;
46 extern std::string g_webPath;
47 extern std::string g_user;
48 extern std::string g_webUser;
49 extern std::string g_binPath;
50 extern int g_majorVersion;
51 extern int g_minorVersion;
52 extern int g_revisionVersion;
53 
54 static constexpr std::chrono::seconds DB_CHECK_TIME { 60s };
55 extern TimePoint g_lastDBKick;
56 
57 const std::string FUNCTION_MONITOR = "Monitor";
58 const std::string FUNCTION_MODECT = "Modect";
59 const std::string FUNCTION_NODECT = "Nodect";
60 const std::string FUNCTION_RECORD = "Record";
61 const std::string FUNCTION_MOCORD = "Mocord";
62 const std::string FUNCTION_NONE = "None";
63 
64 const std::string RESTART = "restart";
65 const std::string RELOAD = "reload";
66 const std::string RUNNING = "running";
67 
68 enum State
69 {
75 };
76 
77 // Prevent clang-tidy modernize-avoid-c-arrays warnings in these
78 // library structures
79 extern "C" {
80 
81 // shared data for ZM version 1.24.x and 1.25.x
82 struct SharedData
83 {
84  int size;
85  bool valid;
86  bool active;
87  bool signal;
94  int action;
96  int hue;
97  int colour;
98  int contrast;
99  int alarm_x;
100  int alarm_y;
101  char control_state[256];
102 };
103 
104 // shared data for ZM version 1.26.x
106 {
107  uint32_t size;
109  uint32_t last_read_index;
110  uint32_t state;
111  uint32_t last_event;
112  uint32_t action;
113  int32_t brightness;
114  int32_t hue;
115  int32_t colour;
116  int32_t contrast;
117  int32_t alarm_x;
118  int32_t alarm_y;
119  uint8_t valid;
120  uint8_t active;
121  uint8_t signal;
122  uint8_t format;
123  uint32_t imagesize;
124  uint32_t epadding1;
125  uint32_t epadding2;
126  union {
128  uint64_t extrapad1;
129  };
130  union {
132  uint64_t extrapad2;
133  };
134  uint8_t control_state[256];
135 };
136 
137 // shared data for ZM version 1.32.x
139 {
140  uint32_t size;
142  uint32_t last_read_index;
143  uint32_t state;
144  uint64_t last_event;
145  uint32_t action;
146  int32_t brightness;
147  int32_t hue;
148  int32_t colour;
149  int32_t contrast;
150  int32_t alarm_x;
151  int32_t alarm_y;
152  uint8_t valid;
153  uint8_t active;
154  uint8_t signal;
155  uint8_t format;
156  uint32_t imagesize;
157  uint32_t epadding1;
158  union {
159  time_t startup_time;
160  uint64_t extrapad1;
161  };
162  union {
164  uint64_t extrapad2;
165  };
166  union {
168  uint64_t extrapad3;
169  };
170  uint8_t control_state[256];
171 
172  char alarm_cause[256];
173 };
174 
175 // shared data for ZM version 1.34.x
177 {
178  uint32_t size;
180  uint32_t last_read_index;
181  uint32_t state;
182  uint64_t last_event;
183  uint32_t action;
184  int32_t brightness;
185  int32_t hue;
186  int32_t colour;
187  int32_t contrast;
188  int32_t alarm_x;
189  int32_t alarm_y;
190  uint8_t valid;
191  uint8_t active;
192  uint8_t signal;
193  uint8_t format;
194  uint32_t imagesize;
195  uint32_t epadding1;
196  union {
197  time_t startup_time;
198  uint64_t extrapad1;
199  };
200  union { /* +72 */
201  time_t zmc_heartbeat_time; /* Constantly updated by zmc. Used to determine if the process is alive or hung or dead */
202  uint64_t extrapad2;
203  };
204  union { /* +80 */
205  time_t zma_heartbeat_time; /* Constantly updated by zma. Used to determine if the process is alive or hung or dead */
206  uint64_t extrapad3;
207  };
208  union {
210  uint64_t extrapad4;
211  };
212  union {
214  uint64_t extrapad5;
215  };
216  uint8_t control_state[256];
217 
218  char alarm_cause[256];
219 };
220 
221 
222 
224 
225 // Triggerdata for ZM version 1.24.x and 1.25.x
227 {
228  int size;
231  char trigger_cause[32];
232  char trigger_text[256];
233  char trigger_showtext[256];
234 };
235 
236 // Triggerdata for ZM version 1.26.x , 1.32.x and 1.34.x
238 {
239  uint32_t size;
240  uint32_t trigger_state;
241  uint32_t trigger_score;
242  uint32_t padding;
243  char trigger_cause[32];
244  char trigger_text[256];
245  char trigger_showtext[256];
246 };
247 
248 // VideoStoreData for ZM version 1.32.x and 1.34.x
250 {
251  uint32_t size;
252  uint64_t current_event;
253  char event_file[4096];
254  timeval recording;
255 };
256 
257 // end of library structures.
258 };
259 
260 class MONITOR
261 {
262  public:
263  MONITOR() = default;
264 
265  void initMonitor(bool debug, const std::string &mmapPath, int shmKey);
266 
267  bool isValid(void);
268 
269  std::string getIdStr(void);
270  int getLastWriteIndex(void);
271  int getSubpixelOrder(void);
272  int getState(void);
273  int getFrameSize(void);
274 
275  std::string m_name {};
276  std::string m_type {};
277  std::string m_function {};
278  int m_enabled {0};
279  std::string m_device {};
280  std::string m_host {};
281  int m_imageBufferCount {0};
282  int m_width {0};
283  int m_height {0};
284  int m_bytesPerPixel {3};
285  int m_monId {0};
286  unsigned char *m_sharedImages {nullptr};
287  int m_lastRead {0};
288  std::string m_status {};
289  int m_palette {0};
290  int m_controllable {0};
291  int m_trackMotion {0};
292  int m_mapFile {-1};
293  void *m_shmPtr {nullptr};
294  private:
295  SharedData *m_sharedData {nullptr};
296  SharedData26 *m_sharedData26 {nullptr};
297  SharedData32 *m_sharedData32 {nullptr};
298  SharedData34 *m_sharedData34 {nullptr};
299  std::string m_id {};
300 };
301 
302 class ZMServer
303 {
304  public:
305  ZMServer(int sock, bool debug);
306  ~ZMServer();
307 
308  bool processRequest(char* buf, int nbytes);
309 
310  private:
311  std::string getZMSetting(const std::string &setting) const;
312  bool send(const std::string &s) const;
313  bool send(const std::string &s, const unsigned char *buffer, int dataLen) const;
314  void sendError(const std::string &error);
315  void getMonitorList(void);
316  static int getFrame(FrameData &buffer, MONITOR *monitor);
317  static long long getDiskSpace(const std::string &filename, long long &total, long long &used);
318  static void tokenize(const std::string &command, std::vector<std::string> &tokens);
319  void handleHello(void);
320  static std::string runCommand(const std::string& command);
321  static void getMonitorStatus(const std::string &id, const std::string &type,
322  const std::string &device, const std::string &host,
323  const std::string &channel, const std::string &function,
324  std::string &zmcStatus, std::string &zmaStatus,
325  const std::string &enabled);
326  void handleGetServerStatus(void);
327  void handleGetMonitorStatus(void);
328  void handleGetAlarmStates(void);
329  void handleGetMonitorList(void);
330  void handleGetCameraList(void);
331  void handleGetEventList(std::vector<std::string> tokens);
332  void handleGetEventFrame(std::vector<std::string> tokens);
333  void handleGetAnalysisFrame(std::vector<std::string> tokens);
334  void handleGetLiveFrame(std::vector<std::string> tokens);
335  void handleGetFrameList(std::vector<std::string> tokens);
336  void handleDeleteEvent(std::vector<std::string> tokens);
337  void handleDeleteEventList(std::vector<std::string> tokens);
338  void handleGetEventDates(std::vector<std::string> tokens);
339  void handleRunZMAudit(void);
340  void handleSetMonitorFunction(std::vector<std::string> tokens);
341  void zmcControl(MONITOR *monitor, const std::string &mode);
342  void zmaControl(MONITOR *monitor, const std::string &mode);
343 
344  bool m_debug {false};
345  int m_sock {-1};
346  std::vector<MONITOR *> m_monitors;
347  std::map<int, MONITOR *> m_monitorMap;
348  bool m_useDeepStorage {false};
349  bool m_useAnalysisImages {false};
350  std::string m_eventFileFormat;
351  std::string m_analysisFileFormat;
352  key_t m_shmKey;
353  std::string m_mmapPath;
354 };
355 
356 
357 #endif
SharedData34::format
uint8_t format
Definition: zmserver.h:193
connectToDatabase
void connectToDatabase(void)
Definition: zmserver.cpp:176
ZMServer::m_shmKey
key_t m_shmKey
Definition: zmserver.h:352
SharedData26::imagesize
uint32_t imagesize
Definition: zmserver.h:123
SharedData34::state
uint32_t state
Definition: zmserver.h:181
SharedData32::last_read_time
time_t last_read_time
Definition: zmserver.h:167
TRIGGER_ON
@ TRIGGER_ON
Definition: zmserver.h:223
SharedData26::format
uint8_t format
Definition: zmserver.h:122
SharedData34::last_read_index
uint32_t last_read_index
Definition: zmserver.h:180
SharedData32::last_event
uint64_t last_event
Definition: zmserver.h:144
SharedData32::extrapad1
uint64_t extrapad1
Definition: zmserver.h:160
SharedData32::active
uint8_t active
Definition: zmserver.h:153
TRIGGER_CANCEL
@ TRIGGER_CANCEL
Definition: zmserver.h:223
SharedData::alarm_y
int alarm_y
Definition: zmserver.h:100
SharedData26::contrast
int32_t contrast
Definition: zmserver.h:116
SharedData::signal
bool signal
Definition: zmserver.h:87
error
static void error(const char *str,...)
Definition: vbi.cpp:42
SharedData32::alarm_x
int32_t alarm_x
Definition: zmserver.h:150
SharedData26::alarm_y
int32_t alarm_y
Definition: zmserver.h:118
IDLE
@ IDLE
Definition: zmserver.h:70
SharedData34::signal
uint8_t signal
Definition: zmserver.h:192
SharedData26::valid
uint8_t valid
Definition: zmserver.h:119
getDiskSpace
int64_t getDiskSpace(const QString &file_on_disk, int64_t &total, int64_t &used)
Definition: mythcoreutil.cpp:43
RELOAD
const std::string RELOAD
Definition: zmserver.h:65
ZMServer::m_analysisFileFormat
std::string m_analysisFileFormat
Definition: zmserver.h:351
SharedData32::epadding1
uint32_t epadding1
Definition: zmserver.h:157
SharedData26::action
uint32_t action
Definition: zmserver.h:112
VideoStoreData::current_event
uint64_t current_event
Definition: zmserver.h:252
SharedData34::zma_heartbeat_time
time_t zma_heartbeat_time
Definition: zmserver.h:205
SharedData26::last_read_time
time_t last_read_time
Definition: zmserver.h:131
TAPE
@ TAPE
Definition: zmserver.h:74
SharedData::state
State state
Definition: zmserver.h:88
FUNCTION_NONE
const std::string FUNCTION_NONE
Definition: zmserver.h:62
FUNCTION_MODECT
const std::string FUNCTION_MODECT
Definition: zmserver.h:58
Clock
std::chrono::system_clock Clock
Definition: zmserver.h:28
VideoStoreData::recording
timeval recording
Definition: zmserver.h:254
SharedData26::last_write_index
uint32_t last_write_index
Definition: zmserver.h:108
SharedData26::last_event
uint32_t last_event
Definition: zmserver.h:111
g_webUser
std::string g_webUser
Definition: zmserver.cpp:86
TimePoint
std::chrono::time_point< Clock > TimePoint
Definition: zmserver.h:29
TriggerData26::trigger_score
uint32_t trigger_score
Definition: zmserver.h:241
SharedData34::hue
int32_t hue
Definition: zmserver.h:185
PREALARM
@ PREALARM
Definition: zmserver.h:71
SharedData32::last_write_time
time_t last_write_time
Definition: zmserver.h:163
SharedData34::extrapad2
uint64_t extrapad2
Definition: zmserver.h:202
SharedData::last_event
int last_event
Definition: zmserver.h:93
g_database
std::string g_database
Definition: zmserver.cpp:83
FUNCTION_MONITOR
const std::string FUNCTION_MONITOR
Definition: zmserver.h:57
SharedData32::startup_time
time_t startup_time
Definition: zmserver.h:159
ZMServer
Definition: zmserver.h:302
DB_CHECK_TIME
static constexpr std::chrono::seconds DB_CHECK_TIME
Definition: zmserver.h:54
SharedData32::valid
uint8_t valid
Definition: zmserver.h:152
TriggerData::size
int size
Definition: zmserver.h:228
SharedData34::last_read_time
time_t last_read_time
Definition: zmserver.h:213
g_server
std::string g_server
Definition: zmserver.cpp:82
SharedData34::extrapad3
uint64_t extrapad3
Definition: zmserver.h:206
SharedData::last_read_index
int last_read_index
Definition: zmserver.h:90
VideoStoreData::size
uint32_t size
Definition: zmserver.h:251
SharedData32::hue
int32_t hue
Definition: zmserver.h:147
SharedData32::signal
uint8_t signal
Definition: zmserver.h:154
SharedData26::active
uint8_t active
Definition: zmserver.h:120
RUNNING
const std::string RUNNING
Definition: zmserver.h:66
SharedData::contrast
int contrast
Definition: zmserver.h:98
SharedData26::extrapad2
uint64_t extrapad2
Definition: zmserver.h:132
minor
#define minor(X)
Definition: compat.h:138
SharedData
Definition: zmserver.h:82
SharedData34::extrapad5
uint64_t extrapad5
Definition: zmserver.h:214
SharedData32::last_read_index
uint32_t last_read_index
Definition: zmserver.h:142
loadZMConfig
void loadZMConfig(const std::string &configfile)
Definition: zmserver.cpp:104
g_minorVersion
int g_minorVersion
Definition: zmserver.cpp:91
SharedData::brightness
int brightness
Definition: zmserver.h:95
SharedData26::colour
int32_t colour
Definition: zmserver.h:115
TriggerData::trigger_score
int trigger_score
Definition: zmserver.h:230
SharedData34::epadding1
uint32_t epadding1
Definition: zmserver.h:195
SharedData34::imagesize
uint32_t imagesize
Definition: zmserver.h:194
SharedData26::epadding1
uint32_t epadding1
Definition: zmserver.h:124
FUNCTION_RECORD
const std::string FUNCTION_RECORD
Definition: zmserver.h:60
debug
VERBOSE_PREAMBLE Most debug(nodatabase, notimestamp, noextra)") VERBOSE_MAP(VB_GENERAL
State
State
Definition: zmserver.h:68
SharedData34::zmc_heartbeat_time
time_t zmc_heartbeat_time
Definition: zmserver.h:201
SharedData32::extrapad3
uint64_t extrapad3
Definition: zmserver.h:168
SharedData::size
int size
Definition: zmserver.h:84
ZMServer::m_eventFileFormat
std::string m_eventFileFormat
Definition: zmserver.h:350
RESTART
const std::string RESTART
Definition: zmserver.h:64
SharedData::valid
bool valid
Definition: zmserver.h:85
ALERT
@ ALERT
Definition: zmserver.h:73
FUNCTION_NODECT
const std::string FUNCTION_NODECT
Definition: zmserver.h:59
VideoStoreData
Definition: zmserver.h:249
TRIGGER_OFF
@ TRIGGER_OFF
Definition: zmserver.h:223
SharedData26::alarm_x
int32_t alarm_x
Definition: zmserver.h:117
SharedData34::alarm_y
int32_t alarm_y
Definition: zmserver.h:189
SharedData34::contrast
int32_t contrast
Definition: zmserver.h:187
SharedData26::extrapad1
uint64_t extrapad1
Definition: zmserver.h:128
g_revisionVersion
int g_revisionVersion
Definition: zmserver.cpp:92
SharedData::action
int action
Definition: zmserver.h:94
SharedData34::active
uint8_t active
Definition: zmserver.h:191
SharedData32::action
uint32_t action
Definition: zmserver.h:145
SharedData26::epadding2
uint32_t epadding2
Definition: zmserver.h:125
g_user
std::string g_user
Definition: zmserver.cpp:85
g_binPath
std::string g_binPath
Definition: zmserver.cpp:87
checkVersion
bool checkVersion(int major, int minor, int revision)
Definition: zmserver.cpp:97
g_dbConn
MYSQL g_dbConn
Definition: zmserver.cpp:79
SharedData26::hue
int32_t hue
Definition: zmserver.h:114
SharedData26::last_read_index
uint32_t last_read_index
Definition: zmserver.h:109
TriggerData
Definition: zmserver.h:226
SharedData32::colour
int32_t colour
Definition: zmserver.h:148
g_webPath
std::string g_webPath
Definition: zmserver.cpp:84
SharedData::hue
int hue
Definition: zmserver.h:96
TriggerData26::size
uint32_t size
Definition: zmserver.h:239
SharedData34::extrapad1
uint64_t extrapad1
Definition: zmserver.h:198
FUNCTION_MOCORD
const std::string FUNCTION_MOCORD
Definition: zmserver.h:61
g_zmversion
std::string g_zmversion
Definition: zmserver.cpp:80
SharedData32::contrast
int32_t contrast
Definition: zmserver.h:149
SharedData32::format
uint8_t format
Definition: zmserver.h:155
SharedData34::last_write_time
time_t last_write_time
Definition: zmserver.h:209
TriggerData26::trigger_state
uint32_t trigger_state
Definition: zmserver.h:240
SharedData34::action
uint32_t action
Definition: zmserver.h:183
ZMServer::m_monitorMap
std::map< int, MONITOR * > m_monitorMap
Definition: zmserver.h:347
SharedData26::signal
uint8_t signal
Definition: zmserver.h:121
SharedData26::last_write_time
time_t last_write_time
Definition: zmserver.h:127
TriggerState
TriggerState
Definition: zmserver.h:223
SharedData34::size
uint32_t size
Definition: zmserver.h:178
SharedData::colour
int colour
Definition: zmserver.h:97
SharedData::last_write_time
time_t last_write_time
Definition: zmserver.h:91
SharedData34::brightness
int32_t brightness
Definition: zmserver.h:184
TriggerData::trigger_state
TriggerState trigger_state
Definition: zmserver.h:229
TriggerData26
Definition: zmserver.h:237
mythburn.runCommand
def runCommand(command)
Runs an external command checking to see if the user has cancelled the DVD creation process.
Definition: mythburn.py:503
SharedData34::extrapad4
uint64_t extrapad4
Definition: zmserver.h:210
SharedData32
Definition: zmserver.h:138
MONITOR
Definition: zmserver.h:260
ALARM
@ ALARM
Definition: zmserver.h:72
SharedData34
Definition: zmserver.h:176
g_password
std::string g_password
Definition: zmserver.cpp:81
SharedData32::brightness
int32_t brightness
Definition: zmserver.h:146
SharedData32::alarm_y
int32_t alarm_y
Definition: zmserver.h:151
ZMServer::m_mmapPath
std::string m_mmapPath
Definition: zmserver.h:353
FrameData
std::array< uint8_t, MAX_IMAGE_SIZE > FrameData
Definition: zmserver.h:33
SharedData26::size
uint32_t size
Definition: zmserver.h:107
SharedData34::alarm_x
int32_t alarm_x
Definition: zmserver.h:188
ZMServer::m_monitors
std::vector< MONITOR * > m_monitors
Definition: zmserver.h:346
SharedData::last_read_time
time_t last_read_time
Definition: zmserver.h:92
SharedData34::last_write_index
uint32_t last_write_index
Definition: zmserver.h:179
SharedData32::last_write_index
uint32_t last_write_index
Definition: zmserver.h:141
SharedData::active
bool active
Definition: zmserver.h:86
build_compdb.filename
filename
Definition: build_compdb.py:21
SharedData26
Definition: zmserver.h:105
TriggerData26::padding
uint32_t padding
Definition: zmserver.h:242
SharedData32::state
uint32_t state
Definition: zmserver.h:143
SharedData::last_write_index
int last_write_index
Definition: zmserver.h:89
SharedData32::extrapad2
uint64_t extrapad2
Definition: zmserver.h:164
SharedData26::state
uint32_t state
Definition: zmserver.h:110
g_majorVersion
int g_majorVersion
Definition: zmserver.cpp:90
SharedData34::valid
uint8_t valid
Definition: zmserver.h:190
SharedData26::brightness
int32_t brightness
Definition: zmserver.h:113
kickDatabase
void kickDatabase(bool debug)
Definition: zmserver.cpp:201
SharedData34::colour
int32_t colour
Definition: zmserver.h:186
SharedData34::startup_time
time_t startup_time
Definition: zmserver.h:197
SharedData::alarm_x
int alarm_x
Definition: zmserver.h:99
SharedData34::last_event
uint64_t last_event
Definition: zmserver.h:182
g_lastDBKick
TimePoint g_lastDBKick
Definition: zmserver.cpp:94
SharedData32::imagesize
uint32_t imagesize
Definition: zmserver.h:156
SharedData32::size
uint32_t size
Definition: zmserver.h:140