Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
FinSH
finsh password 问题
发布于 2022-03-31 22:41:14 浏览:1227
订阅该版
```c * set a new password for finsh * * @param password new password * * @return result, RT_EOK on OK, -RT_ERROR on the new password length is less than * FINSH_PASSWORD_MIN or greater than FINSH_PASSWORD_MAX */ rt_err_t finsh_set_password(const char *password) { rt_ubase_t level; rt_size_t pw_len = rt_strlen(password); if (pw_len < FINSH_PASSWORD_MIN || pw_len > FINSH_PASSWORD_MAX) return -RT_ERROR; level = rt_hw_interrupt_disable(); rt_strncpy(shell->password, password, FINSH_PASSWORD_MAX); rt_hw_interrupt_enable(level); return RT_EOK; } /** * get the finsh password * * @return password */ const char *finsh_get_password(void) { return shell->password; } static void finsh_wait_auth(void) { char ch; rt_bool_t input_finish = RT_FALSE; char password[FINSH_PASSWORD_MAX] = { 0 }; rt_size_t cur_pos = 0; /* password not set */ if (rt_strlen(finsh_get_password()) == 0) return; while (1) { rt_kprintf("Password for login: "); while (!input_finish) { while (1) { /* read one character from device */ ch = finsh_getchar(); if (ch >= ' ' && ch <= '~' && cur_pos < FINSH_PASSWORD_MAX) { /* change the printable characters to '*' */ rt_kprintf("*"); password[cur_pos++] = ch; } else if (ch == '\b' && cur_pos > 0) { /* backspace */ password[cur_pos] = '\0'; cur_pos--; rt_kprintf("\b \b"); } else if (ch == '\r' || ch == '\n') { rt_kprintf("\n"); input_finish = RT_TRUE; break; } } } if (!rt_strncmp(shell->password, password, FINSH_PASSWORD_MAX)) return; else { /* authentication failed, delay 2S for retry */ rt_thread_delay(2 * RT_TICK_PER_SECOND); rt_kprintf("Sorry, try again.\n"); cur_pos = 0; input_finish = RT_FALSE; rt_memset(password, '\0', FINSH_PASSWORD_MAX); } } } ``` 这段代码,在那里引用。
查看更多
crystal266
2022-04-01
嵌入式
设置密码和密码验证的代码是在 finsh 线程的执行函数里面调用的,finsh 线程的创建代码如下: ```c /* 代码位于 rt-thread/components/finsh/shell.c 文件中 */ int finsh_system_init(void) { ... ... // 省略 #ifdef RT_USING_HEAP /* create or set shell structure */ shell = (struct finsh_shell *)rt_calloc(1, sizeof(struct finsh_shell)); if (shell == RT_NULL) { rt_kprintf("no memory for shell\n"); return -1; } /* 创建 finsh 线程 */ tid = rt_thread_create(FINSH_THREAD_NAME, finsh_thread_entry, RT_NULL, FINSH_THREAD_STACK_SIZE, FINSH_THREAD_PRIORITY, 10); #else shell = &_shell; tid = &finsh_thread; result = rt_thread_init(&finsh_thread, FINSH_THREAD_NAME, finsh_thread_entry, RT_NULL, &finsh_thread_stack[0], sizeof(finsh_thread_stack), FINSH_THREAD_PRIORITY, 10); #endif /* RT_USING_HEAP */ rt_sem_init(&(shell->rx_sem), "shrx", 0, 0); finsh_set_prompt_mode(1); if (tid != NULL && result == RT_EOK) rt_thread_startup(tid); /* 启动 finsh 线程 */ return 0; } INIT_APP_EXPORT(finsh_system_init); ``` 启动 finsh 线程后,该线程的处理执行函数如下:从代码可以看出线程执行函数上来一开始就执行了密码设置的工作,实质是把定义的密码拷贝到了 struct finsh_shell *shell; 这个全局的结构体指针变量里面。然后在 finsh_wait_auth() 函数里面等待用户输入密码,然后和 shell->password 存放的密码进行比较,比对一致接着往下执等待用户输入命令,比对不一致提示密码错误等待用户下一次输入。 ```c /* 代码位于 rt-thread/components/finsh/shell.c 文件中 */ void finsh_thread_entry(void *parameter) { ... ... // 省略 #ifdef FINSH_USING_AUTH /* set the default password when the password isn't setting */ if (rt_strlen(finsh_get_password()) == 0) { if (finsh_set_password(FINSH_DEFAULT_PASSWORD) != RT_EOK) // 设置密码 { rt_kprintf("Finsh password set failed.\n"); } } /* waiting authenticate success */ finsh_wait_auth(); // 密码验证,验证失败不往下运行一直等到验证成功 #endif rt_kprintf(FINSH_PROMPT); while (1) { ch = (int)finsh_getchar(); // 阻塞读取输入的字符 if (ch < 0) { continue; } ... ... // 省略 /* handle end of line, break */ if (ch == '\r' || ch == '\n') { #ifdef FINSH_USING_HISTORY shell_push_history(shell); #endif if (shell->echo_mode) rt_kprintf("\n"); msh_exec(shell->line, shell->line_position); // 查找输入的命令并执行 rt_kprintf(FINSH_PROMPT); memset(shell->line, 0, sizeof(shell->line)); shell->line_curpos = shell->line_position = 0; continue; } ... ... // 省略 } /* end of device read */ } ```
2
个回答
默认排序
按发布时间排序
出出啊
2022-04-01
恃人不如自恃,人之为己者不如己之自为也
我记得开启 auth 后只需要调用 finsh_set_password 设置一下密码就可以用了 登录的时候输入密码
撰写答案
登录
注册新账号
关注者
0
被浏览
1.2k
关于作者
韩永根
这家伙很懒,什么也没写!
提问
1
回答
0
被采纳
0
关注TA
发私信
相关问题
1
RT-THREAD shell无反应呢?
2
RT-thread2.0beta下用类似linux风格MSH,参数如何输入和导出
3
rt-thread finsh windows下的那个终端软件叫什么来着
4
板子上只有485接口,能把FINSH改造成485的么?
5
finsh最大字符问题
6
finsh命令个数是不是有限制啊
7
finsh支持转义字符吗
8
不用finsh如何知道堆栈使用量
9
强烈建议 RT-Thread下finsh原理深入分析
10
finsh输入命令全部返回null node
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
【RT-Thread】【ci】【scons】将ci.attachconfig.yml和scons结合使用
2
Rt-thread中OTA下载后,bootloader不搬程序
3
ulog 日志 LOG_HEX 输出时间改为本地日期时间
4
在RT-Thread Studio中构建前执行python命令
5
研究一了一段时间RTT,直接标准版上手太难,想用nano,但又舍不得组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
I2C_IIC
ESP8266
UART
WIZnet_W5500
ota在线升级
cubemx
PWM
flash
freemodbus
BSP
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
flashDB
GD32
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
SFUD
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
出出啊
1518
个答案
343
次被采纳
小小李sunny
1444
个答案
290
次被采纳
张世争
813
个答案
177
次被采纳
crystal266
547
个答案
161
次被采纳
whj467467222
1222
个答案
149
次被采纳
本月文章贡献
出出啊
1
篇文章
5
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
3
次点赞
crystal266
2
篇文章
2
次点赞
whj467467222
2
篇文章
2
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部