Refactoring internal clients (#987)

* Remove all output flags
* Masse replace name function / class to sync with ESP3D-TFT
* Create ESP3DMessageManager  class to handle messages creation / deletion (import functions of ESp3DClient from ESp3D-TFT)
*  Move to new messaging API of ESP3D-TFT
* Remove empty line from remote screen dispatching
* Replace \n to space and \r to nothing in remote screen dispatching
* Add missing default entry for sensor device
* Fix buzzer for ESP32 missing ledc initialization with latest core
* Move formatBytes to esp3d_string
* Add welcome message to telnet connection\
* Add display to the new messaging API
* Add sticky authentication on Serial / SerialBridge /Telnet/Data web socket and BT
* Log simplification
* Use enum class instead of typdef enum for ESP3DAuthenticationLevel  to be sure correct enum is used
* (v3) Home Assistant notification support (#971)
* Add notification via post request
* Extend t1 size to 255 bytes
* Hide Home Assistant notifications from web UI (#974)
* Sync with ESP3DLib on serial_socket
* Add some sanity check to avoid unnecessary message copies
* Update ESP0.cpp

---------

Co-authored-by: David Buezas <dbuezas@users.noreply.github.com>
This commit is contained in:
Luc 2024-01-03 10:46:13 +08:00 committed by GitHub
parent dabc95bc8c
commit a10a7f74bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
274 changed files with 21311 additions and 21782 deletions

42
.vscode/settings.json vendored
View File

@ -1,6 +1,8 @@
{
"git.ignoreLimitWarning": true,
"files.associations": {
"**/frontmatter.json": "jsonc",
"**/.frontmatter/config/*.json": "jsonc",
"*.tcc": "cpp",
"fstream": "cpp",
"string": "cpp",
@ -11,7 +13,45 @@
"vector": "cpp",
"string_view": "cpp",
"initializer_list": "cpp",
"typeinfo": "cpp"
"typeinfo": "cpp",
"atomic": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"list": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"map": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"cinttypes": "cpp"
},
"cmake.configureOnOpen": false
}

View File

@ -56,6 +56,7 @@
* Telegram
* PushOver
* IFTTT
* HomeAssistant
* Sensors support
* DHT 11/22
* Analog

View File

@ -53,6 +53,7 @@ Please use discord : [![discord](https://img.shields.io/discord/7528221487955969
- Pushover support come from https://github.com/ArduinoHannover/Pushover
- Email support come from https://github.com/CosmicBoris/ESP8266SMTP and https://www.electronicshub.org/send-an-email-using-esp8266/
- Telegram support come from https://medium.com/@xabaras/sending-a-message-to-a-telegram-channel-the-easy-way-eb0a0b32968
- HomeAssistant support come from https://developers.home-assistant.io/docs/api/rest/
# Credits: libraries

View File

@ -164,7 +164,7 @@ label can be: light/framesize/quality/contrast/brightness/saturation/gainceiling
ESP_INTERNET_TIME 120 //1 byte = flag
ESP_HTTP_PORT 121 //4 bytes = int
ESP_TELNET_PORT 125 //4 bytes = int
ESP_SERIAL_FLAG 129 //1 bytes = flag
FREE 129 //1 bytes = flag
ESP_HOSTNAME 130 //33 bytes 32+1 = string ; warning does not support multibyte char like chinese
ESP_SENSOR_INTERVAL 164 //4 bytes = int
ESP_SETTINGS_VERSION 168 //8 bytes = 7+1 = string ESP3D + 2 digits
@ -179,44 +179,44 @@ label can be: light/framesize/quality/contrast/brightness/saturation/gainceiling
ESP_TELNET_ON 329 //1 byte = flag
ESP_WEBSOCKET_ON 330 //1 byte = flag
ESP_SD_SPEED_DIV 331 //1 byte = flag
ESP_NOTIFICATION_TOKEN1 332 //64 bytes 63+1 = string ; warning does not support multibyte char like chinese
ESP_NOTIFICATION_TOKEN2 396 //64 bytes 63+1 = string ; warning does not support multibyte char like chinese
ESP_SENSOR_TYPE 460 //1 bytes = flag
ESP_TARGET_FW 461 //1 bytes = flag
ESP_FREE 462 //1 bytes = flag
ESP_TIME_IS_DST 463 //1 bytes = flag
ESP_TIME_SERVER1 464 //129 bytes 128+1 = string ; warning does not support multibyte char like chinese
ESP_TIME_SERVER2 593 //129 bytes 128+1 = string ; warning does not support multibyte char like chinese
ESP_TIME_SERVER3 722 //129 bytes 128+1 = string ; warning does not support multibyte char like chinese
ESP_REMOTE_SCREEN 851 //1 bytes = flag
ESP_SD_MOUNT 852 //1 bytes = flag
ESP_SESSION_TIMEOUT 853 //1 bytes = flag
ESP_WEBSOCKET_FLAG 854 //1 bytes = flag
ESP_SD_CHECK_UPDATE_AT_BOOT 855//1 bytes = flag
ESP_NOTIFICATION_SETTINGS 856 //129 bytes 128+1 = string ; warning does not support multibyte char like chinese
ESP_CALIBRATION_1 985 //4 bytes = int
ESP_CALIBRATION_2 989 //4 bytes = int
ESP_CALIBRATION_3 993 //4 bytes = int
ESP_CALIBRATION_4 997 //4 bytes = int
ESP_CALIBRATION_5 1001 //4 bytes = int
ESP_SETUP 1005 //1 byte = flag
ESP_TELNET_FLAG 1006 //1 byte = flag
ESP_BT_FLAG 1007 //1 byte = flag
ESP_SCREEEN_FLAG 1008 //1 byte = flag
ESP_FTP_CTRL_PORT 1009 //4 bytes = int
ESP_FTP_DATA_ACTIVE_PORT 1013 //4 bytes = int
ESP_FTP_DATA_PASSIVE_PORT 1017 //4 bytes = int
ESP_FTP_ON 1021 //1 byte = flag
ESP_AUTO_NOTIFICATION 1022 //1 byte = flag
ESP_VERBOSE_BOOT 1023 //1 byte = flag
ESP_WEBDAV_ON 1024 //1 byte = flag
ESP_WEBDAV_PORT 1025 //4 bytes = int
ESP_STA_DNS_VALUE 1029 //4 bytes= int
ESP_SECURE_SERIAL 1033 //1 byte = flag
ESP_SERIAL_BRIDGE_ON 1036 //1 byte = flag
ESP_SERIAL_BRIDGE_FLAG 1037 //1 byte = flag
ESP_SERIAL_BRIDGE_BAUD 1038 //4 bytes= int
ESP_TIME_ZONE 1042 //7 bytes= string
ESP_NOTIFICATION_TOKEN1 332 //251 bytes 250+1 = string ; warning does not support multibyte char like chinese
ESP_NOTIFICATION_TOKEN2 583 //64 bytes 63+1 = string ; warning does not support multibyte char like chinese
ESP_SENSOR_TYPE 647 //1 bytes = flag
ESP_TARGET_FW 648 //1 bytes = flag
FREE 649 //1 bytes = flag
FREE 650 //1 bytes = flag
ESP_TIME_SERVER1 651 //129 bytes 128+1 = string ; warning does not support multibyte char like chinese
ESP_TIME_SERVER2 780 //129 bytes 128+1 = string ; warning does not support multibyte char like chinese
ESP_TIME_SERVER3 909 //129 bytes 128+1 = string ; warning does not support multibyte char like chinese
ESP_REMOTE_SCREEN 1038 //1 bytes = flag
ESP_SD_MOUNT 1039 //1 bytes = flag
ESP_SESSION_TIMEOUT 1040 //1 bytes = flag
FREE FLAG 1041 //1 bytes = flag
ESP_SD_CHECK_UPDATE_AT_BOOT 1042//1 bytes = flag
ESP_NOTIFICATION_SETTINGS 1043 //129 bytes 128+1 = string ; warning does not support multibyte char like chinese
ESP_CALIBRATION_1 1172 //4 bytes = int
ESP_CALIBRATION_2 1176 //4 bytes = int
ESP_CALIBRATION_3 1180 //4 bytes = int
ESP_CALIBRATION_4 1184 //4 bytes = int
ESP_CALIBRATION_5 1188 //4 bytes = int
ESP_SETUP 1192 //1 byte = flag
FREE 1193 //1 byte = flag
FREE 1194 //1 byte = flag
FREE 1195 //1 byte = flag
ESP_FTP_CTRL_PORT 1196 //4 bytes = int
ESP_FTP_DATA_ACTIVE_PORT 1200 //4 bytes = int
ESP_FTP_DATA_PASSIVE_PORT 1204 //4 bytes = int
ESP_FTP_ON 1208 //1 byte = flag
ESP_AUTO_NOTIFICATION 1209 //1 byte = flag
ESP_VERBOSE_BOOT 1210 //1 byte = flag
ESP_WEBDAV_ON 1211 //1 byte = flag
ESP_WEBDAV_PORT 1212 //4 bytes = int
ESP_STA_DNS_VALUE 1216 //4 bytes= int
ESP_SECURE_SERIAL 1220 //1 byte = flag
ESP_SERIAL_BRIDGE_ON 1223 //1 byte = flag
FREE 1224 //1 byte = flag
ESP_SERIAL_BRIDGE_BAUD 1225 //4 bytes= int
ESP_TIME_ZONE 1229 //7 bytes= string
```
* Get/Set Check update at boot state which can be ON, OFF
@ -238,6 +238,12 @@ label can be: light/framesize/quality/contrast/brightness/saturation/gainceiling
output is JSON or plain text according parameter
`[ESP450]json=<no> <pwd=admin/user>`
* Get authentication level
`[ESP500] json=<no>`
* Get/Set session timeout
`[ESP510]<timeout in minutes> json=<no> pwd=<admin password>`
* Change admin password
`[ESP550]<password> json=<no> pwd=<admin password>`
@ -299,8 +305,6 @@ label can be: light/framesize/quality/contrast/brightness/saturation/gainceiling
* Get state / Set Enable / Disable buzzer
`[ESP910]<ENABLE/DISABLE> json=<no> pwd=<admin/user password>`
* Get state / Set state of output message clients
`[ESP920]<SERIAL / SCREEN / REMOTE_SCREEN/ WEBSOCKET / TELNET /BT / ALL>=<ON/OFF> json=<no> pwd=<admin/user password>`
* Get state / Set Enable / Disable Serial Bridge Communication
`[ESP930]<ENABLE/DISABLE> json=<no> pwd=<admin/user password>`

157
docs/authentication.md Normal file
View File

@ -0,0 +1,157 @@
+++
archetype = "section"
title = "Authentication"
description = "What is authentication in ESP3D?"
weight = 800
+++
# Definition
The authentication is an additional security layer to protect the ESP3D web interface and ESP3D commands from unauthorized access. It is based on a username and a password. The authentication is optional and can be enabled/disabled in the ESP3D configuration. There are 3 login levels for authentication:
- guest, which is does not need any authentication
- user, which has limited access to ESP3D features
- admin, which has full access to ESP3D features
Currently the login cannot be customized and so is limited to `user` and `admin` levels. The `guest` level is always available and cannot be disabled.
# Configuration
In configuration.h just uncomment the following line to enable the authentication:
```c++
#define AUTHENTICATION_FEATURE
```
Default password authentication for `admin` is `admin` and for 'user' is `user`. You can change them using WebInterface or [ESP550] and [ESP555] commands.
# Usage
## Web Interface
When user authentication is enabled, the web interface will ask for a username and a password. If the authentication is successful, the user will be redirected to the web interface. If the authentication fails, the user will be redirected to the login page.
The web interface allows also inline authentication. This means that you can pass the username and password in the URL. This is useful if you want to use some command line to access the web interface like curl or wget. The URL format is the following:
```html
http://user:password@<ip_address>
```
On the web interface an authenticated session will stay open until the browser is closed. So if you close the browser and reopen it, you will be asked for authentication again. This session can also have a timeout. The default timeout is 3 minutes of inactivity. This timeout can be changed in the ESP3D configuration web interface or using `[ESP510]` command.
## ESPXXX Command
When user authentication is enabled, the ESPXXX commands will ask for a password. If the authentication is successful, the command will be executed. If the authentication fails, the command will be rejected.
The session for ESPXXX commands is a sticky session. This means that once authenticated, the session will stay authenticated until the ESP3D is restarted or session is closed (e.g: Telnet / WebSocket).
# Limitations
The current authentication miss a lot of features, like:
- user management
- https support
- password encryption
- password recovery
- password expiration in time
- password lockout if too many failed attempts
So you must not consider authentication as fullproof for security. It is just an additional layer of security.
Because ESPXXX commands only rely on password, do not use same password for user and admin users. If you do so, you will not be able to use ESPXXX commands with user level, everything will be considered as admin when authenticated.
The password are never been displayed in clear text, but they are stored in the ESP3D configuration in clear text. So if you want to change the password, you must use the WebInterface or ESPXXX commands.
In web interface the passwords are replaced by `*******` so any modification must be complete not partial.
All passwords and sensitive informations are sent using plain text. So if you want to use ESP3D in a public network or outside of your local network (which is not recommended), you must use a VPN.
# Scope
Here the scope of right for each authentication level:
|Feature | not authenticated | guest | user | admin |
|-|:-------------------:|:--------:|:------:|:-------:|
| Web Interface | No | No | Yes | Yes |
| Telnet | No | No | Yes | Yes |
| WebSocket | No | No | Yes | Yes |
| WebDav | No | No | Yes | Yes |
| Bluetooth | No | No | Yes | Yes |
| Upload | No | No | Yes | Yes |
| Update | No | No | No | Yes |
| ESP0 | Yes | Yes | Yes | Yes |
| ESP100 | No | No | Get | Get/Set |
| ESP101 | No | No | No | Set |
| ESP102 | No | No | Get | Get/Set |
| ESP103 | No | No | Get | Get/Set |
| ESP104 | No | No | Get | Get/Set |
| ESP105 | No | No | Get | Get/Set |
| ESP106 | No | No | Set | Set |
| ESP107 | No | No | Get | Get/Set |
| ESP108 | No | No | Get | Get/Set |
| ESP110 | No | No | Get | Get/Set |
| ESP111 | No | No | Get | Get |
| ESP112 | No | No | Get | Get/Set |
| ESP114 | No | No | Get | Get/Set |
| ESP115 | No | No | Get | Get/Set |
| ESP120 | No | No | Get | Get/Set |
| ESP121 | No | No | Get | Get/Set |
| ESP130 | No | No | Get | Get/Set |
| ESP131 | No | No | Get | Get/Set |
| ESP140 | No | No | Get | Get/Set |
| ESP150 | No | No | Get | Get/Set |
| ESP160 | No | No | Get | Get/Set |
| ESP161 | No | No | Get | Get/Set |
| ESP170 | No | No | Get/Set | Get/Set |
| ESP171 | No | No | Get | Get|
| ESP180 | No | No | Get | Get/Set |
| ESP181 | No | No | Get | Get/Set |
| ESP190 | No | No | Get | Get/Set |
| ESP191 | No | No | Get | Get/Set |
| ESP200 | No | No | Get/Set | Get/Set |
| ESP201 | No | No | Get/Set | Get/Set |
| ESP202 | No | No | Get | Get/Set |
| ESP210 | No | No | Get | Get/Set |
| ESP214 | No | No | Set | Set |
| ESP215 | No | No | No | Set |
| ESP220 | No | No | Get | Get |
| ESP250 | No | No | Set | Set |
| ESP290 | No | No | Set | Set |
| ESP400 | No | No | Get | Get |
| ESP401 | No | No | No | Set |
| ESP402 | No | No | Get | Get/Set |
| ESP410 | No | No | Get | Get |
| ESP420 | No | No | Get | Get |
| ESP444 | No | No | Set(only RESTART) | Set |
| ESP450 | No | No | Get | Get |
| ESP500 | Get/Set | Get/Set | Get/Set | Get/Set |
| ESP510 | No | No | Get | Get/Set |
| ESP550 | No | No | No | Get/Set |
| ESP555 | No | No | Get/Set | Get/Set |
| ESP600 | No | No | Set | Set |
| ESP610 | No | No | Get | Get/Set |
| ESP620 | No | No | Set | Set |
| ESP700 | No | No | Set | Set |
| ESP701 | No | No | Get/Set | Get/Set |
| ESP710 | No | No | No | Set |
| ESP715 | No | No | No | Set |
| ESP720 | No | No | Get | Get |
| ESP730 | No | No | Get/Set | Get/Set |
| ESP740 | No | No | Get | Get |
| ESP750 | No | No | Get/Set | Get/Set |
| ESP780 | No | No | Get | Get |
| ESP790 | No | No | Get/Set | Get/Set |
| ESP800 | No | No | Get/Set | Get/Set |
| ESP900 | No | No | Get/Set | Get/Set |
| ESP901 | No | No | Get | Get/Set |
| ESP910 | No | No | Get/Set | Get/Set |
| ESP930 | No | No | Get | Get/Set |
| ESP931 | No | No | Get | Get/Set |
| ESP999 | No | No | No | Set |
# API Description
## Global
Each authenticated session have unique session id, that will be stored on ESP3D with additionnal informations:
- session id (25 characters)
- session level (Guest / Admin / User)
- client_id (serial / http / telnet / WebSocket)
- session last activity (timestamp)
- client IP (http)
- Client socket ID (telnet / WebSocket)
## Http
When authentication is enabled, the http server will check if the session is authenticated. If not, it will ask for authentication. If the session is authenticated, it will check if the session is still valid. If not, it will ask for authentication again. If the session is still valid, it will process the request.
the Session ID is stored in the cookie `ESP3D_SESSIONID`.

View File

@ -90,7 +90,7 @@ FTP_Passive_Port = 55600
#Auto notification
AUTONOTIFICATION = Yes
#Notification type None / PushOver / Line / Email / Telegram /IFTTT
#Notification type None / PushOver / Line / Email / Telegram / IFTTT / HomeAssistant
NOTIF_TYPE = None
#Notification token 1 string of 64 chars max

File diff suppressed because it is too large Load Diff

View File

@ -329,7 +329,7 @@ app.get("/command", function (req, res) {
},
{
F: "service/ftp",
P: "1021",
P: "1208",
T: "B",
V: "1",
H: "enable",
@ -337,7 +337,7 @@ app.get("/command", function (req, res) {
},
{
F: "service/ftp",
P: "1009",
P: "1196",
T: "I",
V: "21",
H: "control port",
@ -346,7 +346,7 @@ app.get("/command", function (req, res) {
},
{
F: "service/ftp",
P: "1013",
P: "1200",
T: "I",
V: "20",
H: "active port",
@ -355,7 +355,7 @@ app.get("/command", function (req, res) {
},
{
F: "service/ftp",
P: "1017",
P: "1204",
T: "I",
V: "55600",
H: "passive port",
@ -364,7 +364,7 @@ app.get("/command", function (req, res) {
},
{
F: "service/notification",
P: "1004",
P: "1191",
T: "B",
V: "1",
H: "auto notif",
@ -389,7 +389,7 @@ app.get("/command", function (req, res) {
},
{
F: "service/notification",
P: "396",
P: "583",
T: "S",
V: "********",
S: "63",
@ -398,7 +398,7 @@ app.get("/command", function (req, res) {
},
{
F: "service/notification",
P: "855",
P: "1042",
T: "S",
V: " ",
S: "127",
@ -407,7 +407,7 @@ app.get("/command", function (req, res) {
},
{
F: "system/system",
P: "461",
P: "648",
T: "B",
V: "40",
H: "targetfw",

View File

@ -498,9 +498,9 @@
// #define CAMERA_DEVICE_FLIP_VERTICALY
/* Flip horizontally
* Flip camera horizontally
*/
//#define CAMERA_DEVICE_FLIP_HORIZONTALY
* Flip camera horizontally
*/
// #define CAMERA_DEVICE_FLIP_HORIZONTALY
/************************************
*
@ -527,7 +527,7 @@
/* Enable authentication
* Force usage of authentication for commands
*/
// #define AUTHENTICATION_FEATURE
//#define AUTHENTICATION_FEATURE
/************************************
*
@ -564,7 +564,7 @@
/* Gcode Host Feature
* This feature allows to process Gcode files like macros.
*/
#define GCODE_HOST_FEATURE
// #define GCODE_HOST_FEATURE
/* Settings location
* SETTINGS_IN_EEPROM //ESP8266/ESP32
@ -576,7 +576,7 @@
/* Add serial task
* ESP32 need to add a task to handle serial communication
*/
#define SERIAL_INDEPENDANT_TASK
// #define SERIAL_INDEPENDANT_TASK
/************************************
*
@ -592,11 +592,11 @@
// LOG_OUTPUT_SERIAL2
// LOG_OUTPUT_TELNET
// LOG_OUTPUT_WEBSOCKET
// #define ESP_LOG_FEATURE LOG_OUTPUT_SERIAL0
#define ESP_LOG_FEATURE LOG_OUTPUT_SERIAL0
#define ESP3D_DEBUG_LEVEL LOG_LEVEL_ERROR
#define ESP3D_DEBUG_LEVEL LOG_LEVEL_DEBUG
// #define ESP_NO_SANITY_CHECK
#define ESP_NO_SANITY_CHECK
#ifdef ESP_LOG_FEATURE
#define LOG_ESP3D_BAUDRATE 115200
@ -643,4 +643,4 @@
#undef NOTIFICATION_FEATURE
#endif
#endif //_CONFIGURATION_H
#endif //_CONFIGURATION_H

View File

@ -1,969 +0,0 @@
/*
commands.cpp - ESP3D commands class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// #define ESP_LOG_FEATURE LOG_OUTPUT_SERIAL0
#include "commands.h"
#include "../include/esp3d_config.h"
#include "esp3d.h"
#include "esp3doutput.h"
#include "settings_esp3d.h"
#if COMMUNICATION_PROTOCOL == MKS_SERIAL
#include "../modules/mks/mks_service.h"
#endif // COMMUNICATION_PROTOCOL == MKS_SERIAL
Commands esp3d_commands;
Commands::Commands() {}
Commands::~Commands() {}
// dispatch the command
void Commands::process(uint8_t *sbuf, size_t len, ESP3DOutput *output,
level_authenticate_type auth, ESP3DOutput *outputonly,
uint8_t outputignore) {
static bool lastIsESP3D = false;
log_esp3d("Client is %d, has only %d, has ignore %d",
output ? output->client() : 0,
outputonly ? outputonly->client() : 0, outputignore);
if (is_esp_command(sbuf, len)) {
lastIsESP3D = true;
size_t slen = len;
String tmpbuf = (const char *)sbuf;
if (tmpbuf.startsWith("echo:")) {
tmpbuf.replace("echo: ", "");
tmpbuf.replace("echo:", "");
slen = tmpbuf.length();
}
uint8_t cmd[4] = {0, 0, 0, 0};
cmd[0] = tmpbuf[4] == ']' ? 0 : tmpbuf[4];
cmd[1] = tmpbuf[5] == ']' ? 0 : tmpbuf[5];
cmd[2] = tmpbuf[6] == ']' ? 0 : tmpbuf[6];
cmd[3] = 0x0;
log_esp3d("It is ESP command %s", cmd);
log_esp3d("Respond to client %d", (outputonly == nullptr)
? output->client()
: outputonly->client());
execute_internal_command(
String((const char *)cmd).toInt(),
(slen > (strlen((const char *)cmd) + 5))
? (const char *)&tmpbuf[strlen((const char *)cmd) + 5]
: "",
auth, (outputonly == nullptr) ? output : outputonly);
} else {
// Work around to avoid to dispatch single \n to everyone as it is part of
// previous ESP3D command
if (lastIsESP3D && len == 1 && sbuf[0] == '\n') {
lastIsESP3D = false;
return;
}
lastIsESP3D = false;
// Dispatch to all clients but current or to define output
#if defined(HTTP_FEATURE)
// the web command will never get answer as answer go to websocket
// This is sanity check as the http client should already answered
if (output->client() == ESP_HTTP_CLIENT && !output->footerSent()) {
if (auth != LEVEL_GUEST) {
output->printMSG("");
} else {
output->printERROR("Wrong authentication!", 401);
return;
}
}
#endif // HTTP_FEATURE
if (outputonly == nullptr) {
log_esp3d("Dispatch from %d, but %d", output->client(), outputignore);
output->dispatch(sbuf, len, outputignore);
} else {
log_esp3d("Dispatch from %d to only %d", output->client(),
outputonly->client());
#if COMMUNICATION_PROTOCOL == MKS_SERIAL
if (outputonly->client() == ESP_SERIAL_CLIENT) {
MKSService::sendGcodeFrame((const char *)sbuf);
} else {
outputonly->write(sbuf, len);
}
#else
outputonly->write(sbuf, len);
#endif // COMMUNICATION_PROTOCOL == MKS_SERIAL
}
}
}
// check if current line is an [ESPXXX] command
bool Commands::is_esp_command(uint8_t *sbuf, size_t len) {
// TODO
// M117 should be handled here and transfered to [ESP214] if it is an host
if (len < 5) {
return false;
}
if ((char(sbuf[0]) == '[') && (char(sbuf[1]) == 'E') &&
(char(sbuf[2]) == 'S') && (char(sbuf[3]) == 'P') &&
((char(sbuf[4]) == ']') || (char(sbuf[5]) == ']') ||
(char(sbuf[6]) == ']') || (char(sbuf[7]) == ']'))) {
return true;
}
if ((char(sbuf[0]) == 'e') && (char(sbuf[1]) == 'c') &&
(char(sbuf[2]) == 'h') && (char(sbuf[3]) == 'o') &&
(char(sbuf[4]) == ':') && (char(sbuf[5]) == ' ') &&
(char(sbuf[6]) == '[') && (char(sbuf[7]) == 'E')) {
if (len >= 14) {
if ((char(sbuf[8]) == 'S') && (char(sbuf[9]) == 'P') &&
((char(sbuf[4]) == ']') || (char(sbuf[5]) == ']') ||
(char(sbuf[6]) == ']') || (char(sbuf[7]) == ']'))) {
return true;
}
}
}
return false;
}
// find space in string
// if space is has \ before it is ignored
int Commands::get_space_pos(const char *string, uint from) {
uint len = strlen(string);
if (len < from) {
return -1;
}
for (uint i = from; i < len; i++) {
if (string[i] == ' ') {
// if it is first char
if (i == from) {
return from;
}
// if not first one and previous char is not '\'
if (string[i - 1] != '\\') {
return (i);
}
}
}
return -1;
}
// return first label but pwd using labelseparator (usualy =) like
// mylabel=myvalue will return mylabel
const char *Commands::get_label(const char *cmd_params,
const char *labelseparator,
uint8_t startindex) {
static String res;
String tmp = "";
res = "";
int start = 1;
int end = -1;
res = cmd_params;
res.replace("\r ", "");
res.replace("\n ", "");
res.trim();
if ((res.length() == 0) || (startindex >= res.length())) {
return res.c_str();
}
if (strlen(labelseparator) > 0) {
end = res.indexOf(labelseparator, startindex);
if (end == -1) {
return "";
}
start = end;
for (int8_t p = end; p >= startindex; p--, start--) {
if (res[p] == ' ') {
p = -1;
start += 2;
}
}
if (start == -1) {
start = 0;
}
if (start > end) {
return "";
}
tmp = res.substring(start, end);
if (tmp == "pwd") {
res = get_label(cmd_params, labelseparator, end + 1);
} else {
res = tmp;
}
}
return res.c_str();
}
const char *Commands::format_response(uint cmdID, bool isjson, bool isok,
const char *message) {
static String res;
res = "";
if (!isjson) {
res += message;
} else {
res = "{\"cmd\":\"";
res += String(cmdID);
res += "\",\"status\":\"";
if (isok) {
res += "ok";
} else {
res += "error";
}
res += "\",\"data\":";
if (message[0] != '{') {
res += "\"";
}
res += message;
if (message[0] != '{') {
res += "\"";
}
res += "}";
}
return res.c_str();
}
const char *Commands::clean_param(const char *cmd_params) {
static String res;
res = cmd_params;
if (strlen(cmd_params) == 0) {
return "";
}
String tmp = cmd_params;
tmp.trim();
if (tmp == "json" || tmp.startsWith("json ")) {
return "";
}
if (tmp.indexOf("json=") != -1) {
// remove formating flag
res = tmp.substring(0, tmp.indexOf("json="));
} else {
if (tmp.endsWith(" json")) {
// remove formating flag
res = tmp.substring(0, tmp.length() - 5);
}
}
return res.c_str();
}
// extract parameter with corresponding label
// if label is empty give whole line without authentication label/parameter
const char *Commands::get_param(const char *cmd_params, const char *label) {
static String res;
res = "";
int start = 1;
int end = -1;
String tmp = "";
String slabel = " ";
res = cmd_params;
res.replace("\r ", "");
res.replace("\n ", "");
res.trim();
if (res.length() == 0) {
return res.c_str();
}
tmp = " " + res;
slabel += label;
if (strlen(label) > 0) {
start = tmp.indexOf(slabel);
if (start == -1) {
return "";
}
start += slabel.length();
end = get_space_pos(tmp.c_str(), start);
}
if (end == -1) {
end = tmp.length();
}
// extract parameter
res = tmp.substring(start, end);
#ifdef AUTHENTICATION_FEATURE
// if no label remove authentication parameters
if (strlen(label) == 0) {
tmp = " " + res;
start = tmp.indexOf(" pwd=");
if (start != -1) {
end = get_space_pos(tmp.c_str(), start + 1);
res = "";
if (start != 0) {
res = tmp.substring(0, start);
}
if (end != -1) {
res += " " + tmp.substring(end + 1, tmp.length());
}
}
}
#endif // AUTHENTICATION_FEATURE
// remove space format
res.replace("\\ ", " ");
// be sure no extra space
res.trim();
return res.c_str();
}
bool Commands::has_tag(const char *cmd_params, const char *tag) {
log_esp3d("Checking for tag: %s, in %s", tag, cmd_params);
String tmp = "";
String stag = " ";
if ((strlen(cmd_params) == 0) || (strlen(tag) == 0)) {
log_esp3d_e("No value provided for tag");
return false;
}
stag += tag;
tmp = cmd_params;
tmp.trim();
tmp = " " + tmp;
if (tmp.indexOf(stag) == -1) {
log_esp3d("No tag detected");
return false;
}
log_esp3d("Tag detected");
// to support plain , plain=yes , plain=no
String param = String(tag) + "=";
log_esp3d("Checking %s , in %s", param.c_str(), cmd_params);
String parameter = get_param(cmd_params, param.c_str());
if (parameter.length() != 0) {
log_esp3d("Parameter is %s", parameter.c_str());
if (parameter == "YES" || parameter == "true" || parameter == "TRUE" ||
parameter == "yes" || parameter == "1") {
return true;
}
log_esp3d("No parameter to enable %s ", param.c_str());
return false;
}
log_esp3d("No parameter for %s but tag detected", param.c_str());
return true;
}
// execute internal command
bool Commands::execute_internal_command(int cmd, const char *cmd_params,
level_authenticate_type auth_level,
ESP3DOutput *output) {
#ifndef SERIAL_COMMAND_FEATURE
if (output->client() == ESP_SERIAL_CLIENT) {
output->printMSG("Feature disabled");
return false;
}
#endif // SERIAL_COMMAND_FEATURE
bool response = true;
level_authenticate_type auth_type = auth_level;
// log_esp3d("Authentication = %d", auth_type);
// override if parameters
#ifdef AUTHENTICATION_FEATURE
// do not overwrite previous authetic <time=YYYY-MM-DD#H24:MM:SS>ation level
if (auth_type == LEVEL_GUEST) {
String pwd = get_param(cmd_params, "pwd=");
auth_type = AuthenticationService::authenticated_level(pwd.c_str(), output);
}
#endif // AUTHENTICATION_FEATURE
// log_esp3d("Authentication = %d", auth_type);
String parameter;
switch (cmd) {
// ESP3D Help
//[ESP0] or [ESP]
case 0:
response = ESP0(cmd_params, auth_type, output);
break;
#if defined(WIFI_FEATURE)
// STA SSID
//[ESP100]<SSID>[pwd=<admin password>]
case 100:
response = ESP100(cmd_params, auth_type, output);
break;
// STA Password
//[ESP101]<Password>[pwd=<admin password>]
case 101:
response = ESP101(cmd_params, auth_type, output);
break;
#endif // WIFI_FEATURE
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
// Change STA IP mode (DHCP/STATIC)
//[ESP102]<mode>pwd=<admin password>
case 102:
response = ESP102(cmd_params, auth_type, output);
break;
// Change STA IP/Mask/GW
//[ESP103]IP=<IP> MSK=<IP> GW=<IP> pwd=<admin password>
case 103:
response = ESP103(cmd_params, auth_type, output);
break;
#endif // WIFI_FEATURE ||ETH_FEATURE
#if defined(WIFI_FEATURE) || defined(BLUETOOTH_FEATURE) || defined(ETH_FEATURE)
// Set fallback mode which can be BT, WIFI-AP, OFF
//[ESP104]<state>pwd=<admin password>
case 104:
response = ESP104(cmd_params, auth_type, output);
break;
#endif // WIFI_FEATURE || BLUETOOTH_FEATURE || ETH_FEATURE)
#if defined(WIFI_FEATURE)
// AP SSID
//[ESP105]<SSID>[pwd=<admin password>]
case 105:
response = ESP105(cmd_params, auth_type, output);
break;
// AP Password
//[ESP106]<Password>[pwd=<admin password>]
case 106:
response = ESP106(cmd_params, auth_type, output);
break;
// Change AP IP
//[ESP107]<IP> pwd=<admin password>
case 107:
response = ESP107(cmd_params, auth_type, output);
break;
// Change AP channel
//[ESP108]<channel>pwd=<admin password>
case 108:
response = ESP108(cmd_params, auth_type, output);
break;
#endif // WIFI_FEATURE
#if defined(WIFI_FEATURE) || defined(BLUETOOTH_FEATURE) || defined(ETH_FEATURE)
// Set radio state at boot which can be BT, WIFI-STA, WIFI-AP, ETH-STA, OFF
//[ESP110]<state>pwd=<admin password>
case 110:
response = ESP110(cmd_params, auth_type, output);
break;
#endif // WIFI_FEATURE || BLUETOOTH_FEATURE || ETH_FEATURE)
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
// Get current IP
//[ESP111]
case 111:
response = ESP111(cmd_params, auth_type, output);
break;
#endif // WIFI_FEATURE || ETH_FEATURE)
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE) || defined(BT_FEATURE)
// Get/Set hostname
//[ESP112]<Hostname> pwd=<admin password>
case 112:
response = ESP112(cmd_params, auth_type, output);
break;
// Get/Set boot Network (WiFi/BT/Ethernet) state which can be ON, OFF
//[ESP114]<state>pwd=<admin password>
case 114:
response = ESP114(cmd_params, auth_type, output);
break;
// Get/Set immediate Network (WiFi/BT/Ethernet) state which can be ON, OFF
//[ESP115]<state>pwd=<admin password>
case 115:
response = ESP115(cmd_params, auth_type, output);
break;
#endif // WIFI_FEATURE|| ETH_FEATURE || BT_FEATURE
#ifdef HTTP_FEATURE
// Set HTTP state which can be ON, OFF
//[ESP120]<state>pwd=<admin password>
case 120:
response = ESP120(cmd_params, auth_type, output);
break;
// Set HTTP port
//[ESP121]<port>pwd=<admin password>
case 121:
response = ESP121(cmd_params, auth_type, output);
break;
#endif // HTTP_FEATURE
#ifdef TELNET_FEATURE
// Set TELNET state which can be ON, OFF
//[ESP130]<state>pwd=<admin password>
case 130:
response = ESP130(cmd_params, auth_type, output);
break;
// Set TELNET port
//[ESP131]<port>pwd=<admin password>
case 131:
response = ESP131(cmd_params, auth_type, output);
break;
#endif // TELNET_FEATURE
#ifdef TIMESTAMP_FEATURE
// Sync / Set / Get current time
//[ESP140]<SYNC> <srv1=XXXXX> <srv2=XXXXX> <srv3=XXXXX> <zone=xxx>
//<dst=YES/NO> <time=YYYY-MM-DD#H24:MM:SS> pwd=<admin password>
case 140:
response = ESP140(cmd_params, auth_type, output);
break;
#endif // TIMESTAMP_FEATURE
// Get/Set display/set boot delay in ms / Verbose boot
//[ESP150]<delay=time in milliseconds><verbose=YES/NO>[pwd=<admin password>]
case 150:
response = ESP150(cmd_params, auth_type, output);
break;
#ifdef WS_DATA_FEATURE
// Set WebSocket state which can be ON, OFF
//[ESP160]<state>pwd=<admin password>
case 160:
response = ESP160(cmd_params, auth_type, output);
break;
// Set WebSocket port
//[ESP161]<port>pwd=<admin password>
case 161:
response = ESP161(cmd_params, auth_type, output);
break;
#endif // WS_DATA_FEATURE
#ifdef CAMERA_DEVICE
// Get/Set Camera command value / list all values in JSON/plain
//[ESP170]label=<value> pwd=<admin/user password>
// label can be:
// light/framesize/quality/contrast/brightness/saturation/gainceiling/colorbar/awb/agc/aec/hmirror/vflip/awb_gain/agc_gain/aec_value/aec2/cw/bpc/wpc/raw_gma/lenc/special_effect/wb_mode/ae_level
case 170:
response = ESP170(cmd_params, auth_type, output);
break;
// Save frame to target path and filename (default target = today date,
// default name=timestamp.jpg) [ESP171]path=<target path> filename=<target
// filename> pwd=<admin/user password>
case 171:
response = ESP171(cmd_params, auth_type, output);
break;
#endif // CAMERA_DEVICE
#ifdef FTP_FEATURE
// Set Ftp state which can be ON, OFF
//[ESP180]<state>pwd=<admin password>
case 180:
response = ESP180(cmd_params, auth_type, output);
break;
// Set/get ftp ports
//[ESP181]ctrl=<port> active=<port> passive=<port> pwd=<admin password>
case 181:
response = ESP181(cmd_params, auth_type, output);
break;
#endif // FTP_FEATURE
#ifdef WEBDAV_FEATURE
// Set webdav state which can be ON, OFF
//[ESP190]<state>pwd=<admin password>
case 190:
response = ESP190(cmd_params, auth_type, output);
break;
// Set/get webdav port
//[ESP191]ctrl=<port> active=<port> passive=<port> pwd=<admin password>
case 191:
response = ESP191(cmd_params, auth_type, output);
break;
#endif // WEBDAV_FEATURE
#if defined(SD_DEVICE)
// Get/Set SD Card Status
//[ESP200] json=<YES/NO> <RELEASESD> <REFRESH> pwd=<user/admin password>
case 200:
response = ESP200(cmd_params, auth_type, output);
break;
#if SD_DEVICE != ESP_SDIO
// Get/Set SD card Speed factor 1 2 4 6 8 16 32
//[ESP202]SPEED=<value>pwd=<user/admin password>
case 202:
response = ESP202(cmd_params, auth_type, output);
break;
#endif // SD_DEVICE != ESP_SDIO
#ifdef SD_UPDATE_FEATURE
// Get/Set SD Check at boot state which can be ON, OFF
//[ESP402]<state>pwd=<admin password>
case 402:
response = ESP402(cmd_params, auth_type, output);
break;
#endif // #ifdef SD_UPDATE_FEATURE
#endif // SD_DEVICE
#ifdef DIRECT_PIN_FEATURE
// Get/Set pin value
//[ESP201]P<pin> V<value> [PULLUP=YES RAW=YES]pwd=<admin password>
case 201:
response = ESP201(cmd_params, auth_type, output);
break;
#endif // DIRECT_PIN_FEATURE
#ifdef SENSOR_DEVICE
// Get SENSOR Value / type/Set SENSOR type
//[ESP210] <TYPE> <type=NONE/xxx> <interval=XXX in millisec>
case 210:
response = ESP210(cmd_params, auth_type, output);
break;
#endif // #ifdef SENSOR_DEVICE
#if defined(DISPLAY_DEVICE)
// Output to esp screen status
//[ESP214]<Text>pwd=<user password>
case 214:
response = ESP214(cmd_params, auth_type, output);
break;
#if defined(DISPLAY_TOUCH_DRIVER)
// Touch Calibration
//[ESP215]<CALIBRATE>[pwd=<user password>]
case 215:
response = ESP215(cmd_params, auth_type, output);
break;
#endif // DISPLAY_TOUCH_DRIVER
#ifdef BUZZER_DEVICE
// Play sound
//[ESP250]F=<frequency> D=<duration> [pwd=<user password>]
case 250:
response = ESP250(cmd_params, auth_type, output);
break;
#endif // BUZZER_DEVICE
#endif // DISPLAY_DEVICE
// Show pins
//[ESP220][pwd=<user password>]
case 220:
response = ESP220(cmd_params, auth_type, output);
break;
// Delay command
//[ESP290]<delay in ms>[pwd=<user password>]
case 290:
response = ESP290(cmd_params, auth_type, output);
break;
// Get full ESP3D settings
//[ESP400]<pwd=admin>
case 400:
response = ESP400(cmd_params, auth_type, output);
break;
// Set EEPROM setting
//[ESP401]P=<position> T=<type> V=<value> pwd=<user/admin password>
case 401:
response = ESP401(cmd_params, auth_type, output);
break;
#if defined(WIFI_FEATURE)
// Get available AP list (limited to 30)
// output is JSON or plain text according parameter
//[ESP410]<plain>
case 410:
response = ESP410(cmd_params, auth_type, output);
break;
#endif // WIFI_FEATURE
// Get ESP current status
// output is JSON or plain text according parameter
//[ESP420]<plain>
case 420:
response = ESP420(cmd_params, auth_type, output);
break;
// Set ESP State
// cmd are RESTART / RESET
//[ESP444]<cmd><pwd=admin>
case 444:
response = ESP444(cmd_params, auth_type, output);
break;
#ifdef MDNS_FEATURE
// Get ESP3D list
//[ESP450] pwd=<admin/user password>
case 450:
response = ESP450(cmd_params, auth_type, output);
break;
#endif // MDNS_FEATURE
#ifdef AUTHENTICATION_FEATURE
// Change admin password
//[ESP550]<password>pwd=<admin password>
case 550:
response = ESP550(cmd_params, auth_type, output);
break;
// Change user password
//[ESP555]<password>pwd=<admin/user password>
case 555:
response = ESP555(cmd_params, auth_type, output);
break;
#endif // AUTHENTICATION_FEATURE
#if defined(NOTIFICATION_FEATURE)
// Send Notification
//[ESP600]<msg>[pwd=<admin password>]
case 600:
response = ESP600(cmd_params, auth_type, output);
break;
// Set/Get Notification settings
//[ESP610]type=<NONE/PUSHOVER/EMAIL/LINE> T1=<token1> T2=<token2>
// TS=<Settings> [pwd=<admin password>] Get will give type and settings only
// not the protected T1/T2
case 610:
response = ESP610(cmd_params, auth_type, output);
break;
// Send Notification using URL
//[ESP620]URL=<encoded url> [pwd=<admin password>]
case 620:
response = ESP620(cmd_params, auth_type, output);
break;
#endif // NOTIFICATION_FEATURE
#if defined(FILESYSTEM_FEATURE)
// Format ESP Filesystem
//[ESP710]FORMAT pwd=<admin password>
case 710:
response = ESP710(cmd_params, auth_type, output);
break;
// List ESP Filesystem
//[ESP720]<Root> pwd=<admin password>
case 720:
response = ESP720(cmd_params, auth_type, output);
break;
// Action on ESP Filesystem
// rmdir / remove / mkdir / exists
//[ESP730]<Action>=<path> pwd=<admin password>
case 730:
response = ESP730(cmd_params, auth_type, output);
break;
#endif // FILESYSTEM_FEATURE
#if defined(SD_DEVICE)
// Format ESP Filesystem
//[ESP715]FORMATSD pwd=<admin password>
case 715:
response = ESP715(cmd_params, auth_type, output);
break;
#endif // SD_DEVICE
#if defined(GCODE_HOST_FEATURE)
// Open local file
//[ESP700]<filename>
case 700:
response = ESP700(cmd_params, auth_type, output);
break;
// Get Status and Control ESP700 stream
//[ESP701]action=<PAUSE/RESUME/ABORT>
case 701:
response = ESP701(cmd_params, auth_type, output);
break;
#endif // GCODE_HOST_FEATURE
#if defined(SD_DEVICE)
// List SD Filesystem
//[ESP740]<Root> pwd=<admin password>
case 740:
response = ESP740(cmd_params, auth_type, output);
break;
// Action on SD Filesystem
// rmdir / remove / mkdir / exists
//[ESP750]<Action>=<path> pwd=<admin password>
case 750:
response = ESP750(cmd_params, auth_type, output);
break;
#endif // SD_DEVICE
#if defined(GLOBAL_FILESYSTEM_FEATURE)
// List Global Filesystem
//[ESP780]<Root> pwd=<admin password>
case 780:
response = ESP780(cmd_params, auth_type, output);
break;
// Action on Global Filesystem
// rmdir / remove / mkdir / exists
//[ESP790]<Action>=<path> pwd=<admin password>
case 790:
response = ESP790(cmd_params, auth_type, output);
break;
#endif // GLOBAL_FILESYSTEM_FEATURE
// Get fw version firmare target and fw version
// eventually set time with pc time
// output is JSON or plain text according parameter
//[ESP800]<plain><time=YYYY-MM-DD-HH-MM-SS>
case 800:
response = ESP800(cmd_params, auth_type, output);
break;
#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL
// Get state / Set Enable / Disable Serial Communication
//[ESP900]<ENABLE/DISABLE>
case 900:
response = ESP900(cmd_params, auth_type, output);
break;
// Get / Set Serial Baud Rate
//[ESP901]<BAUD RATE> json=<no> pwd=<admin/user password>
case 901:
response = ESP901(cmd_params, auth_type, output);
break;
#endif // COMMUNICATION_PROTOCOL != SOCKET_SERIAL
#ifdef BUZZER_DEVICE
// Get state / Set Enable / Disable buzzer
//[ESP910]<ENABLE/DISABLE>
case 910:
response = ESP910(cmd_params, auth_type, output);
break;
#endif // BUZZER_DEVICE
case 920:
// Get state / Set state of output message clients
//[ESP910]<SERIAL / SCREEN / REMOTE_SCREEN/ WEBSOCKET / TELNET /BT /
// ALL>=<ON/OFF>[pwd=<admin password>]
response = ESP920(cmd_params, auth_type, output);
break;
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
// Get state / Set Enable / Disable Serial Bridge Communication
//[ESP930]<ENABLE/DISABLE>
case 930:
response = ESP930(cmd_params, auth_type, output);
break;
// Get / Set Serial Bridge Baud Rate
//[ESP931]<BAUD RATE> json=<no> pwd=<admin/user password>
case 931:
response = ESP931(cmd_params, auth_type, output);
break;
#endif // defined(ESP_SERIAL_BRIDGE_OUTPUT)
#if defined(ARDUINO_ARCH_ESP32) && \
(CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32S2 || \
CONFIG_IDF_TARGET_ESP32C3)
case 999:
// Set quiet boot if strapping pin is High
//[ESP999]<QUIETBOOT> [pwd=<admin/user password>]
response = ESP999(cmd_params, auth_type, output);
break;
#endif // ARDUINO_ARCH_ESP32
default:
output->printERROR("Invalid Command");
response = false;
}
return response;
}
bool Commands::_dispatchSetting(
bool json, const char *filter, ESP3DSettingIndex index, const char *help,
const char **optionValues, const char **optionLabels, uint32_t maxsize,
uint32_t minsize, uint32_t minsize2, uint8_t precision, const char *unit,
bool needRestart, ESP3DOutput *output, bool isFirst) {
String tmpstr;
String value;
char out_str[255];
tmpstr.reserve(
350); // to save time and avoid several memories allocation delay
const ESP3DSettingDescription *elementSetting =
Settings_ESP3D::getSettingPtr(index);
if (!elementSetting) {
return false;
}
switch (elementSetting->type) {
case ESP3DSettingType::byte_t:
value = String(Settings_ESP3D::read_byte(index));
break;
case ESP3DSettingType::integer_t:
value = String(Settings_ESP3D::read_uint32(index));
break;
case ESP3DSettingType::ip_t:
value = Settings_ESP3D::read_IP_String(index);
break;
case ESP3DSettingType::float_t:
// TODO Add float support ?
value = "Not supported";
log_esp3d_e("Float not supported");
return false;
break;
case ESP3DSettingType::mask:
// TODO Add Mask support ?
value = "Not supported";
log_esp3d_e("Mask not supported");
return false;
break;
case ESP3DSettingType::bitsfield:
// TODO Add bitfield support ?
value = "Not supported";
log_esp3d_e("Bitsfield not supported");
return false;
break;
case ESP3DSettingType::string_t:
// String
if (index == ESP_AP_PASSWORD || index == ESP_STA_PASSWORD ||
#ifdef NOTIFICATION_FEATURE
index == ESP_NOTIFICATION_TOKEN1 ||
index == ESP_NOTIFICATION_TOKEN2 ||
#endif // NOTIFICATION_FEATURE
index == ESP_USER_PWD || index == ESP_ADMIN_PWD) { // hide passwords
// using ********
value = HIDDEN_PASSWORD;
} else {
value = Settings_ESP3D::read_string(index);
}
break;
default:
value = "Not supported";
log_esp3d_e("Type not supported");
return false;
break;
}
if (json) {
if (!isFirst) {
tmpstr += ",";
}
tmpstr += "{\"F\":\"";
tmpstr += filter;
tmpstr += "\",\"P\":\"";
tmpstr += String(static_cast<uint16_t>(index));
tmpstr += "\",\"T\":\"";
// Limited support already done for above so even not supported we can
// define it once will be enabled
switch (elementSetting->type) {
case ESP3DSettingType::byte_t:
tmpstr += "B";
break;
case ESP3DSettingType::integer_t:
tmpstr += "I";
break;
case ESP3DSettingType::ip_t:
tmpstr += "A";
break;
case ESP3DSettingType::float_t:
tmpstr += "F";
break;
case ESP3DSettingType::mask:
tmpstr += "M";
break;
case ESP3DSettingType::bitsfield:
tmpstr += "X";
break;
default:
tmpstr += "S";
}
tmpstr += "\",\"V\":\"";
tmpstr += value;
tmpstr += "\",\"H\":\"";
tmpstr += help;
tmpstr += "\"";
if (needRestart) {
tmpstr += ",\"R\":\"1\"";
}
if (optionValues && optionLabels) {
tmpstr += ",\"O\":[";
for (uint8_t i = 0; i < maxsize; i++) {
if (i > 0) {
tmpstr += ",";
}
tmpstr += "{\"";
// be sure we have same size for both array to avoid overflow
tmpstr += optionLabels[i];
tmpstr += "\":\"";
tmpstr += optionValues[i];
tmpstr += "\"}";
}
tmpstr += "]";
}
if (unit) {
tmpstr += ",\"R\":\"";
tmpstr += unit;
tmpstr += "\"";
}
if (precision != ((uint8_t)-1)) {
tmpstr += ",\"E\":\"";
tmpstr += String(precision);
tmpstr += "\"";
}
if (maxsize != (uint32_t)-1 && !optionValues) {
tmpstr += ",\"S\":\"";
tmpstr += String(maxsize);
tmpstr += "\"";
}
if (minsize != (uint32_t)-1) {
tmpstr += ",\"M\":\"";
tmpstr += String(minsize);
tmpstr += "\"";
}
if (minsize2 != (uint32_t)-1) {
tmpstr += ",\"MS\":\"";
tmpstr += String(minsize2);
tmpstr += "\"";
}
tmpstr += "}";
} else {
tmpstr = filter;
tmpstr += "/";
tmpstr += help;
tmpstr += ": ";
tmpstr += value;
tmpstr += "\n";
}
output->print(tmpstr.c_str());
return true;
}

View File

@ -1,264 +0,0 @@
/*
commands.h - ESP3D commands class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef COMMANDS_H
#define COMMANDS_H
#include <Arduino.h>
#include "../modules/authentication/authentication_service.h"
class ESP3DOutput;
class Commands {
public:
Commands();
~Commands();
void process(uint8_t* sbuf, size_t len, ESP3DOutput* output,
level_authenticate_type auth = LEVEL_GUEST,
ESP3DOutput* outputonly = nullptr, uint8_t outputignore = 0);
bool is_esp_command(uint8_t* sbuf, size_t len);
bool execute_internal_command(int cmd, const char* cmd_params,
level_authenticate_type auth_level,
ESP3DOutput* output);
int get_space_pos(const char* string, uint from = 0);
const char* get_param(const char* cmd_params, const char* label);
const char* get_label(const char* cmd_params, const char* labelseparator,
uint8_t startindex = 0);
const char* clean_param(const char* cmd_params);
const char* format_response(uint cmdID, bool isjson = false, bool isok = true,
const char* message = "");
bool has_tag(const char* cmd_params, const char* tag);
bool ESP0(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#if defined(WIFI_FEATURE)
bool ESP100(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP101(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // WIFI_FEATURE
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
bool ESP102(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP103(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // WIFI_FEATURE ||ETH_FEATURE
#if defined(WIFI_FEATURE) || defined(BLUETOOTH_FEATURE) || defined(ETH_FEATURE)
bool ESP104(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // WIFI_FEATURE || BLUETOOTH_FEATURE || ETH_FEATURE
#if defined(WIFI_FEATURE)
bool ESP105(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP106(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP107(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP108(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // WIFI_FEATURE
#if defined(WIFI_FEATURE) || defined(BLUETOOTH_FEATURE) || defined(ETH_FEATURE)
bool ESP110(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // WIFI_FEATURE || BLUETOOTH_FEATURE || ETH_FEATURE
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
bool ESP111(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // WIFI_FEATURE || ETH_FEATURE
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE) || defined(BT_FEATURE)
bool ESP112(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP114(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP115(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // WIFI_FEATURE || BLUETOOTH_FEATURE || ETH_FEATURE
#if defined(HTTP_FEATURE)
bool ESP120(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP121(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // HTTP_FEATURE
#if defined(TELNET_FEATURE)
bool ESP130(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP131(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // TELNET_FEATURE
#if defined(TIMESTAMP_FEATURE)
bool ESP140(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // TIMESTAMP_FEATURE
bool ESP150(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#if defined(WS_DATA_FEATURE)
bool ESP160(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP161(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // WS_DATA_FEATURE
#if defined(CAMERA_DEVICE)
bool ESP170(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP171(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // CAMERA_DEVICE
#if defined(FTP_FEATURE)
bool ESP180(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP181(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // FTP_FEATURE
#if defined(WEBDAV_FEATURE)
bool ESP190(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP191(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // WEBDAV_FEATURE
#if defined(SD_DEVICE)
bool ESP200(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#if SD_DEVICE != ESP_SDIO
bool ESP202(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // SD_DEVICE != ESP_SDIO
#ifdef SD_UPDATE_FEATURE
bool ESP402(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // SD_UPDATE_FEATURE
#endif // SD_DEVICE
#ifdef DIRECT_PIN_FEATURE
bool ESP201(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // DIRECT_PIN_FEATURE
#if defined(DISPLAY_DEVICE)
bool ESP214(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#if defined(DISPLAY_TOUCH_DRIVER)
bool ESP215(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // DISPLAY_TOUCH_DRIVER
#endif // DISPLAY_DEVICE
#ifdef SENSOR_DEVICE
bool ESP210(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // SENSOR_DEVICE
bool ESP220(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP290(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP400(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP401(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#if defined(WIFI_FEATURE)
bool ESP410(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // WIFI_FEATURE
bool ESP420(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP444(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#ifdef MDNS_FEATURE
bool ESP450(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // MDNS_FEATURE
#if defined(AUTHENTICATION_FEATURE)
bool ESP550(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP555(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // AUTHENTICATION_FEATURE
#if defined(NOTIFICATION_FEATURE)
bool ESP600(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP610(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP620(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // NOTIFICATION_FEATURE
#if defined(GCODE_HOST_FEATURE)
bool ESP700(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP701(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // GCODE_HOST_FEATURE
#if defined(FILESYSTEM_FEATURE)
bool ESP710(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP720(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP730(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // FILESYSTEM_FEATURE
#if defined(SD_DEVICE)
bool ESP715(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP750(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP740(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // SD_DEVICE
#if defined(GLOBAL_FILESYSTEM_FEATURE)
bool ESP780(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP790(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // GLOBAL_FILESYSTEM_FEATURE
bool ESP800(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL
bool ESP900(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP901(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // COMMUNICATION_PROTOCOL != SOCKET_SERIAL
bool ESP920(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
bool ESP930(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP931(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // defined (ESP_SERIAL_BRIDGE_OUTPUT)
#ifdef BUZZER_DEVICE
bool ESP910(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
bool ESP250(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // BUZZER_DEVICE
#if defined(ARDUINO_ARCH_ESP32) && \
(CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32S2 || \
CONFIG_IDF_TARGET_ESP32C3)
bool ESP999(const char* cmd_params, level_authenticate_type auth_level,
ESP3DOutput* output);
#endif // ARDUINO_ARCH_ESP32
private:
bool _dispatchSetting(bool json, const char* filter, ESP3DSettingIndex index,
const char* help, const char** optionValues,
const char** optionLabels, uint32_t maxsize,
uint32_t minsize, uint32_t minsize2, uint8_t precision,
const char* unit, bool needRestart, ESP3DOutput* output,
bool isFirst = false);
};
extern Commands esp3d_commands;
#endif // COMMANDS_H

View File

@ -18,9 +18,8 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#include "../commands.h"
#include "../esp3doutput.h"
#include "../settings_esp3d.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
const char* help[] = {
"[ESP] (id) - display this help",
#if defined(WIFI_FEATURE)
@ -46,7 +45,7 @@ const char* help[] = {
"WIFI-AP, WIFI-SETUP, ETH-STA, OFF",
#endif // WIFI_FEATURE || BLUETOOTH_FEATURE || ETH_FEATURE
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
"[ESP111](header)display current IP",
"[ESP111](OUTPUT=PRINT)(ALL) display current IP and network informations",
#endif // WIFI_FEATURE || ETH_FEATURE
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE) || defined(BT_FEATURE)
"[ESP112](Hostname) - display/set Hostname",
@ -129,12 +128,14 @@ const char* help[] = {
"[ESP450]display ESP3D list on network",
#endif // MDNS_FEATURE
#if defined(AUTHENTICATION_FEATURE)
"[ESP500]display authentication level",
"[ESP510](timeout) - set/display session time out",
"[ESP550](password) - change admin password",
"[ESP555](password) - change user password",
#endif // AUTHENTICATION_FEATURE
#if defined(NOTIFICATION_FEATURE)
"[ESP600](message) - send notification",
"[ESP610]type=(NONE/PUSHOVER/EMAIL/LINE/TELEGRAM/IFTTT) (T1=xxx) (T2=xxx) "
"[ESP610]type=(NONE/PUSHOVER/EMAIL/LINE/TELEGRAM/IFTTT/HOMEASSISTANT) (T1=xxx) (T2=xxx) "
"(TS=xxx) - display/set Notification settings",
"[ESP620]URL=http://XXXXXX - send GET notification",
#endif // NOTIFICATION_FEATURE
@ -172,10 +173,8 @@ const char* help[] = {
#ifdef BUZZER_DEVICE
"[ESP910](ENABLE/DISABLE) - display/set buzzer state",
#endif // BUZZER_DEVICE
"[ESP920](client)=(ON/OFF) - display/set SERIAL / SCREEN / REMOTE_SCREEN / "
"WEBSOCKET / TELNET /BT / ALL client state if available",
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
"[ESP930](ON/OFF/CLOSE) - display/set serial bridge state",
"[ESP930](ENABLE/DISABLE/CLOSE) - display/set serial bridge state",
"[ESP931]<BAUD RATE> - display/set serial bridge baud rate",
#endif // defined(ESP_SERIAL_BRIDGE_OUTPUT)
#if defined(ARDUINO_ARCH_ESP32) && \
@ -183,193 +182,232 @@ const char* help[] = {
CONFIG_IDF_TARGET_ESP32C3)
"[ESP999](QUIETBOOT) [pwd=<admin/user password>] - set quiet boot mode",
#endif // ARDUINO_ARCH_ESP32
""};
const uint cmdlist[] = {0,
};
const uint cmdlist[] = {
0,
#if defined(WIFI_FEATURE)
100, 101,
100, 101,
#endif // WIFI_FEATURE
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
102, 103,
102, 103,
#endif // WIFI_FEATURE || ETH_FEATURE
#if defined(WIFI_FEATURE) || defined(BLUETOOTH_FEATURE) || defined(ETH_FEATURE)
104,
104,
#endif // WIFI_FEATURE || BLUETOOTH_FEATURE || ETH_FEATURE
#if defined(WIFI_FEATURE)
105, 106, 107, 108,
105, 106, 107, 108,
#endif // WIFI_FEATURE
#if defined(WIFI_FEATURE) || defined(BLUETOOTH_FEATURE) || defined(ETH_FEATURE)
110,
110,
#endif // WIFI_FEATURE || BLUETOOTH_FEATURE || ETH_FEATURE
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
111,
111,
#endif // WIFI_FEATURE || ETH_FEATURE
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE) || defined(BT_FEATURE)
112, 114, 115,
112, 114, 115,
#endif // WIFI_FEATURE || ETH_FEATURE || BT_FEATURE
#if defined(HTTP_FEATURE)
120, 121,
120, 121,
#endif // HTTP_FEATURE
#if defined(TELNET_FEATURE)
130, 131,
130, 131,
#endif // TELNET_FEATURE
#if defined(TIMESTAMP_FEATURE)
140,
140,
#endif // TIMESTAMP_FEATURE
150,
150,
#if defined(WS_DATA_FEATURE)
160, 161,
160, 161,
#endif // WS_DATA_FEATURE
#if defined(CAMERA_DEVICE)
170, 171,
170, 171,
#endif // CAMERA_DEVICE
#if defined(FTP_FEATURE)
180, 181,
180, 181,
#endif // FTP_FEATURE
#if defined(WEBDAV_FEATURE)
190, 191,
190, 191,
#endif // WEBDAV_FEATURE
#if defined(SD_DEVICE)
200,
200,
#endif // SD_DEVICE
#ifdef DIRECT_PIN_FEATURE
201,
201,
#endif // DIRECT_PIN_FEATURE
#if defined(SD_DEVICE) && SD_DEVICE != ESP_SDIO
202,
202,
#endif // SD_DEVICE
#ifdef SENSOR_DEVICE
210,
210,
#endif // SENSOR_DEVICE
#if defined(DISPLAY_DEVICE)
214,
214,
#if defined(DISPLAY_TOUCH_DRIVER)
215,
215,
#endif // DISPLAY_TOUCH_DRIVER
#endif // DISPLAY_DEVICE
220,
220,
#ifdef BUZZER_DEVICE
250,
250,
#endif // BUZZER_DEVICE
290, 400, 401,
290, 400, 401,
#ifdef SD_UPDATE_FEATURE
402,
#endif // SD_UPDATE_FEATURE
#if defined(WIFI_FEATURE)
410,
410,
#endif // WIFI_FEATURE
420, 444,
420, 444,
#ifdef MDNS_FEATURE
450,
450,
#endif // MDNS_FEATURE
#if defined(AUTHENTICATION_FEATURE)
550, 555,
500, 510, 550, 555,
#endif // AUTHENTICATION_FEATURE
#if defined(NOTIFICATION_FEATURE)
600, 610, 620,
600, 610, 620,
#endif // NOTIFICATION_FEATURE
#if defined(GCODE_HOST_FEATURE)
700, 701,
700, 701,
#endif // GCODE_HOST_FEATURE
#if defined(FILESYSTEM_FEATURE)
710,
710,
#endif // FILESYSTEM_FEATURE
#if defined(SD_DEVICE)
715,
715,
#endif // SD_DEVICE
#if defined(FILESYSTEM_FEATURE)
720, 730,
720, 730,
#endif // FILESYSTEM_FEATURE
#if defined(SD_DEVICE)
740, 750,
740, 750,
#endif // SD_DEVICE
#if defined(GLOBAL_FILESYSTEM_FEATURE)
780, 790,
780, 790,
#endif // GLOBAL_FILESYSTEM_FEATURE
800,
800,
#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL
900, 901,
900, 901,
#endif // COMMUNICATION_PROTOCOL != SOCKET_SERIAL
#ifdef BUZZER_DEVICE
910,
910,
#endif // BUZZER_DEVICE
920,
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
930, 935,
930, 931,
#endif // defined(ESP_SERIAL_BRIDGE_OUTPUT)
#if defined(ARDUINO_ARCH_ESP32) && \
(CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32S2 || \
CONFIG_IDF_TARGET_ESP32C3)
999,
999,
#endif // ARDUINO_ARCH_ESP32 && CONFIG_IDF_TARGET_ESP32S3 ||
// CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3
0};
};
// ESP3D Help
//[ESP0] or [ESP]<command>
bool Commands::ESP0(const char* cmd_params, level_authenticate_type auth_type,
ESP3DOutput* output) {
bool noError = true;
String parameter;
void ESP3DCommands::ESP0(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
msg->target = target;
msg->origin = ESP3DClientType::command;
String tmpstr;
const uint cmdNb = sizeof(help) / sizeof(char*);
(void)auth_type;
bool json = has_tag(cmd_params, "json");
parameter = clean_param(get_param(cmd_params, ""));
if (parameter.length() == 0) {
const uint cmdlistNb = sizeof(cmdlist) / sizeof(uint);
bool json = hasTag(msg, cmd_params_pos, "json");
if (cmdNb != cmdlistNb) {
esp3d_log("Help corrupted: %d vs %d", cmdNb, cmdlistNb);
msg->type = ESP3DMessageType::unique;
if (!dispatch(msg, "Help corrupted")) {
esp3d_log_e("Error sending command to clients");
}
return;
}
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
// use msg for first answer
if (json) {
output->print("{\"cmd\":\"0\",\"status\":\"ok\",\"data\":[");
tmpstr = "{\"cmd\":\"0\",\"status\":\"ok\",\"data\":[";
} else {
output->printMSGLine("[List of ESP3D commands]");
tmpstr = "[List of ESP3D commands]\n";
}
msg->type = ESP3DMessageType::head;
if (!dispatch(msg, tmpstr.c_str())) {
esp3d_log_e("Error sending command to clients");
return;
}
yield();
for (uint i = 0; i < cmdNb; i++) {
if (json) {
tmpstr = "{\"id\":\"";
tmpstr += String(cmdlist[i]);
tmpstr += "\",\"help\":\"";
tmpstr += help[i];
tmpstr += "\"}";
if (i < cmdNb - 1 && (cmdNb - 1) > 0) {
tmpstr += ",";
}
} else {
tmpstr = help[i];
tmpstr += "\n";
}
if (!dispatch(tmpstr.c_str(), target, requestId,
ESP3DMessageType::core)) {
esp3d_log_e("Error sending answer to clients");
return;
}
esp3d_log("Help sent: %d", i);
yield();
}
for (uint i = 0; i < cmdNb - 1; i++) {
if (json) {
output->print("{\"id\":\"");
output->print(String(cmdlist[i]).c_str());
output->print("\",\"help\":\"");
output->print(String(help[i]).c_str());
output->print("\"}");
if (i < cmdNb - 2) {
output->print(",");
if (json) {
if (!dispatch("]}", target, requestId, ESP3DMessageType::tail)) {
esp3d_log_e("Error sending answer to clients");
return;
}
} else {
if (!dispatch("ok\n", target, requestId, ESP3DMessageType::tail)) {
esp3d_log_e("Error sending answer to clients");
return;
}
}
} else {
uint cmdval = atoi(tmpstr.c_str());
for (uint i = 0; i < cmdNb; i++) {
if (cmdlist[i] == cmdval) {
if (json) {
tmpstr = "{\"cmd\":\"0\",\"status\":\"ok\",\"data\":{\"id\":\"";
tmpstr += String(cmdval);
tmpstr += "\",\"help\":\"";
tmpstr += help[i];
tmpstr += "\"}}";
} else {
tmpstr = help[i];
tmpstr += "\n";
}
} else {
output->printMSGLine(help[i]);
msg->type = ESP3DMessageType::unique;
if (!dispatch(msg, tmpstr.c_str())) {
return;
}
return;
}
}
if (json) {
output->printLN("]}");
tmpstr =
"{\"cmd\":\"0\",\"status\":\"error\",\"data\":\"This command is not "
"supported: ";
tmpstr += String(cmdval);
tmpstr += "\"}";
} else {
tmpstr = "This command is not supported: ";
tmpstr += String(cmdval);
tmpstr += "\n";
}
} else {
bool found = false;
uint cmdval = parameter.toInt();
if (sizeof(help) / sizeof(char*) != sizeof(cmdlist) / sizeof(uint)) {
String s = "Error in code:" + String(sizeof(help) / sizeof(char*)) +
"entries vs " + String(sizeof(cmdlist) / sizeof(uint));
output->printLN(s.c_str());
return false;
}
for (uint i = 0; i < cmdNb - 1; i++) {
if (cmdlist[i] == cmdval) {
if (json) {
output->print("{\"cmd\":\"0\",\"status\":\"ok\",\"data\":{\"id\":\"");
output->print(String(cmdval).c_str());
output->print("\",\"help\":\"");
output->print(help[i]);
output->printLN("\"}}");
} else {
output->printMSGLine(help[i]);
}
found = true;
}
}
if (!found) {
String msg = "This command is not supported: ";
msg += parameter;
noError = false;
String response = format_response(0, json, noError, msg.c_str());
if (json) {
output->printLN(response.c_str());
} else {
output->printERROR(response.c_str());
}
msg->type = ESP3DMessageType::unique;
if (!dispatch(msg, tmpstr.c_str())) {
return;
}
}
return noError;
}

View File

@ -0,0 +1,86 @@
/*
ESP100.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(WIFI_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/wifi/wificonfig.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 100
// STA SSID
//[ESP100]<SSID>[json=no] [pwd=<admin password>]
void ESP3DCommands::ESP100(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
const ESP3DSettingDescription* settingPtr =
ESP3DSettings::getSettingPtr(ESP_STA_SSID);
if (settingPtr) {
ok_msg = ESP3DSettings::readString(ESP_STA_SSID);
} else {
hasError = true;
error_msg = "This setting is unknown";
}
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
esp3d_log(
"got %s param for a value of %s, is valid %d", tmpstr.c_str(),
tmpstr.c_str(),
ESP3DSettings::isValidStringSetting(tmpstr.c_str(), ESP_STA_SSID));
if (ESP3DSettings::isValidStringSetting(tmpstr.c_str(), ESP_STA_SSID)) {
esp3d_log("Value %s is valid", tmpstr.c_str());
if (!ESP3DSettings::writeString(ESP_STA_SSID, tmpstr.c_str())) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // WIFI_FEATURE

View File

@ -0,0 +1,78 @@
/*
ESP101.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(WIFI_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/wifi/wificonfig.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 101
// STA Password
//[ESP101]<Password> [json=no] [pwd=<admin password>]
void ESP3DCommands::ESP101(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
bool clearSetting = hasTag(msg, cmd_params_pos, "NOPASSWORD");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
hasError = true;
error_msg = "Password not displayable";
} else {
if (clearSetting) {
esp3d_log("NOPASSWORD flag detected, set string to empty string");
tmpstr = "";
}
esp3d_log(
"got %s param for a value of %s, is valid %d", tmpstr.c_str(),
tmpstr.c_str(),
ESP3DSettings::isValidStringSetting(tmpstr.c_str(), ESP_STA_PASSWORD));
if (ESP3DSettings::isValidStringSetting(tmpstr.c_str(), ESP_STA_PASSWORD)) {
esp3d_log("Value %s is valid", tmpstr.c_str());
if (!ESP3DSettings::writeString(ESP_STA_PASSWORD, tmpstr.c_str())) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // WIFI_FEATURE

View File

@ -0,0 +1,100 @@
/*
ESP102.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
#include "../../modules/network/netconfig.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#if defined(WIFI_FEATURE)
#include "../../modules/wifi/wificonfig.h"
#endif // WIFI_FEATURE
#if defined(ETH_FEATURE)
#include "../../modules/ethernet/ethconfig.h"
#endif // ETH_FEATURE
#include "../../modules/authentication/authentication_service.h"
#define COMMAND_ID 102
// Change STA IP mode (DHCP/STATIC)
//[ESP102]<mode>[json=no] [pwd=<admin password>]
void ESP3DCommands::ESP102(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
uint8_t byteValue = (uint8_t)-1;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
byteValue = ESP3DSettings::readByte(ESP_STA_IP_MODE);
if (byteValue == static_cast<uint8_t>(DHCP_MODE)) {
ok_msg = "DHCP";
} else if (byteValue == static_cast<uint8_t>(STATIC_IP_MODE)) {
ok_msg = "STATIC";
} else {
ok_msg = "Unknown:" + String(byteValue);
}
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (tmpstr == "DHCP") {
byteValue = static_cast<uint8_t>(DHCP_MODE);
} else if (tmpstr == "STATIC") {
byteValue = static_cast<uint8_t>(STATIC_IP_MODE);
} else {
byteValue = (uint8_t)-1; // unknow flag so put outof range value
}
esp3d_log("got %s param for a value of %d, is valid %d", tmpstr.c_str(),
byteValue,
ESP3DSettings::isValidByteSetting(byteValue, ESP_STA_IP_MODE));
if (ESP3DSettings::isValidByteSetting(byteValue, ESP_STA_IP_MODE)) {
esp3d_log("Value %d is valid", byteValue);
if (!ESP3DSettings::writeByte(ESP_STA_IP_MODE, byteValue)) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // WIFI_FEATURE || ETH_FEATURE

View File

@ -0,0 +1,124 @@
/*
ESP103.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
#include "../../modules/network/netconfig.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#if defined(WIFI_FEATURE)
#include "../../modules/wifi/wificonfig.h"
#endif // WIFI_FEATURE
#if defined(ETH_FEATURE)
#include "../../modules/ethernet/ethconfig.h"
#endif // ETH_FEATURE
#include "../../modules/authentication/authentication_service.h"
#define COMMAND_ID 103
// Change STA IP/Mask/GW
//[ESP103]IP=<IP> MSK=<IP> GW=<IP> DNS=<IP> [json=no] [pwd=<admin password>
void ESP3DCommands::ESP103(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
const char* cmdList[] = {"IP=", "MSK=", "GW=", "DNS="};
uint8_t cmdListSize = sizeof(cmdList) / sizeof(char*);
const ESP3DSettingIndex settingIndex[] = {
ESP_STA_IP_VALUE, ESP_STA_MASK_VALUE, ESP_STA_GATEWAY_VALUE,
ESP_STA_DNS_VALUE};
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
if (json) {
ok_msg = "{\"ip\":\"";
} else {
ok_msg = "IP: ";
}
ok_msg += ESP3DSettings::readIPString(ESP_STA_IP_VALUE);
if (json) {
ok_msg += "\",\"gw\":\"";
} else {
ok_msg += ", GW: ";
}
ok_msg += ESP3DSettings::readIPString(ESP_STA_GATEWAY_VALUE);
if (json) {
ok_msg += "\",\"msk\":\"";
} else {
ok_msg += ", MSK: ";
}
ok_msg += ESP3DSettings::readIPString(ESP_STA_MASK_VALUE);
if (json) {
ok_msg += "\",\"dns\":\"";
} else {
ok_msg += ", DNS: ";
}
ok_msg += ESP3DSettings::readIPString(ESP_STA_DNS_VALUE);
if (json) {
ok_msg += "\"}";
}
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
bool hasParam = false;
for (uint8_t i = 0; i < cmdListSize; i++) {
tmpstr = get_param(msg, cmd_params_pos, cmdList[i]);
if (tmpstr.length() != 0) {
hasParam = true;
if (ESP3DSettings::isValidIPStringSetting(tmpstr.c_str(),
settingIndex[i])) {
esp3d_log("Value %s is valid", tmpstr.c_str());
if (!ESP3DSettings::writeIPString(settingIndex[i], tmpstr.c_str())) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
}
if (!hasParam && !hasError) {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // WIFI_FEATURE || ETH_FEATURE

View File

@ -0,0 +1,111 @@
/*
ESP104.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(WIFI_FEATURE) || defined(BLUETOOTH_FEATURE) || defined(ETH_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/network/netconfig.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 104
// Set STA fallback mode state at boot which can be BT, WIFI-SETUP, OFF
//[ESP104]<state> json=<no> pwd=<admin password>
void ESP3DCommands::ESP104(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
uint8_t byteValue = (uint8_t)-1;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
byteValue = ESP3DSettings::readByte(ESP_STA_FALLBACK_MODE);
#if defined(BLUETOOTH_FEATURE)
if (byteValue == (uint8_t)ESP_BT) {
ok_msg = "BT";
} else
#endif // BLUETOOTH_FEATURE
#if defined(WIFI_FEATURE)
if (byteValue == (uint8_t)ESP_AP_SETUP) {
ok_msg = "CONFIG";
} else
#endif // WIFI_FEATURE
if (byteValue == (uint8_t)ESP_NO_NETWORK) {
ok_msg = "OFF";
} else {
ok_msg = "Unknown";
}
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
#if defined(BLUETOOTH_FEATURE)
if (tmpstr == "BT") {
byteValue = (uint8_t)ESP_BT;
} else
#endif // BLUETOOTH_FEATURE
#if defined(WIFI_FEATURE)
if (tmpstr == "CONFIG") {
byteValue = (uint8_t)ESP_AP_SETUP;
} else
#endif // WIFI_FEATURE
if (tmpstr == "OFF") {
byteValue = (uint8_t)ESP_NO_NETWORK;
} else {
byteValue = (uint8_t)-1; // unknow flag so put outof range value
}
esp3d_log(
"got %s param for a value of %d, is valid %d", tmpstr.c_str(),
byteValue,
ESP3DSettings::isValidByteSetting(byteValue, ESP_STA_FALLBACK_MODE));
if (ESP3DSettings::isValidByteSetting(byteValue, ESP_STA_FALLBACK_MODE)) {
esp3d_log("Value %d is valid", byteValue);
if (!ESP3DSettings::writeByte(ESP_STA_FALLBACK_MODE, byteValue)) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // WIFI_FEATURE

View File

@ -0,0 +1,85 @@
/*
ESP105.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(WIFI_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/wifi/wificonfig.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 105
// AP SSID
//[ESP105]<SSID> [json=no] [pwd=<admin password>]
void ESP3DCommands::ESP105(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
const ESP3DSettingDescription* settingPtr =
ESP3DSettings::getSettingPtr(ESP_AP_SSID);
if (settingPtr) {
ok_msg = ESP3DSettings::readString(ESP_AP_SSID);
} else {
hasError = true;
error_msg = "This setting is unknown";
}
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
esp3d_log("got %s param for a value of %s, is valid %d", tmpstr.c_str(),
tmpstr.c_str(),
ESP3DSettings::isValidStringSetting(tmpstr.c_str(), ESP_AP_SSID));
if (ESP3DSettings::isValidStringSetting(tmpstr.c_str(), ESP_AP_SSID)) {
esp3d_log("Value %s is valid", tmpstr.c_str());
if (!ESP3DSettings::writeString(ESP_AP_SSID, tmpstr.c_str())) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // WIFI_FEATURE

View File

@ -0,0 +1,85 @@
/*
ESP106.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(WIFI_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/wifi/wificonfig.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 106
// AP Password
//[ESP106]<Password> [json=no] [pwd=<admin password>]
void ESP3DCommands::ESP106(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool clearSetting = hasTag(msg, cmd_params_pos, "NOPASSWORD");
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
hasError = true;
error_msg = "Password not displayable";
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (clearSetting) {
esp3d_log("NOPASSWORD flag detected, set string to empty string");
tmpstr = "";
}
esp3d_log(
"got %s param for a value of %s, is valid %d", tmpstr.c_str(),
tmpstr.c_str(),
ESP3DSettings::isValidStringSetting(tmpstr.c_str(), ESP_AP_PASSWORD));
if (ESP3DSettings::isValidStringSetting(tmpstr.c_str(), ESP_AP_PASSWORD)) {
esp3d_log("Value %s is valid", tmpstr.c_str());
if (!ESP3DSettings::writeString(ESP_AP_PASSWORD, tmpstr.c_str())) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // WIFI_FEATURE

View File

@ -0,0 +1,78 @@
/*
ESP107.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(WIFI_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/network/netconfig.h"
#include "../../modules/wifi/wificonfig.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 107
// Change AP IP
//[ESP107]<IP> [json=no] pwd=<admin password>
void ESP3DCommands::ESP107(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
ok_msg = ESP3DSettings::readIPString(ESP_AP_IP_VALUE);
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (ESP3DSettings::isValidIPStringSetting(tmpstr.c_str(),
ESP_AP_IP_VALUE)) {
esp3d_log("Value %s is valid", tmpstr.c_str());
if (!ESP3DSettings::writeIPString(ESP_AP_IP_VALUE, tmpstr.c_str())) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // WIFI_FEATURE

View File

@ -0,0 +1,82 @@
/*
ESP108.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(WIFI_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/wifi/wificonfig.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 108
// Change AP channel
//[ESP108]<channel> [json=no] [pwd=<admin password>]
void ESP3DCommands::ESP108(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
uint8_t byteValue = 0;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
byteValue = ESP3DSettings::readByte(ESP_AP_CHANNEL);
ok_msg = String(byteValue);
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
byteValue = atoi(tmpstr.c_str());
esp3d_log("got %s param for a value of %d, is valid %d", tmpstr.c_str(),
byteValue,
ESP3DSettings::isValidByteSetting(byteValue, ESP_AP_CHANNEL));
if (ESP3DSettings::isValidByteSetting(byteValue, ESP_AP_CHANNEL)) {
esp3d_log("Value %d is valid", byteValue);
if (!ESP3DSettings::writeByte(ESP_AP_CHANNEL, byteValue)) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // WIFI_FEATURE

View File

@ -0,0 +1,127 @@
/*
ESP110.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(WIFI_FEATURE) || defined(BLUETOOTH_FEATURE) || defined(ETH_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/network/netconfig.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 110
// Set radio state at boot which can be BT, WIFI-STA, WIFI-AP, ETH-STA, OFF
//[ESP110]<state> json=<no> pwd=<admin password>
void ESP3DCommands::ESP110(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
uint8_t byteValue = (uint8_t)-1;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
byteValue = ESP3DSettings::readByte(ESP_RADIO_MODE);
if (byteValue == ESP_BT) {
ok_msg = "BT";
} else
#if defined(WIFI_FEATURE)
if (byteValue == ESP_WIFI_AP) {
ok_msg = "WIFI-AP";
} else if (byteValue == ESP_WIFI_STA) {
ok_msg = "WIFI-STA";
} else if (byteValue == ESP_AP_SETUP) {
ok_msg = "WIFI-SETUP";
} else if (byteValue == ESP_ETH_STA) {
ok_msg = "ETH-STA";
} else
#endif // WIFI_FEATURE
if (byteValue == ESP_NO_NETWORK) {
ok_msg = "OFF";
} else {
ok_msg = "Unknown";
}
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr.toUpperCase();
if (tmpstr == "BT") {
byteValue = ESP_BT;
} else
#if defined(WIFI_FEATURE)
if (tmpstr == "WIFI-AP") {
byteValue = ESP_WIFI_AP;
} else if (tmpstr == "WIFI-STA") {
byteValue = ESP_WIFI_STA;
} else if (tmpstr == "WIFI-SETUP") {
byteValue = ESP_AP_SETUP;
} else
#endif // WIFI_FEATURE
#if defined(ETH_FEATURE)
if (tmpstr == "ETH-STA") {
byteValue = ESP_ETH_STA;
} else
#endif // ETH_FEATURE
if (tmpstr == "OFF") {
byteValue = ESP_NO_NETWORK;
} else {
byteValue = (uint8_t)-1; // unknow flag so put outof range value
}
esp3d_log("got %s param for a value of %d, is valid %d", tmpstr.c_str(),
byteValue,
ESP3DSettings::isValidByteSetting(byteValue, ESP_RADIO_MODE));
if (ESP3DSettings::isValidByteSetting(byteValue, ESP_RADIO_MODE)) {
esp3d_log("Value %d is valid", byteValue);
if (!ESP3DSettings::writeByte(ESP_RADIO_MODE, byteValue)) {
hasError = true;
error_msg = "Set value failed";
} else {
if (!NetConfig::begin()) {
hasError = true;
error_msg = "Cannot setup network";
}
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // WIFI_FEATURE

View File

@ -0,0 +1,110 @@
/*
ESP111.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// #define ESP_LOG_FEATURE LOG_OUTPUT_SERIAL0
#include "../../include/esp3d_config.h"
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/network/netconfig.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 111
// Get current IP
//[ESP111] OUTPUT=PRINTER ALL [json=no]
void ESP3DCommands::ESP111(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
bool respjson = json;
bool showAll = hasTag(msg, cmd_params_pos, "ALL");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
tmpstr = get_param(msg, cmd_params_pos, "OUTPUT=");
if (tmpstr == "PRINTER") {
msg->target = ESP3DClientType::remote_screen;
json = false;
}
if (!showAll) {
ok_msg = NetConfig::localIP().c_str();
} else {
if (json) {
ok_msg = "{\"ip\":\"";
} else {
ok_msg = "IP: ";
}
ok_msg += NetConfig::localIP().c_str();
if (json) {
ok_msg += "\",\"gw\":\"";
} else {
ok_msg += "\nGW: ";
}
ok_msg += NetConfig::localGW().c_str();
if (json) {
ok_msg += "\",\"msk\":\"";
} else {
ok_msg += "\nMSK: ";
}
ok_msg += NetConfig::localMSK().c_str();
if (json) {
ok_msg += "\",\"dns\":\"";
} else {
ok_msg += "\nDNS: ";
}
ok_msg += NetConfig::localDNS().c_str();
if (json) {
ok_msg += "\"}";
}
}
if (msg->target == ESP3DClientType::remote_screen) {
if (respjson) {
tmpstr = "{\"cmd\":\"111\",\"status\":\"ok\",\"data\":\"ok\"}";
} else {
tmpstr = "ok\n";
}
// send response to original client
if (!dispatch(tmpstr.c_str(), target, msg->request_id,
ESP3DMessageType::unique)) {
esp3d_log_e("Error sending response to original client");
}
}
// Printer does not support json just normal serial
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // WIFI_FEATURE

View File

@ -0,0 +1,87 @@
/*
ESP112.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE) || defined(BT_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/network/netconfig.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
// Get/Set hostname
//[ESP112]<Hostname> [json=no] pwd=<admin password>
#define COMMAND_ID 112
void ESP3DCommands::ESP112(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
const ESP3DSettingDescription* settingPtr =
ESP3DSettings::getSettingPtr(ESP_HOSTNAME);
if (settingPtr) {
ok_msg = ESP3DSettings::readString(ESP_HOSTNAME);
} else {
hasError = true;
error_msg = "This setting is unknown";
}
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
esp3d_log(
"got %s param for a value of %s, is valid %d", tmpstr.c_str(),
tmpstr.c_str(),
ESP3DSettings::isValidStringSetting(tmpstr.c_str(), ESP_HOSTNAME));
if (ESP3DSettings::isValidStringSetting(tmpstr.c_str(), ESP_HOSTNAME)) {
esp3d_log("Value %s is valid", tmpstr.c_str());
if (!ESP3DSettings::writeString(ESP_HOSTNAME, tmpstr.c_str())) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // WIFI_FEATURE || ETH_FEATURE || BT_FEATURE

View File

@ -0,0 +1,94 @@
/*
ESP114.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE) || defined(BT_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
// Get/Set Boot radio state which can be ON, OFF
//[ESP114]<state> json=<no> pwd=<user/admin password>
#define COMMAND_ID 114
void ESP3DCommands::ESP114(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
uint8_t byteValue = (uint8_t)-1;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
byteValue = ESP3DSettings::readByte(ESP_BOOT_RADIO_STATE);
if (byteValue == (uint8_t)ESP3DState::off) {
ok_msg = "OFF";
} else if (byteValue == (uint8_t)ESP3DState::on) {
ok_msg = "ON";
} else {
ok_msg = "Unknown";
}
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (tmpstr == "OFF") {
byteValue = (uint8_t)ESP3DState::off;
} else if (tmpstr == "ON") {
byteValue = (uint8_t)ESP3DState::on;
} else {
byteValue = (uint8_t)-1; // unknow flag so put outof range value
}
esp3d_log(
"got %s param for a value of %d, is valid %d", tmpstr.c_str(),
byteValue,
ESP3DSettings::isValidByteSetting(byteValue, ESP_BOOT_RADIO_STATE));
if (ESP3DSettings::isValidByteSetting(byteValue, ESP_BOOT_RADIO_STATE)) {
esp3d_log("Value %d is valid", byteValue);
if (!ESP3DSettings::writeByte(ESP_BOOT_RADIO_STATE, byteValue)) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // defined(WIFI_FEATURE) || defined(ETH_FEATURE) || defined(BT_FEATURE)

View File

@ -0,0 +1,86 @@
/*
ESP115.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE) || defined(BT_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/network/netconfig.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 115
// Get/Set immediate Network (WiFi/BT/Ethernet) state which can be ON, OFF
//[ESP115]<state> json=<no> pwd=<admin password>
void ESP3DCommands::ESP115(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
uint8_t current_radio_mode = NetConfig::getMode();
if (tmpstr.length() == 0) {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (current_radio_mode == ESP_NO_NETWORK) {
ok_msg = "OFF";
} else {
ok_msg = "ON";
}
} else {
if (tmpstr == "OFF") {
if (current_radio_mode != ESP_NO_NETWORK) {
NetConfig::end();
}
} else if (tmpstr == "ON") {
if (current_radio_mode == ESP_NO_NETWORK) {
if (!NetConfig::begin()) {
hasError = true;
error_msg = "Fail to start network";
}
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // WIFI_FEATURE || ETH_FEATURE || BT_FEATURE

View File

@ -0,0 +1,79 @@
/*
ESP120.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(HTTP_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 120
// Set HTTP state which can be ON, OFF
//[ESP120]<state> json=<no> pwd=<admin password>
void ESP3DCommands::ESP120(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
ESP3DState setting_http_mode =
(ESP3DState)ESP3DSettings::readByte(ESP_HTTP_ON);
if (tmpstr.length() == 0) {
if (setting_http_mode == ESP3DState::off) {
ok_msg = "OFF";
} else {
ok_msg = "ON";
}
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (tmpstr == "OFF" || tmpstr == "ON") {
if (!ESP3DSettings::writeByte(ESP_HTTP_ON, tmpstr == "OFF" ? 0 : 1)) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // HTTP_FEATURE

View File

@ -0,0 +1,81 @@
/*
ESP121.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(HTTP_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 121
// Set HTTP port
//[ESP121]<port> json=<no> pwd=<admin password>
void ESP3DCommands::ESP121(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
uint32_t intValue = 0;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
intValue = ESP3DSettings::readUint32(ESP_HTTP_PORT);
ok_msg = String(intValue);
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
intValue = atoi(tmpstr.c_str());
esp3d_log("got %s param for a value of %ld, is valid %d", tmpstr.c_str(),
intValue,
ESP3DSettings::isValidIntegerSetting(intValue, ESP_HTTP_PORT));
if (ESP3DSettings::isValidIntegerSetting(intValue, ESP_HTTP_PORT)) {
esp3d_log("Value %ld is valid", intValue);
if (!ESP3DSettings::writeUint32(ESP_HTTP_PORT, intValue)) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // HTTP_FEATURE

View File

@ -0,0 +1,89 @@
/*
ESP130.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(TELNET_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/telnet/telnet_server.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 130
// Set TELNET state which can be ON, OFF, CLOSE
//[ESP130]<state> json=<no> pwd=<admin password>
void ESP3DCommands::ESP130(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
bool closeClients = hasTag(msg, cmd_params_pos, "CLOSE");
bool stateON = hasTag(msg, cmd_params_pos, "ON");
bool stateOFF = hasTag(msg, cmd_params_pos, "OFF");
bool has_param = false;
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
ESP3DState setting_mode = (ESP3DState)ESP3DSettings::readByte(ESP_TELNET_ON);
if (tmpstr.length() == 0) {
if (setting_mode == ESP3DState::off) {
ok_msg = "OFF";
} else {
ok_msg = "ON";
}
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (stateON || stateOFF) {
if (!ESP3DSettings::writeByte(ESP_TELNET_ON, stateOFF ? 0 : 1)) {
hasError = true;
error_msg = "Set value failed";
}
has_param = true;
}
if (closeClients) {
has_param = true;
telnet_server.closeClient();
}
if (!has_param) {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // TELNET_FEATURE

View File

@ -0,0 +1,81 @@
/*
ESP131.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(TELNET_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 131
// Set TELNET port
//[ESP131]<port> json=<no> pwd=<admin password>
void ESP3DCommands::ESP131(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
uint32_t intValue = 0;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
intValue = ESP3DSettings::readUint32(ESP_TELNET_PORT);
ok_msg = String(intValue);
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
intValue = atoi(tmpstr.c_str());
esp3d_log("got %s param for a value of %ld, is valid %d", tmpstr.c_str(),
intValue,
ESP3DSettings::isValidIntegerSetting(intValue, ESP_TELNET_PORT));
if (ESP3DSettings::isValidIntegerSetting(intValue, ESP_TELNET_PORT)) {
esp3d_log("Value %ld is valid", intValue);
if (!ESP3DSettings::writeUint32(ESP_TELNET_PORT, intValue)) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // TELNET_FEATURE

View File

@ -0,0 +1,180 @@
/*
ESP140.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(TIMESTAMP_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/time/time_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 140
// Sync / Set / Get current time
//[ESP140]<SYNC> <srv1=XXXXX> <srv2=XXXXX> <srv3=XXXXX> <tzone=+HH:SS>
//<ntp=YES/NO> <time=YYYY-MM-DDTHH:mm:ss> NOW json=<no> pwd=<admin password>
void ESP3DCommands::ESP140(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
bool sync = hasTag(msg, cmd_params_pos, "sync");
bool now = hasTag(msg, cmd_params_pos, "now");
String tmpstr;
const char* cmdList[] = {"srv1", "srv2", "srv3", "tzone", "ntp", "time"};
uint8_t cmdListSize = sizeof(cmdList) / sizeof(char*);
const ESP3DSettingIndex settingIndex[] = {
ESP_TIME_SERVER1, ESP_TIME_SERVER2, ESP_TIME_SERVER3,
ESP_TIME_ZONE, ESP_INTERNET_TIME, (ESP3DSettingIndex)-1,
};
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
if (json) {
ok_msg = "{";
} else {
ok_msg = "";
}
// no need to show time right now
for (uint8_t i = 0; i < cmdListSize - 2; i++) {
if (json) {
if (i > 0) {
ok_msg += ",";
}
ok_msg += "\"";
} else {
if (i > 0) {
ok_msg += ", ";
}
}
ok_msg += cmdList[i];
if (json) {
ok_msg += "\":\"";
} else {
ok_msg += ": ";
}
ok_msg += ESP3DSettings::readString(settingIndex[i]);
if (json) {
ok_msg += "\"";
} else {
}
}
if (json) {
ok_msg += ",\"ntp\":\"";
} else {
ok_msg += ", ntp: ";
}
ok_msg += ESP3DSettings::readByte(ESP_INTERNET_TIME) ? "yes" : "no";
if (json) {
ok_msg += "\"";
} else {
}
if (json) {
ok_msg += "}";
}
} else {
bool hasParam = false;
String tmpkey;
for (uint8_t i = 0; i < cmdListSize; i++) {
tmpkey = cmdList[i];
tmpkey += "=";
tmpstr = get_param(msg, cmd_params_pos, tmpkey.c_str());
if (tmpstr.length() != 0) {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
hasParam = true;
if (settingIndex[i] == (ESP3DSettingIndex)-1) {
// set time
if (!timeService.setTime(tmpstr.c_str())) {
hasError = true;
error_msg = "Set time failed";
}
} else if (settingIndex[i] == ESP_INTERNET_TIME) {
tmpstr.toLowerCase();
if (tmpstr == "yes" || tmpstr == "no" || tmpstr == "1" ||
tmpstr == "0" || tmpstr == "true" || tmpstr == "false") {
uint8_t val = 0;
if (tmpstr == "yes" || tmpstr == "1" || tmpstr == "true") {
val = 1;
}
if (!ESP3DSettings::writeByte(settingIndex[i], val)) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid token parameter";
}
} else {
if (ESP3DSettings::isValidStringSetting(tmpstr.c_str(),
settingIndex[i])) {
esp3d_log("Value %s is valid", tmpstr.c_str());
if (!ESP3DSettings::writeString(settingIndex[i], tmpstr.c_str())) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid token parameter";
esp3d_log_e("Value %s is invalid", tmpstr.c_str());
}
}
}
}
if (!hasError && now) {
ok_msg = timeService.getCurrentTime();
ok_msg += " (";
ok_msg += timeService.getTimeZone();
ok_msg += ")";
hasParam = true;
}
if (!hasError && sync) {
// apply changes without restarting the board
timeService.begin();
hasParam = true;
}
if (!hasParam && !hasError) {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // TIMESTAMP_FEATURE

View File

@ -0,0 +1,123 @@
/*
ESP150.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#include "../../modules/authentication/authentication_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 150
// Get/Set display/set boot delay in ms / Verbose boot
//[ESP150]<delay=time in milliseconds><verbose=ON/OFF>[pwd=<admin password>]
void ESP3DCommands::ESP150(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
String tmpstr;
bool json = hasTag(msg, cmd_params_pos, "json");
bool hasParam = false;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
if (json) {
ok_msg = "{\"delay\":\"";
} else {
ok_msg = "delay=";
}
ok_msg += String(ESP3DSettings::readUint32(ESP_BOOT_DELAY));
if (json) {
ok_msg += "\",\"verbose\":\"";
} else {
ok_msg += ", verbose=";
}
ok_msg += ESP3DSettings::isVerboseBoot(true) ? "ON" : "OFF";
if (json) {
ok_msg += "\"}";
}
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_param(msg, cmd_params_pos, "delay=");
if (tmpstr.length() != 0) {
hasParam = true;
if (ESP3DSettings::isValidIntegerSetting(tmpstr.toInt(),
ESP_BOOT_DELAY)) {
if (!ESP3DSettings::writeUint32(ESP_BOOT_DELAY, tmpstr.toInt())) {
hasError = true;
error_msg = "Set delay failed";
esp3d_log_e("%s", error_msg.c_str());
}
} else {
hasError = true;
error_msg = "Incorrect delay";
esp3d_log_e("%s", error_msg.c_str());
}
}
if (!hasError) {
tmpstr = get_param(msg, cmd_params_pos, "verbose=");
if (tmpstr.length() != 0) {
hasParam = true;
tmpstr.toUpperCase();
if (tmpstr == "ON" || tmpstr == "OFF" || tmpstr == "1" ||
tmpstr == "0" || tmpstr == "TRUE" || tmpstr == "FALSE" ||
tmpstr == "YES" || tmpstr == "NO") {
uint8_t val = 0;
if (tmpstr == "ON" || tmpstr == "1" || tmpstr == "TRUE" ||
tmpstr == "YES") {
val = 1;
}
if (!ESP3DSettings::writeByte(ESP_VERBOSE_BOOT, val)) {
hasError = true;
error_msg = "Set delay failed";
esp3d_log_e("%s", error_msg.c_str());
}
} else {
hasError = true;
error_msg = "Incorrect verbose setting";
esp3d_log_e("%s", error_msg.c_str());
}
}
}
if (!hasError && !hasParam) {
hasError = true;
error_msg = "Incorrect parameter";
esp3d_log_e("%s", error_msg.c_str());
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}

View File

@ -0,0 +1,90 @@
/*
ESP160.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(WS_DATA_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/websocket/websocket_server.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 160
// Set WebSocket state which can be ON, OFF, CLOSE
//[ESP160]<state> json=<no> pwd=<admin password>
void ESP3DCommands::ESP160(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
bool closeClients = hasTag(msg, cmd_params_pos, "CLOSE");
bool stateON = hasTag(msg, cmd_params_pos, "ON");
bool stateOFF = hasTag(msg, cmd_params_pos, "OFF");
bool has_param = false;
String tmpstr;
#ifdef AUTHENTICATION_FEATURE
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
ESP3DState setting_mode =
(ESP3DState)ESP3DSettings::readByte(ESP_WEBSOCKET_ON);
if (tmpstr.length() == 0) {
if (setting_mode == ESP3DState::off) {
ok_msg = "OFF";
} else {
ok_msg = "ON";
}
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (stateON || stateOFF) {
if (!ESP3DSettings::writeByte(ESP_WEBSOCKET_ON, stateOFF ? 0 : 1)) {
hasError = true;
error_msg = "Set value failed";
}
has_param = true;
}
if (closeClients) {
has_param = true;
websocket_data_server.closeClients();
}
if (!has_param) {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // WS_DATA_FEATURE

View File

@ -0,0 +1,82 @@
/*
ESP161.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(WS_DATA_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 161
// Set Websocket port
//[ESP161]<port> json=<no> pwd=<admin password>
void ESP3DCommands::ESP161(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
uint32_t intValue = 0;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
intValue = ESP3DSettings::readUint32(ESP_WEBSOCKET_PORT);
ok_msg = String(intValue);
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
intValue = atoi(tmpstr.c_str());
esp3d_log(
"got %s param for a value of %ld, is valid %d", tmpstr.c_str(),
intValue,
ESP3DSettings::isValidIntegerSetting(intValue, ESP_WEBSOCKET_PORT));
if (ESP3DSettings::isValidIntegerSetting(intValue, ESP_WEBSOCKET_PORT)) {
esp3d_log("Value %ld is valid", intValue);
if (!ESP3DSettings::writeUint32(ESP_WEBSOCKET_PORT, intValue)) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // WS_DATA_FEATURE

View File

@ -0,0 +1,303 @@
/*
ESP122.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(CAMERA_DEVICE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/camera/camera.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#include "esp_camera.h"
#define COMMAND_ID 170
// Set Camera command value / list all values in JSON/plain
//[ESP170]<plain><label=value> pwd=<admin password>
// label can be:
// light/framesize/quality/contrast/brightness/saturation/gainceiling/colorbar
// /awb/agc/aec/hmirror/vflip/awb_gain/agc_gain/aec_value/aec2/cw/bpc/wpc
// /raw_gma/lenc/special_effect/wb_mode/ae_level
void ESP3DCommands::ESP170(int cmd_params_pos, ESP3DMessage* msg) {
const char* camcmd = [] = {
"framesize",
"quality",
"contrast",
"brightness",
"saturation",
"sharpness",
"special_effect",
"wb_mode",
"awb",
"awb_gain",
"aec",
"aec2",
"ae_level",
"aec_value",
"agc",
"agc_gain",
"gainceiling",
"bpc",
"wpc",
"raw_gma",
"lenc",
"vflip",
"hmirror",
"dcw",
"colorbar",
#if CAM_LED_PIN != -1
"light",
#endif // CAM_LED_PIN
};
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if AUTHENTICATION_FEATURE
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (!esp3d_camera.started()) {
hasError = true;
error_msg = "No camera initialized";
esp3d_log_e("%s", error_msg.c_str());
} else {
tmpstr = get_clean_param(msg, cmd_params_pos);
sensor_t* s = esp_camera_sensor_get();
if (s == nullptr) {
hasError = true;
error_msg = "No camera settings available";
esp3d_log_e("%s", error_msg.c_str());
} else {
if (tmpstr.length() == 0) {
if (json) {
tmpstr = "{\"cmd\":\"170\",\"status\":\"ok\",\"data\":[";
} else {
tmpstr = "Camera:\n";
}
msg->type = ESP3DMessageType::head;
if (!dispatch(msg, tmpstr.c_str())) {
esp3d_log_e("Error sending response to clients");
return;
}
// now send all settings one by one
// framesize
if (!dispatchIdValue(json, "framesize",
String(status.framesize).c_str(), target,
requestId, true)) {
return;
}
// quality
if (!dispatchIdValue(json, "quality", String(status.quality).c_str(),
target, requestId)) {
return;
}
// brightness
if (!dispatchIdValue(json, "brightness",
String(status.brightness).c_str(), target,
requestId)) {
return;
}
// contrast
if (!dispatchIdValue(json, "contrast", String(status.contrast).c_str(),
target, requestId)) {
return;
}
// saturation
if (!dispatchIdValue(json, "saturation",
String(status.saturation).c_str(), target,
requestId)) {
return;
// sharpness
if (!dispatchIdValue(json, "sharpness",
String(status.sharpness).c_str(), target,
requestId)) {
return;
}
// special_effect
if (!dispatchIdValue(json, "special_effect",
String(status.special_effect).c_str(), target,
requestId)) {
return;
}
// wb_mode
if (!dispatchIdValue(json, "wb_mode", String(status.wb_mode).c_str(),
target, requestId)) {
return;
}
// awb
if (!dispatchIdValue(json, "awb", String(status.awb).c_str(), target,
requestId)) {
return;
}
// awb_gain
if (!dispatchIdValue(json, "awb_gain",
String(status.awb_gain).c_str(), target,
requestId)) {
return;
}
// aec
if (!dispatchIdValue(json, "aec", String(status.aec).c_str(), target,
requestId)) {
return;
}
// aec2
if (!dispatchIdValue(json, "aec2", String(status.aec2).c_str(),
target, requestId)) {
return;
}
// ae_level
if (!dispatchIdValue(json, "ae_level",
String(status.ae_level).c_str(), target,
requestId)) {
return;
}
// aec_value
if (!dispatchIdValue(json, "aec_value",
String(status.aec_value).c_str(), target,
requestId)) {
return;
}
// agc
if (!dispatchIdValue(json, "agc", String(status.agc).c_str(), target,
requestId)) {
return;
}
// agc_gain
if (!dispatchIdValue(json, "agc_gain",
String(status.agc_gain).c_str(), target,
requestId)) {
return;
}
// gainceiling
if (!dispatchIdValue(json, "gainceiling",
String(status.gainceiling).c_str(), target,
requestId)) {
return;
}
// bpc
if (!dispatchIdValue(json, "bpc", String(status.bpc).c_str(), target,
requestId)) {
return;
}
// wpc
if (!dispatchIdValue(json, "wpc", String(status.wpc).c_str(), target,
requestId)) {
return;
}
// raw_gma
if (!dispatchIdValue(json, "raw_gma", String(status.raw_gma).c_str(),
target, requestId)) {
return;
}
// lenc
if (!dispatchIdValue(json, "lenc", String(status.lenc).c_str(),
target, requestId)) {
return;
}
// vflip
if (!dispatchIdValue(json, "vflip", String(status.vflip).c_str(),
target, requestId)) {
return;
}
// hmirror
if (!dispatchIdValue(json, "hmirror", String(status.hmirror).c_str(),
target, requestId)) {
return;
}
// dcw
if (!dispatchIdValue(json, "dcw", String(status.dcw).c_str(), target,
requestId)) {
return;
}
// colorbar
if (!dispatchIdValue(json, "colorbar",
String(status.colorbar).c_str(), target,
requestId)) {
return;
}
#if CAM_LED_PIN != -1
// light
if (!dispatchIdValue(
json, "light",
String(digitalRead(CAM_LED_PIN) == HIGH ? 1 : 0).c_str(),
target, requestId)) {
return;
}
#endif // CAM_LED_PIN
}
if (json) {
if (!dispatch("]}", target, requestId, ESP3DMessageType::tail)) {
esp3d_log_e("Error sending answer to clients");
}
} else {
{
tmpstr = "ok\n";
if (!dispatch(tmpstr.c_str(), target, requestId,
ESP3DMessageType::tail)) {
esp3d_log_e("Error sending answer to clients");
}
}
}
return;
} else {
size_t s = sizeof(camcmd) / sizeof(const char*);
for (size_t i = 0; i < s; i++) {
String label = camcmd[i];
label += "=";
tmpstr = get_param(msg, cmd_params_pos, label.c_str());
if (tmpstr.length() > 0) {
int r = esp3d_camera.command(camcmd[i], tmpstr.c_str());
if (r == -1) {
hasError = true;
error_msg = "Unknow command";
esp3d_log_e("%s", error_msg.c_str());
} else if (r == 1) {
hasError = true;
error_msg = "Invalid value";
esp3d_log_e("%s", error_msg.c_str());
} else {
ok_msg = "ok";
}
}
}
}
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // CAMERA_DEVICE

View File

@ -0,0 +1,108 @@
/*
ESP122.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(CAMERA_DEVICE)
#include <time.h>
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/camera/camera.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#include "esp_camera.h"
#define COMMAND_ID 171
// Save frame to target path and filename (default target = today date, default
// name=timestamp.jpg)
//[ESP171]path=<target path> filename=<target filename> pwd=<admin/user
// password>
void ESP3DCommands::ESP171(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok Snapshot taken";
String path;
String filename;
String tmpstr;
bool json = hasTag(msg, cmd_params_pos, "json");
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (!esp3d_camera.started()) {
hasError = true;
error_msg = "No camera initialized";
esp3d_log_e("%s", error_msg.c_str());
} else {
tmpstr = get_param(msg, cmd_params_pos, "path=");
if (tmpstr.length() != 0) {
// get path
path = tmpstr;
}
tmpstr = get_param(msg, cmd_params_pos, "filename=");
if (tmpstr.length() != 0) {
// get filename
filename = tmpstr;
}
// if nothing provided, use default filename / path
if (path.length() == 0) {
struct tm tmstruct;
time_t now;
path = "";
time(&now);
localtime_r(&now, &tmstruct);
path = String((tmstruct.tm_year) + 1900) + "-";
if (((tmstruct.tm_mon) + 1) < 10) {
path += "0";
}
path += String((tmstruct.tm_mon) + 1) + "-";
if (tmstruct.tm_mday < 10) {
path += "0";
}
path += String(tmstruct.tm_mday);
}
if (filename.length() == 0) {
struct tm tmstruct;
time_t now;
time(&now);
localtime_r(&now, &tmstruct);
filename = String(now) + ".jpg";
}
if (!esp3d_camera.handle_snap(nullptr, path.c_str(), filename.c_str())) {
hasError = true;
error_msg = "Error taking snapshot";
esp3d_log_e("%s", error_msg.c_str());
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // CAMERA_DEVICE

View File

@ -0,0 +1,89 @@
/*
ESP130.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(FTP_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/ftp/FtpServer.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 180
// Set ftp state which can be ON, OFF, CLOSE
//[ESP180]<state> json=<no> pwd=<admin password>
void ESP3DCommands::ESP180(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
bool closeClients = hasTag(msg, cmd_params_pos, "CLOSE");
bool stateON = hasTag(msg, cmd_params_pos, "ON");
bool stateOFF = hasTag(msg, cmd_params_pos, "OFF");
bool has_param = false;
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
ESP3DState setting_mode = (ESP3DState)ESP3DSettings::readByte(ESP_FTP_ON);
if (tmpstr.length() == 0) {
if (setting_mode == ESP3DState::off) {
ok_msg = "OFF";
} else {
ok_msg = "ON";
}
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (stateON || stateOFF) {
if (!ESP3DSettings::writeByte(ESP_FTP_ON, stateOFF ? 0 : 1)) {
hasError = true;
error_msg = "Set value failed";
}
has_param = true;
}
if (closeClients) {
has_param = true;
ftp_server.closeClient();
}
if (!has_param) {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // FTP_FEATURE

View File

@ -0,0 +1,151 @@
/*
ESP181.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(FTP_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 181
// Set/Get Ftp ports
//[ESP181]ctrl=<port> active=<port> passive=<port> json=<no> pwd=<admin
// password>
void ESP3DCommands::ESP181(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
bool has_param = false;
String tmpstr;
uint32_t intValue = 0;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
if (json) {
ok_msg = "{\"ctrl\":\"";
} else {
ok_msg = "ctrl=";
}
intValue = ESP3DSettings::readUint32(ESP_FTP_CTRL_PORT);
ok_msg += String(intValue);
if (json) {
ok_msg += "\",\"active\":\"";
} else {
ok_msg += ", active=";
}
intValue = ESP3DSettings::readUint32(ESP_FTP_DATA_ACTIVE_PORT);
ok_msg += String(intValue);
if (json) {
ok_msg += "\",\"passive\":\"";
} else {
ok_msg += ", passive=";
}
intValue = ESP3DSettings::readUint32(ESP_FTP_DATA_PASSIVE_PORT);
ok_msg += String(intValue);
if (json) {
ok_msg += "\"}";
}
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_param(msg, cmd_params_pos, "ctrl=");
if (tmpstr.length() > 0) {
has_param = true;
intValue = atoi(tmpstr.c_str());
if (ESP3DSettings::isValidIntegerSetting(intValue, ESP_FTP_CTRL_PORT)) {
esp3d_log("Value %ld is valid", intValue);
if (!ESP3DSettings::writeUint32(ESP_FTP_CTRL_PORT, intValue)) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter ctrl";
}
}
if (!hasError) {
tmpstr = get_param(msg, cmd_params_pos, "active=");
if (tmpstr.length() > 0) {
has_param = true;
intValue = atoi(tmpstr.c_str());
if (ESP3DSettings::isValidIntegerSetting(intValue,
ESP_FTP_DATA_ACTIVE_PORT)) {
esp3d_log("Value %ld is valid", intValue);
if (!ESP3DSettings::writeUint32(ESP_FTP_DATA_ACTIVE_PORT, intValue)) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter active";
}
}
}
if (!hasError) {
tmpstr = get_param(msg, cmd_params_pos, "passive=");
if (tmpstr.length() > 0) {
has_param = true;
intValue = atoi(tmpstr.c_str());
if (ESP3DSettings::isValidIntegerSetting(intValue,
ESP_FTP_DATA_PASSIVE_PORT)) {
esp3d_log("Value %ld is valid", intValue);
if (!ESP3DSettings::writeUint32(ESP_FTP_DATA_PASSIVE_PORT,
intValue)) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter passive";
}
}
}
if (!hasError && !has_param) {
hasError = true;
error_msg = "Invalid parameter";
esp3d_log_e("%s", error_msg.c_str());
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // TELNET_FEATURE

View File

@ -0,0 +1,89 @@
/*
ESP190.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(WEBDAV_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/webdav/webdav_server.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 190
// Set WebDav state which can be ON, OFF, CLOSE
//[ESP190]<state> json=<no> pwd=<admin password>
void ESP3DCommands::ESP190(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
bool closeClients = hasTag(msg, cmd_params_pos, "CLOSE");
bool stateON = hasTag(msg, cmd_params_pos, "ON");
bool stateOFF = hasTag(msg, cmd_params_pos, "OFF");
bool has_param = false;
String tmpstr;
#ifdef AUTHENTICATION_FEATURE
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
ESP3DState setting_mode = (ESP3DState)ESP3DSettings::readByte(ESP_WEBDAV_ON);
if (tmpstr.length() == 0) {
if (setting_mode == ESP3DState::off) {
ok_msg = "OFF";
} else {
ok_msg = "ON";
}
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (stateON || stateOFF) {
if (!ESP3DSettings::writeByte(ESP_WEBDAV_ON, stateOFF ? 0 : 1)) {
hasError = true;
error_msg = "Set value failed";
}
has_param = true;
}
if (closeClients) {
has_param = true;
webdav_server.closeClient();
}
if (!has_param) {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // WEBDAV_FEATURE

View File

@ -0,0 +1,81 @@
/*
ESP191.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(WEBDAV_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 191
// Set webdav port
//[ESP191]<port> json=<no> pwd=<admin password>
void ESP3DCommands::ESP191(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
uint32_t intValue = 0;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
intValue = ESP3DSettings::readUint32(ESP_WEBDAV_PORT);
ok_msg = String(intValue);
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
intValue = atoi(tmpstr.c_str());
esp3d_log("got %s param for a value of %ld, is valid %d", tmpstr.c_str(),
intValue,
ESP3DSettings::isValidIntegerSetting(intValue, ESP_WEBDAV_PORT));
if (ESP3DSettings::isValidIntegerSetting(intValue, ESP_WEBDAV_PORT)) {
esp3d_log("Value %ld is valid", intValue);
if (!ESP3DSettings::writeUint32(ESP_WEBDAV_PORT, intValue)) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // WEBDAV_FEATURE

View File

@ -0,0 +1,78 @@
/*
ESP200.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(SD_DEVICE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/filesystem/esp_sd.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 200
// Get SD Card Status
//[ESP200] json=<YES/NO> <RELEASESD> <REFRESH> pwd=<user/admin password>
void ESP3DCommands::ESP200(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool releasesd = hasTag(msg, cmd_params_pos, "RELEASESD");
bool refreshsd = hasTag(msg, cmd_params_pos, "REFRESH");
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (releasesd) {
ESP_SD::releaseFS();
ok_msg = "SD card released";
}
if (!ESP_SD::accessFS()) {
if (ESP_SD::getState() == ESP_SDCARD_BUSY) {
ok_msg = "Busy";
} else {
ok_msg = "Not available";
}
} else {
int8_t state = ESP_SD::getState(true);
if (state == ESP_SDCARD_IDLE) {
ok_msg = "SD card ok";
if (refreshsd) {
ESP_SD::refreshStats(true);
}
}
ESP_SD::releaseFS();
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // SD_DEVICE

View File

@ -0,0 +1,138 @@
/*
ESP201.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(DIRECT_PIN_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_hal.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 201
// Get/Set pin value
//[ESP201]P=<pin> V=<value> PULLUP=YES RAW=YES ANALOG=NO
// ANALOG_RANGE=255]pwd=<admin password> Range can be 255 / 1024 / 2047 / 4095 /
// 8191
void ESP3DCommands::ESP201(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
String P = get_param(msg, cmd_params_pos, "P=");
String V = get_param(msg, cmd_params_pos, "V=");
bool isPULLUP = hasTag(msg, cmd_params_pos, "PULLUP");
bool isRAW = hasTag(msg, cmd_params_pos, "RAW");
bool isANALOG = hasTag(msg, cmd_params_pos, "ANALOG");
int analog_range = 255;
String ANALOG_RANGE = get_param(msg, cmd_params_pos, "ANALOG_RANGE=");
if (ANALOG_RANGE.length() > 0) {
analog_range = ANALOG_RANGE.toInt();
}
bool found = false;
get_param(msg, cmd_params_pos, "PULLUP=", &found);
if (!found && !isPULLUP) {
esp3d_log("Missing PULLUP parameter");
isPULLUP = true; // default value
}
found = false;
int value = 0;
get_param(msg, cmd_params_pos, "RAW=", &found);
if (!found && !isRAW) {
esp3d_log("Missing RAW parameter");
isRAW = true; // default value
}
found = false;
get_param(msg, cmd_params_pos, "ANALOG=", &found);
if (!found && !isANALOG) {
esp3d_log("Missing ANALOG parameter");
isANALOG = false; // default value
}
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (P.length() == 0) {
hasError = true;
error_msg = "Missing pin";
esp3d_log_e("%s", error_msg.c_str());
} else {
int pin = P.toInt();
esp3d_log("P=%d V=%s RAW=%d PULLUP=%d ANALOG=%d RANGE=%d", pin, V.c_str(),
isRAW, isPULLUP, isANALOG, analog_range);
if (!ESP3DHal::is_pin_usable(pin)) {
hasError = true;
error_msg = "Invalid pin";
esp3d_log_e("%s", error_msg.c_str());
} else {
if (V.length() > 0) { // Set
value = V.toInt();
ESP3DHal::pinMode(pin, OUTPUT);
if (isANALOG) {
if ((value >= 0) || (value <= analog_range + 1)) {
ESP3DHal::analogRange(analog_range);
ESP3DHal::analogWriteFreq(1000);
analogWrite(pin, value);
} else {
hasError = true;
error_msg = "Invalid value";
esp3d_log_e("%s", error_msg.c_str());
}
} else {
if ((value == 0) || (value == 1)) {
digitalWrite(pin, (value == 0) ? LOW : HIGH);
} else {
hasError = true;
error_msg = "Invalid value";
esp3d_log_e("%s", error_msg.c_str());
}
}
} else { // Get
if (isANALOG) {
value = ESP3DHal::analogRead(pin);
} else {
if (!isRAW) {
if (isPULLUP) {
ESP3DHal::pinMode(pin, INPUT_PULLUP);
} else {
ESP3DHal::pinMode(pin, INPUT);
}
}
value = digitalRead(pin);
ok_msg = String(value);
}
}
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // DIRECT_PIN_FEATURE

View File

@ -0,0 +1,85 @@
/*
ESP202.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(SD_DEVICE) && SD_DEVICE != ESP_SDIO
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/filesystem/esp_sd.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 202
// Get/Set SD card Speed factor 1 2 4 6 8 16 32
//[ESP202]SPEED=<value> json=<no> pwd=<user/admin password>
void ESP3DCommands::ESP202(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
String speed = get_param(msg, cmd_params_pos, "SPEED=");
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (speed.length() == 0) { // Get
esp3d_log("Reading SD speed divider");
uint8_t s = ESP3DSettings::readByte(ESP_SD_SPEED_DIV);
esp3d_log("SD speed divider is %d", s);
ok_msg = String(s);
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
// Set
esp3d_log("Setting SD speed divider");
uint8_t s = speed.toInt();
if (ESP3DSettings::isValidByteSetting(s, ESP_SD_SPEED_DIV)) {
if (!ESP3DSettings::writeByte(ESP_SD_SPEED_DIV, s)) {
hasError = true;
error_msg = "Set failed";
esp3d_log_e("Error setting SD speed divider");
} else {
ESP_SD::setSPISpeedDivider(s);
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // SD_DEVICE

View File

@ -0,0 +1,134 @@
/*
ESP210.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(SENSOR_DEVICE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/sensor/sensor.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 210
// Get Sensor Value / type/Set Sensor type
//[ESP210]<type=NONE/xxx> <interval=XXX in millisec> json=<no> pwd=<admin
// password>
void ESP3DCommands::ESP210(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
String stype = get_param(msg, cmd_params_pos, "type=");
String sint = get_param(msg, cmd_params_pos, "interval=");
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (stype.length() == 0 && sint.length() == 0) { // Get
String s;
if (json) {
s = "{\"type\":\"";
} else {
s = "type=";
}
if (esp3d_sensor.started()) {
s += esp3d_sensor.GetCurrentModelString();
} else {
s += "NONE";
}
if (json) {
s += ":\",\"interval\":";
} else {
s += ", interval=";
}
s += esp3d_sensor.interval();
if (json) {
s += ":\",\"value\":";
} else {
s += "ms, value=";
}
s += esp3d_sensor.GetData();
if (json) {
s += "}";
}
ok_msg = s;
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
// Set
if (stype.length() != 0) {
stype.toUpperCase();
int8_t v = -1;
if (stype == "NONE") {
v = 0;
} else if (esp3d_sensor.isModelValid(
esp3d_sensor.getIDFromString(stype.c_str()))) {
v = esp3d_sensor.getIDFromString(stype.c_str());
} else {
hasError = true;
error_msg = "Invalid parameter";
esp3d_log_e("%s", error_msg.c_str());
}
if (v != -1) {
if (!ESP3DSettings::writeByte(ESP_SENSOR_TYPE, v)) {
hasError = true;
error_msg = "Set failed";
esp3d_log_e("%s", error_msg.c_str());
}
if (!esp3d_sensor.begin()) {
hasError = true;
error_msg = "Starting failed";
esp3d_log_e("%s", error_msg.c_str());
}
} else {
hasError = true;
error_msg = "Invalid type";
esp3d_log_e("%s", error_msg.c_str());
}
}
if (!hasError && sint.length() != 0 &&
ESP3DSettings::isValidIntegerSetting(sint.toInt(),
ESP_SENSOR_INTERVAL)) {
if (!ESP3DSettings::writeUint32(ESP_SENSOR_INTERVAL, sint.toInt())) {
hasError = true;
error_msg = "Set failed";
esp3d_log_e("%s", error_msg.c_str());
} else {
esp3d_sensor.setInterval(sint.toInt());
}
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // SENSOR_DEVICE

View File

@ -21,46 +21,36 @@
#if defined(DISPLAY_DEVICE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/display/display.h"
#include "../commands.h"
#include "../esp3doutput.h"
#include "../settings_esp3d.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMANDID 214
#define COMMAND_ID 214
// Output to esp screen status
//[ESP214]<Text>json=<no> pwd=<user/admin password>
bool Commands::ESP214(const char* cmd_params, level_authenticate_type auth_type,
ESP3DOutput* output) {
bool noError = true;
bool json = has_tag(cmd_params, "json");
String response;
String parameter;
int errorCode = 200; // unless it is a server error use 200 as default and
// set error in json instead
#ifdef AUTHENTICATION_FEATURE
if (auth_type == LEVEL_GUEST) {
response = format_response(COMMANDID, json, false,
"Guest user can't use this command");
noError = false;
errorCode = 401;
void ESP3DCommands::ESP214(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#else
(void)auth_type;
#endif // AUTHENTICATION_FEATURE
if (noError) {
parameter = clean_param(get_param(cmd_params, ""));
esp3d_display.setStatus(parameter.c_str());
response = format_response(COMMANDID, json, true, "ok");
tmpstr = get_clean_param(msg, cmd_params_pos);
esp3d_display.setStatus(tmpstr.c_str());
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
if (json) {
output->printLN(response.c_str());
} else {
if (noError) {
output->printMSG(response.c_str());
} else {
output->printERROR(response.c_str(), errorCode);
}
}
return noError;
}
#endif // DISPLAY_DEVICE

View File

@ -0,0 +1,78 @@
/*
ESP215.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(DISPLAY_DEVICE) && defined(DISPLAY_TOUCH_DRIVER)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/display/display.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 215
// Touch Calibration
//[ESP215]<CALIBRATE> json=<no> [pwd=<user password>]
void ESP3DCommands::ESP215(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
bool calibrate = hasTag(msg, cmd_params_pos, "CALIBRATE");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
const ESP3DSettingDescription* settingPtr =
ESP3DSettings::getSettingPtr(ESP_CALIBRATION);
if (settingPtr) {
ok_msg =
ESP3DSettings::readByte(ESP_CALIBRATION) == 1 ? "Done" : "Not done";
} else {
hasError = true;
error_msg = "This setting is unknown";
esp3d_log_e("%s", error_msg.c_str());
}
} else {
tmpstr.toUpperCase();
if (!calibrate) {
hasError = true;
error_msg = "Missing parameter";
esp3d_log_e("%s", error_msg.c_str());
} else {
ok_msg = "ok - Please follow screen instructions";
esp3d_display.startCalibration();
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // DISPLAY_DEVICE && DISPLAY_TOUCH_DRIVER

View File

@ -0,0 +1,194 @@
/*
ESP220.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#include "../../modules/authentication/authentication_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 220
// Get ESP pins definition
// output is JSON or plain text according parameter
//[ESP220]json=<no>
void ESP3DCommands::ESP220(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
String tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() != 0) {
hasError = true;
error_msg = "This command doesn't take parameters";
esp3d_log_e("%s", error_msg.c_str());
} else {
ok_msg = "";
if (json) {
ok_msg = "{\"cmd\":\"220\",\"status\":\"ok\",\"data\":[";
} else {
ok_msg = "Pins:\n";
}
msg->type = ESP3DMessageType::head;
if (!dispatch(msg, ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
return;
}
bool isFirst = true;
bool hasPin = false;
#if defined(SD_DEVICE) && SD_DEVICE != ESP_SDIO
// SD CS
tmpstr = String(ESP_SD_CS_PIN == -1 ? SS : ESP_SD_CS_PIN);
if (!dispatchIdValue(json, " SD CS", tmpstr.c_str(), target, requestId,
isFirst)) {
return;
}
isFirst = false;
hasPin = true;
// SD MOSI
tmpstr = String(ESP_SD_MOSI_PIN == -1 ? MOSI : ESP_SD_MOSI_PIN);
if (!dispatchIdValue(json, " SD MOSI", tmpstr.c_str(), target, requestId,
isFirst)) {
return;
}
// SD MISO
tmpstr = String(ESP_SD_MISO_PIN == -1 ? MISO : ESP_SD_MISO_PIN);
if (!dispatchIdValue(json, " SD MISO", tmpstr.c_str(), target, requestId,
isFirst)) {
return;
}
// SD SCK
tmpstr = String(ESP_SD_SCK_PIN == -1 ? SCK : ESP_SD_SCK_PIN);
if (!dispatchIdValue(json, " SD SCK", tmpstr.c_str(), target, requestId,
isFirst)) {
return;
}
// SD DETECT
tmpstr = String(ESP_SD_DETECT_PIN);
if (!dispatchIdValue(json, " SD DETECT", tmpstr.c_str(), target, requestId,
isFirst)) {
return;
}
#if ESP_SD_DETECT_PIN != -1
// SD DETECT STATE
tmpstr = String(ESP_SD_DETECT_VALUE);
if (!dispatchIdValue(json, " SD DETECT STATE", tmpstr.c_str(), target,
requestId, isFirst)) {
return;
}
#endif // ESP_SD_DETECT_PIN !=-1
#endif // defined(SD_DEVICE) && SD_DEVICE != ESP_SDIO
#if SD_DEVICE_CONNECTION == ESP_SHARED_SD && defined(ESP_FLAG_SHARED_SD_PIN)
// SD SWITCH
tmpstr = String(ESP_FLAG_SHARED_SD_PIN);
if (!dispatchIdValue(json, " SD SWITCH", tmpstr.c_str(), target, requestId,
isFirst)) {
return;
}
hasPin = true;
#endif // SD_DEVICE_CONNECTION == ESP_SHARED_SD
#ifdef BUZZER_DEVICE
// BUZZER
tmpstr = String(ESP3D_BUZZER_PIN);
if (!dispatchIdValue(json, " BUZZER", tmpstr.c_str(), target, requestId,
isFirst)) {
return;
}
hasPin = true;
#endif // BUZZER_DEVICE
#if defined(PIN_RESET_FEATURE) && defined(ESP3D_RESET_PIN) && \
ESP3D_RESET_PIN != -1
// RESET
tmpstr = String(ESP3D_RESET_PIN);
if (!dispatchIdValue(json, " RESET", tmpstr.c_str(), target, requestId,
isFirst)) {
return;
}
hasPin = true;
#endif // PIN_RESET_FEATURE
#ifdef SENSOR_DEVICE
// SENSOR
tmpstr = String(ESP3D_SENSOR_PIN);
if (!dispatchIdValue(json, " SENSOR", tmpstr.c_str(), target, requestId,
isFirst)) {
return;
}
hasPin = true;
#endif // SENSOR_DEVICE
#ifdef DISPLAY_DEVICE
#if (DISPLAY_DEVICE == OLED_I2C_SSD1306) || \
(DISPLAY_DEVICE == OLED_I2C_SSDSH1106)
// SDA
tmpstr = String(ESP_SDA_PIN);
if (!dispatchIdValue(json, " SDA", tmpstr.c_str(), target, requestId,
isFirst)) {
return;
}
// SCL
tmpstr = String(ESP_SCL_PIN);
if (!dispatchIdValue(json, " SCL", tmpstr.c_str(), target, requestId,
isFirst)) {
return;
}
hasPin = true;
#endif //(DISPLAY_DEVICE == OLED_I2C_SSD1306) || (DISPLAY_DEVICE ==
// OLED_I2C_SSDSH1106)
#endif // DISPLAY_DEVICE
if (!hasPin) {
tmpstr = "NO PIN";
if (!dispatchIdValue(json, " PIN", tmpstr.c_str(), target, requestId,
isFirst)) {
return;
}
}
if (json) {
if (!dispatch("]}", target, requestId, ESP3DMessageType::tail)) {
esp3d_log_e("Error sending answer to clients");
}
} else {
{
tmpstr = "ok\n";
if (!dispatch(tmpstr.c_str(), target, requestId,
ESP3DMessageType::tail)) {
esp3d_log_e("Error sending answer to clients");
}
}
}
return;
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}

View File

@ -0,0 +1,75 @@
/*
ESP250.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(BUZZER_DEVICE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/buzzer/buzzer.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 250
// Play sound
//[ESP250]F=<frequency> D=<duration> json=<no> [pwd=<user password>]
void ESP3DCommands::ESP250(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
String F = get_param(msg, cmd_params_pos, "F=");
String D = get_param(msg, cmd_params_pos, "D=");
int freq = -1;
int duration = 0;
if (F.length() > 0) {
freq = F.toInt();
}
if (D.length() > 0) {
duration = D.toInt();
}
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (freq == -1 && duration == 0) {
esp3d_buzzer.beep();
} else {
if (freq == -1) {
freq = 1000;
}
if (duration != 0) {
esp3d_buzzer.beep(freq, duration);
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // BUZZER_DEVICE

View File

@ -0,0 +1,57 @@
/*
ESP290.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#include "../../modules/authentication/authentication_service.h"
#include "../esp3d_commands.h"
// Delay command
//[ESP290]<delay in ms> json=<no> [pwd=<user password>]
#define COMMAND_ID 290
void ESP3DCommands::ESP290(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
hasError = true;
error_msg = "Missing parameter";
esp3d_log_e("%s", error_msg.c_str());
} else {
ESP3DHal::wait(tmpstr.toInt());
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}

View File

@ -0,0 +1,489 @@
/*
ESP400.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL
#include "../../modules/serial/serial_service.h"
#endif // COMMUNICATION_PROTOCOL != SOCKET_SERIAL
#include "../../modules/authentication/authentication_service.h"
#if defined(SENSOR_DEVICE)
#include "../../modules/sensor/sensor.h"
#endif // SENSOR_DEVICE
#ifdef TIMESTAMP_FEATURE
#include "../../modules/time/time_service.h"
#endif // TIMESTAMP_FEATURE
#define COMMAND_ID 400
const char* YesNoLabels[] = {"no", "yes"};
const char* YesNoValues[] = {"0", "1"};
const char* RadioModeLabels[] = {"none"
#ifdef WIFI_FEATURE
,
"sta",
"ap",
"setup"
#endif // WIFI_FEATURE
#ifdef BLUETOOTH_FEATURE
,
"bt"
#endif // BLUETOOTH_FEATURE
#ifdef ETH_FEATURE
,
"eth-sta"
#endif // ETH_FEATURE
};
const char* RadioModeValues[] = {"0"
#ifdef WIFI_FEATURE
,
"1",
"2",
"5"
#endif // WIFI_FEATURE
#ifdef BLUETOOTH_FEATURE
,
"3"
#endif // BLUETOOTH_FEATURE
#ifdef ETH_FEATURE
,
"4"
#endif // ETH_FEATURE
};
const char* FallbackLabels[] = {"none"
#ifdef WIFI_FEATURE
,
"setup"
#endif // WIFI_FEATURE
#ifdef BLUETOOTH_FEATURE
,
"bt"
#endif // BLUETOOTH_FEATURE
};
const char* FallbackValues[] = {"0"
#ifdef WIFI_FEATURE
,
"5"
#endif // WIFI_FEATURE
#ifdef BLUETOOTH_FEATURE
,
"3"
#endif // BLUETOOTH_FEATURE
};
const char* FirmwareLabels[] = {"Unknown", "Grbl", "Marlin", "Smoothieware",
"Repetier"};
const char* FirmwareValues[] = {"0", "10", "20", "40", "50"};
#ifdef NOTIFICATION_FEATURE
const char* NotificationsLabels[] = {"none", "pushover", "email",
"line", "telegram", "ifttt", "home-assistant"};
const char* NotificationsValues[] = {"0", "1", "2", "3", "4", "5", "6"};
#endif // NOTIFICATION_FEATURE
const char* IpModeLabels[] = {"static", "dhcp"};
const char* IpModeValues[] = {"0", "1"};
const char* SupportedApChannelsStr[] = {"1", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "11", "12", "13", "14"};
const char* SupportedBaudListSizeStr[] = {
"9600", "19200", "38400", "57600", "74880", "115200",
"230400", "250000", "500000", "921600", "1958400"};
#ifdef SENSOR_DEVICE
const char* SensorLabels[] = {"none"
#if SENSOR_DEVICE == DHT11_DEVICE || SENSOR_DEVICE == DHT22_DEVICE
,
"DHT11",
"DHT22"
#endif // SENSOR_DEVICE == DHT11_DEVICE || SENSOR_DEVICE == DHT22_DEVICE
#if SENSOR_DEVICE == ANALOG_DEVICE
,
"Analog"
#endif // SENSOR_DEVICE == ANALOG_DEVICE
#if SENSOR_DEVICE == BMP280_DEVICE || SENSOR_DEVICE == BME280_DEVICE
,
"BMP280",
"BME280"
#endif // SENSOR_DEVICE == BMP280_DEVICE || SENSOR_DEVICE == BME280_DEVICE
};
const char* SensorValues[] = {"0"
#if SENSOR_DEVICE == DHT11_DEVICE || SENSOR_DEVICE == DHT22_DEVICE
,
"1",
"2"
#endif // SENSOR_DEVICE == DHT11_DEVICE || SENSOR_DEVICE == DHT22_DEVICE
#if SENSOR_DEVICE == ANALOG_DEVICE
,
"3"
#endif // SENSOR_DEVICE == ANALOG_DEVICE
#if SENSOR_DEVICE == BMP280_DEVICE || SENSOR_DEVICE == BME280_DEVICE
,
"4",
"5"
#endif // SENSOR_DEVICE == BMP280_DEVICE || SENSOR_DEVICE == BME280_DEVICE
};
#endif // SENSOR_DEVICE
#ifdef SD_DEVICE
#if SD_DEVICE != ESP_SDIO
const char* SupportedSPIDividerStr[] = {"1", "2", "4", "6", "8", "16", "32"};
const uint8_t SupportedSPIDividerStrSize =
sizeof(SupportedSPIDividerStr) / sizeof(char*);
#endif // SD_DEVICE != ESP_SDIO
#endif // SD_DEVICE
// Get full ESP3D settings
//[ESP400]<pwd=admin>
void ESP3DCommands::ESP400(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (json) {
tmpstr = "{\"cmd\":\"400\",\"status\":\"ok\",\"data\":[";
} else {
tmpstr = "Settings:\n";
}
msg->type = ESP3DMessageType::head;
if (!dispatch(msg, tmpstr.c_str())) {
esp3d_log_e("Error sending response to clients");
return;
}
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE) || defined(BT_FEATURE)
// Hostname network/network
dispatchSetting(json, "network/network", ESP_HOSTNAME, "hostname", NULL, NULL,
32, 1, 1, -1, NULL, true, target, requestId, true);
#endif // WIFI_FEATURE || ETH_FEATURE || BT_FEATURE
// radio mode network/network
dispatchSetting(json, "network/network", ESP_RADIO_MODE, "radio mode",
RadioModeValues, RadioModeLabels,
sizeof(RadioModeValues) / sizeof(char*), -1, -1, -1, NULL,
true, target, requestId);
// Radio State at Boot
dispatchSetting(json, "network/network", ESP_BOOT_RADIO_STATE, "radio_boot",
YesNoValues, YesNoLabels, sizeof(YesNoValues) / sizeof(char*),
-1, -1, -1, NULL, true, target, requestId);
#ifdef WIFI_FEATURE
// STA SSID network/sta
dispatchSetting(json, "network/sta", ESP_STA_SSID, "SSID", nullptr, nullptr,
32, 1, 1, -1, nullptr, true, target, requestId);
// STA Password network/sta
dispatchSetting(json, "network/sta", ESP_STA_PASSWORD, "pwd", nullptr,
nullptr, 64, 8, 0, -1, nullptr, true, target, requestId);
#endif // WIFI_FEATURE
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
// STA IP mode
dispatchSetting(json, "network/sta", ESP_STA_IP_MODE, "ip mode", IpModeValues,
IpModeLabels, sizeof(IpModeLabels) / sizeof(char*), -1, -1,
-1, nullptr, true, target, requestId);
// STA static IP
dispatchSetting(json, "network/sta", ESP_STA_IP_VALUE, "ip", nullptr, nullptr,
-1, -1, -1, -1, nullptr, true, target, requestId);
// STA static Gateway
dispatchSetting(json, "network/sta", ESP_STA_GATEWAY_VALUE, "gw", nullptr,
nullptr, -1, -1, -1, -1, nullptr, true, target, requestId);
// STA static Mask
dispatchSetting(json, "network/sta", ESP_STA_MASK_VALUE, "msk", nullptr,
nullptr, -1, -1, -1, -1, nullptr, true, target, requestId);
// STA static DNS
dispatchSetting(json, "network/sta", ESP_STA_DNS_VALUE, "DNS", nullptr,
nullptr, -1, -1, -1, -1, nullptr, true, target, requestId);
#endif // WIFI_FEATURE || ETH_FEATURE
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE) || defined(BT_FEATURE)
// Sta fallback mode
dispatchSetting(json, "network/sta", ESP_STA_FALLBACK_MODE,
"sta fallback mode", FallbackValues, FallbackLabels,
sizeof(FallbackValues) / sizeof(char*), -1, -1, -1, nullptr,
true, target, requestId);
#endif // WIFI_FEATURE || ETH_FEATURE || BT_FEATURE
#if defined(WIFI_FEATURE)
// AP SSID network/ap
dispatchSetting(json, "network/ap", ESP_AP_SSID, "SSID", nullptr, nullptr, 32,
1, 1, -1, nullptr, true, target, requestId);
// AP password
dispatchSetting(json, "network/ap", ESP_AP_PASSWORD, "pwd", nullptr, nullptr,
64, 8, 0, -1, nullptr, true, target, requestId);
// AP static IP
dispatchSetting(json, "network/ap", ESP_AP_IP_VALUE, "ip", nullptr, nullptr,
-1, -1, -1, -1, nullptr, true, target, requestId);
// AP Channel
dispatchSetting(json, "network/ap", ESP_AP_CHANNEL, "channel",
SupportedApChannelsStr, SupportedApChannelsStr,
sizeof(SupportedApChannelsStr) / sizeof(char*), -1, -1, -1,
nullptr, true, target, requestId);
#endif // WIFI_FEATURE
#ifdef AUTHENTICATION_FEATURE
// Admin password
dispatchSetting(json, "security/security", ESP_ADMIN_PWD, "adm pwd", nullptr,
nullptr, 20, 0, -1, -1, nullptr, true, target, requestId);
// User password
dispatchSetting(json, "security/security", ESP_USER_PWD, "user pwd", nullptr,
nullptr, 20, 0, -1, -1, nullptr, true, target, requestId);
// session timeout
dispatchSetting(json, "security/security", ESP_SESSION_TIMEOUT,
"session timeout", nullptr, nullptr, 255, 0, -1, -1, nullptr,
true, target, requestId);
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
// Secure Serial
dispatchSetting(json, "security/security", ESP_SECURE_SERIAL, "serial",
YesNoValues, YesNoLabels, sizeof(YesNoValues) / sizeof(char*),
-1, -1, -1, nullptr, true, target, requestId);
#endif // COMMUNICATION_PROTOCOL
#endif // AUTHENTICATION_FEATURE
#ifdef HTTP_FEATURE
// HTTP On service/http
dispatchSetting(json, "service/http", ESP_HTTP_ON, "enable", YesNoValues,
YesNoLabels, sizeof(YesNoValues) / sizeof(char*), -1, -1, -1,
nullptr, true, target, requestId);
// HTTP port
dispatchSetting(json, "service/http", ESP_HTTP_PORT, "port", nullptr, nullptr,
65535, 1, -1, -1, nullptr, true, target, requestId);
#endif // HTTP_FEATURE
#ifdef TELNET_FEATURE
// TELNET On service/telnet
dispatchSetting(json, "service/telnetp", ESP_TELNET_ON, "enable", YesNoValues,
YesNoLabels, sizeof(YesNoValues) / sizeof(char*), -1, -1, -1,
nullptr, true, target, requestId);
// TELNET Port
dispatchSetting(json, "service/telnetp", ESP_TELNET_PORT, "port", nullptr,
nullptr, 65535, 1, -1, -1, nullptr, true, target, requestId);
#endif // TELNET_FEATURE
#ifdef WS_DATA_FEATURE
// Websocket On service
dispatchSetting(json, "service/websocketp", ESP_WEBSOCKET_ON, "enable",
YesNoValues, YesNoLabels, sizeof(YesNoValues) / sizeof(char*),
-1, -1, -1, nullptr, true, target, requestId);
// Websocket Port
dispatchSetting(json, "service/websocketp", ESP_WEBSOCKET_PORT, "port",
nullptr, nullptr, 65535, 1, -1, -1, nullptr, true, target,
requestId);
#endif // WS_DATA_FEATURE
#ifdef WEBDAV_FEATURE
// WebDav On service
dispatchSetting(json, "service/webdavp", ESP_WEBDAV_ON, "enable", YesNoValues,
YesNoLabels, sizeof(YesNoValues) / sizeof(char*), -1, -1, -1,
nullptr, true, target, requestId);
// WebDav Port
dispatchSetting(json, "service/webdavp", ESP_WEBDAV_PORT, "port", nullptr,
nullptr, 65535, 1, -1, -1, nullptr, true, target, requestId);
#endif // WEBDAV_FEATURE
#ifdef FTP_FEATURE
// FTP On service/ftp
dispatchSetting(json, "service/ftp", ESP_FTP_ON, "enable", YesNoValues,
YesNoLabels, sizeof(YesNoValues) / sizeof(char*), -1, -1, -1,
nullptr, true, target, requestId);
// FTP Ports
// CTRL Port
dispatchSetting(json, "service/ftp", ESP_FTP_CTRL_PORT, "control port",
nullptr, nullptr, 65535, 1, -1, -1, nullptr, true, target,
requestId);
// Active Port
dispatchSetting(json, "service/ftp", ESP_FTP_DATA_ACTIVE_PORT, "active port",
nullptr, nullptr, 65535, 1, -1, -1, nullptr, true, target,
requestId);
// Passive Port
dispatchSetting(json, "service/ftp", ESP_FTP_DATA_PASSIVE_PORT,
"passive port", nullptr, nullptr, 65535, 1, -1, -1, nullptr,
true, target, requestId);
#endif // FTP_FEATURE
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
// Serial bridge On service
dispatchSetting(json, "service/serial_bridge", ESP_SERIAL_BRIDGE_ON, "enable",
YesNoValues, YesNoLabels, sizeof(YesNoValues) / sizeof(char*),
-1, -1, -1, nullptr, true, target, requestId);
// Baud Rate
dispatchSetting(json, "service/serial_bridge", ESP_SERIAL_BRIDGE_BAUD, "baud",
SupportedBaudListSizeStr, SupportedBaudListSizeStr,
sizeof(SupportedBaudListSizeStr) / sizeof(char*), -1, -1, -1,
nullptr, true, target, requestId);
#endif // ESP_SERIAL_BRIDGE_OUTPUT
#ifdef TIMESTAMP_FEATURE
// Internet Time
dispatchSetting(json, "service/time", ESP_INTERNET_TIME, "i-time",
YesNoValues, YesNoLabels, sizeof(YesNoValues) / sizeof(char*),
-1, -1, -1, nullptr, true, target, requestId);
// Time zone
dispatchSetting(json, "service/time", ESP_TIME_ZONE, "tzone",
SupportedTimeZones, SupportedTimeZones,
SupportedTimeZonesSize, -1, -1, -1, nullptr, true, target,
requestId);
// Time Server1
dispatchSetting(json, "service/time", ESP_TIME_SERVER1, "t-server", nullptr,
nullptr, 127, 0, -1, -1, nullptr, true, target, requestId);
// Time Server2
dispatchSetting(json, "service/time", ESP_TIME_SERVER2, "t-server", nullptr,
nullptr, 127, 0, -1, -1, nullptr, true, target, requestId);
// Time Server3
dispatchSetting(json, "service/time", ESP_TIME_SERVER3, "t-server", nullptr,
nullptr, 127, 0, -1, -1, nullptr, true, target, requestId);
#endif // TIMESTAMP_FEATURE
#ifdef NOTIFICATION_FEATURE
// Auto notification
dispatchSetting(json, "service/notification", ESP_AUTO_NOTIFICATION,
"auto notif", YesNoValues, YesNoLabels,
sizeof(YesNoValues) / sizeof(char*), -1, -1, -1, nullptr,
true, target, requestId);
// Notification type
dispatchSetting(json, "service/notification", ESP_NOTIFICATION_TYPE,
"notification", NotificationsValues, NotificationsLabels,
sizeof(NotificationsValues) / sizeof(char*), -1, -1, -1,
nullptr, true, target, requestId);
// Token 1
dispatchSetting(json, "service/notification", ESP_NOTIFICATION_TOKEN1, "t1",
nullptr, nullptr, 250, 0, -1, -1, nullptr, true, target,
requestId);
// Token 2
dispatchSetting(json, "service/notification", ESP_NOTIFICATION_TOKEN2, "t2",
nullptr, nullptr, 63, 0, -1, -1, nullptr, true, target,
requestId);
// Notifications Settings
dispatchSetting(json, "service/notification", ESP_NOTIFICATION_SETTINGS, "ts",
nullptr, nullptr, 128, 0, -1, -1, nullptr, true, target,
requestId);
#endif // NOTIFICATION_FEATURE
#ifdef BUZZER_DEVICE
// Buzzer state
dispatchSetting(json, "device/device", ESP_BUZZER, "buzzer", YesNoValues,
YesNoLabels, sizeof(YesNoValues) / sizeof(char*), -1, -1, -1,
nullptr, true, target, requestId);
#endif // BUZZER_DEVICE
#ifdef SENSOR_DEVICE
// Sensor type
dispatchSetting(json, "device/sensor", ESP_SENSOR_TYPE, "type", SensorValues,
SensorLabels, sizeof(SensorValues) / sizeof(char*), -1, -1,
-1, nullptr, true, target, requestId);
// Sensor interval
dispatchSetting(json, "device/sensor", ESP_SENSOR_INTERVAL, "intervalms",
nullptr, nullptr, 60000, 0, -1, -1, nullptr, true, target,
requestId);
#endif // SENSOR_DEVICE
#if defined(SD_DEVICE)
#if SD_DEVICE != ESP_SDIO
// SPI SD Divider
dispatchSetting(json, "device/sd", ESP_SD_SPEED_DIV, "speedx",
SupportedSPIDividerStr, SupportedSPIDividerStr,
SupportedSPIDividerStrSize, -1, -1, -1, nullptr, true, target,
requestId);
#endif // SD_DEVICE != ESP_SDIO
#ifdef SD_UPDATE_FEATURE
// SD CHECK UPDATE AT BOOT feature
dispatchSetting(json, "device/sd", ESP_SD_CHECK_UPDATE_AT_BOOT, "SD updater",
YesNoValues, YesNoLabels, sizeof(YesNoValues) / sizeof(char*),
-1, -1, -1, nullptr, true, target, requestId);
#endif // SD_UPDATE_FEATURE
#endif // SD_DEVICE
#if !defined(FIXED_FW_TARGET)
// Target FW
dispatchSetting(json, "system/system", ESP_TARGET_FW, "targetfw",
FirmwareValues, FirmwareLabels,
sizeof(FirmwareValues) / sizeof(char*), -1, -1, -1, nullptr,
true, target, requestId);
#endif // FIXED_FW_TARGET
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
// Baud Rate
dispatchSetting(json, "system/system", ESP_BAUD_RATE, "baud",
SupportedBaudListSizeStr, SupportedBaudListSizeStr,
sizeof(SupportedBaudListSizeStr) / sizeof(char*), -1, -1, -1,
nullptr, true, target, requestId);
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
// Start delay
dispatchSetting(json, "system/boot", ESP_BOOT_DELAY, "bootdelay", nullptr,
nullptr, 40000, 0, -1, -1, nullptr, true, target, requestId);
// Verbose boot
dispatchSetting(json, "system/boot", ESP_VERBOSE_BOOT, "verbose", YesNoValues,
YesNoLabels, sizeof(YesNoValues) / sizeof(char*), -1, -1, -1,
nullptr, true, target, requestId);
if (json) {
if (!dispatch("]}", target, requestId, ESP3DMessageType::tail)) {
esp3d_log_e("Error sending response to clients");
}
} else {
if (!dispatch("ok\n", target, requestId, ESP3DMessageType::tail)) {
esp3d_log_e("Error sending response to clients");
}
}
}

View File

@ -0,0 +1,255 @@
/*
ESP401.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#include "../../modules/authentication/authentication_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 401
#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL || defined(ESP_SERIAL_BRIDGE_OUTPUT)
#include "../../modules/serial/serial_service.h"
#endif // COMMUNICATION_PROTOCOL != SOCKET_SERIAL
#ifdef SENSOR_DEVICE
#include "../../modules/sensor/sensor.h"
#endif // SENSOR_DEVICE
#ifdef BUZZER_DEVICE
#include "../../modules/buzzer/buzzer.h"
#endif // BUZZER_DEVICE
#ifdef TIMESTAMP_FEATURE
#include "../../modules/time/time_service.h"
#endif // TIMESTAMP_FEATURE
#ifdef NOTIFICATION_FEATURE
#include "../../modules/notifications/notifications_service.h"
#endif // NOTIFICATION_FEATURE
#ifdef SD_DEVICE
#include "../../modules/filesystem/esp_sd.h"
#endif // SD_DEVICE
// Set EEPROM setting
//[ESP401]P=<position> T=<type> V=<value> json=<no> pwd=<user/admin password>
void ESP3DCommands::ESP401(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
String spos = get_param(msg, cmd_params_pos, "P=");
bool foundV = false;
String sval = get_param(msg, cmd_params_pos, "V=", &foundV);
String styp = get_param(msg, cmd_params_pos, "T=");
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (spos.length() == 0) {
error_msg = "Invalid parameter P";
hasError = true;
} else if (styp.length() == 0) {
error_msg = "Invalid parameter T";
hasError = true;
} else if (sval.length() == 0 && !foundV) {
error_msg = "Invalid parameter V";
hasError = true;
} else {
if (!(styp == "B" || styp == "S" || styp == "A" || styp == "I")) {
error_msg = "Invalid value for T";
hasError = true;
}
}
if (!hasError) {
switch (styp[0]) {
case 'B': // Byte value
if (ESP3DSettings::isValidByteSetting((uint8_t)sval.toInt(),
spos.toInt())) {
if (!ESP3DSettings::writeByte(spos.toInt(), (uint8_t)sval.toInt())) {
error_msg = "Set failed";
hasError = true;
esp3d_log_e("Set failed");
}
} else {
error_msg = "Invalid value for T";
hasError = true;
esp3d_log_e("Invalid value for T");
}
break;
case 'I': // Integer value
if (ESP3DSettings::isValidIntegerSetting(sval.toInt(), spos.toInt())) {
if (!ESP3DSettings::writeUint32(spos.toInt(), sval.toInt())) {
error_msg = "Set failed";
hasError = true;
esp3d_log_e("Set failed");
}
} else {
error_msg = "Invalid value for T";
hasError = true;
esp3d_log_e("Invalid value for T");
}
break;
case 'S': // String value
if (ESP3DSettings::isValidStringSetting(sval.c_str(), spos.toInt())) {
if (!ESP3DSettings::writeString(spos.toInt(), sval.c_str())) {
error_msg = "Set failed";
hasError = true;
esp3d_log_e("Set failed");
}
} else {
error_msg = "Invalid value for T";
hasError = true;
esp3d_log_e("Invalid value for T");
}
break;
case 'A': // IP address
if (ESP3DSettings::isValidIPStringSetting(sval.c_str(), spos.toInt())) {
if (!ESP3DSettings::writeIPString(spos.toInt(), sval.c_str())) {
error_msg = "Set failed";
hasError = true;
}
} else {
error_msg = "Invalid value for T";
hasError = true;
esp3d_log_e("Invalid value for T");
}
break;
default:
error_msg = "Invalid value for T";
hasError = true;
break;
}
if (!hasError) {
switch (spos.toInt()) {
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
case ESP_SERIAL_BRIDGE_ON:
if (!serial_bridge_service.started()) {
serial_bridge_service.begin(ESP_SERIAL_BRIDGE_OUTPUT);
}
break;
#endif // ESP_SERIAL_BRIDGE_OUTPUT
case ESP_VERBOSE_BOOT:
ESP3DSettings::isVerboseBoot(true);
break;
case ESP_TARGET_FW:
ESP3DSettings::GetFirmwareTarget(true);
break;
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
case ESP_SECURE_SERIAL:
esp3d_serial_service.setParameters();
break;
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
#ifdef AUTHENTICATION_FEATURE
case ESP_SESSION_TIMEOUT:
AuthenticationService::setSessionTimeout(1000 * 60 * sval.toInt());
break;
#endif // AUTHENTICATION_FEATURE
#ifdef SD_DEVICE
case ESP_SD_SPEED_DIV:
ESP_SD::setSPISpeedDivider(sval.toInt());
break;
#endif // SD_DEVICE
#ifdef TIMESTAMP_FEATURE
case ESP_INTERNET_TIME:
timeService.begin();
break;
#endif // TIMESTAMP_FEATURE
#ifdef NOTIFICATION_FEATURE
case ESP_AUTO_NOTIFICATION:
notificationsservice.setAutonotification((sval.toInt() == 0) ? false
: true);
break;
#endif // NOTIFICATION_FEATURE
#ifdef SENSOR_DEVICE
case ESP_SENSOR_TYPE:
esp3d_sensor.begin();
break;
#endif // SENSOR_DEVICE
#ifdef BUZZER_DEVICE
case ESP_BUZZER:
if (sval.toInt() == 1) {
esp3d_buzzer.begin();
} else if (sval.toInt() == 0) {
esp3d_buzzer.end();
}
break;
#endif // BUZZER_DEVICE
#ifdef SENSOR_DEVICE
case ESP_SENSOR_INTERVAL:
esp3d_sensor.setInterval(sval.toInt());
break;
#endif // SENSOR_DEVICE
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
case ESP_BAUD_RATE:
esp3d_serial_service.updateBaudRate(sval.toInt());
break;
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
case ESP_SERIAL_BRIDGE_BAUD:
serial_bridge_service.updateBaudRate(sval.toInt());
break;
#endif // defined(ESP_SERIAL_BRIDGE_OUTPUT)
#ifdef AUTHENTICATION_FEATURE
case ESP_ADMIN_PWD:
case ESP_USER_PWD:
AuthenticationService::update();
break;
#endif // AUTHENTICATION_FEATURE
default:
break;
}
}
}
if (!hasError) {
if (json) {
ok_msg = spos;
} else {
ok_msg = "ok";
}
} else {
if (json) {
String tmp = "{\"error\":\"";
tmp += error_msg;
tmp += "\"";
if (spos.length() > 0) {
tmp += ",\"position\":\"";
tmp += spos;
tmp += "\"";
}
tmp += "}";
error_msg = tmp;
} else {
error_msg += spos.length() > 0 ? " for P=" + spos : "";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}

View File

@ -0,0 +1,80 @@
/*
ESP402.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(SD_UPDATE_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
// Set SD Check at boot state which can be ON, OFF
//[ESP402]<state> json=<no> pwd=<admin password>
#define COMMAND_ID 402
void ESP3DCommands::ESP402(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
ESP3DState setting_http_mode =
(ESP3DState)ESP3DSettings::readByte(ESP_SD_CHECK_UPDATE_AT_BOOT);
if (tmpstr.length() == 0) {
if (setting_http_mode == ESP3DState::off) {
ok_msg = "OFF";
} else {
ok_msg = "ON";
}
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (tmpstr == "OFF" || tmpstr == "ON") {
if (!ESP3DSettings::writeByte(ESP_SD_CHECK_UPDATE_AT_BOOT,
tmpstr == "OFF" ? 0 : 1)) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // SD_UPDATE_FEATURE

View File

@ -0,0 +1,158 @@
/*
ESP410.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(WIFI_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/wifi/wificonfig.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#include "../esp3d_string.h"
// Get available AP list (limited to 30)
// output is JSON or plain text according parameter
//[ESP410]json=<no>
#define COMMAND_ID 410
void ESP3DCommands::ESP410(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() != 0) {
hasError = true;
error_msg = "This command doesn't take parameters";
esp3d_log_e("%s", error_msg.c_str());
} else {
if (WiFi.getMode() == WIFI_STA || WiFi.getMode() == WIFI_AP) {
uint8_t currentmode = WiFi.getMode();
int n = 0;
uint8_t total = 0;
if (json) {
tmpstr = "{\"cmd\":\"410\",\"status\":\"ok\",\"data\":[";
} else {
tmpstr = "Start Scan\n";
}
msg->type = ESP3DMessageType::head;
if (!dispatch(msg, tmpstr.c_str())) {
esp3d_log_e("Error sending response to clients");
return;
}
// Set Sta Mode if necessary
if (currentmode == WIFI_AP) {
WiFi.mode(WIFI_AP_STA);
}
// Scan
n = WiFi.scanNetworks();
for (int i = 0; i < n; ++i) {
tmpstr = "";
if (WiFi.RSSI(i) >= MIN_RSSI) {
if (total > 0) {
if (json) {
tmpstr += ",";
}
}
total++;
if (json) {
tmpstr += "{\"SSID\":\"";
tmpstr += esp3d_string::encodeString(WiFi.SSID(i).c_str());
} else {
tmpstr += WiFi.SSID(i).c_str();
}
if (json) {
tmpstr += "\",\"SIGNAL\":\"";
} else {
tmpstr += "\t";
}
tmpstr += String(WiFiConfig::getSignal(WiFi.RSSI(i)));
if (!json) {
tmpstr += "%";
}
if (json) {
tmpstr += "\",\"IS_PROTECTED\":\"";
}
if (WiFi.encryptionType(i) == ENC_TYPE_NONE) {
if (json) {
tmpstr += "0";
} else {
tmpstr += "\tOpen";
}
} else {
if (json) {
tmpstr += "1";
} else {
tmpstr += "\tSecure";
}
}
if (json) {
tmpstr += "\"}";
} else {
tmpstr += "\n";
}
if (!dispatch(tmpstr.c_str(), target, requestId,
ESP3DMessageType::core)) {
esp3d_log_e("Error sending answer to clients");
}
}
}
// Restore mode
if (currentmode == WIFI_AP) {
WiFi.mode((WiFiMode_t)currentmode);
}
WiFi.scanDelete();
if (json) {
tmpstr = "]}";
} else {
tmpstr = "End Scan\n";
}
if (!dispatch(tmpstr.c_str(), target, requestId,
ESP3DMessageType::tail)) {
esp3d_log_e("Error sending answer to clients");
}
return;
} else {
hasError = true;
error_msg = "WiFi not enabled";
esp3d_log_e("%s", error_msg.c_str());
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // WIFI_FEATURE

View File

@ -0,0 +1,735 @@
/*
ESP420.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#include "../../modules/authentication/authentication_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#include "../esp3d_string.h"
#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL || defined(ESP_SERIAL_BRIDGE_OUTPUT)
#include "../../modules/serial/serial_service.h"
#endif // COMMUNICATION_PROTOCOL != SOCKET_SERIAL
#ifdef FILESYSTEM_FEATURE
#include "../../modules/filesystem/esp_filesystem.h"
#endif // FILESYSTEM_FEATURE
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE) || defined(BLUETOOTH_FEATURE)
#include "../../modules/network/netconfig.h"
#if defined(WIFI_FEATURE)
#include "../../modules/wifi/wificonfig.h"
#endif // WIFI_FEATURE
#if defined(ETH_FEATURE)
#include "../../modules/ethernet/ethconfig.h"
#endif // ETH_FEATURE
#if defined(BLUETOOTH_FEATURE)
#include "../../modules/bluetooth/BT_service.h"
#endif // BLUETOOTH_FEATURE
#endif // WIFI_FEATURE || ETH_FEATURE || BLUETOOTH_FEATURE
#ifdef HTTP_FEATURE
#include "../../modules/http/http_server.h"
#endif // HTTP_FEATURE
#ifdef TELNET_FEATURE
#include "../../modules/telnet/telnet_server.h"
#endif // TELNET_FEATURE
#ifdef FTP_FEATURE
#include "../../modules/ftp/FtpServer.h"
#endif // FTP_FEATURE
#ifdef WS_DATA_FEATURE
#include "../../modules/websocket/websocket_server.h"
#endif // WS_DATA_FEATURE
#ifdef WEBDAV_FEATURE
#include "../../modules/webdav/webdav_server.h"
#endif // WEBDAV_FEATURE
#if defined(TIMESTAMP_FEATURE)
#include "../../modules/time/time_service.h"
#endif // TIMESTAMP_FEATURE
#if defined(SENSOR_DEVICE)
#include "../../modules/sensor/sensor.h"
#endif // SENSOR_DEVICE
#ifdef NOTIFICATION_FEATURE
#include "../../modules/notifications/notifications_service.h"
#endif // NOTIFICATION_FEATURE
#ifdef BUZZER_DEVICE
#include "../../modules/buzzer/buzzer.h"
#endif // BUZZER_DEVICE
#ifdef CAMERA_DEVICE
#include "../../modules/camera/camera.h"
#endif // CAMERA_DEVICE
#ifdef SD_DEVICE
#include "../../modules/filesystem/esp_sd.h"
#endif // SD_DEVICE
#if defined(DISPLAY_DEVICE)
#include "../../modules/display/display.h"
#endif // DISPLAY_DEVICE
#define COMMAND_ID 420
#if defined(AUTHENTICATION_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#endif // AUTHENTICATION_FEATURE
// Get ESP current status
// output is JSON or plain text according parameter
//[ESP420]json=<no>
void ESP3DCommands::ESP420(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool json = hasTag(msg, cmd_params_pos, "json");
bool addPreTag = hasTag(msg, cmd_params_pos, "addPreTag");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (json) {
tmpstr = "{\"cmd\":\"420\",\"status\":\"ok\",\"data\":[";
} else {
if (addPreTag) {
tmpstr = "<pre>\n";
} else {
tmpstr = "Configuration:\n";
}
}
msg->type = ESP3DMessageType::head;
if (!dispatch(msg, tmpstr.c_str())) {
esp3d_log_e("Error sending response to clients");
return;
}
// First entry
// Chip ID
tmpstr = ESP3DHal::getChipID();
if (!dispatchIdValue(json, "chip id", tmpstr.c_str(), target, requestId,
true)) {
return;
}
// CPU Freq
tmpstr = String(ESP.getCpuFreqMHz());
tmpstr += "Mhz";
if (!dispatchIdValue(json, "CPU Freq", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// CPU Temp
if (ESP3DHal::has_temperature_sensor()) {
tmpstr = String(ESP3DHal::temperature(), 1);
tmpstr += "C";
if (!dispatchIdValue(json, "CPU Temp", tmpstr.c_str(), target, requestId,
false)) {
return;
}
}
// Free Memory
tmpstr = esp3d_string::formatBytes(ESP.getFreeHeap());
#ifdef ARDUINO_ARCH_ESP32
#ifdef BOARD_HAS_PSRAM
tmpstr += " - PSRAM:";
tmpstr += esp3d_string::formatBytes(ESP.getFreePsram());
#endif // BOARD_HAS_PSRAM
#endif // ARDUINO_ARCH_ESP32
if (!dispatchIdValue(json, "free mem", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// SDK Version
tmpstr = ESP.getSdkVersion();
if (!dispatchIdValue(json, "SDK", tmpstr.c_str(), target, requestId, false)) {
return;
}
// Flash size
tmpstr = esp3d_string::formatBytes(ESP.getFlashChipSize());
if (!dispatchIdValue(json, "flash size", tmpstr.c_str(), target, requestId,
false)) {
return;
}
#if (defined(WIFI_FEATURE) || defined(ETH_FEATURE)) && \
(defined(OTA_FEATURE) || defined(WEB_UPDATE_FEATURE))
// update space
tmpstr = esp3d_string::formatBytes(ESP_FileSystem::max_update_size());
if (!dispatchIdValue(json, "size for update", tmpstr.c_str(), target,
requestId, false)) {
return;
}
#endif // WIFI_FEATURE || ETH_FEATURE
#if defined(FILESYSTEM_FEATURE)
// FileSystem type
tmpstr = ESP_FileSystem::FilesystemName();
if (!dispatchIdValue(json, "FS type", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// FileSystem capacity
tmpstr = esp3d_string::formatBytes(ESP_FileSystem::usedBytes());
tmpstr += "/";
tmpstr += esp3d_string::formatBytes(ESP_FileSystem::totalBytes());
if (!dispatchIdValue(json, "FS usage", tmpstr.c_str(), target, requestId,
false)) {
return;
}
#endif // FILESYSTEM_FEATURE
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
// baud rate
tmpstr = String(esp3d_serial_service.baudRate());
if (!dispatchIdValue(json, "baud", tmpstr.c_str(), target, requestId,
false)) {
return;
}
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
#if defined(WIFI_FEATURE)
if (WiFi.getMode() != WIFI_OFF) {
// sleep mode
tmpstr = WiFiConfig::getSleepModeString();
if (!dispatchIdValue(json, "sleep mode", tmpstr.c_str(), target, requestId,
false)) {
return;
}
}
#endif // WIFI_FEATURE
#if defined(WIFI_FEATURE)
// Wifi enabled
tmpstr = (WiFi.getMode() == WIFI_OFF) ? "OFF" : "ON";
if (!dispatchIdValue(json, "wifi", tmpstr.c_str(), target, requestId,
false)) {
return;
}
#endif // WIFI_FEATURE
#if defined(ETH_FEATURE)
// Ethernet enabled
tmpstr = (EthConfig::started()) ? "ON" : "OFF";
if (!dispatchIdValue(json, "ethernet", tmpstr.c_str(), target, requestId,
false)) {
return;
}
#endif // ETH_FEATURE
#if defined(BLUETOOTH_FEATURE)
// BT enabled
tmpstr = (bt_service.started()) ? "ON" : "OFF";
if (!dispatchIdValue(json, "bt", tmpstr.c_str(), target, requestId, false)) {
return;
}
#endif // BLUETOOTH_FEATURE
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
// Hostname
tmpstr = NetConfig::hostname();
if (!dispatchIdValue(json, "hostname", tmpstr.c_str(), target, requestId,
false)) {
return;
}
#if defined(HTTP_FEATURE)
if (HTTP_Server::started()) {
// http port
tmpstr = String(HTTP_Server::port());
if (!dispatchIdValue(json, "HTTP port", tmpstr.c_str(), target, requestId,
false)) {
return;
}
}
#endif // HTTP_FEATURE
#if defined(TELNET_FEATURE)
if (telnet_server.started()) {
// telnet port
tmpstr = String(telnet_server.port());
if (!dispatchIdValue(json, "Telnet port", tmpstr.c_str(), target, requestId,
false)) {
return;
}
}
if (telnet_server.isConnected()) {
// telnet client IP
tmpstr = telnet_server.clientIPAddress();
if (!dispatchIdValue(json, "Telnet Client", tmpstr.c_str(), target,
requestId, false)) {
return;
}
}
#endif // TELNET_FEATURE
#if defined(WEBDAV_FEATURE)
if (webdav_server.started()) {
// WebDav port
tmpstr = String(webdav_server.port());
if (!dispatchIdValue(json, "WebDav port", tmpstr.c_str(), target, requestId,
false)) {
return;
}
}
#endif // WEBDAV_FEATURE
#if defined(FTP_FEATURE)
if (ftp_server.started()) {
// ftp ports
tmpstr = String(ftp_server.ctrlport());
tmpstr += ",";
tmpstr += String(ftp_server.dataactiveport());
tmpstr += ",";
tmpstr += String(ftp_server.datapassiveport());
if (!dispatchIdValue(json, "Ftp ports", tmpstr.c_str(), target, requestId,
false)) {
return;
}
}
#endif // FTP_FEATURE
#if defined(WS_DATA_FEATURE)
if (websocket_data_server.started()) {
// websocket port
tmpstr = String(websocket_data_server.port());
if (!dispatchIdValue(json, "Websocket port", tmpstr.c_str(), target,
requestId, false)) {
return;
}
}
#endif // WS_DATA_FEATURE
#if defined(CAMERA_DEVICE)
if (esp3d_camera.started()) {
// camera name
tmpstr = esp3d_camera.GetModelString();
tmpstr += "(";
tmpstr += esp3d_camera.GetModel();
tmpstr += ")";
if (!dispatchIdValue(json, "camera name", tmpstr.c_str(), target, requestId,
false)) {
return;
}
}
#endif // CAMERA_DEVICE
#endif // #if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
#if defined(DISPLAY_DEVICE)
tmpstr = esp3d_display.getModelString();
if (!dispatchIdValue(json, "display", tmpstr.c_str(), target, requestId,
false)) {
return;
}
#endif // DISPLAY_DEVICE
#if defined(BLUETOOTH_FEATURE)
if (bt_service.started()) {
// BT mode
tmpstr = BTService::macAddress();
if (!dispatchIdValue(json, "bt", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// BT status
tmpstr = (bt_service.isConnected()) ? "connected" : "disconnected";
if (bt_service.isConnected()) {
tmpstr += " (client: ";
tmpstr += BTService::clientmacAddress();
tmpstr += ")";
}
if (!dispatchIdValue(json, "BT Status", tmpstr.c_str(), target, requestId,
false)) {
return;
}
}
#endif // BLUETOOTH_FEATURE
#if defined(ETH_FEATURE)
if (EthConfig::started()) {
// Ethernet mode
tmpstr = ETH.macAddress();
if (!dispatchIdValue(json, "ethernet", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// Ethernet cable
tmpstr = (EthConfig::linkUp()) ? "connected" : "disconnected";
if (EthConfig::linkUp()) {
tmpstr += " (";
tmpstr += ETH.linkSpeed();
tmpstr += "Mbps)";
}
if (!dispatchIdValue(json, "cable", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// IP mode
tmpstr = (NetConfig::isIPModeDHCP(ESP_ETH_STA)) ? "dhcp" : "static";
if (!dispatchIdValue(json, "ip mode", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// IP value
tmpstr = ETH.localIP().toString();
if (!dispatchIdValue(json, "ip", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// GW value
tmpstr = ETH.gatewayIP().toString();
if (!dispatchIdValue(json, "gw", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// Mask value
tmpstr = ETH.subnetMask().toString();
if (!dispatchIdValue(json, "msk", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// DNS value
tmpstr = ETH.dnsIP().toString();
if (!dispatchIdValue(json, "DNS", tmpstr.c_str(), target, requestId,
false)) {
return;
}
}
#endif // ETH_FEATURE
#if defined(WIFI_FEATURE)
if (WiFi.getMode() != WIFI_OFF) {
// WiFi Mode
tmpstr = (WiFi.getMode() == WIFI_STA) ? "sta"
: (WiFi.getMode() == WIFI_AP) ? "ap"
: (WiFi.getMode() == WIFI_AP_STA) ? "mixed"
: "unknown";
if (!dispatchIdValue(json, "wifi mode", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// WiFi mac
tmpstr = (WiFi.getMode() == WIFI_STA) ? WiFi.macAddress()
: (WiFi.getMode() == WIFI_AP) ? WiFi.softAPmacAddress()
: (WiFi.getMode() == WIFI_AP_STA)
? WiFi.macAddress() + "/" + WiFi.softAPmacAddress()
: "unknown";
if (!dispatchIdValue(json, "mac", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// WiFi Station
if (WiFi.getMode() == WIFI_STA) {
// Connected to SSID
tmpstr = (WiFi.isConnected()) ? WiFi.SSID() : "";
if (!dispatchIdValue(json, "SSID", tmpstr.c_str(), target, requestId,
false)) {
return;
}
if (WiFi.isConnected()) { // in case query come from serial
// Signal strength
tmpstr = WiFiConfig::getSignal(WiFi.RSSI(), false);
tmpstr += "%";
if (!dispatchIdValue(json, "signal", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// Phy Mode
tmpstr = WiFiConfig::getPHYModeString(WIFI_STA);
if (!dispatchIdValue(json, "phy mode", tmpstr.c_str(), target,
requestId, false)) {
return;
}
// Channel
tmpstr = String(WiFi.channel());
if (!dispatchIdValue(json, "channel", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// IP Mode
tmpstr = (NetConfig::isIPModeDHCP(ESP_WIFI_STA)) ? "dhcp" : "static";
if (!dispatchIdValue(json, "ip mode", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// IP value
tmpstr = WiFi.localIP().toString();
if (!dispatchIdValue(json, "ip", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// Gateway value
tmpstr = WiFi.gatewayIP().toString();
if (!dispatchIdValue(json, "gw", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// Mask value
tmpstr = WiFi.subnetMask().toString();
if (!dispatchIdValue(json, "msk", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// DNS value
tmpstr = WiFi.dnsIP().toString();
if (!dispatchIdValue(json, "DNS", tmpstr.c_str(), target, requestId,
false)) {
return;
}
}
// Disabled Mode
tmpstr = "OFF";
if (!dispatchIdValue(json, "ap", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// Disabled Mode
tmpstr = WiFi.softAPmacAddress();
if (!dispatchIdValue(json, "mac", tmpstr.c_str(), target, requestId,
false)) {
return;
}
} else if (WiFi.getMode() == WIFI_AP) {
// AP SSID
tmpstr = WiFiConfig::AP_SSID();
if (!dispatchIdValue(json, "SSID", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// AP Visibility
tmpstr = (WiFiConfig::is_AP_visible()) ? "yes" : "no";
if (!dispatchIdValue(json, "visible", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// AP Authentication
tmpstr = WiFiConfig::AP_Auth_String();
if (!dispatchIdValue(json, "authentication", tmpstr.c_str(), target,
requestId, false)) {
return;
}
// DHCP Server
tmpstr = (NetConfig::isDHCPServer(ESP_WIFI_AP)) ? "ON" : "OFF";
if (!dispatchIdValue(json, "DHCP Server", tmpstr.c_str(), target,
requestId, false)) {
return;
}
// IP Value
tmpstr = WiFi.softAPIP().toString();
if (!dispatchIdValue(json, "ip", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// Gateway Value
tmpstr = WiFiConfig::AP_Gateway_String();
if (!dispatchIdValue(json, "gw", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// Mask Value
tmpstr = WiFiConfig::AP_Mask_String();
if (!dispatchIdValue(json, "msk", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// Connected clients
const char* entry = NULL;
uint8_t nb = 0;
entry = WiFiConfig::getConnectedSTA(&nb, true);
tmpstr = String(nb);
if (!dispatchIdValue(json, "clients", tmpstr.c_str(), target, requestId,
false)) {
return;
}
for (uint8_t i = 0; i < nb; i++) {
// Client
tmpstr = entry;
String label = "# " + String(i);
if (!dispatchIdValue(json, label.c_str(), tmpstr.c_str(), target,
requestId, false)) {
return;
}
entry = WiFiConfig::getConnectedSTA();
}
// Disabled Mode
tmpstr = "OFF";
if (!dispatchIdValue(json, "sta", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// Disabled Mode
tmpstr = WiFi.macAddress();
if (!dispatchIdValue(json, "mac", tmpstr.c_str(), target, requestId,
false)) {
return;
}
}
}
#endif // WIFI_FEATURE
#if defined(TIMESTAMP_FEATURE)
// NTP enabled
tmpstr = (timeService.started()) ? "ON" : "OFF";
if (!dispatchIdValue(json, "ntp", tmpstr.c_str(), target, requestId, false)) {
return;
}
#endif // TIMESTAMP_FEATURE
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
// serial enabled
tmpstr = (esp3d_serial_service.started()) ? "ON" : "OFF";
if (!dispatchIdValue(json, "serial", tmpstr.c_str(), target, requestId,
false)) {
return;
}
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
#if defined(AUTHENTICATION_FEATURE)
// authentication enabled
tmpstr = "ON";
#else
tmpstr = "OFF";
#endif // AUTHENTICATION_FEATURE
if (!dispatchIdValue(json, "authentication", tmpstr.c_str(), target,
requestId, false)) {
return;
}
#if defined(NOTIFICATION_FEATURE)
// notification enabled
tmpstr = (notificationsservice.started()) ? "ON" : "OFF";
if (notificationsservice.started()) {
tmpstr += " (";
tmpstr += notificationsservice.getTypeString();
tmpstr += ")";
}
if (!dispatchIdValue(json, "notification", tmpstr.c_str(), target, requestId,
false)) {
return;
}
#endif // NOTIFICATION_FEATURE
#if defined(SD_DEVICE)
// SD enabled
tmpstr = (ESP3DSettings::GetSDDevice() == ESP_DIRECT_SD) ? "direct "
: (ESP3DSettings::GetSDDevice() == ESP_SHARED_SD) ? "shared "
: "none ";
tmpstr += "(";
tmpstr += ESP_SD::FilesystemName();
tmpstr += ")";
if (!dispatchIdValue(json, "sd", tmpstr.c_str(), target, requestId, false)) {
return;
}
#ifdef SD_UPDATE_FEATURE
// SD updater enabled
tmpstr =
ESP3DSettings::readByte(ESP_SD_CHECK_UPDATE_AT_BOOT) != 0 ? "ON" : "OFF";
if (!dispatchIdValue(json, "SD updater", tmpstr.c_str(), target, requestId,
false)) {
return;
}
#endif // SD_UPDATE_FEATURE
#endif // SD_DEVICE
#if defined(SENSOR_DEVICE)
// sensor enabled
tmpstr = (esp3d_sensor.started()) ? "ON" : "OFF";
if (esp3d_sensor.started()) {
tmpstr += " (";
tmpstr += esp3d_sensor.GetCurrentModelString();
tmpstr += ")";
}
if (!dispatchIdValue(json, "sensor", tmpstr.c_str(), target, requestId,
false)) {
return;
}
#endif // SENSOR_DEVICE
#if defined(BUZZER_DEVICE)
// buzzer enabled
tmpstr = (esp3d_buzzer.started()) ? "ON" : "OFF";
if (!dispatchIdValue(json, "buzzer", tmpstr.c_str(), target, requestId,
false)) {
return;
}
#endif // BUZZER_DEVICE
#if defined(CAMERA_DEVICE)
// camera enabled
tmpstr = (esp3d_camera.started()) ? "ON" : "OFF";
if (esp3d_camera.started()) {
tmpstr += " (";
tmpstr += esp3d_camera.GetModelString();
tmpstr += ")";
}
if (!dispatchIdValue(json, "camera", tmpstr.c_str(), target, requestId,
false)) {
return;
}
#endif // CAMERA_DEVICE
// Serial mode
#if COMMUNICATION_PROTOCOL == MKS_SERIAL
tmpstr = "MKS";
if (!dispatchIdValue(json, "serial", tmpstr.c_str(), target, requestId,
false)) {
return;
}
#endif // COMMUNICATION_PROTOCOL == MKS_SERIAL
// Target Firmware
tmpstr = ESP3DSettings::GetFirmwareTargetShortName();
if (!dispatchIdValue(json, "targetfw", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// FW version
tmpstr = "";
#if defined(SHORT_BUILD_VERSION)
tmpstr += SHORT_BUILD_VERSION "-";
#endif // SHORT_BUILD_VERSION
tmpstr += FW_VERSION;
if (!dispatchIdValue(json, "FW ver", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// FW architecture
tmpstr = ESP3DSettings::TargetBoard();
if (!dispatchIdValue(json, "FW arch", tmpstr.c_str(), target, requestId,
false)) {
return;
}
// end of list
if (json) {
if (!dispatch("]}", target, requestId, ESP3DMessageType::tail)) {
esp3d_log_e("Error sending answer to clients");
}
} else {
{
if (addPreTag) {
tmpstr = "</pre>\n";
} else {
tmpstr = "ok\n";
}
if (!dispatch(tmpstr.c_str(), target, requestId,
ESP3DMessageType::tail)) {
esp3d_log_e("Error sending answer to clients");
}
}
}
}

View File

@ -0,0 +1,83 @@
/*
ESP444.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#include "../../modules/authentication/authentication_service.h"
#include "../esp3d.h"
#include "../esp3d_commands.h"
// Set ESP State
// cmd are RESTART / RESET
//[ESP444]<cmd> json=<no> <pwd=admin>
#define COMMAND_ID 444
void ESP3DCommands::ESP444(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
bool isRestart = hasTag(msg, cmd_params_pos, "RESTART");
bool isReset = hasTag(msg, cmd_params_pos, "RESET");
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "";
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (isReset) {
Esp3D::reset();
esp3d_log("Resetting settings");
}
if (!(isRestart || isReset)) {
hasError = true;
esp3d_log_e("%s", error_msg.c_str());
} else {
if (isReset) {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
ok_msg += "Reset done";
}
if (isRestart) {
if (ok_msg.length() > 0) {
ok_msg += ", ";
}
ok_msg += "Now restarting...";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
return;
}
if (isRestart) {
ESP3DHal::wait(100);
Esp3D::restart_esp();
}
}

View File

@ -0,0 +1,142 @@
/*
ESP410.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(MDNS_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/mDNS/mDNS.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
// Get available ESP3D list
// output is JSON or plain text according parameter
//[ESP4\50]json=<no>
#define COMMAND_ID 450
void ESP3DCommands::ESP450(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (!(WiFi.getMode() == WIFI_STA || WiFi.getMode() == WIFI_AP)) {
tmpstr = "Network not enabled";
dispatchAnswer(msg, COMMAND_ID, json, true, tmpstr.c_str());
return;
}
if (json) {
tmpstr = "{\"cmd\":\"450\",\"status\":\"ok\",\"data\":[";
} else {
tmpstr = "Start Scan\n";
}
msg->type = ESP3DMessageType::head;
if (!dispatch(msg, tmpstr.c_str())) {
esp3d_log_e("Error sending response to clients");
return;
}
uint16_t count = esp3d_mDNS.servicesCount();
uint16_t nbListed = 0;
esp3d_log("Total : %d", count);
for (uint16_t i = 0; i < count; i++) {
if (strlen(esp3d_mDNS.answerHostname(i)) == 0) {
continue;
}
// Currently esp3d support only IPV4 and only one address per device
tmpstr = "";
if (json) {
if (nbListed > 0) {
tmpstr += ",";
}
tmpstr += "{\"Hostname\":\"";
}
tmpstr += esp3d_mDNS.answerHostname(i);
if (json) {
tmpstr += "\",\"IP\":\"";
} else {
tmpstr += " (";
}
tmpstr += esp3d_mDNS.answerIP(i);
if (json) {
tmpstr += "\",\"port\":\"";
tmpstr += String(esp3d_mDNS.answerPort(i));
tmpstr += "\",\"TxT\":[";
} else {
tmpstr += ":";
tmpstr += String(esp3d_mDNS.answerPort(i));
tmpstr += ") ";
}
uint16_t nbtxt = esp3d_mDNS.answerTxtCount(i);
for (uint8_t t = 0; t < nbtxt; t++) {
if (t != 0) {
tmpstr += ",";
}
if (json) {
tmpstr += "{\"key\":\"";
}
tmpstr += esp3d_mDNS.answerTxtKey(i, t);
if (json) {
tmpstr += "\",\"value\":\"";
} else {
tmpstr += "=";
}
tmpstr += esp3d_mDNS.answerTxt(i, t);
if (json) {
tmpstr += "\"}";
}
}
if (json) {
tmpstr += "]}";
} else {
tmpstr += "\n";
}
if (!dispatch(tmpstr.c_str(), target, requestId, ESP3DMessageType::core)) {
esp3d_log_e("Error sending answer to clients");
}
nbListed++;
}
// end of list
if (json) {
tmpstr = "]}";
} else {
tmpstr = "End Scan\n";
}
if (!dispatch(tmpstr.c_str(), target, requestId, ESP3DMessageType::tail)) {
esp3d_log_e("Error sending answer to clients");
}
}
#endif // MDNS_FEATURE

View File

@ -0,0 +1,67 @@
/*
ESP550.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(AUTHENTICATION_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 500
// Get connection status
//[ESP500] json=<no> pwd=<admin password>
void ESP3DCommands::ESP500(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
switch (msg->authentication_level) {
case ESP3DAuthenticationLevel::admin:
ok_msg = "admin";
break;
case ESP3DAuthenticationLevel::user:
ok_msg = "user";
break;
case ESP3DAuthenticationLevel::not_authenticated:
ok_msg = "Not authenticated";
break;
default:
ok_msg = "guest";
break;
}
} else {
esp3d_log_e("Got %s", tmpstr.c_str());
hasError = true;
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // AUTHENTICATION_FEATURE

View File

@ -0,0 +1,83 @@
/*
ESP550.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(AUTHENTICATION_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 510
// set/display session time out
//[ESP510]<timeout> json=<no> pwd=<admin password>
void ESP3DCommands::ESP510(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
const ESP3DSettingDescription* settingPtr =
ESP3DSettings::getSettingPtr(ESP_SESSION_TIMEOUT);
if (settingPtr) {
ok_msg = String(ESP3DSettings::readByte(ESP_SESSION_TIMEOUT));
} else {
hasError = true;
error_msg = "This setting is unknown";
}
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (ESP3DSettings::isValidByteSetting((uint8_t)tmpstr.toInt(),
ESP_SESSION_TIMEOUT)) {
esp3d_log("Value %s is valid", tmpstr.c_str());
if (!ESP3DSettings::writeByte(ESP_SESSION_TIMEOUT,
(uint8_t)tmpstr.toInt())) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // AUTHENTICATION_FEATURE

View File

@ -0,0 +1,79 @@
/*
ESP550.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(AUTHENTICATION_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 550
// Change admin password
//[ESP550]<password> json=<no> pwd=<admin password>
void ESP3DCommands::ESP550(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
const ESP3DSettingDescription* settingPtr =
ESP3DSettings::getSettingPtr(ESP_ADMIN_PWD);
if (settingPtr) {
ok_msg = ESP3DSettings::readString(ESP_ADMIN_PWD);
} else {
hasError = true;
error_msg = "This setting is unknown";
}
} else {
esp3d_log(
"got %s param for a value of %s, is valid %d", tmpstr.c_str(),
tmpstr.c_str(),
ESP3DSettings::isValidStringSetting(tmpstr.c_str(), ESP_ADMIN_PWD));
if (ESP3DSettings::isValidStringSetting(tmpstr.c_str(), ESP_ADMIN_PWD)) {
esp3d_log("Value %s is valid", tmpstr.c_str());
if (!ESP3DSettings::writeString(ESP_ADMIN_PWD, tmpstr.c_str())) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // AUTHENTICATION_FEATURE

View File

@ -0,0 +1,79 @@
/*
ESP555.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(AUTHENTICATION_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
// Change user password
//[ESP555]<password> json=<no> pwd=<admin/user password>
#define COMMAND_ID 555
void ESP3DCommands::ESP555(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
const ESP3DSettingDescription* settingPtr =
ESP3DSettings::getSettingPtr(ESP_USER_PWD);
if (settingPtr) {
ok_msg = ESP3DSettings::readString(ESP_USER_PWD);
} else {
hasError = true;
error_msg = "This setting is unknown";
}
} else {
esp3d_log(
"got %s param for a value of %s, is valid %d", tmpstr.c_str(),
tmpstr.c_str(),
ESP3DSettings::isValidStringSetting(tmpstr.c_str(), ESP_USER_PWD));
if (ESP3DSettings::isValidStringSetting(tmpstr.c_str(), ESP_USER_PWD)) {
esp3d_log("Value %s is valid", tmpstr.c_str());
if (!ESP3DSettings::writeString(ESP_USER_PWD, tmpstr.c_str())) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // AUTHENTICATION_FEATURE

View File

@ -0,0 +1,66 @@
/*
ESP600.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(NOTIFICATION_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/notifications/notifications_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 600
// Send Notification
//[ESP600]msg json=<no> pwd=<admin/user password>
void ESP3DCommands::ESP600(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
hasError = true;
error_msg = "Message is missing";
esp3d_log_e("%s", error_msg.c_str());
} else {
if (!notificationsservice.sendMSG(ESP_NOTIFICATION_TITLE, tmpstr.c_str())) {
hasError = true;
error_msg = "Send notification failed";
esp3d_log_e("%s", error_msg.c_str());
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // NOTIFICATION_FEATURE

View File

@ -0,0 +1,139 @@
/*
ESP610.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(NOTIFICATION_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/notifications/notifications_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 610
// Set/Get Notification settings
//[ESP610]
// Get will give type and settings
// but not T1/T2 chich are protected
void ESP3DCommands::ESP610(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
const char* cmdList[] = {"type=", "T1=", "T2=", "TS="};
uint8_t cmdListSize = sizeof(cmdList) / sizeof(char*);
const char* notificationStr[] = {"NONE", "PUSHOVER", "EMAIL",
"LINE", "TELEGRAM", "IFTTT", "HOMEASSISTANT"};
uint8_t notificationStrSize = sizeof(notificationStr) / sizeof(char*);
const ESP3DSettingIndex settingIndex[] = {
ESP_NOTIFICATION_TYPE, ESP_NOTIFICATION_TOKEN1, ESP_NOTIFICATION_TOKEN2,
ESP_NOTIFICATION_SETTINGS};
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
if (json) {
ok_msg = "{\"type\":\"";
} else {
ok_msg = "type: ";
}
uint8_t Ntype = ESP3DSettings::readByte(ESP_NOTIFICATION_TYPE);
ok_msg += Ntype < notificationStrSize ? notificationStr[Ntype] : "Unknown";
if (json) {
ok_msg += "\",\"TS\":\"";
} else {
ok_msg += ", TS: ";
}
ok_msg += ESP3DSettings::readString(ESP_NOTIFICATION_SETTINGS);
if (json) {
ok_msg += "\"}";
}
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
bool hasParam = false;
for (uint8_t i = 0; i < cmdListSize; i++) {
bool isPresent = false;
tmpstr = get_param(msg, cmd_params_pos, cmdList[i], &isPresent);
if (tmpstr.length() != 0 || isPresent) {
hasParam = true;
if (i > 0) {
if (ESP3DSettings::isValidStringSetting(tmpstr.c_str(),
settingIndex[i])) {
esp3d_log("Value %s is valid", tmpstr.c_str());
if (!ESP3DSettings::writeString(settingIndex[i], tmpstr.c_str())) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
} else {
tmpstr.toUpperCase();
int id = -1;
for (uint8_t j = 0; j < notificationStrSize; j++) {
if (tmpstr == notificationStr[j]) {
id = j;
break;
}
}
if (id != -1) {
if (!ESP3DSettings::writeByte(ESP_NOTIFICATION_TYPE, id)) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
esp3d_log_e("%s", error_msg.c_str());
}
}
}
}
if (!hasParam && !hasError) {
hasError = true;
error_msg = "Invalid parameter";
}
if (!hasError) {
notificationsservice.begin();
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // NOTIFICATION_FEATURE

View File

@ -0,0 +1,66 @@
/*
ESP620.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(NOTIFICATION_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/notifications/notifications_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 620
// Send Notification using encoded URL
//[ESP620]URL=<encoded url> json=<no>[pwd=<admin/user password>]
void ESP3DCommands::ESP620(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_param(msg, cmd_params_pos, "URL=");
if (tmpstr.length() == 0) {
hasError = true;
error_msg = "URL is missing";
} else {
if (notificationsservice.GET(tmpstr.c_str())) {
ok_msg = "Notification sent";
} else {
hasError = true;
error_msg = "Send notification failed";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // NOTIFICATION_FEATURE

View File

@ -0,0 +1,81 @@
/*
ESP700.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(GCODE_HOST_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/gcode_host/gcode_host.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 700
// TODO :
// - on ESP3DLib or GRBL_ESP32 the file/line must be processed like a SD gcode
// file
// - on ESP3D the file/line must be processed and/or streamed like a SD gcode
// file
// process local file
//[ESP700]<filename>
void ESP3DCommands::ESP700(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
hasError = true;
error_msg = "Missing parameter";
esp3d_log_e("%s", error_msg.c_str());
} else {
if (esp3d_gcode_host.getStatus() == HOST_NO_STREAM) {
if (esp3d_gcode_host.processFile(tmpstr.c_str(),
msg->authentication_level)) {
esp3d_log("Processing %s", parameter.c_str());
} else {
hasError = true;
error_msg = "Error processing file";
esp3d_log_e("%s", error_msg.c_str());
}
} else {
hasError = true;
error_msg = "Streaming already in progress";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // GCODE_HOST_FEATURE

View File

@ -0,0 +1,146 @@
/*
ESP701.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// #define ESP_LOG_FEATURE LOG_OUTPUT_SERIAL0
#include "../../include/esp3d_config.h"
#if defined(GCODE_HOST_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/gcode_host/gcode_host.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 701
// Query and Control ESP700 stream
//[ESP701]action=<PAUSE/RESUME/ABORT> CLEAR_ERROR
void ESP3DCommands::ESP701(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
bool hasClearError = hasTag(msg, cmd_params_pos, "CLEAR_ERROR");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
// give status
switch (esp3d_gcode_host.getStatus()) {
case HOST_START_STREAM:
case HOST_READ_LINE:
case HOST_PROCESS_LINE:
case HOST_WAIT4_ACK:
// TODO add % of progress and filename if any
// totalSize / processedSize / fileName
if (json) {
ok_msg = "{\"status\":\"processing\",\"total\":\"" +
String(esp3d_gcode_host.totalSize()) +
"\",\"processed\":\"" +
String(esp3d_gcode_host.processedSize()) + "\",\"type\":\"" +
String(esp3d_gcode_host.getFSType());
if (esp3d_gcode_host.getFSType() != TYPE_SCRIPT_STREAM) {
ok_msg += "\",\"name\":\"" + String(esp3d_gcode_host.fileName());
}
ok_msg += "\"}";
} else {
ok_msg = "processing";
}
break;
case HOST_PAUSE_STREAM:
ok_msg = "pause";
break;
case HOST_NO_STREAM:
esp3d_log("No stream %d", esp3d_gcode_host.getErrorNum());
if (esp3d_gcode_host.getErrorNum() != ERROR_NO_ERROR) {
hasError = true;
if (json) {
error_msg = "{\"status\":\"no stream\",\"code\":\"" +
String(esp3d_gcode_host.getErrorNum()) + "\"}";
} else {
error_msg = "no stream, last error " +
String(esp3d_gcode_host.getErrorNum());
}
} else {
ok_msg = "no stream";
}
break;
default:
error_msg = String(esp3d_gcode_host.getStatus());
hasError = true;
break;
}
} else {
tmpstr = get_param(msg, cmd_params_pos, "action=");
if (tmpstr.length() == 0) {
hasError = true;
error_msg = "Missing parameter";
esp3d_log_e("%s", error_msg.c_str());
} else {
tmpstr.toUpperCase();
if (tmpstr != "PAUSE" && tmpstr != "ABORT" && tmpstr != "RESUME") {
hasError = true;
error_msg = "Unknown action";
esp3d_log_e("%s", error_msg.c_str());
} else {
if (tmpstr == "PAUSE") {
if (esp3d_gcode_host.pause()) {
ok_msg = "Stream paused";
} else {
hasError = true;
error_msg = "No stream to pause";
}
} else if (tmpstr == "RESUME") {
if (esp3d_gcode_host.resume()) {
ok_msg = "Stream resumed";
} else {
error_msg = "No stream to resume";
hasError = true;
}
} else if (tmpstr == "ABORT") {
if (esp3d_gcode_host.abort()) {
ok_msg = "Stream aborted";
} else {
error_msg = "No stream to abort";
hasError = true;
}
}
}
if (hasClearError) {
esp3d_gcode_host.setErrorNum(ERROR_NO_ERROR);
}
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // GCODE_HOST_FEATURE

View File

@ -0,0 +1,88 @@
/*
ESP710.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(FILESYSTEM_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/filesystem/esp_filesystem.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 710
// Format ESP Filesystem
//[ESP710]FORMATFS json=<no> pwd=<admin password>
void ESP3DCommands::ESP710(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
bool formatfs = hasTag(msg, cmd_params_pos, "FORMATFS");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
ESP3DMessage* endMsg = nullptr;
if (tmpstr.length() == 0) {
hasError = true;
error_msg = "Missing parameter";
esp3d_log_e("%s", error_msg.c_str());
} else {
if (formatfs) {
ok_msg = "Starting formating...";
endMsg = ESP3DMessageManager::copyMsgInfos(*msg);
} else {
hasError = true;
error_msg = "Invalid parameter";
esp3d_log_e("%s", error_msg.c_str());
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
if (!hasError && formatfs) {
ESP_FileSystem::accessFS();
bool success = ESP_FileSystem::format();
if (success) {
ok_msg = "Formating done";
} else {
hasError = true;
error_msg = "Formating failed";
esp3d_log_e("%s", error_msg.c_str());
}
if (!dispatchAnswer(endMsg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
ESP_FileSystem::releaseFS();
}
}
#endif // FILESYSTEM_FEATURE

View File

@ -0,0 +1,102 @@
/*
ESP715.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(SD_DEVICE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/filesystem/esp_sd.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 715
// Format SD Filesystem
//[ESP715]FORMATSD json=<no> pwd=<admin password>
void ESP3DCommands::ESP715(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
bool formatsd = hasTag(msg, cmd_params_pos, "FORMATSD");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
ESP3DMessage* endMsg = nullptr;
if (tmpstr.length() == 0) {
hasError = true;
error_msg = "Missing parameter";
esp3d_log_e("%s", error_msg.c_str());
} else {
if (formatsd) {
if (ESP_SD::getState() != ESP_SDCARD_BUSY) {
ok_msg = "Starting formating...";
endMsg = ESP3DMessageManager::copyMsgInfos(*msg);
} else {
hasError = true;
error_msg = "SD card busy";
esp3d_log_e("%s", error_msg.c_str());
}
} else {
hasError = true;
error_msg = "Invalid parameter";
esp3d_log_e("%s", error_msg.c_str());
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
if (!hasError && formatsd) {
if (ESP_SD::accessFS()) {
ESP_SD::setState(ESP_SDCARD_BUSY);
bool success = ESP_SD::format();
if (success) {
ok_msg = "Formating done";
} else {
hasError = true;
error_msg = "Formating failed";
esp3d_log_e("%s", error_msg.c_str());
}
ESP_SD::releaseFS();
} else {
hasError = true;
error_msg = "SD not available";
esp3d_log_e("%s", error_msg.c_str());
}
if (!dispatchAnswer(endMsg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
}
#endif // SD_DEVICE

View File

@ -0,0 +1,233 @@
/*
ESP720.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(FILESYSTEM_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/filesystem/esp_filesystem.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#include "../esp3d_string.h"
#ifdef FILESYSTEM_TIMESTAMP_FEATURE
#include "../../modules/time/time_service.h"
#endif // FILESYSTEM_TIMESTAMP_FEATURE
#define COMMAND_ID 720
// List ESP Filesystem
//[ESP720]<Root> json=<no> pwd=<admin password>
void ESP3DCommands::ESP720(int cmd_params_pos, ESP3DMessage* msg) {
bool hasError = false;
String error_msg = "Path inccorrect";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
// prepare answer msg
msg->target = msg->origin;
msg->origin = ESP3DClientType::command;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
ESP3DMessage msgInfo;
ESP3DMessageManager::copyMsgInfos(&msgInfo, *msg);
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
tmpstr = "/";
}
if (ESP_FileSystem::accessFS()) {
ESP_File f;
f = ESP_FileSystem::open(tmpstr.c_str(), ESP_FILE_READ);
if (f) {
if (json) {
ok_msg = "{\"cmd\":\"720\",\"status\":\"ok\",\"data\":{\"path\":\"";
ok_msg += tmpstr.c_str();
ok_msg += "\",\"files\":[";
} else {
ok_msg = "Directory on Flash : ";
ok_msg += tmpstr.c_str();
ok_msg += "\n";
}
msg->type = ESP3DMessageType::head;
if (!dispatch(msg, ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
hasError = true;
}
if (!hasError) {
uint nbDirs = 0;
uint nbFiles = 0;
size_t totalSpace = ESP_FileSystem::totalBytes();
size_t usedSpace = ESP_FileSystem::usedBytes();
size_t freeSpace = ESP_FileSystem::freeBytes();
ESP_File sub;
sub = f.openNextFile();
while (sub && !hasError) {
if (sub.isDirectory()) {
ok_msg = "";
nbDirs++;
if (json) {
ok_msg = "";
if (nbDirs > 1) {
ok_msg += ",";
}
ok_msg += "{\"name\":\"";
ok_msg += sub.name();
ok_msg += "\",\"size\":\"-1\"}";
} else {
ok_msg = "[DIR] \t";
ok_msg += sub.name();
}
if (!json) {
ok_msg += "\n";
}
ESP3DMessage* newMsg = ESP3DMessageManager::copyMsgInfos(msgInfo);
if (newMsg) {
newMsg->type = ESP3DMessageType::core;
if (!dispatch(newMsg, ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
hasError = true;
}
} else {
esp3d_log_e("Error creating message");
hasError = true;
}
}
sub.close();
sub = f.openNextFile();
}
f.close();
f = ESP_FileSystem::open(tmpstr.c_str(), ESP_FILE_READ);
// Check files
sub = f.openNextFile();
while (sub && !hasError) {
if (!sub.isDirectory()) {
nbFiles++;
String time = "";
ok_msg = "";
#ifdef FILESYSTEM_TIMESTAMP_FEATURE
time = timeService.getDateTime((time_t)sub.getLastWrite());
#endif // FILESYSTEM_TIMESTAMP_FEATURE
if (json) {
ok_msg = "";
if (nbDirs > 0 || nbFiles > 1) {
ok_msg += ",";
}
ok_msg += "{\"name\":\"";
ok_msg += sub.name();
ok_msg += "\",\"size\":\"";
ok_msg += esp3d_string::formatBytes(sub.size());
if (time.length() > 0) {
ok_msg += "\",\"time\":\"";
ok_msg += time;
}
ok_msg += "\"}";
} else {
ok_msg += " \t ";
ok_msg += sub.name();
ok_msg += " \t";
ok_msg += esp3d_string::formatBytes(sub.size());
ok_msg += " \t";
ok_msg += time;
}
if (!json) {
ok_msg += "\n";
}
ESP3DMessage* newMsg = ESP3DMessageManager::copyMsgInfos(msgInfo);
if (newMsg) {
newMsg->type = ESP3DMessageType::core;
if (!dispatch(newMsg, ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
hasError = true;
}
} else {
esp3d_log_e("Error creating message");
hasError = true;
}
}
sub.close();
sub = f.openNextFile();
}
// end of json
if (json) {
ok_msg = "], \"total\":\"";
ok_msg += esp3d_string::formatBytes(totalSpace);
ok_msg += "\",\"used\":\"";
ok_msg += esp3d_string::formatBytes(usedSpace);
ok_msg += "\",\"occupation\":\"";
if (totalSpace == 0) {
totalSpace = 1;
}
uint occupation = round(100.0 * usedSpace / totalSpace);
if ((occupation < 1) && (usedSpace > 0)) {
occupation = 1;
}
ok_msg += String(occupation);
ok_msg += "\"}}";
} else {
ok_msg = "Files: ";
ok_msg += String(nbFiles);
ok_msg += ", Dirs :";
ok_msg += String(nbDirs);
ok_msg += "\nTotal: ";
ok_msg += esp3d_string::formatBytes(totalSpace);
ok_msg += ", Used: ";
ok_msg += esp3d_string::formatBytes(usedSpace);
ok_msg += ", Available: ";
ok_msg += esp3d_string::formatBytes(freeSpace);
ok_msg += "\n";
}
ESP3DMessage* newMsg = ESP3DMessageManager::copyMsgInfos(msgInfo);
newMsg->type = ESP3DMessageType::tail;
if (!dispatch(newMsg, ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
f.close();
}
} else {
hasError = true;
error_msg = "Invalid directory";
esp3d_log_e("%s", error_msg.c_str());
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
ESP_FileSystem::releaseFS();
} else {
hasError = true;
error_msg = "FS not available";
esp3d_log_e("%s", error_msg.c_str());
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
}
#endif // FILESYSTEM_FEATURE

View File

@ -0,0 +1,116 @@
/*
ESP730.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(FILESYSTEM_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/filesystem/esp_filesystem.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 730
// Action on ESP Filesystem
// rmdir / remove / mkdir / exists / create
//[ESP730]<Action>=<path> json=<no> pwd=<admin password>
void ESP3DCommands::ESP730(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
const char* cmdList[] = {"rmdir=", "remove=", "mkdir=", "exists=", "create="};
uint8_t cmdListSize = sizeof(cmdList) / sizeof(char*);
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
uint8_t i;
for (i = 0; i < cmdListSize && !hasError; i++) {
tmpstr = get_param(msg, cmd_params_pos, cmdList[i]);
if (tmpstr.length() != 0) {
break;
}
}
if (i >= cmdListSize || tmpstr.length() == 0) {
hasError = true;
} else {
if (ESP_FileSystem::accessFS()) {
switch (i) {
case 0:
if (!ESP_FileSystem::rmdir(tmpstr.c_str())) {
hasError = true;
error_msg = "rmdir failed";
}
break;
case 1:
if (!ESP_FileSystem::remove(tmpstr.c_str())) {
hasError = true;
error_msg = "remove failed";
}
break;
case 2:
if (!ESP_FileSystem::mkdir(tmpstr.c_str())) {
hasError = true;
error_msg = "mkdir failed";
}
break;
case 3:
if (ESP_FileSystem::exists(tmpstr.c_str())) {
ok_msg = "yes";
} else {
ok_msg = "no";
}
break;
case 4: {
ESP_File f = ESP_FileSystem::open(tmpstr.c_str(), ESP_FILE_WRITE);
if (f.isOpen()) {
f.close();
} else {
hasError = true;
error_msg = "creation failed";
}
} break;
default:
hasError = true;
error_msg = "Invalid parameters";
break;
}
ESP_FileSystem::releaseFS();
} else {
hasError = true;
error_msg = "Flash not available";
esp3d_log_e("%s", error_msg.c_str());
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // FILESYSTEM_FEATURE

View File

@ -0,0 +1,248 @@
/*
ESP740.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(SD_DEVICE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/filesystem/esp_sd.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#include "../esp3d_string.h"
#ifdef SD_TIMESTAMP_FEATURE
#include "../../modules/time/time_service.h"
#endif // SD_TIMESTAMP_FEATURE
#define COMMAND_ID 740
// List SD Filesystem
//[ESP740]<Root> json=<no> pwd=<admin password>
void ESP3DCommands::ESP740(int cmd_params_pos, ESP3DMessage* msg) {
bool hasError = false;
String error_msg = "Path inccorrect";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
// prepare answer msg
msg->target = msg->origin;
msg->origin = ESP3DClientType::command;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
ESP3DMessage msgInfo;
ESP3DMessageManager::copyMsgInfos(&msgInfo, *msg);
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
tmpstr = "/";
}
if (ESP_SD::accessFS()) {
if (ESP_SD::getState(true) == ESP_SDCARD_NOT_PRESENT) {
hasError = true;
error_msg = "No SD card";
esp3d_log_e("%s", error_msg.c_str());
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
} else {
ESP_SD::setState(ESP_SDCARD_BUSY);
ESP_SDFile f;
f = ESP_SD::open(tmpstr.c_str(), ESP_FILE_READ);
if (f) {
if (json) {
ok_msg = "{\"cmd\":\"740\",\"status\":\"ok\",\"data\":{\"path\":\"";
ok_msg += tmpstr.c_str();
ok_msg += "\",\"files\":[";
} else {
ok_msg = "Directory on SD : ";
ok_msg += tmpstr.c_str();
ok_msg += "\n";
}
msg->type = ESP3DMessageType::head;
if (!dispatch(msg, ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
hasError = true;
}
if (ESP_SD::getState(true) == ESP_SDCARD_NOT_PRESENT) {
hasError = true;
error_msg = "No SD card";
esp3d_log_e("%s", error_msg.c_str());
}
if (!hasError) {
uint nbDirs = 0;
uint nbFiles = 0;
uint64_t totalSpace = ESP_SD::totalBytes();
uint64_t usedSpace = ESP_SD::usedBytes();
uint64_t freeSpace = ESP_SD::freeBytes();
ESP_SDFile sub;
sub = f.openNextFile();
while (sub && !hasError) {
if (sub.isDirectory()) {
ok_msg = "";
nbDirs++;
if (json) {
ok_msg = "";
if (nbDirs > 1) {
ok_msg += ",";
}
ok_msg += "{\"name\":\"";
ok_msg += sub.name();
ok_msg += "\",\"size\":\"-1\"}";
} else {
ok_msg = "[DIR] \t";
ok_msg += sub.name();
}
if (!json) {
ok_msg += "\n";
}
ESP3DMessage* newMsg = ESP3DMessageManager::copyMsgInfos(msgInfo);
if (newMsg) {
newMsg->type = ESP3DMessageType::core;
if (!dispatch(newMsg, ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
hasError = true;
}
} else {
esp3d_log_e("Error creating message");
hasError = true;
}
}
sub.close();
sub = f.openNextFile();
}
f.close();
f = ESP_SD::open(tmpstr.c_str(), ESP_FILE_READ);
// Check files
sub = f.openNextFile();
while (sub && !hasError) {
if (!sub.isDirectory()) {
nbFiles++;
String time = "";
ok_msg = "";
#ifdef SD_TIMESTAMP_FEATURE
time = timeService.getDateTime((time_t)sub.getLastWrite());
#endif // SD_TIMESTAMP_FEATURE
if (json) {
ok_msg = "";
if (nbDirs > 0 || nbFiles > 1) {
ok_msg += ",";
}
ok_msg += "{\"name\":\"";
ok_msg += sub.name();
ok_msg += "\",\"size\":\"";
ok_msg += esp3d_string::formatBytes(sub.size());
if (time.length() > 0) {
ok_msg += "\",\"time\":\"";
ok_msg += time;
}
ok_msg += "\"}";
} else {
ok_msg += " \t ";
ok_msg += sub.name();
ok_msg += " \t";
ok_msg += esp3d_string::formatBytes(sub.size());
ok_msg += " \t";
ok_msg += time;
}
if (!json) {
ok_msg += "\n";
}
ESP3DMessage* newMsg = ESP3DMessageManager::copyMsgInfos(msgInfo);
if (newMsg) {
newMsg->type = ESP3DMessageType::core;
if (!dispatch(newMsg, ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
hasError = true;
}
} else {
esp3d_log_e("Error creating message");
hasError = true;
}
}
sub.close();
sub = f.openNextFile();
}
// end of json
if (json) {
ok_msg = "], \"total\":\"";
ok_msg += esp3d_string::formatBytes(totalSpace);
ok_msg += "\",\"used\":\"";
ok_msg += esp3d_string::formatBytes(usedSpace);
ok_msg += "\",\"occupation\":\"";
if (totalSpace == 0) {
totalSpace = 1;
}
uint occupation = round(100.0 * usedSpace / totalSpace);
if ((occupation < 1) && (usedSpace > 0)) {
occupation = 1;
}
ok_msg += String(occupation);
ok_msg += "\"}}";
} else {
ok_msg = "Files: ";
ok_msg += String(nbFiles);
ok_msg += ", Dirs :";
ok_msg += String(nbDirs);
ok_msg += "\nTotal: ";
ok_msg += esp3d_string::formatBytes(totalSpace);
ok_msg += ", Used: ";
ok_msg += esp3d_string::formatBytes(usedSpace);
ok_msg += ", Available: ";
ok_msg += esp3d_string::formatBytes(freeSpace);
ok_msg += "\n";
}
ESP3DMessage* newMsg = ESP3DMessageManager::copyMsgInfos(msgInfo);
newMsg->type = ESP3DMessageType::tail;
if (!dispatch(newMsg, ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
f.close();
}
} else {
hasError = true;
error_msg = "Invalid directory";
esp3d_log_e("%s", error_msg.c_str());
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
}
ESP_SD::releaseFS();
} else {
hasError = true;
error_msg = "FS not available";
esp3d_log_e("%s", error_msg.c_str());
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
}
#endif // SD_DEVICE

View File

@ -0,0 +1,122 @@
/*
ESP750.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(SD_DEVICE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/filesystem/esp_sd.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 750
// Action on SD Filesystem
// rmdir / remove / mkdir / exists /create
//[ESP750]<Action>=<path> json=<no> pwd=<admin password>
void ESP3DCommands::ESP750(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
const char* cmdList[] = {"rmdir=", "remove=", "mkdir=", "exists=", "create="};
uint8_t cmdListSize = sizeof(cmdList) / sizeof(char*);
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
uint8_t i;
for (i = 0; i < cmdListSize && !hasError; i++) {
tmpstr = get_param(msg, cmd_params_pos, cmdList[i]);
if (tmpstr.length() != 0) {
break;
}
}
if (i >= cmdListSize || tmpstr.length() == 0) {
hasError = true;
} else {
if (ESP_SD::accessFS()) {
if (ESP_SD::getState(true) == ESP_SDCARD_NOT_PRESENT) {
hasError = true;
error_msg = "No SD card";
esp3d_log_e("%s", error_msg.c_str());
} else {
switch (i) {
case 0:
if (!ESP_SD::rmdir(tmpstr.c_str())) {
hasError = true;
error_msg = "rmdir failed";
}
break;
case 1:
if (!ESP_SD::remove(tmpstr.c_str())) {
hasError = true;
error_msg = "remove failed";
}
break;
case 2:
if (!ESP_SD::mkdir(tmpstr.c_str())) {
hasError = true;
error_msg = "mkdir failed";
}
break;
case 3:
if (ESP_SD::exists(tmpstr.c_str())) {
ok_msg = "yes";
} else {
ok_msg = "no";
}
break;
case 4: {
ESP_SDFile f = ESP_SD::open(tmpstr.c_str(), ESP_FILE_WRITE);
if (f.isOpen()) {
f.close();
} else {
hasError = true;
error_msg = "creation failed";
}
} break;
default:
hasError = true;
error_msg = "Invalid parameters";
break;
}
}
ESP_SD::releaseFS();
} else {
hasError = true;
error_msg = "Flash not available";
esp3d_log_e("%s", error_msg.c_str());
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // SD_DEVICE

View File

@ -0,0 +1,234 @@
/*
ESP780.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(GLOBAL_FILESYSTEM_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/filesystem/esp_globalFS.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#include "../esp3d_string.h"
#if defined(SD_TIMESTAMP_FEATURE) || defined(FILESYSTEM_TIMESTAMP_FEATURE)
#include "../../modules/time/time_service.h"
#endif // SD_TIMESTAMP_FEATURE || FILESYSTEM_TIMESTAMP_FEATURE
#define COMMAND_ID 780
// List Global Filesystem
//[ESP780]<Root> json=<no> pwd=<admin password>
void ESP3DCommands::ESP780(int cmd_params_pos, ESP3DMessage* msg) {
bool hasError = false;
String error_msg = "Path inccorrect";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
// prepare answer msg
msg->target = msg->origin;
msg->origin = ESP3DClientType::command;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
ESP3DMessage msgInfo;
ESP3DMessageManager::copyMsgInfos(&msgInfo, *msg);
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
tmpstr = "/";
}
uint8_t fsType = ESP_GBFS::getFSType(tmpstr.c_str());
if (fsType != FS_UNKNOWN && ESP_GBFS::accessFS(fsType)) {
ESP_GBFile f;
f = ESP_GBFS::open(tmpstr.c_str(), ESP_FILE_READ);
if (f) {
if (json) {
ok_msg = "{\"cmd\":\"780\",\"status\":\"ok\",\"data\":{\"path\":\"";
ok_msg += tmpstr.c_str();
ok_msg += "\",\"files\":[";
} else {
ok_msg = "Directory on Global FS : ";
ok_msg += tmpstr.c_str();
ok_msg += "\n";
}
msg->type = ESP3DMessageType::head;
if (!dispatch(msg, ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
hasError = true;
}
if (!hasError) {
uint nbDirs = 0;
uint nbFiles = 0;
size_t totalSpace = ESP_GBFS::totalBytes();
size_t usedSpace = ESP_GBFS::usedBytes();
size_t freeSpace = ESP_GBFS::freeBytes();
ESP_GBFile sub;
sub = f.openNextFile();
while (sub && !hasError) {
if (sub.isDirectory()) {
ok_msg = "";
nbDirs++;
if (json) {
ok_msg = "";
if (nbDirs > 1) {
ok_msg += ",";
}
ok_msg += "{\"name\":\"";
ok_msg += sub.name();
ok_msg += "\",\"size\":\"-1\"}";
} else {
ok_msg = "[DIR] \t";
ok_msg += sub.name();
}
if (!json) {
ok_msg += "\n";
}
ESP3DMessage* newMsg = ESP3DMessageManager::copyMsgInfos(msgInfo);
if (newMsg) {
newMsg->type = ESP3DMessageType::core;
if (!dispatch(newMsg, ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
hasError = true;
}
} else {
esp3d_log_e("Error creating message");
hasError = true;
}
}
sub.close();
sub = f.openNextFile();
}
f.close();
f = ESP_GBFS::open(tmpstr.c_str(), ESP_FILE_READ);
// Check files
sub = f.openNextFile();
while (sub && !hasError) {
if (!sub.isDirectory()) {
nbFiles++;
String time = "";
ok_msg = "";
#ifdef FILESYSTEM_TIMESTAMP_FEATURE
time = timeService.getDateTime((time_t)sub.getLastWrite());
#endif // FILESYSTEM_TIMESTAMP_FEATURE
if (json) {
ok_msg = "";
if (nbDirs > 0 || nbFiles > 1) {
ok_msg += ",";
}
ok_msg += "{\"name\":\"";
ok_msg += sub.name();
ok_msg += "\",\"size\":\"";
ok_msg += esp3d_string::formatBytes(sub.size());
if (time.length() > 0) {
ok_msg += "\",\"time\":\"";
ok_msg += time;
}
ok_msg += "\"}";
} else {
ok_msg += " \t ";
ok_msg += sub.name();
ok_msg += " \t";
ok_msg += esp3d_string::formatBytes(sub.size());
ok_msg += " \t";
ok_msg += time;
}
if (!json) {
ok_msg += "\n";
}
ESP3DMessage* newMsg = ESP3DMessageManager::copyMsgInfos(msgInfo);
if (newMsg) {
newMsg->type = ESP3DMessageType::core;
if (!dispatch(newMsg, ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
hasError = true;
}
} else {
esp3d_log_e("Error creating message");
hasError = true;
}
}
sub.close();
sub = f.openNextFile();
}
// end of json
if (json) {
ok_msg = "], \"total\":\"";
ok_msg += esp3d_string::formatBytes(totalSpace);
ok_msg += "\",\"used\":\"";
ok_msg += esp3d_string::formatBytes(usedSpace);
ok_msg += "\",\"occupation\":\"";
if (totalSpace == 0) {
totalSpace = 1;
}
uint occupation = round(100.0 * usedSpace / totalSpace);
if ((occupation < 1) && (usedSpace > 0)) {
occupation = 1;
}
ok_msg += String(occupation);
ok_msg += "\"}}";
} else {
ok_msg = "Files: ";
ok_msg += String(nbFiles);
ok_msg += ", Dirs :";
ok_msg += String(nbDirs);
ok_msg += "\nTotal: ";
ok_msg += esp3d_string::formatBytes(totalSpace);
ok_msg += ", Used: ";
ok_msg += esp3d_string::formatBytes(usedSpace);
ok_msg += ", Available: ";
ok_msg += esp3d_string::formatBytes(freeSpace);
ok_msg += "\n";
}
ESP3DMessage* newMsg = ESP3DMessageManager::copyMsgInfos(msgInfo);
newMsg->type = ESP3DMessageType::tail;
if (!dispatch(newMsg, ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
f.close();
}
} else {
hasError = true;
error_msg = "Invalid directory";
esp3d_log_e("%s", error_msg.c_str());
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
ESP_GBFS::releaseFS(fsType);
} else {
hasError = true;
error_msg = "FS not available";
esp3d_log_e("%s", error_msg.c_str());
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
}
#endif // GLOBAL_FILESYSTEM_FEATURE

View File

@ -0,0 +1,117 @@
/*
ESP790.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(GLOBAL_FILESYSTEM_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/filesystem/esp_globalFS.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 790
// Action on Global Filesystem
// rmdir / remove / mkdir / exists /create
//[ESP790]<Action>=<path> json=<no> pwd=<admin password>
void ESP3DCommands::ESP790(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
const char* cmdList[] = {"rmdir=", "remove=", "mkdir=", "exists=", "create="};
uint8_t cmdListSize = sizeof(cmdList) / sizeof(char*);
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
uint8_t i;
for (i = 0; i < cmdListSize && !hasError; i++) {
tmpstr = get_param(msg, cmd_params_pos, cmdList[i]);
if (tmpstr.length() != 0) {
break;
}
}
if (i >= cmdListSize || tmpstr.length() == 0) {
hasError = true;
} else {
uint8_t fsType = ESP_GBFS::getFSType(tmpstr.c_str());
if (fsType != FS_UNKNOWN && ESP_GBFS::accessFS(fsType)) {
switch (i) {
case 0:
if (!ESP_GBFS::rmdir(tmpstr.c_str())) {
hasError = true;
error_msg = "rmdir failed";
}
break;
case 1:
if (!ESP_GBFS::remove(tmpstr.c_str())) {
hasError = true;
error_msg = "remove failed";
}
break;
case 2:
if (!ESP_GBFS::mkdir(tmpstr.c_str())) {
hasError = true;
error_msg = "mkdir failed";
}
break;
case 3:
if (ESP_GBFS::exists(tmpstr.c_str())) {
ok_msg = "yes";
} else {
ok_msg = "no";
}
break;
case 4: {
ESP_GBFile f = ESP_GBFS::open(tmpstr.c_str(), ESP_FILE_WRITE);
if (f.isOpen()) {
f.close();
} else {
hasError = true;
error_msg = "creation failed";
}
} break;
default:
hasError = true;
error_msg = "Invalid parameters";
break;
}
ESP_GBFS::releaseFS(fsType);
} else {
hasError = true;
error_msg = "FS not available";
esp3d_log_e("%s", error_msg.c_str());
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // GLOBAL_FILESYSTEM_FEATURE

View File

@ -0,0 +1,299 @@
/*
ESP800.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/network/netconfig.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#ifdef FILESYSTEM_FEATURE
#include "../../modules/filesystem/esp_filesystem.h"
#endif // FILESYSTEM_FEATURE
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE) || defined(BLUETOOTH_FEATURE)
#include "../../modules/network/netconfig.h"
#if defined(WIFI_FEATURE)
#include "../../modules/wifi/wificonfig.h"
#endif // WIFI_FEATURE
#endif // WIFI_FEATURE || ETH_FEATURE || BLUETOOTH_FEATURE
#ifdef HTTP_FEATURE
#include "../../modules/http/http_server.h"
#include "../../modules/websocket/websocket_server.h"
#endif // HTTP_FEATURE
#ifdef TIMESTAMP_FEATURE
#include "../../modules/time/time_service.h"
#endif // TIMESTAMP_FEATURE
#ifdef CAMERA_DEVICE
#include "../../modules/camera/camera.h"
#endif // CAMERA_DEVICE
#define COMMAND_ID 800
// get fw capabilities
// eventually set time with pc time
// output is JSON or plain text according parameter
//[ESP800]json=<no><time=YYYY-MM-DDTHH:mm:ss> <tz=+HH:ss> <version=3.0.0-a11>
//<setup=0/1>
void ESP3DCommands::ESP800(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
msg->target = target;
msg->origin = ESP3DClientType::command;
String timestr = "none";
#if defined(TIMESTAMP_FEATURE)
String timeparam = get_param(msg, cmd_params_pos, "time=");
String tzparam = get_param(msg, cmd_params_pos, "tz=");
#endif // TIMESTAMP_FEATURE
String setupparam = get_param(msg, cmd_params_pos, "setup=");
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
#if defined(TIMESTAMP_FEATURE)
// set time if internet time is not enabled
if (!timeService.isInternetTime()) {
if (tzparam.length() > 0) {
if (!timeService.setTimeZone(tzparam.c_str())) {
// not blocking error
esp3d_log_e("Error setting timezone");
timestr = "Failed to set timezone";
} else {
timestr = "Manual";
}
} else {
timestr = "Not set";
}
if (timeparam.length() > 0) {
if (!timeService.setTime(timeparam.c_str())) {
// not blocking error
esp3d_log_e("Error setting time");
timestr = "Failed to set time";
}
}
} else {
if (timeService.started()) {
timestr = "Auto";
} else {
timestr = "Failed to set";
}
}
#else
timestr = "none";
#endif // ESP3D_TIMESTAMP_FEATURE
if (setupparam.length() > 0) {
if (!ESP3DSettings::writeByte(ESP_SETUP, setupparam == "1" ? 1 : 0)) {
// not blocking error
esp3d_log_e("Error writing setup state");
}
}
if (json) {
tmpstr = "{\"cmd\":\"800\",\"status\":\"ok\",\"data\":{";
} else {
tmpstr = "Capabilities:\n";
}
msg->type = ESP3DMessageType::head;
if (!dispatch(msg, tmpstr.c_str())) {
esp3d_log_e("Error sending response to clients");
return;
}
// list capabilities
tmpstr = "";
#if defined(SHORT_BUILD_VERSION)
tmpstr += SHORT_BUILD_VERSION;
tmpstr += "-";
#endif // SHORT_BUILD_VERSION
tmpstr += FW_VERSION;
// FW version
if (!dispatchKeyValue(json, "FWVersion", tmpstr.c_str(), target, requestId,
false, true)) {
return;
}
// FW Target
tmpstr = ESP3DSettings::GetFirmwareTargetShortName();
if (!dispatchKeyValue(json, "FWTarget", tmpstr.c_str(), target, requestId)) {
return;
}
// FW ID
tmpstr = ESP3DSettings::GetFirmwareTarget();
if (!dispatchKeyValue(json, "FWTargetID", tmpstr.c_str(), target,
requestId)) {
return;
}
// Setup done
tmpstr = ESP3DSettings::readByte(ESP_SETUP) == 0 ? "Enabled" : "Disabled";
if (!dispatchKeyValue(json, "Setup", tmpstr.c_str(), target, requestId)) {
return;
}
// SD connection
if (ESP3DSettings::GetSDDevice() == ESP_DIRECT_SD) {
tmpstr = "direct";
} else if (ESP3DSettings::GetSDDevice() == ESP_SHARED_SD) {
tmpstr = "shared";
} else {
tmpstr = "none";
}
if (!dispatchKeyValue(json, "SDConnection", tmpstr.c_str(), target,
requestId)) {
return;
}
// Serial protocol
#if COMMUNICATION_PROTOCOL == MKS_SERIAL
tmpstr = "MKS";
#endif // COMMUNICATION_PROTOCOL == MKS_SERIAL
#if COMMUNICATION_PROTOCOL == RAW_SERIAL
tmpstr = "Raw";
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL
#if COMMUNICATION_PROTOCOL == SOCKET_SERIAL
tmpstr = "Socket";
#endif // COMMUNICATION_PROTOCOL == SOCKET_SERIAL
if (!dispatchKeyValue(json, "SerialProtocol", tmpstr.c_str(), target,
requestId)) {
return;
}
// Authentication
#if defined(AUTHENTICATION_FEATURE)
tmpstr = "Enabled";
#else
tmpstr = "Disabled";
#endif // AUTHENTICATION_FEATURE
if (!dispatchKeyValue(json, "Authentication", tmpstr.c_str(), target,
requestId)) {
return;
}
// Web Communication
#if (defined(WIFI_FEATURE) || defined(ETH_FEATURE)) && defined(HTTP_FEATURE)
#if defined(ASYNCWEBSERVER_FEATURE)
tmpstr = "Asynchronous";
#else
tmpstr = "Synchronous";
#endif // ASYNCWEBSERVER_FEATURE
if (!dispatchKeyValue(json, "WebCommunication", tmpstr.c_str(), target,
requestId)) {
return;
}
// WebSocket IP
tmpstr = NetConfig::localIP().c_str();
if (!dispatchKeyValue(json, "WebSocketIP", tmpstr.c_str(), target,
requestId)) {
return;
}
// WebSocket Port
#if defined(ASYNCWEBSERVER_FEATURE)
tmpstr = HTTP_Server::port();
#else
tmpstr = websocket_terminal_server.getPort();
#endif // ASYNCWEBSERVER_FEATURE
if (!dispatchKeyValue(json, "WebSocketPort", tmpstr.c_str(), target,
requestId)) {
return;
}
#endif // (WIFI_FEATURE) || ETH_FEATURE) && HTTP_FEATURE)
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE) || defined(BT_FEATURE)
// Hostname
tmpstr = NetConfig::hostname();
if (!dispatchKeyValue(json, "Hostname", tmpstr.c_str(), target, requestId)) {
return;
}
#endif // WIFI_FEATURE|| ETH_FEATURE || BT_FEATURE
#if defined(WIFI_FEATURE)
if (WiFiConfig::started()) {
// WiFi mode
tmpstr = (WiFi.getMode() == WIFI_AP) ? "AP" : "STA";
if (!dispatchKeyValue(json, "WiFiMode", tmpstr.c_str(), target,
requestId)) {
return;
}
}
#endif // WIFI_FEATURE
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
// Update
#if defined(WEB_UPDATE_FEATURE)
if (ESP_FileSystem::max_update_size() != 0) {
tmpstr = "Enabled";
} else {
tmpstr = "Disabled";
}
#else
tmpstr = "Disabled";
#endif // WEB_UPDATE_FEATURE
if (!dispatchKeyValue(json, "WebUpdate", tmpstr.c_str(), target, requestId)) {
return;
}
#endif // WIFI_FEATURE|| ETH_FEATURE
// FS
#if defined(FILESYSTEM_FEATURE)
tmpstr = ESP_FileSystem::FilesystemName();
#else
tmpstr = "none";
#endif // FILESYSTEM_FEATURE
if (!dispatchKeyValue(json, "FlashFileSystem", tmpstr.c_str(), target,
requestId)) {
return;
}
// Host path
tmpstr = ESP3D_HOST_PATH;
if (!dispatchKeyValue(json, "HostPath", tmpstr.c_str(), target, requestId)) {
return;
}
// time server
#if defined(TIMESTAMP_FEATURE)
tmpstr = timestr;
#else
tmpstr = "none";
#endif // TIMESTAMP_FEATURE
if (!dispatchKeyValue(json, "Time", tmpstr.c_str(), target, requestId)) {
return;
}
#ifdef CAMERA_DEVICE
// camera ID
tmpstr = esp3d_camera.GetModel();
if (!dispatchKeyValue(json, "CameraID", tmpstr.c_str(), target, requestId)) {
return;
}
// camera Name
tmpstr = esp3d_camera.GetModelString();
if (!dispatchKeyValue(json, "CameraName", tmpstr.c_str(), target,
requestId)) {
return;
}
#endif // CAMERA_DEVICE
// end of list
if (json) {
if (!dispatch("}}", target, requestId, ESP3DMessageType::tail)) {
esp3d_log_e("Error sending answer to clients");
}
} else {
{
if (!dispatch("ok\n", target, requestId, ESP3DMessageType::tail)) {
esp3d_log_e("Error sending answer to clients");
}
}
}
}

View File

@ -0,0 +1,79 @@
/*
ESP900.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/serial/serial_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 900
// Get state / Set Enable / Disable Serial Communication
//[ESP900]<ENABLE/DISABLE> json=<no> [pwd=<admin password>]
void ESP3DCommands::ESP900(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
bool enabled = hasTag(msg, cmd_params_pos, "ENABLE");
bool disabled = hasTag(msg, cmd_params_pos, "DISABLE");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
if (esp3d_serial_service.started()) {
ok_msg = "ENABLED";
} else {
ok_msg = "DISABLED";
}
ok_msg += " - Serial" + String(esp3d_serial_service.serialIndex());
} else {
if (enabled || disabled) {
if (enabled) {
if (!esp3d_serial_service.begin(ESP_SERIAL_OUTPUT)) {
hasError = true;
error_msg = "Cannot enable serial communication";
}
} else {
esp3d_serial_service.end();
}
} else {
hasError = true;
error_msg = "Invalid parameters";
esp3d_log_e("%s", error_msg.c_str());
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif

View File

@ -0,0 +1,86 @@
/*
ESP901.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/serial/serial_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 901
// Set Serial baudrate
//[ESP901]<baude rate> json=<no> pwd=<admin password>
void ESP3DCommands::ESP901(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
const ESP3DSettingDescription* settingPtr =
ESP3DSettings::getSettingPtr(ESP_BAUD_RATE);
if (settingPtr) {
ok_msg = String(ESP3DSettings::readUint32(ESP_BAUD_RATE));
} else {
hasError = true;
error_msg = "This setting is unknown";
}
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
esp3d_log(
"got %s param for a value of %s, is valid %d", tmpstr.c_str(),
tmpstr.c_str(),
ESP3DSettings::isValidIntegerSetting(tmpstr.toInt(), ESP_BAUD_RATE));
if (ESP3DSettings::isValidIntegerSetting(tmpstr.toInt(), ESP_BAUD_RATE)) {
esp3d_log("Value %s is valid", tmpstr.c_str());
if (!ESP3DSettings::writeUint32(ESP_BAUD_RATE, tmpstr.toInt())) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // COMMUNICATION_PROTOCOL != SOCKET_SERIAL

View File

@ -0,0 +1,79 @@
/*
ESP910.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(BUZZER_DEVICE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/buzzer/buzzer.h"
#include "../esp3d_commands.h"
#include "../esp3d_message.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 910
// Get state / Set Enable / Disable buzzer
//[ESP910]<ENABLE/DISABLE>[pwd=<admin password>]
void ESP3DCommands::ESP910(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
bool enabled = hasTag(msg, cmd_params_pos, "ENABLE");
bool disabled = hasTag(msg, cmd_params_pos, "DISABLE");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
if (esp3d_buzzer.started()) {
ok_msg = "ENABLED";
} else {
ok_msg = "DISABLED";
}
} else {
if (enabled || disabled) {
if (enabled) {
if (!esp3d_buzzer.begin()) {
hasError = true;
error_msg = "Cannot enable buzzer";
}
} else {
esp3d_buzzer.end();
}
} else {
hasError = true;
error_msg = "Invalid parameters";
esp3d_log_e("%s", error_msg.c_str());
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // BUZZER_DEVICE

View File

@ -0,0 +1,95 @@
/*
ESP930.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/serial/serial_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 930
// Set Bridge Serial state which can be ON, OFF, CLOSE
//[ESP930]<state> json=<no> pwd=<admin password>
void ESP3DCommands::ESP930(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
bool enabled = hasTag(msg, cmd_params_pos, "ENABLE");
bool disabled = hasTag(msg, cmd_params_pos, "DISABLE");
bool close = hasTag(msg, cmd_params_pos, "CLOSE");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
if (ESP3DSettings::readByte(ESP_SERIAL_BRIDGE_ON) == 1) {
ok_msg = "ENABLED";
} else {
ok_msg = "DISABLED";
}
ok_msg += " - Serial" + String(serial_bridge_service.serialIndex());
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
if (enabled || disabled || close) {
if (disabled || enabled) {
if (!ESP3DSettings::writeByte(ESP_SERIAL_BRIDGE_ON,
(enabled) ? 1 : 0)) {
hasError = true;
error_msg = "Set failed";
}
}
if (enabled && !serial_bridge_service.started()) {
if (!serial_bridge_service.begin(ESP_SERIAL_BRIDGE_OUTPUT)) {
hasError = true;
error_msg = "Cannot enable serial bridge communication";
}
}
if (close) {
serial_bridge_service.end();
}
} else {
hasError = true;
error_msg = "Invalid parameters";
esp3d_log_e("%s", error_msg.c_str());
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // TELNET_FEATURE

View File

@ -0,0 +1,87 @@
/*
ESP931.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/serial/serial_service.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#define COMMAND_ID 931
// Set Serial bridge baudrate
//[ESP931]<baude rate> json=<no> pwd=<admin password>
void ESP3DCommands::ESP931(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level == ESP3DAuthenticationLevel::guest) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
const ESP3DSettingDescription* settingPtr =
ESP3DSettings::getSettingPtr(ESP_SERIAL_BRIDGE_BAUD);
if (settingPtr) {
ok_msg = String(ESP3DSettings::readUint32(ESP_SERIAL_BRIDGE_BAUD));
} else {
hasError = true;
error_msg = "This setting is unknown";
}
} else {
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#endif // AUTHENTICATION_FEATURE
esp3d_log("got %s param for a value of %s, is valid %d", tmpstr.c_str(),
tmpstr.c_str(),
ESP3DSettings::isValidIntegerSetting(tmpstr.toInt(),
ESP_SERIAL_BRIDGE_BAUD));
if (ESP3DSettings::isValidIntegerSetting(tmpstr.toInt(),
ESP_SERIAL_BRIDGE_BAUD)) {
esp3d_log("Value %s is valid", tmpstr.c_str());
if (!ESP3DSettings::writeUint32(ESP_SERIAL_BRIDGE_BAUD, tmpstr.toInt())) {
hasError = true;
error_msg = "Set value failed";
}
} else {
hasError = true;
error_msg = "Invalid parameter";
}
}
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // ESP_SERIAL_BRIDGE_OUTPUT

View File

@ -19,63 +19,54 @@
*/
#include "../../include/esp3d_config.h"
#include "../../modules/authentication/authentication_service.h"
#include "../commands.h"
#include "../esp3doutput.h"
#include "../settings_esp3d.h"
#include "../esp3d_commands.h"
#include "../esp3d_settings.h"
#if defined(ARDUINO_ARCH_ESP32) && \
(CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32S2 || \
CONFIG_IDF_TARGET_ESP32C3)
#include <esp_efuse.h>
#define COMMANDID 999
#define COMMAND_ID 999
// Set quiet boot if strapping pin is High
//[ESP999]QUIETBOOT [pwd=<admin/user password>]
bool Commands::ESP999(const char* cmd_params, level_authenticate_type auth_type,
ESP3DOutput* output) {
bool noError = true;
bool json = has_tag(cmd_params, "json");
String response;
String parameter;
bool hasParam = false;
int errorCode = 200; // unless it is a server error use 200 as default and
// set error in json instead
#ifdef AUTHENTICATION_FEATURE
if (auth_type == LEVEL_GUEST) {
response = format_response(COMMANDID, json, false,
"Guest user can't use this command");
noError = false;
errorCode = 401;
void ESP3DCommands::ESP999(int cmd_params_pos, ESP3DMessage* msg) {
ESP3DClientType target = msg->origin;
ESP3DRequest requestId = msg->request_id;
(void)requestId;
msg->target = target;
msg->origin = ESP3DClientType::command;
bool hasError = false;
String error_msg = "Invalid parameters";
String ok_msg = "ok";
bool json = hasTag(msg, cmd_params_pos, "json");
bool quietboot = hasTag(msg, cmd_params_pos, "QUIETBOOT");
String tmpstr;
#if defined(AUTHENTICATION_FEATURE)
if (msg->authentication_level != ESP3DAuthenticationLevel::admin) {
msg->authentication_level = ESP3DAuthenticationLevel::not_authenticated;
dispatchAuthenticationError(msg, COMMAND_ID, json);
return;
}
#else
(void)auth_type;
#endif // AUTHENTICATION_FEATURE
if (noError) {
parameter = clean_param(get_param(cmd_params, ""));
// get
if (parameter.length() == 0) {
response = format_response(COMMANDID, json, false, "Incorrect parameter");
noError = false;
} else { // set
tmpstr = get_clean_param(msg, cmd_params_pos);
if (tmpstr.length() == 0) {
hasError = true;
error_msg = "Missing parameter"
} else {
if (quietboot) {
if (esp_efuse_set_rom_log_scheme(ESP_EFUSE_ROM_LOG_ON_GPIO_HIGH) !=
ESP_OK) {
response = format_response(COMMANDID, json, false,
"Set failed(May be already set?)");
noError = false;
} else {
response = format_response(COMMANDID, json, true, "ok");
hasError = true;
error_msg = "Set failed(May be already set?)";
}
}
}
if (json) {
output->printLN(response.c_str());
} else {
if (noError) {
output->printMSG(response.c_str());
} else {
output->printERROR(response.c_str(), errorCode);
hasError = true;
}
}
return noError;
if (!dispatchAnswer(msg, COMMAND_ID, json, hasError,
hasError ? error_msg.c_str() : ok_msg.c_str())) {
esp3d_log_e("Error sending response to clients");
}
}
#endif // defined(ARDUINO_ARCH_ESP32) && (CONFIG_IDF_TARGET_ESP32S3 ||

View File

@ -27,7 +27,7 @@
#include "esp3d.h"
#include "../include/esp3d_config.h"
#include "settings_esp3d.h"
#include "esp3d_settings.h"
#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL || ESP_SERIAL_BRIDGE_OUTPUT
#include "../modules/serial/serial_service.h"
@ -57,7 +57,7 @@
#include "../modules/update/update_service.h"
#endif // SD_UPDATE_FEATURE
#include "../modules/boot_delay/boot_delay.h"
#include "esp3doutput.h"
#include "esp3d_message.h"
bool Esp3D::restart = false;
@ -70,17 +70,16 @@ Esp3D::~Esp3D() { end(); }
// Begin which setup everything
bool Esp3D::begin() {
BootDelay bd;
Hal::begin();
LOG_ESP3D_INIT
ESP3DHal::begin();
ESP3D_LOG_INIT_FN
#if COMMUNICATION_PROTOCOL == SOCKET_SERIAL
Serial2Socket.enable();
#endif // COMMUNICATION_PROTOCOL == SOCKET_SERIAL
// init output
ESP3DOutput::isOutput(ESP_ALL_CLIENTS, true);
bool res = true;
#if defined(CONNECTED_DEVICES_FEATURE)
esp3d_log("Starting Devices feature");
if (!DevicesServices::begin()) {
log_esp3d_e("Error setup connected devices");
esp3d_log_e("Error setup connected devices");
res = false;
}
#endif // CONNECTED_DEVICES_FEATURE
@ -88,14 +87,14 @@ bool Esp3D::begin() {
bd.begin();
#ifdef SD_UPDATE_FEATURE
if (update_service.begin()) {
log_esp3d("Need restart due to update");
esp3d_log("Need restart due to update");
// no need to continue as there was an update
restart_now();
}
#endif // SD_UPDATE_FEATURE
log_esp3d("Mode %d", WiFi.getMode());
if (!Settings_ESP3D::begin()) {
log_esp3d("Need reset settings");
esp3d_log("Mode %d", WiFi.getMode());
if (!ESP3DSettings::begin()) {
esp3d_log("Need reset settings");
reset();
// Restart ESP3D
restart_now();
@ -103,8 +102,8 @@ bool Esp3D::begin() {
// BT do not start automaticaly so should be OK
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
// Serial service
if (!serial_service.begin(ESP_SERIAL_OUTPUT)) {
log_esp3d_e("Error with serial service");
if (!esp3d_serial_service.begin(ESP_SERIAL_OUTPUT)) {
esp3d_log_e("Error with serial service");
res = false;
}
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
@ -112,26 +111,28 @@ bool Esp3D::begin() {
// Serial bridge
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
if (!serial_bridge_service.begin(ESP_SERIAL_BRIDGE_OUTPUT)) {
log_esp3d_e("Error with serial bridge service");
esp3d_log_e("Error with serial bridge service");
res = false;
}
#endif // ESP_SERIAL_BRIDGE_OUTPUT
// Setup Filesystem
#if defined(FILESYSTEM_FEATURE)
esp3d_log("Starting Filesystem feature");
if (!ESP_FileSystem::begin()) {
log_esp3d_e("Error with filesystem service");
esp3d_log_e("Error with filesystem service");
res = false;
}
#endif // FILESYSTEM_FEATURE
#ifdef DISPLAY_DEVICE
esp3d_display.showScreenID(MAIN_SCREEN);
log_esp3d("Main screen");
esp3d_log("Main screen");
#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) {
esp3d_log("Starting Netconfig feature");
if (ESP3DSettings::readByte(ESP_BOOT_RADIO_STATE) == 1) {
if (!NetConfig::begin()) {
log_esp3d_e("Error setup network");
esp3d_log_e("Error setup network");
res = false;
}
}
@ -145,6 +146,7 @@ bool Esp3D::begin() {
esp3d_gcode_host.processFile(ESP_AUTOSTART_SCRIPT_FILE);
#endif // ESP_AUTOSTART_FEATURE
#endif // GCODE_HOST_FEATURE
esp3d_log("Esp3d Started");
_started = true;
return res;
}
@ -159,7 +161,7 @@ void Esp3D::handle() {
restart_now();
}
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
serial_service.handle();
esp3d_serial_service.handle();
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
@ -197,7 +199,7 @@ bool Esp3D::end() {
ESP_FileSystem::end();
#endif // FILESYSTEM_FEATURE
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
serial_service.end();
esp3d_serial_service.end();
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
return true;
@ -207,20 +209,20 @@ bool Esp3D::end() {
bool Esp3D::reset() {
bool resetOk = true;
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
if (!serial_service.reset()) {
if (!esp3d_serial_service.reset()) {
resetOk = false;
log_esp3d_e("Reset serial error");
esp3d_log_e("Reset serial error");
}
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
if (!serial_bridge_service.reset()) {
resetOk = false;
log_esp3d_e("Reset serial bridge error");
esp3d_log_e("Reset serial bridge error");
}
#endif // ESP_SERIAL_BRIDGE_OUTPUT
if (!Settings_ESP3D::reset()) {
log_esp3d_e("Reset settings error");
if (!ESP3DSettings::reset()) {
esp3d_log_e("Reset settings error");
resetOk = false;
}
return resetOk;
@ -235,19 +237,19 @@ void Esp3D::restart_now() {
#if defined(ETH_FEATURE) && defined(ESP3D_ETH_PHY_POWER_PIN)
digitalWrite(ESP3D_ETH_PHY_POWER_PIN, LOW);
#endif // ESP3D_ETH_PHY_POWER_PIN
log_esp3d("Restarting");
esp3d_log("Restarting");
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
if (!serial_service.started()) {
serial_service.begin(ESP_SERIAL_OUTPUT);
if (!esp3d_serial_service.started()) {
esp3d_serial_service.begin(ESP_SERIAL_OUTPUT);
}
serial_service.flush();
esp3d_serial_service.flush();
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
#if defined(FILESYSTEM_FEATURE)
ESP_FileSystem::end();
#endif // FILESYSTEM_FEATURE
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
serial_service.swap();
esp3d_serial_service.swap();
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
ESP.restart();

View File

@ -20,7 +20,7 @@
#include "../include/esp3d_config.h"
#if defined(ESP_BENCHMARK_FEATURE)
#include "benchmark.h"
#include "esp3d_benchmark.h"
#include "../modules/websocket/websocket_server.h"
void report_esp3d(const char * format, ...)
{

View File

@ -0,0 +1,52 @@
/*
esp3d_client_types
Copyright (c) 2022 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#pragma once
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
enum class ESP3DClientType : uint8_t {
no_client = 0,
serial = 1,
usb_serial = 2,
stream = 3,
telnet = 4,
http = 5,
webui_websocket = 6,
websocket = 7,
rendering = 8, // Target only (same as esp_screen)
bluetooth = 9,
socket_serial = 10,
echo_serial = 11, // target only
serial_bridge = 12,
remote_screen = 13, // target only = M117
mks_serial = 14,
command, // origin only
system, // origin only
all_clients
};
#ifdef __cplusplus
} // extern "C"
#endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,230 @@
/*
esp3d_commands.h - ESP3D commands class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef COMMANDS_H
#define COMMANDS_H
#include <Arduino.h>
#include "../modules/authentication/authentication_service.h"
#include "esp3d_message.h"
class ESP3DCommands {
public:
ESP3DCommands();
~ESP3DCommands();
void process(ESP3DMessage* msg);
bool is_esp_command(uint8_t* sbuf, size_t len);
void execute_internal_command(int cmd, int cmd_params_pos, ESP3DMessage* msg);
const char* get_param(ESP3DMessage* msg, uint start, const char* label,
bool* found = nullptr);
const char* get_param(const char* data, uint size, uint start,
const char* label, bool* found = nullptr);
const char* format_response(uint cmdID, bool isjson = false, bool isok = true,
const char* message = "");
bool hasTag(ESP3DMessage* msg, uint start, const char* label);
const char* get_clean_param(ESP3DMessage* msg, uint start);
bool has_param(ESP3DMessage* msg, uint start);
void ESP0(int cmd_params_pos, ESP3DMessage* msg);
#if defined(WIFI_FEATURE)
void ESP100(int cmd_params_pos, ESP3DMessage* msg);
void ESP101(int cmd_params_pos, ESP3DMessage* msg);
#endif // WIFI_FEATURE
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
void ESP102(int cmd_params_pos, ESP3DMessage* msg);
void ESP103(int cmd_params_pos, ESP3DMessage* msg);
#endif // WIFI_FEATURE ||ETH_FEATURE
#if defined(WIFI_FEATURE) || defined(BLUETOOTH_FEATURE) || defined(ETH_FEATURE)
void ESP104(int cmd_params_pos, ESP3DMessage* msg);
#endif // WIFI_FEATURE || BLUETOOTH_FEATURE || ETH_FEATURE
#if defined(WIFI_FEATURE)
void ESP105(int cmd_params_pos, ESP3DMessage* msg);
void ESP106(int cmd_params_pos, ESP3DMessage* msg);
void ESP107(int cmd_params_pos, ESP3DMessage* msg);
void ESP108(int cmd_params_pos, ESP3DMessage* msg);
#endif // WIFI_FEATURE
#if defined(WIFI_FEATURE) || defined(BLUETOOTH_FEATURE) || defined(ETH_FEATURE)
void ESP110(int cmd_params_pos, ESP3DMessage* msg);
#endif // WIFI_FEATURE || BLUETOOTH_FEATURE || ETH_FEATURE
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
void ESP111(int cmd_params_pos, ESP3DMessage* msg);
#endif // WIFI_FEATURE || ETH_FEATURE
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE) || defined(BT_FEATURE)
void ESP112(int cmd_params_pos, ESP3DMessage* msg);
void ESP114(int cmd_params_pos, ESP3DMessage* msg);
void ESP115(int cmd_params_pos, ESP3DMessage* msg);
#endif // WIFI_FEATURE || BLUETOOTH_FEATURE || ETH_FEATURE
#if defined(HTTP_FEATURE)
void ESP120(int cmd_params_pos, ESP3DMessage* msg);
void ESP121(int cmd_params_pos, ESP3DMessage* msg);
#endif // HTTP_FEATURE
#if defined(TELNET_FEATURE)
void ESP130(int cmd_params_pos, ESP3DMessage* msg);
void ESP131(int cmd_params_pos, ESP3DMessage* msg);
#endif // TELNET_FEATURE
#if defined(TIMESTAMP_FEATURE)
void ESP140(int cmd_params_pos, ESP3DMessage* msg);
#endif // TIMESTAMP_FEATURE
void ESP150(int cmd_params_pos, ESP3DMessage* msg);
#if defined(WS_DATA_FEATURE)
void ESP160(int cmd_params_pos, ESP3DMessage* msg);
void ESP161(int cmd_params_pos, ESP3DMessage* msg);
#endif // WS_DATA_FEATURE
#if defined(CAMERA_DEVICE)
void ESP170(int cmd_params_pos, ESP3DMessage* msg);
void ESP171(int cmd_params_pos, ESP3DMessage* msg);
#endif // CAMERA_DEVICE
#if defined(FTP_FEATURE)
void ESP180(int cmd_params_pos, ESP3DMessage* msg);
void ESP181(int cmd_params_pos, ESP3DMessage* msg);
#endif // FTP_FEATURE
#if defined(WEBDAV_FEATURE)
void ESP190(int cmd_params_pos, ESP3DMessage* msg);
void ESP191(int cmd_params_pos, ESP3DMessage* msg);
#endif // WEBDAV_FEATURE
#if defined(SD_DEVICE)
void ESP200(int cmd_params_pos, ESP3DMessage* msg);
#if SD_DEVICE != ESP_SDIO
void ESP202(int cmd_params_pos, ESP3DMessage* msg);
#endif // SD_DEVICE != ESP_SDIO
#ifdef SD_UPDATE_FEATURE
void ESP402(int cmd_params_pos, ESP3DMessage* msg);
#endif // SD_UPDATE_FEATURE
#endif // SD_DEVICE
#ifdef DIRECT_PIN_FEATURE
void ESP201(int cmd_params_pos, ESP3DMessage* msg);
#endif // DIRECT_PIN_FEATURE
#if defined(DISPLAY_DEVICE)
void ESP214(int cmd_params_pos, ESP3DMessage* msg);
#if defined(DISPLAY_TOUCH_DRIVER)
void ESP215(int cmd_params_pos, ESP3DMessage* msg);
#endif // DISPLAY_TOUCH_DRIVER
#endif // DISPLAY_DEVICE
#ifdef SENSOR_DEVICE
void ESP210(int cmd_params_pos, ESP3DMessage* msg);
#endif // SENSOR_DEVICE
void ESP220(int cmd_params_pos, ESP3DMessage* msg);
void ESP290(int cmd_params_pos, ESP3DMessage* msg);
void ESP400(int cmd_params_pos, ESP3DMessage* msg);
void ESP401(int cmd_params_pos, ESP3DMessage* msg);
#if defined(WIFI_FEATURE)
void ESP410(int cmd_params_pos, ESP3DMessage* msg);
#endif // WIFI_FEATURE
void ESP420(int cmd_params_pos, ESP3DMessage* msg);
void ESP444(int cmd_params_pos, ESP3DMessage* msg);
#ifdef MDNS_FEATURE
void ESP450(int cmd_params_pos, ESP3DMessage* msg);
#endif // MDNS_FEATURE
#if defined(AUTHENTICATION_FEATURE)
void ESP500(int cmd_params_pos, ESP3DMessage* msg);
void ESP510(int cmd_params_pos, ESP3DMessage* msg);
void ESP550(int cmd_params_pos, ESP3DMessage* msg);
void ESP555(int cmd_params_pos, ESP3DMessage* msg);
#endif // AUTHENTICATION_FEATURE
#if defined(NOTIFICATION_FEATURE)
void ESP600(int cmd_params_pos, ESP3DMessage* msg);
void ESP610(int cmd_params_pos, ESP3DMessage* msg);
void ESP620(int cmd_params_pos, ESP3DMessage* msg);
#endif // NOTIFICATION_FEATURE
#if defined(GCODE_HOST_FEATURE)
void ESP700(int cmd_params_pos, ESP3DMessage* msg);
void ESP701(int cmd_params_pos, ESP3DMessage* msg);
#endif // GCODE_HOST_FEATURE
#if defined(FILESYSTEM_FEATURE)
void ESP710(int cmd_params_pos, ESP3DMessage* msg);
void ESP720(int cmd_params_pos, ESP3DMessage* msg);
void ESP730(int cmd_params_pos, ESP3DMessage* msg);
#endif // FILESYSTEM_FEATURE
#if defined(SD_DEVICE)
void ESP715(int cmd_params_pos, ESP3DMessage* msg);
void ESP750(int cmd_params_pos, ESP3DMessage* msg);
void ESP740(int cmd_params_pos, ESP3DMessage* msg);
#endif // SD_DEVICE
#if defined(GLOBAL_FILESYSTEM_FEATURE)
void ESP780(int cmd_params_pos, ESP3DMessage* msg);
void ESP790(int cmd_params_pos, ESP3DMessage* msg);
#endif // GLOBAL_FILESYSTEM_FEATURE
void ESP800(int cmd_params_pos, ESP3DMessage* msg);
#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL
void ESP900(int cmd_params_pos, ESP3DMessage* msg);
void ESP901(int cmd_params_pos, ESP3DMessage* msg);
#endif // COMMUNICATION_PROTOCOL != SOCKET_SERIAL
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
void ESP930(int cmd_params_pos, ESP3DMessage* msg);
void ESP931(int cmd_params_pos, ESP3DMessage* msg);
#endif // defined (ESP_SERIAL_BRIDGE_OUTPUT)
#ifdef BUZZER_DEVICE
void ESP910(int cmd_params_pos, ESP3DMessage* msg);
void ESP250(int cmd_params_pos, ESP3DMessage* msg);
#endif // BUZZER_DEVICE
#if defined(ARDUINO_ARCH_ESP32) && \
(CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32S2 || \
CONFIG_IDF_TARGET_ESP32C3)
void ESP999(int cmd_params_pos, ESP3DMessage* msg);
#endif // ARDUINO_ARCH_ESP32
bool dispatch(const char* sbuf, ESP3DClientType target,
ESP3DRequest requestId,
ESP3DMessageType type = ESP3DMessageType::head,
ESP3DClientType origin = ESP3DClientType::command,
ESP3DAuthenticationLevel authentication_level =
ESP3DAuthenticationLevel::guest);
bool dispatch(ESP3DMessage* msg, const char* sbuf);
bool dispatch(ESP3DMessage* msg, uint8_t* sbuf, size_t len);
bool dispatch(ESP3DMessage* msg);
bool dispatchAnswer(ESP3DMessage* msg, uint cmdid, bool json, bool hasError,
const char* answerMsg);
bool dispatchIdValue(bool json, const char* Id, const char* value,
ESP3DClientType target, ESP3DRequest requestId,
bool isFirst = false);
bool dispatchKeyValue(bool json, const char* key, const char* value,
ESP3DClientType target, ESP3DRequest requestId,
bool nested = false, bool isFirst = false);
bool dispatch(uint8_t* sbuf, size_t size, ESP3DClientType target,
ESP3DRequest requestId, ESP3DMessageType type,
ESP3DClientType origin,
ESP3DAuthenticationLevel authentication_level);
bool dispatchSetting(bool json, const char* filter, ESP3DSettingIndex index,
const char* help, const char** optionValues,
const char** optionLabels, uint32_t maxsize,
uint32_t minsize, uint32_t minsize2, uint8_t precision,
const char* unit, bool needRestart,
ESP3DClientType target, ESP3DRequest requestId,
bool isFirst = false);
bool dispatchAuthenticationError(ESP3DMessage* msg, uint cmdid, bool json);
bool formatCommand(char* cmd, size_t len);
bool isRealTimeCommand(char* cmd, size_t len);
ESP3DClientType getOutputClient(bool fromSettings = false);
void setOutputClient(ESP3DClientType output_client) {
_output_client = output_client;
}
private:
ESP3DClientType _output_client;
};
extern ESP3DCommands esp3d_commands;
#endif // COMMANDS_H

View File

@ -34,15 +34,15 @@
#include <esp_task_wdt.h>
#include <soc/rtc_cntl_reg.h>
TaskHandle_t Hal::xHandle = nullptr;
TaskHandle_t ESP3DHal::xHandle = nullptr;
#endif // ARDUINO_ARCH_ESP32
#include "esp3doutput.h"
#include "esp3d_message.h"
uint32_t Hal::_analogRange = 255;
uint32_t Hal::_analogWriteFreq = 1000;
uint32_t ESP3DHal::_analogRange = 255;
uint32_t ESP3DHal::_analogWriteFreq = 1000;
void Hal::pinMode(uint8_t pin, uint8_t mode) {
void ESP3DHal::pinMode(uint8_t pin, uint8_t mode) {
#if defined(ARDUINO_ARCH_ESP8266)
if ((pin == 16) && (mode == INPUT_PULLUP)) {
::pinMode(pin, INPUT_PULLDOWN_16);
@ -52,7 +52,7 @@ void Hal::pinMode(uint8_t pin, uint8_t mode) {
::pinMode(pin, mode);
}
int Hal::analogRead(uint8_t pin) {
int ESP3DHal::analogRead(uint8_t pin) {
#ifdef ARDUINO_ARCH_ESP8266 // only one ADC on ESP8266 A0
(void)pin;
return ::analogRead(A0);
@ -61,7 +61,7 @@ int Hal::analogRead(uint8_t pin) {
#endif
}
bool Hal::analogWrite(uint8_t pin, uint value) {
bool ESP3DHal::analogWrite(uint8_t pin, uint value) {
if (value > (_analogRange - 1)) {
return false;
}
@ -73,13 +73,13 @@ bool Hal::analogWrite(uint8_t pin, uint value) {
#endif // ARDUINO_ARCH_ESP32
return true;
}
void Hal::analogWriteFreq(uint32_t freq) {
void ESP3DHal::analogWriteFreq(uint32_t freq) {
_analogWriteFreq = freq;
#ifdef ARDUINO_ARCH_ESP8266
::analogWriteFreq(_analogWriteFreq);
#endif // ARDUINO_ARCH_ESP8266
}
void Hal::analogRange(uint32_t range) {
void ESP3DHal::analogRange(uint32_t range) {
_analogRange = range;
uint8_t resolution = 0;
switch (_analogRange) {
@ -110,9 +110,9 @@ void Hal::analogRange(uint32_t range) {
}
// Setup
bool Hal::begin() {
bool ESP3DHal::begin() {
#if defined(ARDUINO_ARCH_ESP32) && defined(CAMERA_DEVICE)
log_esp3d("Disable brown out");
esp3d_log("Disable brown out");
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); // disable brownout detector
#endif // ARDUINO_ARCH_ESP32 && CAMERA_DEVICE
// Clear all wifi state
@ -134,10 +134,10 @@ bool Hal::begin() {
}
// End ESP3D
void Hal::end() {}
void ESP3DHal::end() {}
// Watchdog feeder
void Hal::wdtFeed() {
void ESP3DHal::wdtFeed() {
#ifdef ARDUINO_ARCH_ESP8266
ESP.wdtFeed();
#endif // ARDUINO_ARCH_ESP8266
@ -157,7 +157,7 @@ void Hal::wdtFeed() {
#ifndef DISABLE_WDT_ESP3DLIB_TASK
if (xHandle && esp_task_wdt_status(xHandle) == ESP_OK) {
if (esp_task_wdt_reset() != ESP_OK) {
log_esp3d_e("WDT Reset failed");
esp3d_log_e("WDT Reset failed");
}
}
#endif // DISABLE_WDT_ESP3DLIB_TASK
@ -165,7 +165,7 @@ void Hal::wdtFeed() {
}
// wait function
void Hal::wait(uint32_t milliseconds) {
void ESP3DHal::wait(uint32_t milliseconds) {
#if defined(ASYNCWEBSERVER)
uint32_t timeout = millis();
while ((millis() - timeout) < milliseconds) {
@ -179,7 +179,7 @@ void Hal::wait(uint32_t milliseconds) {
#endif // !ASYNCWEBSERVER
}
uint16_t Hal::getChipID() {
uint16_t ESP3DHal::getChipID() {
#ifdef ARDUINO_ARCH_ESP8266
return ESP.getChipId();
#endif // ARDUINO_ARCH_ESP8266
@ -188,7 +188,7 @@ uint16_t Hal::getChipID() {
#endif // ARDUINO_ARCH_ESP32
}
bool Hal::has_temperature_sensor() {
bool ESP3DHal::has_temperature_sensor() {
#ifdef ARDUINO_ARCH_ESP8266
return false;
#endif // ARDUINO_ARCH_ESP8266
@ -202,7 +202,7 @@ bool Hal::has_temperature_sensor() {
#endif // ARDUINO_ARCH_ESP32
}
float Hal::temperature() {
float ESP3DHal::temperature() {
#ifdef ARDUINO_ARCH_ESP8266
return 0.0;
#endif // ARDUINO_ARCH_ESP8266
@ -217,7 +217,7 @@ float Hal::temperature() {
#endif // ARDUINO_ARCH_ESP32
}
bool Hal::is_pin_usable(uint pin) {
bool ESP3DHal::is_pin_usable(uint pin) {
#ifdef ARDUINO_ARCH_ESP8266
if ((pin <= 5) || ((pin >= 12) && (pin <= 16))) {
return true;

View File

@ -1,5 +1,5 @@
/*
hal.h - esp3d hal class
esp3d_hal.h - esp3d hal class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
@ -33,7 +33,7 @@
#endif // ARDUINO_ARCH_ESP32
#include <Arduino.h>
class Hal {
class ESP3DHal {
public:
static bool begin();
static void end();
@ -65,7 +65,7 @@ class Esp3dTimout {
uint64_t getTimeout() { return _timeout; };
private:
uint64_t _start;
uint64_t _timeout;
uint64_t _start = 0;
uint64_t _timeout = 0;
};
#endif //_ESP3D_HAL_H

View File

@ -0,0 +1,169 @@
/*
esp3d_log.cpp - log esp3d functions class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../include/esp3d_config.h"
#if defined(ESP_LOG_FEATURE)
// telnet
#if ESP_LOG_FEATURE == LOG_OUTPUT_TELNET
#include "../modules/telnet/telnet_server.h"
Telnet_Server telnet_log;
#endif // ESP_LOG_FEATURE == LOG_OUTPUT_TELNET
// Websocket
#if ESP_LOG_FEATURE == LOG_OUTPUT_WEBSOCKET
#include "../modules/websocket/websocket_server.h"
WebSocket_Server websocket_log("log");
#endif // ESP_LOG_FEATURE == LOG_OUTPUT_WEBSOCKET
#ifndef LOG_ESP3D_BAUDRATE
#define LOG_ESP3D_BAUDRATE 115200
#endif //~LOG_ESP3D_BAUDRATE
#if defined(ARDUINO_ARCH_ESP8266)
// no need with latest esp8266 core
#define pathToFileName(p) p
#endif // ARDUINO_ARCH_ESP8266
// Serial
#if (ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL0) || \
(ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL1) || \
(ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL2)
#ifndef ESP3DLIB_ENV
#if ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL0
#define LOG_OUTPUT_SERIAL Serial
#endif // LOG_OUTPUT_SERIAL0
#if ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL1
#define LOG_OUTPUT_SERIAL Serial1
#endif // LOG_OUTPUT_SERIAL1
#if ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL2
#define LOG_OUTPUT_SERIAL Serial2
#endif // LOG_OUTPUT_SERIAL2
#else
#define LOG_OUTPUT_SERIAL MYSERIAL1
#endif // ESP3DLIB_ENV
#endif // ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL0 || ESP_LOG_FEATURE ==
// LOG_OUTPUT_SERIAL1 || ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL2
void esp3d_logf(uint8_t level, const char* format, ...) {
#if (((ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL0) || \
(ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL1) || \
(ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL2)) && \
!defined(ESP3DLIB_ENV))
if (!LOG_OUTPUT_SERIAL.availableForWrite()) return;
#endif // ((ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL0) || (ESP_...
#if ESP_LOG_FEATURE == LOG_OUTPUT_TELNET
if (!telnet_log.started() || !telnet_log.isConnected()) {
return;
}
#endif // ESP_LOG_FEATURE == LOG_OUTPUT_TELNET
#if ESP_LOG_FEATURE == LOG_OUTPUT_WEBSOCKET
if (!websocket_log.started()) {
return;
}
#endif // ESP_LOG_FEATURE == LOG_OUTPUT_WEBSOCKET
size_t len = 0;
char default_buffer[64];
char* buffer_ptr = default_buffer;
va_list arg;
va_list copy;
va_start(arg, format);
va_copy(copy, arg);
len = vsnprintf(NULL, 0, format, arg);
va_end(copy);
if (len >= sizeof(default_buffer)) {
buffer_ptr = (char*)malloc((len + 1) * sizeof(char));
if (buffer_ptr == NULL) {
return;
}
}
len = vsnprintf(buffer_ptr, len + 1, format, arg);
#if (((ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL0) || \
(ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL1) || \
(ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL2)) && \
!defined(ESP3DLIB_ENV))
LOG_OUTPUT_SERIAL.write((uint8_t*)buffer_ptr, strlen(buffer_ptr));
LOG_OUTPUT_SERIAL.flush();
#endif // ((ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL0) || (ESP_...
#if ESP_LOG_FEATURE == LOG_OUTPUT_TELNET
telnet_log.writeBytes((uint8_t*)buffer_ptr, strlen(buffer_ptr));
#endif // ESP_LOG_FEATURE == LOG_OUTPUT_TELNET
#if ESP_LOG_FEATURE == LOG_OUTPUT_WEBSOCKET
websocket_log.writeBytes((uint8_t*)buffer_ptr, strlen(buffer_ptr));
#endif // ESP_LOG_FEATURE == LOG_OUTPUT_WEBSOCKET
va_end(arg);
if (buffer_ptr != default_buffer) {
free(buffer_ptr);
}
}
void esp3d_log_init() {
#if (ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL0) || \
(ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL1) || \
(ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL2)
#ifdef ARDUINO_ARCH_ESP8266
LOG_OUTPUT_SERIAL.begin(LOG_ESP3D_BAUDRATE, SERIAL_8N1, SERIAL_FULL,
(ESP_LOG_TX_PIN == -1) ? 1 : ESP_LOG_TX_PIN);
#if ESP_LOG_RX_PIN != -1
LOG_OUTPUT_SERIAL
.pins((ESP_LOG_TX_PIN == -1) ? 1 : ESP_LOG_TX_PIN, ESP_LOG_RX_PIN)
#endif // ESP_LOG_RX_PIN != -1
#endif // ARDUINO_ARCH_ESP8266
#if defined(ARDUINO_ARCH_ESP32)
LOG_OUTPUT_SERIAL.begin(LOG_ESP3D_BAUDRATE, SERIAL_8N1,
ESP_LOG_RX_PIN, ESP_LOG_TX_PIN);
#endif // ARDUINO_ARCH_ESP32
#endif // (ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL0) || (ESP_LOG_FEATURE ==
// LOG_OUTPUT_SERIAL1)||(ESP_LOG_FEATURE == LOG_OUTPUT_SERIAL2)
}
void esp3d_network_log_init() {
#if ESP_LOG_FEATURE == LOG_OUTPUT_TELNET
telnet_log.begin(LOG_ESP3D_OUTPUT_PORT, true);
#endif // ESP_LOG_FEATURE == LOG_OUTPUT_TELNET
#if ESP_LOG_FEATURE == LOG_OUTPUT_WEBSOCKET
websocket_log.begin(LOG_ESP3D_OUTPUT_PORT);
#endif // ESP_LOG_FEATURE == LOG_OUTPUT_WEBSOCKET
}
void esp3d_network_log_handle() {
#if ESP_LOG_FEATURE == LOG_OUTPUT_TELNET
telnet_log.handle();
#endif // ESP_LOG_FEATURE == LOG_OUTPUT_TELNET
#if ESP_LOG_FEATURE == LOG_OUTPUT_WEBSOCKET
websocket_log.handle();
#endif // ESP_LOG_FEATURE == LOG_OUTPUT_WEBSOCKET
}
void esp3d_network_log_end() {
#if ESP_LOG_FEATURE == LOG_OUTPUT_TELNET
telnet_log.end();
#endif // ESP_LOG_FEATURE == LOG_OUTPUT_TELNET
#if ESP_LOG_FEATURE == LOG_OUTPUT_WEBSOCKET
websocket_log.end();
#endif // ESP_LOG_FEATURE == LOG_OUTPUT_WEBSOCKET
}
#endif // ESP_LOG_FEATURE

View File

@ -0,0 +1,78 @@
/*
esp3d_log.h - esp3d log functions
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#pragma once
#include "../include/esp3d_config.h"
#include "../include/esp3d_defines.h"
#if defined(ESP_LOG_FEATURE)
extern void esp3d_logf(uint8_t level, const char* format, ...);
extern void esp3d_network_log_init();
extern void esp3d_network_log_handle();
extern void esp3d_network_log_end();
extern void esp3d_log_init();
#if !defined(ESP3D_DEBUG_LEVEL)
#define ESP3D_DEBUG_LEVEL LOG_LEVEL_NONE
#endif // ESP3D_DEBUG_LEVEL
#if defined(ARDUINO_ARCH_ESP8266)
// no need with latest esp8266 core
#define pathToFileName(p) p
#endif // ARDUINO_ARCH_ESP8266
#if ESP3D_DEBUG_LEVEL >= LOG_LEVEL_VERBOSE
#define esp3d_log(format, ...) \
esp3d_logf(LOG_LEVEL_VERBOSE, "[ESP3D-VERBOSE][%s:%u] %s(): " format "\r\n", \
pathToFileName(__FILE__), __LINE__, __FUNCTION__, ##__VA_ARGS__)
#else
#define esp3d_log(format, ...)
#endif // ESP3D_DEBUG_LEVEL>= LOG_LEVEL_VERBOSE
#if ESP3D_DEBUG_LEVEL >= LOG_LEVEL_DEBUG
#define esp3d_log_d(format, ...) \
esp3d_logf(LOG_LEVEL_DEBUG, "[ESP3D-DEBUG][%s:%u] %s(): " format "\r\n", \
pathToFileName(__FILE__), __LINE__, __FUNCTION__, ##__VA_ARGS__)
#else
#define esp3d_log_d(format, ...)
#endif // ESP3D_DEBUG_LEVEL>= LOG_LEVEL_DEBUG
#if ESP3D_DEBUG_LEVEL >= LOG_LEVEL_ERROR
#define esp3d_log_e(format, ...) \
esp3d_logf(LOG_LEVEL_ERROR, "[ESP3D-ERROR][%s:%u] %s(): " format "\r\n", \
pathToFileName(__FILE__), __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define ESP3D_LOG_INIT_FN esp3d_log_init();
#define ESP3D_LOG_NETWORK_INIT_FN esp3d_network_log_init();
#define ESP3D_LOG_NETWORK_HANDLE_FN esp3d_network_log_handle();
#define ESP3D_LOG_NETWORK_END_FN esp3d_network_log_end();
#else
#define esp3d_log_e(format, ...)
#endif // ESP3D_DEBUG_LEVEL >= LOG_LEVEL_ERROR
#else
#define esp3d_log_e(format, ...)
#define esp3d_log_d(format, ...)
#define esp3d_log(format, ...)
#undef ESP3D_DEBUG_LEVEL
#define ESP3D_LOG_INIT_FN
#define ESP3D_LOG_NETWORK_INIT_FN
#define ESP3D_LOG_NETWORK_HANDLE_FN
#define ESP3D_LOG_NETWORK_END_FN
#endif // ESP_LOG_FEATURE

View File

@ -0,0 +1,158 @@
/*
esp3d_message.cpp - output functions class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// #define ESP_LOG_FEATURE LOG_OUTPUT_SERIAL0
#include "esp3d_message.h"
#include "../include/esp3d_config.h"
ESP3DRequest no_id{.id = 0};
#if defined(ESP_LOG_FEATURE)
int msg_counting = 0;
#endif // ESP_LOG_FEATURE
bool ESP3DMessageManager::deleteMsg(ESP3DMessage* message) {
if (!message) return false;
if (message->data) {
free(message->data);
}
free(message);
message = NULL;
#if defined(ESP_LOG_FEATURE)
esp3d_log("Deletion : Now we have %ld msg", --msg_counting);
#endif // ESP_LOG_FEATURE
return true;
}
ESP3DMessage* ESP3DMessageManager::newMsg() {
ESP3DMessage* newMsgPtr = (ESP3DMessage*)malloc(sizeof(ESP3DMessage));
if (newMsgPtr) {
#if defined(ESP_LOG_FEATURE)
esp3d_log("Creation : Now we have %ld msg", ++msg_counting);
#endif // ESP_LOG_FEATURE
newMsgPtr->data = nullptr;
newMsgPtr->size = 0;
newMsgPtr->origin = ESP3DClientType::no_client;
newMsgPtr->target = ESP3DClientType::all_clients;
newMsgPtr->authentication_level = ESP3DAuthenticationLevel::guest;
newMsgPtr->request_id.id = millis();
newMsgPtr->type = ESP3DMessageType::head;
} else {
esp3d_log_e("Out of memory");
}
return newMsgPtr;
}
ESP3DMessage* ESP3DMessageManager::newMsg(ESP3DRequest requestId) {
ESP3DMessage* newMsgPtr = newMsg();
if (newMsgPtr) {
newMsgPtr->origin = ESP3DClientType::command;
newMsgPtr->request_id = requestId;
}
return newMsgPtr;
}
bool ESP3DMessageManager::copyMsgInfos(ESP3DMessage* newMsgPtr,
ESP3DMessage msg) {
if (!newMsgPtr) {
return false;
}
newMsgPtr->origin = msg.origin;
newMsgPtr->target = msg.target;
newMsgPtr->authentication_level = msg.authentication_level;
newMsgPtr->request_id = msg.request_id;
newMsgPtr->type = msg.type;
return true;
}
ESP3DMessage* ESP3DMessageManager::copyMsgInfos(ESP3DMessage msg) {
ESP3DMessage* newMsgPtr = newMsg();
if (newMsgPtr) {
copyMsgInfos(newMsgPtr, msg);
}
return newMsgPtr;
}
ESP3DMessage* ESP3DMessageManager::copyMsg(ESP3DMessage msg) {
ESP3DMessage* newMsgPtr = newMsg(msg.origin, msg.target, msg.data, msg.size,
msg.authentication_level);
if (newMsgPtr) {
newMsgPtr->request_id = msg.request_id;
newMsgPtr->type = msg.type;
}
return newMsgPtr;
}
ESP3DMessage* ESP3DMessageManager::newMsg(
ESP3DClientType origin, ESP3DClientType target, const uint8_t* data,
size_t length, ESP3DAuthenticationLevel authentication_level) {
ESP3DMessage* newMsgPtr = newMsg(origin, target, authentication_level);
if (newMsgPtr) {
if (!setDataContent(newMsgPtr, data, length)) {
deleteMsg(newMsgPtr);
newMsgPtr = nullptr;
esp3d_log_e("newMsg failed for origin %d, target %d, data %s",
(uint8_t)origin, (uint8_t)target,
data ? (char*)data : "null");
}
}
return newMsgPtr;
}
ESP3DMessage* ESP3DMessageManager::newMsg(
ESP3DClientType origin, ESP3DClientType target,
ESP3DAuthenticationLevel authentication_level) {
ESP3DMessage* newMsgPtr = newMsg();
if (newMsgPtr) {
newMsgPtr->origin = origin;
newMsgPtr->target = target;
newMsgPtr->authentication_level = authentication_level;
}
return newMsgPtr;
}
bool ESP3DMessageManager::setDataContent(ESP3DMessage* msg, const uint8_t* data,
size_t length) {
if (!msg) {
esp3d_log_e("no valid msg container");
return false;
}
if (!data || length == 0) {
esp3d_log_e("no data to set for %d origin, %d target", (uint8_t)msg->origin,
(uint8_t)msg->target);
return false;
}
if (msg->data) {
free(msg->data);
}
// add some security in case data is called as string so add 1 byte for \0
msg->data = (uint8_t*)malloc(sizeof(uint8_t) * (length + 1));
if (msg->data) {
memcpy(msg->data, data, length);
msg->size = length;
msg->data[length] =
'\0'; // add some security in case data is called as string
esp3d_log("Data content set to %s", msg->data);
return true;
}
esp3d_log_e("Out of memory");
return false;
}

View File

@ -0,0 +1,88 @@
/*
esp3d_message.h - output functions class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define ESP_OUTPUT_IP_ADDRESS 0
#define ESP_OUTPUT_STATUS 1
#define ESP_OUTPUT_PROGRESS 2
#define ESP_OUTPUT_STATE 3
#define ESP_STATE_DISCONNECTED 0
#ifndef _ESP3DOUTPUT_H
#define _ESP3DOUTPUT_H
#include "../include/esp3d_config.h"
#ifdef HTTP_FEATURE
#if defined(ARDUINO_ARCH_ESP32)
class WebServer;
#define WEBSERVER WebServer
#endif // ARDUINO_ARCH_ESP32
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WebServer.h>
#define WEBSERVER ESP8266WebServer
#endif // ARDUINO_ARCH_ESP8266
#endif // HTTP_FEATURE
#include "../modules/authentication/authentication_level_types.h"
#include "esp3d_client_types.h"
enum class ESP3DMessageType : uint8_t { head, core, tail, unique };
union ESP3DRequest {
uint id;
uint code;
#ifdef HTTP_FEATURE
WEBSERVER *http_request;
#endif // HTTP_FEATURE
};
extern ESP3DRequest no_id;
struct ESP3DMessage {
uint8_t *data;
size_t size;
ESP3DClientType origin;
ESP3DClientType target;
ESP3DAuthenticationLevel authentication_level;
ESP3DRequest request_id;
ESP3DMessageType type;
};
class ESP3DMessageManager final {
public:
static ESP3DMessage *newMsg();
static ESP3DMessage *newMsg(ESP3DRequest requestId);
static bool deleteMsg(ESP3DMessage *message);
static bool copyMsgInfos(ESP3DMessage *newMsgPtr, ESP3DMessage msg);
static ESP3DMessage *copyMsgInfos(ESP3DMessage msg);
static ESP3DMessage *copyMsg(ESP3DMessage msg);
static ESP3DMessage *newMsg(ESP3DClientType origin, ESP3DClientType target,
const uint8_t *data, size_t length,
ESP3DAuthenticationLevel authentication_level =
ESP3DAuthenticationLevel::guest);
static ESP3DMessage *newMsg(ESP3DClientType origin, ESP3DClientType target,
ESP3DAuthenticationLevel authentication_level =
ESP3DAuthenticationLevel::guest);
static bool setDataContent(ESP3DMessage *msg, const uint8_t *data,
size_t length);
};
#endif //_ESP3DOUTPUT_H

View File

@ -19,11 +19,13 @@
*/
#include "../include/esp3d_config.h"
#ifndef STRINGIFY
#define STRINGIFY(x) #x
#endif // STRINGIFY
#define STRING(x) STRINGIFY(x)
#if defined(ESP_SAVE_SETTINGS)
#include "esp3doutput.h"
#include "settings_esp3d.h"
#include "esp3d_message.h"
#include "esp3d_settings.h"
#if ESP_SAVE_SETTINGS == SETTINGS_IN_EEPROM
#include <EEPROM.h>
@ -53,7 +55,7 @@
#include "../modules/serial/serial_service.h"
// Current Settings Version
#define CURRENT_SETTINGS_VERSION "ESP3D04"
#define CURRENT_SETTINGS_VERSION "ESP3D05"
// boundaries
#define MAX_SENSOR_INTERVAL 60000
@ -65,7 +67,7 @@
#define MIN_BOOT_DELAY 0
#define MIN_NOTIFICATION_TOKEN_LENGTH 0
#define MIN_NOTIFICATION_SETTINGS_LENGTH 0
#define MAX_NOTIFICATION_TOKEN_LENGTH 63
#define MAX_NOTIFICATION_TOKEN_LENGTH 250
#define MAX_NOTIFICATION_SETTINGS_LENGTH 128
#define MAX_SERVER_ADDRESS_LENGTH 128
#define MAX_TIME_ZONE_LENGTH 6
@ -140,16 +142,6 @@
#define DEFAULT_CALIBRATION_DONE "0"
#define DEFAULT_SESSION_TIMEOUT "3"
// Default flags
#define DEFAULT_SERIAL_OUTPUT_FLAG "1"
#define DEFAULT_REMOTE_SCREEN_FLAG "1"
#define DEFAULT_WEBSOCKET_FLAG "1"
#define DEFAULT_TELNET_FLAG "1"
#define DEFAULT_BT_FLAG "1"
#define DEFAULT_SCREEN_FLAG "1"
#define DEFAULT_SERIAL_BRIDGE_FLAG "1"
#ifdef WIFI_FEATURE
// default string values
#define DEFAULT_AP_SSID "ESP3D"
#define DEFAULT_AP_PASSWORD "12345678"
@ -160,10 +152,8 @@
#define DEFAULT_STA_SSID "NETWORK_SSID"
#define DEFAULT_STA_PASSWORD "12345678"
#endif // STATION_WIFI_SSID && STATION_WIFI_PASSWORD
#endif // WIFI_FEATURE
#if defined(BLUETOOTH_FEATURE) || defined(WIFI_FEATURE) || defined(ETH_FEATURE)
#define DEFAULT_HOSTNAME "esp3d"
#endif // BLUETOOTH_FEATURE ||WIFI_FEATURE || ETH_FEATURE
#define DEFAULT_ADMIN_PWD "admin"
#define DEFAULT_USER_PWD "user"
@ -172,7 +162,7 @@
#define DEFAULT_TIME_SERVER2 "time.google.com"
#define DEFAULT_TIME_SERVER3 "0.pool.ntp.org"
#define DEFAULT_SETTINGS_VERSION "ESP3D30"
#define DEFAULT_SETTINGS_VERSION "ESP3D31"
// default IP values
#define DEFAULT_STA_IP_VALUE "192.168.0.254"
@ -185,8 +175,8 @@
#define DEFAULT_AP_MASK_VALUE "255.255.255.0"
#define DEFAULT_AP_DNS_VALUE DEFAULT_AP_IP_VALUE
uint8_t Settings_ESP3D::_FirmwareTarget = 0;
bool Settings_ESP3D::_isverboseboot = false;
uint8_t ESP3DSettings::_FirmwareTarget = 0;
bool ESP3DSettings::_isverboseboot = false;
uint16_t ESP3DSettingsData[] = {ESP_RADIO_MODE,
ESP_STA_PASSWORD,
@ -200,35 +190,28 @@ uint16_t ESP3DSettingsData[] = {ESP_RADIO_MODE,
ESP_STA_MASK_VALUE,
ESP_STA_DNS_VALUE,
ESP_AP_IP_VALUE,
ESP_STA_IP_MODE,
ESP_SETTINGS_VERSION,
ESP_NOTIFICATION_TYPE,
ESP_CALIBRATION,
ESP_AP_CHANNEL,
ESP_BUZZER,
ESP_INTERNET_TIME,
ESP_SERIAL_FLAG,
ESP_HTTP_ON,
ESP_TELNET_ON,
ESP_WEBSOCKET_ON,
ESP_SD_SPEED_DIV,
ESP_SENSOR_TYPE,
ESP_TARGET_FW,
ESP_TIME_IS_DST,
ESP_REMOTE_SCREEN_FLAG,
ESP_SD_MOUNT,
ESP_SESSION_TIMEOUT,
ESP_WEBSOCKET_FLAG,
ESP_SD_CHECK_UPDATE_AT_BOOT,
ESP_SETUP,
ESP_TELNET_FLAG,
ESP_BT_FLAG,
ESP_SCREEN_FLAG,
ESP_FTP_ON,
ESP_AUTO_NOTIFICATION,
ESP_VERBOSE_BOOT,
ESP_WEBDAV_ON,
ESP_SECURE_SERIAL,
ESP_SERIAL_BRIDGE_FLAG,
ESP_SERIAL_BRIDGE_ON,
ESP_HOSTNAME,
ESP_ADMIN_PWD,
@ -270,20 +253,20 @@ const uint8_t SupportedApChannelsSize =
sizeof(SupportedApChannels) / sizeof(uint8_t);
#endif // WIFI_FEATURE
bool Settings_ESP3D::begin() {
bool ESP3DSettings::begin() {
if (GetSettingsVersion() == -1) {
return false;
}
// get target FW
Settings_ESP3D::GetFirmwareTarget(true);
Settings_ESP3D::isVerboseBoot(true);
ESP3DSettings::GetFirmwareTarget(true);
ESP3DSettings::isVerboseBoot(true);
return true;
}
bool Settings_ESP3D::isVerboseBoot(bool fromsettings) {
bool ESP3DSettings::isVerboseBoot(bool fromsettings) {
#if COMMUNICATION_PROTOCOL != MKS_SERIAL
if (fromsettings) {
_isverboseboot = read_byte(ESP_VERBOSE_BOOT);
_isverboseboot = readByte(ESP_VERBOSE_BOOT);
}
#else
_isverboseboot = false;
@ -291,19 +274,19 @@ bool Settings_ESP3D::isVerboseBoot(bool fromsettings) {
return _isverboseboot;
}
uint8_t Settings_ESP3D::GetFirmwareTarget(bool fromsettings) {
uint8_t ESP3DSettings::GetFirmwareTarget(bool fromsettings) {
#if defined(FIXED_FW_TARGET)
(void)fromsettings;
_FirmwareTarget = FIXED_FW_TARGET;
#else
if (fromsettings) {
_FirmwareTarget = read_byte(ESP_TARGET_FW);
_FirmwareTarget = readByte(ESP_TARGET_FW);
}
#endif // #if defined( FIXED_FW_TARGET )
return _FirmwareTarget;
}
uint8_t Settings_ESP3D::GetSDDevice() {
uint8_t ESP3DSettings::GetSDDevice() {
#ifdef SD_DEVICE
return SD_DEVICE_CONNECTION;
#else // !SD_DEVICE
@ -311,7 +294,7 @@ uint8_t Settings_ESP3D::GetSDDevice() {
#endif // SD_DEVICE
}
const char *Settings_ESP3D::GetFirmwareTargetShortName() {
const char *ESP3DSettings::GetFirmwareTargetShortName() {
static String response;
if (_FirmwareTarget == REPETIER) {
@ -330,17 +313,17 @@ const char *Settings_ESP3D::GetFirmwareTargetShortName() {
return response.c_str();
}
uint8_t Settings_ESP3D::read_byte(int pos, bool *haserror) {
uint8_t ESP3DSettings::readByte(int pos, bool *haserror) {
if (haserror) {
*haserror = true;
}
log_esp3d("read_byte %d", pos);
esp3d_log("readByte %d", pos);
uint8_t value = getDefaultByteSetting(pos);
log_esp3d("default value %d", value);
esp3d_log("default value %d", value);
#if ESP_SAVE_SETTINGS == SETTINGS_IN_EEPROM
// check if parameters are acceptable
if ((pos + 1 > EEPROM_SIZE)) {
log_esp3d_e("Error read byte %d", pos);
esp3d_log_e("Error read byte %d", pos);
return value;
}
// read byte
@ -351,7 +334,7 @@ uint8_t Settings_ESP3D::read_byte(int pos, bool *haserror) {
#if ESP_SAVE_SETTINGS == SETTINGS_IN_PREFERENCES
Preferences prefs;
if (!prefs.begin(NAMESPACE, true)) {
log_esp3d_e("Error opening %s", NAMESPACE);
esp3d_log_e("Error opening %s", NAMESPACE);
return value;
}
String p = "P_" + String(pos);
@ -369,17 +352,17 @@ uint8_t Settings_ESP3D::read_byte(int pos, bool *haserror) {
}
// write a flag / byte
bool Settings_ESP3D::write_byte(int pos, const uint8_t value) {
bool ESP3DSettings::writeByte(int pos, const uint8_t value) {
#if ESP_SAVE_SETTINGS == SETTINGS_IN_EEPROM
// check if parameters are acceptable
if (pos + 1 > EEPROM_SIZE) {
log_esp3d_e("Error read byte %d", pos);
esp3d_log_e("Error read byte %d", pos);
return false;
}
EEPROM.begin(EEPROM_SIZE);
EEPROM.write(pos, value);
if (!EEPROM.commit()) {
log_esp3d_e("Error commit %d", pos);
esp3d_log_e("Error commit %d", pos);
return false;
}
EEPROM.end();
@ -387,21 +370,21 @@ bool Settings_ESP3D::write_byte(int pos, const uint8_t value) {
#if ESP_SAVE_SETTINGS == SETTINGS_IN_PREFERENCES
Preferences prefs;
if (!prefs.begin(NAMESPACE, false)) {
log_esp3d_e("Error opening %s", NAMESPACE);
esp3d_log_e("Error opening %s", NAMESPACE);
return false;
}
String p = "P_" + String(pos);
uint8_t r = prefs.putChar(p.c_str(), value);
prefs.end();
if (r == 0) {
log_esp3d_e("Error commit %s", p.c_str());
esp3d_log_e("Error commit %s", p.c_str());
return false;
}
#endif // SETTINGS_IN_PREFERENCES
return true;
}
bool Settings_ESP3D::is_string(const char *s, uint len) {
bool ESP3DSettings::is_string(const char *s, uint len) {
for (uint p = 0; p < len; p++) {
if (!isPrintable(char(s[p]))) {
return false;
@ -413,16 +396,16 @@ bool Settings_ESP3D::is_string(const char *s, uint len) {
// read a string
// a string is multibyte + \0, this is won't work if 1 char is multibyte like
// chinese char
const char *Settings_ESP3D::read_string(int pos, bool *haserror) {
const char *ESP3DSettings::readString(int pos, bool *haserror) {
const ESP3DSettingDescription *query = getSettingPtr(pos);
if (haserror) {
*haserror = true;
}
if (!query) {
log_esp3d_e("Error unknow entry %d", pos);
esp3d_log_e("Error unknow entry %d", pos);
return "";
}
uint8_t size_max = query->size;
size_t size_max = query->size;
#if ESP_SAVE_SETTINGS == SETTINGS_IN_EEPROM
static char *byte_buffer = NULL;
@ -433,17 +416,17 @@ const char *Settings_ESP3D::read_string(int pos, bool *haserror) {
}
// check if parameters are acceptable
if (pos + size_max + 1 > EEPROM_SIZE) {
log_esp3d_e("Error read string %d", pos);
esp3d_log_e("Error read string %d", pos);
return "";
}
byte_buffer = (char *)malloc(size_max + 1);
if (!byte_buffer) {
log_esp3d_e("Error mem read string %d", pos);
esp3d_log_e("Error mem read string %d", pos);
return "";
}
EEPROM.begin(EEPROM_SIZE);
byte b = 1; // non zero for the while loop below
int i = 0;
size_t i = 0;
// read until max size is reached or \0 is found
while (i < size_max && b != 0) {
@ -469,7 +452,7 @@ const char *Settings_ESP3D::read_string(int pos, bool *haserror) {
static String res;
if (!prefs.begin(NAMESPACE, true)) {
log_esp3d_e("Error opening %s", NAMESPACE);
esp3d_log_e("Error opening %s", NAMESPACE);
return "";
}
String p = "P_" + String(pos);
@ -481,7 +464,7 @@ const char *Settings_ESP3D::read_string(int pos, bool *haserror) {
prefs.end();
if (res.length() > size_max) {
log_esp3d_e("String too long %d vs %d", res.length(), size_max);
esp3d_log_e("String too long %d vs %d", res.length(), size_max);
res = res.substring(0, size_max - 1);
}
@ -494,38 +477,38 @@ const char *Settings_ESP3D::read_string(int pos, bool *haserror) {
}
// write a string (array of byte with a 0x00 at the end)
bool Settings_ESP3D::write_string(int pos, const char *byte_buffer) {
int size_buffer = strlen(byte_buffer);
bool ESP3DSettings::writeString(int pos, const char *byte_buffer) {
size_t size_buffer = strlen(byte_buffer);
const ESP3DSettingDescription *query = getSettingPtr(pos);
if (!query) {
log_esp3d_e("Error unknow entry %d", pos);
esp3d_log_e("Error unknow entry %d", pos);
return false;
}
uint8_t size_max = query->size;
size_t size_max = query->size;
// check if parameters are acceptable
if (size_max == 0) {
log_esp3d_e("Error unknow entry %d", pos);
esp3d_log_e("Error unknow entry %d", pos);
return false;
}
if (size_max < size_buffer) {
log_esp3d_e("Error string too long %d, %d", pos, size_buffer);
esp3d_log_e("Error string too long %d, %d", pos, size_buffer);
return false;
}
#if ESP_SAVE_SETTINGS == SETTINGS_IN_EEPROM
if (pos + size_buffer + 1 > EEPROM_SIZE || byte_buffer == NULL) {
log_esp3d_e("Error write string %d", pos);
esp3d_log_e("Error write string %d", pos);
return false;
}
// copy the value(s)
EEPROM.begin(EEPROM_SIZE);
for (int i = 0; i < size_buffer; i++) {
for (size_t i = 0; i < size_buffer; i++) {
EEPROM.write(pos + i, byte_buffer[i]);
}
// 0 terminal
EEPROM.write(pos + size_buffer, 0x00);
if (!EEPROM.commit()) {
log_esp3d_e("Error commit %d", pos);
esp3d_log_e("Error commit %d", pos);
return false;
}
EEPROM.end();
@ -533,14 +516,14 @@ bool Settings_ESP3D::write_string(int pos, const char *byte_buffer) {
#if ESP_SAVE_SETTINGS == SETTINGS_IN_PREFERENCES
Preferences prefs;
if (!prefs.begin(NAMESPACE, false)) {
log_esp3d_e("Error opening %s", NAMESPACE);
esp3d_log_e("Error opening %s", NAMESPACE);
return false;
}
String p = "P_" + String(pos);
uint8_t r = prefs.putString(p.c_str(), byte_buffer);
prefs.end();
if (r != size_buffer) {
log_esp3d_e("Error commit %s", p.c_str());
esp3d_log_e("Error commit %s", p.c_str());
return false;
}
#endif // SETTINGS_IN_PREFERENCES
@ -548,7 +531,7 @@ bool Settings_ESP3D::write_string(int pos, const char *byte_buffer) {
}
// read a uint32
uint32_t Settings_ESP3D::read_uint32(int pos, bool *haserror) {
uint32_t ESP3DSettings::readUint32(int pos, bool *haserror) {
if (haserror) {
*haserror = true;
}
@ -557,7 +540,7 @@ uint32_t Settings_ESP3D::read_uint32(int pos, bool *haserror) {
// check if parameters are acceptable
uint8_t size_buffer = sizeof(uint32_t);
if (pos + size_buffer > EEPROM_SIZE) {
log_esp3d_e("Error read int %d", pos);
esp3d_log_e("Error read int %d", pos);
return res;
}
uint8_t i = 0;
@ -572,7 +555,7 @@ uint32_t Settings_ESP3D::read_uint32(int pos, bool *haserror) {
#if ESP_SAVE_SETTINGS == SETTINGS_IN_PREFERENCES
Preferences prefs;
if (!prefs.begin(NAMESPACE, true)) {
log_esp3d_e("Error opening %s", NAMESPACE);
esp3d_log_e("Error opening %s", NAMESPACE);
return res;
}
String p = "P_" + String(pos);
@ -590,22 +573,22 @@ uint32_t Settings_ESP3D::read_uint32(int pos, bool *haserror) {
}
// read an IP
uint32_t Settings_ESP3D::read_IP(int pos, bool *haserror) {
return read_uint32(pos, haserror);
uint32_t ESP3DSettings::read_IP(int pos, bool *haserror) {
return readUint32(pos, haserror);
}
// read an IP
String Settings_ESP3D::read_IP_String(int pos, bool *haserror) {
return _IpToString(read_uint32(pos, haserror));
String ESP3DSettings::readIPString(int pos, bool *haserror) {
return _IpToString(readUint32(pos, haserror));
}
// write a uint32
bool Settings_ESP3D::write_uint32(int pos, const uint32_t value) {
bool ESP3DSettings::writeUint32(int pos, const uint32_t value) {
#if ESP_SAVE_SETTINGS == SETTINGS_IN_EEPROM
uint8_t size_buffer = sizeof(uint32_t);
// check if parameters are acceptable
if (pos + size_buffer > EEPROM_SIZE) {
log_esp3d_e("Error invalid entry %d", pos);
esp3d_log_e("Error invalid entry %d", pos);
return false;
}
EEPROM.begin(EEPROM_SIZE);
@ -614,7 +597,7 @@ bool Settings_ESP3D::write_uint32(int pos, const uint32_t value) {
EEPROM.write(pos + i, ((uint8_t *)(&value))[i]);
}
if (!EEPROM.commit()) {
log_esp3d_e("Error commit %d", pos);
esp3d_log_e("Error commit %d", pos);
return false;
}
EEPROM.end();
@ -622,27 +605,22 @@ bool Settings_ESP3D::write_uint32(int pos, const uint32_t value) {
#if ESP_SAVE_SETTINGS == SETTINGS_IN_PREFERENCES
Preferences prefs;
if (!prefs.begin(NAMESPACE, false)) {
log_esp3d_e("Error opening %s", NAMESPACE);
esp3d_log_e("Error opening %s", NAMESPACE);
return false;
}
String p = "P_" + String(pos);
uint8_t r = prefs.putUInt(p.c_str(), value);
prefs.end();
if (r == 0) {
log_esp3d_e("Error commit %s", p.c_str());
esp3d_log_e("Error commit %s", p.c_str());
return false;
}
#endif // SETTINGS_IN_PREFERENCES
return true;
}
// write a IP
bool Settings_ESP3D::write_IP(int pos, const uint32_t value) {
return write_uint32(pos, value);
}
// clear all entries
bool Settings_ESP3D::reset(bool networkonly) {
bool ESP3DSettings::reset(bool networkonly) {
uint nb_settings = sizeof(ESP3DSettingsData) / sizeof(uint16_t);
for (uint j = 0; j < nb_settings; j++) {
uint16_t i = ESP3DSettingsData[j];
@ -654,31 +632,31 @@ bool Settings_ESP3D::reset(bool networkonly) {
if (query) {
switch (query->type) {
case ESP3DSettingType::string_t:
if (!Settings_ESP3D::write_string(i, query->default_val)) {
log_esp3d_e("Error reset string %d to %s", i, query->default_val);
if (!ESP3DSettings::writeString(i, query->default_val)) {
esp3d_log_e("Error reset string %d to %s", i, query->default_val);
return false;
}
break;
case ESP3DSettingType::byte_t:
if (!Settings_ESP3D::write_byte(
if (!ESP3DSettings::writeByte(
i, (uint8_t)strtoul(query->default_val, NULL, 0))) {
log_esp3d_e("Error reset byte %d to %s", i, query->default_val);
esp3d_log_e("Error reset byte %d to %s", i, query->default_val);
return false;
}
break;
case ESP3DSettingType::integer_t:
case ESP3DSettingType::ip_t:
if (!Settings_ESP3D::write_uint32(i, getDefaultIntegerSetting(i))) {
log_esp3d_e("Error reset uint32 %d to %s", i, query->default_val);
if (!ESP3DSettings::writeUint32(i, getDefaultIntegerSetting(i))) {
esp3d_log_e("Error reset uint32 %d to %s", i, query->default_val);
return false;
}
break;
default:
log_esp3d_e("Error unknow entry %d", i);
esp3d_log_e("Error unknow entry %d", i);
break;
}
} else {
log_esp3d_e("Error unknow entry %d", i);
esp3d_log_e("Error unknow entry %d", i);
}
}
return true;
@ -688,27 +666,28 @@ bool Settings_ESP3D::reset(bool networkonly) {
// * -1 means no version detected
// * 00 / 01 Not used
// * 03 and up is version
int8_t Settings_ESP3D::GetSettingsVersion() {
int8_t ESP3DSettings::GetSettingsVersion() {
int8_t v = -1;
String version = Settings_ESP3D::read_string(ESP_SETTINGS_VERSION);
if (!Settings_ESP3D::isValidStringSetting(version.c_str(),
ESP_SETTINGS_VERSION)) {
log_esp3d_e("Invalid Settings Version %s expected %s", version.c_str(),
String version = ESP3DSettings::readString(ESP_SETTINGS_VERSION);
if (!ESP3DSettings::isValidStringSetting(version.c_str(),
ESP_SETTINGS_VERSION)) {
esp3d_log_e("Invalid Settings Version %s, should be %s", version.c_str(),
DEFAULT_SETTINGS_VERSION);
return v;
}
v = version.substring(5).toInt();
log_esp3d("Settings Version %d", v);
esp3d_log("Settings Version %d", v);
return v;
}
// write a IP from string
bool Settings_ESP3D::write_IP_String(int pos, const char *value) {
return write_uint32(pos, _stringToIP(value));
bool ESP3DSettings::writeIPString(int pos, const char *value) {
return writeUint32(pos, _stringToIP(value));
}
// Helper to convert IP string to int
uint32_t Settings_ESP3D::_stringToIP(const char *s) {
uint32_t ESP3DSettings::_stringToIP(const char *s) {
uint32_t ip_int = 0;
IPAddress ipaddr;
if (ipaddr.fromString(s)) {
@ -718,13 +697,13 @@ uint32_t Settings_ESP3D::_stringToIP(const char *s) {
}
// Helper to convert int to IP string
String Settings_ESP3D::_IpToString(uint32_t ip_int) {
String ESP3DSettings::_IpToString(uint32_t ip_int) {
static IPAddress ipaddr;
ipaddr = ip_int;
return ipaddr.toString();
}
const char *Settings_ESP3D::TargetBoard() {
const char *ESP3DSettings::TargetBoard() {
#ifdef ARDUINO_ARCH_ESP32
#if CONFIG_IDF_TARGET_ESP32
#define TYPE_BOARD "ESP32"
@ -749,8 +728,8 @@ const char *Settings_ESP3D::TargetBoard() {
#endif // ESP_SAVE_SETTINGS
bool Settings_ESP3D::isValidIPStringSetting(const char *value,
ESP3DSettingIndex settingElement) {
bool ESP3DSettings::isValidIPStringSetting(const char *value,
ESP3DSettingIndex settingElement) {
const ESP3DSettingDescription *settingPtr = getSettingPtr(settingElement);
if (!settingPtr) {
return false;
@ -786,8 +765,8 @@ bool Settings_ESP3D::isValidIPStringSetting(const char *value,
}
return true;
}
bool Settings_ESP3D::isValidStringSetting(const char *value,
ESP3DSettingIndex settingElement) {
bool ESP3DSettings::isValidStringSetting(const char *value,
ESP3DSettingIndex settingElement) {
const ESP3DSettingDescription *settingPtr = getSettingPtr(settingElement);
if (!(settingPtr->type == ESP3DSettingType::string_t)) {
return false;
@ -798,7 +777,7 @@ bool Settings_ESP3D::isValidStringSetting(const char *value,
return false;
}
// only printable char allowed
for (uint i = 0; i < strlen(value); i++) {
for (size_t i = 0; i < strlen(value); i++) {
if (!isPrintable(value[i])) {
return false;
}
@ -809,7 +788,7 @@ bool Settings_ESP3D::isValidStringSetting(const char *value,
break;
case ESP_HOSTNAME:
// only letter and digit
for (uint i = 0; i < strlen(value); i++) {
for (size_t i = 0; i < strlen(value); i++) {
char c = value[i];
if (!(isdigit(c) || isalpha(c) || c == '-')) {
return false;
@ -826,7 +805,7 @@ bool Settings_ESP3D::isValidStringSetting(const char *value,
break;
case ESP_ADMIN_PWD:
case ESP_USER_PWD:
for (uint i = 0; i < strlen(value); i++) {
for (size_t i = 0; i < strlen(value); i++) {
if (value[i] == ' ') { // no space allowed
return false;
}
@ -851,7 +830,7 @@ bool Settings_ESP3D::isValidStringSetting(const char *value,
return true; // no more check for time server
break;
case ESP_TIME_ZONE:
if (len != settingPtr->size - 1) {
if (len != settingPtr->size) {
return false;
}
for (uint8_t i = 0; i < SupportedTimeZonesSize; i++) {
@ -866,8 +845,8 @@ bool Settings_ESP3D::isValidStringSetting(const char *value,
}
return false;
}
bool Settings_ESP3D::isValidIntegerSetting(uint32_t value,
ESP3DSettingIndex settingElement) {
bool ESP3DSettings::isValidIntegerSetting(uint32_t value,
ESP3DSettingIndex settingElement) {
const ESP3DSettingDescription *settingPtr = getSettingPtr(settingElement);
if (!settingPtr) {
return false;
@ -876,6 +855,7 @@ bool Settings_ESP3D::isValidIntegerSetting(uint32_t value,
return false;
}
switch (settingElement) {
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
case ESP_SERIAL_BRIDGE_BAUD:
case ESP_BAUD_RATE:
for (uint8_t i = 0; i < SupportedBaudListSize; i++) {
@ -884,6 +864,7 @@ bool Settings_ESP3D::isValidIntegerSetting(uint32_t value,
}
}
break;
#endif //#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
case ESP_WEBDAV_PORT:
case ESP_HTTP_PORT:
case ESP_TELNET_PORT:
@ -918,8 +899,8 @@ bool Settings_ESP3D::isValidIntegerSetting(uint32_t value,
}
return false;
}
bool Settings_ESP3D::isValidByteSetting(uint8_t value,
ESP3DSettingIndex settingElement) {
bool ESP3DSettings::isValidByteSetting(uint8_t value,
ESP3DSettingIndex settingElement) {
const ESP3DSettingDescription *settingPtr = getSettingPtr(settingElement);
if (!settingPtr) {
return false;
@ -931,20 +912,13 @@ bool Settings_ESP3D::isValidByteSetting(uint8_t value,
switch (settingElement) {
case ESP_BUZZER:
case ESP_INTERNET_TIME:
case ESP_SERIAL_FLAG:
case ESP_REMOTE_SCREEN_FLAG:
case ESP_HTTP_ON:
case ESP_TELNET_ON:
case ESP_WEBSOCKET_ON:
case ESP_WEBSOCKET_FLAG:
case ESP_SD_CHECK_UPDATE_AT_BOOT:
case ESP_SETUP:
case ESP_TELNET_FLAG:
case ESP_BT_FLAG:
case ESP_SCREEN_FLAG:
case ESP_FTP_ON:
case ESP_AUTO_NOTIFICATION:
case ESP_SERIAL_BRIDGE_FLAG:
case ESP_SERIAL_BRIDGE_ON:
case ESP_VERBOSE_BOOT:
case ESP_WEBDAV_ON:
@ -961,7 +935,8 @@ bool Settings_ESP3D::isValidByteSetting(uint8_t value,
if (value == ESP_NO_NOTIFICATION || value == ESP_PUSHOVER_NOTIFICATION ||
value == ESP_EMAIL_NOTIFICATION || value == ESP_LINE_NOTIFICATION ||
value == ESP_TELEGRAM_NOTIFICATION ||
value == ESP_IFTTT_NOTIFICATION) {
value == ESP_IFTTT_NOTIFICATION ||
value == ESP_HOMEASSISTANT_NOTIFICATION) {
return true;
}
@ -974,7 +949,7 @@ bool Settings_ESP3D::isValidByteSetting(uint8_t value,
value == ESP_AP_SETUP
#endif // WIFI_FEATURE
#if defined(ETH_FEATURE)
|| value == ESP_ETH
|| value == ESP_ETH_STA
#endif // ETH_FEATURE
#if defined(BLUETOOTH_FEATURE)
|| value == ESP_BT
@ -1055,7 +1030,7 @@ bool Settings_ESP3D::isValidByteSetting(uint8_t value,
return true;
}
uint32_t Settings_ESP3D::getDefaultIntegerSetting(
uint32_t ESP3DSettings::getDefaultIntegerSetting(
ESP3DSettingIndex settingElement) {
const ESP3DSettingDescription *query = getSettingPtr(settingElement);
if (query) {
@ -1065,13 +1040,14 @@ uint32_t Settings_ESP3D::getDefaultIntegerSetting(
if (query->type == ESP3DSettingType::integer_t)
return (uint32_t)strtoul(query->default_val, NULL, 0);
else {
log_esp3d_e("Error invalid type %d for %d", query->type, settingElement);
esp3d_log_e("Error invalid type %d for %d", (uint)query->type,
(uint)settingElement);
}
}
return 0;
}
const char *Settings_ESP3D::getDefaultStringSetting(
const char *ESP3DSettings::getDefaultStringSetting(
ESP3DSettingIndex settingElement) {
const ESP3DSettingDescription *query = getSettingPtr(settingElement);
if (query) {
@ -1079,27 +1055,28 @@ const char *Settings_ESP3D::getDefaultStringSetting(
query->type == ESP3DSettingType::ip_t)
return query->default_val;
else {
log_esp3d_e("Error invalid type %d for %d", query->type, settingElement);
esp3d_log_e("Error invalid type %d for %d", (uint)query->type,
(uint)settingElement);
}
}
return NULL;
}
uint8_t Settings_ESP3D::getDefaultByteSetting(
ESP3DSettingIndex settingElement) {
log_esp3d("getDefaultByteSetting %d", settingElement);
uint8_t ESP3DSettings::getDefaultByteSetting(ESP3DSettingIndex settingElement) {
esp3d_log("getDefaultByteSetting %d", (uint)settingElement);
const ESP3DSettingDescription *query = getSettingPtr(settingElement);
if (query) {
log_esp3d("getDefaultByteSetting found");
esp3d_log("getDefaultByteSetting found");
if (query->type == ESP3DSettingType::byte_t) {
log_esp3d("getDefaultByteSetting is %s", query->default_val);
esp3d_log("getDefaultByteSetting is %s", query->default_val);
return (uint8_t)strtoul(query->default_val, NULL, 0);
} else {
log_esp3d_e("Error invalid type %d for %d", query->type, settingElement);
esp3d_log_e("Error invalid type %d for %d", (uint)query->type,
(uint)settingElement);
}
} else {
log_esp3d_e("Error unknow entry %d", settingElement);
esp3d_log_e("Error unknow entry %d", (uint)settingElement);
}
return 0;
}
@ -1107,7 +1084,7 @@ uint8_t Settings_ESP3D::getDefaultByteSetting(
// get the description of a setting
// Unlike esp32 esp8266 does not have lot of memory so we hard code the
// settings and just generate one setting on demand
const ESP3DSettingDescription *Settings_ESP3D::getSettingPtr(
const ESP3DSettingDescription *ESP3DSettings::getSettingPtr(
const ESP3DSettingIndex index) {
static ESP3DSettingDescription setting;
memset(&setting, 0, sizeof(setting));
@ -1121,23 +1098,16 @@ const ESP3DSettingDescription *Settings_ESP3D::getSettingPtr(
case ESP_AP_CHANNEL:
case ESP_BUZZER:
case ESP_INTERNET_TIME:
case ESP_SERIAL_FLAG:
case ESP_HTTP_ON:
case ESP_TELNET_ON:
case ESP_WEBSOCKET_ON:
case ESP_SD_SPEED_DIV:
case ESP_SENSOR_TYPE:
case ESP_TARGET_FW:
case ESP_TIME_IS_DST:
case ESP_REMOTE_SCREEN_FLAG:
case ESP_SD_MOUNT:
case ESP_SESSION_TIMEOUT:
case ESP_WEBSOCKET_FLAG:
case ESP_SD_CHECK_UPDATE_AT_BOOT:
case ESP_SETUP:
case ESP_TELNET_FLAG:
case ESP_BT_FLAG:
case ESP_SCREEN_FLAG:
case ESP_FTP_ON:
case ESP_AUTO_NOTIFICATION:
case ESP_VERBOSE_BOOT:
@ -1145,7 +1115,6 @@ const ESP3DSettingDescription *Settings_ESP3D::getSettingPtr(
case ESP_SECURE_SERIAL:
case ESP_BOOT_RADIO_STATE:
case ESP_STA_FALLBACK_MODE:
case ESP_SERIAL_BRIDGE_FLAG:
case ESP_SERIAL_BRIDGE_ON:
case ESP_STA_IP_MODE:
setting.type = ESP3DSettingType::byte_t; // byte
@ -1210,23 +1179,16 @@ const ESP3DSettingDescription *Settings_ESP3D::getSettingPtr(
case ESP_AP_CHANNEL:
case ESP_BUZZER:
case ESP_INTERNET_TIME:
case ESP_SERIAL_FLAG:
case ESP_HTTP_ON:
case ESP_TELNET_ON:
case ESP_WEBSOCKET_ON:
case ESP_SD_SPEED_DIV:
case ESP_SENSOR_TYPE:
case ESP_TARGET_FW:
case ESP_TIME_IS_DST:
case ESP_REMOTE_SCREEN_FLAG:
case ESP_SD_MOUNT:
case ESP_SESSION_TIMEOUT:
case ESP_WEBSOCKET_FLAG:
case ESP_SD_CHECK_UPDATE_AT_BOOT:
case ESP_SETUP:
case ESP_TELNET_FLAG:
case ESP_BT_FLAG:
case ESP_SCREEN_FLAG:
case ESP_FTP_ON:
case ESP_AUTO_NOTIFICATION:
case ESP_VERBOSE_BOOT:
@ -1234,7 +1196,6 @@ const ESP3DSettingDescription *Settings_ESP3D::getSettingPtr(
case ESP_SECURE_SERIAL:
case ESP_BOOT_RADIO_STATE:
case ESP_STA_FALLBACK_MODE:
case ESP_SERIAL_BRIDGE_FLAG:
case ESP_SERIAL_BRIDGE_ON:
case ESP_STA_IP_MODE:
setting.size = 1; // 1 byte
@ -1292,6 +1253,8 @@ const ESP3DSettingDescription *Settings_ESP3D::getSettingPtr(
// like chinese chars
break;
case ESP_NOTIFICATION_TOKEN1:
setting.size = 250; // 250 bytes
break;
case ESP_NOTIFICATION_TOKEN2:
setting.size = 63; // 63 bytes
break;
@ -1337,9 +1300,6 @@ const ESP3DSettingDescription *Settings_ESP3D::getSettingPtr(
case ESP_INTERNET_TIME:
setting.default_val = DEFAULT_INTERNET_TIME;
break;
case ESP_SERIAL_FLAG:
setting.default_val = DEFAULT_SERIAL_OUTPUT_FLAG;
break;
case ESP_HTTP_ON:
setting.default_val = DEFAULT_HTTP_ON;
break;
@ -1358,36 +1318,18 @@ const ESP3DSettingDescription *Settings_ESP3D::getSettingPtr(
case ESP_TARGET_FW:
setting.default_val = STRING(DEFAULT_FW);
break;
case ESP_TIME_IS_DST:
setting.default_val = DEFAULT_TIME_DST;
break;
case ESP_REMOTE_SCREEN_FLAG:
setting.default_val = DEFAULT_REMOTE_SCREEN_FLAG;
break;
case ESP_SD_MOUNT:
setting.default_val = DEFAULT_SD_MOUNT;
break;
case ESP_SESSION_TIMEOUT:
setting.default_val = DEFAULT_SESSION_TIMEOUT;
break;
case ESP_WEBSOCKET_FLAG:
setting.default_val = DEFAULT_WEBSOCKET_FLAG;
break;
case ESP_SD_CHECK_UPDATE_AT_BOOT:
setting.default_val = DEFAULT_SD_CHECK_UPDATE_AT_BOOT;
break;
case ESP_SETUP:
setting.default_val = DEFAULT_SETUP;
break;
case ESP_TELNET_FLAG:
setting.default_val = DEFAULT_TELNET_FLAG;
break;
case ESP_BT_FLAG:
setting.default_val = DEFAULT_BT_FLAG;
break;
case ESP_SCREEN_FLAG:
setting.default_val = DEFAULT_SCREEN_FLAG;
break;
case ESP_FTP_ON:
setting.default_val = DEFAULT_FTP_ON;
break;
@ -1409,9 +1351,6 @@ const ESP3DSettingDescription *Settings_ESP3D::getSettingPtr(
case ESP_STA_FALLBACK_MODE:
setting.default_val = DEFAULT_STA_FALLBACK_MODE;
break;
case ESP_SERIAL_BRIDGE_FLAG:
setting.default_val = DEFAULT_SERIAL_BRIDGE_FLAG;
break;
case ESP_SERIAL_BRIDGE_ON:
setting.default_val = DEFAULT_SERIAL_BRIDGE_ON;
break;
@ -1513,13 +1452,13 @@ const ESP3DSettingDescription *Settings_ESP3D::getSettingPtr(
setting.default_val = DEFAULT_WEBDAV_PORT;
break;
default:
log_esp3d_e("Invalid setting %d", index);
esp3d_log_e("Invalid setting %d", index);
return NULL;
break;
}
log_esp3d("Got index %d:", setting.index);
log_esp3d("type %d:", setting.type);
log_esp3d("size %d:", setting.size);
log_esp3d("default_val %s:", setting.default_val);
esp3d_log("Got index %d:", setting.index);
esp3d_log("type %d:", setting.type);
esp3d_log("size %d:", setting.size);
esp3d_log("default_val %s:", setting.default_val);
return &setting;
}

View File

@ -1,6 +1,6 @@
/*
settings_esp3d.h - settings esp3d functions class
esp3d_settings.h - settings esp3d functions class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
@ -57,19 +57,18 @@ extern const uint8_t SupportedApChannels[];
extern const uint8_t SupportedApChannelsSize;
#endif // WIFI_FEATURE
class Settings_ESP3D {
class ESP3DSettings {
public:
static bool begin();
static uint8_t read_byte(int pos, bool *haserror = NULL);
static uint32_t read_uint32(int pos, bool *haserror = NULL);
static uint8_t readByte(int pos, bool *haserror = NULL);
static uint32_t readUint32(int pos, bool *haserror = NULL);
static uint32_t read_IP(int pos, bool *haserror = NULL);
static String read_IP_String(int pos, bool *haserror = NULL);
static const char *read_string(int pos, bool *haserror = NULL);
static bool write_byte(int pos, const uint8_t value);
static bool write_string(int pos, const char *byte_buffer);
static bool write_uint32(int pos, const uint32_t value);
static bool write_IP(int pos, const uint32_t value);
static bool write_IP_String(int pos, const char *value);
static String readIPString(int pos, bool *haserror = NULL);
static const char *readString(int pos, bool *haserror = NULL);
static bool writeByte(int pos, const uint8_t value);
static bool writeString(int pos, const char *byte_buffer);
static bool writeUint32(int pos, const uint32_t value);
static bool writeIPString(int pos, const char *value);
static bool reset(bool networkonly = false);
static int8_t GetSettingsVersion();
static uint8_t GetFirmwareTarget(bool fromsettings = false);

View File

@ -60,7 +60,7 @@ void update_hash(uint8_t* data, size_t len, uint8_t* hash_buffer,
static bool reverse = false;
reverse = !reverse;
int start_index = reverse ? hash_size : 0;
for (int i = 0; i < hash_size; i++) {
for (uint8_t i = 0; i < hash_size; i++) {
int idx =
reverse ? (start_index - i) % hash_size : (start_index + i) % hash_size;
if (i >= len) {
@ -147,4 +147,35 @@ const char* esp3d_string::getContentType(const char* filename) {
return "text/plain";
}
return "application/octet-stream";
}
// tool function to avoid string corrupt JSON files
const char* esp3d_string::encodeString(const char* s) {
static String tmp;
tmp = s;
while (tmp.indexOf("'") != -1) {
tmp.replace("'", "&#39;");
}
while (tmp.indexOf("\"") != -1) {
tmp.replace("\"", "&#34;");
}
if (tmp == "") {
tmp = " ";
}
return tmp.c_str();
}
// helper to format size to readable string
const char* esp3d_string::formatBytes(uint64_t bytes) {
static String res;
if (bytes < 1024) {
res = String((uint16_t)bytes) + " B";
} else if (bytes < (1024 * 1024)) {
res = String((float)(bytes / 1024.0), 2) + " KB";
} else if (bytes < (1024 * 1024 * 1024)) {
res = String((float)(bytes / 1024.0 / 1024.0), 2) + " MB";
} else {
res = String((float)(bytes / 1024.0 / 1024.0 / 1024.0), 2) + " GB";
}
return res.c_str();
}

View File

@ -25,6 +25,8 @@ namespace esp3d_string {
const char* getTimeString(time_t time, bool isGMT);
const char* generateUUID(const char* seed);
const char* getContentType(const char* filename);
const char* encodeString(const char* s);
const char* formatBytes(uint64_t bytes);
} // namespace esp3d_string
#endif //_ESP3D_STRING_H

View File

@ -1,847 +0,0 @@
/*
esp3d output.h - output functions class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// #define ESP_LOG_FEATURE LOG_OUTPUT_SERIAL0
#include "esp3doutput.h"
#include "../include/esp3d_config.h"
#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL || defined(ESP_SERIAL_BRIDGE_OUTPUT)
#include "../modules/serial/serial_service.h"
#endif // COMMUNICATION_PROTOCOL != SOCKET_SERIAL
#if COMMUNICATION_PROTOCOL == SOCKET_SERIAL
#include "../modules/serial2socket/serial2socket.h"
#endif // COMMUNICATION_PROTOCOL == SOCKET_SERIAL
#include "settings_esp3d.h"
#if defined(HTTP_FEATURE) || defined(WS_DATA_FEATURE)
#include "../modules/websocket/websocket_server.h"
#endif // HTTP_FEATURE || WS_DATA_FEATURE
#if defined(BLUETOOTH_FEATURE)
#include "../modules/bluetooth/BT_service.h"
#endif // BLUETOOTH_FEATURE
#if defined(TELNET_FEATURE)
#include "../modules/telnet/telnet_server.h"
#endif // TELNET_FEATURE
#if COMMUNICATION_PROTOCOL == MKS_SERIAL
#include "../modules/mks/mks_service.h"
#endif // COMMUNICATION_PROTOCOL == MKS_SERIAL
#if defined(GCODE_HOST_FEATURE)
#include "../modules/gcode_host/gcode_host.h"
#endif // GCODE_HOST_FEATURE
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || \
COMMUNICATION_PROTOCOL == MKS_SERIAL || \
COMMUNICATION_PROTOCOL == SOCKET_SERIAL
uint8_t ESP3DOutput::_serialoutputflags = 0;
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL || COMMUNICATION_PROTOCOL == SOCKET_SERIAL
#if defined(HAS_DISPLAY) || defined(HAS_SERIAL_DISPLAY)
uint8_t ESP3DOutput::_remotescreenoutputflags = 0;
#endif // HAS_DISPLAY || HAS_SERIAL_DISPLAY
#if defined(WS_DATA_FEATURE)
uint8_t ESP3DOutput::_websocketoutputflags = DEFAULT_WEBSOCKET_FLAG;
#endif // WS_DATA_FEATURE
#if defined(TELNET_FEATURE)
uint8_t ESP3DOutput::_telnetoutputflags = 0;
#endif // TELNET_FEATURE
#if defined(DISPLAY_DEVICE)
uint8_t ESP3DOutput::_screenoutputflags = DEFAULT_SCREEN_FLAG;
#endif // DISPLAY_DEVICE
#if defined(BLUETOOTH_FEATURE)
uint8_t ESP3DOutput::_BToutputflags = DEFAULT_BT_FLAG;
#endif // BLUETOOTH_FEATURE
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
uint8_t ESP3DOutput::_serialBridgeoutputflags = DEFAULT_SERIAL_BRIDGE_FLAG;
#endif // ESP_SERIAL_BRIDGE_OUTPUT
#if defined(HTTP_FEATURE)
#include "../modules/http/http_server.h"
#if defined(ARDUINO_ARCH_ESP32)
#include <WebServer.h>
#endif // ARDUINO_ARCH_ESP32
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WebServer.h>
#endif // ARDUINO_ARCH_ESP8266
#endif // HTTP_FEATURE
#if defined(DISPLAY_DEVICE)
#include "../modules/display/display.h"
#endif // DISPLAY_DEVICE
const uint8_t activeClients[] = {
#if !(defined(HAS_DISPLAY) || defined(HAS_SERIAL_DISPLAY)) && \
(COMMUNICATION_PROTOCOL == RAW_SERIAL || \
COMMUNICATION_PROTOCOL == MKS_SERIAL)
ESP_SERIAL_CLIENT,
#endif // ESP_SERIAL_CLIENT
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
ESP_SERIAL_BRIDGE_CLIENT,
#endif // ESP_SERIAL_BRIDGE_OUTPUT
#if defined(TELNET_FEATURE)
ESP_TELNET_CLIENT,
#endif // TELNET_FEATURE
#if defined(HTTP_FEATURE)
ESP_HTTP_CLIENT,
#endif // HTTP_FEATURE
#if defined(HAS_DISPLAY) || defined(HAS_SERIAL_DISPLAY)
ESP_REMOTE_SCREEN_CLIENT,
#endif // defined(HAS_DISPLAY) || defined(HAS_SERIAL_DISPLAY)
#if defined(BLUETOOTH_FEATURE)
ESP_BT_CLIENT,
#endif // BLUETOOTH_FEATURE
#if defined(DISPLAY_DEVICE)
ESP_SCREEN_CLIENT,
#endif // DISPLAY_DEVICE
#if defined(WS_DATA_FEATURE)
ESP_WEBSOCKET_CLIENT,
#endif // WS_DATA_FEATURE
#if COMMUNICATION_PROTOCOL == SOCKET_SERIAL
ESP_SOCKET_SERIAL_CLIENT,
ESP_ECHO_SERIAL_CLIENT,
#endif // COMMUNICATION_PROTOCOL == SOCKET_SERIAL
ESP_NO_CLIENT};
// tool function to avoid string corrupt JSON files
const char *ESP3DOutput::encodeString(const char *s) {
static String tmp;
tmp = s;
while (tmp.indexOf("'") != -1) {
tmp.replace("'", "&#39;");
}
while (tmp.indexOf("\"") != -1) {
tmp.replace("\"", "&#34;");
}
if (tmp == "") {
tmp = " ";
}
return tmp.c_str();
}
void ESP3DOutput::toScreen(uint8_t output_type, const char *s) {
switch (output_type) {
case ESP_OUTPUT_IP_ADDRESS:
#ifdef DISPLAY_DEVICE
esp3d_display.updateIP();
#endif // DISPLAY_DEVICE
break;
case ESP_OUTPUT_STATUS:
#ifdef DISPLAY_DEVICE
esp3d_display.setStatus(s);
#endif // DISPLAY_DEVICE
break;
case ESP_OUTPUT_PROGRESS:
#ifdef DISPLAY_DEVICE
esp3d_display.progress((uint8_t)atoi(s));
#endif // DISPLAY_DEVICE
break;
case ESP_OUTPUT_STATE:
#ifdef DISPLAY_DEVICE
switch (atoi(s)) {
case ESP_STATE_DISCONNECTED:
esp3d_display.setStatus("Disconnected");
break;
default:
break;
}
#endif // DISPLAY_DEVICE
break;
default:
(void)s;
break;
}
}
// constructor
ESP3DOutput::ESP3DOutput(uint8_t client) {
_client = client;
#ifdef HTTP_FEATURE
_code = 200;
_headerSent = false;
_footerSent = false;
_webserver = nullptr;
#endif // HTTP_FEATURE
}
uint8_t ESP3DOutput::client(uint8_t client) {
if (client != 0) {
_client = client;
}
return _client;
}
#ifdef HTTP_FEATURE
// constructor
ESP3DOutput::ESP3DOutput(WEBSERVER *webserver) {
_client = ESP_HTTP_CLIENT;
_code = 200;
_headerSent = false;
_footerSent = false;
_webserver = webserver;
}
#endif // HTTP_FEATURE
// destructor
ESP3DOutput::~ESP3DOutput() { flush(); }
bool ESP3DOutput::isOutput(uint8_t flag, bool fromsettings) {
if (fromsettings) {
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || \
COMMUNICATION_PROTOCOL == MKS_SERIAL || \
COMMUNICATION_PROTOCOL == SOCKET_SERIAL
_serialoutputflags = Settings_ESP3D::read_byte(ESP_SERIAL_FLAG);
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
_serialBridgeoutputflags =
Settings_ESP3D::read_byte(ESP_SERIAL_BRIDGE_FLAG);
#endif // ESP_SERIAL_BRIDGE_OUTPUT
#if defined(HAS_DISPLAY) || defined(HAS_SERIAL_DISPLAY)
_remotescreenoutputflags =
Settings_ESP3D::read_byte(ESP_REMOTE_SCREEN_FLAG);
#endif // defined(HAS_DISPLAY) || defined(HAS_SERIAL_DISPLAY)
#if defined(WS_DATA_FEATURE)
_websocketoutputflags = Settings_ESP3D::read_byte(ESP_WEBSOCKET_FLAG);
#endif // WS_DATA_FEATURE
#if defined(TELNET_FEATURE)
_telnetoutputflags = Settings_ESP3D::read_byte(ESP_TELNET_FLAG);
#endif // TELNET_FEATURE
#if defined(DISPLAY_DEVICE)
_screenoutputflags = Settings_ESP3D::read_byte(ESP_SCREEN_FLAG);
#endif // DISPLAY_DEVICE
#if defined(BLUETOOTH_FEATURE)
_BToutputflags = Settings_ESP3D::read_byte(ESP_BT_FLAG);
#endif // BLUETOOTH_FEATURE
}
switch (flag) {
case ESP_ECHO_SERIAL_CLIENT:
case ESP_SERIAL_CLIENT:
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || \
COMMUNICATION_PROTOCOL == MKS_SERIAL || \
COMMUNICATION_PROTOCOL == SOCKET_SERIAL
return _serialoutputflags;
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
return 0;
case ESP_SERIAL_BRIDGE_CLIENT:
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
return _serialBridgeoutputflags;
#endif // ESP_SERIAL_BRIDGE_OUTPUT
return 0;
case ESP_REMOTE_SCREEN_CLIENT:
#if defined(HAS_DISPLAY) || defined(HAS_SERIAL_DISPLAY)
return _remotescreenoutputflags;
#endif // defined(HAS_DISPLAY) || defined(HAS_SERIAL_DISPLAY)
return 0;
case ESP_WEBSOCKET_CLIENT:
#if defined(WS_DATA_FEATURE)
return _websocketoutputflags;
#endif // WS_DATA_FEATURE
return 0;
case ESP_TELNET_CLIENT:
#if defined(TELNET_FEATURE)
return _telnetoutputflags;
#endif // TELNET_FEATURE
return 0;
case ESP_SCREEN_CLIENT:
#if defined(DISPLAY_DEVICE)
return _screenoutputflags;
#endif // DISPLAY_DEVICE
return 0;
case ESP_BT_CLIENT:
#if defined(BLUETOOTH_FEATURE)
return _BToutputflags;
#endif // BLUETOOTH_FEATURE
return 0;
default:
return true;
}
}
size_t ESP3DOutput::dispatch(const uint8_t *sbuf, size_t len,
uint8_t ignoreClient) {
log_esp3d("Dispatch %d chars from client %d and ignore %d", len, _client,
ignoreClient);
#if defined(GCODE_HOST_FEATURE)
if (!(_client == ESP_STREAM_HOST_CLIENT ||
ESP_STREAM_HOST_CLIENT == ignoreClient)) {
log_esp3d("Dispatch to gcode host");
esp3d_gcode_host.push(sbuf, len);
}
#endif // GCODE_HOST_FEATURE
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
if (!(_client == ESP_SERIAL_CLIENT || ESP_SERIAL_CLIENT == ignoreClient)) {
if (isOutput(ESP_SERIAL_CLIENT)) {
#if COMMUNICATION_PROTOCOL == MKS_SERIAL
log_esp3d("Dispatch to gcode frame");
MKSService::sendGcodeFrame((const char *)sbuf);
#else
log_esp3d("Dispatch to serial service");
serial_service.write(sbuf, len);
#endif // COMMUNICATION_PROTOCOL == MKS_SERIAL
}
}
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
#if COMMUNICATION_PROTOCOL == SOCKET_SERIAL
if (!(_client == ESP_SOCKET_SERIAL_CLIENT ||
ESP_SOCKET_SERIAL_CLIENT == ignoreClient)) {
log_esp3d("Dispatch to serial socket client %d is %d, or is %d", _client,
ESP_SOCKET_SERIAL_CLIENT, ignoreClient);
Serial2Socket.push(sbuf, len);
}
if (!(_client == ESP_ECHO_SERIAL_CLIENT ||
ESP_ECHO_SERIAL_CLIENT == ignoreClient ||
_client == ESP_SOCKET_SERIAL_CLIENT)) {
log_esp3d("Dispatch to echo serial");
MYSERIAL1.write(sbuf, len);
}
#endif // COMMUNICATION_PROTOCOL == SOCKET_SERIAL
#if defined(HTTP_FEATURE) // no need to block it never
if (!((_client == ESP_WEBSOCKET_TERMINAL_CLIENT) ||
(_client == ESP_HTTP_CLIENT) ||
(ESP_WEBSOCKET_TERMINAL_CLIENT == ignoreClient) ||
(ESP_HTTP_CLIENT == ignoreClient))) {
if (websocket_terminal_server) {
log_esp3d("Dispatch websocket terminal");
websocket_terminal_server.write(sbuf, len);
}
}
#endif // HTTP_FEATURE
#if defined(BLUETOOTH_FEATURE)
if (!(_client == ESP_BT_CLIENT || ESP_BT_CLIENT == ignoreClient)) {
if (isOutput(ESP_BT_CLIENT) && bt_service.started()) {
log_esp3d("Dispatch to bt");
bt_service.write(sbuf, len);
}
}
#endif // BLUETOOTH_FEATURE
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
if (!(_client == ESP_SERIAL_BRIDGE_CLIENT ||
ESP_SERIAL_BRIDGE_CLIENT == ignoreClient)) {
if (isOutput(ESP_SERIAL_BRIDGE_CLIENT) && serial_bridge_service.started()) {
log_esp3d("Dispatch to serial bridge");
serial_bridge_service.write(sbuf, len);
}
}
#endif // ESP_SERIAL_BRIDGE_OUTPUT
#if defined(TELNET_FEATURE)
if (!(_client == ESP_TELNET_CLIENT || ESP_TELNET_CLIENT == ignoreClient)) {
if (isOutput(ESP_TELNET_CLIENT) && telnet_server.started()) {
log_esp3d("Dispatch to telnet");
telnet_server.write(sbuf, len);
}
}
#endif // TELNET_FEATURE
#if defined(WS_DATA_FEATURE)
if (!(_client == ESP_WEBSOCKET_CLIENT ||
ESP_WEBSOCKET_CLIENT == ignoreClient)) {
if (isOutput(ESP_WEBSOCKET_CLIENT) && websocket_data_server.started()) {
log_esp3d("Dispatch to websocket data server");
websocket_data_server.write(sbuf, len);
}
}
#endif // WS_DATA_FEATURE
return len;
}
// Flush
void ESP3DOutput::flush() {
if (!isOutput(_client)) {
return;
}
switch (_client) {
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
case ESP_SERIAL_CLIENT:
serial_service.flush();
break;
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
#ifdef HTTP_FEATURE
case ESP_HTTP_CLIENT:
if (_webserver) {
if (_headerSent && !_footerSent) {
_webserver->sendContent("");
_footerSent = true;
}
}
break;
#endif // HTTP_FEATURE
#if defined(BLUETOOTH_FEATURE)
case ESP_BT_CLIENT:
bt_service.flush();
break;
#endif // BLUETOOTH_FEATURE
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
case ESP_SERIAL_BRIDGE_CLIENT:
serial_bridge_service.flush();
break;
#endif // ESP_SERIAL_BRIDGE_OUTPUT
#if defined(TELNET_FEATURE)
case ESP_TELNET_CLIENT:
telnet_server.flush();
break;
#endif // TELNET_FEATURE
case ESP_ALL_CLIENTS:
// do nothing because there are side effects
break;
default:
break;
}
}
size_t ESP3DOutput::printLN(const char *s) {
if (!isOutput(_client)) {
return 0;
}
switch (_client) {
case ESP_HTTP_CLIENT:
if (strlen(s) > 0) {
println(s);
return strlen(s) + 1;
} else {
println(" ");
return strlen(s) + 2;
}
return 0;
case ESP_TELNET_CLIENT:
print(s);
println("\r");
return strlen(s) + 2;
default:
break;
}
return println(s);
}
size_t ESP3DOutput::printMSGLine(const char *s) {
if (_client == ESP_ALL_CLIENTS) {
// process each client one by one
log_esp3d("PrintMSG to all clients");
for (uint8_t c = 0; c < sizeof(activeClients); c++) {
if (activeClients[c]) {
log_esp3d("Sending PrintMSG to client %d", activeClients[c]);
_client = activeClients[c];
printMSG(s);
}
}
_client = ESP_ALL_CLIENTS;
return strlen(s);
}
if (!isOutput(_client)) {
return 0;
}
String display;
log_esp3d("PrintMSG to client %d", _client);
if (_client == ESP_HTTP_CLIENT) {
#ifdef HTTP_FEATURE
if (_webserver) {
if (!_headerSent && !_footerSent) {
_webserver->setContentLength(CONTENT_LENGTH_UNKNOWN);
_webserver->sendHeader("Content-Type", "text/html");
_webserver->sendHeader("Cache-Control", "no-cache");
HTTP_Server::set_http_headers();
_webserver->send(_code);
_headerSent = true;
}
if (_headerSent && !_footerSent) {
_webserver->sendContent_P((const char *)s, strlen(s));
_webserver->sendContent_P((const char *)"\n", 1);
return strlen(s + 1);
}
}
#endif // HTTP_FEATURE
return 0;
}
// this is not supposed to be displayed on any screen
if (_client == ESP_SCREEN_CLIENT || _client == ESP_REMOTE_SCREEN_CLIENT ||
_client == ESP_SCREEN_CLIENT) {
return print(s);
}
switch (Settings_ESP3D::GetFirmwareTarget()) {
case GRBL:
display = "[MSG:";
display += s;
display += "]";
break;
case MARLIN_EMBEDDED:
case MARLIN:
if (((_client == ESP_ECHO_SERIAL_CLIENT) ||
(_client == ESP_STREAM_HOST_CLIENT)) &&
(strcmp(s, "ok") == 0)) {
return 0;
}
if (_client == ESP_ECHO_SERIAL_CLIENT) {
display = "echo:";
} else {
display = ";echo:";
}
display += s;
break;
case SMOOTHIEWARE:
case REPETIER:
default:
display = ";";
display += s;
}
return printLN(display.c_str());
}
size_t ESP3DOutput::printMSG(const char *s, bool withNL) {
if (_client == ESP_ALL_CLIENTS) {
// process each client one by one
log_esp3d("PrintMSG to all clients");
for (uint8_t c = 0; c < sizeof(activeClients); c++) {
if (activeClients[c]) {
log_esp3d("Sending PrintMSG to client %d", activeClients[c]);
_client = activeClients[c];
printMSG(s, withNL);
}
}
_client = ESP_ALL_CLIENTS;
return strlen(s);
}
if (!isOutput(_client)) {
return 0;
}
String display;
log_esp3d("PrintMSG to client %d", _client);
if (_client == ESP_HTTP_CLIENT) {
#ifdef HTTP_FEATURE
if (_webserver) {
if (!_headerSent && !_footerSent) {
_webserver->sendHeader("Cache-Control", "no-cache");
HTTP_Server::set_http_headers();
#ifdef ESP_ACCESS_CONTROL_ALLOW_ORIGIN
_webserver->sendHeader("Access-Control-Allow-Origin", "*");
#endif // ESP_ACCESS_CONTROL_ALLOw_ORIGIN
_webserver->send(_code, "text/plain", s);
_headerSent = true;
_footerSent = true;
return strlen(s);
}
}
#endif // HTTP_FEATURE
return 0;
}
if (_client == ESP_SCREEN_CLIENT) {
return print(s);
}
switch (Settings_ESP3D::GetFirmwareTarget()) {
case GRBL:
display = "[MSG:";
display += s;
display += "]";
break;
case MARLIN_EMBEDDED:
case MARLIN:
if (((_client == ESP_ECHO_SERIAL_CLIENT) ||
(_client == ESP_STREAM_HOST_CLIENT)) &&
(strcmp(s, "ok") == 0)) {
return 0;
}
if (_client == ESP_REMOTE_SCREEN_CLIENT) {
#if defined(HAS_SERIAL_DISPLAY)
display = HAS_SERIAL_DISPLAY;
#endif // HAS_REMOTE_SCREEN
display += "M117 ";
withNL = true;
log_esp3d("Screen should display %s%s", display.c_str(), s);
} else {
if (_client == ESP_ECHO_SERIAL_CLIENT) {
display = "echo:";
} else {
display = ";echo:";
}
}
display += s;
break;
case SMOOTHIEWARE:
case REPETIER:
default:
if (_client == ESP_REMOTE_SCREEN_CLIENT) {
display = "M117 ";
withNL = true;
} else {
display = ";";
}
display += s;
}
if (withNL) {
return printLN(display.c_str());
} else {
return print(display.c_str());
}
}
size_t ESP3DOutput::printERROR(const char *s, int code_error) {
String display = "";
if (!isOutput(_client)) {
return 0;
}
if (_client == ESP_SCREEN_CLIENT) {
return print(s);
}
if (_client == ESP_HTTP_CLIENT) {
#ifdef HTTP_FEATURE
(void)code_error;
if (_webserver) {
if (!_headerSent && !_footerSent) {
_webserver->sendHeader("Cache-Control", "no-cache");
HTTP_Server::set_http_headers();
if (s[0] != '{') {
display = "error: ";
} else {
display = "";
}
display += s;
_webserver->send(code_error, "text/plain", display.c_str());
_headerSent = true;
_footerSent = true;
return display.length();
}
}
#endif // HTTP_FEATURE
return 0;
}
switch (Settings_ESP3D::GetFirmwareTarget()) {
case GRBL:
if (s[0] != '{') {
display = "error: ";
}
display += s;
break;
case MARLIN_EMBEDDED:
case MARLIN:
if (s[0] != '{') {
display = "error: ";
}
display += s;
break;
case SMOOTHIEWARE:
case REPETIER:
default:
if (s[0] != '{') {
display = ";error: ";
}
display += s;
}
return printLN(display.c_str());
}
int ESP3DOutput::availableforwrite() {
switch (_client) {
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
case ESP_SERIAL_CLIENT:
return serial_service.availableForWrite();
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
case ESP_SERIAL_BRIDGE_CLIENT:
return serial_bridge_service.availableForWrite();
#endif // ESP_SERIAL_BRIDGE_OUTPUT
#if defined(BLUETOOTH_FEATURE)
case ESP_BT_CLIENT:
return bt_service.availableForWrite();
break;
#endif // BLUETOOTH_FEATURE
#if defined(TELNET_FEATURE)
case ESP_TELNET_CLIENT:
return telnet_server.availableForWrite();
break;
#endif // TELNET_FEATURE
#if defined(WS_DATA_FEATURE)
case ESP_WEBSOCKET_CLIENT:
return websocket_data_server.availableForWrite();
break;
#endif // WS_DATA_FEATURE
case ESP_ALL_CLIENTS:
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
return serial_service.availableForWrite();
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
default:
break;
}
return 0;
}
size_t ESP3DOutput::write(uint8_t c) {
if (!isOutput(_client)) {
return 0;
}
switch (_client) {
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
case ESP_SERIAL_CLIENT:
return serial_service.write(c);
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
#if COMMUNICATION_PROTOCOL == SOCKET_SERIAL
case ESP_ECHO_SERIAL_CLIENT:
return MYSERIAL1.write(c);
case ESP_SOCKET_SERIAL_CLIENT:
return Serial2Socket.write(c);
#endif // COMMUNICATION_PROTOCOL == SOCKET_SERIAL
#if defined(BLUETOOTH_FEATURE)
case ESP_BT_CLIENT:
return bt_service.write(c);
#endif // BLUETOOTH_FEATURE
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
case ESP_SERIAL_BRIDGE_CLIENT:
return serial_bridge_service.write(c);
break;
#endif // ESP_SERIAL_BRIDGE_OUTPUT
#if defined(TELNET_FEATURE)
case ESP_TELNET_CLIENT:
return telnet_server.write(c);
#endif // TELNET_FEATURE
#if defined(WS_DATA_FEATURE)
case ESP_WEBSOCKET_CLIENT:
return websocket_data_server.write(c);
#endif // WS_DATA_FEATURE
case ESP_ALL_CLIENTS:
#if defined(BLUETOOTH_FEATURE)
bt_service.write(c);
#endif // BLUETOOTH_FEATURE
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
serial_bridge_service.write(c);
#endif // ESP_SERIAL_BRIDGE_OUTPUT
#if defined(TELNET_FEATURE)
telnet_server.write(c);
#endif // TELNET_FEATURE
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
serial_service.write(c);
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
#if COMMUNICATION_PROTOCOL == SOCKET_SERIAL
MYSERIAL1.write(c);
#endif // COMMUNICATION_PROTOCOL == SOCKET_SERIAL
return 1;
default:
return 0;
}
}
size_t ESP3DOutput::write(const uint8_t *buffer, size_t size) {
if (!isOutput(_client)) {
return 0;
}
switch (_client) {
#ifdef HTTP_FEATURE
case ESP_HTTP_CLIENT:
if (_webserver) {
if (!_headerSent && !_footerSent) {
_webserver->setContentLength(CONTENT_LENGTH_UNKNOWN);
_webserver->sendHeader("Content-Type", "text/html");
_webserver->sendHeader("Cache-Control", "no-cache");
HTTP_Server::set_http_headers();
_webserver->send(_code);
_headerSent = true;
}
if (_headerSent && !_footerSent) {
_webserver->sendContent_P((const char *)buffer, size);
}
}
break;
#endif // HTTP_FEATURE
#if defined(DISPLAY_DEVICE)
case ESP_SCREEN_CLIENT:
esp3d_display.setStatus((const char *)buffer);
return size;
#endif // DISPLAY_DEVICE
#if defined(BLUETOOTH_FEATURE)
case ESP_BT_CLIENT:
return bt_service.write(buffer, size);
#endif // BLUETOOTH_FEATURE
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
case ESP_SERIAL_BRIDGE_CLIENT:
return serial_bridge_service.write(buffer, size);
#endif // ESP_SERIAL_BRIDGE_OUTPUT
#if defined(TELNET_FEATURE)
case ESP_TELNET_CLIENT:
return telnet_server.write(buffer, size);
#endif // TELNET_FEATURE
#if defined(WS_DATA_FEATURE)
case ESP_WEBSOCKET_CLIENT:
return websocket_data_server.write(buffer, size);
#endif // WS_DATA_FEATURE
#if defined(GCODE_HOST_FEATURE)
case ESP_STREAM_HOST_CLIENT: {
#if COMMUNICATION_PROTOCOL == SOCKET_SERIAL
log_esp3d(
"ESP_STREAM_HOST_CLIENT do a dispatch to all clients but socket "
"serial");
dispatch(buffer, size, ESP_SOCKET_SERIAL_CLIENT);
#endif // COMMUNICATION_PROTOCOL == SOCKET_SERIAL
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
log_esp3d(
"ESP_STREAM_HOST_CLIENT do a dispatch to all clients but serial");
dispatch(buffer, size, ESP_SERIAL_CLIENT);
#endif // COMMUNICATION_PROTOCOL == SOCKET_SERIAL
}
return size;
break;
#endif // GCODE_HOST_FEATURE
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
case ESP_REMOTE_SCREEN_CLIENT:
case ESP_SERIAL_CLIENT:
return serial_service.write(buffer, size);
break;
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
#if COMMUNICATION_PROTOCOL == SOCKET_SERIAL
case ESP_REMOTE_SCREEN_CLIENT:
log_esp3d("Writing to remote screen: %s", buffer);
return Serial2Socket.push(buffer, size);
break;
case ESP_ECHO_SERIAL_CLIENT:
return MYSERIAL1.write(buffer, size);
break;
case ESP_SOCKET_SERIAL_CLIENT:
return Serial2Socket.push(buffer, size);
break;
#endif // COMMUNICATION_PROTOCOL == SOCKET_SERIAL
case ESP_ALL_CLIENTS:
#if defined(BLUETOOTH_FEATURE)
bt_service.write(buffer, size);
#endif // BLUETOOTH_FEATURE
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
serial_bridge_service.write(buffer, size);
#endif // ESP_SERIAL_BRIDGE_OUTPUT
#if defined(TELNET_FEATURE)
telnet_server.write(buffer, size);
#endif // TELNET_FEATURE
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
serial_service.write(buffer, size);
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
#if COMMUNICATION_PROTOCOL == SOCKET_SERIAL
MYSERIAL1.write(buffer, size);
#endif // COMMUNICATION_PROTOCOL == SOCKET_SERIAL
return size;
default:
break;
}
return 0;
}

View File

@ -1,142 +0,0 @@
/*
esp3doutput.h - output functions class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define ESP_NO_CLIENT 0
#define ESP_SERIAL_CLIENT 1
#define ESP_TELNET_CLIENT 2
#define ESP_HTTP_CLIENT 4
#define ESP_WEBSOCKET_TERMINAL_CLIENT 8
#define ESP_REMOTE_SCREEN_CLIENT 16
#define ESP_STREAM_HOST_CLIENT 30
#define ESP_BT_CLIENT 32
#define ESP_SCREEN_CLIENT 64
#define ESP_WEBSOCKET_CLIENT 128
#define ESP_SOCKET_SERIAL_CLIENT 129
#define ESP_ECHO_SERIAL_CLIENT 130
#define ESP_SERIAL_BRIDGE_CLIENT 150
#define ESP_ALL_CLIENTS 255
#define ESP_STREAM_HOST_OUTPUT ESP_SERIAL_CLIENT
#define ESP_OUTPUT_IP_ADDRESS 0
#define ESP_OUTPUT_STATUS 1
#define ESP_OUTPUT_PROGRESS 2
#define ESP_OUTPUT_STATE 3
#define ESP_STATE_DISCONNECTED 0
#ifndef _ESP3DOUTPUT_H
#define _ESP3DOUTPUT_H
#include "Print.h"
#include "../include/esp3d_config.h"
#ifdef HTTP_FEATURE
#if defined (ARDUINO_ARCH_ESP32)
class WebServer;
#define WEBSERVER WebServer
#endif //ARDUINO_ARCH_ESP32
#if defined (ARDUINO_ARCH_ESP8266)
#include <ESP8266WebServer.h>
#define WEBSERVER ESP8266WebServer
#endif //ARDUINO_ARCH_ESP8266
#endif //HTTP_FEATURE
class ESP3DOutput : public Print
{
public:
ESP3DOutput(uint8_t client = 0);
#ifdef HTTP_FEATURE
ESP3DOutput(WEBSERVER * webserver);
#endif //HTTP_FEATURE
~ESP3DOutput();
size_t write(uint8_t c);
size_t write(const uint8_t *buffer, size_t size);
inline size_t write(const char * s)
{
return write((uint8_t*) s, strlen(s));
}
inline size_t write(unsigned long n)
{
return write((uint8_t) n);
}
inline size_t write(long n)
{
return write((uint8_t) n);
}
inline size_t write(unsigned int n)
{
return write((uint8_t) n);
}
inline size_t write(int n)
{
return write((uint8_t) n);
}
uint8_t client(uint8_t client = 0);
size_t dispatch (const uint8_t * sbuf, size_t len, uint8_t ignoreClient = 0);
size_t printMSG(const char * s, bool withNL = true);
size_t printMSGLine(const char * s);
size_t printERROR(const char * s, int code_error = 500);
size_t printLN(const char * s);
void flush();
int availableforwrite();
static bool isOutput(uint8_t flag, bool fromsettings = false);
static void toScreen(uint8_t output_type, const char * s);
static const char * encodeString(const char * s);
#ifdef HTTP_FEATURE
bool footerSent()
{
return _footerSent;
}
#endif //HTTP_FEATURE
private:
uint8_t _client;
#ifdef HTTP_FEATURE
int _code;
bool _headerSent;
bool _footerSent;
WEBSERVER * _webserver;
#endif //HTTP_FEATURE
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL || COMMUNICATION_PROTOCOL == SOCKET_SERIAL
static uint8_t _serialoutputflags;
#endif //COMMUNICATION_PROTOCOL
#if defined(HAS_DISPLAY) || defined(HAS_SERIAL_DISPLAY)
static uint8_t _remotescreenoutputflags;
#endif //HAS_DISPLAY || HAS_SERIAL_DISPLAY
#if defined (WS_DATA_FEATURE)
static uint8_t _websocketoutputflags;
#endif //WS_DATA_FEATURE
#if defined (TELNET_FEATURE)
static uint8_t _telnetoutputflags;
#endif //TELNET_FEATURE
#if defined (DISPLAY_DEVICE)
static uint8_t _screenoutputflags;
#endif //DISPLAY_DEVICE
#if defined (BLUETOOTH_FEATURE)
static uint8_t _BToutputflags;
#endif //BLUETOOTH_FEATURE
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
static uint8_t _serialBridgeoutputflags;
#endif //ESP_SERIAL_BRIDGE_OUTPUT
};
#endif //_ESP3DOUTPUT_H

View File

@ -1,93 +0,0 @@
/*
ESP100.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(WIFI_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/wifi/wificonfig.h"
#include "../commands.h"
#include "../esp3doutput.h"
#include "../settings_esp3d.h"
#define COMMANDID 100
// STA SSID
//[ESP100]<SSID>[json=no] [pwd=<admin password>]
bool Commands::ESP100(const char* cmd_params, level_authenticate_type auth_type,
ESP3DOutput* output) {
bool noError = true;
bool json = has_tag(cmd_params, "json");
String response;
String parameter;
int errorCode = 200; // unless it is a server error use 200 as default and
// set error in json instead
#ifdef AUTHENTICATION_FEATURE
if (auth_type == LEVEL_GUEST) {
response = format_response(COMMANDID, json, false,
"Guest user can't use this command");
noError = false;
errorCode = 401;
}
#else
(void)auth_type;
#endif // AUTHENTICATION_FEATURE
if (noError) {
parameter = clean_param(get_param(cmd_params, ""));
// get
if (parameter.length() == 0) {
response = format_response(COMMANDID, json, true,
Settings_ESP3D::read_string(ESP_STA_SSID));
} else { // set
#ifdef AUTHENTICATION_FEATURE
if (auth_type != LEVEL_ADMIN) {
response = format_response(COMMANDID, json, false,
"Wrong authentication level");
noError = false;
errorCode = 401;
}
#endif // AUTHENTICATION_FEATURE
if (noError) {
if (!Settings_ESP3D::isValidStringSetting(parameter.c_str(),
ESP_STA_SSID)) {
response = format_response(COMMANDID, json, false, "Incorrect SSID");
noError = false;
} else {
if (!Settings_ESP3D::write_string(ESP_STA_SSID, parameter.c_str())) {
response = format_response(COMMANDID, json, false, "Set failed");
noError = false;
} else {
response = format_response(COMMANDID, json, true, "ok");
}
}
}
}
}
if (json) {
output->printLN(response.c_str());
} else {
if (noError) {
output->printMSG(response.c_str());
} else {
output->printERROR(response.c_str(), errorCode);
}
}
return noError;
}
#endif // WIFI_FEATURE

View File

@ -1,89 +0,0 @@
/*
ESP101.cpp - ESP3D command class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined(WIFI_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/wifi/wificonfig.h"
#include "../commands.h"
#include "../esp3doutput.h"
#include "../settings_esp3d.h"
#define COMMANDID 101
// STA Password
//[ESP101]<Password> [json=no] [pwd=<admin password>]
bool Commands::ESP101(const char* cmd_params, level_authenticate_type auth_type,
ESP3DOutput* output) {
bool noError = true;
bool json = has_tag(cmd_params, "json");
bool clearSetting = has_tag(cmd_params, "NOPASSWORD");
String response;
String parameter;
int errorCode = 200; // unless it is a server error use 200 as default and
// set error in json instead
#ifdef AUTHENTICATION_FEATURE
if (auth_type != LEVEL_ADMIN) {
response =
format_response(COMMANDID, json, false, "Wrong authentication level");
noError = false;
errorCode = 401;
}
#else
(void)auth_type;
#endif // AUTHENTICATION_FEATURE
if (noError) {
parameter = clean_param(get_param(cmd_params, ""));
if (parameter.length() == 0) {
response =
format_response(COMMANDID, json, false, "Password not displayable");
noError = false;
} else {
if (clearSetting) {
parameter = "";
}
if (!Settings_ESP3D::isValidStringSetting(parameter.c_str(),
ESP_STA_PASSWORD)) {
response =
format_response(COMMANDID, json, false, "Incorrect password");
noError = false;
} else {
if (!Settings_ESP3D::write_string(ESP_STA_PASSWORD,
parameter.c_str())) {
response = format_response(COMMANDID, json, false, "Set failed");
noError = false;
} else {
response = format_response(COMMANDID, json, true, "ok");
}
}
}
}
if (json) {
output->printLN(response.c_str());
} else {
if (noError) {
output->printMSG(response.c_str());
} else {
output->printERROR(response.c_str(), errorCode);
}
}
return noError;
}
#endif // WIFI_FEATURE

Some files were not shown because too many files have changed in this diff Show More