Skip to content

New device process #445

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

Merged
merged 3 commits into from
May 13, 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
4 changes: 2 additions & 2 deletions src/ArduinoIoTCloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
ArduinoIoTCloudClass::ArduinoIoTCloudClass()
: _connection{nullptr}
, _time_service(TimeService)
, _thing_id{""}
, _thing_id{"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}
, _lib_version{AIOT_CONFIG_LIB_VERSION}
, _device_id{""}
, _device_id{"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}
, _cloud_event_callback{nullptr}
{

Expand Down
144 changes: 144 additions & 0 deletions src/ArduinoIoTCloudDevice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
This file is part of the ArduinoIoTCloud library.

Copyright (c) 2024 Arduino SA

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

/******************************************************************************
* INCLUDE
******************************************************************************/

#include <AIoTC_Config.h>

#ifdef HAS_TCP

#include "ArduinoIoTCloudDevice.h"
#include "interfaces/CloudProcess.h"

/******************************************************************************
CTOR/DTOR
******************************************************************************/
ArduinoCloudDevice::ArduinoCloudDevice(MessageStream *ms)
: CloudProcess(ms),
_state{State::Init},
_attachAttempt(0, 0),
_attached(false),
_registered(false) {
}

void ArduinoCloudDevice::begin() {
_attachAttempt.begin(AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms,
AIOT_CONFIG_MAX_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms);
}

void ArduinoCloudDevice::update() {
/* Run through the state machine. */
State nextState = _state;
switch (_state) {
case State::Init: nextState = handleInit(); break;
case State::SendCapabilities: nextState = handleSendCapabilities(); break;
case State::Connected: nextState = handleConnected(); break;
case State::Disconnected: nextState = handleDisconnected(); break;
}

/* Handle external events */
switch (_command) {
case DeviceAttachedCmdId:
_attached = true;
_registered = true;
DEBUG_VERBOSE("CloudDevice::%s Device is attached", __FUNCTION__);
nextState = State::Connected;
break;

case DeviceDetachedCmdId:
_attached = false;
_registered = false;
nextState = State::Init;
break;

case DeviceRegisteredCmdId:
_registered = true;
nextState = State::Connected;
break;

/* We have received a reset command */
case ResetCmdId:
nextState = State::Init;
break;

default:
break;
}

_command = UnknownCmdId;
_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,
AIOT_CONFIG_MAX_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms);

_attached = false;
_registered = false;

return State::SendCapabilities;
}

ArduinoCloudDevice::State ArduinoCloudDevice::handleSendCapabilities() {
/* Sends device capabilities message */
Message message = { DeviceBeginCmdId };
deliver(&message);

/* Subscribe to device topic to request */
message = { ThingBeginCmdId };
deliver(&message);

/* No device configuration received. Wait: 4s -> 8s -> 16s -> 32s -> 32s ...*/
_attachAttempt.retry();
DEBUG_VERBOSE("CloudDevice::%s not attached. %d next configuration request in %d ms",
__FUNCTION__, _attachAttempt.getRetryCount(), _attachAttempt.getWaitTime());
return State::Connected;
}

ArduinoCloudDevice::State ArduinoCloudDevice::handleConnected() {
/* Max retry than disconnect */
if (_attachAttempt.getRetryCount() > AIOT_CONFIG_DEVICE_TOPIC_MAX_RETRY_CNT) {
return State::Disconnected;
}

if (!_attached && _attachAttempt.isExpired()) {
if (_registered) {
/* Device configuration received, but invalid thing_id. Do not increase
* counter, but recompute delay.
* Wait: 4s -> 80s -> 160s -> 320s -> 640s -> 1280s -> 1280s ...
*/
_attachAttempt.reconfigure(AIOT_CONFIG_DEVICE_TOPIC_ATTACH_RETRY_DELAY_ms,
AIOT_CONFIG_MAX_DEVICE_TOPIC_ATTACH_RETRY_DELAY_ms);
}
return State::SendCapabilities;
}

return State::Connected;
}

ArduinoCloudDevice::State ArduinoCloudDevice::handleDisconnected() {
return State::Disconnected;
}

#endif /* HAS_TCP */
70 changes: 70 additions & 0 deletions src/ArduinoIoTCloudDevice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
This file is part of the Arduino_SecureElement library.

Copyright (c) 2024 Arduino SA

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#ifndef ARDUINO_IOT_CLOUD_DEVICE_H
#define ARDUINO_IOT_CLOUD_DEVICE_H

/******************************************************************************
* INCLUDE
******************************************************************************/

#include "interfaces/CloudProcess.h"
#include "utility/time/TimedAttempt.h"
#include "property/PropertyContainer.h"

/******************************************************************************
* CLASS DECLARATION
******************************************************************************/

class ArduinoCloudDevice : public CloudProcess {
public:

ArduinoCloudDevice(MessageStream* stream);
virtual void update() override;
virtual void handleMessage(Message* m) override;

virtual void begin();
virtual int connected();

inline PropertyContainer &getPropertyContainer() {
return _propertyContainer;
};
inline unsigned int &getPropertyContainerIndex() {
return _propertyContainerIndex;
}
inline bool isAttached() {
return _attached;
};


private:

enum class State {
Disconnected,
Init,
SendCapabilities,
Connected,
};

State _state;
CommandId _command;
TimedAttempt _attachAttempt;
PropertyContainer _propertyContainer;
unsigned int _propertyContainerIndex;
bool _attached;
bool _registered;

State handleInit();
State handleSendCapabilities();
State handleConnected();
State handleDisconnected();
};

#endif /* ARDUINO_IOT_CLOUD_DEVICE_H */
Loading
Loading