simple_message.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Southwest Research Institute
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * * Neither the name of the Southwest Research Institute, nor the names
16  * of its contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef FLATHEADERS
35 #else
36 #include "simple_message.h"
37 #include "log_wrapper.h"
38 #endif
39 
40 #ifdef SIMPLE_MESSAGE_MOTOPLUS
41 #include "motoPlus.h"
42 #endif
43 
44 
45 using namespace industrial::byte_array;
46 
47 namespace industrial
48 {
49 
50 namespace simple_message
51 {
52 
53 SimpleMessage::SimpleMessage(void)
54 {
55 }
56 
57 SimpleMessage::~SimpleMessage(void)
58 {
59 }
60 
61 
62 
63 bool SimpleMessage::init(int msgType, int commType, int replyCode)
64 {
65  ByteArray data;
66  data.init();
67  return this->init(msgType, commType, replyCode, data);
68 }
69 
70 bool SimpleMessage::init(int msgType, int commType, int replyCode, ByteArray & data )
71 {
72  LOG_COMM("SimpleMessage::init(type: %d, comm: %d, reply: %d, data[%d]...)",
73  msgType, commType, replyCode, data.getBufferSize());
74  this->setMessageType(msgType);
75  this->setCommType(commType);
76  this->setReplyCode(replyCode);
77  this->data_.copyFrom(data);
78 
79  return this->validateMessage();
80 }
81 
82 bool SimpleMessage::init(ByteArray & msg)
83 {
84  int dataSize = 0;
85  bool rtn = false;
86 
87  if (msg.getBufferSize() >= this->getHeaderSize())
88  {
89  // Check to see if the message is larger than the standard header
90  // If so, copy out the data portion.
91  if (msg.getBufferSize() > this->getHeaderSize())
92  {
93  dataSize = msg.getBufferSize() - this->getHeaderSize();
94  LOG_COMM("Unloading data portion of message: %d bytes", dataSize);
95  msg.unload(this->data_, dataSize);
96  }
97  LOG_COMM("Unloading header data");
98  msg.unload(this->reply_code_);
99  msg.unload(this->comm_type_);
100  msg.unload(this->message_type_);
101  LOG_COMM("SimpleMessage::init(type: %d, comm: %d, reply: %d, data[%d]...)",
102  this->message_type_, this->comm_type_, this->reply_code_, this->data_.getBufferSize());
103  rtn = this->validateMessage();
104  }
105  else
106  {
107  LOG_ERROR("Failed to init message, buffer size too small: %u", msg.getBufferSize());
108  rtn = false;
109  }
110  return rtn;
111 }
112 
113 void SimpleMessage::toByteArray(ByteArray & msg)
114 {
115  msg.init();
116 
117  msg.load(this->getMessageType());
118  msg.load(this->getCommType());
119  msg.load(this->getReplyCode());
120  if (this->data_.getBufferSize() > 0 )
121  {
122  msg.load(this->data_);
123  }
124 
125 }
126 
127 
128 void SimpleMessage::setData( ByteArray & data)
129 {
130  this->data_.copyFrom(data);
131 }
132 
133 
134 bool SimpleMessage::validateMessage()
135 {
136 
137  if ( StandardMsgTypes::INVALID == this->getMessageType())
138  {
139  LOG_WARN("Invalid message type: %u", this->getMessageType());
140  return false;
141  }
142 
143  if ( CommTypes::INVALID == this->getCommType())
144  {
145  LOG_WARN("Invalid comms. type: %u", this->getCommType());
146  return false;
147  }
148 
149  if (
150  (CommTypes::SERVICE_REPLY == this->getCommType() &&
151  ReplyTypes::INVALID == this->getReplyCode()) ||
152  ((CommTypes::SERVICE_REPLY != this->getCommType() &&
153  ReplyTypes::INVALID != this->getReplyCode()))
154  )
155  {
156  LOG_WARN("Invalid reply. Comm type: %u, Reply type: %u",
157  this->getCommType(), this->getReplyCode());
158  return false;
159  }
160 
161  return true;
162 }
163 
164 
165 
166 
167 } // namespace simple_message
168 } // namespace industrial
LOG_WARN
#define LOG_WARN(format,...)
Definition: log_wrapper.h:133
industrial
Definition: byte_array.h:45
simple_message.h
industrial::byte_array::ByteArray::unload
bool unload(industrial::shared_types::shared_bool &value)
unloads a boolean value from the byte array
Definition: byte_array.cpp:233
LOG_ERROR
#define LOG_ERROR(format,...)
Definition: log_wrapper.h:134
industrial::byte_array::ByteArray
The byte array wraps a dynamic array of bytes (i.e. char).
Definition: byte_array.h:80
LOG_COMM
#define LOG_COMM(format,...)
Definition: log_wrapper.h:130
industrial::byte_array::ByteArray::init
void init()
Initializes or Reinitializes an empty buffer.
Definition: byte_array.cpp:62
industrial::simple_message::StandardMsgTypes::INVALID
@ INVALID
Definition: simple_message.h:60
log_wrapper.h
industrial::byte_array
Definition: byte_array.h:56
industrial::byte_array::ByteArray::load
bool load(industrial::shared_types::shared_bool value)
loads a boolean into the byte array
Definition: byte_array.cpp:142
init
void init(const M_string &remappings)
industrial::simple_message::CommTypes::SERVICE_REPLY
@ SERVICE_REPLY
Definition: simple_message.h:100
industrial::byte_array::ByteArray::getBufferSize
unsigned int getBufferSize()
gets current buffer size
Definition: byte_array.cpp:387


simple_message
Author(s): Shaun Edwards
autogenerated on Wed Mar 2 2022 00:24:53