ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
ESP740.cpp
Go to the documentation of this file.
1 /*
2  ESP740.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 #ifdef SD_TIMESTAMP_FEATURE
28 #include "../../modules/time/time_server.h"
29 #endif //SD_TIMESTAMP_FEATURE
30 //List SD Filesystem
31 //[ESP740]<Root> pwd=<admin password>
32 bool Commands::ESP740(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output)
33 {
34  bool response = true;
35  String parameter;
36  parameter = get_param (cmd_params, "");
37 #ifdef AUTHENTICATION_FEATURE
38  if (auth_type != LEVEL_ADMIN) {
39  output->printERROR("Wrong authentication!", 401);
40  return false;
41  }
42 #else
43  (void)auth_type;
44 #endif //AUTHENTICATION_FEATURE
45  if (parameter.length() == 0) {
46  parameter = "/";
47  }
48  int8_t state = ESP_SD::getState(false);
49  if (state != ESP_SDCARD_IDLE) {
50  state = ESP_SD::getState(true);
51  }
52  if (state == ESP_SDCARD_NOT_PRESENT) {
53  output->printERROR ("No SD card");
54  return false;
55  } else if (state != ESP_SDCARD_IDLE) {
56  output->printERROR ("Busy");
57  return false;
58  }
59  output->printf("Directory on SD : %s", parameter.c_str());
60  output->printLN("");
61  if (ESP_SD::exists(parameter.c_str())) {
62  ESP_SDFile f = ESP_SD::open(parameter.c_str(), ESP_FILE_READ);
63  uint countf = 0;
64  uint countd = 0;
65  if (f) {
66  //Check directories
67  ESP_SDFile sub = f.openNextFile();
68  while (sub) {
69  if (sub.isDirectory()) {
70  countd++;
71  output->print("<DIR> \t");
72  output->print(sub.name());
73  output->print(" \t");
74  output->printLN("");
75  }
76  sub.close();
77  sub = f.openNextFile();
78  }
79  f.close();
80  f = ESP_SD::open(parameter.c_str(), ESP_FILE_READ);
81  //Check files
82  sub = f.openNextFile();
83  while (sub) {
84  if (!sub.isDirectory()) {
85  countf++;
86  output->print(" \t");
87  output->print(sub.name());
88  output->print(" \t");
89  output->print(ESP_SD::formatBytes(sub.size()).c_str());
90  output->print(" \t");
91 #ifdef SD_TIMESTAMP_FEATURE
92  output->print(timeserver.current_time(sub.getLastWrite()));
93  output->print(" \t");
94 #endif //SD_TIMESTAMP_FEATURE
95  output->printLN("");
96  }
97  sub.close();
98  sub = f.openNextFile();
99  }
100  f.close();
101  output->printf("%d file%s, %d dir%s", countf, (countf > 1)?"(s)":"", countd, (countd > 1)?"(s)":"");
102  output->printLN("");
106  output->printf("Total %s, Used %s, Available: %s", t.c_str(), u.c_str(),f.c_str());
107  output->printLN("");
108  } else {
109  output->printERROR ("Invalid directory!");
110  }
111  } else {
112  output->printERROR ("Invalid directory!");
113  response = false;
114  }
115  return response;
116 }
117 
118 #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
ESP3DOutput::printLN
size_t printLN(const char *s)
Definition: esp3doutput.cpp:165
ESP_SDFile::isDirectory
bool isDirectory()
Definition: esp_sd.cpp:125
ESP_SD::open
static ESP_SDFile open(const char *path, uint8_t mode=ESP_FILE_READ)
ESP_SDFile::name
const char * name() const
Definition: esp_sd.cpp:115
ESP_FILE_READ
#define ESP_FILE_READ
Definition: defines.h:120
ESP_SD::totalBytes
static uint64_t totalBytes()
ESP_SDFile
Definition: esp_sd.h:31
ESP_SD::freeBytes
static uint64_t freeBytes()
ESP_SDFile::size
size_t size()
Definition: esp_sd.cpp:130
ESP_SDCARD_IDLE
#define ESP_SDCARD_IDLE
Definition: defines.h:80
timeserver
TimeServer timeserver
ESP_SDFile::getLastWrite
time_t getLastWrite()
Definition: esp_sd.cpp:135
ESP_SD::formatBytes
static String & formatBytes(uint64_t bytes)
Definition: esp_sd.cpp:68
ESP_SDFile::openNextFile
ESP_SDFile openNextFile()
ESP_SD::getState
static uint8_t getState(bool refresh)
level_authenticate_type
level_authenticate_type
Definition: authentication_service.h:25
ESP3DOutput::printERROR
size_t printERROR(const char *s, int code_error=200)
Definition: esp3doutput.cpp:247
ESP_SDFile::close
void close()
Commands::ESP740
bool ESP740(const char *cmd_params, level_authenticate_type auth_level, ESP3DOutput *output)
Definition: ESP740.cpp:32
LEVEL_ADMIN
@ LEVEL_ADMIN
Definition: authentication_service.h:28
ESP_SD::exists
static bool exists(const char *path)
ESP3DOutput
Definition: esp3doutput.h:48
ESP_SD::usedBytes
static uint64_t usedBytes()
TimeServer::current_time
const char * current_time(time_t t=0)