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