9 #include "RtosHeap.hpp"
10 #include "FsDriveSdio.hpp"
11 #include "LibDebug.hpp"
13 extern FsDriveSdio fsDriveSdio;
15 Telnet::Telnet(
int priority,
size_t stackSize) :
16 NetServerTelnet(priority, stackSize) {
17 mEntries = (
const ENTRY[] ) {
18 {
"help", (NetServerTelnet::CMD_HANDLER) &Telnet::handlerHelp,
"Display available commands" },
19 {
"exit", (NetServerTelnet::CMD_HANDLER) &Telnet::handlerExit,
"Close telnet connection" },
20 {
"sendmail", (NetServerTelnet::CMD_HANDLER) &Telnet::handlerSendMail,
"Send an e-mail" },
21 {
"filetest", (NetServerTelnet::CMD_HANDLER) &Telnet::handlerFileSpeedTest,
"Test file system" },
22 {
"filecreate", (NetServerTelnet::CMD_HANDLER) &Telnet::handlerFileCreate,
"Create file" },
23 {
"filedelete", (NetServerTelnet::CMD_HANDLER) &Telnet::handlerFileDelete,
"Delete file" },
24 {
"filelist", (NetServerTelnet::CMD_HANDLER) &Telnet::handlerFileList,
"List files in a folder" },
25 {
"fileread", (NetServerTelnet::CMD_HANDLER) &Telnet::handlerFileRead,
"Read a file to screen" },
26 {
"filewrite", (NetServerTelnet::CMD_HANDLER) &Telnet::handlerFileWrite,
"Write to an existing file" },
27 {
"driveformat", (NetServerTelnet::CMD_HANDLER) &Telnet::handlerDriveFormat,
"Format drive" },
28 {
"stat", (NetServerTelnet::CMD_HANDLER) &Telnet::handlerGetStatistics,
"Get system statistics" },
37 void Telnet::handlerHelp(
int argc,
char *argv[]) {
38 for (
const ENTRY* entry = mEntries; entry->name; entry++) {
39 mSocket.printf(
"%-16s%s\r\n", entry->name, entry->descritpion);
43 void Telnet::handlerExit(
int argc,
char *argv[]) {
44 mSocket.writeByte(
'\0');
48 void Telnet::handlerSendMail(
int argc,
char *argv[]) {
50 mSocket.printf(
"Invalid arguments count, usage: sendmail to subject\r\n");
55 bool isSentOk =
false;
58 mSocket.printf(
"Start sending mail to:%s, subject:%s\r\n", argv[0], argv[1]);
59 if (smtp.start(
"smtp.mail.yahoo.com",
true,
"mlib.test@yahoo.com",
"JU9a*A#R$p",
"mlib.test@yahoo.com", argv[0], argv[1])) {
60 mSocket.printf(
"Type content[ctrl+c to finish]:\r\n");
64 int result = mSocket.read(&c, 1);
73 smtp.queueBody(
"%c", c);
78 mSocket.printf(
"\r\n\r\nEmail sent\r\n");
83 mSocket.printf(
"\r\n\r\nSend email error!\r\n");
86 smtp.closeConnection();
92 void Telnet::handlerFileSpeedTest(
int argc,
char *argv[]) {
94 FsDefs::RESULT result = file.create(
"0/TEST.TXT");
95 if (result != FsDefs::RES_SUCCESS) {
96 mSocket.printf(
"Create file error %s\r\n", FsDefs::getResultString(result));
100 const static size_t BUFFER_SIZE = 512;
101 uint8_t* buffer = (uint8_t*) malloc(BUFFER_SIZE);
104 mSocket.printf(
"Out of memory\r\n");
107 for (
size_t i = 0; i < BUFFER_SIZE; i++) {
108 buffer[i] = (i % 16) +
'A';
111 size_t totalTransfered = 0;
112 for (
int i = 0; i < 1024; i++) {
114 result = file.write(buffer, BUFFER_SIZE, &transfered);
115 totalTransfered += transfered;
116 if (result != FsDefs::RES_SUCCESS || transfered != BUFFER_SIZE) {
119 if ((i + 1) % 8 == 0) {
123 if ((i + 1) % (8 * 64) == 0) {
124 mSocket.printf(
"\r\n");
127 mSocket.printf(
"\r\n");
129 unsigned int speed = 0;
131 speed = totalTransfered / ms;
133 mSocket.printf(
"Written %ubytes in %lums (%ukBytes/s)\r\n", totalTransfered, ms, speed);
135 result = file.close();
136 result = file.open(
"0/TEST.TXT");
137 if (result != FsDefs::RES_SUCCESS) {
139 mSocket.printf(
"Open file error %s\r\n", FsDefs::getResultString(result));
145 for (
int i = 0; i < 1024; i++) {
147 result = file.read(buffer, BUFFER_SIZE, &transfered);
148 totalTransfered += transfered;
149 if (result != FsDefs::RES_SUCCESS || transfered != BUFFER_SIZE) {
152 if ((i + 1) % 8 == 0) {
156 if ((i + 1) % (8 * 64) == 0) {
157 mSocket.printf(
"\r\n");
160 mSocket.printf(
"\r\n");
164 speed = totalTransfered / ms;
166 mSocket.printf(
"Read %ubytes in %lums (%ukBytes/s)\r\n", totalTransfered, ms, speed);
169 result = file.del(
"0/TEST.TXT");
170 if (result != FsDefs::RES_SUCCESS) {
172 mSocket.printf(
"Delete file error %s\r\n", FsDefs::getResultString(result));
175 mSocket.printf(
"File deleted\r\n");
180 void Telnet::handlerFileCreate(
int argc,
char *argv[]) {
182 mSocket.printf(
"Invalid arguments count, usage: filecreate filename\r\n");
186 mSocket.printf(
"Creating file %s\r\n", argv[0]);
189 FsDefs::RESULT result = file.create(argv[0]);
190 if (result != FsDefs::RES_SUCCESS) {
191 mSocket.printf(
"Create file error %s\r\n", FsDefs::getResultString(result));
194 result = file.close();
195 if (result != FsDefs::RES_SUCCESS) {
196 mSocket.printf(
"Close file error %s\r\n", FsDefs::getResultString(result));
199 mSocket.printf(
"File created successfully\r\n");
202 void Telnet::handlerFileDelete(
int argc,
char *argv[]) {
204 mSocket.printf(
"Invalid arguments count, usage: filedelete filename\r\n");
208 FsDefs::RESULT result = file.del(argv[0]);
209 if (result != FsDefs::RES_SUCCESS) {
210 mSocket.printf(
"Create file error %s\r\n", FsDefs::getResultString(result));
213 mSocket.printf(
"File deleted successfully\r\n");
216 void Telnet::handlerFileList(
int argc,
char *argv[]) {
218 mSocket.printf(
"Invalid arguments count, usage: filelist directory\r\n");
223 FsDefs::RESULT result = dir.open(argv[0]);
224 if (result != FsDefs::RES_SUCCESS) {
225 mSocket.printf(
"Open directory error %s\r\n", FsDefs::getResultString(result));
230 FsDir::ENTRY_INFO info;
231 result = dir.getNextEntry(&info);
232 if (result != FsDefs::RES_SUCCESS) {
235 mSocket.printf(
"%04u-%02u-%02u ", info.createDate.bits.year + 1980, info.createDate.bits.month, info.createDate.bits.day);
236 mSocket.printf(
"%02uh%02um%02us ", info.createTime.bits.hours, info.createTime.bits.minutes, info.createTime.bits.seconds);
237 if (info.attributes.bits.directory) {
238 mSocket.printf(
" <DIR> ");
241 mSocket.printf(
"%10u ", (
unsigned int) info.fileSize);
243 if (info.longFileName) {
244 mSocket.printf(
"%s\r\n", info.longFileName);
247 mSocket.printf(
"%s\r\n", info.shortFileName);
251 result = dir.close();
252 if (result != FsDefs::RES_SUCCESS) {
253 mSocket.printf(
"Close directory error %s\r\n", FsDefs::getResultString(result));
259 void Telnet::handlerFileRead(
int argc,
char *argv[]) {
263 mSocket.printf(
"Invalid arguments count, usage: fileread filename\r\n");
267 FsDefs::RESULT result = file.open(argv[0]);
268 if (result != FsDefs::RES_SUCCESS) {
269 mSocket.printf(
"Open file error %s\r\n", FsDefs::getResultString(result));
273 const static size_t BUFFER_SIZE = 512;
274 uint8_t* buffer = (uint8_t*) malloc(BUFFER_SIZE);
277 mSocket.printf(
"Out of memory\r\n");
282 size_t totalTransfered = 0;
285 result = file.read(buffer, BUFFER_SIZE, &transfered);
286 mSocket.write(buffer, transfered);
287 totalTransfered += transfered;
288 if (result != FsDefs::RES_SUCCESS || transfered != BUFFER_SIZE) {
303 void Telnet::handlerFileWrite(
int argc,
char *argv[]) {
307 mSocket.printf(
"Invalid arguments count, usage: filewrite filename\r\n");
311 FsDefs::RESULT result = file.open(argv[0]);
312 if (result != FsDefs::RES_SUCCESS) {
313 mSocket.printf(
"Open file error %s\r\n", FsDefs::getResultString(result));
316 result = file.seek(file.getSize());
317 if (result != FsDefs::RES_SUCCESS) {
319 mSocket.printf(
"Seek file error %s\r\n", FsDefs::getResultString(result));
323 mSocket.printf(
"Type content[ctrl+c to finish]:\r\n");
327 int read = mSocket.read(&c, 1);
334 else if (c <= 0xFE) {
335 mSocket.writeByte(c);
337 result = file.write(&c, 1, &transfered);
338 if (result != FsDefs::RES_SUCCESS) {
339 mSocket.printf(
"\r\nWrite file error %s\r\n", FsDefs::getResultString(result));
344 mSocket.printf(
"\r\nFinished\r\n");
348 void Telnet::handlerDriveFormat(
int argc,
char *argv[]) {
350 mSocket.printf(
"Invalid arguments count, usage: driveformat\r\n");
354 mSocket.printf(
"Formating drive\r\n");
355 ansiSaveCursorPosition();
357 mCurrentPercent = -1;
358 FsDefs::RESULT result = Fs::format(&fsDriveSdio,
this);
359 mSocket.printf(
"\r\n");
360 if (result != FsDefs::RES_SUCCESS) {
361 mSocket.printf(
"Drive format error %s\r\n", FsDefs::getResultString(result));
365 mSocket.printf(
"Disk formatted\r\n");
369 void Telnet::handlerGetStatistics(
int argc,
char *argv[]) {
370 mSocket.printf(
"Heap:\r\n");
374 mSocket.printf(
"\r\nNet memory:\r\n");
375 mSocket.printf(
"\tTotal memory: %ubytes\r\n", Net::getTotalMemory());
376 mSocket.printf(
"\tFree memory: %ubytes\r\n", Net::getFreeMemory());
377 mSocket.printf(
"\tBlock count: %ublocks\r\n", Net::getBlockCount());
380 void Telnet::formatStatusUpdate(uint32_t current, uint32_t total) {
381 int percent = current * 100 / total;
382 if (mCurrentPercent != percent) {
383 ansiRestoreCursorPosition();
384 mCurrentPercent = percent;
385 mSocket.printf(
"%d%%", mCurrentPercent);
389 void Telnet::onConnection() {
390 USER_DEBUG(LibLog::LEVEL_VERBOSE,
"Telnet connected");
391 mSocket.printf(
"-EMB_KERNEL- (build on %s @ %s)\r\n[Type help for a list of commands]\r\n", __DATE__, __TIME__);
394 void Telnet::onDisconnection() {
395 USER_DEBUG(LibLog::LEVEL_VERBOSE,
"Telnet disconnected");