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 <inttypes.h>
|
---|
38 |
|
---|
39 | //-----------------------------------------------------------------------------
|
---|
40 |
|
---|
41 | namespace hu { namespace varadiistvan { namespace scpl { namespace io {
|
---|
42 |
|
---|
43 | class LocalClientSocket;
|
---|
44 | class DataStream;
|
---|
45 |
|
---|
46 | } /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
|
---|
47 |
|
---|
48 | //------------------------------------------------------------------------------
|
---|
49 |
|
---|
50 | namespace hu { namespace varadiistvan { namespace xplra {
|
---|
51 |
|
---|
52 | //------------------------------------------------------------------------------
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * The main class to access X-Plane.
|
---|
56 | *
|
---|
57 | * The calls are synchronous and not thread-safe.
|
---|
58 | */
|
---|
59 | class XPlane
|
---|
60 | {
|
---|
61 | private:
|
---|
62 | /**
|
---|
63 | * The waiter to use.
|
---|
64 | */
|
---|
65 | scpl::io::Waiter waiter;
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * The local client socket over which we are communicating with X-Plane.
|
---|
69 | */
|
---|
70 | scpl::io::LocalClientSocket* socket;
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * The data stream to handle the data conversions.
|
---|
74 | */
|
---|
75 | scpl::io::DataStream* stream;
|
---|
76 |
|
---|
77 | public:
|
---|
78 | /**
|
---|
79 | * Construct the object. It will not be connected to the simulator
|
---|
80 | * yet.
|
---|
81 | */
|
---|
82 | XPlane() throw();
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Destroy the object. If connected, the connection will be
|
---|
86 | * closed.
|
---|
87 | */
|
---|
88 | ~XPlane() throw();
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Connect to the simulator.
|
---|
92 | */
|
---|
93 | void connect() throw(IOException);
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Check if we are connected to the simulator.
|
---|
97 | */
|
---|
98 | bool isConnected() const throw();
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Disconnect from the simulator.
|
---|
102 | */
|
---|
103 | void disconnect() throw();
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Get the integer value of the dataref with the given name.
|
---|
107 | */
|
---|
108 | int getInt(const char* name) throw(Exception);
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Get a float value.
|
---|
112 | */
|
---|
113 | float getFloat(const char* name) throw(Exception);
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Get a double value.
|
---|
117 | */
|
---|
118 | double getDouble(const char* name) throw(Exception);
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Get a possibly partial array of floats.
|
---|
122 | *
|
---|
123 | * @param length the length of the destination buffer
|
---|
124 | * @param offset the offset from which to get the array
|
---|
125 | *
|
---|
126 | * @return the number of values acquired actually
|
---|
127 | */
|
---|
128 | size_t getFloatArray(const char* name, float* dest,
|
---|
129 | size_t length, size_t offset = 0) throw(Exception);
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Get a possibly partial array of floats. The result array will
|
---|
133 | * be created with a length needed to hold the returned value.
|
---|
134 | *
|
---|
135 | * @param offset the offset from which to get the array
|
---|
136 | */
|
---|
137 | float* getFloatArray(const char* name, size_t& length,
|
---|
138 | size_t offset = 0) throw(Exception);
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Get a possibly partial array of integers.
|
---|
142 | *
|
---|
143 | * @param length the length of the destination buffer
|
---|
144 | * @param offset the offset from which to get the array
|
---|
145 | *
|
---|
146 | * @return the number of values acquired actually
|
---|
147 | */
|
---|
148 | size_t getIntArray(const char* name, int32_t* dest,
|
---|
149 | size_t length, size_t offset = 0) throw(Exception);
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Get a possibly partial array of integers. The result array will
|
---|
153 | * be created with a length needed to hold the returned value.
|
---|
154 | *
|
---|
155 | * @param offset the offset from which to get the array
|
---|
156 | */
|
---|
157 | int32_t* getIntArray(const char* name, size_t& length,
|
---|
158 | size_t offset = 0) throw(Exception);
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Get a possibly partial array of bytes.
|
---|
162 | *
|
---|
163 | * @param length the length of the destination buffer
|
---|
164 | * @param offset the offset from which to get the array
|
---|
165 | *
|
---|
166 | * @return the number of values acquired actually
|
---|
167 | */
|
---|
168 | size_t getByteArray(const char* name, uint8_t* dest,
|
---|
169 | size_t length, size_t offset = 0) throw(Exception);
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Get a possibly partial array of bytes. The result array will
|
---|
173 | * be created with a length needed to hold the returned value.
|
---|
174 | *
|
---|
175 | * @param offset the offset from which to get the array
|
---|
176 | */
|
---|
177 | uint8_t* getByteArray(const char* name, size_t& lengyh,
|
---|
178 | size_t offset = 0) throw(Exception);
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Get a string. A string is a byte array.
|
---|
182 | */
|
---|
183 | std::string getString(const char* name, size_t offset = 0) throw(Exception);
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Set the given dataref to the given integer value.
|
---|
187 | */
|
---|
188 | void setInt(const char* name, int value) throw(Exception);
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Set the given dataref to the given float value.
|
---|
192 | */
|
---|
193 | void setFloat(const char* name, float value) throw(Exception);
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Set the given dataref to the given double value.
|
---|
197 | */
|
---|
198 | void setDouble(const char* name, double value) throw(Exception);
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Set the given float array to values in the given buffer.
|
---|
202 | */
|
---|
203 | void setFloatArray(const char* name, const float* values, size_t length,
|
---|
204 | size_t offset = 0) throw(Exception);
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Set the given integer array to values in the given buffer.
|
---|
208 | */
|
---|
209 | void setIntArray(const char* name, const int32_t* values, size_t length,
|
---|
210 | size_t offset = 0) throw(Exception);
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Set the given byte array to values in the given buffer.
|
---|
214 | */
|
---|
215 | void setByteArray(const char* name, const uint8_t* values, size_t length,
|
---|
216 | size_t offset = 0) throw(Exception);
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Set the given string to the given value. Since strings are byte
|
---|
220 | * arrays, and the bytes after the string should be zeroed, the
|
---|
221 | * length must be given. The string will be converted to a byte
|
---|
222 | * array of the given length, padded with 0s.
|
---|
223 | */
|
---|
224 | void setString(const char* name, const char* value, size_t length,
|
---|
225 | size_t offset = 0) throw(Exception);
|
---|
226 |
|
---|
227 | private:
|
---|
228 | /**
|
---|
229 | * Check if we have a stream and if it has not failed.
|
---|
230 | */
|
---|
231 | void checkStream() throw(NotConnectedException, IOException);
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Check the given protocol result. If it signifies an error,
|
---|
235 | * throw a ProtocolException with the correct error code.
|
---|
236 | */
|
---|
237 | void checkResult(uint8_t result) throw(ProtocolException);
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Read and check the result. If it signifies an error,
|
---|
241 | * throw a ProtocolException with the correct error code.
|
---|
242 | */
|
---|
243 | void checkResult() throw(ProtocolException);
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * Issue the command to get a single, scalar value. The result is
|
---|
247 | * also checked, but the value should be read by the caller.
|
---|
248 | */
|
---|
249 | void getScalar(const char* name, uint8_t type) throw(Exception);
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Issue the command to get an array of values of the given
|
---|
253 | * type. It checks the result and retrieves the number of value
|
---|
254 | * items available.
|
---|
255 | *
|
---|
256 | * @return the number of value items available
|
---|
257 | */
|
---|
258 | size_t getArray(const char* name, uint8_t type,
|
---|
259 | ssize_t length, size_t offset) throw(Exception);
|
---|
260 |
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Issue the command to set a scalar value of the given type.
|
---|
264 | */
|
---|
265 | void setScalar(const char* name, uint8_t type) throw(Exception);
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Issue the command to set an array value of the given type.
|
---|
269 | */
|
---|
270 | void setArray(const char* name, uint8_t type, size_t length,
|
---|
271 | size_t offset) throw(Exception);
|
---|
272 | };
|
---|
273 |
|
---|
274 | //------------------------------------------------------------------------------
|
---|
275 | // Inline definitions
|
---|
276 | //------------------------------------------------------------------------------
|
---|
277 |
|
---|
278 | inline XPlane::XPlane() throw() :
|
---|
279 | socket(0),
|
---|
280 | stream(0)
|
---|
281 | {
|
---|
282 | }
|
---|
283 |
|
---|
284 | //------------------------------------------------------------------------------
|
---|
285 |
|
---|
286 | inline bool XPlane::isConnected() const throw()
|
---|
287 | {
|
---|
288 | return socket!=0;
|
---|
289 | }
|
---|
290 |
|
---|
291 | //------------------------------------------------------------------------------
|
---|
292 |
|
---|
293 | } /* namespace hu::varadiistvan::xplra */ } /* namespace hu::varadiistvan */ } /* namespace hu */
|
---|
294 |
|
---|
295 | //------------------------------------------------------------------------------
|
---|
296 | #endif // HU_VARADIISTVAN_XPLRA_XPLANE_H
|
---|
297 |
|
---|
298 | // Local Variables:
|
---|
299 | // mode: C++
|
---|
300 | // c-basic-offset: 4
|
---|
301 | // indent-tabs-mode: nil
|
---|
302 | // End:
|
---|