smpl_msg_connection.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
36 #else
37 #include "log_wrapper.h"
38 #include "smpl_msg_connection.h"
39 #include "byte_array.h"
40 #endif
41 
42 #ifdef SIMPLE_MESSAGE_MOTOPLUS
43 #include "motoPlus.h"
44 #endif
45 
46 using namespace industrial::simple_message;
47 using namespace industrial::shared_types;
48 using namespace industrial::byte_array;
49 
50 namespace industrial
51 {
52 
53 namespace smpl_msg_connection
54 {
55 
56 
57 bool SmplMsgConnection::sendMsg(SimpleMessage & message)
58 {
59  bool rtn;
60  ByteArray sendBuffer;
61  ByteArray msgData;
62 
63  if (message.validateMessage())
64  {
65  message.toByteArray(msgData);
66  sendBuffer.load((int)msgData.getBufferSize());
67  sendBuffer.load(msgData);
68  rtn = this->sendBytes(sendBuffer);
69  }
70  else
71  {
72  rtn = false;
73  LOG_ERROR("Message validation failed, message not sent");
74  }
75 
76 return rtn;
77 }
78 
79 
80 bool SmplMsgConnection::receiveMsg(SimpleMessage & message)
81 {
82  return receiveMsg(message, -1);
83 }
84 
85 
86 bool SmplMsgConnection::receiveMsg(SimpleMessage & message, shared_int timeout_ms)
87 {
88  ByteArray lengthBuffer;
89  ByteArray msgBuffer;
90  int length;
91 
92  bool rtn = false;
93 
94 
95  rtn = this->receiveBytes(lengthBuffer, message.getLengthSize(), timeout_ms);
96 
97  if (rtn)
98  {
99  rtn = lengthBuffer.unload(length);
100  LOG_COMM("Message length: %d", length);
101 
102  if (rtn)
103  {
104  rtn = this->receiveBytes(msgBuffer, length, timeout_ms);
105 
106  if (rtn)
107  {
108  rtn = message.init(msgBuffer);
109  }
110  else
111  {
112  LOG_ERROR("Failed to initialize message");
113  rtn = false;
114  }
115 
116  }
117  else
118  {
119  LOG_ERROR("Failed to receive message");
120  rtn = false;
121  }
122  }
123  else
124  {
125  LOG_ERROR("Failed to receive message length");
126  rtn = false;
127  }
128 
129  return rtn;
130 }
131 
132 
133 bool SmplMsgConnection::sendAndReceiveMsg(SimpleMessage & send, SimpleMessage & recv, bool verbose)
134 {
135  return sendAndReceiveMsg(send, recv, -1, verbose);
136 }
137 
138 bool SmplMsgConnection::sendAndReceiveMsg(SimpleMessage & send, SimpleMessage & recv,
139  shared_int timeout_ms, bool verbose)
140 {
141  bool rtn = false;
142  rtn = this->sendMsg(send);
143  if (rtn)
144  {
145  if(verbose) {
146  LOG_ERROR("Sent message");
147  }
148  rtn = this->receiveMsg(recv, timeout_ms);
149  if(verbose) {
150  LOG_ERROR("Got message");
151  }
152  }
153  else
154  {
155  rtn = false;
156  }
157 
158  return rtn;
159 }
160 
161 
162 }//smpl_msg_connection
163 }//industrial
void toByteArray(industrial::byte_array::ByteArray &msg)
Populates a raw byte array with the message. Any data stored in the passed in byte array is deleted...
Contains platform specific type definitions that guarantee the size of primitive data types...
Definition: shared_types.h:52
bool validateMessage()
performs logical checks to ensure that the message is fully defined and adheres to the message conven...
This class defines a simple messaging protocol for communicating with an industrial robot controller...
#define LOG_COMM(format,...)
Definition: log_wrapper.h:130
bool load(industrial::shared_types::shared_bool value)
loads a boolean into the byte array
Definition: byte_array.cpp:142
#define LOG_ERROR(format,...)
Definition: log_wrapper.h:134
The byte array wraps a dynamic array of bytes (i.e. char).
Definition: byte_array.h:80
bool init(int msgType, int commType, int replyCode, industrial::byte_array::ByteArray &data)
Initializes a fully populated simple message.
static unsigned int getLengthSize()
Gets size of message length member in bytes (fixed)
unsigned int getBufferSize()
gets current buffer size
Definition: byte_array.cpp:387
bool unload(industrial::shared_types::shared_bool &value)
unloads a boolean value from the byte array
Definition: byte_array.cpp:233


simple_message
Author(s): Shaun Edwards
autogenerated on Mon Jul 19 2021 12:45:19