Skip to content

Commit

Permalink
NMEAParser::ParseNMEAString_Internal : reduce mutex lock contention
Browse files Browse the repository at this point in the history
  • Loading branch information
brunotl committed Apr 9, 2024
1 parent 58743fa commit 0ff7bc1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
6 changes: 6 additions & 0 deletions Common/Source/Comm/LKFlarm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ BOOL NMEAParser::PFLAV(const char *String, char **params, size_t nparams, NMEA_I
*/
if(nparams > 3) {
ScopeLock lock(CritSec_FlightData);

pGPS->FLARM_HW_Version = strtod(params[1], nullptr);
pGPS->FLARM_SW_Version = strtod(params[2], nullptr);

Expand Down Expand Up @@ -365,6 +367,8 @@ BOOL NMEAParser::PFLAU(const char *String, char **params, size_t nparams, NMEA_I
return FALSE;
}

ScopeLock lock(CritSec_FlightData);

setFlarmAvailable(pGPS);

pGPS->FLARM_RX = strtoul(params[0], nullptr, 10); // number of received FLARM devices
Expand Down Expand Up @@ -574,6 +578,8 @@ BOOL NMEAParser::PFLAA(const char *String, char **params, size_t nparams, NMEA_I
return FALSE;
}

ScopeLock lock(CritSec_FlightData);

setFlarmAvailable(pGPS);

// 5 id, 6 digit hex
Expand Down
47 changes: 21 additions & 26 deletions Common/Source/Comm/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,27 +232,21 @@ BOOL NMEAParser::ParseNMEAString_Internal(DeviceDescriptor_t& d, const char* Str
std::string_view token = params[0] + 2;
// Proprietary String
if (token == "TAS1"sv) {
ScopeLock lock(CritSec_FlightData);
return PTAS1(d, &String[7], params + 1, n_params - 1, pGPS);
}
if (token == "FLAV"sv) {
ScopeLock lock(CritSec_FlightData);
return PFLAV(&String[7], params + 1, n_params - 1, pGPS);
}
if (token == "FLAA"sv) {
ScopeLock lock(CritSec_FlightData);
return PFLAA(&String[7], params + 1, n_params - 1, pGPS);
}
if (token == "FLAU"sv) {
ScopeLock lock(CritSec_FlightData);
return PFLAU(&String[7], params + 1, n_params - 1, pGPS);
}
if (token == "GRMZ"sv) {
ScopeLock lock(CritSec_FlightData);
return RMZ(d, &String[7], params + 1, n_params - 1, pGPS);
}
if (token == "LKAS"sv) {
ScopeLock lock(CritSec_FlightData);
return PLKAS(&String[7], params + 1, n_params - 1, pGPS);
}
return FALSE;
Expand All @@ -262,39 +256,32 @@ BOOL NMEAParser::ParseNMEAString_Internal(DeviceDescriptor_t& d, const char* Str
// GNSS String
std::string_view token = params[0] + 3;
if (token == "GSA"sv) {
ScopeLock lock(CritSec_FlightData);
return GSA(&String[7], params + 1, n_params - 1, pGPS);
}
if (token == "GLL"sv) {
/*
ScopeLock lock(CritSec_FlightData);
return GLL(&String[7], params + 1, n_params-1, pGPS);
*/
return FALSE;
}
if (token == "RMB"sv) {
/*
ScopeLock lock(CritSec_FlightData);
return RMB(&String[7], params + 1, n_params-1, pGPS);
*/
return FALSE;
}
if (token == "RMC"sv) {
ScopeLock lock(CritSec_FlightData);
return RMC(&String[7], params + 1, n_params - 1, pGPS);
}
if (token == "GGA"sv) {
ScopeLock lock(CritSec_FlightData);
return GGA(&String[7], params + 1, n_params - 1, pGPS);
}
if (token == "VTG"sv) {
ScopeLock lock(CritSec_FlightData);
return VTG(&String[7], params + 1, n_params - 1, pGPS);
}
}

if (std::string_view(params[0] + 1) == "HCHDG"sv) {
ScopeLock lock(CritSec_FlightData);
return HCHDG(&String[7], params + 1, n_params - 1, pGPS);
}

Expand Down Expand Up @@ -428,6 +415,8 @@ BOOL NMEAParser::GLL(const char* String, char** params, size_t nparams, NMEA_INF

if (!activeGPS) return TRUE;

ScopeLock lock(CritSec_FlightData);

pGPS->NAVWarning = !gpsValid;

// use valid time with invalid fix
Expand Down Expand Up @@ -482,6 +471,9 @@ BOOL NMEAParser::VTG(const char* String, char** params, size_t nparams, NMEA_INF
// if no valid fix, we dont get speed either!
if (gpsValid) {
double speed = StrToDouble(params[4], NULL);

ScopeLock lock(CritSec_FlightData);

pGPS->Speed = Units::From(unKnots, speed);

if (ISCAR) {
Expand Down Expand Up @@ -524,6 +516,7 @@ BOOL NMEAParser::RMC(const char* String, char** params, size_t nparams, NMEA_INF
double ps = GM130BarPressure();
double Altitude = (1 - pow(fabs(ps / QNH), 0.190284)) * 44307.69;

ScopeLock lock(CritSec_FlightData);
UpdateBaroSource(pGPS, nullptr, Altitude);
}

Expand All @@ -532,12 +525,8 @@ BOOL NMEAParser::RMC(const char* String, char** params, size_t nparams, NMEA_INF
double ps = Royaltek3200_GetPressure();
double Altitude = (1 - pow(fabs(ps / QNH), 0.190284)) * 44307.69;

ScopeLock lock(CritSec_FlightData);
UpdateBaroSource(pGPS, nullptr, Altitude);

#if 0
pGPS->TemperatureAvailable=true;
pGPS->OutsideAirTemperature = Royaltek3200_GetTemperature();
#endif
}
}

Expand All @@ -553,6 +542,8 @@ BOOL NMEAParser::RMC(const char* String, char** params, size_t nparams, NMEA_INF
speed = StrToDouble(params[6], NULL);
}

ScopeLock lock(CritSec_FlightData);

pGPS->NAVWarning = !gpsValid;

// say we are updated every time we get this,
Expand Down Expand Up @@ -733,6 +724,9 @@ BOOL NMEAParser::GGA(const char* String, char** params, size_t nparams, NMEA_INF
if(gpsValid && (nSatellites == 0)) {
nSatellites = -1; // unknown count but valid fix !
}

ScopeLock lock(CritSec_FlightData);

pGPS->SatellitesUsed = nSatellites; // 091208
pGPS->NAVWarning = !gpsValid; // 091208

Expand Down Expand Up @@ -833,15 +827,15 @@ BOOL NMEAParser::GGA(const char* String, char** params, size_t nparams, NMEA_INF
// LK8000 IAS , in m/s*10 example: 346 for 34.6 m/s which is = 124.56 km/h
BOOL NMEAParser::PLKAS(const char* String, char** params, size_t nparams, NMEA_INFO *pGPS)
{
(void)pGPS;

if(nparams < 1) {
TESTBENCH_DO_ONLY(10,StartupStore(_T(". NMEAParser invalid PLKAS sentence, nparams=%u%s"),(unsigned)nparams,NEWLINE));
// max index used is 0...
return FALSE;
}

double vias=StrToDouble(params[0],NULL)/10.0;

ScopeLock lock(CritSec_FlightData);
if (vias > 1) {
double qne_altitude = QNHAltitudeToQNEAltitude(pGPS->Altitude);
pGPS->TrueAirspeed = TrueAirSpeed(vias, qne_altitude);
Expand All @@ -859,8 +853,6 @@ BOOL NMEAParser::PLKAS(const char* String, char** params, size_t nparams, NMEA_I

BOOL NMEAParser::RMZ(DeviceDescriptor_t& d, const char* String, char **params, size_t nparams, NMEA_INFO *pGPS)
{
(void)pGPS;

if(nparams < 2) {
TESTBENCH_DO_ONLY(10,StartupStore(_T(". NMEAParser invalid RMZ sentence, nparams=%u%s"),(unsigned)nparams,NEWLINE));
// max index used is 1...
Expand All @@ -877,6 +869,8 @@ BOOL NMEAParser::RMZ(DeviceDescriptor_t& d, const char* String, char **params, s
LastRMZHB = LKHearthBeats;

double Altitude = ParseAltitude(params[0], params[1]);

ScopeLock lock(CritSec_FlightData);
UpdateBaroSource(pGPS, &d, QNEAltitudeToQNHAltitude(Altitude));

return FALSE;
Expand All @@ -892,6 +886,8 @@ BOOL NMEAParser::PTAS1(DeviceDescriptor_t& d, const char* String, char **params,
return FALSE;
}

ScopeLock lock(CritSec_FlightData);

if(*params[0] != _T('\0')) {
const double wnet = Units::From(unKnots, (StrToDouble(params[0],NULL) - 200.0) / 10);
UpdateVarioSource(*pGPS, d, wnet);
Expand Down Expand Up @@ -923,14 +919,13 @@ BOOL NMEAParser::HCHDG(const char* String, char** params, size_t nparams, NMEA_I
return FALSE;
}

(void)pGPS;
double mag=0;
mag=StrToDouble(params[0],NULL);
double mag = StrToDouble(params[0],NULL);
if (mag>=0 && mag<=360) {
ScopeLock lock(CritSec_FlightData);
pGPS->MagneticHeading=mag;
pGPS->MagneticHeadingAvailable=TRUE;
}
return FALSE;
return TRUE;
}


0 comments on commit 0ff7bc1

Please sign in to comment.