1 /* bluetooth_devices_dialog.cpp
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later*/
9 #include "bluetooth_devices_dialog.h"
10 #include <ui_bluetooth_devices_dialog.h>
12 #include "bluetooth_device_dialog.h"
14 #include <ui/qt/utils/color_utils.h>
16 #include "epan/epan.h"
17 #include "epan/addr_resolv.h"
18 #include "epan/to_str.h"
19 #include "epan/epan_dissect.h"
20 #include "epan/prefs.h"
21 #include "epan/dissectors/packet-bluetooth.h"
22 #include "epan/dissectors/packet-bthci_evt.h"
24 #include <ui/qt/utils/variant_pointer.h>
26 #include "ui/simple_dialog.h"
29 #include <QContextMenuEvent>
30 #include <QPushButton>
31 #include <QTreeWidget>
32 #include <QFileDialog>
34 static const int column_number_bd_addr = 0;
35 static const int column_number_bd_addr_oui = 1;
36 static const int column_number_name = 2;
37 static const int column_number_lmp_version = 3;
38 static const int column_number_lmp_subversion = 4;
39 static const int column_number_manufacturer = 5;
40 static const int column_number_hci_version = 6;
41 static const int column_number_hci_revision = 7;
42 static const int column_number_is_local_adapter = 8;
46 bluetooth_device_tap_packet(void *tapinfo_ptr, packet_info *pinfo, epan_dissect_t *edt, const void* data)
48 bluetooth_devices_tapinfo_t *tapinfo = (bluetooth_devices_tapinfo_t *) tapinfo_ptr;
50 if (tapinfo->tap_packet)
51 tapinfo->tap_packet(tapinfo, pinfo, edt, data);
57 bluetooth_device_tap_reset(void *tapinfo_ptr)
59 bluetooth_devices_tapinfo_t *tapinfo = (bluetooth_devices_tapinfo_t *) tapinfo_ptr;
61 if (tapinfo->tap_reset)
62 tapinfo->tap_reset(tapinfo);
65 BluetoothDevicesDialog::BluetoothDevicesDialog(QWidget &parent, CaptureFile &cf, PacketList *packet_list) :
66 WiresharkDialog(parent, cf),
67 ui(new Ui::BluetoothDevicesDialog)
70 loadGeometry(parent.width() * 4 / 5, parent.height() * 2 / 3);
72 packet_list_ = packet_list;
74 connect(ui->tableTreeWidget, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(tableContextMenu(const QPoint &)));
75 connect(ui->tableTreeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(tableItemDoubleClicked(QTreeWidgetItem *, int)));
76 connect(ui->interfaceComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(interfaceCurrentIndexChanged(int)));
77 connect(ui->showInformationStepsCheckBox, SIGNAL(stateChanged(int)), this, SLOT(showInformationStepsChanged(int)));
79 ui->tableTreeWidget->sortByColumn(column_number_bd_addr, Qt::AscendingOrder);
81 ui->tableTreeWidget->setStyleSheet("QTreeView::item:hover{background-color:lightyellow; color:black;}");
83 context_menu_.addActions(QList<QAction *>() << ui->actionMark_Unmark_Cell);
84 context_menu_.addActions(QList<QAction *>() << ui->actionMark_Unmark_Row);
85 context_menu_.addActions(QList<QAction *>() << ui->actionCopy_Cell);
86 context_menu_.addActions(QList<QAction *>() << ui->actionCopy_Rows);
87 context_menu_.addActions(QList<QAction *>() << ui->actionCopy_All);
88 context_menu_.addActions(QList<QAction *>() << ui->actionSave_as_image);
90 tapinfo_.tap_packet = tapPacket;
91 tapinfo_.tap_reset = tapReset;
94 registerTapListener("bluetooth.device", &tapinfo_, NULL,
96 bluetooth_device_tap_reset,
97 bluetooth_device_tap_packet,
100 ui->hintLabel->setText(ui->hintLabel->text().arg(0));
102 cap_file_.retapPackets();
106 BluetoothDevicesDialog::~BluetoothDevicesDialog()
112 void BluetoothDevicesDialog::captureFileClosing()
114 ui->interfaceComboBox->setEnabled(FALSE);
115 ui->showInformationStepsCheckBox->setEnabled(FALSE);
117 WiresharkDialog::captureFileClosing();
121 void BluetoothDevicesDialog::changeEvent(QEvent *event)
125 switch (event->type())
127 case QEvent::LanguageChange:
128 ui->retranslateUi(this);
134 QDialog::changeEvent(event);
138 void BluetoothDevicesDialog::keyPressEvent(QKeyEvent *event)
140 /* NOTE: Do nothing*, but in real it "takes focus" from button_box so allow user
141 * to use Enter button to jump to frame from tree widget */
142 /* * - reimplement shortcuts from contex menu */
144 if (event->modifiers() & Qt::ControlModifier && event->key()== Qt::Key_M)
145 on_actionMark_Unmark_Row_triggered();
149 void BluetoothDevicesDialog::tableContextMenu(const QPoint &pos)
151 context_menu_.exec(ui->tableTreeWidget->viewport()->mapToGlobal(pos));
154 void BluetoothDevicesDialog::tableItemDoubleClicked(QTreeWidgetItem *item, int)
156 bluetooth_item_data_t *item_data;
157 BluetoothDeviceDialog *bluetooth_device_dialog;
159 item_data = VariantPointer<bluetooth_item_data_t>::asPtr(item->data(0, Qt::UserRole));
160 bluetooth_device_dialog = new BluetoothDeviceDialog(*this, cap_file_, item->text(column_number_bd_addr), item->text(column_number_name), item_data->interface_id, item_data->adapter_id, !item->text(column_number_is_local_adapter).isEmpty());
161 connect(bluetooth_device_dialog, SIGNAL(goToPacket(int)),
162 packet_list_, SLOT(goToPacket(int)));
163 bluetooth_device_dialog->show();
167 void BluetoothDevicesDialog::on_actionMark_Unmark_Cell_triggered()
172 if (ui->tableTreeWidget->currentItem()->background(ui->tableTreeWidget->currentColumn()) == QBrush(ColorUtils::fromColorT(&prefs.gui_marked_bg))) {
176 fg = QBrush(ColorUtils::fromColorT(&prefs.gui_marked_fg));
177 bg = QBrush(ColorUtils::fromColorT(&prefs.gui_marked_bg));
180 ui->tableTreeWidget->currentItem()->setForeground(ui->tableTreeWidget->currentColumn(), fg);
181 ui->tableTreeWidget->currentItem()->setBackground(ui->tableTreeWidget->currentColumn(), bg);
185 void BluetoothDevicesDialog::on_actionMark_Unmark_Row_triggered()
189 bool is_marked = TRUE;
191 for (int i = 0; i < ui->tableTreeWidget->columnCount(); i += 1) {
192 if (ui->tableTreeWidget->currentItem()->background(i) != QBrush(ColorUtils::fromColorT(&prefs.gui_marked_bg)))
200 fg = QBrush(ColorUtils::fromColorT(&prefs.gui_marked_fg));
201 bg = QBrush(ColorUtils::fromColorT(&prefs.gui_marked_bg));
204 for (int i = 0; i < ui->tableTreeWidget->columnCount(); i += 1) {
205 ui->tableTreeWidget->currentItem()->setForeground(i, fg);
206 ui->tableTreeWidget->currentItem()->setBackground(i, bg);
211 void BluetoothDevicesDialog::on_actionCopy_Cell_triggered()
213 QClipboard *clipboard = QApplication::clipboard();
216 copy = QString(ui->tableTreeWidget->currentItem()->text(ui->tableTreeWidget->currentColumn()));
218 clipboard->setText(copy);
222 void BluetoothDevicesDialog::on_actionCopy_Rows_triggered()
224 QClipboard *clipboard = QApplication::clipboard();
226 QList<QTreeWidgetItem *> items;
227 QList<QTreeWidgetItem *>::iterator i_item;
229 items = ui->tableTreeWidget->selectedItems();
231 for (i_item = items.begin(); i_item != items.end(); ++i_item) {
232 copy += QString("%1 %2 %3 %4 %5 %6 %7 %8 %9\n")
233 .arg((*i_item)->text(column_number_bd_addr), -20)
234 .arg((*i_item)->text(column_number_bd_addr_oui), -20)
235 .arg((*i_item)->text(column_number_name), -30)
236 .arg((*i_item)->text(column_number_lmp_version), -20)
237 .arg((*i_item)->text(column_number_lmp_subversion), -20)
238 .arg((*i_item)->text(column_number_manufacturer), -30)
239 .arg((*i_item)->text(column_number_hci_version), -20)
240 .arg((*i_item)->text(column_number_hci_revision), -20)
241 .arg((*i_item)->text(column_number_is_local_adapter), -20);
244 clipboard->setText(copy);
247 void BluetoothDevicesDialog::tapReset(void *tapinfo_ptr)
249 bluetooth_devices_tapinfo_t *tapinfo = (bluetooth_devices_tapinfo_t *) tapinfo_ptr;
250 BluetoothDevicesDialog *bluetooth_devices_dialog = static_cast<BluetoothDevicesDialog *>(tapinfo->ui);
252 bluetooth_devices_dialog->ui->tableTreeWidget->clear();
255 gboolean BluetoothDevicesDialog::tapPacket(void *tapinfo_ptr, packet_info *pinfo, epan_dissect_t *, const void *data)
257 bluetooth_devices_tapinfo_t *tapinfo = static_cast<bluetooth_devices_tapinfo_t *>(tapinfo_ptr);
258 BluetoothDevicesDialog *dialog = static_cast<BluetoothDevicesDialog *>(tapinfo->ui);
259 bluetooth_device_tap_t *tap_device = static_cast<bluetooth_device_tap_t *>(const_cast<void *>(data));
263 QTreeWidgetItem *item = NULL;
265 if (dialog->file_closed_)
268 if (pinfo->phdr->presence_flags & WTAP_HAS_INTERFACE_ID) {
270 const char *interface_name;
272 interface_name = epan_get_interface_name(pinfo->epan, pinfo->phdr->interface_id);
273 interface = wmem_strdup_printf(wmem_packet_scope(), "%u: %s", pinfo->phdr->interface_id, interface_name);
275 if (dialog->ui->interfaceComboBox->findText(interface) == -1)
276 dialog->ui->interfaceComboBox->addItem(interface);
278 if (interface && dialog->ui->interfaceComboBox->currentIndex() > 0) {
279 if (dialog->ui->interfaceComboBox->currentText() != interface)
284 if (tap_device->has_bd_addr) {
285 bd_addr.sprintf("%02x:%02x:%02x:%02x:%02x:%02x", tap_device->bd_addr[0], tap_device->bd_addr[1], tap_device->bd_addr[2], tap_device->bd_addr[3], tap_device->bd_addr[4], tap_device->bd_addr[5]);
287 manuf = get_ether_name(tap_device->bd_addr);
291 bd_addr_oui = QString(manuf);
292 pos = bd_addr_oui.indexOf('_');
296 bd_addr_oui.remove(pos, bd_addr_oui.size());
304 if (dialog->ui->showInformationStepsCheckBox->checkState() != Qt::Checked) {
305 QTreeWidgetItemIterator i_item(dialog->ui->tableTreeWidget);
308 QTreeWidgetItem *current_item = static_cast<QTreeWidgetItem*>(*i_item);
309 bluetooth_item_data_t *item_data = VariantPointer<bluetooth_item_data_t>::asPtr(current_item->data(0, Qt::UserRole));
311 if ((tap_device->has_bd_addr && current_item->text(column_number_bd_addr) == bd_addr) ||
312 (tap_device->is_local &&
313 item_data->interface_id == tap_device->interface_id &&
314 item_data->adapter_id == tap_device->adapter_id &&
315 !current_item->text(column_number_is_local_adapter).isEmpty())) {
324 item = new QTreeWidgetItem(dialog->ui->tableTreeWidget);
325 item->setText(column_number_bd_addr, bd_addr);
326 item->setText(column_number_bd_addr_oui, bd_addr_oui);
327 if (tap_device->is_local) {
328 item->setText(column_number_is_local_adapter, tr("true"));
331 bluetooth_item_data_t *item_data = wmem_new(wmem_file_scope(), bluetooth_item_data_t);
332 item_data->interface_id = tap_device->interface_id;
333 item_data->adapter_id = tap_device->adapter_id;
334 item_data->frame_number = pinfo->num;
335 item->setData(0, Qt::UserRole, VariantPointer<bluetooth_item_data_t>::asQVariant(item_data));
338 if (tap_device->type == BLUETOOTH_DEVICE_BD_ADDR) {
339 item->setText(column_number_bd_addr, bd_addr);
340 item->setText(column_number_bd_addr_oui, bd_addr_oui);
343 if (tap_device->type == BLUETOOTH_DEVICE_NAME) {
344 item->setText(column_number_name, tap_device->data.name);
347 if (tap_device->type == BLUETOOTH_DEVICE_LOCAL_ADAPTER)
348 item->setText(column_number_is_local_adapter, tr("true"));
350 if (tap_device->type == BLUETOOTH_DEVICE_LOCAL_VERSION) {
351 item->setText(column_number_hci_version, val_to_str_const(tap_device->data.local_version.hci_version, bthci_evt_hci_version, "Unknown 0x%02x"));
352 item->setText(column_number_hci_revision, QString("").sprintf("%u", tap_device->data.local_version.hci_revision));
353 item->setText(column_number_lmp_version, val_to_str_const(tap_device->data.local_version.lmp_version, bthci_evt_lmp_version, "Unknown 0x%02x"));
354 item->setText(column_number_lmp_subversion, QString("").sprintf("%u", tap_device->data.local_version.lmp_subversion));
355 item->setText(column_number_manufacturer, val_to_str_ext_const(tap_device->data.local_version.manufacturer, &bluetooth_company_id_vals_ext, "Unknown 0x%04x"));
357 if (tap_device->type == BLUETOOTH_DEVICE_REMOTE_VERSION) {
358 item->setText(column_number_lmp_version, val_to_str_const(tap_device->data.remote_version.lmp_version, bthci_evt_lmp_version, "Unknown 0x%02x"));
359 item->setText(column_number_lmp_subversion, QString("").sprintf("%u", tap_device->data.remote_version.lmp_subversion));
360 item->setText(column_number_manufacturer, val_to_str_ext_const(tap_device->data.remote_version.manufacturer, &bluetooth_company_id_vals_ext, "Unknown 0x%04x"));
363 for (int i = 0; i < dialog->ui->tableTreeWidget->columnCount(); i++) {
364 dialog->ui->tableTreeWidget->resizeColumnToContents(i);
367 dialog->ui->hintLabel->setText(QString(tr("%1 items; Right click for more option; Double click for device details")).arg(dialog->ui->tableTreeWidget->topLevelItemCount()));
372 void BluetoothDevicesDialog::interfaceCurrentIndexChanged(int)
374 cap_file_.retapPackets();
377 void BluetoothDevicesDialog::showInformationStepsChanged(int)
379 cap_file_.retapPackets();
382 void BluetoothDevicesDialog::on_tableTreeWidget_itemActivated(QTreeWidgetItem *item, int)
387 bluetooth_item_data_t *item_data = VariantPointer<bluetooth_item_data_t>::asPtr(item->data(0, Qt::UserRole));
389 emit goToPacket(item_data->frame_number);
393 void BluetoothDevicesDialog::on_actionCopy_All_triggered()
395 QClipboard *clipboard = QApplication::clipboard();
397 QTreeWidgetItemIterator i_item(ui->tableTreeWidget);
398 QTreeWidgetItem *item;
400 item = ui->tableTreeWidget->headerItem();
402 copy += QString("%1 %2 %3 %4 %5 %6 %7 %8 %9\n")
403 .arg(item->text(column_number_bd_addr), -20)
404 .arg(item->text(column_number_bd_addr_oui), -20)
405 .arg(item->text(column_number_name), -30)
406 .arg(item->text(column_number_lmp_version), -20)
407 .arg(item->text(column_number_lmp_subversion), -20)
408 .arg(item->text(column_number_manufacturer), -30)
409 .arg(item->text(column_number_hci_version), -20)
410 .arg(item->text(column_number_hci_revision), -20)
411 .arg(item->text(column_number_is_local_adapter), -20);
414 item = static_cast<QTreeWidgetItem*>(*i_item);
415 copy += QString("%1 %2 %3 %4 %5 %6 %7 %8 %9\n")
416 .arg(item->text(column_number_bd_addr), -20)
417 .arg(item->text(column_number_bd_addr_oui), -20)
418 .arg(item->text(column_number_name), -30)
419 .arg(item->text(column_number_lmp_version), -20)
420 .arg(item->text(column_number_lmp_subversion), -20)
421 .arg(item->text(column_number_manufacturer), -30)
422 .arg(item->text(column_number_hci_version), -20)
423 .arg(item->text(column_number_hci_revision), -20)
424 .arg(item->text(column_number_is_local_adapter), -20);
428 clipboard->setText(copy);
431 void BluetoothDevicesDialog::on_actionSave_as_image_triggered()
435 QString fileName = QFileDialog::getSaveFileName(this,
436 tr("Save Table Image"),
437 "bluetooth_devices_table.png",
438 tr("PNG Image (*.png)"));
440 if (fileName.isEmpty()) return;
442 image = QPixmap::grabWidget(ui->tableTreeWidget);
443 image.save(fileName, "PNG");
446 void BluetoothDevicesDialog::on_buttonBox_clicked(QAbstractButton *)
448 /* if (button == foo_button_) */
457 * indent-tabs-mode: nil
460 * ex: set shiftwidth=4 tabstop=8 expandtab:
461 * :indentSize=4:tabSize=8:noTabs=true: