EepromSecureData
CRC-controlled EEPROM memory storage
EepromSecureData.h
1 /*
2  Copyright (C) 2023 epsilonrt <epsilonrt@gmail.com>
3  SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5 #include <EEPROM.h>
6 #include <assert.h>
7 #include "CrcIButton.h"
8 #include "EepromBase.h"
9 #pragma once
10 
16 template <class T>
18 
19  public:
25  EepromSecureData () : m_eem (EepromBase::counter) {
26  EepromBase::counter += size();
27  // assert (EepromBase::counter < EEPROM.length());
28  }
29 
36  m_data = data;
37  }
38 
45  save();
46  }
47 
53  bool save() {
54 
55  updateCrc();
56  EEPROM.put (m_eem, m_data);
57  EEPROM.write (m_eem + sizeof (T), m_crc.value ());
58  #if defined(ESP32) || defined(ESP8266)
59  return EEPROM.commit();
60  #else
61  return true;
62  #endif
63  }
64 
72  bool load (const T &defaultData = T()) {
73 
74  EEPROM.get (m_eem, m_data);
75  uint8_t crc = EEPROM.read (m_eem + sizeof (T));
76  updateCrc();
77 
78  if (m_crc.value () != crc) {
79  m_data = defaultData;
80  updateCrc();
81  save();
82  return false;
83  }
84  return true;
85  }
86 
90  inline uint16_t size() const {
91  return sizeof (T) + sizeof (m_crc);
92  }
93 
97  T &data() {
98  return m_data;
99  }
100 
104  const T &data() const {
105  return m_data;
106  }
107 
113  operator T &() {
114  return m_data;
115  }
116 
122  operator const T &() const {
123  return m_data;
124  }
125 
131  T *operator&() {
132  return & m_data;
133  }
134 
140  const T *operator&() const {
141  return & m_data;
142  }
143 
147  T &operator= (const T &t) {
148  m_data = t;
149  updateCrc();
150  return m_data;
151  }
152 
157  m_data = rhs.m_data;
158  return *this;
159  }
160 
164  bool operator == (const EepromSecureData &rhs) const {
165  return m_data == rhs.m_data;
166  }
167 
171  bool operator != (const EepromSecureData &rhs) const {
172  return ! (*this == rhs);
173  }
174 
175  protected:
176  // update CRC
177  void updateCrc () {
178  m_crc.reset ();
179  m_crc.update (m_data);
180  }
181 
182  protected:
183  uint16_t m_eem; // EEPROM memory address of the object
184  T m_data; // data object itself (RAM)
185  CrcIButton <T> m_crc; // CRC object
186 };
iButton CRC template class
Definition: CrcIButton.h:15
EEPROM memory storage management class.
Definition: EepromBase.h:21
CRC-controlled EEPROM memory storage template class.
EepromSecureData()
Construct a new Eeprom Secure Data object.
const T * operator&() const
Overloaded cast operator to const T.
T & operator=(const T &t)
Overloaded assignment operator from T.
const T & data() const
Get the data object as const.
uint16_t size() const
Get the size of the object with CRC.
bool load(const T &defaultData=T())
Load data from EEPROM memory with CRC check.
~EepromSecureData()
Destroy the Eeprom Secure Data object.
EepromSecureData(const T &data)
Construct a new Eeprom Secure Data object with initial data.
bool save()
Save data to EEPROM memory with CRC.
bool operator==(const EepromSecureData &rhs) const
Overloaded equality operator.
T * operator&()
Overloaded cast operator to T *.
bool operator!=(const EepromSecureData &rhs) const
Overloaded not-equality operator.
T & data()
Get the data object.