1 | // Copyright (C) 2011-2012 Denis Shienkov <denis.shienkov@gmail.com> |
2 | // Copyright (C) 2011 Sergey Belyashov <Sergey.Belyashov@gmail.com> |
3 | // Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> |
4 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
5 | |
6 | #include "qserialportinfo.h" |
7 | #include "qserialportinfo_p.h" |
8 | #include "qserialport.h" |
9 | #include "qserialport_p.h" |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | // We changed from QScopedPointer to std::unique_ptr, make sure it's |
14 | // binary compatible. The QScopedPointer had a non-default deleter, but |
15 | // the deleter just provides a static function to use for deletion so we don't |
16 | // include it in this template definition (the deleter-class was deleted). |
17 | static_assert(sizeof(std::unique_ptr<QSerialPortInfoPrivate>) |
18 | == sizeof(QScopedPointer<QSerialPortInfoPrivate>)); |
19 | |
20 | /*! |
21 | \class QSerialPortInfo |
22 | |
23 | \brief Provides information about existing serial ports. |
24 | |
25 | \ingroup serialport-main |
26 | \inmodule QtSerialPort |
27 | \since 5.1 |
28 | |
29 | Use the static \l availablePorts() function to generate a list of |
30 | QSerialPortInfo objects. Each QSerialPortInfo object in the list represents |
31 | a single serial port and can be queried for the \l {portName}{port name}, |
32 | \l {systemLocation}{system location}, \l description, \l manufacturer, and |
33 | some other hardware parameters. The QSerialPortInfo class can also be |
34 | used as an input parameter for the \l {QSerialPort::}{setPort()} method of |
35 | the QSerialPort class. |
36 | |
37 | \section1 Example Usage |
38 | |
39 | The example code enumerates all available serial ports and prints their |
40 | parameters to console: |
41 | |
42 | \snippet doc_src_serialport.cpp enumerate_ports |
43 | |
44 | \section1 Port enumeration on Linux |
45 | |
46 | By default Linux uses \c {libudev} to enumerate the available serial ports. |
47 | If the library is not available, it falls back to reading files in the |
48 | \c {/sys/class/tty} directory. |
49 | |
50 | It is known that some versions of \c {libudev} have a bug and incorrectly |
51 | report VID and PID of a USB hub instead of the actual device. In such cases, |
52 | define the \c {QT_SERIALPORT_SKIP_UDEV_LOOKUP} environment variable to skip |
53 | the \c {libudev} lookup and only use the information from the |
54 | \c {/sys/class/tty} directory. |
55 | |
56 | \sa QSerialPort |
57 | */ |
58 | |
59 | /*! |
60 | Constructs an empty QSerialPortInfo object. |
61 | |
62 | \sa isNull() |
63 | */ |
64 | QSerialPortInfo::QSerialPortInfo() |
65 | { |
66 | } |
67 | |
68 | /*! |
69 | Constructs a copy of \a other. |
70 | */ |
71 | QSerialPortInfo::QSerialPortInfo(const QSerialPortInfo &other) |
72 | : d_ptr(other.d_ptr ? new QSerialPortInfoPrivate(*other.d_ptr) : nullptr) |
73 | { |
74 | } |
75 | |
76 | /*! |
77 | Constructs a QSerialPortInfo object from serial \a port. |
78 | */ |
79 | QSerialPortInfo::QSerialPortInfo(const QSerialPort &port) |
80 | : QSerialPortInfo(port.portName()) |
81 | { |
82 | } |
83 | |
84 | /*! |
85 | Constructs a QSerialPortInfo object from serial port \a name. |
86 | |
87 | This constructor finds the relevant serial port among the available ones |
88 | according to the port name \a name, and constructs the serial port info |
89 | instance for that port. |
90 | */ |
91 | QSerialPortInfo::QSerialPortInfo(const QString &name) |
92 | { |
93 | const auto infos = QSerialPortInfo::availablePorts(); |
94 | for (const QSerialPortInfo &info : infos) { |
95 | if (name == info.portName()) { |
96 | *this = info; |
97 | break; |
98 | } |
99 | } |
100 | } |
101 | |
102 | QSerialPortInfo::QSerialPortInfo(const QSerialPortInfoPrivate &dd) |
103 | : d_ptr(new QSerialPortInfoPrivate(dd)) |
104 | { |
105 | } |
106 | |
107 | /*! |
108 | Destroys the QSerialPortInfo object. References to the values in the |
109 | object become invalid. |
110 | */ |
111 | QSerialPortInfo::~QSerialPortInfo() |
112 | { |
113 | } |
114 | |
115 | /*! |
116 | Swaps QSerialPortInfo \a other with this QSerialPortInfo. This operation is |
117 | very fast and never fails. |
118 | */ |
119 | void QSerialPortInfo::swap(QSerialPortInfo &other) |
120 | { |
121 | d_ptr.swap(u&: other.d_ptr); |
122 | } |
123 | |
124 | /*! |
125 | Sets the QSerialPortInfo object to be equal to \a other. |
126 | */ |
127 | QSerialPortInfo& QSerialPortInfo::operator=(const QSerialPortInfo &other) |
128 | { |
129 | QSerialPortInfo(other).swap(other&: *this); |
130 | return *this; |
131 | } |
132 | |
133 | /*! |
134 | Returns the name of the serial port. |
135 | |
136 | \sa systemLocation() |
137 | */ |
138 | QString QSerialPortInfo::portName() const |
139 | { |
140 | Q_D(const QSerialPortInfo); |
141 | return !d ? QString() : d->portName; |
142 | } |
143 | |
144 | /*! |
145 | Returns the system location of the serial port. |
146 | |
147 | \sa portName() |
148 | */ |
149 | QString QSerialPortInfo::systemLocation() const |
150 | { |
151 | Q_D(const QSerialPortInfo); |
152 | return !d ? QString() : d->device; |
153 | } |
154 | |
155 | /*! |
156 | Returns the description string of the serial port, |
157 | if available; otherwise returns an empty string. |
158 | |
159 | \sa manufacturer(), serialNumber() |
160 | */ |
161 | QString QSerialPortInfo::description() const |
162 | { |
163 | Q_D(const QSerialPortInfo); |
164 | return !d ? QString() : d->description; |
165 | } |
166 | |
167 | /*! |
168 | Returns the manufacturer string of the serial port, |
169 | if available; otherwise returns an empty string. |
170 | |
171 | \sa description(), serialNumber() |
172 | */ |
173 | QString QSerialPortInfo::manufacturer() const |
174 | { |
175 | Q_D(const QSerialPortInfo); |
176 | return !d ? QString() : d->manufacturer; |
177 | } |
178 | |
179 | /*! |
180 | \since 5.3 |
181 | |
182 | Returns the serial number string of the serial port, |
183 | if available; otherwise returns an empty string. |
184 | |
185 | \note The serial number may include letters. |
186 | |
187 | \sa description(), manufacturer() |
188 | */ |
189 | QString QSerialPortInfo::serialNumber() const |
190 | { |
191 | Q_D(const QSerialPortInfo); |
192 | return !d ? QString() : d->serialNumber; |
193 | } |
194 | |
195 | /*! |
196 | Returns the 16-bit vendor number for the serial port, if available; |
197 | otherwise returns zero. |
198 | |
199 | \sa hasVendorIdentifier(), productIdentifier(), hasProductIdentifier() |
200 | */ |
201 | quint16 QSerialPortInfo::vendorIdentifier() const |
202 | { |
203 | Q_D(const QSerialPortInfo); |
204 | return !d ? 0 : d->vendorIdentifier; |
205 | } |
206 | |
207 | /*! |
208 | Returns the 16-bit product number for the serial port, if available; |
209 | otherwise returns zero. |
210 | |
211 | \sa hasProductIdentifier(), vendorIdentifier(), hasVendorIdentifier() |
212 | */ |
213 | quint16 QSerialPortInfo::productIdentifier() const |
214 | { |
215 | Q_D(const QSerialPortInfo); |
216 | return !d ? 0 : d->productIdentifier; |
217 | } |
218 | |
219 | /*! |
220 | Returns \c true if there is a valid \c 16-bit vendor number present; otherwise |
221 | returns \c false. |
222 | |
223 | \sa vendorIdentifier(), productIdentifier(), hasProductIdentifier() |
224 | */ |
225 | bool QSerialPortInfo::hasVendorIdentifier() const |
226 | { |
227 | Q_D(const QSerialPortInfo); |
228 | return !d ? false : d->hasVendorIdentifier; |
229 | } |
230 | |
231 | /*! |
232 | Returns \c true if there is a valid \c 16-bit product number present; otherwise |
233 | returns \c false. |
234 | |
235 | \sa productIdentifier(), vendorIdentifier(), hasVendorIdentifier() |
236 | */ |
237 | bool QSerialPortInfo::hasProductIdentifier() const |
238 | { |
239 | Q_D(const QSerialPortInfo); |
240 | return !d ? false : d->hasProductIdentifier; |
241 | } |
242 | |
243 | /*! |
244 | \fn bool QSerialPortInfo::isNull() const |
245 | |
246 | Returns whether this QSerialPortInfo object holds a |
247 | serial port definition. |
248 | */ |
249 | |
250 | /*! |
251 | \fn QList<qint32> QSerialPortInfo::standardBaudRates() |
252 | |
253 | Returns a list of available standard baud rates supported |
254 | by the target platform. |
255 | */ |
256 | QList<qint32> QSerialPortInfo::standardBaudRates() |
257 | { |
258 | return QSerialPortPrivate::standardBaudRates(); |
259 | } |
260 | |
261 | /*! |
262 | \fn QList<QSerialPortInfo> QSerialPortInfo::availablePorts() |
263 | |
264 | Returns a list of available serial ports on the system. |
265 | |
266 | \sa {Port enumeration on Linux} |
267 | */ |
268 | |
269 | QT_END_NAMESPACE |
270 | |