-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmbroker.cpp
220 lines (201 loc) · 6.76 KB
/
stmbroker.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
#include "stmbroker.h"
#include "aliseconstants.h"
#include <QRandomGenerator>
#include <interfaces/connectionmanager.h>
#include <interfaces/types/usbhidportinfo.h>
#include <iostream>
StmBroker::StmBroker(QObject *parent)
: Broker(parent), m_manager(new Interface::ConnectionManager(this)), m_conn(nullptr)
{
clearBSIReady();
}
bool StmBroker::connect()
{
#ifndef ALISE_LOCALDEBUG
const auto devices = UsbHidPortInfo::devicesFound(0x0483);
if (devices.isEmpty())
{
std::cout << "No devices" << std::endl;
return false;
}
for (const auto &device : devices)
{
std::cout << "Vendor id:" << std::hex << device.vendor_id << std::dec << " : "
<< "Product id:" << std::hex << device.product_id << std::dec << " : "
<< "Serial number:" << device.serial.toStdString() << std::endl;
}
UsbHidSettings settings { devices.first() };
settings.m_timeout = 1000;
settings.m_maxErrors = 5;
settings.m_maxTimeouts = 5;
settings.m_isLoggingEnabled = true;
settings.m_reconnectInterval = 100;
settings.m_silentInterval = 100;
m_conn = m_manager->createConnection(settings);
if (m_conn == nullptr)
{
std::cout << "Couldn't connect" << std::endl;
return false;
}
else
{
m_conn->connection(this, &StmBroker::currentIndicationReceived);
m_conn->connection(static_cast<Broker *>(this), &Broker::updateBlock);
m_conn->connection(static_cast<Broker *>(this), &StmBroker::updateTime);
return true;
}
#else
return true;
#endif
}
bool StmBroker::connect(AliseSettings &asettings)
{
if (connect())
{
m_conn->connection(this, [&](const DataTypes::BitStringStruct &resp) { updateBsi(asettings, resp); });
clearBSIReady();
m_conn->writeCommand(Interface::Commands::C_ReqBSI);
return true;
}
return false;
}
void StmBroker::writeHiddenBlock()
{
#ifndef ALISE_LOCALDEBUG
QMutexLocker locker(&_mutex);
Q_CHECK_PTR(m_conn);
DataTypes::BlockStruct block;
block.ID = 0x01; // base block
block.data.resize(sizeof(Alise::AliseConstants::ModuleInfo));
memcpy(block.data.data(), &Alise::AliseConstants::s_moduleInfo, sizeof(Alise::AliseConstants::ModuleInfo));
m_conn->writeCommand(Interface::Commands::C_WriteHiddenBlock, QVariant::fromValue(block));
#endif
}
bool StmBroker::BSIReady()
{
return !m_BSINotCompleted;
}
void StmBroker::clearBSIReady()
{
m_BSINotCompleted = 0x0F; // 4 LSBs are for SerialNums & Versions
}
void StmBroker::checkPowerUnit()
{
#ifndef ALISE_LOCALDEBUG
QMutexLocker locker(&_mutex);
Q_CHECK_PTR(m_conn);
m_conn->writeCommand(Interface::Commands::C_ReqBlkData, AVTUK_CCU::MainBlock);
#endif
}
void StmBroker::checkIndication()
{
#ifndef ALISE_LOCALDEBUG
QMutexLocker locker(&_mutex);
Q_CHECK_PTR(m_conn);
m_conn->writeCommand(Interface::Commands::C_ReqBlkData, AVTUK_CCU::IndicationBlock);
#endif
}
void StmBroker::setIndication(const AVTUK_CCU::Indication &indication)
{
QMutexLocker locker(&_mutex);
if (m_currentIndication == indication)
return;
m_currentIndication.PulseCnt1 = indication.PulseCnt1;
m_currentIndication.PulseCnt2 = indication.PulseCnt2;
m_currentIndication.PulseFreq1 = Alise::AliseConstants::freqByPeriod(indication.PulseFreq1);
m_currentIndication.PulseFreq2 = Alise::AliseConstants::freqByPeriod(indication.PulseFreq2);
qDebug() << "Indication is: cnt1: " << indication.PulseCnt1 << ", freq1: " << indication.PulseFreq1
<< ", cnt2: " << indication.PulseCnt2 << ", freq2: " << indication.PulseFreq2;
#ifndef ALISE_LOCALDEBUG
DataTypes::BlockStruct block;
block.ID = AVTUK_CCU::IndicationBlock;
block.data.resize(sizeof(m_currentIndication));
memcpy(block.data.data(), &m_currentIndication, sizeof(m_currentIndication));
m_conn->writeCommand(Interface::Commands::C_WriteUserValues, QVariant::fromValue(block));
#endif
}
void StmBroker::setTime(const timespec &time)
{
#ifndef ALISE_LOCALDEBUG
QMutexLocker locker(&_mutex);
Q_CHECK_PTR(m_conn);
m_conn->writeTime(time);
#endif
}
void StmBroker::getTime()
{
#ifndef ALISE_LOCALDEBUG
QMutexLocker locker(&_mutex);
Q_CHECK_PTR(m_conn);
m_conn->reqTime();
#endif
}
void StmBroker::rebootMyself()
{
#ifndef ALISE_LOCALDEBUG
m_conn->writeCommand(Interface::Commands::C_Reboot, 0xff);
#endif
}
void StmBroker::currentIndicationReceived(const DataTypes::BlockStruct &blk)
{
AVTUK_CCU::Indication indic;
// qDebug() << "[StmBroker] <= MCU : Block ID = " << blk.ID << ", data = " << blk.data;
if (blk.ID == AVTUK_CCU::IndicationBlock)
{
memcpy(&indic, blk.data.data(), sizeof(indic));
if (!(indic == m_currentIndication))
{
qDebug() << "indications doens't equals: currentIndic = " << m_currentIndication.PulseCnt1 << " " << m_currentIndication.PulseFreq1 << " " << m_currentIndication.PulseCnt2 << " " << m_currentIndication.PulseFreq2 <<
" and received indication is: " << indic.PulseCnt1 << " " << indic.PulseFreq1 << " " << indic.PulseCnt2 << " " << indic.PulseFreq2;
m_currentIndication = indic;
m_oldCode = 0; // force setting indication
}
}
}
void StmBroker::writeCompleted(const DataTypes::GeneralResponseStruct &resp)
{
Q_UNUSED(resp)
emit operationCompleted();
}
timespec StmBroker::transform(google::protobuf::Timestamp timestamp) const
{
timespec temp;
temp.tv_nsec = timestamp.nanos();
temp.tv_sec = timestamp.seconds();
return temp;
}
void StmBroker::updateBsi(AliseSettings &m_settings, const DataTypes::BitStringStruct &resp)
{
if (!m_BSINotCompleted)
return; // BSI is received already
switch (resp.sigAdr)
{
case ModuleSerialNumAdr:
m_settings.serialNum = resp.sigVal;
m_BSINotCompleted &= 0x0E;
break;
case SerialNumBAdr:
m_settings.serialNumB = resp.sigVal;
m_BSINotCompleted &= 0x0D;
break;
case HWAdr:
m_settings.hwVersion = resp.sigVal;
m_BSINotCompleted &= 0x0B;
break;
case SWAdr:
m_settings.swVersion = resp.sigVal;
m_BSINotCompleted &= 0x07;
break;
}
if (!m_BSINotCompleted)
{
qDebug() << "BSI receiving complete: serialNum = " << m_settings.serialNum
<< ", serialNumB = " << m_settings.serialNumB << ", HWVersion = " << m_settings.hwVersion
<< ", SWVersion = " << m_settings.swVersion;
Alise::AliseConstants::s_moduleInfo.ModuleSerialNumber = m_settings.serialNum;
Alise::AliseConstants::s_moduleInfo.SerialNumber = m_settings.serialNumB;
Alise::AliseConstants::s_moduleInfo.HWVersion = m_settings.hwVersion;
m_settings.writeSetting();
emit ModuleInfoFilled();
}
}