изменения в выводе пригласительной информации, изменения в пароле, убрана...

изменения в выводе пригласительной информации, изменения в пароле, убрана блокировка при неправильном вводе пароля 3 раза, так как затрудняет дальнейшую работу, теперь просто выводится ошибка а неизвестной команде на неправильный ввод пароля.
parent a7ae97e4
...@@ -60,10 +60,6 @@ static u8 cli_password_len = 0; // Фактическая д ...@@ -60,10 +60,6 @@ static u8 cli_password_len = 0; // Фактическая д
// Таблица обработчиков команд. Индекс = код команды (0..255) // Таблица обработчиков команд. Индекс = код команды (0..255)
static cli_cmd_handler_t cmd_handlers[CLI_MAX_COMMANDS]; static cli_cmd_handler_t cmd_handlers[CLI_MAX_COMMANDS];
// Защита от перебора пароля
static u8 cli_password_attempts = 0; // Счётчик неудачных попыток
static bool cli_blocked = false; // Флаг блокировки всех команд
//************************************ Прототипы локальных функций ****************************************** //************************************ Прототипы локальных функций ******************************************
static inline void byte_to_hex(u8 byte, char* hex); static inline void byte_to_hex(u8 byte, char* hex);
static u8 hex_to_byte(const char* hex); static u8 hex_to_byte(const char* hex);
...@@ -197,27 +193,27 @@ static void send_error(u8 cmd, u8 err) ...@@ -197,27 +193,27 @@ static void send_error(u8 cmd, u8 err)
static void handle_cmd_open(const u8* data, u8 len) static void handle_cmd_open(const u8* data, u8 len)
{ {
(void)len; (void)len;
// Проверяем, не открыто ли уже соединение
if (cli_connected) { if (cli_connected) {
send_error(CLI_CMD_OPEN, CLI_ERR_INTERNAL); send_error(CLI_CMD_OPEN, CLI_ERR_INTERNAL);
return; return;
} }
if (cli_password_attempts >= CLI_MAX_PASSWORD_ATTEMPTS) {
cli_blocked = true; // Проверка пароля, если он задан
return;
}
if (cli_password_len > 0) { if (cli_password_len > 0) {
if (len != cli_password_len || memcmp(data, cli_password, len) != 0) { if (len != cli_password_len || memcmp(data, cli_password, len) != 0) {
cli_password_attempts++; // Неверный пароль - возвращаем ошибку "неизвестная команда"
if (cli_password_attempts >= CLI_MAX_PASSWORD_ATTEMPTS) { send_error(CLI_CMD_OPEN, CLI_ERR_UNKNOWN_CMD);
cli_blocked = true;
}
send_error(CLI_CMD_OPEN, CLI_ERR_INTERNAL);
return; return;
} }
} }
cli_password_attempts = 0;
// Пароль верен или не требуется
cli_connected = true; cli_connected = true;
if (cli_get_tick) cli_last_activity = cli_get_tick(); if (cli_get_tick) {
cli_last_activity = cli_get_tick();
}
send_response(CLI_CMD_OPEN, NULL, 0); send_response(CLI_CMD_OPEN, NULL, 0);
} }
...@@ -461,15 +457,15 @@ fun_res_t cli_protocol_init(void) ...@@ -461,15 +457,15 @@ fun_res_t cli_protocol_init(void)
const u8 default_pass[] = CLI_DEFAULT_PASSWORD_BYTES; const u8 default_pass[] = CLI_DEFAULT_PASSWORD_BYTES;
cli_protocol_set_password(default_pass, sizeof(default_pass)); cli_protocol_set_password(default_pass, sizeof(default_pass));
cli_send_string("\r\n>----- LTA PROTOCOL ENABLED -----<\r\n"); cli_send_string("\r\n------ LTA PROTOCOL ENABLED -----\r\n");
cli_send_string("> T0001 - open without password\r\n"); cli_send_string("- T0001 - open without password\r\n");
cli_send_string("> T000131333537 - open with password '1357' (hex bytes 0x31,0x33,0x35,0x37)\r\n"); cli_send_string("- T000131333537 - open with password '1357' (hex bytes 0x31,0x33,0x35,0x37)\r\n");
cli_send_string("> T0101 - close connection\r\n"); cli_send_string("- T0101 - close connection\r\n");
cli_send_string("> T0201AABB - ping with data 0xAA,0xBB\r\n"); cli_send_string("- T0201AABB - ping with data 0xAA,0xBB\r\n");
cli_send_string("> T0A01 - get device info\r\n"); cli_send_string("- T0A01 - get device info\r\n");
cli_send_string("> T14010800000004 - read 4 bytes from 0x08000000\r\n"); cli_send_string("- T14010800000004 - read 4 bytes from 0x08000000\r\n");
cli_send_string("> T150120000000AABB- write 0xAA,0xBB to SRAM 0x20000000\r\n"); cli_send_string("- T150120000000AABB - write 0xAA,0xBB to SRAM 0x20000000\r\n");
cli_send_string(">------------------------------------------------<\r\n"); cli_send_string("--------------------------------------------------\r\n");
cli_send_string("> "); cli_send_string("> ");
return ERR_OK; return ERR_OK;
} }
...@@ -491,14 +487,6 @@ void cli_protocol_process(void) ...@@ -491,14 +487,6 @@ void cli_protocol_process(void)
EXPECT_NL // Ожидание \n после \r EXPECT_NL // Ожидание \n после \r
} state = WAIT_CMD; } state = WAIT_CMD;
if (cli_blocked) {
while (rb_get_items_qty(&fifo_rx) > 0) {
u8 dummy;
rb_get_item(&fifo_rx, &dummy);
}
return;
}
while (rb_get_items_qty(&fifo_rx) > 0) { while (rb_get_items_qty(&fifo_rx) > 0) {
u8 ch; u8 ch;
if (rb_get_item(&fifo_rx, &ch) != RB_RES_OK) continue; if (rb_get_item(&fifo_rx, &ch) != RB_RES_OK) continue;
...@@ -510,6 +498,7 @@ void cli_protocol_process(void) ...@@ -510,6 +498,7 @@ void cli_protocol_process(void)
cli_cmd_buffer[cli_cmd_index++] = ch; cli_cmd_buffer[cli_cmd_index++] = ch;
state = RECEIVING; state = RECEIVING;
break; break;
case RECEIVING: case RECEIVING:
if (ch == '\r' || ch == '\n') { if (ch == '\r' || ch == '\n') {
if (cli_cmd_index > 0) { if (cli_cmd_index > 0) {
...@@ -528,6 +517,7 @@ void cli_protocol_process(void) ...@@ -528,6 +517,7 @@ void cli_protocol_process(void)
} }
} }
break; break;
case EXPECT_NL: case EXPECT_NL:
if (ch == '\n') { if (ch == '\n') {
state = WAIT_CMD; state = WAIT_CMD;
...@@ -540,7 +530,7 @@ void cli_protocol_process(void) ...@@ -540,7 +530,7 @@ void cli_protocol_process(void)
} }
} }
// Таймаут соединения (работает только если cli_get_tick != NULL) // Таймаут соединения
if (cli_connected && cli_get_tick) { if (cli_connected && cli_get_tick) {
if ((cli_get_tick() - cli_last_activity) > CLI_CONNECTION_TIMEOUT_MS) { if ((cli_get_tick() - cli_last_activity) > CLI_CONNECTION_TIMEOUT_MS) {
cli_connected = false; cli_connected = false;
...@@ -586,13 +576,6 @@ bool cli_protocol_verify_password(const u8* pass, u8 len) ...@@ -586,13 +576,6 @@ bool cli_protocol_verify_password(const u8* pass, u8 len)
return (len == cli_password_len) && (memcmp(pass, cli_password, len) == 0); return (len == cli_password_len) && (memcmp(pass, cli_password, len) == 0);
} }
//-----------------------------------------------------------------------------------------------------------
void cli_protocol_reset_block(void)
{
cli_password_attempts = 0;
cli_blocked = false;
}
//----------------------------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------------------------
fun_res_t cli_register_command(const cli_command_t* cmd) fun_res_t cli_register_command(const cli_command_t* cmd)
{ {
......
...@@ -16,10 +16,13 @@ ...@@ -16,10 +16,13 @@
//************************************ Пароль по умолчанию ************************************************** //************************************ Пароль по умолчанию **************************************************
// Пароль по умолчанию "1357" // Пароль по умолчанию "1357"
#define CLI_DEFAULT_PASSWORD_BYTES {0x31, 0x33, 0x35, 0x37} #define CLI_DEFAULT_PASSWORD_BYTES {0x31, 0x33, 0x35, 0x37}
#define CLI_DEFAULT_PASSWORD_LEN 4 #define CLI_DEFAULT_PASSWORD_LEN 4
#define CLI_MAX_COMMANDS 256 // Максимальное количество регистрируемых команд (0x00..0xFF) #define CLI_MAX_COMMANDS 256 // Максимальное количество регистрируемых команд (0x00..0xFF)
#define CLI_PASSWORD_MAX_LEN 15 // Максимальная длина пароля в байтах #define CLI_PASSWORD_MAX_LEN 15 // Максимальная длина пароля в байтах
#define CLI_MAX_PASSWORD_ATTEMPTS 3 // Количество попыток ввода пароля до блокировки
#define CLI_ACTIVITY_TIMEOUT_TICKS 20000U // ~20 сек при вызове каждые 1 мс #define CLI_ACTIVITY_TIMEOUT_TICKS 20000U // ~20 сек при вызове каждые 1 мс
#endif #endif
\ No newline at end of file
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
//************************************ Глобальные (публичные) переменные ************************************ //************************************ Глобальные (публичные) переменные ************************************
//************************************* Локальные (приватные) переменные ************************************ //************************************* Локальные (приватные) переменные ************************************
...@@ -27,6 +28,7 @@ void main(void) ...@@ -27,6 +28,7 @@ void main(void)
cli_protocol_init(); cli_protocol_init();
cli_protocol_set_device_info("LTA27 MCU", "LTA27V2-001", "1.0.0", 0x01); cli_protocol_set_device_info("LTA27 MCU", "LTA27V2-001", "1.0.0", 0x01);
while(1) while(1)
{ {
streams_loop(); streams_loop();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment