bakalarka/main/main.c

246 lines
7.5 KiB
C
Raw Normal View History

2020-03-20 17:01:25 +00:00
/* Console example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <string.h>
#include "esp_system.h"
#include "esp_log.h"
#include "esp_console.h"
#include "esp_vfs_dev.h"
#include "driver/uart.h"
#include "linenoise/linenoise.h"
#include "argtable3/argtable3.h"
#include "cmd_decl.h"
#include "esp_vfs_fat.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "esp_netif.h"
#include <esp_event.h>
2020-03-21 19:55:47 +00:00
#include <file.h>
2020-03-20 17:01:25 +00:00
2020-03-29 02:08:36 +00:00
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_freertos_hooks.h"
#include "freertos/semphr.h"
#include "esp_system.h"
#include "driver/gpio.h"
/* Littlevgl specific */
#include "lvgl/lvgl.h"
#include "lvgl_driver.h"
#include "lv_examples/lv_apps/demo/demo.h"
#include "display.h"
2020-03-30 11:21:12 +00:00
#include "esp_netif.h"
struct ca_status display = {
.wifi = false,
.webserver = false,
.serial = 0,
.init = false
};
2020-03-29 02:08:36 +00:00
2020-03-20 17:01:25 +00:00
static const char* TAG = "konzola";
/* Console command history can be stored to and loaded from a file.
* The easiest way to do this is to use FATFS filesystem on top of
* wear_levelling library.
*/
2020-04-05 10:27:20 +00:00
/*Historia prikazov moze byt ukladana a nacitana zo suboru*/
2020-03-20 17:01:25 +00:00
#if CONFIG_STORE_HISTORY
2020-04-05 10:27:20 +00:00
#define HISTORY_PATH "/spiffs/history.txt"
2020-03-20 17:01:25 +00:00
#endif // CONFIG_STORE_HISTORY
static void initialize_nvs(void)
{
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK( nvs_flash_erase() );
err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);
}
static void initialize_console(void)
{
/* Drain stdout before reconfiguring it */
fflush(stdout);
fsync(fileno(stdout));
/* Disable buffering on stdin */
setvbuf(stdin, NULL, _IONBF, 0);
/* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
/* Move the caret to the beginning of the next line on '\n' */
esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
/* Configure UART. Note that REF_TICK is used so that the baud rate remains
* correct while APB frequency is changing in light sleep mode.
*/
const uart_config_t uart_config = {
.baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.source_clk = UART_SCLK_REF_TICK,
};
/* Install UART driver for interrupt-driven reads and writes */
ESP_ERROR_CHECK( uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM,
256, 0, 0, NULL, 0) );
ESP_ERROR_CHECK( uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config) );
/* Tell VFS to use UART driver */
esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
/* Initialize the console */
esp_console_config_t console_config = {
.max_cmdline_args = 8,
.max_cmdline_length = 256,
#if CONFIG_LOG_COLORS
.hint_color = atoi(LOG_COLOR_CYAN)
#endif
};
ESP_ERROR_CHECK( esp_console_init(&console_config) );
/* Configure linenoise line completion library */
/* Enable multiline editing. If not set, long commands will scroll within
* single line.
*/
linenoiseSetMultiLine(1);
/* Tell linenoise where to get command completions and hints */
linenoiseSetCompletionCallback(&esp_console_get_completion);
linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
/* Set command history size */
linenoiseHistorySetMaxLen(100);
#if CONFIG_STORE_HISTORY
/* Load command history from filesystem */
linenoiseHistoryLoad(HISTORY_PATH);
#endif
}
void app_main(void)
{
2020-03-31 16:12:53 +00:00
/* Vytvorenie ulohy pre displej*/
xTaskCreatePinnedToCore(guiTask, "gui", 4096, &display, 0, NULL, 1);
2020-03-29 02:08:36 +00:00
2020-03-20 17:01:25 +00:00
initialize_nvs();
2020-03-31 16:12:53 +00:00
/*Inicializacia SPIFFS*/
2020-03-21 19:55:47 +00:00
init_memory();
2020-03-20 17:01:25 +00:00
#if CONFIG_STORE_HISTORY
ESP_LOGI(TAG, "Command history enabled");
#else
ESP_LOGI(TAG, "Command history disabled");
#endif
2020-03-31 16:12:53 +00:00
/*Inicializacia TCP/IP zasobnika*/
2020-03-20 17:01:25 +00:00
ESP_ERROR_CHECK(esp_netif_init());
2020-03-31 16:12:53 +00:00
/*Vytvorenie specialneho event loopu pre systemove eventy*/
2020-03-20 17:01:25 +00:00
ESP_ERROR_CHECK(esp_event_loop_create_default());
2020-04-05 10:27:20 +00:00
2020-03-20 17:01:25 +00:00
initialize_console();
2020-03-31 16:12:53 +00:00
/* Registrovanie príkazov */
2020-04-05 10:27:20 +00:00
/*Registrovanie prikazu help ktory vypise vsetky prikazy*/
2020-03-20 17:01:25 +00:00
esp_console_register_help_command();
2020-04-05 10:27:20 +00:00
/*Registrovanie prikazov free pre zobrazenie aktualnej velkosti zasobnika,
*heap pre zobrazenie velkosti zasobnika pri spusteni zariadenia,
*restart pre restartovanie zariadenia,
*/
2020-03-20 17:01:25 +00:00
register_system();
2020-04-05 10:27:20 +00:00
/*Registrovanie prikazu join pre pripojenie na wifi*/
2020-03-20 17:01:25 +00:00
register_wifi();
2020-04-05 10:27:20 +00:00
//
2020-03-20 17:01:25 +00:00
register_nvs();
2020-04-05 10:27:20 +00:00
/*Registrovanie prikazu write_cert pre vytvorenie certifikatov*/
2020-03-20 17:01:25 +00:00
register_ca();
2020-04-05 10:27:20 +00:00
/*Registrovanie prikazu server_on pre HTTP + SSL servera*/
2020-03-20 17:01:25 +00:00
register_server();
2020-04-05 10:27:20 +00:00
/*Registrovanie prikazu pre generovanie verejnych klucov*/
2020-03-20 17:01:25 +00:00
register_gen_key();
2020-03-31 16:12:53 +00:00
read_file("/spiffs/keyfile.key");
read_file("/spiffs/ca.crt");
read_file("/spiffs/user.crt");
2020-03-20 17:01:25 +00:00
/* Prompt to be printed before each line.
* This can be customized, made dynamic, etc.
*/
2020-03-31 16:12:53 +00:00
2020-03-20 17:01:25 +00:00
const char* prompt = LOG_COLOR_I "esp32> " LOG_RESET_COLOR;
printf("\n"
"Type 'help' to get the list of commands.\n"
"Use UP/DOWN arrows to navigate through command history.\n"
"Press TAB when typing command name to auto-complete.\n");
/* Figure out if the terminal supports escape sequences */
int probe_status = linenoiseProbe();
if (probe_status) { /* zero indicates success */
printf("\n"
"Your terminal application does not support escape sequences.\n"
"Line editing and history features are disabled.\n"
"On Windows, try using Putty instead.\n");
linenoiseSetDumbMode(1);
#if CONFIG_LOG_COLORS
/* Since the terminal doesn't support escape sequences,
* don't use color codes in the prompt.
*/
prompt = "esp32> ";
#endif //CONFIG_LOG_COLORS
}
/* Main loop */
while(true) {
/* Get a line using linenoise.
* The line is returned when ENTER is pressed.
*/
char* line = linenoise(prompt);
if (line == NULL) { /* Ignore empty lines */
continue;
}
/* Add the command to the history */
linenoiseHistoryAdd(line);
#if CONFIG_STORE_HISTORY
/* Save command history to filesystem */
linenoiseHistorySave(HISTORY_PATH);
#endif
/* Try to run the command */
int ret;
2020-04-05 10:27:20 +00:00
/*Rozparsovanie riadku na argumenty, prvy argument je brany ako prikaz
*v pripade ze prikaz nie je registrovany tak funkcia vrati hodnotu ESP_ERR_NOT_FOUND
*/
2020-03-20 17:01:25 +00:00
esp_err_t err = esp_console_run(line, &ret);
if (err == ESP_ERR_NOT_FOUND) {
printf("Unrecognized command\n");
} else if (err == ESP_ERR_INVALID_ARG) {
// command was empty
} else if (err == ESP_OK && ret != ESP_OK) {
printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(ret));
} else if (err != ESP_OK) {
printf("Internal error: %s\n", esp_err_to_name(err));
}
/* linenoise allocates line buffer on the heap, so need to free it */
linenoiseFree(line);
}
}