packet.c
来自「gpsd, a popular GPS daemon.」· C语言 代码 · 共 1,271 行 · 第 1/3 页
C
1,271 行
/* $Id: packet.c 4660 2008-01-17 19:43:57Z esr $ *//****************************************************************************NAME: packet.c -- a packet-sniffing engine for reading from GPS devicesDESCRIPTION:Initial conditions of the problem:1. We have a file descriptor open for (possibly non-blocking) read. The device on the other end is sending packets at us.2. It may require more than one read to gather a packet. Reads may span packet boundaries.3. There may be leading garbage before the first packet. After the first start-of-packet, the input should be well-formed.The problem: how do we recognize which kind of packet we're getting?No need to handle Garmin USB binary, we know that type by the fact we'reconnected to the Garmin kernel driver. But we need to be able to tell theothers apart and distinguish them from baud barf.***************************************************************************/#include <sys/types.h>#include <ctype.h>#include <stdio.h>#include <unistd.h>#include <string.h>#include <errno.h>#include "gpsd_config.h"#include "gpsd.h"/* * The packet-recognition state machine. It can be fooled by garbage * that looks like the head of a binary packet followed by a NMEA * packet; in that case it won't reset until it notices that the * binary trailer is not where it should be, and the NMEA packet will * be lost. The reverse scenario is not possible because none of the * binary leader characters can occur in an NMEA packet. Caller should * consume a packet when it sees one of the *_RECOGNIZED states. * It's good practice to follow the _RECOGNIZED transition with one * that recognizes a leader of the same packet type rather than * dropping back to ground state -- this for example will prevent * the state machine from hopping between recognizing TSIP and * EverMore packets that both start with a DLE. * * Error handling is brutally simple; any time we see an unexpected * character, go to GROUND_STATE and reset the machine (except that a * $ in an NMEA payload only resets back to NMEA_DOLLAR state). Because * another good packet will usually be along in less than a second * repeating the same data, Boyer-Moore-like attempts to do parallel * recognition beyond the headers would make no sense in this * application, they'd just add complexity. * * This state machine allows the following talker IDs: * GP -- Global Positioning System. * II -- Integrated Instrumentation (Raytheon's SeaTalk system). * IN -- Integrated Navigation (Garmin uses this). * */enum {#include "packet_states.h"};#define DLE 0x10#define STX 0x02#define ETX 0x03static void nextstate(struct gps_packet_t *lexer, unsigned char c){#ifdef RTCM104_ENABLE enum isgpsstat_t isgpsstat;#endif /* RTCM104_ENABLE *//*@ +charint -casebreak @*/ switch(lexer->state) { case GROUND_STATE: if (c == '#') { lexer->state = COMMENT_BODY; break; }#ifdef NMEA_ENABLE if (c == '$') { lexer->state = NMEA_DOLLAR; break; } if (c == '!') { lexer->state = NMEA_BANG; break; }#endif /* NMEA_ENABLE */#if defined(TNT_ENABLE) || defined(GARMINTXT_ENABLE) if (c == '@') { lexer->state = TNT_LEADER; break; }#endif#ifdef SIRF_ENABLE if (c == 0xa0) { lexer->state = SIRF_LEADER_1; break; }#endif /* SIRF_ENABLE */#if defined(TSIP_ENABLE) || defined(EVERMORE_ENABLE) || defined(GARMIN_ENABLE) if (c == DLE) { lexer->state = DLE_LEADER; break; }#endif /* TSIP_ENABLE || EVERMORE_ENABLE || GARMIN_ENABLE */#ifdef TRIPMATE_ENABLE if (c == 'A') {#ifdef RTCM104_ENABLE if (rtcm_decode(lexer, c) == ISGPS_MESSAGE) { lexer->state = RTCM_RECOGNIZED; break; }#endif /* RTCM104_ENABLE */ lexer->state = ASTRAL_1; break; }#endif /* TRIPMATE_ENABLE */#ifdef EARTHMATE_ENABLE if (c == 'E') {#ifdef RTCM104_ENABLE if (rtcm_decode(lexer, c) == ISGPS_MESSAGE) { lexer->state = RTCM_RECOGNIZED; break; }#endif /* RTCM104_ENABLE */ lexer->state = EARTHA_1; break; }#endif /* EARTHMATE_ENABLE */#ifdef ZODIAC_ENABLE if (c == 0xff) { lexer->state = ZODIAC_LEADER_1; break; }#endif /* ZODIAC_ENABLE */#ifdef UBX_ENABLE if (c == 0xb5) { lexer->state = UBX_LEADER_1; break; }#endif /* UBX_ENABLE */#ifdef ITRAX_ENABLE if (c == '<') { lexer->state = ITALK_LEADER_1; break; }#endif /* ITRAX_ENABLE */#ifdef NAVCOM_ENABLE if (c == 0x02) { lexer->state = NAVCOM_LEADER_1; break; }#endif /* NAVCOM_ENABLE */#ifdef RTCM104_ENABLE if ((isgpsstat = rtcm_decode(lexer, c)) == ISGPS_SYNC) { lexer->state = RTCM_SYNC_STATE; break; } else if (isgpsstat == ISGPS_MESSAGE) { lexer->state = RTCM_RECOGNIZED; break; }#endif /* RTCM104_ENABLE */ break; case COMMENT_BODY: if (c == '\n') lexer->state = COMMENT_RECOGNIZED; else if (!isprint(c)) lexer->state = GROUND_STATE; break;#ifdef NMEA_ENABLE case NMEA_DOLLAR: if (c == 'G') lexer->state = NMEA_PUB_LEAD; else if (c == 'P') /* vendor sentence */ lexer->state = NMEA_VENDOR_LEAD; else if (c =='I') /* Seatalk */ lexer->state = SEATALK_LEAD_1; else if (c =='A') /* SiRF Ack */ lexer->state = SIRF_ACK_LEAD_1; else lexer->state = GROUND_STATE; break; case NMEA_PUB_LEAD: if (c == 'P') lexer->state = NMEA_LEADER_END; else lexer->state = GROUND_STATE; break; case NMEA_VENDOR_LEAD: if (isalpha(c)) lexer->state = NMEA_LEADER_END; else lexer->state = GROUND_STATE; break; case NMEA_BANG: if (c == 'A') lexer->state = AIS_LEAD_1; else lexer->state = GROUND_STATE; break; case AIS_LEAD_1: if (c == 'I') lexer->state = AIS_LEAD_2; else lexer->state = GROUND_STATE; break; case AIS_LEAD_2: if (isalpha(c)) lexer->state = NMEA_LEADER_END; else lexer->state = GROUND_STATE; break;#if defined(TNT_ENABLE) || defined(GARMINTXT_ENABLE) case TNT_LEADER: lexer->state = NMEA_LEADER_END; break;#endif case NMEA_LEADER_END: if (c == '\r') lexer->state = NMEA_CR; else if (c == '\n') /* not strictly correct, but helps for interpreting logfiles */ lexer->state = NMEA_RECOGNIZED; else if (c == '$') /* faster recovery from missing sentence trailers */ lexer->state = NMEA_DOLLAR; else if (!isprint(c)) lexer->state = GROUND_STATE; break; case NMEA_CR: if (c == '\n') lexer->state = NMEA_RECOGNIZED; else lexer->state = GROUND_STATE; break; case NMEA_RECOGNIZED: if (c == '$') lexer->state = NMEA_DOLLAR; else if (c == '!') lexer->state = NMEA_BANG; else lexer->state = GROUND_STATE; break; case SEATALK_LEAD_1: if (c == 'I' || c == 'N') /* II or IN are accepted */ lexer->state = NMEA_LEADER_END; else lexer->state = GROUND_STATE; break;#ifdef TRIPMATE_ENABLE case ASTRAL_1: if (c == 'S') {#ifdef RTCM104_ENABLE if (rtcm_decode(lexer, c) == ISGPS_MESSAGE) { lexer->state = RTCM_RECOGNIZED; break; }#endif /* RTCM104_ENABLE */ lexer->state = ASTRAL_2; } else lexer->state = GROUND_STATE; break; case ASTRAL_2: if (c == 'T') {#ifdef RTCM104_ENABLE if (rtcm_decode(lexer, c) == ISGPS_MESSAGE) { lexer->state = RTCM_RECOGNIZED; break; }#endif /* RTCM104_ENABLE */ lexer->state = ASTRAL_3; } else lexer->state = GROUND_STATE; break; case ASTRAL_3: if (c == 'R') {#ifdef RTCM104_ENABLE if (rtcm_decode(lexer, c) == ISGPS_MESSAGE) { lexer->state = RTCM_RECOGNIZED; break; }#endif /* RTCM104_ENABLE */ lexer->state = ASTRAL_5; } else lexer->state = GROUND_STATE; break; case ASTRAL_4: if (c == 'A') {#ifdef RTCM104_ENABLE if (rtcm_decode(lexer, c) == ISGPS_MESSAGE) { lexer->state = RTCM_RECOGNIZED; break; }#endif /* RTCM104_ENABLE */ lexer->state = ASTRAL_2; } else lexer->state = GROUND_STATE; break; case ASTRAL_5: if (c == 'L') {#ifdef RTCM104_ENABLE if (rtcm_decode(lexer, c) == ISGPS_MESSAGE) { lexer->state = RTCM_RECOGNIZED; break; }#endif /* RTCM104_ENABLE */ lexer->state = NMEA_RECOGNIZED; } else lexer->state = GROUND_STATE; break;#endif /* TRIPMATE_ENABLE */#ifdef EARTHMATE_ENABLE case EARTHA_1: if (c == 'A') {#ifdef RTCM104_ENABLE if (rtcm_decode(lexer, c) == ISGPS_MESSAGE) { lexer->state = RTCM_RECOGNIZED; break; }#endif /* RTCM104_ENABLE */ lexer->state = EARTHA_2; } else lexer->state = GROUND_STATE; break; case EARTHA_2: if (c == 'R') {#ifdef RTCM104_ENABLE if (rtcm_decode(lexer, c) == ISGPS_MESSAGE) { lexer->state = RTCM_RECOGNIZED; break; }#endif /* RTCM104_ENABLE */ lexer->state = EARTHA_3; } else lexer->state = GROUND_STATE; break; case EARTHA_3: if (c == 'T') {#ifdef RTCM104_ENABLE if (rtcm_decode(lexer, c) == ISGPS_MESSAGE) { lexer->state = RTCM_RECOGNIZED; break; }#endif /* RTCM104_ENABLE */ lexer->state = EARTHA_4; } else lexer->state = GROUND_STATE; break; case EARTHA_4: if (c == 'H') {#ifdef RTCM104_ENABLE if (rtcm_decode(lexer, c) == ISGPS_MESSAGE) { lexer->state = RTCM_RECOGNIZED; break; }#endif /* RTCM104_ENABLE */ lexer->state = EARTHA_5; } else lexer->state = GROUND_STATE; break; case EARTHA_5: if (c == 'A') {#ifdef RTCM104_ENABLE if (rtcm_decode(lexer, c) == ISGPS_MESSAGE) { lexer->state = RTCM_RECOGNIZED; break; }#endif /* RTCM104_ENABLE */ lexer->state = NMEA_RECOGNIZED; } else lexer->state = GROUND_STATE; break;#endif /* EARTHMATE_ENABLE */ case SIRF_ACK_LEAD_1: if (c == 'c') lexer->state = SIRF_ACK_LEAD_2; else if (c == 'I') lexer->state = AIS_LEAD_2; else lexer->state = GROUND_STATE; break; case SIRF_ACK_LEAD_2: if (c == 'k') lexer->state = NMEA_LEADER_END; else lexer->state = GROUND_STATE; break;#endif /* NMEA_ENABLE */#ifdef SIRF_ENABLE case SIRF_LEADER_1: if (c == 0xa2) lexer->state = SIRF_LEADER_2; else lexer->state = GROUND_STATE; break; case SIRF_LEADER_2: lexer->length = (size_t)(c << 8); lexer->state = SIRF_LENGTH_1; break; case SIRF_LENGTH_1: lexer->length += c + 2; if (lexer->length <= MAX_PACKET_LENGTH) lexer->state = SIRF_PAYLOAD; else lexer->state = GROUND_STATE; break; case SIRF_PAYLOAD: if (--lexer->length == 0) lexer->state = SIRF_DELIVERED; break; case SIRF_DELIVERED: if (c == 0xb0) lexer->state = SIRF_TRAILER_1; else lexer->state = GROUND_STATE;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?