Files
esp-protocols/components/esp_modem/include/cxx_include/esp_modem_dce.hpp

130 lines
3.1 KiB
C++
Raw Normal View History

2021-03-29 19:34:45 +02:00
// Copyright 2021 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.
2021-05-26 15:57:25 +02:00
#pragma once
2021-03-24 21:06:27 +01:00
2021-03-29 19:34:45 +02:00
#include <utility>
#include "cxx_include/esp_modem_netif.hpp"
2021-03-24 21:06:27 +01:00
#include "cxx_include/esp_modem_dce_module.hpp"
2021-03-03 20:35:08 +01:00
2021-03-29 19:34:45 +02:00
namespace esp_modem {
2021-03-24 21:06:27 +01:00
2021-04-14 17:57:42 +02:00
/**
* @defgroup ESP_MODEM_DCE
* @brief Definition of DCE abstraction
*/
/** @addtogroup ESP_MODEM_DCE
* @{
*/
/**
* @brief Helper class responsible for switching modes of the DCE's
*/
2021-03-29 19:34:45 +02:00
class DCE_Mode {
2021-03-24 21:06:27 +01:00
public:
2021-03-29 19:34:45 +02:00
DCE_Mode(): mode(modem_mode::COMMAND_MODE) {}
~DCE_Mode() = default;
bool set(DTE *dte, ModuleIf *module, Netif &netif, modem_mode m);
2021-03-24 21:06:27 +01:00
modem_mode get();
private:
modem_mode mode;
};
2021-03-29 19:34:45 +02:00
2021-04-14 17:57:42 +02:00
/**
* @brief General DCE class templated on a specific module. It is responsible for all the necessary transactions
* related to switching modes and consequent synergy with aggregated objects of DTE, Netif and a specific Module
*/
template<class SpecificModule>
2021-03-24 21:06:27 +01:00
class DCE_T {
static_assert(std::is_base_of<ModuleIf, SpecificModule>::value, "DCE must be instantiated with Module class only");
2021-03-03 20:35:08 +01:00
public:
2021-06-01 10:21:51 +02:00
explicit DCE_T(const std::shared_ptr<DTE> &dte, std::shared_ptr<SpecificModule> dev, esp_netif_t *netif):
dte(dte), device(std::move(dev)), netif(dte, netif)
{ }
2021-03-24 21:06:27 +01:00
~DCE_T() = default;
2021-04-13 20:29:55 +02:00
/**
* @brief Set data mode!
*/
2021-06-01 10:21:51 +02:00
void set_data()
{
set_mode(modem_mode::DATA_MODE);
}
2021-03-24 21:06:27 +01:00
2021-06-01 10:21:51 +02:00
void exit_data()
{
set_mode(modem_mode::COMMAND_MODE);
}
2021-03-24 21:06:27 +01:00
2021-06-01 10:21:51 +02:00
void set_cmux()
{
set_mode(modem_mode::CMUX_MODE);
}
2021-03-24 21:06:27 +01:00
2021-06-01 10:21:51 +02:00
SpecificModule *get_module()
{
return device.get();
}
2021-03-24 21:06:27 +01:00
2021-06-01 10:21:51 +02:00
command_result command(const std::string &command, got_line_cb got_line, uint32_t time_ms)
2021-03-24 21:06:27 +01:00
{
return dte->command(command, std::move(got_line), time_ms);
}
2021-06-01 10:21:51 +02:00
bool set_mode(modem_mode m)
{
return mode.set(dte.get(), device.get(), netif, m);
}
2021-03-24 21:06:27 +01:00
2021-04-04 22:15:46 +02:00
protected:
2021-03-24 21:06:27 +01:00
std::shared_ptr<DTE> dte;
2021-05-18 19:10:32 +02:00
std::shared_ptr<SpecificModule> device;
Netif netif;
2021-03-29 19:34:45 +02:00
DCE_Mode mode;
2021-03-24 21:06:27 +01:00
};
2021-04-14 17:57:42 +02:00
/**
* @brief Common abstraction of the modem DCE, specialized by the GenericModule which is a parent class for the supported
* devices and most common modems, as well.
2021-04-14 17:57:42 +02:00
*/
class DCE : public DCE_T<GenericModule> {
2021-03-24 21:06:27 +01:00
public:
using DCE_T<GenericModule>::DCE_T;
2021-04-15 14:59:30 +02:00
#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, num, ...) \
template <typename ...Agrs> \
return_type name(Agrs&&... args) \
{ \
2021-05-18 19:10:32 +02:00
return device->name(std::forward<Agrs>(args)...); \
}
2021-06-01 10:21:51 +02:00
DECLARE_ALL_COMMAND_APIS(forwards name(...)
{
device->name(...);
} )
#undef ESP_MODEM_DECLARE_DCE_COMMAND
2021-03-28 20:22:44 +02:00
};
2021-03-29 19:34:45 +02:00
2021-04-14 17:57:42 +02:00
/**
* @}
*/
2021-03-29 19:34:45 +02:00
} // esp_modem