]> sigrok.org Git - pulseview.git/blame_incremental - pv/devices/hardwaredevice.cpp
HardwareDevice: Make serial number display a bit clearer.
[pulseview.git] / pv / devices / hardwaredevice.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2015 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <boost/algorithm/string/join.hpp>
21
22#include <QString>
23
24#include <libsigrokcxx/libsigrokcxx.hpp>
25
26#include <pv/devicemanager.hpp>
27
28#include "hardwaredevice.hpp"
29
30using std::shared_ptr;
31using std::static_pointer_cast;
32using std::string;
33using std::vector;
34
35using boost::algorithm::join;
36
37using sigrok::HardwareDevice;
38
39namespace pv {
40namespace devices {
41
42HardwareDevice::HardwareDevice(const shared_ptr<sigrok::Context> &context,
43 shared_ptr<sigrok::HardwareDevice> device) :
44 context_(context),
45 device_open_(false)
46{
47 device_ = device;
48}
49
50HardwareDevice::~HardwareDevice()
51{
52 close();
53}
54
55string HardwareDevice::full_name() const
56{
57 vector<string> parts = {device_->vendor(), device_->model(),
58 device_->version()};
59 if (device_->serial_number().length() > 0)
60 parts.push_back("[S/N: " + device_->serial_number() + "]");
61 if (device_->connection_id().length() > 0)
62 parts.push_back("(" + device_->connection_id() + ")");
63 return join(parts, " ");
64}
65
66shared_ptr<sigrok::HardwareDevice> HardwareDevice::hardware_device() const
67{
68 return static_pointer_cast<sigrok::HardwareDevice>(device_);
69}
70
71string HardwareDevice::display_name(
72 const DeviceManager &device_manager) const
73{
74 const auto hw_dev = hardware_device();
75
76 // If we can find another device with the same model/vendor then
77 // we have at least two such devices and need to distinguish them.
78 const auto &devices = device_manager.devices();
79 const bool multiple_dev = hw_dev && any_of(
80 devices.begin(), devices.end(),
81 [&](shared_ptr<devices::HardwareDevice> dev) {
82 return dev->hardware_device()->vendor() ==
83 hw_dev->vendor() &&
84 dev->hardware_device()->model() ==
85 hw_dev->model() &&
86 dev->device_ != device_;
87 });
88
89 vector<string> parts = {device_->vendor(), device_->model()};
90
91 if (multiple_dev) {
92 parts.push_back(device_->version());
93 parts.push_back("[S/N: " + device_->serial_number() + "]");
94
95 if ((device_->serial_number().length() == 0) &&
96 (device_->connection_id().length() > 0))
97 parts.push_back("(" + device_->connection_id() + ")");
98 }
99
100 return join(parts, " ");
101}
102
103void HardwareDevice::open()
104{
105 if (device_open_)
106 close();
107
108 try {
109 device_->open();
110 } catch (const sigrok::Error &e) {
111 throw QString(e.what());
112 }
113
114 device_open_ = true;
115
116 // Set up the session
117 session_ = context_->create_session();
118 session_->add_device(device_);
119}
120
121void HardwareDevice::close()
122{
123 if (device_open_)
124 device_->close();
125
126 if (session_)
127 session_->remove_devices();
128
129 device_open_ = false;
130}
131
132} // namespace devices
133} // namespace pv