Repository navigation
[drivers][serial] improve TIOCGWINSZ and termios updates #11891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BernardXiong
wants to merge
4
commits into
RT-Thread:master
Choose a base branch
from
BernardXiong:pr/serial-tiocgwinsz
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b717951
[drivers][serial] improve TIOCGWINSZ and termios updates
BernardXiong 390c395
[drivers][serial] apply clang-format to TIOCGWINSZ changes
BernardXiong cd8ac61
[drivers][serial] use tick delta and non-blocking TIOCGWINSZ query
BernardXiong 1bcfeb6
[drivers][serial] skip redundant UART configure for termios changes
BernardXiong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ | |
| #endif /* RT_SERIAL_BUF_STRATEGY_DROP */ | ||
|
|
||
| #ifdef RT_USING_POSIX_STDIO | ||
| #include <stdlib.h> | ||
| #include <unistd.h> | ||
| #include <fcntl.h> | ||
| #include <poll.h> | ||
|
|
@@ -1843,13 +1844,17 @@ static rt_err_t rt_serial_control(struct rt_device *dev, | |
| else | ||
| config.flowcontrol = RT_SERIAL_FLOWCONTROL_NONE; | ||
|
|
||
| ret = serial->ops->configure(serial, &config); | ||
| if (ret != RT_EOK) | ||
| /* Software-only termios changes must not reset the UART FIFO. */ | ||
| if (rt_memcmp(&config, &serial->config, sizeof(config)) != 0) | ||
| { | ||
| break; | ||
| ret = serial->ops->configure(serial, &config); | ||
| if (ret != RT_EOK) | ||
| { | ||
| break; | ||
| } | ||
| /* set serial configure */ | ||
| serial->config = config; | ||
| } | ||
| /* set serial configure */ | ||
| serial->config = config; | ||
| } | ||
| break; | ||
| case TCFLSH: | ||
|
|
@@ -1873,85 +1878,75 @@ static rt_err_t rt_serial_control(struct rt_device *dev, | |
| break; | ||
| case TIOCGWINSZ: | ||
| { | ||
| struct winsize *p_winsize; | ||
| p_winsize = (struct winsize *)args; | ||
|
|
||
| if (rt_thread_self() != rt_thread_find(FINSH_THREAD_NAME)) | ||
| struct winsize *winsize = (struct winsize *)args; | ||
| char response[32] = { 0 }; | ||
| const char query[] = "\033[18t"; | ||
| rt_tick_t start_tick; | ||
| rt_size_t i = 0; | ||
| unsigned long rows, columns; | ||
| char *end; | ||
| rt_int32_t old_rx_timeout; | ||
| rt_int32_t no_wait_timeout = RT_WAITING_NO; | ||
| rt_bool_t rx_timeout_changed = RT_FALSE; | ||
|
|
||
| if (winsize == RT_NULL) | ||
| { | ||
| /* only can be used in tshell thread; otherwise, return default size */ | ||
| p_winsize->ws_col = 80; | ||
| p_winsize->ws_row = 24; | ||
| ret = -RT_EINVAL; | ||
| break; | ||
| } | ||
| else | ||
| { | ||
| #include <shell.h> | ||
| #define _TIO_BUFLEN 20 | ||
| char _tio_buf[_TIO_BUFLEN]; | ||
| unsigned char cnt1, cnt2, cnt3, i; | ||
| char row_s[4], col_s[4]; | ||
| char *p; | ||
|
|
||
| rt_memset(_tio_buf, 0, _TIO_BUFLEN); | ||
| rt_memset(winsize, 0, sizeof(*winsize)); | ||
| winsize->ws_row = 24; | ||
| winsize->ws_col = 80; | ||
|
|
||
| /* A blocking serial read waits forever if the terminal does not | ||
| * answer; make the query non-blocking for its duration. */ | ||
| if (rt_device_control(dev, RT_SERIAL_CTRL_GET_RX_TIMEOUT, &old_rx_timeout) == RT_EOK && | ||
| rt_device_control(dev, RT_SERIAL_CTRL_SET_RX_TIMEOUT, &no_wait_timeout) == RT_EOK) | ||
| { | ||
| rx_timeout_changed = RT_TRUE; | ||
| } | ||
|
|
||
| /* send the command to terminal for getting the window size of the terminal */ | ||
| rt_kprintf("\033[18t"); | ||
| rt_device_write(dev, -1, query, sizeof(query) - 1); | ||
|
|
||
| /* waiting for the response from the terminal */ | ||
| i = 0; | ||
| while (i < _TIO_BUFLEN) | ||
| start_tick = rt_tick_get(); | ||
| while (i < sizeof(response) - 1 && | ||
| rt_tick_get_delta(start_tick) < rt_tick_from_millisecond(200)) | ||
| { | ||
| if (rt_device_read(dev, -1, &response[i], 1) == 1) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 是否需要判断之前是否设置过O_NONBLOCK,v2默认read是永久等待
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已处理,commit cd8ac61。
|
||
| { | ||
| _tio_buf[i] = finsh_getchar(); | ||
| if (_tio_buf[i] != 't') | ||
| { | ||
| i++; | ||
| } | ||
| else | ||
| if (response[i++] == 't') | ||
| { | ||
| break; | ||
| } | ||
| } | ||
| if (i == _TIO_BUFLEN) | ||
| { | ||
| /* buffer overloaded, and return default size */ | ||
| p_winsize->ws_col = 80; | ||
| p_winsize->ws_row = 24; | ||
| break; | ||
| } | ||
|
|
||
| /* interpreting data eg: "\033[8;1;15t" which means row is 1 and col is 15 (unit: size of ONE character) */ | ||
| rt_memset(row_s, 0, 4); | ||
| rt_memset(col_s, 0, 4); | ||
| cnt1 = 0; | ||
| while (cnt1 < _TIO_BUFLEN && _tio_buf[cnt1] != ';') | ||
| { | ||
| cnt1++; | ||
| } | ||
| cnt2 = ++cnt1; | ||
| while (cnt2 < _TIO_BUFLEN && _tio_buf[cnt2] != ';') | ||
| { | ||
| cnt2++; | ||
| } | ||
| p = row_s; | ||
| while (cnt1 < cnt2) | ||
| { | ||
| *p++ = _tio_buf[cnt1++]; | ||
| } | ||
| p = col_s; | ||
| cnt2++; | ||
| cnt3 = rt_strlen(_tio_buf) - 1; | ||
| while (cnt2 < cnt3) | ||
| else | ||
| { | ||
| *p++ = _tio_buf[cnt2++]; | ||
| rt_thread_mdelay(1); | ||
| } | ||
| } | ||
|
|
||
| /* load the window size date */ | ||
| p_winsize->ws_col = atoi(col_s); | ||
| p_winsize->ws_row = atoi(row_s); | ||
| #undef _TIO_BUFLEN | ||
| if (rx_timeout_changed) | ||
| { | ||
| rt_device_control(dev, RT_SERIAL_CTRL_SET_RX_TIMEOUT, &old_rx_timeout); | ||
| } | ||
|
|
||
| p_winsize->ws_xpixel = 0; /* unused */ | ||
| p_winsize->ws_ypixel = 0; /* unused */ | ||
| if (rt_memcmp(response, "\033[8;", 4) != 0) | ||
| { | ||
| break; | ||
| } | ||
| rows = strtoul(response + 4, &end, 10); | ||
| if (*end != ';') | ||
| { | ||
| break; | ||
| } | ||
| columns = strtoul(end + 1, &end, 10); | ||
| if (*end == 't' && end[1] == '\0' && | ||
| rows != 0 && rows <= 999 && columns != 0 && columns <= 999) | ||
| { | ||
| winsize->ws_row = rows; | ||
| winsize->ws_col = columns; | ||
| } | ||
| } | ||
| break; | ||
| case FIONREAD: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
v2 版本是否也需要添加该逻辑
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已同步到 v2(commit 1bcfeb6):
dev_serial_v2.c的TCSETS/TCSETSW/TCSETSF路径现在也会先比较新配置与serial->config,没有变化就跳过ops->configure(),避免仅修改软件 termios(如 ONLCR/ICANON)时重新配置 UART 并重置 FIFO。