Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Examples

vos/corelibs/tracking/gpsd_position.cc

Go to the documentation of this file.
00001 
00002 /*
00003 
00004     Copyright (C) 2002 Reed Hedges
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Lesser General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Lesser General Public License for more details.
00015 
00016     You should have received a copy of the GNU Lesser General Public
00017     License along with this library; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00019 
00020 */
00021 
00022 #include <errno.h>
00023 #include <sys/types.h>
00024 #include <sys/socket.h>
00025 #include <netdb.h>
00026 #include <netinet/in.h>
00027 #include <string>
00028 
00029 #include "gpsd_position.hh"
00030 
00031 using namespace std;
00032 
00033 /* Constructor */
00034 GPSDPosition::GPSDPosition(char* host, int port) throw(DeviceError) :
00035     socket_fd(-1), gpsd_host(host), gpsd_port(port)
00036 {
00037     struct sockaddr_in address;
00038     if( (socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
00039         throw DeviceError(string("Error creating TCP socket: ") + strerror(errno));
00040     }
00041     struct hostent* he;
00042     if(!(he = gethostbyname(gpsd_host))) {
00043         throw DeviceError(string("Invalid address for GPS daemon: ") + gpsd_host);
00044     }
00045     memset(&address, 0, sizeof(address));
00046     address.sin_port=htons(gpsd_port);
00047     address.sin_family=AF_INET;
00048     address.sin_addr=*((struct in_addr *)he->h_addr);
00049     if( connect(socket_fd, (struct sockaddr *)&address, sizeof(address)) == -1)
00050 {
00051         char port_s[16];
00052         sprintf(port_s, "%d", gpsd_port);
00053         throw DeviceError(string("Error connecting to GPS daemon ") + gpsd_host + ":" + port_s + ". " + strerror(errno));
00054     }
00055 }
00056 
00057 /* Destructor */
00058 GPSDPosition::~GPSDPosition() {
00059     if ( shutdown(socket_fd, 2) < 0 ) {
00060         fprintf(stderr, "GPSDPosition: Warning: error closing socket. %s\n",  strerror(errno));
00061     }
00062 }
00063 
00064 
00065 
00066 /* gpsd commands are:
00067 
00068     Command  Reply             Meaning
00069 
00070     P/p      GPSD,P=%f %f      position, where %f %f are latitude and longitude
00071 values
00072     D/d      GPSD,D=%s         time?? ('utc')
00073     A/a      GPSD,A=%f         altitude (%f is value)
00074     V/v      GPSD,V=%f         velocity (%f is value-- m/sec??)
00075     R/r      GPSD,R=%d         reset?? (where %i is 0 for ok and 1 for error??)
00076     S/s      GPSD,S=%d         status (0 = ok? 1 = error?)
00077     M/m      GPSD,M=%d         mode??
00078 
00079 */
00080 
00081 
00082 
00083 
00084 /* get position */
00085 void GPSDPosition::getPosition(double *x, double *y, double *z) throw(DeviceError) {
00086     char msg[128];
00087     int nrecvd;
00088 
00089     if(send(socket_fd, "P\n", 2, 0) != 2)
00090         throw DeviceError("Error sending command to gpsd!");
00091     nrecvd = recv(socket_fd, msg, 128, 0);
00092     sscanf(msg, "GPSD,P=%lf %lf", x, z);
00093 
00094     if(send(socket_fd, "A\n", 2, 0) != 2)
00095         throw DeviceError("Error sending command to gpsd!");
00096     nrecvd = recv(socket_fd, msg, 128, 0);
00097     sscanf(msg, "GPSD,A=%lf", y);
00098 
00099     *x *= xScale;
00100     *y *= yScale;
00101     *z *= zScale;
00102     *x += xOffset;
00103     *y += yOffset;
00104     *z += zOffset;
00105 }
00106 
00107 /* get status */
00108 int GPSDPosition::getStatus() throw(DeviceError){
00109     int status;
00110     char msg[128];
00111     if(send(socket_fd, "S\n", 2, 0) != 2) {
00112         throw DeviceError("Error sending command to gpsd!");
00113         return -1;
00114     }
00115     recv(socket_fd, msg, 128, 0);
00116     sscanf(msg, "GPSD,S=%d", &status);
00117     return status;
00118 }

Generated on Tue Aug 12 03:55:37 2003 for Interreality Project - VOS by doxygen 1.3.2