Astyle update

This commit is contained in:
Luc 2021-09-30 15:54:14 +08:00
parent db43baec5c
commit b4668d41ee
9 changed files with 93 additions and 91 deletions

View File

@ -117,13 +117,13 @@ bool Esp3D::begin()
#endif //DISPLAY_DEVICE
//Setup Network
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE) || defined(BLUETOOTH_FEATURE)
if (Settings_ESP3D::read_byte(ESP_BOOT_RADIO_STATE) == 1){
if (Settings_ESP3D::read_byte(ESP_BOOT_RADIO_STATE) == 1) {
if (!NetConfig::begin()) {
log_esp3d("Error setup network");
res = false;
log_esp3d("Error setup network");
res = false;
}
}
#endif //WIFI_FEATURE
#if defined(ESP_AUTOSTART_SCRIPT)
esp3d_gcode_host.processscript(ESP_AUTOSTART_SCRIPT);

View File

@ -54,13 +54,12 @@ bool Commands::ESP114(const char* cmd_params, level_authenticate_type auth_type,
output->printERROR("Only ON or OFF mode supported!");
return false;
} else {
if (!Settings_ESP3D::write_byte (ESP_BOOT_RADIO_STATE, (parameter == "ON")?1:0)) {
output->printERROR ("Set failed!");
response = false;
}
else {
output->printMSG ("ok");
}
if (!Settings_ESP3D::write_byte (ESP_BOOT_RADIO_STATE, (parameter == "ON")?1:0)) {
output->printERROR ("Set failed!");
response = false;
} else {
output->printMSG ("ok");
}
}
}
return response;

View File

