|
| 1 | +/* |
| 2 | + This file is part of ArduinoIoTCloud. |
| 3 | +
|
| 4 | + Copyright 2019 ARDUINO SA (http://www.arduino.cc/) |
| 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 modify or |
| 13 | + otherwise use the software for commercial activities involving the Arduino |
| 14 | + software without disclosing the source code of your own applications. To purchase |
| 15 | + a commercial license, send an email to license@arduino.cc. |
| 16 | +*/ |
| 17 | + |
| 18 | +//TODO: Understand how `_keep_alive` is updated and used in the `ConnectionHandler` class |
| 19 | + |
| 20 | +#ifndef ARDUINO_NOTECARD_CONNECTION_HANDLER_H_ |
| 21 | +#define ARDUINO_NOTECARD_CONNECTION_HANDLER_H_ |
| 22 | + |
| 23 | +/****************************************************************************** |
| 24 | + INCLUDE |
| 25 | + ******************************************************************************/ |
| 26 | + |
| 27 | +#if defined(USE_NOTECARD) /* Only compile if the Notecard is present */ |
| 28 | + |
| 29 | +#include <stdint.h> |
| 30 | +#include <Notecard.h> |
| 31 | + |
| 32 | +#include "Arduino_ConnectionHandler.h" |
| 33 | + |
| 34 | +/****************************************************************************** |
| 35 | + CLASS DECLARATION |
| 36 | + ******************************************************************************/ |
| 37 | + |
| 38 | +class NotecardConnectionHandler final : public ConnectionHandler |
| 39 | +{ |
| 40 | + public: |
| 41 | + enum class TopicType : uint8_t { |
| 42 | + Invalid = 0, |
| 43 | + Command, |
| 44 | + Thing, |
| 45 | + Notehub = 255 |
| 46 | + }; |
| 47 | + |
| 48 | + typedef enum { |
| 49 | + NOTECARD_ERROR_NONE = 0, |
| 50 | + NOTECARD_ERROR_NO_DATA_AVAILABLE = -1, |
| 51 | + NOTECARD_ERROR_GENERIC = -2, |
| 52 | + HOST_ERROR_OUT_OF_MEMORY = -3, |
| 53 | + } NotecardCommunicationError; |
| 54 | + |
| 55 | + static const uint32_t NOTEHUB_CONN_TIMEOUT_MS = 185000; |
| 56 | + |
| 57 | + NotecardConnectionHandler( |
| 58 | + const String & project_uid, |
| 59 | + bool en_hw_int = false, |
| 60 | + bool keep_alive = true, |
| 61 | + uint32_t i2c_address = NOTE_I2C_ADDR_DEFAULT, |
| 62 | + uint32_t i2c_max = NOTE_I2C_MAX_DEFAULT, |
| 63 | + TwoWire & wire = Wire, |
| 64 | + const String & notehub_url = "" |
| 65 | + ); |
| 66 | + |
| 67 | + NotecardConnectionHandler( |
| 68 | + const String & project_uid, |
| 69 | + HardwareSerial & serial, |
| 70 | + uint32_t speed = 9600, |
| 71 | + bool en_hw_int = false, |
| 72 | + bool keep_alive = true, |
| 73 | + const String & notehub_url = "" |
| 74 | + ); |
| 75 | + |
| 76 | + // Notehub Logging |
| 77 | + inline int disableNotehubLogging (void) const |
| 78 | + { |
| 79 | + Debug.print(DBG_INFO, F("Disabling Notehub logging...")); |
| 80 | + return notehubLogging(false); |
| 81 | + } |
| 82 | + inline int enableNotehubLogging (void) const |
| 83 | + { |
| 84 | + Debug.print(DBG_INFO, F("Enabling Notehub logging...")); |
| 85 | + return notehubLogging(true); |
| 86 | + } |
| 87 | + |
| 88 | + // Accessors for Unique Hardware Identifiers |
| 89 | + const String & getNotecardUid(void) const { |
| 90 | + return _notecard_uid; |
| 91 | + } |
| 92 | + |
| 93 | + // Identify the target topic for R/W operations |
| 94 | + TopicType getTopicType(void) const { |
| 95 | + return _topic_type; |
| 96 | + } |
| 97 | + void setTopicType(TopicType topic) { |
| 98 | + _topic_type = topic; |
| 99 | + } |
| 100 | + |
| 101 | + const String & syncArduinoDeviceId (const String & device_id); |
| 102 | + int syncSecretDeviceKey (const String & secret_device_key); |
| 103 | + |
| 104 | + // ConnectionHandler interface |
| 105 | + virtual bool available() override; |
| 106 | + virtual unsigned long getTime() override; |
| 107 | + virtual int read() override; |
| 108 | + virtual int write(const uint8_t *buf, size_t size) override; |
| 109 | + |
| 110 | + protected: |
| 111 | + |
| 112 | + virtual NetworkConnectionState update_handleInit () override; |
| 113 | + virtual NetworkConnectionState update_handleConnecting () override; |
| 114 | + virtual NetworkConnectionState update_handleConnected () override; |
| 115 | + virtual NetworkConnectionState update_handleDisconnecting() override; |
| 116 | + virtual NetworkConnectionState update_handleDisconnected () override; |
| 117 | + |
| 118 | + private: |
| 119 | + |
| 120 | + // Private members |
| 121 | + HardwareSerial * _serial; |
| 122 | + TwoWire * _wire; |
| 123 | + uint8_t * _inbound_buffer; |
| 124 | + uint32_t _conn_start_ms; |
| 125 | + uint32_t _i2c_address; |
| 126 | + uint32_t _i2c_max; |
| 127 | + uint32_t _uart_speed; |
| 128 | + uint32_t _inbound_buffer_index; |
| 129 | + uint32_t _inbound_buffer_size; |
| 130 | + bool _en_hw_int; |
| 131 | + TopicType _topic_type; |
| 132 | + Notecard _notecard; |
| 133 | + String _device_id; |
| 134 | + String _notecard_uid; |
| 135 | + String _notehub_url; |
| 136 | + String _project_uid; |
| 137 | + |
| 138 | + // Private methods |
| 139 | + bool armInterrupt (void) const; |
| 140 | + bool configureConnection (bool connect) const; |
| 141 | + uint_fast8_t connected (void) const; |
| 142 | + J * getNote (bool pop = false) const; |
| 143 | + int initiateNotehubSync (void) const; |
| 144 | + int notehubLogging (bool enable) const; |
| 145 | + bool updateUidCache (void); |
| 146 | +}; |
| 147 | + |
| 148 | +#endif /* #ifdef USE_NOTECARD */ |
| 149 | + |
| 150 | +#endif /* ARDUINO_NOTECARD_CONNECTION_HANDLER_H_ */ |
0 commit comments