Skip to content

Commit db1d45e

Browse files
committed
Addition of console samples.
1 parent 809dbbe commit db1d45e

File tree

10 files changed

+1417
-0
lines changed

10 files changed

+1417
-0
lines changed

console/main.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
extern "C" {
2+
void app_main(void);
3+
}
4+
5+
extern void test_linenoise();
6+
extern void test_argtable();
7+
extern void test_console();
8+
9+
void app_main(void)
10+
{
11+
//test_linenoise();
12+
//test_argtable();
13+
test_console();
14+
}

console/test_argtable.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Test of argtable
3+
*
4+
* @author Neil Kolban
5+
* @date 2018-05-26
6+
*
7+
*/
8+
#include <stdio.h>
9+
#include <linenoise/linenoise.h>
10+
#include <stdint.h>
11+
#include <esp_console.h>
12+
#include <esp_vfs_dev.h>
13+
#include <esp_err.h>
14+
#include <driver/uart.h>
15+
#include <argtable3/argtable3.h>
16+
#include "sdkconfig.h"
17+
18+
void test_argtable(void)
19+
{
20+
// Boiler plate setup for using linenoise
21+
setvbuf(stdin, NULL, _IONBF, 0);
22+
setvbuf(stdout, NULL, _IONBF, 0);
23+
esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
24+
esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
25+
uart_driver_install((uart_port_t)CONFIG_CONSOLE_UART_NUM, 256, 0, 0, NULL, 0);
26+
esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM);
27+
28+
29+
linenoiseClearScreen();
30+
linenoiseSetMultiLine(0);
31+
32+
struct arg_lit* help = arg_lit0("h", "help", "Generate some help!");
33+
struct arg_lit* test = arg_lit1("t", "test", "Generate some help!");
34+
struct arg_end* end = arg_end(20);
35+
36+
void *argtable[] = {
37+
help,
38+
test,
39+
end
40+
};
41+
42+
char *argv[10];
43+
44+
while(1) {
45+
char* line = linenoise("Enter command > ");
46+
if (line != NULL) {
47+
printf("Got: %s\n", line);
48+
size_t argc = esp_console_split_argv(line, argv, 10);
49+
printf("Parsed to %d argc count\n", argc);
50+
for (int i=0; i<argc; i++) {
51+
printf("argv[%d]: %s\n", i, argv[i]);
52+
}
53+
int numErrors = arg_parse(argc, argv, argtable);
54+
if (numErrors>0) {
55+
printf("Number of errors: %d\n", numErrors);
56+
arg_print_errors(stdout, end, "myprog");
57+
arg_print_syntaxv(stdout, argtable, "Here:");
58+
}
59+
else {
60+
if (help->count > 0) {
61+
printf("Found help!\n");
62+
}
63+
}
64+
linenoiseHistoryAdd(line);
65+
linenoiseFree(line);
66+
} // Line is not null
67+
printf("--------------\n");
68+
} // End while loop
69+
}

console/test_console.cpp

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* Test of console
3+
*
4+
* @author Neil Kolban
5+
* @date 2018-05-26
6+
*
7+
*/
8+
#include <stdio.h>
9+
#include <linenoise/linenoise.h>
10+
#include <stdint.h>
11+
#include <esp_console.h>
12+
#include <esp_vfs_dev.h>
13+
#include <esp_err.h>
14+
#include <driver/uart.h>
15+
#include <argtable3/argtable3.h>
16+
#include "sdkconfig.h"
17+
18+
19+
struct arg_lit* show_arg_show = arg_lit0("s", "show", "generate show");
20+
struct arg_end* show_arg_end = arg_end(10);
21+
void *show_argtable[] = {
22+
show_arg_show,
23+
show_arg_end
24+
};
25+
26+
int runShow(int argc, char *argv[]) {
27+
printf("Found show!\n");
28+
return 0;
29+
}
30+
31+
struct arg_str* greet_arg_name = arg_str1("n", "name", "<string>", "generate help");
32+
struct arg_end* greet_arg_end = arg_end(10);
33+
void *greet_argtable[] = {
34+
greet_arg_name,
35+
greet_arg_end
36+
};
37+
38+
int runGreet(int argc, char *argv[]) {
39+
printf("Found Greet!\n");
40+
int numErrors = arg_parse(argc, argv, greet_argtable);
41+
if (numErrors > 0) {
42+
arg_print_errors(stdout, greet_arg_end, "greet");
43+
} else {
44+
printf("Hello %s\n", greet_arg_name->sval[0]);
45+
}
46+
return 0;
47+
}
48+
49+
void test_console(void)
50+
{
51+
// Boiler plate setup for using linenoise
52+
setvbuf(stdin, NULL, _IONBF, 0);
53+
setvbuf(stdout, NULL, _IONBF, 0);
54+
esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
55+
esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
56+
uart_driver_install((uart_port_t)CONFIG_CONSOLE_UART_NUM, 256, 0, 0, NULL, 0);
57+
esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM);
58+
59+
esp_console_config_t consoleConfig;
60+
consoleConfig.max_cmdline_args = 5;
61+
consoleConfig.max_cmdline_length = 100;
62+
63+
esp_console_init(&consoleConfig);
64+
esp_console_register_help_command();
65+
66+
esp_console_cmd_t consoleCmd;
67+
consoleCmd.command = "show";
68+
consoleCmd.func = runShow;
69+
consoleCmd.help = "Show something";
70+
consoleCmd.argtable = show_argtable;
71+
esp_console_cmd_register(&consoleCmd);
72+
73+
consoleCmd.command = "greet";
74+
consoleCmd.func = runGreet;
75+
consoleCmd.help = "Greet someone";
76+
consoleCmd.argtable = greet_argtable;
77+
esp_console_cmd_register(&consoleCmd);
78+
79+
80+
linenoiseClearScreen();
81+
linenoiseSetMultiLine(0);
82+
83+
84+
while(1) {
85+
char* line = linenoise("Enter command > ");
86+
if (line != NULL) {
87+
printf("Got: %s\n", line);
88+
int ret;
89+
esp_console_run(line, &ret);
90+
linenoiseHistoryAdd(line);
91+
linenoiseFree(line);
92+
} // Line is not null
93+
printf("--------------\n");
94+
} // End while loop
95+
}

