fix a stupid bug which made mountpoint detection based on folders non-functional...
[Rockbox.git] / rbutil / rbutilqt / autodetection.cpp
blob7c194c8bbf824a1bd6d2b26a4cd3f5e5f7f41286
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Wenger
10 * $Id$
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "autodetection.h"
22 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
23 #include <stdio.h>
24 #include <usb.h>
25 #endif
26 #if defined(Q_OS_LINUX)
27 #include <mntent.h>
28 #endif
29 #if defined(Q_OS_MACX)
30 #include <sys/param.h>
31 #include <sys/ucred.h>
32 #include <sys/mount.h>
33 #endif
34 #if defined(Q_OS_WIN32)
35 #if defined(UNICODE)
36 #define _UNICODE
37 #endif
38 #include <stdio.h>
39 #include <tchar.h>
40 #include <windows.h>
41 #include <setupapi.h>
42 #endif
44 Autodetection::Autodetection(QObject* parent): QObject(parent)
49 bool Autodetection::detect()
51 m_device = "";
52 m_mountpoint = "";
53 m_errdev = "";
55 detectUsb();
57 // Try detection via rockbox.info / rbutil.log
58 QStringList mountpoints = getMountpoints();
60 for(int i=0; i< mountpoints.size();i++)
62 // do the file checking
63 QDir dir(mountpoints.at(i));
64 if(dir.exists())
66 qDebug() << "file checking:" << mountpoints.at(i);
67 // check logfile first.
68 if(QFile(mountpoints.at(i) + "/.rockbox/rbutil.log").exists()) {
69 QSettings log(mountpoints.at(i) + "/.rockbox/rbutil.log",
70 QSettings::IniFormat, this);
71 if(!log.value("platform").toString().isEmpty()) {
72 if(m_device.isEmpty())
73 m_device = log.value("platform").toString();
74 m_mountpoint = mountpoints.at(i);
75 qDebug() << "rbutil.log detected:" << m_device << m_mountpoint;
76 return true;
80 // check rockbox-info.txt afterwards.
81 QFile file(mountpoints.at(i) + "/.rockbox/rockbox-info.txt");
82 if(file.exists())
84 file.open(QIODevice::ReadOnly | QIODevice::Text);
85 QString line = file.readLine();
86 if(line.startsWith("Target: "))
88 line.remove("Target: ");
89 if(m_device.isEmpty())
90 m_device = line.trimmed(); // trim whitespaces
91 m_mountpoint = mountpoints.at(i);
92 qDebug() << "rockbox-info.txt detected:" << m_device << m_mountpoint;
93 return true;
96 // check for some specific files in root folder
97 QDir root(mountpoints.at(i));
98 QStringList rootentries = root.entryList(QDir::Files);
99 if(rootentries.contains("archos.mod", Qt::CaseInsensitive))
101 // archos.mod in root folder -> Archos Player
102 m_device = "player";
103 m_mountpoint = mountpoints.at(i);
104 return true;
106 if(rootentries.contains("ONDIOST.BIN", Qt::CaseInsensitive))
108 // ONDIOST.BIN in root -> Ondio FM
109 m_device = "ondiofm";
110 m_mountpoint = mountpoints.at(i);
111 return true;
113 if(rootentries.contains("ONDIOSP.BIN", Qt::CaseInsensitive))
115 // ONDIOSP.BIN in root -> Ondio SP
116 m_device = "ondiosp";
117 m_mountpoint = mountpoints.at(i);
118 return true;
120 if(rootentries.contains("ajbrec.ajz", Qt::CaseInsensitive))
122 qDebug() << "ajbrec.ajz found. Trying detectAjbrec()";
123 if(detectAjbrec(mountpoints.at(i))) {
124 m_mountpoint = mountpoints.at(i);
125 qDebug() << m_device;
126 return true;
129 // detection based on player specific folders
130 QStringList rootfolders = root.entryList(QDir::Dirs
131 | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
132 if(rootfolders.contains("GBSYSTEM", Qt::CaseInsensitive))
134 // GBSYSTEM folder -> Gigabeat
135 m_device = "gigabeatf";
136 m_mountpoint = mountpoints.at(i);
137 return true;
139 #if defined(Q_OS_WIN32)
140 // on windows, try to detect the drive letter of an Ipod
141 if(rootfolders.contains("iPod_Control", Qt::CaseInsensitive))
143 // iPod_Control folder -> Ipod found
144 // detecting of the Ipod type is done below using ipodpatcher
145 m_mountpoint = mountpoints.at(i);
147 #endif
152 int n;
153 //try ipodpatcher
154 struct ipod_t ipod;
155 n = ipod_scan(&ipod);
156 if(n == 1) {
157 qDebug() << "Ipod found:" << ipod.modelstr << "at" << ipod.diskname;
158 m_device = ipod.targetname;
159 #if !defined(Q_OS_WIN32)
160 m_mountpoint = resolveMountPoint(ipod.diskname);
161 #endif
162 return true;
165 //try sansapatcher
166 struct sansa_t sansa;
167 n = sansa_scan(&sansa);
168 if(n == 1) {
169 qDebug() << "Sansa found:" << sansa.targetname << "at" << sansa.diskname;
170 m_device = QString("sansa%1").arg(sansa.targetname);
171 m_mountpoint = resolveMountPoint(sansa.diskname);
172 return true;
175 if(m_mountpoint.isEmpty() && m_device.isEmpty() && m_errdev.isEmpty())
176 return false;
177 return true;
181 QStringList Autodetection::getMountpoints()
183 QStringList tempList;
184 #if defined(Q_OS_WIN32)
185 QFileInfoList list = QDir::drives();
186 for(int i=0; i<list.size();i++)
188 tempList << list.at(i).absolutePath();
191 #elif defined(Q_OS_MACX)
192 int num;
193 struct statfs *mntinf;
195 num = getmntinfo(&mntinf, MNT_WAIT);
196 while(num--) {
197 tempList << QString(mntinf->f_mntonname);
198 mntinf++;
200 #elif defined(Q_OS_LINUX)
202 FILE *mn = setmntent("/etc/mtab", "r");
203 if(!mn)
204 return QStringList("");
206 struct mntent *ent;
207 while((ent = getmntent(mn)))
208 tempList << QString(ent->mnt_dir);
209 endmntent(mn);
211 #else
212 #error Unknown Plattform
213 #endif
214 return tempList;
217 QString Autodetection::resolveMountPoint(QString device)
219 qDebug() << "Autodetection::resolveMountPoint(QString)" << device;
221 #if defined(Q_OS_LINUX)
222 FILE *mn = setmntent("/etc/mtab", "r");
223 if(!mn)
224 return QString("");
226 struct mntent *ent;
227 while((ent = getmntent(mn))) {
228 if(QString(ent->mnt_fsname).startsWith(device)
229 && QString(ent->mnt_type).contains("vfat", Qt::CaseInsensitive)) {
230 endmntent(mn);
231 return QString(ent->mnt_dir);
234 endmntent(mn);
236 #endif
238 #if defined(Q_OS_MACX)
239 int num;
240 struct statfs *mntinf;
242 num = getmntinfo(&mntinf, MNT_WAIT);
243 while(num--) {
244 if(QString(mntinf->f_mntfromname).startsWith(device)
245 && QString(mntinf->f_fstypename).contains("vfat", Qt::CaseInsensitive))
246 return QString(mntinf->f_mntonname);
247 mntinf++;
249 #endif
250 return QString("");
255 /** @brief detect devices based on usb pid / vid.
256 * @return true upon success, false otherwise.
258 bool Autodetection::detectUsb()
260 // usbids holds the mapping in the form
261 // ((VID<<16)|(PID)), targetname
262 // the ini file needs to hold the IDs as hex values.
263 QMap<int, QString> usbids = settings->usbIdMap();
264 QMap<int, QString> usberror = settings->usbIdErrorMap();
266 // usb pid detection
267 #if defined(Q_OS_LINUX) | defined(Q_OS_MACX)
268 usb_init();
269 usb_find_busses();
270 usb_find_devices();
271 struct usb_bus *b;
272 b = usb_get_busses();
274 while(b) {
275 qDebug() << "bus:" << b->dirname << b->devices;
276 if(b->devices) {
277 qDebug() << "devices present.";
278 struct usb_device *u;
279 u = b->devices;
280 while(u) {
281 uint32_t id;
282 id = u->descriptor.idVendor << 16 | u->descriptor.idProduct;
283 m_usbconid.append(id);
284 qDebug("%x", id);
286 if(usbids.contains(id)) {
287 m_device = usbids.value(id);
288 return true;
290 if(usberror.contains(id)) {
291 m_errdev = usberror.value(id);
292 // we detected something, so return true
293 qDebug() << "detected device with problems via usb!";
294 return true;
296 u = u->next;
299 b = b->next;
301 #endif
303 #if defined(Q_OS_WIN32)
304 HDEVINFO deviceInfo;
305 SP_DEVINFO_DATA infoData;
306 DWORD i;
308 // Iterate over all devices
309 // by doing it this way it's unneccessary to use GUIDs which might be not
310 // present in current MinGW. It also seemed to be more reliably than using
311 // a GUID.
312 // See KB259695 for an example.
313 deviceInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
315 infoData.cbSize = sizeof(SP_DEVINFO_DATA);
317 for(i = 0; SetupDiEnumDeviceInfo(deviceInfo, i, &infoData); i++) {
318 DWORD data;
319 LPTSTR buffer = NULL;
320 DWORD buffersize = 0;
322 // get device desriptor first
323 // for some reason not doing so results in bad things (tm)
324 while(!SetupDiGetDeviceRegistryProperty(deviceInfo, &infoData,
325 SPDRP_DEVICEDESC,&data, (PBYTE)buffer, buffersize, &buffersize)) {
326 if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
327 if(buffer) free(buffer);
328 // double buffer size to avoid problems as per KB888609
329 buffer = (LPTSTR)malloc(buffersize * 2);
331 else {
332 break;
336 // now get the hardware id, which contains PID and VID.
337 while(!SetupDiGetDeviceRegistryProperty(deviceInfo, &infoData,
338 SPDRP_HARDWAREID,&data, (PBYTE)buffer, buffersize, &buffersize)) {
339 if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
340 if(buffer) free(buffer);
341 // double buffer size to avoid problems as per KB888609
342 buffer = (LPTSTR)malloc(buffersize * 2);
344 else {
345 break;
349 unsigned int vid, pid, rev;
350 if(_stscanf(buffer, _TEXT("USB\\Vid_%x&Pid_%x&Rev_%x"), &vid, &pid, &rev) != 3) {
351 qDebug() << "Error getting USB ID -- possibly no USB device";
353 else {
354 uint32_t id;
355 id = vid << 16 | pid;
356 m_usbconid.append(id);
357 qDebug("VID: %04x PID: %04x", vid, pid);
358 if(usbids.contains(id)) {
359 m_device = usbids.value(id);
360 if(buffer) free(buffer);
361 SetupDiDestroyDeviceInfoList(deviceInfo);
362 qDebug() << "detectUsb: Got" << m_device;
363 return true;
365 if(usberror.contains(id)) {
366 m_errdev = usberror.value(id);
367 // we detected something, so return true
368 if(buffer) free(buffer);
369 SetupDiDestroyDeviceInfoList(deviceInfo);
370 qDebug() << "detectUsb: Got" << m_device;
371 qDebug() << "detected device with problems via usb!";
372 return true;
375 if(buffer) free(buffer);
377 SetupDiDestroyDeviceInfoList(deviceInfo);
379 #endif
380 return false;
384 bool Autodetection::detectAjbrec(QString root)
386 QFile f(root + "/ajbrec.ajz");
387 char header[24];
388 f.open(QIODevice::ReadOnly);
389 if(!f.read(header, 24)) return false;
391 // check the header of the file.
392 // recorder v1 had a 6 bytes sized header
393 // recorder v2, FM, Ondio SP and FM have a 24 bytes header.
395 // recorder v1 has the binary length in the first 4 bytes, so check
396 // for them first.
397 int len = (header[0]<<24) | (header[1]<<16) | (header[2]<<8) | header[3];
398 qDebug() << "possible bin length:" << len;
399 qDebug() << "file len:" << f.size();
400 if((f.size() - 6) == len)
401 m_device = "recorder";
403 // size didn't match, now we need to assume we have a headerlength of 24.
404 switch(header[11]) {
405 case 2:
406 m_device = "recorderv2";
407 break;
409 case 4:
410 m_device = "fmrecorder";
411 break;
413 case 8:
414 m_device = "ondiofm";
415 break;
417 case 16:
418 m_device = "ondiosp";
419 break;
421 default:
422 break;
424 f.close();
426 if(m_device.isEmpty()) return false;
427 return true;