X-Plane Remote Access Plugin and Client Library
MultiGetter.cc
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 //------------------------------------------------------------------------------
30 
31 #include "MultiGetter.h"
32 
33 #include "XPlane.h"
34 
35 #include <xplra/Protocol.h>
36 
37 #include <hu/varadiistvan/scpl/io/DataStream.h>
38 
39 #include <cstdio>
40 
41 //------------------------------------------------------------------------------
42 
44 
45 using xplra::Protocol;
46 
47 using std::vector;
48 using std::min;
49 
50 //------------------------------------------------------------------------------
51 
52 MultiGetter::MultiGetter(XPlane& xplane) noexcept :
53  MultiBuffer(xplane, Protocol::COMMAND_REGISTER_GET_MULTI,
54  Protocol::COMMAND_UNREGISTER_GET_MULTI)
55 {
56 }
57 
58 //------------------------------------------------------------------------------
59 
60 inline void MultiGetter::read(const DataRef& dataRef)
61 {
62  switch (dataRef.type) {
63  case Protocol::TYPE_INT:
64  *reinterpret_cast<int32_t*>(data + dataRef.dataOffset) =
65  xplane.stream->readS32();
66  break;
67  case Protocol::TYPE_FLOAT:
68  *reinterpret_cast<float*>(data + dataRef.dataOffset) =
69  xplane.stream->readFloat();
70  break;
71  case Protocol::TYPE_DOUBLE:
72  *reinterpret_cast<double*>(data + dataRef.dataOffset) =
73  xplane.stream->readDouble();
74  break;
75  case Protocol::TYPE_FLOAT_ARRAY:
76  case Protocol::TYPE_INT_ARRAY:
77  case Protocol::TYPE_BYTE_ARRAY:
78  {
79  size_t length = xplane.stream->readS32();
81  length = min(length, dataRef.length);
82  size_t elementSize =
83  (dataRef.type==Protocol::TYPE_FLOAT_ARRAY) ? sizeof(float) :
84  ((dataRef.type==Protocol::TYPE_INT_ARRAY) ?
85  sizeof(int32_t) : sizeof(uint8_t));
86  xplane.stream->read(data + dataRef.dataOffset, length * elementSize);
87  }
88  }
89 }
90 
91 //------------------------------------------------------------------------------
92 
94 {
96  xplane.stream->writeU8(Protocol::COMMAND_EXECUTE_GET_MULTI);
97  xplane.stream->writeU32(registeredID);
98  xplane.stream->flush();
99  xplane.checkResult(true);
100 
101  readValues();
102 }
103 
104 
105 //------------------------------------------------------------------------------
106 
108 {
109  writeSpec(Protocol::COMMAND_GET_MULTI);
110 
111  readValues();
112 }
113 
114 //------------------------------------------------------------------------------
115 
117 {
118  for(vector<DataRef>::const_iterator i = dataRefs.begin();
119  i!=dataRefs.end(); ++i)
120  {
122  const DataRef& dataRef = *i;
123  read(dataRef);
124  }
125 }
126 
127 //------------------------------------------------------------------------------
128 
129 // Local Variables:
130 // mode: C++
131 // c-basic-offset: 4
132 // indent-tabs-mode: nil
133 // End:
void writeSpec(uint8_t command) const
Definition: MultiBuffer.cc:578
std::vector< DataRef > dataRefs
Definition: MultiBuffer.h:122
void read(const DataRef &dataRef)
Definition: MultiGetter.cc:60
scpl::io::DataStream * stream
Definition: XPlane.h:94
void checkResult(uint8_t result, bool hasParameter=false, long parameter=0)
Definition: XPlane.cc:113