mirror of
https://github.com/esphome/esphome.git
synced 2024-11-27 09:18:00 +01:00
Removed WiFiClientSecure for libretiny
This commit is contained in:
parent
d7652bb18a
commit
ace6228812
3 changed files with 191 additions and 34 deletions
|
@ -1,7 +1,10 @@
|
|||
#include <string>
|
||||
|
||||
#include "mqtt_backend_esp8266.h"
|
||||
|
||||
#ifdef USE_MQTT
|
||||
#ifdef USE_ESP8266
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome {
|
||||
|
@ -102,3 +105,6 @@ void MQTTBackendESP8266::loop() {
|
|||
|
||||
} // namespace mqtt
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
||||
#endif
|
91
esphome/components/mqtt/mqtt_backend_libretiny.cpp
Normal file
91
esphome/components/mqtt/mqtt_backend_libretiny.cpp
Normal file
|
@ -0,0 +1,91 @@
|
|||
#include "mqtt_backend_libretiny.h"
|
||||
|
||||
#ifdef USE_MQTT
|
||||
#ifdef USE_LIBRETINY
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace mqtt {
|
||||
|
||||
static const char *const TAG = "mqtt-backend-libretiny";
|
||||
|
||||
void MQTTBackendLibreTiny::on_mqtt_message_wrapper_(MQTTClient *client, char topic[], char bytes[], int length) {
|
||||
static_cast<MQTTBackendLibreTiny *>(client->ref)->on_mqtt_message_(client, topic, bytes, length);
|
||||
}
|
||||
|
||||
void MQTTBackendLibreTiny::on_mqtt_message_(MQTTClient *client, char topic[], char bytes[], int length) {
|
||||
this->on_message_.call(topic, bytes, length, 0, length);
|
||||
}
|
||||
|
||||
void MQTTBackendLibreTiny::initialize_() {
|
||||
this->mqtt_client_.ref = this;
|
||||
mqtt_client_.onMessageAdvanced(MQTTBackendLibreTiny::on_mqtt_message_wrapper_);
|
||||
this->is_initalized_ = true;
|
||||
}
|
||||
|
||||
void MQTTBackendLibreTiny::handleErrors_() {
|
||||
lwmqtt_err_t error = this->mqtt_client_.lastError();
|
||||
lwmqtt_return_code_t return_code = this->mqtt_client_.returnCode();
|
||||
if (error != LWMQTT_SUCCESS) {
|
||||
ESP_LOGD(TAG, "Error: %d, returnCode: %d", error, return_code);
|
||||
|
||||
MQTTClientDisconnectReason reason = MQTTClientDisconnectReason::TCP_DISCONNECTED;
|
||||
|
||||
if (return_code != LWMQTT_CONNECTION_ACCEPTED) {
|
||||
switch (return_code) {
|
||||
case LWMQTT_CONNECTION_ACCEPTED:
|
||||
assert(false);
|
||||
break;
|
||||
case LWMQTT_UNACCEPTABLE_PROTOCOL:
|
||||
reason = MQTTClientDisconnectReason::MQTT_UNACCEPTABLE_PROTOCOL_VERSION;
|
||||
break;
|
||||
case LWMQTT_IDENTIFIER_REJECTED:
|
||||
reason = MQTTClientDisconnectReason::MQTT_IDENTIFIER_REJECTED;
|
||||
break;
|
||||
case LWMQTT_SERVER_UNAVAILABLE:
|
||||
reason = MQTTClientDisconnectReason::MQTT_SERVER_UNAVAILABLE;
|
||||
break;
|
||||
case LWMQTT_BAD_USERNAME_OR_PASSWORD:
|
||||
reason = MQTTClientDisconnectReason::MQTT_MALFORMED_CREDENTIALS;
|
||||
break;
|
||||
case LWMQTT_NOT_AUTHORIZED:
|
||||
reason = MQTTClientDisconnectReason::MQTT_NOT_AUTHORIZED;
|
||||
break;
|
||||
case LWMQTT_UNKNOWN_RETURN_CODE:
|
||||
reason = MQTTClientDisconnectReason::TCP_DISCONNECTED;
|
||||
break;
|
||||
}
|
||||
}
|
||||
this->on_disconnect_.call(reason);
|
||||
}
|
||||
}
|
||||
|
||||
void MQTTBackendLibreTiny::connect() {
|
||||
if (!this->is_initalized_) {
|
||||
this->initialize_();
|
||||
}
|
||||
this->mqtt_client_.begin(this->host_.c_str(), this->port_, this->wifi_client_);
|
||||
this->mqtt_client_.connect(this->client_id_.c_str(), this->username_.c_str(), this->password_.c_str());
|
||||
this->handleErrors_();
|
||||
}
|
||||
|
||||
void MQTTBackendLibreTiny::loop() {
|
||||
this->mqtt_client_.loop();
|
||||
if (!this->is_connected_ && this->mqtt_client_.connected()) {
|
||||
this->is_connected_ = true;
|
||||
this->on_connect_.call(this->clean_session_);
|
||||
}
|
||||
if (this->is_connected_ && !this->mqtt_client_.connected()) {
|
||||
this->is_connected_ = false;
|
||||
this->on_disconnect_.call(MQTTClientDisconnectReason::TCP_DISCONNECTED);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace mqtt
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
||||
#endif
|
|
@ -4,67 +4,127 @@
|
|||
#ifdef USE_MQTT
|
||||
#ifdef USE_LIBRETINY
|
||||
|
||||
#include <AsyncMqttClient.h>
|
||||
#include "mqtt_backend.h"
|
||||
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#include <WiFiClient.h>
|
||||
#include <MQTT.h>
|
||||
|
||||
namespace esphome {
|
||||
namespace mqtt {
|
||||
|
||||
class MQTTBackendLibreTiny final : public MQTTBackend {
|
||||
public:
|
||||
void set_keep_alive(uint16_t keep_alive) final { mqtt_client_.setKeepAlive(keep_alive); }
|
||||
void set_client_id(const char *client_id) final { mqtt_client_.setClientId(client_id); }
|
||||
void set_clean_session(bool clean_session) final { mqtt_client_.setCleanSession(clean_session); }
|
||||
void set_keep_alive(uint16_t keep_alive) final { this->mqtt_client_.setKeepAlive(keep_alive); }
|
||||
void set_client_id(const char *client_id) final { this->client_id_ = client_id; }
|
||||
void set_clean_session(bool clean_session) final {
|
||||
this->clean_session_ = clean_session;
|
||||
this->mqtt_client_.setCleanSession(clean_session);
|
||||
}
|
||||
void set_credentials(const char *username, const char *password) final {
|
||||
mqtt_client_.setCredentials(username, password);
|
||||
if (username)
|
||||
this->username_ = username;
|
||||
if (password)
|
||||
this->password_ = password;
|
||||
}
|
||||
void set_will(const char *topic, uint8_t qos, bool retain, const char *payload) final {
|
||||
mqtt_client_.setWill(topic, qos, retain, payload);
|
||||
if (topic)
|
||||
this->lwt_topic_ = topic;
|
||||
if (payload)
|
||||
this->lwt_message_ = payload;
|
||||
this->mqtt_client_.setWill(this->lwt_topic_.c_str(), this->lwt_message_.c_str(), retain, qos);
|
||||
}
|
||||
void set_server(network::IPAddress ip, uint16_t port) final {
|
||||
this->host_ = ip.str();
|
||||
this->port_ = port;
|
||||
this->mqtt_client_.setHost(ip, port);
|
||||
}
|
||||
void set_server(const char *host, uint16_t port) final {
|
||||
this->host_ = host;
|
||||
this->port_ = port;
|
||||
this->mqtt_client_.setHost(this->host_.c_str(), port);
|
||||
}
|
||||
void set_server(network::IPAddress ip, uint16_t port) final { mqtt_client_.setServer(IPAddress(ip), port); }
|
||||
void set_server(const char *host, uint16_t port) final { mqtt_client_.setServer(host, port); }
|
||||
#if ASYNC_TCP_SSL_ENABLED
|
||||
void set_secure(bool secure) { mqtt_client.setSecure(secure); }
|
||||
void add_server_fingerprint(const uint8_t *fingerprint) { mqtt_client.addServerFingerprint(fingerprint); }
|
||||
#endif
|
||||
|
||||
void set_on_connect(std::function<on_connect_callback_t> &&callback) final {
|
||||
this->mqtt_client_.onConnect(std::move(callback));
|
||||
this->on_connect_.add(std::move(callback));
|
||||
}
|
||||
void set_on_disconnect(std::function<on_disconnect_callback_t> &&callback) final {
|
||||
auto async_callback = [callback](AsyncMqttClientDisconnectReason reason) {
|
||||
// int based enum so casting isn't a problem
|
||||
callback(static_cast<MQTTClientDisconnectReason>(reason));
|
||||
};
|
||||
this->mqtt_client_.onDisconnect(std::move(async_callback));
|
||||
this->on_disconnect_.add(std::move(callback));
|
||||
}
|
||||
void set_on_subscribe(std::function<on_subscribe_callback_t> &&callback) final {
|
||||
this->mqtt_client_.onSubscribe(std::move(callback));
|
||||
this->on_subscribe_.add(std::move(callback));
|
||||
}
|
||||
void set_on_unsubscribe(std::function<on_unsubscribe_callback_t> &&callback) final {
|
||||
this->mqtt_client_.onUnsubscribe(std::move(callback));
|
||||
this->on_unsubscribe_.add(std::move(callback));
|
||||
}
|
||||
void set_on_message(std::function<on_message_callback_t> &&callback) final {
|
||||
auto async_callback = [callback](const char *topic, const char *payload,
|
||||
AsyncMqttClientMessageProperties async_properties, size_t len, size_t index,
|
||||
size_t total) { callback(topic, payload, len, index, total); };
|
||||
mqtt_client_.onMessage(std::move(async_callback));
|
||||
this->on_message_.add(std::move(callback));
|
||||
}
|
||||
void set_on_publish(std::function<on_publish_user_callback_t> &&callback) final {
|
||||
this->mqtt_client_.onPublish(std::move(callback));
|
||||
this->on_publish_.add(std::move(callback));
|
||||
}
|
||||
|
||||
bool connected() const final { return mqtt_client_.connected(); }
|
||||
void connect() final { mqtt_client_.connect(); }
|
||||
void disconnect() final { mqtt_client_.disconnect(true); }
|
||||
bool subscribe(const char *topic, uint8_t qos) final { return mqtt_client_.subscribe(topic, qos) != 0; }
|
||||
bool unsubscribe(const char *topic) final { return mqtt_client_.unsubscribe(topic) != 0; }
|
||||
bool connected() const final { return this->is_connected_; }
|
||||
void connect() final;
|
||||
void disconnect() final {
|
||||
if (this->is_initalized_)
|
||||
this->mqtt_client_.disconnect();
|
||||
}
|
||||
bool subscribe(const char *topic, uint8_t qos) final {
|
||||
bool res = this->mqtt_client_.subscribe(topic, qos);
|
||||
this->on_subscribe_.call(this->mqtt_client_.lastPacketID(), qos);
|
||||
return res;
|
||||
}
|
||||
bool unsubscribe(const char *topic) final {
|
||||
bool res = this->mqtt_client_.unsubscribe(topic);
|
||||
this->on_unsubscribe_.call(this->mqtt_client_.lastPacketID());
|
||||
return res;
|
||||
}
|
||||
bool publish(const char *topic, const char *payload, size_t length, uint8_t qos, bool retain) final {
|
||||
return mqtt_client_.publish(topic, qos, retain, payload, length, false, 0) != 0;
|
||||
bool res = this->mqtt_client_.publish(topic, payload, length, retain, qos);
|
||||
this->on_publish_.call(this->mqtt_client_.lastPacketID());
|
||||
return res;
|
||||
}
|
||||
using MQTTBackend::publish;
|
||||
|
||||
void loop() final;
|
||||
|
||||
void set_ca_certificate(const std::string &cert) { this->ca_certificate_str_ = cert; }
|
||||
void set_skip_cert_cn_check(bool skip_check) { this->skip_cert_cn_check_ = skip_check; }
|
||||
void set_ssl_fingerprint(const std::array<uint8_t, 20> &fingerprint) { this->ssl_fingerprint_ = fingerprint; };
|
||||
|
||||
protected:
|
||||
AsyncMqttClient mqtt_client_;
|
||||
void initialize_();
|
||||
void handleErrors_();
|
||||
static void on_mqtt_message_wrapper_(MQTTClient *client, char topic[], char bytes[], int length);
|
||||
void on_mqtt_message_(MQTTClient *client, char topic[], char bytes[], int length);
|
||||
|
||||
WiFiClient wifi_client_;
|
||||
MQTTClient mqtt_client_;
|
||||
|
||||
bool is_connected_{false};
|
||||
bool is_initalized_{false};
|
||||
|
||||
std::string host_;
|
||||
uint16_t port_;
|
||||
bool clean_session_{true};
|
||||
std::string username_;
|
||||
std::string password_;
|
||||
std::string lwt_topic_;
|
||||
std::string lwt_message_;
|
||||
std::string client_id_;
|
||||
optional<std::string> ca_certificate_str_;
|
||||
optional<std::array<uint8_t, 20>> ssl_fingerprint_;
|
||||
bool skip_cert_cn_check_{false};
|
||||
|
||||
// callbacks
|
||||
CallbackManager<on_connect_callback_t> on_connect_;
|
||||
CallbackManager<on_disconnect_callback_t> on_disconnect_;
|
||||
CallbackManager<on_subscribe_callback_t> on_subscribe_;
|
||||
CallbackManager<on_unsubscribe_callback_t> on_unsubscribe_;
|
||||
CallbackManager<on_message_callback_t> on_message_;
|
||||
CallbackManager<on_publish_user_callback_t> on_publish_;
|
||||
};
|
||||
|
||||
} // namespace mqtt
|
||||
|
|
Loading…
Reference in a new issue