This is an old revision of the document!


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 {
        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);

SDK Script gps-udp-client.are

gps-udp-broadcast.are
/* DESC: This script sends the local GPS NMEA via UDP broadcast to multiple ports.
 * Copyright (C) 2013-2017 NetModule AG, Switzerland
 */
 
BroadcastIP = "172.21.132.255";
 
// Connect to GPS Deamon
int connect_gpsd() {
    nb_syslog("Connecting to GPS daemon.");
    sock = -1;
    while(true)
    {
        sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (sock < 0)
        {
            nb_syslog("Unable to open TCP socket. Retrying in 5 seconds\n");
            sleep(5);
            continue;
        }
 
        if (connect(sock, "127.0.0.1", 2947) < 0)
        {
            nb_syslog("Could not connect to GPS daemon. Retrying in 5 seconds\n");
            close(sock);
            sleep(5);
            continue;
        }
		nb_syslog("GPS daemon connected.");
		return(sock);
        break;
    }
}
 
 
 
gpsd = connect_gpsd();
 
/* 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);
}
setsockopt(server, SOL_SOCKET, SO_BROADCAST, 1);
 
nb_syslog("GPS Broadcast to %s", BroadcastIP);
// process NMEA stream 
 
nb_syslog("Processing NMEA data");
 
while (true) {
	/* 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(-2);
		}
	} else {
		// printf("got >>>%s<<<\n", data);
		while(true)   //handle CR /LF problems and separate data into singel $Gxxx frames
			{
				newlinepos = strstr(data, "\r\n");
				seplen = 0;
				if (newlinepos != ()) {
					seplen = 2;
				} else {
					newlinepos = strstr(data, "\n");
					if (newlinepos != ()) {
						seplen = 1;
					}
				}
 
				if (seplen != 0)
				{
					frame = strcat(left(data, newlinepos), "\r\n");
					// printf("frame >>>%s<<<\n", frame);
					if (left(frame, 6) == "$GPRMC") {
						//printf("send $GPRMC\n");
						if (sendto(server, frame, BroadcastIP, 5125) != len)
							nb_syslog("Unable to broadcast message $GPRMC to port 5125");
						if (sendto(server, frame, BroadcastIP, 5126) != len)
							nb_syslog("Unable to broadcast message $GPRMC to port 5126");
						if (sendto(server, frame, BroadcastIP, 6060) != len)
							nb_syslog("Unable to broadcast message $GPRMC to port 6060");
					}
					if (left(frame, 6) == "$GPGGA") {
						//printf("send $GPGGA\n");
						if (sendto(server, frame, BroadcastIP, 5126) != len)
							nb_syslog("Unable to broadcast message $GPGGA to port 5126");
					}
					if (left(frame, 6) == "$GPGSA") {
						//printf("send $GPGSA\n");
						if (sendto(server, frame, BroadcastIP, 5126) != len)
							nb_syslog("Unable to broadcast message $GPGGA to port 5126");
					}		
					data = substr(data, newlinepos + seplen);
				}
				else
				{
					break;
				}
			}
 
 
 
 
 
	}
}
exit(-3);