Skip to content

Commit

Permalink
fix(modem): Fixed documentation and example on creating custom device
Browse files Browse the repository at this point in the history
Updates docs and examples per recent changes:
* Modem example does no longer demonstrate how to overwrite an AT method
* PPPoS client example now contains much simpler custom module
definition

Closes #452
  • Loading branch information
david-cermak committed Jan 9, 2024
1 parent ae38110 commit 577de67
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
26 changes: 10 additions & 16 deletions components/esp_modem/examples/modem_console/main/my_module_dce.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
Expand All @@ -17,26 +17,20 @@
#include "cxx_include/esp_modem_dce_module.hpp"

/**
* @brief Definition of a custom modem which inherits from the GenericModule, uses all its methods
* and could override any of them. Here, for demonstration purposes only, we redefine just `get_module_name()`
* @brief Definition of a custom DCE uses GenericModule and all its methods
* but could override command processing. Here, for demonstration purposes only,
* we "inject" URC handler to the actual command processing.
* This is possible since we inherit from `CommandableIf` and redefine `command()` method.
* Then we're able to use declare all common methods from the command library
* to be processed using "our" `command()` method (with custom URC handler).
*/
class MyShinyModem: public esp_modem::GenericModule {
using GenericModule::GenericModule;
public:
esp_modem::command_result get_module_name(std::string &name) override
{
name = "Custom Shiny Module";
return esp_modem::command_result::OK;
}
};

namespace Shiny {

using namespace esp_modem;

class DCE : public esp_modem::DCE_T<MyShinyModem>, public CommandableIf {
class DCE : public esp_modem::DCE_T<GenericModule>, public CommandableIf {
public:
using DCE_T<MyShinyModem>::DCE_T;
using DCE_T<GenericModule>::DCE_T;

command_result
command(const std::string &cmd, got_line_cb got_line, uint32_t time_ms) override
Expand Down Expand Up @@ -97,7 +91,7 @@ class Factory: public ::esp_modem::dce_factory::Factory {
std::shared_ptr<esp_modem::DTE> dte,
esp_netif_t *netif)
{
return build_generic_DCE<MyShinyModem, DCE, std::unique_ptr<DCE>>(config, std::move(dte), netif);
return build_generic_DCE<GenericModule, DCE, std::unique_ptr<DCE>>(config, std::move(dte), netif);
}

};
Expand Down
14 changes: 8 additions & 6 deletions docs/esp_modem/en/advanced_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ Create custom module

Creating a custom module is necessary if the application needs to use a specific device that is not supported
and their commands differ from any of the supported devices. In this case it is recommended to define a new class
representing this specific device and derive from the :cpp:class:`esp_modem::GenericModule`. In order to instantiate
the appropriate DCE of this module, application could use :ref:`the DCE factory<dce_factory>`, and build the DCE with
the specific module, using :cpp:func:`esp_modem::dce_factory::Factory::build`.
representing this specific device and derive from the :cpp:class:`esp_modem::GenericModule` (or any other available
module, that's close to your custom device). Then you can create the DCE using :ref:`the DCE factory<dce_factory>`
public method :cpp:func:`esp_modem::dce_factory::Factory::create_unique_dce_from`.

Please refer to the implementation of the existing modules.

Please note that the ``modem_console`` example defines a trivial custom modem DCE which overrides one command,
Please note that the ``pppos_client`` example defines a trivial custom DCE which overrides one command, and adds a new command
for demonstration purposes only.

It is also possible to create a specific DCE class that would conform to the generic ``DCE`` API and use all generic commands,
work with commands differently. This might be useful to add some custom preprocessing of commands or replies.
Please check the ``modem_console`` example with ``CONFIG_EXAMPLE_MODEM_DEVICE_SHINY=y`` configuration which demonstrates
overriding default ``command()`` method to implement URC processing in user space.

Create new communication interface
----------------------------------
Expand Down

0 comments on commit 577de67

Please sign in to comment.