Skip to content

Commit

Permalink
Getting close
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchBradley committed Jan 27, 2024
1 parent e8394a4 commit 7a1c43e
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 48 deletions.
5 changes: 5 additions & 0 deletions FluidNC/src/Channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ class Channel : public Stream {
push(*data++);
}
}
void push(std::string_view data) {
for (auto const& c : data) {
push((uint8_t)c);
}
}

void push(const std::string& s) { push((uint8_t*)s.c_str(), s.length()); }

Expand Down
4 changes: 2 additions & 2 deletions FluidNC/src/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ class MachineConfigProxySetting : public Setting {

public:
MachineConfigProxySetting(const char* grblName, const char* fullName, std::function<T(Machine::MachineConfig const&)> getter) :
Setting(fullName, type_t::GRBL, permissions_t::WU, grblName, fullName, nullptr), _getter(getter), _cachedValue("") {}
Setting(fullName, type_t::GRBL, permissions_t::WU, grblName, fullName), _getter(getter), _cachedValue("") {}

const char* getStringValue() override;
Error setStringValue(char* value) override { return Error::ReadOnlySetting; }
Error setStringValue(std::string_view value) { return Error::ReadOnlySetting; }
const char* getDefaultString() override { return ""; }
};

Expand Down
10 changes: 4 additions & 6 deletions FluidNC/src/WebUI/WSChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ namespace WebUI {
}
}

