Skip to content

Commit 4c0d20e

Browse files
authored
Merge pull request #490 from pennam/dev-thi-loop-fix
Improve device and thing state machine message handling and fix infinite disconnetct loop
2 parents 8b6ff40 + 8196fbc commit 4c0d20e

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

src/ArduinoIoTCloudDevice.cpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ void ArduinoCloudDevice::begin() {
3838
}
3939

4040
void ArduinoCloudDevice::update() {
41+
handleMessage(nullptr);
42+
}
43+
44+
int ArduinoCloudDevice::connected() {
45+
return _state != State::Disconnected ? 1 : 0;
46+
}
47+
48+
void ArduinoCloudDevice::handleMessage(Message *m) {
49+
_command = UnknownCmdId;
50+
if (m != nullptr) {
51+
_command = m->id;
52+
}
53+
4154
/* Run through the state machine. */
4255
State nextState = _state;
4356
switch (_state) {
@@ -80,17 +93,6 @@ void ArduinoCloudDevice::update() {
8093
_state = nextState;
8194
}
8295

83-
int ArduinoCloudDevice::connected() {
84-
return _state != State::Disconnected ? 1 : 0;
85-
}
86-
87-
void ArduinoCloudDevice::handleMessage(Message *m) {
88-
_command = UnknownCmdId;
89-
if (m != nullptr) {
90-
_command = m->id;
91-
}
92-
}
93-
9496
ArduinoCloudDevice::State ArduinoCloudDevice::handleInit() {
9597
/* Reset attempt struct for the nex retry after disconnection */
9698
_attachAttempt.begin(AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms,

src/ArduinoIoTCloudThing.cpp

+17-15
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ void ArduinoCloudThing::begin() {
5959
}
6060

6161
void ArduinoCloudThing::update() {
62+
handleMessage(nullptr);
63+
}
64+
65+
int ArduinoCloudThing::connected() {
66+
return _state > State::Disconnect ? 1 : 0;
67+
}
68+
69+
void ArduinoCloudThing::handleMessage(Message* m) {
70+
_command = UnknownCmdId;
71+
if (m != nullptr) {
72+
_command = m->id;
73+
if (_command == TimezoneCommandDownId) {
74+
_utcOffset = reinterpret_cast<TimezoneCommandDown*>(m)->params.offset;
75+
_utcOffsetExpireTime = reinterpret_cast<TimezoneCommandDown*>(m)->params.until;
76+
}
77+
}
78+
6279
/* Run through the state machine. */
6380
State nextState = _state;
6481
switch (_state) {
@@ -95,21 +112,6 @@ void ArduinoCloudThing::update() {
95112
_state = nextState;
96113
}
97114

98-
int ArduinoCloudThing::connected() {
99-
return _state > State::Disconnect ? 1 : 0;
100-
}
101-
102-
void ArduinoCloudThing::handleMessage(Message* m) {
103-
_command = UnknownCmdId;
104-
if (m != nullptr) {
105-
_command = m->id;
106-
if (_command == TimezoneCommandDownId) {
107-
_utcOffset = reinterpret_cast<TimezoneCommandDown*>(m)->params.offset;
108-
_utcOffsetExpireTime = reinterpret_cast<TimezoneCommandDown*>(m)->params.until;
109-
}
110-
}
111-
}
112-
113115
ArduinoCloudThing::State ArduinoCloudThing::handleInit() {
114116
_syncAttempt.begin(AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms);
115117
return State::RequestLastValues;

0 commit comments

Comments
 (0)