SDK Script gps-udp-client.are

gps-udp-client.are
/* DESC: This script sends the local GPS NMEA stream to a remote UDP server.
 * Copyright (C) 2013-2015 NetModule AG, Switzerland
 */
 
void usage() 
{
    nb_syslog("Usage: gps-udp-client.are <server> <port>");
    exit(1);
}
 
int connect_gpsd ()
{
    for (attempt = 0; attempt < 60; attempt++) {
        sleep(1);
 
        sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (sock < 0) {
            nb_syslog("Unable to create socket");
            continue;
        }
        nb_syslog("Connecting to GPS daemon");
        if (connect(sock, "127.0.0.1", 2947) < 0){
            nb_syslog("Could not connect to GPS daemon");
            close(sock);
            continue;
        }
        nb_syslog("Requesting raw NMEA stream");
        if (send(sock, "R\n") < 0) {
            nb_syslog("Unable to request raw NMEA stream");
            close(sock);
            continue;
        }
 
        nb_syslog("Successfully connected to GPS daemon");
        return sock;
    }
 
    return -1;
}
 
if (argc < 2) {
    usage();
}
 
SERVER = trim((string) argv[1]);
PORT = (int) argv[2];
 
gpsd = connect_gpsd();
if (gpsd < 0) {
    exit(1);
}
 
/* open UDP server socket */
server = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (server < 0) {
    nb_syslog("Unable to open server socket");
    close(gpsd);
    exit(1);
}
 
/* process NMEA stream */
nb_syslog("Processing NMEA data");
 
while (1) {
    /* wait for socket data  */
    rv = select(gpsd, 3);
    if (rv > 0) {
        data = recv(gpsd);
        len = strlen(data);
    } else if (rv < 0) {
        nb_syslog("select failed");
        len = -1;
    } else {
        /* nothing received */
        continue;
    } 
    if (len <= 0) {
        nb_syslog("Receive failed, re-connecting to GPS daemon");
        close(gpsd);
        gpsd = connect_gpsd();
        if (gpsd < 0) {
            exit(1);
        }
    } else {
 
       /* Convert message $GN to $GP */
       /* if (strstr(data,"$GNGGA")==0) {
        *  a_arr = explode(data);
        *  a_arr[2]="P";
        *  data = implode(a_arr);
        * }
	*/
       /* skip all messages different from GPGGA */
       /* if (strstr(data,"$GPGGA")==())
        *  continue;
        */          
        sent = sendto(server, data, SERVER, PORT);
        if (sent != len) {
            nb_syslog("Unable to send message to %s:%d", SERVER, PORT);
        }
    }
}
 
close(gpsd);
close(server);
 
exit(0);