Skip to content
New issue

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

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

Already on GitHub? # to your account

Improve device and thing state machine message handling and fix infinite disconnetct loop #490

Merged
merged 2 commits into from
Jul 22, 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
24 changes: 13 additions & 11 deletions src/ArduinoIoTCloudDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ void ArduinoCloudDevice::begin() {
}

void ArduinoCloudDevice::update() {
handleMessage(nullptr);
}

int ArduinoCloudDevice::connected() {
return _state != State::Disconnected ? 1 : 0;
}

void ArduinoCloudDevice::handleMessage(Message *m) {
_command = UnknownCmdId;
if (m != nullptr) {
_command = m->id;
}

/* Run through the state machine. */
State nextState = _state;
switch (_state) {
Expand Down Expand Up @@ -80,17 +93,6 @@ void ArduinoCloudDevice::update() {
_state = nextState;
}

int ArduinoCloudDevice::connected() {
return _state != State::Disconnected ? 1 : 0;
}

void ArduinoCloudDevice::handleMessage(Message *m) {
_command = UnknownCmdId;
if (m != nullptr) {
_command = m->id;
}
}

ArduinoCloudDevice::State ArduinoCloudDevice::handleInit() {
/* Reset attempt struct for the nex retry after disconnection */
_attachAttempt.begin(AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms,
Expand Down
32 changes: 17 additions & 15 deletions src/ArduinoIoTCloudThing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ void ArduinoCloudThing::begin() {
}

void ArduinoCloudThing::update() {
handleMessage(nullptr);
}

int ArduinoCloudThing::connected() {
return _state > State::Disconnect ? 1 : 0;
}

void ArduinoCloudThing::handleMessage(Message* m) {
_command = UnknownCmdId;
if (m != nullptr) {
_command = m->id;
if (_command == TimezoneCommandDownId) {
_utcOffset = reinterpret_cast<TimezoneCommandDown*>(m)->params.offset;
_utcOffsetExpireTime = reinterpret_cast<TimezoneCommandDown*>(m)->params.until;
}
}

/* Run through the state machine. */
State nextState = _state;
switch (_state) {
Expand Down Expand Up @@ -95,21 +112,6 @@ void ArduinoCloudThing::update() {
_state = nextState;
}

int ArduinoCloudThing::connected() {
return _state > State::Disconnect ? 1 : 0;
}

void ArduinoCloudThing::handleMessage(Message* m) {
_command = UnknownCmdId;
if (m != nullptr) {
_command = m->id;
if (_command == TimezoneCommandDownId) {
_utcOffset = reinterpret_cast<TimezoneCommandDown*>(m)->params.offset;
_utcOffsetExpireTime = reinterpret_cast<TimezoneCommandDown*>(m)->params.until;
}
}
}

ArduinoCloudThing::State ArduinoCloudThing::handleInit() {
_syncAttempt.begin(AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms);
return State::RequestLastValues;
Expand Down
Loading