-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDeviceScanner.cpp
629 lines (542 loc) · 23.9 KB
/
DeviceScanner.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*!
* This file is part of OffloadBuddy.
* COPYRIGHT (C) 2020 Emeric Grange - All Rights Reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* \date 2018
* \author Emeric Grange <[email protected]>
*/
#include "DeviceScanner.h"
#include "DeviceManager.h"
#include "GenericFileModel.h"
#include "GoProFileModel.h"
#include "Insta360FileModel.h"
#ifdef ENABLE_LIBMTP
#include <libmtp.h>
#endif
#include <QDir>
#include <QFile>
#include <QStorageInfo>
#include <QDebug>
/* ************************************************************************** */
DeviceScanner::DeviceScanner()
{
connect(&m_watcherFilesystem, &QFileSystemWatcher::directoryChanged, this, &DeviceScanner::removeFilesystem);
}
DeviceScanner::~DeviceScanner()
{
//
}
/* ************************************************************************** */
/* ************************************************************************** */
void DeviceScanner::searchDevices()
{
emit scanningStarted();
scanFilesystems();
scanVirtualFilesystems();
scanMtpDevices();
emit scanningFinished();
}
/* ************************************************************************** */
void DeviceScanner::scanFilesystems()
{
QStringList connectedFilesystems;
// Check if we have new device(s)
for (auto const &storage : QStorageInfo::mountedVolumes())
{
//qDebug() << "> MOUNTPOINT(" << storage.fileSystemType() << ") > " << storage.rootPath();
if (storage.fileSystemType() == "nfs" ||
storage.fileSystemType() == "nfs4")
{
//qDebug() << "> skipping network filesystem";
continue;
}
if (storage.fileSystemType() == "tmpfs")
{
//qDebug() << "> skipping virtual filesystem";
continue;
}
if (storage.rootPath() == "/" || storage.rootPath() == "/home" ||
storage.rootPath().startsWith("/boot") ||
storage.rootPath().startsWith("/run/user/") ||
storage.rootPath().startsWith("/Users/"))
{
//qDebug() << "> skipping OS internal filesystem";
continue;
}
connectedFilesystems.push_back(storage.rootPath());
// Path already in "watched" list? bail early!
if (m_watchedFilesystems.contains(storage.rootPath()))
{
//qDebug() << "> skipping '" << storage.rootPath() << "', already handled";
continue;
}
if (storage.isValid() && storage.isReady())
{
QString deviceRootpath = storage.rootPath();
bool found = false;
if (!found)
{
auto *goproDeviceInfos = new gopro_device_infos;
if (goproDeviceInfos)
{
if (parseGoProVersionFile(deviceRootpath, *goproDeviceInfos))
{
found = true;
// Send device infos to the DeviceManager
emit fsDeviceFound(deviceRootpath, goproDeviceInfos);
// Watch this path
m_watchedFilesystems.push_back(deviceRootpath);
if (!m_watcherFilesystem.addPath(deviceRootpath))
qDebug() << "FILE WATCHER FAILED for " << deviceRootpath;
}
else
{
delete goproDeviceInfos;
}
}
}
if (!found)
{
auto *insta360DeviceInfos = new insta360_device_infos;
if (insta360DeviceInfos)
{
if (parseInsta360VersionFile(deviceRootpath, *insta360DeviceInfos))
{
found = true;
// Send device infos to the DeviceManager
emit fsDeviceFound(deviceRootpath, insta360DeviceInfos);
// Watch this path
m_watchedFilesystems.push_back(deviceRootpath);
if (!m_watcherFilesystem.addPath(deviceRootpath))
qDebug() << "FILE WATCHER FAILED for " << deviceRootpath;
}
else
{
delete insta360DeviceInfos;
}
}
}
if (!found)
{
auto *genericDeviceInfos = new generic_device_infos;
if (genericDeviceInfos)
{
if (parseGenericDCIM(deviceRootpath, *genericDeviceInfos))
{
found = true;
// Send device infos to the DeviceManager
emit fsDeviceFound(deviceRootpath, genericDeviceInfos);
// Watch this path
m_watchedFilesystems.push_back(deviceRootpath);
if (!m_watcherFilesystem.addPath(deviceRootpath))
qDebug() << "FILE WATCHER FAILED for " << deviceRootpath;
}
else
{
delete genericDeviceInfos;
}
}
}
}
else
{
qDebug() << "* mountpoint invalid? '" << storage.displayName() << "'";
}
}
// Check if we lost some device(s) since last scan
for (auto const &storage: std::as_const(m_watchedFilesystems))
{
if (!connectedFilesystems.contains(storage))
{
//qDebug() << storage << "has gone missing, removing device...";
removeFilesystem(storage);
}
}
}
/* ************************************************************************** */
void DeviceScanner::scanVirtualFilesystems()
{
#if defined(Q_OS_LINUX)
QStringList connectedVirtualFilesystems;
// Check if we have new device(s)
for (const auto &storage : QStorageInfo::mountedVolumes())
{
if (storage.fileSystemType() == "tmpfs" ||
storage.fileSystemType() == "fuse.gvfsd-fuse")
{
//qDebug() << "> MOUNTPOINT(" << storage.fileSystemType() << ") > " << storage.rootPath();
if (storage.rootPath() == "/run" ||
storage.rootPath() == "/tmp")
{
//qDebug() << "> skipping OS internal filesystem";
continue;
}
// Chances are, we now have a virtual MTP filesystem, so let's look for it
// ex: /run/user/1000/gvfs/gphoto2:host=%5Busb%3A005%2C012%5D/
// ex: /run/user/1000/gvfs/mtp:host=%5Busb%3A005%2C012%5D/
// ex: /run/user/1000/gvfs/smb-share:server=myserver.local,share=myuser
// 0x2C: ',' 0x3A: ':' 0x5B: '[' 0x5D: ']'
QDir gvfsDirectory(storage.rootPath() + "/gvfs");
const QStringList &gvfsDirectory_list = gvfsDirectory.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
for (const auto &subdir_device : gvfsDirectory_list)
{
QString virtual_mountpoint = storage.rootPath() + "/gvfs/" + subdir_device;
//qDebug() << "> VIRTUAL MOUNTPOINT(" << storage.fileSystemType() << ") > " << virtual_mountpoint;
if (virtual_mountpoint.contains("smb-share"))
{
//qDebug() << "> skipping network filesystem";
continue;
}
connectedVirtualFilesystems.push_back(storage.rootPath() + "/gvfs/" + subdir_device);
ofb_vfs_device *deviceInfos = new ofb_vfs_device;
if (deviceInfos)
{
// first, try to get device bus and number
int bus = -1, dev = -1;
if (subdir_device.startsWith("mtp"))
{
bool ok = false;
bus = subdir_device.mid(18,3).toInt(&ok);
if (!ok) bus = -1;
dev = subdir_device.mid(24,3).toInt(&ok);
if (!ok) dev = -1;
}
else if (subdir_device.startsWith("gphoto2"))
{
bool ok = false;
bus = subdir_device.mid(22,3).toInt(&ok);
if (!ok) bus = -1;
dev = subdir_device.mid(28,3).toInt(&ok);
if (!ok) dev = -1;
}
if (bus >= 0 && dev >= 0)
{
deviceInfos->devBus = static_cast<uint32_t>(bus);
deviceInfos->devNum = static_cast<uint32_t>(dev);
DeviceManager::getMtpDeviceName(deviceInfos->devBus, deviceInfos->devNum,
deviceInfos->brand, deviceInfos->model);
//qDebug() << "MTP device infos:" << deviceInfos->devBus << "/" << deviceInfos->devNum;
//qDebug() << "MTP device infos:" << deviceInfos->brand << "/" << deviceInfos->model;
}
// as a fallback, try to get a string able to 'identify' the device
if (bus < 0 || dev < 0)
{
if (subdir_device.startsWith("mtp:host="))
deviceInfos->stringId = subdir_device.mid(9);
//qDebug() << "MTP device infos:" << deviceInfos->stringId;
DeviceManager::getMtpDeviceName(deviceInfos->stringId,
deviceInfos->brand, deviceInfos->model);
// Backup: manual associations
if (deviceInfos->stringId.contains("GoPro", Qt::CaseInsensitive))
{
deviceInfos->brand = "GoPro";
if (deviceInfos->stringId.contains("HERO11"))
deviceInfos->model = "HERO11";
else if (deviceInfos->stringId.contains("HERO10"))
deviceInfos->model = "HERO10";
else if (deviceInfos->stringId.contains("HERO9"))
deviceInfos->model = "HERO9";
else if (deviceInfos->stringId.contains("HERO8"))
deviceInfos->model = "HERO8";
else if (deviceInfos->stringId.contains("HERO7"))
deviceInfos->model = "HERO7";
else if (deviceInfos->stringId.contains("HERO_6"))
deviceInfos->model = "HERO6";
else if (deviceInfos->stringId.contains("HERO5"))
deviceInfos->model = "HERO5";
else if (deviceInfos->stringId.contains("HERO4"))
deviceInfos->model = "HERO4";
else if (deviceInfos->stringId.contains("HERO", Qt::CaseInsensitive))
deviceInfos->model = "HERO";
if (deviceInfos->stringId.contains("BLACK", Qt::CaseInsensitive))
deviceInfos->model += " BLACK";
}
else if (deviceInfos->stringId.contains("Sony", Qt::CaseInsensitive))
{
deviceInfos->brand = "Sony";
}
else if (deviceInfos->stringId.contains("Garmin", Qt::CaseInsensitive))
{
deviceInfos->brand = "Garmin";
}
else if (deviceInfos->stringId.contains("Xiaomi", Qt::CaseInsensitive))
{
deviceInfos->brand = "Xiaomi";
}
}
// Device already in watch list? bail early!
if (deviceInfos->devBus > 0 && deviceInfos->devNum > 0)
{
// FIXME m_watchedMtpDevices is never cleaned but bus ids are dynamic anyway
if (m_watchedMtpDevices.contains(std::make_pair(deviceInfos->devBus, deviceInfos->devNum)))
{
//qDebug() << "> skipping device @ bus" << deviceInfos->devBus << ", dev" << deviceInfos->devNum << ", already handled";
delete deviceInfos;
continue;
}
}
else if (!deviceInfos->stringId.isEmpty())
{
bool devicefound = false;
for (const auto &s: std::as_const(m_watchedVirtualFilesystems))
{
if (s.contains(virtual_mountpoint))
{
devicefound = true;
break;
}
}
if (devicefound)
{
//qDebug() << "> skipping device @ " << deviceInfos->stringId << ", already handled";
delete deviceInfos;
continue;
}
}
else
{
//qDebug() << "> skipping device: probably not handled by libMTP...";
delete deviceInfos;
continue;
}
// Then we usually have a subdirectory per MTP 'volume'
// ex: one volume for the internal flash of a phone and one for its SD card
QDir gvfsSubDirectory(virtual_mountpoint);
const QStringList &gvfsSubDirectory_list = gvfsSubDirectory.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
for (const auto &subdir_volume : gvfsSubDirectory_list)
{
QString devicePath = virtual_mountpoint + "/" + subdir_volume;
//qDebug() << "Scanning MTP subdir_volume:" << subdir_volume;
//qDebug() << "deviceRootpath:" << devicePath;
QDir dcim(devicePath + "/DCIM");
if (dcim.exists() && dcim.isReadable())
{
//qDebug() << "WE HAVE a DCIM directory on" << devicePath;
deviceInfos->paths.push_back(devicePath);
}
}
if (deviceInfos->paths.size() > 0)
{
// Send device infos to the DeviceManager
emit vfsDeviceFound(deviceInfos);
// And watch for disconnection
m_watchedVirtualFilesystems.push_back(deviceInfos->paths.front());
if (deviceInfos->devBus > 0 && deviceInfos->devNum > 0)
m_watchedMtpDevices.push_back(std::make_pair(deviceInfos->devBus, deviceInfos->devNum));
}
else
{
delete deviceInfos;
}
}
}
}
}
// Check if we lost some device(s) since last scan
for (const auto &watchedFs: std::as_const(m_watchedVirtualFilesystems))
{
bool connected = false;
for (const auto &connectedFs: connectedVirtualFilesystems)
{
if (connectedFs.startsWith(watchedFs))
{
connected = true;
}
}
if (connected == false)
{
//qDebug() << watchedFs << "has gone missing, removing device...";
removeFilesystem(watchedFs);
}
}
#endif // defined(Q_OS_LINUX)
}
/* ************************************************************************** */
void DeviceScanner::scanMtpDevices()
{
#ifdef ENABLE_LIBMTP
QList <std::pair<unsigned, unsigned>> connectedMtpDevices;
int numrawdevices = 0;
LIBMTP_raw_device_t *rawdevices = nullptr;
// use this to get *already* connected devices? ??
//LIBMTP_Get_Connected_Devices(LIBMTP_mtpdevice_t **device_list)
// check for devices that have disapeared
//LIBMTP_Check_Specific_Device()
//LIBMTP_Get_Connected_Devices(LIBMTP_mtpdevice_t **device_list)
LIBMTP_error_number_t err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices);
switch (err)
{
case LIBMTP_ERROR_NONE:
//qDebug() << "MTP: Found" << numrawdevices << "device(s)";
break;
case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
break;
case LIBMTP_ERROR_CONNECTING:
qWarning() << "MTP: There has been a connection error!";
break;
case LIBMTP_ERROR_MEMORY_ALLOCATION:
qWarning() << "MTP: Encountered a Memory Allocation Error!";
break;
case LIBMTP_ERROR_GENERAL:
default:
qWarning() << "MTP: Unknown connection error!";
break;
}
// Check if we have new device(s)
for (int i = 0; i < numrawdevices; i++)
{
/*
qDebug() << "> MTP DEVICE(" << rawdevices[i].device_entry.vendor << rawdevices[i].device_entry.product \
<< ") [" << rawdevices[i].device_entry.vendor_id << ":" << rawdevices[i].device_entry.product_id \
<< "] @ bus" << rawdevices[i].bus_location << ", dev" << rawdevices[i].devnum;
*/
// Device in watch list? bail early!
std::pair <unsigned, unsigned> currentMtpDevice = std::make_pair(rawdevices[i].bus_location, rawdevices[i].devnum);
connectedMtpDevices.push_back(currentMtpDevice);
if (m_watchedMtpDevices.contains(currentMtpDevice))
{
//qDebug() << "> skipping device @ bus" << currentMtpDevice.first << ", dev" << currentMtpDevice.second << ", already handled";
continue;
}
LIBMTP_mtpdevice_t *mtpDevice = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]);
if (mtpDevice == nullptr)
{
qWarning() << "MTP: Unable to open raw device #" << i;
continue;
}
/*
LIBMTP_Dump_Errorstack(device);
LIBMTP_Clear_Errorstack(device);
LIBMTP_Dump_Device_Info(device);
*/
ofb_mtp_device *deviceInfos = new ofb_mtp_device;
if (deviceInfos)
{
// Device infos
deviceInfos->brand = LIBMTP_Get_Manufacturername(mtpDevice);
deviceInfos->model = LIBMTP_Get_Modelname(mtpDevice);
deviceInfos->serial = LIBMTP_Get_Serialnumber(mtpDevice);
deviceInfos->firmware = LIBMTP_Get_Deviceversion(mtpDevice);
deviceInfos->devBus = rawdevices[i].bus_location;
deviceInfos->devNum = rawdevices[i].devnum;
deviceInfos->device = mtpDevice;
// Battery infos
uint8_t maxbattlevel, currbattlevel;
int ret = LIBMTP_Get_Batterylevel(mtpDevice, &maxbattlevel, &currbattlevel);
if (ret == 0 && maxbattlevel > 0)
{
deviceInfos->battery = (static_cast<float>(currbattlevel) / static_cast<float>(maxbattlevel)) * 100.f;
//qDebug() << "MTP Battery level:" << deviceInfos->battery << "%";
}
else
{
// Silently ignore. Some devices does not support getting the battery level.
LIBMTP_Clear_Errorstack(mtpDevice);
}
// Storage infos
for (LIBMTP_devicestorage_t *storage = mtpDevice->storage;
storage != nullptr;
storage = storage->next)
{
//storage->AccessCapability // 0x0000 read/write
//storage->FreeSpaceInBytes
//storage->MaxCapacity
// Get file listing for the root directory only, search for a DCIM directory
LIBMTP_file_t *files = LIBMTP_Get_Files_And_Folders(mtpDevice, storage->id, LIBMTP_FILES_AND_FOLDERS_ROOT);
if (files != nullptr)
{
LIBMTP_file_t *file = files;
LIBMTP_file_t *tmp;
while (file != nullptr)
{
//qDebug() << "-" << file->filename;
if (strcmp(file->filename, "DCIM") == 0 ||
strcmp(file->filename, "Pictures") == 0 ||
strcmp(file->filename, "Movies") == 0)
{
StorageMtp *s = new StorageMtp;
s->m_device = mtpDevice;
s->m_storage = storage;
s->m_dcim_id = file->item_id;
s->m_writable = (storage->AccessCapability == 0);
/*
qDebug() << "MTP storage:";
qDebug() << "-" << s->m_device;
qDebug() << "-" << s->m_storage;
qDebug() << "-" << s->m_storage->id;
*/
deviceInfos->storages.push_back(s);
LIBMTP_destroy_file_t(file);
break;
}
tmp = file;
file = file->next;
LIBMTP_destroy_file_t(tmp);
}
}
}
if (!deviceInfos->storages.empty())
{
// Send device infos to the DeviceManager
emit mtpDeviceFound(deviceInfos);
m_watchedMtpDevices.push_back(currentMtpDevice);
}
else
{
LIBMTP_Release_Device(mtpDevice);
delete deviceInfos;
}
}
}
free(rawdevices);
// Check if we lost some device(s) since last scan
for (auto watchedDevice: std::as_const(m_watchedMtpDevices))
{
if (!connectedMtpDevices.contains(watchedDevice))
{
//qDebug() << "Device @ bus" << watchedDevice.first << ", dev" << watchedDevice.second << "has gone missing, removing device...";
removeMtpDevice(watchedDevice);
}
}
#endif // ENABLE_LIBMTP
}
/* ************************************************************************** */
/* ************************************************************************** */
void DeviceScanner::removeFilesystem(const QString &path)
{
if (path.isEmpty())
return;
//qDebug() << "DeviceScanner::removeFilesystem()" << path;
QDir dir(path);
if (dir.exists())
{
// FIXME virtual filesystem sometimes still exists after physical removal
qDebug() << "DeviceScanner::removeFilesystem()" << path << "but associated directory STILL exists";
}
else
{
m_watcherFilesystem.removePath(path);
m_watchedFilesystems.removeOne(path);
m_watchedVirtualFilesystems.removeOne(path);
Q_EMIT fsDeviceRemoved(path);
}
}
void DeviceScanner::removeMtpDevice(std::pair<unsigned, unsigned> device)
{
m_watchedMtpDevices.removeOne(device);
Q_EMIT mtpDeviceRemoved(device.first, device.second);
}
/* ************************************************************************** */