mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-08-01 03:34:40 +02:00
fix(wifi_remote): Make services restartable, code cleanup
This commit is contained in:
@@ -7,17 +7,14 @@
|
||||
#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();
|
||||
eppp_config_t config = EPPP_DEFAULT_CONFIG(our_ip, their_ip);
|
||||
#if CONFIG_ESP_WIFI_REMOTE_EPPP_TRANSPORT_UART
|
||||
// We currently support only UART transport
|
||||
config.transport = EPPP_TRANSPORT_UART;
|
||||
config.uart.tx_io = CONFIG_ESP_WIFI_REMOTE_EPPP_UART_TX_PIN;
|
||||
config.uart.rx_io = CONFIG_ESP_WIFI_REMOTE_EPPP_UART_RX_PIN;
|
||||
#else
|
||||
#error ESP_WIFI_REMOTE supports only UART transport
|
||||
#endif
|
||||
return eppp_open(role, &config, portMAX_DELAY);
|
||||
}
|
||||
|
@@ -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);
|
||||
@@ -48,7 +51,7 @@ struct Sync {
|
||||
}
|
||||
esp_err_t wait_for(EventBits_t bits, uint32_t timeout = portMAX_DELAY)
|
||||
{
|
||||
return xEventGroupWaitBits(events, bits, pdTRUE, pdTRUE, timeout) == bits ? ESP_OK : ESP_FAIL;
|
||||
return (xEventGroupWaitBits(events, bits, pdTRUE, pdTRUE, timeout) & bits) == bits ? ESP_OK : ESP_FAIL;
|
||||
}
|
||||
esp_err_t notify(EventBits_t bits)
|
||||
{
|
||||
@@ -64,30 +67,36 @@ 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;
|
||||
|
||||
const int restart = 8;
|
||||
};
|
||||
|
||||
class RpcInstance {
|
||||
friend class Sync;
|
||||
public:
|
||||
|
||||
template<typename T>
|
||||
esp_err_t send(api_id id, T *t)
|
||||
{
|
||||
ESP_RETURN_ON_ERROR(sync.notify(sync.request), TAG, "failed to notify req");
|
||||
pending_resp = id;
|
||||
ESP_RETURN_ON_ERROR(sync.notify(sync.request), TAG, "failed to notify req");
|
||||
ESP_RETURN_ON_ERROR(rpc.send<T>(id, t), TAG, "Failed to send request");
|
||||
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");
|
||||
pending_resp = id;
|
||||
ESP_RETURN_ON_ERROR(sync.notify(sync.request), TAG, "failed to notify req");
|
||||
ESP_RETURN_ON_ERROR(rpc.send(id), TAG, "Failed to send request");
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -103,6 +112,7 @@ public:
|
||||
esp_err_t init()
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(netif = wifi_remote_eppp_init(EPPP_CLIENT), ESP_FAIL, TAG, "Failed to connect to EPPP server");
|
||||
ESP_RETURN_ON_ERROR(esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_GOT_IP, got_ip, this), TAG, "Failed to register event");
|
||||
ESP_RETURN_ON_ERROR(sync.init(), TAG, "Failed to init sync primitives");
|
||||
ESP_RETURN_ON_ERROR(rpc.init(), TAG, "Failed to init RPC engine");
|
||||
return xTaskCreate(task, "client", 8192, this, 5, nullptr) == pdTRUE ? ESP_OK : ESP_FAIL;
|
||||
@@ -168,15 +178,28 @@ private:
|
||||
static void task(void *ctx)
|
||||
{
|
||||
auto instance = static_cast<RpcInstance *>(ctx);
|
||||
while (instance->perform() == ESP_OK) {}
|
||||
do {
|
||||
while (instance->perform() == ESP_OK) {}
|
||||
} while (instance->restart() == ESP_OK);
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
esp_err_t restart()
|
||||
{
|
||||
rpc.deinit();
|
||||
ESP_RETURN_ON_ERROR(sync.wait_for(sync.restart, pdMS_TO_TICKS(10000)), TAG, "Didn't receive EPPP address in time");
|
||||
return rpc.init();
|
||||
}
|
||||
static void got_ip(void *ctx, esp_event_base_t base, int32_t id, void *data)
|
||||
{
|
||||
auto instance = static_cast<RpcInstance *>(ctx);
|
||||
instance->sync.notify(instance->sync.restart);
|
||||
}
|
||||
esp_netif_t *netif{nullptr};
|
||||
};
|
||||
|
||||
|
||||
namespace client {
|
||||
RpcInstance instance;
|
||||
constinit RpcInstance instance;
|
||||
} // namespace client
|
||||
|
||||
RpcInstance *RpcEngine::init_client()
|
||||
@@ -196,20 +219,17 @@ RpcInstance *RpcEngine::init_client()
|
||||
cfg.clientkey_bytes = sizeof(client::key);
|
||||
cfg.common_name = "espressif.local";
|
||||
|
||||
tls_ = esp_tls_init();
|
||||
if (!tls_) {
|
||||
ESP_LOGE(TAG, "Failed to allocate esp_tls handle!");
|
||||
goto exit;
|
||||
}
|
||||
if (esp_tls_conn_new_sync(host, strlen(host), rpc_port, &cfg, tls_) <= 0) {
|
||||
ESP_LOGE(TAG, "Failed to open a new connection %s", host);
|
||||
goto exit;
|
||||
ESP_RETURN_ON_FALSE(tls_ = esp_tls_init(), nullptr, TAG, "Failed to create ESP-TLS instance");
|
||||
int retries = 0;
|
||||
while (esp_tls_conn_new_sync(host, strlen(host), rpc_port, &cfg, tls_) <= 0) {
|
||||
esp_tls_conn_destroy(tls_);
|
||||
tls_ = nullptr;
|
||||
ESP_RETURN_ON_FALSE(retries++ < 3, nullptr, TAG, "Failed to open connection to %s", host);
|
||||
ESP_LOGW(TAG, "Connection to RPC server failed! Will retry in %d second(s)", retries);
|
||||
vTaskDelay(pdMS_TO_TICKS(1000 * retries));
|
||||
ESP_RETURN_ON_FALSE(tls_ = esp_tls_init(), nullptr, TAG, "Failed to create ESP-TLS instance");
|
||||
}
|
||||
return &client::instance;
|
||||
exit:
|
||||
esp_tls_conn_destroy(tls_);
|
||||
tls_ = nullptr;
|
||||
return nullptr;
|
||||
}
|
||||
} // namespace eppp_rpc
|
||||
|
||||
|
@@ -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<typename T>
|
||||
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()
|
||||
{
|
||||
@@ -74,6 +86,19 @@ public:
|
||||
return instance == nullptr ? ESP_FAIL : ESP_OK;
|
||||
}
|
||||
|
||||
void deinit()
|
||||
{
|
||||
if (tls_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (role_ == role::CLIENT) {
|
||||
esp_tls_conn_destroy(tls_);
|
||||
} else if (role_ == role::SERVER) {
|
||||
esp_tls_server_session_delete(tls_);
|
||||
}
|
||||
tls_ = nullptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
esp_err_t send(api_id id, T *t)
|
||||
{
|
||||
@@ -90,7 +115,7 @@ public:
|
||||
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));
|
||||
|
@@ -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)
|
||||
|
||||
}
|
||||
|
||||
@@ -53,7 +54,7 @@ private:
|
||||
{
|
||||
auto instance = static_cast<RpcInstance *>(ctx);
|
||||
while (instance->perform() == ESP_OK) {}
|
||||
vTaskDelete(nullptr);
|
||||
esp_restart();
|
||||
}
|
||||
esp_err_t start_server()
|
||||
{
|
||||
@@ -181,7 +182,7 @@ private:
|
||||
|
||||
|
||||
namespace server {
|
||||
RpcInstance instance;
|
||||
constinit RpcInstance instance;
|
||||
}
|
||||
|
||||
RpcInstance *RpcEngine::init_server()
|
||||
|
Reference in New Issue
Block a user