Skip to content

Commit

Permalink
Handle MCU->WiFi handshake properly so the OEM module can be disabled
Browse files Browse the repository at this point in the history
mill1000 committed Nov 7, 2023
1 parent 35c06e2 commit 622a9a3
Showing 2 changed files with 82 additions and 12 deletions.
81 changes: 69 additions & 12 deletions components/winix_c545/winix_c545.cpp
Original file line number Diff line number Diff line change
@@ -99,6 +99,19 @@ void WinixC545Component::parse_aws_sentence_(const char *sentence) {

bool valid = false;
switch (api_code) {
case 102: // Wifi disconnect
{
// Acknowledge the message
this->write_sentence_("AWS_SEND:OK");
this->write_sentence_("AWS_IND:SEND OK");
this->write_sentence_("AWS_IND:DISCONNECTED");

// Reset handshake state
this->handshake_state_ = HandshakeState::Reset;
this->last_handshake_event_ = millis();
return;
}

case 210: // Overall device state
case 220: // Sensor update
{
@@ -129,7 +142,7 @@ void WinixC545Component::parse_aws_sentence_(const char *sentence) {

// Update internal state
this->update_state_(states);

valid = true;
break;
}

@@ -150,6 +163,10 @@ void WinixC545Component::parse_aws_sentence_(const char *sentence) {
// Acknowledge the message
this->write_sentence_("AWS_SEND:OK");
this->write_sentence_("AWS_IND:SEND OK");

// If a valid packet was received, force connected state
this->handshake_state_ = HandshakeState::Connected;
this->last_handshake_event_ = millis();
}
}

@@ -180,16 +197,21 @@ void WinixC545Component::parse_sentence_(const 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");
return;
}

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

ESP_LOGI(TAG, "MIB:OK");
// 7595 is version of OEM wifi module
this->write_sentence_("MIB:OK 7595");
this->write_sentence_("MIB:OK 7595"); // 7595 is version of OEM wifi module
return;
}

@@ -238,9 +260,50 @@ bool WinixC545Component::readline_(char data, char *buffer, int max_length) {
return false;
}

void WinixC545Component::update_handshake_state_() {
switch (this->handshake_state_) {
case HandshakeState::Connected:
// Protocol is connected, all good
return;

case HandshakeState::Reset:
case HandshakeState::DeviceReady: {
// If there was activity recently assume the handshake is in progress
if ((millis() - this->last_handshake_event_) < 10000)
return;

// Indicate device is ready to start handshake with MCU
this->handshake_state_ = HandshakeState::DeviceReady;
this->last_handshake_event_ = millis();

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

case HandshakeState::MIB: {
this->handshake_state_ = HandshakeState::Connected;
this->last_handshake_event_ = millis();

// Some subset of these may be needed
// *ICT*ASSOCIATED:0
// *ICT*IPALLOCATED:10.100.1.250 255.255.255.0 10.100.1.1 10.100.1.6
// *ICT*AWS_IND:MQTT OK
// *ICT*AWS_IND:SUBSCRIBE OK
// *ICT*AWS_IND:CONNECT OK
ESP_LOGI(TAG, "CONNECTED");
this->write_sentence_("AWS_IND:CONNECT OK");
break;
}
}
}

void WinixC545Component::loop() {
static char buffer[MAX_LINE_LENGTH];

// Handle protocol handshake state
this->update_handshake_state_();

if (this->available() < RX_PREFIX.size()) return;

while (this->available() > 0) {
@@ -285,15 +348,9 @@ void WinixC545Component::setup() {
// this->write_state_();
// }

// Indicate device is ready
// TODO base on wifi state?
this->write_sentence_("DEVICEREADY");
// Some subset of these may be needed too
// *ICT*ASSOCIATED:0
// *ICT*IPALLOCATED:10.100.1.250 255.255.255.0 10.100.1.1 10.100.1.6
// *ICT*AWS_IND:MQTT OK
// *ICT*AWS_IND:SUBSCRIBE OK
// *ICT*AWS_IND:CONNECT OK
// Reset handshake
this->handshake_state_ = HandshakeState::Reset;
this->last_handshake_event_ = millis();
}

void WinixC545Fan::dump_config() {
13 changes: 13 additions & 0 deletions components/winix_c545/winix_c545.h
Original file line number Diff line number Diff line change
@@ -72,6 +72,19 @@ class WinixC545Component : public uart::UARTDevice, public Component {
const std::string TX_PREFIX{"*ICT*"};

static constexpr uint32_t MAX_LINE_LENGTH = 255;

enum class HandshakeState {
Reset,
DeviceReady,
MIB,
McuReady,
Connected,
};

HandshakeState handshake_state_{HandshakeState::Reset};
uint32_t last_handshake_event_ = 0;

void update_handshake_state_();
bool readline_(char, char *, int);
void parse_sentence_(const char *);
void parse_aws_sentence_(const char *);

0 comments on commit 622a9a3

Please sign in to comment.