4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "TrackingGlue.hpp"
25 #include "NMEA/MoreData.hpp"
26 #include "NMEA/Derived.hpp"
27 #include "Units/System.hpp"
28 #include "Util/Macros.hpp"
30 #ifdef HAVE_LIVETRACK24
32 static LiveTrack24::VehicleType
33 MapVehicleTypeToLifetrack24(TrackingSettings::VehicleType vt
)
35 static constexpr LiveTrack24::VehicleType vehicleTypeMap
[] = {
36 LiveTrack24::VehicleType::GLIDER
,
37 LiveTrack24::VehicleType::PARAGLIDER
,
38 LiveTrack24::VehicleType::POWERED_AIRCRAFT
,
39 LiveTrack24::VehicleType::HOT_AIR_BALLOON
42 unsigned vti
= (unsigned) vt
;
43 if (vti
>= ARRAY_SIZE(vehicleTypeMap
))
46 return vehicleTypeMap
[vti
];
51 TrackingGlue::TrackingGlue()
52 #ifdef HAVE_LIVETRACK24
57 settings
.SetDefaults();
58 #ifdef HAVE_LIVETRACK24
59 LiveTrack24::SetServer(settings
.livetrack24
.server
);
62 #ifdef HAVE_SKYLINES_TRACKING_HANDLER
63 skylines
.SetHandler(this);
67 #ifdef HAVE_LIVETRACK24
70 TrackingGlue::StopAsync()
72 ScopeLock
protect(mutex
);
73 StandbyThread::StopAsync();
77 TrackingGlue::WaitStopped()
79 ScopeLock
protect(mutex
);
80 StandbyThread::WaitStopped();
86 TrackingGlue::SetSettings(const TrackingSettings
&_settings
)
88 #ifdef HAVE_SKYLINES_TRACKING
89 skylines
.SetSettings(_settings
.skylines
);
92 #ifdef HAVE_LIVETRACK24
93 if (_settings
.livetrack24
.server
!= settings
.livetrack24
.server
||
94 _settings
.livetrack24
.username
!= settings
.livetrack24
.username
||
95 _settings
.livetrack24
.password
!= settings
.livetrack24
.password
) {
96 /* wait for the current job to finish */
99 /* now it's safe to access these variables without a lock */
100 settings
= _settings
;
101 state
.ResetSession();
102 LiveTrack24::SetServer(_settings
.livetrack24
.server
);
104 /* no fundamental setting changes; the write needs to be protected
105 by the mutex, because another job may be running already */
106 ScopeLock
protect(mutex
);
107 settings
= _settings
;
113 TrackingGlue::OnTimer(const MoreData
&basic
, const DerivedInfo
&calculated
)
115 #ifdef HAVE_SKYLINES_TRACKING
116 skylines
.Tick(basic
);
119 #ifdef HAVE_LIVETRACK24
120 if (!settings
.livetrack24
.enabled
)
121 /* disabled by configuration */
122 /* note that we are allowed to read "settings" without locking the
123 mutex, because the background thread never writes to this
127 if (!basic
.time_available
|| !basic
.location_available
)
128 /* can't track without a valid GPS fix */
131 if (!clock
.CheckUpdate(settings
.interval
* 1000))
135 ScopeLock
protect(mutex
);
137 /* still running, skip this submission */
140 date_time
= basic
.date_time_utc
;
141 if (!basic
.date_available
)
142 /* use "today" if the GPS didn't provide a date */
143 (BrokenDate
&)date_time
= BrokenDate::TodayUTC();
145 location
= basic
.location
;
146 /* XXX use nav_altitude? */
147 altitude
= basic
.NavAltitudeAvailable() && positive(basic
.nav_altitude
)
148 ? (unsigned)basic
.nav_altitude
150 ground_speed
= basic
.ground_speed_available
151 ? (unsigned)Units::ToUserUnit(basic
.ground_speed
, Unit::KILOMETER_PER_HOUR
)
153 track
= basic
.track_available
157 last_flying
= flying
;
158 flying
= calculated
.flight
.flying
;
164 #ifdef HAVE_LIVETRACK24
169 if (!settings
.livetrack24
.enabled
)
170 /* settings have been cleared meanwhile, bail out */
173 unsigned tracking_interval
= settings
.interval
;
174 LiveTrack24Settings copy
= this->settings
.livetrack24
;
179 if (last_flying
&& state
.HasSession()) {
180 /* landing: end tracking session */
181 LiveTrack24::EndTracking(state
.session_id
, state
.packet_id
);
182 state
.ResetSession();
186 /* don't track if not flying */
191 const int64_t current_timestamp
= date_time
.ToUnixTimeUTC();
193 if (state
.HasSession() && current_timestamp
+ 60 < last_timestamp
) {
194 /* time warp: create a new session */
195 LiveTrack24::EndTracking(state
.session_id
, state
.packet_id
);
196 state
.ResetSession();
199 last_timestamp
= current_timestamp
;
201 if (!state
.HasSession()) {
202 LiveTrack24::UserID user_id
= 0;
203 if (!copy
.username
.empty() && !copy
.password
.empty())
204 user_id
= LiveTrack24::GetUserID(copy
.username
, copy
.password
);
207 copy
.username
.clear();
208 copy
.password
.clear();
209 state
.session_id
= LiveTrack24::GenerateSessionID();
211 state
.session_id
= LiveTrack24::GenerateSessionID(user_id
);
214 if (!LiveTrack24::StartTracking(state
.session_id
, copy
.username
,
215 copy
.password
, tracking_interval
,
216 MapVehicleTypeToLifetrack24(settings
.vehicleType
))) {
217 state
.ResetSession();
225 LiveTrack24::SendPosition(state
.session_id
, state
.packet_id
++,
226 location
, altitude
, ground_speed
, track
,
234 #ifdef HAVE_SKYLINES_TRACKING_HANDLER
237 TrackingGlue::OnTraffic(uint32_t pilot_id
, unsigned time_of_day_ms
,
238 const GeoPoint
&location
, int altitude
)
243 const ScopeLock
protect(skylines_data
.mutex
);
244 const SkyLinesTracking::Data::Traffic
traffic(time_of_day_ms
,
246 skylines_data
.traffic
[pilot_id
] = traffic
;
248 user_known
= skylines_data
.IsUserKnown(pilot_id
);
252 /* we don't know this user's name yet - try to find it out by
254 skylines
.RequestUserName(pilot_id
);
258 TrackingGlue::OnUserName(uint32_t user_id
, const TCHAR
*name
)
260 const ScopeLock
protect(skylines_data
.mutex
);
261 skylines_data
.user_names
[user_id
] = name
;