/**
 * @file
 * $Id$
 * $Revision$
 * $Author$
 * $Date$
 *
 * This file is part of The iWear Framework.
 *
 * The iWear Framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation as in version 2 of the License.

 *
 * The iWear Framework is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * The iWear Framework; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef GPS_H
#define GPS_H

#include <netdb.h>

#define SERVER_PORT 2947 // gpsd listens on port 2947

using namespace std;

namespace iwear
{
    namespace sensor
    {
        namespace location
        {

struct struct_gps {
    double latitude;
    double longitude;
    time_t date;
    double altitude;
    double speed;       /* Speed over ground, kmh */
    int    status;      /* 0 = no fix, 1 = fix, 2 = dgps fix */
    int    mode;        /* 1 = no fix, 2 = 2D, 3 = 3D */
    double heading;

    /* if satellites is 0, then you haven`t right values !!! */
    int    satellites;  /* Number of satellites used in solution */
    double pdop;        /* Position dilution of precision */
    double hdop;        /* Horizontal dilution of precision */
    double vdop;        /* Vertical dilution of precision */
    double pquali; /* Quality of the measurement (total) */
    double hquali; /* Quality of the measurement (longitude and latitude) */
    double vquali; /* Quality of the measurement (altitude) */

    char   last_date[12]; /* last date an update arrived */
    char   last_time[10]; /* last time an update arrived */
};

/**
 * @short GPS-Client which reads data from server and put it into struct
 *
 * GPS module of the iWear Project.
 * It works with an modified version of gpsd (returns the data from gps hardware)
 * and connects to it via a socket. Then it sends asks to it and get answers.
 * The whole data will then be put into the struct (at top)
 */
class GPS {/*{{{*/
private:
  /**
    * Global socket descriptor
    */
  int i_serverSocket;

  /**
    * Global receiving result
    */
  mutable int i_receiveResult;

  /**
   * Global variable for representing connection status to gpsd
   */
  bool b_connected;

  /**
    * Asks the server and gets the asnwer to parameter
    * @param pc_datatype is the question string for gpsd
    * @return The answer string from gpsd
    */
  char* getData(char* pc_datatype) const;

public:
  /**
    * Constructor to set connection status
    */
  GPS(char* pc_hostname = "localhost", int i_port = SERVER_PORT);

  /*
   * Destructor to close the socket connection
   */
  virtual ~GPS();

  /**
    * @name Operating functions
    * @{
    */

  /**
    * Asks the server and gets the asnwer to parameter
    * @param pc_datatype is the answer string from gpsd
    * @return true if you get new values
    */
  bool updateData(struct_gps &gps_data) const;

  /**
    * Returns the connection status with the gpsd
    * @return true if there is a connection to the gpsd
    */
  bool isConnected( void );

  /**
    * Opens the socket to host at port
    * @param pc_hostname is the host to connect to
    * @param port is the destination port for the connection
    * @return reserved
    */
  int openSocket(char* pc_hostname = "localhost", int i_port = SERVER_PORT);

  /**
    * @}
    */
};/*}}}*/
} // namespace iwear
}
}
#endif
