|
| 1 | +/* |
| 2 | + This file is part of the ArduinoIoTCloud library. |
| 3 | +
|
| 4 | + Copyright 2024 Blues (http://www.blues.com/) |
| 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_NOTECARD_CONNECTION_HANDLER_H_ |
| 12 | +#define ARDUINO_NOTECARD_CONNECTION_HANDLER_H_ |
| 13 | + |
| 14 | +/****************************************************************************** |
| 15 | + INCLUDE |
| 16 | + ******************************************************************************/ |
| 17 | + |
| 18 | +#include <stdint.h> |
| 19 | + |
| 20 | +#include <Arduino.h> |
| 21 | +#include <Notecard.h> |
| 22 | +#include <Wire.h> |
| 23 | + |
| 24 | +#include "ConnectionHandlerInterface.h" |
| 25 | + |
| 26 | +/****************************************************************************** |
| 27 | + DEFINES |
| 28 | + ******************************************************************************/ |
| 29 | + |
| 30 | +#define NOTECARD_CONNECTION_HANDLER_VERSION_MAJOR 1 |
| 31 | +#define NOTECARD_CONNECTION_HANDLER_VERSION_MINOR 0 |
| 32 | +#define NOTECARD_CONNECTION_HANDLER_VERSION_PATCH 0 |
| 33 | + |
| 34 | +#define NOTECARD_CONNECTION_HANDLER_VERSION NOTE_C_STRINGIZE(NOTECARD_CONNECTION_HANDLER_VERSION_MAJOR) "." NOTE_C_STRINGIZE(NOTECARD_CONNECTION_HANDLER_VERSION_MINOR) "." NOTE_C_STRINGIZE(NOTECARD_CONNECTION_HANDLER_VERSION_PATCH) |
| 35 | + |
| 36 | +/****************************************************************************** |
| 37 | + CLASS DECLARATION |
| 38 | + ******************************************************************************/ |
| 39 | + |
| 40 | +/** |
| 41 | + * @brief The NotecardConnectionHandler class |
| 42 | + * |
| 43 | + * The NotecardConnectionHandler class is a concrete implementation of the |
| 44 | + * ConnectionHandler interface that provides connectivity to the Arduino IoT |
| 45 | + * Cloud using a Notecard. |
| 46 | + */ |
| 47 | +class NotecardConnectionHandler final : public ConnectionHandler |
| 48 | +{ |
| 49 | + public: |
| 50 | + /** |
| 51 | + * @brief The manner in which the Notecard is synchronized with Notehub |
| 52 | + * |
| 53 | + * The SyncType enum defines the valid types of synchronization operations |
| 54 | + * that can be performed by the NotecardConnectionHandler class. |
| 55 | + * |
| 56 | + * @par |
| 57 | + * - Full - synchronize both the inbound and outbound queues. |
| 58 | + * - Inbound - synchronize only the inbound queues. |
| 59 | + * - Outbound - synchronize only the outbound queues. |
| 60 | + */ |
| 61 | + enum class SyncType : uint8_t { |
| 62 | + Full, |
| 63 | + Inbound, |
| 64 | + Outbound, |
| 65 | + }; |
| 66 | + |
| 67 | + /** |
| 68 | + * @brief The type of topic to be used for R/W operations |
| 69 | + * |
| 70 | + * The Notecard uses topics to identify the target of a read or write |
| 71 | + * operation. The TopicType enum defines the valid types of topics. |
| 72 | + * |
| 73 | + * @par |
| 74 | + * - Command - used to interact with the Arduino IoT Cloud. |
| 75 | + * - Thing - used to send application data to the Arduino IoT Cloud. |
| 76 | + */ |
| 77 | + enum class TopicType : uint8_t { |
| 78 | + Invalid = 0, |
| 79 | + Command, |
| 80 | + Thing, |
| 81 | + }; |
| 82 | + |
| 83 | + /** |
| 84 | + * @brief The error codes for communicating with the Notecard |
| 85 | + * |
| 86 | + * The NotecardCommunicationError enum defines the error codes that can be |
| 87 | + * returned by the NotecardConnectionHandler class. |
| 88 | + * |
| 89 | + * @par |
| 90 | + * - NOTECARD_ERROR_NONE - No error occurred. |
| 91 | + * - NOTECARD_ERROR_NO_DATA_AVAILABLE - No data is available. |
| 92 | + * - NOTECARD_ERROR_GENERIC - A generic error occurred. |
| 93 | + * - HOST_ERROR_OUT_OF_MEMORY - The host is out of memory. |
| 94 | + */ |
| 95 | + typedef enum { |
| 96 | + NOTECARD_ERROR_NONE = 0, |
| 97 | + NOTECARD_ERROR_NO_DATA_AVAILABLE = -1, |
| 98 | + NOTECARD_ERROR_GENERIC = -2, |
| 99 | + HOST_ERROR_OUT_OF_MEMORY = -3, |
| 100 | + } NotecardCommunicationError; |
| 101 | + |
| 102 | + /** |
| 103 | + * @brief The default timeout for the Notecard to connect to Notehub |
| 104 | + */ |
| 105 | + static const uint32_t NOTEHUB_CONN_TIMEOUT_MS = 185000; |
| 106 | + |
| 107 | + /** |
| 108 | + * @brief The I2C constructor for the Notecard |
| 109 | + * |
| 110 | + * @param project_uid[in] The project UID of the related Notehub account |
| 111 | + * @param i2c_address[in] The I2C address of the Notecard |
| 112 | + * @param i2c_max[in] The maximum I2C transaction size (MTU) |
| 113 | + * @param wire[in] The I2C bus to use |
| 114 | + * @param keep_alive[in] Keep the connection alive if connection to Notehub drops |
| 115 | + */ |
| 116 | + NotecardConnectionHandler( |
| 117 | + const String & project_uid, |
| 118 | + uint32_t i2c_address = NOTE_I2C_ADDR_DEFAULT, |
| 119 | + uint32_t i2c_max = NOTE_I2C_MAX_DEFAULT, |
| 120 | + TwoWire & wire = Wire, |
| 121 | + bool keep_alive = true |
| 122 | + ); |
| 123 | + |
| 124 | + /** |
| 125 | + * @brief The UART constructor for the Notecard |
| 126 | + * |
| 127 | + * @param project_uid[in] The project UID of the related Notehub account |
| 128 | + * @param serial[in] The serial port to use |
| 129 | + * @param baud[in] The baud rate of the serial port |
| 130 | + * @param keep_alive[in] Keep the connection alive if connection to Notehub drops |
| 131 | + */ |
| 132 | + NotecardConnectionHandler( |
| 133 | + const String & project_uid, |
| 134 | + HardwareSerial & serial, |
| 135 | + uint32_t baud = 9600, |
| 136 | + bool keep_alive = true |
| 137 | + ); |
| 138 | + |
| 139 | + /** |
| 140 | + * @brief Disable hardware interrupts |
| 141 | + * |
| 142 | + * When hardware interrupts are disabled, the `NotecardConnectionHandler` |
| 143 | + * must be polled for incoming data. This is necessary when the host |
| 144 | + * microcontroller is unable to use the ATTN pin of the Notecard. |
| 145 | + */ |
| 146 | + inline void disableHardwareInterrupts (void) { |
| 147 | + _en_hw_int = false; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * @brief Enable hardware interrupts |
| 152 | + * |
| 153 | + * Hardware interrupts allow the `NotecardConnectionHandler` to leverage the |
| 154 | + * ATTN pin of the Notecard. This improves the responsiveness of the |
| 155 | + * `NotecardConnectionHandler` by eliminating the need for the host |
| 156 | + * microcontroller to poll the Notecard for incoming data. |
| 157 | + */ |
| 158 | + inline void enableHardwareInterrupts (void) { |
| 159 | + _en_hw_int = true; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * @brief Get the Arduino IoT Cloud Device ID |
| 164 | + * |
| 165 | + * The Arduino IoT Cloud Device ID is set as the serial number of the |
| 166 | + * Notecard when the device is provisioned in Notehub. The serial number is |
| 167 | + * updated on each sync between the Notecard and Notehub and cached by the |
| 168 | + * Notecard. As a result, this value can lag behind the actual value of the |
| 169 | + * Arduino IoT Cloud Device ID used by the Notehub. However, this value is |
| 170 | + * typically unchanged during the life of the Notecard, so this is rarely, |
| 171 | + * if ever, an issue. |
| 172 | + * |
| 173 | + * @return The Arduino IoT Cloud Device ID |
| 174 | + */ |
| 175 | + inline const String & getDeviceId (void) { |
| 176 | + check(); // Ensure the connection to the Notecard is initialized |
| 177 | + return _device_id; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * @brief Get the Notecard object |
| 182 | + * |
| 183 | + * The Notecard object is used to interact with the Notecard. This object |
| 184 | + * provides methods to read and write data to the Notecard, as well as |
| 185 | + * methods to configure the Notecard. |
| 186 | + * |
| 187 | + * @return The Notecard object |
| 188 | + */ |
| 189 | + inline const Notecard & getNotecard (void) { |
| 190 | + return _notecard; |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * @brief Get the Notecard Device ID |
| 195 | + * |
| 196 | + * The Notecard Device ID is the unique identifier of the Notecard. This |
| 197 | + * value is set at time of manufacture, and is used to identify the Notecard |
| 198 | + * in Notehub. |
| 199 | + * |
| 200 | + * @return The Notecard Device ID |
| 201 | + */ |
| 202 | + inline const String & getNotecardUid (void) { |
| 203 | + check(); // Ensure the connection to the Notecard is initialized |
| 204 | + return _notecard_uid; |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * @brief Get the topic type of the most recent R/W operations |
| 209 | + * |
| 210 | + * @return The current topic type |
| 211 | + * |
| 212 | + * @see TopicType |
| 213 | + */ |
| 214 | + TopicType getTopicType (void) const { |
| 215 | + return _topic_type; |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * @brief Initiate a synchronization operation with Notehub |
| 220 | + * |
| 221 | + * The Notecard maintains two queues: an inbound queue and an outbound |
| 222 | + * queue. The inbound queue is used to receive data from Notehub, while the |
| 223 | + * outbound queue is used to send data to Notehub. This method initiates a |
| 224 | + * synchronization operation between the Notecard and Notehub. |
| 225 | + * |
| 226 | + * As the name implies, this method is asynchronous and will only initiate |
| 227 | + * the synchronization operation. The actual synchronization operation will |
| 228 | + * be performed by the Notecard in the background. |
| 229 | + * |
| 230 | + * @param type[in] The type of synchronization operation to perform |
| 231 | + * @par |
| 232 | + * - SyncType::Full - synchronize both the inbound and outbound queues (default) |
| 233 | + * - SyncType::Inbound - synchronize only the inbound queues. |
| 234 | + * - SyncType::Outbound - synchronize only the outbound queues. |
| 235 | + * |
| 236 | + * @return 0 if successful, otherwise an error code |
| 237 | + * |
| 238 | + * @see SyncType |
| 239 | + * @see NotecardCommunicationError |
| 240 | + */ |
| 241 | + int initiateNotehubSync (SyncType type = SyncType::Full) const; |
| 242 | + |
| 243 | + /** |
| 244 | + * @brief Set the inbound polling interval (in minutes) |
| 245 | + * |
| 246 | + * A cellular Notecard will receive inbound traffic from the Arduino IoT |
| 247 | + * Cloud in real-time. As such, the polling interval is used as a fail-safe |
| 248 | + * to ensure the Notecard is guaranteed to receive inbound traffic at the |
| 249 | + * interval specified by this method. |
| 250 | + * |
| 251 | + * Alternatively, a LoRa (or Satellite) Notecard does not maintain a |
| 252 | + * continuous connection, and therefore must rely on the polling interval to |
| 253 | + * establish the maximum acceptable delay before receiving any unsolicited, |
| 254 | + * inbound traffic from the Arduino IoT Cloud. The polling interval must |
| 255 | + * balance the needs of the application against the regulatory limitations |
| 256 | + * of LoRa (or bandwidth limitations and cost of Satellite). |
| 257 | + * |
| 258 | + * LoRaWAN Fair Use Policy: |
| 259 | + * https://www.thethingsnetwork.org/forum/t/fair-use-policy-explained/1300 |
| 260 | + * |
| 261 | + * @param interval_min[in] The inbound polling interval (in minutes) |
| 262 | + * |
| 263 | + * @note Set the interval to 0 to disable inbound polling. |
| 264 | + */ |
| 265 | + inline void setNotehubPollingInterval (int32_t interval_min) { |
| 266 | + _inbound_polling_interval_min = (interval_min ? interval_min : -1); |
| 267 | + } |
| 268 | + |
| 269 | + /** |
| 270 | + * @brief Set the topic type for R/W operations |
| 271 | + * |
| 272 | + * @param topic[in] The topic type |
| 273 | + * @par |
| 274 | + * - TopicType::Command - used to interact with the Arduino IoT Cloud. |
| 275 | + * - TopicType::Thing - used to send application data to the Arduino IoT Cloud. |
| 276 | + * |
| 277 | + * @see TopicType |
| 278 | + */ |
| 279 | + void setTopicType (TopicType topic) { |
| 280 | + _topic_type = topic; |
| 281 | + } |
| 282 | + |
| 283 | + /** |
| 284 | + * @brief Set the WiFi credentials to be used by the Notecard |
| 285 | + * |
| 286 | + * @param ssid[in] The SSID of the WiFi network |
| 287 | + * @param pass[in] The password of the WiFi network |
| 288 | + * |
| 289 | + * @return 0 if successful, otherwise an error code |
| 290 | + * |
| 291 | + * @note This method is only applicable when using a Wi-Fi capable Notecard, |
| 292 | + * and is unnecessary when using a Notecard with cellular connectivity. |
| 293 | + * If the Notecard is not Wi-Fi capable, this method will be a no-op. |
| 294 | + * |
| 295 | + * @see NotecardCommunicationError |
| 296 | + */ |
| 297 | + int setWiFiCredentials (const String & ssid, const String & pass); |
| 298 | + |
| 299 | + // ConnectionHandler interface |
| 300 | + virtual bool available() override; |
| 301 | + virtual unsigned long getTime() override; |
| 302 | + virtual int read() override; |
| 303 | + virtual int write(const uint8_t *buf, size_t size) override; |
| 304 | + |
| 305 | + protected: |
| 306 | + |
| 307 | + virtual NetworkConnectionState update_handleInit () override; |
| 308 | + virtual NetworkConnectionState update_handleConnecting () override; |
| 309 | + virtual NetworkConnectionState update_handleConnected () override; |
| 310 | + virtual NetworkConnectionState update_handleDisconnecting() override; |
| 311 | + virtual NetworkConnectionState update_handleDisconnected () override; |
| 312 | + |
| 313 | + private: |
| 314 | + |
| 315 | + // Private members |
| 316 | + Notecard _notecard; |
| 317 | + String _device_id; |
| 318 | + String _notecard_uid; |
| 319 | + String _project_uid; |
| 320 | + HardwareSerial * _serial; |
| 321 | + TwoWire * _wire; |
| 322 | + uint8_t * _inbound_buffer; |
| 323 | + uint32_t _conn_start_ms; |
| 324 | + uint32_t _i2c_address; |
| 325 | + uint32_t _i2c_max; |
| 326 | + uint32_t _inbound_buffer_index; |
| 327 | + uint32_t _inbound_buffer_size; |
| 328 | + int32_t _inbound_polling_interval_min; |
| 329 | + uint32_t _uart_baud; |
| 330 | + bool _en_hw_int; |
| 331 | + TopicType _topic_type; |
| 332 | + |
| 333 | + // Private methods |
| 334 | + bool armInterrupt (void) const; |
| 335 | + bool configureConnection (bool connect) const; |
| 336 | + uint_fast8_t connected (void) const; |
| 337 | + J * getNote (bool pop = false) const; |
| 338 | + bool updateUidCache (void); |
| 339 | +}; |
| 340 | + |
| 341 | +#endif /* ARDUINO_NOTECARD_CONNECTION_HANDLER_H_ */ |
0 commit comments