Skip to content

Commit c15cc70

Browse files
committed
Use table for MSD magic files.
1 parent 6fbcdb5 commit c15cc70

File tree

1 file changed

+105
-39
lines changed

1 file changed

+105
-39
lines changed

source/daplink/drag-n-drop/vfs_user.c

Lines changed: 105 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,35 @@
4040
// device. This is to accomodate for hex file programming.
4141
static const uint32_t disc_size = MB(64);
4242

43+
//! @brief Constants for magic action or config files.
44+
//!
45+
//! The "magic files" are files with a special name that if created on the USB MSC volume, will
46+
//! cause an event. There are two classes of magic files: action files and config files. The former
47+
//! causes a given action to take place, while the latter changes a persistent configuration setting
48+
//! to a predetermined value.
49+
//!
50+
//! See #s_magic_file_info for the mapping of filenames to these enums.
51+
typedef enum _magic_file {
52+
kDAPLinkModeActionFile, //!< Switch between interface and bootloader.
53+
kTestAssertActionFile, //!< Force an assertion for testing.
54+
kRefreshActionFile, //!< Force a remount.
55+
kEraseActionFile, //!< Erase the target flash.
56+
kAutoResetConfigFile, //!< Enable reset after flash.
57+
kHardResetConfigFile, //!< Disable reset after flash.
58+
kAutomationOnConfigFile, //!< Enable automation.
59+
kAutomationOffConfigFile, //!< Disable automation.
60+
kOverflowOnConfigFile, //!< Enable UART overflow reporting.
61+
kOverflowOffConfigFile, //!< Disable UART overflow reporting.
62+
kMSDOnConfigFile, //!< Enable USB MSC. Uh....
63+
kMSDOffConfigFile, //!< Disable USB MSC.
64+
} magic_file_t;
65+
66+
//! @brief Mapping from filename string to magic file enum.
67+
typedef struct _magic_file_info {
68+
const char *name; //!< Name of the magic file, must be in 8.3 format.
69+
magic_file_t which; //!< Enum for the file.
70+
} magic_file_info_t;
71+
4372
static const char mbed_redirect_file[] =
4473
"<!doctype html>\r\n"
4574
"<!-- mbed Platform Website and Authentication Shortcut -->\r\n"
@@ -60,6 +89,22 @@ static const char error_type_prefix[] = "type: ";
6089

6190
static const vfs_filename_t assert_file = "ASSERT TXT";
6291

92+
//! @brief Table of magic files and their names.
93+
static const magic_file_info_t s_magic_file_info[] = {
94+
{ daplink_mode_file_name, kDAPLinkModeActionFile },
95+
{ "ASSERT ACT", kTestAssertActionFile },
96+
{ "REFRESH ACT", kRefreshActionFile },
97+
{ "ERASE ACT", kEraseActionFile },
98+
{ "AUTO_RSTCFG", kAutoResetConfigFile },
99+
{ "HARD_RSTCFG", kHardResetConfigFile },
100+
{ "AUTO_ON CFG", kAutomationOnConfigFile },
101+
{ "AUTO_OFFCFG", kAutomationOffConfigFile },
102+
{ "OVFL_ON CFG", kOverflowOnConfigFile },
103+
{ "OVFL_OFFCFG", kOverflowOffConfigFile },
104+
{ "MSD_ON CFG", kMSDOnConfigFile },
105+
{ "MSD_OFF CFG", kMSDOffConfigFile },
106+
};
107+
63108
static uint8_t file_buffer[VFS_SECTOR_SIZE];
64109
static char assert_buf[64 + 1];
65110
static uint16_t assert_line;
@@ -139,47 +184,68 @@ void vfs_user_file_change_handler(const vfs_filename_t filename, vfs_file_change
139184
}
140185

