SoDaRadio-5.0.3-master:8901fb5
GPSmon.cxx
Go to the documentation of this file.
1 /*
2  Copyright (c) 2012, Matthew H. Reilly (kb1vc)
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8 
9  Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in
13  the documentation and/or other materials provided with the
14  distribution.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #include "GPSmon.hxx"
30 #include <list>
31 #include <errno.h>
32 
33 SoDa::GPSmon::GPSmon(Params * params, CmdMBox * _cmd_stream) : SoDa::SoDaThread("GPSmon")
34 {
35  cmd_stream = _cmd_stream;
37 
38 #if HAVE_GPSLIB
39  // now open the server connection
40  errno = 0;
41  int stat = gps_open(params->getGPSHostName().c_str(),
42  params->getGPSPortName().c_str(),
43  &gps_data);
44  if(stat != 0) {
45  std::cerr << boost::format("Could not open gpsd server connection. Error : %s\n") % gps_errstr(errno);
46  gps_server_ready = false;
47  }
48  else gps_server_ready = true;
49 
50  (void) gps_stream(&gps_data, WATCH_ENABLE, NULL);
51 #else
52  gps_server_ready = false;
53 #endif
54 
55 }
56 
58 {
59  bool exitflag = false;
60  Command * cmd;
61 
62  int stat;
63 
64  while(!exitflag) {
65  while((cmd = cmd_stream->get(cmd_subs)) != NULL) {
66  // process the command.
67  execCommand(cmd);
68  exitflag |= (cmd->target == Command::STOP);
69  // std::cerr << "GPSmon got a message. target = " << cmd->target << std::endl;
70  cmd_stream->free(cmd);
71  }
72  if(gps_server_ready) {
73  // there is a leak. It is either from gps_waiting or gps_read
74 #if HAVE_GPSLIB
75  while (gps_waiting(&gps_data, 100000)) {
76  errno = 0;
77 
78  stat = gps_read(&gps_data);
79  if(stat == -1) gps_server_ready = false;
80  else if(stat != 0) {
81 
82  time_t utc_time = (time_t) gps_data.fix.time;
83  struct tm btime;
84 
85  gmtime_r(&utc_time, &btime);
86 
88  (int) btime.tm_hour,
89  (int) btime.tm_min,
90  (int) btime.tm_sec));
91 
93  gps_data.fix.latitude,
94  gps_data.fix.longitude));
95  }
96  else {
97  std::cerr << boost::format("gps_read returned %d size is %d\n")
98  % stat % (sizeof(struct gps_data_t));
99  }
100  }
101 #endif
102  }
103  else {
104  usleep(100000);
105  }
106  }
107 }
108 
110 {
111  (void) cmd;
112  return;
113 }
114 
116 {
117  (void) cmd;
118  return;
119 }
120 
122 {
123  (void) cmd;
124  return;
125 }
The Thread baseclass for all SoDa thread objects.
Definition: SoDaBase.hxx:284
T * get(unsigned int subscriber_id)
Definition: MultiMBox.hxx:110
void execGetCommand(Command *cmd)
optional method to handle "GET" commands – commands that request a response
Definition: GPSmon.cxx:109
unsigned int cmd_subs
Definition: GPSmon.hxx:55
void execSetCommand(Command *cmd)
optional method to handle "SET" commands – commands that set internal state in the object...
Definition: GPSmon.cxx:115
std::string getGPSHostName() const
Definition: Params.hxx:119
This class handles command line parameters and built-ins.
Definition: Params.hxx:42
std::string getGPSPortName() const
Definition: Params.hxx:121
CmdTarget target
the thing we&#39;re touching
Definition: Command.hxx:673
Report UTC (time) from GPS receiver.
Definition: Command.hxx:326
void run()
Each thread object must define its "run" loop.
Definition: GPSmon.cxx:57
GPSmon(Params *params, CmdMBox *cmd_stream)
Definition: GPSmon.cxx:33
On receipt of a STOP command, all threads should exit their run loop.
Definition: Command.hxx:371
This is a list of all the commands that can "do something" to one or more components in the SoDa radi...
Definition: Command.hxx:47
CmdMBox * cmd_stream
Definition: GPSmon.hxx:54
bool gps_server_ready
Definition: GPSmon.hxx:60
Report LAT and LON from GPS receiver.
Definition: Command.hxx:318
void free(T *m)
Definition: MultiMBox.hxx:118
void execRepCommand(Command *cmd)
optional method that reports status or the result of some action.
Definition: GPSmon.cxx:121
void put(T *m)
Definition: MultiMBox.hxx:97
void execCommand(Command *cmd)
Execute (dispatch) a message removed from the command stream to one of the basic Command handler func...
Definition: SoDaBase.hxx:335