ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
ESP750.cpp
Go to the documentation of this file.
1 /*
2  ESP750.cpp - ESP3D command class
3 
4  Copyright (c) 2014 Luc Lebosse. All rights reserved.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include "../../include/esp3d_config.h"
21 #if defined (SD_DEVICE)
22 #include "../commands.h"
23 #include "../esp3doutput.h"
24 #include "../settings_esp3d.h"
25 #include "../../modules/authentication/authentication_service.h"
26 #include "../../modules/filesystem/esp_sd.h"
27 // Action on SD Filesystem
28 //rmdir / remove / mkdir / exists /create
29 //[ESP750]<Action>=<path> pwd=<admin password>
30 bool Commands::ESP750(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output)
31 {
32  bool response = true;
33  String parameter;
34  parameter = get_param (cmd_params, "");
35 #ifdef AUTHENTICATION_FEATURE
36  if (auth_type != LEVEL_ADMIN) {
37  output->printERROR("Wrong authentication!", 401);
38  return false;
39  }
40 #else
41  (void)auth_type;
42 #endif //AUTHENTICATION_FEATURE
43  int8_t state = ESP_SD::getState(false);
44  if (state != ESP_SDCARD_IDLE) {
45  state = ESP_SD::getState(true);
46  }
47  if (state == ESP_SDCARD_NOT_PRESENT) {
48  output->printERROR ("No SD card");
49  return false;
50  } else if (state != ESP_SDCARD_IDLE) {
51  output->printERROR ("Busy");
52  return false;
53  }
54  parameter = get_param (cmd_params, "mkdir=");
55  if (parameter.length() != 0) {
56  if (ESP_SD::mkdir(parameter.c_str())) {
57  output->printMSG ("ok");
58  } else {
59  output->printERROR ("failed!");
60  response = false;
61  }
62  return response;
63  }
64  parameter = get_param (cmd_params, "rmdir=");
65  if (parameter.length() != 0) {
66  if (ESP_SD::rmdir(parameter.c_str())) {
67  output->printMSG ("ok");
68  } else {
69  output->printERROR ("failed!");
70  response = false;
71  }
72  return response;
73  }
74  parameter = get_param (cmd_params, "remove=");
75  if (parameter.length() != 0) {
76  if (ESP_SD::remove(parameter.c_str())) {
77  output->printMSG ("ok");
78  } else {
79  output->printERROR ("failed!");
80  response = false;
81  }
82  return response;
83  }
84  parameter = get_param (cmd_params, "exists=");
85  if (parameter.length() != 0) {
86  if (ESP_SD::exists(parameter.c_str())) {
87  output->printMSG ("yes");
88  } else {
89  output->printMSG ("no");
90  }
91  return response;
92  }
93  parameter = get_param (cmd_params, "create=");
94  if (parameter.length() != 0) {
95  ESP_SDFile f = ESP_SD::open(parameter.c_str(), ESP_FILE_WRITE);
96  if (f.isOpen()) {
97  f.close();
98  output->printMSG ("ok");
99  } else {
100  output->printERROR ("failed!");
101  response = false;
102  }
103  return response;
104  }
105  output->printERROR ("Incorrect command!");
106  return false;
107 }
108 
109 #endif //SD_DEVICE
ESP_SDCARD_NOT_PRESENT
#define ESP_SDCARD_NOT_PRESENT
Definition: defines.h:81
Commands::get_param
const char * get_param(const char *cmd_params, const char *label)
Definition: commands.cpp:162
ESP_SD::open
static ESP_SDFile open(const char *path, uint8_t mode=ESP_FILE_READ)
ESP_FILE_WRITE
#define ESP_FILE_WRITE
Definition: defines.h:121
ESP_SDFile
Definition: esp_sd.h:31
ESP_SD::remove
static bool remove(const char *path)
ESP_SDCARD_IDLE
#define ESP_SDCARD_IDLE
Definition: defines.h:80
ESP_SD::rmdir
static bool rmdir(const char *path)
ESP_SD::mkdir
static bool mkdir(const char *path)
Commands::ESP750
bool ESP750(const char *cmd_params, level_authenticate_type auth_level, ESP3DOutput *output)
Definition: ESP750.cpp:30
ESP_SD::getState
static uint8_t getState(bool refresh)
level_authenticate_type
level_authenticate_type
Definition: authentication_service.h:25
ESP3DOutput::printMSG
size_t printMSG(const char *s, bool withNL=true)
Definition: esp3doutput.cpp:190
ESP_SDFile::isOpen
bool isOpen()
Definition: esp_sd.cpp:110
ESP3DOutput::printERROR
size_t printERROR(const char *s, int code_error=200)
Definition: esp3doutput.cpp:247
ESP_SDFile::close
void close()
LEVEL_ADMIN
@ LEVEL_ADMIN
Definition: authentication_service.h:28
ESP_SD::exists
static bool exists(const char *path)
ESP3DOutput
Definition: esp3doutput.h:48