|
| 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 */ |
0 commit comments