mirror of
https://github.com/esphome/esphome.git
synced 2025-04-15 23:30:28 +01:00
code clean
This commit is contained in:
parent
4b0ae086dc
commit
1b9c9e9ccb
@ -148,7 +148,7 @@ void ModemComponent::setup() {
|
|||||||
|
|
||||||
void ModemComponent::modem_lazy_init_() {
|
void ModemComponent::modem_lazy_init_() {
|
||||||
// destroy previous dte/dce, and recreate them.
|
// destroy previous dte/dce, and recreate them.
|
||||||
// no communication is done with the modem. s
|
// no communication is done with the modem.
|
||||||
|
|
||||||
this->dte_.reset();
|
this->dte_.reset();
|
||||||
this->dce.reset();
|
this->dce.reset();
|
||||||
@ -201,7 +201,6 @@ void ModemComponent::modem_lazy_init_() {
|
|||||||
bool ModemComponent::modem_sync_() {
|
bool ModemComponent::modem_sync_() {
|
||||||
// force command mode, check sim, and send init_at commands
|
// force command mode, check sim, and send init_at commands
|
||||||
// close cmux/data if needed, and may reboot the modem.
|
// close cmux/data if needed, and may reboot the modem.
|
||||||
// This is needed at boot.
|
|
||||||
if (!this->watchdog_)
|
if (!this->watchdog_)
|
||||||
this->watchdog_ = std::make_shared<watchdog::WatchdogManager>(60000);
|
this->watchdog_ = std::make_shared<watchdog::WatchdogManager>(60000);
|
||||||
|
|
||||||
@ -215,13 +214,13 @@ bool ModemComponent::modem_sync_() {
|
|||||||
|
|
||||||
std::string result;
|
std::string result;
|
||||||
|
|
||||||
auto command_mode_ = [this]() -> bool {
|
auto command_mode = [this]() -> bool {
|
||||||
ESP_LOGVV(TAG, "trying command mode");
|
ESP_LOGVV(TAG, "trying command mode");
|
||||||
this->dce->set_mode(modem_mode::UNDEF);
|
this->dce->set_mode(modem_mode::UNDEF);
|
||||||
return this->dce->set_mode(modem_mode::COMMAND_MODE) && this->modem_ready();
|
return this->dce->set_mode(modem_mode::COMMAND_MODE) && this->modem_ready();
|
||||||
};
|
};
|
||||||
|
|
||||||
auto cmux_command_mode_ = [this]() -> bool {
|
auto cmux_command_mode = [this]() -> bool {
|
||||||
ESP_LOGVV(TAG, "trying cmux command mode");
|
ESP_LOGVV(TAG, "trying cmux command mode");
|
||||||
return this->dce->set_mode(modem_mode::CMUX_MANUAL_MODE) &&
|
return this->dce->set_mode(modem_mode::CMUX_MANUAL_MODE) &&
|
||||||
this->dce->set_mode(modem_mode::CMUX_MANUAL_COMMAND) && this->modem_ready();
|
this->dce->set_mode(modem_mode::CMUX_MANUAL_COMMAND) && this->modem_ready();
|
||||||
@ -230,9 +229,9 @@ bool ModemComponent::modem_sync_() {
|
|||||||
// The cmux state is supposed to be the same before the reboot. But if it has changed (new firwmare), we will try
|
// The cmux state is supposed to be the same before the reboot. But if it has changed (new firwmare), we will try
|
||||||
// to fallback to inverted cmux state.
|
// to fallback to inverted cmux state.
|
||||||
if (this->cmux_) {
|
if (this->cmux_) {
|
||||||
status = cmux_command_mode_() || (command_mode_() && cmux_command_mode_());
|
status = cmux_command_mode() || (command_mode() && cmux_command_mode());
|
||||||
} else {
|
} else {
|
||||||
status = command_mode_() || (cmux_command_mode_() && command_mode_());
|
status = command_mode() || (cmux_command_mode() && command_mode());
|
||||||
}
|
}
|
||||||
|
|
||||||
elapsed_ms = millis() - start_ms;
|
elapsed_ms = millis() - start_ms;
|
||||||
@ -481,7 +480,7 @@ void ModemComponent::loop() {
|
|||||||
if (!this->watchdog_)
|
if (!this->watchdog_)
|
||||||
this->watchdog_ = std::make_shared<watchdog::WatchdogManager>(60000);
|
this->watchdog_ = std::make_shared<watchdog::WatchdogManager>(60000);
|
||||||
// want to start connect, make sure the modem is ready
|
// want to start connect, make sure the modem is ready
|
||||||
if (!this->modem_ready() && !this->modem_sync_()) {
|
if (!this->modem_sync_()) {
|
||||||
ESP_LOGE(TAG, "Modem not responding");
|
ESP_LOGE(TAG, "Modem not responding");
|
||||||
this->state_ = ModemComponentState::NOT_RESPONDING;
|
this->state_ = ModemComponentState::NOT_RESPONDING;
|
||||||
break;
|
break;
|
||||||
@ -490,7 +489,7 @@ void ModemComponent::loop() {
|
|||||||
network_attach_retry = 10;
|
network_attach_retry = 10;
|
||||||
if (this->start_ppp_()) {
|
if (this->start_ppp_()) {
|
||||||
connecting = true;
|
connecting = true;
|
||||||
next_loop_millis = millis() + 1000; // delay for next loop
|
next_loop_millis = millis() + 2000; // delay for next loop
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGD(TAG, "Waiting for the modem to be attached to a network (left retries: %" PRIu8 ")",
|
ESP_LOGD(TAG, "Waiting for the modem to be attached to a network (left retries: %" PRIu8 ")",
|
||||||
@ -501,8 +500,7 @@ void ModemComponent::loop() {
|
|||||||
if (this->power_pin_) {
|
if (this->power_pin_) {
|
||||||
this->poweroff_();
|
this->poweroff_();
|
||||||
} else {
|
} else {
|
||||||
// fatal error ?
|
this->state_ = ModemComponentState::NOT_RESPONDING;
|
||||||
this->start_ = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
next_loop_millis = millis() + 1000; // delay to retry
|
next_loop_millis = millis() + 1000; // delay to retry
|
||||||
@ -533,10 +531,7 @@ void ModemComponent::loop() {
|
|||||||
ESP_LOGD(TAG, "Disconnecting...");
|
ESP_LOGD(TAG, "Disconnecting...");
|
||||||
this->stop_ppp_();
|
this->stop_ppp_();
|
||||||
delay(200); // NOLINT
|
delay(200); // NOLINT
|
||||||
ESP_LOGD(TAG, "Hanging up connection after %.1fmin", float(this->connect_begin_) / (1000 * 60));
|
ESP_LOGD(TAG, "Disconnected after %.1fmin", float(this->connect_begin_) / (1000 * 60));
|
||||||
// ESPMODEM_ERROR_CHECK(this->dce->hang_up(),
|
|
||||||
// "Unable to hang up modem. Trying to continue anyway."); // FIXME: needed ?
|
|
||||||
// this->dump_connect_params_();
|
|
||||||
} else {
|
} else {
|
||||||
// disconnecting
|
// disconnecting
|
||||||
// Waiting for IP_EVENT_PPP_LOST_IP.
|
// Waiting for IP_EVENT_PPP_LOST_IP.
|
||||||
|
@ -38,12 +38,14 @@ enum class ModemComponentState {
|
|||||||
DISABLED,
|
DISABLED,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#ifdef USE_MODEM_POWER
|
||||||
enum class ModemPowerState {
|
enum class ModemPowerState {
|
||||||
TON,
|
TON,
|
||||||
TONUART,
|
TONUART,
|
||||||
TOFF,
|
TOFF,
|
||||||
TOFFUART,
|
TOFFUART,
|
||||||
};
|
};
|
||||||
|
#endif // USE_MODEM_POWER
|
||||||
|
|
||||||
class ModemComponent : public Component {
|
class ModemComponent : public Component {
|
||||||
public:
|
public:
|
||||||
@ -118,12 +120,14 @@ class ModemComponent : public Component {
|
|||||||
std::string use_address_;
|
std::string use_address_;
|
||||||
// timeout for AT commands
|
// timeout for AT commands
|
||||||
uint32_t command_delay_ = 500;
|
uint32_t command_delay_ = 500;
|
||||||
// Will be true when power transitionning
|
|
||||||
bool power_transition_{false};
|
|
||||||
// guess power state
|
// guess power state
|
||||||
bool powered_on_{false};
|
bool powered_on_{false};
|
||||||
|
#ifdef USE_MODEM_POWER
|
||||||
|
// Will be true when power transitionning
|
||||||
|
bool power_transition_{false};
|
||||||
// states for triggering on/off signals
|
// states for triggering on/off signals
|
||||||
ModemPowerState power_state_{ModemPowerState::TOFFUART};
|
ModemPowerState power_state_{ModemPowerState::TOFFUART};
|
||||||
|
#endif // USE_MODEM_POWER
|
||||||
// separate handler for `on_not_responding` (we want to know when it's ended)
|
// separate handler for `on_not_responding` (we want to know when it's ended)
|
||||||
Trigger<> *not_responding_cb_{nullptr};
|
Trigger<> *not_responding_cb_{nullptr};
|
||||||
CallbackManager<void(ModemComponentState)> on_state_callback_;
|
CallbackManager<void(ModemComponentState)> on_state_callback_;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user