console/test_linenoise.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Test of linenoise
3+
*
4+
* @author Neil Kolban
5+
* @date 2018-05-26
6+
*
7+
*/
8+
#include <stdio.h>
9+
#include <linenoise/linenoise.h>
10+
#include <stdint.h>
11+
#include <esp_console.h>
12+
#include <esp_vfs_dev.h>
13+
#include <esp_err.h>
14+
#include <driver/uart.h>
15+
#include "sdkconfig.h"
16+
17+
void test_linenoise(void)
18+
{
19+
// Boiler plate setup for using linenoise
20+
setvbuf(stdin, NULL, _IONBF, 0);
21+
setvbuf(stdout, NULL, _IONBF, 0);
22+
esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
23+
esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
24+
uart_driver_install((uart_port_t)CONFIG_CONSOLE_UART_NUM, 256, 0, 0, NULL, 0);
25+
esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM);
26+
27+
28+
linenoiseClearScreen();
29+
linenoiseSetMultiLine(0);
30+
31+
while(1) {
32+
char* line = linenoise("Enter command > ");
33+
if (line != NULL) {
34+
printf("Got: %s\n", line);
35+
linenoiseHistoryAdd(line);
36+
linenoiseFree(line);
37+
} // Line is not null
38+
printf("--------------\n");
39+
} // End while loop
40+
}

eclipse/c_includes.xml

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<includepath>${IDF_PATH}/components/aws_iot/aws-iot-device-sdk-embedded-C/include</includepath>
7575
<includepath>${IDF_PATH}/components/fatfs/src</includepath>
7676
<includepath>${IDF_PATH}/components/wear_levelling/include</includepath>
77+
<includepath>${IDF_PATH}/components/console</includepath>
7778

7879
</language>
7980
<language name="GNU C">

