2016-03-11 07:28:08 -06:00
|
|
|
/*
|
|
|
|
Copyright (C) 2016 Srivats P.
|
|
|
|
|
|
|
|
This file is part of "Ostinato"
|
|
|
|
|
|
|
|
This 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, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "devicemodel.h"
|
|
|
|
|
2016-03-14 09:41:40 -05:00
|
|
|
#include "arpstatusmodel.h"
|
2016-03-14 10:46:46 -05:00
|
|
|
#include "ndpstatusmodel.h"
|
2016-03-11 07:28:08 -06:00
|
|
|
#include "port.h"
|
|
|
|
|
|
|
|
#include "emulproto.pb.h"
|
|
|
|
#include "uint128.h"
|
|
|
|
|
2016-03-14 09:41:40 -05:00
|
|
|
#include <QBrush>
|
|
|
|
#include <QColor>
|
|
|
|
#include <QFont>
|
2016-03-11 07:28:08 -06:00
|
|
|
#include <QHostAddress>
|
|
|
|
|
|
|
|
enum {
|
|
|
|
kMacAddress,
|
|
|
|
kVlans,
|
|
|
|
kIp4Address,
|
|
|
|
kIp4Gateway,
|
|
|
|
kIp6Address,
|
|
|
|
kIp6Gateway,
|
2016-03-12 07:26:35 -06:00
|
|
|
kArpInfo,
|
|
|
|
kNdpInfo,
|
2016-03-11 07:28:08 -06:00
|
|
|
kFieldCount
|
|
|
|
};
|
|
|
|
|
|
|
|
static QStringList columns_ = QStringList()
|
|
|
|
<< "Mac"
|
|
|
|
<< "Vlans"
|
|
|
|
<< "IPv4 Address"
|
|
|
|
<< "IPv4 Gateway"
|
|
|
|
<< "IPv6 Address"
|
2016-03-12 07:26:35 -06:00
|
|
|
<< "IPv6 Gateway"
|
|
|
|
<< "ARP"
|
|
|
|
<< "NDP";
|
2016-03-11 07:28:08 -06:00
|
|
|
|
|
|
|
DeviceModel::DeviceModel(QObject *parent)
|
|
|
|
: QAbstractTableModel(parent)
|
|
|
|
{
|
|
|
|
port_ = NULL;
|
2016-03-14 09:41:40 -05:00
|
|
|
arpStatusModel_ = new ArpStatusModel(this);
|
2016-03-14 10:46:46 -05:00
|
|
|
ndpStatusModel_ = new NdpStatusModel(this);
|
2016-03-11 07:28:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int DeviceModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
if (!port_ || parent.isValid())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return port_->numDevices();
|
|
|
|
}
|
|
|
|
|
|
|
|
int DeviceModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
if (parent.isValid())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return columns_.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DeviceModel::headerData(
|
|
|
|
int section,
|
|
|
|
Qt::Orientation orientation,
|
|
|
|
int role) const
|
|
|
|
{
|
|
|
|
if (role != Qt::DisplayRole)
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
switch (orientation) {
|
|
|
|
case Qt::Horizontal:
|
|
|
|
return columns_[section];
|
|
|
|
case Qt::Vertical:
|
|
|
|
return QString("%1").arg(section + 1);
|
|
|
|
default:
|
|
|
|
Q_ASSERT(false); // Unreachable
|
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DeviceModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
QString str;
|
|
|
|
|
|
|
|
if (!port_ || !index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
int devIdx = index.row();
|
|
|
|
int field = index.column();
|
|
|
|
|
|
|
|
Q_ASSERT(devIdx < port_->numDevices());
|
|
|
|
Q_ASSERT(field < kFieldCount);
|
|
|
|
|
|
|
|
const OstEmul::Device *dev = port_->deviceByIndex(devIdx);
|
|
|
|
|
|
|
|
Q_ASSERT(dev);
|
|
|
|
|
|
|
|
switch (field) {
|
|
|
|
case kMacAddress:
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
return QString("%1").arg(dev->mac(), 6*2, 16, QChar('0'))
|
|
|
|
.replace(QRegExp("([0-9a-fA-F]{2}\\B)"), "\\1:")
|
|
|
|
.toUpper();
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
case kVlans:
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
if (!dev->vlan_size())
|
|
|
|
return QString("None");
|
|
|
|
for (int i = 0; i < dev->vlan_size(); i++)
|
|
|
|
str.append(i == 0 ? "" : ", ")
|
|
|
|
.append(QString::number(dev->vlan(i) & 0xfff));
|
|
|
|
return str;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
case kIp4Address:
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
if (dev->has_ip4_prefix_length())
|
|
|
|
return QString("%1/%2")
|
|
|
|
.arg(QHostAddress(dev->ip4()).toString())
|
|
|
|
.arg(dev->ip4_prefix_length());
|
|
|
|
else
|
|
|
|
return QString("--");
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
case kIp4Gateway:
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
if (dev->has_ip4_prefix_length())
|
|
|
|
return QHostAddress(dev->ip4_default_gateway())
|
|
|
|
.toString();
|
|
|
|
else
|
|
|
|
return QString("--");
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
case kIp6Address:
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
if (dev->has_ip6_prefix_length()) {
|
|
|
|
OstEmul::Ip6Address ip = dev->ip6();
|
|
|
|
return QHostAddress(
|
|
|
|
UInt128(ip.hi(), ip.lo()).toArray())
|
|
|
|
.toString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return QString("--");
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
case kIp6Gateway:
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
if (dev->has_ip6_prefix_length()) {
|
|
|
|
OstEmul::Ip6Address ip = dev->ip6_default_gateway();
|
|
|
|
return QHostAddress(
|
|
|
|
UInt128(ip.hi(), ip.lo()).toArray())
|
|
|
|
.toString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return QString("--");
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
|
2016-03-12 07:26:35 -06:00
|
|
|
case kArpInfo:
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
2016-03-14 09:41:40 -05:00
|
|
|
if (dev->has_ip4_prefix_length())
|
|
|
|
return QString("%1/%2")
|
2016-03-12 07:26:35 -06:00
|
|
|
.arg(port_->numArpResolved(devIdx))
|
|
|
|
.arg(port_->numArp(devIdx));
|
2016-03-14 09:41:40 -05:00
|
|
|
else
|
|
|
|
return QString("--");
|
2016-03-12 07:26:35 -06:00
|
|
|
default:
|
2016-03-14 09:41:40 -05:00
|
|
|
if (dev->has_ip4_prefix_length())
|
|
|
|
return drillableStyle(role);
|
2016-03-12 07:26:35 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
case kNdpInfo:
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
2016-03-14 09:41:40 -05:00
|
|
|
if (dev->has_ip6_prefix_length())
|
|
|
|
return QString("%1/%2")
|
2016-03-12 07:26:35 -06:00
|
|
|
.arg(port_->numNdpResolved(devIdx))
|
|
|
|
.arg(port_->numNdp(devIdx));
|
2016-03-14 09:41:40 -05:00
|
|
|
else
|
|
|
|
return QString("--");
|
2016-03-12 07:26:35 -06:00
|
|
|
default:
|
2016-03-14 09:41:40 -05:00
|
|
|
if (dev->has_ip6_prefix_length())
|
|
|
|
return drillableStyle(role);
|
2016-03-12 07:26:35 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
|
2016-03-11 07:28:08 -06:00
|
|
|
default:
|
|
|
|
Q_ASSERT(false); // unreachable!
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
qWarning("%s: Unsupported field #%d", __FUNCTION__, field);
|
2016-03-14 09:41:40 -05:00
|
|
|
|
2016-03-11 07:28:08 -06:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceModel::setPort(Port *port)
|
|
|
|
{
|
|
|
|
port_ = port;
|
|
|
|
if (port_)
|
2016-03-12 07:26:35 -06:00
|
|
|
connect(port_, SIGNAL(deviceInfoChanged()), SLOT(updateDeviceList()));
|
2016-03-11 07:28:08 -06:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
2016-03-14 09:41:40 -05:00
|
|
|
QAbstractItemModel* DeviceModel::detailModel(const QModelIndex &index)
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
switch(index.column()) {
|
|
|
|
case kArpInfo:
|
|
|
|
arpStatusModel_->setDeviceIndex(port_, index.row());
|
|
|
|
return arpStatusModel_;
|
|
|
|
case kNdpInfo:
|
2016-03-14 10:46:46 -05:00
|
|
|
ndpStatusModel_->setDeviceIndex(port_, index.row());
|
|
|
|
return ndpStatusModel_;
|
2016-03-14 09:41:40 -05:00
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-11 07:28:08 -06:00
|
|
|
void DeviceModel::updateDeviceList()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
2016-03-14 09:41:40 -05:00
|
|
|
|
|
|
|
// Style roles for drillable fields
|
|
|
|
QVariant DeviceModel::drillableStyle(int role) const
|
|
|
|
{
|
|
|
|
QFont f;
|
|
|
|
switch (role) {
|
|
|
|
case Qt::ToolTipRole:
|
|
|
|
return QString("Click for details ...");
|
|
|
|
case Qt::ForegroundRole:
|
|
|
|
return QBrush(QColor(Qt::blue));
|
|
|
|
case Qt::FontRole:
|
|
|
|
f.setUnderline(true);
|
|
|
|
return f;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|