9#include <esp_heap_caps.h>
10#include <esp_system.h>
11#include <esp_chip_info.h>
12#include <esp_partition.h>
23static const char *
const TAG =
"debug";
27static const char *
const RESET_REASONS[] = {
31 "software via esp_restart",
36 "exiting deep sleep mode",
42 "power glitch detected",
46static const char *
const REBOOT_KEY =
"reboot_source";
47static const size_t REBOOT_MAX_LEN = 24;
52 char buffer[REBOOT_MAX_LEN]{};
54 if (component !=
nullptr) {
55 strncpy(buffer, component->get_component_source(), REBOOT_MAX_LEN - 1);
57 ESP_LOGD(TAG,
"Storing reboot source: %s", buffer);
63 std::string reset_reason;
64 unsigned reason = esp_reset_reason();
65 if (reason <
sizeof(RESET_REASONS) /
sizeof(RESET_REASONS[0])) {
66 reset_reason = RESET_REASONS[reason];
67 if (reason == ESP_RST_SW) {
69 char buffer[REBOOT_MAX_LEN]{};
70 if (pref.load(&buffer)) {
71 reset_reason =
"Reboot request from " + std::string(buffer);
75 reset_reason =
"unknown source";
77 ESP_LOGD(TAG,
"Reset Reason: %s", reset_reason.c_str());
81static const char *
const WAKEUP_CAUSES[] = {
84 "external signal using RTC_IO",
85 "external signal using RTC_CNTL",
98 const char *wake_reason;
99 unsigned reason = esp_sleep_get_wakeup_cause();
100 if (reason <
sizeof(WAKEUP_CAUSES) /
sizeof(WAKEUP_CAUSES[0])) {
101 wake_reason = WAKEUP_CAUSES[reason];
103 wake_reason =
"unknown source";
105 ESP_LOGD(TAG,
"Wakeup Reason: %s", wake_reason);
110 ESP_LOGCONFIG(TAG,
"Partition table:");
111 ESP_LOGCONFIG(TAG,
" %-12s %-4s %-8s %-10s %-10s",
"Name",
"Type",
"Subtype",
"Address",
"Size");
112 esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL);
114 const esp_partition_t *partition = esp_partition_get(it);
115 ESP_LOGCONFIG(TAG,
" %-12s %-4d %-8d 0x%08" PRIX32
" 0x%08" PRIX32, partition->label, partition->type,
116 partition->subtype, partition->address, partition->size);
117 it = esp_partition_next(it);
119 esp_partition_iterator_release(it);
124static const std::map<int, const char *> CHIP_FEATURES = {
125 {CHIP_FEATURE_BLE,
"BLE"},
126 {CHIP_FEATURE_BT,
"BT"},
127 {CHIP_FEATURE_EMB_FLASH,
"EMB Flash"},
128 {CHIP_FEATURE_EMB_PSRAM,
"EMB PSRAM"},
129 {CHIP_FEATURE_WIFI_BGN,
"2.4GHz WiFi"},
133#if defined(USE_ARDUINO)
134 const char *flash_mode;
135 switch (ESP.getFlashChipMode()) {
149 flash_mode =
"FAST_READ";
152 flash_mode =
"SLOW_READ";
155 flash_mode =
"UNKNOWN";
157 ESP_LOGD(TAG,
"Flash Chip: Size=%ukB Speed=%uMHz Mode=%s",
158 ESP.getFlashChipSize() / 1024,
159 ESP.getFlashChipSpeed() / 1000000, flash_mode);
160 device_info +=
"|Flash: " +
to_string(ESP.getFlashChipSize() / 1024) +
161 "kB Speed:" +
to_string(ESP.getFlashChipSpeed() / 1000000) +
"MHz Mode:";
162 device_info += flash_mode;
165 esp_chip_info_t info;
166 esp_chip_info(&info);
167 const char *model = ESPHOME_VARIANT;
168 std::string features;
169 for (
auto feature : CHIP_FEATURES) {
170 if (info.features & feature.first) {
171 features += feature.second;
173 info.features &= ~feature.first;
176 if (info.features != 0)
177 features +=
"Other:" +
format_hex(info.features);
178 ESP_LOGD(TAG,
"Chip: Model=%s, Features=%s Cores=%u, Revision=%u", model, features.c_str(), info.cores,
180 device_info +=
"|Chip: ";
181 device_info += model;
182 device_info +=
" Features:";
183 device_info += features;
184 device_info +=
" Cores:" +
to_string(info.cores);
185 device_info +=
" Revision:" +
to_string(info.revision);
190 device_info +=
"|Framework: ";
192 ESP_LOGD(TAG,
"Framework: Arduino");
193 device_info +=
"Arduino";
194#elif defined(USE_ESP_IDF)
195 ESP_LOGD(TAG,
"Framework: ESP-IDF");
196 device_info +=
"ESP-IDF";
198 ESP_LOGW(TAG,
"Framework: UNKNOWN");
199 device_info +=
"UNKNOWN";
202 ESP_LOGD(TAG,
"ESP-IDF Version: %s", esp_get_idf_version());
203 device_info +=
"|ESP-IDF: ";
204 device_info += esp_get_idf_version();
207 ESP_LOGD(TAG,
"EFuse MAC: %s", mac.c_str());
208 device_info +=
"|EFuse MAC: ";
211 device_info +=
"|Reset: ";
215 device_info +=
"|Wakeup: ";
216 device_info += wakeup_reason;
Component * get_current_component()
const std::string & get_name() const
Get the name of this Application set by pre_setup().
virtual bool sync()=0
Commit pending writes to flash.
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
void log_partition_info_()
Logs information about the device's partition table.
std::string get_wakeup_cause_()
sensor::Sensor * psram_sensor_
void on_shutdown() override
uint32_t get_free_heap_()
sensor::Sensor * block_sensor_
std::string get_reset_reason_()
void get_device_info_(std::string &device_info)
void publish_state(float state)
Publish a new state to the front-end.
Providing packet encoding functions for exchanging data with a remote host.
uint32_t fnv1_hash(const std::string &str)
Calculate a FNV-1 hash of str.
std::string format_hex(const uint8_t *data, size_t length)
Format the byte array data of length len in lowercased hex.
ESPPreferences * global_preferences
std::string get_mac_address_pretty()
Get the device MAC address as a string, in colon-separated uppercase hex notation.
uint32_t arch_get_cpu_freq_hz()
std::string to_string(int value)
std::string str_sprintf(const char *fmt,...)
Application App
Global storage of Application pointer - only one Application can exist.