ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
types.h
Go to the documentation of this file.
1#pragma once
2
3// Public types; no CANopenNode headers here.
4
5#include <array>
6#include <cstddef>
7#include <cstdint>
8#include <cstring>
9#include <type_traits>
10
12
13namespace esphome {
14struct LogString;
15} // namespace esphome
16
17namespace esphome::canopen {
18
19enum class SDOResult : uint8_t {
21 SDO_RESULT_ABORT, // see abort_code
22 SDO_RESULT_TIMEOUT, // no response within response_timeout
23 SDO_RESULT_SIZE_MISMATCH, // value of another width than expected, or over 4 bytes
24 SDO_RESULT_TRANSPORT_ERROR, // couldn't be started or failed locally
25 SDO_RESULT_REJECTED, // not queued: queue full, invalid arguments or failed hub
26};
27// A write that ended with TIMEOUT or TRANSPORT_ERROR may still have been done. SDO has no transaction id, so a late
28// reply to a timed-out request can be taken as the reply to the next request for the same object of that node.
29
30const LogString *sdo_result_to_string(SDOResult result);
31
32// Values use value/size (zero-extended), buffer reads data/len (valid only during the callback). abort_code is set
33// for ABORT and TIMEOUT.
34struct SDOReply {
38 uint8_t size;
39 const uint8_t *data;
40 size_t len;
41
42 bool ok() const { return this->result == SDOResult::SDO_RESULT_SUCCESS; }
43 // sign-extended from size bytes for signed T, bit cast for float
44 template<typename T> T as() const {
45 static_assert(sizeof(T) <= 4, "SDO values are at most 4 bytes");
46 if constexpr (std::is_same_v<T, float>) {
47 float f;
48 std::memcpy(&f, &this->value, sizeof(f));
49 return f;
50 } else if constexpr (std::is_signed_v<T>) {
51 if (this->size == 0 || this->size >= 4)
52 return static_cast<T>(static_cast<int32_t>(this->value));
53 uint32_t sign = 1u << (this->size * 8 - 1);
54 return static_cast<T>(static_cast<int32_t>((this->value ^ sign) - sign));
55 } else {
56 return static_cast<T>(this->value);
57 }
58 }
59};
60
61// Called exactly once per request; with REJECTED before the request call returns false, or from a failed setup().
62using SDOCallback = void (*)(void *context, const SDOReply &reply);
63
64// NMT state as sent in a heartbeat (CiA 301); also used for our own state
65enum class NMTState : uint8_t {
71};
72
73// the states a heartbeat can carry; CiA 301 values are sparse
77
78const LogString *nmt_state_to_string(NMTState state);
79
80// Events of a monitored node (CANopenComponent::monitor_node(), from CANopenNode's heartbeat consumer):
81// ONLINE: first heartbeat, or the first after a timeout; OFFLINE: no heartbeat within the timeout;
82// NMT_CHANGED: the state in the heartbeat changed (also unknown -> state right after ONLINE, state -> unknown after
83// OFFLINE; not for a boot-up); RESET: boot-up message, the node restarted and lost its runtime configuration, offline
84// until its next heartbeat.
86
87const LogString *node_event_to_string(NodeEvent event);
88
89// NMT commands (CiA 301), see CANopenComponent::send_nmt()
97
98// one object mapped into a PDO, see CANopenComponent::configure_tpdo() / configure_rpdo()
99struct PdoMapping {
100 uint16_t index;
101 uint8_t sub;
102 uint8_t bits; // multiple of 8
103};
104
105// raw CAN frame listener (see CANopenComponent::add_frame_listener()); data is valid only during the call
106using FrameCallback = void (*)(void *context, uint32_t can_id, const uint8_t *data, uint8_t len);
107
108// A raw frame listener, owned by the caller and kept for the device's lifetime (e.g. a static)
110 uint16_t can_id;
111 uint16_t mask;
113 void *context;
114 FrameListener *next{nullptr}; // set by add_frame_listener()
115};
116
117// EMCY frame (CiA 301): error code, error register (0x1001), manufacturer-specific bytes
118struct Emergency {
119 uint16_t error_code;
121 std::array<uint8_t, 5> manufacturer_data;
122};
123
124// Heartbeat frame: COB-ID 0x700 + node (1..127), 1 byte. Returns false for any other frame.
125inline bool decode_heartbeat(uint32_t can_id, const uint8_t *data, size_t len, uint8_t &node, NMTState &state) {
126 if (can_id < 0x701 || can_id > 0x77F || len != 1)
127 return false;
128 node = can_id - 0x700;
129 // bit 7 is the node guarding toggle bit, always 0 in a heartbeat
130 const auto raw = static_cast<NMTState>(data[0] & 0x7F);
132 for (NMTState known : NMT_STATES) {
133 if (known == raw)
134 state = raw;
135 }
136 return true;
137}
138
139// EMCY frame: COB-ID 0x80 + node (1..127; 0x80 itself is SYNC), 8 bytes. Returns false for any other frame.
140inline bool decode_emcy(uint32_t can_id, const uint8_t *data, size_t len, uint8_t &node, Emergency &emcy) {
141 if (can_id < 0x81 || can_id > 0xFF || len != 8)
142 return false;
143 node = can_id - 0x80;
144 emcy.error_code = encode_uint16(data[1], data[0]);
145 emcy.error_register = data[2];
146 std::memcpy(emcy.manufacturer_data.data(), data + 3, emcy.manufacturer_data.size());
147 return true;
148}
149
150} // namespace esphome::canopen
uint8_t raw[35]
Definition bl0939.h:0
bool state
Definition fan.h:2
bool decode_heartbeat(uint32_t can_id, const uint8_t *data, size_t len, uint8_t &node, NMTState &state)
Definition types.h:125
void(*)(void *context, uint32_t can_id, const uint8_t *data, uint8_t len) FrameCallback
Definition types.h:106
const LogString * sdo_result_to_string(SDOResult result)
bool decode_emcy(uint32_t can_id, const uint8_t *data, size_t len, uint8_t &node, Emergency &emcy)
Definition types.h:140
const LogString * nmt_state_to_string(NMTState state)
Definition canopen.cpp:33
const LogString * node_event_to_string(NodeEvent event)
constexpr std::array< NMTState, 4 > NMT_STATES
Definition types.h:74
void(*)(void *context, const SDOReply &reply) SDOCallback
Definition types.h:62
const void size_t len
Definition hal.h:68
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1227
static void uint32_t
std::array< uint8_t, 5 > manufacturer_data
Definition types.h:121
const uint8_t * data
Definition types.h:39
esp_err_t result