| 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 | |
| 64 | HexIntegerValidator::HexIntegerValidator(QObject *parent) : |
| 65 | QValidator(parent), |
| 66 | m_maximum(MaxStandardId) |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | QValidator::State HexIntegerValidator::validate(QString &input, int &) const |
| 71 | { |
| 72 | bool ok; |
| 73 | uint value = input.toUInt(ok: &ok, base: 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 | |
| 84 | void HexIntegerValidator::setMaximum(uint maximum) |
| 85 | { |
| 86 | m_maximum = maximum; |
| 87 | } |
| 88 | |
| 89 | HexStringValidator::HexStringValidator(QObject *parent) : |
| 90 | QValidator(parent), |
| 91 | m_maxLength(MaxPayload) |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | 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(c: 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(c: space))) |
| 107 | return Invalid; |
| 108 | |
| 109 | // check if all input is valid |
| 110 | const QRegularExpression re(QStringLiteral("^[[:xdigit:]]*$" )); |
| 111 | if (!re.match(subject: 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(subject: input).hasMatch()) { |
| 117 | input.insert(i: input.size() - 1, c: space); |
| 118 | pos = input.size(); |
| 119 | } |
| 120 | |
| 121 | return Acceptable; |
| 122 | } |
| 123 | |
| 124 | void HexStringValidator::setMaxLength(int maxLength) |
| 125 | { |
| 126 | m_maxLength = maxLength; |
| 127 | } |
| 128 | |
| 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(sender: m_ui->dataFrame, signal: &QRadioButton::toggled, slot: [this](bool set) { |
| 141 | if (set) |
| 142 | m_ui->flexibleDataRateBox->setEnabled(true); |
| 143 | }); |
| 144 | |
| 145 | connect(sender: m_ui->remoteFrame, signal: &QRadioButton::toggled, slot: [this](bool set) { |
| 146 | if (set) { |
| 147 | m_ui->flexibleDataRateBox->setEnabled(false); |
| 148 | m_ui->flexibleDataRateBox->setChecked(false); |
| 149 | } |
| 150 | }); |
| 151 | |
| 152 | connect(sender: m_ui->errorFrame, signal: &QRadioButton::toggled, slot: [this](bool set) { |
| 153 | if (set) { |
| 154 | m_ui->flexibleDataRateBox->setEnabled(false); |
| 155 | m_ui->flexibleDataRateBox->setChecked(false); |
| 156 | } |
| 157 | }); |
| 158 | |
| 159 | connect(sender: m_ui->extendedFormatBox, signal: &QCheckBox::toggled, slot: [this](bool set) { |
| 160 | m_hexIntegerValidator->setMaximum(set ? MaxExtendedId : MaxStandardId); |
| 161 | }); |
| 162 | |
| 163 | connect(sender: m_ui->flexibleDataRateBox, signal: &QCheckBox::toggled, slot: [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(s: "Cannot send because no Frame ID was given." )); |
| 175 | }; |
| 176 | connect(sender: m_ui->frameIdEdit, signal: &QLineEdit::textChanged, slot: frameIdTextChanged); |
| 177 | frameIdTextChanged(); |
| 178 | |
| 179 | connect(sender: m_ui->sendButton, signal: &QPushButton::clicked, slot: [this]() { |
| 180 | const uint frameId = m_ui->frameIdEdit->text().toUInt(ok: nullptr, base: 16); |
| 181 | QString data = m_ui->payloadEdit->text(); |
| 182 | const QByteArray payload = QByteArray::fromHex(hexEncoded: data.remove(c: 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 | |
| 198 | SendFrameBox::~SendFrameBox() |
| 199 | { |
| 200 | delete m_ui; |
| 201 | } |
| 202 | |