mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-23 07:17:29 +02:00
esp_modem: Moved to component folder
This commit is contained in:
committed by
David Cermak
parent
61f264f97a
commit
90641c89eb
@ -0,0 +1,8 @@
|
||||
# The following lines of boilerplate have to be in your project's CMakeLists
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
set(EXTRA_COMPONENT_DIRS "../..")
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(modem-console)
|
11
components/esp_modem/examples/modem_console/Makefile
Normal file
11
components/esp_modem/examples/modem_console/Makefile
Normal file
@ -0,0 +1,11 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := modem-console
|
||||
|
||||
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
17
components/esp_modem/examples/modem_console/README.md
Normal file
17
components/esp_modem/examples/modem_console/README.md
Normal file
@ -0,0 +1,17 @@
|
||||
# PPPoS simple client example
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
## Overview
|
||||
|
||||
This example is mainly targets experimenting with a modem device, sending custom commands and switching to PPP mode using esp-console, command line API.
|
||||
Please check the list of supported commands using `help` command.
|
||||
|
||||
This example implements two very simple network commands to demonstrate and test basic network functionality.
|
||||
* `httpget`: Connect and get http content
|
||||
* `ping`: Send ICMP pings
|
||||
|
||||
To demonstrate creating custom modem devices, this example creates a DCE object using a locally defined create method,
|
||||
that sets up the DCE based on a custom module implemented in the `my_module_dce.hpp` file. The module class only overrides
|
||||
`get_module_name()` method supplying a user defined name, but keeps all other commands the same as defined in the `GenericModule`
|
||||
class.
|
@ -0,0 +1,5 @@
|
||||
idf_component_register(SRCS "modem_console_main.cpp"
|
||||
"console_helper.cpp"
|
||||
"httpget_handle.c"
|
||||
"ping_handle.c"
|
||||
INCLUDE_DIRS ".")
|
@ -0,0 +1,4 @@
|
||||
#
|
||||
# "main" pseudo-component makefile.
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
@ -0,0 +1,132 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
#include "console_helper.hpp"
|
||||
#include "esp_log.h"
|
||||
|
||||
static const char *TAG = "modem_console_helper";
|
||||
|
||||
ConsoleCommand::ConsoleCommand(const char* command, const char* help, const std::vector<CommandArgs>& args, std::function<bool(ConsoleCommand *)> f):
|
||||
func(std::move(f))
|
||||
{
|
||||
RegisterCommand(command, help, args);
|
||||
}
|
||||
|
||||
void ConsoleCommand::RegisterCommand(const char* command, const char* help, const std::vector<CommandArgs>& args)
|
||||
{
|
||||
assert(last_command <= MAX_REPEAT_NR);
|
||||
void * common_arg = nullptr;
|
||||
for (auto it: args) {
|
||||
switch(it.type) {
|
||||
case ARG_END:
|
||||
break;
|
||||
case STR0:
|
||||
common_arg = arg_str0(it.shortopts, it.longopts, it.datatype, it.glossary);
|
||||
break;
|
||||
case STR1:
|
||||
common_arg = arg_str1(it.shortopts, it.longopts, it.datatype, it.glossary);
|
||||
break;
|
||||
case INT0:
|
||||
common_arg = arg_int0(it.shortopts, it.longopts, it.datatype, it.glossary);
|
||||
break;
|
||||
case INT1:
|
||||
common_arg = arg_int1(it.shortopts, it.longopts, it.datatype, it.glossary);
|
||||
break;
|
||||
case LIT0:
|
||||
common_arg = arg_lit0(it.shortopts, it.longopts, it.glossary);
|
||||
break;
|
||||
}
|
||||
if (common_arg) {
|
||||
arg_table.emplace_back(common_arg);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Creating argument parser failed for %s", it.glossary);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
arg_table.emplace_back( arg_end(1));
|
||||
const esp_console_cmd_t command_def = {
|
||||
.command = command,
|
||||
.help = help,
|
||||
.hint = nullptr,
|
||||
.func = command_func_pts[last_command],
|
||||
.argtable = &arg_table[0]
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&command_def));
|
||||
last_command++;
|
||||
console_commands.emplace_back(this);
|
||||
}
|
||||
|
||||
int ConsoleCommand::get_count(int index)
|
||||
{
|
||||
return ((struct arg_str *)arg_table[index])->count;
|
||||
}
|
||||
|
||||
std::string ConsoleCommand::get_string(int index)
|
||||
{
|
||||
if (get_count(index) > 0) {
|
||||
return std::string(((struct arg_str *)arg_table[index])->sval[0]);
|
||||
}
|
||||
return std::string();
|
||||
}
|
||||
|
||||
int ConsoleCommand::get_int(int index)
|
||||
{
|
||||
if (get_count(index) > 0) {
|
||||
return *((struct arg_int *)arg_table[index])->ival;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
if (nerrors != 0) {
|
||||
arg_print_errors(stderr, (struct arg_end *) arg_table.back(), argv[0]);
|
||||
return 1;
|
||||
}
|
||||
if (func) {
|
||||
return func(this);
|
||||
}
|
||||
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;
|
@ -0,0 +1,103 @@
|
||||
/* 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_CONSOLE_HELPER_H
|
||||
#define MODEM_CONSOLE_CONSOLE_HELPER_H
|
||||
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <esp_console.h>
|
||||
#include "argtable3/argtable3.h"
|
||||
#include "repeat_helper.inc"
|
||||
|
||||
#define MAX_REPEAT_NR 20
|
||||
|
||||
/**
|
||||
* @brief Argument types used for ConsoleCommand
|
||||
*/
|
||||
enum arg_type {
|
||||
STR0,
|
||||
STR1,
|
||||
INT0,
|
||||
INT1,
|
||||
LIT0,
|
||||
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) {}
|
||||
CommandArgs(arg_type t, const char * shopts, const char * lopts, const char * glos):
|
||||
type(t), shortopts(shopts), longopts(lopts), datatype(nullptr), glossary(glos) {}
|
||||
|
||||
arg_type type;
|
||||
const char *shortopts;
|
||||
const char *longopts;
|
||||
const char *datatype;
|
||||
const char *glossary;
|
||||
};
|
||||
|
||||
class StaticCommands;
|
||||
|
||||
/**
|
||||
* @brief This class simplifies console command definition in more object wise fashion
|
||||
*/
|
||||
class ConsoleCommand {
|
||||
friend class StaticCommands;
|
||||
public:
|
||||
/**
|
||||
* @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);
|
||||
std::vector<CommandArgs> args(first_arg, first_arg + args_plain_size);
|
||||
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);
|
||||
|
||||
/**
|
||||
* @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::vector<void*> arg_table;
|
||||
int command_func(int argc, char **argv);
|
||||
|
||||
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
|
@ -0,0 +1,108 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_console.h"
|
||||
#include "argtable3/argtable3.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_http_client.h"
|
||||
|
||||
static const char *TAG = "modem_console_httpget";
|
||||
|
||||
static esp_err_t http_event_handler(esp_http_client_event_t *evt)
|
||||
{
|
||||
switch(evt->event_id) {
|
||||
case HTTP_EVENT_ERROR:
|
||||
ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
|
||||
break;
|
||||
case HTTP_EVENT_ON_CONNECTED:
|
||||
ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
|
||||
break;
|
||||
case HTTP_EVENT_HEADER_SENT:
|
||||
ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
|
||||
break;
|
||||
case HTTP_EVENT_ON_HEADER:
|
||||
ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
|
||||
break;
|
||||
case HTTP_EVENT_ON_DATA:
|
||||
ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
|
||||
if ((bool)evt->user_data &&
|
||||
!esp_http_client_is_chunked_response(evt->client)) {
|
||||
ESP_LOG_BUFFER_HEXDUMP(TAG, evt->data, evt->data_len, ESP_LOG_INFO);
|
||||
}
|
||||
|
||||
break;
|
||||
case HTTP_EVENT_ON_FINISH:
|
||||
ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
|
||||
break;
|
||||
case HTTP_EVENT_DISCONNECTED:
|
||||
ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
|
||||
break;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static struct {
|
||||
struct arg_str *host;
|
||||
struct arg_lit *hex;
|
||||
struct arg_end *end;
|
||||
} http_args;
|
||||
|
||||
static int do_http_client(int argc, char **argv)
|
||||
{
|
||||
int nerrors = arg_parse(argc, argv, (void **)&http_args);
|
||||
if (nerrors != 0) {
|
||||
arg_print_errors(stderr, http_args.end, argv[0]);
|
||||
return 1;
|
||||
}
|
||||
esp_http_client_config_t config = {
|
||||
.event_handler = http_event_handler,
|
||||
};
|
||||
|
||||
if (http_args.host->count > 0) {
|
||||
config.url = http_args.host->sval[0];
|
||||
} else {
|
||||
config.url = "http://httpbin.org/get";
|
||||
}
|
||||
|
||||
if (http_args.hex->count > 0) {
|
||||
// show hex data from http-get
|
||||
config.user_data = (void*)true;
|
||||
}
|
||||
|
||||
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
|
||||
esp_err_t err = esp_http_client_perform(client);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
return 0;
|
||||
}
|
||||
ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
|
||||
return 1;
|
||||
}
|
||||
|
||||
void modem_console_register_http(void)
|
||||
{
|
||||
http_args.host = arg_str0(NULL, NULL, "<host>", "address or host-name to send GET request (defaults to http://httpbin.org/get)");
|
||||
http_args.hex = arg_litn("p", "print-hex", 0, 1, "print hex output"),
|
||||
http_args.end = arg_end(1);
|
||||
const esp_console_cmd_t http_cmd = {
|
||||
.command = "httpget",
|
||||
.help = "http get command to test data mode",
|
||||
.hint = NULL,
|
||||
.func = &do_http_client,
|
||||
.argtable = &http_args
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&http_cmd));
|
||||
}
|
@ -0,0 +1,219 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_console.h"
|
||||
#include "esp_event.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "cxx_include/esp_modem_dte.hpp"
|
||||
#include "esp_modem_config.h"
|
||||
#include "cxx_include/esp_modem_api.hpp"
|
||||
#include "esp_log.h"
|
||||
#include "console_helper.hpp"
|
||||
#include "my_module_dce.hpp"
|
||||
|
||||
#define CHECK_ERR(cmd, success_action) do { \
|
||||
auto err = cmd; \
|
||||
if (err == command_result::OK) { \
|
||||
success_action; \
|
||||
return 0; \
|
||||
} else { \
|
||||
ESP_LOGE(TAG, "Failed with %s", err == command_result::TIMEOUT ? "TIMEOUT":"ERROR"); \
|
||||
return 1; \
|
||||
} } while (0)
|
||||
|
||||
/**
|
||||
* Please update the default APN name here (this could be updated runtime)
|
||||
*/
|
||||
#define DEFAULT_APN "my_apn"
|
||||
|
||||
extern "C" void modem_console_register_http(void);
|
||||
extern "C" void modem_console_register_ping(void);
|
||||
|
||||
static const char *TAG = "modem_console";
|
||||
static esp_console_repl_t *s_repl = nullptr;
|
||||
|
||||
using namespace esp_modem;
|
||||
|
||||
|
||||
extern "C" void app_main(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(nvs_flash_init());
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
|
||||
// init the netif, DTE and DCE respectively
|
||||
esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_CONFIG();
|
||||
esp_netif_config_t ppp_netif_config = ESP_NETIF_DEFAULT_PPP();
|
||||
esp_netif_t *esp_netif = esp_netif_new(&ppp_netif_config);
|
||||
assert(esp_netif);
|
||||
auto uart_dte = create_uart_dte(&dte_config);
|
||||
esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(DEFAULT_APN);
|
||||
auto dce = create_shiny_dce(&dce_config, uart_dte, esp_netif);
|
||||
assert(dce != nullptr);
|
||||
|
||||
// init console REPL environment
|
||||
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();
|
||||
ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart_config, &repl_config, &s_repl));
|
||||
|
||||
modem_console_register_http();
|
||||
modem_console_register_ping();
|
||||
const struct SetModeArgs {
|
||||
SetModeArgs(): mode(STR1, nullptr, nullptr, "<mode>", "PPP, CMD or CMUX") {}
|
||||
CommandArgs mode;
|
||||
} set_mode_args;
|
||||
const ConsoleCommand SetModeParser("set_mode", "sets modem mode", &set_mode_args, sizeof(set_mode_args), [&](ConsoleCommand *c){
|
||||
if (c->get_count_of(&SetModeArgs::mode)) {
|
||||
auto mode = c->get_string_of(&SetModeArgs::mode);
|
||||
modem_mode dev_mode;
|
||||
if (mode == "CMD") {
|
||||
dev_mode = esp_modem::modem_mode::COMMAND_MODE;
|
||||
} else if (mode == "PPP") {
|
||||
dev_mode = esp_modem::modem_mode::DATA_MODE;
|
||||
} else if (mode == "CMUX") {
|
||||
dev_mode = esp_modem::modem_mode::CMUX_MODE;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Unsupported mode: %s", mode.c_str());
|
||||
return 1;
|
||||
}
|
||||
ESP_LOGI(TAG, "Switching to %s name...", mode.c_str());
|
||||
if (!dce->set_mode(dev_mode)) {
|
||||
ESP_LOGE(TAG, "Failed to set the desired mode");
|
||||
return 1;
|
||||
}
|
||||
ESP_LOGI(TAG, "OK");
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
|
||||
const struct SetPinArgs {
|
||||
SetPinArgs(): pin(STR1, nullptr, nullptr, "<pin>", "PIN") {}
|
||||
CommandArgs pin;
|
||||
} set_pin_args;
|
||||
const ConsoleCommand SetPinParser("set_pin", "sets SIM card PIN", &set_pin_args, sizeof(set_pin_args), [&](ConsoleCommand *c){
|
||||
if (c->get_count_of(&SetPinArgs::pin)) {
|
||||
auto pin = c->get_string_of(&SetPinArgs::pin);
|
||||
ESP_LOGI(TAG, "Setting pin=%s...", pin.c_str());
|
||||
auto err = dce->set_pin(pin);
|
||||
if (err == command_result::OK) {
|
||||
ESP_LOGI(TAG, "OK");
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed %s", err == command_result::TIMEOUT ? "TIMEOUT":"");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
|
||||
const std::vector<CommandArgs> no_args;
|
||||
const ConsoleCommand ReadPinArgs("read_pin", "checks if SIM is unlocked", no_args, [&](ConsoleCommand *c){
|
||||
bool pin_ok;
|
||||
ESP_LOGI(TAG, "Checking pin...");
|
||||
auto err = dce->read_pin(pin_ok);
|
||||
if (err == command_result::OK) {
|
||||
ESP_LOGI(TAG, "OK. Pin status: %s", pin_ok ? "true": "false");
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed %s", err == command_result::TIMEOUT ? "TIMEOUT":"");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
|
||||
const ConsoleCommand GetModuleName("get_module_name", "reads the module name", no_args, [&](ConsoleCommand *c){
|
||||
std::string module_name;
|
||||
ESP_LOGI(TAG, "Reading module name...");
|
||||
CHECK_ERR(dce->get_module_name(module_name), ESP_LOGI(TAG, "OK. Module name: %s", module_name.c_str()));
|
||||
});
|
||||
|
||||
const ConsoleCommand GetOperatorName("get_operator_name", "reads the operator name", no_args, [&](ConsoleCommand *c){
|
||||
std::string operator_name;
|
||||
ESP_LOGI(TAG, "Reading operator name...");
|
||||
CHECK_ERR(dce->get_operator_name(operator_name), ESP_LOGI(TAG, "OK. Operator name: %s", operator_name.c_str()));
|
||||
});
|
||||
|
||||
const struct GenericCommandArgs {
|
||||
GenericCommandArgs():
|
||||
cmd(STR1, nullptr, nullptr, "<command>", "AT command to send to the modem"),
|
||||
timeout(INT0, "t", "timeout", "<timeout>", "command timeout"),
|
||||
pattern(STR0, "p", "pattern", "<pattern>", "command response to wait for"),
|
||||
no_cr(LIT0, "n", "no-cr", "do not add trailing CR to the command") {}
|
||||
CommandArgs cmd;
|
||||
CommandArgs timeout;
|
||||
CommandArgs pattern;
|
||||
CommandArgs no_cr;
|
||||
} send_cmd_args;
|
||||
const ConsoleCommand SendCommand("cmd", "sends generic AT command, no_args", &send_cmd_args, sizeof(send_cmd_args), [&](ConsoleCommand *c) {
|
||||
auto cmd = c->get_string_of(&GenericCommandArgs::cmd);
|
||||
auto timeout = c->get_count_of(&GenericCommandArgs::timeout) ? c->get_int_of(&GenericCommandArgs::timeout)
|
||||
: 1000;
|
||||
auto pattern = c->get_string_of(&GenericCommandArgs::pattern);
|
||||
if (c->get_count_of(&GenericCommandArgs::no_cr) == 0) {
|
||||
cmd += '\r';
|
||||
}
|
||||
ESP_LOGI(TAG, "Sending command %s with timeout %d", cmd.c_str(), timeout);
|
||||
CHECK_ERR(dce->command(cmd, [&](uint8_t *data, size_t len) {
|
||||
std::string response((char *) data, len);
|
||||
ESP_LOGI(TAG, "%s", response.c_str());
|
||||
if (pattern.empty() || response.find(pattern) != std::string::npos)
|
||||
return command_result::OK;
|
||||
if (response.find(pattern) != std::string::npos)
|
||||
return command_result::OK;
|
||||
return command_result::TIMEOUT;
|
||||
}, timeout),);
|
||||
});
|
||||
|
||||
const ConsoleCommand GetSignalQuality("get_signal_quality", "Gets signal quality", no_args, [&](ConsoleCommand *c){
|
||||
int rssi, ber;
|
||||
CHECK_ERR(dce->get_signal_quality(rssi, ber), ESP_LOGI(TAG, "OK. rssi=%d, ber=%d", rssi, ber));
|
||||
});
|
||||
const ConsoleCommand GetBatteryStatus("get_battery_status", "Reads voltage/battery status", no_args, [&](ConsoleCommand *c){
|
||||
int volt, bcl, bcs;
|
||||
CHECK_ERR(dce->get_battery_status(volt, bcl, bcs), ESP_LOGI(TAG, "OK. volt=%d, bcl=%d, bcs=%d", volt, bcl, bcs));
|
||||
});
|
||||
const ConsoleCommand PowerDown("power_down", "power down the module", no_args, [&](ConsoleCommand *c){
|
||||
ESP_LOGI(TAG, "Power down the module...");
|
||||
CHECK_ERR(dce->power_down(), ESP_LOGI(TAG, "OK"));
|
||||
});
|
||||
const ConsoleCommand Reset("reset", "reset the module", no_args, [&](ConsoleCommand *c){
|
||||
ESP_LOGI(TAG, "Resetting the module...");
|
||||
CHECK_ERR(dce->reset(), ESP_LOGI(TAG, "OK"));
|
||||
});
|
||||
const struct SetApn {
|
||||
SetApn(): apn(STR1, nullptr, nullptr, "<apn>", "APN (Access Point Name)") {}
|
||||
CommandArgs apn;
|
||||
} set_apn;
|
||||
const ConsoleCommand SetApnParser("set_apn", "sets APN", &set_apn, sizeof(set_apn), [&](ConsoleCommand *c){
|
||||
if (c->get_count_of(&SetApn::apn)) {
|
||||
auto apn = c->get_string_of(&SetApn::apn);
|
||||
ESP_LOGI(TAG, "Setting the APN=%s...", apn.c_str());
|
||||
auto new_pdp = std::unique_ptr<PdpContext>(new PdpContext(apn));
|
||||
dce->get_module()->configure_pdp_context(std::move(new_pdp));
|
||||
ESP_LOGI(TAG, "OK");
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
|
||||
SignalGroup exit_signal;
|
||||
const ConsoleCommand ExitConsole("exit", "exit the console application", no_args, [&](ConsoleCommand *c){
|
||||
ESP_LOGI(TAG, "Exiting...");
|
||||
exit_signal.set(1);
|
||||
s_repl->del(s_repl);
|
||||
return 0;
|
||||
});
|
||||
// start console REPL
|
||||
ESP_ERROR_CHECK(esp_console_start_repl(s_repl));
|
||||
// wait for exit
|
||||
exit_signal.wait_any(1, UINT32_MAX);
|
||||
ESP_LOGI(TAG, "Exiting...%d", esp_get_free_heap_size());
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
/* Modem console example: Custom DCE
|
||||
|
||||
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 __MY_MODULE_DCE_HPP__
|
||||
#define __MY_MODULE_DCE_HPP__
|
||||
|
||||
|
||||
#include "cxx_include/esp_modem_dce_factory.hpp"
|
||||
#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()`
|
||||
*/
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Helper create method which employs the DCE factory for creating DCE objects templated by a custom module
|
||||
* @return unique pointer of the resultant DCE
|
||||
*/
|
||||
std::unique_ptr<esp_modem::DCE> create_shiny_dce(const esp_modem::dce_config *config,
|
||||
std::shared_ptr<esp_modem::DTE> dte,
|
||||
esp_netif_t *netif)
|
||||
{
|
||||
return esp_modem::dce_factory::Factory::build_unique<MyShinyModem>(config, std::move(dte), netif);
|
||||
}
|
||||
|
||||
|
||||
#endif //__MY_MODULE_DCE_HPP__
|
141
components/esp_modem/examples/modem_console/main/ping_handle.c
Normal file
141
components/esp_modem/examples/modem_console/main/ping_handle.c
Normal file
@ -0,0 +1,141 @@
|
||||
/* Ping handle 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.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "esp_console.h"
|
||||
#include "argtable3/argtable3.h"
|
||||
#include "esp_log.h"
|
||||
#include "ping/ping_sock.h"
|
||||
#include "lwip/netdb.h"
|
||||
|
||||
static const char *TAG = "modem_console_ping";
|
||||
|
||||
static void cmd_ping_on_ping_success(esp_ping_handle_t hdl, void *args)
|
||||
{
|
||||
uint8_t ttl;
|
||||
uint16_t seqno;
|
||||
uint32_t elapsed_time, recv_len;
|
||||
ip_addr_t target_addr;
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_TTL, &ttl, sizeof(ttl));
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len));
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time));
|
||||
ESP_LOGI(TAG, "%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n",
|
||||
recv_len, inet_ntoa(target_addr.u_addr.ip4), seqno, ttl, elapsed_time);
|
||||
}
|
||||
|
||||
static void cmd_ping_on_ping_timeout(esp_ping_handle_t hdl, void *args)
|
||||
{
|
||||
uint16_t seqno;
|
||||
ip_addr_t target_addr;
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
|
||||
ESP_LOGE(TAG, "From %s icmp_seq=%d timeout\n", inet_ntoa(target_addr.u_addr.ip4), seqno);
|
||||
}
|
||||
|
||||
static void cmd_ping_on_ping_end(esp_ping_handle_t hdl, void *args)
|
||||
{
|
||||
ip_addr_t target_addr;
|
||||
uint32_t transmitted;
|
||||
uint32_t received;
|
||||
uint32_t total_time_ms;
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_REQUEST, &transmitted, sizeof(transmitted));
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_REPLY, &received, sizeof(received));
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
|
||||
esp_ping_get_profile(hdl, ESP_PING_PROF_DURATION, &total_time_ms, sizeof(total_time_ms));
|
||||
uint32_t loss = (uint32_t)((1 - ((float)received) / transmitted) * 100);
|
||||
if (IP_IS_V4(&target_addr)) {
|
||||
ESP_LOGI(TAG, "\n--- %s ping statistics ---\n", inet_ntoa(*ip_2_ip4(&target_addr)));
|
||||
} else {
|
||||
ESP_LOGI(TAG, "\n--- %s ping statistics ---\n", inet6_ntoa(*ip_2_ip6(&target_addr)));
|
||||
}
|
||||
ESP_LOGI(TAG, "%d packets transmitted, %d received, %d%% packet loss, time %dms\n",
|
||||
transmitted, received, loss, total_time_ms);
|
||||
// delete the ping sessions, so that we clean up all resources and can create a new ping session
|
||||
// we don't have to call delete function in the callback, instead we can call delete function from other tasks
|
||||
esp_ping_delete_session(hdl);
|
||||
}
|
||||
|
||||
static struct {
|
||||
struct arg_dbl *timeout;
|
||||
struct arg_int *count;
|
||||
struct arg_str *host;
|
||||
struct arg_end *end;
|
||||
} ping_args;
|
||||
|
||||
static int do_ping_cmd(int argc, char **argv)
|
||||
{
|
||||
esp_ping_config_t config = ESP_PING_DEFAULT_CONFIG();
|
||||
|
||||
int nerrors = arg_parse(argc, argv, (void **)&ping_args);
|
||||
if (nerrors != 0) {
|
||||
arg_print_errors(stderr, ping_args.end, argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ping_args.timeout->count > 0) {
|
||||
config.timeout_ms = (uint32_t)(ping_args.timeout->dval[0] * 1000);
|
||||
}
|
||||
|
||||
if (ping_args.count->count > 0) {
|
||||
config.count = (uint32_t)(ping_args.count->ival[0]);
|
||||
}
|
||||
|
||||
// parse IP address
|
||||
ip_addr_t target_addr;
|
||||
struct addrinfo hint;
|
||||
struct addrinfo *res = NULL;
|
||||
memset(&hint, 0, sizeof(hint));
|
||||
memset(&target_addr, 0, sizeof(target_addr));
|
||||
/* convert domain name to IP address */
|
||||
if (getaddrinfo(ping_args.host->sval[0], NULL, &hint, &res) != 0) {
|
||||
printf("ping: unknown host %s\n", ping_args.host->sval[0]);
|
||||
return 1;
|
||||
}
|
||||
if (res->ai_family == AF_INET) {
|
||||
struct in_addr addr4 = ((struct sockaddr_in *) (res->ai_addr))->sin_addr;
|
||||
inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4);
|
||||
} else {
|
||||
struct in6_addr addr6 = ((struct sockaddr_in6 *) (res->ai_addr))->sin6_addr;
|
||||
inet6_addr_to_ip6addr(ip_2_ip6(&target_addr), &addr6);
|
||||
}
|
||||
freeaddrinfo(res);
|
||||
config.target_addr = target_addr;
|
||||
|
||||
/* set callback functions */
|
||||
esp_ping_callbacks_t cbs = {
|
||||
.on_ping_success = cmd_ping_on_ping_success,
|
||||
.on_ping_timeout = cmd_ping_on_ping_timeout,
|
||||
.on_ping_end = cmd_ping_on_ping_end,
|
||||
.cb_args = NULL
|
||||
};
|
||||
esp_ping_handle_t ping;
|
||||
esp_ping_new_session(&config, &cbs, &ping);
|
||||
esp_ping_start(ping);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void modem_console_register_ping(void)
|
||||
{
|
||||
ping_args.timeout = arg_dbl0("W", "timeout", "<t>", "Time to wait for a response, in seconds");
|
||||
ping_args.count = arg_int0("c", "count", "<n>", "Stop after sending count packets");
|
||||
ping_args.host = arg_str1(NULL, NULL, "<host>", "Host address");
|
||||
ping_args.end = arg_end(1);
|
||||
const esp_console_cmd_t ping_cmd = {
|
||||
.command = "ping",
|
||||
.help = "send ICMP ECHO_REQUEST to network hosts",
|
||||
.hint = NULL,
|
||||
.func = &do_ping_cmd,
|
||||
.argtable = &ping_args
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&ping_cmd));
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
/* 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
|
||||
|
||||
/**
|
||||
* @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 _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
|
@ -0,0 +1,8 @@
|
||||
# Override some defaults to enable PPP
|
||||
CONFIG_LWIP_PPP_SUPPORT=y
|
||||
CONFIG_LWIP_PPP_PAP_SUPPORT=y
|
||||
CONFIG_LWIP_TCPIP_TASK_STACK_SIZE=4096
|
||||
# Do not enable IPV6 in dte<->dce link local
|
||||
CONFIG_LWIP_PPP_ENABLE_IPV6=n
|
||||
# Disable legacy API
|
||||
CONFIG_MODEM_LEGACY_API=n
|
Reference in New Issue
Block a user