Skip to content

Commit be7aa4d

Browse files
authored
Merge pull request #445 from arduino-libraries/new-device-process
New device process
2 parents d017d4a + 181bba7 commit be7aa4d

5 files changed

+356
-212
lines changed

Diff for: src/ArduinoIoTCloud.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
ArduinoIoTCloudClass::ArduinoIoTCloudClass()
2929
: _connection{nullptr}
3030
, _time_service(TimeService)
31-
, _thing_id{""}
31+
, _thing_id{"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}
3232
, _lib_version{AIOT_CONFIG_LIB_VERSION}
33-
, _device_id{""}
33+
, _device_id{"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}
3434
, _cloud_event_callback{nullptr}
3535
{
3636

Diff for: src/ArduinoIoTCloudDevice.cpp

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
This file is part of the ArduinoIoTCloud library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
/******************************************************************************
12+
* INCLUDE
13+
******************************************************************************/
14+
15+
#include <AIoTC_Config.h>
16+
17+
#ifdef HAS_TCP
18+
19+
#include "ArduinoIoTCloudDevice.h"
20+
#include "interfaces/CloudProcess.h"
21+
22+
/******************************************************************************
23+
CTOR/DTOR
24+
******************************************************************************/
25+
ArduinoCloudDevice::ArduinoCloudDevice(MessageStream *ms)
26+
: CloudProcess(ms),
27+
_state{State::Init},
28+
_attachAttempt(0, 0),
29+
_attached(false),
30+
_registered(false) {
31+
}
32+
33+
void ArduinoCloudDevice::begin() {
34+
_attachAttempt.begin(AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms,
35+
AIOT_CONFIG_MAX_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms);
36+
}
37+
38+
void ArduinoCloudDevice::update() {
39+
/* Run through the state machine. */
40+
State nextState = _state;
41+
switch (_state) {
42+
case State::Init: nextState = handleInit(); break;
43+
case State::SendCapabilities: nextState = handleSendCapabilities(); break;
44+
case State::Connected: nextState = handleConnected(); break;
45+
case State::Disconnected: nextState = handleDisconnected(); break;
46+
}
47+
48+
/* Handle external events */
49+
switch (_command) {
50+
case DeviceAttachedCmdId:
51+
_attached = true;
52+
_registered = true;
53+
DEBUG_VERBOSE("CloudDevice::%s Device is attached", __FUNCTION__);
54+
nextState = State::Connected;
55+
break;
56+
57+
case DeviceDetachedCmdId:
58+
_attached = false;
59+
_registered = false;
60+
nextState = State::Init;
61+
break;
62+
63+
case DeviceRegisteredCmdId:
64+
_registered = true;
65+
nextState = State::Connected;
66+
break;
67+
68+
/* We have received a reset command */
69+
case ResetCmdId:
70+
nextState = State::Init;
71+
break;
72+
73+
default:
74+
break;
75+
}
76+
77+
_command = UnknownCmdId;
78+
_state = nextState;
79+
}
80+
81+
int ArduinoCloudDevice::connected() {
82+
return _state != State::Disconnected ? 1 : 0;
83+
}
84+
85+
void ArduinoCloudDevice::handleMessage(Message *m) {
86+
_command = UnknownCmdId;
87+
if (m != nullptr) {
88+
_command = m->id;
89+
}
90+
}
91+
92+
ArduinoCloudDevice::State ArduinoCloudDevice::handleInit() {
93+
/* Reset attempt struct for the nex retry after disconnection */
94+
_attachAttempt.begin(AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms,
95+
AIOT_CONFIG_MAX_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms);
96+
97+
_attached = false;
98+
_registered = false;
99+
100+
return State::SendCapabilities;
101+
}
102+
103+
ArduinoCloudDevice::State ArduinoCloudDevice::handleSendCapabilities() {
104+
/* Sends device capabilities message */
105+
Message message = { DeviceBeginCmdId };
106+
deliver(&message);
107+
108+
/* Subscribe to device topic to request */
109+
message = { ThingBeginCmdId };
110+
deliver(&message);
111+
112+
/* No device configuration received. Wait: 4s -> 8s -> 16s -> 32s -> 32s ...*/
113+
_attachAttempt.retry();
114+
DEBUG_VERBOSE("CloudDevice::%s not attached. %d next configuration request in %d ms",
115+
__FUNCTION__, _attachAttempt.getRetryCount(), _attachAttempt.getWaitTime());
116+
return State::Connected;
117+
}
118+
119+
ArduinoCloudDevice::State ArduinoCloudDevice::handleConnected() {
120+
/* Max retry than disconnect */
121+
if (_attachAttempt.getRetryCount() > AIOT_CONFIG_DEVICE_TOPIC_MAX_RETRY_CNT) {
122+
return State::Disconnected;
123+
}
124+
125+
if (!_attached && _attachAttempt.isExpired()) {
126+
if (_registered) {
127+
/* Device configuration received, but invalid thing_id. Do not increase
128+
* counter, but recompute delay.
129+
* Wait: 4s -> 80s -> 160s -> 320s -> 640s -> 1280s -> 1280s ...
130+
*/
131+
_attachAttempt.reconfigure(AIOT_CONFIG_DEVICE_TOPIC_ATTACH_RETRY_DELAY_ms,
132+
AIOT_CONFIG_MAX_DEVICE_TOPIC_ATTACH_RETRY_DELAY_ms);
133+
}
134+
return State::SendCapabilities;
135+
}
136+
137+
return State::Connected;
138+
}
139+
140+
ArduinoCloudDevice::State ArduinoCloudDevice::handleDisconnected() {
141+
return State::Disconnected;
142+
}
143+
144+
#endif /* HAS_TCP */

Diff for: src/ArduinoIoTCloudDevice.h

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
This file is part of the Arduino_SecureElement library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#ifndef ARDUINO_IOT_CLOUD_DEVICE_H
12+
#define ARDUINO_IOT_CLOUD_DEVICE_H
13+
14+
/******************************************************************************
15+
* INCLUDE
16+
******************************************************************************/
17+
18+
#include "interfaces/CloudProcess.h"
19+
#include "utility/time/TimedAttempt.h"
20+
#include "property/PropertyContainer.h"
21+
22+
/******************************************************************************
23+
* CLASS DECLARATION
24+
******************************************************************************/
25+
26+
class ArduinoCloudDevice : public CloudProcess {
27+
public:
28+
29+
ArduinoCloudDevice(MessageStream* stream);
30+
virtual void update() override;
31+
virtual void handleMessage(Message* m) override;
32+
33+
virtual void begin();
34+
virtual int connected();
35+
36+
inline PropertyContainer &getPropertyContainer() {
37+
return _propertyContainer;
38+
};
39+
inline unsigned int &getPropertyContainerIndex() {
40+
return _propertyContainerIndex;
41+
}
42+
inline bool isAttached() {
43+
return _attached;
44+
};
45+
46+
47+
private:
48+
49+
enum class State {
50+
Disconnected,
51+
Init,
52+
SendCapabilities,
53+
Connected,
54+
};
55+
56+
State _state;
57+
CommandId _command;
58+
TimedAttempt _attachAttempt;
59+
PropertyContainer _propertyContainer;
60+
unsigned int _propertyContainerIndex;
61+
bool _attached;
62+
bool _registered;
63+
64+
State handleInit();
65+
State handleSendCapabilities();
66+
State handleConnected();
67+
State handleDisconnected();
68+
};
69+
70+
#endif /* ARDUINO_IOT_CLOUD_DEVICE_H */

0 commit comments

Comments
 (0)