This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
sdk:scripts:gps-upd-client-compat [2016/06/29 13:41] juraschek removed |
— (current) | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== SDK Script gps-upd-client-compat.are ====== | ||
- | <code c gps-upd-client-compat.are> | ||
- | /* DESC: This script sends the local GPS NMEA stream to a remote UDP server (incl. device identity). | ||
- | * Copyright (C) 2013 NetModule AG, Switzerland | ||
- | */ | ||
- | |||
- | void usage() | ||
- | { | ||
- | nb_syslog("usage: gps-udp-client.are <server> <port>"); | ||
- | exit(1); | ||
- | } | ||
- | |||
- | if (argc < 2) { | ||
- | usage(); | ||
- | } | ||
- | |||
- | SERVER = trim((string) argv[1]); | ||
- | PORT = (int) argv[2]; | ||
- | |||
- | /* connect to gpsd */ | ||
- | gpsd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | ||
- | if (gpsd < 0) { | ||
- | nb_syslog("unable to open socket"); | ||
- | exit(1); | ||
- | } | ||
- | |||
- | if (connect(gpsd, "127.0.0.1", 2947) < 0){ | ||
- | nb_syslog("Could not connect to gpsd"); | ||
- | close(gpsd); | ||
- | exit(1); | ||
- | } | ||
- | |||
- | nb_syslog("Requesting NMEA"); | ||
- | send(gpsd, "R\n"); | ||
- | |||
- | /* open UDP server socket */ | ||
- | server = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | ||
- | if (server < 0) { | ||
- | nb_syslog("Unable to open socket"); | ||
- | close(gpsd); | ||
- | exit(1); | ||
- | } | ||
- | |||
- | /* process NMEA stream */ | ||
- | nb_syslog("Processing NMEA data"); | ||
- | |||
- | while(1) { | ||
- | /* wait for client socket data */ | ||
- | rv = select(gpsd, 3); | ||
- | if (rv == -1) { | ||
- | nb_syslog("ERROR: select failed"); | ||
- | break; | ||
- | } else if (rv == 0) { | ||
- | /*nb_syslog("nothing received");*/ | ||
- | continue; | ||
- | } | ||
- | data = recv(gpsd); | ||
- | status = nb_status("system"); | ||
- | serial = struct_get(status, "SERIAL_NUMBER"); | ||
- | sentence = strcat("PNMID,", serial); | ||
- | sentenceLength = strlen(sentence); | ||
- | sentenceArray = explode(sentence); | ||
- | checksum = 0; | ||
- | for (i = 0; i < sentenceLength; i++) { | ||
- | currentChar = ord(sentenceArray[i]); | ||
- | checksum ^= currentChar; | ||
- | } | ||
- | |||
- | checksum = sprintf("%02X",checksum); | ||
- | nmsentence = strcat("$", sentence , "*", checksum ,"\r\n"); | ||
- | data = strcat(data, nmsentence); | ||
- | |||
- | len = strlen(data); | ||
- | if (len <= 0) continue; | ||
- | |||
- | sent = sendto(server, data, SERVER, PORT); | ||
- | if (sent != len) { | ||
- | nb_syslog("ERROR: sento failed"); | ||
- | } | ||
- | } | ||
- | |||
- | close(gpsd); | ||
- | close(server); | ||
- | |||
- | exit(0); | ||
- | </code> | ||