]> sigrok.org Git - pulseview.git/blame - pv/devices/hardwaredevice.cpp
Rename sigrok-logo-notext.svg to pulseview.svg.
[pulseview.git] / pv / devices / hardwaredevice.cpp
CommitLineData
da30ecb7
JH
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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
da30ecb7
JH
18 */
19
b485408f
JH
20#include <boost/algorithm/string/join.hpp>
21
da30ecb7
JH
22#include <QString>
23
24#include <libsigrokcxx/libsigrokcxx.hpp>
25
b485408f
JH
26#include <pv/devicemanager.hpp>
27
da30ecb7
JH
28#include "hardwaredevice.hpp"
29
30using std::shared_ptr;
31using std::static_pointer_cast;
b485408f
JH
32using std::string;
33using std::vector;
34
35using boost::algorithm::join;
36
37using sigrok::HardwareDevice;
da30ecb7
JH
38
39namespace pv {
40namespace devices {
41
6f925ba9
UH
42HardwareDevice::HardwareDevice(const shared_ptr<sigrok::Context> &context,
43 shared_ptr<sigrok::HardwareDevice> device) :
b961bf17 44 context_(context),
2ad82c2e
UH
45 device_open_(false)
46{
da30ecb7
JH
47 device_ = device;
48}
49
2ad82c2e
UH
50HardwareDevice::~HardwareDevice()
51{
4d6c6ea3 52 close();
da30ecb7
JH
53}
54
2ad82c2e
UH
55string HardwareDevice::full_name() const
56{
b485408f
JH
57 vector<string> parts = {device_->vendor(), device_->model(),
58 device_->version(), device_->serial_number()};
59 if (device_->connection_id().length() > 0)
60 parts.push_back("(" + device_->connection_id() + ")");
61 return join(parts, " ");
62}
63
2ad82c2e
UH
64shared_ptr<sigrok::HardwareDevice> HardwareDevice::hardware_device() const
65{
da30ecb7
JH
66 return static_pointer_cast<sigrok::HardwareDevice>(device_);
67}
68
b485408f 69string HardwareDevice::display_name(
2ad82c2e
UH
70 const DeviceManager &device_manager) const
71{
b485408f
JH
72 const auto hw_dev = hardware_device();
73
74 // If we can find another device with the same model/vendor then
75 // we have at least two such devices and need to distinguish them.
76 const auto &devices = device_manager.devices();
77 const bool multiple_dev = hw_dev && any_of(
78 devices.begin(), devices.end(),
79 [&](shared_ptr<devices::HardwareDevice> dev) {
80 return dev->hardware_device()->vendor() ==
81 hw_dev->vendor() &&
82 dev->hardware_device()->model() ==
83 hw_dev->model() &&
84 dev->device_ != device_;
85 });
86
87 vector<string> parts = {device_->vendor(), device_->model()};
88
89 if (multiple_dev) {
90 parts.push_back(device_->version());
91 parts.push_back(device_->serial_number());
92
93 if ((device_->serial_number().length() == 0) &&
94 (device_->connection_id().length() > 0))
95 parts.push_back("(" + device_->connection_id() + ")");
96 }
97
98 return join(parts, " ");
99}
100
2ad82c2e
UH
101void HardwareDevice::open()
102{
4d6c6ea3
SA
103 if (device_open_)
104 close();
105
b8f99b84
SA
106 try {
107 device_->open();
2ad82c2e 108 } catch (const sigrok::Error &e) {
b8f99b84
SA
109 throw QString(e.what());
110 }
da30ecb7 111
b8f99b84 112 device_open_ = true;
b961bf17 113
b8f99b84
SA
114 // Set up the session
115 session_ = context_->create_session();
116 session_->add_device(device_);
da30ecb7
JH
117}
118
2ad82c2e
UH
119void HardwareDevice::close()
120{
4d6c6ea3
SA
121 if (device_open_)
122 device_->close();
123
124 if (session_)
125 session_->remove_devices();
126
127 device_open_ = false;
128}
129
da30ecb7
JH
130} // namespace devices
131} // namespace pv