PX4 FMU启动流程 2. 三、nsh_session
PX4 FMU启动流程 2. 三、nsh_session-------- 转载请注明出处
-------- 2014-11-27.冷月追风
-------- email:merafour@163.com
有了前面的铺垫,这时候我们看 “nsh_session”函数就简单很多了。
int nsh_session(FAR struct console_stdio_s *pstate)
{
int ret;
DEBUGASSERT(pstate);
/* Present a greeting */
fputs(g_nshgreeting, pstate->cn_outstream);
fflush(pstate->cn_outstream);
/* Execute the login script */
#ifdef CONFIG_NSH_ROMFSRC
(void)nsh_loginscript(&pstate->cn_vtbl);
#endif
/* Then enter the command line parsing loop */
for (;;)
{
/* For the case of debugging the USB console... dump collected USB trace data */
#ifdef CONFIG_NSH_USBDEV_TRACE
nsh_usbtrace();
#endif
/* Display the prompt string */
fputs(g_nshprompt, pstate->cn_outstream);
fflush(pstate->cn_outstream);
/* Get the next line of input. readline() returns EOF on end-of-file
* or any read failure.
*/
ret = readline(pstate->cn_line, CONFIG_NSH_LINELEN,
INSTREAM(pstate), OUTSTREAM(pstate));
if (ret != EOF)
{
/* Parse process the command */
(void)nsh_parse(&pstate->cn_vtbl, pstate->cn_line);
fflush(pstate->cn_outstream);
}
/* Readline normally returns the number of characters read,
* but will return EOF on end of file or if an error occurs.
* EOF will cause the session to terminate.
*/
else
{
fprintf(pstate->cn_outstream, g_fmtcmdfailed, "nsh_session",
"readline", NSH_ERRNO_OF(-ret));
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
}
/* We do not get here, but this is necessary to keep some compilers happy.
* But others will complain that this code is not reachable.
*/
return EXIT_SUCCESS;
}
跟 “nsh_script”函数还是挺相似的。不同在于这里的命令行不是来自脚本文件,而事实来自 outstream,是什么呢?我们可以在 “nsh_newconsole”函数中找到。其实就是与用户交互的命令行终端。我们用上位机连上飞控的时候可以在一个窗口中用命令与飞控交互,这部分工作就是由这里完成的。
应该来说,从中断读数据是不会读到文件结束标志的,所以该循环不会跳出。
评论