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);
112 " %-12s %-4s %-8s %-10s %-10s",
113 "Name",
"Type",
"Subtype",
"Address",
"Size");
114 esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL);
116 const esp_partition_t *partition = esp_partition_get(it);
117 ESP_LOGCONFIG(TAG,
" %-12s %-4d %-8d 0x%08" PRIX32
" 0x%08" PRIX32, partition->label, partition->type,
118 partition->subtype, partition->address, partition->size);
119 it = esp_partition_next(it);
121 esp_partition_iterator_release(it);
126static const std::map<int, const char *> CHIP_FEATURES = {
127 {CHIP_FEATURE_BLE,
"BLE"},
128 {CHIP_FEATURE_BT,
"BT"},
129 {CHIP_FEATURE_EMB_FLASH,
"EMB Flash"},
130 {CHIP_FEATURE_EMB_PSRAM,
"EMB PSRAM"},
131 {CHIP_FEATURE_WIFI_BGN,
"2.4GHz WiFi"},
135#if defined(USE_ARDUINO)
136 const char *flash_mode;
137 switch (ESP.getFlashChipMode()) {
151 flash_mode =
"FAST_READ";
154 flash_mode =
"SLOW_READ";
157 flash_mode =
"UNKNOWN";
159 ESP_LOGD(TAG,
"Flash Chip: Size=%ukB Speed=%uMHz Mode=%s",
160 ESP.getFlashChipSize() / 1024,
161 ESP.getFlashChipSpeed() / 1000000, flash_mode);
162 device_info +=
"|Flash: " +
to_string(ESP.getFlashChipSize() / 1024) +
163 "kB Speed:" +
to_string(ESP.getFlashChipSpeed() / 1000000) +
"MHz Mode:";
164 device_info += flash_mode;
167 esp_chip_info_t info;
168 esp_chip_info(&info);
169 const char *model = ESPHOME_VARIANT;
170 std::string features;
171 for (
auto feature : CHIP_FEATURES) {
172 if (info.features & feature.first) {
173 features += feature.second;
175 info.features &= ~feature.first;
178 if (info.features != 0)
179 features +=
"Other:" +
format_hex(info.features);
180 ESP_LOGD(TAG,
"Chip: Model=%s, Features=%s Cores=%u, Revision=%u", model, features.c_str(), info.cores,
182 device_info +=
"|Chip: ";
183 device_info += model;
184 device_info +=
" Features:";
185 device_info += features;
186 device_info +=
" Cores:" +
to_string(info.cores);
187 device_info +=
" Revision:" +
to_string(info.revision);
192 device_info +=
"|Framework: ";
194 ESP_LOGD(TAG,
"Framework: Arduino");
195 device_info +=
"Arduino";
196#elif defined(USE_ESP_IDF)
197 ESP_LOGD(TAG,
"Framework: ESP-IDF");
198 device_info +=
"ESP-IDF";
200 ESP_LOGW(TAG,
"Framework: UNKNOWN");
201 device_info +=
"UNKNOWN";
204 ESP_LOGD(TAG,
"ESP-IDF Version: %s", esp_get_idf_version());
205 device_info +=
"|ESP-IDF: ";
206 device_info += esp_get_idf_version();
209 ESP_LOGD(TAG,
"EFuse MAC: %s", mac.c_str());
210 device_info +=
"|EFuse MAC: ";
213 device_info +=
"|Reset: ";
217 device_info +=
"|Wakeup: ";
218 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.