28
28
#include < algorithm>
29
29
#include < functional>
30
30
31
+ #include < Arduino.h>
31
32
#include < Arduino_NotecardConnectionHandler.h>
32
33
33
34
#include " cbor/CBOREncoder.h"
@@ -79,9 +80,6 @@ ArduinoIoTCloudNotecard::ArduinoIoTCloudNotecard()
79
80
,_notecard_read_interval_ms{DEFAULT_READ_INTERVAL_MS}
80
81
,_interrupt_pin{-1 }
81
82
,_data_available{false }
82
- #if defined(BOARD_HAS_SECRET_KEY)
83
- ,_secret_device_key{" " }
84
- #endif /* BOARD_HAS_SECRET_KEY */
85
83
#if OTA_ENABLED
86
84
,_ota_cap{false }
87
85
,_ota_error{static_cast <int >(OTAError::None)}
@@ -99,33 +97,36 @@ ArduinoIoTCloudNotecard::ArduinoIoTCloudNotecard()
99
97
* PUBLIC MEMBER FUNCTIONS
100
98
******************************************************************************/
101
99
102
- int ArduinoIoTCloudNotecard::begin (ConnectionHandler &connection , int interrupt_pin )
100
+ int ArduinoIoTCloudNotecard::begin (ConnectionHandler &connection_ , int interrupt_pin_ )
103
101
{
104
- _connection = &connection;
102
+ _connection = &connection_;
103
+ NotecardConnectionHandler *notecard_connection = reinterpret_cast <NotecardConnectionHandler *>(_connection);
104
+
105
+ // Configure the interrupt pin
106
+ if (interrupt_pin_ >= 0 ) {
107
+ _interrupt_pin = interrupt_pin_;
108
+ ::pinMode (_interrupt_pin, INPUT);
109
+ ::attachInterrupt (digitalPinToInterrupt(_interrupt_pin), ISR_dataAvailable, RISING);
110
+ notecard_connection->enableHardwareInterrupts ();
111
+ }
112
+
113
+ // Initialize the connection to the Notecard
105
114
if (NetworkConnectionState::ERROR == _connection->check ()) {
106
115
DEBUG_ERROR (" ArduinoIoTCloudNotecard::%s encountered fatal connection error!" , __FUNCTION__);
107
116
return 0 ; // (false -> failure)
108
117
}
109
118
110
- #ifdef BOARD_HAS_SECRET_KEY
111
- // Update Device ID using connection cache
112
- setBoardId (getDeviceId ());
113
- #endif
114
-
115
- // Configure the interrupt pin
116
- if (interrupt_pin >= 0 ) {
117
- ::pinMode (interrupt_pin, INPUT);
118
- ::attachInterrupt (digitalPinToInterrupt(interrupt_pin), ISR_dataAvailable, RISING);
119
- _interrupt_pin = interrupt_pin;
120
- }
119
+ // TODO: Remove if not needed
120
+ // Pull the Arduino IoT Cloud Device ID from the Notecard
121
+ setDeviceId (notecard_connection->getDeviceId ());
121
122
122
123
#if OTA_ENABLED
123
124
/* Setup OTA TLS client */
124
125
_otaClient.begin (connection);
125
126
#endif
126
127
127
128
// Begin the Notecard time service
128
- _time_service.begin (&connection );
129
+ _time_service.begin (&connection_ );
129
130
130
131
/* Setup retry timers */
131
132
_connection_attempt.begin (AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);
@@ -148,6 +149,7 @@ void ArduinoIoTCloudNotecard::printDebugInfo()
148
149
NetworkConnectionState conn_state = _connection->check ();
149
150
DEBUG_INFO (" ***** Arduino IoT Cloud Notecard - configuration info *****" );
150
151
DEBUG_INFO (" Notecard UID: %s" , reinterpret_cast <NotecardConnectionHandler *>(_connection)->getNotecardUid ().c_str ());
152
+ // TODO: Remove if not needed
151
153
DEBUG_INFO (" Arduino Device ID: %s" , getDeviceId ().c_str ());
152
154
if (NetworkConnectionState::CONNECTED == conn_state)
153
155
{
@@ -167,7 +169,6 @@ void ArduinoIoTCloudNotecard::update()
167
169
{
168
170
case State::ConnectPhy: next_state = handle_ConnectPhy (); break ;
169
171
case State::SyncTime: next_state = handle_SyncTime (); break ;
170
- case State::ConfigureNotehub: next_state = handle_ConfigureNotehub (); break ;
171
172
case State::Connected: next_state = handle_Connected (); break ;
172
173
case State::Disconnect: next_state = handle_Disconnect (); break ;
173
174
}
@@ -199,39 +200,20 @@ ArduinoIoTCloudNotecard::State ArduinoIoTCloudNotecard::handle_SyncTime()
199
200
if (TimeServiceClass::isTimeValid (current_time))
200
201
{
201
202
DEBUG_VERBOSE (" ArduinoIoTCloudNotecard::%s internal clock configured to posix timestamp %d" , __FUNCTION__, current_time);
202
- return State::ConfigureNotehub ;
203
+ return State::Connected ;
203
204
}
204
205
205
206
DEBUG_ERROR (" ArduinoIoTCloudNotecard::%s could not get valid time. Retrying now." , __FUNCTION__);
206
207
return State::ConnectPhy;
207
208
}
208
209
209
- ArduinoIoTCloudNotecard::State ArduinoIoTCloudNotecard::handle_ConfigureNotehub ()
210
- {
211
- if (!connected ())
212
- {
213
- DEBUG_ERROR (" ArduinoIoTCloudNotecard::%s connection to Notehub lost" , __FUNCTION__);
214
- return State::ConnectPhy;
215
- }
216
-
217
- #if defined(BOARD_HAS_SECRET_KEY)
218
- if (reinterpret_cast <NotecardConnectionHandler *>(_connection)->syncSecretDeviceKey (_secret_device_key)) {
219
- DEBUG_WARNING (" ArduinoIoTCloudNotecard::%s failed to set secret device key" , __FUNCTION__);
220
- DEBUG_WARNING (" You may manually enter the secret key on Notehub as a device level environment variable named `arduino_iot_cloud_secret_device_key`" );
221
- }
222
- #endif
223
-
224
- return State::Connected;
225
- }
226
-
227
210
ArduinoIoTCloudNotecard::State ArduinoIoTCloudNotecard::handle_Connected ()
228
211
{
229
212
if (!connected () || !_thing.connected () || !_device.connected ())
230
213
{
231
214
return State::Disconnect;
232
215
}
233
216
234
- // TODO: Understand how available applies to the _device.update() and _thing.update()
235
217
/* Poll Notecard for new messages */
236
218
pollNotecard ();
237
219
0 commit comments