renamed from 'gsm' to 'modem'

This commit is contained in:
oarcher 2024-05-13 23:14:44 +02:00
parent a3ed854b51
commit 4f7bd585e9
4 changed files with 71 additions and 71 deletions

View file

@ -24,14 +24,14 @@ CONF_STATUS_PIN = "status_pin"
CONF_DTR_PIN = "dtr_pin" CONF_DTR_PIN = "dtr_pin"
gsm_ns = cg.esphome_ns.namespace("gsm") modem_ns = cg.esphome_ns.namespace("modem")
GSMComponent = gsm_ns.class_("GSMComponent", cg.Component) ModemComponent = modem_ns.class_("ModemComponent", cg.Component)
CONFIG_SCHEMA = cv.All( CONFIG_SCHEMA = cv.All(
cv.Schema( cv.Schema(
{ {
cv.GenerateID(): cv.declare_id(GSMComponent), cv.GenerateID(): cv.declare_id(ModemComponent),
cv.Required(CONF_TX_PIN): cv.positive_int, cv.Required(CONF_TX_PIN): cv.positive_int,
cv.Required(CONF_RX_PIN): cv.positive_int, cv.Required(CONF_RX_PIN): cv.positive_int,
cv.Required(CONF_MODEL): cv.string, cv.Required(CONF_MODEL): cv.string,
@ -75,7 +75,7 @@ async def to_code(config):
add_idf_sdkconfig_option("CONFIG_LWIP_PPP_VJ_HEADER_COMPRESSION", True) add_idf_sdkconfig_option("CONFIG_LWIP_PPP_VJ_HEADER_COMPRESSION", True)
add_idf_sdkconfig_option("CONFIG_LWIP_PPP_NOTIFY_PHASE_SUPPORT", True) add_idf_sdkconfig_option("CONFIG_LWIP_PPP_NOTIFY_PHASE_SUPPORT", True)
cg.add_define("USE_GSM") cg.add_define("USE_MODEM")
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
if use_address := config.get(CONF_USE_ADDRESS, None): if use_address := config.get(CONF_USE_ADDRESS, None):

View file

@ -1,5 +1,5 @@
#ifdef USE_ESP_IDF #ifdef USE_ESP_IDF
#include "gsm_component.h" #include "modem_component.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "esphome/core/application.h" #include "esphome/core/application.h"
#include "esphome/core/defines.h" #include "esphome/core/defines.h"
@ -22,9 +22,9 @@ static const size_t CONFIG_MODEM_UART_EVENT_TASK_STACK_SIZE = 2048;
static const uint8_t CONFIG_MODEM_UART_EVENT_TASK_PRIORITY = 5; static const uint8_t CONFIG_MODEM_UART_EVENT_TASK_PRIORITY = 5;
namespace esphome { namespace esphome {
namespace gsm { namespace modem {
GSMComponent *global_gsm_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) ModemComponent *global_modem_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
#define ESPHL_ERROR_CHECK(err, message) \ #define ESPHL_ERROR_CHECK(err, message) \
if ((err) != ESP_OK) { \ if ((err) != ESP_OK) { \
@ -35,15 +35,15 @@ GSMComponent *global_gsm_component; // NOLINT(cppcoreguidelines-avoid-non-const
using namespace esp_modem; using namespace esp_modem;
GSMComponent::GSMComponent() { global_gsm_component = this; } ModemComponent::ModemComponent() { global_modem_component = this; }
void GSMComponent::dump_config() { ESP_LOGCONFIG(TAG, "Config GSM:"); } void ModemComponent::dump_config() { ESP_LOGCONFIG(TAG, "Config Modem:"); }
float GSMComponent::get_setup_priority() const { return setup_priority::WIFI; } float ModemComponent::get_setup_priority() const { return setup_priority::WIFI; }
bool GSMComponent::can_proceed() { return this->is_connected(); } bool ModemComponent::can_proceed() { return this->is_connected(); }
network::IPAddresses GSMComponent::get_ip_addresses() { network::IPAddresses ModemComponent::get_ip_addresses() {
network::IPAddresses addresses; network::IPAddresses addresses;
esp_netif_ip_info_t ip; esp_netif_ip_info_t ip;
ESP_LOGV(TAG, "get_ip_addresses"); ESP_LOGV(TAG, "get_ip_addresses");
@ -58,19 +58,19 @@ network::IPAddresses GSMComponent::get_ip_addresses() {
return addresses; return addresses;
} }
std::string GSMComponent::get_use_address() const { std::string ModemComponent::get_use_address() const {
if (this->use_address_.empty()) { if (this->use_address_.empty()) {
return App.get_name() + ".local"; return App.get_name() + ".local";
} }
return this->use_address_; return this->use_address_;
} }
void GSMComponent::set_use_address(const std::string &use_address) { this->use_address_ = use_address; } void ModemComponent::set_use_address(const std::string &use_address) { this->use_address_ = use_address; }
bool GSMComponent::is_connected() { return this->state_ == GSMComponentState::CONNECTED; } bool ModemComponent::is_connected() { return this->state_ == ModemComponentState::CONNECTED; }
void GSMComponent::setup() { void ModemComponent::setup() {
ESP_LOGI(TAG, "Setting up GSM..."); ESP_LOGI(TAG, "Setting up Modem...");
this->config_gpio_(); this->config_gpio_();
@ -124,22 +124,22 @@ void GSMComponent::setup() {
// ESPHL_ERROR_CHECK(esp_netif_set_dns_info(this->ppp_netif_, ESP_NETIF_DNS_MAIN, &dns_main), "dns_main"); // ESPHL_ERROR_CHECK(esp_netif_set_dns_info(this->ppp_netif_, ESP_NETIF_DNS_MAIN, &dns_main), "dns_main");
// Register user defined event handers // Register user defined event handers
err = esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_GOT_IP, &GSMComponent::got_ip_event_handler, nullptr); err = esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_GOT_IP, &ModemComponent::got_ip_event_handler, nullptr);
ESPHL_ERROR_CHECK(err, "GOT IP event handler register error"); ESPHL_ERROR_CHECK(err, "GOT IP event handler register error");
ESP_LOGV(TAG, "DCE setup"); ESP_LOGV(TAG, "DCE setup");
switch (this->model_) { switch (this->model_) {
case GSMModel::BG96: case ModemModel::BG96:
this->dce_ = create_BG96_dce(&dce_config, this->dte_, this->ppp_netif_); this->dce_ = create_BG96_dce(&dce_config, this->dte_, this->ppp_netif_);
break; break;
case GSMModel::SIM800: case ModemModel::SIM800:
this->dce_ = create_SIM800_dce(&dce_config, this->dte_, this->ppp_netif_); this->dce_ = create_SIM800_dce(&dce_config, this->dte_, this->ppp_netif_);
break; break;
case GSMModel::SIM7000: case ModemModel::SIM7000:
this->dce_ = create_SIM7000_dce(&dce_config, this->dte_, this->ppp_netif_); this->dce_ = create_SIM7000_dce(&dce_config, this->dte_, this->ppp_netif_);
break; break;
case GSMModel::SIM7600: case ModemModel::SIM7600:
this->dce_ = create_SIM7600_dce(&dce_config, this->dte_, this->ppp_netif_); this->dce_ = create_SIM7600_dce(&dce_config, this->dte_, this->ppp_netif_);
break; break;
default: default:
@ -155,7 +155,7 @@ void GSMComponent::setup() {
ESP_LOGV(TAG, "Setup finished"); ESP_LOGV(TAG, "Setup finished");
} }
void GSMComponent::start_connect_() { void ModemComponent::start_connect_() {
this->connect_begin_ = millis(); this->connect_begin_ = millis();
this->status_set_warning("Starting connection"); this->status_set_warning("Starting connection");
@ -169,7 +169,7 @@ void GSMComponent::start_connect_() {
// ESP_LOGW(TAG, "esp_netif_set_hostname failed: %s", esp_err_to_name(err)); // ESP_LOGW(TAG, "esp_netif_set_hostname failed: %s", esp_err_to_name(err));
// } // }
global_gsm_component->got_ipv4_address_ = false; // why not this ? global_modem_component->got_ipv4_address_ = false; // why not this ?
this->dce_->set_mode(esp_modem::modem_mode::CMUX_MANUAL_COMMAND); this->dce_->set_mode(esp_modem::modem_mode::CMUX_MANUAL_COMMAND);
vTaskDelay(pdMS_TO_TICKS(2000)); vTaskDelay(pdMS_TO_TICKS(2000));
@ -232,56 +232,56 @@ void GSMComponent::start_connect_() {
vTaskDelay(pdMS_TO_TICKS(2000)); vTaskDelay(pdMS_TO_TICKS(2000));
} }
void GSMComponent::got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { void ModemComponent::got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data; ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
const esp_netif_ip_info_t *ip_info = &event->ip_info; const esp_netif_ip_info_t *ip_info = &event->ip_info;
ESP_LOGW(TAG, "[IP event] Got IP " IPSTR, IP2STR(&ip_info->ip)); ESP_LOGW(TAG, "[IP event] Got IP " IPSTR, IP2STR(&ip_info->ip));
vTaskDelay(pdMS_TO_TICKS(1000)); // FIXME tmp vTaskDelay(pdMS_TO_TICKS(1000)); // FIXME tmp
global_gsm_component->got_ipv4_address_ = true; global_modem_component->got_ipv4_address_ = true;
global_gsm_component->connected_ = true; global_modem_component->connected_ = true;
} }
void GSMComponent::loop() { void ModemComponent::loop() {
const uint32_t now = millis(); const uint32_t now = millis();
switch (this->state_) { switch (this->state_) {
case GSMComponentState::STOPPED: case ModemComponentState::STOPPED:
if (this->started_) { if (this->started_) {
ESP_LOGI(TAG, "Starting gsm connection"); ESP_LOGI(TAG, "Starting modem connection");
this->state_ = GSMComponentState::CONNECTING; this->state_ = ModemComponentState::CONNECTING;
this->start_connect_(); this->start_connect_();
} }
break; break;
case GSMComponentState::CONNECTING: case ModemComponentState::CONNECTING:
if (!this->started_) { if (!this->started_) {
ESP_LOGI(TAG, "Stopped ethernet connection"); ESP_LOGI(TAG, "Stopped ethernet connection");
this->state_ = GSMComponentState::STOPPED; this->state_ = ModemComponentState::STOPPED;
} else if (this->connected_) { } else if (this->connected_) {
// connection established // connection established
ESP_LOGI(TAG, "Connected via GSM"); ESP_LOGI(TAG, "Connected via Modem");
this->state_ = GSMComponentState::CONNECTED; this->state_ = ModemComponentState::CONNECTED;
this->dump_connect_params_(); this->dump_connect_params_();
this->status_clear_warning(); this->status_clear_warning();
} else if (now - this->connect_begin_ > 45000) { } else if (now - this->connect_begin_ > 45000) {
ESP_LOGW(TAG, "Connecting via GSM failed! Re-connecting..."); ESP_LOGW(TAG, "Connecting via Modem failed! Re-connecting...");
this->start_connect_(); this->start_connect_();
} }
break; break;
case GSMComponentState::CONNECTED: case ModemComponentState::CONNECTED:
if (!this->started_) { if (!this->started_) {
ESP_LOGI(TAG, "Stopped GSM connection"); ESP_LOGI(TAG, "Stopped Modem connection");
this->state_ = GSMComponentState::STOPPED; this->state_ = ModemComponentState::STOPPED;
} else if (!this->connected_) { } else if (!this->connected_) {
ESP_LOGW(TAG, "Connection via GSM lost! Re-connecting..."); ESP_LOGW(TAG, "Connection via Modem lost! Re-connecting...");
this->state_ = GSMComponentState::CONNECTING; this->state_ = ModemComponentState::CONNECTING;
this->start_connect_(); this->start_connect_();
} }
break; break;
} }
} }
void GSMComponent::dump_connect_params_() { void ModemComponent::dump_connect_params_() {
esp_netif_ip_info_t ip; esp_netif_ip_info_t ip;
esp_netif_get_ip_info(this->ppp_netif_, &ip); esp_netif_get_ip_info(this->ppp_netif_, &ip);
ESP_LOGCONFIG(TAG, " IP Address: %s", network::IPAddress(&ip.ip).str().c_str()); ESP_LOGCONFIG(TAG, " IP Address: %s", network::IPAddress(&ip.ip).str().c_str());
@ -298,7 +298,7 @@ void GSMComponent::dump_connect_params_() {
ESP_LOGCONFIG(TAG, " DNS fallback: %s", network::IPAddress(dns_fallback_ip).str().c_str()); ESP_LOGCONFIG(TAG, " DNS fallback: %s", network::IPAddress(dns_fallback_ip).str().c_str());
} }
void GSMComponent::config_gpio_() { void ModemComponent::config_gpio_() {
ESP_LOGV(TAG, "Configuring GPIOs..."); ESP_LOGV(TAG, "Configuring GPIOs...");
gpio_config_t io_conf = {}; gpio_config_t io_conf = {};
io_conf.intr_type = GPIO_INTR_DISABLE; io_conf.intr_type = GPIO_INTR_DISABLE;
@ -328,7 +328,7 @@ void GSMComponent::config_gpio_() {
gpio_config(&io_conf); gpio_config(&io_conf);
} }
void GSMComponent::poweron() { void ModemComponent::poweron() {
ESP_LOGI(TAG, "Power on modem"); ESP_LOGI(TAG, "Power on modem");
if (this->get_status()) { if (this->get_status()) {
@ -372,7 +372,7 @@ void GSMComponent::poweron() {
App.feed_wdt(); App.feed_wdt();
} }
void GSMComponent::powerdown() { void ModemComponent::powerdown() {
ESP_LOGI(TAG, "Power down modem"); ESP_LOGI(TAG, "Power down modem");
if (this->get_status()) { if (this->get_status()) {
// https://github.com/Xinyuan-LilyGO/T-SIM7600X/blob/master/examples/PowefOffModem/PowefOffModem.ino#L69-L71 // https://github.com/Xinyuan-LilyGO/T-SIM7600X/blob/master/examples/PowefOffModem/PowefOffModem.ino#L69-L71
@ -393,7 +393,7 @@ void GSMComponent::powerdown() {
} }
} }
} // namespace gsm } // namespace modem
} // namespace esphome } // namespace esphome
#endif #endif

View file

@ -16,21 +16,21 @@ using esphome::esp_log_printf_; // esp_modem will use esphome logger (needed if
#include <utility> #include <utility>
namespace esphome { namespace esphome {
namespace gsm { namespace modem {
static const char *const TAG = "gsm"; static const char *const TAG = "modem";
enum class GSMComponentState { enum class ModemComponentState {
STOPPED, STOPPED,
CONNECTING, CONNECTING,
CONNECTED, CONNECTED,
}; };
enum class GSMModel { BG96, SIM800, SIM7000, SIM7070, SIM7070_GNSS, SIM7600, UNKNOWN }; enum class ModemModel { BG96, SIM800, SIM7000, SIM7070, SIM7070_GNSS, SIM7600, UNKNOWN };
class GSMComponent : public Component { class ModemComponent : public Component {
public: public:
GSMComponent(); ModemComponent();
void dump_config() override; void dump_config() override;
void setup() override; void setup() override;
void loop() override; void loop() override;
@ -54,7 +54,7 @@ class GSMComponent : public Component {
void set_status_pin(gpio_num_t status_pin) { this->status_pin_ = status_pin; } void set_status_pin(gpio_num_t status_pin) { this->status_pin_ = status_pin; }
void set_dtr_pin(gpio_num_t dtr_pin) { this->dtr_pin_ = dtr_pin; } void set_dtr_pin(gpio_num_t dtr_pin) { this->dtr_pin_ = dtr_pin; }
void set_model(const std::string &model) { void set_model(const std::string &model) {
this->model_ = this->gsm_model_map_.count(model) ? gsm_model_map_[model] : GSMModel::UNKNOWN; this->model_ = this->modem_model_map_.count(model) ? modem_model_map_[model] : ModemModel::UNKNOWN;
} }
bool get_status() { return gpio_get_level(this->status_pin_); } bool get_status() { return gpio_get_level(this->status_pin_); }
@ -70,18 +70,18 @@ class GSMComponent : public Component {
std::string username_; std::string username_;
std::string password_; std::string password_;
std::string apn_; std::string apn_;
GSMModel model_; ModemModel model_;
std::unordered_map<std::string, GSMModel> gsm_model_map_ = {{"BG96", GSMModel::BG96}, std::unordered_map<std::string, ModemModel> modem_model_map_ = {{"BG96", ModemModel::BG96},
{"SIM800", GSMModel::SIM800}, {"SIM800", ModemModel::SIM800},
{"SIM7000", GSMModel::SIM7000}, {"SIM7000", ModemModel::SIM7000},
{"SIM7070", GSMModel::SIM7070}, {"SIM7070", ModemModel::SIM7070},
{"SIM7070_GNSS", GSMModel::SIM7070_GNSS}, {"SIM7070_GNSS", ModemModel::SIM7070_GNSS},
{"SIM7600", GSMModel::SIM7600}}; {"SIM7600", ModemModel::SIM7600}};
std::shared_ptr<esp_modem::DTE> dte_; std::shared_ptr<esp_modem::DTE> dte_;
std::unique_ptr<esp_modem::DCE> dce_; // public ? std::unique_ptr<esp_modem::DCE> dce_; // public ?
esp_modem::esp_netif_t *ppp_netif_{nullptr}; esp_modem::esp_netif_t *ppp_netif_{nullptr};
esp_modem_dte_config_t dte_config_; esp_modem_dte_config_t dte_config_;
GSMComponentState state_{GSMComponentState::STOPPED}; ModemComponentState state_{ModemComponentState::STOPPED};
void start_connect_(); void start_connect_();
bool started_{false}; bool started_{false};
bool connected_{false}; bool connected_{false};
@ -94,9 +94,9 @@ class GSMComponent : public Component {
}; };
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
extern GSMComponent *global_gsm_component; extern ModemComponent *global_modem_component;
} // namespace gsm } // namespace modem
} // namespace esphome } // namespace esphome
#endif #endif

View file

@ -9,8 +9,8 @@
#include "esphome/components/ethernet/ethernet_component.h" #include "esphome/components/ethernet/ethernet_component.h"
#endif #endif
#ifdef USE_GSM #ifdef USE_MODEM
#include "esphome/components/gsm/gsm_component.h" #include "esphome/components/modem/modem_component.h"
#endif #endif
namespace esphome { namespace esphome {
@ -27,9 +27,9 @@ bool is_connected() {
return wifi::global_wifi_component->is_connected(); return wifi::global_wifi_component->is_connected();
#endif #endif
#ifdef USE_GSM #ifdef USE_MODEM
if (gsm::global_gsm_component != nullptr) if (modem::global_modem_component != nullptr)
return gsm::global_gsm_component->is_connected(); return modem::global_modem_component->is_connected();
#endif #endif
#ifdef USE_HOST #ifdef USE_HOST
@ -68,9 +68,9 @@ std::string get_use_address() {
if (wifi::global_wifi_component != nullptr) if (wifi::global_wifi_component != nullptr)
return wifi::global_wifi_component->get_use_address(); return wifi::global_wifi_component->get_use_address();
#endif #endif
#ifdef USE_GSM #ifdef USE_MODEM
if (gsm::global_gsm_component != nullptr) if (modem::global_modem_component != nullptr)
return gsm::global_gsm_component->get_use_address(); return modem::global_modem_component->get_use_address();
#endif #endif
return ""; return "";
} }