mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-04 06:06:45 +02:00
Compare commits
25 Commits
asio-v1.0.
...
mdns-v1.0.
Author | SHA1 | Date | |
---|---|---|---|
48c157bc46 | |||
444fae9066 | |||
2099434b3f | |||
8f00fc182a | |||
d74c296182 | |||
c4e85bd099 | |||
85ba60e405 | |||
89e1bd27b3 | |||
341fcb0f40 | |||
469f953b28 | |||
381eb314dc | |||
1ffc20c8e3 | |||
5c245dbdb5 | |||
134a9a9eee | |||
010f98ca80 | |||
6e4e4fab1d | |||
b6852a0588 | |||
415e04a55f | |||
3456781494 | |||
d1129f3d19 | |||
973837dd66 | |||
36de9afe0c | |||
0d5081b841 | |||
85c7282641 | |||
238ee96783 |
2
.github/workflows/target-test.yml
vendored
2
.github/workflows/target-test.yml
vendored
@ -23,7 +23,7 @@ jobs:
|
||||
container: espressif/idf:${{ matrix.idf_ver }}
|
||||
steps:
|
||||
- name: Checkout esp-protocols
|
||||
uses: actions/checkout@master
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
path: esp-protocols
|
||||
- name: Build ${{ matrix.example }} with IDF-${{ matrix.idf_ver }} for ${{ matrix.idf_target }}
|
||||
|
@ -1,4 +1,4 @@
|
||||
idf_component_register(SRCS "connect.c" "stdin_out.c" "addr_from_stdin.c"
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_REQUIRES esp_netif driver esp_eth
|
||||
PRIV_REQUIRES esp_netif driver esp_eth esp_wifi vfs
|
||||
)
|
||||
|
@ -11,9 +11,10 @@ else()
|
||||
src/esp_modem_uart.cpp
|
||||
src/esp_modem_term_uart.cpp
|
||||
src/esp_modem_netif.cpp)
|
||||
set(dependencies driver)
|
||||
set(dependencies driver esp_event esp_netif)
|
||||
endif()
|
||||
|
||||
|
||||
set(srcs ${platform_srcs}
|
||||
"src/esp_modem_dte.cpp"
|
||||
"src/esp_modem_dce.cpp"
|
||||
@ -34,12 +35,14 @@ idf_component_register(SRCS "${srcs}"
|
||||
PRIV_INCLUDE_DIRS private_include
|
||||
REQUIRES ${dependencies})
|
||||
|
||||
|
||||
set_target_properties(${COMPONENT_LIB} PROPERTIES
|
||||
CXX_STANDARD 17
|
||||
CXX_STANDARD_REQUIRED ON
|
||||
CXX_EXTENSIONS ON
|
||||
)
|
||||
|
||||
|
||||
if(${target} STREQUAL "linux")
|
||||
# This is needed for ESP_LOGx() macros, as integer formats differ on ESP32(..) and x64
|
||||
set_target_properties(${COMPONENT_LIB} PROPERTIES COMPILE_FLAGS -Wno-format)
|
||||
|
@ -7,3 +7,4 @@ endif()
|
||||
idf_component_register(SRCS "ap_to_pppos.c"
|
||||
${NETWORK_DCE}
|
||||
INCLUDE_DIRS ".")
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")
|
||||
|
@ -61,7 +61,7 @@ int main()
|
||||
|
||||
bool pin_ok = true;
|
||||
if (dce->read_pin(pin_ok) == command_result::OK && !pin_ok) {
|
||||
throw_if_false(dce->set_pin(CONFIG_EXAMPLE_SIM_PIN) == command_result::OK, "Cannot set PIN!");
|
||||
ESP_MODEM_THROW_IF_FALSE(dce->set_pin(CONFIG_EXAMPLE_SIM_PIN) == command_result::OK, "Cannot set PIN!");
|
||||
usleep(1000000);
|
||||
}
|
||||
std::string str;
|
||||
|
@ -70,9 +70,9 @@ public:
|
||||
.skip_phy_setup = false,
|
||||
.intr_flags = ESP_INTR_FLAG_LEVEL1,
|
||||
};
|
||||
throw_if_esp_fail(usb_host_install(&host_config), "USB Host install failed");
|
||||
ESP_MODEM_THROW_IF_ERROR(usb_host_install(&host_config), "USB Host install failed");
|
||||
ESP_LOGD(TAG, "USB Host installed");
|
||||
throw_if_false(pdTRUE == xTaskCreatePinnedToCore(usb_host_task, "usb_host", 4096, NULL, config->task_priority + 1, NULL, usb_config->xCoreID), "USB host task failed");
|
||||
ESP_MODEM_THROW_IF_FALSE(pdTRUE == xTaskCreatePinnedToCore(usb_host_task, "usb_host", 4096, NULL, config->task_priority + 1, NULL, usb_config->xCoreID), "USB host task failed");
|
||||
}
|
||||
|
||||
// Install CDC-ACM driver
|
||||
@ -95,11 +95,11 @@ public:
|
||||
};
|
||||
|
||||
if (usb_config->cdc_compliant) {
|
||||
throw_if_esp_fail(this->CdcAcmDevice::open(usb_config->vid, usb_config->pid,
|
||||
ESP_MODEM_THROW_IF_ERROR(this->CdcAcmDevice::open(usb_config->vid, usb_config->pid,
|
||||
usb_config->interface_idx, &esp_modem_cdc_acm_device_config),
|
||||
"USB Device open failed");
|
||||
} else {
|
||||
throw_if_esp_fail(this->CdcAcmDevice::open_vendor_specific(usb_config->vid, usb_config->pid,
|
||||
ESP_MODEM_THROW_IF_ERROR(this->CdcAcmDevice::open_vendor_specific(usb_config->vid, usb_config->pid,
|
||||
usb_config->interface_idx, &esp_modem_cdc_acm_device_config),
|
||||
"USB Device open failed");
|
||||
}
|
||||
|
@ -22,11 +22,13 @@ static EventGroupHandle_t event_group = NULL;
|
||||
static const int CONNECT_BIT = BIT0;
|
||||
static const int GOT_DATA_BIT = BIT2;
|
||||
|
||||
static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
|
||||
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
|
||||
{
|
||||
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
|
||||
esp_mqtt_event_handle_t event = event_data;
|
||||
esp_mqtt_client_handle_t client = event->client;
|
||||
int msg_id;
|
||||
switch (event->event_id) {
|
||||
switch ((esp_mqtt_event_id_t)event_id) {
|
||||
case MQTT_EVENT_CONNECTED:
|
||||
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
|
||||
msg_id = esp_mqtt_client_subscribe(client, "/topic/esp-pppos", 0);
|
||||
@ -59,7 +61,6 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
|
||||
ESP_LOGI(TAG, "MQTT other event id: %d", event->event_id);
|
||||
break;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void on_ppp_changed(void *arg, esp_event_base_t event_base,
|
||||
@ -200,11 +201,17 @@ void app_main(void)
|
||||
/* Wait for IP address */
|
||||
xEventGroupWaitBits(event_group, CONNECT_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
|
||||
/* Config MQTT */
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
esp_mqtt_client_config_t mqtt_config = {
|
||||
.broker.address.uri = BROKER_URL,
|
||||
};
|
||||
#else
|
||||
esp_mqtt_client_config_t mqtt_config = {
|
||||
.uri = BROKER_URL,
|
||||
.event_handle = mqtt_event_handler,
|
||||
};
|
||||
#endif
|
||||
esp_mqtt_client_handle_t mqtt_client = esp_mqtt_client_init(&mqtt_config);
|
||||
esp_mqtt_client_register_event(mqtt_client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
|
||||
esp_mqtt_client_start(mqtt_client);
|
||||
xEventGroupWaitBits(event_group, GOT_DATA_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
|
||||
esp_mqtt_client_destroy(mqtt_client);
|
||||
|
@ -82,7 +82,7 @@ extern "C" void app_main(void)
|
||||
#if CONFIG_EXAMPLE_NEED_SIM_PIN == 1
|
||||
bool pin_ok = true;
|
||||
if (dce->read_pin(pin_ok) == command_result::OK && !pin_ok) {
|
||||
throw_if_false(dce->set_pin(CONFIG_EXAMPLE_SIM_PIN) == command_result::OK, "Cannot set PIN!");
|
||||
ESP_MODEM_THROW_IF_FALSE(dce->set_pin(CONFIG_EXAMPLE_SIM_PIN) == command_result::OK, "Cannot set PIN!");
|
||||
vTaskDelay(pdMS_TO_TICKS(1000)); // Need to wait for some time after unlocking the SIM
|
||||
}
|
||||
#endif
|
||||
|
@ -26,7 +26,11 @@ struct MqttClientHandle {
|
||||
explicit MqttClientHandle(const std::string &uri)
|
||||
{
|
||||
esp_mqtt_client_config_t config = { };
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
config.broker.address.uri = uri.c_str();
|
||||
#else
|
||||
config.uri = uri.c_str();
|
||||
#endif
|
||||
client = esp_mqtt_client_init(&config);
|
||||
esp_mqtt_client_register_event(client, MQTT_EVENT_ANY, mqtt_event_handler, this);
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
version: "0.1.18"
|
||||
version: "0.1.19"
|
||||
description: esp modem
|
||||
url: https://github.com/espressif/esp-protocols/tree/master/components/esp_modem
|
||||
dependencies:
|
||||
|
@ -69,12 +69,12 @@ class Creator {
|
||||
public:
|
||||
Creator(std::shared_ptr<DTE> dte, esp_netif_t *esp_netif): dte(std::move(dte)), device(nullptr), netif(esp_netif)
|
||||
{
|
||||
throw_if_false(netif != nullptr, "Null netif");
|
||||
ESP_MODEM_THROW_IF_FALSE(netif != nullptr, "Null netif");
|
||||
}
|
||||
|
||||
Creator(std::shared_ptr<DTE> dte, esp_netif_t *esp_netif, std::shared_ptr<T_Module> dev): dte(std::move(dte)), device(std::move(dev)), netif(esp_netif)
|
||||
{
|
||||
throw_if_false(netif != nullptr, "Null netif");
|
||||
ESP_MODEM_THROW_IF_FALSE(netif != nullptr, "Null netif");
|
||||
}
|
||||
|
||||
~Creator()
|
||||
|
@ -16,14 +16,21 @@
|
||||
|
||||
#include <string>
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#ifndef __FILENAME__
|
||||
#define __FILENAME__ __FILE__
|
||||
#endif
|
||||
#define ESP_MODEM_THROW_IF_FALSE(...) esp_modem::throw_if_false(__FILENAME__, __LINE__, __VA_ARGS__)
|
||||
#define ESP_MODEM_THROW_IF_ERROR(...) esp_modem::throw_if_error(__FILENAME__, __LINE__, __VA_ARGS__)
|
||||
|
||||
namespace esp_modem {
|
||||
|
||||
#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
|
||||
#define THROW(exception) throw(exception)
|
||||
#define ESP_MODEM_THROW(exception) throw(exception)
|
||||
|
||||
class esp_err_exception: virtual public std::exception {
|
||||
public:
|
||||
explicit esp_err_exception(esp_err_t err): esp_err(err) {}
|
||||
explicit esp_err_exception(std::string msg): esp_err(ESP_FAIL), message(std::move(msg)) {}
|
||||
explicit esp_err_exception(std::string msg, esp_err_t err): esp_err(err), message(std::move(msg)) {}
|
||||
virtual esp_err_t get_err_t()
|
||||
@ -31,7 +38,7 @@ public:
|
||||
return esp_err;
|
||||
}
|
||||
~esp_err_exception() noexcept override = default;
|
||||
virtual const char *what() const noexcept
|
||||
[[nodiscard]] const char *what() const noexcept override
|
||||
{
|
||||
return message.c_str();
|
||||
}
|
||||
@ -39,28 +46,43 @@ private:
|
||||
esp_err_t esp_err;
|
||||
std::string message;
|
||||
};
|
||||
|
||||
#else
|
||||
#define THROW(exception) abort()
|
||||
#define ESP_MODEM_THROW(exception) do { exception; abort(); } while(0)
|
||||
|
||||
class esp_err_exception {
|
||||
void print(std::string msg) { ESP_LOGE("ESP_MODEM_THROW", "%s\n", msg.c_str()); }
|
||||
public:
|
||||
explicit esp_err_exception(std::string msg) { print(std::move(msg)); }
|
||||
explicit esp_err_exception(std::string msg, esp_err_t err) { print(std::move(msg)); }
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
static inline void throw_if_false(bool condition, std::string message)
|
||||
static inline std::string make_message(const std::string& filename, int line, const std::string& message = "ERROR")
|
||||
{
|
||||
std::string text = filename + ":" + std::to_string(line) + " " + message;
|
||||
return text;
|
||||
}
|
||||
|
||||
static inline void throw_if_false(const std::string& filename, int line, bool condition, const std::string& message)
|
||||
{
|
||||
if (!condition) {
|
||||
THROW(esp_err_exception(std::move(message)));
|
||||
ESP_MODEM_THROW(esp_err_exception(make_message(filename, line, message)));
|
||||
}
|
||||
}
|
||||
|
||||
static inline void throw_if_esp_fail(esp_err_t err, std::string message)
|
||||
static inline void throw_if_error(const std::string& filename, int line, esp_err_t err, const std::string& message)
|
||||
{
|
||||
if (err != ESP_OK) {
|
||||
THROW(esp_err_exception(std::move(message), err));
|
||||
ESP_MODEM_THROW(esp_err_exception(make_message(filename, line, message), err));
|
||||
}
|
||||
}
|
||||
|
||||
static inline void throw_if_esp_fail(esp_err_t err)
|
||||
static inline void throw_if_error(const std::string& filename, int line, esp_err_t err)
|
||||
{
|
||||
if (err != ESP_OK) {
|
||||
THROW(esp_err_exception(err));
|
||||
ESP_MODEM_THROW(esp_err_exception(make_message(filename, line), err));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,2 +1,3 @@
|
||||
idf_component_register(INCLUDE_DIRS include
|
||||
idf_component_register(SRCS "esp_err_to_name.c"
|
||||
INCLUDE_DIRS include
|
||||
REQUIRES esp_netif_linux esp_event_mock)
|
||||
|
@ -0,0 +1,14 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: Franz Hoepfinger
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
static const char esp_unknown_msg[] = "ERROR";
|
||||
|
||||
const char *esp_err_to_name(int code)
|
||||
{
|
||||
return esp_unknown_msg;
|
||||
}
|
@ -25,3 +25,11 @@ typedef int esp_err_t;
|
||||
#define ESP_ERR_NOT_FOUND 0x105
|
||||
#define ESP_ERR_NOT_SUPPORTED 0x106
|
||||
#define ESP_ERR_TIMEOUT 0x107
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
const char *esp_err_to_name(int code);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -22,7 +22,7 @@
|
||||
action; \
|
||||
} catch (::esp_modem::esp_err_exception& e) { \
|
||||
esp_err_t err = e.get_err_t(); \
|
||||
ESP_LOGE(TAG, "%s: Exception caught with ESP err_code=%d", __func__, err); \
|
||||
ESP_LOGE(TAG, "%s: Exception caught with ESP err_code=%d %s", __func__, err, esp_err_to_name(err)); \
|
||||
ESP_LOGE(TAG, "%s", e.what()); \
|
||||
action; \
|
||||
}
|
||||
|
@ -82,7 +82,7 @@ void CMux::send_disconnect(size_t i)
|
||||
{
|
||||
if (i == 0) { // control terminal
|
||||
uint8_t frame[] = {
|
||||
SOF_MARKER, 0x3, 0xFF, 0x5, 0xC3, 0x1, 0xE7, SOF_MARKER };
|
||||
SOF_MARKER, 0x3, 0xEF, 0x5, 0xC3, 0x1, 0xF2, SOF_MARKER };
|
||||
term->write(frame, 8);
|
||||
} else { // separate virtual terminal
|
||||
uint8_t frame[] = {
|
||||
@ -142,7 +142,7 @@ void CMux::data_available(uint8_t *data, size_t len)
|
||||
read_cb[virtual_term](payload_start, total_payload_size);
|
||||
#endif
|
||||
}
|
||||
} else if (type == 0xFF && dlci == 0) { // notify the internal DISC command
|
||||
} else if ((type&FT_UIH) == FT_UIH && dlci == 0) { // notify the internal DISC command
|
||||
Scoped<Lock> l(lock);
|
||||
sabm_ack = dlci;
|
||||
}
|
||||
@ -325,11 +325,12 @@ bool CMux::on_cmux_data(uint8_t *data, size_t actual_len)
|
||||
|
||||
bool CMux::deinit()
|
||||
{
|
||||
int timeout = 0;
|
||||
int timeout;
|
||||
sabm_ack = -1;
|
||||
// First disconnect all (2) virtual terminals
|
||||
for (size_t i = 1; i < 3; i++) {
|
||||
send_disconnect(i);
|
||||
timeout = 0;
|
||||
while (true) {
|
||||
usleep(10'000);
|
||||
Scoped<Lock> l(lock);
|
||||
@ -345,6 +346,7 @@ bool CMux::deinit()
|
||||
sabm_ack = -1;
|
||||
// Then disconnect the control terminal
|
||||
send_disconnect(0);
|
||||
timeout = 0;
|
||||
while (true) {
|
||||
usleep(10'000);
|
||||
Scoped<Lock> l(lock);
|
||||
|
@ -56,7 +56,7 @@ command_result DTE::command(const std::string &command, got_line_cb got_line, ui
|
||||
command_term->write((uint8_t *)command.c_str(), command.length());
|
||||
auto got_lf = signal.wait(GOT_LINE, time_ms);
|
||||
if (got_lf && res == command_result::TIMEOUT) {
|
||||
throw_if_esp_fail(ESP_ERR_INVALID_STATE);
|
||||
ESP_MODEM_THROW_IF_ERROR(ESP_ERR_INVALID_STATE);
|
||||
}
|
||||
buffer.consumed = 0;
|
||||
command_term->set_read_cb(nullptr);
|
||||
|
@ -82,11 +82,11 @@ Netif::Netif(std::shared_ptr<DTE> e, esp_netif_t *ppp_netif) :
|
||||
driver.base.netif = ppp_netif;
|
||||
driver.ppp = this;
|
||||
driver.base.post_attach = esp_modem_post_attach;
|
||||
throw_if_esp_fail(esp_event_handler_register(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed, (void *) this));
|
||||
throw_if_esp_fail(esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_GOT_IP, esp_netif_action_connected, ppp_netif));
|
||||
throw_if_esp_fail(
|
||||
ESP_MODEM_THROW_IF_ERROR(esp_event_handler_register(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed, (void *) this));
|
||||
ESP_MODEM_THROW_IF_ERROR(esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_GOT_IP, esp_netif_action_connected, ppp_netif));
|
||||
ESP_MODEM_THROW_IF_ERROR(
|
||||
esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_LOST_IP, esp_netif_action_disconnected, ppp_netif));
|
||||
throw_if_esp_fail(esp_netif_attach(ppp_netif, &driver));
|
||||
ESP_MODEM_THROW_IF_ERROR(esp_netif_attach(ppp_netif, &driver));
|
||||
}
|
||||
|
||||
void Netif::start()
|
||||
|
@ -28,7 +28,7 @@ void Lock::unlock()
|
||||
Lock::Lock(): m(nullptr)
|
||||
{
|
||||
m = xSemaphoreCreateRecursiveMutex();
|
||||
throw_if_false(m != nullptr, "create signal event group failed");
|
||||
ESP_MODEM_THROW_IF_FALSE(m != nullptr, "create signal event group failed");
|
||||
}
|
||||
|
||||
Lock::~Lock()
|
||||
@ -45,7 +45,7 @@ void Lock::lock()
|
||||
SignalGroup::SignalGroup(): event_group(nullptr)
|
||||
{
|
||||
event_group = xEventGroupCreate();
|
||||
throw_if_false(event_group != nullptr, "create signal event group failed");
|
||||
ESP_MODEM_THROW_IF_FALSE(event_group != nullptr, "create signal event group failed");
|
||||
}
|
||||
|
||||
void SignalGroup::set(uint32_t bits)
|
||||
@ -86,7 +86,7 @@ Task::Task(size_t stack_size, size_t priority, void *task_param, TaskFunction_t
|
||||
: task_handle(nullptr)
|
||||
{
|
||||
BaseType_t ret = xTaskCreate(task_function, "vfs_task", stack_size, task_param, priority, &task_handle);
|
||||
throw_if_false(ret == pdTRUE, "create vfs task failed");
|
||||
ESP_MODEM_THROW_IF_FALSE(ret == pdTRUE, "create vfs task failed");
|
||||
}
|
||||
|
||||
Task::~Task()
|
||||
|
@ -40,7 +40,7 @@ uart_resource::uart_resource(const esp_modem_uart_term_config *config, QueueHand
|
||||
: UART_HW_FLOWCTRL_DISABLE;
|
||||
uart_config.source_clk = config->source_clk;
|
||||
|
||||
throw_if_esp_fail(uart_param_config(config->port_num, &uart_config), "config uart parameter failed");
|
||||
ESP_MODEM_THROW_IF_ERROR(uart_param_config(config->port_num, &uart_config), "config uart parameter failed");
|
||||
|
||||
if (config->flow_control == ESP_MODEM_FLOW_CONTROL_HW) {
|
||||
res = uart_set_pin(config->port_num, config->tx_io_num, config->rx_io_num,
|
||||
@ -49,24 +49,24 @@ uart_resource::uart_resource(const esp_modem_uart_term_config *config, QueueHand
|
||||
res = uart_set_pin(config->port_num, config->tx_io_num, config->rx_io_num,
|
||||
UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
|
||||
}
|
||||
throw_if_esp_fail(res, "config uart gpio failed");
|
||||
ESP_MODEM_THROW_IF_ERROR(res, "config uart gpio failed");
|
||||
/* Set flow control threshold */
|
||||
if (config->flow_control == ESP_MODEM_FLOW_CONTROL_HW) {
|
||||
res = uart_set_hw_flow_ctrl(config->port_num, UART_HW_FLOWCTRL_CTS_RTS, UART_FIFO_LEN - 8);
|
||||
} else if (config->flow_control == ESP_MODEM_FLOW_CONTROL_SW) {
|
||||
res = uart_set_sw_flow_ctrl(config->port_num, true, 8, UART_FIFO_LEN - 8);
|
||||
}
|
||||
throw_if_esp_fail(res, "config uart flow control failed");
|
||||
ESP_MODEM_THROW_IF_ERROR(res, "config uart flow control failed");
|
||||
|
||||
/* Install UART driver and get event queue used inside driver */
|
||||
res = uart_driver_install(config->port_num,
|
||||
config->rx_buffer_size, config->tx_buffer_size,
|
||||
config->event_queue_size, config->event_queue_size ? event_queue : nullptr,
|
||||
0);
|
||||
throw_if_esp_fail(res, "install uart driver failed");
|
||||
throw_if_esp_fail(uart_set_rx_timeout(config->port_num, 1), "set rx timeout failed");
|
||||
ESP_MODEM_THROW_IF_ERROR(res, "install uart driver failed");
|
||||
ESP_MODEM_THROW_IF_ERROR(uart_set_rx_timeout(config->port_num, 1), "set rx timeout failed");
|
||||
|
||||
throw_if_esp_fail(uart_set_rx_full_threshold(config->port_num, 64), "config rx full threshold failed");
|
||||
ESP_MODEM_THROW_IF_ERROR(uart_set_rx_full_threshold(config->port_num, 64), "config rx full threshold failed");
|
||||
|
||||
/* mark UART as initialized */
|
||||
port = config->port_num;
|
||||
|
@ -33,7 +33,7 @@ struct uart_task {
|
||||
task_handle(nullptr)
|
||||
{
|
||||
BaseType_t ret = xTaskCreate(task_function, "uart_task", stack_size, task_param, priority, &task_handle);
|
||||
throw_if_false(ret == pdTRUE, "create uart event task failed");
|
||||
ESP_MODEM_THROW_IF_FALSE(ret == pdTRUE, "create uart event task failed");
|
||||
}
|
||||
|
||||
~uart_task()
|
||||
|
@ -27,7 +27,7 @@ uart_resource::uart_resource(const esp_modem_uart_term_config *config, QueueHand
|
||||
{
|
||||
ESP_LOGD(TAG, "Creating uart resource" );
|
||||
struct termios tty = {};
|
||||
throw_if_false(tcgetattr(fd, &tty) == 0, "Failed to tcgetattr()");
|
||||
ESP_MODEM_THROW_IF_FALSE(tcgetattr(fd, &tty) == 0, "Failed to tcgetattr()");
|
||||
|
||||
tty.c_cflag &= ~PARENB;
|
||||
tty.c_cflag &= ~CSTOPB;
|
||||
|
@ -91,7 +91,7 @@ bool vfs_create_socket(struct esp_modem_vfs_socket_creator *config, struct esp_m
|
||||
}
|
||||
TRY_CATCH_OR_DO(
|
||||
int fd = -1;
|
||||
esp_modem::throw_if_esp_fail(hostname_to_fd(config->host_name, config->port, &fd));
|
||||
ESP_MODEM_THROW_IF_ERROR(hostname_to_fd(config->host_name, config->port, &fd));
|
||||
|
||||
// Set the FD to non-blocking mode
|
||||
int flags = fcntl(fd, F_GETFL, nullptr) | O_NONBLOCK;
|
||||
|
@ -48,7 +48,7 @@ bool vfs_create_uart(struct esp_modem_vfs_uart_creator *config, struct esp_modem
|
||||
}
|
||||
TRY_CATCH_OR_DO(
|
||||
int fd = open(config->dev_name, O_RDWR);
|
||||
esp_modem::throw_if_false(fd >= 0, "Cannot open the fd");
|
||||
ESP_MODEM_THROW_IF_FALSE(fd >= 0, "Cannot open the fd");
|
||||
|
||||
created_config->resource = new esp_modem_vfs_resource(&config->uart, fd);
|
||||
created_config->fd = fd;
|
||||
|
@ -9,5 +9,5 @@ endif()
|
||||
idf_component_register(SRCS "esp_websocket_client.c"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES lwip esp-tls tcp_transport http_parser
|
||||
PRIV_REQUIRES esp_timer)
|
||||
PRIV_REQUIRES esp_timer esp_event)
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include "esp_log.h"
|
||||
#include "esp_websocket_client.h"
|
||||
#include "esp_event.h"
|
||||
#include <cJSON.h>
|
||||
|
||||
#define NO_DATA_TIMEOUT_SEC 5
|
||||
|
||||
@ -74,6 +75,19 @@ static void websocket_event_handler(void *handler_args, esp_event_base_t base, i
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Received=%.*s", data->data_len, (char *)data->data_ptr);
|
||||
}
|
||||
|
||||
// If received data contains json structure it succeed to parse
|
||||
cJSON *root = cJSON_Parse(data->data_ptr);
|
||||
if (root) {
|
||||
for (int i = 0 ; i < cJSON_GetArraySize(root) ; i++) {
|
||||
cJSON *elem = cJSON_GetArrayItem(root, i);
|
||||
cJSON *id = cJSON_GetObjectItem(elem, "id");
|
||||
cJSON *name = cJSON_GetObjectItem(elem, "name");
|
||||
ESP_LOGW(TAG, "Json={'id': '%s', 'name': '%s'}", id->valuestring, name->valuestring);
|
||||
}
|
||||
cJSON_Delete(root);
|
||||
}
|
||||
|
||||
ESP_LOGW(TAG, "Total payload length=%d, data_len=%d, current payload offset=%d\r\n", data->payload_len, data->data_len, data->payload_offset);
|
||||
|
||||
xTimerReset(shutdown_signal_timer, portMAX_DELAY);
|
||||
|
@ -6,6 +6,8 @@ import string
|
||||
from threading import Event, Thread
|
||||
import pytest
|
||||
import sys
|
||||
import json
|
||||
import time
|
||||
|
||||
from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
|
||||
from pytest_embedded import Dut
|
||||
@ -81,8 +83,33 @@ def test_examples_protocol_websocket(dut):
|
||||
def test_close(dut):
|
||||
code = dut.expect(re.compile(b'WEBSOCKET: Received closed message with code=(\\d*)'))[0]
|
||||
print('Received close frame with code {}'.format(code))
|
||||
|
||||
def test_json(dut, websocket):
|
||||
json_string = """
|
||||
[
|
||||
{
|
||||
"id":"1",
|
||||
"name":"user1"
|
||||
},
|
||||
{
|
||||
"id":"2",
|
||||
"name":"user2"
|
||||
}
|
||||
]
|
||||
"""
|
||||
websocket.send_data(json_string)
|
||||
data = json.loads(json_string)
|
||||
|
||||
match = dut.expect(re.compile(b'Json=([a-zA-Z0-9]*).*')).group(0).decode()[5:]
|
||||
if match == str(data[0]):
|
||||
print('Sent message and received message are equal')
|
||||
else:
|
||||
raise ValueError('DUT received string do not match sent string, \nexpected: {}\nwith length {}\
|
||||
\nreceived: {}\nwith length {}'.format(data[0], len(data[0]), match, len(match)))
|
||||
|
||||
|
||||
def test_recv_long_msg(dut, websocket, msg_len, repeats):
|
||||
|
||||
send_msg = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(msg_len))
|
||||
|
||||
for _ in range(repeats):
|
||||
@ -121,6 +148,7 @@ def test_examples_protocol_websocket(dut):
|
||||
test_echo(dut)
|
||||
# Message length should exceed DUT's buffer size to test fragmentation, default is 1024 byte
|
||||
test_recv_long_msg(dut, ws, 2000, 3)
|
||||
test_json(dut, ws)
|
||||
test_close(dut)
|
||||
else:
|
||||
print('DUT connecting to {}'.format(uri))
|
||||
|
@ -1,4 +1,4 @@
|
||||
version: "0.0.2"
|
||||
version: "0.0.3"
|
||||
description: esp websocket client
|
||||
dependencies:
|
||||
idf:
|
||||
|
@ -10,7 +10,7 @@ if(${target} STREQUAL "linux")
|
||||
set(srcs "mdns.c" ${MDNS_NETWORKING})
|
||||
else()
|
||||
set(dependencies lwip console esp_netif)
|
||||
set(private_dependencies esp_timer)
|
||||
set(private_dependencies esp_timer esp_wifi)
|
||||
set(srcs "mdns.c" ${MDNS_NETWORKING} "mdns_console.c")
|
||||
endif()
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
set(EXTRA_COMPONENT_DIRS "../..")
|
||||
set(EXTRA_COMPONENT_DIRS "..")
|
||||
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
|
||||
list(APPEND EXTRA_COMPONENT_DIRS "../../../common_components/protocol_examples_common")
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
version: "1.0.4"
|
||||
version: "1.0.6"
|
||||
description: mDNS
|
||||
dependencies:
|
||||
idf:
|
||||
|
@ -20,6 +20,8 @@
|
||||
#if CONFIG_ETH_ENABLED
|
||||
#include "esp_eth.h"
|
||||
#endif
|
||||
#include "esp_wifi.h"
|
||||
|
||||
|
||||
#ifdef MDNS_ENABLE_DEBUG
|
||||
void mdns_debug_packet(const uint8_t * data, size_t len);
|
||||
@ -1684,7 +1686,7 @@ static void _mdns_create_answer_from_parsed_packet(mdns_parsed_packet_t *parsed_
|
||||
if (!packet) {
|
||||
return;
|
||||
}
|
||||
packet->flags = MDNS_FLAGS_AUTHORITATIVE;
|
||||
packet->flags = MDNS_FLAGS_QR_AUTHORITATIVE;
|
||||
packet->distributed = parsed_packet->distributed;
|
||||
packet->id = parsed_packet->id;
|
||||
|
||||
@ -1933,7 +1935,7 @@ static mdns_tx_packet_t * _mdns_create_announce_packet(mdns_if_t tcpip_if, mdns_
|
||||
if (!packet) {
|
||||
return NULL;
|
||||
}
|
||||
packet->flags = MDNS_FLAGS_AUTHORITATIVE;
|
||||
packet->flags = MDNS_FLAGS_QR_AUTHORITATIVE;
|
||||
|
||||
uint8_t i;
|
||||
for (i=0; i<len; i++) {
|
||||
@ -1963,7 +1965,7 @@ static mdns_tx_packet_t * _mdns_create_announce_from_probe(mdns_tx_packet_t * pr
|
||||
if (!packet) {
|
||||
return NULL;
|
||||
}
|
||||
packet->flags = MDNS_FLAGS_AUTHORITATIVE;
|
||||
packet->flags = MDNS_FLAGS_QR_AUTHORITATIVE;
|
||||
|
||||
mdns_out_answer_t * s = probe->servers;
|
||||
while (s) {
|
||||
@ -2003,7 +2005,7 @@ static void _mdns_pcb_send_bye(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protoco
|
||||
if (!packet) {
|
||||
return;
|
||||
}
|
||||
packet->flags = MDNS_FLAGS_AUTHORITATIVE;
|
||||
packet->flags = MDNS_FLAGS_QR_AUTHORITATIVE;
|
||||
size_t i;
|
||||
for (i=0; i<len; i++) {
|
||||
if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, services[i]->service, NULL, true, true)) {
|
||||
@ -3340,13 +3342,13 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
|
||||
memset(name, 0, sizeof(mdns_name_t));
|
||||
|
||||
header.id = _mdns_read_u16(data, MDNS_HEAD_ID_OFFSET);
|
||||
header.flags.value = _mdns_read_u16(data, MDNS_HEAD_FLAGS_OFFSET);
|
||||
header.flags = _mdns_read_u16(data, MDNS_HEAD_FLAGS_OFFSET);
|
||||
header.questions = _mdns_read_u16(data, MDNS_HEAD_QUESTIONS_OFFSET);
|
||||
header.answers = _mdns_read_u16(data, MDNS_HEAD_ANSWERS_OFFSET);
|
||||
header.servers = _mdns_read_u16(data, MDNS_HEAD_SERVERS_OFFSET);
|
||||
header.additional = _mdns_read_u16(data, MDNS_HEAD_ADDITIONAL_OFFSET);
|
||||
|
||||
if (header.flags.value == MDNS_FLAGS_AUTHORITATIVE && packet->src_port != MDNS_SERVICE_PORT) {
|
||||
if (header.flags == MDNS_FLAGS_QR_AUTHORITATIVE && packet->src_port != MDNS_SERVICE_PORT) {
|
||||
free(parsed_packet);
|
||||
return;
|
||||
}
|
||||
@ -3360,8 +3362,8 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
|
||||
parsed_packet->tcpip_if = packet->tcpip_if;
|
||||
parsed_packet->ip_protocol = packet->ip_protocol;
|
||||
parsed_packet->multicast = packet->multicast;
|
||||
parsed_packet->authoritative = header.flags.value == MDNS_FLAGS_AUTHORITATIVE;
|
||||
parsed_packet->distributed = header.flags.value == MDNS_FLAGS_DISTRIBUTED;
|
||||
parsed_packet->authoritative = (header.flags == MDNS_FLAGS_QR_AUTHORITATIVE);
|
||||
parsed_packet->distributed = header.flags == MDNS_FLAGS_DISTRIBUTED;
|
||||
parsed_packet->id = header.id;
|
||||
esp_netif_ip_addr_copy(&parsed_packet->src, &packet->src);
|
||||
parsed_packet->src_port = packet->src_port;
|
||||
@ -3497,7 +3499,7 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
|
||||
service = _mdns_get_service_item(name->service, name->proto, NULL);
|
||||
}
|
||||
} else {
|
||||
if (!parsed_packet->authoritative || record_type == MDNS_NS) {
|
||||
if ((header.flags & MDNS_FLAGS_QUERY_REPSONSE) == 0 || record_type == MDNS_NS) {
|
||||
//skip this record
|
||||
continue;
|
||||
}
|
||||
@ -6073,11 +6075,11 @@ void mdns_debug_packet(const uint8_t * data, size_t len)
|
||||
header.additional = _mdns_read_u16(data, MDNS_HEAD_ADDITIONAL_OFFSET);
|
||||
|
||||
_mdns_dbg_printf("%s",
|
||||
(header.flags.value == MDNS_FLAGS_AUTHORITATIVE)?"AUTHORITATIVE\n":
|
||||
(header.flags.value == MDNS_FLAGS_QR_AUTHORITATIVE)?"AUTHORITATIVE\n":
|
||||
(header.flags.value == MDNS_FLAGS_DISTRIBUTED)?"DISTRIBUTED\n":
|
||||
(header.flags.value == 0)?"\n":" "
|
||||
);
|
||||
if (header.flags.value && header.flags.value != MDNS_FLAGS_AUTHORITATIVE) {
|
||||
if (header.flags.value && header.flags.value != MDNS_FLAGS_QR_AUTHORITATIVE) {
|
||||
_mdns_dbg_printf("0x%04X\n", header.flags.value);
|
||||
}
|
||||
|
||||
|
@ -338,11 +338,19 @@ size_t _mdns_udp_pcb_write(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, c
|
||||
}
|
||||
memcpy((uint8_t *)pbt->payload, data, len);
|
||||
|
||||
ip_addr_t ip_add_copy;
|
||||
#if CONFIG_LWIP_IPV6
|
||||
ip_add_copy.type = ip->type;
|
||||
memcpy(&(ip_add_copy.u_addr), &(ip->u_addr), sizeof(ip_add_copy.u_addr));
|
||||
#else
|
||||
memcpy(&(ip_add_copy.addr), &(ip->u_addr), sizeof(ip_addr_copy.addr));
|
||||
#endif // CONFIG_LWIP_IPV6
|
||||
|
||||
mdns_api_call_t msg = {
|
||||
.tcpip_if = tcpip_if,
|
||||
.ip_protocol = ip_protocol,
|
||||
.pbt = pbt,
|
||||
.ip = (ip_addr_t *)ip,
|
||||
.ip = &ip_add_copy,
|
||||
.port = port
|
||||
};
|
||||
tcpip_api_call(_mdns_udp_pcb_write_api, &msg.call);
|
||||
|
@ -71,7 +71,9 @@
|
||||
#define MDNS_ANSWER_A_TTL 120
|
||||
#define MDNS_ANSWER_AAAA_TTL 120
|
||||
|
||||
#define MDNS_FLAGS_AUTHORITATIVE 0x8400
|
||||
#define MDNS_FLAGS_QUERY_REPSONSE 0x8000
|
||||
#define MDNS_FLAGS_AUTHORITATIVE 0x0400
|
||||
#define MDNS_FLAGS_QR_AUTHORITATIVE (MDNS_FLAGS_QUERY_REPSONSE | MDNS_FLAGS_AUTHORITATIVE)
|
||||
#define MDNS_FLAGS_DISTRIBUTED 0x0200
|
||||
|
||||
#define MDNS_NAME_REF 0xC000
|
||||
@ -211,21 +213,7 @@ typedef enum {
|
||||
|
||||
typedef struct {
|
||||
uint16_t id;
|
||||
union {
|
||||
struct {
|
||||
uint16_t qr :1;
|
||||
uint16_t opCode :4;
|
||||
uint16_t aa :1;
|
||||
uint16_t tc :1;
|
||||
uint16_t rd :1;
|
||||
uint16_t ra :1;
|
||||
uint16_t z :1;
|
||||
uint16_t ad :1;
|
||||
uint16_t cd :1;
|
||||
uint16_t rCode :4;//response/error code
|
||||
};
|
||||
uint16_t value;
|
||||
} flags;
|
||||
uint16_t flags;
|
||||
uint16_t questions; //QDCOUNT
|
||||
uint16_t answers; //ANCOUNT
|
||||
uint16_t servers; //NSCOUNT
|
||||
|
@ -1,2 +1,3 @@
|
||||
idf_component_register(SRCS "main.c" "mdns_test.c"
|
||||
INCLUDE_DIRS ".")
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")
|
||||
|
Reference in New Issue
Block a user