17static const char *csi_capture_profile_name(::espectre::CsiCapturePolicy profile) {
19 case ::espectre::CsiCapturePolicy::LLTF:
20 return LOG_STR_LITERAL(
"lltf");
21 case ::espectre::CsiCapturePolicy::HT_VHT:
22 return LOG_STR_LITERAL(
"ht_vht");
24 return LOG_STR_LITERAL(
"auto");
29void ESPectreComponent::request_traffic_generator_mode(
const char *name) {
30 this->pending_traffic_mode_ = ::espectre::parse_traffic_generator_mode(name);
33void ESPectreComponent::request_detection_algorithm(
const char *name) {
34 this->pending_detection_algorithm_ = ::espectre::parse_detection_algorithm(name);
37void ESPectreComponent::request_csi_sample_spacing(
const char *name) {
38 this->pending_csi_sample_spacing_ = ::espectre::parse_csi_sample_spacing(name);
42#ifdef USE_ESPECTRE_TARGET_MAC_TEXT
43static bool parse_target_mac(
const std::string &
value,
TargetMac &mac) {
44 if (
value ==
"auto") {
48 if (
value.size() != MAC_ADDRESS_PRETTY_BUFFER_SIZE - 1)
50 for (
size_t i = 0; i < mac.size(); i++) {
51 if ((i != 0 &&
value[i * 3 - 1] !=
':') ||
parse_hex(
value.data() + i * 3, 2, &mac[i], 1) != 2)
54 return mac !=
TargetMac{} && (mac[0] & 1) == 0;
57static const char *format_target_mac(
const TargetMac &mac,
char (&
buffer)[MAC_ADDRESS_PRETTY_BUFFER_SIZE]) {
61void ESPectreComponent::request_traffic_generator_target_mac(
const std::string &target) {
62 if (!parse_target_mac(target, this->pending_target_mac_)) {
64 this->pending_target_mac_[0] = 0xff;
65 ESP_LOGW(TAG,
"Invalid target MAC");
67 this->target_mac_pending_ =
true;
70void ESPectreComponent::restore_target_mac_() {
71 if (this->target_mac_text_ ==
nullptr)
74 fnv1a_hash(this->runtime_.config().traffic_generator_target_mac));
76 char buffer[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
77 if (this->target_mac_pref_.
load(&saved))
78 this->runtime_.set_traffic_generator_target_mac(format_target_mac(saved,
buffer));
82static int log_level(::espectre::LogLevel level) {
84 case ::espectre::LogLevel::ERROR:
85 return ESPHOME_LOG_LEVEL_ERROR;
86 case ::espectre::LogLevel::WARNING:
87 return ESPHOME_LOG_LEVEL_WARN;
88 case ::espectre::LogLevel::INFO:
89 return ESPHOME_LOG_LEVEL_INFO;
90 case ::espectre::LogLevel::DEBUG:
91 return ESPHOME_LOG_LEVEL_DEBUG;
93 return ESPHOME_LOG_LEVEL_VERBOSE;
99 this->runtime_.config().wifi_scan_results_managed_externally =
true;
100 this->restore_controls_();
101 this->start_runtime_();
104void ESPectreComponent::start_runtime_() {
105#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_ROAMING_SUPPRESSION)
110 this->roaming_suppressed_ =
true;
113 ::espectre::set_log_sink({
115 .enabled = [](
void *, ::espectre::LogLevel level,
const char *) {
return log_level(level) <= ESPHOME_LOG_LEVEL; },
116 .write = [](
void *, ::espectre::LogLevel level,
const char *
tag,
int line,
const char *
format,
119 if (!this->runtime_.setup(
this)) {
120 ESP_LOGE(TAG,
"Runtime setup failed");
122 this->schedule_restart_();
125 this->running_ =
true;
128 this->publish_traffic_mode_();
130 this->publish_controls_();
133void ESPectreComponent::schedule_restart_() {
135 this->runtime_fault_ =
false;
137 ESP_LOGW(TAG,
"Restarting the runtime in %" PRIu32
" s", RESTART_DELAY_MS / 1000);
138 this->restart_pending_ =
true;
144static_assert(
static_cast<uint8_t
>(::espectre::TrafficGeneratorMode::PING) == 0 &&
145 static_cast<uint8_t
>(::espectre::TrafficGeneratorMode::DNS) == 1 &&
146 static_cast<uint8_t
>(::espectre::TrafficGeneratorMode::DNS_TCP) == 2 &&
147 static_cast<uint8_t
>(::espectre::TrafficGeneratorMode::WIFI_RAW) == 3 &&
148 static_cast<uint8_t
>(::espectre::TrafficGeneratorMode::EXTERNAL_HOST) == 4,
149 "Saved traffic generator modes depend on these values");
150static_assert(
static_cast<uint8_t
>(::espectre::DetectionAlgorithm::LIGHTWEIGHT) == 0 &&
151 static_cast<uint8_t
>(::espectre::DetectionAlgorithm::HIGH_ACCURACY) == 1,
152 "Saved detection algorithms depend on these values");
153static_assert(
static_cast<uint8_t
>(::espectre::TemporalCsiSpacing::HALF_SLOT) == 0 &&
154 static_cast<uint8_t
>(::espectre::TemporalCsiSpacing::DISTINCT) == 1,
155 "Saved CSI sample spacings depend on these values");
157template<
typename T,
typename F>
158static void restore_select_choice(select::Select *select, ESPPreferenceObject &pref, T &
value, F accept) {
159 if (select ==
nullptr)
161 pref = select->make_entity_preference<uint8_t>(
static_cast<uint32_t>(
value) + 1);
163 if (pref.load(&saved) && accept(
static_cast<T
>(saved)))
164 value =
static_cast<T
>(saved);
169static bool apply_select_choice(::espectre::RuntimeFrontendController &runtime, optional<T> &pending,
170 ESPPreferenceObject &pref,
bool (::espectre::RuntimeFrontendController::*set)(T),
171 const char *(*name)(T)) {
172 if (!pending.has_value())
174 const T
value = *pending;
176 if ((runtime.*set)(
value)) {
177 const auto saved =
static_cast<uint8_t
>(
value);
180 ESP_LOGW(TAG,
"Option %s was rejected",
name(
value));
185void ESPectreComponent::apply_pending_traffic_mode_() {
186 if (apply_select_choice(this->runtime_, this->pending_traffic_mode_, this->traffic_mode_pref_,
187 &::espectre::RuntimeFrontendController::set_traffic_generator_mode,
188 ::espectre::traffic_generator_mode_name))
189 this->publish_traffic_mode_();
192void ESPectreComponent::publish_traffic_mode_() {
193 if (this->traffic_mode_select_ ==
nullptr)
195 this->traffic_mode_select_->publish_state(
196 ::espectre::traffic_generator_mode_name(this->runtime_.config().traffic_generator_mode));
201static void restore_motion_hits(number::Number *number, ESPPreferenceObject &pref, uint8_t &hits) {
202 if (number ==
nullptr)
204 pref = number->make_entity_preference<uint8_t>(hits);
206 if (pref.load(&saved) && saved >= ::espectre::RUNTIME_MOTION_HITS_MIN && saved <= ::espectre::RUNTIME_MOTION_HITS_MAX)
211void ESPectreComponent::restore_controls_() {
213 auto &config = this->runtime_.config();
215 restore_motion_hits(this->motion_on_hits_number_, this->motion_on_hits_pref_, config.motion_on_hits);
216 restore_motion_hits(this->motion_off_hits_number_, this->motion_off_hits_pref_, config.motion_off_hits);
219 restore_select_choice(this->traffic_mode_select_, this->traffic_mode_pref_, config.traffic_generator_mode,
220 [
this](::espectre::TrafficGeneratorMode
mode) {
221 return ::espectre::runtime_traffic_generator_mode_valid(mode) &&
222 this->traffic_mode_select_->has_option(::espectre::traffic_generator_mode_name(mode));
225 if (this->detection_algorithm_select_ !=
nullptr)
226 config.runtime_detector_selection_enabled =
true;
227 restore_select_choice(this->detection_algorithm_select_, this->detection_algorithm_pref_, config.detection_algorithm,
228 ::espectre::runtime_detection_algorithm_valid);
229 restore_select_choice(this->csi_sample_spacing_select_, this->csi_sample_spacing_pref_, config.csi_sample_spacing,
230 ::espectre::runtime_csi_sample_spacing_valid);
232#ifdef USE_ESPECTRE_TARGET_MAC_TEXT
233 this->restore_target_mac_();
237 if (this->sensing_switch_ !=
nullptr)
238 this->runtime_.set_services_armed(this->sensing_switch_->get_initial_state_with_restore_mode().value_or(
true));
242void ESPectreComponent::apply_pending_controls_() {
244 if (!std::isnan(this->pending_threshold_)) {
245 const float threshold = this->pending_threshold_;
246 this->pending_threshold_ = NAN;
247 if (!this->runtime_.set_threshold(threshold)) {
248 ESP_LOGW(TAG,
"Threshold %.2f was rejected", threshold);
251 PUBLISH_STATE(this->threshold_number_, this->runtime_.snapshot().threshold);
253 if (this->pending_motion_on_hits_ != 0 || this->pending_motion_off_hits_ != 0) {
254 const auto &config = this->runtime_.config();
255 const uint8_t on_hits = this->pending_motion_on_hits_ != 0 ? this->pending_motion_on_hits_ : config.motion_on_hits;
256 const uint8_t off_hits =
257 this->pending_motion_off_hits_ != 0 ? this->pending_motion_off_hits_ : config.motion_off_hits;
258 this->pending_motion_on_hits_ = 0;
259 this->pending_motion_off_hits_ = 0;
260 if (this->runtime_.set_motion_hits(on_hits, off_hits)) {
261 this->motion_on_hits_pref_.
save(&on_hits);
262 this->motion_off_hits_pref_.
save(&off_hits);
264 ESP_LOGW(TAG,
"Motion on/off hits %u/%u were rejected", on_hits, off_hits);
266 PUBLISH_STATE(this->motion_on_hits_number_, config.motion_on_hits);
267 PUBLISH_STATE(this->motion_off_hits_number_, config.motion_off_hits);
273 if (apply_select_choice(this->runtime_, this->pending_detection_algorithm_, this->detection_algorithm_pref_,
274 &::espectre::RuntimeFrontendController::set_detection_algorithm,
275 ::espectre::detection_algorithm_name)) {
276 PUBLISH_STATE(this->detection_algorithm_select_,
277 ::espectre::detection_algorithm_name(this->runtime_.config().detection_algorithm));
279 if (apply_select_choice(this->runtime_, this->pending_csi_sample_spacing_, this->csi_sample_spacing_pref_,
280 &::espectre::RuntimeFrontendController::set_csi_sample_spacing,
281 ::espectre::csi_sample_spacing_name)) {
282 PUBLISH_STATE(this->csi_sample_spacing_select_,
283 ::espectre::csi_sample_spacing_name(this->runtime_.config().csi_sample_spacing));
286#ifdef USE_ESPECTRE_TARGET_MAC_TEXT
287 if (this->target_mac_pending_) {
288 this->target_mac_pending_ =
false;
290 const TargetMac target = this->pending_target_mac_;
291 char buffer[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
292 const char *
value = (target[0] & 1) == 0 ? format_target_mac(target,
buffer) :
nullptr;
293 if (
value !=
nullptr && this->runtime_.set_traffic_generator_target_mac(
value)) {
294 this->target_mac_pref_.
save(&target);
297 if (
value !=
nullptr) {
298 ESP_LOGW(TAG,
"Target MAC '%s' was rejected",
value);
305 if (this->pending_sensing_.has_value()) {
306 this->runtime_.set_services_armed(*this->pending_sensing_);
307 this->pending_sensing_.reset();
308 PUBLISH_STATE(this->sensing_switch_, this->runtime_.services_armed());
313void ESPectreComponent::publish_controls_() {
315 PUBLISH_STATE(this->threshold_number_, this->runtime_.snapshot().threshold);
316 PUBLISH_STATE(this->motion_on_hits_number_, this->runtime_.config().motion_on_hits);
317 PUBLISH_STATE(this->motion_off_hits_number_, this->runtime_.config().motion_off_hits);
320 PUBLISH_STATE(this->detection_algorithm_select_,
321 ::espectre::detection_algorithm_name(this->runtime_.config().detection_algorithm));
322 PUBLISH_STATE(this->csi_sample_spacing_select_,
323 ::espectre::csi_sample_spacing_name(this->runtime_.config().csi_sample_spacing));
325#ifdef USE_ESPECTRE_TARGET_MAC_TEXT
327 const auto &
value = this->runtime_.config().traffic_generator_target_mac;
329 char buffer[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
330 PUBLISH_STATE(this->target_mac_text_,
331 parse_target_mac(
value, target) ? format_target_mac(target,
buffer) :
value.c_str());
334 PUBLISH_STATE(this->sensing_switch_, this->runtime_.services_armed());
338void ESPectreComponent::loop() {
339 if (this->runtime_fault_) {
341 this->schedule_restart_();
344 if (!this->running_) {
346 this->runtime_.loop();
347 if (this->restart_pending_ &&
349 this->restart_pending_ =
false;
350 this->start_runtime_();
354 if (this->recalibrate_pending_) {
355 this->recalibrate_pending_ =
false;
356 if (!this->runtime_.trigger_recalibration()) {
357 ESP_LOGW(TAG,
"Recalibration is not available");
361 this->apply_pending_traffic_mode_();
363 this->apply_pending_controls_();
364 this->runtime_.loop();
365 if (this->runtime_fault_)
369 const auto &snapshot = this->runtime_.snapshot();
372 if (this->threshold_number_ !=
nullptr && this->threshold_number_->state != snapshot.threshold)
373 this->threshold_number_->publish_state(snapshot.threshold);
375#ifdef USE_BINARY_SENSOR
376 if (this->calibrating_binary_sensor_ !=
nullptr &&
377 (!this->calibrating_published_ || this->calibrating_state_ != snapshot.calibrating)) {
378 this->calibrating_state_ = snapshot.calibrating;
379 this->calibrating_published_ =
true;
380 this->calibrating_binary_sensor_->publish_state(snapshot.calibrating);
383 if (!snapshot.ready_to_publish) {
385 this->invalidate_sensing_();
386 this->movement_pending_ =
false;
390#ifdef USE_BINARY_SENSOR
391 const bool motion = snapshot.motion_state == ::espectre::MotionState::MOTION;
392 if (this->motion_binary_sensor_ !=
nullptr && (!this->ready_ || this->motion_state_ != motion)) {
393 this->motion_state_ = motion;
394 this->motion_binary_sensor_->publish_state(motion);
398 if (this->movement_sensor_ !=
nullptr && (this->movement_pending_ || !this->ready_))
399 this->movement_sensor_->publish_state(snapshot.movement_metric);
401 this->movement_pending_ =
false;
405void ESPectreComponent::invalidate_sensing_() {
406#ifdef USE_BINARY_SENSOR
407 if (this->motion_binary_sensor_ !=
nullptr)
408 this->motion_binary_sensor_->invalidate_state();
411 PUBLISH_STATE(this->movement_sensor_, NAN);
413 this->ready_ =
false;
416void ESPectreComponent::on_calibration_finished(const ::espectre::RuntimeSnapshot &snapshot,
bool success) {
418 this->calibrated_ =
true;
420 ESP_LOGI(TAG,
"Calibration complete");
424 if (!this->calibrated_)
426 ESP_LOGW(TAG,
"Calibration failed; retaining the previous threshold");
429void ESPectreComponent::on_runtime_fault(
const char *
message) {
430 ESP_LOGE(TAG,
"Runtime fault: %s",
message);
431 this->runtime_fault_ =
true;
434void ESPectreComponent::stop_() {
435#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_ROAMING_SUPPRESSION)
440 this->roaming_suppressed_ =
false;
441 this->running_ =
false;
443 this->calibrated_ =
false;
444 this->calibrating_published_ =
false;
445 this->runtime_.shutdown();
446 ::espectre::clear_log_sink();
447 this->invalidate_sensing_();
448#ifdef USE_BINARY_SENSOR
449 if (this->calibrating_binary_sensor_ !=
nullptr)
450 this->calibrating_binary_sensor_->invalidate_state();
454void ESPectreComponent::on_shutdown() {
455 this->restart_pending_ =
false;
459void ESPectreComponent::dump_config() {
460 const auto &config = this->runtime_.config();
463 " Detection algorithm: %s\n"
464 " CSI capture profile: %s\n"
465 " Traffic generator mode: %s\n"
466 " Traffic generator target MAC: %s\n"
467 " CSI sample spacing: %s\n"
468 " Motion on/off hits: %u/%u\n"
469 " Window size: %" PRIu32
" ms\n"
470 " CSI target rate: %" PRIu32
" pps\n"
471 " Evaluation interval: %" PRIu32
" ms\n"
472 " Low-pass filter: %s (%.1f Hz)\n"
473 " Hampel filter: %s (window %u, threshold %.1f)",
474 ::espectre::detection_algorithm_name(config.detection_algorithm),
475 csi_capture_profile_name(config.csi_capture_policy),
476 ::espectre::traffic_generator_mode_name(config.traffic_generator_mode),
477 config.traffic_generator_target_mac.c_str(),
478 ::espectre::csi_sample_spacing_name(config.csi_sample_spacing), config.motion_on_hits,
479 config.motion_off_hits, config.window_size_ms, config.csi_target_pps, config.evaluation_interval_ms,
480 ONOFF(config.lowpass_enabled), config.lowpass_cutoff, ONOFF(config.hampel_enabled),
481 config.hampel_window, config.hampel_threshold);
482#ifdef USE_BINARY_SENSOR
483 LOG_BINARY_SENSOR(
" ",
"Motion", this->motion_binary_sensor_);
484 LOG_BINARY_SENSOR(
" ",
"Calibrating", this->calibrating_binary_sensor_);
487 LOG_SENSOR(
" ",
"Movement score", this->movement_sensor_);
490 LOG_NUMBER(
" ",
"Threshold", this->threshold_number_);
491 LOG_NUMBER(
" ",
"Motion on hits", this->motion_on_hits_number_);
492 LOG_NUMBER(
" ",
"Motion off hits", this->motion_off_hits_number_);
495 LOG_SELECT(
" ",
"Traffic generator mode", this->traffic_mode_select_);
496 LOG_SELECT(
" ",
"Detection algorithm", this->detection_algorithm_select_);
497 LOG_SELECT(
" ",
"CSI sample spacing", this->csi_sample_spacing_select_);
499#ifdef USE_ESPECTRE_TARGET_MAC_TEXT
500 LOG_TEXT(
" ",
"Traffic generator target MAC", this->target_mac_text_);
503 LOG_SWITCH(
" ",
"Sensing", this->sensing_switch_);