esp32-warm-water
esp32 based project for the control of a heating element based on temperature
spiffs.c
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2021 wolffshots
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
30 // rest of the includes
31 #include <stdio.h>
32 #include <string.h>
33 #include <sys/unistd.h>
34 #include <sys/stat.h>
35 #include "esp_err.h"
36 #include "esp_log.h"
37 #include "esp_spiffs.h"
38 
39 #include <sys/param.h>
40 #include <sys/unistd.h>
41 #include <dirent.h>
42 
43 #include "spiffs.h"
44 
45 #include "utility.h"
46 
47 // variables
48 static const char *TAG = CONFIG_SPIFFS_TAG;
49 
50 // function definitions
51 
52 off_t get_file_size(const char *filename)
53 {
54  struct stat st;
55  if (stat(filename, &st) == 0)
56  {
57  ESP_LOGI(TAG, "file size: %ld", st.st_size);
58  return st.st_size;
59  }
60  ESP_LOGI(TAG, "Cannot determine size of %s\n",
61  filename);
62  return -1;
63 }
64 
65 void read_file(const char *pwd)
66 {
67  ESP_LOGI(TAG, "Reading %s", pwd);
68  // Open for reading hello.txt
69  FILE *file = fopen(pwd, "r");
70  if (file == NULL)
71  {
72  ESP_LOGE(TAG, "Failed to open %s", pwd);
73  return;
74  }
75  // print_chip_info();
76  off_t file_size = get_file_size(pwd);
77  char buf[file_size < 2000 ? file_size : 2000];
78  // print_chip_info();
79  off_t current_position = 2000;
80  while (current_position < file_size + 2000)
81  {
82  memset(buf, 0, sizeof(buf));
83  fread(buf, 1, sizeof(buf), file);
84  ESP_LOGI(TAG, "%s", buf);
85  current_position += 1088;
86  }
87  fclose(file);
88  // Display the read contents from the file
89  ESP_LOGI(TAG, "Read from %s", pwd);
90 }
91 
92 void list_files()
93 {
94  /* Iterate over all files / folders and fetch their names and sizes */
95  char entrypath[40];
96  const char *entrytype;
97  struct dirent *entry;
98  DIR *dir = opendir("/spiffs");
99  const size_t dirpath_len = strlen("/spiffs");
100  while ((entry = readdir(dir)) != NULL)
101  {
102  entrytype = (entry->d_type == DT_DIR ? "directory" : "file");
103  strlcpy(entrypath + dirpath_len, entry->d_name, sizeof(entrypath) - dirpath_len);
104  ESP_LOGI(TAG, "%s : %s", entrytype, entry->d_name);
105  }
106  closedir(dir);
107 }
112 {
113  ESP_LOGI(TAG, "Initializing SPIFFS");
114 
115  esp_vfs_spiffs_conf_t conf = {
116  .base_path = "/spiffs",
117  .partition_label = NULL,
118  .max_files = 5,
119  .format_if_mount_failed = false};
120 
121  // Use settings defined above to initialize and mount SPIFFS filesystem.
122  // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
123  esp_err_t ret = esp_vfs_spiffs_register(&conf);
124 
125  if (ret != ESP_OK)
126  {
127  if (ret == ESP_FAIL)
128  {
129  ESP_LOGE(TAG, "Failed to mount or format filesystem");
130  }
131  else if (ret == ESP_ERR_NOT_FOUND)
132  {
133  ESP_LOGE(TAG, "Failed to find SPIFFS partition");
134  }
135  else
136  {
137  ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
138  }
139  return;
140  }
141 
142  size_t total = 0, used = 0;
143  ret = esp_spiffs_info(NULL, &total, &used);
144  if (ret != ESP_OK)
145  {
146  ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
147  }
148  else
149  {
150  ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
151  }
152 
153  list_files();
154 
155  // Read and display the contents of a small text file (hello.txt)
156  // read_file("/spiffs/LICENSE.md");
157 }
158 
160  // All done, unmount partition and disable SPIFFS
161  esp_vfs_spiffs_unregister(NULL);
162  ESP_LOGI(TAG, "SPIFFS unmounted");
163 }
void spiffs_deinit()
Definition: spiffs.c:159
void list_files()
Definition: spiffs.c:92
void spiffs_init()
Definition: spiffs.c:111
off_t get_file_size(const char *filename)
Definition: spiffs.c:52
void read_file(const char *pwd)
Definition: spiffs.c:65
defininitions for spiffs
defininitions for simple utility functions
const char * TAG
Definition: wifi_sta.c:42