9#include <esp_heap_caps.h>
10#include <esp_system.h>
11#include <esp_chip_info.h>
12#include <esp_partition.h>
21static const char *
const TAG =
"debug";
25static const char *
const RESET_REASONS[] = {
29 "software via esp_restart",
34 "exiting deep sleep mode",
40 "power glitch detected",
44static const char *
const REBOOT_KEY =
"reboot_source";
45static const size_t REBOOT_MAX_LEN = 24;
50 char buffer[REBOOT_MAX_LEN]{};
53 strncpy(buffer, LOG_STR_ARG(
component->get_component_log_str()), REBOOT_MAX_LEN - 1);
54 buffer[REBOOT_MAX_LEN - 1] =
'\0';
56 ESP_LOGD(TAG,
"Storing reboot source: %s", buffer);
62 std::string reset_reason;
63 unsigned reason = esp_reset_reason();
64 if (reason <
sizeof(RESET_REASONS) /
sizeof(RESET_REASONS[0])) {
65 reset_reason = RESET_REASONS[reason];
66 if (reason == ESP_RST_SW) {
68 char buffer[REBOOT_MAX_LEN]{};
69 if (pref.load(&buffer)) {
70 buffer[REBOOT_MAX_LEN - 1] =
'\0';
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);
131static constexpr ChipFeature CHIP_FEATURES[] = {
132 {CHIP_FEATURE_BLE,
"BLE"},
133 {CHIP_FEATURE_BT,
"BT"},
134 {CHIP_FEATURE_EMB_FLASH,
"EMB Flash"},
135 {CHIP_FEATURE_EMB_PSRAM,
"EMB PSRAM"},
136 {CHIP_FEATURE_WIFI_BGN,
"2.4GHz WiFi"},
140#if defined(USE_ARDUINO)
141 const char *flash_mode;
142 switch (ESP.getFlashChipMode()) {
156 flash_mode =
"FAST_READ";
159 flash_mode =
"SLOW_READ";
162 flash_mode =
"UNKNOWN";
164 ESP_LOGD(TAG,
"Flash Chip: Size=%ukB Speed=%uMHz Mode=%s",
165 ESP.getFlashChipSize() / 1024,
166 ESP.getFlashChipSpeed() / 1000000, flash_mode);
167 device_info +=
"|Flash: " + to_string(ESP.getFlashChipSize() / 1024) +
168 "kB Speed:" + to_string(ESP.getFlashChipSpeed() / 1000000) +
"MHz Mode:";
169 device_info += flash_mode;
172 esp_chip_info_t info;
173 esp_chip_info(&info);
174 const char *model = ESPHOME_VARIANT;
175 std::string features;
178 for (
const auto &feature : CHIP_FEATURES) {
179 if (info.features & feature.bit) {
180 features += feature.name;
182 info.features &= ~feature.bit;
185 if (info.features != 0)
186 features +=
"Other:" +
format_hex(info.features);
187 ESP_LOGD(TAG,
"Chip: Model=%s, Features=%s Cores=%u, Revision=%u", model, features.c_str(), info.cores,
189 device_info +=
"|Chip: ";
190 device_info += model;
191 device_info +=
" Features:";
192 device_info += features;
193 device_info +=
" Cores:" + to_string(info.cores);
194 device_info +=
" Revision:" + to_string(info.revision);
199 device_info +=
"|Framework: ";
201 ESP_LOGD(TAG,
"Framework: Arduino");
202 device_info +=
"Arduino";
203#elif defined(USE_ESP_IDF)
204 ESP_LOGD(TAG,
"Framework: ESP-IDF");
205 device_info +=
"ESP-IDF";
207 ESP_LOGW(TAG,
"Framework: UNKNOWN");
208 device_info +=
"UNKNOWN";
211 ESP_LOGD(TAG,
"ESP-IDF Version: %s", esp_get_idf_version());
212 device_info +=
"|ESP-IDF: ";
213 device_info += esp_get_idf_version();
216 ESP_LOGD(TAG,
"EFuse MAC: %s", mac.c_str());
217 device_info +=
"|EFuse MAC: ";
220 device_info +=
"|Reset: ";
224 device_info +=
"|Wakeup: ";
225 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.
const Component * component
Providing packet encoding functions for exchanging data with a remote host.
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
uint32_t fnv1_hash(const char *str)
Calculate a FNV-1 hash of str.
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 str_sprintf(const char *fmt,...)
Application App
Global storage of Application pointer - only one Application can exist.