ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
od_handler.h
Go to the documentation of this file.
1#pragma once
2
3// OD handlers: OdObject subclasses get every access (OdStream) to their entries.
4#include <cstddef>
5#include <cstdint>
6
7#include "esphome/core/defines.h" // before CANopenNode: clang-tidy gets the CO_CONFIG_* values from it
8
9#define OD_DEFINITION
10// Use the library search path: quoted includes match our canopen.h on Windows.
11#include <CANopen.h>
12
13namespace esphome::canopen {
14
15// CANopenNode's ODR_t values; errors become SDO abort codes
16enum class OdResult : int8_t {
17 OD_RESULT_PARTIAL = ODR_PARTIAL, // more read / write calls follow
18 OD_RESULT_OK = ODR_OK,
19 OD_RESULT_OUT_OF_MEMORY = ODR_OUT_OF_MEM,
20 OD_RESULT_UNSUPPORTED_ACCESS = ODR_UNSUPP_ACCESS,
21 OD_RESULT_WRITE_ONLY = ODR_WRITEONLY,
22 OD_RESULT_READ_ONLY = ODR_READONLY,
23 OD_RESULT_NOT_EXIST = ODR_IDX_NOT_EXIST,
24 OD_RESULT_DEVICE_INCOMPATIBLE = ODR_DEV_INCOMPAT,
25 OD_RESULT_HARDWARE = ODR_HW,
26 OD_RESULT_TYPE_MISMATCH = ODR_TYPE_MISMATCH,
27 OD_RESULT_DATA_LONG = ODR_DATA_LONG,
28 OD_RESULT_DATA_SHORT = ODR_DATA_SHORT,
29 OD_RESULT_SUB_NOT_EXIST = ODR_SUB_NOT_EXIST,
30 OD_RESULT_INVALID_VALUE = ODR_INVALID_VALUE,
31 OD_RESULT_VALUE_HIGH = ODR_VALUE_HIGH,
32 OD_RESULT_VALUE_LOW = ODR_VALUE_LOW,
33 OD_RESULT_GENERAL = ODR_GENERAL,
34 OD_RESULT_DATA_TRANSFER = ODR_DATA_TRANSF,
35 OD_RESULT_DEVICE_STATE = ODR_DATA_DEV_STATE,
36 OD_RESULT_NO_DATA = ODR_NO_DATA,
37};
38
39// one access to an OD sub-entry, chunked for large data (wraps OD_stream_t)
40class OdStream {
41 public:
42 explicit OdStream(OD_stream_t *stream) : stream_(stream) {}
43
44 uint16_t index() const { return this->stream_->index; }
45 uint8_t sub() const { return this->stream_->subIndex; }
46 // bytes already transferred; a handler returning PARTIAL advances it
47 size_t offset() const { return this->stream_->dataOffset; }
48 void set_offset(size_t offset) { this->stream_->dataOffset = offset; }
49 // the entry's length. A domain download has 0 until the last chunk, then the total; a read handler sets it at
50 // offset 0 to indicate the upload size.
51 size_t size() const { return this->stream_->dataLength; }
52 void set_size(size_t size) { this->stream_->dataLength = size; }
53 // the entry's storage, may be nullptr
54 void *data() const { return this->stream_->dataOrig; }
55
56 // the default access to the entry's storage
57 OdResult read_original(void *buf, size_t count, size_t *done) {
58 OD_size_t n = 0;
59 auto ret = static_cast<OdResult>(OD_readOriginal(this->stream_, buf, count, &n));
60 *done = n;
61 return ret;
62 }
63 OdResult write_original(const void *buf, size_t count, size_t *done) {
64 OD_size_t n = 0;
65 auto ret = static_cast<OdResult>(OD_writeOriginal(this->stream_, buf, count, &n));
66 *done = n;
67 return ret;
68 }
69
70 protected:
71 OD_stream_t *stream_;
72};
73
74// Base of OD handlers (set_object()): subclasses hide od_read() / od_write(), called without virtual dispatch, for SDO
75// transfers, write_od() / read_od() and PDO-mapped entries, after the attribute check.
76struct OdObject {
77 OdResult od_read(OdStream stream, void *buf, size_t count, size_t *done) {
78 return stream.read_original(buf, count, done);
79 }
80 OdResult od_write(OdStream stream, const void *buf, size_t count, size_t *done) {
81 return stream.write_original(buf, count, done);
82 }
83};
84
85} // namespace esphome::canopen
OdResult write_original(const void *buf, size_t count, size_t *done)
Definition od_handler.h:63
OdResult read_original(void *buf, size_t count, size_t *done)
Definition od_handler.h:57
void set_size(size_t size)
Definition od_handler.h:52
uint16_t index() const
Definition od_handler.h:44
void set_offset(size_t offset)
Definition od_handler.h:48
OdStream(OD_stream_t *stream)
Definition od_handler.h:42
unsigned count
Definition gif.cpp:51
int ret
OdResult od_read(OdStream stream, void *buf, size_t count, size_t *done)
Definition od_handler.h:77
OdResult od_write(OdStream stream, const void *buf, size_t count, size_t *done)
Definition od_handler.h:80
SemaphoreHandle_t done