1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 Andre Hartmann <aha_1980@gmx.de>
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the QtSerialBus module.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #include "sendframebox.h"
52 #include "ui_sendframebox.h"
53 
54 enum {
55     MaxStandardId = 0x7FF,
56     MaxExtendedId = 0x10000000
57 };
58 
59 enum {
60     MaxPayload = 8,
61     MaxPayloadFd = 64
62 };
63 
HexIntegerValidator(QObject * parent)64 HexIntegerValidator::HexIntegerValidator(QObject *parent) :
65     QValidator(parent),
66     m_maximum(MaxStandardId)
67 {
68 }
69 
validate(QString & input,int &) const70 QValidator::State HexIntegerValidator::validate(QString &input, int &) const
71 {
72     bool ok;
73     uint value = input.toUInt(&ok, 16);
74 
75     if (input.isEmpty())
76         return Intermediate;
77 
78     if (!ok || value > m_maximum)
79         return Invalid;
80 
81     return Acceptable;
82 }
83 
setMaximum(uint maximum)84 void HexIntegerValidator::setMaximum(uint maximum)
85 {
86     m_maximum = maximum;
87 }
88 
HexStringValidator(QObject * parent)89 HexStringValidator::HexStringValidator(QObject *parent) :
90     QValidator(parent),
91     m_maxLength(MaxPayload)
92 {
93 }
94 
validate(QString & input,int & pos) const95 QValidator::State HexStringValidator::validate(QString &input, int &pos) const
96 {
97     const int maxSize = 2 * m_maxLength;
98     const QChar space = QLatin1Char(' ');
99     QString data = input;
100     data.remove(space);
101 
102     if (data.isEmpty())
103         return Intermediate;
104 
105     // limit maximum size and forbid trailing spaces
106     if ((data.size() > maxSize) || (data.size() == maxSize && input.endsWith(space)))
107         return Invalid;
108 
109     // check if all input is valid
110     const QRegularExpression re(QStringLiteral("^[[:xdigit:]]*$"));
111     if (!re.match(data).hasMatch())
112         return Invalid;
113 
114     // insert a space after every two hex nibbles
115     const QRegularExpression insertSpace(QStringLiteral("(?:[[:xdigit:]]{2} )*[[:xdigit:]]{3}"));
116     if (insertSpace.match(input).hasMatch()) {
117         input.insert(input.size() - 1, space);
118         pos = input.size();
119     }
120 
121     return Acceptable;
122 }
123 
setMaxLength(int maxLength)124 void HexStringValidator::setMaxLength(int maxLength)
125 {
126     m_maxLength = maxLength;
127 }
128 
SendFrameBox(QWidget * parent)129 SendFrameBox::SendFrameBox(QWidget *parent) :
130     QGroupBox(parent),
131     m_ui(new Ui::SendFrameBox)
132 {
133     m_ui->setupUi(this);
134 
135     m_hexIntegerValidator = new HexIntegerValidator(this);
136     m_ui->frameIdEdit->setValidator(m_hexIntegerValidator);
137     m_hexStringValidator = new HexStringValidator(this);
138     m_ui->payloadEdit->setValidator(m_hexStringValidator);
139 
140     connect(m_ui->dataFrame, &QRadioButton::toggled, [this](bool set) {
141         if (set)
142             m_ui->flexibleDataRateBox->setEnabled(true);
143     });
144 
145     connect(m_ui->remoteFrame, &QRadioButton::toggled, [this](bool set) {
146         if (set) {
147             m_ui->flexibleDataRateBox->setEnabled(false);
148             m_ui->flexibleDataRateBox->setChecked(false);
149         }
150     });
151 
152     connect(m_ui->errorFrame, &QRadioButton::toggled, [this](bool set) {
153         if (set) {
154             m_ui->flexibleDataRateBox->setEnabled(false);
155             m_ui->flexibleDataRateBox->setChecked(false);
156         }
157     });
158 
159     connect(m_ui->extendedFormatBox, &QCheckBox::toggled, [this](bool set) {
160         m_hexIntegerValidator->setMaximum(set ? MaxExtendedId : MaxStandardId);
161     });
162 
163     connect(m_ui->flexibleDataRateBox, &QCheckBox::toggled, [this](bool set) {
164         m_hexStringValidator->setMaxLength(set ? MaxPayloadFd : MaxPayload);
165         m_ui->bitrateSwitchBox->setEnabled(set);
166         if (!set)
167             m_ui->bitrateSwitchBox->setChecked(false);
168     });
169 
170     auto frameIdTextChanged = [this]() {
171         const bool hasFrameId = !m_ui->frameIdEdit->text().isEmpty();
172         m_ui->sendButton->setEnabled(hasFrameId);
173         m_ui->sendButton->setToolTip(hasFrameId
174                                      ? QString() : tr("Cannot send because no Frame ID was given."));
175     };
176     connect(m_ui->frameIdEdit, &QLineEdit::textChanged, frameIdTextChanged);
177     frameIdTextChanged();
178 
179     connect(m_ui->sendButton, &QPushButton::clicked, [this]() {
180         const uint frameId = m_ui->frameIdEdit->text().toUInt(nullptr, 16);
181         QString data = m_ui->payloadEdit->text();
182         const QByteArray payload = QByteArray::fromHex(data.remove(QLatin1Char(' ')).toLatin1());
183 
184         QCanBusFrame frame = QCanBusFrame(frameId, payload);
185         frame.setExtendedFrameFormat(m_ui->extendedFormatBox->isChecked());
186         frame.setFlexibleDataRateFormat(m_ui->flexibleDataRateBox->isChecked());
187         frame.setBitrateSwitch(m_ui->bitrateSwitchBox->isChecked());
188 
189         if (m_ui->errorFrame->isChecked())
190             frame.setFrameType(QCanBusFrame::ErrorFrame);
191         else if (m_ui->remoteFrame->isChecked())
192             frame.setFrameType(QCanBusFrame::RemoteRequestFrame);
193 
194         emit sendFrame(frame);
195     });
196 }
197 
~SendFrameBox()198 SendFrameBox::~SendFrameBox()
199 {
200     delete m_ui;
201 }
202