ESPHome
2026.12.0-dev
Loading...
Searching...
No Matches
esphome
core
preferences.cpp
Go to the documentation of this file.
1
#include "
esphome/core/preferences.h
"
2
#include "
esphome/core/log.h
"
3
#include <cinttypes>
4
5
namespace
esphome
{
6
7
#ifdef USE_PREFERENCE_KEY_LOOKUP
8
ESPHOME_LOG_TAG
(TAG,
"preferences"
);
9
#endif
10
11
bool
migrate_preference
(
ESPPreferenceObject
&new_pref, uint8_t *
scratch
,
size_t
size
, [[maybe_unused]]
uint32_t
old_key,
12
[[maybe_unused]]
uint32_t
new_key) {
13
if
(new_pref.
load
(
scratch
,
size
))
14
return
true
;
// Current data present - never overwrite newer data with the old copy
15
#ifdef USE_PREFERENCE_KEY_LOOKUP
16
// One-shot read by key: no backend is allocated for the old key, so boots with
17
// nothing to migrate (for example fresh installs) cost no heap
18
if
(old_key == new_key || !
global_preferences
->
load_from_key
(old_key,
scratch
,
size
))
19
return
false
;
// No data stored under the old key, nothing to migrate
20
if
(!new_pref.
save
(
scratch
,
size
)) {
21
ESP_LOGW(TAG,
"Pref migration %"
PRIx32
" -> %"
PRIx32
" failed"
, old_key, new_key);
22
}
23
return
true
;
24
#else
25
return
false
;
26
#endif
27
}
28
29
}
// namespace esphome
esphome::ESPPreferenceObject
Definition
preference_backend.h:58
esphome::ESPPreferenceObject::save
bool save(const T *src)
Definition
preference_backend.h:63
esphome::ESPPreferenceObject::load
bool load(T *dest)
Definition
preference_backend.h:65
preferences.h
log.h
esphome::esp32::ESPHOME_LOG_TAG
ESPHOME_LOG_TAG(TAG, CRASH_LOG_TAG)
esphome
Definition
a01nyub.cpp:7
esphome::size
uint16_t size
Definition
helpers.cpp:44
esphome::global_preferences
ESPPreferences * global_preferences
Definition
preferences.cpp:477
esphome::migrate_preference
bool migrate_preference(ESPPreferenceObject &new_pref, uint8_t *scratch, size_t size, uint32_t old_key, uint32_t new_key)
Copy preference data stored under old_key into new_pref (created for new_key) if the keys differ and ...
Definition
preferences.cpp:11
uint32_t
static void uint32_t
Definition
crash_handler.cpp:179
scratch
watchdog_hw scratch[0]
Definition
crash_handler.cpp:210
esphome::Preferences::load_from_key
bool load_from_key(uint32_t, uint8_t *, size_t)
Definition
preferences.h:26
Generated by
1.12.0