Debian Codegraph @

src/Autopilot/autopilotgroup.cxx

raw symbols SCIP
1 /*
2 * SPDX-FileName: autopilotgroup.cxx
3 * SPDX-FileComment: an even more flexible, generic way to build autopilots
4 * SPDX-FileCopyrightText: Copyright (C) 2010 Torsten Dreyer - Torsten (at) t3r (dot) de
5 * SPDX-FileContributor: Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #ifdef HAVE_CONFIG_H
10 # include <config.h>
11 #endif
12
13 #include "autopilot.hxx"
14 #include "autopilotgroup.hxx"
15
16 #include <string>
17 #include <vector>
18
19 #include <simgear/debug/ErrorReportingCallback.hxx>
20 #include <simgear/props/props_io.hxx>
21 #include <simgear/structure/exception.hxx>
22 #include <simgear/structure/subsystem_mgr.hxx>
23
24 #include <Main/fg_props.hxx>
25 #include <Main/sentryIntegration.hxx>
26
27 using std::vector;
28 using namespace std::string_literals;
29
30 using simgear::PropertyList;
31 using FGXMLAutopilot::Autopilot;
32
33 class FGXMLAutopilotGroupImplementation : public FGXMLAutopilotGroup
34 {
35 public:
36 FGXMLAutopilotGroupImplementation(const std::string& nodeName):
37 FGXMLAutopilotGroup(),
38 _nodeName(nodeName)
39 {}
40
41 // Subsystem API.
42 void init() override;
43 InitStatus incrementalInit() override;
44 void reinit() override;
45
46 // Subsystem identification.
47 static const char* staticSubsystemClassId() { return "xml-autopilot-group"; }
48
49 virtual void addAutopilot( const std::string& name,
50 SGPropertyNode_ptr apNode,
51 SGPropertyNode_ptr config );
52 virtual void removeAutopilot( const std::string & name );
53
54 private:
55 void initFrom( SGPropertyNode_ptr rootNode, const char * childName );
56 std::string _nodeName;
57 };
58
59 //------------------------------------------------------------------------------
60 void FGXMLAutopilotGroupImplementation::addAutopilot( const std::string& name,
61 SGPropertyNode_ptr apNode,
62 SGPropertyNode_ptr config )
63 {
64 if( has_subsystem(name) )
65 {
66 SG_LOG( SG_AUTOPILOT,
67 SG_ALERT,
68 "NOT adding duplicate " << _nodeName << " name '" << name << "'");
69 return;
70 }
71
72 Autopilot* ap = new Autopilot(apNode, config);
73 ap->set_name( name );
74
75 double updateInterval = config->getDoubleValue("update-interval-secs", 0.0);
76 set_subsystem( name, ap, updateInterval );
77 }
78
79 //------------------------------------------------------------------------------
80 void FGXMLAutopilotGroupImplementation::removeAutopilot(const std::string& name)
81 {
82 Autopilot* ap = static_cast<Autopilot*>(get_subsystem(name));
83 if( !ap )
84 {
85 SG_LOG( SG_AUTOPILOT,
86 SG_ALERT,
87 "CAN NOT remove unknown " << _nodeName << " '" << name << "'");
88 return;
89 }
90
91 remove_subsystem(name);
92 }
93
94 //------------------------------------------------------------------------------
95 void FGXMLAutopilotGroupImplementation::reinit()
96 {
97 SGSubsystemGroup::unbind();
98 clearSubsystems();
99
100 // ensure we bind again, so the SGSubsystemGroup state is correct before
101 // we call init. Since there's no actual group members at this point (we
102 // cleared them just above) this is purely to ensure SGSubsystemGroup::_state
103 // is BIND, so that ::init doesn't assert
104 SGSubsystemGroup::bind();
105 init();
106 }
107
108 //------------------------------------------------------------------------------
109 SGSubsystem::InitStatus FGXMLAutopilotGroupImplementation::incrementalInit()
110 {
111 simgear::ErrorReportContext ec("primary-aircraft"s, "yes"s);
112 init();
113 return INIT_DONE;
114 }
115
116 //------------------------------------------------------------------------------
117 void FGXMLAutopilotGroupImplementation::init()
118 {
119 initFrom(fgGetNode("/sim/systems"), _nodeName.c_str());
120 SGSubsystemGroup::init();
121 }
122
123 //------------------------------------------------------------------------------
124 void FGXMLAutopilotGroupImplementation::initFrom( SGPropertyNode_ptr rootNode,
125 const char* childName )
126 {
127 if( !rootNode )
128 return;
129
130 for( auto autopilotNode : rootNode->getChildren(childName) )
131 {
132 SGPropertyNode_ptr pathNode = autopilotNode->getNode("path");
133 if( !pathNode )
134 {
135 SG_LOG
136 (
137 SG_AUTOPILOT,
138 SG_WARN,
139 "No configuration file specified for this " << childName << "!"
140 );
141 continue;
142 }
143
144 std::string apName;
145 SGPropertyNode_ptr nameNode = autopilotNode->getNode( "name" );
146 if( nameNode != NULL ) {
147 apName = nameNode->getStringValue();
148 } else {
149 std::ostringstream buf;
150 buf << "unnamed_autopilot_" << autopilotNode->getIndex();
151 apName = buf.str();
152 }
153
154 {
155 // check for duplicate names
156 std::string name = apName;
157 for( unsigned i = 0; get_subsystem( apName.c_str() ) != NULL; i++ ) {
158 std::ostringstream buf;
159 buf << name << "_" << i;
160 apName = buf.str();
161 }
162 if( apName != name )
163 SG_LOG
164 (
165 SG_AUTOPILOT,
166 SG_DEV_WARN,
167 "Duplicate " << childName << " configuration name " << name
168 << ", renamed to " << apName
169 );
170 }
171
172 addAutopilotFromFile(apName, autopilotNode, pathNode->getStringValue());
173 }
174 }
175
176 void FGXMLAutopilotGroup::addAutopilotFromFile( const std::string& name,
177 SGPropertyNode_ptr apNode,
178 const std::string& path )
179 {
180 SGPath config = globals->resolve_maybe_aircraft_path(path);
181 if( config.isNull() )
182 {
183 simgear::reportFailure(simgear::LoadFailure::NotFound, simgear::ErrorCode::AircraftSystems,
184 std::string{"Autopilot XML not found:"} + path, sg_location{path});
185 SG_LOG(
186 SG_AUTOPILOT,
187 SG_ALERT,
188 "Cannot find property-rule configuration file '" << path << "'.");
189 return;
190 }
191 SG_LOG
192 (
193 SG_AUTOPILOT,
194 SG_INFO,
195 "Reading property-rule configuration from " << config
196 );
197
198 try
199 {
200 SGPropertyNode_ptr configNode = new SGPropertyNode();
201 readProperties(config, configNode);
202
203 SG_LOG(SG_AUTOPILOT, SG_INFO, "adding property-rule subsystem " << name);
204 addAutopilot(name, apNode, configNode);
205 }
206 catch (const sg_exception& e)
207 {
208 SG_LOG
209 (
210 SG_AUTOPILOT,
211 SG_ALERT,
212 "Failed to load property-rule configuration: " << config
213 << ": " << e.getMessage()
214 );
215 simgear::reportFailure(simgear::LoadFailure::BadData, simgear::ErrorCode::AircraftSystems,
216 std::string{"Autopilot XML failed to load:"} + e.getFormattedMessage(), e.getLocation());
217 return;
218 }
219 }
220
221 //------------------------------------------------------------------------------
222 FGXMLAutopilotGroup*
223 FGXMLAutopilotGroup::createInstance(const std::string& nodeName)
224 {
225 return new FGXMLAutopilotGroupImplementation(nodeName);
226 }