forked from neundorf/CuteCom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
counterplugin.cpp
140 lines (125 loc) · 4.85 KB
/
counterplugin.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
/*
* Copyright (c) 208 Dimitris Tassopoulos <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* For more information on the GPL, please go to:
* http://www.gnu.org/copyleft/gpl.html
*/
#include "counterplugin.h"
#include "ui_counterplugin.h"
#include <QMessageBox>
#define TRACE \
if (!debug) { \
} else \
qDebug()
static bool debug = false;
static CounterPlugin *m_counter = NULL;
static int processCmd(const QString *text, QString *new_text);
CounterPlugin::CounterPlugin(QFrame *parent, Settings *settings)
: QFrame(parent)
, ui(new Ui::CounterPlugin)
, m_settings(settings)
{
ui->setupUi(this);
/* Plugin by default disabled, no injection, has QFrame, no injection process cmd */
m_plugin = new Plugin(this, "Byte Counter", this, (Plugin::processCmd_fp)&processCmd);
/* reset values */
ui->m_lbl_rx_value->setText("0");
ui->m_lbl_tx_value->setText("0");
ui->m_lbl_mrx_value->setText("0");
ui->m_lbl_mtx_value->setText("0");
connect(ui->m_bt_unload, &QPushButton::clicked, this, &CounterPlugin::removePlugin);
connect(ui->m_bt_help, &QPushButton::clicked, this, &CounterPlugin::helpMsg);
connect(ui->m_bt_clear, &QPushButton::clicked, this, [=]() {
ui->m_lbl_rx_value->setText("0");
ui->m_lbl_tx_value->setText("0");
});
connect(ui->m_bt_clear_memory, &QPushButton::clicked, this, [=]() {
ui->m_lbl_mrx_value->setText("0");
ui->m_lbl_mtx_value->setText("0");
});
connect(ui->m_bt_memory, &QPushButton::clicked, this, [=]() {
ui->m_lbl_mrx_value->setText(ui->m_lbl_rx_value->text());
ui->m_lbl_mtx_value->setText(ui->m_lbl_tx_value->text());
});
m_counter = this;
TRACE << "[CounterPlugin::CounterPlugin]";
}
CounterPlugin::~CounterPlugin()
{
delete ui;
if (m_counter)
m_counter = NULL;
}
/**
* @brief Static function to use as proxy to call public
* member funtions from this object
* @param text The data that are about to be sent
* @param new_text The new data
* @return int Always retutn 0 in this case
*/
int processCmd(const QString *text, QString *new_text)
{
m_counter->txBytes(text->length());
TRACE << "[CounterPlugin::processCmd] " << text->toLatin1();
return 0;
}
/**
* @brief Handle Tx bytes.
* @param len The number of received bytes
*/
void CounterPlugin::txBytes(int len)
{
int value = m_counter->ui->m_lbl_tx_value->text().toInt();
value += len;
ui->m_lbl_tx_value->setText(QString::number(value));
}
/**
* @brief
* @param data
*/
void CounterPlugin::rxBytes(QByteArray data)
{
TRACE << "[CounterPlugin::rxBytes]: " << data.length();
int value = ui->m_lbl_rx_value->text().toInt();
value += data.length();
ui->m_lbl_rx_value->setText(QString::number(value));
}
/**
* @brief Return a pointer to the plugin data
* @return
*/
const Plugin *CounterPlugin::plugin() { return m_plugin; }
/**
* @brief [SLOT] Send unload command to the plugin manager
*/
void CounterPlugin::removePlugin(bool) { emit unload(m_plugin); }
/**
* @brief Help message for the Byte counter plugin
*/
void CounterPlugin::helpMsg(void)
{
QString help_str = tr("This plugin provides a TX/RX byte counting functionality.\n"
"This is useful in cases that you need to either count the\n"
"bytes of a transaction or verify that the correct number of\n"
"bytes has beed send or received.\n\n"
"The plugin also supports to save the current Tx/Rx values to\n"
"the MTx/MRx. Also, you can clear those values independently.\n\n"
"Press 'C' to clear the Tx/Rx values\n"
"Press 'M' to store the Tx/Rx to MTx/MRx\n"
"Press 'CM' to clear the MTx/Rx values\n");
QMessageBox::information(this, tr("How to use TCP forwarding"), help_str);
}