Skip to content

Add extended callback support #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 89 additions & 48 deletions extras/tests/Arduino_POSIXStorage_Test/Arduino_POSIXStorage_Test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ enum TestTypes : uint8_t
TEST_PORTENTA_H7_USB,
TEST_PORTENTA_MACHINE_CONTROL_SDCARD,
TEST_PORTENTA_MACHINE_CONTROL_USB,
TEST_OPTA_SDCARD,
TEST_OPTA_USB
TEST_OPTA_SDCARD, // Not currently implemented
TEST_OPTA_USB // Logging to thumb drive
};

// !!! TEST CONFIGURATION !!! -->
Expand All @@ -35,12 +35,18 @@ constexpr enum TestTypes selectedTest = TEST_PORTENTA_C33_SDCARD;
// <-- !!! TEST CONFIGURATION !!!

volatile bool usbAttached = false;
volatile bool usbDetached = false;

void usbCallback()
{
usbAttached = true;
}

void usbCallback2()
{
usbDetached = true;
}

void setup() {
bool allTestsOk = true;
enum StorageDevices deviceName;
Expand Down Expand Up @@ -71,22 +77,24 @@ void setup() {
Serial.println("Testing started, please wait...");
Serial.println();

if ((TEST_PORTENTA_MACHINE_CONTROL_SDCARD == selectedTest) || (TEST_OPTA_SDCARD == selectedTest))
if (TEST_PORTENTA_MACHINE_CONTROL_SDCARD == selectedTest)
{
// Machine Control and Opta no SD Card supported test -->
// Machine Control no SD Card supported test -->
retVal = mount(DEV_SDCARD, FS_FAT, MNT_DEFAULT);
if ((-1 != retVal) || (ENOTBLK != errno))
{
Serial.println("[FAIL] Machine Control and Opta no SD Card supported test failed");
Serial.println("[FAIL] Machine Control no SD Card supported test failed");
Serial.println();
Serial.println("FAILURE: Finished with errors (see list above for details)");
}
else
{
Serial.println("Testing complete.");
Serial.println();
Serial.println("SUCCESS: Finished without errors");
(void) umount(DEV_SDCARD);
for ( ; ; ) ; // Stop testing here
}
for ( ; ; ) ; // Stop testing here
// <-- Machine Control and Opta no SD Card supported test
}

Expand All @@ -104,60 +112,55 @@ void setup() {
}
// <-- Register hotplug callback for SD Card test

if (TEST_PORTENTA_C33_USB == selectedTest)
// Register unplug callback for SD Card test -->
if (DEV_SDCARD == deviceName)
{
// Register nullptr callback test -->
retVal = register_hotplug_callback(DEV_USB, nullptr);
if ((-1 != retVal) || (EFAULT != errno))
// Using usbCallback2() is fine because it doesn't get registered anyway
retVal = register_unplug_callback(DEV_SDCARD, usbCallback2);
if ((-1 != retVal) || (ENOTSUP != errno))
{
allTestsOk = false;
Serial.print("[FAIL] Register nullptr callback test failed");
Serial.print("[FAIL] Register unplug callback for SD Card test failed");
Serial.println();
}
// <-- Register nullptr callback test
}
// <-- Register unplug callback for SD Card test

if ((TEST_PORTENTA_H7_USB == selectedTest) || (TEST_PORTENTA_MACHINE_CONTROL_USB == selectedTest) || (TEST_OPTA_USB == selectedTest))
if (DEV_USB == deviceName)
{
// Register unsupported callback test -->
retVal = register_hotplug_callback(DEV_USB, usbCallback);
if ((-1 != retVal) || (ENOTSUP != errno))
// Register nullptr callback test (hotplug) -->
retVal = register_hotplug_callback(DEV_USB, nullptr);
if ((-1 != retVal) || (EFAULT != errno))
{
allTestsOk = false;
Serial.println("[FAIL] Register unsupported callback test");
Serial.print("[FAIL] Register nullptr callback test failed (hotplug)");
Serial.println();
}
// <-- Register unsupported callback test
// <-- Register nullptr callback test (hotplug)

// Register nullptr callback test (unplug) -->
retVal = register_unplug_callback(DEV_USB, nullptr);
if ((-1 != retVal) || (EFAULT != errno))
{
allTestsOk = false;
Serial.print("[FAIL] Register nullptr callback test failed (unplug)");
Serial.println();
}
// <-- Register nullptr callback test (unplug)
}

// This isn't a test, just wait for a USB thumb drive -->
// Wait for a USB thumb drive -->
if (DEV_USB == deviceName)
{
Serial.println("Please insert a thumb drive");
if (TEST_PORTENTA_C33_USB == selectedTest)
{
// This board supports hotplug callbacks
(void) register_hotplug_callback(DEV_USB, usbCallback);
while (false == usbAttached) {
delay(500);
}
}
else if ((TEST_PORTENTA_H7_USB == selectedTest) || (TEST_PORTENTA_MACHINE_CONTROL_USB == selectedTest) || (TEST_OPTA_USB == selectedTest))
{
// These boards don't support hotplug callbacks, so loop on mount() tries
while (0 != mount(DEV_USB, FS_FAT, MNT_DEFAULT)) {
delay(500);
}
(void) umount(DEV_USB);
}
else
{
for ( ; ;) ; // Shouldn't get here unless there's a bug in the test code
(void) register_hotplug_callback(DEV_USB, usbCallback);
while (false == usbAttached) {
delay(500);
}
Serial.println("Thank you!");
Serial.println();
}
// <-- This isn't a test, just wait for a USB thumb drive
// <-- Wait for a USB thumb drive

#if defined(PERFORM_FORMATTING_TESTS)
Serial.println("The formatting tests you selected can take a while to complete");
Expand Down Expand Up @@ -335,26 +338,35 @@ void setup() {
(void) umount(deviceName);
// <-- mount() when already mounted test

if (TEST_PORTENTA_C33_USB == selectedTest)
if (DEV_USB == deviceName)
{
// Register multiple callbacks test -->
// Register multiple callbacks test (hotplug) -->
retVal = register_hotplug_callback(DEV_USB, usbCallback);
if ((-1 != retVal) || (EBUSY != errno))
{
allTestsOk = false;
Serial.println("[FAIL] Register multiple callbacks test failed");
Serial.println("[FAIL] Register multiple callbacks test failed (hotplug)");
}
// <-- Register multiple callbacks test
// <-- Register multiple callbacks test (hotplug)
}

// Deregister callback not supported test -->
// Deregister callback not supported test (hotplug) -->
retVal = deregister_hotplug_callback(DEV_USB);
if ((-1 != retVal) || (ENOSYS != errno))
{
allTestsOk = false;
Serial.println("[FAIL] Deregister callback not supported test failed");
Serial.println("[FAIL] Deregister callback not supported test failed (hotplug)");
}
// <-- Deregister callback not supported test
// <-- Deregister callback not supported test (hotplug)

// Deregister callback not supported test (unplug) -->
retVal = deregister_unplug_callback(DEV_USB);
if ((-1 != retVal) || (ENOSYS != errno))
{
allTestsOk = false;
Serial.println("[FAIL] Deregister callback not supported test failed (unplug)");
}
// <-- Deregister callback not supported test (unplug)

// Remove before persistent storage test -->
(void) mount(deviceName, FS_FAT, MNT_DEFAULT);
Expand Down Expand Up @@ -451,6 +463,35 @@ void setup() {
(void) umount(deviceName);
// <-- Persistent storage test

// These tests can't be performed on the Opta because we log to USB
if (TEST_OPTA_USB != selectedTest)
{
// Wait for USB thumb drive removal -->
if (DEV_USB == deviceName)
{
Serial.println();
Serial.println("Please remove the thumb drive");
(void) register_unplug_callback(DEV_USB, usbCallback2);
while (false == usbDetached) {
delay(500);
}
Serial.println("Thank you!");
}
// <-- Wait for USB thumb drive removal

if (DEV_USB == deviceName)
{
// Register multiple callbacks test (unplug) -->
retVal = register_unplug_callback(DEV_USB, usbCallback2);
if ((-1 != retVal) || (EBUSY != errno))
{
allTestsOk = false;
Serial.println("[FAIL] Register multiple callbacks test failed (unplug)");
}
// <-- Register multiple callbacks test (unplug)
}
}

// Final report -->
Serial.println();
Serial.println("Testing complete.");
Expand All @@ -468,7 +509,7 @@ void setup() {
// Opta final report -->
if (TEST_OPTA_USB == selectedTest)
{
(void) mount(deviceName, FS_FAT, MNT_DEFAULT);
(void) mount(DEV_USB, FS_FAT, MNT_DEFAULT);
FILE *logFile = fopen("/usb/testlog.txt", "w");
if (true == allTestsOk)
{
Expand All @@ -480,7 +521,7 @@ void setup() {
fprintf(logFile, "FAILURE: Finished with errors");
fclose(logFile);
}
(void) umount(deviceName);
(void) umount(DEV_USB);
}
// <--
}
Expand Down
Loading