/* DESC: This script sends the local GPS NMEA stream to a remote UDP server. * Copyright (C) 2013-2015 NetModule AG, Switzerland */ void usage() { printf("usage: gps-udp-client.are <server> <port>\n"); exit(1); } int gpsd_restart () { nb_syslog("Restarting GPS daemon"); nb_config_set("gpsd.0.status=0"); sleep(3); nb_config_set("gpsd.0.status=1"); sleep(5); return 0; } int gpsd_connect () { nb_syslog("Connecting to GPS daemon"); gpsd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (gpsd < 0) { nb_syslog("ERROR: Unable to open socket"); exit(-1); } for (attempt = 0; attempt <= 5; attempt++) { sleep(3); if (connect(gpsd, "127.0.0.1", 2947) < 0) { nb_syslog("Could not connect to daemon"); if (attempt == 3) { nb_syslog("Unable to connect, restarting daemon"); gpsd_restart(); } } else { break; } } nb_syslog("Connected to daemon, requesting NMEA"); send(gpsd,"R=1\n"); nb_syslog("Processing NMEA"); return gpsd; } if (argc < 2) { usage(); } SERVER = trim((string) argv[1]); PORT = (int) argv[2]; gpsd = -1; /* open UDP server socket */ server = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (server < 0) { nb_syslog("Unable to open socket"); exit(-1); } /* process NMEA stream */ nb_syslog("Processing NMEA data"); while (1) { /* connect to gpsd */ if (gpsd < 0) { gpsd = gpsd_connect(gpsd); if (gpsd < 0) { nb_syslog("ERROR: Unable to connect to daemon"); sleep(5); continue; } } /* wait for client socket data */ rv = select(gpsd, 3); if (rv == -1) { nb_syslog("ERROR: select failed, re-connecting"); close(gpsd); gpsd = -1; continue; } else if (rv == 0) { /* nothing received */ continue; } data = recv(gpsd); len = strlen(data); if (len == 0) { nb_syslog("ERROR: no data, re-connecting"); close(gpsd); gpsd = -1; continue; } sent = sendto(server, data, SERVER, PORT); if (sent != len) { nb_syslog("ERROR: Unable to send %d bytes to %s:%d", len, SERVER, PORT); } } if (gpsd > -1) close(gpsd); if (server > -1) close(server); exit(-1);