Skip to content

Commit 6d0bb46

Browse files
authored
Merge pull request #147 from adafruit/fix-reg-polling-2
Registration workflow refactoring and bugfixes
2 parents a2c8634 + 52a7915 commit 6d0bb46

File tree

8 files changed

+301
-322
lines changed

8 files changed

+301
-322
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ PROJECT_VER_MAJOR := 1
77
PROJECT_VER_MINOR := 0
88
PROJECT_VER_PATCH := 0
99
PROJECT_VER_BUILD := beta
10-
PROJECT_VER_BUILD_NUM := 8
10+
PROJECT_VER_BUILD_NUM := 9
1111

1212
BOARD_PYPORTAL := samd51-pyportal
1313
BOARD_METRO_AIRLIFT := samd51-metro-airlift

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Adafruit WipperSnapper
2-
version=1.0.0-beta.8
2+
version=1.0.0-beta.9
33
author=Adafruit
44
maintainer=Adafruit <[email protected]>
55
sentence=Arduino library to access WipperSnapper

src/.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"C_Cpp.dimInactiveRegions": false,
3+
"files.associations": {
4+
"__config": "cpp"
5+
}
6+
}

src/Wippersnapper.cpp

Lines changed: 113 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,14 @@ bool cbSignalMsg(pb_istream_t *stream, const pb_field_t *field, void **arg) {
383383
// decode each ConfigurePinRequest sub-message
384384
if (!pb_decode(stream, wippersnapper_pin_v1_ConfigurePinRequests_fields,
385385
&msg)) {
386-
WS_DEBUG_PRINTLN("ERROR: Could not decode CreateSign2alRequest")
386+
WS_DEBUG_PRINTLN("ERROR: Could not decode CreateSignalRequest")
387387
is_success = false;
388+
WS.pinCfgCompleted = false;
389+
}
390+
// If this is the initial configuration
391+
if (!WS.pinCfgCompleted) {
392+
WS_DEBUG_PRINTLN("Initial Pin Configuration Complete!");
393+
WS.pinCfgCompleted = true;
388394
}
389395
} else if (field->tag ==
390396
wippersnapper_signal_v1_CreateSignalRequest_pin_events_tag) {
@@ -503,11 +509,12 @@ bool Wippersnapper::encodePinEvent(
503509
/**************************************************************************/
504510
/*!
505511
@brief Called when broker responds to a device's publish across
506-
the registration topic.
512+
the registration topic.
507513
*/
508514
/**************************************************************************/
509515
void cbRegistrationStatus(char *data, uint16_t len) {
510-
WS._registerBoard->decodeRegMsg(data, len);
516+
// call decoder for registration response msg
517+
WS.decodeRegistrationResp(data, len);
511518
}
512519

513520
/**************************************************************************/
@@ -734,6 +741,18 @@ bool Wippersnapper::buildWSTopics() {
734741
strlen(_device_uid) + strlen(TOPIC_DESCRIPTION) + strlen("status") +
735742
strlen("broker") + 1);
736743

744+
// Registration status completion topic
745+
WS._topic_description_status_complete = (char *)malloc(
746+
sizeof(char) * strlen(WS._username) + +strlen("/wprsnpr/") +
747+
strlen(_device_uid) + strlen(TOPIC_DESCRIPTION) + strlen("status") +
748+
strlen("/device/complete") + 1);
749+
750+
// Topic to signal pin configuration complete from device to broker
751+
WS._topic_device_pin_config_complete = (char *)malloc(
752+
sizeof(char) * strlen(WS._username) + +strlen("/") + strlen(_device_uid) +
753+
strlen("/wprsnpr/") + strlen(TOPIC_SIGNALS) +
754+
strlen("device/pinConfigComplete") + 1);
755+
737756
// Topic for signals from device to broker
738757
WS._topic_signal_device = (char *)malloc(
739758
sizeof(char) * strlen(WS._username) + +strlen("/") + strlen(_device_uid) +
@@ -768,6 +787,19 @@ bool Wippersnapper::buildWSTopics() {
768787
is_success = false;
769788
}
770789

790+
// Create registration status complete topic
791+
if (WS._topic_description_status_complete) {
792+
strcpy(WS._topic_description_status_complete, WS._username);
793+
strcat(WS._topic_description_status_complete, "/wprsnpr/");
794+
strcat(WS._topic_description_status_complete, _device_uid);
795+
strcat(WS._topic_description_status_complete, TOPIC_DESCRIPTION);
796+
strcat(WS._topic_description_status_complete, "status");
797+
strcat(WS._topic_description_status_complete, "/device/complete");
798+
} else { // malloc failed
799+
WS._topic_description_status_complete = 0;
800+
is_success = false;
801+
}
802+
771803
// Create device-to-broker signal topic
772804
if (WS._topic_signal_device) {
773805
strcpy(WS._topic_signal_device, WS._username);
@@ -780,6 +812,18 @@ bool Wippersnapper::buildWSTopics() {
780812
is_success = false;
781813
}
782814

815+
// Create device-to-broker signal topic
816+
if (WS._topic_device_pin_config_complete) {
817+
strcpy(WS._topic_device_pin_config_complete, WS._username);
818+
strcat(WS._topic_device_pin_config_complete, "/wprsnpr/");
819+
strcat(WS._topic_device_pin_config_complete, _device_uid);
820+
strcat(WS._topic_device_pin_config_complete, TOPIC_SIGNALS);
821+
strcat(WS._topic_device_pin_config_complete, "device/pinConfigComplete");
822+
} else { // malloc failed
823+
WS._topic_device_pin_config_complete = 0;
824+
is_success = false;
825+
}
826+
783827
// Create broker-to-device signal topic
784828
if (WS._topic_signal_brkr) {
785829
strcpy(WS._topic_signal_brkr, WS._username);
@@ -892,20 +936,27 @@ void Wippersnapper::haltError(String error) {
892936

893937
/**************************************************************************/
894938
/*!
895-
@brief Sends board description message to Wippersnapper
896-
@param retries
897-
Amount of times to retry registration process.
939+
@brief Attempts to register hardware with Adafruit.io WipperSnapper.
898940
@returns True if successful, False otherwise.
899941
*/
900942
/**************************************************************************/
901-
bool Wippersnapper::registerBoard(uint8_t retries = 10) {
943+
bool Wippersnapper::registerBoard() {
902944
bool is_success = false;
903-
WS_DEBUG_PRINTLN("registerBoard()");
904-
// Create new board
905-
_registerBoard = new Wippersnapper_Registration();
906-
// Run the FSM for the registration process
907-
is_success = _registerBoard->processRegistration();
908-
return is_success;
945+
WS_DEBUG_PRINTLN("Registering hardware with IO...");
946+
947+
// Encode and publish registration request message to broker
948+
runNetFSM();
949+
feedWDT();
950+
WS_DEBUG_PRINT("Encoding registration request...");
951+
if (!encodePubRegistrationReq())
952+
return false;
953+
954+
// Blocking, attempt to obtain broker's response message
955+
runNetFSM();
956+
feedWDT();
957+
pollRegistrationResp();
958+
959+
return true;
909960
}
910961

911962
/**************************************************************************/
@@ -1034,17 +1085,60 @@ void Wippersnapper::connect() {
10341085
setStatusLEDColor(LED_CONNECTED);
10351086

10361087
// Register hardware with Wippersnapper
1037-
WS_DEBUG_PRINTLN("Registering Board...")
1088+
WS_DEBUG_PRINTLN("Registering hardware with WipperSnapper...")
10381089
setStatusLEDColor(LED_IO_REGISTER_HW);
1039-
if (!registerBoard(10)) {
1040-
haltError("Unable to register board with Wippersnapper.");
1090+
1091+
if (!registerBoard()) {
1092+
haltError("Unable to register with WipperSnapper.");
10411093
}
1042-
feedWDT(); // Hardware registered with IO, feed WDT
1094+
feedWDT(); // Hardware registered with IO, feed WDT */
1095+
WS_DEBUG_PRINTLN("Registered with WipperSnapper.");
1096+
1097+
WS_DEBUG_PRINTLN("Polling for configuration message...");
1098+
WS._mqtt->processPackets(2000);
1099+
1100+
// did we get and process the registration message?
1101+
if (!WS.pinCfgCompleted)
1102+
haltError("Did not get configuration message from broker, resetting...");
1103+
1104+
// Publish that we have completed the configuration workflow
1105+
feedWDT();
1106+
runNetFSM();
1107+
publishPinConfigComplete();
10431108

1044-
WS_DEBUG_PRINTLN("Registered board with Wippersnapper.");
1109+
// Run application
10451110
statusLEDBlink(WS_LED_STATUS_CONNECTED);
1111+
WS_DEBUG_PRINTLN(
1112+
"Registration and configuration complete!\nRunning application...");
1113+
}
10461114

1047-
WS._mqtt->processPackets(1000);
1115+
void Wippersnapper::publishPinConfigComplete() {
1116+
// Publish that we've set up the pins and are ready to run
1117+
wippersnapper_signal_v1_SignalResponse msg =
1118+
wippersnapper_signal_v1_SignalResponse_init_zero;
1119+
msg.which_payload =
1120+
wippersnapper_signal_v1_SignalResponse_configuration_complete_tag;
1121+
msg.payload.configuration_complete = true;
1122+
1123+
// encode registration request message
1124+
uint8_t _message_buffer[128];
1125+
pb_ostream_t _msg_stream =
1126+
pb_ostream_from_buffer(_message_buffer, sizeof(_message_buffer));
1127+
1128+
bool _status =
1129+
pb_encode(&_msg_stream,
1130+
wippersnapper_description_v1_RegistrationComplete_fields, &msg);
1131+
size_t _message_len = _msg_stream.bytes_written;
1132+
1133+
// verify message encoded correctly
1134+
if (!_status)
1135+
haltError("Could not encode, resetting...");
1136+
1137+
// Publish message
1138+
WS_DEBUG_PRINTLN("Publishing to pin config complete...");
1139+
WS.publish(WS._topic_device_pin_config_complete, _message_buffer,
1140+
_message_len, 1);
1141+
WS_DEBUG_PRINTLN("Completed registration process, configuration next!");
10481142
}
10491143

10501144
/**************************************************************************/
@@ -1055,12 +1149,8 @@ void Wippersnapper::connect() {
10551149
/**************************************************************************/
10561150
ws_status_t Wippersnapper::run() {
10571151
// Check networking
1058-
// TODO: Handle MQTT errors within the new netcode, or bring them outwards to
1059-
// another fsm
10601152
runNetFSM();
10611153
feedWDT();
1062-
1063-
// keepalive
10641154
pingBroker();
10651155

10661156
// Process all incoming packets from Wippersnapper MQTT Broker

src/Wippersnapper.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
// Wippersnapper API Helpers
3232
#include "Wippersnapper_Boards.h"
33-
#include "Wippersnapper_Registration.h"
3433
#include "components/statusLED/Wippersnapper_StatusLED_Colors.h"
3534

3635
// Wippersnapper GPIO Components
@@ -49,9 +48,10 @@
4948
#include "Adafruit_SleepyDog.h"
5049
#endif
5150

52-
// Uncomment to use the staging IO server staging builds
53-
// #define IO_MQTT_SERVER "io.adafruit.us" ///< Adafruit IO MQTT Server
54-
// (Staging)
51+
// Uncomment the following use the staging IO server //
52+
//#define USE_STAGING
53+
//#define IO_MQTT_SERVER "io.adafruit.us" ///< Adafruit IO MQTT Server
54+
5555
#define IO_MQTT_SERVER \
5656
"io.adafruit.com" ///< Adafruit IO MQTT Server (Production)
5757

@@ -64,7 +64,7 @@
6464
#endif
6565

6666
#define WS_VERSION \
67-
"1.0.0-beta.8" ///< WipperSnapper app. version (semver-formatted)
67+
"1.0.0-beta.9" ///< WipperSnapper app. version (semver-formatted)
6868

6969
// Reserved Adafruit IO MQTT topics
7070
#define TOPIC_IO_THROTTLE "/throttle" ///< Adafruit IO Throttle MQTT Topic
@@ -154,9 +154,8 @@ typedef enum {
154154
4000 ///< Session keepalive interval time, in milliseconds
155155

156156
#define WS_MQTT_MAX_PAYLOAD_SIZE \
157-
256 ///< MAXIMUM expected payload size, in bytes
157+
300 ///< MAXIMUM expected payload size, in bytes
158158

159-
class Wippersnapper_Registration;
160159
class Wippersnapper_DigitalGPIO;
161160
class Wippersnapper_AnalogIO;
162161
class Wippersnapper_FS;
@@ -207,8 +206,13 @@ class Wippersnapper {
207206
bool buildErrorTopics();
208207
void subscribeErrorTopics();
209208

210-
// Performs board registration FSM
211-
bool registerBoard(uint8_t retries);
209+
// Registration API
210+
bool registerBoard();
211+
bool encodePubRegistrationReq();
212+
void decodeRegistrationResp(char *data, uint16_t len);
213+
void pollRegistrationResp();
214+
// Configuration API
215+
void publishPinConfigComplete();
212216

213217
// run() loop
214218
ws_status_t run();
@@ -249,8 +253,6 @@ class Wippersnapper {
249253

250254
ws_board_status_t _boardStatus; ///< Hardware's registration status
251255

252-
Wippersnapper_Registration *_registerBoard =
253-
NULL; ///< Instance of registration class
254256
Wippersnapper_DigitalGPIO *_digitalGPIO; ///< Instance of digital gpio class
255257
Wippersnapper_AnalogIO *_analogIO; ///< Instance of analog io class
256258
Wippersnapper_FS *_fileSystem; ///< Instance of filesystem class
@@ -280,6 +282,8 @@ class Wippersnapper {
280282
wippersnapper_signal_v1_CreateSignalRequest
281283
_incomingSignalMsg; /*!< Incoming signal message from broker */
282284

285+
bool pinCfgCompleted = false;
286+
283287
private:
284288
void _init();
285289

@@ -299,7 +303,9 @@ class Wippersnapper {
299303
// MQTT topics
300304
char *_topic_description_status; /*!< MQTT subtopic carrying the description
301305
status resp. from the broker */
302-
char *_topic_signal_brkr; /*!< Wprsnpr->Device messages */
306+
char *_topic_description_status_complete;
307+
char *_topic_device_pin_config_complete;
308+
char *_topic_signal_brkr; /*!< Wprsnpr->Device messages */
303309

304310
Adafruit_MQTT_Subscribe
305311
*_topic_description_sub; /*!< Subscription for registration topic. */

0 commit comments

Comments
 (0)