From 76fd82c44aaae3cfaf745d3cb75627fed7c88637 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Tue, 16 Aug 2022 16:57:47 +0200 Subject: [PATCH] added config for GSM and ethernet, added GSM transport --- CMakeLists.txt | 3 + Kconfig | 136 +++++++++++++- include/SystemConfiguration.h | 8 +- src/EthTransport.c | 341 ++++++++++++++++++++++++++++++++++ src/GSMTransport.c | 277 +++++++++++++++++++++++++++ src/HTTPPostSystem.c | 3 +- src/HTTPPrintSystem.c | 161 ++++++++++++++++ 7 files changed, 923 insertions(+), 6 deletions(-) create mode 100644 src/EthTransport.c create mode 100644 src/GSMTransport.c diff --git a/CMakeLists.txt b/CMakeLists.txt index ca2bd86..f22cede 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ idf_component_register( "src/Helpers.c" "src/NetTransport.c" "src/WiFiTransport.c" + "src/GSMTransport.c" INCLUDE_DIRS "." "include" "src" REQUIRES nvs_flash @@ -15,4 +16,6 @@ idf_component_register( mbedtls lwip mqtt + esp_modem + ) \ No newline at end of file diff --git a/Kconfig b/Kconfig index 93b33df..0230697 100644 --- a/Kconfig +++ b/Kconfig @@ -180,12 +180,144 @@ menu "WebGuiApp configuration" help Set enabled GPRS adapter - if WEBGUIAPP_GPRS_ENABLE + if WEBGUIAPP_GPRS_ENABLE config WEBGUIAPP_GPRS_ON bool "Default GPRS switched on" default n - + + choice MODEM_DEVICE + prompt "Choose supported modem device (DCE)" + default MODEM_DEVICE_SIM800 + help + Select modem device connected to the ESP DTE. + config MODEM_DEVICE_SIM800 + bool "SIM800" + help + SIMCom SIM800L is a GSM/GPRS module. + It supports Quad-band 850/900/1800/1900MHz. + config MODEM_DEVICE_BG96 + bool "BG96" + help + Quectel BG96 is a series of LTE Cat M1/Cat NB1/EGPRS module. + config MODEM_DEVICE_SIM7600 + bool "SIM7600" + help + SIM7600 is Multi-Band LTE-TDD/LTE-FDD/HSPA+ and GSM/GPRS/EDGE module + endchoice + + config MODEM_PPP_APN + string "Set MODEM APN" + default "internet" + help + Set APN (Access Point Name), a logical name to choose data network + + config MODEM_PPP_AUTH_USERNAME + string "Set username for authentication" + default "gdata" + depends on !MODEM_PPP_AUTH_NONE + help + Set username for PPP Authentication. + + config MODEM_PPP_AUTH_PASSWORD + string "Set password for authentication" + default "gdata" + depends on !MODEM_PPP_AUTH_NONE + help + Set password for PPP Authentication. + + config MODEM_PPP_AUTH_NONE + bool "Skip PPP authentication" + default n + help + Set to true for the PPP client to skip authentication + + + config MODEM_NEED_SIM_PIN + bool "SIM PIN needed" + default n + help + Enable to set SIM PIN before starting the example + + config MODEM_SIM_PIN + string "Set SIM PIN" + default "1234" + depends on MODEM_NEED_SIM_PIN + help + Pin to unlock the SIM + + menu "UART Configuration" + config MODEM_UART_TX_PIN + int "TXD Pin Number" + default 17 + range 0 31 + help + Pin number of UART TX. + + config MODEM_UART_RX_PIN + int "RXD Pin Number" + default 16 + range 0 31 + help + Pin number of UART RX. + + config MODEM_UART_RTS_PIN + int "RTS Pin Number" + default 0 + range 0 31 + help + Pin number of UART RTS. + + config MODEM_UART_CTS_PIN + int "CTS Pin Number" + default 0 + range 0 31 + help + Pin number of UART CTS. + + config 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 MODEM_UART_EVENT_TASK_PRIORITY + int "UART Event Task Priority" + range 3 22 + default 5 + help + Priority of UART event task. + + config MODEM_UART_EVENT_QUEUE_SIZE + int "UART Event Queue Size" + range 10 40 + default 30 + help + Length of UART event queue. + + config MODEM_UART_PATTERN_QUEUE_SIZE + int "UART Pattern Queue Size" + range 10 40 + default 20 + help + Length of UART pattern queue. + + config MODEM_UART_TX_BUFFER_SIZE + int "UART TX Buffer Size" + range 256 2048 + default 512 + help + Buffer size of UART TX buffer. + + config MODEM_UART_RX_BUFFER_SIZE + int "UART RX Buffer Size" + range 256 2048 + default 1024 + help + Buffer size of UART RX buffer. + endmenu + endif endmenu diff --git a/include/SystemConfiguration.h b/include/SystemConfiguration.h index f2537c6..986e6f8 100644 --- a/include/SystemConfiguration.h +++ b/include/SystemConfiguration.h @@ -153,6 +153,7 @@ } Flags1; } mqttStation[MQTT_CLIENTS_NUM]; +#if CONFIG_WEBGUIAPP_ETHERNET_ENABLE struct { ip4_addr_t IPAddr; // IP address @@ -176,9 +177,9 @@ } Flags1; // Flag structure } ethSettings; +#endif - -#ifdef CONFIG_WEBGUIAPP_WIFI_ENABLE +#if CONFIG_WEBGUIAPP_WIFI_ENABLE struct { ip4_addr_t InfIPAddr; // IP address in infrastructure(INF) mode @@ -212,7 +213,7 @@ } wifiSettings; #endif - +#if CONFIG_WEBGUIAPP_GPRS_ENABLE struct { ip4_addr_t IPAddr; // IP address @@ -235,6 +236,7 @@ } Flags1; // Flag structure } gsmSettings; +#endif } SYS_CONFIG; diff --git a/src/EthTransport.c b/src/EthTransport.c new file mode 100644 index 0000000..7068acb --- /dev/null +++ b/src/EthTransport.c @@ -0,0 +1,341 @@ + /* Copyright 2022 Bogdan Pilyugin + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * File name: EthTransport.c + * Project: ChargePointMainboard + * Created on: 2022-07-21 + * Author: Bogdan Pilyugin + * Description: + */ + +#include "AppConfiguration.h" +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_netif.h" +#include "esp_eth.h" +#include "esp_event.h" +#include "esp_log.h" +#include "driver/gpio.h" +#include "NetTransport.h" +#include "sdkconfig.h" +#if CONFIG_ETH_USE_SPI_ETHERNET +#include "driver/spi_master.h" +#endif // CONFIG_ETH_USE_SPI_ETHERNET +//#include "PortExtender.h" + +static const char *TAG = "EthTransport"; +esp_netif_t *eth_netif_spi[CONFIG_SPI_ETHERNETS_NUM] = { NULL }; +static bool isEthConn = false; + +#if CONFIG_USE_SPI_ETHERNET +#define INIT_SPI_ETH_MODULE_CONFIG(eth_module_config, num) \ + do { \ + eth_module_config[num].spi_cs_gpio = CONFIG_ETH_SPI_CS ##num## _GPIO; \ + eth_module_config[num].int_gpio = CONFIG_ETH_SPI_INT ##num## _GPIO; \ + eth_module_config[num].phy_reset_gpio = CONFIG_ETH_SPI_PHY_RST ##num## _GPIO; \ + eth_module_config[num].phy_addr = CONFIG_ETH_SPI_PHY_ADDR ##num; \ + } while(0) + +typedef struct +{ + uint8_t spi_cs_gpio; + uint8_t int_gpio; + int8_t phy_reset_gpio; + uint8_t phy_addr; +} spi_eth_module_config_t; +#endif + +esp_netif_t* GetETHNetifAdapter(void) +{ + return eth_netif_spi[0]; +} + +bool isETHConnected(void) +{ + return isEthConn; +} + +/** Event handler for Ethernet events */ +static void eth_event_handler(void *arg, esp_event_base_t event_base, + int32_t event_id, + void *event_data) +{ + uint8_t mac_addr[6] = { 0 }; + /* we can get the ethernet driver handle from event data */ + esp_eth_handle_t eth_handle = *(esp_eth_handle_t*) event_data; + + switch (event_id) + { + case ETHERNET_EVENT_CONNECTED: + esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr); + ESP_LOGI(TAG, "Ethernet Link Up"); + ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x", + mac_addr[0], + mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); + break; + case ETHERNET_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "Ethernet Link Down"); + isEthConn = false; + + break; + case ETHERNET_EVENT_START: + ESP_LOGI(TAG, "Ethernet Started"); + break; + case ETHERNET_EVENT_STOP: + ESP_LOGI(TAG, "Ethernet Stopped"); + break; + default: + break; + } +} + +/** Event handler for IP_EVENT_ETH_GOT_IP */ +static void 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; + const esp_netif_ip_info_t *ip_info = &event->ip_info; + ESP_LOGI(TAG, "Ethernet Got IP Address"); + ESP_LOGI(TAG, "~~~~~~~~~~~"); + ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip)); + ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask)); + ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw)); + ESP_LOGI(TAG, "~~~~~~~~~~~"); + isEthConn = true; +} + +static void on_ethernet_got_ip(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) +{ + +} + +static void eth_init(void *pvParameter) +{ + //SetETH_RST(0); + gpio_set_level(CONFIG_ETH_SPI_PHY_RST0_GPIO, 0); + vTaskDelay(pdMS_TO_TICKS(10)); + //SetETH_RST(1); + gpio_set_level(CONFIG_ETH_SPI_PHY_RST0_GPIO, 1); + vTaskDelay(pdMS_TO_TICKS(10)); + +#if CONFIG_USE_INTERNAL_ETHERNET + // Create new default instance of esp-netif for Ethernet + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); + esp_netif_t *eth_netif = esp_netif_new(&cfg); + + // Init MAC and PHY configs to default + eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); + eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); + + phy_config.phy_addr = CONFIG_ETH_PHY_ADDR; + phy_config.reset_gpio_num = CONFIG_ETH_PHY_RST_GPIO; + mac_config.smi_mdc_gpio_num = CONFIG_ETH_MDC_GPIO; + mac_config.smi_mdio_gpio_num = CONFIG_ETH_MDIO_GPIO; + esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config); +#if CONFIG_ETH_PHY_IP101 + esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config); +#elif CONFIG_ETH_PHY_RTL8201 + esp_eth_phy_t *phy = esp_eth_phy_new_rtl8201(&phy_config); +#elif CONFIG_ETH_PHY_LAN87XX + esp_eth_phy_t *phy = esp_eth_phy_new_lan87xx(&phy_config); +#elif CONFIG_ETH_PHY_DP83848 + esp_eth_phy_t *phy = esp_eth_phy_new_dp83848(&phy_config); +#elif CONFIG_ETH_PHY_KSZ8041 + esp_eth_phy_t *phy = esp_eth_phy_new_ksz8041(&phy_config); +#elif CONFIG_ETH_PHY_KSZ8081 + esp_eth_phy_t *phy = esp_eth_phy_new_ksz8081(&phy_config); +#endif + esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); + esp_eth_handle_t eth_handle = NULL; + ESP_ERROR_CHECK(esp_eth_driver_install(&config, ð_handle)); + /* attach Ethernet driver to TCP/IP stack */ + ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle))); +#endif //CONFIG_USE_INTERNAL_ETHERNET + +#if CONFIG_USE_SPI_ETHERNET + // Create instance(s) of esp-netif for SPI Ethernet(s) + esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH(); + esp_netif_config_t cfg_spi = { + .base = &esp_netif_config, + .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH + }; + + char if_key_str[10]; + char if_desc_str[10]; + char num_str[3]; + for (int i = 0; i < CONFIG_SPI_ETHERNETS_NUM; i++) + { + itoa(i, num_str, 10); + strcat(strcpy(if_key_str, "ETH_SPI_"), num_str); + strcat(strcpy(if_desc_str, "eth"), num_str); + esp_netif_config.if_key = if_key_str; + esp_netif_config.if_desc = if_desc_str; + esp_netif_config.route_prio = ETH_PRIO-i; + eth_netif_spi[i] = esp_netif_new(&cfg_spi); + } + + // Init MAC and PHY configs to default + eth_mac_config_t mac_config_spi = ETH_MAC_DEFAULT_CONFIG(); + eth_phy_config_t phy_config_spi = ETH_PHY_DEFAULT_CONFIG(); + + // Install GPIO ISR handler to be able to service SPI Eth modlues interrupts + //gpio_install_isr_service(0); + + // Init SPI bus + spi_device_handle_t spi_handle[CONFIG_SPI_ETHERNETS_NUM] = { NULL }; + + // Init specific SPI Ethernet module configuration from Kconfig (CS GPIO, Interrupt GPIO, etc.) + spi_eth_module_config_t spi_eth_module_config[CONFIG_SPI_ETHERNETS_NUM]; + INIT_SPI_ETH_MODULE_CONFIG(spi_eth_module_config, 0); +#if CONFIG_SPI_ETHERNETS_NUM > 1 + INIT_SPI_ETH_MODULE_CONFIG(spi_eth_module_config, 1); +#endif + + // Configure SPI interface and Ethernet driver for specific SPI module + esp_eth_mac_t *mac_spi[CONFIG_SPI_ETHERNETS_NUM]; + esp_eth_phy_t *phy_spi[CONFIG_SPI_ETHERNETS_NUM]; + esp_eth_handle_t eth_handle_spi[CONFIG_SPI_ETHERNETS_NUM] = { NULL }; +#if CONFIG_EXAMPLE_USE_KSZ8851SNL + spi_device_interface_config_t devcfg = { + .mode = 0, + .clock_speed_hz = CONFIG_ETH_SPI_CLOCK_MHZ * 1000 * 1000, + .queue_size = 20 + }; + + for (int i = 0; i < CONFIG_SPI_ETHERNETS_NUM; i++) { + // Set SPI module Chip Select GPIO + devcfg.spics_io_num = spi_eth_module_config[i].spi_cs_gpio; + + ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_SPI_HOST, &devcfg, &spi_handle[i])); + // KSZ8851SNL ethernet driver is based on spi driver + eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(spi_handle[i]); + + // Set remaining GPIO numbers and configuration used by the SPI module + ksz8851snl_config.int_gpio_num = spi_eth_module_config[i].int_gpio; + phy_config_spi.phy_addr = spi_eth_module_config[i].phy_addr; + phy_config_spi.reset_gpio_num = spi_eth_module_config[i].phy_reset_gpio; + + mac_spi[i] = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config_spi); + phy_spi[i] = esp_eth_phy_new_ksz8851snl(&phy_config_spi); + } +#elif CONFIG_DM9051 + spi_device_interface_config_t devcfg = { + .command_bits = 1, + .address_bits = 7, + .mode = 0, + .clock_speed_hz = CONFIG_ETH_SPI_CLOCK_MHZ * 1000 * 1000, + .queue_size = 20 + }; + + for (int i = 0; i < CONFIG_SPI_ETHERNETS_NUM; i++) { + // Set SPI module Chip Select GPIO + devcfg.spics_io_num = spi_eth_module_config[i].spi_cs_gpio; + + ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_SPI_HOST, &devcfg, &spi_handle[i])); + // dm9051 ethernet driver is based on spi driver + eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle[i]); + + // Set remaining GPIO numbers and configuration used by the SPI module + dm9051_config.int_gpio_num = spi_eth_module_config[i].int_gpio; + phy_config_spi.phy_addr = spi_eth_module_config[i].phy_addr; + phy_config_spi.reset_gpio_num = spi_eth_module_config[i].phy_reset_gpio; + + mac_spi[i] = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config_spi); + phy_spi[i] = esp_eth_phy_new_dm9051(&phy_config_spi); + } +#elif CONFIG_W5500 + spi_device_interface_config_t devcfg = { + .command_bits = 16, // Actually it's the address phase in W5500 SPI frame + .address_bits = 8, // Actually it's the control phase in W5500 SPI frame + .mode = 0, + .clock_speed_hz = CONFIG_ETH_SPI_CLOCK_MHZ * 1000 * 1000, + .queue_size = 20 + }; + + for (int i = 0; i < CONFIG_SPI_ETHERNETS_NUM; i++) + { + // Set SPI module Chip Select GPIO + devcfg.spics_io_num = spi_eth_module_config[i].spi_cs_gpio; + + ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_SPI_HOST, &devcfg, &spi_handle[i])); + ESP_LOGI(TAG, "ETHERNET SPI device added OK"); + // w5500 ethernet driver is based on spi driver + eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle[i]); + + // Set remaining GPIO numbers and configuration used by the SPI module + w5500_config.int_gpio_num = spi_eth_module_config[i].int_gpio; + phy_config_spi.phy_addr = spi_eth_module_config[i].phy_addr; + phy_config_spi.reset_gpio_num = spi_eth_module_config[i].phy_reset_gpio; + + mac_spi[i] = esp_eth_mac_new_w5500(&w5500_config, &mac_config_spi); + phy_spi[i] = esp_eth_phy_new_w5500(&phy_config_spi); + } +#endif //CONFIG_EXAMPLE_USE_W5500 + + for (int i = 0; i < CONFIG_SPI_ETHERNETS_NUM; i++) + { + esp_eth_config_t eth_config_spi = ETH_DEFAULT_CONFIG(mac_spi[i], phy_spi[i]); + ESP_ERROR_CHECK(esp_eth_driver_install(ð_config_spi, ð_handle_spi[i])); + + /* The SPI Ethernet module might not have a burned factory MAC address, we cat to set it manually. + 02:00:00 is a Locally Administered OUI range so should not be used except when testing on a LAN under your control. + */ + uint8_t mac_addr[6] = { 0 }; + if (esp_read_mac(mac_addr, ESP_MAC_ETH) == ESP_OK) + { + ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle_spi[i], ETH_CMD_S_MAC_ADDR, mac_addr)); + } + else + { + ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle_spi[i], ETH_CMD_S_MAC_ADDR, (uint8_t[] ) { + 0x02, 0x00, 0x00, 0x12, 0x34, 0x56 + i})); + } + // attach Ethernet driver to TCP/IP stack + ESP_ERROR_CHECK(esp_netif_attach(eth_netif_spi[i], esp_eth_new_netif_glue(eth_handle_spi[i]))); + esp_netif_dns_info_t fldns; + //esp_netif_str_to_ip4(&GetAppConf()->ethSettings.DNSAddr3, (esp_ip4_addr_t*) (&fldns.ip)); + memcpy(&fldns.ip, &GetAppConf()->ethSettings.DNSAddr3, sizeof(esp_ip4_addr_t)); + esp_netif_set_dns_info(eth_netif_spi[i], ESP_NETIF_DNS_FALLBACK, &fldns); + } +#endif // CONFIG_ETH_USE_SPI_ETHERNET + + // Register user defined event handers + ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_ethernet_got_ip, NULL)); + + /* start Ethernet driver state machine */ +#if CONFIG_USE_INTERNAL_ETHERNET + ESP_ERROR_CHECK(esp_eth_start(eth_handle)); +#endif // CONFIG_USE_INTERNAL_ETHERNET +#if CONFIG_USE_SPI_ETHERNET + for (int i = 0; i < CONFIG_SPI_ETHERNETS_NUM; i++) + { + ESP_ERROR_CHECK(esp_eth_start(eth_handle_spi[i])); + } +#endif // CONFIG_USE_SPI_ETHERNET + + vTaskDelete(NULL); +} + + +void EthStart(void) +{ + xTaskCreate(eth_init, "EthInitTask", 1024 * 4, (void*) 0, 3, NULL); +} + + diff --git a/src/GSMTransport.c b/src/GSMTransport.c new file mode 100644 index 0000000..5bacda8 --- /dev/null +++ b/src/GSMTransport.c @@ -0,0 +1,277 @@ + /* Copyright 2022 Bogdan Pilyugin + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * File name: GSMTransport.c + * Project: ChargePointMainboard + * Created on: 2022-07-21 + * Author: Bogdan Pilyugin + * Description: + */ + +#include +#include "freertos/FreeRTOS.h" +#include "freertos/event_groups.h" +#include "freertos/task.h" +#include "esp_netif.h" +#include "esp_netif_ppp.h" +#include "esp_event.h" +#include "esp_modem_api.h" +#include "esp_log.h" +#include "driver/gpio.h" +#include "SystemConfiguration.h" +#include "NetTransport.h" + + +#if CONFIG_WEBGUIAPP_GPRS_ENABLE +static EventGroupHandle_t event_group = NULL; +static const int CONNECT_BIT = BIT0; +static const char *TAG = "GSMTransport"; +static int ResetType = 0; +static bool isPPPinitializing = false; +#endif + +static bool isPPPConn = false; +TaskHandle_t initTaskhandle; + + +#define PPP_MODEM_TIMEOUT 40 + +MODEM_INFO mod_info = { "-", "-", "-", "-" }; +esp_netif_t *ppp_netif; +esp_modem_dce_t *dce; +TaskHandle_t trasporttask; + + + +esp_netif_t* GetPPPNetifAdapter(void) +{ + return ppp_netif; +} + +MODEM_INFO* GetPPPModemInfo(void) +{ + return &mod_info; +} + +bool isPPPConnected(void) +{ + return isPPPConn; +} + +#if CONFIG_WEBGUIAPP_GPRS_ENABLE +static void on_ppp_changed(void *arg, esp_event_base_t event_base, + int32_t event_id, + void *event_data) +{ + ESP_LOGI(TAG, "PPP state changed event %d", event_id); + if (event_id == NETIF_PPP_ERRORUSER) + { + /* User interrupted event from esp-netif */ + esp_netif_t *netif = event_data; + ESP_LOGI(TAG, "User interrupted event from netif:%p", netif); + } +} + +static void on_ip_event(void *arg, esp_event_base_t event_base, + int32_t event_id, + void *event_data) +{ + ESP_LOGD(TAG, "IP event! %d", event_id); + if (event_id == IP_EVENT_PPP_GOT_IP) + { + esp_netif_dns_info_t dns_info; + + ip_event_got_ip_t *event = (ip_event_got_ip_t*) event_data; + esp_netif_t *netif = event->esp_netif; + + ESP_LOGI(TAG, "Modem Connect to PPP Server"); + ESP_LOGI(TAG, "~~~~~~~~~~~~~~"); + ESP_LOGI(TAG, "IP : " IPSTR, IP2STR(&event->ip_info.ip)); + ESP_LOGI(TAG, "Netmask : " IPSTR, IP2STR(&event->ip_info.netmask)); + ESP_LOGI(TAG, "Gateway : " IPSTR, IP2STR(&event->ip_info.gw)); + esp_netif_get_dns_info(netif, 0, &dns_info); + ESP_LOGI(TAG, "Name Server1: " IPSTR, IP2STR(&dns_info.ip.u_addr.ip4)); + esp_netif_get_dns_info(netif, 1, &dns_info); + ESP_LOGI(TAG, "Name Server2: " IPSTR, IP2STR(&dns_info.ip.u_addr.ip4)); + ESP_LOGI(TAG, "~~~~~~~~~~~~~~"); + xEventGroupSetBits(event_group, CONNECT_BIT); + isPPPConn = true; + ESP_LOGI(TAG, "GOT ip event!!!"); + } + else if (event_id == IP_EVENT_PPP_LOST_IP) + { + ESP_LOGI(TAG, "Modem Disconnect from PPP Server"); + isPPPConn = false; + } + else if (event_id == IP_EVENT_GOT_IP6) + { + ESP_LOGI(TAG, "GOT IPv6 event!"); + ip_event_got_ip6_t *event = (ip_event_got_ip6_t*) event_data; + ESP_LOGI(TAG, "Got IPv6 address " IPV6STR, IPV62STR(event->ip6_info.ip)); + } +} + +void ModemNotReady(void) +{ + +} + +static void GSMInitTask(void *pvParameter) +{ + isPPPinitializing = true; + int starttype = *((int*) pvParameter); + esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, &on_ip_event); + esp_event_handler_unregister(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed); + + if (dce) + { + esp_modem_destroy(dce); + } + + if (ppp_netif != NULL) + { + esp_netif_destroy(ppp_netif); + } + + if (starttype == 0) + { + //SetGSM_PWR(OFF); + gpio_set_level(GPIO_NUM_2, 0); + vTaskDelay(pdMS_TO_TICKS(1000)); + //SetGSM_PWR(ON); + gpio_set_level(GPIO_NUM_2, 1); + } + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &on_ip_event, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed, NULL)); + event_group = xEventGroupCreate(); + /* 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.uart_config.tx_io_num = CONFIG_MODEM_UART_TX_PIN; + dte_config.uart_config.rx_io_num = CONFIG_MODEM_UART_RX_PIN; + dte_config.uart_config.rts_io_num = CONFIG_MODEM_UART_RTS_PIN; + dte_config.uart_config.cts_io_num = CONFIG_MODEM_UART_CTS_PIN; + dte_config.uart_config.rx_buffer_size = CONFIG_MODEM_UART_RX_BUFFER_SIZE; + dte_config.uart_config.tx_buffer_size = CONFIG_MODEM_UART_TX_BUFFER_SIZE; + dte_config.uart_config.event_queue_size = CONFIG_MODEM_UART_EVENT_QUEUE_SIZE; + dte_config.task_stack_size = CONFIG_MODEM_UART_EVENT_TASK_STACK_SIZE; + dte_config.task_priority = CONFIG_MODEM_UART_EVENT_TASK_PRIORITY; + dte_config.dte_buffer_size = CONFIG_MODEM_UART_RX_BUFFER_SIZE / 2; + /* Configure the DCE */ + esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(CONFIG_MODEM_PPP_APN); + /* Configure the PPP netif */ + esp_netif_inherent_config_t esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_PPP() + ; + esp_netif_conf.route_prio = PPP_PRIO; + esp_netif_config_t netif_ppp_config = ESP_NETIF_DEFAULT_PPP(); + + netif_ppp_config.base = &esp_netif_conf; + /* Init netif object */ + ppp_netif = esp_netif_new(&netif_ppp_config); + assert(ppp_netif); + dce = esp_modem_new_dev(ESP_MODEM_DCE_SIM800, &dte_config, &dce_config, ppp_netif); + assert(dce); + + mod_info.model[0] = 0x00; + int GSMConnectTimeout = 0; + while (esp_modem_get_module_name(dce, mod_info.model) != ESP_OK) + { + if (++GSMConnectTimeout >= PPP_MODEM_TIMEOUT) + goto modem_init_fail; + vTaskDelay(pdMS_TO_TICKS(1000)); + } + ESP_LOGI(TAG, "Module type:%s", mod_info.model); + + mod_info.imsi[0] = 0x00; + while (esp_modem_get_imsi(dce, mod_info.imsi) != ESP_OK) + { + if (++GSMConnectTimeout >= PPP_MODEM_TIMEOUT) + goto modem_init_fail; + vTaskDelay(pdMS_TO_TICKS(1000)); + } + ESP_LOGI(TAG, "IMSI:%s", mod_info.imsi); + + mod_info.oper[0] = 0x00; + while (esp_modem_get_operator_name(dce, mod_info.oper) != ESP_OK) + { + if (++GSMConnectTimeout >= PPP_MODEM_TIMEOUT) + goto modem_init_fail; + vTaskDelay(pdMS_TO_TICKS(1000)); + } + ESP_LOGI(TAG, "Operator:%s", mod_info.oper); + + mod_info.imei[0] = 0x00; + while (esp_modem_get_imei(dce, mod_info.imei) != ESP_OK) + { + if (++GSMConnectTimeout >= PPP_MODEM_TIMEOUT) + goto modem_init_fail; + vTaskDelay(pdMS_TO_TICKS(1000)); + } + ESP_LOGI(TAG, "IMEI:%s", mod_info.imei); + + while (esp_modem_set_mode(dce, ESP_MODEM_MODE_DATA) != ESP_OK) + { + if (++GSMConnectTimeout >= PPP_MODEM_TIMEOUT) + goto modem_init_fail; + vTaskDelay(pdMS_TO_TICKS(1000)); + } + + ESP_LOGI(TAG, "PPP data mode OK"); + + xEventGroupWaitBits(event_group, CONNECT_BIT, pdTRUE, pdTRUE, portMAX_DELAY); + + isPPPinitializing = false; + vTaskDelete(NULL); + + return; +modem_init_fail: + ESP_LOGE(TAG, "PPP modem init error"); + isPPPinitializing = false; + vTaskDelete(NULL); +} + +void PPPModemColdStart(void) +{ + ResetType = 0; + xTaskCreate(GSMInitTask, "GSMInitTask", 1024 * 4, &ResetType, 3, initTaskhandle); +} + +void PPPModemSoftRestart(void) +{ + ResetType = 1; + xTaskCreate(GSMInitTask, "GSMInitTask", 1024 * 4, &ResetType, 3, initTaskhandle); +} + +static void GSMRunTask(void *pvParameter) +{ + while (1) + { + if (!isPPPConn && !isPPPinitializing) + { //try to reconnect modem + ESP_LOGI(TAG, "PPP modem restart"); + PPPModemColdStart(); + } + vTaskDelay(pdMS_TO_TICKS(30000)); + } +} + +void PPPModemStart(void) +{ + xTaskCreate(GSMRunTask, "GSMRunTask", 1024 * 4, &ResetType, 3, NULL); +} + +#endif + + + diff --git a/src/HTTPPostSystem.c b/src/HTTPPostSystem.c index c731136..577bf21 100644 --- a/src/HTTPPostSystem.c +++ b/src/HTTPPostSystem.c @@ -151,6 +151,7 @@ static HTTP_IO_RESULT HTTPPostIndex(httpd_req_t *req, char *PostData) static HTTP_IO_RESULT HTTPPostIndex20(httpd_req_t *req, char *PostData) { +#if CONFIG_WEBGUIAPP_ETHERNET_ENABLE char tmp[32]; bool TempIsETHEnabled = false; bool TempIsDHCPEnabled = false; @@ -189,7 +190,7 @@ static HTTP_IO_RESULT HTTPPostIndex20(httpd_req_t *req, char *PostData) return HTTP_IO_REDIRECT; } } - +#endif return HTTP_IO_DONE; } diff --git a/src/HTTPPrintSystem.c b/src/HTTPPrintSystem.c index 01a1aea..f557e7a 100644 --- a/src/HTTPPrintSystem.c +++ b/src/HTTPPrintSystem.c @@ -248,10 +248,140 @@ static void HTTPPrint_apmacadr(char *VarData, void *arg) { PrintMACFromInterface(VarData, arg, GetAPNetifAdapter()); } +#endif +#if CONFIG_WEBGUIAPP_ETHERNET_ENABLE +static void HTTPPrint_ethen(char *VarData, void *arg) +{ + PrintCheckbox(VarData, arg, GetSysConf()->ethSettings.Flags1.bIsETHEnabled); +} +static void HTTPPrint_ecbdh(char *VarData, void *arg) +{ + PrintCheckbox(VarData, arg, GetSysConf()->ethSettings.Flags1.bIsDHCPEnabled); +} +static void HTTPPrint_ethstat(char *VarData, void *arg) +{ + PrintInterfaceState(VarData, arg, GetETHNetifAdapter()); +} +/*Etherbox IP*/ +static void HTTPPrint_eip(char *VarData, void *arg) +{ + if (GetETHNetifAdapter() && esp_netif_is_netif_up(GetETHNetifAdapter())) + PrintIPFromInterface(VarData, arg, GetETHNetifAdapter(), IP); + else + snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", ip4addr_ntoa(&GetSysConf()->ethSettings.IPAddr)); +} +/*Etherbox NETMASK*/ +static void HTTPPrint_emsk(char *VarData, void *arg) +{ + if (GetETHNetifAdapter() && esp_netif_is_netif_up(GetETHNetifAdapter())) + PrintIPFromInterface(VarData, arg, GetETHNetifAdapter(), NETMASK); + else + snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", ip4addr_ntoa(&GetSysConf()->ethSettings.Mask)); +} +/*Ethernet GATEWAY*/ +static void HTTPPrint_egate(char *VarData, void *arg) +{ + if (GetETHNetifAdapter() && esp_netif_is_netif_up(GetETHNetifAdapter())) + PrintIPFromInterface(VarData, arg, GetETHNetifAdapter(), GW); + else + snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", ip4addr_ntoa(&GetSysConf()->ethSettings.Gateway)); +} +/*Current DNS*/ +static void HTTPPrint_edns(char *VarData, void *arg) +{ + if (GetETHNetifAdapter() && esp_netif_is_netif_up(GetETHNetifAdapter())) + PrintDNSFromInterface(VarData, arg, GetETHNetifAdapter(), ESP_NETIF_DNS_MAIN); + else + snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", "0.0.0.0"); +} +static void HTTPPrint_bkedns(char *VarData, void *arg) +{ + + if (GetETHNetifAdapter() && esp_netif_is_netif_up(GetETHNetifAdapter())) + PrintDNSFromInterface(VarData, arg, GetETHNetifAdapter(), ESP_NETIF_DNS_BACKUP); + else + snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", "0.0.0.0"); +} +static void HTTPPrint_fledns(char *VarData, void *arg) +{ + + if (GetETHNetifAdapter() && esp_netif_is_netif_up(GetETHNetifAdapter())) + PrintDNSFromInterface(VarData, arg, GetETHNetifAdapter(), ESP_NETIF_DNS_FALLBACK); + else + snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", "0.0.0.0"); +} + +static void HTTPPrint_emacadr(char *VarData, void *arg) +{ + PrintMACFromInterface(VarData, arg, GetETHNetifAdapter()); +} #endif +#if CONFIG_WEBGUIAPP_GPRS_ENABLE +/*GSM MODEM*/ +void HTTPPrint_gsmen(char *VarData, void *arg) +{ + PrintCheckbox(VarData, arg, GetSysConf()->gsmSettings.Flags1.bIsGSMEnabled); +} +void HTTPPrint_gsmstat(char *VarData, void *arg) +{ + PrintInterfaceState(VarData, arg, GetPPPNetifAdapter()); +} +void HTTPPrint_gsmmod(char *VarData, void *arg) +{ + snprintf(VarData, MAX_DYNVAR_LENGTH, GetPPPModemInfo()->model); +} +void HTTPPrint_gsmopr(char *VarData, void *arg) +{ + snprintf(VarData, MAX_DYNVAR_LENGTH, GetPPPModemInfo()->oper); +} +void HTTPPrint_gimei(char *VarData, void *arg) +{ + snprintf(VarData, MAX_DYNVAR_LENGTH, GetPPPModemInfo()->imei); +} +void HTTPPrint_gimsi(char *VarData, void *arg) +{ + snprintf(VarData, MAX_DYNVAR_LENGTH, GetPPPModemInfo()->imsi); +} + +/*PPP IP*/ +void HTTPPrint_gsmip(char *VarData, void *arg) +{ + PrintIPFromInterface(VarData, arg, GetPPPNetifAdapter(), IP); +} +/*PPP NETMASK*/ +void HTTPPrint_gsmmsk(char *VarData, void *arg) +{ + PrintIPFromInterface(VarData, arg, GetPPPNetifAdapter(), NETMASK); +} +/*PPP GATEWAY*/ +void HTTPPrint_gsmgate(char *VarData, void *arg) +{ + PrintIPFromInterface(VarData, arg, GetPPPNetifAdapter(), GW); +} +/*Current DNS*/ +void HTTPPrint_gsmdns(char *VarData, void *arg) +{ + PrintDNSFromInterface(VarData, arg, GetPPPNetifAdapter(), ESP_NETIF_DNS_MAIN); +} +void HTTPPrint_bkgsmdns(char *VarData, void *arg) +{ + PrintDNSFromInterface(VarData, arg, GetPPPNetifAdapter(), ESP_NETIF_DNS_BACKUP); +} +void HTTPPrint_flgsmdns(char *VarData, void *arg) +{ + PrintDNSFromInterface(VarData, arg, GetPPPNetifAdapter(), ESP_NETIF_DNS_FALLBACK); +} + +void HTTPPrint_gsmmac(char *VarData, void *arg) +{ + PrintMACFromInterface(VarData, arg, GetPPPNetifAdapter()); +} +#endif + + dyn_var_handler_t HANDLERS_ARRAY[] = { /*Ststem settings*/ { "dname", sizeof("dname") - 1, &HTTPPrint_dname }, @@ -259,6 +389,7 @@ dyn_var_handler_t HANDLERS_ARRAY[] = { { "pass", sizeof("pass") - 1, &HTTPPrint_pass }, #if CONFIG_WEBGUIAPP_WIFI_ENABLE + /*WiFi network*/ { "wfen", sizeof("wfen") - 1, &HTTPPrint_wfen }, { "wfstat", sizeof("wfstat") - 1, &HTTPPrint_wfstat }, { "cln", sizeof("cln") - 1, &HTTPPrint_cln }, @@ -279,6 +410,36 @@ dyn_var_handler_t HANDLERS_ARRAY[] = { { "apmacadr", sizeof("apmacadr") - 1, &HTTPPrint_apmacadr }, #endif +#if CONFIG_WEBGUIAPP_ETHERNET_ENABLE + /*ETHERNET network*/ + { "ethen", sizeof("ethen") - 1, &HTTPPrint_ethen }, + { "ecbdh", sizeof("ecbdh") - 1, &HTTPPrint_ecbdh }, + { "ethstat", sizeof("ethstat") - 1, &HTTPPrint_ethstat }, + { "eip", sizeof("eip") - 1, &HTTPPrint_eip }, + { "emsk", sizeof("emsk") - 1, &HTTPPrint_emsk }, + { "egate", sizeof("egate") - 1, &HTTPPrint_egate }, + { "edns", sizeof("edns") - 1, &HTTPPrint_edns }, + { "bkedns", sizeof("bkedns") - 1, &HTTPPrint_bkedns }, + { "fledns", sizeof("fledns") - 1, &HTTPPrint_fledns }, + { "emacadr", sizeof("emacadr") - 1, &HTTPPrint_emacadr }, +#endif + +#if CONFIG_WEBGUIAPP_GPRS_ENABLE + /*GSM modem*/ + { "gsmen", sizeof("gsmen") - 1, &HTTPPrint_gsmen }, + { "gsmstat", sizeof("gsmstat") - 1, &HTTPPrint_gsmstat }, + { "gsmmod", sizeof("gsmmod") - 1, &HTTPPrint_gsmmod }, + { "gsmopr", sizeof("gsmopr") - 1, &HTTPPrint_gsmopr }, + { "gimei", sizeof("gimei") - 1, &HTTPPrint_gimei }, + { "gimsi", sizeof("gimsi") - 1, &HTTPPrint_gimsi }, + { "gsmip", sizeof("gsmip") - 1, &HTTPPrint_gsmip }, + { "gsmmsk", sizeof("gsmmsk") - 1, &HTTPPrint_gsmmsk }, + { "gsmgate", sizeof("gsmgate") - 1, &HTTPPrint_gsmgate }, + { "gsmdns", sizeof("gsmdns") - 1, &HTTPPrint_gsmdns }, + { "bkgsmdns", sizeof("bkgsmdns") - 1, &HTTPPrint_bkgsmdns }, + { "flgsmdns", sizeof("flgsmdns") - 1, &HTTPPrint_flgsmdns }, + { "gsmmac", sizeof("gsmmac") - 1, &HTTPPrint_gsmmac }, +#endif /*ERROR report*/ { "status_fail", sizeof("status_fail") - 1, &HTTPPrint_status_fail },