-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusb_relay.cpp
735 lines (632 loc) · 22.5 KB
/
usb_relay.cpp
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/*****************************************************************************
The MIT License
Copyright © 2021 Pavel Karelin (hkarel), <hkarel@yandex.ru>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#include "usb_relay.h"
#include "shared/utils.h"
#include "shared/logger/logger.h"
#include "shared/logger/format.h"
#include "shared/qt/logger_operators.h"
#include <stdlib.h>
#include <string.h>
#define log_error_m alog::logger().error (alog_line_location, "UsbRelay")
#define log_warn_m alog::logger().warn (alog_line_location, "UsbRelay")
#define log_info_m alog::logger().info (alog_line_location, "UsbRelay")
#define log_verbose_m alog::logger().verbose (alog_line_location, "UsbRelay")
#define log_debug_m alog::logger().debug (alog_line_location, "UsbRelay")
#define log_debug2_m alog::logger().debug2 (alog_line_location, "UsbRelay")
#define USB_RELAY_VENDOR_ID 0x16c0
#define USB_RELAY_DEVICE_ID 0x05df
#define USBRQ_HID_GET_REPORT 0x01
#define USBRQ_HID_SET_REPORT 0x09
#define REPORT_REQUEST_TIMEOUT 2*1000 // 2 секунды
#define USB_CONTINUOUS_ERRORS_1 3
#define USB_CONTINUOUS_ERRORS_2 5
namespace usb {
// Допустимый диапазон имен [USBRelay1...USBRelay8]
static const char* baseProductName = "USBRelay";
bool Relay::init(const QVector<int>& states)
{
QMutexLocker locker {&_threadLock}; (void) locker;
_initStates = states;
int res = libusb_init(&_context);
if (res != LIBUSB_SUCCESS)
{
log_error_m << "Failed libusb init"
<< ". Error code: " << res
<< ". Detail: " << libusb_error_name(res);
return false;
}
return true;
}
void Relay::deinit()
{
if (_context)
{
libusb_exit(_context);
_context = nullptr;
}
}
QString Relay::product() const
{
QMutexLocker locker {&_threadLock}; (void) locker;
return _product;
}
QString Relay::serial() const
{
QMutexLocker locker {&_threadLock}; (void) locker;
return _serial;
}
bool Relay::setSerial(const QString& value)
{
const int serialLen = 5;
QByteArray val = value.toUtf8();
if (val.length() > serialLen)
val.resize(serialLen);
while (val.length() < serialLen)
val.append('0');
for (int i = 0; i < serialLen; ++i)
{
uchar ch = uchar(val[i]);
if ((ch <= 0x20) || (ch >= 0x7F))
{
log_error_m << log_format(
"Incorrect USB relay serial. Symbol index: %?; code: %?",
i, int(ch));
return false;
}
}
char buff[8] = {0};
int buffSize = sizeof(buff);
buff[0] = 0xFA; // CMD_SET_SERIAL;
for (int i = 1; i <= serialLen; ++i)
buff[i] = val[i - 1];
int res = libusb_control_transfer(_deviceHandle,
LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_ENDPOINT_OUT,
USBRQ_HID_SET_REPORT,
0, // value
0, // index
(uchar*)buff, buffSize,
REPORT_REQUEST_TIMEOUT);
if (res != buffSize)
{
alog::Line logLine =
log_error_m << "Failed set USB relay serial: " << val;
if (res < 0)
{
_usbLastErrorCode = res;
logLine << ". Error code: " << res
<< ". Detail: " << libusb_error_name(res);
}
++_usbContinuousErrors;
return false;
}
memset(buff, 0, buffSize);
if (readStates(buff, buffSize) < 0)
{
log_error_m << "Failed get USB relay serial";
return false;
}
if (buff[serialLen + 1] != 0)
{
log_error_m << "Bad USB relay serial string";
return false;
}
QString serial = QString::fromLatin1(buff);
log_verbose_m << "USB relay new serial: " << serial;
QMutexLocker locker {&_threadLock}; (void) locker;
_serial = serial;
return true;
}
QString Relay::attachSerial() const
{
QMutexLocker locker {&_threadLock}; (void) locker;
return _attachSerial;
}
void Relay::setAttachSerial(const QString& val)
{
QMutexLocker locker {&_threadLock}; (void) locker;
_attachSerial = val;
}
int Relay::count() const
{
QMutexLocker locker {&_threadLock}; (void) locker;
return _count;
}
bool Relay::claimDevice()
{
int res;
bool deviceFound = false;
libusb_device** devList;
ssize_t devCount = libusb_get_device_list(0, &devList);
#define USB_DEV_CLOSE { \
libusb_close(_deviceHandle); \
_deviceHandle = nullptr; \
log_verbose_m << "USB device closed"; }
for (ssize_t i = 0; i < devCount; ++i)
{
CHECK_QTHREADEX_STOP
libusb_device* device = devList[i];
libusb_device_descriptor descript;
res = libusb_get_device_descriptor(device, &descript);
if (res != LIBUSB_SUCCESS)
{
log_error_m << "Failed get device descriptor"
<< ". Error code: " << res
<< ". Detail: " << libusb_error_name(res);
continue;
}
if (descript.idVendor == USB_RELAY_VENDOR_ID
&& descript.idProduct == USB_RELAY_DEVICE_ID)
{
deviceFound = true;
_usbBusNumber = libusb_get_bus_number(device);
_usbDeviceNumber = libusb_get_device_address(device);
log_info_m << "USB device found on bus "
<< utl::formatMessage("%03d/%03d", _usbBusNumber, _usbDeviceNumber);
res = libusb_open(device, &_deviceHandle);
if (res != LIBUSB_SUCCESS)
{
log_error_m << "Failed open USB device"
<< ". Error code: " << res
<< ". Detail: " << libusb_error_name(res);
_deviceHandle = nullptr;
continue;
}
log_verbose_m << "USB device is open";
/** TODO Аналог num_children в linusb-1.0 не найден **
if (device->num_children != 0)
{
log_error_m << "Children not supported. USB interface will be closed";
USB_CLOSE;
continue;
} */
char buff[128];
res = libusb_get_string_descriptor_ascii(
_deviceHandle, descript.iManufacturer,
(uchar*)buff, sizeof(buff));
if (res < LIBUSB_SUCCESS)
{
log_error_m << "Failed get manufacturer description"
<< ". Error code: " << res
<< ". Detail: " << libusb_error_name(res);
USB_DEV_CLOSE;
continue;
}
log_verbose_m << "USB manufacturer: " << buff;
res = libusb_get_string_descriptor_ascii(
_deviceHandle, descript.iProduct,
(uchar*)buff, sizeof(buff));
if (res < LIBUSB_SUCCESS)
{
log_error_m << "Failed get product description"
<< ". Error code: " << res
<< ". Detail: " << libusb_error_name(res);
USB_DEV_CLOSE;
continue;
}
QString product = QString::fromLatin1(buff);
log_verbose_m << "USB product: " << product;
int len = strlen(baseProductName);
if (strncmp(buff, baseProductName, len) != 0)
{
log_error_m << log_format(
"The base name of product must be %?"
". USB device will be closed", baseProductName);
USB_DEV_CLOSE;
continue;
}
if (strlen(buff) != size_t(len + 1))
{
log_error_m << log_format(
"The base product name does not contain a product index"
". USB device will be closed", baseProductName);
USB_DEV_CLOSE;
continue;
}
int relayCount = int(buff[len]) - int('0');
QSet<int> countCheck {1, 2, 4, 8};
if (!countCheck.contains(relayCount))
{
log_error_m << log_format(
"The number of relays must be one of values [1, 2, 4, 8]"
"Current value %?. USB device will be closed", relayCount);
USB_DEV_CLOSE;
continue;
}
log_verbose_m << "USB relay count: " << relayCount;
// Чтение состояний реле и серийного номера
int states = readStates(buff, 8);
if (states < 0)
{
USB_DEV_CLOSE;
continue;
}
const int serialLen = 5;
for (int i = 0; i < serialLen; ++i)
{
uchar ch = uchar(buff[i]);
if ((ch <= 0x20) || (ch >= 0x7F))
{
log_error_m << log_format(
"Incorrect USB relay serial. Symbol index: %?; code: %?",
i, int(ch));
}
}
if (buff[serialLen + 1] != 0)
{
log_error_m << "Bad USB relay serial string";
USB_DEV_CLOSE;
continue;
}
QString serial = QString::fromLatin1(buff);
log_verbose_m << "USB relay serial: " << serial;
{ //Block for QMutexLocker
QMutexLocker locker {&_threadLock}; (void) locker;
if (!_attachSerial.isEmpty())
{
if (_attachSerial != serial)
{
log_verbose_m << log_format(
"USB relay serial (%?) not match attach-serial (%?)",
serial, _attachSerial);
USB_DEV_CLOSE;
continue;
}
log_verbose_m << "USB relay serial to match attach-serial";
}
}
libusb_config_descriptor* config;
res = libusb_get_active_config_descriptor(device, &config);
if (res != LIBUSB_SUCCESS)
{
log_error_m << "Failed libusb_get_active_config_descriptor"
<< ". Error code: " << res
<< ". Detail: " << libusb_error_name(res);
USB_DEV_CLOSE;
continue;
}
res = libusb_set_auto_detach_kernel_driver(_deviceHandle, 1);
if (res != LIBUSB_SUCCESS)
{
log_error_m << "Failed set auto_detach_kernel_driver flag"
<< ". Error code: " << res
<< ". Detail: " << libusb_error_name(res);
USB_DEV_CLOSE;
continue;
}
const int intfNumber = 0;
res = libusb_claim_interface(_deviceHandle, intfNumber);
if (res != LIBUSB_SUCCESS)
{
log_error_m << "Failed claim USB interface " << intfNumber
<< ". Error code: " << res
<< ". Detail: " << libusb_error_name(res)
<< ". Perhaps need to create a UDEV rule to access the device";
USB_DEV_CLOSE;
continue;
}
log_verbose_m << log_format("USB interface %? claimed", intfNumber);
{ //Block for QMutexLocker
QMutexLocker locker {&_threadLock}; (void) locker;
_product = product;
_serial = serial;
_states = quint8(states);
_count = relayCount;
QVariant vstat;
vstat.setValue(statesInternal());
log_verbose_m << "USB relay states: " << vstat;
}
libusb_free_device_list(devList, true);
return true;
}
}
#undef USB_DEV_CLOSE
/* Free the libusb device list freeing unused devices */
libusb_free_device_list(devList, true);
if (deviceFound)
log_debug_m << "Device failed initialize";
else
log_debug_m << "Device not found";
_deviceHandle = nullptr;
return false;
}
void Relay::releaseDevice(bool deviceDetached)
{
if (_deviceHandle)
{
if (!deviceDetached)
{
const int intfNumber = 0;
int res = libusb_release_interface(_deviceHandle, intfNumber);
if (res != LIBUSB_SUCCESS)
log_error_m << "Failed release USB interface " << intfNumber
<< ". Error code: " << res
<< ". Detail: " << libusb_error_name(res);
else
log_verbose_m << log_format("USB interface %? released", intfNumber);
}
libusb_close(_deviceHandle);
log_verbose_m << "USB device closed";
}
_deviceHandle = nullptr;
_deviceInitialized = false;
_usbContinuousErrors = 0;
_usbLastErrorCode = 0;
_product.clear();
_serial.clear();
_count = 0;
}
void Relay::run()
{
log_info_m << "Started";
quint32 claimAttempts = 0;
bool deviceDetached = false;
while (true)
{
if (threadStop())
break;
_deviceInitialized = false;
if (!claimDevice())
{
releaseDevice(false);
int timeout = 2;
if (claimAttempts > 40)
timeout = 15;
else if (claimAttempts > 20)
timeout = 10;
sleep(timeout);
claimAttempts++;
CHECK_QTHREADEX_STOP
continue;
}
claimAttempts = 0;
deviceDetached = false;
_deviceInitialized = true;
{ //Block for QMutexLocker
QMutexLocker locker(&_threadLock); (void) locker;
if (!_initStates.isEmpty())
{
if (_initStates.count() > _count)
_initStates.resize(_count);
QVector<int> states = statesInternal();
for (int i = 0; i < _count; ++i)
if (_initStates[i] != states[i])
toggleInternal(i + 1, _initStates[i], 0);
_initStates.clear();
QVariant vstat;
vstat.setValue(statesInternal());
log_verbose_m << "USB init relay states: " << vstat;
}
}
log_info_m << "USB relay emit signal 'attached'";
emit attached();
while (true)
{
CHECK_QTHREADEX_STOP
if (_usbContinuousErrors >= USB_CONTINUOUS_ERRORS_1
&& _usbLastErrorCode == LIBUSB_ERROR_NO_DEVICE)
{
deviceDetached = true;
break;
}
if (_usbContinuousErrors >= USB_CONTINUOUS_ERRORS_2)
{
deviceDetached = true;
break;
}
{ //Block for QMutexLocker
QMutexLocker locker(&_threadLock); (void) locker;
_threadCond.wait(&_threadLock, 200);
}
if (threadStop())
continue;
{ //Block for QMutexLocker
QMutexLocker locker {&_threadLock}; (void) locker;
char buff[8] = {0};
int states = readStates(buff, sizeof(buff));
if (states < 0)
continue;
if (_states != quint8(states))
{
log_debug_m << log_format(
"USB relay state was changed from outside"
". Old value: %?. New value: %?", int(_states), states);
_states = quint8(states);
}
}
} // while (true)
log_info_m << "USB relay emit signal 'detached'";
emit detached();
releaseDevice(deviceDetached);
} // while (true)
releaseDevice(deviceDetached);
log_info_m << "Stopped";
}
void Relay::threadStopEstablished()
{
QMutexLocker locker {&_threadLock}; (void) locker;
_threadCond.wakeAll();
}
int Relay::readStates(char* buff, int buffSize)
{
int res = libusb_control_transfer(_deviceHandle,
LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_ENDPOINT_IN,
USBRQ_HID_GET_REPORT,
0, // value
0, // index
(uchar*)buff, buffSize,
REPORT_REQUEST_TIMEOUT);
if (res != buffSize)
{
alog::Line logLine =
log_error_m << "Failed send message to USB interface";
if (res < 0)
{
_usbLastErrorCode = res;
logLine << ". Error code: " << res
<< ". Detail: " << libusb_error_name(res);
}
++_usbContinuousErrors;
return -1;
}
if (alog::logger().level() >= alog::Level::Debug)
{
if (_usbContinuousErrors != 0)
log_debug_m << "USB continuous errors: " << int(_usbContinuousErrors);
}
_usbContinuousErrors = 0;
_usbLastErrorCode = 0;
return quint8(buff[7]); // Байт 7 содержит битовые флаги состояний реле
}
QVector<int> Relay::states() const
{
QMutexLocker locker {&_threadLock}; (void) locker;
return statesInternal();
}
QVector<int> Relay::statesInternal() const
{
QVector<int> st;
st.resize(_count);
for (int i = 0; i < _count; ++i)
st[i] = bool(_states & (1U << i));
return st;
}
//bool Relay::toggle(const QVector<int> states)
//{
//}
bool Relay::toggle(int relayNumber, bool value, int tag)
{
QMutexLocker locker {&_threadLock}; (void) locker;
return toggleInternal(relayNumber, value, tag);
}
bool Relay::toggleInternal(int relayNumber, bool value, int tag)
{
if (!_deviceInitialized)
{
alog::Line logLine =
log_error_m << "Failed toggle relay. Device not initialized";
emit failChange(relayNumber, logLine.impl->buff.c_str(), tag);
return false;
}
int relayCount = _count;
if (relayNumber > relayCount)
{
alog::Line logLine = log_error_m << log_format(
"Failed toggle relay number %?. Number out of range [1..%?]",
relayNumber, count());
emit failChange(relayNumber, logLine.impl->buff.c_str(), tag);
return false;
}
char buff[8];
int buffSize = sizeof(buff);
quint8 cmd1 = 0;
quint8 cmd2 = 0;
quint8 expectStates = 0;
if (relayNumber <= 0)
{
if (value == true)
{
cmd1 = 0xFE; // Включить все реле
expectStates = (1U << relayCount) - 1;
}
else
{
cmd1 = 0xFC; // Выключить все реле
expectStates = 0;
}
relayNumber = 0;
}
else
{
memset(buff, 0, buffSize);
int states = readStates(buff, buffSize);
if (states < 0)
{
alog::Line logLine = log_error_m << "Failed get relays current state";
emit failChange(relayNumber, logLine.impl->buff.c_str(), tag);
return false;
}
expectStates = quint8(states);
if (value == true)
{
cmd1 = 0xFF; // Включить реле по номеру
expectStates |= (1U << (relayNumber - 1));
}
else
{
cmd1 = 0xFD; // Выключить реле по номеру
expectStates &= ~(1U << (relayNumber - 1));
}
cmd2 = quint8(relayNumber);
}
memset(buff, 0, sizeof(buff));
buff[0] = cmd1;
buff[1] = cmd2;
int res = libusb_control_transfer(_deviceHandle,
LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_ENDPOINT_OUT,
USBRQ_HID_SET_REPORT,
0, // value
0, // index
(uchar*)buff, buffSize,
REPORT_REQUEST_TIMEOUT);
if (res != buffSize)
{
alog::Line logLine =
log_error_m << "Failed send message to USB interface";
if (res < 0)
{
_usbLastErrorCode = res;
logLine << ". Error code: " << res
<< ". Detail: " << libusb_error_name(res);
}
++_usbContinuousErrors;
emit failChange(relayNumber, logLine.impl->buff.c_str(), tag);
return false;
}
int states = readStates(buff, buffSize);
if (states < 0)
{
alog::Line logLine = log_error_m << "Failed get relays current state";
emit failChange(relayNumber, logLine.impl->buff.c_str(), tag);
return false;
}
_states = quint8(states);
if (_states != expectStates)
{
alog::Line logLine = log_error_m << "Failed set relays to new state";
emit failChange(relayNumber, logLine.impl->buff.c_str(), tag);
return false;
}
if (relayNumber <= 0)
log_verbose_m << log_format(
"USB all relay turn %?", (value) ? "ON" : "OFF");
else
log_verbose_m << log_format(
"USB relay %? turn %?", relayNumber, (value) ? "ON" : "OFF");
emit changed(relayNumber, tag);
_usbContinuousErrors = 0;
_usbLastErrorCode = 0;
return true;
}
Relay& relay()
{
return safe::singleton<Relay>();
}
} // namespace usb