Skip to content

Commit

Permalink
added napt select based on lwip variant
Browse files Browse the repository at this point in the history
  • Loading branch information
suraji committed Feb 22, 2020
1 parent 993d93e commit 1e01d7d
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 23 deletions.
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=esp8266-framework
version=1.0
version=1.1
author=Suraj I.
maintainer=Suraj I. <surajinamdar151@gmail.com>
sentence=esp8266 framework stack for easy configurable applications
paragraph=esp8266 framework stack for easy configurable applications
paragraph=esp8266 framework includes all services like gpio, wifi, http, mqtt, ntp, ota, napt, espnow, mesh, server etc. which are ready to use in all applications
category=Communication
url=https://github.com/Suraj151/esp8266-framework
architectures=esp8266
Expand Down
19 changes: 12 additions & 7 deletions src/EwingsEsp8266Stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ void EwingsEsp8266Stack::initialize(){
#endif
__database_service.init_default_database();
__event_service.begin();
#if defined( ENABLE_NAPT_FEATURE ) || defined( ENABLE_NAPT_FEATURE_LWIP_V2 )
__event_service.add_event_listener( EVENT_WIFI_STA_CONNECTED, [&](void*sta_connected) {
__task_scheduler.setTimeout( [&]() { this->enable_napt_service(); }, NAPT_INIT_DURATION_AFTER_WIFI_CONNECT );
} );
#endif

__wifi_service.begin( this->wifi );
__ping_service.init_ping( this->wifi );
#ifdef ENABLE_EWING_HTTP_SERVER
Expand All @@ -44,10 +50,6 @@ void EwingsEsp8266Stack::initialize(){
__task_scheduler.setInterval( [&]() { __factory_reset.handleFlashKeyPress(); }, FLASH_KEY_PRESS_DURATION );
__factory_reset.run_while_factory_reset( [&]() { __database_service.clear_default_tables(); this->wifi->disconnect(true); } );

#if defined( ENABLE_NAPT_FEATURE ) || defined( ENABLE_NAPT_FEATURE_LWIP_V2 )
this->enable_napt_service();
#endif

#ifdef ENABLE_ESP_NOW
__espnow_service.beginEspNow( this->wifi );
__task_scheduler.setInterval( [&]() { __espnow_service.handlePeers(); }, ESP_NOW_HANDLE_DURATION );
Expand All @@ -63,14 +65,16 @@ void EwingsEsp8266Stack::initialize(){
* enable napt feature
*/
void EwingsEsp8266Stack::enable_napt_service(){

// Initialize the NAT feature
ip_napt_init(IP_NAPT_MAX, IP_PORTMAP_MAX);
// Enable NAT on the AP interface
ip_napt_enable_no(1, 1);
// Set the DNS server for clients of the AP to the one we also use for the STA interface
dhcps_set_DNS(this->wifi->dnsIP());
#ifdef EW_SERIAL_LOG
Logln(F("NAPT initialization done"));
Log(F("NAPT(lwip "));Log(LWIP_VERSION_MAJOR);
Logln(F(") initialization done"));
#endif
}
#elif defined( ENABLE_NAPT_FEATURE_LWIP_V2 )
Expand All @@ -79,14 +83,15 @@ void EwingsEsp8266Stack::enable_napt_service(){
*/
void EwingsEsp8266Stack::enable_napt_service(){

// Initialize the NAT feature
// Initialize the NAPT feature
err_t ret = ip_napt_init(IP_NAPT_MAX, IP_PORTMAP_MAX);
if (ret == ERR_OK) {
// Enable NAT on the AP interface
ret = ip_napt_enable_no(SOFTAP_IF, 1);
if (ret == ERR_OK) {
#ifdef EW_SERIAL_LOG
Logln(F("NAPT initialization done"));
Log(F("NAPT(lwip "));Log(LWIP_VERSION_MAJOR);
Logln(F(") initialization done"));
#endif
// Set the DNS server for clients of the AP to the one we also use for the STA interface
dhcps_set_dns(0, this->wifi->dnsIP(0));
Expand Down
25 changes: 20 additions & 5 deletions src/config/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ extern "C" {
#include "user_interface.h"
}

/**
* @define common time durations
*/
#define MILLISECOND_DURATION_1000 1000
#define MILLISECOND_DURATION_5000 5000
#define MILLISECOND_DURATION_10000 10000

/**
* enable/disable gpio and mqtt feature here
*/
Expand Down Expand Up @@ -60,21 +67,29 @@ extern "C" {
* @define flash key parameters for reset factory
*/
#define FLASH_KEY_PIN D3
#define FLASH_KEY_PRESS_DURATION 1000
#define FLASH_KEY_PRESS_DURATION MILLISECOND_DURATION_1000
#define FLASH_KEY_PRESS_COUNT_THR 5

/**
* @define wifi & internet connectivity check cycle durations
*/
#define WIFI_STATION_CONNECT_ATTEMPT_TIMEOUT 5 // will try to connect within this seconds
#define WIFI_CONNECTIVITY_CHECK_DURATION 10000
#define WIFI_CONNECTIVITY_CHECK_DURATION MILLISECOND_DURATION_10000
#define INTERNET_CONNECTIVITY_CHECK_DURATION WIFI_CONNECTIVITY_CHECK_DURATION

/**
* @define network address & port translation feature
*/
// #define ENABLE_NAPT_FEATURE
#define ENABLE_NAPT_FEATURE_LWIP_V2
#if IP_NAPT && LWIP_VERSION_MAJOR==1
#define ENABLE_NAPT_FEATURE
#elif IP_NAPT && LWIP_VERSION_MAJOR==2
#define ENABLE_NAPT_FEATURE_LWIP_V2
#endif


#if defined( ENABLE_NAPT_FEATURE ) || defined( ENABLE_NAPT_FEATURE_LWIP_V2 )
#define NAPT_INIT_DURATION_AFTER_WIFI_CONNECT MILLISECOND_DURATION_5000
#endif

/**
* @define default username/ssid and password
Expand All @@ -86,7 +101,7 @@ extern "C" {
* @define general http parameters
*/
#define HTTP_HOST_ADDR_MAX_SIZE 100
#define HTTP_REQUEST_DURATION 10000
#define HTTP_REQUEST_DURATION MILLISECOND_DURATION_10000
#define HTTP_REQUEST_RETRY 1

/**
Expand Down
2 changes: 1 addition & 1 deletion src/config/EspnowConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extern "C" {
#define ESP_NOW_DEVICE_TABLE_MAX_SIZE 20
#define ESP_NOW_MAX_BUFF_SIZE 250

#define ESP_NOW_HANDLE_DURATION 5000
#define ESP_NOW_HANDLE_DURATION MILLISECOND_DURATION_5000

enum esp_now_state {
ESP_NOW_STATE_EMPTY,
Expand Down
2 changes: 1 addition & 1 deletion src/config/GpioConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ created Date : 1st June 2019
/**
* @define gpio parameters
*/
#define GPIO_OPERATION_DURATION 1000
#define GPIO_OPERATION_DURATION MILLISECOND_DURATION_1000
#define GPIO_TABLE_UPDATE_DURATION 300000

/**
Expand Down
2 changes: 1 addition & 1 deletion src/config/MqttConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ created Date : 1st June 2019
#define MQTT_MAX_PUBLISH_TOPIC 2
#define MQTT_MAX_SUBSCRIBE_TOPIC MQTT_MAX_PUBLISH_TOPIC

#define MQTT_INITIALIZE_DURATION 5000
#define MQTT_INITIALIZE_DURATION MILLISECOND_DURATION_5000

/**
* enable/disable mqtt config modification here
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/SMTPDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ enum smtp_command_status {
};

#define SMTP_RESPONSE_BUFFER_SIZE 520
#define SMTP_DEFAULT_TIMEOUT 5000
#define SMTP_DEFAULT_TIMEOUT MILLISECOND_DURATION_5000

/**
* SMTPdriver class
Expand Down
2 changes: 1 addition & 1 deletion src/service_provider/GpioServiceProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ void GpioServiceProvider::handleGpioModes( int _gpio_config_type ){
this->_gpio_http_request_cb_id = __task_scheduler.updateInterval(
this->_gpio_http_request_cb_id,
[&]() { this->handleGpioHttpRequest(); },
this->virtual_gpio_configs.gpio_post_frequency*1000
this->virtual_gpio_configs.gpio_post_frequency*MILLISECOND_DURATION_1000
);
}else{
__task_scheduler.clearInterval( this->_gpio_http_request_cb_id );
Expand Down
6 changes: 3 additions & 3 deletions src/service_provider/MqttServiceProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void MqttServiceProvider::handleMqttConfigChange( int _mqtt_config_type ){
this->_mqtt_timer_cb_id = __task_scheduler.updateInterval(
this->_mqtt_timer_cb_id,
[&]() { this->mqtt_client.mqtt_timer(); },
1000
MILLISECOND_DURATION_1000
);
}else{
__task_scheduler.clearInterval( this->_mqtt_timer_cb_id );
Expand Down Expand Up @@ -200,7 +200,7 @@ void MqttServiceProvider::handleMqttConfigChange( int _mqtt_config_type ){
this->_mqtt_publish_cb_id = __task_scheduler.updateInterval(
this->_mqtt_publish_cb_id,
[&]() { this->handleMqttPublish(); },
_mqtt_pubsub_configs.publish_frequency*1000
_mqtt_pubsub_configs.publish_frequency*MILLISECOND_DURATION_1000
);
}else{
__task_scheduler.clearInterval( this->_mqtt_publish_cb_id );
Expand All @@ -211,7 +211,7 @@ void MqttServiceProvider::handleMqttConfigChange( int _mqtt_config_type ){
this->_mqtt_subscribe_cb_id = __task_scheduler.updateInterval(
this->_mqtt_subscribe_cb_id,
[&]() { this->handleMqttSubScribe(); },
_mqtt_general_configs.keepalive*1000
_mqtt_general_configs.keepalive*MILLISECOND_DURATION_1000
);
}else{
__task_scheduler.clearInterval( this->_mqtt_subscribe_cb_id );
Expand Down
3 changes: 2 additions & 1 deletion src/utility/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ created Date : 1st June 2019
#define __EWINGS_LOG_UTILITY_H__

#include <Arduino.h>
#include <config/Config.h>

#define EW_SERIAL_LOG
#define EW_DEFAULT_LOG_DURATION 5000
#define EW_DEFAULT_LOG_DURATION MILLISECOND_DURATION_5000

#ifdef EW_SERIAL_LOG

Expand Down

0 comments on commit 1e01d7d

Please sign in to comment.