@ -72,7 +72,7 @@ bool Commands::ESP400(const char* cmd_params, level_authenticate_type auth_type,
#endif //ETH_FEATURE
output->print ("]}");
#if defined (WIFI_FEATURE) || defined (ETH_FEATURE) || defined(BT_FEATURE)
//Radio State at Boot
//Radio State at Boot
output->print (",{\"F\":\"network/network\",\"P\":\"");
output->print (ESP_BOOT_RADIO_STATE);
output->print ("\",\"T\":\"B\",\"R\":\"1\",\"V\":\"");

View File

@ -41,7 +41,9 @@ bool Commands::ESP410(const char* cmd_params, level_authenticate_type auth_type,
#endif //AUTHENTICATION_FEATURE
//Backup current mode
uint8_t currentmode = WiFi.getMode();
if(currentmode==WIFI_AP)WiFi.mode(WIFI_AP_STA);
if(currentmode==WIFI_AP) {
WiFi.mode(WIFI_AP_STA);
}
bool plain = hastag(cmd_params,"plain");
int n = 0;
uint8_t total = 0;

View File

@ -22,54 +22,54 @@
#include "../../core/esp3doutput.h"
#include "../../core/settings_esp3d.h"
#if defined (AUTHENTICATION_FEATURE)
#if defined (HTTP_FEATURE)
#if defined (ARDUINO_ARCH_ESP32)
#if defined(AUTHENTICATION_FEATURE)
#if defined(HTTP_FEATURE)
#if defined(ARDUINO_ARCH_ESP32)
#include <WebServer.h>
#endif //ARDUINO_ARCH_ESP32
#if defined (ARDUINO_ARCH_ESP8266)
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WebServer.h>
#endif //ARDUINO_ARCH_ESP8266
Authwebserver * AuthenticationService::_webserver = nullptr;
Authwebserver *AuthenticationService::_webserver = nullptr;
#endif //HTTP_FEATURE
#endif //AUTHENTICATION_FEATURE
#if defined (AUTHENTICATION_FEATURE)
String AuthenticationService::_adminpwd="";
String AuthenticationService::_userpwd="";
#if defined (HTTP_FEATURE)
#if defined(AUTHENTICATION_FEATURE)
String AuthenticationService::_adminpwd = "";
String AuthenticationService::_userpwd = "";
#if defined(HTTP_FEATURE)
uint32_t AuthenticationService::_sessionTimeout = 360000;
auth_ip * AuthenticationService::_head = nullptr;
auth_ip *AuthenticationService::_head = nullptr;
uint8_t AuthenticationService::_current_nb_ip = 0;
#endif //HTTP_FEATURE
#endif //HTTP_FEATURE
#endif //AUTHENTICATION_FEATURE
#define MAX_AUTH_IP 10
//#define ALLOW_MULTIPLE_SESSIONS
//check authentification
level_authenticate_type AuthenticationService::authenticated_level(const char * pwd, ESP3DOutput * output)
level_authenticate_type AuthenticationService::authenticated_level(const char *pwd, ESP3DOutput *output)
{
#ifdef AUTHENTICATION_FEATURE
level_authenticate_type auth_type = LEVEL_GUEST;
if(pwd != nullptr) {
if (pwd != nullptr) {
if (isadmin(pwd)) {
auth_type = LEVEL_ADMIN;
}
if (isuser (pwd) && (auth_type != LEVEL_ADMIN)) {
if (isuser(pwd) && (auth_type != LEVEL_ADMIN)) {
auth_type = LEVEL_USER;
}
return auth_type;
} else {
if(output) {
if (output->client() !=ESP_HTTP_CLIENT) {
if (output) {
if (output->client() != ESP_HTTP_CLIENT) {
return auth_type;
}
}
#if defined (HTTP_FEATURE)
#if defined(HTTP_FEATURE)
if (_webserver) {
if (_webserver->hasHeader ("Authorization") ) {
if (_webserver->hasHeader("Authorization")) {
//log_esp3d("Check authorization %",(_webserver->uri()).c_str());
if (_webserver->authenticate(DEFAULT_ADMIN_LOGIN, _adminpwd.c_str())) {
auth_type = LEVEL_ADMIN;
@ -79,16 +79,16 @@ level_authenticate_type AuthenticationService::authenticated_level(const char *
}
}
}
if (_webserver->hasHeader ("Cookie") ) {
if (_webserver->hasHeader("Cookie")) {
//log_esp3d("Check Cookie %s",(_webserver->uri()).c_str());
String cookie = _webserver->header ("Cookie");
int pos = cookie.indexOf ("ESPSESSIONID=");
String cookie = _webserver->header("Cookie");
int pos = cookie.indexOf("ESPSESSIONID=");
if (pos != -1) {
int pos2 = cookie.indexOf (";", pos);
String sessionID = cookie.substring (pos + strlen ("ESPSESSIONID="), pos2);
int pos2 = cookie.indexOf(";", pos);
String sessionID = cookie.substring(pos + strlen("ESPSESSIONID="), pos2);
IPAddress ip = _webserver->client().remoteIP();
//check if cookie can be reset and clean table in same time
auth_type = ResetAuthIP (ip, sessionID.c_str() );
auth_type = ResetAuthIP(ip, sessionID.c_str());
//log_esp3d("Authentication = %d", auth_type);
}
}
@ -104,7 +104,7 @@ level_authenticate_type AuthenticationService::authenticated_level(const char *
}
#ifdef AUTHENTICATION_FEATURE
#if defined (HTTP_FEATURE)
#if defined(HTTP_FEATURE)
uint32_t AuthenticationService::setSessionTimeout(uint32_t timeout)
{
if (timeout >= 0) {
@ -118,11 +118,11 @@ uint32_t AuthenticationService::getSessionTimeout()
}
#endif //HTTP_FEATURE
bool AuthenticationService::begin(Authwebserver * webserver)
bool AuthenticationService::begin(Authwebserver *webserver)
{
end();
update();
#if defined (HTTP_FEATURE)
#if defined(HTTP_FEATURE)
_webserver = webserver;
#endif //HTTP_FEATURE
//value is in ms but storage is in min
@ -131,7 +131,7 @@ bool AuthenticationService::begin(Authwebserver * webserver)
}
void AuthenticationService::end()
{
#if defined (HTTP_FEATURE)
#if defined(HTTP_FEATURE)
_webserver = nullptr;
ClearAllSessions();
#endif //HTTP_FEATURE
@ -139,8 +139,8 @@ void AuthenticationService::end()
void AuthenticationService::update()
{
_adminpwd=Settings_ESP3D::read_string (ESP_ADMIN_PWD);
_userpwd=Settings_ESP3D::read_string (ESP_USER_PWD);
_adminpwd = Settings_ESP3D::read_string(ESP_ADMIN_PWD);
_userpwd = Settings_ESP3D::read_string(ESP_USER_PWD);
}
void AuthenticationService::handle()
@ -148,9 +148,9 @@ void AuthenticationService::handle()
}
//check admin password
bool AuthenticationService::isadmin (const char *pwd)
bool AuthenticationService::isadmin(const char *pwd)
{
if (strcmp(_adminpwd.c_str(), pwd) !=0 ) {
if (strcmp(_adminpwd.c_str(), pwd) != 0) {
return false;
} else {
return true;
@ -158,20 +158,20 @@ bool AuthenticationService::isadmin (const char *pwd)
}
//check user password - admin password is also valid
bool AuthenticationService::isuser (const char *pwd)
bool AuthenticationService::isuser(const char *pwd)
{
//it is not user password
if (strcmp(_userpwd.c_str(), pwd)!=0) {
if (strcmp(_userpwd.c_str(), pwd) != 0) {
//check admin password
return isadmin (pwd);
return isadmin(pwd);
} else {
return true;
}
}
#if defined (HTTP_FEATURE)
#if defined(HTTP_FEATURE)
//add the information in the linked list if possible
bool AuthenticationService::AddAuthIP (auth_ip * item)
bool AuthenticationService::AddAuthIP(auth_ip *item)
{
if (_current_nb_ip > MAX_AUTH_IP) {
return false;
@ -183,20 +183,20 @@ bool AuthenticationService::AddAuthIP (auth_ip * item)
}
//Session ID based on IP and time using 16 char
char * AuthenticationService::create_session_ID()
char *AuthenticationService::create_session_ID()
{
static char sessionID[17];
//reset SESSIONID
static char sessionID[17];
//reset SESSIONID
for (int i = 0; i < 17; i++) {
sessionID[i] = '\0';
}
//get time
//get time
uint32_t now = millis();
//get remote IP
//get remote IP
IPAddress remoteIP = _webserver->client().remoteIP();
//generate SESSIONID
if (0 > sprintf (sessionID, "%02X%02X%02X%02X%02X%02X%02X%02X", remoteIP[0], remoteIP[1], remoteIP[2], remoteIP[3], (uint8_t) ( (now >> 0) & 0xff), (uint8_t) ( (now >> 8) & 0xff), (uint8_t) ( (now >> 16) & 0xff), (uint8_t) ( (now >> 24) & 0xff) ) ) {
strcpy (sessionID, "NONE");
//generate SESSIONID
if (0 > sprintf(sessionID, "%02X%02X%02X%02X%02X%02X%02X%02X", remoteIP[0], remoteIP[1], remoteIP[2], remoteIP[3], (uint8_t)((now >> 0) & 0xff), (uint8_t)((now >> 8) & 0xff), (uint8_t)((now >> 16) & 0xff), (uint8_t)((now >> 24) & 0xff))) {
strcpy(sessionID, "NONE");
}
return sessionID;
}
@ -204,7 +204,7 @@ char * AuthenticationService::create_session_ID()
bool AuthenticationService::ClearAllSessions()
{
while (_head) {
auth_ip * current = _head;
auth_ip *current = _head;
_head = _head->_next;
delete current;
}
@ -219,21 +219,21 @@ bool AuthenticationService::ClearCurrentSession()
String cookie = _webserver->header("Cookie");
int pos = cookie.indexOf("ESPSESSIONID=");
String sessionID;
if (pos!= -1) {
int pos2 = cookie.indexOf(";",pos);
sessionID = cookie.substring(pos+strlen("ESPSESSIONID="),pos2);
if (pos != -1) {
int pos2 = cookie.indexOf(";", pos);
sessionID = cookie.substring(pos + strlen("ESPSESSIONID="), pos2);
}
return ClearAuthIP(_webserver->client().remoteIP(), sessionID.c_str());
}
bool AuthenticationService::CreateSession(level_authenticate_type auth_level, const char * username, const char* session_ID)
bool AuthenticationService::CreateSession(level_authenticate_type auth_level, const char *username, const char *session_ID)
{
auth_ip * current_auth = new auth_ip;
auth_ip *current_auth = new auth_ip;
current_auth->level = auth_level;
current_auth->ip=_webserver->client().remoteIP();
current_auth->ip = _webserver->client().remoteIP();
strcpy(current_auth->sessionID, session_ID);
strcpy(current_auth->userID,username);
current_auth->last_time=millis();
strcpy(current_auth->userID, username);
current_auth->last_time = millis();
#ifndef ALLOW_MULTIPLE_SESSIONS
//if not multiple session no need to keep all session, current one is enough
ClearAllSessions();
@ -246,13 +246,13 @@ bool AuthenticationService::CreateSession(level_authenticate_type auth_level, co
}
}
bool AuthenticationService::ClearAuthIP (IPAddress ip, const char * sessionID)
bool AuthenticationService::ClearAuthIP(IPAddress ip, const char *sessionID)
{
auth_ip * current = _head;
auth_ip * previous = NULL;
auth_ip *current = _head;
auth_ip *previous = NULL;
bool done = false;
while (current) {
if ( (ip == current->ip) && (strcmp (sessionID, current->sessionID) == 0) ) {
if ((ip == current->ip) && (strcmp(sessionID, current->sessionID) == 0)) {
//remove
done = true;
if (current == _head) {
@ -275,12 +275,12 @@ bool AuthenticationService::ClearAuthIP (IPAddress ip, const char * sessionID)
}
//Get info
auth_ip * AuthenticationService::GetAuth (IPAddress ip, const char * sessionID)
auth_ip *AuthenticationService::GetAuth(IPAddress ip, const char *sessionID)
{
auth_ip * current = _head;
auth_ip *current = _head;
while (current) {
if (ip == current->ip) {
if (strcmp (sessionID, current->sessionID) == 0) {
if (strcmp(sessionID, current->sessionID) == 0) {
//found
return current;
}
@ -292,20 +292,20 @@ auth_ip * AuthenticationService::GetAuth (IPAddress ip, const char * sessionID)
}
//Get time left for specific session
uint32_t AuthenticationService::getSessionRemaining(const char * sessionID)
uint32_t AuthenticationService::getSessionRemaining(const char *sessionID)
{
auth_ip * current = _head;
auth_ip *current = _head;
if ((sessionID == nullptr) || (strlen(sessionID) == 0)) {
return 0;
}
while (current) {
if (strcmp (sessionID, current->sessionID) == 0) {
if (strcmp(sessionID, current->sessionID) == 0) {
//found
uint32_t now = millis();
if ((now - current->last_time) > _sessionTimeout) {
return 0;
}
return _sessionTimeout - (now-current->last_time);
return _sessionTimeout - (now - current->last_time);
}
//previous = current;
current = current->_next;
@ -314,16 +314,16 @@ uint32_t AuthenticationService::getSessionRemaining(const char * sessionID)
}
//Review all IP to reset timers
level_authenticate_type AuthenticationService::ResetAuthIP (IPAddress ip, const char * sessionID)
level_authenticate_type AuthenticationService::ResetAuthIP(IPAddress ip, const char *sessionID)
{
auth_ip * current = _head;
auth_ip * previous = NULL;
auth_ip *current = _head;
auth_ip *previous = NULL;
//get time
//uint32_t now = millis();
while (current) {
//if time out is reached and time out is not disabled
//if IP is not current one and time out is disabled
if ( (((millis() - current->last_time) > _sessionTimeout) && (_sessionTimeout!=0)) || ((ip != current->ip) && (_sessionTimeout==0)) ) {
if ((((millis() - current->last_time) > _sessionTimeout) && (_sessionTimeout != 0)) || ((ip != current->ip) && (_sessionTimeout == 0))) {
//remove
if (current == _head) {
_head = current->_next;
@ -338,10 +338,10 @@ level_authenticate_type AuthenticationService::ResetAuthIP (IPAddress ip, const
}
} else {
if (ip == current->ip) {
if (strcmp (sessionID, current->sessionID) == 0) {
if (strcmp(sessionID, current->sessionID) == 0) {
//reset time
current->last_time = millis();
return (level_authenticate_type) current->level;
return (level_authenticate_type)current->level;
}
}
previous = current;
@ -352,5 +352,4 @@ level_authenticate_type AuthenticationService::ResetAuthIP (IPAddress ip, const
}
#endif //HTTP_FEATURE
#endif //AUTHENTICATION_FEATURE

View File

@ -271,8 +271,8 @@ bool ESP_SD::format(ESP3DOutput * output)
return true;
}
if (output) {
output->printMSG("cannot erase");
}
output->printMSG("cannot erase");
}
return false;
}

View File

@ -277,8 +277,8 @@ bool ESP_SD::format(ESP3DOutput * output)
return true;
}
if (output) {
output->printMSG("cannot erase");
}
output->printMSG("cannot erase");
}
return false;
}

View File

@ -259,7 +259,9 @@ void MKSService::sendWifiHotspots()
uint dataOffset = 1;
uint8_t total_hotspots = 0;
uint8_t currentmode = WiFi.getMode();
if(currentmode==WIFI_AP)WiFi.mode(WIFI_AP_STA);
if(currentmode==WIFI_AP) {
WiFi.mode(WIFI_AP_STA);
}
clearFrame();
//clean memory
WiFi.scanDelete();

View File

@ -279,7 +279,7 @@ bool NetServices::begin()
log_esp3d("Add mdns service http / tcp port %d", HTTP_Server::port());
MDNS.addService("http", "tcp", HTTP_Server::port());
//ESP3D service
//TODO list all services available (http/tcp/ws/ftp/webdav/etc...)
//TODO list all services available (http/tcp/ws/ftp/webdav/etc...)
MDNS.addService("esp3d", "tcp", HTTP_Server::port());
MDNS.addServiceTxt("esp3d", "tcp", "version", FW_VERSION);
//Add TXT records