ESPHome 2026.3.3
Loading...
Searching...
No Matches
openthread.h
Go to the documentation of this file.
1#pragma once
3#ifdef USE_OPENTHREAD
4
8
9#include <openthread/srp_client.h>
10#include <openthread/srp_client_buffers.h>
11#include <openthread/instance.h>
12#include <openthread/thread.h>
13
14#include <atomic>
15#include <optional>
16#include <vector>
17
18namespace esphome::openthread {
19
20class InstanceLock;
21
23 public:
26 void dump_config() override;
27 void setup() override;
28 bool teardown() override;
29 float get_setup_priority() const override { return setup_priority::WIFI; }
30
31 bool is_connected() const { return this->connected_; }
33 bool is_lock_initialized() const { return this->lock_initialized_; }
35 std::optional<otIp6Address> get_omr_address();
36 void ot_main();
37 void on_factory_reset(std::function<void()> callback);
39
40 const char *get_use_address() const;
41 void set_use_address(const char *use_address);
42#if CONFIG_OPENTHREAD_MTD
43 void set_poll_period(uint32_t poll_period) { this->poll_period_ = poll_period; }
44#endif
45 void set_output_power(int8_t output_power) { this->output_power_ = output_power; }
46
47 protected:
48 std::optional<otIp6Address> get_omr_address_(InstanceLock &lock);
49 static void on_state_changed_(otChangedFlags flags, void *context);
50 otInstance *get_openthread_instance_();
51 int openthread_stop_();
52 std::function<void()> factory_reset_external_callback_;
53#if CONFIG_OPENTHREAD_MTD
55#endif
56 std::optional<int8_t> output_power_{};
57 std::atomic<bool> lock_initialized_{false};
58 bool teardown_started_{false};
59 bool teardown_complete_{false};
60 bool connected_{false};
61
62 private:
63 // Stores a pointer to a string literal (static storage duration).
64 // ONLY set from Python-generated code with string literals - never dynamic strings.
65 const char *use_address_{""};
66};
67
68extern OpenThreadComponent *global_openthread_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
69
71 public:
73 // This has to run after the mdns component or else no services are available to advertise
74 float get_setup_priority() const override { return this->mdns_->get_setup_priority() - 1.0; }
75 void setup() override;
76 static void srp_callback(otError err, const otSrpClientHostInfo *host_info, const otSrpClientService *services,
77 const otSrpClientService *removed_services, void *context);
78 static void srp_start_callback(const otSockAddr *server_socket_address, void *context);
79 static void srp_factory_reset_callback(otError err, const otSrpClientHostInfo *host_info,
80 const otSrpClientService *services, const otSrpClientService *removed_services,
81 void *context);
82
83 protected:
85 std::vector<std::unique_ptr<uint8_t[]>> memory_pool_;
86 void *pool_alloc_(size_t size);
87};
88
90 public:
91 static std::optional<InstanceLock> try_acquire(int delay);
92 static InstanceLock acquire();
94
95 // Returns the global openthread instance guarded by this lock
96 otInstance *get_instance();
97
98 private:
99 // Use a private constructor in order to force the handling
100 // of acquisition failure
101 InstanceLock() {}
102};
103
104} // namespace esphome::openthread
105#endif
float get_setup_priority() const override
static std::optional< InstanceLock > try_acquire(int delay)
void set_poll_period(uint32_t poll_period)
Definition openthread.h:43
void set_use_address(const char *use_address)
std::optional< otIp6Address > get_omr_address_(InstanceLock &lock)
std::optional< int8_t > output_power_
Definition openthread.h:56
std::function< void()> factory_reset_external_callback_
Definition openthread.h:52
void set_output_power(int8_t output_power)
Definition openthread.h:45
static void on_state_changed_(otChangedFlags flags, void *context)
bool is_lock_initialized() const
Returns true once esp_openthread_init() has completed and the OT lock is usable.
Definition openthread.h:33
std::optional< otIp6Address > get_omr_address()
float get_setup_priority() const override
Definition openthread.h:29
void on_factory_reset(std::function< void()> callback)
std::vector< std::unique_ptr< uint8_t[]> > memory_pool_
Definition openthread.h:85
static void srp_start_callback(const otSockAddr *server_socket_address, void *context)
void set_mdns(esphome::mdns::MDNSComponent *mdns)
static void srp_factory_reset_callback(otError err, const otSrpClientHostInfo *host_info, const otSrpClientService *services, const otSrpClientService *removed_services, void *context)
float get_setup_priority() const override
Definition openthread.h:74
static void srp_callback(otError err, const otSrpClientHostInfo *host_info, const otSrpClientService *services, const otSrpClientService *removed_services, void *context)
esphome::mdns::MDNSComponent * mdns_
Definition openthread.h:84
uint16_t flags
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:187
OpenThreadComponent * global_openthread_component
constexpr float WIFI
Definition component.h:36
size_t size
Definition helpers.h:929
void HOT delay(uint32_t ms)
Definition core.cpp:28
static void uint32_t