networking/FTPServer/FTPCallbacks.cpp

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
#include "FTPServer.h"
2+
#include <stdint.h>
3+
#include <fstream>
4+
#include <dirent.h>
5+
6+
7+
/**
8+
* Called at the start of a STOR request. The file name is the name of the file the client would like to
9+
* save.
10+
*/
11+
void FTPFileCallbacks::onStoreStart(std::string fileName) {
12+
printf(">> FTPFileCallbacks::onStoreStart: fileName=%s\n", fileName.c_str());
13+
m_storeFile.open(fileName, std::ios::binary); // Open the file for writing.
14+
if (m_storeFile.fail()) {
15+
throw FTPServer::FileException();
16+
}
17+
printf("<< FTPFileCallbacks::onStoreStart\n");
18+
} // FTPFileCallbacks#onStoreStart
19+
20+
21+
/**
22+
* Called when the client presents a new chunk of data to be saved.
23+
*/
24+
size_t FTPFileCallbacks::onStoreData(uint8_t* data, size_t size) {
25+
printf(">> FTPFileCallbacks::onStoreData: size=%ld\n", size);
26+
m_storeFile.write((char *)data, size); // Store data received.
27+
printf("<< FTPFileCallbacks::onStoreData: size=%ld\n", size);
28+
return size;
29+
} // FTPFileCallbacks#onStoreData
30+
31+
32+
/**
33+
* Called at the end of a STOR request. This indicates that the client has completed its transmission of the
34+
* file.
35+
*/
36+
void FTPFileCallbacks::onStoreEnd() {
37+
printf(">> FTPFileCallbacks::onStoreEnd\n");
38+
m_storeFile.close(); // Close the open file.
39+
printf("<< FTPFileCallbacks::onStoreEnd\n");
40+
} // FTPFileCallbacks#onStoreEnd
41+
42+
43+
/**
44+
* Called when the client requests retrieval of a file.
45+
*/
46+
void FTPFileCallbacks::onRetrieveStart(std::string fileName) {
47+
printf(">> FTPFileCallbacks::onRetrieveStart: fileName=%s\n", fileName.c_str());
48+
m_byteCount = 0;
49+
m_retrieveFile.open(fileName, std::ios::binary);
50+
if (m_retrieveFile.fail()) {
51+
printf("<< FTPFileCallbacks::onRetrieveStart: ***FileException***\n");
52+
throw FTPServer::FileException();
53+
}
54+
printf("<< FTPFileCallbacks::onRetrieveStart\n");
55+
} // FTPFileCallbacks#onRetrieveStart
56+
57+
58+
/**
59+
* Called when the client is ready to receive the next piece of the file. To indicate that there
60+
* is no more data to send, return a size of 0.
61+
* @param data The data buffer that we can fill to return data back to the client.
62+
* @param size The maximum size of the data buffer that we can populate.
63+
* @return The size of data being returned. Return 0 to indicate that there is no more data to return.
64+
*/
65+
size_t FTPFileCallbacks::onRetrieveData(uint8_t* data, size_t size) {
66+
printf(">> FTPFileCallbacks::onRetrieveData\n");
67+
m_retrieveFile.read((char *)data, size);
68+
size_t readSize = m_retrieveFile.gcount();
69+
m_byteCount += readSize;
70+
printf("<< FTPFileCallbacks::onRetrieveData: sizeRead=%ld\n", readSize);
71+
return m_retrieveFile.gcount(); // Return the number of bytes read.
72+
} // FTPFileCallbacks#onRetrieveData
73+
74+
75+
/**
76+
* Called when the retrieval has been completed.
77+
*/
78+
void FTPFileCallbacks::onRetrieveEnd() {
79+
printf(">> FTPFileCallbacks::onRetrieveEnd\n");
80+
m_retrieveFile.close();
81+
printf("<< FTPFileCallbacks::onRetrieveEnd: bytesTransmitted=%d\n", m_byteCount);
82+
} // FTPFileCallbacks#onRetrieveEnd
83+
84+
85+
/**
86+
* Return a list of files in the file system.
87+
* @return a list of files in the file system.
88+
*/
89+
std::string FTPFileCallbacks::onDir() {
90+
91+
DIR* dir = opendir(FTPServer::getCurrentDirectory().c_str());
92+
std::stringstream ss;
93+
while(1) {
94+
struct dirent* pDirentry = readdir(dir);
95+
if (pDirentry == nullptr) {
96+
break;
97+
}
98+
ss << pDirentry->d_name << "\r\n";
99+
}
100+
closedir(dir);
101+
return ss.str();
102+
} // FTPFileCallbacks#onDir
103+
104+
105+
/// ---- END OF FTPFileCallbacks
106+
107+
108+
void FTPCallbacks::onStoreStart(std::string fileName) {
109+
printf(">> FTPCallbacks::onStoreStart: fileName=%s\n", fileName.c_str());
110+
printf("<< FTPCallbacks::onStoreStart\n");
111+
} // FTPCallbacks#onStoreStart
112+
113+
114+
size_t FTPCallbacks::onStoreData(uint8_t* data, size_t size) {
115+
printf(">> FTPCallbacks::onStoreData: size=%ld\n", size);
116+
printf("<< FTPCallbacks::onStoreData\n");
117+
return 0;
118+
} // FTPCallbacks#onStoreData
119+
120+
121+
void FTPCallbacks::onStoreEnd() {
122+
printf(">> FTPCallbacks::onStoreEnd\n");
123+
printf("<< FTPCallbacks::onStoreEnd\n");
124+
} // FTPCallbacks#onStoreEnd
125+
126+
127+
void FTPCallbacks::onRetrieveStart(std::string fileName) {
128+
printf(">> FTPCallbacks::onRetrieveStart\n");
129+
printf("<< FTPCallbacks::onRetrieveStart\n");
130+
} // FTPCallbacks#onRetrieveStart
131+
132+
133+
size_t FTPCallbacks::onRetrieveData(uint8_t *data, size_t size) {
134+
printf(">> FTPCallbacks::onRetrieveData\n");
135+
printf("<< FTPCallbacks::onRetrieveData: 0\n");
136+
return 0;
137+
} // FTPCallbacks#onRetrieveData
138+
139+
140+
void FTPCallbacks::onRetrieveEnd() {
141+
printf(">> FTPCallbacks::onRetrieveEnd\n");
142+
printf("<< FTPCallbacks::onRetrieveEnd\n");
143+
} // FTPCallbacks#onRetrieveEnd
144+
145+
146+
std::string FTPCallbacks::onDir() {
147+
printf(">> FTPCallbacks::onDir\n");
148+
printf("<< FTPCallbacks::onDir\n");
149+
return "";
150+
} // FTPCallbacks#onDir
151+
152+
FTPCallbacks::~FTPCallbacks() {
153+
154+
} // FTPCallbacks#~FTPCallbacks

0 commit comments

Comments
 (0)