pelion-example-common-DISCO_F413ZH

Revision:
23:64f3f756acf6
Parent:
19:7e760f65ed78
Child:
25:d8a84856bd96
--- a/main.cpp	Thu Mar 21 19:44:55 2019 +0000
+++ b/main.cpp	Thu Mar 21 22:03:46 2019 +0000
@@ -30,10 +30,10 @@
 
 #if COMPONENT_SD || COMPONENT_NUSD
 // Use FATFileSystem for SD card type blockdevices
-FATFileSystem fs("fs", bd);
+FATFileSystem fs("fs");
 #else
 // Use LittleFileSystem for non-SD block devices to enable wear leveling and other functions
-LittleFileSystem fs("fs", bd);
+LittleFileSystem fs("fs");
 #endif
 
 // Default User button for GET example
@@ -134,17 +134,26 @@
 int main(void) {
     printf("\nStarting Simple Pelion Device Management Client example\n");
 
+    int storage_status = fs.mount(bd);
+    if (storage_status != 0) {
+        printf("Storage mounting failed.\n");
+    }
+
+#if USE_BUTTON == 1
     // If the User button is pressed ons start, then format storage.
-    if (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE) {
-        printf("User button is pushed on start. Formatting the storage...\n");
-        int storage_status = fs.reformat(bd);
+    bool btn_pressed = (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE);
+    if (btn_pressed) {
+        printf("User button is pushed on start...\n");
+    }
+#else
+    bool btn_pressed = FALSE;
+#endif /* USE_BUTTON */
+
+    if (storage_status || btn_pressed) {
+        printf("Formatting the storage...\n");
+        int storage_status = StorageHelper::format(&fs, bd);
         if (storage_status != 0) {
-            if (bd->erase(0, bd->size()) == 0) {
-                if (fs.format(bd) == 0) {
-                    storage_status = 0;
-                    printf("The storage reformatted successfully.\n");
-                }
-            }
+            printf("ERROR: Failed to reformat the storage (%d).\n", storage_status);
         }
     } else {
         printf("You can hold the user button during boot to format the storage and change the device identity.\n");