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 "Types.h"
27 #include "TransmissionException.h"
28 //#include <strstream>
29 #include <stdexcept>
30 #include <boost/format.hpp>
31 #include <vector>
33 
34 struct Response
35 {
36 public:
37  Response(int res, unsigned char cmdId, status_t status, const std::vector<unsigned char>& data, unsigned int len)
38  : res(res), cmdId(cmdId), status(status), data(data), len(len) {}
39 
40  unsigned int getUInt(int index)
41  {
42  return (unsigned int)data[index] | ((unsigned int)data[index + 1] << 8) | ((unsigned int)data[index + 2] << 16) | ((unsigned int)data[index + 3] << 24);
43  }
44 
45  unsigned short getShort(int index)
46  {
47  return (unsigned short)data[index] | ((unsigned short)data[index + 1] << 8);
48  }
49  unsigned char getByte(int index)
50  {
51  return data[index];
52  }
53 
54  void ensureMinLength(int len)
55  {
56  if (res < len)
57  {
58  //std::strstream strStream;
59  //strStream << "Response length is too short, should be = " << len << " (is " << res << ")";
60  //throw std::runtime_error(strStream.str());
61  throw TransmissionException(str(boost::format("Response length is too short, should be = %1% (is %2%)") % len % res));
62  }
63  }
64 
66  {
67  if (status != E_SUCCESS)
68  {
69  //std::strstream strStream;
70  //strStream << "Command not successful: " << status_to_str( status );
71  //throw std::runtime_error(strStream.str());
72  std::stringstream ss;
73  ss << " status != E_SUCCESS";
74 
75  for (int i = 0; i < (int)len; i++)
76  {
77  ss << boost::format("%02X ") % (int)data[i];
78  }
79 
80  ARMARX_ERROR_S << ss.str();
81  throw TransmissionException(str(boost::format("Command not successful: %1% (0x%2$02X)") % status_to_str(status) % status));
82  }
83  }
84 
85  int res;
86  unsigned char cmdId;
88  std::vector<unsigned char> data;
89  unsigned int len;
90 
91  static const char* status_to_str(status_t status)
92  {
93  switch (status)
94  {
95  case E_SUCCESS:
96  return ("No error");
97 
98  case E_NOT_AVAILABLE:
99  return ("Service or data is not available");
100 
101  case E_NO_SENSOR:
102  return ("No sensor connected");
103 
104  case E_NOT_INITIALIZED:
105  return ("The device is not initialized");
106 
107  case E_ALREADY_RUNNING:
108  return ("Service is already running");
109 
111  return ("The requested feature is not supported");
112 
113  case E_INCONSISTENT_DATA:
114  return ("One or more dependent parameters mismatch");
115 
116  case E_TIMEOUT:
117  return ("Timeout error");
118 
119  case E_READ_ERROR:
120  return ("Error while reading from a device");
121 
122  case E_WRITE_ERROR:
123  return ("Error while writing to a device");
124 
126  return ("No memory available");
127 
128  case E_CHECKSUM_ERROR:
129  return ("Checksum error");
130 
131  case E_NO_PARAM_EXPECTED:
132  return ("No parameters expected");
133 
134  case E_NOT_ENOUGH_PARAMS:
135  return ("Not enough parameters");
136 
137  case E_CMD_UNKNOWN:
138  return ("Unknown command");
139 
140  case E_CMD_FORMAT_ERROR:
141  return ("Command format error");
142 
143  case E_ACCESS_DENIED:
144  return ("Access denied");
145 
146  case E_ALREADY_OPEN:
147  return ("Interface already open");
148 
149  case E_CMD_FAILED:
150  return ("Command failed");
151 
152  case E_CMD_ABORTED:
153  return ("Command aborted");
154 
155  case E_INVALID_HANDLE:
156  return ("Invalid handle");
157 
158  case E_NOT_FOUND:
159  return ("Device not found");
160 
161  case E_NOT_OPEN:
162  return ("Device not open");
163 
164  case E_IO_ERROR:
165  return ("General I/O-Error");
166 
167  case E_INVALID_PARAMETER:
168  return ("Invalid parameter");
169 
171  return ("Index out of bounds");
172 
173  case E_CMD_PENDING:
174  return ("Command is pending...");
175 
176  case E_OVERRUN:
177  return ("Data overrun");
178 
179  case E_RANGE_ERROR:
180  return ("Value out of range");
181 
182  case E_AXIS_BLOCKED:
183  return ("Axis is blocked");
184 
185  case E_FILE_EXISTS:
186  return ("File already exists");
187 
188  default:
189  return ("Internal error. Unknown error code.");
190  }
191  }
192 };
193 
Response::len
unsigned int len
Definition: Response.h:89
E_NOT_FOUND
@ E_NOT_FOUND
Definition: Types.h:59
Response::getByte
unsigned char getByte(int index)
Definition: Response.h:49
E_CMD_UNKNOWN
@ E_CMD_UNKNOWN
Definition: Types.h:52
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:42
Response::cmdId
unsigned char cmdId
Definition: Response.h:86
Response::data
std::vector< unsigned char > data
Definition: Response.h:88
E_TIMEOUT
@ E_TIMEOUT
Definition: Types.h:45
Response::res
int res
Definition: Response.h:85
index
uint8_t index
Definition: EtherCATFrame.h:59
E_WRITE_ERROR
@ E_WRITE_ERROR
Definition: Types.h:47
E_NOT_ENOUGH_PARAMS
@ E_NOT_ENOUGH_PARAMS
Definition: Types.h:51
TransmissionException.h
Response::status_to_str
static const char * status_to_str(status_t status)
Definition: Response.h:91
status_t
status_t
Definition: Types.h:36
E_NO_PARAM_EXPECTED
@ E_NO_PARAM_EXPECTED
Definition: Types.h:50
E_IO_ERROR
@ E_IO_ERROR
Definition: Types.h:61
E_FEATURE_NOT_SUPPORTED
@ E_FEATURE_NOT_SUPPORTED
Definition: Types.h:43
E_INCONSISTENT_DATA
@ E_INCONSISTENT_DATA
Definition: Types.h:44
E_CMD_FORMAT_ERROR
@ E_CMD_FORMAT_ERROR
Definition: Types.h:53
Response::ensureMinLength
void ensureMinLength(int len)
Definition: Response.h:54
E_INDEX_OUT_OF_BOUNDS
@ E_INDEX_OUT_OF_BOUNDS
Definition: Types.h:63
Response::status
status_t status
Definition: Response.h:87
E_ALREADY_RUNNING
@ E_ALREADY_RUNNING
Definition: Types.h:42
E_INSUFFICIENT_RESOURCES
@ E_INSUFFICIENT_RESOURCES
Definition: Types.h:48
ARMARX_ERROR_S
#define ARMARX_ERROR_S
Definition: Logging.h:209
Response::getUInt
unsigned int getUInt(int index)
Definition: Response.h:40
E_OVERRUN
@ E_OVERRUN
Definition: Types.h:65
E_NOT_AVAILABLE
@ E_NOT_AVAILABLE
Definition: Types.h:39
E_CMD_FAILED
@ E_CMD_FAILED
Definition: Types.h:56
E_ACCESS_DENIED
@ E_ACCESS_DENIED
Definition: Types.h:54
E_NO_SENSOR
@ E_NO_SENSOR
Definition: Types.h:40
Response::Response
Response(int res, unsigned char cmdId, status_t status, const std::vector< unsigned char > &data, unsigned int len)
Definition: Response.h:37
E_INVALID_HANDLE
@ E_INVALID_HANDLE
Definition: Types.h:58
E_INVALID_PARAMETER
@ E_INVALID_PARAMETER
Definition: Types.h:62
E_CHECKSUM_ERROR
@ E_CHECKSUM_ERROR
Definition: Types.h:49
Response::ensureSuccess
void ensureSuccess()
Definition: Response.h:65
E_NOT_INITIALIZED
@ E_NOT_INITIALIZED
Definition: Types.h:41
Response
Definition: Response.h:34
E_NOT_OPEN
@ E_NOT_OPEN
Definition: Types.h:60
Response::getShort
unsigned short getShort(int index)
Definition: Response.h:45
E_READ_ERROR
@ E_READ_ERROR
Definition: Types.h:46
TransmissionException
Definition: TransmissionException.h:28
Logging.h
E_CMD_ABORTED
@ E_CMD_ABORTED
Definition: Types.h:57
E_SUCCESS
@ E_SUCCESS
Definition: Types.h:38
E_CMD_PENDING
@ E_CMD_PENDING
Definition: Types.h:64
E_RANGE_ERROR
@ E_RANGE_ERROR
Definition: Types.h:66
E_ALREADY_OPEN
@ E_ALREADY_OPEN
Definition: Types.h:55
Types.h
E_FILE_EXISTS
@ E_FILE_EXISTS
Definition: Types.h:68
E_AXIS_BLOCKED
@ E_AXIS_BLOCKED
Definition: Types.h:67