Lely core libraries  1.9.2
sdo_error.cpp
Go to the documentation of this file.
1 
24 #include "coapp.hpp"
25 #include <lely/co/sdo.h>
26 #include <lely/coapp/sdo_error.hpp>
27 
28 #include <iomanip>
29 #include <sstream>
30 
31 namespace lely {
32 
33 namespace canopen {
34 
35 namespace {
36 
37 class SdoErrcCategory : public ::std::error_category {
38  public:
39  const char*
40  name() const noexcept override {
41  return "SDO";
42  }
43 
44  ::std::error_condition default_error_condition(int ev) const
45  noexcept override;
46  // bool equivalent(int code, const ::std::error_condition& condition) const
47  // noexcept override; bool equivalent(const std::error_code& code, int
48  // condition) const noexcept override;
49 
50  ::std::string
51  message(int ev) const override {
52  return co_sdo_ac2str(ev);
53  }
54 };
55 
56 ::std::error_condition
57 SdoErrcCategory::default_error_condition(int ev) const noexcept {
58  switch (static_cast<SdoErrc>(ev)) {
59  case SdoErrc::TOGGLE:
60  return ::std::errc::protocol_error;
61  case SdoErrc::TIMEOUT:
62  return ::std::errc::timed_out;
63  case SdoErrc::NO_CS:
64  case SdoErrc::BLK_SIZE:
65  case SdoErrc::BLK_SEQ:
66  return ::std::errc::protocol_error;
67  case SdoErrc::BLK_CRC:
68  return ::std::errc::illegal_byte_sequence;
69  case SdoErrc::NO_MEM:
70  return ::std::errc::not_enough_memory;
71  case SdoErrc::NO_ACCESS:
72  case SdoErrc::NO_READ:
73  case SdoErrc::NO_WRITE:
74  return ::std::errc::permission_denied;
75  case SdoErrc::NO_OBJ:
76  return ::std::errc::no_such_file_or_directory;
77  // case SdoErrc::NO_PDO:
78  // case SdoErrc::PDO_LEN:
79  case SdoErrc::PARAM:
80  return ::std::errc::invalid_argument;
81  case SdoErrc::COMPAT:
82  return ::std::errc::no_such_device_or_address;
83  case SdoErrc::HARDWARE:
84  return ::std::errc::io_error;
85  case SdoErrc::TYPE_LEN:
88  return ::std::errc::invalid_argument;
89  case SdoErrc::NO_SUB:
90  return ::std::errc::no_such_file_or_directory;
91  case SdoErrc::PARAM_VAL:
92  return ::std::errc::invalid_argument;
93  case SdoErrc::PARAM_HI:
94  case SdoErrc::PARAM_LO:
96  return ::std::errc::result_out_of_range;
97  case SdoErrc::NO_SDO:
98  return ::std::errc::protocol_not_supported;
99  // case SdoErrc::ERROR:
100  case SdoErrc::DATA:
101  return ::std::errc::io_error;
102  case SdoErrc::DATA_CTL:
103  case SdoErrc::DATA_DEV:
104  return ::std::errc::device_or_resource_busy;
105  // case SdoErrc::NO_OD:
106  case SdoErrc::NO_VAL:
107  return ::std::errc::no_message_available;
108  default:
109  return ::std::error_condition(ev, *this);
110  }
111 }
112 
113 ::std::string
114 SdoWhat(uint8_t netid, uint8_t id, uint16_t idx, uint8_t subidx,
115  ::std::error_code ec, const ::std::string& what_arg = "") {
116  ::std::stringstream ss;
117  ss << ::std::uppercase << ::std::setfill('0') << ::std::hex;
118  if (!what_arg.empty()) ss << what_arg << ':';
119  ss << ::std::setw(2) << int(netid) << ':' << int(id) << ':';
120  ss << ::std::setw(4) << idx << ':' << ::std::setw(2) << int(subidx) << ": ";
121  ss << ec.message();
122  ss << " (" << ::std::setw(8) << uint32_t(ec.value()) << ')';
123  return ss.str();
124 }
125 
126 } // namespace
127 
128 SdoError::SdoError(uint8_t netid, uint8_t id, uint16_t idx, uint8_t subidx,
129  ::std::error_code ec)
130  : ::std::runtime_error(SdoWhat(netid, id, idx, subidx, ec)),
131  netid_(netid),
132  id_(id),
133  idx_(idx),
134  subidx_(subidx),
135  ec_(ec) {}
136 
137 SdoError::SdoError(uint8_t netid, uint8_t id, uint16_t idx, uint8_t subidx,
138  ::std::error_code ec, const ::std::string& what_arg)
139  : ::std::runtime_error(SdoWhat(netid, id, idx, subidx, ec, what_arg)),
140  netid_(netid),
141  id_(id),
142  idx_(idx),
143  subidx_(subidx),
144  ec_(ec) {}
145 
146 SdoError::SdoError(uint8_t netid, uint8_t id, uint16_t idx, uint8_t subidx,
147  ::std::error_code ec, const char* what_arg)
148  : ::std::runtime_error(SdoWhat(netid, id, idx, subidx, ec, what_arg)),
149  netid_(netid),
150  id_(id),
151  idx_(idx),
152  subidx_(subidx),
153  ec_(ec) {}
154 
155 SdoError::SdoError(uint8_t netid, uint8_t id, uint16_t idx, uint8_t subidx,
156  int ev, const char* what_arg)
157  : SdoError(netid, id, idx, subidx, ::std::error_code(ev, SdoCategory()),
158  what_arg) {}
159 
160 SdoError::SdoError(uint8_t netid, uint8_t id, uint16_t idx, uint8_t subidx,
161  int ev)
162  : SdoError(netid, id, idx, subidx, ::std::error_code(ev, SdoCategory())) {}
163 
164 const ::std::error_category&
165 SdoCategory() noexcept {
166  static const SdoErrcCategory category;
167  return category;
168 }
169 
170 ::std::error_code
172  return {static_cast<int>(e), SdoCategory()};
173 }
174 
175 ::std::error_condition
177  return {static_cast<int>(e), SdoCategory()};
178 }
179 
180 } // namespace canopen
181 
182 } // namespace lely
General parameter incompatibility reason.
Data cannot be transferred or stored to the application because of the present device state...
Maximum value is less than minimum value (download only).
Invalid value for parameter (download only).
This header file is part of the C++ CANopen application library; it contains the SDO error declaratio...
Invalid block size (block mode only).
Attempt to write a read only object.
STL namespace.
Access failed due to a hardware error.
Resource not available: SDO connection.
This header file is part of the CANopen library; it contains the Service Data Object (SDO) declaratio...
Unsupported access to an object.
Data cannot be transferred or stored to the application because of local control. ...
The mask to get/set the toggle bit from an NMT state.
Sub-index does not exist.
No data available. (NO_DATA is a macro defined in <netdb.h>)
Data cannot be transferred or stored to the application.
SdoErrc
The SDO abort codes.
Definition: sdo_error.hpp:37
Data type does not match, length of service parameter too high.
Data type does not match, length of service parameter does not match.
CRC error (block mode only).
Invalid sequence number (block mode only).
This is the internal header file of the C++ CANopen application library.
const char * co_sdo_ac2str(co_unsigned32_t ac)
Returns a string describing an SDO abort code.
Definition: sdo.c:57
SDO protocol timed out.
Object does not exist in the object dictionary.
Client/server command specifier not valid or unknown.
Value of parameter written too low (download only).
::std::error_code make_error_code(SdoErrc e) noexcept
Creates an error code corresponding to an SDO abort code.
Definition: sdo_error.cpp:171
Data type does not match, length of service parameter too low.
Global namespace for the Lely Industries N.V. libraries.
Definition: buf.hpp:32
General internal incompatibility in the device.
const ::std::error_category & SdoCategory() noexcept
Returns a reference to the error category object for SDO abort codes.
Definition: sdo_error.cpp:165
::std::error_condition make_error_condition(SdoErrc e) noexcept
Creates an error condition corresponding to an SDO abort code.
Definition: sdo_error.cpp:176
Attempt to read a write only object.
Value of parameter written too high (download only).