Added devices::File as a common base of file devices
[pulseview.git] / pv / devices / hardwaredevice.cpp
blob6374dafe6e03aaddb219a05c1fa4c3a21ab71dd5
1 /*
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>
23 #include <QString>
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;
34 using std::string;
35 using std::vector;
37 using boost::algorithm::join;
39 using sigrok::HardwareDevice;
41 namespace pv {
42 namespace devices {
44 HardwareDevice::HardwareDevice(const std::shared_ptr<sigrok::Context> &context,
45 std::shared_ptr<sigrok::HardwareDevice> device) :
46 context_(context) {
47 device_ = device;
50 HardwareDevice::~HardwareDevice() {
51 device_->close();
52 if (session_)
53 session_->remove_devices();
56 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 {
65 return static_pointer_cast<sigrok::HardwareDevice>(device_);
68 string HardwareDevice::display_name(
69 const DeviceManager &device_manager) const {
70 const auto hw_dev = hardware_device();
72 // If we can find another device with the same model/vendor then
73 // we have at least two such devices and need to distinguish them.
74 const auto &devices = device_manager.devices();
75 const bool multiple_dev = hw_dev && any_of(
76 devices.begin(), devices.end(),
77 [&](shared_ptr<devices::HardwareDevice> dev) {
78 return dev->hardware_device()->vendor() ==
79 hw_dev->vendor() &&
80 dev->hardware_device()->model() ==
81 hw_dev->model() &&
82 dev->device_ != device_;
83 });
85 vector<string> parts = {device_->vendor(), device_->model()};
87 if (multiple_dev) {
88 parts.push_back(device_->version());
89 parts.push_back(device_->serial_number());
91 if ((device_->serial_number().length() == 0) &&
92 (device_->connection_id().length() > 0))
93 parts.push_back("(" + device_->connection_id() + ")");
96 return join(parts, " ");
99 void HardwareDevice::create() {
100 // Open the device
101 try {
102 device_->open();
103 } catch(const sigrok::Error &e) {
104 throw QString(e.what());
107 // Set up the session
108 session_ = context_->create_session();
109 session_->add_device(device_);
112 } // namespace devices
113 } // namespace pv