forked from g4klx/MMDVM-Display
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextionUpdater.cpp
More file actions
388 lines (295 loc) · 8.55 KB
/
Copy pathNextionUpdater.cpp
File metadata and controls
388 lines (295 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Copyright (C) 2015-2023,2025 by Jonathan Naylor G4KLX
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "NextionUpdater.h"
#include "ModemSerialPort.h"
#include "MQTTConnection.h"
#include "UARTController.h"
#include "StopWatch.h"
#include "Version.h"
#include "Defines.h"
#include "Thread.h"
#include "Utils.h"
#include "Conf.h"
#include "Log.h"
#include "GitVersion.h"
#include <cstdio>
#include <vector>
#include <cstdlib>
#if !defined(_WIN32) && !defined(_WIN64)
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <pwd.h>
#endif
#if defined(_WIN32) || defined(_WIN64)
const char* DEFAULT_INI_FILE = "NextionUpdater.ini";
#else
const char* DEFAULT_INI_FILE = "/etc/NextionUpdater.ini";
#endif
// In Log.cpp
extern CMQTTConnection* m_mqtt;
CNextionUpdater* driver = nullptr;
#if defined(_WIN32) || defined(_WIN64)
char* optarg = nullptr;
int optind = 1;
int getopt(int argc, char* const argv[], const char* optstring)
{
if ((optind >= argc) || (argv[optind][0] != '-') || (argv[optind][0] == 0))
return -1;
int opt = argv[optind][1];
const char *p = strchr(optstring, opt);
if (p == nullptr) {
return '?';
}
if (p[1] == ':') {
optind++;
if (optind >= argc)
return '?';
optarg = argv[optind];
}
optind++;
return opt;
}
#else
#include <unistd.h>
#endif
int main(int argc, char** argv)
{
std::string iniFile = DEFAULT_INI_FILE;
int c;
while ((c = ::getopt(argc, argv, "c:v")) != -1) {
switch (c) {
case 'c':
iniFile = std::string(optarg);
break;
case 'v':
::fprintf(stdout, "NextionUpdater version %s git #%.7s\n", VERSION, gitversion);
return 0;
case '?':
break;
default:
::fprintf(stderr, "Usage: NextionUpdater [-v|--version] [-c <config filename>] <filename>\n");
break;
}
}
if (optind > (argc - 1)) {
::fprintf(stderr, "Usage: NextionUpdater [-v|--version] [-c <config filename>] <filename>\n");
return 1;
}
driver = new CNextionUpdater(std::string(iniFile), std::string(argv[argc - 1]));
int ret = driver->run();
delete driver;
::LogFinalise();
return ret;
}
CNextionUpdater::CNextionUpdater(const std::string& confFile, const std::string& filename) :
m_filename(filename),
m_conf(confFile),
m_msp(nullptr)
{
}
CNextionUpdater::~CNextionUpdater()
{
}
int CNextionUpdater::run()
{
bool ret = m_conf.read();
if (!ret) {
::fprintf(stderr, "NextionUpdater: cannot read the .ini file\n");
return 1;
}
FILE* file = ::fopen(m_filename.c_str(), "rb");
if (file == nullptr) {
::fprintf(stderr, "NextionUpdater: cannot read the firmware file\n");
return 1;
}
struct stat statbuf;
::stat(m_filename.c_str(), &statbuf);
::LogInitialise(m_conf.getLogDisplayLevel(), m_conf.getLogMQTTLevel());
const std::string displayName = m_conf.getMQTTHostName() + "/display-out";
std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>> subscriptions;
subscriptions.push_back(std::make_pair(displayName, CNextionUpdater::onDisplay));
m_mqtt = new CMQTTConnection(m_conf.getMQTTAddress(), m_conf.getMQTTPort(), m_conf.getMQTTName(), m_conf.getMQTTAuthEnabled(), m_conf.getMQTTUsername(), m_conf.getMQTTPassword(), subscriptions, m_conf.getMQTTKeepalive());
ret = m_mqtt->open();
if (!ret) {
::fprintf(stderr, "NextionUpdater: unable to start the MQTT Publisher\n");
delete m_mqtt;
return 1;
}
LogInfo("NextionUpdater-%s is starting", VERSION);
LogInfo("Built %s %s (GitID #%.7s)", __TIME__, __DATE__, gitversion);
writeJSONMessage("NextionUpdater is starting");
std::string type = m_conf.getDisplay();
if (type != "Nextion") {
LogInfo("Display is not a Nextion");
writeJSONMessage("Display is not a Nextion");
::fclose(file);
return 1;
}
std::string port = m_conf.getNextionPort();
if (port == "modem")
ret = uploadViaMQTT(file, statbuf.st_size);
else
ret = uploadViaUART(port, file, statbuf.st_size);
::fclose(file);
if (!ret) {
LogInfo("File upload failure");
writeJSONMessage("File upload failure");
return 1;
}
LogInfo("File uploaded");
writeJSONMessage("File uploaded");
return 0;
}
bool CNextionUpdater::uploadViaUART(const std::string& port, FILE* file, long fileSize)
{
assert(file != nullptr);
unsigned int screenLayout = m_conf.getNextionScreenLayout();
unsigned int baudrate = 9600U;
if (screenLayout == 4U)
baudrate = 115200U;
CUARTController serial(port, baudrate);
bool ret = serial.open();
if (!ret) {
LogInfo("Cannot open the serial port");
writeJSONMessage("Cannot open the serial port");
return false;
}
char command[100U];
::sprintf(command, "whmi-wri %ld,%u,0\xFF\xFF\xFF", fileSize, baudrate);
serial.write((unsigned char*)command, (unsigned int)::strlen(command));
CUtils::dump(1U, "Nextion command", (unsigned char*)command, (unsigned int)::strlen(command));
ret = waitForResponse(serial, 500U, false);
if (!ret) {
LogInfo("No response to the upload command");
writeJSONMessage("No response to the upload command");
serial.close();
return false;
}
unsigned char buffer[4096U];
size_t count = ::fread(buffer, 1U, 4096U, file);
while (count > 0U) {
int n = serial.write(buffer, (unsigned int)count);
while (n != int(count)) {
if (n < 0) {
LogInfo("Error from the serial port");
writeJSONMessage("Error from the serial port");
serial.close();
return false;
}
count -= n;
CThread::sleep(10U);
n = serial.write(buffer, (unsigned int)count);
}
ret = waitForResponse(serial, 500U, true);
if (!ret) {
LogInfo("No response to a data upload");
writeJSONMessage("No response to a data upload");
serial.close();
return false;
}
count = ::fread(buffer, 1U, 4096U, file);
}
serial.close();
return true;
}
bool CNextionUpdater::uploadViaMQTT(FILE* file, long fileSize)
{
assert(file != nullptr);
m_msp = new CModemSerialPort(m_conf.getMQTTHostName());
bool ret = m_msp->open();
if (!ret) {
LogInfo("Cannot open the serial port");
writeJSONMessage("Cannot open the serial port");
delete m_msp;
return false;
}
char command[100U];
::sprintf(command, "whmi-wri %ld,9600,0\xFF\xFF\xFF", fileSize);
m_msp->write((unsigned char*)command, (unsigned int)::strlen(command));
CUtils::dump(1U, "Nextion command", (unsigned char*)command, (unsigned int)::strlen(command));
ret = waitForResponse(*m_msp, 500U, false);
if (!ret) {
LogInfo("No response to the upload command");
writeJSONMessage("No response to the upload command");
m_msp->close();
delete m_msp;
return false;
}
unsigned char buffer[4096U];
size_t count = ::fread(buffer, 1U, 4096U, file);
while (count > 0U) {
m_msp->write(buffer, (unsigned int)count);
ret = waitForResponse(*m_msp, 4000U, true);
if (!ret) {
LogInfo("No response to a data upload");
writeJSONMessage("No response to a data upload");
m_msp->close();
delete m_msp;
return false;
}
count = ::fread(buffer, 1U, 4096U, file);
}
m_msp->close();
delete m_msp;
return true;
}
bool CNextionUpdater::waitForResponse(ISerialPort& serial, unsigned int timeout, bool wait)
{
CStopWatch stopWatch;
stopWatch.start();
unsigned int ms = 0U;
bool found = false;
while (ms < timeout) {
unsigned char c;
unsigned int n = serial.read(&c, 1U);
if (n == 1U) {
if (c == 0x05U) {
found = true;
if (!wait)
break;
}
}
CThread::sleep(10U);
ms = stopWatch.elapsed();
}
return found;
}
void CNextionUpdater::writeJSONMessage(const std::string& message)
{
nlohmann::json json;
json["timestamp"] = CUtils::createTimestamp();
json["message"] = message;
WriteJSON("status", json);
}
void CNextionUpdater::readDisplay(const unsigned char* data, unsigned int length)
{
assert(data != nullptr);
assert(length > 0U);
if (m_msp != nullptr)
m_msp->readData(data, length);
}
void CNextionUpdater::onDisplay(const unsigned char* data, unsigned int length)
{
assert(data != nullptr);
assert(length > 0U);
assert(driver != nullptr);
driver->readDisplay(data, length);
}