replyobject.cpp
Go to the documentation of this file.
1 
2 // Copyright (c) 2003-2021 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32 
33 
34 // Copyright (c) 2003-2021 Xsens Technologies B.V. or subsidiaries worldwide.
35 // All rights reserved.
36 //
37 // Redistribution and use in source and binary forms, with or without modification,
38 // are permitted provided that the following conditions are met:
39 //
40 // 1. Redistributions of source code must retain the above copyright notice,
41 // this list of conditions, and the following disclaimer.
42 //
43 // 2. Redistributions in binary form must reproduce the above copyright notice,
44 // this list of conditions, and the following disclaimer in the documentation
45 // and/or other materials provided with the distribution.
46 //
47 // 3. Neither the names of the copyright holders nor the names of their contributors
48 // may be used to endorse or promote products derived from this software without
49 // specific prior written permission.
50 //
51 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
52 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
53 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
54 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
56 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
58 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
60 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
61 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
62 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
63 //
64 
65 #include "replyobject.h"
66 #include "replymonitor.h"
67 #include <xstypes/xsthread.h>
68 #include <xstypes/xsresultvalue.h>
69 
70 using namespace xsens;
71 
74  : m_mutex(new Mutex)
75  , m_waitCondition(new WaitCondition(*m_mutex))
76  , m_delivered(false)
77 {
78 }
79 
81 {
82  try
83  {
84  delete m_waitCondition;
85  delete m_mutex;
86  }
87  catch (...)
88  {
89  }
90 }
91 
95 {
96  xsens::Lock locker(m_mutex);
97 
98  m_message = msg;
99  m_delivered = true;
101 }
102 
106 {
107  xsens::Lock locker(m_mutex);
108 
109  if (!m_delivered)
110  m_waitCondition->wait(timeout);
111  if (m_delivered)
112  return m_message;
113  else
114  return XsMessage();
115 }
116 
121  : ReplyObject()
122  , m_messageId(messageId)
123 {
124 }
125 
129 {
130 }
131 
134 uint8_t MidReplyObject::msgId() const
135 {
136  return m_messageId;
137 }
138 
143 {
144  if (m_messageId == msg.getMessageId())
145  return true;
146 
147  if (msg.getMessageId() == XMID_Error)
148  {
149  assert(msg.getDataSize());
150  return (static_cast<XsResultValue>(msg.getDataByte()) != XRV_DATAOVERFLOW);
151  }
152  return false;
153 }
154 
161 MidAndDataReplyObject::MidAndDataReplyObject(uint8_t messageId, XsSize offset, XsSize size, uint8_t const* data)
162  : ReplyObject()
163  , m_messageId(messageId)
164  , m_dataOffset(offset)
165  , m_dataSize(size)
166  , m_data(0)
167 {
168  assert(m_dataSize > 0);
169  setData(data);
170 }
171 
175 {
176  try
177  {
178  freeData();
179  }
180  catch (...)
181  {
182  assert(false);
183  }
184 }
185 
189 {
190  return m_messageId;
191 }
192 
197 {
198  if (msg.getMessageId() == XMID_Error)
199  return true;
200  if (m_messageId != msg.getMessageId())
201  return false;
202  return memcmp(msg.getDataBuffer(m_dataOffset), m_data, m_dataSize) == 0;
203 }
204 
208 {
209  if (m_data != 0)
210  {
211  free(m_data);
212  m_data = 0;
213  }
214 }
215 
219 void MidAndDataReplyObject::setData(uint8_t const* data)
220 {
221  freeData();
222  if (data != 0)
223  {
224  m_data = reinterpret_cast<uint8_t*>(malloc(m_dataSize));
225  if (m_data != 0)
226  memcpy(m_data, data, m_dataSize);
227  }
228 }
ReplyObject::m_mutex
xsens::Mutex * m_mutex
Definition: replyobject.h:99
ReplyObject
Abstract reply object. Blocks on a semaphore when requesting the message until the message has been s...
Definition: replyobject.h:81
msg
msg
XsMessage
struct XsMessage XsMessage
Definition: xsmessage.h:85
MidAndDataReplyObject::isReplyFor
virtual bool isReplyFor(XsMessage const &message)
Definition: replyobject.cpp:196
replymonitor.h
XMID_Error
@ XMID_Error
Definition: xsxbusmessageid.h:187
ReplyObject::message
XsMessage message(uint32_t timeout)
Blocks until a message has been set by setMessage() then returns that message.
Definition: replyobject.cpp:105
MidAndDataReplyObject::~MidAndDataReplyObject
~MidAndDataReplyObject()
MidAndDataReplyObject destructor.
Definition: replyobject.cpp:174
ReplyObject::~ReplyObject
virtual ~ReplyObject()
Definition: replyobject.cpp:80
xsthread.h
MidAndDataReplyObject::setData
void setData(uint8_t const *data)
Copies data from 'data' into this object. This is the data to wait for.
Definition: replyobject.cpp:219
data
data
MidAndDataReplyObject::freeData
void freeData()
Frees allocated data (if any)
Definition: replyobject.cpp:207
ReplyObject::m_message
XsMessage m_message
Definition: replyobject.h:101
MidReplyObject::MidReplyObject
MidReplyObject(uint8_t messageId)
MidReplyObject constructor.
Definition: replyobject.cpp:120
XsResultValue
XsResultValue
Xsens result values.
Definition: xsresultvalue.h:82
MidAndDataReplyObject::m_dataOffset
XsSize m_dataOffset
Definition: replyobject.h:134
MidAndDataReplyObject::MidAndDataReplyObject
MidAndDataReplyObject(uint8_t messageId, XsSize offset, XsSize size, uint8_t const *data)
MidAndDataReplyObject constructor.
Definition: replyobject.cpp:161
MidReplyObject::m_messageId
uint8_t m_messageId
Definition: replyobject.h:117
uint32_t
unsigned int uint32_t
Definition: pstdint.h:485
xsens::WaitCondition::wait
bool wait()
Wait until we're signalled to continue.
Definition: threading.cpp:994
MidAndDataReplyObject::msgId
virtual uint8_t msgId() const
Definition: replyobject.cpp:188
xsens::WaitCondition::signal
void signal()
Unblock a single waiting thread.
Definition: threading.cpp:967
ReplyObject::ReplyObject
ReplyObject()
Default constructor.
Definition: replyobject.cpp:73
xsens::Mutex
A base mutex class.
Definition: xsens_mutex.h:132
replyobject.h
XsSize
size_t XsSize
XsSize must be unsigned number!
Definition: xstypedefs.h:74
XsMessage
Structure for storing a single message.
Definition: xsmessage.h:202
ReplyObject::m_delivered
bool m_delivered
Definition: replyobject.h:102
ReplyObject::m_waitCondition
xsens::WaitCondition * m_waitCondition
Definition: replyobject.h:100
MidAndDataReplyObject::m_dataSize
XsSize m_dataSize
Definition: replyobject.h:135
XRV_DATAOVERFLOW
@ XRV_DATAOVERFLOW
41: The device generates more data than the bus communication can handle (baud rate may be too low)
Definition: xsresultvalue.h:110
MidReplyObject::isReplyFor
virtual bool isReplyFor(XsMessage const &message)
Definition: replyobject.cpp:142
xsens::Lock
A base class for a Lock.
Definition: xsens_mutex.h:947
MidAndDataReplyObject::m_messageId
uint8_t m_messageId
Definition: replyobject.h:133
ReplyObject::setMessage
void setMessage(const XsMessage &msg)
Sets a message as reply message and trigger the semaphore which will unblock any waiting message() ca...
Definition: replyobject.cpp:94
MidAndDataReplyObject::m_data
uint8_t * m_data
Definition: replyobject.h:136
MidReplyObject::msgId
virtual uint8_t msgId() const
Definition: replyobject.cpp:134
xsens::WaitCondition
A platform independent wait condition implementation.
Definition: xsens_mutex.h:1806
xsresultvalue.h
xsens
Definition: threading.cpp:78
MidReplyObject::~MidReplyObject
~MidReplyObject(void)
MidReplyObject destructor.
Definition: replyobject.cpp:128


xsens_mti_driver
Author(s):
autogenerated on Sun Sep 3 2023 02:43:20