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