Compare commits

...

42 Commits

Author SHA1 Message Date
f148c98b13 ASIO: Updated package version to "1.14.1~3" 2022-10-17 09:46:10 +02:00
06103e4bb4 Merge pull request #157 from david-cermak/bugfix/modem_lambda_capture
fix(esp-modem): On read/command callback fixes
2022-10-13 21:25:04 +02:00
c6fc267e5f Merge pull request #162 from gabsuren/CI/asio_package_update
CI: Updated repo-checkout flag to consider submodules (IDFGH-8515)
2022-10-13 21:24:21 +02:00
52b7f001a2 Merge pull request #161 from espressif-abhikroy/esp_modem/power_saving_mode_commands
Quick fix esp_modem: Added modem_console commands for deep-sleep and modem PSM.
2022-10-13 21:23:28 +02:00
cf504ec900 Quick fix esp_modem: Added modem_console commands for deep-sleep and modem PSM. 2022-10-13 20:54:08 +05:30
ab2e08f96c CI: Updated checkout flag to consider submodules 2022-10-13 17:23:43 +02:00
511ed541b8 fix(esp-modem) Make simple_cmux_client example configurable
* extended simple_cmux_client

* add the Descriptions as proposed by @david-cermak

* EXAMPLE_FLOW_CONTROL

