This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| sdk:scripts:gps-udp-client [2019/07/08 12:47] – fachet | sdk:scripts:gps-udp-client [2022/09/05 12:04] (current) – schmitt | ||
|---|---|---|---|
| Line 6: | Line 6: | ||
| */ | */ | ||
| - | void usage() | + | void usage() { |
| + | printf(" | ||
| + | exit(1); | ||
| + | } | ||
| + | |||
| + | int gpsd_restart | ||
| { | { | ||
| - | nb_syslog(" | + | nb_syslog(" |
| - | | + | |
| + | nb_config_set(" | ||
| + | | ||
| + | nb_config_set(" | ||
| + | sleep(5); | ||
| + | |||
| + | return 0; | ||
| } | } | ||
| - | int connect_gpsd | + | int gpsd_connect |
| { | { | ||
| - | | + | |
| - | sleep(1); | + | |
| - | sock = socket(AF_INET, | + | gpsd = socket(AF_INET, |
| - | if (sock < 0) { | + | if (gpsd < 0) { |
| - | nb_syslog(" | + | nb_syslog(" |
| - | | + | |
| - | } | + | } |
| - | nb_syslog(" | + | |
| - | if (connect(sock, | + | sleep(3); |
| - | nb_syslog(" | + | |
| - | | + | |
| - | continue; | + | |
| - | | + | |
| - | | + | |
| - | if (send(sock, " | + | |
| - | | + | |
| - | close(sock); | + | |
| - | continue; | + | |
| - | } | + | |
| - | nb_syslog(" | + | |
| - | | + | |
| + | |||
| + | if (attempt == 3) { | ||
| + | nb_syslog(" | ||
| + | gpsd_restart(); | ||
| + | } | ||
| + | | ||
| + | break; | ||
| + | } | ||
| } | } | ||
| + | nb_syslog(" | ||
| + | send(gpsd," | ||
| + | |||
| + | nb_syslog(" | ||
| - | return | + | return |
| } | } | ||
| if (argc < 2) { | if (argc < 2) { | ||
| - | | + | usage(); |
| } | } | ||
| SERVER = trim((string) argv[1]); | SERVER = trim((string) argv[1]); | ||
| PORT = (int) argv[2]; | PORT = (int) argv[2]; | ||
| - | + | gpsd = -1; | |
| - | gpsd = connect_gpsd(); | + | |
| - | if (gpsd < 0) { | + | |
| - | exit(1); | + | |
| - | } | + | |
| /* open UDP server socket */ | /* open UDP server socket */ | ||
| server = socket(AF_INET, | server = socket(AF_INET, | ||
| if (server < 0) { | if (server < 0) { | ||
| - | | + | nb_syslog(" |
| - | | + | exit(-1); |
| - | | + | |
| } | } | ||
| /* process NMEA stream */ | /* process NMEA stream */ | ||
| nb_syslog(" | nb_syslog(" | ||
| - | |||
| while (1) { | while (1) { | ||
| - | /* wait for socket data | + | /* connect to gpsd */ |
| - | rv = select(gpsd, | + | if (gpsd < 0) { |
| - | if (rv > 0) { | + | |
| - | data = recv(gpsd); | + | |
| - | len = strlen(data); | + | |
| - | } else if (rv < 0) { | + | |
| - | | + | |
| - | len = -1; | + | |
| - | } else { | + | |
| - | /* nothing received */ | + | |
| - | continue; | + | |
| - | } | + | |
| - | if (len <= 0) { | + | |
| - | nb_syslog(" | + | |
| - | close(gpsd); | + | |
| - | gpsd = connect_gpsd(); | + | |
| if (gpsd < 0) { | if (gpsd < 0) { | ||
| - | exit(1); | + | nb_syslog(" |
| - | } | + | sleep(5); |
| - | } else { | + | continue; |
| - | + | ||
| - | /* Convert message $GN to $GP */ | + | |
| - | /* if (strstr(data, | + | |
| - | * a_arr = explode(data); | + | |
| - | | + | |
| - | * data = implode(a_arr); | + | |
| - | * } | + | |
| - | */ | + | |
| - | /* skip all messages different from GPGGA */ | + | |
| - | /* if (strstr(data," | + | |
| - | * | + | |
| - | */ | + | |
| - | sent = sendto(server, | + | |
| - | if (sent != len) { | + | |
| - | nb_syslog(" | + | |
| } | } | ||
| + | } | ||
| + | |||
| + | /* wait for client socket data */ | ||
| + | rv = select(gpsd, | ||
| + | |||
| + | if (rv == -1) { | ||
| + | nb_syslog(" | ||
| + | close(gpsd); | ||
| + | gpsd = -1; | ||
| + | continue; | ||
| + | } else if (rv == 0) { | ||
| + | /* nothing received */ | ||
| + | continue; | ||
| + | } | ||
| + | data = recv(gpsd); | ||
| + | len = strlen(data); | ||
| + | |||
| + | if (len == 0) { | ||
| + | nb_syslog(" | ||
| + | close(gpsd); | ||
| + | gpsd = -1; | ||
| + | continue; | ||
| + | } | ||
| + | |||
| + | sent = sendto(server, | ||
| + | if (sent != len) { | ||
| + | nb_syslog(" | ||
| } | } | ||
| } | } | ||
| - | close(gpsd); | + | if (gpsd > -1) close(gpsd); |
| - | close(server); | + | if (server > -1) close(server); |
| + | |||
| + | exit(-1); | ||
| - | exit(0); | ||
| </ | </ | ||