summaryrefslogtreecommitdiff
path: root/tests/guidevtest/unittestio.cpp
blob: 2aff9c0614ce571ee496251355969e83632b9bfc (plain)
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
#include "unittests.h"
#include "serialport.h"

#include <QtCore/QTimer>
#include <QtCore/QStringList>


enum {
    RATES_COUNT    = 2,
    DATABITS_COUNT = 1,
    PARITY_COUNT   = 5,
    STOPBITS_COUNT = 2,
    FLOW_COUNT     = 3
};

static const SerialPort::Rate vratesarray[RATES_COUNT] = {
    SerialPort::Rate9600,
    SerialPort::Rate115200
};
static const char *sratesarray[] = {
    "9600\0",
    "115200\0"
};

static const SerialPort::DataBits vdatabitsarray[DATABITS_COUNT] = {
    SerialPort::Data8
};
static const char *sdatabitsarray[] = {
    "8\0"
};

static const SerialPort::Parity vparitysarray[PARITY_COUNT] = {
    SerialPort::NoParity,
    SerialPort::EvenParity,
    SerialPort::OddParity,
    SerialPort::SpaceParity,
    SerialPort::MarkParity
};
static const char *sparitysarray[] = {
    "none\0",
    "even\0",
    "odd\0",
    "space\0",
    "mark\0"
};

static const SerialPort::StopBits vstopbitsarray[STOPBITS_COUNT] = {
    SerialPort::OneStop,
    SerialPort::TwoStop
};
static const char *sstopbitsarray[] = {
    "one\0",
    "two\0"
};

static const SerialPort::FlowControl vflowsarray[FLOW_COUNT] = {
    SerialPort::NoFlowControl,
    SerialPort::HardwareControl,
    SerialPort::SoftwareControl
};
static const char *sflowsarray[] = {
    "none\0",
    "hardware\0",
    "software\0"
};

static QString split_on_table(const QByteArray &data, int tablewidth)
{
    QString result;
    int datacount = data.count();
    int i = 0;

    while (i < datacount) {
        result.append(data.mid(i, tablewidth).toHex());
        result.append('\n');
        i += tablewidth;
    }
    return result;
}

static QByteArray random_data_array(int arraysize)
{
    QByteArray result;
    while (arraysize--) {
        // Here, protection of control characters 11h, 13h
        // with software flow control.
        char c;
        do {
            c = qrand();
        } while ((c == 0x11) || (c == 0x13));

        result.append(c);
    }
    return result;
}



/* Public methods */

UnitTestIO::UnitTestIO(Logger *logger, QObject *parent)
    : UnitTestBase(UnitTestBase::IOUnitId, logger, parent)
    , m_rateIterator(0)
    , m_databitsIterator(0)
    , m_parityIterator(0)
    , m_stopbitsIterator(0)
    , m_flowIterator(0)
    , m_bytesWrite(0)
    , m_bytesRead(0)
{
    m_name = QString(tr("IO Test"));
    m_description = QString(tr("\"IO Test\" designed to test the I/O between the two ports\n"
                               "Source port sends a data packet to the destination port,\n"
                               "that reads the packet.\n"
                               "  The default packet size is 500 bytes, the size can be changed\n"
                               "programmatically by changing the value of the\n"
                               "variable TransferBytesCount. Also, before sending the package\n"
                               "is filled with a random value.\n"
                               "  Both ports after each transaction, change their parameters:\n"
                               "speed, number of data bits, parity, number of stop bits,\n"
                               "flow regime, until the end all enumerated parameters.\n"
                               "After each transaction is recorded in a log the contents of the\n"
                               "sent and received packet, and check their size. If the packet\n"
                               "sizes are different, the test is aborted with an error which is\n"
                               "recorded in the log.\n"
                               ));

    m_srcPort = new SerialPort(this);
    m_dstPort = new SerialPort(this);
}

/* Public slots */

void UnitTestIO::start(bool first)
{
    if (first) {
        QString header(tr("\n[ Test: ID#%1, Name: %2 ]\n%3\n\n"));
        header = header
                .arg(m_id)
                .arg(m_name)
                .arg(QString("timestamp"));/*.arg(UnitTestManager::timestamp());*/

        m_logger->addContent(header);

        m_srcPort->setPort(m_srcPortName);
        m_dstPort->setPort(m_dstPortName);

        if (!(open(UnitTestBase::SrcPort) && open(UnitTestBase::DstPort))) {
            emit error();
            return;
        } else {
            QString content(tr("\nSource and destination ports is opened.\n"));
            m_logger->addContent(content);
        }

        m_rateIterator = 0;
        m_databitsIterator = 0;
        m_parityIterator = 0;
        m_stopbitsIterator = 0;
        m_flowIterator = 0;
    }

    transaction();
}

