@@ -383,8 +383,14 @@ bool cbSignalMsg(pb_istream_t *stream, const pb_field_t *field, void **arg) {
383
383
// decode each ConfigurePinRequest sub-message
384
384
if (!pb_decode (stream, wippersnapper_pin_v1_ConfigurePinRequests_fields,
385
385
&msg)) {
386
- WS_DEBUG_PRINTLN (" ERROR: Could not decode CreateSign2alRequest " )
386
+ WS_DEBUG_PRINTLN (" ERROR: Could not decode CreateSignalRequest " )
387
387
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 ;
388
394
}
389
395
} else if (field->tag ==
390
396
wippersnapper_signal_v1_CreateSignalRequest_pin_events_tag) {
@@ -503,11 +509,12 @@ bool Wippersnapper::encodePinEvent(
503
509
/* *************************************************************************/
504
510
/* !
505
511
@brief Called when broker responds to a device's publish across
506
- the registration topic.
512
+ the registration topic.
507
513
*/
508
514
/* *************************************************************************/
509
515
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);
511
518
}
512
519
513
520
/* *************************************************************************/
@@ -734,6 +741,18 @@ bool Wippersnapper::buildWSTopics() {
734
741
strlen (_device_uid) + strlen (TOPIC_DESCRIPTION) + strlen (" status" ) +
735
742
strlen (" broker" ) + 1 );
736
743
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
+
737
756
// Topic for signals from device to broker
738
757
WS._topic_signal_device = (char *)malloc (
739
758
sizeof (char ) * strlen (WS._username ) + +strlen (" /" ) + strlen (_device_uid) +
@@ -768,6 +787,19 @@ bool Wippersnapper::buildWSTopics() {
768
787
is_success = false ;
769
788
}
770
789
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
+
771
803
// Create device-to-broker signal topic
772
804
if (WS._topic_signal_device ) {
773
805
strcpy (WS._topic_signal_device , WS._username );
@@ -780,6 +812,18 @@ bool Wippersnapper::buildWSTopics() {
780
812
is_success = false ;
781
813
}
782
814
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
+
783
827
// Create broker-to-device signal topic
784
828
if (WS._topic_signal_brkr ) {
785
829
strcpy (WS._topic_signal_brkr , WS._username );
@@ -892,20 +936,27 @@ void Wippersnapper::haltError(String error) {
892
936
893
937
/* *************************************************************************/
894
938
/* !
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.
898
940
@returns True if successful, False otherwise.
899
941
*/
900
942
/* *************************************************************************/
901
- bool Wippersnapper::registerBoard (uint8_t retries = 10 ) {
943
+ bool Wippersnapper::registerBoard () {
902
944
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 ;
909
960
}
910
961
911
962
/* *************************************************************************/
@@ -1034,17 +1085,60 @@ void Wippersnapper::connect() {
1034
1085
setStatusLEDColor (LED_CONNECTED);
1035
1086
1036
1087
// Register hardware with Wippersnapper
1037
- WS_DEBUG_PRINTLN (" Registering Board ..." )
1088
+ WS_DEBUG_PRINTLN (" Registering hardware with WipperSnapper ..." )
1038
1089
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." );
1041
1093
}
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 ();
1043
1108
1044
- WS_DEBUG_PRINTLN ( " Registered board with Wippersnapper. " );
1109
+ // Run application
1045
1110
statusLEDBlink (WS_LED_STATUS_CONNECTED);
1111
+ WS_DEBUG_PRINTLN (
1112
+ " Registration and configuration complete!\n Running application..." );
1113
+ }
1046
1114
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!" );
1048
1142
}
1049
1143
1050
1144
/* *************************************************************************/
@@ -1055,12 +1149,8 @@ void Wippersnapper::connect() {
1055
1149
/* *************************************************************************/
1056
1150
ws_status_t Wippersnapper::run () {
1057
1151
// Check networking
1058
- // TODO: Handle MQTT errors within the new netcode, or bring them outwards to
1059
- // another fsm
1060
1152
runNetFSM ();
1061
1153
feedWDT ();
1062
-
1063
- // keepalive
1064
1154
pingBroker ();
1065
1155
1066
1156
// Process all incoming packets from Wippersnapper MQTT Broker
0 commit comments