-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathArduino_POSIXStorage.cpp
738 lines (687 loc) · 27.2 KB
/
Arduino_POSIXStorage.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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/*
*********************************************************************************************************
* Arduino_POSIXStorage Library
*
* Copyright 2023 Arduino SA. http://arduino.cc
*
* Original Author: A. Vidstrom ([email protected])
*
* Complements the POSIX storage functions already included in the Renesas
* core and the Arduino_USBHostMbed5 library.
*
* The library supports and is tested on:
*
* - Portenta C33 with Portenta Breakout Board (SD Card and USB Thumb Drive)
* - Portenta C33 with Portenta Vision Shield (SD Card)
* - Portenta H7 with Portenta Breakout Board (SD Card and USB Thumb Drive)
* - Portenta H7 with Portenta Vision Shield (SD Card)
* - Portenta Machine Control (USB Thumb Drive)
* - Opta (USB Thumb Drive)
*
* After making changes:
*
* - Make sure all test combinations finish without errors (tests directory)
* - Make sure the library itself compiles cleanly with all compiler warnings
* enabled (several lower-level libraries don't at the time of writing)
* - Review the whole library carefully, because there are many combinations
* to handle correctly
*
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* Included header files
*********************************************************************************************************
*/
#include "Arduino_POSIXStorage.h"
#include <Arduino.h>
#if defined(ARDUINO_PORTENTA_C33)
#include <SDCardBlockDevice.h>
#include <UsbHostMsd.h>
#elif defined(ARDUINO_PORTENTA_H7_M7)
#include <Arduino_USBHostMbed5.h>
#include <BlockDevice.h>
#include <DigitalIn.h>
#include <SDMMCBlockDevice.h>
#elif defined(ARDUINO_OPTA)
#include <Arduino_USBHostMbed5.h>
#include <BlockDevice.h>
#else
#error "The POSIXStorage library does not support this board"
#endif
/*
*********************************************************************************************************
* Library-internal using declarations
*********************************************************************************************************
*/
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_OPTA)
using mbed::BlockDevice;
using mbed::FileSystem;
#endif
/*
*********************************************************************************************************
* Library-internal data structures
*********************************************************************************************************
*/
struct DeviceFileSystemCombination {
BlockDevice *device = nullptr; // Set if mounted or hotplug callback registered
FileSystem *fileSystem = nullptr; // Set only if mounted
};
/*
*********************************************************************************************************
* Library-internal enumerations
*********************************************************************************************************
*/
enum ActionTypes : uint8_t
{
ACTION_MOUNT,
ACTION_FORMAT
};
enum BoardTypes : uint8_t
{
BOARD_UNKNOWN,
BOARD_MACHINE_CONTROL,
BOARD_PORTENTA_H7,
BOARD_PORTENTA_C33
};
enum CallbackTypes : uint8_t
{
CALLBACK_HOTPLUG,
CALLBACK_UNPLUG
};
/*
*********************************************************************************************************
* Unnamed namespace for library-internal global variables
*********************************************************************************************************
*/
namespace {
// Always set unused members to nullptr at all times because the rest of the code expects it -->
struct DeviceFileSystemCombination sdcard = {nullptr, nullptr};
struct DeviceFileSystemCombination usb = {nullptr, nullptr};
// <--
bool hotplugCallbackAlreadyRegistered = false;
bool unplugCallbackAlreadyRegistered = false;
// Used to handle special case (powering USB A female socket separately) for Machine Control -->
bool hasMountedBefore = false;
bool runningOnMachineControl = false;
// <--
} // End of unnamed namespace
/*
*********************************************************************************************************
* Unnamed namespace for library-internal functions
*********************************************************************************************************
*/
namespace {
// This detection code only works on the Portenta H7 boards -->
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_OPTA)
// WARNING: This algorithm has been tested for radiated interference immunity with a
// chattering relay device, so please don't modify it without running such a test again!
enum BoardTypes detectPortentaH7TypeOnce()
{
int inAfterUp, inAfterDown;
mbed::DigitalIn vbusPin(PB_14);
// Inject high level into input, wait 1 ms to settle
vbusPin.mode(PullUp);
delay(1);
// Shortly float the input and read the state
vbusPin.mode(PullNone);
inAfterUp = vbusPin.read();
// Inject low level into input, wait 1 ms to settle
vbusPin.mode(PullDown);
delay(1);
// Shortly float the input
vbusPin.mode(PullNone);
// Give the external pull-up resistor on the Portenta Machine control enough time to
// raise the voltage to an acceptable high
delayMicroseconds(50);
// Read the state
inAfterDown = vbusPin.read();
// End floating state and leave the input in pull-up state
vbusPin.mode(PullUp);
// The Machine Control has an external pull-up resistor
// and will never float, the input is always 1.
// A plain H7 board floats, and the input follows the
// previously injected level, first 1 and then 0.
// H7 plus Vision Shield or Breakout Board count as plain
// boards too.
if ((1 == inAfterUp) && (0 == inAfterDown))
{
return BOARD_PORTENTA_H7;
}
if ((1 == inAfterUp) && (1 == inAfterDown))
{
return BOARD_MACHINE_CONTROL;
}
else
{
return BOARD_UNKNOWN; // Illegal state, detection failed
}
} // End of detectPortentaH7TypeOnce()
// WARNING: This algorithm has been tested for radiated interference immunity with a
// chattering relay device, so please don't modify it without running such a test again!
enum BoardTypes detectPortentaH7Type()
{
enum BoardTypes boardType = detectPortentaH7TypeOnce();
// Repeat the test a few times to mitigate incorrect
// results because of electrical interference
for (int i=0; i<5; i++)
{
if (boardType != detectPortentaH7TypeOnce())
{
return BOARD_UNKNOWN;
}
}
return boardType;
} // End of detectPortentaH7Type()
#endif
// <-- This detection code only works on the Portenta H7 boards
// Automatic detection unless AUTOMATIC_OVERRIDE_PORTENTA_H7 or AUTOMATIC_OVERRIDE_PORTENTA_MACHINE_CONTROL
// is defined
enum BoardTypes detectBoardType()
{
#if defined(ARDUINO_PORTENTA_C33)
return BOARD_PORTENTA_C33;
#elif defined(ARDUINO_OPTA)
// This case must be handled separately, because PB14 that enables USB on the PMC is used for RS485 on the Opta,
// so performing automatic detection on the Opta breaks RS485
return BOARD_PORTENTA_H7;
#elif defined(ARDUINO_PORTENTA_H7_M7)
#if (defined(AUTOMATIC_OVERRIDE_PORTENTA_H7) && defined(AUTOMATIC_OVERRIDE_PORTENTA_MACHINE_CONTROL))
#error "You have defined AUTOMATIC_OVERRIDE_PORTENTA_H7 and AUTOMATIC_OVERRIDE_PORTENTA_MACHINE_CONTROL at the same time"
#endif
#if defined(AUTOMATIC_OVERRIDE_PORTENTA_H7)
return BOARD_PORTENTA_H7;
#elif defined(AUTOMATIC_OVERRIDE_PORTENTA_MACHINE_CONTROL)
return BOARD_MACHINE_CONTROL;
#else
return detectPortentaH7Type(); // Automatic detection
#endif
#else
#error "This board is not supported"
#endif
} // End of detectBoardType()
void portentaMachineControlPowerHandling()
{
// Determine if we're running on Machine Control or not on the first call to mount(), mkfs(),
// register_hotplug_callback(), or register_unplug_callback()
if (false == hasMountedBefore)
{
hasMountedBefore = true;
if (BOARD_MACHINE_CONTROL == detectBoardType())
{
runningOnMachineControl = true;
}
#if defined(ARDUINO_PORTENTA_H7_M7)
if (true == runningOnMachineControl)
{
// We need to apply power manually to the female USB A connector on the Machine Control
mbed::DigitalOut enablePower(PB_14, 0);
}
#endif
}
} // End of portentaMachineControlPowerHandling()
void deleteDevice(const enum StorageDevices deviceName, struct DeviceFileSystemCombination * const deviceFileSystemCombination)
{
// The USBHostMSD class for the H7 doesn't correctly support object destruction, so we only delete
// the device object on other platforms or if the device is an SD Card -->
bool deleteDevice = false;
#if (!(defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_OPTA)))
(void) deviceName; // Silence -Wunused-parameter, because this variable is only used on the H7
deleteDevice = true;
#else
if (DEV_SDCARD == deviceName)
{
deleteDevice = true;
}
#endif
if (true == deleteDevice)
{
// Ok to delete with base class pointer because the destructor of the base class is virtual
delete deviceFileSystemCombination->device;
deviceFileSystemCombination->device = nullptr;
}
} // End of deleteDevice()
// WARNING: Don't set errno and return -1 in this function - just return 0 for success or the errno code!
int mountOrFormatFileSystemOnDevice(const enum StorageDevices deviceName,
struct DeviceFileSystemCombination * const deviceFileSystemCombination,
const enum FileSystems fileSystem,
const char * const mountPoint,
const enum ActionTypes mountOrFormat)
{
if ((nullptr == mountPoint) || (nullptr == deviceFileSystemCombination))
{
return EFAULT;
}
if (FS_FAT == fileSystem)
{
deviceFileSystemCombination->fileSystem = new(std::nothrow) FATFileSystem(mountPoint);
}
else if (FS_LITTLEFS == fileSystem)
{
deviceFileSystemCombination->fileSystem = new(std::nothrow) LittleFileSystem(mountPoint);
}
else
{
return ENODEV; // This shouldn't happen unless there is a bug in the code
}
if (nullptr == (deviceFileSystemCombination->fileSystem))
{
return ENODEV;
}
// Check before use in mount(), umount(), or reformat() calls below
if (nullptr == deviceFileSystemCombination->device)
{
// Ok to delete with base class pointer because the destructor of the base class is virtual
delete deviceFileSystemCombination->fileSystem;
deviceFileSystemCombination->fileSystem = nullptr;
return EFAULT;
}
if (ACTION_MOUNT == mountOrFormat)
{
// See note (1) at the bottom of the file
int mountReturn = deviceFileSystemCombination->fileSystem->mount(deviceFileSystemCombination->device);
if (0 != mountReturn)
{
// Ok to delete with base class pointer because the destructor of the base class is virtual
delete deviceFileSystemCombination->fileSystem;
deviceFileSystemCombination->fileSystem = nullptr;
// mbed's mount() returns negative errno codes
return (-mountReturn); // See note (1) at the bottom of the file
}
return 0;
} // End of ACTION_MOUNT
else if (ACTION_FORMAT == mountOrFormat)
{
int reformatReturn = -1; // See note (1) at the bottom of the file
if (FS_FAT == fileSystem)
{
// Ok to downcast with static_cast because we know for sure that fileSystem isn't pointing to a
// base-class object, and dynamic_cast wouldn't work anyway because compilation is done with -fno-rtti
FATFileSystem *fatFileSystem = static_cast<FATFileSystem*>(deviceFileSystemCombination->fileSystem);
// FS_FAT needs an allocation unit size specified and 0 just asks for the default one
reformatReturn = fatFileSystem->reformat(deviceFileSystemCombination->device, 0);
}
else if (FS_LITTLEFS == fileSystem)
{
reformatReturn = deviceFileSystemCombination->fileSystem->reformat(deviceFileSystemCombination->device);
}
else // This shouldn't happen unless there is a bug in the code
{
// Ok to delete with base class pointer because the destructor of the base class is virtual
delete deviceFileSystemCombination->fileSystem;
deviceFileSystemCombination->fileSystem = nullptr;
return ENODEV;
}
if (0 != reformatReturn)
{
// Ok to delete with base class pointer because the destructor of the base class is virtual
delete deviceFileSystemCombination->fileSystem;
deviceFileSystemCombination->fileSystem = nullptr;
// mbed's reformat() returns negative errno codes
return (-reformatReturn); // See note (1) at the bottom of the file
}
if (0 == deviceFileSystemCombination->fileSystem->unmount())
{
// Ok to delete with base class pointer because the destructor of the base class is virtual
delete deviceFileSystemCombination->fileSystem;
deviceFileSystemCombination->fileSystem = nullptr;
deleteDevice(deviceName, deviceFileSystemCombination);
}
else
{
// If unmount() fails, we have left the filesystem mounted, but there's no good way to signal that
// to the user with errno codes, so we fail this step silently in those (presumably) very rare cases
}
return 0;
} // End of ACTION_FORMAT
else
{
// Ok to delete with base class pointer because the destructor of the base class is virtual
delete deviceFileSystemCombination->fileSystem;
deviceFileSystemCombination->fileSystem = nullptr;
return ENOTSUP; // This shouldn't happen unless there's a bug in the code
}
} // End of mountOrFormatFileSystemOnDevice()
// WARNING: Don't set errno and return -1 in this function - just return 0 for success or the errno code!
int mountOrFormatSDCard(const enum FileSystems fileSystem,
const enum ActionTypes mountOrFormat)
{
if (nullptr != sdcard.device) // An SD card is already mounted at that mount point
{
return EBUSY;
}
// The Machine Control doesn't have an SD Card connector
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_OPTA)
if (true == runningOnMachineControl)
{
return ENOTBLK;
}
#endif
// Create the appropriate BlockDevice -->
#if defined(ARDUINO_PORTENTA_C33)
sdcard.device = new(std::nothrow) SDCardBlockDevice(PIN_SDHI_CLK,
PIN_SDHI_CMD,
PIN_SDHI_D0,
PIN_SDHI_D1,
PIN_SDHI_D2,
PIN_SDHI_D3,
PIN_SDHI_CD,
PIN_SDHI_WP);
#elif defined(ARDUINO_PORTENTA_H7_M7) || !defined(ARDUINO_OPTA)
sdcard.device = new(std::nothrow) SDMMCBlockDevice();
#else
sdcard.device = nullptr;
#endif
if (nullptr == sdcard.device)
{
return ENOTBLK;
}
// <--
const int mountOrFormatReturn = mountOrFormatFileSystemOnDevice(DEV_SDCARD, &sdcard, fileSystem, "sdcard", mountOrFormat);
if (0 != mountOrFormatReturn)
{
delete sdcard.device;
sdcard.device = nullptr;
return mountOrFormatReturn;
}
return 0;
} // End of mountOrFormatSDCard()
// WARNING: Don't set errno and return -1 in this function - just return 0 for success or the errno code!
int mountOrFormatUSBDevice(const enum FileSystems fileSystem,
const enum ActionTypes mountOrFormat)
{
// We'll need a USBHostMSD pointer because connect() and connected() we'll use later aren't member
// functions of the base class BlockDevice
USBHostMSD *usbHostDevice = nullptr;
// If the device is registered for the hotplug event or on H7, we mustn't delete it even if
// we fail to mount a filesystem to it
bool hotplugKeep = false;
if (nullptr != usb.device) // Already mounted or registered for the hotplug event
{
if (nullptr != usb.fileSystem) // The device is already mounted
{
return EBUSY;
}
else // The device is registered for the hotplug event, or left intact on H7, so we can reuse it
{
// Ok to downcast with static_cast because we know for sure that usb.device isn't pointing to a
// base-class object, and dynamic_cast wouldn't work anyway because compilation is done with -fno-rtti
usbHostDevice = static_cast<USBHostMSD*>(usb.device);
// Make sure not to remove the device object if mount() fails
hotplugKeep = true;
}
}
else { // The device isn't used at all yet
usbHostDevice = new(std::nothrow) USBHostMSD;
if (nullptr == usbHostDevice)
{
return ENOTBLK;
}
usb.device = usbHostDevice;
}
if (false == (usbHostDevice->connected()))
{
if (false == (usbHostDevice->connect()))
{
// Only delete if the object was created by this function
if (false == hotplugKeep)
{
deleteDevice(DEV_USB, &usb);
}
return ENOTBLK;
}
}
const int mountOrFormatReturn = mountOrFormatFileSystemOnDevice(DEV_USB, &usb, fileSystem, "usb", mountOrFormat);
if (0 != mountOrFormatReturn)
{
// Only delete if the object was created by this function
if (false == hotplugKeep)
{
deleteDevice(DEV_USB, &usb);
}
return mountOrFormatReturn;
}
return 0;
} // End of mountOrFormatUSB()
// WARNING: Don't set errno and return -1 in this function - just return 0 for success or the errno code!
int mountOrFormat(const enum StorageDevices deviceName,
const enum FileSystems fileSystem,
const enum ActionTypes mountOrFormat)
{
portentaMachineControlPowerHandling();
switch (deviceName)
{
case DEV_SDCARD:
return mountOrFormatSDCard(fileSystem, mountOrFormat);
case DEV_USB:
return mountOrFormatUSBDevice(fileSystem, mountOrFormat);
default:
return ENOTBLK;
}
} // End of mountOrFormat()
// WARNING: Don't set errno and return -1 in this function - just return 0 for success or the errno code!
int register_callback(const enum StorageDevices deviceName, void (* const callbackFunction)(), enum CallbackTypes callbackType)
{
if (((CALLBACK_HOTPLUG == callbackType) && (true == hotplugCallbackAlreadyRegistered)) ||
((CALLBACK_UNPLUG == callbackType) && (true == unplugCallbackAlreadyRegistered)))
{
return EBUSY;
}
if (nullptr == callbackFunction)
{
return EFAULT;
}
switch (deviceName)
{
case DEV_SDCARD: // There is no support for callbacks in the any of the SD Card block device classes
return ENOTSUP;
case DEV_USB:
{ // Curly braces necessary to keep new variables inside the case statement
portentaMachineControlPowerHandling();
USBHostMSD *usbHostDevice = nullptr;
if (nullptr == usb.device)
{
// We must create a USBHostMSD object to attach the callback to, but we
// don't create a file system object because we don't fully mount() anything
usbHostDevice = new(std::nothrow) USBHostMSD;
if (nullptr == usbHostDevice)
{
return ENOTBLK;
}
#if ((defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_OPTA)))
// The Arduino_USBHostMbed5 library doesn't initialize the USB stack until
// the first connect() call because of an older bugfix (commit 72d0aa6), so
// we perform one connect() here to initialize the stack
usbHostDevice->connect();
#endif
// This is necessary for future calls to mount(), umount(), and mkfs()
usb.device = usbHostDevice;
}
else
{
// Ok to downcast with static_cast because we know for sure that usb.device isn't pointing to a
// base-class object, and dynamic_cast wouldn't work anyway because compilation is done with -fno-rtti
usbHostDevice = static_cast<USBHostMSD*>(usb.device);
}
bool attachCallbackReturn;
if (CALLBACK_HOTPLUG == callbackType)
{
attachCallbackReturn = usbHostDevice->attach_detected_callback(callbackFunction);
}
else if (CALLBACK_UNPLUG == callbackType)
{
attachCallbackReturn = usbHostDevice->attach_removed_callback(callbackFunction);
}
else
{
// This should only happen if there's a bug in the code
attachCallbackReturn = false;
}
if (false == attachCallbackReturn)
{
deleteDevice(DEV_USB, &usb);
return EINVAL;
}
// Prevent multiple registrations
if (CALLBACK_HOTPLUG == callbackType)
{
hotplugCallbackAlreadyRegistered = true;
}
else if (CALLBACK_UNPLUG == callbackType)
{
unplugCallbackAlreadyRegistered = true;
}
else
{
// This should only happen if there's a bug in the code
}
return 0;
} // Curly braces necessary to keep new variables inside the case statement
default:
return ENOTBLK;
}
} // End of register_unplug_callback()
} // End of unnamed namespace
/*
*********************************************************************************************************
* Non-retargeted storage API functions
*********************************************************************************************************
*/
int mount(const enum StorageDevices deviceName,
const enum FileSystems fileSystem,
const enum MountFlags mountFlags)
{
// Only MNT_DEFAULT is allowed on this platform, but other platforms could also allow MNT_RDONLY
if (MNT_DEFAULT != mountFlags)
{
errno = ENOTSUP;
return -1;
}
const int mountOrFormatReturn = mountOrFormat(deviceName, fileSystem, ACTION_MOUNT);
if (0 != mountOrFormatReturn)
{
errno = mountOrFormatReturn;
return -1;
}
return 0;
} // End of mount()
int mkfs(const enum StorageDevices deviceName, const enum FileSystems fileSystem)
{
const int mountOrFormatReturn = mountOrFormat(deviceName, fileSystem, ACTION_FORMAT);
if (0 != mountOrFormatReturn)
{
errno = mountOrFormatReturn;
return -1;
}
return 0;
} // End of mkfs()
int umount(const enum StorageDevices deviceName)
{
struct DeviceFileSystemCombination *deviceFileSystemCombination = nullptr;
switch (deviceName)
{
case DEV_SDCARD:
deviceFileSystemCombination = &sdcard;
break;
case DEV_USB:
deviceFileSystemCombination = &usb;
break;
default:
errno = EINVAL; // This shouldn't happen unless there's a bug in the code
return -1;
}
// Error if the device isn't mounted
if ((nullptr == deviceFileSystemCombination->device) || (nullptr == deviceFileSystemCombination->fileSystem))
{
errno = EINVAL;
return -1;
}
// See note (1) at the bottom of the file
const int unmountRet = deviceFileSystemCombination->fileSystem->unmount();
if (0 == unmountRet)
{
// Ok to delete with base class pointer because the destructor of the base class is virtual
delete deviceFileSystemCombination->fileSystem;
deviceFileSystemCombination->fileSystem = nullptr;
deleteDevice(deviceName, deviceFileSystemCombination);
return 0;
}
else
{
// mbed's mount() returns negative errno codes
errno = -unmountRet; // See note (1) at the bottom of the file
return -1;
}
} // End of umount()
int register_hotplug_callback(const enum StorageDevices deviceName, void (* const callbackFunction)())
{
const int callbackReturn = register_callback(deviceName, callbackFunction, CALLBACK_HOTPLUG);
if (0 != callbackReturn)
{
errno = callbackReturn;
return -1;
}
return 0;
} // End of register_hotplug_callback()
// Currently not supported on any platform, but might be in the future
int deregister_hotplug_callback(const enum StorageDevices deviceName)
{
(void) deviceName; // Remove when implemented, only here to silence -Wunused-parameter
errno = ENOSYS;
return -1;
} // End of deregister_hotplug_callback()
int register_unplug_callback(const enum StorageDevices deviceName, void (* const callbackFunction)())
{
const int callbackReturn = register_callback(deviceName, callbackFunction, CALLBACK_UNPLUG);
if (0 != callbackReturn)
{
errno = callbackReturn;
return -1;
}
return 0;
} // End of register_unplug_callback()
// Currently not supported on any platform, but might be in the future
int deregister_unplug_callback(const enum StorageDevices deviceName)
{
(void) deviceName; // Remove when implemented, only here to silence -Wunused-parameter
errno = ENOSYS;
return -1;
} // End of deregister_unplug_callback()
/*
*********************************************************************************************************
* Notes
*********************************************************************************************************
*/
//
// Note (1):
//
// The mbed POSIX implementation has a potential problem, because it uses the fat_error_remap()
// function to remap error codes from ChaN's FatFs to negative errno numbers. It does this with
// a long switch statement that ends with the default "return -res;" where res is the return code
// from FatFs. The integer ranges used by these two types of error codes overlap, and should
// FatFs add a new error code, it will be turned into an erroneous errno. At the time of writing,
// fat_error_remap() remaps every possible FatFs code. But if FatFs adds another code, it will
// become number 20, which will be automatically remapped to -20, which incorrectly corresponds to
// -ENOTDIR. And so on. This potentially affects several of the POSIX functions in mbed. By the time
// we get the code, it will be impossible to tell correct and incorrect apart. The return codes
// from LittleFS are, in general, negative errno numbers. New ones are just passed along as-is,
// so the same potential problem doesn't exist for LittleFS.
//