-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusb_storage_example.cpp
More file actions
408 lines (347 loc) · 14 KB
/
usb_storage_example.cpp
File metadata and controls
408 lines (347 loc) · 14 KB
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
/*
* usb_storage_example.cpp
*
* Copyright (c) 2024 Apra Labs
*
* This file is part of ApraUtils.
*
* Licensed under the MIT License.
* See LICENSE file in the project root for full license information.
*/
/*
* USB Storage Detection and Monitoring Example
*
* This example demonstrates how to use the ApraUtils StorageUSB interface to:
* 1. Detect USB storage devices (flash drives, external HDDs, SD cards)
* 2. Monitor for USB insertion and removal events
* 3. Get storage device information (size, capacity, filesystem)
* 4. Handle mounting and unmounting operations
* 5. Check storage status and handle safe/unsafe eject scenarios
*
* Hardware Requirements:
* ---------------------
* - Linux-based embedded system with USB host support
* - USB flash drive or external storage device
* - Sufficient permissions to mount/unmount devices
*
* System Requirements:
* --------------------
* - udev library (libudev) for device enumeration
* - Mount point directory (e.g., /mnt/usb)
* - User must be in appropriate groups (plugdev, disk) or run as root
*
* Supported Filesystems:
* ----------------------
* - FAT32 (vfat)
* - NTFS
* - EXT4
*
* Compilation:
* ------------
* g++ -std=c++14 usb_storage_example.cpp -o usb_storage_example -lApraLinuxUtils -ludev
*
* Execution:
* ----------
* sudo ./usb_storage_example
* (sudo required for mount/unmount operations)
*
* Notes:
* ------
* - Create mount point before running: sudo mkdir -p /mnt/usb
* - Ensure mount point has appropriate permissions
* - For auto-mounting, run without root using skipMount=true option
*/
#include <ApraUtils.h>
#include <iostream>
#include <unistd.h>
#include <signal.h>
#include <iomanip>
using namespace apra;
using namespace std;
// Configuration
#define USB_MOUNT_PATH "/mnt/usb"
#define POLL_INTERVAL_SECONDS 2
// Control flag
volatile bool g_keepRunning = true;
// Signal handler for graceful shutdown
void signalHandler(int signum) {
cout << "\nInterrupt signal (" << signum << ") received. Shutting down..." << endl;
g_keepRunning = false;
}
// Function to convert storage state enum to readable string
string storageStateToString(STORAGE_STATE state) {
switch (state) {
case STORAGE_INSERTED:
return "INSERTED (not mounted)";
case STORAGE_INSERTED_UNSUPPORTED:
return "INSERTED_UNSUPPORTED (unsupported filesystem)";
case STORAGE_MOUNTED:
return "MOUNTED (ready for use)";
case STORAGE_UNSAFE_EJECT:
return "UNSAFE_EJECT (still in use)";
case STORAGE_SAFE_EJECT:
return "SAFE_EJECT (can be removed)";
default:
return "UNKNOWN";
}
}
// Function to format bytes into human-readable size
string formatBytes(uint64_t bytes) {
const char* units[] = {"B", "KB", "MB", "GB", "TB"};
int unitIndex = 0;
double size = static_cast<double>(bytes);
while (size >= 1024.0 && unitIndex < 4) {
size /= 1024.0;
unitIndex++;
}
// Format with 2 decimal places
char buffer[64];
snprintf(buffer, sizeof(buffer), "%.2f %s", size, units[unitIndex]);
return string(buffer);
}
// Function to display storage information
void displayStorageInfo(StorageUSB& storage) {
cout << "\n--- Storage Device Information ---" << endl;
// Get status
STORAGE_STATE state = storage.getStatus();
cout << "Status: " << storageStateToString(state) << endl;
// Get mount path
string mountPath = storage.getMountPath();
cout << "Mount path: " << mountPath << endl;
// If mounted, get storage info
if (state == STORAGE_MOUNTED) {
uint64_t freeSpaceMB = 0;
uint64_t totalCapacityMB = 0;
if (storage.getStorageInfo(freeSpaceMB, totalCapacityMB)) {
uint64_t usedSpaceMB = totalCapacityMB - freeSpaceMB;
double usedPercentage = (totalCapacityMB > 0) ?
(static_cast<double>(usedSpaceMB) / totalCapacityMB * 100.0) : 0.0;
cout << "\nCapacity Information:" << endl;
cout << " Total capacity: " << formatBytes(totalCapacityMB * 1024 * 1024)
<< " (" << totalCapacityMB << " MB)" << endl;
cout << " Used space: " << formatBytes(usedSpaceMB * 1024 * 1024)
<< " (" << usedSpaceMB << " MB)" << endl;
cout << " Free space: " << formatBytes(freeSpaceMB * 1024 * 1024)
<< " (" << freeSpaceMB << " MB)" << endl;
cout << " Usage: " << fixed << setprecision(1)
<< usedPercentage << "%" << endl;
} else {
cout << "Unable to retrieve storage capacity information." << endl;
}
}
cout << "-----------------------------------" << endl;
}
// Function to demonstrate basic USB detection and mounting
void basicDetectionExample() {
cout << "\n==================================================" << endl;
cout << "Basic USB Storage Detection Example" << endl;
cout << "==================================================" << endl;
// Create StorageUSB instance
// Parameters: mount_path, supported_types, should_print, skip_mount
vector<STORAGE_TYPE> supportedTypes = {FAT32, NTFS, EXT4};
StorageUSB storage(USB_MOUNT_PATH, supportedTypes, true, false);
cout << "Checking for USB storage devices..." << endl;
// Check if USB device is inserted
string devicePath = storage.insertCheck();
if (devicePath.empty()) {
cout << "No USB storage device detected." << endl;
cout << "Please insert a USB flash drive." << endl;
return;
}
cout << "USB storage device detected: " << devicePath << endl;
// Display initial status
displayStorageInfo(storage);
// If not mounted, try to mount
if (storage.getStatus() == STORAGE_INSERTED) {
cout << "\nAttempting to mount device..." << endl;
string mountedPath = storage.mountDevice();
if (!mountedPath.empty()) {
cout << "Successfully mounted at: " << mountedPath << endl;
displayStorageInfo(storage);
} else {
cerr << "Failed to mount device." << endl;
cerr << "Possible reasons:" << endl;
cerr << " - Insufficient permissions (try running as root)" << endl;
cerr << " - Mount point doesn't exist or not accessible" << endl;
cerr << " - Unsupported filesystem" << endl;
cerr << " - Device is already mounted elsewhere" << endl;
}
}
// Wait before cleanup
cout << "\nPress Enter to unmount and cleanup...";
cin.get();
// Eject device
if (storage.getStatus() == STORAGE_MOUNTED) {
cout << "Ejecting device..." << endl;
if (storage.ejectDevice()) {
cout << "Device ejected successfully." << endl;
// Check if it was safe eject
if (storage.isUnsafeEject()) {
cout << "Warning: Device was in use during eject (unsafe)." << endl;
} else {
cout << "Device ejected safely. You can now remove it." << endl;
}
} else {
cerr << "Failed to eject device." << endl;
}
}
}
// Function to demonstrate continuous USB monitoring
void continuousMonitoringExample() {
cout << "\n==================================================" << endl;
cout << "Continuous USB Storage Monitoring Example" << endl;
cout << "==================================================" << endl;
cout << "This will monitor for USB insertion and removal." << endl;
cout << "Press Ctrl+C to stop monitoring." << endl;
cout << "==================================================" << endl;
// Setup signal handler
signal(SIGINT, signalHandler);
// Create StorageUSB instance with auto-mount enabled
vector<STORAGE_TYPE> supportedTypes = {FAT32, NTFS, EXT4};
StorageUSB storage(USB_MOUNT_PATH, supportedTypes, true, false);
STORAGE_STATE previousState = STORAGE_SAFE_EJECT;
bool devicePresent = false;
cout << "\nMonitoring started. Waiting for USB device..." << endl;
while (g_keepRunning) {
// Check for device insertion
string devicePath = storage.insertCheck();
STORAGE_STATE currentState = storage.getStatus();
// Detect state changes
if (!devicePresent && !devicePath.empty()) {
// Device inserted
devicePresent = true;
cout << "\n*** USB DEVICE INSERTED ***" << endl;
cout << "Device: " << devicePath << endl;
// Attempt to mount
cout << "Attempting to mount..." << endl;
string mountedPath = storage.mountDevice();
if (!mountedPath.empty()) {
cout << "Successfully mounted!" << endl;
displayStorageInfo(storage);
} else {
cerr << "Failed to mount device." << endl;
}
}
else if (devicePresent && devicePath.empty()) {
// Device removed
devicePresent = false;
cout << "\n*** USB DEVICE REMOVED ***" << endl;
// Check if it was a safe removal
if (previousState == STORAGE_MOUNTED) {
cout << "Warning: Device removed while mounted (unsafe removal)!" << endl;
cout << "Data loss may have occurred." << endl;
} else {
cout << "Device was properly ejected before removal." << endl;
}
}
else if (devicePresent && currentState != previousState) {
// State changed while device present
cout << "\nStorage state changed: "
<< storageStateToString(previousState)
<< " -> "
<< storageStateToString(currentState)
<< endl;
if (currentState == STORAGE_MOUNTED) {
displayStorageInfo(storage);
}
}
previousState = currentState;
// Poll every few seconds
sleep(POLL_INTERVAL_SECONDS);
}
// Cleanup on exit
cout << "\n\nStopping monitoring..." << endl;
if (storage.getStatus() == STORAGE_MOUNTED) {
cout << "Unmounting device..." << endl;
storage.ejectDevice();
}
cout << "Monitoring stopped." << endl;
}
// Function to demonstrate detection without auto-mounting
void detectionOnlyExample() {
cout << "\n==================================================" << endl;
cout << "USB Detection Without Auto-Mount Example" << endl;
cout << "==================================================" << endl;
cout << "This mode only detects USB devices without mounting." << endl;
cout << "Useful when system auto-mounts or manual mount needed." << endl;
cout << "==================================================" << endl;
// Create StorageUSB with skipMount=true
vector<STORAGE_TYPE> supportedTypes = {FAT32, NTFS, EXT4};
StorageUSB storage(USB_MOUNT_PATH, supportedTypes, true, true);
cout << "\nChecking for USB devices..." << endl;
for (int i = 0; i < 10 && g_keepRunning; i++) {
string devicePath = storage.insertCheck();
if (!devicePath.empty()) {
cout << "\nUSB device detected: " << devicePath << endl;
cout << "Status: " << storageStateToString(storage.getStatus()) << endl;
// In skip mount mode, you can still get info if system auto-mounted
uint64_t freeMB = 0, totalMB = 0;
if (storage.getStorageInfo(freeMB, totalMB)) {
cout << "Device is mounted and accessible." << endl;
cout << "Free: " << freeMB << " MB, Total: " << totalMB << " MB" << endl;
} else {
cout << "Device detected but not mounted." << endl;
cout << "Manual mount command:" << endl;
cout << " sudo mount " << devicePath << " " << USB_MOUNT_PATH << endl;
}
} else {
cout << "." << flush;
}
sleep(2);
}
cout << "\nDetection-only example completed." << endl;
}
// Main function
int main() {
cout << "==================================================" << endl;
cout << "ApraLinuxUtils USB Storage Detection Example" << endl;
cout << "==================================================" << endl;
cout << "This example demonstrates:" << endl;
cout << " 1. Basic USB detection and mounting" << endl;
cout << " 2. Continuous monitoring for USB events" << endl;
cout << " 3. Detection without auto-mounting" << endl;
cout << "\nRequirements:" << endl;
cout << " - Mount point created: " << USB_MOUNT_PATH << endl;
cout << " - Sufficient permissions (run as root if needed)" << endl;
cout << " - USB storage device for testing" << endl;
cout << "==================================================" << endl;
// Check command line arguments
if (system("test -d " USB_MOUNT_PATH) != 0) {
cerr << "\nWarning: Mount point " << USB_MOUNT_PATH
<< " does not exist." << endl;
cerr << "Create it with: sudo mkdir -p " << USB_MOUNT_PATH << endl;
cerr << "\nContinuing anyway..." << endl;
}
// Menu for selecting example
cout << "\nSelect example to run:" << endl;
cout << " 1. Basic detection and mounting" << endl;
cout << " 2. Continuous monitoring (recommended)" << endl;
cout << " 3. Detection only (no auto-mount)" << endl;
cout << " q. Quit" << endl;
cout << "\nEnter choice: ";
char choice;
cin >> choice;
cin.ignore(); // Clear newline
switch (choice) {
case '1':
basicDetectionExample();
break;
case '2':
continuousMonitoringExample();
break;
case '3':
detectionOnlyExample();
break;
case 'q':
case 'Q':
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice. Exiting..." << endl;
break;
}
cout << "\n==================================================" << endl;
cout << "USB Storage example completed." << endl;
cout << "==================================================" << endl;
return 0;
}