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 "ProtectedTaskManager.hpp"
25 #include "Serialiser.hpp"
26 #include "Deserialiser.hpp"
27 #include "XML/DataNodeXML.hpp"
28 #include "Task/TaskFile.hpp"
29 #include "LocalPath.hpp"
30 #include "Task/RoutePlannerGlue.hpp"
31 #include "Task/Ordered/Points/AATPoint.hpp"
32 #include "Engine/Task/Ordered/OrderedTask.hpp"
33 #include "Engine/Route/ReachResult.hpp"
35 #include <windef.h> // for MAX_PATH
37 ProtectedTaskManager::ProtectedTaskManager(TaskManager
&_task_manager
,
38 const TaskBehaviour
&tb
)
39 :Guard
<TaskManager
>(_task_manager
),
44 ProtectedTaskManager::~ProtectedTaskManager() {
45 UnprotectedLease
lease(*this);
46 lease
->SetIntersectionTest(NULL
); // de-register
50 ProtectedTaskManager::SetGlidePolar(const GlidePolar
&glide_polar
)
52 ExclusiveLease
lease(*this);
53 lease
->SetGlidePolar(glide_polar
);
56 const OrderedTaskBehaviour
57 ProtectedTaskManager::GetOrderedTaskBehaviour() const
60 return lease
->GetOrderedTask().GetOrderedTaskBehaviour();
64 ProtectedTaskManager::GetActiveWaypoint() const
67 const TaskWaypoint
*tp
= lease
->GetActiveTaskPoint();
69 return &tp
->GetWaypoint();
75 ProtectedTaskManager::TargetLock(const unsigned index
, bool do_lock
)
77 ExclusiveLease
lease(*this);
78 return lease
->TargetLock(index
, do_lock
);
82 ProtectedTaskManager::IncrementActiveTaskPoint(int offset
)
84 ExclusiveLease
lease(*this);
85 lease
->IncrementActiveTaskPoint(offset
);
89 ProtectedTaskManager::IncrementActiveTaskPointArm(int offset
)
91 ExclusiveLease
lease(*this);
92 TaskAdvance
&advance
= lease
->SetTaskAdvance();
94 switch (advance
.GetState()) {
95 case TaskAdvance::MANUAL
:
96 case TaskAdvance::AUTO
:
97 lease
->IncrementActiveTaskPoint(offset
);
99 case TaskAdvance::START_DISARMED
:
100 case TaskAdvance::TURN_DISARMED
:
102 advance
.SetArmed(true);
104 lease
->IncrementActiveTaskPoint(offset
);
107 case TaskAdvance::START_ARMED
:
108 case TaskAdvance::TURN_ARMED
:
110 lease
->IncrementActiveTaskPoint(offset
);
112 advance
.SetArmed(false);
119 ProtectedTaskManager::DoGoto(const Waypoint
&wp
)
121 ExclusiveLease
lease(*this);
122 return lease
->DoGoto(wp
);
126 ProtectedTaskManager::TaskClone() const
129 return lease
->Clone(task_behaviour
);
133 ProtectedTaskManager::TaskCommit(const OrderedTask
& that
)
135 ExclusiveLease
lease(*this);
136 return lease
->Commit(that
);
140 ProtectedTaskManager::TaskSave(const TCHAR
* path
, const OrderedTask
& task
)
142 DataNodeXML
root(DataNodeXML::CreateRoot(_T("Task")));
143 Serialiser
tser(root
);
144 tser
.Serialise(task
);
146 return root
.Save(path
);
150 ProtectedTaskManager::TaskSave(const TCHAR
* path
)
152 OrderedTask
* task
= TaskClone();
153 bool retval
= TaskSave(path
, *task
);
158 const TCHAR
ProtectedTaskManager::default_task_path
[] = _T("Default.tsk");
162 ProtectedTaskManager::TaskCreateDefault(const Waypoints
*waypoints
,
163 TaskFactoryType factoryfail
)
165 TCHAR path
[MAX_PATH
];
166 LocalPath(path
, default_task_path
);
167 OrderedTask
*task
= TaskFile::GetTask(path
, task_behaviour
, waypoints
, 0);
169 task
= new OrderedTask(task_behaviour
);
171 task
->SetFactory(factoryfail
);
177 ProtectedTaskManager::TaskSaveDefault()
179 TCHAR path
[MAX_PATH
];
180 LocalPath(path
, default_task_path
);
181 return TaskSave(path
);
185 ProtectedTaskManager::Reset()
187 ExclusiveLease
lease(*this);
192 ProtectedTaskManager::SetRoutePlanner(const RoutePlannerGlue
*_route
) {
193 intersection_test
.SetRoute(_route
);
195 ExclusiveLease
lease(*this);
196 lease
->SetIntersectionTest(&intersection_test
);
200 ReachIntersectionTest::Intersects(const AGeoPoint
& destination
)
206 if (!route
->FindPositiveArrival(destination
, result
))
209 // we use find_positive_arrival here instead of is_inside, because may use
210 // arrival height for sorting later
211 return result
.terrain_valid
== ReachResult::Validity::UNREACHABLE
||
212 (result
.terrain_valid
== ReachResult::Validity::VALID
&&
213 result
.terrain
< destination
.altitude
);