/* Private slots */

void UnitTestIO::procSingleShot()
{
    QByteArray data = m_dstPort->readAll();

    QString content("r:\n%1\n");
    content = content.arg(split_on_table(data, 32));
    m_logger->addContent(content);

    m_bytesRead = data.count();

    content = QString(tr("= write: %1 read: %2 =\n"));
    content = content
            .arg(m_bytesWrite)
            .arg(m_bytesRead);
    m_logger->addContent(content);

    if (m_bytesWrite != m_bytesRead) {
        content = QString(tr("\nError: Mismatch of write and read bytes.\n"));
        m_logger->addContent(content);
        close(UnitTestBase::SrcPort);
        close(UnitTestBase::DstPort);
        emit error();
        return;
    }

    ++m_rateIterator;
    if (m_rateIterator == RATES_COUNT) {
        m_rateIterator = 0;

        ++m_databitsIterator;
        if (m_databitsIterator == DATABITS_COUNT) {
            m_databitsIterator = 0;

            ++m_parityIterator;
            if (m_parityIterator == PARITY_COUNT) {
                m_parityIterator = 0;

                ++m_stopbitsIterator;
                if (m_stopbitsIterator == STOPBITS_COUNT) {
                    m_stopbitsIterator = 0;

                    ++m_flowIterator;
                    if (m_flowIterator == FLOW_COUNT) {
                        m_flowIterator = 0;

                        close(UnitTestBase::SrcPort);
                        close(UnitTestBase::DstPort);
                        emit finished();
                        return;
                    }
                }
            }
        }
    }

    transaction();
}

void UnitTestIO::transaction()
{
    if (!(configure(UnitTestBase::SrcPort) && configure(UnitTestBase::DstPort))) {
        emit error();
        return;
    }

    QString content(tr("\nrate    : %1"
                       "\ndatabits: %2"
                       "\npatity  : %3"
                       "\nstopbits: %4"
                       "\nflow    : %5\n\n"));

    content = content
            .arg(QString(sratesarray[m_rateIterator]))
            .arg(QString(sdatabitsarray[m_databitsIterator]))
            .arg(QString(sparitysarray[m_parityIterator]))
            .arg(QString(sstopbitsarray[m_stopbitsIterator]))
            .arg(QString(sflowsarray[m_flowIterator]));

    m_logger->addContent(content);

    QByteArray data = random_data_array(TransferBytesCount);
    m_bytesWrite = m_srcPort->write(data);

    content = "w:\n%1\n";
    content = content.arg(split_on_table(data, 32));
    m_logger->addContent(content);

    QTimer::singleShot(TransactionMsecDelay, this, SLOT(procSingleShot()));
}

/* Private */

bool UnitTestIO::open(DirPorts dir)
{
    SerialPort *port = (dir == UnitTestBase::SrcPort) ?
                m_srcPort : m_dstPort;
    QIODevice::OpenMode mode = (dir == UnitTestBase::SrcPort) ?
                QIODevice::WriteOnly : QIODevice::ReadOnly;

    QString error("\nError: Can\'t open port %1\n");
    if (!port->open(mode)) {
        error = error.arg(port->portName());
        m_logger->addContent(error);
        return false;
    }
    return true;
}

bool UnitTestIO::configure(DirPorts dir)
{
    SerialPort *port = (dir == UnitTestBase::SrcPort) ?
                m_srcPort : m_dstPort;

    if (!(port->setRate(vratesarray[m_rateIterator])
          && port->setDataBits(vdatabitsarray[m_databitsIterator])
          && port->setParity(vparitysarray[m_parityIterator])
          && port->setStopBits(vstopbitsarray[m_stopbitsIterator])
          && port->setFlowControl(vflowsarray[m_flowIterator]))) {

        QString error("\nError: Can\'t configure port %1\n");
        error = error.arg(port->portName());
        m_logger->addContent(error);
        return false;
    }
    return true;
}

void UnitTestIO::close(DirPorts dir)
{
    if (dir == UnitTestBase::SrcPort) {
        if (m_srcPort->isOpen())
            m_srcPort->close();
    } else {
        if (m_dstPort->isOpen())
            m_dstPort->close();
    }
}