Sygaldry
Loading...
Searching...
No Matches
sygbe-spiffs.hpp
1#pragma once
2/*
3Copyright 2023 Travis J. West, https://traviswest.ca, Input Devices and Music
4Interaction Laboratory (IDMIL), Centre for Interdisciplinary Research in Music
5Media and Technology (CIRMMT), McGill University, Montréal, Canada, and Univ.
6Lille, Inria, CNRS, Centrale Lille, UMR 9189 CRIStAL, F-59000 Lille, France
7
8SPDX-License-Identifier: MIT
9*/
10
11
12#include <cstdio>
13#include <rapidjson/filereadstream.h>
14#include <rapidjson/filewritestream.h>
15#include <rapidjson/writer.h>
16#include <esp_spiffs.h>
17#include "sygbp-rapid_json.hpp"
18
19namespace sygaldry { namespace sygbe {
24
26{
27 static constexpr const char * file_path = "/spiffs/session_storage.json";
28 static constexpr std::size_t buffer_size = 1024;
29 std::FILE * fp;
30 char buffer[buffer_size];
31 rapidjson::FileWriteStream ostream;
32 rapidjson::Writer<rapidjson::FileWriteStream> writer;
34 : fp{std::fopen(file_path, "w")}, buffer{0}
35 , ostream{fp, buffer, buffer_size}, writer{ostream}
36 {
37 if (fp == nullptr) printf("spiffs: unable to open file for writing!\n");
38 // The program will probably crash if this happens for some reason...
39 }
40 ~SpiffsJsonOStream() {std::fclose(fp);}
41};
42
43template<typename Components>
45
46template<typename Components>
48: name_<"SPIFFS Session Storage">
49{
50 Storage<Components> * storage;
51 static constexpr const char * spiffs_base_path = "/spiffs";
52 static constexpr const char * file_path = SpiffsJsonOStream::file_path;
53 static constexpr std::size_t buffer_size = SpiffsJsonOStream::buffer_size;
54
55 void init(Components& components)
56 {
57 storage = new Storage<Components>();
58 // Set up spiffs
59 esp_vfs_spiffs_conf_t conf = {
60 .base_path = "/spiffs",
61 .partition_label = NULL,
62 .max_files = 5,
63 .format_if_mount_failed = true
64 };
65 esp_err_t ret = esp_vfs_spiffs_register(&conf);
66 ESP_ERROR_CHECK_WITHOUT_ABORT(ret);
67 if (ret != ESP_OK) return;
68 // Check partition size info
69 size_t total = 0, used = 0;
70 ret = esp_spiffs_info(conf.partition_label, &total, &used);
71 if (ret != ESP_OK) {
72 printf("spiffs: Failed to get SPIFFS partition information (%s). Formatting...", esp_err_to_name(ret));
73 esp_spiffs_format(conf.partition_label);
74 return;
75 }
76 printf("spiffs: Partition size: total: %d, used: %d\n", total, used);
77
78 // Check consistency of reported partiton size info.
79 if (used > total) {
80 printf("spiffs: Number of used bytes cannot be larger than total. Performing SPIFFS_check().");
81 ret = esp_spiffs_check(conf.partition_label);
82 // Could be also used to mend broken files, to clean unreferenced pages, etc.
83 // More info at https://github.com/pellepl/spiffs/wiki/FAQ#powerlosses-contd-when-should-i-run-spiffs_check
84 if (ret != ESP_OK) {
85 printf("spiffs: SPIFFS_check() failed (%s)", esp_err_to_name(ret));
86 return;
87 } else {
88 printf("spiffs: SPIFFS_check() successful");
89 }
90 }
91 // Open existing file or create an empty one
92 std::FILE * fp = std::fopen(file_path, "r");
93 if (fp == nullptr) fp = std::fopen(file_path, "w+");
94 if (fp == nullptr) {
95 printf("spiffs: Unable to open file for initialization!\n");
96 return;
97 }
98 char buffer[buffer_size];
99 rapidjson::FileReadStream istream{fp, buffer, buffer_size};
100 storage->init(istream, components);
101 std::fclose(fp);
102 }
103
104 void external_destinations(Components& components)
105 {
106 storage->external_destinations(components);
107 }
108};
109
112} }
Document the name of an entity, e.g. an endpoint, component, or binding.
Definition sygah-metadata.hpp:33
Definition sygbe-spiffs.hpp:26
Definition sygbe-spiffs.hpp:49
Definition sygbp-rapid_json.hpp:27