tty.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-12-07 linzhenxing first version
  9. * 2023-06-26 WangXiaoyao fix bug on foreground app switch
  10. */
  11. #include <dfs_file.h>
  12. #include <dfs_fs.h>
  13. #include <lwp.h>
  14. #include <rtdevice.h>
  15. #include <rtthread.h>
  16. #include <tty.h>
  17. #include <tty_ldisc.h>
  18. #include <shell.h>
  19. #if defined(RT_USING_POSIX_DEVIO)
  20. #include <termios.h>
  21. #endif
  22. #define DBG_TAG "TTY"
  23. #ifdef RT_TTY_DEBUG
  24. #define DBG_LVL DBG_LOG
  25. #else
  26. #define DBG_LVL DBG_INFO
  27. #endif /* RT_TTY_DEBUG */
  28. #include <rtdbg.h>
  29. const struct termios tty_std_termios = { /* for the benefit of tty drivers */
  30. .c_iflag = IMAXBEL | IUCLC | INLCR | ICRNL | IGNPAR,
  31. .c_oflag = OPOST,
  32. .c_cflag = B38400 | CS8 | CREAD | HUPCL,
  33. .c_lflag = ISIG | ECHOE | TOSTOP | NOFLSH,
  34. RT_NULL,/* .c_line = N_TTY, */
  35. .c_cc = INIT_C_CC,
  36. .__c_ispeed = 38400,
  37. .__c_ospeed = 38400
  38. };
  39. void tty_initstack(struct tty_node *node)
  40. {
  41. node->lwp = RT_NULL;
  42. node->next = RT_NULL;
  43. }
  44. static struct tty_node tty_node_cache = { RT_NULL, RT_NULL };
  45. static struct tty_node *_tty_node_alloc(void)
  46. {
  47. struct tty_node *node = tty_node_cache.next;
  48. if (node == RT_NULL)
  49. {
  50. node = rt_calloc(1, sizeof(struct tty_node));
  51. }
  52. else
  53. {
  54. tty_node_cache.next = node->next;
  55. }
  56. return node;
  57. }
  58. static void _tty_node_free(struct tty_node *node)
  59. {
  60. node->next = tty_node_cache.next;
  61. tty_node_cache.next = node;
  62. }
  63. int tty_push(struct tty_node **head, struct rt_lwp *lwp)
  64. {
  65. struct tty_node *node = _tty_node_alloc();
  66. if (!node)
  67. {
  68. return -1;
  69. }
  70. node->lwp = lwp;
  71. node->next = *head;
  72. *head = node;
  73. return 0;
  74. }
  75. struct rt_lwp *tty_pop(struct tty_node **head, struct rt_lwp *target_lwp)
  76. {
  77. struct tty_node *node;
  78. struct rt_lwp *lwp = RT_NULL;
  79. if (!head || !*head)
  80. {
  81. return RT_NULL;
  82. }
  83. node = *head;
  84. if (target_lwp != RT_NULL && node->lwp != target_lwp)
  85. {
  86. struct tty_node *prev = RT_NULL;
  87. while (node != RT_NULL && node->lwp != target_lwp)
  88. {
  89. prev = node;
  90. node = node->next;
  91. }
  92. if (node != RT_NULL)
  93. {
  94. /* prev is impossible equ RT_NULL */
  95. prev->next = node->next;
  96. lwp = target_lwp;
  97. _tty_node_free(node);
  98. }
  99. }
  100. else
  101. {
  102. lwp = (*head)->lwp;
  103. *head = (*head)->next;
  104. node->lwp = RT_NULL;
  105. _tty_node_free(node);
  106. }
  107. return lwp;
  108. }
  109. /**
  110. * tty_check_change - check for POSIX terminal changes
  111. * @tty: tty to check
  112. *
  113. * If we try to write to, or set the state of, a terminal and we're
  114. * not in the foreground, send a SIGTTOU. If the signal is blocked or
  115. * ignored, go ahead and perform the operation. (POSIX 7.2)
  116. *
  117. * Locking: ctrl_lock
  118. */
  119. int __tty_check_change(struct tty_struct *tty, int sig)
  120. {
  121. pid_t pgrp = 0, tty_pgrp = 0;
  122. int ret = 0;
  123. int level = 0;
  124. level = rt_hw_interrupt_disable();
  125. if (current == RT_NULL)
  126. {
  127. rt_hw_interrupt_enable(level);
  128. return 0;
  129. }
  130. if (current->tty != tty)
  131. {
  132. rt_hw_interrupt_enable(level);
  133. return 0;
  134. }
  135. pgrp = current->__pgrp;
  136. tty_pgrp = tty->pgrp;
  137. if (tty_pgrp && (pgrp != tty->pgrp))
  138. {
  139. lwp_signal_kill(current, sig, SI_USER, 0);
  140. }
  141. rt_hw_interrupt_enable(level);
  142. if (!tty_pgrp)
  143. {
  144. LOG_D("sig=%d, tty->pgrp == -1!\n", sig);
  145. }
  146. return ret;
  147. }
  148. int tty_check_change(struct tty_struct *tty)
  149. {
  150. return __tty_check_change(tty, SIGTTOU);
  151. }
  152. static int tty_open(struct dfs_file *fd)
  153. {
  154. int ret = 0;
  155. int noctty = 0;
  156. struct tty_struct *tty = RT_NULL;
  157. struct tty_ldisc *ld = RT_NULL;
  158. tty = (struct tty_struct *)fd->vnode->data;
  159. RT_ASSERT(tty != RT_NULL);
  160. ld = tty->ldisc;
  161. if (ld->ops->open)
  162. {
  163. ret = ld->ops->open(fd);
  164. }
  165. noctty = (fd->flags & O_NOCTTY);
  166. rt_device_t device = (rt_device_t)fd->vnode->data;
  167. if (fd->vnode->ref_count == 1)
  168. {
  169. ret = rt_device_open(device, fd->flags);
  170. }
  171. if (current == RT_NULL) //kernel mode not lwp
  172. {
  173. return ret;
  174. }
  175. if (!noctty &&
  176. current->leader &&
  177. !current->tty &&
  178. tty->session == -1)
  179. {
  180. current->tty = tty;
  181. current->tty_old_pgrp = 0;
  182. tty->session = current->session;
  183. tty->pgrp = current->__pgrp;
  184. tty->foreground = current;
  185. }
  186. return ret;
  187. }
  188. static int tty_close(struct dfs_file *fd)
  189. {
  190. int ret = 0;
  191. struct tty_struct *tty = RT_NULL;
  192. struct tty_ldisc *ld = RT_NULL;
  193. tty = (struct tty_struct *)fd->vnode->data;
  194. RT_ASSERT(tty != RT_NULL);
  195. ld = tty->ldisc;
  196. if (ld->ops->close)
  197. {
  198. //ld->ops->close(tty);
  199. }
  200. if (fd->vnode->ref_count == 1)
  201. {
  202. ret = rt_device_close((rt_device_t)tty);
  203. }
  204. return ret;
  205. }
  206. static int tiocsctty(struct tty_struct *tty, int arg)
  207. {
  208. if (current->leader &&
  209. (current->session == tty->session))
  210. {
  211. return 0;
  212. }
  213. /*
  214. * The process must be a session leader and
  215. * not have a controlling tty already.
  216. */
  217. if (!current->leader || current->tty)
  218. {
  219. return -EPERM;
  220. }
  221. if (tty->session > 0)
  222. {
  223. LOG_E("this tty have control process\n");
  224. }
  225. current->tty = tty;
  226. current->tty_old_pgrp = 0;
  227. tty->session = current->session;
  228. tty->pgrp = current->__pgrp;
  229. tty->foreground = current;
  230. if (tty->type == TTY_DRIVER_TYPE_PTY)
  231. {
  232. tty->other_struct->foreground = current;
  233. }
  234. return 0;
  235. }
  236. static int tiocswinsz(struct tty_struct *tty, struct winsize *p_winsize)
  237. {
  238. rt_kprintf("\x1b[8;%d;%dt", p_winsize->ws_col, p_winsize->ws_row);
  239. return 0;
  240. }
  241. static int tiocgwinsz(struct tty_struct *tty, struct winsize *p_winsize)
  242. {
  243. if(rt_thread_self() != rt_thread_find(FINSH_THREAD_NAME))
  244. {
  245. /* only can be used in tshell thread; otherwise, return default size */
  246. p_winsize->ws_col = 80;
  247. p_winsize->ws_row = 24;
  248. }
  249. else
  250. {
  251. #define _TIO_BUFLEN 20
  252. char _tio_buf[_TIO_BUFLEN];
  253. unsigned char cnt1, cnt2, cnt3, i;
  254. char row_s[4], col_s[4];
  255. char *p;
  256. rt_memset(_tio_buf, 0, _TIO_BUFLEN);
  257. /* send the command to terminal for getting the window size of the terminal */
  258. rt_kprintf("\033[18t");
  259. /* waiting for the response from the terminal */
  260. i = 0;
  261. while(i < _TIO_BUFLEN)
  262. {
  263. _tio_buf[i] = finsh_getchar();
  264. if(_tio_buf[i] != 't')
  265. {
  266. i ++;
  267. }
  268. else
  269. {
  270. break;
  271. }
  272. }
  273. if(i == _TIO_BUFLEN)
  274. {
  275. /* buffer overloaded, and return default size */
  276. p_winsize->ws_col = 80;
  277. p_winsize->ws_row = 24;
  278. return 0;
  279. }
  280. /* interpreting data eg: "\033[8;1;15t" which means row is 1 and col is 15 (unit: size of ONE character) */
  281. rt_memset(row_s,0,4);
  282. rt_memset(col_s,0,4);
  283. cnt1 = 0;
  284. while(cnt1 < _TIO_BUFLEN && _tio_buf[cnt1] != ';')
  285. {
  286. cnt1++;
  287. }
  288. cnt2 = ++cnt1;
  289. while(cnt2 < _TIO_BUFLEN && _tio_buf[cnt2] != ';')
  290. {
  291. cnt2++;
  292. }
  293. p = row_s;
  294. while(cnt1 < cnt2)
  295. {
  296. *p++ = _tio_buf[cnt1++];
  297. }
  298. p = col_s;
  299. cnt2++;
  300. cnt3 = rt_strlen(_tio_buf) - 1;
  301. while(cnt2 < cnt3)
  302. {
  303. *p++ = _tio_buf[cnt2++];
  304. }
  305. /* load the window size date */
  306. p_winsize->ws_col = atoi(col_s);
  307. p_winsize->ws_row = atoi(row_s);
  308. #undef _TIO_BUFLEN
  309. }
  310. p_winsize->ws_xpixel = 0;/* unused */
  311. p_winsize->ws_ypixel = 0;/* unused */
  312. return 0;
  313. }
  314. static int tty_ioctl(struct dfs_file *fd, int cmd, void *args)
  315. {
  316. int ret = 0;
  317. void *p = (void *)args;
  318. struct tty_struct *tty = RT_NULL;
  319. struct tty_struct *real_tty = RT_NULL;
  320. struct tty_ldisc *ld = RT_NULL;
  321. tty = (struct tty_struct *)fd->vnode->data;
  322. RT_ASSERT(tty != RT_NULL);
  323. if (tty->type == TTY_DRIVER_TYPE_PTY && tty->subtype == PTY_TYPE_MASTER)
  324. {
  325. real_tty = tty->other_struct;
  326. }
  327. else
  328. {
  329. real_tty = tty;
  330. }
  331. switch (cmd)
  332. {
  333. case TIOCSCTTY:
  334. return tiocsctty(real_tty, 1);
  335. case TIOCGWINSZ:
  336. return tiocgwinsz(real_tty, p);
  337. case TIOCSWINSZ:
  338. return tiocswinsz(real_tty, p);
  339. }
  340. ld = tty->ldisc;
  341. if (ld->ops->ioctl)
  342. {
  343. ret = ld->ops->ioctl(fd, cmd, args);
  344. }
  345. return ret;
  346. }
  347. #ifdef RT_USING_DFS_V2
  348. static ssize_t tty_read(struct dfs_file *fd, void *buf, size_t count, off_t *pos)
  349. #else
  350. static ssize_t tty_read(struct dfs_file *fd, void *buf, size_t count)
  351. #endif
  352. {
  353. int ret = 0;
  354. struct tty_struct *tty = RT_NULL;
  355. struct tty_ldisc *ld = RT_NULL;
  356. tty = (struct tty_struct *)fd->vnode->data;
  357. RT_ASSERT(tty != RT_NULL);
  358. ld = tty->ldisc;
  359. if (ld && ld->ops->read)
  360. {
  361. ret = ld->ops->read(fd, buf, count);
  362. }
  363. return ret;
  364. }
  365. #ifdef RT_USING_DFS_V2
  366. static ssize_t tty_write(struct dfs_file *fd, const void *buf, size_t count, off_t *pos)
  367. #else
  368. static ssize_t tty_write(struct dfs_file *fd, const void *buf, size_t count )
  369. #endif
  370. {
  371. int ret = 0;
  372. struct tty_struct *tty = RT_NULL;
  373. struct tty_ldisc *ld = RT_NULL;
  374. tty = (struct tty_struct *)fd->vnode->data;
  375. RT_ASSERT(tty != RT_NULL);
  376. ld = tty->ldisc;
  377. if (ld && ld->ops->write)
  378. {
  379. ret = ld->ops->write(fd, buf, count);
  380. }
  381. return ret;
  382. }
  383. static int tty_poll(struct dfs_file *fd, struct rt_pollreq *req)
  384. {
  385. int ret = 0;
  386. struct tty_struct *tty = RT_NULL;
  387. struct tty_ldisc *ld = RT_NULL;
  388. tty = (struct tty_struct *)fd->vnode->data;
  389. RT_ASSERT(tty != RT_NULL);
  390. ld = tty->ldisc;
  391. if (ld->ops->poll)
  392. {
  393. ret = ld->ops->poll(fd, req);
  394. }
  395. return ret;
  396. }
  397. static const struct dfs_file_ops tty_fops =
  398. {
  399. .open = tty_open,
  400. .close = tty_close,
  401. .ioctl = tty_ioctl,
  402. .read = tty_read,
  403. .write = tty_write,
  404. .poll = tty_poll,
  405. };
  406. const struct dfs_file_ops *tty_get_fops(void)
  407. {
  408. return &tty_fops;
  409. }
  410. int tty_init(struct tty_struct *tty, int type, int subtype, struct rt_device *iodev)
  411. {
  412. if (tty)
  413. {
  414. struct tty_node *node = NULL;
  415. node = rt_calloc(1, sizeof(struct tty_node));
  416. if (node)
  417. {
  418. tty->type = type;
  419. tty->subtype = subtype;
  420. tty->io_dev = iodev;
  421. tty->head = node;
  422. tty_initstack(tty->head);
  423. tty->pgrp = -1;
  424. tty->session = -1;
  425. tty->foreground = RT_NULL;
  426. rt_mutex_init(&tty->lock, "ttyLock", RT_IPC_FLAG_PRIO);
  427. rt_wqueue_init(&tty->wait_queue);
  428. tty_ldisc_init(tty);
  429. tty->init_termios = tty_std_termios;
  430. tty->init_flag = TTY_INIT_FLAG_REGED;
  431. }
  432. }
  433. return 0;
  434. }