summaryrefslogtreecommitdiff
path: root/rbutil/rbutilqt/autodetection.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'rbutil/rbutilqt/autodetection.cpp')
-rw-r--r--rbutil/rbutilqt/autodetection.cpp87
1 files changed, 83 insertions, 4 deletions
diff --git a/rbutil/rbutilqt/autodetection.cpp b/rbutil/rbutilqt/autodetection.cpp
index f29df535ab..c16b6976cc 100644
--- a/rbutil/rbutilqt/autodetection.cpp
+++ b/rbutil/rbutilqt/autodetection.cpp
@@ -19,9 +19,10 @@
19 19
20#include "autodetection.h" 20#include "autodetection.h"
21 21
22#if defined(Q_OS_LINUX) 22#if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
23#include <stdio.h> 23#include <stdio.h>
24#include <mntent.h> 24#include <mntent.h>
25#include <usb.h>
25#endif 26#endif
26 27
27Autodetection::Autodetection(QObject* parent): QObject(parent) 28Autodetection::Autodetection(QObject* parent): QObject(parent)
@@ -33,6 +34,9 @@ bool Autodetection::detect()
33{ 34{
34 m_device = ""; 35 m_device = "";
35 m_mountpoint = ""; 36 m_mountpoint = "";
37 m_errdev = "";
38
39 detectUsb();
36 40
37 // Try detection via rockbox.info / rbutil.log 41 // Try detection via rockbox.info / rbutil.log
38 QStringList mountpoints = getMountpoints(); 42 QStringList mountpoints = getMountpoints();
@@ -48,7 +52,8 @@ bool Autodetection::detect()
48 QSettings log(mountpoints.at(i) + "/.rockbox/rbutil.log", 52 QSettings log(mountpoints.at(i) + "/.rockbox/rbutil.log",
49 QSettings::IniFormat, this); 53 QSettings::IniFormat, this);
50 if(!log.value("platform").toString().isEmpty()) { 54 if(!log.value("platform").toString().isEmpty()) {
51 m_device = log.value("platform").toString(); 55 if(m_device.isEmpty())
56 m_device = log.value("platform").toString();
52 m_mountpoint = mountpoints.at(i); 57 m_mountpoint = mountpoints.at(i);
53 qDebug() << "rbutil.log detected:" << m_device << m_mountpoint; 58 qDebug() << "rbutil.log detected:" << m_device << m_mountpoint;
54 return true; 59 return true;
@@ -64,7 +69,8 @@ bool Autodetection::detect()
64 if(line.startsWith("Target: ")) 69 if(line.startsWith("Target: "))
65 { 70 {
66 line.remove("Target: "); 71 line.remove("Target: ");
67 m_device = line.trimmed(); // trim whitespaces 72 if(m_device.isEmpty())
73 m_device = line.trimmed(); // trim whitespaces
68 m_mountpoint = mountpoints.at(i); 74 m_mountpoint = mountpoints.at(i);
69 qDebug() << "rockbox-info.txt detected:" << m_device << m_mountpoint; 75 qDebug() << "rockbox-info.txt detected:" << m_device << m_mountpoint;
70 return true; 76 return true;
@@ -133,7 +139,9 @@ bool Autodetection::detect()
133 return true; 139 return true;
134 } 140 }
135 141
136 return false; 142 if(m_mountpoint.isEmpty() && m_device.isEmpty() && m_errdev.isEmpty())
143 return false;
144 return true;
137} 145}
138 146
139 147
@@ -192,3 +200,74 @@ QString Autodetection::resolveMountPoint(QString device)
192 return QString(""); 200 return QString("");
193 201
194} 202}
203
204
205/** @brief detect devices based on usb pid / vid.
206 * @return true upon success, false otherwise.
207 */
208bool Autodetection::detectUsb()
209{
210 // autodetection only uses the buildin device settings only
211 QSettings dev(":/ini/rbutil.ini", QSettings::IniFormat, this);
212
213 // get a list of ID -> target name
214 QStringList platforms;
215 dev.beginGroup("platforms");
216 platforms = dev.childKeys();
217 dev.endGroup();
218
219 // usbids holds the mapping in the form
220 // ((VID<<16)|(PID)), targetname
221 // the ini file needs to hold the IDs as hex values.
222 QMap<int, QString> usbids;
223 QMap<int, QString> usberror;
224
225 for(int i = 0; i < platforms.size(); i++) {
226 dev.beginGroup("platforms");
227 QString target = dev.value(platforms.at(i)).toString();
228 dev.endGroup();
229 dev.beginGroup(target);
230 if(!dev.value("usbid").toString().isEmpty())
231 usbids.insert(dev.value("usbid").toString().toInt(0, 16), target);
232 if(!dev.value("usberror").toString().isEmpty())
233 usberror.insert(dev.value("usberror").toString().toInt(0, 16), target);
234 dev.endGroup();
235 }
236
237 // usb pid detection
238#if defined(Q_OS_LINUX) | defined(Q_OS_MACX)
239 usb_init();
240 usb_find_busses();
241 usb_find_devices();
242 struct usb_bus *b;
243 b = usb_get_busses();
244
245 while(b) {
246 qDebug() << "bus:" << b->dirname << b->devices;
247 if(b->devices) {
248 qDebug() << "devices present.";
249 struct usb_device *u;
250 u = b->devices;
251 while(u) {
252 uint32_t id;
253 id = u->descriptor.idVendor << 16 | u->descriptor.idProduct;
254 qDebug("%x", id);
255
256 if(usbids.contains(id)) {
257 m_device = usbids.value(id);
258 return true;
259 }
260 if(usberror.contains(id)) {
261 m_errdev = usberror.value(id);
262 // we detected something, so return true
263 qDebug() << "detected device with problems via usb!";
264 return true;
265 }
266 u = u->next;
267 }
268 }
269 b = b->next;
270 }
271#endif
272 return false;
273}