esphome-api-client 0.5.0
Modern C++17 client for the ESPHome native API (plaintext + Noise)
Loading...
Searching...
No Matches
connection.hpp
Go to the documentation of this file.
1#pragma once
2
7
15
16#include <cstdint>
17#include <exception>
18#include <functional>
19#include <memory>
20#include <string>
21#include <system_error>
22#include <vector>
23
24namespace esphome::api {
25
30 std::string server_info;
31 std::string name;
32};
33
41public:
42 using MessageHandler = std::function<void(const ProtoMessage&)>;
43 using RawHandler = std::function<void(std::uint32_t msg_type, ByteView payload)>;
44 using StateHandler = std::function<void(ConnectionState)>;
45 using ErrorHandler = std::function<void(std::error_code, const std::string&)>;
46 using ConnectHandler = std::function<void(std::error_code)>;
47
49 std::unique_ptr<Transport> transport,
50 std::unique_ptr<FrameHelper> frame_helper,
51 ConnectionOptions options);
53
54 Connection(const Connection&) = delete;
55 Connection& operator=(const Connection&) = delete;
56
59 void start(const std::string& host, std::uint16_t port, ConnectHandler on_done);
60
62 void send(const ProtoMessage& msg) const;
63
65 void send_raw(std::uint32_t msg_type, ByteView payload) const;
66
68 void on(std::uint32_t msg_type, MessageHandler handler);
69
72 void on_any(MessageHandler handler);
73
76 void on_raw(RawHandler handler);
77
79 void on_state(StateHandler handler);
80
82 void on_error(ErrorHandler handler);
83
85 void disconnect();
86
87 [[nodiscard]] ConnectionState state() const noexcept {
88 return state_;
89 }
90 [[nodiscard]] const ServerHello& server_hello() const noexcept {
91 return hello_;
92 }
93 [[nodiscard]] bool is_connected() const noexcept {
94 return state_ == ConnectionState::Connected;
95 }
96
101 [[nodiscard]] std::exception_ptr last_error() const noexcept {
102 return last_error_;
103 }
104
105private:
106 void set_state(ConnectionState state);
107 void on_connected_transport(std::error_code ec);
108 void on_bytes(std::error_code ec, ByteView data);
109 void drain_frames();
110 void handle_frame(std::uint32_t msg_type, ByteView payload);
111 void handle_internal(std::uint32_t msg_type, const ProtoMessage& msg);
112 void send_hello() const;
113 void finish_connect(std::error_code ec);
114 void fail(std::error_code ec, const std::string& what);
115
116 void start_keepalive();
117 void stop_keepalive();
118 void send_ping();
119
120 Executor& executor_;
121 std::unique_ptr<Transport> transport_;
122 std::unique_ptr<FrameHelper> frame_helper_;
123 ConnectionOptions options_;
124
126 ServerHello hello_;
127
128 std::vector<MessageHandler> handlers_; // indexed by message id
129 MessageHandler any_handler_;
130 RawHandler raw_handler_;
131 StateHandler state_handler_;
132 ErrorHandler error_handler_;
133 ConnectHandler connect_handler_;
134 std::exception_ptr last_error_;
135
136 TimerId connect_timer_ = invalid_timer;
137 TimerId ping_timer_ = invalid_timer;
138 TimerId pong_timer_ = invalid_timer;
139 bool awaiting_pong_ = false;
140};
141
142} // namespace esphome::api
Byte buffer / view aliases and protobuf base-128 varint codec.
Non-owning view over a contiguous run of bytes (a C++17 stand-in for std::span<const std::uint8_t>).
Definition bytes.hpp:17
Drives one logical session with an ESPHome device: TCP connect, optional Noise handshake (via the inj...
Definition connection.hpp:40
Connection(Executor &executor, std::unique_ptr< Transport > transport, std::unique_ptr< FrameHelper > frame_helper, ConnectionOptions options)
std::function< void(std::error_code)> ConnectHandler
Definition connection.hpp:46
std::function< void(std::error_code, const std::string &)> ErrorHandler
Definition connection.hpp:45
std::function< void(std::uint32_t msg_type, ByteView payload)> RawHandler
Definition connection.hpp:43
std::function< void(ConnectionState)> StateHandler
Definition connection.hpp:44
void send(const ProtoMessage &msg) const
Serialize and send a typed protobuf message.
bool is_connected() const noexcept
Definition connection.hpp:93
ConnectionState state() const noexcept
Definition connection.hpp:87
void start(const std::string &host, std::uint16_t port, ConnectHandler on_done)
Connect to host:port, perform the handshake and signal completion via on_done exactly once (empty err...
void on(std::uint32_t msg_type, MessageHandler handler)
Install a handler for one message id (replaces any previous handler).
Connection & operator=(const Connection &)=delete
void on_error(ErrorHandler handler)
Observe terminal errors (read failure, protocol violation, timeout).
Connection(const Connection &)=delete
void on_raw(RawHandler handler)
Install a handler invoked with the raw bytes of every inbound frame (even messages the registry canno...
const ServerHello & server_hello() const noexcept
Definition connection.hpp:90
void disconnect()
Begin a graceful disconnect (DisconnectRequest, then close).
void send_raw(std::uint32_t msg_type, ByteView payload) const
Send a pre-serialized payload under an explicit message type id.
std::exception_ptr last_error() const noexcept
The exception that caused the most recent failure, if it carried richer type/message information than...
Definition connection.hpp:101
std::function< void(const ProtoMessage &)> MessageHandler
Definition connection.hpp:42
void on_state(StateHandler handler)
Observe state transitions.
void on_any(MessageHandler handler)
Install a catch-all handler invoked for every decoded inbound message, after any id-specific handler.
Minimal executor: deferred execution plus one-shot timers.
Definition executor.hpp:20
Base class every generated message derives from.
Definition proto_message.hpp:23
Tunables for a Connection's handshake and keepalive behaviour.
Connection state-machine states.
Executor + timer abstraction (asio-free).
Abstract frame codec: turns a byte stream into ESPHome message frames and back, independent of the un...
Definition bytes.hpp:10
std::uint64_t TimerId
Opaque handle to a scheduled timer; invalid_timer means "no timer".
Definition executor.hpp:14
ConnectionState
Lifecycle of a Connection.
Definition connection_state.hpp:11
@ Connected
Handshake complete; messages flow freely.
@ Disconnected
Idle, never connected or fully torn down.
constexpr TimerId invalid_timer
Definition executor.hpp:15
Lightweight base class for every generated ESPHome API message.
Knobs controlling a single Connection. Defaults mirror aioesphomeapi.
Definition connection_options.hpp:12
Information returned by the device in its HelloResponse.
Definition connection.hpp:27
int api_version_minor
Definition connection.hpp:29
std::string name
Definition connection.hpp:31
std::string server_info
Definition connection.hpp:30
int api_version_major
Definition connection.hpp:28
Transport abstraction (asio-free) the connection drives.