mirror of
https://github.com/esphome/esphome.git
synced 2025-10-27 05:03:48 +00:00
Co-authored-by: J. Nick Koston <nick@home-assistant.io> Co-authored-by: J. Nick Koston <nick@koston.org> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
86 lines
2.8 KiB
C++
86 lines
2.8 KiB
C++
#pragma once
|
|
#include "esphome/core/defines.h"
|
|
#ifdef USE_OPENTHREAD
|
|
|
|
#include "esphome/components/mdns/mdns_component.h"
|
|
#include "esphome/components/network/ip_address.h"
|
|
#include "esphome/core/component.h"
|
|
|
|
#include <openthread/srp_client.h>
|
|
#include <openthread/srp_client_buffers.h>
|
|
#include <openthread/thread.h>
|
|
|
|
#include <optional>
|
|
#include <vector>
|
|
|
|
namespace esphome {
|
|
namespace openthread {
|
|
|
|
class InstanceLock;
|
|
|
|
class OpenThreadComponent : public Component {
|
|
public:
|
|
OpenThreadComponent();
|
|
~OpenThreadComponent();
|
|
void setup() override;
|
|
bool teardown() override;
|
|
float get_setup_priority() const override { return setup_priority::WIFI; }
|
|
|
|
bool is_connected();
|
|
network::IPAddresses get_ip_addresses();
|
|
std::optional<otIp6Address> get_omr_address();
|
|
void ot_main();
|
|
void on_factory_reset(std::function<void()> callback);
|
|
void defer_factory_reset_external_callback();
|
|
|
|
const std::string &get_use_address() const;
|
|
void set_use_address(const std::string &use_address);
|
|
|
|
protected:
|
|
std::optional<otIp6Address> get_omr_address_(InstanceLock &lock);
|
|
bool teardown_started_{false};
|
|
bool teardown_complete_{false};
|
|
std::function<void()> factory_reset_external_callback_;
|
|
std::string use_address_;
|
|
};
|
|
|
|
extern OpenThreadComponent *global_openthread_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
|
|
|
class OpenThreadSrpComponent : public Component {
|
|
public:
|
|
void set_mdns(esphome::mdns::MDNSComponent *mdns);
|
|
// This has to run after the mdns component or else no services are available to advertise
|
|
float get_setup_priority() const override { return this->mdns_->get_setup_priority() - 1.0; }
|
|
void setup() override;
|
|
static void srp_callback(otError err, const otSrpClientHostInfo *host_info, const otSrpClientService *services,
|
|
const otSrpClientService *removed_services, void *context);
|
|
static void srp_start_callback(const otSockAddr *server_socket_address, void *context);
|
|
static void srp_factory_reset_callback(otError err, const otSrpClientHostInfo *host_info,
|
|
const otSrpClientService *services, const otSrpClientService *removed_services,
|
|
void *context);
|
|
|
|
protected:
|
|
esphome::mdns::MDNSComponent *mdns_{nullptr};
|
|
std::vector<std::unique_ptr<uint8_t[]>> memory_pool_;
|
|
void *pool_alloc_(size_t size);
|
|
};
|
|
|
|
class InstanceLock {
|
|
public:
|
|
static std::optional<InstanceLock> try_acquire(int delay);
|
|
static InstanceLock acquire();
|
|
~InstanceLock();
|
|
|
|
// Returns the global openthread instance guarded by this lock
|
|
otInstance *get_instance();
|
|
|
|
private:
|
|
// Use a private constructor in order to force thehandling
|
|
// of acquisition failure
|
|
InstanceLock() {}
|
|
};
|
|
|
|
} // namespace openthread
|
|
} // namespace esphome
|
|
#endif
|