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 MOTOPLUS
43 #include "motoPlus.h"
44 #endif
45 
46 using namespace industrial::simple_message;
47 using namespace industrial::byte_array;
48 
49 namespace industrial
50 {
51 
52 namespace smpl_msg_connection
53 {
54 
55 
56 bool SmplMsgConnection::sendMsg(SimpleMessage & message)
57 {
58  bool rtn;
59  ByteArray sendBuffer;
60  ByteArray msgData;
61 
62  if (message.validateMessage())
63  {
64  message.toByteArray(msgData);
65  sendBuffer.load((int)msgData.getBufferSize());
66  sendBuffer.load(msgData);
67  rtn = this->sendBytes(sendBuffer);
68  }
69  else
70  {
71  rtn = false;
72  LOG_ERROR("Message validation failed, message not sent");
73  }
74 
75 return rtn;
76 }
77 
78 
79 bool SmplMsgConnection::receiveMsg(SimpleMessage & message)
80 {
81  ByteArray lengthBuffer;
82  ByteArray msgBuffer;
83  int length;
84 
85  bool rtn = false;
86 
87 
88  rtn = this->receiveBytes(lengthBuffer, message.getLengthSize());
89 
90  if (rtn)
91  {
92  rtn = lengthBuffer.unload(length);
93  LOG_COMM("Message length: %d", length);
94 
95  if (rtn)
96  {
97  rtn = this->receiveBytes(msgBuffer, length);
98 
99  if (rtn)
100  {
101  rtn = message.init(msgBuffer);
102  }
103  else
104  {
105  LOG_ERROR("Failed to initialize message");
106  rtn = false;
107  }
108 
109  }
110  else
111  {
112  LOG_ERROR("Failed to receive message");
113  rtn = false;
114  }
115  }
116  else
117  {
118  LOG_ERROR("Failed to receive message length");
119  rtn = false;
120  }
121 
122  return rtn;
123 }
124 
125 
126 
127 bool SmplMsgConnection::sendAndReceiveMsg(SimpleMessage & send, SimpleMessage & recv, bool verbose)
128 {
129  bool rtn = false;
130  rtn = this->sendMsg(send);
131  if (rtn)
132  {
133  if(verbose) {
134  LOG_ERROR("Sent message");
135  }
136  rtn = this->receiveMsg(recv);
137  if(verbose) {
138  LOG_ERROR("Got message");
139  }
140  }
141  else
142  {
143  rtn = false;
144  }
145 
146  return rtn;
147 }
148 
149 
150 }//smpl_msg_connection
151 }//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...
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:104
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:108
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 Sat Sep 21 2019 03:30:09