# nmea_parse.c

Functions:- | Parser for NMEA strings, generic and proprietary.
Notes based on code as of Mon Apr 5 21:38:06 2010 -0400.
`static void do_lat_lon(char *field[], struct gps_fix_t *out)` | Reads a four element array containing the value and hemisphere of the latitude and longitude of a location as text. It converts them into signed (-ve for S and W) values. The gps_data_t structure is updated with the new value(s).
`static void merge_ddmmyy(char *ddmmyy, struct gps_device_t *session)` | If the century has not yet been stored in the nmea driver private data, take the supplied ddmmyy date and generate and store a ddmmyyyy date, using the century value compiled in from `gpsd.h`.
`static void merge_hhmmss(char *hhmmss, struct gps_device_t *session)` | Stash the present hour value before updating it from the incoming data. If the new hour is less than the stashed value, we have passed midnight, so update the day value. Finally update the minutes, seconds and fractions of a second from the incoming data.
`static gps_mask_t processGPRMC(int count, char *field[], struct gps_device_t *session)` | Handle a $GPRMC sentence stored in an array of strings, one member per field. Check if the message is stamped valid or not. If it is invalid, set the status and fix mode to NO_FIX and save the corresponding flags locally; also save the online flag to indicate we have handled a known sentence. If the fix is autonomous and valid, start to decode the fields. First, test if there are enough fields available; then handle the date and time via `merge_ddmmyy()` and `merge_hhmmss()`, storing the TIME_SET flag and storing the fix time as a UNIX-epoch relative value. If the sentence time and this fix time are different, we have started a new cycle of observation, so update the sentence time and the store the CYCLE_START_SET flag. Whatever the number of fields, store the fix coordinates via `do_lat_lon()`, store the speed and the track and save the corresponding flags. Return the local aggregated flags to allow the main copy in the session data to be updated.
`static gps_mask_t processGPGLL(int count, char *field[], struct gps_device_t *session)` | Preload the local flag with the ERROR_SET flag. Check that the sentence is usable, exiting with the preset error flag if it is not. If it is usable, clear the local flags and start processing the fields, updating any local flag fields on the way. If the year is already known, update the time and check for the start of cycle (see `processGPRMC()` above). Handle the fix location and, if the number of received fixes is more than 8 and the status is differential, stash the new status as STATUS_DGPS_FIX; otherwise stash STATUS_FIX. If the present mode is less than 2D_FIX, update it to 2D_FIX. Write the stashed value of newstatus into the session status and return all the locally aggregated flags.
`static gps_mask_t processGPGGA(int c UNUSED, char *field[], struct gps_device_t *session)` | Stash the last fix time. Set the status to the value in the message and update the local flag variable. If the status is STATUS_NO_FIX, exit immediately, returning the locally aggregated flags. If there is a fix, process it. Handle the time as in `processGPGLL()` above. Handle the latitiude and longitude with a call to `do_lat_lon()` above and set the local flag. Update the `satellites_used` field and stash the altitude. If the altitude is empty, force the fix mode and status to 2D if it was 3D previously. If it is not empty, stash the old value of altitude and replace it with the new value stashed earlier and set the local flag variable. If the mode is presently less than 3D, update it to 3D and set the local flag. If the stashed old altitude is NaN or the stashed fix time and current fix time are equal, set the climb rate to 0 otherwise calculate it by dividing the altitude difference by the time difference and set the local flag. If the geoid separation is available, store it, otherwise store the value from `wgs84_separation()` that depends on current location. Finally, return all the locally aggregated flags.
`static gps_mask_t processGPGSA(int count, char *field[], struct gps_device_t *session)` | Start with a simple validity check on the number of fields (for i.Trek M3) and bail out with a simple indication of on-line status if it fails. Set the fix mode from the sentence and either clear the local flag variable (if an Antaris chipset says we are in dead-reckoning mode) or set the MODE_SET flag. Update all the DOP fields from the sentence, clear the count of used satellites, then scan all the satellite data. If any satellite is good (prn != 0), store the prn and increment the count of used satellites. Finally, set the local flags to indicate that DOPs are available and return all the locally aggregated flags.
`static gps_mask_t processGPGSV(int count, char *field[], struct gps_device_t *session)` | Check if the sentence has too few fields or the wrong number of fields. In this case, clear the data for all satellites and return with an error indication. Start to parse the sentence. First, note how many sentences are to be expected to complete the data transfer. If the sentence number is invalid, clear the data for all satellites and return with an error indication. If this is the first sentence of the sequence, clear the data for all satellites. Loop through the sentence fields, updating the session's satellite data. If any satellite number is higher than the number of channels, clear all satellite data and break out of the loop. Assuming this is not a buggy chipset (e.g. Motorola Oncore GT+), update the satellite count and loop again. If this was the last sentence of the block and the number of satellites seen is not the same as the number reported, generate an error log. If this is not the last sentence of the block, exit early and return an error flag as a guard. Finally, on the last sentence, carry out a sanity check and either return an error flag or a SATELLITE_SET flag.
`static gps_mask_t processPGRME(int c UNUSED, char *field[], struct gps_device_t *session)` | Check that the error estimate data is good. If not, set all error estimate fields to 100m and return an error flag. If they are good, calculate the error value and store it. Return the appropriate flag values.
`static gps_mask_t processGPZDA(int c UNUSED, char *field[], struct gps_device_t *session)` | Set the local flag variable to indicate that the time is available. Store the actual time by a call to `merge_hhmmss()` and fill in the other fields from the sentence data. If the sentence is not timestamped the same as the fixtime, set the CYCLE_START_SET flag. Update the fixtime to the sentence timestamp. Finally, return all the locally aggregated flags.
`static gps_mask_t processTNTHTM(int c UNUSED, char *field[], struct gps_device_t *session)` | Set the local variable to indicate the unit is on-line. Fill all appropriate fields from the sentence and set the associated flags in the local flag variable. Set the fix status and return all the locally aggregated flags.
`static short nmea_checksum(char *sentence, unsigned char *correct_sum)` | Calculate and return the checksum of an NMEA sentence.
`gps_mask_t nmea_parse(char *sentence, struct gps_device_t *session)` | Test that the length of the NMEA sentence is acceptable, simply returning an on-line indication if it is too long to handle. If it is within limits, make a local copy and split it on the commas into an array, one field per element. Use the first element to match the command to the table of decodable commands. Check if it is supported and the number of fields is reasonable, invoke the correct decoder and return the value from that call. If it fails the check, simply return an on-line status. This function is also responsible for performing adaptive end-of-cycle detection.
`void nmea_add_checksum(char *sentence)` | Calculate the checksum then append '*' + the checksum + CR/LF to the end of an NMEA sentence, skipping any existing '*'.
`int nmea_write(struct gps_device_t *session, const char *fmt, ... )` | Ship a string to an NMEA device, adding a checksum and CR/LF if needed. A checksum is added only if the sentence begins with '$'. Bytes written are returned.
`int nmea_send(struct gps_device_t *session, , const char *fmt, ... )` | A wrapper around `nmea_write()` to give it sprintf-like varargs behavior.