-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.cpp
More file actions
204 lines (184 loc) · 6.22 KB
/
storage.cpp
File metadata and controls
204 lines (184 loc) · 6.22 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "storage.hpp"
#include <FFat.h>
#include <USB.h>
#include <USBMSC.h>
#include <atomic>
#include <cassert>
#include <condition_variable>
#include <cstdint>
#include <esp32-hal-log.h>
#include <esp_err.h>
#include <esp_partition.h>
#include <iostream>
#include <mutex>
#include <thread>
#if defined(ARDUINO_USB_MODE)
static_assert(ARDUINO_USB_MODE == 0, "USB must be in OTG mode");
#endif
const esp_partition_t *check_ffat_partition(const char *label); // defined in FFat.cpp
static USBMSC usbMsc;
static constexpr std::uint16_t blockSize = 512; // Should be 512
static const esp_partition_t *partition = nullptr;
static const char *const TAG = "STORAGE";
static std::condition_variable stateChangeRequested;
static std::condition_variable stateChanged;
static std::atomic<bool> requestFileSystemActive;
static std::thread stateMachine;
static std::mutex fileSystemState_mutex; //!< used to lock the actual state
static bool fileSystemIsActive; //!< describes the current state
static bool storageIsInitialized = false;
std::optional<std::shared_ptr<fs::FS>> Storage::getFileSystem_locking(const std::chrono::milliseconds rel_time)
{
if (!storageIsInitialized)
{
return std::nullopt;
}
std::unique_lock fs_state_lock{fileSystemState_mutex};
if (!fileSystemIsActive)
{
if (!stateChanged.wait_for(fs_state_lock, rel_time, []() { return fileSystemIsActive; }))
{
return std::nullopt;
}
}
fs_state_lock.release();
return std::shared_ptr<fs::FS>{&FFat, [](fs::FS *) { fileSystemState_mutex.unlock(); }};
}
static void requestState(const bool fileSystemActive)
{
requestFileSystemActive = fileSystemActive;
ESP_LOGD(TAG, "request new state: %s", fileSystemActive ? "true" : "false");
stateChangeRequested.notify_all();
}
static void processStateRequests()
{
while (true)
{
std::unique_lock fs_state_lock{fileSystemState_mutex};
ESP_LOGD(TAG, "waiting for state change request");
stateChangeRequested.wait(fs_state_lock, []() { return requestFileSystemActive != fileSystemIsActive; });
if (fileSystemIsActive = requestFileSystemActive)
{
ESP_LOGI(TAG, "mount FS");
usbMsc.mediaPresent(false);
FFat.end(); // invalidate cache
assert(FFat.begin()); // update data
}
else
{
ESP_LOGI(TAG, "unmount FS");
FFat.end(); // flush and unmount
usbMsc.mediaPresent(true);
}
stateChanged.notify_all();
}
}
/**
* Callback invoked when received WRITE10 command.
*
* Process data in buffer to disk's storage.
*
* @param lba logical block address
* @returns the number of written bytes (must be multiple of block size)
*/
static std::int32_t usbMsc_onWrite(const std::uint32_t lba, const std::uint32_t offset, std::uint8_t *const buffer,
const uint32_t bufsize)
{
ESP_LOGV(TAG, "MSC WRITE: lba: %u, offset: %u, bufsize: %u\n", lba, offset, bufsize);
const std::uint32_t byteOffset = lba * blockSize + offset;
ESP_ERROR_CHECK(esp_partition_erase_range(partition, byteOffset, bufsize)); // erase must be called before write
ESP_ERROR_CHECK(esp_partition_write(partition, byteOffset, buffer, bufsize));
return bufsize;
}
/**
* Callback invoked when received READ10 command.
*
* Copy disk's data to buffer (up to bufsize).
*
* @param lba logical block address
* @returns the number of copied bytes (must be multiple of block size)
*/
static std::int32_t usbMsc_onRead(const std::uint32_t lba, const std::uint32_t offset, void *const buffer,
const std::uint32_t bufsize)
{
ESP_LOGV(TAG, "MSC READ: lba: %u, offset: %u, bufsize: %u\n", lba, offset, bufsize);
const std::uint32_t byteOffset = lba * blockSize + offset;
ESP_ERROR_CHECK(esp_partition_read(partition, byteOffset, buffer, bufsize));
return bufsize;
}
static bool usbMsc_onStartStop(const std::uint8_t power_condition, const bool start, const bool load_eject)
{
ESP_LOGV(TAG, "MSC START/STOP: power: %u, start: %u, eject: %u\n", power_condition, start, load_eject);
return true;
}
std::optional<std::size_t> Storage::size()
{
if (!storageIsInitialized)
{
return std::nullopt;
}
return FFat.totalBytes();
}
static std::atomic<bool> usbIsRunning = false;
static void usbStoppedCallback(void *, esp_event_base_t, int32_t, void *)
{
if (!usbIsRunning)
{
return;
}
usbIsRunning = false;
requestState(true);
}
static void usbStartedCallback(void *, esp_event_base_t, int32_t, void *)
{
usbIsRunning = true;
requestState(false);
}
bool Storage::begin()
{
if (!FFat.begin(true))
{
ESP_LOGE(TAG, "Failed to init files system, flash may not be formatted");
return storageIsInitialized = false;
}
ESP_LOGI(TAG, "file system initialized");
partition = check_ffat_partition(FFAT_PARTITION_LABEL);
if (!partition)
{
ESP_LOGE(TAG, "Error with FAT partition");
return storageIsInitialized = false;
}
ESP_LOGI(TAG, "Flash has a size of %u bytes\n", FFat.totalBytes());
// define state before callbacks are activated
fileSystemIsActive = true;
requestFileSystemActive = true;
stateMachine = std::thread(processStateRequests);
usbMsc.vendorID("ESP32"); // max 8 chars
usbMsc.productID("USB_MSC"); // max 16 chars
usbMsc.productRevision("1.0"); // max 4 chars
usbMsc.onStartStop(usbMsc_onStartStop);
usbMsc.mediaPresent(false);
// Set callback
usbMsc.onRead(usbMsc_onRead);
usbMsc.onWrite(usbMsc_onWrite);
USB.onEvent(ARDUINO_USB_STARTED_EVENT, usbStartedCallback);
USB.onEvent(ARDUINO_USB_STOPPED_EVENT, usbStoppedCallback);
// Set disk size, block size should be 512 regardless of spi flash page size
if (!usbMsc.begin(FFat.totalBytes() / blockSize, blockSize))
{
ESP_LOGE(TAG, "starting USB MSC failed");
return storageIsInitialized = false;
}
if (!USB.begin())
{
ESP_LOGE(TAG, "starting USB failed");
return storageIsInitialized = false;
}
return storageIsInitialized = true;
}
void Storage::end()
{
usbMsc.end();
usbIsRunning = false;
FFat.end();
}