bool WSChannels::runGCode(int pageid, std::string& cmd) {
bool WSChannels::runGCode(int pageid, std::string_view cmd) {
WSChannel* wsChannel = getWSChannel(pageid);
if (wsChannel) {
if (cmd.length()) {
Expand Down Expand Up @@ -228,7 +228,6 @@ namespace WebUI {
}
}


void WSChannels::handlev3Event(WebSocketsServer* server, uint8_t num, uint8_t type, uint8_t* payload, size_t length) {
switch (type) {
case WStype_DISCONNECTED:
Expand Down Expand Up @@ -261,11 +260,10 @@ namespace WebUI {
server->broadcastTXT(s.c_str());
}

//
for (uint8_t i=0; i<WEBSOCKETS_SERVER_CLIENT_MAX; i++)
if(i!=num && server->clientIsConnected(i)) {
for (uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++)
if (i != num && server->clientIsConnected(i)) {
server->disconnect(i);
}
}
}
} break;
case WStype_TEXT:
Expand Down
2 changes: 1 addition & 1 deletion FluidNC/src/WebUI/WSChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ namespace WebUI {
static void removeChannel(WSChannel* channel);
static void removeChannel(uint8_t num);

static bool runGCode(int pageid, std::string& cmd);
static bool runGCode(int pageid, std::string_view cmd);
static bool sendError(int pageid, std::string error);
static void sendPing();
static void handleEvent(WebSocketsServer* server, uint8_t num, uint8_t type, uint8_t* payload, size_t length);
Expand Down
20 changes: 10 additions & 10 deletions FluidNC/src/WebUI/WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ namespace WebUI {
}
return -1;
}
void Web_Server::synchronousCommand(const char* cmd) {
void Web_Server::synchronousCommand(const char* cmd, bool silent, AuthenticationLevel auth_level) {
char line[256];
strncpy(line, cmd, 255);
webClient.attachWS(_webserver, silent);
Expand All @@ -455,36 +455,36 @@ namespace WebUI {
}
webClient.detachWS();
}
void Web_Server::websocketCommand(const char* cmd, int pageid) {
void Web_Server::websocketCommand(const char* cmd, int pageid, AuthenticationLevel auth_level) {
if (auth_level == AuthenticationLevel::LEVEL_GUEST) {
_webserver->send(401, "text/plain", "Authentication failed\n");
return;
}
std::string cmd = std::string(cmd);

bool hasError = WSChannels::runGCode(pageid, cmd);
_webserver->send(hasError ? 500 : 200, "text/plain", hasError ? "WebSocket dead" : "");
}
void Web_Server::_handle_web_command(bool silent) {
AuthenticationLevel auth_level = is_authenticated();
if (_webserver->hasArg("cmd")) { // WebUI3
const char* cmd = _webserver->arg("plain").c_str()'

auto cmd = _webserver->arg("cmd");
// [ESPXXX] commands expect data in the HTTP response
if (cmd.find("[ESP") != std::string::npos) {
synchronousCommand(cmd);
if (cmd.startsWith("[ESP")) {
synchronousCommand(cmd.c_str(), silent, auth_level);
} else {
websocketCommand(cmd, -1); // WebUI3 does not support PAGEID
websocketCommand(cmd.c_str(), -1, auth_level); // WebUI3 does not support PAGEID
}
return;
}
if (_webserver->hasArg("plain")) {
synchronousCommand(_webserver->arg("plain").c_str());
synchronousCommand(_webserver->arg("plain").c_str(), silent, auth_level);
return;
}
if (_webserver->hasArg("commandText")) {
websocketCommand(_webserver->arg("commandText").c_str(), getPageid());
websocketCommand(_webserver->arg("commandText").c_str(), getPageid(), auth_level);
return;
}
}
_webserver->send(500, "text/plain", "Invalid command");
}

Expand Down
3 changes: 3 additions & 0 deletions FluidNC/src/WebUI/WebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ namespace WebUI {
static void uploadStop();
static void uploadCheck();

static void synchronousCommand(const char* cmd, bool silent, AuthenticationLevel auth_level);
static void websocketCommand(const char* cmd, int pageid, AuthenticationLevel auth_level);

static void sendFSError(Error err);
static void sendJSON(int code, const char* s);
static void sendJSON(int code, const std::string& s) { sendJSON(code, s.c_str()); }
Expand Down
61 changes: 32 additions & 29 deletions FluidNC/src/WebUI/WebSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <cstring>
#include <sstream>
#include <iomanip>
#include <charconv>

namespace WebUI {

Expand Down Expand Up @@ -422,14 +423,14 @@ namespace WebUI {
return Error::Ok;
}

static bool split(char* input, char** next, char delim) {
char* pos = strchr(input, delim);
if (pos) {
*pos = '\0';
*next = pos + 1;
static bool split(std::string_view input, std::string_view& next, char delim) {
auto pos = input.find_first_of(delim);
if (pos != std::string_view::npos) {
next = input.substr(pos + 1);
input = input.substr(0, pos);
return true;
}
*next = input + strlen(input); // End of string
next = "";
return false;
}

Expand All @@ -440,7 +441,7 @@ namespace WebUI {
return showFile("", parameter, auth_level, out);
}

static Error fileShowSome(char* parameter, AuthenticationLevel auth_level, Channel& out) { // ESP221
static Error fileShowSome(const char* parameter, AuthenticationLevel auth_level, Channel& out) { // ESP221
if (notIdleOrAlarm()) {
return Error::IdleError;
}
Expand All @@ -449,39 +450,41 @@ namespace WebUI {
return Error::InvalidValue;
}

int firstline = 0;
int lastline = 0;
char* filename;
split(parameter, &filename, ',');
if (*filename == '\0') {
log_error_to(out, "Missing filename");
std::string_view args(parameter);

int firstline = 0;
int lastline = 0;

std::string_view filename;
split(args, filename, ',');
if (filename.empty() || args.empty()) {
log_error_to(out, "Invalid syntax");
return Error::InvalidValue;
}

// Parameter is the list of lines to display
// Args is the list of lines to display
// N means the first N lines
// N:M means lines N through M inclusive
if (!*parameter) {
log_error_to(out, "Missing line count");
return Error::InvalidValue;
}

JSONencoder j(false, &out);
char* second;
split(parameter, &second, ':');
if (*second) {
firstline = atoi(parameter);
lastline = atoi(second);
} else {

std::string_view second;
split(args, second, ':');
if (second.empty()) {
firstline = 0;
lastline = atoi(parameter);
std::from_chars(args.data(), args.data() + args.length(), lastline);
} else {
std::from_chars(args.data(), args.data() + args.length(), firstline);
std::from_chars(second.data(), second.data() + second.length(), lastline);
}
const char* error = "";
j.begin();
j.begin_array("file_lines");

InputFile* theFile;
Error err;
if ((err = openFile(sdName, filename, auth_level, out, theFile)) != Error::Ok) {
InputFile* theFile;
Error err;
std::string fn(filename);
if ((err = openFile(sdName, fn.c_str(), auth_level, out, theFile)) != Error::Ok) {
error = "Cannot open file";
} else {
char fileLine[255];
Expand All @@ -500,7 +503,7 @@ namespace WebUI {
if (*error) {
j.member("error", error);
} else {
j.member("path", filename);
j.member("path", fn.c_str());
j.member("firstline", firstline);
}

Expand Down

0 comments on commit 7a1c43e

Please sign in to comment.