Skip to content

Commit a1ca3d4

Browse files
committed
Removed classAndIdMatch flag on packet buffers. Gradually moving state management out of the process and processUBX functions.
1 parent a92139c commit a1ca3d4

File tree

2 files changed

+17
-118
lines changed

2 files changed

+17
-118
lines changed

src/SparkFun_u-blox_GNSS_Arduino_Library.cpp

Lines changed: 13 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,7 +1625,7 @@ void SFE_UBLOX_GNSS::processUBX(uint8_t incoming, ubxPacket *incomingUBX, uint8_
16251625
else if (checkAutomatic(incomingUBX->cls, incomingUBX->id))
16261626
{
16271627
// This isn't the message we are looking for...
1628-
// Let's say so and leave incomingUBX->classAndIDmatch _unchanged_
1628+
// Let's say so...
16291629
if (_printDebug == true)
16301630
{
16311631
_debugSerial->print(F("processUBX: incoming \"automatic\" message: Class: 0x"));
@@ -1654,20 +1654,6 @@ void SFE_UBLOX_GNSS::processUBX(uint8_t incoming, ubxPacket *incomingUBX, uint8_
16541654
{
16551655
incomingUBX->valid = SFE_UBLOX_PACKET_VALIDITY_NOT_VALID;
16561656

1657-
// Let's check if the class and ID match the requestedClass and requestedID.
1658-
// This is potentially risky as we are saying that we saw the requested Class and ID
1659-
// but that the packet checksum failed. Potentially it could be the class or ID bytes
1660-
// that caused the checksum error!
1661-
if ((incomingUBX->cls == requestedClass) && (incomingUBX->id == requestedID))
1662-
{
1663-
incomingUBX->classAndIDmatch = SFE_UBLOX_PACKET_VALIDITY_NOT_VALID; // If we have a match, set the classAndIDmatch flag to not valid
1664-
}
1665-
// If this is an ACK then let's check if the class and ID match the requestedClass and requestedID
1666-
else if ((incomingUBX->cls == UBX_CLASS_ACK) && (incomingUBX->payload[0] == requestedClass) && (incomingUBX->payload[1] == requestedID))
1667-
{
1668-
incomingUBX->classAndIDmatch = SFE_UBLOX_PACKET_VALIDITY_NOT_VALID; // If we have a match, set the classAndIDmatch flag to not valid
1669-
}
1670-
16711657
if ((_printDebug == true) || (_printLimitedDebug == true)) // This is important. Print this if doing limited debugging
16721658
{
16731659
//Drive an external pin to allow for easier logic analyzation
@@ -2987,10 +2973,6 @@ sfe_ublox_status_e SFE_UBLOX_GNSS::waitForACKResponse(ubxPacket *outgoingUBX, ui
29872973
packetAck.valid = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED;
29882974
packetBuf.valid = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED;
29892975
packetAuto.valid = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED;
2990-
outgoingUBX->classAndIDmatch = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED; // This will go VALID (or NOT_VALID) when we receive a packet that matches the requested class and ID
2991-
packetAck.classAndIDmatch = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED;
2992-
packetBuf.classAndIDmatch = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED;
2993-
packetAuto.classAndIDmatch = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED;
29942976

29952977
unsigned long startTime = millis();
29962978
while (millis() - startTime < maxTime)
@@ -3022,12 +3004,6 @@ sfe_ublox_status_e SFE_UBLOX_GNSS::waitForACKResponse(ubxPacket *outgoingUBX, ui
30223004
}
30233005
return (SFE_UBLOX_STATUS_DATA_RECEIVED); //We received valid data and a correct ACK!
30243006
}
3025-
3026-
// We can be confident that the data packet (if we are going to get one) will always arrive
3027-
// before the matching ACK. So if we sent a config packet which only produces an ACK
3028-
// then outgoingUBX->classAndIDmatch will be NOT_DEFINED and the packetAck.classAndIDmatch will VALID.
3029-
// We should not check outgoingUBX->valid, outgoingUBX->cls or outgoingUBX->id
3030-
// as these may have been changed by an automatic packet.
30313007
else if (!packetCfg.isClassAndIdMatch(requestedClass, requestedID) && packetAck.isClassAndIdMatch(requestedClass, requestedID))
30323008
{
30333009
if (_printDebug == true)
@@ -3039,27 +3015,9 @@ sfe_ublox_status_e SFE_UBLOX_GNSS::waitForACKResponse(ubxPacket *outgoingUBX, ui
30393015
return (SFE_UBLOX_STATUS_DATA_SENT); //We got an ACK but no data...
30403016
}
30413017

3042-
// If both the outgoingUBX->classAndIDmatch and packetAck.classAndIDmatch are VALID
3043-
// but the outgoingUBX->cls or ID no longer match then we can be confident that we had
3044-
// valid data but it has been or is currently being overwritten by an automatic packet (e.g. PVT).
3045-
// If (e.g.) a PVT packet is _being_ received: outgoingUBX->valid will be NOT_DEFINED
3046-
// If (e.g.) a PVT packet _has been_ received: outgoingUBX->valid will be VALID (or just possibly NOT_VALID)
3047-
// So we cannot use outgoingUBX->valid as part of this check.
3048-
// Note: the addition of packetBuf should make this check redundant!
3049-
else if ((outgoingUBX->classAndIDmatch == SFE_UBLOX_PACKET_VALIDITY_VALID) && (packetAck.classAndIDmatch == SFE_UBLOX_PACKET_VALIDITY_VALID) && ((outgoingUBX->cls != requestedClass) || (outgoingUBX->id != requestedID)))
3050-
{
3051-
if (_printDebug == true)
3052-
{
3053-
_debugSerial->print(F("waitForACKResponse: data being OVERWRITTEN after "));
3054-
_debugSerial->print(millis() - startTime);
3055-
_debugSerial->println(F(" msec"));
3056-
}
3057-
return (SFE_UBLOX_STATUS_DATA_OVERWRITTEN); // Data was valid but has been or is being overwritten
3058-
}
3059-
3060-
// If packetAck.classAndIDmatch is VALID but both outgoingUBX->valid and outgoingUBX->classAndIDmatch
3018+
// If packetAck is for requested class/ID but both outgoingUBX->valid and packetCfg isClassAndIDmatch
30613019
// are NOT_VALID then we can be confident we have had a checksum failure on the data packet
3062-
else if ((packetAck.classAndIDmatch == SFE_UBLOX_PACKET_VALIDITY_VALID) && (outgoingUBX->classAndIDmatch == SFE_UBLOX_PACKET_VALIDITY_NOT_VALID) && (outgoingUBX->valid == SFE_UBLOX_PACKET_VALIDITY_NOT_VALID))
3020+
else if ((packetAck.isAckForClassAndId(requestedClass, requestedID) && (packetCfg.valid == SFE_UBLOX_PACKET_VALIDITY_NOT_VALID)))
30633021
{
30643022
if (_printDebug == true)
30653023
{
@@ -3069,14 +3027,6 @@ sfe_ublox_status_e SFE_UBLOX_GNSS::waitForACKResponse(ubxPacket *outgoingUBX, ui
30693027
}
30703028
return (SFE_UBLOX_STATUS_CRC_FAIL); //Checksum fail
30713029
}
3072-
3073-
// If our packet was not-acknowledged (NACK) we do not receive a data packet - we only get the NACK.
3074-
// So you would expect outgoingUBX->valid and outgoingUBX->classAndIDmatch to still be NOT_DEFINED
3075-
// But if a full PVT packet arrives afterwards outgoingUBX->valid could be VALID (or just possibly NOT_VALID)
3076-
// but outgoingUBX->cls and outgoingUBX->id would not match...
3077-
// So I think this is telling us we need a special state for packetAck.classAndIDmatch to tell us
3078-
// the packet was definitely NACK'd otherwise we are possibly just guessing...
3079-
// Note: the addition of packetBuf changes the logic of this, but we'll leave the code as is for now.
30803030
else if (packetAck.isNackForClassAndId(requestedClass, requestedID))
30813031
{
30823032
if (_printDebug == true)
@@ -3088,10 +3038,10 @@ sfe_ublox_status_e SFE_UBLOX_GNSS::waitForACKResponse(ubxPacket *outgoingUBX, ui
30883038
return (SFE_UBLOX_STATUS_COMMAND_NACK); //We received a NACK!
30893039
}
30903040

3091-
// If the outgoingUBX->classAndIDmatch is VALID but the packetAck.classAndIDmatch is NOT_VALID
3041+
// If the packetCfg.isClassAndIdMatch but the packetAck is not for this class/ID
30923042
// then the ack probably had a checksum error. We will take a gamble and return DATA_RECEIVED.
30933043
// If we were playing safe, we should return FAIL instead
3094-
else if ((outgoingUBX->classAndIDmatch == SFE_UBLOX_PACKET_VALIDITY_VALID) && (packetAck.classAndIDmatch == SFE_UBLOX_PACKET_VALIDITY_NOT_VALID) && (outgoingUBX->valid == SFE_UBLOX_PACKET_VALIDITY_VALID) && (outgoingUBX->cls == requestedClass) && (outgoingUBX->id == requestedID))
3044+
else if ((packetCfg.isClassAndIdMatch(requestedClass, requestedID) && (packetAck.isClassAndIdMatch(requestedClass, requestedID) == false) && (packetCfg.valid == SFE_UBLOX_PACKET_VALIDITY_VALID)))
30953045
{
30963046
if (_printDebug == true)
30973047
{
@@ -3102,9 +3052,9 @@ sfe_ublox_status_e SFE_UBLOX_GNSS::waitForACKResponse(ubxPacket *outgoingUBX, ui
31023052
return (SFE_UBLOX_STATUS_DATA_RECEIVED); //We received valid data and an invalid ACK!
31033053
}
31043054

3105-
// If the outgoingUBX->classAndIDmatch is NOT_VALID and the packetAck.classAndIDmatch is NOT_VALID
3055+
// If classes of ack and data do not match
31063056
// then we return a FAIL. This must be a double checksum failure?
3107-
else if ((outgoingUBX->classAndIDmatch == SFE_UBLOX_PACKET_VALIDITY_NOT_VALID) && (packetAck.classAndIDmatch == SFE_UBLOX_PACKET_VALIDITY_NOT_VALID))
3057+
else if ((packetCfg.isClassAndIdMatch(requestedClass, requestedID) == false) && (packetAck.isClassAndIdMatch(requestedClass, requestedID) == false))
31083058
{
31093059
if (_printDebug == true)
31103060
{
@@ -3115,9 +3065,9 @@ sfe_ublox_status_e SFE_UBLOX_GNSS::waitForACKResponse(ubxPacket *outgoingUBX, ui
31153065
return (SFE_UBLOX_STATUS_FAIL); //We received invalid data and an invalid ACK!
31163066
}
31173067

3118-
// If the outgoingUBX->classAndIDmatch is VALID and the packetAck.classAndIDmatch is NOT_DEFINED
3068+
// If the config packet is VALID and packetAck does not match the requested class/ID
31193069
// then the ACK has not yet been received and we should keep waiting for it
3120-
else if ((outgoingUBX->classAndIDmatch == SFE_UBLOX_PACKET_VALIDITY_VALID) && (packetAck.classAndIDmatch == SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED))
3070+
else if ((packetCfg.isClassAndIdMatch(requestedClass, requestedID) == true) && (packetAck.isClassAndIdMatch(requestedClass, requestedID) == false))
31213071
{
31223072
// if (_printDebug == true)
31233073
// {
@@ -3133,7 +3083,7 @@ sfe_ublox_status_e SFE_UBLOX_GNSS::waitForACKResponse(ubxPacket *outgoingUBX, ui
31333083
} //while (millis() - startTime < maxTime)
31343084

31353085
// We have timed out...
3136-
// If the outgoingUBX->classAndIDmatch is VALID then we can take a gamble and return DATA_RECEIVED
3086+
// If the packetCfg matches the requested class/ID then we can take a gamble and return DATA_RECEIVED
31373087
// even though we did not get an ACK
31383088
if ((packetCfg.isClassAndIdMatch(requestedClass, requestedID) && !packetAck.isClassAndIdMatch(requestedClass, requestedID)) && (packetCfg.valid == SFE_UBLOX_PACKET_VALIDITY_VALID))
31393089
{
@@ -3168,20 +3118,16 @@ sfe_ublox_status_e SFE_UBLOX_GNSS::waitForNoACKResponse(ubxPacket *outgoingUBX,
31683118
packetAck.valid = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED;
31693119
packetBuf.valid = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED;
31703120
packetAuto.valid = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED;
3171-
outgoingUBX->classAndIDmatch = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED; // This will go VALID (or NOT_VALID) when we receive a packet that matches the requested class and ID
3172-
packetAck.classAndIDmatch = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED;
3173-
packetBuf.classAndIDmatch = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED;
3174-
packetAuto.classAndIDmatch = SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED;
3175-
3121+
31763122
unsigned long startTime = millis();
31773123
while (millis() - startTime < maxTime)
31783124
{
31793125
if (checkUbloxInternal(&packetBuf, requestedClass, requestedID) == true) //See if new data is available. Process bytes as they come in.
31803126
{
31813127

3182-
// If outgoingUBX->classAndIDmatch is VALID
3128+
// If packetBuf matches
31833129
// and outgoingUBX->valid is _still_ VALID and the class and ID _still_ match
3184-
// then we can be confident that the data in outgoingUBX is valid
3130+
// then we can be confident that the data in packetBuf is valid
31853131
if (packetBuf.isClassAndIdMatch(requestedClass, requestedID) && packetBuf.valid == SFE_UBLOX_PACKET_VALIDITY_VALID)
31863132
{
31873133
if (_printDebug == true)
@@ -3192,53 +3138,7 @@ sfe_ublox_status_e SFE_UBLOX_GNSS::waitForNoACKResponse(ubxPacket *outgoingUBX,
31923138
}
31933139
return (SFE_UBLOX_STATUS_DATA_RECEIVED); //We received valid data!
31943140
}
3195-
3196-
// If the outgoingUBX->classAndIDmatch is VALID
3197-
// but the outgoingUBX->cls or ID no longer match then we can be confident that we had
3198-
// valid data but it has been or is currently being overwritten by another packet (e.g. PVT).
3199-
// If (e.g.) a PVT packet is _being_ received: outgoingUBX->valid will be NOT_DEFINED
3200-
// If (e.g.) a PVT packet _has been_ received: outgoingUBX->valid will be VALID (or just possibly NOT_VALID)
3201-
// So we cannot use outgoingUBX->valid as part of this check.
3202-
// Note: the addition of packetBuf should make this check redundant!
3203-
else if ((outgoingUBX->classAndIDmatch == SFE_UBLOX_PACKET_VALIDITY_VALID) && ((outgoingUBX->cls != requestedClass) || (outgoingUBX->id != requestedID)))
3204-
{
3205-
if (_printDebug == true)
3206-
{
3207-
_debugSerial->print(F("waitForNoACKResponse: data being OVERWRITTEN after "));
3208-
_debugSerial->print(millis() - startTime);
3209-
_debugSerial->println(F(" msec"));
3210-
}
3211-
return (SFE_UBLOX_STATUS_DATA_OVERWRITTEN); // Data was valid but has been or is being overwritten
3212-
}
3213-
3214-
// If outgoingUBX->classAndIDmatch is NOT_DEFINED
3215-
// and outgoingUBX->valid is VALID then this must be (e.g.) a PVT packet
3216-
else if ((outgoingUBX->classAndIDmatch == SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED) && (outgoingUBX->valid == SFE_UBLOX_PACKET_VALIDITY_VALID))
3217-
{
3218-
// if (_printDebug == true)
3219-
// {
3220-
// _debugSerial->print(F("waitForNoACKResponse: valid but UNWANTED data after "));
3221-
// _debugSerial->print(millis() - startTime);
3222-
// _debugSerial->print(F(" msec. Class: "));
3223-
// _debugSerial->print(outgoingUBX->cls);
3224-
// _debugSerial->print(F(" ID: "));
3225-
// _debugSerial->print(outgoingUBX->id);
3226-
// }
3227-
}
3228-
3229-
// If the outgoingUBX->classAndIDmatch is NOT_VALID then we return CRC failure
3230-
/*else if (outgoingUBX->classAndIDmatch == SFE_UBLOX_PACKET_VALIDITY_NOT_VALID)
3231-
{
3232-
if (_printDebug == true)
3233-
{
3234-
_debugSerial->print(F("waitForNoACKResponse: CLS/ID match but failed CRC after "));
3235-
_debugSerial->print(millis() - startTime);
3236-
_debugSerial->println(F(" msec"));
3237-
}
3238-
return (SFE_UBLOX_STATUS_CRC_FAIL); //We received invalid data
3239-
}*/
32403141
}
3241-
32423142
delayMicroseconds(500);
32433143
}
32443144

src/SparkFun_u-blox_GNSS_Arduino_Library.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,6 @@ struct ubxPacket
506506
uint8_t checksumA; //Given to us from module. Checked against the rolling calculated A/B checksums.
507507
uint8_t checksumB;
508508
sfe_ublox_packet_validity_e valid; //Goes from NOT_DEFINED to VALID or NOT_VALID when checksum is checked
509-
sfe_ublox_packet_validity_e classAndIDmatch; // Goes from NOT_DEFINED to VALID or NOT_VALID when the Class and ID match the requestedClass and requestedID
510509
bool isClassAndIdMatch(uint8_t theClass, uint8_t theId) {
511510
return cls == theClass && theId == id;
512511
}
@@ -1318,10 +1317,10 @@ class SFE_UBLOX_GNSS
13181317
uint8_t spiBufferIndex = 0; // The index into the SPI buffer
13191318

13201319
//Init the packet structures and init them with pointers to the payloadAck, payloadCfg, payloadBuf and payloadAuto arrays
1321-
ubxPacket packetAck = {0, 0, 0, 0, 0, payloadAck, 0, 0, SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED, SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED};
1322-
ubxPacket packetBuf = {0, 0, 0, 0, 0, payloadBuf, 0, 0, SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED, SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED};
1323-
ubxPacket packetCfg = {0, 0, 0, 0, 0, payloadCfg, 0, 0, SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED, SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED};
1324-
ubxPacket packetAuto = {0, 0, 0, 0, 0, payloadAuto, 0, 0, SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED, SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED};
1320+
ubxPacket packetAck = {0, 0, 0, 0, 0, payloadAck, 0, 0, SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED};
1321+
ubxPacket packetBuf = {0, 0, 0, 0, 0, payloadBuf, 0, 0, SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED};
1322+
ubxPacket packetCfg = {0, 0, 0, 0, 0, payloadCfg, 0, 0, SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED};
1323+
ubxPacket packetAuto = {0, 0, 0, 0, 0, payloadAuto, 0, 0, SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED};
13251324

13261325
//Flag if this packet is unrequested (and so should be ignored and not copied into packetCfg or packetAck)
13271326
boolean ignoreThisPayload = false;

0 commit comments

Comments
 (0)