Examples: Cleanup the modem console

This commit is contained in:
David Cermak
2021-04-18 20:19:53 +02:00
parent 77db911a48
commit 26bde89834
8 changed files with 130 additions and 180 deletions

View File

@ -114,9 +114,9 @@ esp_err_t PPPNetwork::init(esp_netif_t *netif, const std::string& apn, const std
{
// configure
esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_CONFIG();
dte_config.event_task_stack_size = 4096;
dte_config.rx_buffer_size = 16384;
dte_config.tx_buffer_size = 2048;
dte_config.uart_config.event_task_stack_size = 4096;
dte_config.uart_config.rx_buffer_size = 16384;
dte_config.uart_config.tx_buffer_size = 2048;
esp_modem_dce_config dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(apn.c_str());
// create DTE and minimal network DCE

View File

@ -84,20 +84,6 @@ int ConsoleCommand::get_int(int index)
}
std::vector<ConsoleCommand*> ConsoleCommand::console_commands;
int ConsoleCommand::last_command = 0;
const esp_console_cmd_func_t ConsoleCommand::command_func_pts[] = {
#define TEMPLATE(index) ConsoleCommand::command_func_ ## index ,
REPEAT_TEMPLATE_DEF(list of function pointers to command_func_XX() )
#undef TEMPLATE
};
int ConsoleCommand::command_func(int argc, char **argv) {
void * plain_arg_array = &arg_table[0];
int nerrors = arg_parse(argc, argv, (void **)plain_arg_array);
@ -110,3 +96,37 @@ int ConsoleCommand::command_func(int argc, char **argv) {
}
return 0;
}
/**
* @brief This class holds definitions of static functions, numbered from 0 index,
* that call indexed methods of ConsoleCommand (used to bridge from static esp-console
* to object oriented ConsoleCommand class)
*/
class StaticCommands {
friend class ConsoleCommand;
#define ITEM_TO_REPEAT(index) \
static inline int command_func_ ## index(int argc, char **argv) \
{ return ConsoleCommand::console_commands[index]->command_func(argc, argv); }
_DO_REPEAT_ITEM()
#undef ITEM_TO_REPEAT
};
/**
* @brief ConsoleCommand list of static callbacks used for getting object context to plain esp-console context
*/
const esp_console_cmd_func_t ConsoleCommand::command_func_pts[] = {
#define ITEM_TO_REPEAT(index) StaticCommands::command_func_ ## index ,
_DO_REPEAT_ITEM()
#undef ITEM_TO_REPEAT
};
/**
* @brief Static members defined for ConsoleCommand
*/
std::vector<ConsoleCommand*> ConsoleCommand::console_commands;
int ConsoleCommand::last_command = 0;

View File

@ -17,8 +17,11 @@
#include "argtable3/argtable3.h"
#include "repeat_helper.inc"
#define MAX_REPEAT_NR 10
#define MAX_REPEAT_NR 20
/**
* @brief Argument types used for ConsoleCommand
*/
enum arg_type {
STR0,
STR1,
@ -28,6 +31,9 @@ enum arg_type {
ARG_END,
};
/**
* Command argument struct definition for list of arguments of one command
*/
struct CommandArgs {
CommandArgs(arg_type t, const char * shopts, const char * lopts, const char * data, const char * glos):
type(t), shortopts(shopts), longopts(lopts), datatype(data), glossary(glos) {}
@ -41,11 +47,24 @@ struct CommandArgs {
const char *glossary;
};
class StaticCommands;
/**
* @brief This class simplifies console command definition in more object wise fashion
*/
class ConsoleCommand {
friend class StaticCommands;
public:
template<typename T> explicit ConsoleCommand(const char* command, const char* help,
const T *arg_struct, size_t srg_struct_size , std::function<bool(ConsoleCommand *)> f):
func(std::move(f))
/**
* @brief This is how we define a generic Console command
* @param command Textual console command
* @param help Contextual help for the command
* @param arg_struct List of argument struct
* @param srg_struct_size Size of the argument struct
* @param f Function callback for the command
*/
template<typename T> explicit ConsoleCommand(const char* command, const char* help, const T *arg_struct, size_t srg_struct_size,
std::function<bool(ConsoleCommand *)> f): func(std::move(f))
{
size_t args_plain_size = srg_struct_size / sizeof(CommandArgs);
auto first_arg = reinterpret_cast<const CommandArgs *>(arg_struct);
@ -53,35 +72,32 @@ public:
RegisterCommand(command, help, args);
}
/**
* @brief Another method of Console command definitions using vector arg struct
*/
explicit ConsoleCommand(const char* command, const char* help, const std::vector<CommandArgs>& args, std::function<bool(ConsoleCommand *)> f);
int get_count(int index);
/**
* @brief Utility getters of various params from the argument list
*/
template<typename T> int get_count_of(CommandArgs T::*member) { return get_count(index_arg(member)); }
template<typename T> std::string get_string_of(CommandArgs T::*member) { return get_string(index_arg(member)); }
template<typename T> int get_int_of(CommandArgs T::*member) { return get_int(index_arg(member)); }
std::string get_string(int index);
int get_int(int index);
private:
int get_count(int index);
void RegisterCommand(const char* command, const char* help, const std::vector<CommandArgs>& args);
template<typename T> static constexpr size_t index_arg(CommandArgs T::*member)
{ return ((uint8_t *)&((T*)nullptr->*member) - (uint8_t *)nullptr)/sizeof(CommandArgs); }
std::function<bool(ConsoleCommand *)> func;
std::vector<void*> arg_table;
int command_func(int argc, char **argv);
#define TEMPLATE(index) \
static inline int command_func_ ## index(int argc, char **argv) \
{ return console_commands[index]->command_func(argc, argv); }
REPEAT_TEMPLATE_DEF(definition of command_func_XX() )
#undef TEMPLATE
static std::vector<ConsoleCommand*> console_commands;
static int last_command;
static std::vector<ConsoleCommand*> console_commands;
std::function<bool(ConsoleCommand *)> func;
const static esp_console_cmd_func_t command_func_pts[];
};
#endif //MODEM_CONSOLE_CONSOLE_HELPER_H

View File

@ -59,7 +59,7 @@ extern "C" void app_main(void)
esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG("internet");
auto dce = create_SIM7600_dce(&dce_config, uart_dte, esp_netif);
assert(dce != NULL);
assert(dce != nullptr);
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();

View File

@ -1,27 +1,44 @@
//
// Created by david on 3/17/21.
//
/* Modem console 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.
*/
#ifndef MODEM_CONSOLE_REPEAT_HELPER_INC_H
#define MODEM_CONSOLE_REPEAT_HELPER_INC_H
#if MAX_REPEAT_NR < 10
#define REPEAT_TEMPLATE_DEF(...) REPEAT_10_TIMES(VA_ARG)
#else
#define REPEAT_TEMPLATE_DEF(...) REPEAT_100_TIMES(VA_ARG)
/**
* @brief This header is used to generate a defined macro many times with iterator
*/
#if MAX_REPEAT_NR > 20
#error "Not enough items to repeat"
#endif
#define REPEAT_10_TIMES(a) \
TEMPLATE(0) \
TEMPLATE(1) \
TEMPLATE(2) \
TEMPLATE(3) \
TEMPLATE(4) \
TEMPLATE(5) \
TEMPLATE(6) \
TEMPLATE(7) \
TEMPLATE(8) \
TEMPLATE(9)
#define _DO_REPEAT_ITEM(a) \
ITEM_TO_REPEAT(0) \
ITEM_TO_REPEAT(1) \
ITEM_TO_REPEAT(2) \
ITEM_TO_REPEAT(3) \
ITEM_TO_REPEAT(4) \
ITEM_TO_REPEAT(5) \
ITEM_TO_REPEAT(6) \
ITEM_TO_REPEAT(7) \
ITEM_TO_REPEAT(8) \
ITEM_TO_REPEAT(9) \
ITEM_TO_REPEAT(10) \
ITEM_TO_REPEAT(11) \
ITEM_TO_REPEAT(12) \
ITEM_TO_REPEAT(13) \
ITEM_TO_REPEAT(14) \
ITEM_TO_REPEAT(15) \
ITEM_TO_REPEAT(16) \
ITEM_TO_REPEAT(17) \
ITEM_TO_REPEAT(18) \
ITEM_TO_REPEAT(19) \
ITEM_TO_REPEAT(20)
#endif //MODEM_CONSOLE_REPEAT_HELPER_INC_H

View File

@ -124,17 +124,16 @@ void app_main(void)
/* Configure the DTE */
esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_CONFIG();
/* setup UART specific configuration based on kconfig options */
dte_config.tx_io_num = CONFIG_EXAMPLE_MODEM_UART_TX_PIN;
dte_config.rx_io_num = CONFIG_EXAMPLE_MODEM_UART_RX_PIN;
dte_config.rts_io_num = CONFIG_EXAMPLE_MODEM_UART_RTS_PIN;
dte_config.cts_io_num = CONFIG_EXAMPLE_MODEM_UART_CTS_PIN;
dte_config.rx_buffer_size = CONFIG_EXAMPLE_MODEM_UART_RX_BUFFER_SIZE;
dte_config.tx_buffer_size = CONFIG_EXAMPLE_MODEM_UART_TX_BUFFER_SIZE;
dte_config.pattern_queue_size = CONFIG_EXAMPLE_MODEM_UART_PATTERN_QUEUE_SIZE;
dte_config.event_queue_size = CONFIG_EXAMPLE_MODEM_UART_EVENT_QUEUE_SIZE;
dte_config.event_task_stack_size = CONFIG_EXAMPLE_MODEM_UART_EVENT_TASK_STACK_SIZE;
dte_config.event_task_priority = CONFIG_EXAMPLE_MODEM_UART_EVENT_TASK_PRIORITY;
dte_config.line_buffer_size = CONFIG_EXAMPLE_MODEM_UART_RX_BUFFER_SIZE / 2;
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.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;
dte_config.uart_config.event_task_stack_size = CONFIG_EXAMPLE_MODEM_UART_EVENT_TASK_STACK_SIZE;
dte_config.uart_config.event_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);

View File

@ -26,118 +26,10 @@ menu "Example Configuration"
help
Set APN (Access Point Name), a logical name to choose data network
config EXAMPLE_MODEM_LEGACY_API
bool "Use Modem legacy API"
default y
select MODEM_LEGACY_API
config EXAMPLE_SIM_PIN
string "Set SIM PIN"
default "1234"
help
Set this to true to use backward compatible API to the original modem
component in example/protocol folder in IDFv4.2 and below
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
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
Pin to unlock the SIM
endmenu

View File

@ -140,7 +140,15 @@ extern "C" void app_main(void)
esp_netif_t *esp_netif = esp_netif_new(&netif_ppp_config);
assert(esp_netif);
#if CONFIG_EXAMPLE_MODEM_DEVICE_BG96 == 1
auto dce = create_BG96_dce(&dce_config, uart_dte, esp_netif);
#elif CONFIG_EXAMPLE_MODEM_DEVICE_SIM800 == 1
auto dce = create_SIM800_dce(&dce_config, uart_dte, esp_netif);
#elif CONFIG_EXAMPLE_MODEM_DEVICE_SIM7600 == 1
auto dce = create_SIM7600_dce(&dce_config, uart_dte, esp_netif);
#else
#error "Unsupported device"
#endif
dce->set_command_mode();
@ -149,7 +157,7 @@ extern "C" void app_main(void)
std::cout << "Module name:" << str << std::endl;
bool pin_ok = true;
if (dce->read_pin(pin_ok) == command_result::OK && !pin_ok) {
throw_if_false(dce->set_pin("1234") == command_result::OK, "Cannot set PIN!");
throw_if_false(dce->set_pin(CONFIG_EXAMPLE_SIM_PIN) == command_result::OK, "Cannot set PIN!");
}
vTaskDelay(pdMS_TO_TICKS(1000));
@ -180,5 +188,3 @@ extern "C" void app_main(void)
ESP_LOGI(TAG, "Example finished");
}