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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <boost/algorithm/string/join.hpp>
25 #include <libsigrokcxx/libsigrokcxx.hpp>
27 #include <pv/devicemanager.hpp>
29 #include "hardwaredevice.hpp"
31 using std::dynamic_pointer_cast;
32 using std::shared_ptr;
33 using std::static_pointer_cast;
37 using boost::algorithm::join;
39 using sigrok::HardwareDevice;
44 HardwareDevice::HardwareDevice(const std::shared_ptr<sigrok::Context> &context,
45 std::shared_ptr<sigrok::HardwareDevice> device) :
52 HardwareDevice::~HardwareDevice()
57 string HardwareDevice::full_name() const
59 vector<string> parts = {device_->vendor(), device_->model(),
60 device_->version(), device_->serial_number()};
61 if (device_->connection_id().length() > 0)
62 parts.push_back("(" + device_->connection_id() + ")");
63 return join(parts, " ");
66 shared_ptr<sigrok::HardwareDevice> HardwareDevice::hardware_device() const
68 return static_pointer_cast<sigrok::HardwareDevice>(device_);
71 string HardwareDevice::display_name(
72 const DeviceManager &device_manager) const
74 const auto hw_dev = hardware_device();
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() ==
84 dev->hardware_device()->model() ==
86 dev->device_ != device_;
89 vector<string> parts = {device_->vendor(), device_->model()};
92 parts.push_back(device_->version());
93 parts.push_back(device_->serial_number());
95 if ((device_->serial_number().length() == 0) &&
96 (device_->connection_id().length() > 0))
97 parts.push_back("(" + device_->connection_id() + ")");
100 return join(parts, " ");
103 void HardwareDevice::open()
110 } catch (const sigrok::Error &e) {
111 throw QString(e.what());
116 // Set up the session
117 session_ = context_->create_session();
118 session_->add_device(device_);
121 void HardwareDevice::close()
127 session_->remove_devices();
129 device_open_ = false;
132 } // namespace devices