|
| 1 | +/* |
| 2 | + This file is part of ArduinoIoTCloud. |
| 3 | +
|
| 4 | + Copyright 2024 Blues (http://www.blues.com/) |
| 5 | +
|
| 6 | + This software is released under the GNU General Public License version 3, |
| 7 | + which covers the main part of arduino-cli. |
| 8 | + The terms of this license can be found at: |
| 9 | + https://www.gnu.org/licenses/gpl-3.0.en.html |
| 10 | +
|
| 11 | + You can be released from the requirements of the above licenses by purchasing |
| 12 | + a commercial license. Buying such a license is mandatory if you want to |
| 13 | + modify or otherwise use the software for commercial activities involving the |
| 14 | + Arduino software without disclosing the source code of your own applications. |
| 15 | + To purchase a commercial license, send an email to license@arduino.cc. |
| 16 | +*/ |
| 17 | + |
| 18 | +#ifndef ARDUINO_NOTECARD_CONNECTION_HANDLER_H_ |
| 19 | +#define ARDUINO_NOTECARD_CONNECTION_HANDLER_H_ |
| 20 | + |
| 21 | +/****************************************************************************** |
| 22 | + INCLUDE |
| 23 | + ******************************************************************************/ |
| 24 | + |
| 25 | +#include <stdint.h> |
| 26 | + |
| 27 | +#include <Arduino.h> |
| 28 | +#include <Notecard.h> |
| 29 | +#include <Wire.h> |
| 30 | + |
| 31 | +#include "Arduino_ConnectionHandlerInterface.h" |
| 32 | + |
| 33 | +/****************************************************************************** |
| 34 | + DEFINES |
| 35 | + ******************************************************************************/ |
| 36 | + |
| 37 | +#define NOTECARD_CONNECTION_HANDLER_VERSION_MAJOR 1 |
| 38 | +#define NOTECARD_CONNECTION_HANDLER_VERSION_MINOR 0 |
| 39 | +#define NOTECARD_CONNECTION_HANDLER_VERSION_PATCH 0 |
| 40 | + |
| 41 | +#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) |
| 42 | + |
| 43 | +/****************************************************************************** |
| 44 | + CLASS DECLARATION |
| 45 | + ******************************************************************************/ |
| 46 | + |
| 47 | +/** |
| 48 | + * @brief The NotecardConnectionHandler class |
| 49 | + * |
| 50 | + * The NotecardConnectionHandler class is a concrete implementation of the |
| 51 | + * ConnectionHandler interface that provides connectivity to the Arduino IoT |
| 52 | + * Cloud using a Notecard. |
| 53 | + */ |
| 54 | +class NotecardConnectionHandler final : public ConnectionHandler |
| 55 | +{ |
| 56 | + public: |
| 57 | + /** |
| 58 | + * @brief The type of topic to be used for R/W operations |
| 59 | + * |
| 60 | + * The Notecard uses topics to identify the target of a read or write |
| 61 | + * operation. The TopicType enum defines the valid types of topics. |
| 62 | + * |
| 63 | + * @par |
| 64 | + * - Command - used to interact with the Arduino IoT Cloud. |
| 65 | + * - Thing - used to send application data to the Arduino IoT Cloud. |
| 66 | + */ |
| 67 | + enum class TopicType : uint8_t { |
| 68 | + Invalid = 0, |
| 69 | + Command, |
| 70 | + Thing, |
| 71 | + }; |
| 72 | + |
| 73 | + /** |
| 74 | + * @brief The error codes for communicating with the Notecard |
| 75 | + * |
| 76 | + * The NotecardCommunicationError enum defines the error codes that can be |
| 77 | + * returned by the NotecardConnectionHandler class. |
| 78 | + * |
| 79 | + * @par |
| 80 | + * - NOTECARD_ERROR_NONE - No error occurred. |
| 81 | + * - NOTECARD_ERROR_NO_DATA_AVAILABLE - No data is available. |
| 82 | + * - NOTECARD_ERROR_GENERIC - A generic error occurred. |
| 83 | + * - HOST_ERROR_OUT_OF_MEMORY - The host is out of memory. |
| 84 | + */ |
| 85 | + typedef enum { |
| 86 | + NOTECARD_ERROR_NONE = 0, |
| 87 | + NOTECARD_ERROR_NO_DATA_AVAILABLE = -1, |
| 88 | + NOTECARD_ERROR_GENERIC = -2, |
| 89 | + HOST_ERROR_OUT_OF_MEMORY = -3, |
| 90 | + } NotecardCommunicationError; |
| 91 | + |
| 92 | + /** |
| 93 | + * @brief The default timeout for the Notecard to connect to Notehub |
| 94 | + */ |
| 95 | + static const uint32_t NOTEHUB_CONN_TIMEOUT_MS = 185000; |
| 96 | + |
| 97 | + /** |
| 98 | + * @brief The I2C constructor for the Notecard |
| 99 | + * |
| 100 | + * @param project_uid[in] The project UID of the related Notehub account |
| 101 | + * @param keep_alive[in] Keep the connection alive if connection to Notehub drops |
| 102 | + * @param i2c_address[in] The I2C address of the Notecard |
| 103 | + * @param i2c_max[in] The maximum I2C transaction size (MTU) |
| 104 | + * @param wire[in] The I2C bus to use |
| 105 | + */ |
| 106 | + NotecardConnectionHandler( |
| 107 | + const String & project_uid, |
| 108 | + bool keep_alive = true, |
| 109 | + uint32_t i2c_address = NOTE_I2C_ADDR_DEFAULT, |
| 110 | + uint32_t i2c_max = NOTE_I2C_MAX_DEFAULT, |
| 111 | + TwoWire & wire = Wire |
| 112 | + ); |
| 113 | + |
| 114 | + /** |
| 115 | + * @brief The UART constructor for the Notecard |
| 116 | + * |
| 117 | + * @param project_uid[in] The project UID of the related Notehub account |
| 118 | + * @param serial[in] The serial port to use |
| 119 | + * @param keep_alive[in] Keep the connection alive if connection to Notehub drops |
| 120 | + * @param baud[in] The baud rate of the serial port |
| 121 | + */ |
| 122 | + NotecardConnectionHandler( |
| 123 | + const String & project_uid, |
| 124 | + HardwareSerial & serial, |
| 125 | + bool keep_alive = true, |
| 126 | + uint32_t baud = 9600 |
| 127 | + ); |
| 128 | + |
| 129 | + /** |
| 130 | + * @brief Disable hardware interrupts |
| 131 | + * |
| 132 | + * When hardware interrupts are disabled, the `NotecardConnectionHandler` |
| 133 | + * must be polled for incoming data. This is necessary when the host |
| 134 | + * microcontroller is unable to use the ATTN pin of the Notecard. |
| 135 | + */ |
| 136 | + inline void disableHardwareInterrupts (void) { |
| 137 | + _en_hw_int = false; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @brief Enable hardware interrupts |
| 142 | + * |
| 143 | + * Hardware interrupts allow the `NotecardConnectionHandler` to leverage the |
| 144 | + * ATTN pin of the Notecard. This improves the responsiveness of the |
| 145 | + * `NotecardConnectionHandler` by eliminating the need for the host |
| 146 | + * microcontroller to poll the Notecard for incoming data. |
| 147 | + */ |
| 148 | + inline void enableHardwareInterrupts (void) { |
| 149 | + _en_hw_int = true; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @brief Get the Arduino IoT Cloud Device ID |
| 154 | + * |
| 155 | + * @return The Arduino IoT Cloud Device ID |
| 156 | + */ |
| 157 | + inline const String & getDeviceId(void) { |
| 158 | + check(); // Ensure the connection to the Notecard is initialized |
| 159 | + return _device_id; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * @brief Get the Notecard Device ID |
| 164 | + * |
| 165 | + * @return The Notecard Device ID |
| 166 | + */ |
| 167 | + inline const String & getNotecardUid(void) { |
| 168 | + check(); // Ensure the connection to the Notecard is initialized |
| 169 | + return _notecard_uid; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * @brief Get the topic type for R/W operations |
| 174 | + * |
| 175 | + * @return The current topic type |
| 176 | + */ |
| 177 | + TopicType getTopicType(void) const { |
| 178 | + return _topic_type; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * @brief Set the topic type for R/W operations |
| 183 | + * |
| 184 | + * @param topic[in] The topic type to set |
| 185 | + * @par |
| 186 | + * - TopicType::Command - used to interact with the Arduino IoT Cloud. |
| 187 | + * - TopicType::Thing - used to send application data to the Arduino IoT Cloud. |
| 188 | + * |
| 189 | + * @see TopicType |
| 190 | + */ |
| 191 | + void setTopicType(TopicType topic) { |
| 192 | + _topic_type = topic; |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * @brief Set the WiFi credentials to be used by the Notecard |
| 197 | + * |
| 198 | + * @param ssid[in] The SSID of the WiFi network |
| 199 | + * @param pass[in] The password of the WiFi network |
| 200 | + * |
| 201 | + * @return 0 if successful, otherwise an error code |
| 202 | + * |
| 203 | + * @note This method is only applicable when using a Wi-Fi capable Notecard, |
| 204 | + * and is unnecessary when using a Notecard with cellular connectivity. |
| 205 | + * If the Notecard is not Wi-Fi capable, this method will be a no-op. |
| 206 | + * |
| 207 | + * @see NotecardCommunicationError |
| 208 | + */ |
| 209 | + int setWiFiCredentials (const String & ssid, const String & pass); |
| 210 | + |
| 211 | + // ConnectionHandler interface |
| 212 | + virtual bool available() override; |
| 213 | + virtual unsigned long getTime() override; |
| 214 | + virtual int read() override; |
| 215 | + virtual int write(const uint8_t *buf, size_t size) override; |
| 216 | + |
| 217 | + protected: |
| 218 | + |
| 219 | + virtual NetworkConnectionState update_handleInit () override; |
| 220 | + virtual NetworkConnectionState update_handleConnecting () override; |
| 221 | + virtual NetworkConnectionState update_handleConnected () override; |
| 222 | + virtual NetworkConnectionState update_handleDisconnecting() override; |
| 223 | + virtual NetworkConnectionState update_handleDisconnected () override; |
| 224 | + |
| 225 | + private: |
| 226 | + |
| 227 | + // Private members |
| 228 | + Notecard _notecard; |
| 229 | + //TODO: Remove if not needed |
| 230 | + String _device_id; |
| 231 | + String _notecard_uid; |
| 232 | + String _project_uid; |
| 233 | + HardwareSerial * _serial; |
| 234 | + TwoWire * _wire; |
| 235 | + uint8_t * _inbound_buffer; |
| 236 | + uint32_t _conn_start_ms; |
| 237 | + uint32_t _i2c_address; |
| 238 | + uint32_t _i2c_max; |
| 239 | + uint32_t _inbound_buffer_index; |
| 240 | + uint32_t _inbound_buffer_size; |
| 241 | + uint32_t _uart_baud; |
| 242 | + bool _en_hw_int; |
| 243 | + TopicType _topic_type; |
| 244 | + |
| 245 | + // Private methods |
| 246 | + bool armInterrupt (void) const; |
| 247 | + bool configureConnection (bool connect) const; |
| 248 | + uint_fast8_t connected (void) const; |
| 249 | + J * getNote (bool pop = false) const; |
| 250 | + int initiateNotehubSync (void) const; |
| 251 | + bool updateUidCache (void); |
| 252 | +}; |
| 253 | + |
| 254 | +#endif /* ARDUINO_NOTECARD_CONNECTION_HANDLER_H_ */ |
0 commit comments