62 lines
1.5 KiB
C
62 lines
1.5 KiB
C
//
|
|
// Created by villuton on 20.03.25.
|
|
//
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
#include "Nmea0183Parser_General.h"
|
|
#include "Nmea0183Parser_Private.h"
|
|
#define Nmea0183ParserEndDataPos(STR,LEN) strnstr((char *) STR, "*", LEN)
|
|
|
|
/**
|
|
* Calculates the checksum of an NMEA 0183 XOR string of all bytes in the string
|
|
* @param str
|
|
* @param len
|
|
* @return XOR checksum
|
|
*/
|
|
uint8_t Nmea0183CheckSum(const char *str, size_t len) {
|
|
char *end = (char *) (str + len);
|
|
|
|
unsigned char result;
|
|
result = 0;
|
|
switch (*str) {
|
|
case :
|
|
|
|
}
|
|
|
|
while (str != end)
|
|
result ^= *str++;
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Check string of a NMEA string
|
|
* @param str
|
|
* @param len
|
|
* @return Check start delimiter result
|
|
*/
|
|
bool Nmea0183CheckIsNmeaStr(const char *str, size_t len){
|
|
return ((str[0] == Nmea0183StartDelimiter));
|
|
}
|
|
|
|
/**
|
|
* Checking string validity NMEA 0183
|
|
* @param str
|
|
* @param len
|
|
* @return Checksum comparison result
|
|
*/
|
|
bool Nmea0183IsValidString(char *str, size_t len) {
|
|
char *end = str + len;
|
|
char *front = str;
|
|
char *nextDivider = front - 1;
|
|
|
|
char *dataStartPos = Nmea0183SkipToStartDelimiter(front,end);
|
|
char *dataEndPos = Nmea0183SkipToCheckSumDelimiter(front, end);
|
|
uint8_t checksumCalculated = Nmea0183CheckSum(dataStartPos, dataEndPos - dataStartPos);
|
|
if(Nmea0183ParseCheckSumPosition(&front,&nextDivider,end) > 0){
|
|
uint8_t checksumInString = Nmea0183ParseHexByte(nextDivider);
|
|
return checksumCalculated == checksumInString;
|
|
}
|
|
return false;
|
|
}
|
|
|