Added mqtt_client

This commit is contained in:
2021-04-26 13:56:16 +02:00
parent 07837be0d2
commit 1cc4285a6b
4 changed files with 98 additions and 23 deletions

View File

@ -11,6 +11,7 @@ set(headers
src/wrappers/counting_semaphore.h
src/wrappers/event_group.h
src/wrappers/http_client.h
src/wrappers/mqtt_client.h
src/wrappers/mutex_semaphore.h
src/wrappers/queue.h
src/wrappers/recursive_mutex_semaphore.h

View File

@ -2,6 +2,7 @@
// system includes
#include <utility>
#include <string_view>
// espressif includes
#include <esp_http_client.h>
@ -27,19 +28,27 @@ public:
operator bool() const { return handle != NULL; }
esp_err_t perform() { return esp_http_client_perform(handle); }
esp_err_t set_url(const char *url) { return esp_http_client_set_url(handle, url); }
esp_err_t set_post_field(const char *data, int len) { return esp_http_client_set_post_field(handle, data, len); }
//esp_err_t set_url(const char *url) { return esp_http_client_set_url(handle, url); }
esp_err_t set_url(std::string_view url) { return esp_http_client_set_url(handle, url.data()); }
//esp_err_t set_post_field(const char *data, int len) { return esp_http_client_set_post_field(handle, data, len); }
esp_err_t set_post_field(std::string_view buf) { return esp_http_client_set_post_field(handle, buf.data(), buf.size()); }
int get_post_field(char **data) { return esp_http_client_get_post_field(handle, data); }
esp_err_t set_header(const char *key, const char *value) { return esp_http_client_set_header(handle, key, value); }
esp_err_t get_header(const char *key, char **value) { return esp_http_client_get_header(handle, key, value); }
//esp_err_t set_header(const char *key, const char *value) { return esp_http_client_set_header(handle, key, value); }
esp_err_t set_header(std::string_view key, std::string_view value) { return esp_http_client_set_header(handle, key.data(), value.data()); }
//esp_err_t get_header(const char *key, char **value) { return esp_http_client_get_header(handle, key, value); }
esp_err_t get_header(std::string_view key, char **value) { return esp_http_client_get_header(handle, key.data(), value); }
esp_err_t get_username(char **value) { return esp_http_client_get_username(handle, value); }
esp_err_t set_username(const char *username) { return esp_http_client_set_username(handle, username); }
//esp_err_t set_username(const char *username) { return esp_http_client_set_username(handle, username); }
esp_err_t set_username(std::string_view username) { return esp_http_client_set_username(handle, username.data()); }
esp_err_t get_password(char **value) { return esp_http_client_get_password(handle, value); }
esp_err_t set_password(char *password) { return esp_http_client_set_password(handle, password); }
//esp_err_t set_password(const char *password) { return esp_http_client_set_password(handle, password); }
esp_err_t set_password(std::string_view password) { return esp_http_client_set_password(handle, password.data()); }
esp_err_t set_method(esp_http_client_method_t method) { return esp_http_client_set_method(handle, method); }
esp_err_t delete_header(const char *key) { return esp_http_client_delete_header(handle, key); }
//esp_err_t delete_header(const char *key) { return esp_http_client_delete_header(handle, key); }
esp_err_t delete_header(std::string_view key) { return esp_http_client_delete_header(handle, key.data()); }
esp_err_t open(int write_len) { return esp_http_client_open(handle, write_len); }
int write(const char *buffer, int len) { return esp_http_client_write(handle, buffer, len); }
//int write(const char *buffer, int len) { return esp_http_client_write(handle, buffer, len); }
int write(std::string_view buf) { return esp_http_client_write(handle, buf.data(), buf.size()); }
int fetch_headers() { return esp_http_client_fetch_headers(handle); }
bool is_chunked_response() const { return esp_http_client_is_chunked_response(handle); }
int read(char *buffer, int len) { return esp_http_client_read(handle, buffer, len); }

View File

@ -0,0 +1,51 @@
#pragma once
// system includes
#include <utility>
#include <string_view>
// esp-idf includes
#include <mqtt_client.h>
namespace espcpputils {
class mqtt_client
{
public:
mqtt_client() : handle{NULL} {}
mqtt_client(const esp_mqtt_client_config_t *config) : handle{esp_mqtt_client_init(config)} {}
mqtt_client(const mqtt_client &) = delete;
mqtt_client(mqtt_client &&other) : handle{std::move(other.handle)} { other.handle = NULL; }
mqtt_client &operator=(const mqtt_client &) = delete;
mqtt_client &operator=(mqtt_client &&other) { if (handle) esp_mqtt_client_destroy(handle); handle = std::move(other.handle); other.handle = NULL; return *this; }
~mqtt_client() { if (handle) esp_mqtt_client_destroy(handle); }
operator bool() const { return handle != NULL; }
//esp_err_t set_uri (const char *uri) { return esp_mqtt_client_set_uri (handle, uri); }
esp_err_t set_uri (std::string_view uri) { return esp_mqtt_client_set_uri (handle, uri.data()); }
esp_err_t start () { return esp_mqtt_client_start (handle); }
esp_err_t reconnect () { return esp_mqtt_client_reconnect (handle); }
esp_err_t disconnect () { return esp_mqtt_client_disconnect (handle); }
esp_err_t stop () { return esp_mqtt_client_stop (handle); }
//int subscribe (const char *topic, int qos) { return esp_mqtt_client_subscribe (handle, topic, qos); }
int subscribe (std::string_view topic, int qos) { return esp_mqtt_client_subscribe (handle, topic.data(), qos); }
//int unsubscribe (const char *topic) { return esp_mqtt_client_unsubscribe (handle, topic); }
int unsubscribe (std::string_view topic) { return esp_mqtt_client_unsubscribe (handle, topic.data()); }
//int publish (const char *topic, const char *data, int len, int qos, int retain) { return esp_mqtt_client_publish (handle, topic, data, len, qos, retain); }
int publish (std::string_view topic, std::string_view buf, int qos, int retain) { return esp_mqtt_client_publish (handle, topic.data(), buf.data(), buf.size(), qos, retain); }
//int enqueue (const char *topic, const char *data, int len, int qos, int retain, bool store) { return esp_mqtt_client_enqueue (handle, topic, data, len, qos, retain, store); }
int enqueue (std::string_view topic, std::string_view buf, int qos, int retain, bool store) { return esp_mqtt_client_enqueue (handle, topic.data(), buf.data(), buf.size(), qos, retain, store); }
esp_err_t set_config (const esp_mqtt_client_config_t *config) { return esp_mqtt_set_config (handle, config); }
esp_err_t register_event (esp_mqtt_event_id_t event, esp_event_handler_t event_handler, void* event_handler_arg) { return esp_mqtt_client_register_event (handle, event, event_handler, event_handler_arg); }
int get_outbox_size() const { return esp_mqtt_client_get_outbox_size(handle); }
private:
esp_mqtt_client_handle_t handle;
};
}

View File

@ -1,36 +1,50 @@
#pragma once
// system includes
#include <utility>
#include <string_view>
// espressif includes
#include <esp_websocket_client.h>
// local includes
#include "cppmacros.h"
namespace espcpputils {
class websocket_client
{
CPP_DISABLE_COPY_MOVE(websocket_client)
public:
websocket_client(const esp_websocket_client_config_t &config) :
handle{esp_websocket_client_init(&config)}
{
}
websocket_client() : handle{NULL} {}
websocket_client(const esp_websocket_client_config_t *config) : handle{esp_websocket_client_init(config)} {}
websocket_client(const websocket_client &) = delete;
websocket_client(websocket_client &&other) : handle{std::move(other.handle)} { other.handle = NULL; }
websocket_client &operator=(const websocket_client &) = delete;
websocket_client &operator=(websocket_client &&other) { if (handle) esp_websocket_client_destroy(handle); handle = std::move(other.handle); other.handle = NULL; return *this; }
~websocket_client() { if (handle) esp_websocket_client_destroy(handle); }
esp_err_t set_uri (const char *uri) { return esp_websocket_client_set_uri (handle, uri); }
operator bool() const { return handle != NULL; }
//esp_err_t set_uri (const char *uri) { return esp_websocket_client_set_uri (handle, uri); }
esp_err_t set_uri (std::string_view uri) { return esp_websocket_client_set_uri (handle, uri.data()); }
esp_err_t start () { return esp_websocket_client_start (handle); }
esp_err_t stop () { return esp_websocket_client_stop (handle); }
int send (const char *data, int len, TickType_t timeout) { return esp_websocket_client_send (handle, data, len, timeout); }
int send_bin (const char *data, int len, TickType_t timeout) { return esp_websocket_client_send_bin (handle, data, len, timeout); }
int send_text (const char *data, int len, TickType_t timeout) { return esp_websocket_client_send_text (handle, data, len, timeout); }
//int send (const char *data, int len, TickType_t timeout) { return esp_websocket_client_send (handle, data, len, timeout); }
int send (std::string_view buf, TickType_t timeout) { return esp_websocket_client_send (handle, buf.data(), buf.size(), timeout); }
//int send_bin (const char *data, int len, TickType_t timeout) { return esp_websocket_client_send_bin (handle, data, len, timeout); }
int send_bin (std::string_view buf, TickType_t timeout) { return esp_websocket_client_send_bin (handle, buf.data(), buf.size(), timeout); }
//int send_text (const char *data, int len, TickType_t timeout) { return esp_websocket_client_send_text (handle, data, len, timeout); }
int send_text (std::string_view buf, TickType_t timeout) { return esp_websocket_client_send_text (handle, buf.data(), buf.size(), timeout); }
esp_err_t close (TickType_t timeout) { return esp_websocket_client_close (handle, timeout); }
esp_err_t close_with_code(int code, const char *data, int len, TickType_t timeout) { return esp_websocket_client_close_with_code(handle, code, data, len, timeout); }
//esp_err_t close_with_code(int code, const char *data, int len, TickType_t timeout) { return esp_websocket_client_close_with_code(handle, code, data, len, timeout); }
esp_err_t close_with_code(int code, std::string_view buf, TickType_t timeout) { return esp_websocket_client_close_with_code(handle, code, buf.data(), buf.size(), timeout); }
bool is_connected () const { return esp_websocket_client_is_connected (handle); }
esp_err_t register_events(esp_websocket_event_id_t event, esp_event_handler_t event_handler, void *event_handler_arg)
{ return esp_websocket_register_events(handle, event, event_handler, event_handler_arg); }
const esp_websocket_client_handle_t handle;
private:
esp_websocket_client_handle_t handle;
};
} // namespace espcpputils