OpenTracker

An Open Architecture for Reconfigurable Tracking based on XML | Contact

GPSModule.cxx

Go to the documentation of this file.
00001 /* ========================================================================
00002  * Copyright (c) 2006,
00003  * Institute for Computer Graphics and Vision
00004  * Graz University of Technology
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are
00009  * met:
00010  *
00011  * Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the following disclaimer.
00013  *
00014  * Redistributions in binary form must reproduce the above copyright
00015  * notice, this list of conditions and the following disclaimer in the
00016  * documentation and/or other materials provided with the distribution.
00017  *
00018  * Neither the name of the Graz University of Technology nor the names of
00019  * its contributors may be used to endorse or promote products derived from
00020  * this software without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00023  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00024  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00025  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00026  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00027  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00028  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  * ========================================================================
00034  * PROJECT: OpenTracker
00035  * ======================================================================== */
00043 /* ======================================================================= */
00044 
00045 #include <stdlib.h>
00046 #include "../tool/FixWinCE.h"
00047 #include <ace/OS.h>
00048 #include <ace/Reactor.h>
00049 #include <ace/Thread.h>
00050 #include <ace/FILE_Connector.h>
00051 #include <ace/FILE_IO.h>
00052 
00053 #include "GPSModule.h"
00054 #include "GPSSource.h"
00055 #include "GPSDirectionSource.h"
00056 #include "GPSGarminCompass.h"
00057 #include "GPSGarminAltitude.h"
00058 #include "GPSInfoSource.h"
00059 
00060 #include <iostream>
00061 
00062 #include <ace/Log_Msg.h>
00063 #include "../tool/OT_ACE_Log.h"
00064 
00065 
00066 #ifndef OT_NO_GPS_SUPPORT
00067 
00068 
00069 namespace ot {
00070 
00071 
00072     ACE_Reactor gps_reactor;
00073 
00074     GPSModule::GPSModule() :
00075         source( NULL ),
00076         dirSource( NULL ),
00077         compassSource( NULL ),
00078         altitudeSource( NULL ),
00079         infoSource( NULL ),
00080         driver( NULL ),
00081         logFile( NULL )
00082     {
00083     }
00084 
00085     GPSModule::~GPSModule()
00086     {
00087         if( source != NULL )
00088             delete source;
00089         if( dirSource != NULL )
00090             delete dirSource;
00091         if( compassSource != NULL )
00092             delete compassSource;
00093         if( altitudeSource != NULL )
00094             delete altitudeSource;
00095         if( infoSource != NULL )
00096             delete infoSource;
00097         if( logFile != NULL )
00098             delete logFile;
00099     }
00100 
00101     void GPSModule::init(StringTable& attributes,  ConfigNode * localTree)
00102     {
00103     device = attributes.get("dev");
00104     dgpsServer = attributes.get("DGPSserver");
00105         rtcmdev = attributes.get("rtcmdev");
00106     if( attributes.get( "baudrate", &baudRate ) != 1 )
00107     {
00108             baudRate = 9600;        // default GPS receiver baud rate
00109     }
00110     if( attributes.get( "DGPSport", &dgpsPort ) != 1 )
00111     {
00112             dgpsPort = 2101;        // default DGPSIP port
00113     }
00114     if( attributes.get("debug").compare("on") == 0 )
00115     {
00116             debug = true;
00117     }
00118     else
00119     {
00120             debug = false;
00121     }
00122         if( attributes.get("DGPSmirror", &dgpsmirror) != 1 )
00123         {
00124             dgpsmirror = -1;
00125         }
00126         if( attributes.containsKey("logfile") )
00127         {
00128             logFile = new ACE_FILE_IO;
00129             ACE_FILE_Connector connector; 
00130             if( connector.connect( *logFile, ACE_FILE_Addr(ACE_TEXT_CHAR_TO_TCHAR(attributes.get("logfile").c_str())), 0, ACE_Addr::sap_any, 0, O_WRONLY|O_CREAT|O_APPEND))
00131             {
00132                 delete logFile;
00133                 logFile = NULL;
00134                 LOG_ACE_ERROR("ot:DGPSModule error opening log file %s\n", attributes.get("logfile").c_str());
00135             }
00136         }
00137         Module::init( attributes, localTree );
00138     LOG_ACE_INFO("ot:GPSModule initialized for port %s and server %s\n", device.c_str(), dgpsServer.c_str());
00139     }
00140 
00141     Node * GPSModule::createNode( const std::string & name, StringTable & attributes )
00142     {
00143     if( name.compare("GPSSource") == 0 )
00144     {
00145             if( source != NULL )
00146             {
00147                 ACE_DEBUG((LM_ERROR, ACE_TEXT("ot:Only one GPSSource can be build !\n")));
00148                 return NULL;
00149             }
00150             if( !isInitialized() )
00151             {
00152                 ACE_DEBUG((LM_ERROR, ACE_TEXT("ot:GPSModule is not initialized, cannot build GPSSource !\n")));
00153                 return NULL;
00154             }
00155             source = new GPSSource;
00156             ACE_DEBUG((LM_INFO, ACE_TEXT("ot:Built GPSSource node.\n")));
00157             return source;
00158     }
00159         if( name.compare("GPSDirectionSource") == 0 )
00160         {
00161             if( dirSource != NULL )
00162             {
00163                 ACE_DEBUG((LM_ERROR, ACE_TEXT("ot:Only one GPSDirectionSource can be build !\n")));
00164                 return NULL;
00165             }
00166             if( !isInitialized() )
00167             {
00168                 ACE_DEBUG((LM_ERROR, ACE_TEXT("ot:GPSModule is not initialized, cannot build GPSDirectionSource !\n")));
00169                 return NULL;
00170             }
00171             dirSource = new GPSDirectionSource;
00172             ACE_DEBUG((LM_INFO, ACE_TEXT("ot:Built GPSDirectionSource node.\n")));
00173             return dirSource;
00174         }
00175         if( name.compare("GPSGarminCompass") == 0 )
00176         {
00177             if( compassSource != NULL )
00178             {
00179                 ACE_DEBUG((LM_ERROR, ACE_TEXT("ot:Only one GPSGarminCompass can be build !\n")));
00180                 return NULL;
00181             }
00182             if( !isInitialized() )
00183             {
00184                 ACE_DEBUG((LM_ERROR, ACE_TEXT("ot:GPSModule is not initialized, cannot build GPSGarminCompass !\n")));
00185                 return NULL;
00186             }
00187             compassSource = new GPSGarminCompass;
00188             ACE_DEBUG((LM_INFO, ACE_TEXT("ot:Built GPSGarminCompass node.\n")));
00189             return compassSource;
00190         }
00191         if( name.compare("GPSGarminAltitude") == 0 )
00192         {
00193             if( altitudeSource != NULL )
00194             {
00195                 ACE_DEBUG((LM_ERROR, ACE_TEXT("ot:Only one GPSGarminAltitude can be build !\n")));
00196                 return NULL;
00197             }
00198             if( !isInitialized() )
00199             {
00200                 ACE_DEBUG((LM_ERROR, ACE_TEXT("ot:GPSModule is not initialized, cannot build GPSGarminAltitude !\n")));
00201                 return NULL;
00202             }
00203             altitudeSource = new GPSGarminAltitude;
00204             ACE_DEBUG((LM_INFO, ACE_TEXT("ot:Built GPSGarminAltitude node.\n")));
00205             return altitudeSource;
00206         }
00207         if( name.compare("GPSInfoSource") == 0 )
00208         {
00209             if( infoSource != NULL )
00210             {
00211                 ACE_DEBUG((LM_ERROR, ACE_TEXT("ot:Only one GPSInfoSource can be build !\n")));
00212                 return NULL;
00213             }
00214             if( !isInitialized() )
00215             {
00216                 ACE_DEBUG((LM_ERROR, ACE_TEXT("ot:GPSModule is not initialized, cannot build GPSInfoSource !\n")));
00217                 return NULL;
00218             }
00219             infoSource = new GPSInfoSource;
00220             ACE_DEBUG((LM_INFO, ACE_TEXT("ot:Built GPSInfoSource node.\n")));
00221             return infoSource;
00222         }
00223     return NULL;    
00224     }
00225 
00226     void GPSModule::pushEvent()
00227     {
00228         // here for some template magic:
00229         // updateSource is a template member function but the compiler appearantly
00230         // creates different variants for each type passed in :)
00231         updateSource(source);
00232         updateSource(dirSource);
00233         updateSource(compassSource);
00234         updateSource(altitudeSource);
00235         updateSource(infoSource);
00236     }
00237 
00238     void GPSModule::start()
00239     {
00240     if( isInitialized() && (source != NULL || dirSource != NULL || compassSource != NULL || altitudeSource != NULL || infoSource != NULL ))
00241     {
00242             ThreadModule::start();
00243     }
00244     }
00245 
00246     void GPSModule::close()
00247     {
00248     if( driver != NULL )
00249     {
00250             if( driver->getReactor() != NULL )
00251             {
00252                 driver->getReactor()->end_reactor_event_loop();
00253             }
00254             ThreadModule::close();
00255     }
00256         if( logFile != NULL )
00257             logFile->close();
00258     }
00259 
00260     void GPSModule::run()
00261     {
00262     driver = new GPSDriver( &gps_reactor );
00263     driver->setDebug( debug ); // only for debug purposes ...
00264         if( source != NULL )
00265             driver->addListener( source, this );
00266         if( dirSource != NULL )
00267             driver->addListener( dirSource, this );
00268         if( compassSource != NULL )
00269             driver->addListener( compassSource, this );
00270         if( altitudeSource != NULL )
00271             driver->addListener( altitudeSource, this );
00272         if( infoSource != NULL )
00273             driver->addListener( infoSource, this );
00274     
00275         if( logFile != NULL )
00276             driver->addListener( this );
00277     driver->getReactor()->owner(ACE_Thread::self());
00278     if( driver->open( device, baudRate, dgpsServer, dgpsPort, dgpsmirror, rtcmdev ) != 0 )
00279     {
00280             ACE_DEBUG((LM_ERROR, ACE_TEXT("ot:GPSModule could not start GPSDriver !\n")));
00281             return;
00282     }
00283         if( debug )
00284     {
00285             ACE_DEBUG((LM_INFO, ACE_TEXT("ot:GPSModule started GPSDriver !\n")));
00286     }
00287     driver->getReactor()->run_reactor_event_loop();
00288     driver->close();
00289     delete driver;
00290     }
00291 
00292     void GPSModule::newData( const GPResult * res, const char * line, void * userData )
00293     {
00294         logFile->send( line, ACE_OS::strlen(line));
00295     }
00296 
00297 
00298 } // namespace ot
00299 
00300 
00301 #else
00302 #pragma message(">>> OT_NO_GPS_SUPPORT")
00303 #endif // OT_NO_GPS_SUPPORT
00304 
00305 /* 
00306  * ------------------------------------------------------------
00307  *   End of GPSModule.cxx
00308  * ------------------------------------------------------------
00309  *   Automatic Emacs configuration follows.
00310  *   Local Variables:
00311  *   mode:c++
00312  *   c-basic-offset: 4
00313  *   eval: (c-set-offset 'substatement-open 0)
00314  *   eval: (c-set-offset 'case-label '+)
00315  *   eval: (c-set-offset 'statement 'c-lineup-runin-statements)
00316  *   eval: (setq indent-tabs-mode nil)
00317  *   End:
00318  * ------------------------------------------------------------ 
00319  */

copyright (c) 2006 Graz University of Technology