44extern UM980 *ptrUM980; // Global pointer for external parser access into library class
55
66// End of message handler
7- // Crack the response into various endpoints
87// If it's a response to a command, is it OK or BAD? $command,badResponse,response:
98// PARSING FAILD NO MATCHING FUNC BADRESPONSE*40
109// If it's Unicore binary, load into target variables If it's NMEA or RTCM, discard
@@ -38,37 +37,33 @@ void eomHandler(PARSE_STATE *parse)
3837 }
3938 else if (parse->messageType == SFE_SENTENCE_TYPE_UNICORE_POUND_RESPONSE)
4039 {
41- Serial.println (" Handling Unicore pound response" );
40+ // Serial.println("Handling Unicore pound response");
4241
43- Serial.printf (" parse->nmeaMessageName: %s\r\n " , (char *)parse->nmeaMessageName );
42+ // Serial.printf("parse->nmeaMessageName: %s\r\n", (char *)parse->nmeaMessageName);
4443
4544 // Does this response contain the command we are looking for?
4645 if (strcasecmp ((char *)parse->nmeaMessageName , ptrUM980->commandName ) == 0 ) // Found
47- {
48- Serial.println (" Command name matches" );
49-
5046 ptrUM980->commandResponse = UM980_RESULT_RESPONSE_COMMAND_OK;
51- }
5247 }
5348 else if (parse->messageType == SFE_SENTENCE_TYPE_NMEA)
5449 {
55- // Serial.println("NMEA handler");
50+ // Serial.println("NMEA handler");
5651
5752 // ID the NMEA message type
5853
5954 // Serial.print("NMEA Handler: ");
6055 // for (int x = 0; x < parse->length; x++)
6156 // Serial.write(parse->buffer[x]);
62- // Serial.println();
57+ // Serial.println();
6358 }
6459 else if (parse->messageType == SFE_SENTENCE_TYPE_UNICORE_BINARY)
6560 {
6661 ptrUM980->unicoreHandler (parse->buffer , parse->length );
67- // Serial.println("Unicore handler");
62+ // Serial.println("Unicore handler");
6863 }
6964 else if (parse->messageType == SFE_SENTENCE_TYPE_RTCM)
7065 {
71- Serial.println (" RTCM handler" );
66+ // Serial.println("RTCM handler");
7267 }
7368}
7469
@@ -99,7 +94,6 @@ void waitForPreamble(PARSE_STATE *parse, uint8_t data)
9994 // Use the same NMEA parser
10095
10196 parse->check = 0 ;
102- parse->computeCrc = false ;
10397 parse->nmeaMessageNameLength = 0 ;
10498 parse->state = PARSE_STATE_NMEA_FIRST_COMMA;
10599 return ;
@@ -120,12 +114,7 @@ void waitForPreamble(PARSE_STATE *parse, uint8_t data)
120114 // |<------------------------ CRC -------------------------->|
121115 //
122116
123- Serial.println (" rtcm" );
124-
125117 // Start the CRC with this byte
126- parse->check = 0 ;
127- parse->check = COMPUTE_CRC24Q (parse, data);
128- parse->computeCrc = true ;
129118 parse->state = PARSE_STATE_RTCM_LENGTH1;
130119 return ;
131120
@@ -145,11 +134,13 @@ void waitForPreamble(PARSE_STATE *parse, uint8_t data)
145134 // |<------------------------ CRC --------------->|
146135 //
147136
148- parse->length = 0 ;
149- parse->buffer [parse->length ++] = data; // This byte is part of the CRC
150137 parse->state = PARSE_STATE_UNICORE_SYNC2;
151138 return ;
152139 }
140+
141+ // Preamble byte not found
142+ parse->length = 0 ;
143+ parse->state = PARSE_STATE_WAITING_FOR_PREAMBLE;
153144}
154145
155146// Read the message name
@@ -198,8 +189,6 @@ void nmeaChecksumByte2(PARSE_STATE *parse, uint8_t data)
198189// Read the line termination
199190void nmeaLineTermination (PARSE_STATE *parse, uint8_t data)
200191{
201- int checksum;
202-
203192 // We expect a \r\n termination, but may vary between receiver types
204193 if (data == ' \r ' || data == ' \n ' )
205194 {
@@ -214,7 +203,7 @@ void nmeaLineTermination(PARSE_STATE *parse, uint8_t data)
214203 parse->length --;
215204
216205 // Convert the checksum characters into binary
217- checksum = AsciiToNibble (parse->buffer [parse->nmeaLength - 2 ]) << 4 ;
206+ int checksum = AsciiToNibble (parse->buffer [parse->nmeaLength - 2 ]) << 4 ;
218207 checksum |= AsciiToNibble (parse->buffer [parse->nmeaLength - 1 ]);
219208
220209 parse->messageType = SFE_SENTENCE_TYPE_NMEA;
@@ -289,6 +278,8 @@ void nmeaLineTermination(PARSE_STATE *parse, uint8_t data)
289278 {
290279 // Serial.println("nmeaTermination Returning after EOM handler");
291280 eomHandler (parse);
281+
282+ parse->length = 0 ;
292283 parse->state = PARSE_STATE_WAITING_FOR_PREAMBLE; // Move to next state
293284 return ;
294285 }
@@ -390,6 +381,18 @@ void unicoreBinaryReadLength(PARSE_STATE *parse, uint8_t data)
390381 // The overall message length is header (24) + data (expectedLength) + CRC (4)
391382 parse->bytesRemaining = um980HeaderLength + expectedLength + 4 ;
392383
384+ if (parse->bytesRemaining > PARSE_BUFFER_LENGTH)
385+ {
386+ Serial.println (" Length overflow" );
387+
388+ // Invalid length, place this byte at the beginning of the buffer
389+ parse->length = 0 ;
390+ parse->buffer [parse->length ++] = data;
391+
392+ // Start searching for a preamble byte
393+ return waitForPreamble (parse, data);
394+ }
395+
393396 // Account for the bytes already received
394397 parse->bytesRemaining -= parse->length ;
395398 parse->state = PARSE_STATE_UNICORE_READ_DATA; // Move on
@@ -439,7 +442,7 @@ uint32_t calculateCRC32(uint8_t *charBuffer, uint16_t bufferSize)
439442{
440443 uint32_t crc = 0 ;
441444 for (uint16_t x = 0 ; x < bufferSize; x++)
442- crc = crcTable32 [(crc ^ charBuffer[x]) & 0xFF ] ^ (crc >> 8 );
445+ crc = crc32Table [(crc ^ charBuffer[x]) & 0xFF ] ^ (crc >> 8 );
443446 return crc;
444447}
445448
@@ -452,7 +455,6 @@ void rtcmReadLength1(PARSE_STATE *parse, uint8_t data)
452455 // Invalid length, place this byte at the beginning of the buffer
453456 parse->length = 0 ;
454457 parse->buffer [parse->length ++] = data;
455- parse->computeCrc = false ;
456458
457459 // Start searching for a preamble byte
458460 return waitForPreamble (parse, data);
@@ -467,21 +469,33 @@ void rtcmReadLength1(PARSE_STATE *parse, uint8_t data)
467469void rtcmReadLength2 (PARSE_STATE *parse, uint8_t data)
468470{
469471 parse->bytesRemaining |= data;
472+
473+ // Check the length
474+ if (parse->bytesRemaining > PARSE_BUFFER_LENGTH)
475+ {
476+ Serial.println (" RTCM length overflow" );
477+
478+ // Invalid length, place this byte at the beginning of the buffer
479+ parse->length = 0 ;
480+ parse->buffer [parse->length ++] = data;
481+
482+ // Start searching for a preamble byte
483+ return waitForPreamble (parse, data);
484+ }
485+
470486 parse->state = PARSE_STATE_RTCM_MESSAGE1;
471487}
472488
473489// Read the upper 8 bits of the message number
474490void rtcmReadMessage1 (PARSE_STATE *parse, uint8_t data)
475491{
476- parse->message = data << 4 ;
477492 parse->bytesRemaining -= 1 ;
478493 parse->state = PARSE_STATE_RTCM_MESSAGE2;
479494}
480495
481496// Read the lower 4 bits of the message number
482497void rtcmReadMessage2 (PARSE_STATE *parse, uint8_t data)
483498{
484- parse->message |= data >> 4 ;
485499 parse->bytesRemaining -= 1 ;
486500 parse->state = PARSE_STATE_RTCM_DATA;
487501}
@@ -495,7 +509,6 @@ void rtcmReadData(PARSE_STATE *parse, uint8_t data)
495509 // Wait until all the data is received
496510 if (parse->bytesRemaining <= 0 )
497511 {
498- // parse->rtcmCrc = parse->check & 0x00ffffff;
499512 parse->bytesRemaining = 3 ;
500513 parse->state = PARSE_STATE_RTCM_CRC;
501514 }
@@ -511,28 +524,37 @@ void rtcmReadCrc(PARSE_STATE *parse, uint8_t data)
511524 if (parse->bytesRemaining > 0 )
512525 return ;
513526
514- // Update the maximum message length
515- if (parse->length > parse->maxLength )
527+ // Get CRC
528+ uint32_t sentenceCRC = (parse->buffer [parse->length - 3 ] << 16 ) | (parse->buffer [parse->length - 2 ] << 8 ) |
529+ (parse->buffer [parse->length - 1 ] << 0 );
530+
531+ // Calculate CRC
532+ parse->check = 0 ;
533+ for (int x = 0 ; x < parse->length - 3 ; x++) // Exclude CRC
516534 {
517- parse->maxLength = parse->length ;
518- // debugPrintf("RTCM parser error maxLength: %d bytes\r\n", parse->maxLength);
535+ parse->check = ((parse)->check << 8 ) ^ crc24q[parse->buffer [x] ^ (((parse)->check >> 16 ) & 0xff )];
519536 }
520-
521537 parse->check &= 0x00ffffff ;
522538
523539 // Process the message if CRC is valid
524- if (parse->check == 0 )
540+ if (parse->check == sentenceCRC )
525541 {
542+ // Serial.printf("RTCM CRC Good length: %d\r\n", parse->length);
526543 parse->messageType = SFE_SENTENCE_TYPE_RTCM;
527544 eomHandler (parse);
528545 }
529546 else
530547 {
531- Serial.println (" RTCM CRC failed" );
548+ Serial.printf (" RTCM CRC failed length: %d sentence CRC: 0x%02X calc CRC: 0x%02X\r\n " , parse->length ,
549+ sentenceCRC, parse->check );
550+ for (int x = 0 ; x < parse->length ; x++)
551+ {
552+ Serial.write (parse->buffer [x]);
553+ }
554+ Serial.println ();
532555 }
533556
534557 // Search for another preamble byte
535558 parse->length = 0 ;
536- parse->computeCrc = false ;
537559 parse->state = PARSE_STATE_WAITING_FOR_PREAMBLE;
538560}
0 commit comments