ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
canopen_node.h
Go to the documentation of this file.
1#pragma once
2
4
5#include "canopen.h"
6#include "types.h"
7
8namespace esphome::canopen {
9
10// Base of components that drive another node (device profiles): the hub's events for that node, SDO / NMT with its
11// id. Monitored with register_node(heartbeat_timeout=...).
13 public:
14 CANopenNode(CANopenComponent *parent, uint8_t node_id);
15 // the hub callbacks capture this: no copies or moves
16 CANopenNode(const CANopenNode &) = delete;
17 CANopenNode &operator=(const CANopenNode &) = delete;
18
19 CANopenComponent *get_canopen() const { return this->canopen_; }
20 uint8_t get_node_id() const { return this->node_id_; }
21
22 // from the node's heartbeats; false / UNKNOWN if it isn't monitored
23 bool is_online() const { return this->canopen_->is_node_online(this->node_id_); }
24 NMTState get_nmt_state() const { return this->canopen_->get_node_state(this->node_id_); }
25 bool is_operational() const { return this->is_online() && this->get_nmt_state() == NMTState::NMT_STATE_OPERATIONAL; }
26
27 // the default SDO context is the CANopenNode subobject: use from_context<T>(), not static_cast
28 template<typename T> static T *from_context(void *context) {
29 return static_cast<T *>(static_cast<CANopenNode *>(context));
30 }
31
32#ifdef USE_CANOPEN_SDO_CLIENT
33 // SDO calls for this node; a nullptr context means this
34 template<typename T> bool read_sdo(uint16_t index, uint8_t sub, SDOCallback callback, void *context = nullptr) {
35 return this->canopen_->read_sdo<T>(this->node_id_, index, sub, callback, this->context_(context));
36 }
37 template<typename T>
38 bool write_sdo(uint16_t index, uint8_t sub, T value, SDOCallback callback = nullptr, void *context = nullptr) {
39 return this->canopen_->write_sdo<T>(this->node_id_, index, sub, value, callback, this->context_(context));
40 }
41 // data must stay valid until the callback
42 bool read_sdo_data(uint16_t index, uint8_t sub, uint8_t *data, size_t capacity, SDOCallback callback,
43 void *context = nullptr) {
44 return this->canopen_->read_sdo_data(this->node_id_, index, sub, data, capacity, callback, this->context_(context));
45 }
46 // data must stay valid until the callback
47 bool write_sdo_data(uint16_t index, uint8_t sub, const uint8_t *data, size_t len, SDOCallback callback = nullptr,
48 void *context = nullptr) {
49 return this->canopen_->write_sdo_data(this->node_id_, index, sub, data, len, callback, this->context_(context));
50 }
51 // drops context's queued requests and aborts its transfer, without callbacks
52 void cancel_sdo(void *context = nullptr) { this->canopen_->cancel_sdo(this->context_(context)); }
53#endif
54#ifdef USE_CANOPEN_NMT_MASTER
55 bool send_nmt(NMTCommand command) { return this->canopen_->send_nmt(this->node_id_, command); }
56#endif
57
58 protected:
59 // not deleted through a CANopenNode pointer
60 ~CANopenNode() = default;
61
62 // on_node_nmt_changed() also follows online and offline: act on online / reset
63 virtual void on_node_online() {}
64 virtual void on_node_offline() {}
66 // boot-up: the node lost its runtime configuration
67 virtual void on_node_reset() {}
68 virtual void on_node_emcy(const Emergency &emergency) {}
69
70 void *context_(void *context) { return context != nullptr ? context : this; }
71
73 uint8_t node_id_;
74};
75
76} // namespace esphome::canopen
bool read_sdo(uint8_t node_id, uint16_t index, uint8_t sub, uint8_t expected_size, SDOCallback callback, void *context)
bool write_sdo_data(uint8_t node_id, uint16_t index, uint8_t sub, const uint8_t *data, size_t len, SDOCallback callback=nullptr, void *context=nullptr)
bool send_nmt(uint8_t node_id, NMTCommand command)
bool write_sdo(uint8_t node_id, uint16_t index, uint8_t sub, uint32_t value, uint8_t size, SDOCallback callback=nullptr, void *context=nullptr)
bool read_sdo_data(uint8_t node_id, uint16_t index, uint8_t sub, uint8_t *data, size_t capacity, SDOCallback callback, void *context)
NMTState get_node_state(uint8_t node_id)
bool is_node_online(uint8_t node_id)
bool send_nmt(NMTCommand command)
CANopenNode(const CANopenNode &)=delete
CANopenComponent * canopen_
bool write_sdo_data(uint16_t index, uint8_t sub, const uint8_t *data, size_t len, SDOCallback callback=nullptr, void *context=nullptr)
CANopenNode & operator=(const CANopenNode &)=delete
virtual void on_node_nmt_changed(NMTState state)
bool read_sdo(uint16_t index, uint8_t sub, SDOCallback callback, void *context=nullptr)
CANopenNode(CANopenComponent *parent, uint8_t node_id)
bool read_sdo_data(uint16_t index, uint8_t sub, uint8_t *data, size_t capacity, SDOCallback callback, void *context=nullptr)
static T * from_context(void *context)
bool write_sdo(uint16_t index, uint8_t sub, T value, SDOCallback callback=nullptr, void *context=nullptr)
void * context_(void *context)
virtual void on_node_emcy(const Emergency &emergency)
void cancel_sdo(void *context=nullptr)
CANopenComponent * get_canopen() const
NMTState get_nmt_state() const
bool state
Definition fan.h:2
void(*)(void *context, const SDOReply &reply) SDOCallback
Definition types.h:62
uint32_t value
Definition hal.cpp:59
const void size_t len
Definition hal.h:68
uint16_t uint16_t & capacity
Definition helpers.cpp:44