2 * This file is part of the PulseView project.
4 * Copyright (C) 2015 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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/>.
20 #include <boost/algorithm/string/join.hpp>
24 #include <libsigrokcxx/libsigrokcxx.hpp>
26 #include <pv/devicemanager.hpp>
28 #include "hardwaredevice.hpp"
30 using std::shared_ptr;
31 using std::static_pointer_cast;
35 using boost::algorithm::join;
37 using sigrok::HardwareDevice;
42 HardwareDevice::HardwareDevice(const shared_ptr<sigrok::Context> &context,
43 shared_ptr<sigrok::HardwareDevice> device) :
50 HardwareDevice::~HardwareDevice()
55 string HardwareDevice::full_name() const
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, " ");
64 shared_ptr<sigrok::HardwareDevice> HardwareDevice::hardware_device() const
66 return static_pointer_cast<sigrok::HardwareDevice>(device_);
69 string HardwareDevice::display_name(
70 const DeviceManager &device_manager) const
72 const auto hw_dev = hardware_device();
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() ==
82 dev->hardware_device()->model() ==
84 dev->device_ != device_;
87 vector<string> parts = {device_->vendor(), device_->model()};
90 parts.push_back(device_->version());
91 parts.push_back(device_->serial_number());
93 if ((device_->serial_number().length() == 0) &&
94 (device_->connection_id().length() > 0))
95 parts.push_back("(" + device_->connection_id() + ")");
98 return join(parts, " ");
101 void HardwareDevice::open()
108 } catch (const sigrok::Error &e) {
109 throw QString(e.what());
114 // Set up the session
115 session_ = context_->create_session();
116 session_->add_device(device_);
119 void HardwareDevice::close()
125 session_->remove_devices();
127 device_open_ = false;
130 } // namespace devices