[13] | 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
|
---|
[14] | 31 | //-----------------------------------------------------------------------------
|
---|
| 32 |
|
---|
| 33 | #include "Exception.h"
|
---|
| 34 |
|
---|
| 35 | #include <hu/varadiistvan/scpl/io/Waiter.h>
|
---|
| 36 |
|
---|
[26] | 37 | #include <set>
|
---|
| 38 |
|
---|
[14] | 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 |
|
---|
[13] | 50 | //------------------------------------------------------------------------------
|
---|
| 51 |
|
---|
| 52 | namespace hu { namespace varadiistvan { namespace xplra {
|
---|
| 53 |
|
---|
| 54 | //------------------------------------------------------------------------------
|
---|
| 55 |
|
---|
[26] | 56 | class MultiBuffer;
|
---|
| 57 | class MultiGetter;
|
---|
| 58 | class MultiSetter;
|
---|
| 59 |
|
---|
| 60 | //------------------------------------------------------------------------------
|
---|
| 61 |
|
---|
[14] | 62 | /**
|
---|
| 63 | * The main class to access X-Plane.
|
---|
| 64 | *
|
---|
| 65 | * The calls are synchronous and not thread-safe.
|
---|
| 66 | */
|
---|
[13] | 67 | class XPlane
|
---|
| 68 | {
|
---|
[14] | 69 | private:
|
---|
| 70 | /**
|
---|
[26] | 71 | * Type for the set of multi-dataref buffers.
|
---|
| 72 | */
|
---|
| 73 | typedef std::set<MultiBuffer*> multiBuffers_t;
|
---|
| 74 |
|
---|
| 75 | /**
|
---|
[14] | 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 |
|
---|
[26] | 90 | /**
|
---|
| 91 | * The buffers created by this object.
|
---|
| 92 | */
|
---|
| 93 | multiBuffers_t multiBuffers;
|
---|
| 94 |
|
---|
[14] | 95 | public:
|
---|
| 96 | /**
|
---|
| 97 | * Construct the object. It will not be connected to the simulator
|
---|
| 98 | * yet.
|
---|
| 99 | */
|
---|
[107] | 100 | XPlane() noexcept;
|
---|
[14] | 101 |
|
---|
| 102 | /**
|
---|
| 103 | * Destroy the object. If connected, the connection will be
|
---|
[26] | 104 | * closed. It destroys all existing buffers, so their references
|
---|
| 105 | * become invalid.
|
---|
[14] | 106 | */
|
---|
[107] | 107 | ~XPlane() noexcept;
|
---|
[14] | 108 |
|
---|
| 109 | /**
|
---|
| 110 | * Connect to the simulator.
|
---|
| 111 | */
|
---|
[107] | 112 | void connect();
|
---|
[14] | 113 |
|
---|
| 114 | /**
|
---|
| 115 | * Check if we are connected to the simulator.
|
---|
| 116 | */
|
---|
[107] | 117 | bool isConnected() const noexcept;
|
---|
[14] | 118 |
|
---|
| 119 | /**
|
---|
| 120 | * Disconnect from the simulator.
|
---|
| 121 | */
|
---|
[107] | 122 | void disconnect() noexcept;
|
---|
[14] | 123 |
|
---|
| 124 | /**
|
---|
[26] | 125 | * Create a new getter of multiple datarefs and return a reference
|
---|
| 126 | * to it.
|
---|
| 127 | */
|
---|
[107] | 128 | MultiGetter& createMultiGetter() noexcept;
|
---|
[26] | 129 |
|
---|
| 130 | /**
|
---|
| 131 | * Create a new setter of multiple datarefs and return a reference
|
---|
| 132 | * to it.
|
---|
| 133 | */
|
---|
[107] | 134 | MultiSetter& createMultiSetter() noexcept;
|
---|
[26] | 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 | */
|
---|
[107] | 144 | bool destroyMultiBuffer(MultiBuffer& buffer);
|
---|
[26] | 145 |
|
---|
| 146 | /**
|
---|
[36] | 147 | * Get the versions of X-Plane, XPLM and the XPLRA plugin.
|
---|
| 148 | */
|
---|
[107] | 149 | void getVersions(int& xplaneVersion, int& xplmVersion, int& xplraVersion);
|
---|
[36] | 150 |
|
---|
| 151 | /**
|
---|
[40] | 152 | * Reload the plugins loaded in X-Plane. After this the connection
|
---|
| 153 | * fails.
|
---|
| 154 | */
|
---|
[107] | 155 | void reloadPlugins();
|
---|
[40] | 156 |
|
---|
| 157 | /**
|
---|
[87] | 158 | * Save the current situation into the file with the given path,
|
---|
| 159 | * which is relative to the directory of X-Plane.
|
---|
| 160 | */
|
---|
[107] | 161 | void saveSituation(const char* path);
|
---|
[87] | 162 |
|
---|
| 163 | /**
|
---|
[14] | 164 | * Get the integer value of the dataref with the given name.
|
---|
| 165 | */
|
---|
[107] | 166 | int getInt(const char* name);
|
---|
[14] | 167 |
|
---|
[15] | 168 | /**
|
---|
| 169 | * Get a float value.
|
---|
| 170 | */
|
---|
[107] | 171 | float getFloat(const char* name);
|
---|
[15] | 172 |
|
---|
| 173 | /**
|
---|
| 174 | * Get a double value.
|
---|
| 175 | */
|
---|
[107] | 176 | double getDouble(const char* name);
|
---|
[15] | 177 |
|
---|
[16] | 178 | /**
|
---|
| 179 | * Get a possibly partial array of floats.
|
---|
| 180 | *
|
---|
[66] | 181 | * @param name the name of the dataref
|
---|
| 182 | * @param dest the destination buffer
|
---|
[16] | 183 | * @param length the length of the destination buffer
|
---|
| 184 | * @param offset the offset from which to get the array
|
---|
| 185 | *
|
---|
| 186 | * @return the number of values acquired actually
|
---|
| 187 | */
|
---|
| 188 | size_t getFloatArray(const char* name, float* dest,
|
---|
[107] | 189 | size_t length, size_t offset = 0);
|
---|
[16] | 190 |
|
---|
| 191 | /**
|
---|
| 192 | * Get a possibly partial array of floats. The result array will
|
---|
| 193 | * be created with a length needed to hold the returned value.
|
---|
| 194 | *
|
---|
[66] | 195 | * @param name the name of the dataref
|
---|
| 196 | * @param length will contain the number of floats read
|
---|
[16] | 197 | * @param offset the offset from which to get the array
|
---|
| 198 | */
|
---|
| 199 | float* getFloatArray(const char* name, size_t& length,
|
---|
[107] | 200 | size_t offset = 0);
|
---|
[16] | 201 |
|
---|
| 202 | /**
|
---|
| 203 | * Get a possibly partial array of integers.
|
---|
| 204 | *
|
---|
[66] | 205 | * @param name the name of the dataref
|
---|
| 206 | * @param dest the destination buffer
|
---|
[16] | 207 | * @param length the length of the destination buffer
|
---|
| 208 | * @param offset the offset from which to get the array
|
---|
| 209 | *
|
---|
| 210 | * @return the number of values acquired actually
|
---|
| 211 | */
|
---|
| 212 | size_t getIntArray(const char* name, int32_t* dest,
|
---|
[107] | 213 | size_t length, size_t offset = 0);
|
---|
[16] | 214 |
|
---|
| 215 | /**
|
---|
| 216 | * Get a possibly partial array of integers. The result array will
|
---|
| 217 | * be created with a length needed to hold the returned value.
|
---|
| 218 | *
|
---|
[66] | 219 | * @param name the name of the dataref
|
---|
| 220 | * @param length will contain the number of integers read
|
---|
[16] | 221 | * @param offset the offset from which to get the array
|
---|
| 222 | */
|
---|
| 223 | int32_t* getIntArray(const char* name, size_t& length,
|
---|
[107] | 224 | size_t offset = 0);
|
---|
[16] | 225 |
|
---|
| 226 | /**
|
---|
| 227 | * Get a possibly partial array of bytes.
|
---|
| 228 | *
|
---|
[66] | 229 | * @param name the name of the dataref
|
---|
| 230 | * @param dest the destination buffer
|
---|
[16] | 231 | * @param length the length of the destination buffer
|
---|
| 232 | * @param offset the offset from which to get the array
|
---|
| 233 | *
|
---|
| 234 | * @return the number of values acquired actually
|
---|
| 235 | */
|
---|
| 236 | size_t getByteArray(const char* name, uint8_t* dest,
|
---|
[107] | 237 | size_t length, size_t offset = 0);
|
---|
[16] | 238 |
|
---|
| 239 | /**
|
---|
| 240 | * Get a possibly partial array of bytes. The result array will
|
---|
| 241 | * be created with a length needed to hold the returned value.
|
---|
| 242 | *
|
---|
[66] | 243 | * @param name the name of the dataref
|
---|
| 244 | * @param length will contain the number of bytes read on return
|
---|
[16] | 245 | * @param offset the offset from which to get the array
|
---|
| 246 | */
|
---|
[66] | 247 | uint8_t* getByteArray(const char* name, size_t& length,
|
---|
[107] | 248 | size_t offset = 0);
|
---|
[16] | 249 |
|
---|
| 250 | /**
|
---|
| 251 | * Get a string. A string is a byte array.
|
---|
| 252 | */
|
---|
[107] | 253 | std::string getString(const char* name, size_t offset = 0);
|
---|
[16] | 254 |
|
---|
[17] | 255 | /**
|
---|
| 256 | * Set the given dataref to the given integer value.
|
---|
| 257 | */
|
---|
[107] | 258 | void setInt(const char* name, int value);
|
---|
[17] | 259 |
|
---|
| 260 | /**
|
---|
| 261 | * Set the given dataref to the given float value.
|
---|
| 262 | */
|
---|
[107] | 263 | void setFloat(const char* name, float value);
|
---|
[17] | 264 |
|
---|
| 265 | /**
|
---|
| 266 | * Set the given dataref to the given double value.
|
---|
| 267 | */
|
---|
[107] | 268 | void setDouble(const char* name, double value);
|
---|
[17] | 269 |
|
---|
[18] | 270 | /**
|
---|
| 271 | * Set the given float array to values in the given buffer.
|
---|
| 272 | */
|
---|
| 273 | void setFloatArray(const char* name, const float* values, size_t length,
|
---|
[107] | 274 | size_t offset = 0);
|
---|
[18] | 275 |
|
---|
| 276 | /**
|
---|
| 277 | * Set the given integer array to values in the given buffer.
|
---|
| 278 | */
|
---|
| 279 | void setIntArray(const char* name, const int32_t* values, size_t length,
|
---|
[107] | 280 | size_t offset = 0);
|
---|
[18] | 281 |
|
---|
| 282 | /**
|
---|
| 283 | * Set the given byte array to values in the given buffer.
|
---|
| 284 | */
|
---|
| 285 | void setByteArray(const char* name, const uint8_t* values, size_t length,
|
---|
[107] | 286 | size_t offset = 0);
|
---|
[18] | 287 |
|
---|
| 288 | /**
|
---|
| 289 | * Set the given string to the given value. Since strings are byte
|
---|
| 290 | * arrays, and the bytes after the string should be zeroed, the
|
---|
| 291 | * length must be given. The string will be converted to a byte
|
---|
| 292 | * array of the given length, padded with 0s.
|
---|
| 293 | */
|
---|
| 294 | void setString(const char* name, const char* value, size_t length,
|
---|
[107] | 295 | size_t offset = 0);
|
---|
[18] | 296 |
|
---|
[40] | 297 | /**
|
---|
| 298 | * Show a textual message for a certain duration.
|
---|
| 299 | */
|
---|
[107] | 300 | void showMessage(const char* message, float duration);
|
---|
[40] | 301 |
|
---|
[54] | 302 | /**
|
---|
| 303 | * Register the given hotkey codes for listening. If there is an
|
---|
| 304 | * existing set of hotkeys registered, those will be overwritten.
|
---|
| 305 | */
|
---|
[107] | 306 | void registerHotkeys(const uint16_t* codes, size_t length);
|
---|
[54] | 307 |
|
---|
| 308 | /**
|
---|
| 309 | * Query the registered hotkeys whether they were pressed.
|
---|
| 310 | */
|
---|
[107] | 311 | void queryHotkeys(uint8_t* states, size_t length);
|
---|
[54] | 312 |
|
---|
| 313 | /**
|
---|
| 314 | * Unregister the hotkeys.
|
---|
| 315 | */
|
---|
| 316 | void unregisterHotkeys();
|
---|
| 317 |
|
---|
[14] | 318 | private:
|
---|
| 319 | /**
|
---|
| 320 | * Check if we have a stream and if it has not failed.
|
---|
| 321 | */
|
---|
[107] | 322 | void checkStream();
|
---|
[14] | 323 |
|
---|
| 324 | /**
|
---|
| 325 | * Check the given protocol result. If it signifies an error,
|
---|
| 326 | * throw a ProtocolException with the correct error code.
|
---|
| 327 | */
|
---|
[40] | 328 | void checkResult(uint8_t result, bool hasParameter = false,
|
---|
[107] | 329 | long parameter = 0);
|
---|
[15] | 330 |
|
---|
| 331 | /**
|
---|
[17] | 332 | * Read and check the result. If it signifies an error,
|
---|
[30] | 333 | * throw a ProtocolException with the correct error code. If there
|
---|
| 334 | * is some problem with the stream, an IOException is thrown.
|
---|
[17] | 335 | */
|
---|
[107] | 336 | void checkResult(bool multi = false);
|
---|
[17] | 337 |
|
---|
| 338 | /**
|
---|
[15] | 339 | * Issue the command to get a single, scalar value. The result is
|
---|
| 340 | * also checked, but the value should be read by the caller.
|
---|
| 341 | */
|
---|
[107] | 342 | void getScalar(const char* name, uint8_t type);
|
---|
[16] | 343 |
|
---|
| 344 | /**
|
---|
| 345 | * Issue the command to get an array of values of the given
|
---|
| 346 | * type. It checks the result and retrieves the number of value
|
---|
| 347 | * items available.
|
---|
| 348 | *
|
---|
| 349 | * @return the number of value items available
|
---|
| 350 | */
|
---|
| 351 | size_t getArray(const char* name, uint8_t type,
|
---|
[107] | 352 | ssize_t length, size_t offset);
|
---|
[16] | 353 |
|
---|
[17] | 354 |
|
---|
| 355 | /**
|
---|
| 356 | * Issue the command to set a scalar value of the given type.
|
---|
| 357 | */
|
---|
[107] | 358 | void setScalar(const char* name, uint8_t type);
|
---|
[18] | 359 |
|
---|
| 360 | /**
|
---|
| 361 | * Issue the command to set an array value of the given type.
|
---|
| 362 | */
|
---|
| 363 | void setArray(const char* name, uint8_t type, size_t length,
|
---|
[107] | 364 | size_t offset);
|
---|
[19] | 365 |
|
---|
| 366 | friend class MultiBuffer;
|
---|
| 367 | friend class MultiGetter;
|
---|
| 368 | friend class MultiSetter;
|
---|
[13] | 369 | };
|
---|
| 370 |
|
---|
| 371 | //------------------------------------------------------------------------------
|
---|
[14] | 372 | // Inline definitions
|
---|
| 373 | //------------------------------------------------------------------------------
|
---|
| 374 |
|
---|
[107] | 375 | inline XPlane::XPlane() noexcept :
|
---|
[14] | 376 | socket(0),
|
---|
| 377 | stream(0)
|
---|
| 378 | {
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | //------------------------------------------------------------------------------
|
---|
| 382 |
|
---|
[107] | 383 | inline bool XPlane::isConnected() const noexcept
|
---|
[14] | 384 | {
|
---|
| 385 | return socket!=0;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | //------------------------------------------------------------------------------
|
---|
[13] | 389 |
|
---|
| 390 | } /* namespace hu::varadiistvan::xplra */ } /* namespace hu::varadiistvan */ } /* namespace hu */
|
---|
| 391 |
|
---|
| 392 | //------------------------------------------------------------------------------
|
---|
| 393 | #endif // HU_VARADIISTVAN_XPLRA_XPLANE_H
|
---|
| 394 |
|
---|
| 395 | // Local Variables:
|
---|
| 396 | // mode: C++
|
---|
| 397 | // c-basic-offset: 4
|
---|
| 398 | // indent-tabs-mode: nil
|
---|
| 399 | // End:
|
---|