This is an old revision of the document!


SDK Script gps-udp-client-GNSStoGPS.are

local_ip = “127.0.0.1”; local_port = 2947;

gpsd = -1;

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, local_ip, local_port) < 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 = trim1)

1)
string) argv[1]); PORT = (int) argv[2]; server = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if(server < 0) {
nb_syslog("Unable to open socket");
exit(-1);
} 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);
printf("received %d\n", len);
/* Check for correct framing: CR+LF */
if(len > 2) {
	if(strrchr(data, "\r") != len-2 || strrchr(data, "\n") != len-1) {
		nb_syslog("ERROR: NMEA framing wrong");
		close(gpsd);
		gpsd = -1;
		gpsd_restart();
		continue;
	}
}
if(len == 0) {
	nb_syslog("ERROR: no data, re-connecting");
	close(gpsd);
	gpsd = -1;
	continue;
}
/* Convert message $GN to $GP for backward compatibility */
printf("vor %s\n", data);
if(strstr(data, "$GN") == 0) {;
	a_arr = explode(data);
	a_arr[2] = "P";
	data = implode(a_arr);
}
printf("nach %s\n", data);
/* skip all messages different from GPGGA 
* if(strstr(data,"$GPGGA")==())
* 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);</code>