Response.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #pragma once
25 
26 #include "TransmissionException.h"
27 #include "Types.h"
28 //#include <strstream>
29 #include <stdexcept>
30 #include <vector>
31 
32 #include <boost/format.hpp>
33 
35 
36 struct Response
37 {
38 public:
40  unsigned char cmdId,
42  const std::vector<unsigned char>& data,
43  unsigned int len) :
45  {
46  }
47 
48  unsigned int
50  {
51  return (unsigned int)data[index] | ((unsigned int)data[index + 1] << 8) |
52  ((unsigned int)data[index + 2] << 16) | ((unsigned int)data[index + 3] << 24);
53  }
54 
55  unsigned short
57  {
58  return (unsigned short)data[index] | ((unsigned short)data[index + 1] << 8);
59  }
60 
61  unsigned char
63  {
64  return data[index];
65  }
66 
67  void
69  {
70  if (res < len)
71  {
72  //std::strstream strStream;
73  //strStream << "Response length is too short, should be = " << len << " (is " << res << ")";
74  //throw std::runtime_error(strStream.str());
76  str(boost::format("Response length is too short, should be = %1% (is %2%)") % len %
77  res));
78  }
79  }
80 
81  void
83  {
84  if (status != E_SUCCESS)
85  {
86  //std::strstream strStream;
87  //strStream << "Command not successful: " << status_to_str( status );
88  //throw std::runtime_error(strStream.str());
89  std::stringstream ss;
90  ss << " status != E_SUCCESS";
91 
92  for (int i = 0; i < (int)len; i++)
93  {
94  ss << boost::format("%02X ") % (int)data[i];
95  }
96 
97  ARMARX_ERROR_S << ss.str();
99  str(boost::format("Command not successful: %1% (0x%2$02X)") %
101  }
102  }
103 
104  int res;
105  unsigned char cmdId;
107  std::vector<unsigned char> data;
108  unsigned int len;
109 
110  static const char*
112  {
113  switch (status)
114  {
115  case E_SUCCESS:
116  return ("No error");
117 
118  case E_NOT_AVAILABLE:
119  return ("Service or data is not available");
120 
121  case E_NO_SENSOR:
122  return ("No sensor connected");
123 
124  case E_NOT_INITIALIZED:
125  return ("The device is not initialized");
126 
127  case E_ALREADY_RUNNING:
128  return ("Service is already running");
129 
131  return ("The requested feature is not supported");
132 
133  case E_INCONSISTENT_DATA:
134  return ("One or more dependent parameters mismatch");
135 
136  case E_TIMEOUT:
137  return ("Timeout error");
138 
139  case E_READ_ERROR:
140  return ("Error while reading from a device");
141 
142  case E_WRITE_ERROR:
143  return ("Error while writing to a device");
144 
146  return ("No memory available");
147 
148  case E_CHECKSUM_ERROR:
149  return ("Checksum error");
150 
151  case E_NO_PARAM_EXPECTED:
152  return ("No parameters expected");
153 
154  case E_NOT_ENOUGH_PARAMS:
155  return ("Not enough parameters");
156 
157  case E_CMD_UNKNOWN:
158  return ("Unknown command");
159 
160  case E_CMD_FORMAT_ERROR:
161  return ("Command format error");
162 
163  case E_ACCESS_DENIED:
164  return ("Access denied");
165 
166  case E_ALREADY_OPEN:
167  return ("Interface already open");
168 
169  case E_CMD_FAILED:
170  return ("Command failed");
171 
172  case E_CMD_ABORTED:
173  return ("Command aborted");
174 
175  case E_INVALID_HANDLE:
176  return ("Invalid handle");
177 
178  case E_NOT_FOUND:
179  return ("Device not found");
180 
181  case E_NOT_OPEN:
182  return ("Device not open");
183 
184  case E_IO_ERROR:
185  return ("General I/O-Error");
186 
187  case E_INVALID_PARAMETER:
188  return ("Invalid parameter");
189 
191  return ("Index out of bounds");
192 
193  case E_CMD_PENDING:
194  return ("Command is pending...");
195 
196  case E_OVERRUN:
197  return ("Data overrun");
198 
199  case E_RANGE_ERROR:
200  return ("Value out of range");
201 
202  case E_AXIS_BLOCKED:
203  return ("Axis is blocked");
204 
205  case E_FILE_EXISTS:
206  return ("File already exists");
207 
208  default:
209  return ("Internal error. Unknown error code.");
210  }
211  }
212 };
Response::len
unsigned int len
Definition: Response.h:108
E_NOT_FOUND
@ E_NOT_FOUND
Definition: Types.h:58
Response::getByte
unsigned char getByte(int index)
Definition: Response.h:62
E_CMD_UNKNOWN
@ E_CMD_UNKNOWN
Definition: Types.h:51
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:43
Response::cmdId
unsigned char cmdId
Definition: Response.h:105
Response::data
std::vector< unsigned char > data
Definition: Response.h:107
E_TIMEOUT
@ E_TIMEOUT
Definition: Types.h:44
Response::res
int res
Definition: Response.h:104
index
uint8_t index
Definition: EtherCATFrame.h:59
E_WRITE_ERROR
@ E_WRITE_ERROR
Definition: Types.h:46
E_NOT_ENOUGH_PARAMS
@ E_NOT_ENOUGH_PARAMS
Definition: Types.h:50
TransmissionException.h
Response::status_to_str
static const char * status_to_str(status_t status)
Definition: Response.h:111
status_t
status_t
Definition: Types.h:35
E_NO_PARAM_EXPECTED
@ E_NO_PARAM_EXPECTED
Definition: Types.h:49
E_IO_ERROR
@ E_IO_ERROR
Definition: Types.h:60
E_FEATURE_NOT_SUPPORTED
@ E_FEATURE_NOT_SUPPORTED
Definition: Types.h:42
E_INCONSISTENT_DATA
@ E_INCONSISTENT_DATA
Definition: Types.h:43
E_CMD_FORMAT_ERROR
@ E_CMD_FORMAT_ERROR
Definition: Types.h:52
Response::ensureMinLength
void ensureMinLength(int len)
Definition: Response.h:68
E_INDEX_OUT_OF_BOUNDS
@ E_INDEX_OUT_OF_BOUNDS
Definition: Types.h:62
Response::status
status_t status
Definition: Response.h:106
E_ALREADY_RUNNING
@ E_ALREADY_RUNNING
Definition: Types.h:41
E_INSUFFICIENT_RESOURCES
@ E_INSUFFICIENT_RESOURCES
Definition: Types.h:47
ARMARX_ERROR_S
#define ARMARX_ERROR_S
Definition: Logging.h:216
Response::getUInt
unsigned int getUInt(int index)
Definition: Response.h:49
E_OVERRUN
@ E_OVERRUN
Definition: Types.h:64
E_NOT_AVAILABLE
@ E_NOT_AVAILABLE
Definition: Types.h:38
E_CMD_FAILED
@ E_CMD_FAILED
Definition: Types.h:55
E_ACCESS_DENIED
@ E_ACCESS_DENIED
Definition: Types.h:53
E_NO_SENSOR
@ E_NO_SENSOR
Definition: Types.h:39
Response::Response
Response(int res, unsigned char cmdId, status_t status, const std::vector< unsigned char > &data, unsigned int len)
Definition: Response.h:39
E_INVALID_HANDLE
@ E_INVALID_HANDLE
Definition: Types.h:57
E_INVALID_PARAMETER
@ E_INVALID_PARAMETER
Definition: Types.h:61
E_CHECKSUM_ERROR
@ E_CHECKSUM_ERROR
Definition: Types.h:48
Response::ensureSuccess
void ensureSuccess()
Definition: Response.h:82
E_NOT_INITIALIZED
@ E_NOT_INITIALIZED
Definition: Types.h:40
Response
Definition: Response.h:36
E_NOT_OPEN
@ E_NOT_OPEN
Definition: Types.h:59
Response::getShort
unsigned short getShort(int index)
Definition: Response.h:56
E_READ_ERROR
@ E_READ_ERROR
Definition: Types.h:45
TransmissionException
Definition: TransmissionException.h:28
Logging.h
E_CMD_ABORTED
@ E_CMD_ABORTED
Definition: Types.h:56
E_SUCCESS
@ E_SUCCESS
Definition: Types.h:37
E_CMD_PENDING
@ E_CMD_PENDING
Definition: Types.h:63
E_RANGE_ERROR
@ E_RANGE_ERROR
Definition: Types.h:65
E_ALREADY_OPEN
@ E_ALREADY_OPEN
Definition: Types.h:54
Types.h
E_FILE_EXISTS
@ E_FILE_EXISTS
Definition: Types.h:67
E_AXIS_BLOCKED
@ E_AXIS_BLOCKED
Definition: Types.h:66