00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <include.h>
00024 #define DEFAULTHOST "localhost"
00025 #define DEFAULTPORT "2947"
00026 int GPSDsock;
00027 float lat, lon, alt, spd, hed;
00028 float last_lat, last_lon, last_hed;
00029 struct sockaddr_in servaddr, localaddr;
00030 int mode;
00031
00032 const char gpsd_command[] = "PAVMH\n";
00033
00034 int OpenGPSD(void)
00035 {
00036 int save_mode;
00037
00038 GPSDsock = -1;
00039 lat = lon = alt = spd = hed = 0;
00040 last_lat = last_lon = last_hed = 0;
00041 mode = -1;
00042
00043 host = strdup(DEFAULTHOST);
00044 port = DEFAULTPORT;
00045
00046
00047 h = gethostbyname(host);
00048 if (h == NULL) {
00049 return GPSD_UNKNOWN_HOST;
00050 }
00051
00052 servaddr.sin_family = h->h_addrtype;
00053 memcpy((char *) &servaddr.sin_addr.s_addr, h->h_addr_list[0],
00054 h->h_length);
00055 servaddr.sin_port = htons(port);
00056
00057
00058 if ((GPSDsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
00059 return GPSD_CANT_OPEN_SOCKET;
00060 }
00061
00062 localaddr.sin_family = AF_INET;
00063 localaddr.sin_addr.s_addr = htonl(INADDR_ANY);
00064 localaddr.sin_port = htons(0);
00065
00066 if (bind(sock, (struct sockaddr *) &localaddr, sizeof(localaddr)) < 0) {
00067 return GPSD_CANT_BIND_PORT;
00068 }
00069
00070 if (connect(sock, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) {
00071 return GPSD_CANT_CONNECT;
00072 }
00073
00074 save_mode = fcntl(sock, F_GETFL, 0);
00075 fcntl(sock, F_SETFL, save_mode | O_NONBLOCK);
00076
00077 }
00078
00079 return 1;
00080 }
00081
00082 GetGPSPos(float lat, float lon)
00083 {
00084 const gpsd_command = "P";
00085 int result;
00086
00087
00088 if (write(sock, gpsd_command, sizeof(gpsd_command)) < 0) {
00089 if (errno != EAGAIN) {
00090 close(GPSDsock);
00091 GPSDsock = -1;
00092 return GPSD_WRITE_ERROR;
00093 }
00094 }
00095 result = read(sock, buf, 1024);
00096 return NO_ERROR;
00097 }
00098