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