|
| 1 | +/* Modem console example: Custom DCE |
| 2 | +
|
| 3 | + This example code is in the Public Domain (or CC0 licensed, at your option.) |
| 4 | +
|
| 5 | + Unless required by applicable law or agreed to in writing, this |
| 6 | + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 7 | + CONDITIONS OF ANY KIND, either express or implied. |
| 8 | +*/ |
| 9 | + |
| 10 | +#include <cstring> |
| 11 | +#include "cxx_include/esp_modem_api.hpp" |
| 12 | +#include "cxx_include/esp_modem_dce_module.hpp" |
| 13 | +#include "generate/esp_modem_command_declare.inc" |
| 14 | +#include "my_module_dce.hpp" |
| 15 | + |
| 16 | +using namespace esp_modem; |
| 17 | + |
| 18 | +// |
| 19 | +// Define preprocessor's forwarding to dce_commands definitions |
| 20 | +// |
| 21 | + |
| 22 | +// Helper macros to handle multiple arguments of declared API |
| 23 | +#define ARGS0 |
| 24 | +#define ARGS1 , p1 |
| 25 | +#define ARGS2 , p1 , p2 |
| 26 | +#define ARGS3 , p1 , p2 , p3 |
| 27 | +#define ARGS4 , p1 , p2 , p3, p4 |
| 28 | +#define ARGS5 , p1 , p2 , p3, p4, p5 |
| 29 | +#define ARGS6 , p1 , p2 , p3, p4, p5, p6 |
| 30 | + |
| 31 | +#define _ARGS(x) ARGS ## x |
| 32 | +#define ARGS(x) _ARGS(x) |
| 33 | + |
| 34 | +// |
| 35 | +// Repeat all declarations and forward to the AT commands defined in esp_modem::dce_commands:: namespace |
| 36 | +// |
| 37 | +#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, arg_nr, ...) \ |
| 38 | + return_type Shiny::DCE::name(__VA_ARGS__) { return esp_modem::dce_commands::name(this ARGS(arg_nr) ); } |
| 39 | + |
| 40 | +DECLARE_ALL_COMMAND_APIS(return_type name(...) ) |
| 41 | + |
| 42 | +#undef ESP_MODEM_DECLARE_DCE_COMMAND |
| 43 | + |
| 44 | +std::unique_ptr<Shiny::DCE> create_shiny_dce(const esp_modem::dce_config *config, |
| 45 | + std::shared_ptr<esp_modem::DTE> dte, |
| 46 | + esp_netif_t *netif) |
| 47 | +{ |
| 48 | + return Shiny::Factory::create(config, std::move(dte), netif); |
| 49 | +} |
| 50 | + |
| 51 | +command_result Shiny::DCE::command(const std::string &cmd, got_line_cb got_line, uint32_t time_ms, const char separator) |
| 52 | +{ |
| 53 | + if (!handling_urc) { |
| 54 | + return dte->command(cmd, got_line, time_ms, separator); |
| 55 | + } |
| 56 | + handle_cmd = got_line; |
| 57 | + signal.clear(3); |
| 58 | + dte->write_cmd((uint8_t *)cmd.c_str(), cmd.length()); |
| 59 | + signal.wait_any(3, time_ms); |
| 60 | + handle_cmd = nullptr; |
| 61 | + if (signal.is_any(1)) { |
| 62 | + return esp_modem::command_result::OK; |
| 63 | + } |
| 64 | + if (signal.is_any(2)) { |
| 65 | + return esp_modem::command_result::FAIL; |
| 66 | + } |
| 67 | + return esp_modem::command_result::TIMEOUT; |
| 68 | +} |
| 69 | + |
| 70 | +command_result Shiny::DCE::handle_data(uint8_t *data, size_t len) |
| 71 | +{ |
| 72 | + if (std::memchr(data, '\n', len)) { |
| 73 | + if (handle_urc) { |
| 74 | + handle_urc(data, len); |
| 75 | + } |
| 76 | + if (handle_cmd) { |
| 77 | + auto ret = handle_cmd(data, len); |
| 78 | + if (ret == esp_modem::command_result::TIMEOUT) { |
| 79 | + return command_result::TIMEOUT; |
| 80 | + } |
| 81 | + if (ret == esp_modem::command_result::OK) { |
| 82 | + signal.set(1); |
| 83 | + } |
| 84 | + if (ret == esp_modem::command_result::FAIL) { |
| 85 | + signal.set(2); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + return command_result::TIMEOUT; |
| 90 | +} |
0 commit comments