KaCanOpen
 All Classes Functions Variables Typedefs Enumerations Pages
sdo_response.cpp
1 /*
2  * Copyright (c) 2015, Thomas Keh
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * 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 HOLDER 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 #include "sdo_response.h"
33 #include "logger.h"
34 
35 #include <cstdint>
36 #include <string>
37 
38 namespace kaco {
39 
40 uint16_t SDOResponse::get_index() const {
41  return (((uint16_t)data[2])<<8) + (uint16_t)data[1];
42 }
43 
44 uint16_t SDOResponse::get_subindex() const {
45  return data[3];
46 }
47 
48 uint8_t SDOResponse::get_length() const {
49  return 4-((command&0x0C)>>2);
50 }
51 
52 uint8_t SDOResponse::failed() const {
53  return command == 0x80;
54 }
55 
56 uint32_t SDOResponse::get_data() const {
57  return (((uint32_t) data[3+3]) << 24)
58  + (((uint32_t) data[3+2]) << 16)
59  + (((uint32_t) data[3+1]) << 8)
60  + (uint32_t) data[3+0];
61 }
62 
63 void SDOResponse::print() const {
64 
65  DUMP_HEX(node_id);
66  DUMP_HEX(command);
67  DUMP_HEX(get_index());
68  DUMP_HEX(get_subindex());
69 
70  for (unsigned i=0;i<7;++i) {
71  if (data[i]>0) {
72  PRINT("data["<<i<<"] = 0x"<<std::hex<<(unsigned)data[i]);
73  }
74  }
75 
76  DUMP_HEX(failed());
77  DUMP_HEX(get_length());
78  DUMP_HEX(get_data());
79  PRINT(get_error());
80 
81 }
82 
83 std::string SDOResponse::get_error() const {
84 
85  if (!failed()) {
86  return "no error";
87  }
88 
89  switch(get_data()) {
90  case 0x05030000: return "Toggle bit not alternated";
91  case 0x05040000: return "SDO protocol timed out";
92  case 0x05040001: return "Client/server command specifier not valid or unknown";
93  case 0x05040002: return "Invalid block size (block mode only)";
94  case 0x05040003: return "Invalid sequence number (block mode only)";
95  case 0x05040004: return "CRC error (block mode only)";
96  case 0x05040005: return "Out of memory";
97  case 0x06010000: return "Unsupported access to an object";
98  case 0x06010001: return "Attempt to read a write only object";
99  case 0x06010002: return "Attempt to write a read only object";
100  case 0x06020000: return "Object does not exist in the object dictionary";
101  case 0x06040041: return "Object cannot be mapped to the PDO";
102  case 0x06040042: return "The number and length of the objects to be mapped whould exeed PDO length";
103  case 0x06040043: return "General parameter incompatibility reason";
104  case 0x06040047: return "General internal incompatibility in the device";
105  case 0x06060000: return "Access failed due to a hardware error";
106  case 0x06070010: return "Data type does not match, length of service parameter does not match";
107  case 0x06070012: return "Data type does not match, length of service parameter too high";
108  case 0x06070013: return "Data type does not match, length of service parameter too low";
109  case 0x06090011: return "Sub-index does not exist.";
110  case 0x06090030: return "Value range of parameter exceeded (only for write access)";
111  case 0x06090031: return "Value of parameter written too high";
112  case 0x06090032: return "Value of parameter written too low";
113  case 0x06090036: return "Maximum value is less than minimum value";
114  case 0x08000000: return "General error";
115  case 0x08000020: return "Data cannot be transferred or stored to the application";
116  case 0x08000021: return "Data cannot be transferred or stored to the application because of local control";
117  case 0x08000022: return "Data cannot be transferred or stored to the application because ofthe present device";
118  case 0x08000023: return "Object dictionary dynamic generation fails or no object dictionary is present.";
119  default: return "unknown error code";
120  }
121 
122 }
123 
124 }
void print() const
Prints the response to command line.
uint8_t get_length() const
Returns the number of data bytes.
uint8_t failed() const
Check if the transfer failed.
uint16_t get_subindex() const
Returns the subindex (only for expedited transfer).
std::string get_error() const
Returns a human-readable representation of the error (only if failed()==true)
uint8_t command
SDO command specifier.
Definition: sdo_response.h:49
uint16_t get_index() const
Returns the dictionary index (only for expedited transfer).
uint32_t get_data() const
Returns the data as a single 4-byte value (only for expedited transfer).
uint8_t node_id
Node id.
Definition: sdo_response.h:46
uint8_t data[7]
Data bytes.
Definition: sdo_response.h:62