// Copyright (c) 2013 by István Váradi // This file is part of XPLRA, a remote-access plugin for X-Plane // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // The views and conclusions contained in the software and documentation are those // of the authors and should not be interpreted as representing official policies, // either expressed or implied, of the FreeBSD Project. //------------------------------------------------------------------------------ #include "XPlane.h" #include #include #include #include //------------------------------------------------------------------------------ using hu::varadiistvan::xplra::XPlane; using hu::varadiistvan::scpl::io::LocalClientSocket; using hu::varadiistvan::scpl::io::LocalConnector; using hu::varadiistvan::scpl::io::DataStream; using xplra::Protocol; using std::auto_ptr; using std::string; //------------------------------------------------------------------------------ void XPlane::checkStream() throw(NotConnectedException, IOException) { if (stream==0) { throw NotConnectedException(); } else if (stream->failed()) { throw IOException(stream->getErrorCode()); } } //------------------------------------------------------------------------------ void XPlane::checkResult(uint8_t result) throw(ProtocolException) { switch(result) { case Protocol::RESULT_OK: return; case Protocol::RESULT_INVALID_COMMAND: throw ProtocolException(ProtocolException::INVALID_COMMAND); case Protocol::RESULT_UNKNOWN_DATAREF: throw ProtocolException(ProtocolException::UNKNOWN_DATAREF); case Protocol::RESULT_INVALID_TYPE: throw ProtocolException(ProtocolException::INVALID_TYPE); case Protocol::RESULT_INVALID_LENGTH: throw ProtocolException(ProtocolException::INVALID_LENGTH); case Protocol::RESULT_INVALID_OFFSET: throw ProtocolException(ProtocolException::INVALID_OFFSET); case Protocol::RESULT_INVALID_COUNT: throw ProtocolException(ProtocolException::INVALID_COUNT); case Protocol::RESULT_INVALID_ID: throw ProtocolException(ProtocolException::INVALID_ID); case Protocol::RESULT_OTHER_ERROR: default: throw ProtocolException(ProtocolException::OTHER); } } //------------------------------------------------------------------------------ void XPlane::checkResult() throw(ProtocolException) { uint8_t result = stream->readU8(); checkStream(); checkResult(result); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ XPlane::~XPlane() throw() { disconnect(); } //------------------------------------------------------------------------------ void XPlane::connect() throw(IOException) { if (socket!=0) return; auto_ptr clientSocket(new LocalClientSocket("xplra", &waiter)); LocalConnector& connector = clientSocket->getConnector(); while (!connector.connect()) { if (connector.failed()) { throw IOException(connector.getErrorCode()); } waiter.wait(); if (waiter.failed()) { throw IOException(waiter.getErrorCode()); } } socket = clientSocket.release(); stream = new DataStream(*socket); } //------------------------------------------------------------------------------ void XPlane::disconnect() throw() { if (socket==0) return; delete stream; stream = 0; delete socket; socket = 0; } //------------------------------------------------------------------------------ void XPlane::getScalar(const char* name, uint8_t type) throw(Exception) { checkStream(); stream->writeU8(Protocol::COMMAND_GET_SINGLE); stream->writeString(name, strlen(name)); stream->writeU8(type); if (!stream->flush()) checkStream(); checkResult(); } //------------------------------------------------------------------------------ int XPlane::getInt(const char* name) throw(Exception) { getScalar(name, Protocol::TYPE_INT); int value = stream->readS32(); checkStream(); return value; } //------------------------------------------------------------------------------ float XPlane::getFloat(const char* name) throw(Exception) { getScalar(name, Protocol::TYPE_FLOAT); float value = stream->readFloat(); checkStream(); return value; } //------------------------------------------------------------------------------ double XPlane::getDouble(const char* name) throw(Exception) { getScalar(name, Protocol::TYPE_DOUBLE); double value = stream->readDouble(); checkStream(); return value; } //------------------------------------------------------------------------------ size_t XPlane::getArray(const char* name, uint8_t type, ssize_t length, size_t offset) throw(Exception) { checkStream(); stream->writeU8(Protocol::COMMAND_GET_SINGLE); stream->writeString(name, strlen(name)); stream->writeU8(type); stream->writeS32(static_cast(length)); stream->writeS32(static_cast(offset)); if (!stream->flush()) checkStream(); uint8_t result = stream->readU8(); length = stream->readS32(); checkStream(); checkResult(result); return length; } //------------------------------------------------------------------------------ size_t XPlane::getFloatArray(const char* name, float* dest, size_t length, size_t offset) throw(Exception) { length = getArray(name, Protocol::TYPE_FLOAT_ARRAY, length, offset); if (!stream->read(dest, length*sizeof(float))) checkStream(); return length; } //------------------------------------------------------------------------------ float* XPlane::getFloatArray(const char* name, size_t& length, size_t offset) throw(Exception) { length = getArray(name, Protocol::TYPE_FLOAT_ARRAY, -1, offset); auto_ptr data(new float[length]); if (!stream->read(data.get(), length*sizeof(float))) checkStream(); return data.release(); } //------------------------------------------------------------------------------ size_t XPlane::getIntArray(const char* name, int32_t* dest, size_t length, size_t offset) throw(Exception) { length = getArray(name, Protocol::TYPE_INT_ARRAY, length, offset); if (!stream->read(dest, length*sizeof(int32_t))) checkStream(); return length; } //------------------------------------------------------------------------------ int32_t* XPlane::getIntArray(const char* name, size_t& length, size_t offset) throw(Exception) { length = getArray(name, Protocol::TYPE_INT_ARRAY, -1, offset); auto_ptr data(new int32_t[length]); if (!stream->read(data.get(), length*sizeof(int32_t))) checkStream(); return data.release(); } //------------------------------------------------------------------------------ size_t XPlane::getByteArray(const char* name, uint8_t* dest, size_t length, size_t offset) throw(Exception) { length = getArray(name, Protocol::TYPE_BYTE_ARRAY, length, offset); if (!stream->read(dest, length*sizeof(uint8_t))) checkStream(); return length; } //------------------------------------------------------------------------------ uint8_t* XPlane::getByteArray(const char* name, size_t& length, size_t offset) throw(Exception) { length = getArray(name, Protocol::TYPE_BYTE_ARRAY, -1, offset); auto_ptr data(new uint8_t[length]); if (!stream->read(data.get(), length*sizeof(uint8_t))) checkStream(); return data.release(); } //------------------------------------------------------------------------------ string XPlane::getString(const char* name, size_t offset) throw(Exception) { size_t length = 0; auto_ptr data(getByteArray(name, length, offset)); return string(reinterpret_cast(data.get())); } //------------------------------------------------------------------------------ void XPlane::setScalar(const char* name, uint8_t type) throw(Exception) { stream->writeU8(Protocol::COMMAND_SET_SINGLE); stream->writeString(name, strlen(name)); stream->writeU8(type); } //------------------------------------------------------------------------------ void XPlane::setInt(const char* name, int value) throw(Exception) { setScalar(name, Protocol::TYPE_INT); stream->writeS32(value); stream->flush(); checkResult(); } //------------------------------------------------------------------------------ void XPlane::setFloat(const char* name, float value) throw(Exception) { setScalar(name, Protocol::TYPE_FLOAT); stream->writeFloat(value); stream->flush(); checkResult(); } //------------------------------------------------------------------------------ void XPlane::setDouble(const char* name, double value) throw(Exception) { setScalar(name, Protocol::TYPE_DOUBLE); stream->writeDouble(value); stream->flush(); checkResult(); } //------------------------------------------------------------------------------ void XPlane::setArray(const char* name, uint8_t type, size_t length, size_t offset) throw(Exception) { stream->writeU8(Protocol::COMMAND_SET_SINGLE); stream->writeString(name, strlen(name)); stream->writeU8(type); stream->writeS32(static_cast(length)); stream->writeS32(static_cast(offset)); } //------------------------------------------------------------------------------ void XPlane::setFloatArray(const char* name, const float* values, size_t length, size_t offset) throw(Exception) { setArray(name, Protocol::TYPE_FLOAT_ARRAY, length, offset); stream->write(values, length*sizeof(*values)); stream->flush(); checkResult(); } //------------------------------------------------------------------------------ void XPlane::setIntArray(const char* name, const int32_t* values, size_t length, size_t offset) throw(Exception) { setArray(name, Protocol::TYPE_INT_ARRAY, length, offset); stream->write(values, length*sizeof(*values)); stream->flush(); checkResult(); } //------------------------------------------------------------------------------ void XPlane::setByteArray(const char* name, const uint8_t* values, size_t length, size_t offset) throw(Exception) { setArray(name, Protocol::TYPE_BYTE_ARRAY, length, offset); stream->write(values, length*sizeof(*values)); stream->flush(); checkResult(); } //------------------------------------------------------------------------------ void XPlane::setString(const char* name, const char* value, size_t length, size_t offset) throw(Exception) { size_t valueLength = strlen(value); if ((valueLength+1)>=length) { setByteArray(name, reinterpret_cast(value), length, offset); } else { auto_ptr buffer(new uint8_t[length]); memcpy(buffer.get(), value, valueLength); memset(buffer.get() + valueLength, 0, length - valueLength); setByteArray(name, buffer.get(), length, offset); } } //------------------------------------------------------------------------------ // Local Variables: // mode: C++ // c-basic-offset: 4 // indent-tabs-mode: nil // End: