Copyright 2023 Travis J. West, https://traviswest.ca, Input Devices and Music Interaction Laboratory (IDMIL), Centre for Interdisciplinary Research in Music Media and Technology (CIRMMT), McGill University, Montréal, Canada, and Univ. Lille, Inria, CNRS, Centrale Lille, UMR 9189 CRIStAL, F-59000 Lille, France
SPDX-License-Identifier: MIT
This document describes the implementation of the SPIFFS session data storage component for ESP32, compatible with the RapidJSON session manager.
Overview
Much of the functionality of this component is derived from the underlying JSON session management component, which formats the stored data when session parameters change. This component, the SPIFFS storage component, has these main responsibilities: to set up the SPIFFS virtual filesystem, open and close files appropriately, and pass streams to RapidJsonSessionStorage so that it can read and write from these files.
The main subroutine simply delegates to the session management class. Most of the SPIFFS-specific functionality for this subroutine is wrapped in the output stream class.
SpiffsJsonOStream
As seen in the tests for RapidJsonSessionStorage, the output stream needs to be wrapped in a class that prepares the stream on construction and cleans it up on destruction. This is achieved very simply here using rapidjson::FileWriteStream and the standard library. The constructor simply opens a file for writing (truncating its contents), zero initializes a buffer, passes these to the rapidjson stream, and passes the stream to a rapidjson writer. The stream is then ready to accept data. When the object destructor is called, the file is closed.
Wrapping the stream in this way allows the overhead of opening and closing a file, and the strong side effect of truncating the file when opening it in write mode, to be avoided when the file does not need to be updated.
In order to use SPIFFS, we are required to provide a custom partition table that declares a data partition with spiffs subtype where the SPIFFS will be located, such as the following:
# name, type, subtype, offset, size, flags
nvs, data, nvs, 0x9000, 0x4000,
phy_init, data, phy, 0xf000, 0x1000,
main, app, factory, 0x10000, 0x290000,
storage, data, spiffs, 0x300000, 1M,
The sdkconfig is then directed to use this partition table by setting the following two lines: