Select one of the symbols to view example projects that use it.
 
Outline
#include <stdio.h>
#include <string.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include "esp_err.h"
#include "esp_log.h"
#include "esp_spiffs.h"
#include "mbedtls/md5.h"
TAG
read_hello_txt()
compute_alice_txt_md5()
app_main()
Files
loading...
SourceVuESP-IDF Framework and Examplesspiffsgen samplemain/spiffsgen_example_main.c
 
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* SPIFFS Image Generation on Build Example * * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense or CC0-1.0 *//* ... */ #include <stdio.h> #include <string.h> #include <sys/unistd.h> #include <sys/stat.h> #include "esp_err.h" #include "esp_log.h" #include "esp_spiffs.h" #include "mbedtls/md5.h"8 includes static const char *TAG = "example"; static void read_hello_txt(void) { ESP_LOGI(TAG, "Reading hello.txt"); // Open for reading hello.txt FILE* f = fopen("/spiffs/hello.txt", "r"); if (f == NULL) { ESP_LOGE(TAG, "Failed to open hello.txt"); return; }{...} char buf[64]; memset(buf, 0, sizeof(buf)); fread(buf, 1, sizeof(buf), f); fclose(f); // Display the read contents from the file ESP_LOGI(TAG, "Read from hello.txt: %s", buf); }{ ... } static void compute_alice_txt_md5(void) { ESP_LOGI(TAG, "Computing alice.txt MD5 hash"); // The file alice.txt lives under a subdirectory, though SPIFFS itself is flat FILE* f = fopen("/spiffs/sub/alice.txt", "r"); if (f == NULL) { ESP_LOGE(TAG, "Failed to open alice.txt"); return; }{...} // Read file and compute the digest chunk by chunk #define MD5_MAX_LEN 16 char buf[64]; mbedtls_md5_context ctx; unsigned char digest[MD5_MAX_LEN]; mbedtls_md5_init(&ctx); mbedtls_md5_starts(&ctx); size_t read; do { read = fread((void*) buf, 1, sizeof(buf), f); mbedtls_md5_update(&ctx, (unsigned const char*) buf, read); }{...} while(read == sizeof(buf)); mbedtls_md5_finish(&ctx, digest); // Create a string of the digest char digest_str[MD5_MAX_LEN * 2]; for (int i = 0; i < MD5_MAX_LEN; i++) { sprintf(&digest_str[i * 2], "%02x", (unsigned int)digest[i]); }{...} // For reference, MD5 should be deeb71f585cbb3ae5f7976d5127faf2a ESP_LOGI(TAG, "Computed MD5 hash of alice.txt: %s", digest_str); fclose(f); }{ ... } void app_main(void) { ESP_LOGI(TAG, "Initializing SPIFFS"); esp_vfs_spiffs_conf_t conf = { .base_path = "/spiffs", .partition_label = NULL, .max_files = 5, .format_if_mount_failed = false }{...}; // Use settings defined above to initialize and mount SPIFFS filesystem. // Note: esp_vfs_spiffs_register is an all-in-one convenience function. esp_err_t ret = esp_vfs_spiffs_register(&conf); if (ret != ESP_OK) { if (ret == ESP_FAIL) { ESP_LOGE(TAG, "Failed to mount or format filesystem"); }{...} else if (ret == ESP_ERR_NOT_FOUND) { ESP_LOGE(TAG, "Failed to find SPIFFS partition"); }{...} else { ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret)); }{...} return; }{...} size_t total = 0, used = 0; ret = esp_spiffs_info(NULL, &total, &used); if (ret != ESP_OK) { ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret)); }{...} else { ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used); }{...} /* The following calls demonstrate reading files from the generated SPIFFS * image. The images should contain the same files and contents as the spiffs_image directory. *//* ... */ // Read and display the contents of a small text file (hello.txt) read_hello_txt(); // Compute and display the MD5 hash of a large text file (alice.txt) compute_alice_txt_md5(); // All done, unmount partition and disable SPIFFS esp_vfs_spiffs_unregister(NULL); ESP_LOGI(TAG, "SPIFFS unmounted"); }{ ... }
Details