From ec322754b26d8225ed4cf3fc81942159656d58ac Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 19 Apr 2024 09:52:02 +0200 Subject: [PATCH] fix(wifi_remote): Minor fixes per review --- components/esp_wifi_remote/eppp/eppp_init.c | 2 +- .../eppp/wifi_remote_rpc_client.cpp | 16 ++++++++++++---- .../eppp/wifi_remote_rpc_impl.hpp | 18 +++++++++++++++--- .../eppp/wifi_remote_rpc_server.cpp | 3 ++- .../examples/mqtt/CMakeLists.txt | 18 ------------------ 5 files changed, 30 insertions(+), 27 deletions(-) diff --git a/components/esp_wifi_remote/eppp/eppp_init.c b/components/esp_wifi_remote/eppp/eppp_init.c index 574ebfd2a3b..38b944f617d 100644 --- a/components/esp_wifi_remote/eppp/eppp_init.c +++ b/components/esp_wifi_remote/eppp/eppp_init.c @@ -7,7 +7,7 @@ #include "esp_wifi.h" #include "eppp_link.h" -esp_netif_t *wifi_remote_eppp_init(eppp_type_t role) +__attribute__((weak)) esp_netif_t *wifi_remote_eppp_init(eppp_type_t role) { uint32_t our_ip = role == EPPP_SERVER ? EPPP_DEFAULT_SERVER_IP() : EPPP_DEFAULT_CLIENT_IP(); uint32_t their_ip = role == EPPP_SERVER ? EPPP_DEFAULT_CLIENT_IP() : EPPP_DEFAULT_SERVER_IP(); diff --git a/components/esp_wifi_remote/eppp/wifi_remote_rpc_client.cpp b/components/esp_wifi_remote/eppp/wifi_remote_rpc_client.cpp index 2bbb7668525..708a98e13b2 100644 --- a/components/esp_wifi_remote/eppp/wifi_remote_rpc_client.cpp +++ b/components/esp_wifi_remote/eppp/wifi_remote_rpc_client.cpp @@ -26,12 +26,15 @@ const char *TAG = "rpc_client"; const unsigned char ca_crt[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_ESP_WIFI_REMOTE_EPPP_SERVER_CA "\n-----END CERTIFICATE-----"; const unsigned char crt[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_ESP_WIFI_REMOTE_EPPP_CLIENT_CRT "\n-----END CERTIFICATE-----"; const unsigned char key[] = "-----BEGIN RSA PRIVATE KEY-----\n" CONFIG_ESP_WIFI_REMOTE_EPPP_CLIENT_KEY "\n-----END RSA PRIVATE KEY-----"; +// TODO: Add option to supply keys and certs via a global symbol (file) } using namespace client; -struct Sync { +class Sync { + friend class RpcInstance; +public: void lock() { xSemaphoreTake(mutex, portMAX_DELAY); @@ -64,16 +67,21 @@ struct Sync { vEventGroupDelete(events); } } + + +private: SemaphoreHandle_t mutex{nullptr}; EventGroupHandle_t events{nullptr}; + const int request = 1; const int resp_header = 2; const int resp_payload = 4; - }; class RpcInstance { + friend class Sync; public: + template esp_err_t send(api_id id, T *t) { @@ -83,7 +91,7 @@ class RpcInstance { return ESP_OK; } - // specialization for (void) + // overload of the templated method (used for functions with no arguments) esp_err_t send(api_id id) { ESP_RETURN_ON_ERROR(sync.notify(sync.request), TAG, "failed to notify req"); @@ -176,7 +184,7 @@ class RpcInstance { namespace client { -RpcInstance instance; +constinit RpcInstance instance; } // namespace client RpcInstance *RpcEngine::init_client() diff --git a/components/esp_wifi_remote/eppp/wifi_remote_rpc_impl.hpp b/components/esp_wifi_remote/eppp/wifi_remote_rpc_impl.hpp index beb87d40705..1f2f441c30c 100644 --- a/components/esp_wifi_remote/eppp/wifi_remote_rpc_impl.hpp +++ b/components/esp_wifi_remote/eppp/wifi_remote_rpc_impl.hpp @@ -9,8 +9,11 @@ namespace eppp_rpc { -const int rpc_port = 3333; +static constexpr int rpc_port = 3333; +/** + * @brief Currently supported RPC commands/events + */ enum class api_id : uint32_t { ERROR, UNDEF, @@ -35,6 +38,9 @@ struct RpcHeader { uint32_t size; } __attribute((__packed__)); +/** + * @brief Structure holding the outgoing or incoming parameter + */ template struct RpcData { RpcHeader head; @@ -54,11 +60,17 @@ struct RpcData { } } __attribute((__packed__)); +/** + * @brief Singleton holding the static data for either the client or server side + */ class RpcInstance; +/** + * @brief Engine that implements a simple RPC mechanism + */ class RpcEngine { public: - explicit RpcEngine(role r) : tls_(nullptr), role_(r) {} + constexpr explicit RpcEngine(role r) : tls_(nullptr), role_(r) {} esp_err_t init() { @@ -90,7 +102,7 @@ class RpcEngine { return ESP_OK; } - esp_err_t send(api_id id) // specialization for (void) + esp_err_t send(api_id id) // overload for (void) { RpcHeader head = {.id = id, .size = 0}; int len = esp_tls_conn_write(tls_, &head, sizeof(head)); diff --git a/components/esp_wifi_remote/eppp/wifi_remote_rpc_server.cpp b/components/esp_wifi_remote/eppp/wifi_remote_rpc_server.cpp index aed9faa54e7..03ce52080c4 100644 --- a/components/esp_wifi_remote/eppp/wifi_remote_rpc_server.cpp +++ b/components/esp_wifi_remote/eppp/wifi_remote_rpc_server.cpp @@ -26,6 +26,7 @@ const char *TAG = "rpc_server"; const unsigned char ca_crt[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_ESP_WIFI_REMOTE_EPPP_CLIENT_CA "\n-----END CERTIFICATE-----"; const unsigned char crt[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_ESP_WIFI_REMOTE_EPPP_SERVER_CRT "\n-----END CERTIFICATE-----"; const unsigned char key[] = "-----BEGIN RSA PRIVATE KEY-----\n" CONFIG_ESP_WIFI_REMOTE_EPPP_SERVER_KEY "\n-----END RSA PRIVATE KEY-----"; +// TODO: Add option to supply keys and certs via a global symbol (file) } @@ -181,7 +182,7 @@ class RpcInstance { namespace server { -RpcInstance instance; +constinit RpcInstance instance; } RpcInstance *RpcEngine::init_server() diff --git a/components/esp_wifi_remote/examples/mqtt/CMakeLists.txt b/components/esp_wifi_remote/examples/mqtt/CMakeLists.txt index b07caaa52cb..500c42d77f7 100644 --- a/components/esp_wifi_remote/examples/mqtt/CMakeLists.txt +++ b/components/esp_wifi_remote/examples/mqtt/CMakeLists.txt @@ -1,23 +1,5 @@ # This project serves as a demo to enable using esp-mqtt on ESP platform targets as well as on linux cmake_minimum_required(VERSION 3.16) -if("${IDF_TARGET}" STREQUAL "linux") -# For linux-target we have two options: -# - With lwIP (must be defined on command line, e.g. idf.py -DWITH_LWIP=1) -# access networking from linux `tap` interface (TAP networking mode) -# - Without lwIP (must be defined on command line, e.g. idf.py -DWITH_LWIP=0) -# no designated interface, accesses user network via linux/socket sys calls - if(WITH_LWIP STREQUAL 1) - set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_tapif_io - "../../common_components/linux_compat/esp_timer") - set(COMPONENTS main esp_netif lwip protocol_examples_tapif_io startup esp_hw_support esp_system nvs_flash mqtt esp_timer) - else() - list(APPEND EXTRA_COMPONENT_DIRS - "../../common_components/linux_compat/esp_timer" - "$ENV{IDF_PATH}/examples/protocols/linux_stubs/esp_stubs") - set(COMPONENTS main nvs_flash esp-tls esp_stubs mqtt protocol_examples_common esp_timer) - endif() -endif() - include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(esp_mqtt_demo)