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
27#include "Types.h"
28//#include <strstream>
29#include <stdexcept>
30#include <vector>
31
32#include <boost/format.hpp>
33
35
37{
38public:
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
125 return ("The device is not initialized");
126
128 return ("Service is already running");
129
131 return ("The requested feature is not supported");
132
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
152 return ("No parameters expected");
153
155 return ("Not enough parameters");
156
157 case E_CMD_UNKNOWN:
158 return ("Unknown command");
159
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
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};
uint8_t index
status_t
Definition Types.h:36
@ E_INVALID_HANDLE
Definition Types.h:57
@ E_OVERRUN
Definition Types.h:64
@ E_TIMEOUT
Definition Types.h:44
@ E_NOT_INITIALIZED
Definition Types.h:40
@ E_INSUFFICIENT_RESOURCES
Definition Types.h:47
@ E_NOT_FOUND
Definition Types.h:58
@ E_ALREADY_RUNNING
Definition Types.h:41
@ E_CMD_PENDING
Definition Types.h:63
@ E_CMD_FORMAT_ERROR
Definition Types.h:52
@ E_ALREADY_OPEN
Definition Types.h:54
@ E_NOT_OPEN
Definition Types.h:59
@ E_INDEX_OUT_OF_BOUNDS
Definition Types.h:62
@ E_INVALID_PARAMETER
Definition Types.h:61
@ E_CMD_FAILED
Definition Types.h:55
@ E_NO_PARAM_EXPECTED
Definition Types.h:49
@ E_CHECKSUM_ERROR
Definition Types.h:48
@ E_FEATURE_NOT_SUPPORTED
Definition Types.h:42
@ E_READ_ERROR
Definition Types.h:45
@ E_RANGE_ERROR
Definition Types.h:65
@ E_CMD_UNKNOWN
Definition Types.h:51
@ E_FILE_EXISTS
Definition Types.h:67
@ E_AXIS_BLOCKED
Definition Types.h:66
@ E_NOT_ENOUGH_PARAMS
Definition Types.h:50
@ E_WRITE_ERROR
Definition Types.h:46
@ E_NO_SENSOR
Definition Types.h:39
@ E_SUCCESS
Definition Types.h:37
@ E_NOT_AVAILABLE
Definition Types.h:38
@ E_CMD_ABORTED
Definition Types.h:56
@ E_IO_ERROR
Definition Types.h:60
@ E_ACCESS_DENIED
Definition Types.h:53
@ E_INCONSISTENT_DATA
Definition Types.h:43
std::string str(const T &t)
#define ARMARX_ERROR_S
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:216
unsigned short getShort(int index)
Definition Response.h:56
void ensureSuccess()
Definition Response.h:82
Response(int res, unsigned char cmdId, status_t status, const std::vector< unsigned char > &data, unsigned int len)
Definition Response.h:39
unsigned int getUInt(int index)
Definition Response.h:49
unsigned char cmdId
Definition Response.h:105
unsigned int len
Definition Response.h:108
unsigned char getByte(int index)
Definition Response.h:62
static const char * status_to_str(status_t status)
Definition Response.h:111
int res
Definition Response.h:104
std::vector< unsigned char > data
Definition Response.h:107
void ensureMinLength(int len)
Definition Response.h:68
status_t status
Definition Response.h:106