AshtechData.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: AshtechData.cpp 861 2007-11-01 14:18:26Z ocibu $"
00002 
00003 //============================================================================
00004 //
00005 //  This file is part of GPSTk, the GPS Toolkit.
00006 //
00007 //  The GPSTk is free software; you can redistribute it and/or modify
00008 //  it under the terms of the GNU Lesser General Public License as published
00009 //  by the Free Software Foundation; either version 2.1 of the License, or
00010 //  any later version.
00011 //
00012 //  The GPSTk 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 Lesser General Public License for more details.
00016 //
00017 //  You should have received a copy of the GNU Lesser General Public
00018 //  License along with GPSTk; if not, write to the Free Software Foundation,
00019 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //  
00021 //  Copyright 2004, The University of Texas at Austin
00022 //
00023 //============================================================================
00024 
00025 //============================================================================
00026 //
00027 //This software developed by Applied Research Laboratories at the University of
00028 //Texas at Austin, under contract to an agency or agencies within the U.S. 
00029 //Department of Defense. The U.S. Government retains all rights to use,
00030 //duplicate, distribute, disclose, or release this software. 
00031 //
00032 //Pursuant to DoD Directive 523024 
00033 //
00034 // DISTRIBUTION STATEMENT A: This software has been approved for public 
00035 //                           release, distribution is unlimited.
00036 //
00037 //=============================================================================
00038 
00039 #include "StringUtils.hpp"
00040 #include "BinUtils.hpp"
00041 
00042 #include "AshtechData.hpp"
00043 #include "AshtechStream.hpp"
00044 
00045 using namespace std;
00046 
00047 namespace gpstk
00048  {
00049     //---------------------------------------------------------------------------
00050     //---------------------------------------------------------------------------
00051     // This is the string that preceeds every message from the receiver.
00052     const string AshtechData::preamble("$PASHR,");
00053 
00054     // This is the string that is at the end of every message.
00055     const string AshtechData::trailer("\015\012");
00056 
00057     // Set to zero for no debugging output
00058     // set to 1 to output text messages about decode/format/range errors
00059     // set to 2 to add a hex dump of those messages
00060     // set to 3+ to add the tossed bytes whether or not they are bad
00061     int AshtechData::debugLevel = 0;
00062 
00063     // set true to print a hex dump of every message to cout
00064     bool AshtechData::hexDump = false;
00065 
00066 
00067     //---------------------------------------------------------------------------
00068     void AshtechData::reallyGetRecord(FFStream& ffs)
00069        throw(exception, FFStreamError, EndOfFile)
00070     {
00071        // Note that this will generate a bad_cast exception if it doesn't work.
00072        AshtechStream& stream=dynamic_cast<AshtechStream&>(ffs);
00073 
00074        // make sure the object is reset before starting the search
00075        clear(fmtbit | lenbit | crcbit);
00076        id.clear();
00077 
00078        readHeader(stream);
00079     } // AshtechData::reallyGetRecord()
00080 
00081 
00082     //---------------------------------------------------------------------------
00083     void AshtechData::readHeader(AshtechStream& stream)
00084        throw(FFStreamError, EndOfFile)
00085     {
00086        string& rawData = stream.rawData;
00087        size_t i;
00088 
00089        while (stream)
00090        {
00091           if (rawData.length() < preamble.length()+4)
00092           {
00093              char buff[512];
00094              stream.read(buff, sizeof(buff));
00095              rawData.append(buff, stream.gcount());
00096           }
00097 
00098           if (stream.header)
00099              i = rawData.find(preamble, preamble.length());
00100           else
00101              i = rawData.find(preamble);
00102           stream.header = false;
00103 
00104           if (i)
00105           {
00106              i = min (rawData.length(), i);
00107              if (debugLevel>2)
00108                 cout << "Tossing " << i
00109                      << " bytes at offset: 0x" << hex << stream.getRawPos() << dec
00110                      << endl;
00111              if (hexDump)
00112                 StringUtils::hexDumpData(cout, rawData.substr(0,i));
00113              rawData.erase(0, i);
00114           }
00115           else
00116           {
00117              id = rawData.substr(7,3);
00118              break;
00119           }
00120        }
00121        stream.header = true;
00122     }
00123 
00124     //---------------------------------------------------------------------------
00125     void AshtechData::readBody(AshtechStream& stream)
00126        throw(FFStreamError, EndOfFile)
00127     {
00128        string& rawData = stream.rawData;
00129        const static string term = trailer+preamble;
00130        size_t term_pos = rawData.find(term);
00131 
00132        while (stream)
00133        {
00134           term_pos = rawData.find(term);
00135           if (term_pos > 0 && term_pos < rawData.length())
00136              break;
00137 
00138           if (stream)
00139           {
00140              char cbuff[512];
00141              stream.read(cbuff, sizeof(cbuff));
00142              rawData.append(cbuff, stream.gcount());
00143           }
00144           else
00145              break;
00146        }
00147 
00148        term_pos += trailer.length();
00149        if (hexDump)
00150           StringUtils::hexDumpData(cout, rawData.substr(0,term_pos));
00151 
00152        decode(rawData.substr(0, term_pos));
00153 
00154        if (!good() && debugLevel>1)
00155           cout << "bad decode starting at at offset 0x"
00156                << hex << stream.getRawPos() << dec
00157                << endl;
00158 
00159        rawData.erase(0, term_pos);
00160        stream.header=false;
00161     }
00162 
00163 
00164     //---------------------------------------------------------------------------
00165     void AshtechData::dump(ostream& out) const throw()
00166     {
00167        ostringstream oss;
00168        oss << getName() << " : id:" << id
00169            << " checksum:" << hex << checksum
00170            << " rdstate:" << rdstate() << dec;
00171        if (crcerr())
00172           oss << "-crc";
00173        if (fmterr())
00174           oss << "-fmt";
00175        if (lenerr())
00176           oss << "-len";
00177        if (parerr())
00178           oss << "-par";
00179 
00180        out << oss.str() << endl;
00181     }  // AshtechData::dump()
00182 } // namespace gpstk

Generated on Thu Jul 29 03:30:51 2010 for GPS ToolKit Software Library by  doxygen 1.3.9.1