1 | // Copyright (c) 2013 by István Váradi
|
---|
2 |
|
---|
3 | // This file is part of XPLRA, a remote-access plugin for X-Plane
|
---|
4 |
|
---|
5 | // Redistribution and use in source and binary forms, with or without
|
---|
6 | // modification, are permitted provided that the following conditions are met:
|
---|
7 |
|
---|
8 | // 1. Redistributions of source code must retain the above copyright notice, this
|
---|
9 | // list of conditions and the following disclaimer.
|
---|
10 | // 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
11 | // this list of conditions and the following disclaimer in the documentation
|
---|
12 | // and/or other materials provided with the distribution.
|
---|
13 |
|
---|
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
---|
15 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
16 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
17 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
---|
18 | // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
19 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
20 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
21 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
22 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
23 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 |
|
---|
25 | // The views and conclusions contained in the software and documentation are those
|
---|
26 | // of the authors and should not be interpreted as representing official policies,
|
---|
27 | // either expressed or implied, of the FreeBSD Project.
|
---|
28 |
|
---|
29 | #ifndef HU_VARADIISTVAN_XPLRA_XPLANE_H
|
---|
30 | #define HU_VARADIISTVAN_XPLRA_XPLANE_H
|
---|
31 | //-----------------------------------------------------------------------------
|
---|
32 |
|
---|
33 | #include "Exception.h"
|
---|
34 |
|
---|
35 | #include <hu/varadiistvan/scpl/io/Waiter.h>
|
---|
36 |
|
---|
37 | #include <set>
|
---|
38 |
|
---|
39 | #include <inttypes.h>
|
---|
40 |
|
---|
41 | //-----------------------------------------------------------------------------
|
---|
42 |
|
---|
43 | namespace hu { namespace varadiistvan { namespace scpl { namespace io {
|
---|
44 |
|
---|
45 | class LocalClientSocket;
|
---|
46 | class DataStream;
|
---|
47 |
|
---|
48 | } /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
|
---|
49 |
|
---|
50 | //------------------------------------------------------------------------------
|
---|
51 |
|
---|
52 | namespace hu { namespace varadiistvan { namespace xplra {
|
---|
53 |
|
---|
54 | //------------------------------------------------------------------------------
|
---|
55 |
|
---|
56 | class MultiBuffer;
|
---|
57 | class MultiGetter;
|
---|
58 | class MultiSetter;
|
---|
59 |
|
---|
60 | //------------------------------------------------------------------------------
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * The main class to access X-Plane.
|
---|
64 | *
|
---|
65 | * The calls are synchronous and not thread-safe.
|
---|
66 | */
|
---|
67 | class XPlane
|
---|
68 | {
|
---|
69 | private:
|
---|
70 | /**
|
---|
71 | * Type for the set of multi-dataref buffers.
|
---|
72 | */
|
---|
73 | typedef std::set<MultiBuffer*> multiBuffers_t;
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * The waiter to use.
|
---|
77 | */
|
---|
78 | scpl::io::Waiter waiter;
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * The local client socket over which we are communicating with X-Plane.
|
---|
82 | */
|
---|
83 | scpl::io::LocalClientSocket* socket;
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * The data stream to handle the data conversions.
|
---|
87 | */
|
---|
88 | scpl::io::DataStream* stream;
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * The buffers created by this object.
|
---|
92 | */
|
---|
93 | multiBuffers_t multiBuffers;
|
---|
94 |
|
---|
95 | public:
|
---|
96 | /**
|
---|
97 | * Construct the object. It will not be connected to the simulator
|
---|
98 | * yet.
|
---|
99 | */
|
---|
100 | XPlane() throw();
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Destroy the object. If connected, the connection will be
|
---|
104 | * closed. It destroys all existing buffers, so their references
|
---|
105 | * become invalid.
|
---|
106 | */
|
---|
107 | ~XPlane() throw();
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Connect to the simulator.
|
---|
111 | */
|
---|
112 | void connect() throw(IOException);
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Check if we are connected to the simulator.
|
---|
116 | */
|
---|
117 | bool isConnected() const throw();
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Disconnect from the simulator.
|
---|
121 | */
|
---|
122 | void disconnect() throw();
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Create a new getter of multiple datarefs and return a reference
|
---|
126 | * to it.
|
---|
127 | */
|
---|
128 | MultiGetter& createMultiGetter() throw();
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Create a new setter of multiple datarefs and return a reference
|
---|
132 | * to it.
|
---|
133 | */
|
---|
134 | MultiSetter& createMultiSetter() throw();
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Destroy the given getter or setter of multiple datarefs. As the
|
---|
138 | * buffer is unregistered, if it has been registered previously,
|
---|
139 | * and unregistration may fail, this function might throw an
|
---|
140 | * exception.
|
---|
141 | *
|
---|
142 | * @return if the buffer was found and could be destroyed
|
---|
143 | */
|
---|
144 | bool destroyMultiBuffer(MultiBuffer& buffer) throw(Exception);
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Get the versions of X-Plane, XPLM and the XPLRA plugin.
|
---|
148 | */
|
---|
149 | void getVersions(int& xplaneVersion, int& xplmVersion, int& xplraVersion) throw(Exception);
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Reload the plugins loaded in X-Plane. After this the connection
|
---|
153 | * fails.
|
---|
154 | */
|
---|
155 | void reloadPlugins() throw(Exception);
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Get the integer value of the dataref with the given name.
|
---|
159 | */
|
---|
160 | int getInt(const char* name) throw(Exception);
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Get a float value.
|
---|
164 | */
|
---|
165 | float getFloat(const char* name) throw(Exception);
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Get a double value.
|
---|
169 | */
|
---|
170 | double getDouble(const char* name) throw(Exception);
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Get a possibly partial array of floats.
|
---|
174 | *
|
---|
175 | * @param length the length of the destination buffer
|
---|
176 | * @param offset the offset from which to get the array
|
---|
177 | *
|
---|
178 | * @return the number of values acquired actually
|
---|
179 | */
|
---|
180 | size_t getFloatArray(const char* name, float* dest,
|
---|
181 | size_t length, size_t offset = 0) throw(Exception);
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Get a possibly partial array of floats. The result array will
|
---|
185 | * be created with a length needed to hold the returned value.
|
---|
186 | *
|
---|
187 | * @param offset the offset from which to get the array
|
---|
188 | */
|
---|
189 | float* getFloatArray(const char* name, size_t& length,
|
---|
190 | size_t offset = 0) throw(Exception);
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Get a possibly partial array of integers.
|
---|
194 | *
|
---|
195 | * @param length the length of the destination buffer
|
---|
196 | * @param offset the offset from which to get the array
|
---|
197 | *
|
---|
198 | * @return the number of values acquired actually
|
---|
199 | */
|
---|
200 | size_t getIntArray(const char* name, int32_t* dest,
|
---|
201 | size_t length, size_t offset = 0) throw(Exception);
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Get a possibly partial array of integers. The result array will
|
---|
205 | * be created with a length needed to hold the returned value.
|
---|
206 | *
|
---|
207 | * @param offset the offset from which to get the array
|
---|
208 | */
|
---|
209 | int32_t* getIntArray(const char* name, size_t& length,
|
---|
210 | size_t offset = 0) throw(Exception);
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Get a possibly partial array of bytes.
|
---|
214 | *
|
---|
215 | * @param length the length of the destination buffer
|
---|
216 | * @param offset the offset from which to get the array
|
---|
217 | *
|
---|
218 | * @return the number of values acquired actually
|
---|
219 | */
|
---|
220 | size_t getByteArray(const char* name, uint8_t* dest,
|
---|
221 | size_t length, size_t offset = 0) throw(Exception);
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Get a possibly partial array of bytes. The result array will
|
---|
225 | * be created with a length needed to hold the returned value.
|
---|
226 | *
|
---|
227 | * @param offset the offset from which to get the array
|
---|
228 | */
|
---|
229 | uint8_t* getByteArray(const char* name, size_t& lengyh,
|
---|
230 | size_t offset = 0) throw(Exception);
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Get a string. A string is a byte array.
|
---|
234 | */
|
---|
235 | std::string getString(const char* name, size_t offset = 0) throw(Exception);
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Set the given dataref to the given integer value.
|
---|
239 | */
|
---|
240 | void setInt(const char* name, int value) throw(Exception);
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * Set the given dataref to the given float value.
|
---|
244 | */
|
---|
245 | void setFloat(const char* name, float value) throw(Exception);
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Set the given dataref to the given double value.
|
---|
249 | */
|
---|
250 | void setDouble(const char* name, double value) throw(Exception);
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Set the given float array to values in the given buffer.
|
---|
254 | */
|
---|
255 | void setFloatArray(const char* name, const float* values, size_t length,
|
---|
256 | size_t offset = 0) throw(Exception);
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Set the given integer array to values in the given buffer.
|
---|
260 | */
|
---|
261 | void setIntArray(const char* name, const int32_t* values, size_t length,
|
---|
262 | size_t offset = 0) throw(Exception);
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Set the given byte array to values in the given buffer.
|
---|
266 | */
|
---|
267 | void setByteArray(const char* name, const uint8_t* values, size_t length,
|
---|
268 | size_t offset = 0) throw(Exception);
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Set the given string to the given value. Since strings are byte
|
---|
272 | * arrays, and the bytes after the string should be zeroed, the
|
---|
273 | * length must be given. The string will be converted to a byte
|
---|
274 | * array of the given length, padded with 0s.
|
---|
275 | */
|
---|
276 | void setString(const char* name, const char* value, size_t length,
|
---|
277 | size_t offset = 0) throw(Exception);
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * Show a textual message for a certain duration.
|
---|
281 | */
|
---|
282 | void showMessage(const char* message, float duration) throw(Exception);
|
---|
283 |
|
---|
284 | private:
|
---|
285 | /**
|
---|
286 | * Check if we have a stream and if it has not failed.
|
---|
287 | */
|
---|
288 | void checkStream() throw(NotConnectedException, IOException);
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Check the given protocol result. If it signifies an error,
|
---|
292 | * throw a ProtocolException with the correct error code.
|
---|
293 | */
|
---|
294 | void checkResult(uint8_t result, bool hasParameter = false,
|
---|
295 | long parameter = 0) throw(ProtocolException);
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * Read and check the result. If it signifies an error,
|
---|
299 | * throw a ProtocolException with the correct error code. If there
|
---|
300 | * is some problem with the stream, an IOException is thrown.
|
---|
301 | */
|
---|
302 | void checkResult(bool multi = false) throw(ProtocolException, IOException);
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Issue the command to get a single, scalar value. The result is
|
---|
306 | * also checked, but the value should be read by the caller.
|
---|
307 | */
|
---|
308 | void getScalar(const char* name, uint8_t type) throw(Exception);
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * Issue the command to get an array of values of the given
|
---|
312 | * type. It checks the result and retrieves the number of value
|
---|
313 | * items available.
|
---|
314 | *
|
---|
315 | * @return the number of value items available
|
---|
316 | */
|
---|
317 | size_t getArray(const char* name, uint8_t type,
|
---|
318 | ssize_t length, size_t offset) throw(Exception);
|
---|
319 |
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * Issue the command to set a scalar value of the given type.
|
---|
323 | */
|
---|
324 | void setScalar(const char* name, uint8_t type) throw(Exception);
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Issue the command to set an array value of the given type.
|
---|
328 | */
|
---|
329 | void setArray(const char* name, uint8_t type, size_t length,
|
---|
330 | size_t offset) throw(Exception);
|
---|
331 |
|
---|
332 | friend class MultiBuffer;
|
---|
333 | friend class MultiGetter;
|
---|
334 | friend class MultiSetter;
|
---|
335 | };
|
---|
336 |
|
---|
337 | //------------------------------------------------------------------------------
|
---|
338 | // Inline definitions
|
---|
339 | //------------------------------------------------------------------------------
|
---|
340 |
|
---|
341 | inline XPlane::XPlane() throw() :
|
---|
342 | socket(0),
|
---|
343 | stream(0)
|
---|
344 | {
|
---|
345 | }
|
---|
346 |
|
---|
347 | //------------------------------------------------------------------------------
|
---|
348 |
|
---|
349 | inline bool XPlane::isConnected() const throw()
|
---|
350 | {
|
---|
351 | return socket!=0;
|
---|
352 | }
|
---|
353 |
|
---|
354 | //------------------------------------------------------------------------------
|
---|
355 |
|
---|
356 | } /* namespace hu::varadiistvan::xplra */ } /* namespace hu::varadiistvan */ } /* namespace hu */
|
---|
357 |
|
---|
358 | //------------------------------------------------------------------------------
|
---|
359 | #endif // HU_VARADIISTVAN_XPLRA_XPLANE_H
|
---|
360 |
|
---|
361 | // Local Variables:
|
---|
362 | // mode: C++
|
---|
363 | // c-basic-offset: 4
|
---|
364 | // indent-tabs-mode: nil
|
---|
365 | // End:
|
---|