This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
sdk:scripts:gps-udp-client [2017/10/11 16:12] – [SDK Script gps-udp-broadcast.are] 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(" |
- | | + | |
- | } | + | } |
- | | + | for (attempt = 0; attempt <= 5; attempt++) { |
- | if (connect(sock, " | + | |
- | nb_syslog(" | + | |
- | | + | if (connect(gpsd, " |
- | | + | nb_syslog(" |
- | } | + | |
- | nb_syslog(" | + | if (attempt == 3) { |
- | | + | nb_syslog(" |
- | nb_syslog(" | + | |
- | close(sock); | + | |
- | | + | } else { |
+ | break; | ||
} | } | ||
+ | } | ||
+ | nb_syslog(" | ||
+ | send(gpsd," | ||
- | | + | |
- | return sock; | + | |
- | } | + | |
- | 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 { | + | |
- | sent = sendto(server, data, SERVER, PORT); | + | |
- | if (sent != len) { | + | |
- | nb_syslog(" | + | |
} | } | ||
} | } | ||
- | } | ||
- | close(gpsd); | + | /* wait for client socket data */ |
- | close(server); | + | rv = select(gpsd, 3); |
- | exit(0); | + | if (rv == -1) { |
- | + | nb_syslog(" | |
- | </ | + | |
- | + | | |
- | ====== SDK Script gps-udp-broadcast.are ====== | + | |
- | <code c gps-udp-broadcast.are> | + | |
- | /* DESC: This script sends the local GPS NMEA via UDP broadcast to multiple ports | + | |
- | * selected by NMEA type. | + | |
- | * Copyright (C) 2013-2017 NetModule AG, Switzerland | + | |
- | */ | + | |
- | BroadcastIP = " | + | |
- | + | ||
- | // Connect to GPS Deamon | + | |
- | int connect_gpsd() { | + | |
- | nb_syslog(" | + | |
- | sock = -1; | + | |
- | while(true) | + | |
- | { | + | |
- | sock = socket(AF_INET, | + | |
- | if (sock < 0) | + | |
- | { | + | |
- | | + | |
- | | + | |
continue; | continue; | ||
- | | + | |
- | + | | |
- | | + | |
- | | + | |
- | | + | |
- | close(sock); | + | |
- | sleep(5); | + | |
continue; | continue; | ||
- | | + | |
- | nb_syslog(" | + | data = recv(gpsd); |
- | | + | len = strlen(data); |
- | | + | |
+ | if (len == 0) { | ||
+ | nb_syslog(" | ||
+ | | ||
+ | | ||
+ | continue; | ||
+ | } | ||
+ | |||
+ | sent = sendto(server, | ||
+ | if (sent != len) { | ||
+ | nb_syslog(" | ||
} | } | ||
} | } | ||
- | + | if (gpsd > -1) close(gpsd); | |
+ | if (server > -1) close(server); | ||
+ | |||
+ | exit(-1); | ||
- | gpsd = connect_gpsd(); | ||
- | /* open UDP server socket */ | ||
- | server = socket(AF_INET, | ||
- | if (server < 0) { | ||
- | | ||
- | | ||
- | | ||
- | } | ||
- | setsockopt(server, | ||
- | nb_syslog(" | ||
- | // process NMEA stream | ||
- | |||
- | nb_syslog(" | ||
- | |||
- | while (true) { | ||
- | /* wait for socket data */ | ||
- | rv = select(gpsd, | ||
- | if (rv > 0) { | ||
- | data = recv(gpsd); | ||
- | len = strlen(data); | ||
- | } else if (rv < 0) { | ||
- | nb_syslog(" | ||
- | len = -1; | ||
- | } else { // nothing received | ||
- | continue; | ||
- | | ||
- | if (len <= 0) { | ||
- | nb_syslog(" | ||
- | close(gpsd); | ||
- | gpsd = connect_gpsd(); | ||
- | if (gpsd < 0) | ||
- | | ||
- | } else { | ||
- | // printf(" | ||
- | while(true) | ||
- | { | ||
- | newlinepos = strstr(data, | ||
- | seplen = 0; | ||
- | if (newlinepos != ()) { | ||
- | | ||
- | } else { | ||
- | | ||
- | if (newlinepos != ()) { | ||
- | seplen = 1; | ||
- | } | ||
- | } | ||
- | | ||
- | if (seplen != 0) { | ||
- | frame = strcat(left(data, | ||
- | // printf(" | ||
- | if (left(frame, | ||
- | sendto(server, | ||
- | sendto(server, | ||
- | sendto(server, | ||
- | } | ||
- | if (left(frame, | ||
- | sendto(server, | ||
- | } | ||
- | if (left(frame, | ||
- | sendto(server, | ||
- | | ||
- | data = substr(data, | ||
- | } | ||
- | else | ||
- | | ||
- | | ||
- | } | ||
- | } | ||
- | } | ||
- | exit(-3); | ||
</ | </ | ||
+ | |||