usb: esp_modem biding
This commit adds esp_modem_usb_dte component. It can be used with esp_modem for communication with USB modems.
This commit is contained in:
parent
b0b65cbdb9
commit
fd9ecc13c1
|
@ -15,6 +15,13 @@ jobs:
|
||||||
- name: Upload components to component service
|
- name: Upload components to component service
|
||||||
uses: espressif/upload-components-ci-action@v1
|
uses: espressif/upload-components-ci-action@v1
|
||||||
with:
|
with:
|
||||||
directories: "bdc_motor;cbor;jsmn;led_strip;libsodium;pid_ctrl;qrcode;nghttp;sh2lib;expat;esp_encrypted_img;coap;pcap;json_generator;json_parser;usb/usb_host_cdc_acm;usb/usb_host_msc;usb/usb_host_uvc;esp_serial_slave_link;esp_jpeg;eigen"
|
directories: >
|
||||||
|
"
|
||||||
|
bdc_motor;cbor;jsmn;led_strip;libsodium;pid_ctrl;qrcode;nghttp;sh2lib;expat;esp_encrypted_img;coap;pcap;json_generator;json_parser;esp_serial_slave_link;esp_jpeg;eigen;
|
||||||
|
usb/usb_host_cdc_acm;
|
||||||
|
usb/usb_host_msc;
|
||||||
|
usb/usb_host_uvc;
|
||||||
|
usb/esp_modem_usb_dte;
|
||||||
|
"
|
||||||
namespace: "espressif"
|
namespace: "espressif"
|
||||||
api_token: ${{ secrets.IDF_COMPONENT_API_TOKEN }}
|
api_token: ${{ secrets.IDF_COMPONENT_API_TOKEN }}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
*/dist/**
|
**/dist/**
|
||||||
build
|
build
|
||||||
sdkconfig
|
sdkconfig
|
||||||
sdkconfig.old
|
sdkconfig.old
|
||||||
dependencies.lock
|
dependencies.lock
|
||||||
|
**/managed_components/**
|
||||||
|
.vscode/**
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
idf_component_register(SRCS "esp_modem_usb.cpp" "esp_modem_usb_api_target.cpp" "esp_modem_usb_c_api.cpp"
|
||||||
|
REQUIRED_IDF_TARGETS esp32s2 esp32s3
|
||||||
|
PRIV_INCLUDE_DIRS "private_include"
|
||||||
|
INCLUDE_DIRS "include")
|
||||||
|
|
||||||
|
set_target_properties(${COMPONENT_LIB} PROPERTIES CXX_STANDARD 17)
|
|
@ -0,0 +1,202 @@
|
||||||
|
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright [yyyy] [name of copyright owner]
|
||||||
|
|
||||||
|
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.
|
|
@ -0,0 +1,34 @@
|
||||||
|
# USB DTE plugin for esp_modem
|
||||||
|
|
||||||
|
> :warning: **Experimental feature**: USB DTE is under development!
|
||||||
|
|
||||||
|
This component extends [esp_modem](https://components.espressif.com/component/espressif/esp_modem) with USB DTE.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
For example usage see esp_modem examples:
|
||||||
|
* [console example](https://github.com/espressif/esp-protocols/tree/master/components/esp_modem/examples/modem_console)
|
||||||
|
* [PPPoS example](https://github.com/espressif/esp-protocols/tree/master/components/esp_modem/examples/pppos_client)
|
||||||
|
|
||||||
|
## USB hotplugging and reconnection
|
||||||
|
USB DTE supports device reconnection and hot-plugging. You must only register callback function for `DEVICE_GONE` event and react accordingly:
|
||||||
|
|
||||||
|
C++ with lambda callback:
|
||||||
|
```cpp
|
||||||
|
auto dte = create_usb_dte(&dte_config);
|
||||||
|
dte->set_error_cb([&](terminal_error err) {
|
||||||
|
if (err == terminal_error::DEVICE_GONE) {
|
||||||
|
// Signal your application that USB modem device has been disconnected
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
C:
|
||||||
|
```c
|
||||||
|
static void usb_terminal_error_handler(esp_modem_terminal_error_t err) {
|
||||||
|
if (err == ESP_MODEM_TERMINAL_DEVICE_GONE) {
|
||||||
|
// Signal your application that USB modem device has been disconnected
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
```
|
|
@ -0,0 +1,183 @@
|
||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "freertos/FreeRTOS.h"
|
||||||
|
#include "freertos/task.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "esp_modem_config.h"
|
||||||
|
#include "esp_modem_usb_config.h"
|
||||||
|
#include "cxx_include/esp_modem_dte.hpp"
|
||||||
|
#include "cxx_include/esp_modem_exception.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 you don't want to install USB Host driver here, you must install it before creating UsbTerminal object.
|
||||||
|
*
|
||||||
|
* This implementation of USB Host Lib handling never returns, which means that the USB Host Lib will keep running
|
||||||
|
* even after all USB devices are disconnected. That allows repeated device reconnections.
|
||||||
|
*
|
||||||
|
* If you want/need to handle lifetime of USB Host Lib, you can set install_usb_host to false and manage it yourself.
|
||||||
|
*
|
||||||
|
* @param arg Unused
|
||||||
|
*/
|
||||||
|
static void usb_host_task(void *arg)
|
||||||
|
{
|
||||||
|
while (1) {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 not already installed)
|
||||||
|
if (usb_config->install_usb_host && !usb_host_lib_task) {
|
||||||
|
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, &usb_host_lib_task, 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,
|
||||||
|
.new_dev_cb = NULL, // We don't forward this information to user. User can poll USB Host Lib.
|
||||||
|
};
|
||||||
|
|
||||||
|
// Silently continue on 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 ©) = delete;
|
||||||
|
UsbTerminal &operator=(const UsbTerminal ©) = delete;
|
||||||
|
bool operator== (const UsbTerminal ¶m) const = delete;
|
||||||
|
bool operator!= (const UsbTerminal ¶m) const = delete;
|
||||||
|
static TaskHandle_t usb_host_lib_task; // Reused by multiple devices or between reconnections
|
||||||
|
|
||||||
|
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(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:
|
||||||
|
ESP_LOGD(TAG, "Ignored USB event %d", event->type);
|
||||||
|
break;
|
||||||
|
case CDC_ACM_HOST_DEVICE_DISCONNECTED:
|
||||||
|
ESP_LOGW(TAG, "USB terminal disconnected");
|
||||||
|
if (this_terminal->on_error) {
|
||||||
|
this_terminal->on_error(terminal_error::DEVICE_GONE);
|
||||||
|
}
|
||||||
|
this_terminal->close();
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
TaskHandle_t UsbTerminal::usb_host_lib_task = nullptr;
|
||||||
|
|
||||||
|
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
|
|
@ -0,0 +1,22 @@
|
||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "usb_terminal.hpp"
|
||||||
|
#include "cxx_include/esp_modem_api.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));
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "esp_modem_usb_c_api.h"
|
||||||
|
#include "cxx_include/esp_modem_usb_api.hpp"
|
||||||
|
#include "esp_private/c_api_wrapper.hpp"
|
||||||
|
|
||||||
|
using namespace esp_modem;
|
||||||
|
|
||||||
|
extern "C" esp_modem_dce_t *esp_modem_new_dev_usb(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;
|
||||||
|
if (dce_wrap == nullptr) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
auto dte = create_usb_dte(dte_config);
|
||||||
|
if (dte == nullptr) {
|
||||||
|
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) {
|
||||||
|
delete dce_wrap;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
dce_wrap->modem_type = convert_modem_enum(module);
|
||||||
|
dce_wrap->dte_type = esp_modem_dce_wrap::modem_wrap_dte_type::USB;
|
||||||
|
return dce_wrap;
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
## IDF Component Manager Manifest File
|
||||||
|
version: "1.0.0"
|
||||||
|
description: USB DTE plugin for esp_modem component
|
||||||
|
url: https://github.com/espressif/idf-extra-components/tree/master/usb/esp_modem_usb_dte
|
||||||
|
|
||||||
|
dependencies:
|
||||||
|
idf: ">=4.4"
|
||||||
|
usb_host_cdc_acm: "1.*"
|
||||||
|
esp_modem: "^0.1.23"
|
|
@ -0,0 +1,20 @@
|
||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "cxx_include/esp_modem_api.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);
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "esp_modem_c_api_types.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Create a DCE handle using the supplied USB DTE
|
||||||
|
*
|
||||||
|
* @param module Specific device for creating this DCE
|
||||||
|
* @param dte_config DTE - USB configuration
|
||||||
|
* @param dce_config DCE configuration
|
||||||
|
* @param netif Network interface handle for the data mode
|
||||||
|
*
|
||||||
|
* @return DCE pointer on success, NULL on failure
|
||||||
|
*/
|
||||||
|
esp_modem_dce_t *esp_modem_new_dev_usb(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);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
|
@ -0,0 +1,56 @@
|
||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#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
|
||||||
|
* @param[in] _intf USB interface number
|
||||||
|
* @see USB host CDC-ACM driver documentation for details about interfaces settings
|
||||||
|
*/
|
||||||
|
#define ESP_MODEM_DEFAULT_USB_CONFIG(_vid, _pid, _intf) \
|
||||||
|
{ \
|
||||||
|
.vid = _vid, \
|
||||||
|
.pid = _pid, \
|
||||||
|
.interface_idx = _intf, \
|
||||||
|
.timeout_ms = 0, \
|
||||||
|
.xCoreID = 0, \
|
||||||
|
.cdc_compliant = false, \
|
||||||
|
.install_usb_host = true \
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "cxx_include/esp_modem_dte.hpp"
|
||||||
|
#include "../private_include/exception_stub.hpp"
|
||||||
|
|
||||||
|
struct esp_modem_dte_config;
|
||||||
|
|
||||||
|
namespace esp_modem {
|
||||||
|
std::unique_ptr<Terminal> create_usb_terminal(const esp_modem_dte_config *config);
|
||||||
|
} // namespace esp_modem
|
|
@ -4,7 +4,9 @@ cmake_minimum_required(VERSION 3.16)
|
||||||
|
|
||||||
set(EXTRA_COMPONENT_DIRS ../usb_host_cdc_acm
|
set(EXTRA_COMPONENT_DIRS ../usb_host_cdc_acm
|
||||||
../usb_host_msc
|
../usb_host_msc
|
||||||
../usb_host_uvc)
|
../usb_host_uvc
|
||||||
|
../esp_modem_usb_dte
|
||||||
|
)
|
||||||
|
|
||||||
# Set the components to include the tests for.
|
# Set the components to include the tests for.
|
||||||
set(TEST_COMPONENTS "usb_host_cdc_acm" "usb_host_msc" "usb_host_uvc" CACHE STRING "List of components to test")
|
set(TEST_COMPONENTS "usb_host_cdc_acm" "usb_host_msc" "usb_host_uvc" CACHE STRING "List of components to test")
|
||||||
|
|
Loading…
Reference in New Issue