@@ -467,13 +467,13 @@ void SFE_UBLOX_GPS::process(uint8_t incoming)
467
467
if (incoming == UBX_CLASS_ACK)
468
468
{
469
469
packetAck.counter = 0 ;
470
- packetAck.valid = false ;
470
+ packetAck.valid = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED ;
471
471
ubxFrameClass = CLASS_ACK;
472
472
}
473
473
else
474
474
{
475
475
packetCfg.counter = 0 ;
476
- packetCfg.valid = false ;
476
+ packetCfg.valid = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED ;
477
477
ubxFrameClass = CLASS_NOT_AN_ACK;
478
478
}
479
479
}
@@ -568,7 +568,7 @@ void SFE_UBLOX_GPS::processRTCM(uint8_t incoming)
568
568
}
569
569
570
570
// Given a character, file it away into the uxb packet structure
571
- // Set valid = true once sentence is completely received and passes CRC
571
+ // Set valid to VALID or NOT_VALID once sentence is completely received and passes or fails CRC
572
572
// The payload portion of the packet can be 100s of bytes but the max array
573
573
// size is roughly 64 bytes. startingSpot can be set so we only record
574
574
// a subset of bytes within a larger packet.
@@ -608,7 +608,7 @@ void SFE_UBLOX_GPS::processUBX(uint8_t incoming, ubxPacket *incomingUBX)
608
608
// Validate this sentence
609
609
if (incomingUBX->checksumA == rollingChecksumA && incomingUBX->checksumB == rollingChecksumB)
610
610
{
611
- incomingUBX->valid = true ;
611
+ incomingUBX->valid = SFE_UBLOX_PACKET_VALIDITY_VALID ;
612
612
613
613
if (_printDebug == true )
614
614
{
@@ -617,14 +617,14 @@ void SFE_UBLOX_GPS::processUBX(uint8_t incoming, ubxPacket *incomingUBX)
617
617
_debugSerial->print (F (" Received: " ));
618
618
printPacket (incomingUBX);
619
619
620
- if (packetCfg.valid == true )
620
+ if (packetCfg.valid == SFE_UBLOX_PACKET_VALIDITY_VALID )
621
621
{
622
622
if (_printDebug == true )
623
623
{
624
624
_debugSerial->println (F (" packetCfg now valid" ));
625
625
}
626
626
}
627
- if (packetAck.valid == true )
627
+ if (packetAck.valid == SFE_UBLOX_PACKET_VALIDITY_VALID )
628
628
{
629
629
if (_printDebug == true )
630
630
{
@@ -635,8 +635,10 @@ void SFE_UBLOX_GPS::processUBX(uint8_t incoming, ubxPacket *incomingUBX)
635
635
636
636
processUBXpacket (incomingUBX); // We've got a valid packet, now do something with it
637
637
}
638
- else
638
+ else // Checksum failure
639
639
{
640
+ incomingUBX->valid = SFE_UBLOX_PACKET_VALIDITY_NOT_VALID;
641
+
640
642
if (_printDebug == true )
641
643
{
642
644
// Drive an external pin to allow for easier logic analyzation
@@ -1062,18 +1064,30 @@ void SFE_UBLOX_GPS::printPacket(ubxPacket *packet)
1062
1064
// "not acknowledge"(UBX-ACK-NAK) message back to the sender, depending on whether or not the message was processed correctly.
1063
1065
// Some messages from other classes also use the same acknowledgement mechanism.
1064
1066
1065
- // If the packetCfg len is 1, then we are querying the device for data
1066
- // If the packetCfg len is >1, then we are sending a new setting
1067
+ // When we poll or get a setting, we will receive _both_ a config packet and an ACK
1068
+ // If the poll or get request is not valid, we will receive _only_ a NACK
1069
+
1070
+ // If we are trying to get or poll a setting, then packetCfg.len will be 0 or 1 when the packetCfg is _sent_.
1071
+ // If we poll the setting for a particular port using UBX-CFG-PRT then .len will be 1 initially
1072
+ // For all other gets or polls, .len will be 0 initially
1073
+ // (It would be possible for .len to be 2 _if_ we were using UBX-CFG-MSG to poll the settings for a particular message - but we don't use that (currently))
1074
+
1075
+ // If the get or poll _fails_, i.e. is NACK'd, then packetCfg.len could still be 0 or 1 after the NACK is received
1076
+ // But if the get or poll is ACK'd, then packetCfg.len will have been updated by the incoming data and will always be at least 2
1077
+
1078
+ // If we are going to set the value for a setting, then packetCfg.len will be at least 3 when the packetCfg is _sent_.
1079
+ // (UBX-CFG-MSG appears to have the shortest set length of 3 bytes)
1067
1080
1068
- // Returns true if we got the following:
1069
- // * If packetCfg len is 1 and we got and ACK and a valid packetCfg (module is responding with register content)
1070
- // * If packetCfg len is >1 and we got an ACK (no valid packetCfg needed, module absorbs new register data)
1071
- // Returns false if we timed out, got a NACK (command unknown), or had a CLS/ID mismatch
1081
+ // Returns SFE_UBLOX_STATUS_DATA_RECEIVED if we got an ACK and a valid packetCfg (module is responding with register content)
1082
+ // Returns SFE_UBLOX_STATUS_DATA_SENT if we got an ACK and no packetCfg (no valid packetCfg needed, module absorbs new register data)
1083
+ // Returns SFE_UBLOX_STATUS_FAIL if we got an invalid packetCfg (checksum failure)
1084
+ // Returns SFE_UBLOX_STATUS_COMMAND_UNKNOWN if we got a NACK (command unknown)
1085
+ // Returns SFE_UBLOX_STATUS_TIMEOUT if we timed out
1072
1086
sfe_ublox_status_e SFE_UBLOX_GPS::waitForACKResponse (uint8_t requestedClass, uint8_t requestedID, uint16_t maxTime)
1073
1087
{
1074
1088
commandAck = UBX_ACK_NONE; // Reset flag
1075
- packetCfg.valid = false ; // This will go true when we receive a response to the packet we sent
1076
- packetAck.valid = false ;
1089
+ packetCfg.valid = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED ; // This will go VALID (or NOT_VALID) when we receive a response to the packet we sent
1090
+ packetAck.valid = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED ;
1077
1091
1078
1092
unsigned long startTime = millis ();
1079
1093
while (millis () - startTime < maxTime)
@@ -1090,42 +1104,43 @@ sfe_ublox_status_e SFE_UBLOX_GPS::waitForACKResponse(uint8_t requestedClass, uin
1090
1104
_debugSerial->println (F (" msec" ));
1091
1105
}
1092
1106
1093
- // Are we expecting data back or just an ACK ?
1094
- if (packetCfg.len == 1 )
1107
+ // We've got the a valid ACK for this CLS/ID, so is packetCfg valid ?
1108
+ if (packetCfg.valid == SFE_UBLOX_PACKET_VALIDITY_VALID )
1095
1109
{
1096
- // We are expecting a data response so now we verify the response packet was valid
1097
- if (packetCfg.valid == true )
1110
+ // We've got a valid packetCfg, so does it match the requested Class and ID?
1111
+ if (packetCfg.cls == requestedClass && packetCfg. id == requestedID )
1098
1112
{
1099
- if (packetCfg.cls == requestedClass && packetCfg.id == requestedID)
1100
- {
1101
- if (_printDebug == true )
1102
- {
1103
- _debugSerial->print (F (" waitForACKResponse: CLS/ID match after " ));
1104
- _debugSerial->print (millis () - startTime);
1105
- _debugSerial->println (F (" msec" ));
1106
- }
1107
- return (SFE_UBLOX_STATUS_DATA_RECEIVED); // Received a data and a correct ACK!
1108
- }
1109
- else
1113
+ if (_printDebug == true )
1110
1114
{
1111
- // Reset packet and continue checking incoming data for matching cls/id
1112
- if (_printDebug == true )
1113
- {
1114
- _debugSerial->println (F (" waitForACKResponse: CLS/ID mismatch, continue to wait..." ));
1115
- }
1116
- packetCfg.valid = false ; // This will go true when we receive a response to the packet we sent
1115
+ _debugSerial->print (F (" waitForACKResponse: CLS/ID match after " ));
1116
+ _debugSerial->print (millis () - startTime);
1117
+ _debugSerial->println (F (" msec" ));
1117
1118
}
1119
+ return (SFE_UBLOX_STATUS_DATA_RECEIVED); // Received a data and a correct ACK!
1118
1120
}
1119
1121
else
1122
+ // The Class and/or ID don't match the requested ones, so keep trying...
1120
1123
{
1121
- // We were expecting data but didn't get a valid config packet
1124
+ // Reset packet and continue checking incoming data for matching cls/id
1122
1125
if (_printDebug == true )
1123
1126
{
1124
- _debugSerial->println (F (" waitForACKResponse: Invalid config packet " ));
1127
+ _debugSerial->println (F (" waitForACKResponse: CLS/ID mismatch, continue to wait... " ));
1125
1128
}
1126
- return (SFE_UBLOX_STATUS_FAIL); // We got an ACK, we're never going to get valid config data
1129
+ packetCfg.valid = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED; // This will go VALID (or NOT_VALID) when we receive a response to the packet we sent
1130
+ }
1131
+ }
1132
+ // If we received an invalid packetCfg (checksum failure) then we can't trust it, including its Class and ID
1133
+ else if (packetCfg.valid == SFE_UBLOX_PACKET_VALIDITY_NOT_VALID)
1134
+ {
1135
+ // We were expecting data but didn't get a valid config packet
1136
+ if (_printDebug == true )
1137
+ {
1138
+ _debugSerial->println (F (" waitForACKResponse: Invalid config packet" ));
1127
1139
}
1140
+ return (SFE_UBLOX_STATUS_FAIL); // We got a checksum failure, we're never going to get valid config data
1128
1141
}
1142
+ // We didn't receive a valid or invalid packetCfg, so we must have only received the ACK
1143
+ // Let's hope this was a set?
1129
1144
else
1130
1145
{
1131
1146
// We have sent new data. We expect an ACK but no return config packet.
@@ -1136,6 +1151,7 @@ sfe_ublox_status_e SFE_UBLOX_GPS::waitForACKResponse(uint8_t requestedClass, uin
1136
1151
return (SFE_UBLOX_STATUS_DATA_SENT); // New data successfully sent
1137
1152
}
1138
1153
}
1154
+ // Did we receive a NACK?
1139
1155
else if (commandAck == UBX_ACK_NACK)
1140
1156
{
1141
1157
if (_printDebug == true )
@@ -1153,7 +1169,7 @@ sfe_ublox_status_e SFE_UBLOX_GPS::waitForACKResponse(uint8_t requestedClass, uin
1153
1169
1154
1170
// TODO add check here if config went valid but we never got the following ack
1155
1171
// Through debug warning, This command might not get an ACK
1156
- if (packetCfg.valid == true )
1172
+ if (packetCfg.valid == SFE_UBLOX_PACKET_VALIDITY_VALID )
1157
1173
{
1158
1174
if (_printDebug == true )
1159
1175
{
@@ -1172,12 +1188,13 @@ sfe_ublox_status_e SFE_UBLOX_GPS::waitForACKResponse(uint8_t requestedClass, uin
1172
1188
}
1173
1189
1174
1190
// For non-CFG queries no ACK is sent so we use this function
1175
- // Returns true if we got a config packet full of response data that has CLS/ID match to our query packet
1176
- // Returns false if we timed out
1191
+ // Returns SFE_UBLOX_STATUS_DATA_RECEIVED if we got a config packet full of response data that has CLS/ID match to our query packet
1192
+ // Returns SFE_UBLOX_STATUS_CRC_FAIL if we got a corrupt config packet that has CLS/ID match to our query packet
1193
+ // Returns SFE_UBLOX_STATUS_TIMEOUT if we timed out
1177
1194
sfe_ublox_status_e SFE_UBLOX_GPS::waitForNoACKResponse (uint8_t requestedClass, uint8_t requestedID, uint16_t maxTime)
1178
1195
{
1179
- packetCfg.valid = false ; // This will go true when we receive a response to the packet we sent
1180
- packetAck.valid = false ;
1196
+ packetCfg.valid = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED ; // This will go VALID (or NOT_VALID) when we receive a response to the packet we sent
1197
+ packetAck.valid = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED ;
1181
1198
packetCfg.cls = 255 ;
1182
1199
packetCfg.id = 255 ;
1183
1200
@@ -1187,10 +1204,10 @@ sfe_ublox_status_e SFE_UBLOX_GPS::waitForNoACKResponse(uint8_t requestedClass, u
1187
1204
if (checkUblox () == true ) // See if new data is available. Process bytes as they come in.
1188
1205
{
1189
1206
// Did we receive a config packet that matches the cls/id we requested?
1190
- if (packetCfg.cls == requestedClass && packetCfg.id == requestedID)
1207
+ if (( packetCfg.cls == requestedClass) && ( packetCfg.id == requestedID) && (packetCfg. valid != SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED) )
1191
1208
{
1192
1209
// This packet might be good or it might be CRC corrupt
1193
- if (packetCfg.valid == true )
1210
+ if (packetCfg.valid == SFE_UBLOX_PACKET_VALIDITY_VALID )
1194
1211
{
1195
1212
if (_printDebug == true )
1196
1213
{
@@ -1200,7 +1217,7 @@ sfe_ublox_status_e SFE_UBLOX_GPS::waitForNoACKResponse(uint8_t requestedClass, u
1200
1217
}
1201
1218
return (SFE_UBLOX_STATUS_DATA_RECEIVED); // We have new data to act upon
1202
1219
}
1203
- else
1220
+ else // if (packetCfg.valid == SFE_UBLOX_PACKET_VALIDITY_NOT_VALID)
1204
1221
{
1205
1222
if (_printDebug == true )
1206
1223
{
@@ -1209,8 +1226,9 @@ sfe_ublox_status_e SFE_UBLOX_GPS::waitForNoACKResponse(uint8_t requestedClass, u
1209
1226
return (SFE_UBLOX_STATUS_CRC_FAIL); // We got the right packet but it was corrupt
1210
1227
}
1211
1228
}
1212
- else if (packetCfg.cls < 255 && packetCfg.id < 255 )
1229
+ else if (( packetCfg.cls < 255 ) && ( packetCfg.id < 255 ) && (packetCfg. valid != SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED) )
1213
1230
{
1231
+ // We got a valid or invalid packet but it was not the droid we were looking for
1214
1232
// Reset packet and continue checking incoming data for matching cls/id
1215
1233
if (_printDebug == true )
1216
1234
{
@@ -1222,7 +1240,7 @@ sfe_ublox_status_e SFE_UBLOX_GPS::waitForNoACKResponse(uint8_t requestedClass, u
1222
1240
_debugSerial->println ();
1223
1241
}
1224
1242
1225
- packetCfg.valid = false ; // This will go true when we receive a response to the packet we sent
1243
+ packetCfg.valid = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED ; // This will go VALID (or NOT_VALID) when we receive a response to the packet we sent
1226
1244
packetCfg.cls = 255 ;
1227
1245
packetCfg.id = 255 ;
1228
1246
}
0 commit comments