ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
node_monitor.cpp
Go to the documentation of this file.
1// Monitoring of other nodes by their heartbeat (CANopenNode heartbeat consumer, 0x1016): node events and states. The
2// consumer is built only with USE_CANOPEN_NODE_MONITOR (canopen.enable_node_monitor()).
3#include "canopen.h"
4
5#include "esphome/core/log.h"
7
8namespace esphome::canopen {
9
10ESPHOME_LOG_TAG(TAG, "canopen");
11
12// indexed by NodeEvent; the last entry for other values
13PROGMEM_STRING_TABLE(NodeEventStrings, "online", "offline", "NMT changed", "reset", "unknown");
14
15const LogString *node_event_to_string(NodeEvent event) {
16 return NodeEventStrings::get_log_str(static_cast<uint8_t>(event), NodeEventStrings::LAST_INDEX);
17}
18
19#ifdef USE_CANOPEN_NODE_MONITOR
20void CANopenComponent::monitor_node_(uint8_t node_id, uint16_t timeout_ms) {
21 if (this->co_ != nullptr) {
22 ESP_LOGE(TAG, "monitor_node(0x%02x) after setup, ignored", node_id);
23 return;
24 }
25 if (this->is_client_only()) {
26 ESP_LOGE(TAG, "monitor_node(0x%02x): needs a CANopen node (node_id), ignored", node_id);
27 return;
28 }
29 // a timeout of 0 or node id 0 would leave the 0x1016 entry unused
30 if (node_id == 0 || node_id > 127 || node_id == this->node_id_ || timeout_ms == 0) {
31 ESP_LOGE(TAG, "monitor_node(0x%02x, %u ms): needs another node's id (1-127) and a timeout, ignored", node_id,
32 (unsigned) timeout_ms);
33 return;
34 }
35 // CANopenNode rejects a node id that is in 0x1016 twice
36 for (uint32_t &c : this->hb_cons_) {
37 if ((c >> 16) == node_id) {
38 if (timeout_ms < (c & 0xFFFF))
39 c = ((uint32_t) node_id << 16) | timeout_ms;
40 return;
41 }
42 }
43 this->hb_cons_.push_back(((uint32_t) node_id << 16) | timeout_ms);
44}
45
47 if (this->co_ == nullptr || this->hb_cons_.empty())
49 int8_t idx = CO_HBconsumer_getIdxByNodeId(this->co_->HBcons, node_id);
50 CO_NMT_internalState_t state;
51 if (idx < 0 || CO_HBconsumer_getNmtState(this->co_->HBcons, idx, &state) != 0)
53 return to_nmt_state(state);
54}
55
56bool CANopenComponent::is_node_online(uint8_t node_id) {
57 if (this->co_ == nullptr || this->hb_cons_.empty())
58 return false;
59 int8_t idx = CO_HBconsumer_getIdxByNodeId(this->co_->HBcons, node_id);
60 return idx >= 0 && CO_HBconsumer_getState(this->co_->HBcons, idx) == CO_HBconsumer_ACTIVE;
61}
62
64#if ESPHOME_CANOPEN_HEARTBEAT_CLIENTS > 0
65 uint8_t n_cons = this->hb_cons_.size();
66 if (n_cons) {
67 this->hb_cons_count_ = n_cons;
68 // read only: without CO_CONFIG_FLAG_OD_DYNAMIC a write wouldn't reach the consumer
69 this->od_.add_array(0x1016, ODA_SDO_R, &this->hb_cons_count_, this->hb_cons_.data(), sizeof(uint32_t), n_cons);
70 }
71#endif
72}
73
75 uint8_t n_cons = this->hb_cons_.size();
76 cfg.CNT_HB_CONS = n_cons ? 1 : 0;
77 cfg.CNT_ARR_1016 = n_cons;
78 cfg.ENTRY_H1016 = this->od_.find(0x1016);
79}
80
82 CO_HBconsumer_t *hb = this->co_->HBcons;
83 if (hb == nullptr)
84 return; // no monitored nodes
85 // CO_process() would stop the consumer in STOPPED (CiA 301 keeps it running): with the count 0 it skips it and
86 // process_node_monitor_() runs it in every state.
87 this->config_.CNT_HB_CONS = 0;
88 // as a first pass leaves it, so that pass doesn't drop a heartbeat received before our first loop()
89 hb->NMTisPreOrOperationalPrev = true;
90 for (uint8_t i = 0, n = this->hb_cons_.size(); i < n; i++) {
91 CO_HBconsumer_initCallbackHeartbeatStarted(hb, i, this, on_hb_started);
92 CO_HBconsumer_initCallbackNmtChanged(hb, i, this, on_hb_nmt_changed);
93 CO_HBconsumer_initCallbackTimeout(hb, i, this, on_hb_timeout);
94 CO_HBconsumer_initCallbackRemoteReset(hb, i, this, on_hb_remote_reset);
95 }
96}
97
99 if (this->co_->HBcons != nullptr)
100 CO_HBconsumer_process(this->co_->HBcons, true, dt_us, nullptr);
101}
102
104 for (uint32_t c : this->hb_cons_) {
105 ESP_LOGCONFIG(TAG, " Heartbeat client: node 0x%02x, timeout %u ms", (unsigned) (c >> 16), (unsigned) (c & 0xFFFF));
106 }
107}
108#endif // USE_CANOPEN_NODE_MONITOR
109
111 if (event == NodeEvent::NODE_EVENT_OFFLINE) {
112 ESP_LOGW(TAG, "node 0x%02x offline (heartbeat timeout)", node_id);
113 } else {
114 ESP_LOGI(TAG, "node 0x%02x %s, NMT state: %s", node_id, LOG_STR_ARG(node_event_to_string(event)),
115 LOG_STR_ARG(nmt_state_to_string(state)));
116 }
117 this->node_event_callback_.call(node_id, event, state);
118}
119
120#ifdef USE_CANOPEN_NODE_MONITOR
121// CANopenNode heartbeat consumer callbacks (called from CO_process(), before the consumer stores the new state): they
122// only record the event, dispatch_node_events_() runs the user callbacks after CO_process().
123// push_back() ignores an event beyond the capacity, which the 2 per node and pass can't reach.
124void CANopenComponent::on_hb_started(uint8_t node_id, uint8_t idx, void *object) {
125 auto *self = static_cast<CANopenComponent *>(object);
126 // the heartbeat's state is already stored (CO_HBconsumer_getNmtState() returns it once HBstate is ACTIVE)
127 CO_NMT_internalState_t state = self->co_->HBcons->monitoredNodes[idx].NMTstate;
128 self->pending_node_events_.push_back({node_id, NodeEvent::NODE_EVENT_ONLINE, to_nmt_state(state)});
129}
130
131void CANopenComponent::push_node_event(void *object, uint8_t node_id, NodeEvent event, NMTState state) {
132 static_cast<CANopenComponent *>(object)->pending_node_events_.push_back({node_id, event, state});
133}
134
135void CANopenComponent::on_hb_nmt_changed(uint8_t node_id, uint8_t idx, CO_NMT_internalState_t state, void *object) {
136 // a boot-up message: the RESET event reports it, and the consumer already counts the node offline until its next
137 // heartbeat (get_node_state() is UNKNOWN)
138 if (state == CO_NMT_INITIALIZING)
139 return;
141}
142
143void CANopenComponent::on_hb_timeout(uint8_t node_id, uint8_t idx, void *object) {
145}
146
147void CANopenComponent::on_hb_remote_reset(uint8_t node_id, uint8_t idx, void *object) {
149}
150
152 for (const PendingNodeEvent &e : this->pending_node_events_)
153 this->node_event_(e.node_id, e.event, e.state);
154 this->pending_node_events_.clear();
155}
156#endif // USE_CANOPEN_NODE_MONITOR
157
158} // namespace esphome::canopen
void node_event_(uint8_t node_id, NodeEvent event, NMTState state)
static void on_hb_nmt_changed(uint8_t node_id, uint8_t idx, CO_NMT_internalState_t state, void *object)
LazyCallbackManager< void(uint8_t, NodeEvent, NMTState)> node_event_callback_
Definition canopen.h:370
SlotList< uint32_t, ESPHOME_CANOPEN_HEARTBEAT_CLIENTS > hb_cons_
Definition canopen.h:394
NMTState get_node_state(uint8_t node_id)
static void on_hb_timeout(uint8_t node_id, uint8_t idx, void *object)
static void push_node_event(void *object, uint8_t node_id, NodeEvent event, NMTState state)
void process_node_monitor_(uint32_t dt_us)
static void on_hb_started(uint8_t node_id, uint8_t idx, void *object)
void monitor_node_(uint8_t node_id, uint16_t timeout_ms)
StaticVector< PendingNodeEvent, 2 *ESPHOME_CANOPEN_HEARTBEAT_CLIENTS > pending_node_events_
Definition canopen.h:392
bool is_node_online(uint8_t node_id)
static void on_hb_remote_reset(uint8_t node_id, uint8_t idx, void *object)
void config_node_monitor_(CO_config_t &cfg)
OD_entry_t * find(uint16_t index)
Definition od.h:73
void add_array(uint16_t index, OD_attr_t attr, uint8_t *count, void *data, OD_size_t elem_len, uint8_t n, OD_attr_t count_attr=ODA_SDO_R)
Definition od.cpp:39
bool state
Definition fan.h:2
PROGMEM_STRING_TABLE(NMTStateStrings, "bootup", "stopped", "operational", "pre-operational", "unknown")
ESPHOME_LOG_TAG(TAG, "canopen")
NMTState to_nmt_state(CO_NMT_internalState_t state)
Definition canopen.h:41
const LogString * nmt_state_to_string(NMTState state)
Definition canopen.cpp:33
const LogString * node_event_to_string(NodeEvent event)
static void uint32_t