Co-authored-by: Franz Höpfinger <krone-trailer@franzhoepfinger.de>
2022-10-13 09:00:16 +02:00
8d0e2aab6b Merge pull request #155 from espressif-abhikroy/esp_modem/power_saving_mode_commands
esp_modem: Added modem_console commands for deep-sleep and modem PSM. (IDFGH-8468)
2022-10-10 19:20:44 +02:00
a02bf05eed fix(esp-modem): Uart Terminal read_cb race 2022-10-10 15:39:09 +02:00
85a2e25093 esp_modem: Added modem_console commands for deep-sleep and modem PSM. 2022-10-10 12:51:47 +05:30
a9b5e66d0f Merge pull request #89 from tore-espressif/feature/esp_modem_usb
Use USB CDC driver from component registry (IDFGH-7816)
2022-10-05 11:58:43 +02:00
988b3f9905 ci: Compile esp_modem examples with USB support 2022-10-05 11:25:20 +02:00
53b59332dc esp_modem: Expand PPPoS example with USB DTE 2022-10-05 11:25:14 +02:00
bf84ae940a esp_modem: Allow USB DTE reconnection
esp_modem_usb_dte component is fetched from IDF component registry.
2022-10-05 11:25:06 +02:00
a89a0ab7a3 esp_modem: Add ConsoleCommand destructor
Commands are registered in Constructor. But there is now of deregistering it
This is needed for correct console re-construction,
for example with USB modem replugging.
2022-10-05 11:14:42 +02:00
fe536e476c esp_modem_get_gnss_power_mode (#136)
Co-authored-by: Franz Höpfinger <krone-trailer@franzhoepfinger.de>
2022-10-05 08:54:41 +02:00
afafcb7c24 Merge pull request #149 from diplfranzhoepfinger/feature/extend_example_modem_console2
remove unused Config Parameters ! (IDFGH-8442)
2022-10-04 10:10:08 +02:00
55b4775ca8 remove unused Config Parameters ! 2022-10-04 09:30:53 +02:00
1c6c610f20 Merge pull request #146 from MicroDev1/master
Prevent crash when hostname is null (IDFGH-8356)
2022-10-01 07:47:38 +02:00
9d9d0db1ee Merge pull request #145 from david-cermak/bugfix/modem_console_flow_ctrl_config
fix(esp_modem): Console example to use configurable flow-ctrl
2022-09-30 16:56:22 +02:00
b704aaa33b Merge pull request #113 from david-cermak/feature/add_slip_example
Add protocols SLIP example
2022-09-30 16:39:30 +02:00
7a21db23ad fix(examples): Update SLIP example with error checks 2022-09-30 16:04:46 +02:00
fcd6f0bb14 feat(examples): Move the slip example to esp-netif folder
And update the slip_modem_get_ipv6_address() to return the address by
copy instead of reference (to employ return value optimizaiton where
possible)
2022-09-30 14:46:13 +02:00
52d7458b99 esp_netif: Migrate SLIP interface to user-space
* Original commit: espressif/esp-idf@83b8556f10
2022-09-30 14:46:13 +02:00
56cb58ced7 build system: re-add -Wno-format as private flag for some example components
* Original commit: espressif/esp-idf@e596c84d49
2022-09-30 14:46:13 +02:00
77a7538a02 docs: changes docs supported targets tables
* Original commit: espressif/esp-idf@c0568611dd
2022-09-30 14:46:13 +02:00
c8d0a13f0e ci: partially enable example build for esp32c2
* Original commit: espressif/esp-idf@f7be540222
2022-09-30 14:46:13 +02:00
582f5b5ff6 tools: Increase the minimal supported CMake version to 3.16
This updates the minimal supported version of CMake to 3.16, which in turn enables us to use more CMake features and have a cleaner build system.
This is the version that provides most new features and also the one we use in our latest docker image for CI.


* Original commit: espressif/esp-idf@facab8c5a7
2022-09-30 14:46:13 +02:00
06dd536d52 G0: target component (components/esp32*) doesn't depend on driver anymore
* Original commit: espressif/esp-idf@2571aaf3c9
2022-09-30 14:46:13 +02:00
e66b19c414 freertos: Remove legacy data types
This commit removes the usage of all legacy FreeRTOS data types that
are exposed via configENABLE_BACKWARD_COMPATIBILITY. Legacy types can
still be used by enabling CONFIG_FREERTOS_ENABLE_BACKWARD_COMPATIBILITY.


* Original commit: espressif/esp-idf@57fd78f5ba
2022-09-30 14:46:13 +02:00
0ad6cad8b2 Build & config: Remove leftover files from the unsupported "make" build system
* Original commit: espressif/esp-idf@766aa57084
2022-09-30 14:46:13 +02:00
ffa13e4b90 bugfix for ipv6_address_value_issue
Closes https://github.com/espressif/esp-idf/issues/5663


* Original commit: espressif/esp-idf@74236f0b29
2022-09-30 14:46:13 +02:00
0fc1fc6b23 Whitespace: Automated whitespace fixes (large commit)
Apply the pre-commit hook whitespace fixes to all files in the repo.

(Line endings, blank lines at end of file, trailing whitespace)


* Original commit: espressif/esp-idf@66fb5a29bb
2022-09-30 14:46:13 +02:00
f1d1d79daf esp-netif: Fix SLIP interface to start with correct IPv6 addr
Merges https://github.com/espressif/esp-idf/pull/4985


* Original commit: espressif/esp-idf@5dae28069f
2022-09-30 14:46:13 +02:00
36d0d32b35 esp-netif: removing SLIP related events as the slip-modem set state synchronously
* Original commit: espressif/esp-idf@bb9a7356ac
2022-09-30 14:46:13 +02:00
3bf488eb86 esp-netif: SLIP interface refactor to isolate interface from drivers
* Original commit: espressif/esp-idf@1a41545c3e
2022-09-30 14:46:13 +02:00
061885ad23 esp-netif: Added esp_netif slip support, slip_modem component and example
Merges https://github.com/espressif/esp-idf/pull/4985


* Original commit: espressif/esp-idf@266be00254
2022-09-30 14:46:13 +02:00
ab70791625 Merge pull request #147 from tore-espressif/feature/esp_modem/c_error
esp_modem: Allow C API extensions (IDFGH-8368)
2022-09-30 14:25:21 +02:00
62cb235aa9 esp_modem: Allow error handler register in C
C++ API offers interface to register error callback.
C API isn't flexible enough, so we offer conversion function between C++ and C enums.
2022-09-30 11:55:09 +02:00
3498e86d8b prevent crash when hostname is null 2022-09-21 21:03:07 +05:30
07a347f907 fix(esp_modem): Fix CRLF endlines in examples 2022-09-20 08:20:05 +02:00
d9c9681094 fix(esp_modem): Console example to use configurable flow-ctrl 2022-09-20 08:18:53 +02:00
47 changed files with 1711 additions and 640 deletions

View File

@ -18,6 +18,7 @@ jobs:
with:
persist-credentials: false
fetch-depth: 0
submodules: recursive
- name: Generate docs
run: |

View File

@ -35,6 +35,28 @@ jobs:
cd $GITHUB_WORKSPACE/esp-protocols/components/esp_modem/examples/${{ matrix.example }}
idf.py build
build_esp_modem_usb:
strategy:
matrix:
idf_ver: ["latest", "release-v4.4", "release-v5.0"]
example: ["modem_console", "pppos_client"]
idf_target: ["esp32s2", "esp32s3"]
runs-on: ubuntu-20.04
container: espressif/idf:${{ matrix.idf_ver }}
steps:
- name: Checkout esp-protocols
uses: actions/checkout@v3
- name: Build ${{ matrix.example }} with IDF-${{ matrix.idf_ver }} for ${{ matrix.idf_target }}
working-directory: components/esp_modem/examples/${{ matrix.example }}
env:
IDF_TARGET: ${{ matrix.idf_target }}
shell: bash
run: |
. ${IDF_PATH}/export.sh
cat sdkconfig.ci.usb >> sdkconfig.defaults
idf.py build
build_mdns:
strategy:
matrix:

View File

@ -1,4 +1,4 @@
version: "1.0.2"
version: "1.14.1~3"
description: ASIO
dependencies:
idf:

View File

@ -90,4 +90,7 @@ build
dependencies.lock
# ignore generated docs
docs/html
docs/html
# esp-idf managed components
**/managed_components/**

View File

@ -2,15 +2,5 @@
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
set(EXTRA_COMPONENT_DIRS "../..")
include($ENV{IDF_PATH}/tools/cmake/version.cmake)
if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER_EQUAL "4.4")
if(("${IDF_TARGET}" STREQUAL "esp32s2") OR ("${IDF_TARGET}" STREQUAL "esp32s3"))
list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/examples/peripherals/usb/host/cdc/common")
endif()
endif()
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(modem-console)

View File

@ -20,7 +20,11 @@ For USB enabled targets (ESP32-S2 and ESP32-S3), it is possible to connect to th
1. In menuconfig, navigate to `Example Configuration->Type of serial connection to the modem` and choose `USB`.
2. Connect the modem USB signals to pin 19 (DATA-) and 20 (DATA+) on your ESP chip.
USB example uses Quactel BG96 modem device.
USB example uses Quactel BG96 modem device. BG96 needs a positive pulse on its PWK pin to boot-up.
This example supports USB modem hot-plugging and reconnection. There is one limitation coming from esp_console component:
When esp_console REPL is being destroyed (after USB mode disconnection or after `exit` command), it will block on UART read.
You must send a character to it (via idf.py monitor), so it unblocks and properly exits.
### Supported IDF versions

View File

@ -1,19 +0,0 @@
include($ENV{IDF_PATH}/tools/cmake/version.cmake)
idf_build_get_property(target IDF_TARGET)
# USB is supported on S2 and S3 targets and IDF version >= 4.4
if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER_EQUAL "4.4")
if(("${target}" STREQUAL "esp32s2") OR ("${target}" STREQUAL "esp32s3"))
idf_component_register(SRCS "esp_modem_usb.cpp" "esp_modem_usb_api_target.cpp"
REQUIRES cdc_acm_host esp_modem
REQUIRED_IDF_TARGETS esp32s2 esp32s3
PRIV_INCLUDE_DIRS "private_include"
INCLUDE_DIRS "include")
set_target_properties(${COMPONENT_LIB} PROPERTIES
CXX_STANDARD 17
)
endif()
endif()

View File

@ -1,191 +0,0 @@
// Copyright 2021 Espressif Systems (Shanghai) CO LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_log.h"
#include "esp_modem_config.h"
#include "esp_modem_usb_config.h"
#include "cxx_include/esp_modem_dte.hpp"
#include "usb/usb_host.h"
#include "usb/cdc_acm_host.h"
#include "sdkconfig.h"
#include "usb_terminal.hpp"
static const char *TAG = "usb_terminal";
/**
* @brief USB Host task
*
* This task is created only if install_usb_host is set to true in DTE configuration.
* In case the user doesn't want in install USB Host driver here, he must install it before creating UsbTerminal object.
*
* @param arg Unused
*/
void usb_host_task(void *arg)
{
while (1) {
// Start handling system events
uint32_t event_flags;
usb_host_lib_handle_events(portMAX_DELAY, &event_flags);
if (event_flags & USB_HOST_LIB_EVENT_FLAGS_NO_CLIENTS) {
ESP_LOGD(TAG, "No more clients: clean up\n");
usb_host_device_free_all();
}
if (event_flags & USB_HOST_LIB_EVENT_FLAGS_ALL_FREE) {
ESP_LOGD(TAG, "All free: uninstall USB lib\n");
break;
}
}
// Clean up USB Host
vTaskDelay(10); // Short delay to allow clients clean-up
usb_host_lib_handle_events(0, NULL); // Make sure there are now pending events
usb_host_uninstall();
vTaskDelete(NULL);
}
namespace esp_modem {
class UsbTerminal : public Terminal, private CdcAcmDevice {
public:
explicit UsbTerminal(const esp_modem_dte_config *config)
{
const struct esp_modem_usb_term_config* usb_config = (struct esp_modem_usb_term_config*)(config->extension_config);
// Install USB Host driver
if (usb_config->install_usb_host) {
const usb_host_config_t host_config = {
.skip_phy_setup = false,
.intr_flags = ESP_INTR_FLAG_LEVEL1,
};
ESP_MODEM_THROW_IF_ERROR(usb_host_install(&host_config), "USB Host install failed");
ESP_LOGD(TAG, "USB Host installed");
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
const cdc_acm_host_driver_config_t esp_modem_cdc_acm_driver_config = {
.driver_task_stack_size = config->task_stack_size,
.driver_task_priority = config->task_priority,
.xCoreID = (BaseType_t)usb_config->xCoreID
};
// Silently continue of error: CDC-ACM driver might be already installed
cdc_acm_host_install(&esp_modem_cdc_acm_driver_config);
// Open CDC-ACM device
const cdc_acm_host_device_config_t esp_modem_cdc_acm_device_config = {
.connection_timeout_ms = usb_config->timeout_ms,
.out_buffer_size = config->dte_buffer_size,
.event_cb = handle_notif,
.data_cb = handle_rx,
.user_arg = this
};
if (usb_config->cdc_compliant) {
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 {
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");
}
};
~UsbTerminal()
{
this->CdcAcmDevice::close();
};
void start() override
{
return;
}
void stop() override
{
return;
}
int write(uint8_t *data, size_t len) override
{
ESP_LOG_BUFFER_HEXDUMP(TAG, data, len, ESP_LOG_DEBUG);
if (this->CdcAcmDevice::tx_blocking(data, len) != ESP_OK) {
return -1;
}
return len;
}
int read(uint8_t *data, size_t len) override
{
// This function should never be called. UsbTerminal provides data through Terminal::on_read callback
ESP_LOGW(TAG, "Unexpected call to UsbTerminal::read function");
return -1;
}
private:
UsbTerminal() = delete;
UsbTerminal(const UsbTerminal &copy) = delete;
UsbTerminal &operator=(const UsbTerminal &copy) = delete;
bool operator== (const UsbTerminal &param) const = delete;
bool operator!= (const UsbTerminal &param) const = delete;
static void handle_rx(uint8_t *data, size_t data_len, void *user_arg)
{
ESP_LOG_BUFFER_HEXDUMP(TAG, data, data_len, ESP_LOG_DEBUG);
UsbTerminal *this_terminal = static_cast<UsbTerminal *>(user_arg);
if (data_len > 0 && this_terminal->on_read) {
this_terminal->on_read(data, data_len);
} else {
ESP_LOGD(TAG, "Unhandled RX data");
}
}
static void handle_notif(cdc_acm_dev_hdl_t cdc_hdl, const cdc_acm_host_dev_event_data_t *event, void *user_ctx)
{
UsbTerminal *this_terminal = static_cast<UsbTerminal *>(user_ctx);
switch (event->type) {
// Notifications like Ring, Rx Carrier indication or Network connection indication are not relevant for USB terminal
case CDC_ACM_HOST_NETWORK_CONNECTION:
case CDC_ACM_HOST_SERIAL_STATE:
break;
case CDC_ACM_HOST_DEVICE_DISCONNECTED:
ESP_LOGW(TAG, "USB terminal disconnected");
cdc_acm_host_close(cdc_hdl);
if (this_terminal->on_error) {
this_terminal->on_error(terminal_error::UNEXPECTED_CONTROL_FLOW);
}
break;
case CDC_ACM_HOST_ERROR:
ESP_LOGE(TAG, "Unexpected CDC-ACM error: %d.", event->data.error);
if (this_terminal->on_error) {
this_terminal->on_error(terminal_error::UNEXPECTED_CONTROL_FLOW);
}
break;
default:
abort();
}
};
};
std::unique_ptr<Terminal> create_usb_terminal(const esp_modem_dte_config *config)
{
TRY_CATCH_RET_NULL(
return std::make_unique<UsbTerminal>(config);
)
}
} // namespace esp_modem

View File

@ -1,32 +0,0 @@
// Copyright 2021-2022 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "usb_terminal.hpp"
#include "cxx_include/esp_modem_api.hpp"
#include "cxx_include/esp_modem_netif.hpp"
#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
static const char *TAG = "modem_usb_api_target";
#endif
namespace esp_modem {
std::shared_ptr<DTE> create_usb_dte(const dte_config *config)
{
TRY_CATCH_RET_NULL(
auto term = create_usb_terminal(config);
return std::make_shared<DTE>(config, std::move(term));
)
}
}

View File

@ -1,29 +0,0 @@
// Copyright 2021-2022 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "cxx_include/esp_modem_dce.hpp"
#include "cxx_include/esp_modem_dce_module.hpp"
namespace esp_modem {
/**
* @brief Create USB DTE
*
* @param config DTE configuration
* @return shared ptr to DTE on success
* nullptr on failure (either due to insufficient memory or wrong dte configuration)
* if exceptions are disabled the API abort()'s on error
*/
std::shared_ptr<DTE> create_usb_dte(const dte_config *config);
}

View File

@ -1,63 +0,0 @@
// Copyright 2021-2022 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <stdint.h>
#include <stdbool.h>
/**
* @brief USB configuration structure
* @see USB host CDC-ACM driver documentation for details about interfaces settings
*/
struct esp_modem_usb_term_config {
uint16_t vid; /*!< Vendor ID of the USB device */
uint16_t pid; /*!< Product ID of the USB device */
int interface_idx; /*!< USB Interface index that will be used */
uint32_t timeout_ms; /*!< Time for a USB modem to connect to USB host. 0 means wait forever. */
int xCoreID; /*!< Core affinity of created tasks: CDC-ACM driver task and optional USB Host task */
bool cdc_compliant; /*!< Treat the USB device as CDC-compliant. Read CDC-ACM driver documentation for more details */
bool install_usb_host; /*!< Flag whether USB Host driver should be installed */
};
/**
* @brief ESP Mode USB DTE Default Configuration
*
* @param[in] _usb_config esp_modem_usb_term_config configuration structure. Can be obtained by ESP_MODEM_DEFAULT_USB_CONFIG
*
*/
#define ESP_MODEM_DTE_DEFAULT_USB_CONFIG(_usb_config) \
{ \
.dte_buffer_size = 512, \
.task_stack_size = 4096, \
.task_priority = 5, \
.extension_config = &_usb_config \
}
/**
* @brief ESP Modem USB Default Configuration
*
* @param[in] _vid USB Vendor ID
* @param[in] _pid USB Product ID
* @see USB host CDC-ACM driver documentation for details about interfaces settings
*/
#define ESP_MODEM_DEFAULT_USB_CONFIG(_vid, _pid) \
{ \
.vid = _vid, \
.pid = _pid, \
.interface_idx = 0, \
.timeout_ms = 0, \
.xCoreID = 0, \
.cdc_compliant = false, \
.install_usb_host = true \
}

View File

@ -1,51 +0,0 @@
// Copyright 2021 Espressif Systems (Shanghai) CO LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "cxx_include/esp_modem_dte.hpp"
#include "sdkconfig.h"
#include "esp_log.h"
struct esp_modem_dte_config;
// Copy-pasted from esp_modem/private_include/exception_stub.hpp
#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
#define TRY_CATCH_OR_DO(block, action) \
try { block \
} catch (std::bad_alloc& e) { \
ESP_LOGE(TAG, "Out of memory"); \
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", e.what()); \
action; \
}
#define TRY_CATCH_RET_NULL(block) TRY_CATCH_OR_DO(block, return nullptr)
#else
#define TRY_CATCH_OR_DO(block, action) \
block
#define TRY_CATCH_RET_NULL(block) \
block
#endif
namespace esp_modem {
std::unique_ptr<Terminal> create_usb_terminal(const esp_modem_dte_config *config);
} // namespace esp_modem

View File

@ -2,6 +2,6 @@ idf_component_register(SRCS "modem_console_main.cpp"
"console_helper.cpp"
"httpget_handle.c"
"ping_handle.c"
REQUIRES console esp_http_client nvs_flash esp_modem esp_modem_usb_dte
REQUIRES console esp_http_client nvs_flash
INCLUDE_DIRS ".")
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")

View File

@ -1,176 +1,172 @@
menu "Example Configuration"
choice EXAMPLE_SERIAL_CONFIG
prompt "Type of serial connection to the modem"
default EXAMPLE_SERIAL_CONFIG_UART
config EXAMPLE_SERIAL_CONFIG_UART
bool "UART"
help
Connect to modem via UART.
config EXAMPLE_SERIAL_CONFIG_USB
bool "USB"
depends on IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
help
Connect to modem via USB (CDC-ACM class). For IDF version >= 4.4.
endchoice
choice EXAMPLE_MODEM_DEVICE
prompt "Choose supported modem device (DCE)"
default EXAMPLE_MODEM_DEVICE_BG96
help
Select modem device connected to the ESP DTE.
config EXAMPLE_MODEM_DEVICE_SHINY
bool "SHINY"
help
SHINY is a GSM/GPRS module.
It supports SHINY.
config EXAMPLE_MODEM_DEVICE_SIM800
bool "SIM800"
help
SIMCom SIM800L is a GSM/GPRS module.
It supports Quad-band 850/900/1800/1900MHz.
config EXAMPLE_MODEM_DEVICE_BG96
bool "BG96"
help
Quectel BG96 is a series of LTE Cat M1/Cat NB1/EGPRS module.
config EXAMPLE_MODEM_DEVICE_SIM7000
bool "SIM7000"
help
SIM7000 is a Multi-Band LTE-FDD and GSM/GPRS/EDGE module.
config EXAMPLE_MODEM_DEVICE_SIM7070
bool "SIM7070"
help
SIM7070 is Multi-Band CAT M and NB IoT module.
config EXAMPLE_MODEM_DEVICE_SIM7600
bool "SIM7600"
help
SIM7600 is a Multi-Band LTE-TDD/LTE-FDD/HSPA+ and GSM/GPRS/EDGE module.
endchoice
config EXAMPLE_MODEM_PPP_APN
string "Set MODEM APN"
default "internet"
help
Set APN (Access Point Name), a logical name to choose data network
config EXAMPLE_MODEM_PPP_AUTH_USERNAME
string "Set username for authentication"
default "espressif"
depends on !EXAMPLE_MODEM_PPP_AUTH_NONE
help
Set username for PPP Authentication.
config EXAMPLE_MODEM_PPP_AUTH_PASSWORD
string "Set password for authentication"
default "esp32"
depends on !EXAMPLE_MODEM_PPP_AUTH_NONE
help
Set password for PPP Authentication.
config EXAMPLE_MODEM_PPP_AUTH_NONE
bool "Skip PPP authentication"
default n
help
Set to true for the PPP client to skip authentication
config EXAMPLE_SEND_MSG
bool "Short message (SMS)"
default n
help
Select this, the modem will send a short message before power off.
if EXAMPLE_SEND_MSG
config EXAMPLE_SEND_MSG_PEER_PHONE_NUMBER
string "Peer Phone Number (with area code)"
default "+8610086"
help
Enter the peer phone number that you want to send message to.
endif
config EXAMPLE_NEED_SIM_PIN
bool "SIM PIN needed"
default n
help
Enable to set SIM PIN before starting the example
config EXAMPLE_SIM_PIN
string "Set SIM PIN"
default "1234"
depends on EXAMPLE_NEED_SIM_PIN
help
Pin to unlock the SIM
menu "UART Configuration"
config EXAMPLE_MODEM_UART_TX_PIN
int "TXD Pin Number"
default 25
range 0 31
help
Pin number of UART TX.
config EXAMPLE_MODEM_UART_RX_PIN
int "RXD Pin Number"
default 26
range 0 31
help
Pin number of UART RX.
config EXAMPLE_MODEM_UART_RTS_PIN
int "RTS Pin Number"
default 27
range 0 31
help
Pin number of UART RTS.
config EXAMPLE_MODEM_UART_CTS_PIN
int "CTS Pin Number"
default 23
range 0 31
help
Pin number of UART CTS.
config EXAMPLE_MODEM_UART_EVENT_TASK_STACK_SIZE
int "UART Event Task Stack Size"
range 2000 6000
default 2048
help
Stack size of UART event task.
config EXAMPLE_MODEM_UART_EVENT_TASK_PRIORITY
int "UART Event Task Priority"
range 3 22
default 5
help
Priority of UART event task.
config EXAMPLE_MODEM_UART_EVENT_QUEUE_SIZE
int "UART Event Queue Size"
range 10 40
default 30
help
Length of UART event queue.
config EXAMPLE_MODEM_UART_PATTERN_QUEUE_SIZE
int "UART Pattern Queue Size"
range 10 40
default 20
help
Length of UART pattern queue.
config EXAMPLE_MODEM_UART_TX_BUFFER_SIZE
int "UART TX Buffer Size"
range 256 2048
default 512
help
Buffer size of UART TX buffer.
config EXAMPLE_MODEM_UART_RX_BUFFER_SIZE
int "UART RX Buffer Size"
range 256 2048
default 1024
help
Buffer size of UART RX buffer.
endmenu
endmenu
menu "Example Configuration"
choice EXAMPLE_SERIAL_CONFIG
prompt "Type of serial connection to the modem"
default EXAMPLE_SERIAL_CONFIG_UART
config EXAMPLE_SERIAL_CONFIG_UART
bool "UART"
help
Connect to modem via UART.
config EXAMPLE_SERIAL_CONFIG_USB
bool "USB"
depends on IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
help
Connect to modem via USB (CDC-ACM class). For IDF version >= 4.4.
endchoice
choice EXAMPLE_MODEM_DEVICE
depends on EXAMPLE_SERIAL_CONFIG_UART
prompt "Choose supported modem device (DCE)"
default EXAMPLE_MODEM_DEVICE_BG96
help
Select modem device connected to the ESP DTE.
config EXAMPLE_MODEM_DEVICE_SHINY
bool "SHINY"
help
SHINY is a GSM/GPRS module.
It supports SHINY.
config EXAMPLE_MODEM_DEVICE_SIM800
bool "SIM800"
help
SIMCom SIM800L is a GSM/GPRS module.
It supports Quad-band 850/900/1800/1900MHz.
config EXAMPLE_MODEM_DEVICE_BG96
bool "BG96"
help
Quectel BG96 is a series of LTE Cat M1/Cat NB1/EGPRS module.
config EXAMPLE_MODEM_DEVICE_SIM7000
bool "SIM7000"
help
SIM7000 is a Multi-Band LTE-FDD and GSM/GPRS/EDGE module.
config EXAMPLE_MODEM_DEVICE_SIM7070
bool "SIM7070"
help
SIM7070 is Multi-Band CAT M and NB IoT module.
config EXAMPLE_MODEM_DEVICE_SIM7600
bool "SIM7600"
help
SIM7600 is a Multi-Band LTE-TDD/LTE-FDD/HSPA+ and GSM/GPRS/EDGE module.
endchoice
config EXAMPLE_MODEM_PPP_APN
string "Set MODEM APN"
default "internet"
help
Set APN (Access Point Name), a logical name to choose data network
config EXAMPLE_MODEM_PPP_AUTH_USERNAME
string "Set username for authentication"
default "espressif"
depends on !EXAMPLE_MODEM_PPP_AUTH_NONE
help
Set username for PPP Authentication.
config EXAMPLE_MODEM_PPP_AUTH_PASSWORD
string "Set password for authentication"
default "esp32"
depends on !EXAMPLE_MODEM_PPP_AUTH_NONE
help
Set password for PPP Authentication.
config EXAMPLE_MODEM_PPP_AUTH_NONE
bool "Skip PPP authentication"
default n
help
Set to true for the PPP client to skip authentication
menu "UART Configuration"
depends on EXAMPLE_SERIAL_CONFIG_UART
choice EXAMPLE_FLOW_CONTROL
bool "Set preferred modem control flow"
default EXAMPLE_FLOW_CONTROL_NONE
help
Set the modem's preferred control flow
config EXAMPLE_FLOW_CONTROL_NONE
bool "No control flow"
config EXAMPLE_FLOW_CONTROL_SW
bool "SW control flow"
config EXAMPLE_FLOW_CONTROL_HW
bool "HW control flow"
endchoice
config EXAMPLE_MODEM_UART_TX_PIN
int "TXD Pin Number"
default 25
range 0 31
help
Pin number of UART TX.
config EXAMPLE_MODEM_UART_RX_PIN
int "RXD Pin Number"
default 26
range 0 31
help
Pin number of UART RX.
config EXAMPLE_MODEM_UART_RTS_PIN
int "RTS Pin Number"
default 27
range 0 31
help
Pin number of UART RTS.
config EXAMPLE_MODEM_UART_CTS_PIN
int "CTS Pin Number"
default 23
range 0 31
help
Pin number of UART CTS.
config EXAMPLE_MODEM_UART_EVENT_TASK_STACK_SIZE
int "UART Event Task Stack Size"
range 2000 6000
default 4096
help
Stack size of UART event task.
config EXAMPLE_MODEM_UART_EVENT_TASK_PRIORITY
int "UART Event Task Priority"
range 3 22
default 5
help
Priority of UART event task.
config EXAMPLE_MODEM_UART_EVENT_QUEUE_SIZE
int "UART Event Queue Size"
range 10 40
default 30
help
Length of UART event queue.
config EXAMPLE_MODEM_UART_PATTERN_QUEUE_SIZE
int "UART Pattern Queue Size"
range 10 40
default 20
help
Length of UART pattern queue.
config EXAMPLE_MODEM_UART_TX_BUFFER_SIZE
int "UART TX Buffer Size"
range 256 2048
default 512
help
Buffer size of UART TX buffer.
config EXAMPLE_MODEM_UART_RX_BUFFER_SIZE
int "UART RX Buffer Size"
range 256 2048
default 1024
help
Buffer size of UART RX buffer.
endmenu
config EXAMPLE_MODEM_PWRKEY_PIN
int "PWRKEY Pin Number"
default 18
range 0 31
help
Pin number connected to modem's power key pin.
endmenu

View File

@ -18,6 +18,16 @@ ConsoleCommand::ConsoleCommand(const char *command, const char *help, const std:
RegisterCommand(command, help, args);
}
ConsoleCommand::~ConsoleCommand()
{
// Find this command in static list of commands and remove it
auto cmd = std::find(console_commands.begin(), console_commands.end(), this);
if (cmd != console_commands.end()) {
console_commands.erase(cmd);
last_command--;
}
}
void ConsoleCommand::RegisterCommand(const char *command, const char *help, const std::vector<CommandArgs> &args)
{
assert(last_command <= MAX_REPEAT_NR);

View File

@ -92,6 +92,11 @@ public:
*/
explicit ConsoleCommand(const char *command, const char *help, const std::vector<CommandArgs> &args, std::function<bool(ConsoleCommand *)> f);
/**
* @brief Destructor of ConsoleCommand
*/
~ConsoleCommand();
/**
* @brief Utility getters of various params from the argument list
*/

View File

@ -0,0 +1,12 @@
## IDF Component Manager Manifest File
dependencies:
## Required IDF version
idf: ">=4.1.0"
espressif/esp_modem:
version: "^0.1.20"
override_path: "../../../"
espressif/esp_modem_usb_dte:
version: "^1.0.0"
rules:
- if: "idf_version >=4.4"
- if: "target in [esp32s2, esp32s3]"

View File

@ -15,10 +15,12 @@
#include "esp_console.h"
#include "esp_event.h"
#include "nvs_flash.h"
#include "esp_sleep.h"
#include "driver/gpio.h"
#include "cxx_include/esp_modem_dte.hpp"
#include "esp_modem_config.h"
#include "cxx_include/esp_modem_api.hpp"
#if defined(CONFIG_USB_OTG_SUPPORTED)
#if defined(CONFIG_EXAMPLE_SERIAL_CONFIG_USB)
#include "esp_modem_usb_config.h"
#include "cxx_include/esp_modem_usb_api.hpp"
#endif
@ -26,6 +28,14 @@
#include "console_helper.hpp"
#include "my_module_dce.hpp"
#if defined(CONFIG_EXAMPLE_FLOW_CONTROL_NONE)
#define EXAMPLE_FLOW_CONTROL ESP_MODEM_FLOW_CONTROL_NONE
#elif defined(CONFIG_EXAMPLE_FLOW_CONTROL_SW)
#define EXAMPLE_FLOW_CONTROL ESP_MODEM_FLOW_CONTROL_SW
#elif defined(CONFIG_EXAMPLE_FLOW_CONTROL_HW)
#define EXAMPLE_FLOW_CONTROL ESP_MODEM_FLOW_CONTROL_HW
#endif
#define CHECK_ERR(cmd, success_action) do { \
auto err = cmd; \
if (err == command_result::OK) { \
@ -33,7 +43,7 @@
return 0; \
} else { \
ESP_LOGE(TAG, "Failed with %s", err == command_result::TIMEOUT ? "TIMEOUT":"ERROR"); \
return 1; \
return 1; \
} } while (0)
/**
@ -41,6 +51,9 @@
*/
#define DEFAULT_APN "my_apn"
#define GPIO_OUTPUT_PWRKEY (gpio_num_t)CONFIG_EXAMPLE_MODEM_PWRKEY_PIN
#define GPIO_OUTPUT_PIN_SEL (1ULL<<GPIO_OUTPUT_PWRKEY)
extern "C" void modem_console_register_http(void);
extern "C" void modem_console_register_ping(void);
@ -48,10 +61,40 @@ static const char *TAG = "modem_console";
static esp_console_repl_t *s_repl = nullptr;
using namespace esp_modem;
static SignalGroup exit_signal;
void config_gpio(void)
{
gpio_config_t io_conf = {}; //zero-initialize the config structure.
io_conf.intr_type = GPIO_INTR_DISABLE; //disable interrupt
io_conf.mode = GPIO_MODE_OUTPUT; //set as output mode
io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL; //bit mask of the pins that you want to set,e.g.GPIO18/19
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; //disable pull-down mode
io_conf.pull_up_en = GPIO_PULLUP_DISABLE; //disable pull-up mode
gpio_config(&io_conf); //configure GPIO with the given settings
}
void wakeup_modem(void)
{
/* Power on the modem */
ESP_LOGI(TAG, "Power on the modem");
gpio_set_level(GPIO_OUTPUT_PWRKEY, 1);
vTaskDelay(pdMS_TO_TICKS(1000));
gpio_set_level(GPIO_OUTPUT_PWRKEY, 0);
vTaskDelay(pdMS_TO_TICKS(2000));
}
extern "C" void app_main(void)
{
static RTC_RODATA_ATTR char apn_rtc[20] = DEFAULT_APN;
static RTC_DATA_ATTR modem_mode mode_rtc = esp_modem::modem_mode::COMMAND_MODE;
config_gpio();
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
@ -69,7 +112,7 @@ extern "C" void app_main(void)
dte_config.uart_config.rx_io_num = CONFIG_EXAMPLE_MODEM_UART_RX_PIN;
dte_config.uart_config.rts_io_num = CONFIG_EXAMPLE_MODEM_UART_RTS_PIN;
dte_config.uart_config.cts_io_num = CONFIG_EXAMPLE_MODEM_UART_CTS_PIN;
dte_config.uart_config.flow_control = ESP_MODEM_FLOW_CONTROL_HW;
dte_config.uart_config.flow_control = EXAMPLE_FLOW_CONTROL;
dte_config.uart_config.rx_buffer_size = CONFIG_EXAMPLE_MODEM_UART_RX_BUFFER_SIZE;
dte_config.uart_config.tx_buffer_size = CONFIG_EXAMPLE_MODEM_UART_TX_BUFFER_SIZE;
dte_config.uart_config.event_queue_size = CONFIG_EXAMPLE_MODEM_UART_EVENT_QUEUE_SIZE;
@ -103,21 +146,27 @@ extern "C" void app_main(void)
#elif defined(CONFIG_EXAMPLE_SERIAL_CONFIG_USB)
struct esp_modem_usb_term_config usb_config = ESP_MODEM_DEFAULT_USB_CONFIG(0x2C7C, 0x0296); // VID and PID of BG96 modem
// BG96 modem implements Vendor Specific class, that is CDC-ACM like. Interface for AT commands has index no. 2.
usb_config.interface_idx = 2;
esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_USB_CONFIG(usb_config);
ESP_LOGI(TAG, "Waiting for USB device connection...");
auto dte = create_usb_dte(&dte_config);
std::unique_ptr<DCE> dce = create_BG96_dce(&dce_config, dte, esp_netif);
while (1) {
exit_signal.clear(1);
struct esp_modem_usb_term_config usb_config = ESP_MODEM_DEFAULT_USB_CONFIG(0x2C7C, 0x0296, 2); // VID, PID and interface num of BG96 modem
const esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_USB_CONFIG(usb_config);
ESP_LOGI(TAG, "Waiting for USB device connection...");
auto dte = create_usb_dte(&dte_config);
dte->set_error_cb([&](terminal_error err) {
ESP_LOGI(TAG, "error handler %d", err);
if (err == terminal_error::DEVICE_GONE) {
exit_signal.set(1);
}
});
std::unique_ptr<DCE> dce = create_BG96_dce(&dce_config, dte, esp_netif);
#else
#error Invalid serial connection to modem.
#endif
assert(dce != nullptr);
if(dte_config.uart_config.flow_control == ESP_MODEM_FLOW_CONTROL_HW) {
if (dte_config.uart_config.flow_control == ESP_MODEM_FLOW_CONTROL_HW) {
if (command_result::OK != dce->set_flow_control(2, 2)) {
ESP_LOGE(TAG, "Failed to set the set_flow_control mode");
return;
@ -130,6 +179,26 @@ extern "C" void app_main(void)
esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart_config, &repl_config, &s_repl));
switch (esp_sleep_get_wakeup_cause()) {
case ESP_SLEEP_WAKEUP_TIMER:
if (esp_modem::modem_mode::CMUX_MODE == mode_rtc) {
ESP_LOGI(TAG, "Deep sleep reset\n");
/* Set APN */
auto new_pdp = std::unique_ptr<PdpContext>(new PdpContext(apn_rtc));
dce->get_module()->configure_pdp_context(std::move(new_pdp));
/* Set CMUX mode */
if (!dce->set_mode(mode_rtc)) {
ESP_LOGE(TAG, "Failed to set the desired mode");
}
}
break;
case ESP_SLEEP_WAKEUP_UNDEFINED:
default:
ESP_LOGD(TAG, "Not a deep sleep reset\n");
}
modem_console_register_http();
modem_console_register_ping();
const struct SetModeArgs {
@ -145,7 +214,10 @@ extern "C" void app_main(void)
} else if (mode == "PPP") {
dev_mode = esp_modem::modem_mode::DATA_MODE;
} else if (mode == "CMUX") {
/* Even if switching to CMUX fails, we make the DTE multiplex terminal.
(This is potentially a bug and might be fixed eventually) */
dev_mode = esp_modem::modem_mode::CMUX_MODE;
mode_rtc = dev_mode;
} else {
ESP_LOGE(TAG, "Unsupported mode: %s", mode.c_str());
return 1;
@ -155,6 +227,8 @@ extern "C" void app_main(void)
ESP_LOGE(TAG, "Failed to set the desired mode");
return 1;
}
mode_rtc = dev_mode;
ESP_LOGI(TAG, "OK");
}
return 0;
@ -262,6 +336,7 @@ extern "C" void app_main(void)
if (c->get_count_of(&SetApn::apn)) {
auto apn = c->get_string_of(&SetApn::apn);
ESP_LOGI(TAG, "Setting the APN=%s...", apn.c_str());
strcpy(apn_rtc, apn.c_str());
auto new_pdp = std::unique_ptr<PdpContext>(new PdpContext(apn));
dce->get_module()->configure_pdp_context(std::move(new_pdp));
ESP_LOGI(TAG, "OK");
@ -269,16 +344,96 @@ extern "C" void app_main(void)
return 0;
});
SignalGroup exit_signal;
const ConsoleCommand ExitConsole("exit", "exit the console application", no_args, [&](ConsoleCommand * c) {
ESP_LOGI(TAG, "Exiting...");
exit_signal.set(1);
s_repl->del(s_repl);
return 0;
});
/* Put the esp32 into deep sleep */
const struct DeepSleepArgs {
DeepSleepArgs(): timeout(INT1, nullptr, nullptr, "<tout>", "TIMEOUT") {}
CommandArgs timeout;
} deep_sleep_args;
const ConsoleCommand SetDeepSleep("set_deep_sleep", "Put esp32 to deep sleep", &deep_sleep_args, sizeof(deep_sleep_args), [&](ConsoleCommand * c) {
int tout = c->get_int_of(&DeepSleepArgs::timeout);
ESP_LOGI(TAG, "Entering deep sleep for %d sec", tout);
ESP_LOGI(TAG, "Wakeup Cause: %d ", esp_sleep_get_wakeup_cause());
esp_deep_sleep(tout * 1000000);
return 0;
});
/* Wake up modem */
const ConsoleCommand WakeupModem("wakeup_modem", "Wakes up the modem from PSM", no_args, [&](ConsoleCommand * c) {
wakeup_modem();
ESP_LOGI(TAG, "OK");
return 0;
});
/* Enable PSM modem */
const ConsoleCommand EnablePSM("enable_psm", "Enables PSM on the modem", no_args, [&](ConsoleCommand * c) {
std::string out;
CHECK_ERR(dce->at("AT+CPSMS=1", out, 500), ESP_LOGI(TAG, "OK. %s", out.c_str()));
return 0;
});
/* Disable PSM modem */
const ConsoleCommand DisablePSM("disable_psm", "Disables PSM on the modem", no_args, [&](ConsoleCommand * c) {
std::string out;
CHECK_ERR(dce->at("AT+CPSMS=0", out, 500), ESP_LOGI(TAG, "OK. %s", out.c_str()));
return 0;
});
/* Get modem PSM cfg */
const ConsoleCommand GetModemCfg("get_psm_cfg", "Get PSM config", no_args, [&](ConsoleCommand * c) {
std::string out;
CHECK_ERR(dce->at("AT+CPSMS?", out, 500), ESP_LOGI(TAG, "OK. %s", out.c_str()));
return 0;
});
/* Set modem PSM config */
const struct SetPsmCfgArgs {
SetPsmCfgArgs():
periodic_tau(STR1, nullptr, nullptr, "<Requested_Periodic-TAU>", "T3412 Timer in 8-bit format"),
active_time(STR0, nullptr, nullptr, "<Requested_Active-Time>", "T3324 Timer in 8-bit format") {}
CommandArgs periodic_tau;
CommandArgs active_time;
} set_psm_cfg_args;
const ConsoleCommand SetPsmCfg("set_psm_cfg", "Set PSM config", &set_psm_cfg_args, sizeof(set_psm_cfg_args), [&](ConsoleCommand * c) {
std::string out;
auto periodic_tau = c->get_string_of(&SetPsmCfgArgs::periodic_tau);
auto active_time = c->get_string_of(&SetPsmCfgArgs::active_time);
/* Validate input */
if ((strlen(periodic_tau.c_str()) != strlen("00000000")) ||
(strlen(active_time.c_str()) != strlen("00000000"))) {
ESP_LOGE(TAG, "Failed with ERROR: Invalid argument length");
return 1;
}
/* Get PSM config */
dce->at("AT+CPSMS?", out, 500);
/* Update PSM config */
if (out.size() > 8) {
std::string set_cmd = "AT+CPSMS=" + std::to_string(out.c_str()[8] - '0') + ",,,\"" + periodic_tau.c_str() + "\",\"" + active_time.c_str() + "\"";
CHECK_ERR(dce->at(set_cmd, out, 500), ESP_LOGI(TAG, "OK. %s", out.c_str()));
} else {
ESP_LOGE(TAG, "Failed with ERROR: Invalid AT+CPSMS? return length");
}
return 0;
});
// start console REPL
ESP_ERROR_CHECK(esp_console_start_repl(s_repl));
// wait for exit
exit_signal.wait_any(1, UINT32_MAX);
s_repl->del(s_repl);
ESP_LOGI(TAG, "Exiting...%d", esp_get_free_heap_size());
#if defined(CONFIG_EXAMPLE_SERIAL_CONFIG_USB)
// USB example runs in a loop to demonstrate hot-plugging and sudden disconnection features.
} // while (1)
#endif
}

View File

@ -0,0 +1 @@
CONFIG_EXAMPLE_SERIAL_CONFIG_USB=y

View File

@ -1,8 +1,7 @@
# Override some defaults to enable PPP
# This file was generated using idf.py save-defconfig. It can be edited manually.
# Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration
#
CONFIG_ESP_MAIN_TASK_STACK_SIZE=7168
CONFIG_LWIP_PPP_SUPPORT=y
# CONFIG_LWIP_PPP_ENABLE_IPV6 is not set
CONFIG_LWIP_PPP_PAP_SUPPORT=y
CONFIG_LWIP_TCPIP_TASK_STACK_SIZE=4096
# Do not enable IPV6 in dte<->dce link local
CONFIG_LWIP_PPP_ENABLE_IPV6=n
# Disable legacy API
CONFIG_MODEM_LEGACY_API=n

View File

@ -9,6 +9,18 @@ This example shows how to act as a MQTT client after the PPPoS channel created b
See the README.md file in the upper level `pppos` directory for more information about the PPPoS examples.
### USB DTE support
For USB enabled targets (ESP32-S2 and ESP32-S3), it is possible to connect to the modem device via USB.
1. In menuconfig, navigate to `Example Configuration->Type of serial connection to the modem` and choose `USB`.
2. Connect the modem USB signals to pin 19 (DATA-) and 20 (DATA+) on your ESP chip.
USB example uses Quactel BG96 modem device. BG96 needs a positive pulse on its PWK pin to boot-up.
This example supports USB modem hot-plugging and reconnection.
### Supported IDF versions
This example is only supported from `v4.1`, as this is the default dependency of `esp-modem` component.
This example is only supported from `v4.1`, as this is the default dependency of `esp-modem` component.
USB example is supported from `v4.4`.

View File

@ -1,6 +1,21 @@
menu "Example Configuration"
choice EXAMPLE_SERIAL_CONFIG
prompt "Type of serial connection to the modem"
default EXAMPLE_SERIAL_CONFIG_UART
config EXAMPLE_SERIAL_CONFIG_UART
bool "UART"
help
Connect to modem via UART.
config EXAMPLE_SERIAL_CONFIG_USB
bool "USB"
depends on IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
help
Connect to modem via USB (CDC-ACM class). For IDF version >= 4.4.
endchoice
choice EXAMPLE_MODEM_DEVICE
depends on EXAMPLE_SERIAL_CONFIG_UART
prompt "Choose supported modem device (DCE)"
default EXAMPLE_MODEM_DEVICE_BG96
help
@ -74,6 +89,7 @@ menu "Example Configuration"
Pin to unlock the SIM
menu "UART Configuration"
depends on EXAMPLE_SERIAL_CONFIG_UART
config EXAMPLE_MODEM_UART_TX_PIN
int "TXD Pin Number"
default 25

View File

@ -0,0 +1,12 @@
## IDF Component Manager Manifest File
dependencies:
## Required IDF version
idf: ">=4.1.0"
espressif/esp_modem:
version: "^0.1.23"
override_path: "../../../"
espressif/esp_modem_usb_dte:
version: "^1.0.0"
rules:
- if: "idf_version >=4.4"
- if: "target in [esp32s2, esp32s3]"

View File

@ -14,6 +14,7 @@
#include "mqtt_client.h"
#include "esp_modem_api.h"
#include "esp_log.h"
#include "sdkconfig.h"
#define BROKER_URL "mqtt://mqtt.eclipseprojects.io"
@ -21,6 +22,28 @@ static const char *TAG = "pppos_example";
static EventGroupHandle_t event_group = NULL;
static const int CONNECT_BIT = BIT0;
static const int GOT_DATA_BIT = BIT2;
static const int USB_DISCONNECTED_BIT = BIT3; // Used only with USB DTE but we define it unconditionally, to avoid too many #ifdefs in the code
#if defined(CONFIG_EXAMPLE_SERIAL_CONFIG_USB)
#include "esp_modem_usb_c_api.h"
#include "esp_modem_usb_config.h"
#include "freertos/task.h"
static void usb_terminal_error_handler(esp_modem_terminal_error_t err)
{
if (err == ESP_MODEM_TERMINAL_DEVICE_GONE) {
ESP_LOGI(TAG, "USB modem disconnected");
assert(event_group);
xEventGroupSetBits(event_group, USB_DISCONNECTED_BIT);
}
}
#define CHECK_USB_DISCONNECTION(event_group) \
if ((xEventGroupGetBits(event_group) & USB_DISCONNECTED_BIT) == USB_DISCONNECTED_BIT) { \
esp_modem_destroy(dce); \
continue; \
}
#else
#define CHECK_USB_DISCONNECTION(event_group)
#endif
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
@ -111,16 +134,22 @@ static void on_ip_event(void *arg, esp_event_base_t event_base,
void app_main(void)
{
/* Init and register system/core components */
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &on_ip_event, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed, NULL));
/* Configure the PPP netif */
esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(CONFIG_EXAMPLE_MODEM_PPP_APN);
esp_netif_config_t netif_ppp_config = ESP_NETIF_DEFAULT_PPP();
esp_netif_t *esp_netif = esp_netif_new(&netif_ppp_config);
assert(esp_netif);
event_group = xEventGroupCreate();
/* Configure the DTE */
#if defined(CONFIG_EXAMPLE_SERIAL_CONFIG_UART)
esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_CONFIG();
/* setup UART specific configuration based on kconfig options */
dte_config.uart_config.tx_io_num = CONFIG_EXAMPLE_MODEM_UART_TX_PIN;
@ -134,17 +163,6 @@ void app_main(void)
dte_config.task_priority = CONFIG_EXAMPLE_MODEM_UART_EVENT_TASK_PRIORITY;
dte_config.dte_buffer_size = CONFIG_EXAMPLE_MODEM_UART_RX_BUFFER_SIZE / 2;
/* Configure the DCE */
esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(CONFIG_EXAMPLE_MODEM_PPP_APN);
/* Configure the PPP netif */
esp_netif_config_t netif_ppp_config = ESP_NETIF_DEFAULT_PPP();
/* Run the modem demo app */
// Init netif object
esp_netif_t *esp_netif = esp_netif_new(&netif_ppp_config);
assert(esp_netif);
#if CONFIG_EXAMPLE_MODEM_DEVICE_BG96 == 1
ESP_LOGI(TAG, "Initializing esp_modem for the BG96 module...");
esp_modem_dce_t *dce = esp_modem_new_dev(ESP_MODEM_DCE_BG96, &dte_config, &dce_config, esp_netif);
@ -158,8 +176,24 @@ void app_main(void)
ESP_LOGI(TAG, "Initializing esp_modem for a generic module...");
esp_modem_dce_t *dce = esp_modem_new(&dte_config, &dce_config, esp_netif);
#endif
assert(dce);
#elif defined(CONFIG_EXAMPLE_SERIAL_CONFIG_USB)
while (1) {
ESP_LOGI(TAG, "Initializing esp_modem for the BG96 module...");
struct esp_modem_usb_term_config usb_config = ESP_MODEM_DEFAULT_USB_CONFIG(0x2C7C, 0x0296, 2); // VID, PID and interface num of BG96 modem
const esp_modem_dte_config_t dte_usb_config = ESP_MODEM_DTE_DEFAULT_USB_CONFIG(usb_config);
ESP_LOGI(TAG, "Waiting for USB device connection...");
esp_modem_dce_t *dce = esp_modem_new_dev_usb(ESP_MODEM_DCE_BG96, &dte_usb_config, &dce_config, esp_netif);
esp_modem_set_error_cb(dce, usb_terminal_error_handler);
vTaskDelay(pdMS_TO_TICKS(1000)); // Although the DTE should be ready after USB enumeration, sometimes it fails to respond without this delay
#else
#error Invalid serial connection to modem.
#endif
assert(dce);
xEventGroupClearBits(event_group, CONNECT_BIT | GOT_DATA_BIT | USB_DISCONNECTED_BIT);
/* Run the modem demo app */
#if CONFIG_EXAMPLE_NEED_SIM_PIN == 1
// check if PIN needed
bool pin_ok = false;
@ -199,7 +233,10 @@ void app_main(void)
return;
}
/* Wait for IP address */
xEventGroupWaitBits(event_group, CONNECT_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
ESP_LOGI(TAG, "Waiting for IP address");
xEventGroupWaitBits(event_group, CONNECT_BIT | USB_DISCONNECTED_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
CHECK_USB_DISCONNECTION(event_group);
/* Config MQTT */
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
esp_mqtt_client_config_t mqtt_config = {
@ -213,7 +250,10 @@ void app_main(void)
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_LOGI(TAG, "Waiting for MQTT data");
xEventGroupWaitBits(event_group, GOT_DATA_BIT | USB_DISCONNECTED_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
CHECK_USB_DISCONNECTION(event_group);
esp_mqtt_client_destroy(mqtt_client);
err = esp_modem_set_mode(dce, ESP_MODEM_MODE_COMMAND);
if (err != ESP_OK) {
@ -228,6 +268,15 @@ void app_main(void)
}
ESP_LOGI(TAG, "IMSI=%s", imsi);
#if defined(CONFIG_EXAMPLE_SERIAL_CONFIG_USB)
// USB example runs in a loop to demonstrate hot-plugging and sudden disconnection features.
ESP_LOGI(TAG, "USB demo finished. Disconnect and connect the modem to run it again");
xEventGroupWaitBits(event_group, USB_DISCONNECTED_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
CHECK_USB_DISCONNECTION(event_group); // dce will be destroyed here
} // while (1)
#else
// UART DTE clean-up
esp_modem_destroy(dce);
esp_netif_destroy(esp_netif);
#endif
}

View File

@ -0,0 +1 @@
CONFIG_EXAMPLE_SERIAL_CONFIG_USB=y

View File

@ -14,6 +14,14 @@ menu "Example Configuration"
bool "BG96"
help
Quectel BG96 is a series of LTE Cat M1/Cat NB1/EGPRS module.
config EXAMPLE_MODEM_DEVICE_SIM7000
bool "SIM7000"
help
SIM7000 is a Multi-Band LTE-FDD and GSM/GPRS/EDGE module.
config EXAMPLE_MODEM_DEVICE_SIM7070
bool "SIM7070"
help
SIM7070 is Multi-Band CAT M and NB IoT module.
config EXAMPLE_MODEM_DEVICE_SIM7600
bool "SIM7600"
help
@ -39,6 +47,50 @@ menu "Example Configuration"
help
Pin to unlock the SIM
choice EXAMPLE_FLOW_CONTROL
bool "Set preferred modem control flow"
default EXAMPLE_FLOW_CONTROL_NONE
help
Set the modem's preferred control flow
config EXAMPLE_FLOW_CONTROL_NONE
bool "No control flow"
config EXAMPLE_FLOW_CONTROL_SW
bool "SW control flow"
config EXAMPLE_FLOW_CONTROL_HW
bool "HW control flow"
endchoice
menu "UART Configuration"
config EXAMPLE_MODEM_UART_TX_PIN
int "TXD Pin Number"
default 25
range 0 31
help
Pin number of UART TX.
config EXAMPLE_MODEM_UART_RX_PIN
int "RXD Pin Number"
default 26
range 0 31
help
Pin number of UART RX.
config EXAMPLE_MODEM_UART_RTS_PIN
int "RTS Pin Number"
default 27
range 0 31
help
Pin number of UART RTS.
config EXAMPLE_MODEM_UART_CTS_PIN
int "CTS Pin Number"
default 23
range 0 31
help
Pin number of UART CTS.
endmenu
config EXAMPLE_USE_VFS_TERM
bool "Use VFS terminal"
default n

View File

@ -21,6 +21,14 @@
#include "esp_https_ota.h" // For potential OTA configuration
#include "vfs_resource/vfs_create.hpp"
#if defined(CONFIG_EXAMPLE_FLOW_CONTROL_NONE)
#define EXAMPLE_FLOW_CONTROL ESP_MODEM_FLOW_CONTROL_NONE
#elif defined(CONFIG_EXAMPLE_FLOW_CONTROL_SW)
#define EXAMPLE_FLOW_CONTROL ESP_MODEM_FLOW_CONTROL_SW
#elif defined(CONFIG_EXAMPLE_FLOW_CONTROL_HW)
#define EXAMPLE_FLOW_CONTROL ESP_MODEM_FLOW_CONTROL_HW
#endif
#define BROKER_URL "mqtt://mqtt.eclipseprojects.io"
@ -39,6 +47,12 @@ extern "C" void app_main(void)
/* Configure and create the DTE */
esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_CONFIG();
/* setup UART specific configuration based on kconfig options */
dte_config.uart_config.tx_io_num = CONFIG_EXAMPLE_MODEM_UART_TX_PIN;
dte_config.uart_config.rx_io_num = CONFIG_EXAMPLE_MODEM_UART_RX_PIN;
dte_config.uart_config.rts_io_num = CONFIG_EXAMPLE_MODEM_UART_RTS_PIN;
dte_config.uart_config.cts_io_num = CONFIG_EXAMPLE_MODEM_UART_CTS_PIN;
dte_config.uart_config.flow_control = EXAMPLE_FLOW_CONTROL;
#if CONFIG_EXAMPLE_USE_VFS_TERM == 1
/* The VFS terminal is just a demonstration of using an abstract file descriptor
* which implements non-block reads, writes and selects to communicate with esp-modem.
@ -71,6 +85,10 @@ extern "C" void app_main(void)
std::unique_ptr<DCE> dce = create_BG96_dce(&dce_config, dte, esp_netif);
#elif CONFIG_EXAMPLE_MODEM_DEVICE_SIM800 == 1
std::unique_ptr<DCE> dce = create_SIM800_dce(&dce_config, dte, esp_netif);
#elif CONFIG_EXAMPLE_MODEM_DEVICE_SIM7000 == 1
std::unique_ptr<DCE> dce = create_SIM7000_dce(&dce_config, dte, esp_netif);
#elif CONFIG_EXAMPLE_MODEM_DEVICE_SIM7070 == 1
std::unique_ptr<DCE> dce = create_SIM7070_dce(&dce_config, dte, esp_netif);
#elif CONFIG_EXAMPLE_MODEM_DEVICE_SIM7600 == 1
std::unique_ptr<DCE> dce = create_SIM7600_dce(&dce_config, dte, esp_netif);
#else
@ -78,6 +96,17 @@ extern "C" void app_main(void)
#endif
assert(dce);
if(dte_config.uart_config.flow_control == ESP_MODEM_FLOW_CONTROL_HW)
{
if (command_result::OK != dce->set_flow_control(2, 2)) {
ESP_LOGE(TAG, "Failed to set the set_flow_control mode");
return;
}
ESP_LOGI(TAG, "set_flow_control OK");
} else {
ESP_LOGI(TAG, "not set_flow_control, because 2-wire mode active.");
}
/* Setup basic operation mode for the DCE (pin if used, CMUX mode) */
#if CONFIG_EXAMPLE_NEED_SIM_PIN == 1
bool pin_ok = true;

View File

@ -122,6 +122,7 @@ private:
std::shared_ptr<Terminal> data_term; /*!< Secondary terminal for this DTE */
modem_mode mode; /*!< DTE operation mode */
SignalGroup signal; /*!< Event group used to signal request-response operations */
command_result result; /*!< Command result of the currently exectuted command */
std::function<bool(uint8_t *data, size_t len)> on_data; /*!< on data callback for current terminal */
};

View File

@ -60,6 +60,23 @@ typedef enum esp_modem_dce_device
ESP_MODEM_DCE_SIM800,
} esp_modem_dce_device_t;
/**
* @brief Terminal errors
*/
typedef enum esp_modem_terminal_error
{
ESP_MODEM_TERMINAL_BUFFER_OVERFLOW,
ESP_MODEM_TERMINAL_CHECKSUM_ERROR,
ESP_MODEM_TERMINAL_UNEXPECTED_CONTROL_FLOW,
ESP_MODEM_TERMINAL_DEVICE_GONE,
ESP_MODEM_TERMINAL_UNKNOWN_ERROR,
} esp_modem_terminal_error_t;
/**
* @brief Terminal error callback
*/
typedef void (*esp_modem_terminal_error_cbt)(esp_modem_terminal_error_t);
/**
* @brief Create a generic DCE handle for new modem API
*
@ -90,6 +107,15 @@ esp_modem_dce_t *esp_modem_new_dev(esp_modem_dce_device_t module, const esp_mode
*/
void esp_modem_destroy(esp_modem_dce_t * dce);
/**
* @brief Set DTE's error callback
*
* @param dce Modem DCE handle
* @param[in] err_cb Error callback
* @return ESP_OK on success, ESP_FAIL on failure
*/
esp_err_t esp_modem_set_error_cb(esp_modem_dce_t * dce, esp_modem_terminal_error_cbt err_cb);
/**
* @brief Set operation mode for this DCE
* @param dce Modem DCE handle

View File

@ -12,6 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @file c_api_wrapper.hpp
* @brief Collection of C API wrappers
*
* This file is located in include/esp_private because it is not intended for users,
* but rather for esp_modem C extension developers.
*
* The C extension API must provide a 'factory function' that returns initialized pointer to esp_modem_dce_wrap.
* Helper functions provided below, can be used for conversion between C++ enum classes and standard C enums.
*/
#pragma once
#include "cxx_include/esp_modem_dce_factory.hpp"
@ -23,6 +34,8 @@ struct esp_modem_dce_wrap { // need to mimic the polymorphic dispatch as CPP use
enum class modem_wrap_dte_type { UART, VFS, USB } dte_type;
dce_factory::ModemType modem_type;
DCE *dce;
std::shared_ptr<DTE> dte;
esp_modem_dce_wrap() : dce(nullptr), dte(nullptr) {}
};
inline dce_factory::ModemType convert_modem_enum(esp_modem_dce_device_t module)
@ -43,3 +56,32 @@ inline dce_factory::ModemType convert_modem_enum(esp_modem_dce_device_t module)
return esp_modem::dce_factory::ModemType::GenericModule;
}
}
inline esp_modem_terminal_error_t convert_terminal_error_enum(terminal_error err)
{
switch (err) {
case terminal_error::BUFFER_OVERFLOW:
return ESP_MODEM_TERMINAL_BUFFER_OVERFLOW;
case terminal_error::CHECKSUM_ERROR:
return ESP_MODEM_TERMINAL_CHECKSUM_ERROR;
case terminal_error::UNEXPECTED_CONTROL_FLOW:
return ESP_MODEM_TERMINAL_UNEXPECTED_CONTROL_FLOW;
case terminal_error::DEVICE_GONE:
return ESP_MODEM_TERMINAL_DEVICE_GONE;
default:
return ESP_MODEM_TERMINAL_UNKNOWN_ERROR;
}
}
inline esp_err_t command_response_to_esp_err(command_result res)
{
switch (res) {
case command_result::OK:
return ESP_OK;
case command_result::FAIL:
return ESP_FAIL;
case command_result::TIMEOUT:
return ESP_ERR_TIMEOUT;
}
return ESP_ERR_INVALID_ARG;
}

View File

@ -295,6 +295,13 @@ ESP_MODEM_DECLARE_DCE_COMMAND(get_network_system_mode, command_result, 1, INT_OU
*/ \
ESP_MODEM_DECLARE_DCE_COMMAND(set_gnss_power_mode, command_result, 1, INT_IN(p1, mode)) \
\
/**
* @brief GNSS power control
* @param[out] mode power mode (0 - off, 1 - on)
* @return OK, FAIL or TIMEOUT
*/ \
ESP_MODEM_DECLARE_DCE_COMMAND(get_gnss_power_mode, command_result, 1, INT_OUT(p1, mode)) \
\
#ifdef GENERATE_DOCS

View File

@ -36,19 +36,6 @@ size_t strlcpy(char *dest, const char *src, size_t len);
// C API definitions
using namespace esp_modem;
static inline esp_err_t command_response_to_esp_err(command_result res)
{
switch (res) {
case command_result::OK:
return ESP_OK;
case command_result::FAIL:
return ESP_FAIL;
case command_result::TIMEOUT:
return ESP_ERR_TIMEOUT;
}
return ESP_ERR_INVALID_ARG;
}
extern "C" esp_modem_dce_t *esp_modem_new_dev(esp_modem_dce_device_t module, const esp_modem_dte_config_t *dte_config, const esp_modem_dce_config_t *dce_config, esp_netif_t *netif)
{
auto dce_wrap = new (std::nothrow) esp_modem_dce_wrap;
@ -60,6 +47,7 @@ extern "C" esp_modem_dce_t *esp_modem_new_dev(esp_modem_dce_device_t module, con
delete dce_wrap;
return nullptr;
}
dce_wrap->dte = dte;
dce_factory::Factory f(convert_modem_enum(module));
dce_wrap->dce = f.build(dce_config, std::move(dte), netif);
if (dce_wrap->dce == nullptr) {
@ -84,6 +72,22 @@ extern "C" void esp_modem_destroy(esp_modem_dce_t *dce_wrap)
}
}
extern "C" esp_err_t esp_modem_set_error_cb(esp_modem_dce_t * dce_wrap, esp_modem_terminal_error_cbt err_cb)
{
if (dce_wrap == nullptr || dce_wrap->dce == nullptr || dce_wrap->dte == nullptr) {
return ESP_ERR_INVALID_ARG;
}
if (err_cb) {
dce_wrap->dte->set_error_cb([err_cb](terminal_error err) {
err_cb(convert_terminal_error_enum(err));
});
} else {
dce_wrap->dte->set_error_cb(nullptr);
}
return ESP_OK;
}
extern "C" esp_err_t esp_modem_sync(esp_modem_dce_t *dce_wrap)
{
if (dce_wrap == nullptr || dce_wrap->dce == nullptr) {
@ -367,6 +371,19 @@ extern "C" esp_err_t esp_modem_set_gnss_power_mode(esp_modem_dce_t *dce_wrap, in
return command_response_to_esp_err(dce_wrap->dce->set_gnss_power_mode(mode));
}
extern "C" esp_err_t esp_modem_get_gnss_power_mode(esp_modem_dce_t *dce_wrap, int *p_mode)
{
if (dce_wrap == nullptr || dce_wrap->dce == nullptr) {
return ESP_ERR_INVALID_ARG;
}
int mode;
auto ret = command_response_to_esp_err(dce_wrap->dce->get_gnss_power_mode(mode));
if (ret == ESP_OK) {
*p_mode = mode;
}
return ret;
}
extern "C" esp_err_t esp_modem_reset(esp_modem_dce_t *dce_wrap)
{
return command_response_to_esp_err(dce_wrap->dce->reset());

View File

@ -554,6 +554,27 @@ command_result set_gnss_power_mode(CommandableIf *t, int mode)
return generic_command_common(t, "AT+CGNSPWR=" + std::to_string(mode) + "\r");
}
command_result get_gnss_power_mode(CommandableIf *t, int &mode)
{
ESP_LOGV(TAG, "%s", __func__ );
std::string_view out;
auto ret = generic_get_string(t, "AT+CGNSPWR?\r", out);
if (ret != command_result::OK) {
return ret;
}
constexpr std::string_view pattern = "+CGNSPWR: ";
constexpr int pos = pattern.size();
if (out.find(pattern) == std::string::npos) {
return command_result::FAIL;
}
if (std::from_chars(out.data() + pos, out.data() + out.size(), mode).ec == std::errc::invalid_argument) {
return command_result::FAIL;
}
return command_result::OK;
}
command_result set_gnss_power_mode_sim76xx(CommandableIf *t, int mode)
{
ESP_LOGV(TAG, "%s", __func__ );

View File

@ -35,9 +35,9 @@ DTE::DTE(std::unique_ptr<Terminal> terminal):
command_result DTE::command(const std::string &command, got_line_cb got_line, uint32_t time_ms, const char separator)
{
Scoped<Lock> l(internal_lock);
command_result res = command_result::TIMEOUT;
result = command_result::TIMEOUT;
signal.clear(GOT_LINE);
command_term->set_read_cb([&](uint8_t *data, size_t len) {
command_term->set_read_cb([this, got_line, separator](uint8_t *data, size_t len) {
if (!data) {
data = buffer.get();
len = command_term->read(data + buffer.consumed, buffer.size - buffer.consumed);
@ -45,8 +45,8 @@ command_result DTE::command(const std::string &command, got_line_cb got_line, ui
buffer.consumed = 0; // if the underlying terminal contains data, we cannot fragment
}
if (memchr(data + buffer.consumed, separator, len)) {
res = got_line(data, buffer.consumed + len);
if (res == command_result::OK || res == command_result::FAIL) {
result = got_line(data, buffer.consumed + len);
if (result == command_result::OK || result == command_result::FAIL) {
signal.set(GOT_LINE);
return true;
}
@ -56,12 +56,12 @@ 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) {
if (got_lf && result == command_result::TIMEOUT) {
ESP_MODEM_THROW_IF_ERROR(ESP_ERR_INVALID_STATE);
}
buffer.consumed = 0;
command_term->set_read_cb(nullptr);
return res;
return result;
}
command_result DTE::command(const std::string &cmd, got_line_cb got_line, uint32_t time_ms)

View File

@ -72,8 +72,8 @@ public:
void set_read_cb(std::function<bool(uint8_t *data, size_t len)> f) override
{
ESP_MODEM_THROW_IF_FALSE(signal.wait(TASK_PARAMS, 1000), "Failed to set UART task params");
on_read = std::move(f);
signal.set(TASK_PARAMS);
}
private:
@ -118,7 +118,6 @@ std::unique_ptr<Terminal> create_uart_terminal(const esp_modem_dte_config *confi
void UartTerminal::task()
{
std::function<bool(uint8_t *data, size_t len)> on_read_priv = nullptr;
uart_event_t event;
size_t len;
signal.set(TASK_INIT);
@ -127,17 +126,15 @@ void UartTerminal::task()
return; // exits to the static method where the task gets deleted
}
while (signal.is_any(TASK_START)) {
signal.set(TASK_PARAMS);
if (get_event(event, 100)) {
if (signal.is_any(TASK_PARAMS)) {
on_read_priv = on_read;
signal.clear(TASK_PARAMS);
}
signal.clear(TASK_PARAMS);
switch (event.type) {
case UART_DATA:
uart_get_buffered_data_len(uart.port, &len);
if (len && on_read_priv) {
if (on_read_priv(nullptr, len)) {
on_read_priv = nullptr;
if (len && on_read) {
if (on_read(nullptr, len)) {
on_read = nullptr;
}
}
break;

View File

@ -2801,7 +2801,8 @@ static int _mdns_check_aaaa_collision(esp_ip6_addr_t * ip, mdns_if_t tcpip_if)
static bool _hostname_is_ours(const char * hostname)
{
if (strcasecmp(hostname, _mdns_server->hostname) == 0) {
if (!_str_null_or_empty(_mdns_server->hostname) &&
strcasecmp(hostname, _mdns_server->hostname) == 0) {
return true;
}
mdns_host_item_t * host = _mdns_host_list;

View File

@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(slip_client)

View File

@ -0,0 +1,88 @@
| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | ESP32-S3 |
| ----------------- | ----- | -------- | -------- | -------- |
# SLIP device client
(See the README.md file in the upper level 'examples' directory for more information about examples.)
## Overview
This provides SLIP support for connection to Contiki gateway devices, allowing the ESP platform board to be used to bridge between low-power networks and IP (Wifi / Ethernet).
This example also demonstrates creating custom network interfaces, including UART drivers and lwIP netif layers, and attaching them to the standard `esp_netif` component, so the generic system interfaces can still use the common approach of listing all interfaces, updating states, posting events and handling routing priorities. Please refer to the implementation of [slip_modem](components/slip_modem) component for more details.
## How to use example
### Hardware Required
To run this example, you need an ESP32 dev board (e.g. ESP32-WROVER Kit) or ESP32 core board (e.g. ESP32-DevKitC).
For test purpose, you also need a SLIP capable gateway device, such as anything running [Contiki](https://github.com/contiki-os/contiki) gateway firmware.
You can also try other modules as long as they implement the SLIP protocol (e.g. linux device with slip module loaded)
#### Setup a test SLIP device
It is possible to configure any device with linux and a serial interface
(e.g. raspberry PI or a PC with USB to serial bridge) to enable SLIP interface.
To test this example with such device, please follow these steps:
- Configure IPv4 mode in the example configuration menu
- Setup SLIP interface
```
slattach -v -L -s 115200 -p slip /dev/ttyAMA0
```
where the `/dev/ttyAMA0` is the device's serial port
- Configure IP addresses
```
ifconfig sl0 10.0.0.1 dstaddr 10.0.0.2
```
where the `10.0.0.2` is IPv4 address of the ESP platform board
- Send and receive back UDP packets, as the example implements UDP echo server
```
nc -u 10.0.0.2 5678
```
#### Pin Assignment
**Note:** The following pin assignments are used by default which can be changed in menuconfig.
| ESP32 | Gateway |
| ------ | -------------- |
| GPIO25 | RX |
| GPIO26 | TX |
| GND | GND |
| 3v3 | VCC |
### Configure the project
Open the project configuration menu (`idf.py menuconfig`). Then go into `Example Configuration` menu.
- Choose the RX and TX pins
- Choose port number and IP protocol for socket udp server
For use in external projects `SLIP support` must be enabled under the `components/lwip` menu.
### Build and Flash
Run `idf.py -p PORT flash monitor` to build and flash the project..
(To exit the serial monitor, type ``Ctrl-]``.)
See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for full steps to configure and use ESP-IDF to build projects.
## Troubleshooting
1. Invalid slip packets
Many slip devices use additional messages for things like ipv6 prefix configuration (or sending log messages over the SLIP serial port). This is supported in the driver through the use of an `rx_filter` function that is called on receipt of all packets and can be used to filter packets prior to passing them to the stack.
2. No packets received
The first layer to check is the serial port, you can enable debugging of the SLIP component by setting the global log level to `DEBUG`, or changing the slip component log levbel with `esp_log_level_set("esp-netif_lwip-slip", ESP_LOG_DEBUG);`
(For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you as soon as possible.)

View File

@ -0,0 +1,8 @@
# SLIP Modem Component
idf_component_register(
SRCS "library/slip_modem.c" "library/slip_modem_netif.c"
INCLUDE_DIRS "include"
REQUIRES esp_netif driver
)
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")

View File

@ -0,0 +1,95 @@
/*
* SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include "esp_netif.h"
#include "driver/uart.h"
/** @brief Configuration of SLIP network interface
*
*/
#define ESP_NETIF_INHERENT_DEFAULT_SLIP() \
{ \
ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(mac) \
ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(ip_info) \
.get_ip_event = 0, \
.lost_ip_event = 0, \
.if_key = "SLP_DEF", \
.if_desc = "slip", \
.route_prio = 16, \
.bridge_info = NULL \
};
extern esp_netif_netstack_config_t *netstack_default_slip;
typedef struct slip_modem* slip_modem_handle;
// Filter callbacks for handling application specific slip messages
typedef bool slip_rx_filter_cb_t(slip_modem_handle slip, uint8_t *data, uint32_t len);
/** @brief Configuration structure for SLIP modem interface
*
*/
typedef struct {
uart_port_t uart_dev; /* UART device for reading and writing SLIP information, this must be initialised externally */
int uart_tx_pin; /* UART TX pin number */
int uart_rx_pin; /* UART TX pin number */
uint32_t uart_baud; /* UART baud rate */
uint32_t rx_buffer_len; /* Length of buffer for RX messages */
slip_rx_filter_cb_t *rx_filter; /* Filter for parsing out non-SLIP messages from incoming SLIP stream */
esp_ip6_addr_t *ipv6_addr;
} slip_modem_config_t;
/** @brief Create a slip modem
*
* @param[in] slip configured esp netif
* @param[in] configuration for the slip modem
*
* @returns
* - slip modem driver glue object
*/
slip_modem_handle slip_modem_create(esp_netif_t *slip_netif, const slip_modem_config_t *modem_config);
/** @brief Destroy a slip modem
*
* @param[in] slip modem handle for destruction
*
* @return
* - ESP_OK on success
*/
esp_err_t slip_modem_destroy(slip_modem_handle slip);
/**
* @brief Getter for the internally configured IPv6 address
*
* @param[in] slip modem handle
*
* @returns
* - ipv6 address
*/
esp_ip6_addr_t slip_modem_get_ipv6_address(slip_modem_handle slip);
/**
* @brief Data path API that forward the supplied data to the attached network interface
*
* @param[in] slip modem object
* @param[in] buffer pointer to the outgoing data
* @param[in] len length of the data
*
*/
void slip_modem_raw_write(slip_modem_handle slip, void *buffer, size_t len);

View File

@ -0,0 +1,256 @@
/*
* SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include "slip_modem.h"
#include "esp_netif.h"
#include "slip_modem_netif.h"
#include "esp_event.h"
#include "esp_log.h"
#define SLIP_RX_TASK_PRIORITY 10
#define SLIP_RX_TASK_STACK_SIZE (4 * 1024)
static const char *TAG = "slip-modem";
// UART container object
typedef struct {
// UART device number for SIO use
uart_port_t uart_dev;
// UART baud rate for configuration
uint32_t uart_baud;
// UART TX pin for configuration
int uart_tx_pin;
// UART RX pin for configuration
int uart_rx_pin;
// QueueHandle for uart driver
QueueHandle_t uart_queue;
// TaskHandle for receive task
TaskHandle_t uart_rx_task;
} esp_slip_uart_t;
// Modem object, implements glue logic for slip_driver and esp_netif
struct slip_modem {
// ESP base netif driver
esp_netif_driver_base_t base;
// Uart for use with slip
esp_slip_uart_t uart;
// Buffer for incoming messages
uint8_t *buffer;
uint32_t buffer_len;
// Filter callbacks for application-specific slip message handling
slip_rx_filter_cb_t *rx_filter;
// Running flag
bool running;
// esp_netif related: SLIP interface IP6 address
esp_ip6_addr_t addr;
};
static void slip_modem_uart_rx_task(void *arg);
static esp_err_t slip_modem_post_attach(esp_netif_t *esp_netif, void *args);
// Create a new slip modem
slip_modem_handle slip_modem_create(esp_netif_t *slip_netif, const slip_modem_config_t *modem_config)
{
if (slip_netif == NULL || modem_config == NULL) {
ESP_LOGE(TAG, "invalid parameters");
return NULL;
}
ESP_LOGI(TAG, "%s: Creating slip modem (netif: %p)", __func__, slip_netif);
ESP_LOGD(TAG, "%s (netif: %p)", __func__, slip_netif);
slip_modem_handle slip_modem = calloc(1, sizeof(struct slip_modem));
if (!slip_modem) {
ESP_LOGE(TAG, "create netif glue failed");
return NULL;
}
// Attach driver and post_attach callbacks
slip_modem->base.post_attach = slip_modem_post_attach;
slip_modem->base.netif = slip_netif;
// Attach config
slip_modem->buffer_len = modem_config->rx_buffer_len;
slip_modem->rx_filter = modem_config->rx_filter;
slip_modem->uart.uart_dev = modem_config->uart_dev;
slip_modem->uart.uart_baud = modem_config->uart_baud;
slip_modem->uart.uart_rx_pin = modem_config->uart_rx_pin;
slip_modem->uart.uart_tx_pin = modem_config->uart_tx_pin;
slip_modem->addr = *modem_config->ipv6_addr;
// Return the new modem
return slip_modem;
}
// Internal handler called on driver start
static esp_err_t esp_slip_driver_start(slip_modem_handle slip_modem)
{
ESP_LOGD(TAG, "%s: Starting SLIP modem (modem %p)", __func__, slip_modem);
// Allocate RX buffer if one does not exist
if (slip_modem->buffer == NULL) {
slip_modem->buffer = malloc(slip_modem->buffer_len);
}
if (slip_modem->buffer == NULL) {
ESP_LOGE(TAG, "error allocating rx buffer");
return ESP_ERR_NO_MEM;
}
// Build configuration
uart_config_t uart_config = {
.baud_rate = slip_modem->uart.uart_baud,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
};
// Initialise uart
ESP_ERROR_CHECK(uart_param_config(slip_modem->uart.uart_dev, &uart_config));
// Set UART pins
ESP_ERROR_CHECK(uart_set_pin(slip_modem->uart.uart_dev, slip_modem->uart.uart_tx_pin, slip_modem->uart.uart_rx_pin, 0, 0));
// Install UART driver
ESP_ERROR_CHECK(uart_driver_install(slip_modem->uart.uart_dev, slip_modem->buffer_len, slip_modem->buffer_len, 10, &slip_modem->uart.uart_queue, 0));
// Start slip RX task
slip_modem->running = true;
xTaskCreate(slip_modem_uart_rx_task, "slip_modem_uart_rx_task", SLIP_RX_TASK_STACK_SIZE, slip_modem, SLIP_RX_TASK_PRIORITY, &slip_modem->uart.uart_rx_task);
// Finally, initialise slip network interface
esp_netif_action_start(slip_modem->base.netif, 0, 0, 0);
ESP_ERROR_CHECK(slip_modem_netif_start(slip_modem->base.netif, &slip_modem->addr));
return ESP_OK;
}
esp_err_t slip_modem_destroy(slip_modem_handle slip)
{
if (slip != NULL) {
// Stop slip driver
esp_netif_action_stop(slip->base.netif, 0, 0, 0);
ESP_ERROR_CHECK(slip_modem_netif_stop(slip->base.netif));
// Stop uart rx task
vTaskDelete(slip->uart.uart_rx_task);
// Delete driver
uart_driver_delete(slip->uart.uart_dev);
// Free slip interface
free(slip);
}
return ESP_OK;
}
// Modem transmit for glue logic
static esp_err_t slip_modem_transmit(void *slip_driver, void *buffer, size_t len)
{
ESP_LOGD(TAG, "%s", __func__);
ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, len, ESP_LOG_DEBUG);
slip_modem_handle slip_modem = (slip_modem_handle)slip_driver;
int32_t res = uart_write_bytes(slip_modem->uart.uart_dev, (char *)buffer, len);
if (res < 0) {
// Handle errors
ESP_LOGE(TAG, "%s: uart_write_bytes error %i", __func__, res);
return ESP_FAIL;
}
return ESP_OK;
}
// Post-attach handler for netif
static esp_err_t slip_modem_post_attach(esp_netif_t *esp_netif, void *args)
{
slip_modem_handle slip_modem = (slip_modem_handle) args;
ESP_LOGD(TAG, "%s (netif: %p args: %p)", __func__, esp_netif, args);
const esp_netif_driver_ifconfig_t driver_ifconfig = {
.driver_free_rx_buffer = NULL,
.transmit = slip_modem_transmit,
.handle = slip_modem,
};
slip_modem->base.netif = esp_netif;
ESP_ERROR_CHECK(esp_netif_set_driver_config(esp_netif, &driver_ifconfig));
esp_slip_driver_start(slip_modem);
return ESP_OK;
}
static void slip_modem_uart_rx_task(void *arg)
{
if (arg == NULL) {
ESP_LOGE(TAG, "Starting a task with invalid parameters, deleting");
vTaskDelete(NULL);
}
slip_modem_handle slip_modem = (slip_modem_handle) arg;
ESP_LOGD(TAG, "Start SLIP modem RX task (slip_modem %p filter: %p)", slip_modem, slip_modem->rx_filter);
ESP_LOGD(TAG, "Uart: %d, buffer: %p (%d bytes)", slip_modem->uart.uart_dev, slip_modem->buffer, slip_modem->buffer_len);
while (slip_modem->running == true) {
// Read data from the UART
int len = uart_read_bytes(slip_modem->uart.uart_dev, slip_modem->buffer, slip_modem->buffer_len, 1 / portTICK_PERIOD_MS);
if (len > 0) {
// Log slip RX data
ESP_LOGD(TAG, "rx %d bytes", len);
ESP_LOG_BUFFER_HEX_LEVEL(TAG, slip_modem->buffer, len, ESP_LOG_DEBUG);
// Ensure null termination
slip_modem->buffer[len] = '\0';
// Filter if provided
if ((slip_modem->rx_filter != NULL) && slip_modem->rx_filter(slip_modem, slip_modem->buffer, len)) {
continue;
}
// Pass received bytes in to slip interface
ESP_LOGI(TAG, "esp_netif %p", slip_modem->base.netif);
esp_netif_receive(slip_modem->base.netif, slip_modem->buffer, len, NULL);
}
// Yield to allow other tasks to progress
vTaskDelay(1 * portTICK_PERIOD_MS);
}
}
/**
* @brief Gets the internally configured ipv6 address
*/
esp_ip6_addr_t slip_modem_get_ipv6_address(slip_modem_handle slip)
{
return slip->addr;
}
void slip_modem_raw_write(slip_modem_handle slip, void *buffer, size_t len)
{
slip_modem_netif_raw_write(slip->base.netif, buffer, len);
}

View File

@ -0,0 +1,195 @@
/*
* SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include "esp_netif.h"
#include "esp_log.h"
#include "esp_netif_net_stack.h"
#include "lwip/esp_netif_net_stack.h"
#include "lwip/dns.h"
#include "lwip/ip6_addr.h"
#include "lwip/netif.h"
#include "netif/slipif.h"
#include "lwip/sio.h"
static const char *TAG = "slip-modem-netif";
/**
* @brief Stops the SLIP interface
*/
esp_err_t slip_modem_netif_stop(esp_netif_t *esp_netif)
{
struct netif *netif = esp_netif_get_netif_impl(esp_netif);
ESP_LOGI(TAG, "%s: Stopped SLIP connection: lwip netif:%p", __func__, netif);
// Stop interface
netif_set_link_down(netif);
return ESP_OK;
}
/**
* @brief Starts the SLIP interface
*/
esp_err_t slip_modem_netif_start(esp_netif_t *esp_netif, esp_ip6_addr_t *addr)
{
struct netif *netif = esp_netif_get_netif_impl(esp_netif);
ESP_LOGI(TAG, "%s: Starting SLIP interface: lwip netif:%p", __func__, netif);
// Set the netif up
netif_set_up(netif);
netif_set_link_up(netif);
#if CONFIG_LWIP_IPV6
int8_t addr_index = 0;
netif_ip6_addr_set(netif, addr_index, (ip6_addr_t *)addr);
netif_ip6_addr_set_state(netif, addr_index, IP6_ADDR_VALID);
#endif
return ESP_OK;
}
/**
* @brief Write incoming serial data to the SLIP interface
*/
void esp_netif_lwip_slip_input(void *h, void *buffer, unsigned int len, void *eb)
{
struct netif *netif = h;
ESP_LOGD(TAG, "%s", __func__);
ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, len, ESP_LOG_DEBUG);
// Update slip netif with data
const int max_batch = 255;
int sent = 0;
while(sent < len) {
int batch = (len - sent) > max_batch ? max_batch : (len - sent);
slipif_received_bytes(netif, buffer+sent, batch);
sent += batch;
}
// Process incoming bytes
for (int i = 0; i < len; i++) {
slipif_process_rxqueue(netif);
}
}
/**
* @brief Write raw data out the SLIP interface
*/
void slip_modem_netif_raw_write(esp_netif_t *netif, void *buffer, size_t len)
{
struct netif *lwip_netif = esp_netif_get_netif_impl(netif);
ESP_LOGD(TAG, "%s", __func__);
struct pbuf p = {
.next = NULL,
.payload = buffer,
.tot_len = len,
.len = len,
};
// Call slip if output function to feed data out slip interface
#if CONFIG_LWIP_IPV6
lwip_netif->output_ip6(lwip_netif, &p, NULL);
#else
lwip_netif->output(lwip_netif, &p, NULL);
#endif
}
/** @brief Get esp-netif object corresponding to registration index
*/
static esp_netif_t * get_netif_with_esp_index(int index)
{
esp_netif_t *netif = NULL;
int counter = 0;
while ((netif = esp_netif_next(netif)) != NULL) {
if (counter == index) {
return netif;
}
counter++;
}
return NULL;
}
/** @brief Return list registration index of the supplied netif ptr
*/
static int get_esp_netif_index(esp_netif_t * esp_netif)
{
esp_netif_t *netif = NULL;
int counter = 0;
while ((netif = esp_netif_next(netif)) != NULL) {
if (esp_netif == netif) {
return counter;
}
counter++;
}
return -1;
}
static err_t esp_slipif_init(struct netif *netif)
{
if (netif == NULL) {
return ERR_IF;
}
esp_netif_t *esp_netif = netif->state;
int esp_index = get_esp_netif_index(esp_netif);
if (esp_index < 0) {
return ERR_IF;
}
// Store netif index in net interface for SIO open command to abstract the dev
netif->state = (void *)esp_index;
return slipif_init(netif);
}
const struct esp_netif_netstack_config s_netif_config_slip = {
.lwip = {
.init_fn = esp_slipif_init,
.input_fn = esp_netif_lwip_slip_input,
}
};
const esp_netif_netstack_config_t *netstack_default_slip = &s_netif_config_slip;
/***
* @brief Open a serial device for communication
*/
sio_fd_t sio_open(uint8_t devnum)
{
ESP_LOGD(TAG, "Opening device: %d\r\n", devnum);
esp_netif_t *esp_netif = get_netif_with_esp_index(devnum);
if (!esp_netif) {
ESP_LOGE(TAG, "didn't find esp-netif with index=%d\n", devnum);
return NULL;
}
// Return SIO handle
return esp_netif;
}
/***
* @brief Send a single character to the serial device (blocking)
*/
void sio_send(uint8_t c, sio_fd_t fd)
{
esp_netif_t *esp_netif = fd;
ESP_LOGD(TAG, "%s", __func__);
ESP_LOG_BUFFER_HEX_LEVEL(TAG, &c, 1, ESP_LOG_DEBUG);
esp_err_t ret = esp_netif_transmit(esp_netif, &c, 1);
if (ret != ESP_OK) {
// Handle errors
ESP_LOGD(TAG, "%s: uart_write_bytes error %i", __func__, ret);
}
}

View File

@ -0,0 +1,42 @@
/*
* SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
/**
* @brief Stop the esp slip netif
*
* @param[in] esp_netif handle to slip esp-netif instance
*
* @return
* - ESP_OK on success
*/
esp_err_t slip_modem_netif_stop(esp_netif_t *esp_netif);
/**
* @brief Start the esp slip netif
*
* @param[in] esp_netif handle to slip esp-netif instance
* @param[in] addr IPv6 address associated with this SLIP interface
*
* @return
* - ESP_OK on success
*/
esp_err_t slip_modem_netif_start(esp_netif_t *esp_netif, esp_ip6_addr_t *addr);
/**
* @brief Data path API to write raw packet ous the SLIP interface
*
* This API is typically used when implementing user defined methods
*
* @param[in] esp_netif handle to slip esp-netif instance
* @param[in] buffer pointer to the outgoing data
* @param[in] len length of the data
*
* @return
* - ESP_OK on success
*/
void slip_modem_netif_raw_write(esp_netif_t *netif, void *buffer, size_t len);

View File

@ -0,0 +1,8 @@
# Slip client example
idf_component_register(
SRCS "slip_client_main.c"
INCLUDE_DIRS "."
REQUIRES esp_netif slip_modem driver
)
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")

View File

@ -0,0 +1,38 @@
menu "Example Configuration"
menu "UART Configuration"
config EXAMPLE_UART_TX_PIN
int "TXD Pin Number"
default 25
range 0 36
help
Pin number of UART TX.
config EXAMPLE_UART_RX_PIN
int "RXD Pin Number"
default 26
range 0 36
help
Pin number of UART RX.
config EXAMPLE_UART_BAUD
int "UART baud rate"
default 115200
help
Baud rate for UART communication
endmenu
config EXAMPLE_UDP_PORT
int "Port for UDP echo server"
default 5678
help
Port for UDP echo server in example
config EXAMPLE_IPV4
bool "Test with IPv4 address"
default n
help
Test interface using IPv4
endmenu

View File

@ -0,0 +1,237 @@
/* SLIP Client Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include <sys/socket.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "slip_modem.h"
static const char *TAG = "slip-example";
#define STACK_SIZE (10 * 1024)
#define PRIORITY 10
static void udp_rx_tx_task(void *arg)
{
char addr_str[128];
uint8_t rx_buff[1024];
int sock = (int)arg;
struct sockaddr_storage source_addr;
socklen_t socklen = sizeof(source_addr);
ESP_LOGI(TAG, "Starting node manager UDP task");
while (1) {
// Receive data
int len = recvfrom(sock, rx_buff, sizeof(rx_buff) - 1, 0, (struct sockaddr *)&source_addr, &socklen);
if (len < 0) {
ESP_LOGE(TAG, "recvfrom failed: errno %d", errno);
break;
}
// Parse out address to string
if (source_addr.ss_family == PF_INET) {
inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
} else if (source_addr.ss_family == PF_INET6) {
inet6_ntoa_r(((struct sockaddr_in6 *)&source_addr)->sin6_addr, addr_str, sizeof(addr_str) - 1);
}
// Force null termination of received data and print
rx_buff[len] = 0;
ESP_LOGI(TAG, "Received '%s' from '%s'", rx_buff, addr_str);
// Send data back
int err = sendto(sock, rx_buff, len, 0, (struct sockaddr *)&source_addr, socklen);
if (err < 0) {
ESP_LOGE(TAG, "sendto failed: errno %d", errno);
break;
}
}
vTaskDelete(NULL);
}
static esp_err_t udp_rx_tx_start(void)
{
// Setup bind address
struct sockaddr_in6 dest_addr;
#if CONFIG_EXAMPLE_IPV4
sa_family_t family = AF_INET;
int ip_protocol = IPPROTO_IP;
struct sockaddr_in *dest_addr_ip4 = (struct sockaddr_in *)&dest_addr;
dest_addr_ip4->sin_addr.s_addr = htonl(INADDR_ANY);
dest_addr_ip4->sin_family = AF_INET;
dest_addr_ip4->sin_port = htons(CONFIG_EXAMPLE_UDP_PORT);
ip_protocol = IPPROTO_IP;
#else
sa_family_t family = AF_INET6;
int ip_protocol = IPPROTO_IPV6;
bzero(&dest_addr.sin6_addr.un, sizeof(dest_addr.sin6_addr.un));
dest_addr.sin6_family = family;
dest_addr.sin6_port = htons(CONFIG_EXAMPLE_UDP_PORT);
#endif
// Create socket
int sock = socket(family, SOCK_DGRAM, ip_protocol);
if (sock < 0) {
ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
return ESP_FAIL;
}
// Disable IPv4 and reuse address
int opt = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
#if !CONFIG_EXAMPLE_IPV4
setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt));
#endif
// Bind socket
int err = bind(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
if (err < 0) {
ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno);
return ESP_FAIL;
}
ESP_LOGI(TAG, "Socket bound, port %d", CONFIG_EXAMPLE_UDP_PORT);
// Start UDP rx thread
xTaskCreate(udp_rx_tx_task, "udp_rx_tx", STACK_SIZE, (void *)sock, PRIORITY, NULL);
return ESP_OK;
}
// Write a prefix to the contiki slip device
static void slip_set_prefix(slip_modem_handle slip)
{
uint8_t buff[10] = {0};
const esp_ip6_addr_t addr = slip_modem_get_ipv6_address(slip);
ESP_LOGI(TAG, "%s: prefix set (%08x:%08x)", __func__,
lwip_ntohl(addr.addr[0]), lwip_ntohl(addr.addr[1]));
// Build slip set message
buff[0] = '!';
buff[1] = 'P';
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
buff[2 + i * 4 + j] = addr.addr[i] >> (j * 8);
}
}
// Write raw data out the slip interface
slip_modem_raw_write(slip, buff, 2 + 8);
}
// slip_rx_filter filters incoming commands from the slip interface
// this implementation is designed for use with contiki slip devices
static bool slip_rx_filter(slip_modem_handle slip, uint8_t *data, uint32_t len)
{
if (data[1] == '?') {
switch (data[2]) {
case 'P':
ESP_LOGI(TAG, "Prefix request");
slip_set_prefix(slip);
return true;
default:
ESP_LOGI(TAG, "Unhandled request '%c'", data[2]);
break;
}
return true;
} else if (data[1] == '!') {
switch (data[2]) {
default:
ESP_LOGI(TAG, "Unhandled command '%c'", data[2]);
break;
}
}
return false;
}
#if CONFIG_EXAMPLE_IPV4
static const esp_netif_ip_info_t s_slip_ip4 = {
.ip = { .addr = ESP_IP4TOADDR( 10, 0, 0, 2) },
};
#endif
// Initialise the SLIP interface
esp_netif_t *slip_if_init(void)
{
ESP_LOGI(TAG, "Initialising SLIP interface");
esp_netif_inherent_config_t base_cfg = ESP_NETIF_INHERENT_DEFAULT_SLIP()
#if CONFIG_EXAMPLE_IPV4
base_cfg.ip_info = &s_slip_ip4;
#endif
esp_netif_config_t cfg = { .base = &base_cfg,
.driver = NULL,
.stack = netstack_default_slip };
esp_netif_t *slip_netif = esp_netif_new(&cfg);
esp_ip6_addr_t local_addr; /* Local IP6 address */
IP6_ADDR(&local_addr,
lwip_htonl(0xfd0000),
lwip_htonl(0x00000000),
lwip_htonl(0x00000000),
lwip_htonl(0x00000001)
);
ESP_LOGI(TAG, "Initialising SLIP modem");
slip_modem_config_t modem_cfg = {
.uart_dev = UART_NUM_1,
.uart_tx_pin = CONFIG_EXAMPLE_UART_TX_PIN,
.uart_rx_pin = CONFIG_EXAMPLE_UART_RX_PIN,
.uart_baud = CONFIG_EXAMPLE_UART_BAUD,
.rx_buffer_len = 1024,
.rx_filter = slip_rx_filter,
.ipv6_addr = &local_addr
};
void *slip_modem = slip_modem_create(slip_netif, &modem_cfg);
assert(slip_modem);
ESP_ERROR_CHECK(esp_netif_attach(slip_netif, slip_modem));
ESP_LOGI(TAG, "SLIP init complete");
return slip_netif;
}
void app_main(void)
{
// Setup networking
esp_netif_init();
esp_log_level_set("*", ESP_LOG_DEBUG);
// Create event loop
ESP_ERROR_CHECK(esp_event_loop_create_default());
// Setup slip interface
esp_netif_t* esp_netif = slip_if_init();
assert(esp_netif);
// Start the UDP user application
udp_rx_tx_start();
}

View File

@ -0,0 +1,2 @@
# Override some defaults to enable SLIP
CONFIG_LWIP_SLIP_SUPPORT=y