Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the AP handshake so fresh devices can be used #10

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions components/winix_c545/winix_c545.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,20 @@ void WinixC545Component::parse_sentence_(char *sentence) {

// Handle MCU_READY message
if (strncmp(sentence, "MCU_READY", strlen("MCU_READY")) == 0) {
this->handshake_state_ = HandshakeState::McuReady;
this->last_handshake_event_ = millis();

ESP_LOGI(TAG, "MCU_READY");
this->write_sentence_("MCU_READY:OK");

if (this->handshake_state_ == HandshakeState::ApDeviceReady) {
this->handshake_state_ = HandshakeState::ApStart;
this->last_handshake_event_ = millis();

ESP_LOGI(TAG, "AP START");
this->write_sentence_("AP_STARTED:OK");
} else {
this->handshake_state_ = HandshakeState::McuReady;
this->last_handshake_event_ = millis();
}

return;
}

Expand All @@ -298,6 +307,9 @@ void WinixC545Component::parse_sentence_(char *sentence) {

// Handle SMODE messages
if (strncmp(sentence, "SMODE", strlen("SMODE")) == 0) {
this->handshake_state_ = HandshakeState::ApReboot;
this->last_handshake_event_ = millis();

ESP_LOGI(TAG, "SMODE:OK");
this->write_sentence_("SMODE:OK");
return;
Expand Down Expand Up @@ -368,6 +380,41 @@ void WinixC545Component::update_handshake_state_() {
this->write_sentence_("AWS_IND:CONNECT OK");
break;
}

case HandshakeState::ApReboot: {
// AP mode requested, pretend to reboot into AP
this->handshake_state_ = HandshakeState::ApDeviceReady;
this->last_handshake_event_ = millis();

ESP_LOGI(TAG, "AP DEVICEREADY");
this->write_sentence_("DEVICEREADY");
break;
}

case HandshakeState::ApStart: {
// Exit AP mode
this->handshake_state_ = HandshakeState::ApStop;
this->last_handshake_event_ = millis();

ESP_LOGI(TAG, "AP STOP");
this->write_sentence_("AP_STOPED:OK");
this->write_sentence_("ASSOCIATED:0");
// TODO could get real network info but I don't think it matters
this->write_sentence_("IPALLOCATED:10.100.1.250 255.255.255.0 10.100.1.1 10.100.1.6");
this->write_sentence_("AWS_IND:CONNECT OK");
break;
}

default: {
// If in an intermediate state and no activity occurs for a while reset the state machine
if ((millis() - this->last_handshake_event_) < 10000)
return;

// Reset handshake state
ESP_LOGW(TAG, "Handshake stalled in state %d. Restarting.", this->handshake_state_);
this->handshake_state_ = HandshakeState::Reset;
break;
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion components/winix_c545/winix_c545.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,13 @@ class WinixC545Component : public uart::UARTDevice, public Component {
enum class HandshakeState {
Reset,
DeviceReady,
MIB,
McuReady,
MIB,
Connected,
ApReboot,
ApDeviceReady,
ApStart,
ApStop,
};

HandshakeState handshake_state_{HandshakeState::Reset};
Expand Down