2021-03-29 19:34:45 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
2022-10-11 16:31:57 +02:00
|
|
|
*
|
2021-03-29 19:34:45 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2021-03-02 21:01:44 +01:00
|
|
|
|
2021-05-13 07:28:05 +02:00
|
|
|
#include <charconv>
|
|
|
|
#include <list>
|
|
|
|
#include "esp_log.h"
|
2021-03-07 19:43:45 +01:00
|
|
|
#include "cxx_include/esp_modem_dte.hpp"
|
|
|
|
#include "cxx_include/esp_modem_dce_module.hpp"
|
|
|
|
#include "cxx_include/esp_modem_command_library.hpp"
|
2022-10-18 16:57:42 +02:00
|
|
|
#include "cxx_include/esp_modem_command_library_utils.hpp"
|
2021-03-04 20:19:18 +01:00
|
|
|
|
2021-03-07 19:43:45 +01:00
|
|
|
namespace esp_modem::dce_commands {
|
2021-03-02 21:01:44 +01:00
|
|
|
|
2021-05-17 14:59:03 +02:00
|
|
|
static const char *TAG = "command_lib";
|
|
|
|
|
2022-10-18 16:57:42 +02:00
|
|
|
static command_result generic_command(CommandableIf *t, const std::string &command,
|
|
|
|
const std::list<std::string_view> &pass_phrase,
|
|
|
|
const std::list<std::string_view> &fail_phrase,
|
|
|
|
uint32_t timeout_ms)
|
2021-03-03 20:35:08 +01:00
|
|
|
{
|
2021-05-17 18:24:35 +02:00
|
|
|
ESP_LOGD(TAG, "%s command %s\n", __func__, command.c_str());
|
2021-03-28 20:22:44 +02:00
|
|
|
return t->command(command, [&](uint8_t *data, size_t len) {
|
2021-06-01 10:21:51 +02:00
|
|
|
std::string_view response((char *)data, len);
|
|
|
|
if (data == nullptr || len == 0 || response.empty()) {
|
2021-05-17 14:59:03 +02:00
|
|
|
return command_result::TIMEOUT;
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
2021-05-17 18:24:35 +02:00
|
|
|
ESP_LOGD(TAG, "Response: %.*s\n", (int)response.length(), response.data());
|
2021-05-17 14:59:03 +02:00
|
|
|
for (auto &it : pass_phrase)
|
2021-06-01 10:21:51 +02:00
|
|
|
if (response.find(it) != std::string::npos) {
|
2021-05-13 07:28:05 +02:00
|
|
|
return command_result::OK;
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
2021-05-17 14:59:03 +02:00
|
|
|
for (auto &it : fail_phrase)
|
2021-06-01 10:21:51 +02:00
|
|
|
if (response.find(it) != std::string::npos) {
|
2021-05-13 07:28:05 +02:00
|
|
|
return command_result::FAIL;
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
2021-03-03 20:35:08 +01:00
|
|
|
return command_result::TIMEOUT;
|
|
|
|
}, timeout_ms);
|
2021-05-13 07:28:05 +02:00
|
|
|
|
2021-03-03 20:35:08 +01:00
|
|
|
}
|
2021-03-04 20:19:18 +01:00
|
|
|
|
2022-08-23 14:13:10 +02:00
|
|
|
command_result generic_command(CommandableIf *t, const std::string &command,
|
|
|
|
const std::string &pass_phrase,
|
|
|
|
const std::string &fail_phrase, uint32_t timeout_ms)
|
2021-05-13 07:28:05 +02:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-05-13 07:28:05 +02:00
|
|
|
const auto pass = std::list<std::string_view>({pass_phrase});
|
|
|
|
const auto fail = std::list<std::string_view>({fail_phrase});
|
|
|
|
return generic_command(t, command, pass, fail, timeout_ms);
|
|
|
|
}
|
|
|
|
|
2022-10-18 16:57:42 +02:00
|
|
|
static command_result generic_get_string(CommandableIf *t, const std::string &command, std::string_view &output, uint32_t timeout_ms = 500)
|
2021-03-04 20:19:18 +01:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-03-28 20:22:44 +02:00
|
|
|
return t->command(command, [&](uint8_t *data, size_t len) {
|
2021-03-04 20:19:18 +01:00
|
|
|
size_t pos = 0;
|
2021-06-01 10:21:51 +02:00
|
|
|
std::string_view response((char *)data, len);
|
2021-03-04 20:19:18 +01:00
|
|
|
while ((pos = response.find('\n')) != std::string::npos) {
|
2021-05-13 07:28:05 +02:00
|
|
|
std::string_view token = response.substr(0, pos);
|
|
|
|
for (auto it = token.end() - 1; it > token.begin(); it--) // strip trailing CR or LF
|
2021-06-01 10:21:51 +02:00
|
|
|
if (*it == '\r' || *it == '\n') {
|
2021-05-13 07:28:05 +02:00
|
|
|
token.remove_suffix(1);
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
2021-05-17 14:59:03 +02:00
|
|
|
ESP_LOGV(TAG, "Token: {%.*s}\n", static_cast<int>(token.size()), token.data());
|
2021-03-04 20:19:18 +01:00
|
|
|
|
|
|
|
if (token.find("OK") != std::string::npos) {
|
|
|
|
return command_result::OK;
|
|
|
|
} else if (token.find("ERROR") != std::string::npos) {
|
|
|
|
return command_result::FAIL;
|
2021-05-13 07:28:05 +02:00
|
|
|
} else if (token.size() > 2) {
|
2021-03-07 19:43:45 +01:00
|
|
|
output = token;
|
2021-03-04 20:19:18 +01:00
|
|
|
}
|
2021-06-01 10:21:51 +02:00
|
|
|
response = response.substr(pos + 1);
|
2021-03-04 20:19:18 +01:00
|
|
|
}
|
|
|
|
return command_result::TIMEOUT;
|
|
|
|
}, timeout_ms);
|
|
|
|
}
|
2021-03-03 20:35:08 +01:00
|
|
|
|
2022-08-23 14:13:10 +02:00
|
|
|
command_result generic_get_string(CommandableIf *t, const std::string &command, std::string &output, uint32_t timeout_ms)
|
2021-05-13 07:28:05 +02:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-05-13 07:28:05 +02:00
|
|
|
std::string_view out;
|
|
|
|
auto ret = generic_get_string(t, command, out, timeout_ms);
|
2021-06-01 10:21:51 +02:00
|
|
|
if (ret == command_result::OK) {
|
2021-05-13 07:28:05 +02:00
|
|
|
output = out;
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
2021-05-13 07:28:05 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-18 16:57:42 +02:00
|
|
|
command_result generic_command_common(CommandableIf *t, const std::string &command, uint32_t timeout_ms)
|
2021-03-03 20:35:08 +01:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2022-10-18 16:57:42 +02:00
|
|
|
return generic_command(t, command, "OK", "ERROR", timeout_ms);
|
2021-03-03 20:35:08 +01:00
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result sync(CommandableIf *t)
|
2021-03-03 20:35:08 +01:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-03-03 20:35:08 +01:00
|
|
|
return generic_command_common(t, "AT\r");
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result store_profile(CommandableIf *t)
|
2021-04-19 11:28:53 +02:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-04-19 11:28:53 +02:00
|
|
|
return generic_command_common(t, "AT&W\r");
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result power_down(CommandableIf *t)
|
2021-04-19 11:28:53 +02:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-04-19 11:28:53 +02:00
|
|
|
return generic_command(t, "AT+QPOWD=1\r", "POWERED DOWN", "ERROR", 1000);
|
|
|
|
}
|
|
|
|
|
2022-04-13 15:36:15 +02:00
|
|
|
command_result power_down_sim76xx(CommandableIf *t)
|
2021-04-19 11:28:53 +02:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-04-19 11:28:53 +02:00
|
|
|
return generic_command_common(t, "AT+CPOF\r", 1000);
|
|
|
|
}
|
|
|
|
|
2022-04-13 15:36:15 +02:00
|
|
|
command_result power_down_sim70xx(CommandableIf *t)
|
2022-04-13 13:42:57 +02:00
|
|
|
{
|
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
|
|
|
return generic_command(t, "AT+CPOWD=1\r", "POWER DOWN", "ERROR", 1000);
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result power_down_sim8xx(CommandableIf *t)
|
2021-04-19 11:28:53 +02:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-04-19 11:28:53 +02:00
|
|
|
return generic_command(t, "AT+CPOWD=1\r", "POWER DOWN", "ERROR", 1000);
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result reset(CommandableIf *t)
|
2021-04-19 11:28:53 +02:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-04-19 11:28:53 +02:00
|
|
|
return generic_command(t, "AT+CRESET\r", "PB DONE", "ERROR", 60000);
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result set_baud(CommandableIf *t, int baud)
|
2021-04-19 11:28:53 +02:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-04-19 11:28:53 +02:00
|
|
|
return generic_command_common(t, "AT+IPR=" + std::to_string(baud) + "\r");
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result hang_up(CommandableIf *t)
|
2021-04-19 11:28:53 +02:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-04-19 11:28:53 +02:00
|
|
|
return generic_command_common(t, "ATH\r", 90000);
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result get_battery_status(CommandableIf *t, int &voltage, int &bcs, int &bcl)
|
2021-04-19 11:28:53 +02:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-05-13 07:28:05 +02:00
|
|
|
std::string_view out;
|
2021-04-19 11:28:53 +02:00
|
|
|
auto ret = generic_get_string(t, "AT+CBC\r", out);
|
2021-06-01 10:21:51 +02:00
|
|
|
if (ret != command_result::OK) {
|
2021-04-19 11:28:53 +02:00
|
|
|
return ret;
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
2021-05-13 07:28:05 +02:00
|
|
|
|
|
|
|
constexpr std::string_view pattern = "+CBC: ";
|
2021-06-01 10:21:51 +02:00
|
|
|
if (out.find(pattern) == std::string_view::npos) {
|
2021-04-19 11:28:53 +02:00
|
|
|
return command_result::FAIL;
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
2021-04-19 11:28:53 +02:00
|
|
|
// Parsing +CBC: <bcs>,<bcl>,<voltage>
|
2021-05-13 07:28:05 +02:00
|
|
|
out = out.substr(pattern.size());
|
|
|
|
int pos, value, property = 0;
|
2021-12-06 11:12:00 +01:00
|
|
|
while ((pos = out.find(',')) != std::string::npos) {
|
2021-06-01 10:21:51 +02:00
|
|
|
if (std::from_chars(out.data(), out.data() + pos, value).ec == std::errc::invalid_argument) {
|
2021-05-13 07:28:05 +02:00
|
|
|
return command_result::FAIL;
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
2021-05-13 07:28:05 +02:00
|
|
|
switch (property++) {
|
2021-06-01 10:21:51 +02:00
|
|
|
case 0: bcs = value;
|
|
|
|
break;
|
|
|
|
case 1: bcl = value;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return command_result::FAIL;
|
2021-05-13 07:28:05 +02:00
|
|
|
}
|
|
|
|
out = out.substr(pos + 1);
|
|
|
|
}
|
2021-06-01 10:21:51 +02:00
|
|
|
if (std::from_chars(out.data(), out.data() + out.size(), voltage).ec == std::errc::invalid_argument) {
|
2021-05-13 07:28:05 +02:00
|
|
|
return command_result::FAIL;
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
2021-04-19 11:28:53 +02:00
|
|
|
return command_result::OK;
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result get_battery_status_sim7xxx(CommandableIf *t, int &voltage, int &bcs, int &bcl)
|
2021-04-19 11:28:53 +02:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-05-13 07:28:05 +02:00
|
|
|
std::string_view out;
|
2021-04-19 11:28:53 +02:00
|
|
|
auto ret = generic_get_string(t, "AT+CBC\r", out);
|
2021-06-01 10:21:51 +02:00
|
|
|
if (ret != command_result::OK) {
|
2021-04-19 11:28:53 +02:00
|
|
|
return ret;
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
2021-04-19 11:28:53 +02:00
|
|
|
// Parsing +CBC: <voltage in Volts> V
|
2021-05-13 07:28:05 +02:00
|
|
|
constexpr std::string_view pattern = "+CBC: ";
|
|
|
|
constexpr int num_pos = pattern.size();
|
|
|
|
int dot_pos;
|
|
|
|
if (out.find(pattern) == std::string::npos ||
|
2021-06-01 10:21:51 +02:00
|
|
|
(dot_pos = out.find('.')) == std::string::npos) {
|
2021-05-13 07:28:05 +02:00
|
|
|
return command_result::FAIL;
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
2021-05-13 07:28:05 +02:00
|
|
|
|
2021-04-19 11:28:53 +02:00
|
|
|
int volt, fraction;
|
2021-06-01 10:21:51 +02:00
|
|
|
if (std::from_chars(out.data() + num_pos, out.data() + dot_pos, volt).ec == std::errc::invalid_argument) {
|
2021-05-13 07:28:05 +02:00
|
|
|
return command_result::FAIL;
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
|
|
|
if (std::from_chars(out.data() + dot_pos + 1, out.data() + out.size() - 1, fraction).ec == std::errc::invalid_argument) {
|
2021-05-13 07:28:05 +02:00
|
|
|
return command_result::FAIL;
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
2021-04-19 11:28:53 +02:00
|
|
|
bcl = bcs = -1; // not available for these models
|
2021-06-01 10:21:51 +02:00
|
|
|
voltage = 1000 * volt + fraction;
|
2021-04-19 11:28:53 +02:00
|
|
|
return command_result::OK;
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result set_flow_control(CommandableIf *t, int dce_flow, int dte_flow)
|
2021-04-19 11:28:53 +02:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2022-12-22 11:55:37 +01:00
|
|
|
return generic_command_common(t, "AT+IFC=" + std::to_string(dce_flow) + "," + std::to_string(dte_flow) + "\r");
|
2021-04-19 11:28:53 +02:00
|
|
|
}
|
|
|
|
|
2022-07-11 21:03:55 +02:00
|
|
|
command_result get_operator_name(CommandableIf *t, std::string &operator_name, int &act)
|
2021-04-19 11:28:53 +02:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-05-13 07:28:05 +02:00
|
|
|
std::string_view out;
|
2021-04-19 11:28:53 +02:00
|
|
|
auto ret = generic_get_string(t, "AT+COPS?\r", out, 75000);
|
2021-06-01 10:21:51 +02:00
|
|
|
if (ret != command_result::OK) {
|
2021-04-19 11:28:53 +02:00
|
|
|
return ret;
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
2021-04-19 11:28:53 +02:00
|
|
|
auto pos = out.find("+COPS");
|
|
|
|
auto property = 0;
|
|
|
|
while (pos != std::string::npos) {
|
2022-07-11 21:03:55 +02:00
|
|
|
// Looking for: +COPS: <mode>[, <format>[, <oper>[, <act>]]]
|
2021-04-19 11:28:53 +02:00
|
|
|
if (property++ == 2) { // operator name is after second comma (as a 3rd property of COPS string)
|
|
|
|
operator_name = out.substr(++pos);
|
2022-07-11 21:03:55 +02:00
|
|
|
auto additional_comma = operator_name.find(','); // check for the optional ACT
|
|
|
|
if (additional_comma != std::string::npos && std::from_chars(operator_name.data() + additional_comma + 1, operator_name.data() + operator_name.length(), act).ec != std::errc::invalid_argument) {
|
|
|
|
operator_name = operator_name.substr(0, additional_comma);
|
|
|
|
}
|
|
|
|
// and strip quotes if present
|
|
|
|
auto quote1 = operator_name.find('"');
|
|
|
|
auto quote2 = operator_name.rfind('"');
|
|
|
|
if (quote1 != std::string::npos && quote2 != std::string::npos) {
|
|
|
|
operator_name = operator_name.substr(quote1 + 1, quote2 - 1);
|
|
|
|
}
|
2021-04-19 11:28:53 +02:00
|
|
|
return command_result::OK;
|
|
|
|
}
|
|
|
|
pos = out.find(',', ++pos);
|
|
|
|
}
|
|
|
|
return command_result::FAIL;
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result set_echo(CommandableIf *t, bool on)
|
2021-03-03 20:35:08 +01:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
|
|
|
if (on) {
|
2021-03-03 20:35:08 +01:00
|
|
|
return generic_command_common(t, "ATE1\r");
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
2021-03-03 20:35:08 +01:00
|
|
|
return generic_command_common(t, "ATE0\r");
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result set_pdp_context(CommandableIf *t, PdpContext &pdp)
|
2021-03-03 20:35:08 +01:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-03-03 20:35:08 +01:00
|
|
|
std::string pdp_command = "AT+CGDCONT=" + std::to_string(pdp.context_id) +
|
|
|
|
",\"" + pdp.protocol_type + "\",\"" + pdp.apn + "\"\r";
|
2022-09-09 11:23:21 +02:00
|
|
|
return generic_command_common(t, pdp_command, 150000);
|
2021-03-03 20:35:08 +01:00
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result set_data_mode(CommandableIf *t)
|
2021-03-03 20:35:08 +01:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-03-03 20:35:08 +01:00
|
|
|
return generic_command(t, "ATD*99##\r", "CONNECT", "ERROR", 5000);
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result set_data_mode_sim8xx(CommandableIf *t)
|
2021-04-19 11:28:53 +02:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2022-04-07 17:02:12 +02:00
|
|
|
return generic_command(t, "ATD*99#\r", "CONNECT", "ERROR", 5000);
|
2021-04-19 11:28:53 +02:00
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result resume_data_mode(CommandableIf *t)
|
2021-03-03 20:35:08 +01:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-03-03 20:35:08 +01:00
|
|
|
return generic_command(t, "ATO\r", "CONNECT", "ERROR", 5000);
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result set_command_mode(CommandableIf *t)
|
2021-03-03 20:35:08 +01:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-05-13 07:28:05 +02:00
|
|
|
const auto pass = std::list<std::string_view>({"NO CARRIER", "OK"});
|
|
|
|
const auto fail = std::list<std::string_view>({"ERROR"});
|
|
|
|
return generic_command(t, "+++", pass, fail, 5000);
|
2021-03-03 20:35:08 +01:00
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result get_imsi(CommandableIf *t, std::string &imsi_number)
|
2021-03-04 20:19:18 +01:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-03-04 20:19:18 +01:00
|
|
|
return generic_get_string(t, "AT+CIMI\r", imsi_number, 5000);
|
|
|
|
}
|
2021-03-03 20:35:08 +01:00
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result get_imei(CommandableIf *t, std::string &out)
|
2021-03-04 20:19:18 +01:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-03-04 20:19:18 +01:00
|
|
|
return generic_get_string(t, "AT+CGSN\r", out, 5000);
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result get_module_name(CommandableIf *t, std::string &out)
|
2021-03-04 20:19:18 +01:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-03-04 20:19:18 +01:00
|
|
|
return generic_get_string(t, "AT+CGMM\r", out, 5000);
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result sms_txt_mode(CommandableIf *t, bool txt = true)
|
2021-04-15 09:46:28 +02:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
|
|
|
if (txt) {
|
|
|
|
return generic_command_common(t, "AT+CMGF=1\r"); // Text mode (default)
|
|
|
|
}
|
2021-04-15 09:46:28 +02:00
|
|
|
return generic_command_common(t, "AT+CMGF=0\r"); // PDU mode
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result sms_character_set(CommandableIf *t)
|
2021-04-15 09:46:28 +02:00
|
|
|
{
|
|
|
|
// Sets the default GSM character set
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-04-15 09:46:28 +02:00
|
|
|
return generic_command_common(t, "AT+CSCS=\"GSM\"\r");
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result send_sms(CommandableIf *t, const std::string &number, const std::string &message)
|
2021-04-15 09:46:28 +02:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-04-15 09:46:28 +02:00
|
|
|
auto ret = t->command("AT+CMGS=\"" + number + "\"\r", [&](uint8_t *data, size_t len) {
|
2021-06-01 10:21:51 +02:00
|
|
|
std::string_view response((char *)data, len);
|
|
|
|
ESP_LOGD(TAG, "Send SMS response %.*s", static_cast<int>(response.size()), response.data());
|
2021-04-15 09:46:28 +02:00
|
|
|
if (response.find('>') != std::string::npos) {
|
|
|
|
return command_result::OK;
|
|
|
|
}
|
|
|
|
return command_result::TIMEOUT;
|
|
|
|
}, 5000, ' ');
|
2021-06-01 10:21:51 +02:00
|
|
|
if (ret != command_result::OK) {
|
2021-04-15 09:46:28 +02:00
|
|
|
return ret;
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
|
|
|
return generic_command_common(t, message + "\x1A", 120000);
|
2021-04-15 09:46:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result set_cmux(CommandableIf *t)
|
2021-03-04 20:19:18 +01:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-03-04 20:19:18 +01:00
|
|
|
return generic_command_common(t, "AT+CMUX=0\r");
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result read_pin(CommandableIf *t, bool &pin_ok)
|
2021-03-04 20:19:18 +01:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-05-13 07:28:05 +02:00
|
|
|
std::string_view out;
|
|
|
|
auto ret = generic_get_string(t, "AT+CPIN?\r", out);
|
2021-06-01 10:21:51 +02:00
|
|
|
if (ret != command_result::OK) {
|
2021-05-13 07:28:05 +02:00
|
|
|
return ret;
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
|
|
|
if (out.find("+CPIN:") == std::string::npos) {
|
2021-05-13 07:28:05 +02:00
|
|
|
return command_result::FAIL;
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
2021-05-13 07:28:05 +02:00
|
|
|
if (out.find("SIM PIN") != std::string::npos || out.find("SIM PUK") != std::string::npos) {
|
|
|
|
pin_ok = false;
|
|
|
|
return command_result::OK;
|
|
|
|
}
|
|
|
|
if (out.find("READY") != std::string::npos) {
|
|
|
|
pin_ok = true;
|
|
|
|
return command_result::OK;
|
|
|
|
}
|
|
|
|
return command_result::FAIL; // Neither pin-ok, nor waiting for pin/puk -> mark as error
|
2021-03-04 20:19:18 +01:00
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result set_pin(CommandableIf *t, const std::string &pin)
|
2021-03-04 20:19:18 +01:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-03-04 20:19:18 +01:00
|
|
|
std::string set_pin_command = "AT+CPIN=" + pin + "\r";
|
|
|
|
return generic_command_common(t, set_pin_command);
|
|
|
|
}
|
2021-03-02 21:01:44 +01:00
|
|
|
|
2022-09-09 11:23:21 +02:00
|
|
|
command_result at(CommandableIf *t, const std::string &cmd, std::string &out, int timeout = 500)
|
2022-05-31 14:22:41 +02:00
|
|
|
{
|
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
|
|
|
std::string at_command = cmd + "\r";
|
2022-09-09 11:23:21 +02:00
|
|
|
return generic_get_string(t, at_command, out, timeout);
|
2022-05-31 14:22:41 +02:00
|
|
|
}
|
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
command_result get_signal_quality(CommandableIf *t, int &rssi, int &ber)
|
2021-03-30 08:25:16 +02:00
|
|
|
{
|
2021-06-01 10:21:51 +02:00
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2021-05-13 07:28:05 +02:00
|
|
|
std::string_view out;
|
|
|
|
auto ret = generic_get_string(t, "AT+CSQ\r", out);
|
2021-06-01 10:21:51 +02:00
|
|
|
if (ret != command_result::OK) {
|
2021-05-13 07:28:05 +02:00
|
|
|
return ret;
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
2021-05-13 07:28:05 +02:00
|
|
|
|
|
|
|
constexpr std::string_view pattern = "+CSQ: ";
|
|
|
|
constexpr int rssi_pos = pattern.size();
|
|
|
|
int ber_pos;
|
|
|
|
if (out.find(pattern) == std::string::npos ||
|
2021-06-01 10:21:51 +02:00
|
|
|
(ber_pos = out.find(',')) == std::string::npos) {
|
2021-05-13 07:28:05 +02:00
|
|
|
return command_result::FAIL;
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
2021-05-13 07:28:05 +02:00
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
if (std::from_chars(out.data() + rssi_pos, out.data() + ber_pos, rssi).ec == std::errc::invalid_argument) {
|
2021-05-13 07:28:05 +02:00
|
|
|
return command_result::FAIL;
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
|
|
|
if (std::from_chars(out.data() + ber_pos + 1, out.data() + out.size(), ber).ec == std::errc::invalid_argument) {
|
2021-05-13 07:28:05 +02:00
|
|
|
return command_result::FAIL;
|
2021-06-01 10:21:51 +02:00
|
|
|
}
|
2021-05-13 07:28:05 +02:00
|
|
|
return command_result::OK;
|
2021-03-30 08:25:16 +02:00
|
|
|
}
|
|
|
|
|
2022-04-13 13:41:56 +02:00
|
|
|
command_result set_operator(CommandableIf *t, int mode, int format, const std::string &oper)
|
|
|
|
{
|
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
|
|
|
return generic_command_common(t, "AT+COPS=" + std::to_string(mode) + "," + std::to_string(format) + ",\"" + oper + "\"\r", 90000);
|
|
|
|
}
|
|
|
|
|
|
|
|
command_result set_network_attachment_state(CommandableIf *t, int state)
|
|
|
|
{
|
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
|
|
|
return generic_command_common(t, "AT+CGATT=" + std::to_string(state) + "\r");
|
|
|
|
}
|
|
|
|
|
|
|
|
command_result get_network_attachment_state(CommandableIf *t, int &state)
|
|
|
|
{
|
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
|
|
|
std::string_view out;
|
|
|
|
auto ret = generic_get_string(t, "AT+CGATT?\r", out);
|
|
|
|
if (ret != command_result::OK) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
constexpr std::string_view pattern = "+CGATT: ";
|
|
|
|
constexpr int pos = pattern.size();
|
|
|
|
if (out.find(pattern) == std::string::npos) {
|
|
|
|
return command_result::FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std::from_chars(out.data() + pos, out.data() + out.size(), state).ec == std::errc::invalid_argument) {
|
|
|
|
return command_result::FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return command_result::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
command_result set_radio_state(CommandableIf *t, int state)
|
|
|
|
{
|
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2022-09-09 11:23:21 +02:00
|
|
|
return generic_command_common(t, "AT+CFUN=" + std::to_string(state) + "\r", 15000);
|
2022-04-13 13:41:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
command_result get_radio_state(CommandableIf *t, int &state)
|
|
|
|
{
|
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
|
|
|
std::string_view out;
|
|
|
|
auto ret = generic_get_string(t, "AT+CFUN?\r", out);
|
|
|
|
if (ret != command_result::OK) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
constexpr std::string_view pattern = "+CFUN: ";
|
|
|
|
constexpr int pos = pattern.size();
|
|
|
|
if (out.find(pattern) == std::string::npos) {
|
|
|
|
return command_result::FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std::from_chars(out.data() + pos, out.data() + out.size(), state).ec == std::errc::invalid_argument) {
|
|
|
|
return command_result::FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return command_result::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
command_result set_network_mode(CommandableIf *t, int mode)
|
|
|
|
{
|
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
|
|
|
return generic_command_common(t, "AT+CNMP=" + std::to_string(mode) + "\r");
|
|
|
|
}
|
|
|
|
|
|
|
|
command_result set_preferred_mode(CommandableIf *t, int mode)
|
|
|
|
{
|
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
|
|
|
return generic_command_common(t, "AT+CMNB=" + std::to_string(mode) + "\r");
|
|
|
|
}
|
|
|
|
|
|
|
|
command_result set_network_bands(CommandableIf *t, const std::string &mode, const int *bands, int size)
|
|
|
|
{
|
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
|
|
|
std::string band_string = "";
|
2022-04-20 09:16:05 +02:00
|
|
|
for (int i = 0; i < size - 1; ++i) {
|
2022-04-13 13:41:56 +02:00
|
|
|
band_string += std::to_string(bands[i]) + ",";
|
|
|
|
}
|
|
|
|
band_string += std::to_string(bands[size - 1]);
|
2022-10-11 16:31:57 +02:00
|
|
|
|
2022-04-13 13:41:56 +02:00
|
|
|
return generic_command_common(t, "AT+CBANDCFG=\"" + mode + "\"," + band_string + "\r");
|
|
|
|
}
|
|
|
|
|
2022-04-20 09:37:39 +02:00
|
|
|
// mode is expected to be 64bit string (in hex)
|
|
|
|
// any_mode = "0xFFFFFFFF7FFFFFFF";
|
2022-04-19 16:23:22 +02:00
|
|
|
command_result set_network_bands_sim76xx(CommandableIf *t, const std::string &mode, const int *bands, int size)
|
|
|
|
{
|
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
2022-05-24 14:54:24 +02:00
|
|
|
static const char *hexDigits = "0123456789ABCDEF";
|
2022-04-19 16:23:22 +02:00
|
|
|
uint64_t band_bits = 0;
|
2022-05-24 14:54:24 +02:00
|
|
|
int hex_len = 16;
|
|
|
|
std::string band_string(hex_len, '0');
|
2022-04-19 16:23:22 +02:00
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
// OR-operation to add bands
|
2022-05-24 14:54:24 +02:00
|
|
|
auto band = bands[i] - 1; // Sim7600 has 0-indexed band selection (band 20 has to be shifted 19 places)
|
|
|
|
band_bits |= 1 << band;
|
2022-04-19 16:23:22 +02:00
|
|
|
}
|
2022-05-24 14:54:24 +02:00
|
|
|
for (int i = hex_len; i > 0; i--) {
|
|
|
|
band_string[i - 1] = hexDigits[(band_bits >> ((hex_len - i) * 4)) & 0xF];
|
|
|
|
}
|
|
|
|
return generic_command_common(t, "AT+CNBP=" + mode + ",0x" + band_string + "\r");
|
2022-04-19 16:23:22 +02:00
|
|
|
}
|
|
|
|
|
2022-04-13 13:41:56 +02:00
|
|
|
command_result get_network_system_mode(CommandableIf *t, int &mode)
|
|
|
|
{
|
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
|
|
|
std::string_view out;
|
|
|
|
auto ret = generic_get_string(t, "AT+CNSMOD?\r", out);
|
|
|
|
if (ret != command_result::OK) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr std::string_view pattern = "+CNSMOD: ";
|
|
|
|
int mode_pos = out.find(",") + 1; // Skip "<n>,"
|
|
|
|
if (out.find(pattern) == std::string::npos) {
|
|
|
|
return command_result::FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std::from_chars(out.data() + mode_pos, out.data() + out.size(), mode).ec == std::errc::invalid_argument) {
|
|
|
|
return command_result::FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return command_result::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
command_result set_gnss_power_mode(CommandableIf *t, int mode)
|
|
|
|
{
|
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
|
|
|
return generic_command_common(t, "AT+CGNSPWR=" + std::to_string(mode) + "\r");
|
|
|
|
}
|
|
|
|
|
2022-10-05 08:54:41 +02:00
|
|
|
command_result get_gnss_power_mode(CommandableIf *t, int &mode)
|
|
|
|
{
|
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
|
|
|
std::string_view out;
|
|
|
|
auto ret = generic_get_string(t, "AT+CGNSPWR?\r", out);
|
|
|
|
if (ret != command_result::OK) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
constexpr std::string_view pattern = "+CGNSPWR: ";
|
|
|
|
constexpr int pos = pattern.size();
|
|
|
|
if (out.find(pattern) == std::string::npos) {
|
|
|
|
return command_result::FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std::from_chars(out.data() + pos, out.data() + out.size(), mode).ec == std::errc::invalid_argument) {
|
|
|
|
return command_result::FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return command_result::OK;
|
|
|
|
}
|
|
|
|
|
2022-04-19 16:23:22 +02:00
|
|
|
command_result set_gnss_power_mode_sim76xx(CommandableIf *t, int mode)
|
|
|
|
{
|
|
|
|
ESP_LOGV(TAG, "%s", __func__ );
|
|
|
|
return generic_command_common(t, "AT+CGPS=" + std::to_string(mode) + "\r");
|
|
|
|
}
|
|
|
|
|
2022-05-31 14:22:41 +02:00
|
|
|
} // esp_modem::dce_commands
|