Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / Internet of things / goam / main / spiffs.c
blob: c92166fa64b63490856ebff1bb77712b752551f5 (plain) (blame)
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
129
130
131
132
133
134
135
#include <sys/unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_log.h"
#include "esp_spiffs.h"

#include "spiffs.h"

static const char *TAG = "example";

/*
 * Public interface
 */

static SemaphoreHandle_t rw_mutex = NULL;

void spiffs_init() {
	esp_vfs_spiffs_conf_t conf = {
		.base_path              = SPIFFS_PREFIX,
		.partition_label        = NULL,
		.max_files              = 1,
		.format_if_mount_failed = true
	};

	// Use settings defined above to initialize and mount SPIFFS filesystem.
	// Note: esp_vfs_spiffs_register is an all-in-one convenience function.
	ESP_ERROR_CHECK(esp_vfs_spiffs_register(&conf));

#ifdef CONFIG_SPIFFS_CHECK_ON_START
	ESP_LOGI(TAG, "Performing SPIFFS_check().");
	ESP_ERROR_CHECK(esp_spiffs_check(conf.partition_label));
#endif

	size_t total = 0, used = 0;
	esp_err_t ret = esp_spiffs_info(conf.partition_label, &total, &used);
	if (ret != ESP_OK) {
		ESP_LOGW(TAG, "Failed to get SPIFFS partition information (%s). Formatting...", esp_err_to_name(ret));
		esp_spiffs_format(conf.partition_label);
		ESP_ERROR_CHECK(esp_spiffs_info(conf.partition_label, &total, &used));
	}

	ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);

	// Check consistency of reported partiton size info.
	if (used > total) {
		ESP_LOGW(TAG, "Number of used bytes cannot be larger than total. Performing SPIFFS_check().");
		ret = esp_spiffs_check(conf.partition_label);
		// Could be also used to mend broken files, to clean unreferenced pages, etc.
		// More info at https://github.com/pellepl/spiffs/wiki/FAQ#powerlosses-contd-when-should-i-run-spiffs_check
		if (ret != ESP_OK) {
			ESP_LOGE(TAG, "SPIFFS_check() failed (%s)", esp_err_to_name(ret));
		} else {
			ESP_LOGI(TAG, "SPIFFS_check() successful");
		}
	}

	rw_mutex = xSemaphoreCreateMutex();
	if (rw_mutex == NULL) {
		ESP_LOGE(TAG, "Couldn't create Read/Write mutex, SPIFFS operations will crash");
	}
}

void spiffs_write(const char* filename, const char* data, int data_len) {
	if (xSemaphoreTake(rw_mutex, 100) == pdFALSE) {
		return;
	}

	int outf = open(filename, O_WRONLY | O_CREAT | O_TRUNC);
	if (outf < 0) {
		ESP_LOGE(TAG, "Couldn't open file \"%s\" for writing!", filename);
		xSemaphoreGive(rw_mutex);
		return;
	}

	int length = write(outf, data, data_len);
	for (int tries = 0; length < data_len && tries < 3; tries++) {
		ESP_LOGW(TAG, "Wrote only %d bytes to \"%s\", trying again...", length, filename);
		vTaskDelay(pdMS_TO_TICKS(50));
		length = write(outf, data, data_len);
	}

	close(outf);
	if (length < 0) {
		ESP_LOGE(TAG, "Wrote only %d/%d bytes for \"%s\"", length, data_len, filename);
	}
	else {
		ESP_LOGD(TAG, "Wrote %d bytes from \"%s\"", length, filename);
	}
	xSemaphoreGive(rw_mutex);
}

static int readers = 0;

int spiffs_read(const char* filename, char** data) {
	if (readers == 0) {
		if (xSemaphoreTake(rw_mutex, 100) == pdTRUE) {
			vTaskDelay(pdMS_TO_TICKS(150));
		}
		else {
			*data = NULL;
			return 0;
		}
	}
	readers++;

	int inf = open(filename, O_RDONLY);
	if (inf < 0) {
		ESP_LOGW(TAG, "Couldn't read from file \"%s\"!", filename);
		readers--;
		if (readers == 0) xSemaphoreGive(rw_mutex);
		return 0;
	}

	struct stat buf;
	if (fstat(inf, &buf) < 0) {
		close(inf);
		ESP_LOGE(TAG, "Couldn't read stat of file \"%s\"!", filename);
		readers--;
		if (readers == 0) xSemaphoreGive(rw_mutex);
		return 0;
	}

	int length = buf.st_size;
	*data = (char*)malloc(length);
	read(inf, *data, length);

	close(inf);
	ESP_LOGD(TAG, "read %d bytes from \"%s\"", length, filename);
	readers--;
	if (readers == 0) xSemaphoreGive(rw_mutex);
	return length;
}