DecoderSelector: Properly name utility methods
[pulseview.git] / pv / devices / hardwaredevice.cpp
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
30 using std::shared_ptr;
31 using std::static_pointer_cast;
32 using std::string;
33 using std::vector;
34
35 using boost::algorithm::join;
36
37 using sigrok::HardwareDevice;
38
39 namespace pv {
40 namespace devices {
41
42 HardwareDevice::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
50 HardwareDevice::~HardwareDevice()
51 {
52         close();
53 }
54
55 string HardwareDevice::full_name() const
56 {
57         vector<string> parts = {};
58         if (device_->vendor().length() > 0)
59                 parts.push_back(device_->vendor());
60         if (device_->model().length() > 0)
61                 parts.push_back(device_->model());
62         if (device_->version().length() > 0)
63                 parts.push_back(device_->version());
64         if (device_->serial_number().length() > 0)
65                 parts.push_back("[S/N: " + device_->serial_number() + "]");
66         if (device_->connection_id().length() > 0)
67                 parts.push_back("(" + device_->connection_id() + ")");
68         return join(parts, " ");
69 }
70
71 shared_ptr<sigrok::HardwareDevice> HardwareDevice::hardware_device() const
72 {
73         return static_pointer_cast<sigrok::HardwareDevice>(device_);
74 }
75
76 string HardwareDevice::display_name(
77         const DeviceManager &device_manager) const
78 {
79         const auto hw_dev = hardware_device();
80
81         // If we can find another device with the same model/vendor then
82         // we have at least two such devices and need to distinguish them.
83         const auto &devices = device_manager.devices();
84         const bool multiple_dev = hw_dev && any_of(
85                 devices.begin(), devices.end(),
86                 [&](shared_ptr<devices::HardwareDevice> dev) {
87                         return dev->hardware_device()->vendor() ==
88                                         hw_dev->vendor() &&
89                                 dev->hardware_device()->model() ==
90                                         hw_dev->model() &&
91                                 dev->device_ != device_;
92                 });
93
94         vector<string> parts = {};
95         if (device_->vendor().length() > 0)
96                 parts.push_back(device_->vendor());
97         if (device_->model().length() > 0)
98                 parts.push_back(device_->model());
99
100         if (multiple_dev) {
101                 if (device_->version().length() > 0)
102                         parts.push_back(device_->version());
103                 if (device_->serial_number().length() > 0)
104                         parts.push_back("[S/N: " + device_->serial_number() + "]");
105
106                 if ((device_->serial_number().length() == 0) &&
107                         (device_->connection_id().length() > 0))
108                         parts.push_back("(" + device_->connection_id() + ")");
109         }
110
111         return join(parts, " ");
112 }
113
114 void HardwareDevice::open()
115 {
116         if (device_open_)
117                 close();
118
119         try {
120                 device_->open();
121         } catch (const sigrok::Error &e) {
122                 throw QString(e.what());
123         }
124
125         device_open_ = true;
126
127         // Set up the session
128         session_ = context_->create_session();
129         session_->add_device(device_);
130 }
131
132 void HardwareDevice::close()
133 {
134         if (device_open_)
135                 device_->close();
136
137         if (session_)
138                 session_->remove_devices();
139
140         device_open_ = false;
141 }
142
143 } // namespace devices
144 } // namespace pv