Skip to content

Commit

Permalink
Refactorize Serial service code to avoid redondant code between esp32…
Browse files Browse the repository at this point in the history
… and esp8266

Implement isrealtimeCommand check for serial and centralize function in string helper
Add new type message : realtimecmd
Update simulator to handle commands and realtime commands
Add simple serial test tool
Generate error if use HAS_DISPLAY with grbl/grblHAL
  • Loading branch information
luc-github committed Dec 7, 2024
1 parent af5832f commit 3a6c216
Show file tree
Hide file tree
Showing 16 changed files with 614 additions and 442 deletions.
8 changes: 4 additions & 4 deletions esp3d/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@
/* Printer screen
* If your printer has a display
*/
#define PRINTER_HAS_DISPLAY
//#define PRINTER_HAS_DISPLAY

/* ESP3D screen
* Screen connected to ESP board
Expand Down Expand Up @@ -644,9 +644,9 @@
// 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_DEBUG
#define ESP3D_DEBUG_LEVEL LOG_LEVEL_DEBUG

#ifdef ESP_LOG_FEATURE
#define LOG_ESP3D_BAUDRATE 115200
Expand All @@ -657,7 +657,7 @@
// #define ESP_BENCHMARK_FEATURE

// Disable sanity check at compilation
// #define ESP_NO_SANITY_CHECK
#define ESP_NO_SANITY_CHECK

/************************************
*
Expand Down
2 changes: 1 addition & 1 deletion esp3d/src/core/esp3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ void Esp3D::restart_now() {
#if defined(FILESYSTEM_FEATURE)
ESP_FileSystem::end();
#endif // FILESYSTEM_FEATURE
#if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL
#if (COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL) & defined(ARDUINO_ARCH_ESP8266)
esp3d_serial_service.swap();
#endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL ==
// MKS_SERIAL
Expand Down
19 changes: 11 additions & 8 deletions esp3d/src/core/esp3d_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ ESP3DCommands::ESP3DCommands() {

ESP3DCommands::~ESP3DCommands() {}

bool ESP3DCommands::isRealTimeCommand(char *cmd, size_t len) { return false; }

// check if current line is an [ESPXXX] command
bool ESP3DCommands::is_esp_command(uint8_t *sbuf, size_t len) {
// TODO
Expand Down Expand Up @@ -1156,10 +1154,6 @@ bool ESP3DCommands::dispatchIdValue(bool json, const char *Id,
}

bool ESP3DCommands::formatCommand(char *cmd, size_t len) {
if (isRealTimeCommand(cmd, len)) {
// TODO: what if is realtime command ?
return true;
}
uint sizestr = strlen(cmd);
if (len > sizestr + 2) {
cmd[sizestr] = '\n';
Expand Down Expand Up @@ -1233,7 +1227,7 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg, uint8_t *sbuf, size_t len) {
// check is need \n at the end of the command
if (msg->type == ESP3DMessageType::unique ||
msg->type == ESP3DMessageType::tail) {
esp3d_log("unique or tail message :*%s*", (char *)sbuf);
esp3d_log_d("unique or tail message :*%s*", (char *)sbuf);
if (!formatCommand((char *)sbuf, len)) {
esp3d_log("format command failed");
String tmpstr = "";
Expand All @@ -1250,7 +1244,7 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg, uint8_t *sbuf, size_t len) {
return false;
}
} else {
esp3d_log("format command success, no need to update");
esp3d_log_d("format command success, no need to update");
if (!esp3d_message_manager.setDataContent(msg, sbuf, len)) {
esp3d_log_e("set data content failed");
esp3d_message_manager.deleteMsg(msg);
Expand All @@ -1259,6 +1253,9 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg, uint8_t *sbuf, size_t len) {
}
} else {
esp3d_log("not unique or tail message");
if (msg->type == ESP3DMessageType::realtimecmd){
esp3d_log_d("realtime command");
}
if (!esp3d_message_manager.setDataContent(msg, sbuf, len)) {
esp3d_log_e("set data content failed");
esp3d_message_manager.deleteMsg(msg);
Expand Down Expand Up @@ -1468,6 +1465,12 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {

#ifdef PRINTER_HAS_DISPLAY
case ESP3DClientType::remote_screen:
if (ESP3DSettings::GetFirmwareTarget() == GRBL ||
ESP3DSettings::GetFirmwareTarget() == GRBLHAL) {
esp3d_log_e("Remote screen message is not supported for GRBL");
sendOk = false;
break;
}
esp3d_log("Remote screen message");
// change target to output client
msg->target = getOutputClient();
Expand Down
1 change: 0 additions & 1 deletion esp3d/src/core/esp3d_commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ class ESP3DCommands {
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;
Expand Down
2 changes: 1 addition & 1 deletion esp3d/src/core/esp3d_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class WebServer;
#endif //pdTRUE
#endif //ESP8266

enum class ESP3DMessageType : uint8_t { head, core, tail, unique };
enum class ESP3DMessageType : uint8_t { head, core, tail, unique, realtimecmd };

union ESP3DRequest {
uint id;
Expand Down
4 changes: 2 additions & 2 deletions esp3d/src/core/esp3d_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ const char* esp3d_string::urlEncode(const char* s) {
static String encoded;
encoded = "";
char temp[4];
for (int i = 0; i < strlen(s); i++) {
for (size_t i = 0; i < strlen(s); i++) {
temp[0] = s[i];
if (temp[0] == 32) { // space
encoded.concat('+');
Expand Down Expand Up @@ -267,7 +267,7 @@ const char* esp3d_string::formatDuration(uint64_t duration) {
return result.c_str();
}

bool esp3d_string::isRealtimeCommand(char c) {
bool esp3d_string::isRealTimeCommand(char c) {
if (ESP3DSettings::GetFirmwareTarget() == GRBL ||
ESP3DSettings::GetFirmwareTarget() == GRBLHAL) {
// Standard characters
Expand Down
2 changes: 1 addition & 1 deletion esp3d/src/core/esp3d_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const char* formatBytes(uint64_t bytes);
bool isPrintableChar(char c);
const char* expandString(const char* s, bool formatspace = false);
const char * urlEncode(const char* s);
bool isRealtimeCommand(char c);
bool isRealTimeCommand(char c);
} // namespace esp3d_string

#endif //_ESP3D_STRING_H
34 changes: 17 additions & 17 deletions esp3d/src/modules/http/handlers/handle-command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,9 @@
#include "../../../core/esp3d_commands.h"
#include "../../../core/esp3d_message.h"
#include "../../../core/esp3d_settings.h"
#include "../../../core/esp3d_string.h"
#include "../../authentication/authentication_service.h"

const unsigned char realTimeCommands[] = {
'!', '~', '?', 0x18, 0x84, 0x85, 0x90, 0x92, 0x93, 0x94, 0x95,
0x96, 0x97, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0xA0, 0xA1};
bool isRealTimeCommand(unsigned char c) {
for (unsigned int i = 0; i < sizeof(realTimeCommands); i++) {
if (c == realTimeCommands[i]) {
return true;
}
}
return false;
}

// Handle web command query and send answer//////////////////////////////
void HTTP_Server::handle_web_command() {
Expand All @@ -53,26 +43,36 @@ void HTTP_Server::handle_web_command() {
}
// esp3d_log("Authentication = %d", auth_level);
String cmd = "";
bool isRealTimeCommand = false;
if (_webserver->hasArg("cmd")) {
cmd = _webserver->arg("cmd");
esp3d_log_d("Command is %s", cmd.c_str());
if (!cmd.endsWith("\n")) {
if (ESP3DSettings::GetFirmwareTarget() == GRBL) {
esp3d_log_d("Command is not ending with \\n");
if (ESP3DSettings::GetFirmwareTarget() == GRBL || ESP3DSettings::GetFirmwareTarget() == GRBLHAL) {
uint len = cmd.length();
if (!((len == 1 && isRealTimeCommand(cmd[0])) ||
(len == 2 && isRealTimeCommand(cmd[1])))) {
if (!((len == 1 && esp3d_string::isRealTimeCommand(cmd[0])) ||
(len == 2 && esp3d_string::isRealTimeCommand(cmd[1])))) {
cmd += "\n";
esp3d_log_d("Command is not realtime, adding \\n");
} else { // no need \n for realtime command
esp3d_log_d("Command is realtime, no need to add \\n");
isRealTimeCommand = true;
// remove the 0XC2 that should not be there
if (len == 2 && isRealTimeCommand(cmd[1]) && cmd[1] == 0xC2) {
if (len == 2 && esp3d_string::isRealTimeCommand(cmd[1]) && cmd[1] == 0xC2) {
cmd[0] = cmd[1];
cmd[1] = 0x0;
esp3d_log_d("Command is realtime, removing 0xC2");
}
}
} else {
esp3d_log_d("Command is not realtime, adding \\n");
cmd += "\n"; // need to validate command
}
} else {
esp3d_log_d("Command is ending with \\n");
}
esp3d_log("Web Command: %s", cmd.c_str());
esp3d_log_d("Message type is %s for %s", isRealTimeCommand ? "realtimecmd" : "unique", cmd.c_str());
if (esp3d_commands.is_esp_command((uint8_t *)cmd.c_str(), cmd.length())) {
ESP3DMessage *msg = esp3d_message_manager.newMsg(
ESP3DClientType::http, esp3d_commands.getOutputClient(),
Expand All @@ -91,7 +91,7 @@ void HTTP_Server::handle_web_command() {
// no need to wait to answer then
_webserver->send(200, "text/plain", "ESP3D says: command forwarded");
esp3d_commands.dispatch(cmd.c_str(), esp3d_commands.getOutputClient(),
no_id, ESP3DMessageType::unique,
no_id, isRealTimeCommand ? ESP3DMessageType::realtimecmd :ESP3DMessageType::unique,
ESP3DClientType::http, auth_level);
}
} else if (_webserver->hasArg("ping")) {
Expand Down
3 changes: 2 additions & 1 deletion esp3d/src/modules/http/http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ bool HTTP_Server::dispatch(ESP3DMessage* msg) {
}
if ((msg->size > 0 && msg->data) || (msg->type == ESP3DMessageType::tail)) {
if (msg->type == ESP3DMessageType::head ||
msg->type == ESP3DMessageType::unique) {
msg->type == ESP3DMessageType::unique ||
msg->type == ESP3DMessageType::realtimecmd) {
set_http_headers();
int code = 200;
if (msg->request_id.code != 0) {
Expand Down
Loading

0 comments on commit 3a6c216

Please sign in to comment.