fix new state machine

This commit is contained in:
Chelios 2024-10-10 22:48:47 +03:00
parent c5580bbbdb
commit cd82189d38
2 changed files with 49 additions and 33 deletions

View file

@ -112,32 +112,31 @@ void ModemComponent::loop() {
this->dce_init(); this->dce_init();
break; break;
case ModemComponentState::TURNING_ON_PWRKEY: case ModemComponentState::TURNING_ON_PWRKEY:
if (time_check_pwrkey + SYNCHRONIZATION_CHECK_PERIOD < now) { if (this->dce->sync() == esp_modem::command_result::OK) {
time_check_pwrkey = now; ESP_LOGD(TAG, "sync OK TURNING_ON_PWRKEY");
if (this->dce->sync() == esp_modem::command_result::OK) { this->turn_off_pwrkey();
ESP_LOGD(TAG, "sync OK TURNING_ON_PWRKEY"); this->state_ = ModemComponentState::REGISTRATION_IN_NETWORK;
this->turn_off_pwrkey(); time_change_state = millis();
this->state_ = ModemComponentState::REGISTRATION_IN_NETWORK; } else {
} else { ESP_LOGD(TAG, "Wait sync TURNING_ON_PWRKEY");
ESP_LOGD(TAG, "Wait sync TURNING_ON_PWRKEY");
}
} }
break; break;
case ModemComponentState::REGISTRATION_IN_NETWORK: case ModemComponentState::REGISTRATION_IN_NETWORK:
if (time_check_rssi + TIME_CHECK_REGISTRATION_IN_NETWORK < now) { if (get_rssi()) {
time_check_rssi = now; ESP_LOGD(TAG, "Starting modem connection");
if (get_rssi()) { ESP_LOGD(TAG, "SIgnal quality: rssi=%d", get_rssi());
ESP_LOGD(TAG, "Starting modem connection"); this->state_ = ModemComponentState::CONNECTING;
ESP_LOGD(TAG, "SIgnal quality: rssi=%d", get_rssi()); time_change_state = millis();
this->state_ = ModemComponentState::CONNECTING; this->dce->set_data();
this->dce->set_data(); // this->start_connect_();
// this->start_connect_(); } else {
} else { ESP_LOGD(TAG, "Wait RSSI");
ESP_LOGD(TAG, "Wait RSSI");
}
} }
break; break;
case ModemComponentState::CONNECTING: case ModemComponentState::CONNECTING:
ESP_LOGD(TAG, "Wait WAN");
break; break;
case ModemComponentState::CONNECTED: case ModemComponentState::CONNECTED:
if (time_info_print < now) { if (time_info_print < now) {
@ -160,6 +159,7 @@ void ModemComponent::loop() {
ESP_LOGD(TAG, "sync OK TURNING_ON_RESET"); ESP_LOGD(TAG, "sync OK TURNING_ON_RESET");
this->turn_off_reset(); this->turn_off_reset();
this->state_ = ModemComponentState::REGISTRATION_IN_NETWORK; this->state_ = ModemComponentState::REGISTRATION_IN_NETWORK;
time_change_state = millis();
} else { } else {
ESP_LOGD(TAG, "Wait sync TURNING_ON_RESET"); ESP_LOGD(TAG, "Wait sync TURNING_ON_RESET");
} }
@ -202,11 +202,19 @@ void ModemComponent::dce_init() {
bool ModemComponent::check_modem_component_state_timings() { bool ModemComponent::check_modem_component_state_timings() {
const int now = millis(); const int now = millis();
ModemComponentStateTiming timing = this->modemComponentStateTimingMap[this->state_]; ModemComponentStateTiming timing = this->modemComponentStateTimingMap[this->state_];
if (last_pull_time + timing.poll_period > now) { if (!timing.poll_period){
return true;
}
if ((last_pull_time + timing.poll_period) < now) {
//ESP_LOGD(TAG, "it's time for pull");//%d %d", timing.poll_period, timing.time_limit);
last_pull_time = now; last_pull_time = now;
return true; return true;
} else if (time_change_state + timing.time_limit < now) { }
return true; if (!timing.time_limit){
if ((time_change_state + timing.time_limit) < now) {
// TODO: REBOOT return true;
return false;
}
} }
return false; return false;
} }
@ -217,6 +225,7 @@ void ModemComponent::turn_on_modem() {
time_turn_on_modem = millis(); time_turn_on_modem = millis();
ESP_LOGD(TAG, "Modem turn on"); ESP_LOGD(TAG, "Modem turn on");
this->state_ = ModemComponentState::TURNING_ON_POWER; this->state_ = ModemComponentState::TURNING_ON_POWER;
time_change_state = millis();
} else { } else {
ESP_LOGD(TAG, "Can't turn on modem power pin because it is not configured, go to turn on pwrkey"); ESP_LOGD(TAG, "Can't turn on modem power pin because it is not configured, go to turn on pwrkey");
this->turn_on_pwrkey(); this->turn_on_pwrkey();
@ -229,6 +238,7 @@ void ModemComponent::turn_off_modem() {
time_turn_off_modem = millis(); time_turn_off_modem = millis();
ESP_LOGD(TAG, "modem turn off"); ESP_LOGD(TAG, "modem turn off");
global_modem_component->state_ = ModemComponentState::STOPPED; global_modem_component->state_ = ModemComponentState::STOPPED;
time_change_state = millis();
// wait no more than 1.9 sec for signs of life to appear // wait no more than 1.9 sec for signs of life to appear
} }
@ -237,6 +247,7 @@ void ModemComponent::turn_on_pwrkey() {
this->pwrkey_pin_->digital_write(false); this->pwrkey_pin_->digital_write(false);
ESP_LOGD(TAG, "pwrkey turn on"); ESP_LOGD(TAG, "pwrkey turn on");
this->state_ = ModemComponentState::TURNING_ON_PWRKEY; this->state_ = ModemComponentState::TURNING_ON_PWRKEY;
time_change_state = millis();
} else { } else {
ESP_LOGD(TAG, "Can't turn on pwrkey pin because it is not configured, go to reset modem"); ESP_LOGD(TAG, "Can't turn on pwrkey pin because it is not configured, go to reset modem");
this->turn_on_reset(); this->turn_on_reset();
@ -323,6 +334,7 @@ void ModemComponent::got_ip_event_handler(void *arg, esp_event_base_t event_base
if (event_id == IP_EVENT_PPP_GOT_IP) { if (event_id == IP_EVENT_PPP_GOT_IP) {
global_modem_component->connected_ = true; global_modem_component->connected_ = true;
global_modem_component->state_ = ModemComponentState::CONNECTED; global_modem_component->state_ = ModemComponentState::CONNECTED;
time_change_state = millis();
esp_netif_dns_info_t dns_info; esp_netif_dns_info_t dns_info;
ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data; ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
@ -373,7 +385,10 @@ void ModemComponent::start_connect_() {
// this->status_set_warning(); // this->status_set_warning();
} }
bool ModemComponent::is_connected() { return this->state_ == ModemComponentState::CONNECTED; } bool ModemComponent::is_connected() {
time_change_state = millis();
return this->state_ == ModemComponentState::CONNECTED;
}
void ModemComponent::set_power_pin(InternalGPIOPin *power_pin) { this->power_pin_ = power_pin; } void ModemComponent::set_power_pin(InternalGPIOPin *power_pin) { this->power_pin_ = power_pin; }
void ModemComponent::set_pwrkey_pin(InternalGPIOPin *pwrkey_pin) { this->pwrkey_pin_ = pwrkey_pin; } void ModemComponent::set_pwrkey_pin(InternalGPIOPin *pwrkey_pin) { this->pwrkey_pin_ = pwrkey_pin; }
void ModemComponent::set_type(ModemType type) { this->type_ = type; } void ModemComponent::set_type(ModemType type) { this->type_ = type; }

View file

@ -37,10 +37,10 @@ enum class ModemComponentState {
}; };
struct ModemComponentStateTiming { struct ModemComponentStateTiming {
int time_limit;
int poll_period; int poll_period;
ModemComponentStateTiming() : time_limit(0), poll_period(0) {} int time_limit;
ModemComponentStateTiming(int time_limit, int poll_period) : time_limit(time_limit), poll_period(poll_period) {} ModemComponentStateTiming() : poll_period(0), time_limit(0) {}
ModemComponentStateTiming(int poll_period, int time_limit) : poll_period(poll_period), time_limit(time_limit) {}
}; };
class ModemComponent : public Component { class ModemComponent : public Component {
@ -74,13 +74,13 @@ class ModemComponent : public Component {
protected: protected:
std::map<ModemComponentState, ModemComponentStateTiming> modemComponentStateTimingMap = { std::map<ModemComponentState, ModemComponentStateTiming> modemComponentStateTimingMap = {
{ModemComponentState::STOPPED, ModemComponentStateTiming(0, 0)}, {ModemComponentState::STOPPED, ModemComponentStateTiming(0, 0)},
{ModemComponentState::TURNING_ON_POWER, ModemComponentStateTiming(1000, 10000)}, {ModemComponentState::TURNING_ON_POWER, ModemComponentStateTiming(0, 0)},
{ModemComponentState::TURNING_ON_PWRKEY, ModemComponentStateTiming(1000, 5000)}, {ModemComponentState::TURNING_ON_PWRKEY, ModemComponentStateTiming(1000, 20000)},
{ModemComponentState::REGISTRATION_IN_NETWORK, ModemComponentStateTiming(1000, 4000)}, {ModemComponentState::REGISTRATION_IN_NETWORK, ModemComponentStateTiming(1000, 20000)},
{ModemComponentState::CONNECTING, ModemComponentStateTiming(1000, 4000)}, {ModemComponentState::CONNECTING, ModemComponentStateTiming(1000, 0)},
{ModemComponentState::CONNECTED, ModemComponentStateTiming(0, 0)}, {ModemComponentState::CONNECTED, ModemComponentStateTiming(0, 0)},
{ModemComponentState::TURNING_ON_RESET, ModemComponentStateTiming(1000, 6000)}, {ModemComponentState::TURNING_ON_RESET, ModemComponentStateTiming(2000, 0)},
{ModemComponentState::TURNING_OFF_POWER, ModemComponentStateTiming(0, 0)}, {ModemComponentState::TURNING_OFF_POWER, ModemComponentStateTiming(2000, 0)},
}; };
static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int event_id, void *event_data); static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int event_id, void *event_data);
@ -98,6 +98,7 @@ class ModemComponent : public Component {
void turn_off_reset(); void turn_off_reset();
int get_rssi(); int get_rssi();
int get_modem_voltage(); int get_modem_voltage();
const char *get_state();
std::shared_ptr<esp_modem::DTE> dte{nullptr}; std::shared_ptr<esp_modem::DTE> dte{nullptr};
std::unique_ptr<esp_modem::DCE> dce{nullptr}; std::unique_ptr<esp_modem::DCE> dce{nullptr};