This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| sdk:gps-tcp-server-gnsstogps [2020/04/28 18:08] – created juraschek | sdk:gps-tcp-server-gnsstogps [2020/07/21 14:33] (current) – [tcp server proxy script] fachet | ||
|---|---|---|---|
| Line 57: | Line 57: | ||
| void usage() { | void usage() { | ||
| - | printf(" | + | |
| - | exit(1); | + | exit(1); |
| } | } | ||
| int gpsd_restart () | int gpsd_restart () | ||
| { | { | ||
| - | nb_syslog(" | + | |
| - | nb_config_set(" | + | |
| - | sleep(3); | + | sleep(3); |
| - | nb_config_set(" | + | nb_config_set(" |
| - | sleep(5); | + | sleep(5); |
| - | return 0; | + | |
| } | } | ||
| int gpsd_connect () | int gpsd_connect () | ||
| { | { | ||
| - | nb_syslog(" | + | |
| - | gpsd = socket(AF_INET, | + | |
| - | if (gpsd < 0) { | + | if (gpsd < 0) { |
| - | nb_syslog(" | + | nb_syslog(" |
| - | exit(-1); | + | exit(-1); |
| - | } | + | } |
| - | for (attempt = 0; attempt <= 5; attempt++) { | + | for (attempt = 0; attempt <= 5; attempt++) { |
| - | sleep(3); | + | sleep(3); |
| - | if (connect(gpsd, | + | |
| - | nb_syslog(" | + | nb_syslog(" |
| - | if (attempt == 3) { | + | if (attempt == 3) { |
| - | nb_syslog(" | + | nb_syslog(" |
| - | gpsd_restart(); | + | gpsd_restart(); |
| - | } | + | } |
| - | } else { | + | } else { |
| - | break; | + | break; |
| - | } | + | } |
| - | } | + | } |
| - | nb_syslog(" | + | nb_syslog(" |
| - | if (nb_config_get(" | + | if (nb_config_get(" |
| - | nb_syslog(" | + | |
| - | return gpsd; | + | |
| } | } | ||
| string checksum(string s) | string checksum(string s) | ||
| { | { | ||
| - | pos = strchr(s, " | + | |
| - | if (is_void(pos)) | + | |
| - | return s; /* no checksum to correct */ | + | return s; /* no checksum to correct */ |
| - | cs = 0; | + | |
| - | chars = explode(s); | + | chars = explode(s); |
| for (i = 1; i < pos; i++) { /* skip $ and *XX */ | for (i = 1; i < pos; i++) { /* skip $ and *XX */ | ||
| - | | + | c = ord(chars[i]); |
| cs ^= c; | cs ^= c; | ||
| } | } | ||
| r = sprintf(" | r = sprintf(" | ||
| - | return r; | + | |
| } | } | ||
| /* main() */ | /* main() */ | ||
| - | if (argc < 2) { | + | if (argc < 2) |
| usage(); | usage(); | ||
| - | } | ||
| - | |||
| /* open TCP server socket */ | /* open TCP server socket */ | ||
| server = socket(AF_INET, | server = socket(AF_INET, | ||
| if (server < 0) { | if (server < 0) { | ||
| - | nb_syslog(" | + | |
| - | exit(-1); | + | exit(-1); |
| } | } | ||
| - | |||
| ret = bind(server, | ret = bind(server, | ||
| Line 145: | Line 142: | ||
| if (ret == -1) { | if (ret == -1) { | ||
| printf(" | printf(" | ||
| - | | + | |
| - | exit(1); | + | exit(1); |
| } | } | ||
| Line 154: | Line 150: | ||
| client = accept(server); | client = accept(server); | ||
| if (client < 0) { | if (client < 0) { | ||
| - | | + | nb_syslog(" |
| sleep(1); | sleep(1); | ||
| continue; | continue; | ||
| - | } else { | + | } else { |
| - | nb_syslog(" | + | nb_syslog(" |
| - | | + | clients=mkarray(client); |
| - | } | + | } |
| clientConnection=true; | clientConnection=true; | ||
| nb_syslog(" | nb_syslog(" | ||
| - | // Sending NMEA Loop | + | |
| - | /* process NMEA stream */ | + | |
| - | nb_syslog(" | + | nb_syslog(" |
| - | while(clientConnection) { | + | while (clientConnection) { |
| - | /* connect to gpsd */ | + | /* connect to gpsd */ |
| - | if(gpsd < 0) { | + | if (gpsd < 0) { |
| - | gpsd = gpsd_connect(gpsd); | + | gpsd = gpsd_connect(gpsd); |
| - | if(gpsd < 0) { | + | if (gpsd < 0) { |
| nb_syslog(" | nb_syslog(" | ||
| sleep(5); | sleep(5); | ||
| - | continue; | + | |
| - | } | + | } |
| - | } | + | } |
| - | /* wait for client socket data */ | + | |
| - | rv = select(array_merge(clients, | + | rv = select(array_merge(clients, |
| - | if(rv == -1) { | + | |
| - | nb_syslog(" | + | nb_syslog(" |
| - | close(gpsd); | + | close(gpsd); |
| - | gpsd = -1; | + | gpsd = -1; |
| - | continue; | + | continue; |
| - | } else if(rv == 0) { | + | } else if (rv == 0) { |
| - | /* nothing received */ | + | /* nothing received */ |
| - | continue; | + | continue; |
| - | } else if(rv == server) { | + | } else if(rv == server) { |
| newclient = accept(server); | newclient = accept(server); | ||
| if (newclient < 0) { | if (newclient < 0) { | ||
| - | nb_syslog(" | + | nb_syslog(" |
| - | continue; | + | continue; |
| } else { | } else { | ||
| - | nb_syslog(" | + | |
| - | clients=array_merge(clients, | + | clients=array_merge(clients, |
| - | | + | |
| - | } | + | } |
| | | ||
| + | data = recv(gpsd); | ||
| + | len = strlen(data); | ||
| + | /* Check for correct framing: CR+LF */ | ||
| + | if (len > 2) { | ||
| + | if (strrchr(data, | ||
| + | nb_syslog(" | ||
| + | close(gpsd); | ||
| + | gpsd = -1; | ||
| + | gpsd_restart(); | ||
| + | continue; | ||
| + | } | ||
| + | } | ||
| - | + | | |
| - | + | nb_syslog(" | |
| - | data = recv(gpsd); | + | close(gpsd); |
| - | len = strlen(data); | + | gpsd = -1; |
| - | /* Check for correct framing: CR+LF */ | + | continue; |
| - | if(len > 2) { | + | } |
| - | if(strrchr(data, | + | /* Convert message $GN to $GP for backward compatibility */ |
| - | nb_syslog(" | + | if (strstr(data, |
| - | close(gpsd); | + | a_arr = explode(data); |
| - | gpsd = -1; | + | a_arr[2] = " |
| - | gpsd_restart(); | + | data = checksum(implode(a_arr)); |
| - | continue; | + | } |
| - | } | + | /* skip all messages different from GPGGA |
| - | } | + | * if(strstr(data," |
| - | + | * continue; | |
| - | if(len == 0) { | + | */ |
| - | nb_syslog(" | + | for (i=0; |
| - | close(gpsd); | + | sent = send(clients[i], |
| - | gpsd = -1; | + | if (sent < 0) { |
| - | continue; | + | nb_syslog(" |
| - | } | + | clients=array_compact(array_unset(clients, |
| - | /* Convert message $GN to $GP for backward compatibility */ | + | } |
| - | if(strstr(data, | + | } |
| - | a_arr = explode(data); | + | } |
| - | a_arr[2] = " | + | |
| - | data = checksum(implode(a_arr)); | + | |
| - | } | + | |
| - | /* skip all messages different from GPGGA | + | |
| - | * if(strstr(data," | + | |
| - | * continue; | + | |
| - | */ | + | |
| - | | + | |
| - | sent = send(clients[i], | + | |
| - | if(sent < 0 ) { | + | |
| - | nb_syslog(" | + | |
| - | clients=array_compact(array_unset(clients, | + | |
| - | } | + | |
| - | } | + | |
| - | } | + | |
| Line 248: | Line 241: | ||
| } | } | ||
| - | if (gpsd > -1) { | + | if (gpsd > -1) |
| - | close(gpsd); | + | close(gpsd); |
| - | } | + | if (server > -1) |
| - | if (server > -1) { | + | close(server); |
| - | close(server); | + | |
| - | } | + | |
| exit(-1); | exit(-1); | ||
| - | |||
| - | |||
| </ | </ | ||