00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "ArExport.h"
00028 #include "ariaOSDef.h"
00029 #include "ArGPSConnector.h"
00030 #include "ArGPS.h"
00031 #include "ArNovatelGPS.h"
00032 #include "ArDeviceConnection.h"
00033 #include "ArSerialConnection.h"
00034 #include "ArTcpConnection.h"
00035
00036 #include "Aria.h"
00037 #include <iostream>
00038
00039
00040 const int ARGPS_DEFAULT_SERIAL_BAUD = 9600;
00041 const char* const ARGPS_DEFAULT_SERIAL_PORT = ArUtil::COM2;
00042 const int ARGPS_DEFAULT_REMOTE_TCP_PORT = 8103;
00043
00044 AREXPORT ArGPSConnector::ArGPSConnector(ArArgumentParser *argParser) :
00045 myDeviceCon(NULL),
00046 myArgParser(argParser),
00047 myParseArgsCallback(this, &ArGPSConnector::parseArgs),
00048 myLogArgsCallback(this, &ArGPSConnector::logArgs),
00049 myBaud(ARGPS_DEFAULT_SERIAL_BAUD),
00050 myPort(ARGPS_DEFAULT_SERIAL_PORT),
00051 myTCPHost(NULL),
00052 myTCPPort(8103),
00053 myDeviceType(Standard)
00054 {
00055 myParseArgsCallback.setName("ArGPSConnector");
00056 myLogArgsCallback.setName("ArGPSConnector");
00057 Aria::addParseArgsCB(&myParseArgsCallback);
00058 Aria::addLogOptionsCB(&myLogArgsCallback);
00059 }
00060
00061
00062 AREXPORT ArGPSConnector::~ArGPSConnector()
00063 {
00064 if(myDeviceCon) delete myDeviceCon;
00065 }
00066
00067
00068 AREXPORT bool ArGPSConnector::parseArgs()
00069 {
00070 if (!myArgParser) return false;
00071 if (!myArgParser->checkParameterArgumentString("-gpsPort", &myPort)) return false;
00072 if (!myArgParser->checkParameterArgumentInteger("-gpsBaud", &myBaud)) return false;
00073 if (!myArgParser->checkParameterArgumentString("-remoteGpsTcpHost", &myTCPHost)) return false;
00074 if (!myArgParser->checkParameterArgumentInteger("-remoteGpsTcpPort", &myTCPPort)) return false;
00075 char *deviceType = myArgParser->checkParameterArgument("-gpsType");
00076 if (deviceType)
00077 {
00078 if (strcasecmp(deviceType, "novatel") == 0)
00079 {
00080 myDeviceType = Novatel;
00081 }
00082 else if (strcasecmp(deviceType, "standard") == 0)
00083 {
00084 myDeviceType = Standard;
00085 }
00086 else
00087 {
00088 ArLog::log(ArLog::Terse, "GPSConnector: Error: unrecognized GPS type.");
00089 return false;
00090 }
00091 }
00092 return true;
00093 }
00094
00095 AREXPORT void ArGPSConnector::logArgs()
00096 {
00097 ArLog::log(ArLog::Terse, "GPS options:");
00098 ArLog::log(ArLog::Terse, "-gpsPort <gpsSerialPort>\tUse the given serial port (default \"%s\")", ARGPS_DEFAULT_SERIAL_PORT);
00099 ArLog::log(ArLog::Terse, "-gpsBaud <gpsSerialBaudRate>\tUse the given serial Baud rate (default %d)", ARGPS_DEFAULT_SERIAL_BAUD);
00100 ArLog::log(ArLog::Terse, "-gpsType <standard|novatel>\tSelect GPS device type (default \"standard\")");
00101 ArLog::log(ArLog::Terse, "-remoteGpsTcpHost <host>\tUse a TCP connection instead of serial, and connect to remote host <host>");
00102 ArLog::log(ArLog::Terse, "-remoteGpsTcpPort <host>\tUse the given port number for TCP connection, if using TCP. (default %d)", ARGPS_DEFAULT_REMOTE_TCP_PORT);
00103 }
00104
00105
00106
00107 AREXPORT ArGPS* ArGPSConnector::createGPS()
00108 {
00109
00110 ArGPS* newGPS = NULL;
00111 switch (myDeviceType)
00112 {
00113 case Novatel:
00114 newGPS = new ArNovatelGPS;
00115 break;
00116 default:
00117 newGPS = new ArGPS;
00118 break;
00119 }
00120
00121 if (myTCPHost == NULL)
00122 {
00123
00124 ArSerialConnection *serialCon = new ArSerialConnection;
00125 if (!serialCon->setBaud(myBaud)) { delete serialCon; return false; }
00126 if (serialCon->open(myPort) != 0) {
00127 ArLog::log(ArLog::Terse, "ArGPSConnector: Error: could not open GPS serial port %s.", myPort);
00128 delete serialCon;
00129 return NULL;
00130 }
00131 newGPS->setDeviceConnection(serialCon);
00132 myDeviceCon = serialCon;
00133 }
00134 else
00135 {
00136
00137 ArTcpConnection *tcpCon = new ArTcpConnection;
00138 ArLog::log(ArLog::Normal, "ArGPSConnector: Opening TCP connection to %s:%d...", myTCPHost, myTCPPort);
00139 int openState = tcpCon->open(myTCPHost, myTCPPort);
00140 if (openState != 0) {
00141 ArLog::log(ArLog::Terse, "ArGPSConnector: Error: could not open TCP connection to %s port %d: %s", tcpCon->getOpenMessage(openState));
00142 delete tcpCon;
00143 return NULL;
00144 }
00145 newGPS->setDeviceConnection(tcpCon);
00146 myDeviceCon = tcpCon;
00147 }
00148
00149 return newGPS;
00150 }
00151
00152 AREXPORT bool ArGPSConnector::connectGPS(ArGPS *gps)
00153 {
00154
00155 return gps->blockingConnect();
00156 }
00157