1 /*
2 * Copyright (c) 208 Dimitris Tassopoulos <dimtass@gmail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 * For more information on the GPL, please go to:
19 * http://www.gnu.org/copyleft/gpl.html
20 */
21
22 #include "counterplugin.h"
23 #include "ui_counterplugin.h"
24 #include <QMessageBox>
25
26 #define TRACE \
27 if (!debug) { \
28 } else \
29 qDebug()
30
31 static bool debug = false;
32 static CounterPlugin *m_counter = NULL;
33 static int processCmd(const QString *text, QString *new_text);
34
CounterPlugin(QFrame * parent,Settings * settings)35 CounterPlugin::CounterPlugin(QFrame *parent, Settings *settings)
36 : QFrame(parent)
37 , ui(new Ui::CounterPlugin)
38 , m_settings(settings)
39 {
40 ui->setupUi(this);
41 /* Plugin by default disabled, no injection, has QFrame, no injection process cmd */
42 m_plugin = new Plugin(this, "Byte Counter", this, (Plugin::processCmd_fp)&processCmd);
43 /* reset values */
44 ui->m_lbl_rx_value->setText("0");
45 ui->m_lbl_tx_value->setText("0");
46 ui->m_lbl_mrx_value->setText("0");
47 ui->m_lbl_mtx_value->setText("0");
48
49 connect(ui->m_bt_unload, &QPushButton::clicked, this, &CounterPlugin::removePlugin);
50 connect(ui->m_bt_help, &QPushButton::clicked, this, &CounterPlugin::helpMsg);
51 connect(ui->m_bt_clear, &QPushButton::clicked, this, [=]() {
52 ui->m_lbl_rx_value->setText("0");
53 ui->m_lbl_tx_value->setText("0");
54 });
55 connect(ui->m_bt_clear_memory, &QPushButton::clicked, this, [=]() {
56 ui->m_lbl_mrx_value->setText("0");
57 ui->m_lbl_mtx_value->setText("0");
58 });
59 connect(ui->m_bt_memory, &QPushButton::clicked, this, [=]() {
60 ui->m_lbl_mrx_value->setText(ui->m_lbl_rx_value->text());
61 ui->m_lbl_mtx_value->setText(ui->m_lbl_tx_value->text());
62 });
63 m_counter = this;
64
65 TRACE << "[CounterPlugin::CounterPlugin]";
66 }
67
~CounterPlugin()68 CounterPlugin::~CounterPlugin()
69 {
70 delete ui;
71 if (m_counter)
72 m_counter = NULL;
73 }
74
75 /**
76 * @brief Static function to use as proxy to call public
77 * member funtions from this object
78 * @param text The data that are about to be sent
79 * @param new_text The new data
80 * @return int Always retutn 0 in this case
81 */
processCmd(const QString * text,QString * new_text)82 int processCmd(const QString *text, QString *new_text)
83 {
84 m_counter->txBytes(text->length());
85
86 TRACE << "[CounterPlugin::processCmd] " << text->toLatin1();
87 return 0;
88 }
89
90 /**
91 * @brief Handle Tx bytes.
92 * @param len The number of received bytes
93 */
txBytes(int len)94 void CounterPlugin::txBytes(int len)
95 {
96 int value = m_counter->ui->m_lbl_tx_value->text().toInt();
97 value += len;
98 ui->m_lbl_tx_value->setText(QString::number(value));
99 }
100
101 /**
102 * @brief
103 * @param data
104 */
rxBytes(QByteArray data)105 void CounterPlugin::rxBytes(QByteArray data)
106 {
107 TRACE << "[CounterPlugin::rxBytes]: " << data.length();
108 int value = ui->m_lbl_rx_value->text().toInt();
109 value += data.length();
110 ui->m_lbl_rx_value->setText(QString::number(value));
111 }
112
113 /**
114 * @brief Return a pointer to the plugin data
115 * @return
116 */
plugin()117 const Plugin *CounterPlugin::plugin() { return m_plugin; }
118
119 /**
120 * @brief [SLOT] Send unload command to the plugin manager
121 */
removePlugin(bool)122 void CounterPlugin::removePlugin(bool) { emit unload(m_plugin); }
123
124 /**
125 * @brief Help message for the Byte counter plugin
126 */
helpMsg(void)127 void CounterPlugin::helpMsg(void)
128 {
129 QString help_str = tr("This plugin provides a TX/RX byte counting functionality.\n"
130 "This is useful in cases that you need to either count the\n"
131 "bytes of a transaction or verify that the correct number of\n"
132 "bytes has beed send or received.\n\n"
133 "The plugin also supports to save the current Tx/Rx values to\n"
134 "the MTx/MRx. Also, you can clear those values independently.\n\n"
135 "Press 'C' to clear the Tx/Rx values\n"
136 "Press 'M' to store the Tx/Rx to MTx/MRx\n"
137 "Press 'CM' to clear the MTx/Rx values\n");
138
139 QMessageBox::information(this, tr("How to use TCP forwarding"), help_str);
140 }
141