API: Expect a name for connections (#2533)

This commit is contained in:
Otto Winter 2022-01-20 12:03:32 +01:00 committed by GitHub
parent 1f8a1f0046
commit 62f9736b1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 3 deletions

View file

@ -95,6 +95,9 @@ message HelloResponse {
// and only exists for debugging/logging purposes. // and only exists for debugging/logging purposes.
// For example "ESPHome v1.10.0 on ESP8266" // For example "ESPHome v1.10.0 on ESP8266"
string server_info = 3; string server_info = 3;
// The name of the server (App.get_name())
string name = 4;
} }
// Message sent at the beginning of each connection to authenticate the client // Message sent at the beginning of each connection to authenticate the client

View file

@ -766,6 +766,8 @@ HelloResponse APIConnection::hello(const HelloRequest &msg) {
resp.api_version_major = 1; resp.api_version_major = 1;
resp.api_version_minor = 6; resp.api_version_minor = 6;
resp.server_info = App.get_name() + " (esphome v" ESPHOME_VERSION ")"; resp.server_info = App.get_name() + " (esphome v" ESPHOME_VERSION ")";
resp.name = App.get_name();
this->connection_state_ = ConnectionState::CONNECTED; this->connection_state_ = ConnectionState::CONNECTED;
return resp; return resp;
} }

View file

@ -3,6 +3,7 @@
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "esphome/core/hal.h" #include "esphome/core/hal.h"
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include "esphome/core/application.h"
#include "proto.h" #include "proto.h"
#include <cstring> #include <cstring>
@ -302,9 +303,16 @@ APIError APINoiseFrameHelper::state_action_() {
} }
if (state_ == State::SERVER_HELLO) { if (state_ == State::SERVER_HELLO) {
// send server hello // send server hello
uint8_t msg[1]; std::vector<uint8_t> msg;
msg[0] = 0x01; // chosen proto // chosen proto
aerr = write_frame_(msg, 1); msg.push_back(0x01);
// node name, terminated by null byte
const std::string &name = App.get_name();
const uint8_t *name_ptr = reinterpret_cast<const uint8_t *>(name.c_str());
msg.insert(msg.end(), name_ptr, name_ptr + name.size() + 1);
aerr = write_frame_(msg.data(), msg.size());
if (aerr != APIError::OK) if (aerr != APIError::OK)
return aerr; return aerr;

View file

@ -319,6 +319,10 @@ bool HelloResponse::decode_length(uint32_t field_id, ProtoLengthDelimited value)
this->server_info = value.as_string(); this->server_info = value.as_string();
return true; return true;
} }
case 4: {
this->name = value.as_string();
return true;
}
default: default:
return false; return false;
} }
@ -327,6 +331,7 @@ void HelloResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_uint32(1, this->api_version_major); buffer.encode_uint32(1, this->api_version_major);
buffer.encode_uint32(2, this->api_version_minor); buffer.encode_uint32(2, this->api_version_minor);
buffer.encode_string(3, this->server_info); buffer.encode_string(3, this->server_info);
buffer.encode_string(4, this->name);
} }
#ifdef HAS_PROTO_MESSAGE_DUMP #ifdef HAS_PROTO_MESSAGE_DUMP
void HelloResponse::dump_to(std::string &out) const { void HelloResponse::dump_to(std::string &out) const {
@ -345,6 +350,10 @@ void HelloResponse::dump_to(std::string &out) const {
out.append(" server_info: "); out.append(" server_info: ");
out.append("'").append(this->server_info).append("'"); out.append("'").append(this->server_info).append("'");
out.append("\n"); out.append("\n");
out.append(" name: ");
out.append("'").append(this->name).append("'");
out.append("\n");
out.append("}"); out.append("}");
} }
#endif #endif

View file

@ -147,6 +147,7 @@ class HelloResponse : public ProtoMessage {
uint32_t api_version_major{0}; uint32_t api_version_major{0};
uint32_t api_version_minor{0}; uint32_t api_version_minor{0};
std::string server_info{}; std::string server_info{};
std::string name{};
void encode(ProtoWriteBuffer buffer) const override; void encode(ProtoWriteBuffer buffer) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP #ifdef HAS_PROTO_MESSAGE_DUMP
void dump_to(std::string &out) const override; void dump_to(std::string &out) const override;