gps.c

Go to the documentation of this file.
00001 /*
00002  * File : gps.c
00003  * Project : WifiScanner (c) 2002-2007 Hervé Schauer Consultants
00004  * Usage : This utility is written for use with IEEE 802.11 adapters based
00005  * on Intersil's PRISM II chipset (PCMCIA).
00006  * 
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020  *
00021  * $Id: $
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   // Find the host
00047   h = gethostbyname(host);
00048   if (h == NULL) {
00049     return GPSD_UNKNOWN_HOST;
00050   }
00051   // Fill in the server
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   // Create the socket
00058   if ((GPSDsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
00059     return GPSD_CANT_OPEN_SOCKET;
00060   }
00061   // Bind to any local port
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   // Connect
00070   if (connect(sock, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) {
00071     return GPSD_CANT_CONNECT;
00072   }
00073   // Set nonblocking mode
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   // Kick a command off into the system, too
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 

Generated on Fri Jul 25 17:10:33 2008 for WifiScanner by  doxygen 1.5.5