141186
if (VFS_FILE_CREATED == change) {
142-
if (!memcmp(filename, daplink_mode_file_name, sizeof(vfs_filename_t))) {
143-
if (daplink_is_interface()) {
144-
config_ram_set_hold_in_bl(true);
145-
} else {
146-
// Do nothing - bootloader will go to interface by default
187+
int32_t which_magic_file = -1;
188+
189+
// Compare the new file's name to our table of magic filenames.
190+
for (int32_t i = 0; i < ARRAY_SIZE(s_magic_file_info); ++i) {
191+
if (!memcmp(filename, s_magic_file_info[i].name, sizeof(vfs_filename_t))) {
192+
which_magic_file = i;
147193
}
194+
}
148195

149-
vfs_mngr_fs_remount();
150-
} else if (!memcmp(filename, "AUTO_RSTCFG", sizeof(vfs_filename_t))) {
151-
config_set_auto_rst(true);
152-
vfs_mngr_fs_remount();
153-
} else if (!memcmp(filename, "HARD_RSTCFG", sizeof(vfs_filename_t))) {
154-
config_set_auto_rst(false);
155-
vfs_mngr_fs_remount();
156-
} else if (!memcmp(filename, "ASSERT ACT", sizeof(vfs_filename_t))) {
157-
// Test asserts
158-
util_assert(0);
159-
} else if (!memcmp(filename, "REFRESH ACT", sizeof(vfs_filename_t))) {
160-
// Remount to update the drive
161-
vfs_mngr_fs_remount();
162-
} else if (!memcmp(filename, "AUTO_ON CFG", sizeof(vfs_filename_t))) {
163-
config_set_automation_allowed(true);
164-
vfs_mngr_fs_remount();
165-
} else if (!memcmp(filename, "AUTO_OFFCFG", sizeof(vfs_filename_t))) {
166-
config_set_automation_allowed(false);
167-
vfs_mngr_fs_remount();
168-
} else if (!memcmp(filename, "ERASE ACT", sizeof(vfs_filename_t))) {
169-
erase_target();
170-
vfs_mngr_fs_remount();
171-
} else if (!memcmp(filename, "OVFL_ON CFG", sizeof(vfs_filename_t))) {
172-
config_set_overflow_detect(true);
173-
vfs_mngr_fs_remount();
174-
} else if (!memcmp(filename, "OVFL_OFFCFG", sizeof(vfs_filename_t))) {
175-
config_set_overflow_detect(false);
176-
vfs_mngr_fs_remount();
177-
} else if (!memcmp(filename, "MSD_ON ACT", sizeof(vfs_filename_t))) {
178-
config_ram_set_disable_msd(false);
179-
vfs_mngr_fs_remount();
180-
} else if (!memcmp(filename, "MSD_OFF ACT", sizeof(vfs_filename_t))) {
181-
config_ram_set_disable_msd(true);
182-
vfs_mngr_fs_remount();
196+
// Check if we matched a magic filename and handle it.
197+
if (which_magic_file != -1) {
198+
bool do_remount = true; // Almost all magic files cause a remount.
199+
200+
switch (which_magic_file) {
201+
case kDAPLinkModeActionFile:
202+
if (daplink_is_interface()) {
203+
config_ram_set_hold_in_bl(true);
204+
} else {
205+
// Do nothing - bootloader will go to interface by default
206+
}
207+
break;
208+
case kTestAssertActionFile:
209+
// Test asserts
210+
util_assert(0);
211+
do_remount = false;
212+
break;
213+
case kRefreshActionFile:
214+
// Remount to update the drive
215+
break;
216+
case kEraseActionFile:
217+
erase_target();
218+
break;
219+
case kAutoResetConfigFile:
220+
config_set_auto_rst(true);
221+
break;
222+
case kHardResetConfigFile:
223+
config_set_auto_rst(false);
224+
break;
225+
case kAutomationOnConfigFile:
226+
config_set_automation_allowed(true);
227+
break;
228+
case kAutomationOffConfigFile:
229+
config_set_automation_allowed(false);
230+
break;
231+
case kOverflowOnConfigFile:
232+
config_set_overflow_detect(true);
233+
break;
234+
case kOverflowOffConfigFile:
235+
config_set_overflow_detect(false);
236+
break;
237+
case kMSDOnConfigFile:
238+
config_ram_set_disable_msd(false);
239+
break;
240+
case kMSDOffConfigFile:
241+
config_ram_set_disable_msd(true);
242+
break;
243+
244+
}
245+
246+
if (do_remount) {
247+
vfs_mngr_fs_remount();
248+
}
183249
}
184250
}
185251

0 commit comments

Comments
 (0)