summaryrefslogtreecommitdiff
path: root/plugins/gpsd/gpsdplugin.cpp
blob: b3432f2aecc2e4656cc25850398560399ade4bdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
Copyright (C) 2012 Intel Corporation

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "gpsdplugin.h"
#include "timestamp.h"

#include <iostream>
#include <boost/assert.hpp>

using namespace std;

#include "debugout.h"

static int updateGpsposition(gpointer data)
{
	GpsdPlugin::Shared* shared = static_cast<GpsdPlugin::Shared*>(data);

#if (GPSD_API_MAJOR_VERSION >= 5)

	if (!gps_waiting (&shared->gps, 0))
	{
		return 1;
	}

	if(gps_read(&shared->gps) == -1)
	{
		DebugOut(0)<<"failed to read gps data from gpsd."<<endl;
		return 1;
	}
#else

	gps_poll(&gps);

#endif

	double time = amb::currentTime();

	if(shared->gps.fix.mode > 2)
	{
		if(shared->gps.fix.latitude != shared->oldlat)
			shared->oldlat = shared->gps.fix.latitude;
		if(shared->gps.fix.longitude != shared->oldlon)
			shared->oldlon = shared->gps.fix.longitude;
		if(shared->gps.fix.altitude != shared->oldalt)
			shared->oldalt = shared->gps.fix.altitude;
		if(shared->gps.fix.track != shared->oldheading)
			shared->oldheading = shared->gps.fix.track;
		if(shared->gps.fix.speed * MPS_TO_KPH != shared->oldspeed)
		{
			shared->oldspeed = shared->gps.fix.speed * MPS_TO_KPH;
		}

		shared->parent->updateProperty();
	}

	return 1;
}

GpsdPlugin::GpsdPlugin(AbstractRoutingEngine* re, map<string, string> config)
	:AbstractSource(re, config)
{
	shared = new Shared(this);

#if(GPSD_API_MAJOR_VERSION >= 5)
	int ret = gps_open("localhost","2947", &shared->gps);

	if(ret != 0)
	{
		debugOut("failed to open gps");
		DebugOut()<<"return value: "<<ret<<endl;
		(void)fprintf(stderr,"no gpsd running or network error: %d, %s\n",
					  errno, gps_errstr(errno));
		return;
	}
#else
	shared->gps = *(gps_open("localhost","2947"));

#endif

	gps_stream(&shared->gps, WATCH_ENABLE | WATCH_JSON, NULL);

	int timeout=200;

	if(config.find("timeout") != config.end())
	{
		timeout = atoi(config["timeout"].c_str());
	}

	updateVelocity = false;

	if(config.find("updateVelocity") != config.end())
	{
		updateVelocity = config["updateVelocity"] == "true";
	}

	g_timeout_add(timeout,updateGpsposition,shared);

	re->setSupported(supported(), this);
}

GpsdPlugin::~GpsdPlugin()
{
	(void) gps_stream(&shared->gps, WATCH_DISABLE, NULL);
	gps_close(&shared->gps);
}

extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine, map<string, string> config)
{
	return new GpsdPlugin(routingengine, config);
	
}

string GpsdPlugin::uuid()
{
	return "326011dd-65cd-4be6-a75e-3e8d46a05b5e";
}


void GpsdPlugin::getPropertyAsync(AsyncPropertyReply *reply)
{
	/*if(reply->property == VehicleProperty::ExteriorBrightness)
	{
		replyQueue.push_back(reply);
	}*/

//	else  ///We don't support what you are asking for.  Reply false
	{
		reply->value = NULL;
		reply->success = false;
		reply->completed(reply);
	}
}

void GpsdPlugin::getRangePropertyAsync(AsyncRangePropertyReply *reply)
{
	throw std::runtime_error("GpsdPlugin does not support this operation.  We should never hit this method.");
}

AsyncPropertyReply *GpsdPlugin::setProperty(AsyncSetPropertyRequest request )
{
	throw std::runtime_error("GpsdPlugin does not support this operation.  We should never hit this method.");
}

void GpsdPlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
{

	//mRequests.push_back(property);
}

void GpsdPlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
{
	//mRequests.remove(property);
}

PropertyList GpsdPlugin::supported()
{
	PropertyList props;
	props.push_back(VehicleProperty::Latitude);
	props.push_back(VehicleProperty::Longitude);
	props.push_back(VehicleProperty::Altitude);
	props.push_back(VehicleProperty::Direction);

	if(updateVelocity)
		props.push_back(VehicleProperty::VehicleSpeed);

	
	return props;
}

int GpsdPlugin::supportedOperations()
{
	return Get;
}


void GpsdPlugin::updateProperty()
{
	VehicleProperty::LatitudeType lat(shared->oldlat);
	routingEngine->updateProperty(VehicleProperty::Latitude,&lat, uuid());

	VehicleProperty::LongitudeType lon(shared->oldlon);
	routingEngine->updateProperty(VehicleProperty::Longitude, &lon, uuid());

	VehicleProperty::AltitudeType alt(shared->oldalt);
	routingEngine->updateProperty(VehicleProperty::Altitude, &alt, uuid());

	VehicleProperty::DirectionType heading(shared->oldheading);
	routingEngine->updateProperty(VehicleProperty::Direction, &heading, uuid());

	if(updateVelocity)
	{
		VehicleProperty::VehicleSpeedType speed(shared->oldspeed);
		routingEngine->updateProperty(VehicleProperty::VehicleSpeed, &speed, uuid());
	}
}